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.

173 lines
5.9 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_BASE_PORTALLOCATOR_H_
  28. #define TALK_P2P_BASE_PORTALLOCATOR_H_
  29. #include <string>
  30. #include <vector>
  31. #include "talk/base/helpers.h"
  32. #include "talk/base/proxyinfo.h"
  33. #include "talk/base/sigslot.h"
  34. #include "talk/p2p/base/portinterface.h"
  35. namespace cricket {
  36. // PortAllocator is responsible for allocating Port types for a given
  37. // P2PSocket. It also handles port freeing.
  38. //
  39. // Clients can override this class to control port allocation, including
  40. // what kinds of ports are allocated.
  41. const uint32 PORTALLOCATOR_DISABLE_UDP = 0x01;
  42. const uint32 PORTALLOCATOR_DISABLE_STUN = 0x02;
  43. const uint32 PORTALLOCATOR_DISABLE_RELAY = 0x04;
  44. const uint32 PORTALLOCATOR_DISABLE_TCP = 0x08;
  45. const uint32 PORTALLOCATOR_ENABLE_SHAKER = 0x10;
  46. const uint32 PORTALLOCATOR_ENABLE_BUNDLE = 0x20;
  47. const uint32 PORTALLOCATOR_ENABLE_IPV6 = 0x40;
  48. const uint32 PORTALLOCATOR_ENABLE_SHARED_UFRAG = 0x80;
  49. const uint32 PORTALLOCATOR_ENABLE_SHARED_SOCKET = 0x100;
  50. const uint32 PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE = 0x200;
  51. const uint32 PORTALLOCATOR_USE_LARGE_SOCKET_SEND_BUFFERS = 0x400;
  52. const uint32 kDefaultPortAllocatorFlags = 0;
  53. class PortAllocatorSessionMuxer;
  54. class PortAllocatorSession : public sigslot::has_slots<> {
  55. public:
  56. // Content name passed in mostly for logging and debugging.
  57. // TODO(mallinath) - Change username and password to ice_ufrag and ice_pwd.
  58. PortAllocatorSession(const std::string& content_name,
  59. int component,
  60. const std::string& username,
  61. const std::string& password,
  62. uint32 flags);
  63. // Subclasses should clean up any ports created.
  64. virtual ~PortAllocatorSession() {}
  65. uint32 flags() const { return flags_; }
  66. void set_flags(uint32 flags) { flags_ = flags; }
  67. std::string content_name() const { return content_name_; }
  68. int component() const { return component_; }
  69. // Prepares an initial set of ports to try.
  70. virtual void GetInitialPorts() = 0;
  71. // Starts and stops the flow of additional ports to try.
  72. virtual void StartGetAllPorts() = 0;
  73. virtual void StopGetAllPorts() = 0;
  74. virtual bool IsGettingAllPorts() = 0;
  75. sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
  76. sigslot::signal2<PortAllocatorSession*,
  77. const std::vector<Candidate>&> SignalCandidatesReady;
  78. sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
  79. virtual uint32 generation() { return generation_; }
  80. virtual void set_generation(uint32 generation) { generation_ = generation; }
  81. sigslot::signal1<PortAllocatorSession*> SignalDestroyed;
  82. protected:
  83. const std::string& username() const { return username_; }
  84. const std::string& password() const { return password_; }
  85. std::string content_name_;
  86. int component_;
  87. private:
  88. uint32 flags_;
  89. uint32 generation_;
  90. std::string username_;
  91. std::string password_;
  92. };
  93. class PortAllocator : public sigslot::has_slots<> {
  94. public:
  95. PortAllocator() :
  96. flags_(kDefaultPortAllocatorFlags),
  97. min_port_(0),
  98. max_port_(0) {
  99. }
  100. virtual ~PortAllocator();
  101. PortAllocatorSession* CreateSession(
  102. const std::string& sid,
  103. const std::string& content_name,
  104. int component,
  105. const std::string& ice_ufrag,
  106. const std::string& ice_pwd);
  107. PortAllocatorSessionMuxer* GetSessionMuxer(const std::string& sid) const;
  108. void OnSessionMuxerDestroyed(PortAllocatorSessionMuxer* session);
  109. uint32 flags() const { return flags_; }
  110. void set_flags(uint32 flags) { flags_ = flags; }
  111. const std::string& user_agent() const { return agent_; }
  112. const talk_base::ProxyInfo& proxy() const { return proxy_; }
  113. void set_proxy(const std::string& agent, const talk_base::ProxyInfo& proxy) {
  114. agent_ = agent;
  115. proxy_ = proxy;
  116. }
  117. // Gets/Sets the port range to use when choosing client ports.
  118. int min_port() const { return min_port_; }
  119. int max_port() const { return max_port_; }
  120. bool SetPortRange(int min_port, int max_port) {
  121. if (min_port > max_port) {
  122. return false;
  123. }
  124. min_port_ = min_port;
  125. max_port_ = max_port;
  126. return true;
  127. }
  128. protected:
  129. virtual PortAllocatorSession* CreateSessionInternal(
  130. const std::string& content_name,
  131. int component,
  132. const std::string& ice_ufrag,
  133. const std::string& ice_pwd) = 0;
  134. typedef std::map<std::string, PortAllocatorSessionMuxer*> SessionMuxerMap;
  135. uint32 flags_;
  136. std::string agent_;
  137. talk_base::ProxyInfo proxy_;
  138. int min_port_;
  139. int max_port_;
  140. SessionMuxerMap muxers_;
  141. };
  142. } // namespace cricket
  143. #endif // TALK_P2P_BASE_PORTALLOCATOR_H_