You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
9.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_
  28. #define TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_
  29. #include <string>
  30. #include <vector>
  31. #include "talk/base/messagequeue.h"
  32. #include "talk/base/network.h"
  33. #include "talk/base/scoped_ptr.h"
  34. #include "talk/base/thread.h"
  35. #include "talk/p2p/base/port.h"
  36. #include "talk/p2p/base/portallocator.h"
  37. namespace cricket {
  38. struct RelayCredentials {
  39. RelayCredentials() {}
  40. RelayCredentials(const std::string& username,
  41. const std::string& password)
  42. : username(username),
  43. password(password) {
  44. }
  45. std::string username;
  46. std::string password;
  47. };
  48. typedef std::vector<ProtocolAddress> PortList;
  49. struct RelayServerConfig {
  50. RelayServerConfig(RelayType type) : type(type) {}
  51. RelayType type;
  52. PortList ports;
  53. RelayCredentials credentials;
  54. };
  55. class BasicPortAllocator : public PortAllocator {
  56. public:
  57. BasicPortAllocator(talk_base::NetworkManager* network_manager,
  58. talk_base::PacketSocketFactory* socket_factory);
  59. explicit BasicPortAllocator(talk_base::NetworkManager* network_manager);
  60. BasicPortAllocator(talk_base::NetworkManager* network_manager,
  61. talk_base::PacketSocketFactory* socket_factory,
  62. const talk_base::SocketAddress& stun_server);
  63. BasicPortAllocator(talk_base::NetworkManager* network_manager,
  64. const talk_base::SocketAddress& stun_server,
  65. const talk_base::SocketAddress& relay_server_udp,
  66. const talk_base::SocketAddress& relay_server_tcp,
  67. const talk_base::SocketAddress& relay_server_ssl);
  68. virtual ~BasicPortAllocator();
  69. talk_base::NetworkManager* network_manager() { return network_manager_; }
  70. // If socket_factory() is set to NULL each PortAllocatorSession
  71. // creates its own socket factory.
  72. talk_base::PacketSocketFactory* socket_factory() { return socket_factory_; }
  73. const talk_base::SocketAddress& stun_address() const {
  74. return stun_address_;
  75. }
  76. const std::vector<RelayServerConfig>& relays() const {
  77. return relays_;
  78. }
  79. virtual void AddRelay(const RelayServerConfig& relay) {
  80. relays_.push_back(relay);
  81. }
  82. // Returns the best (highest priority) phase that has produced a port that
  83. // produced a writable connection. If no writable connections have been
  84. // produced, this returns -1.
  85. int best_writable_phase() const;
  86. virtual PortAllocatorSession* CreateSessionInternal(
  87. const std::string& content_name,
  88. int component,
  89. const std::string& ice_ufrag,
  90. const std::string& ice_pwd);
  91. // Called whenever a connection becomes writable with the argument being the
  92. // phase that the corresponding port was created in.
  93. void AddWritablePhase(int phase);
  94. bool allow_tcp_listen() const {
  95. return allow_tcp_listen_;
  96. }
  97. void set_allow_tcp_listen(bool allow_tcp_listen) {
  98. allow_tcp_listen_ = allow_tcp_listen;
  99. }
  100. private:
  101. void Construct();
  102. talk_base::NetworkManager* network_manager_;
  103. talk_base::PacketSocketFactory* socket_factory_;
  104. const talk_base::SocketAddress stun_address_;
  105. std::vector<RelayServerConfig> relays_;
  106. int best_writable_phase_;
  107. bool allow_tcp_listen_;
  108. };
  109. struct PortConfiguration;
  110. class AllocationSequence;
  111. class BasicPortAllocatorSession : public PortAllocatorSession,
  112. public talk_base::MessageHandler {
  113. public:
  114. BasicPortAllocatorSession(BasicPortAllocator* allocator,
  115. const std::string& content_name,
  116. int component,
  117. const std::string& ice_ufrag,
  118. const std::string& ice_pwd);
  119. ~BasicPortAllocatorSession();
  120. virtual BasicPortAllocator* allocator() { return allocator_; }
  121. talk_base::Thread* network_thread() { return network_thread_; }
  122. talk_base::PacketSocketFactory* socket_factory() { return socket_factory_; }
  123. virtual void GetInitialPorts();
  124. virtual void StartGetAllPorts();
  125. virtual void StopGetAllPorts();
  126. virtual bool IsGettingAllPorts() { return running_; }
  127. protected:
  128. // Starts the process of getting the port configurations.
  129. virtual void GetPortConfigurations();
  130. // Adds a port configuration that is now ready. Once we have one for each
  131. // network (or a timeout occurs), we will start allocating ports.
  132. virtual void ConfigReady(PortConfiguration* config);
  133. // MessageHandler. Can be overriden if message IDs do not conflict.
  134. virtual void OnMessage(talk_base::Message *message);
  135. private:
  136. class PortData {
  137. public:
  138. PortData() : port_(NULL), sequence_(NULL), state_(STATE_INIT) {}
  139. PortData(Port* port, AllocationSequence* seq)
  140. : port_(port), sequence_(seq), state_(STATE_INIT) {
  141. }
  142. Port* port() { return port_; }
  143. AllocationSequence* sequence() { return sequence_; }
  144. bool ready() const { return state_ == STATE_READY; }
  145. bool complete() const {
  146. // Returns true if candidate allocation has completed one way or another.
  147. return ((state_ == STATE_READY) || (state_ == STATE_ERROR));
  148. }
  149. void set_ready() { ASSERT(state_ == STATE_INIT); state_ = STATE_READY; }
  150. void set_error() { ASSERT(state_ == STATE_INIT); state_ = STATE_ERROR; }
  151. private:
  152. enum State {
  153. STATE_INIT, // No candidates allocated yet.
  154. STATE_READY, // All candidates allocated and ready for process.
  155. STATE_ERROR // Error in gathering candidates.
  156. };
  157. Port* port_;
  158. AllocationSequence* sequence_;
  159. State state_;
  160. };
  161. void OnConfigReady(PortConfiguration* config);
  162. void OnConfigStop();
  163. void AllocatePorts();
  164. void OnAllocate();
  165. void DoAllocate();
  166. void OnNetworksChanged();
  167. void OnAllocationSequenceObjectsCreated();
  168. void DisableEquivalentPhases(talk_base::Network* network,
  169. PortConfiguration* config, uint32* flags);
  170. void AddAllocatedPort(Port* port, AllocationSequence* seq,
  171. bool prepare_address = true);
  172. void OnCandidateReady(Port* port, const Candidate& c);
  173. void OnPortReady(Port* port);
  174. void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
  175. void OnPortDestroyed(PortInterface* port);
  176. void OnAddressError(Port* port);
  177. void OnConnectionCreated(Port* port, Connection* conn);
  178. void OnConnectionStateChange(Connection* conn);
  179. void OnShake();
  180. void MaybeSignalCandidatesAllocationDone();
  181. void OnPortAllocationComplete(AllocationSequence* seq);
  182. PortData* FindPort(Port* port);
  183. BasicPortAllocator* allocator_;
  184. talk_base::Thread* network_thread_;
  185. talk_base::scoped_ptr<talk_base::PacketSocketFactory> owned_socket_factory_;
  186. talk_base::PacketSocketFactory* socket_factory_;
  187. bool configuration_done_;
  188. bool allocation_started_;
  189. bool network_manager_started_;
  190. bool running_; // set when StartGetAllPorts is called
  191. bool allocation_sequences_created_;
  192. std::vector<PortConfiguration*> configs_;
  193. std::vector<AllocationSequence*> sequences_;
  194. std::vector<PortData> ports_;
  195. friend class AllocationSequence;
  196. };
  197. // Records configuration information useful in creating ports.
  198. struct PortConfiguration : public talk_base::MessageData {
  199. talk_base::SocketAddress stun_address;
  200. std::string username;
  201. std::string password;
  202. typedef std::vector<RelayServerConfig> RelayList;
  203. RelayList relays;
  204. PortConfiguration(const talk_base::SocketAddress& stun_address,
  205. const std::string& username,
  206. const std::string& password);
  207. // Adds another relay server, with the given ports and modifier, to the list.
  208. void AddRelay(const RelayServerConfig& config);
  209. // Determines whether the given relay server supports the given protocol.
  210. static bool SupportsProtocol(const RelayServerConfig& relay,
  211. ProtocolType type);
  212. };
  213. } // namespace cricket
  214. #endif // TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_