stun_port.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef P2P_BASE_STUN_PORT_H_
  11. #define P2P_BASE_STUN_PORT_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include "absl/memory/memory.h"
  16. #include "p2p/base/port.h"
  17. #include "p2p/base/stun_request.h"
  18. #include "rtc_base/async_packet_socket.h"
  19. // TODO(mallinath) - Rename stunport.cc|h to udpport.cc|h.
  20. namespace cricket {
  21. // Lifetime chosen for STUN ports on low-cost networks.
  22. static const int INFINITE_LIFETIME = -1;
  23. // Lifetime for STUN ports on high-cost networks: 2 minutes
  24. static const int HIGH_COST_PORT_KEEPALIVE_LIFETIME = 2 * 60 * 1000;
  25. // Communicates using the address on the outside of a NAT.
  26. class UDPPort : public Port {
  27. public:
  28. static std::unique_ptr<UDPPort> Create(
  29. rtc::Thread* thread,
  30. rtc::PacketSocketFactory* factory,
  31. rtc::Network* network,
  32. rtc::AsyncPacketSocket* socket,
  33. const std::string& username,
  34. const std::string& password,
  35. const std::string& origin,
  36. bool emit_local_for_anyaddress,
  37. absl::optional<int> stun_keepalive_interval) {
  38. // Using `new` to access a non-public constructor.
  39. auto port = absl::WrapUnique(new UDPPort(thread, factory, network, socket,
  40. username, password, origin,
  41. emit_local_for_anyaddress));
  42. port->set_stun_keepalive_delay(stun_keepalive_interval);
  43. if (!port->Init()) {
  44. return nullptr;
  45. }
  46. return port;
  47. }
  48. static std::unique_ptr<UDPPort> Create(
  49. rtc::Thread* thread,
  50. rtc::PacketSocketFactory* factory,
  51. rtc::Network* network,
  52. uint16_t min_port,
  53. uint16_t max_port,
  54. const std::string& username,
  55. const std::string& password,
  56. const std::string& origin,
  57. bool emit_local_for_anyaddress,
  58. absl::optional<int> stun_keepalive_interval) {
  59. // Using `new` to access a non-public constructor.
  60. auto port = absl::WrapUnique(
  61. new UDPPort(thread, factory, network, min_port, max_port, username,
  62. password, origin, emit_local_for_anyaddress));
  63. port->set_stun_keepalive_delay(stun_keepalive_interval);
  64. if (!port->Init()) {
  65. return nullptr;
  66. }
  67. return port;
  68. }
  69. ~UDPPort() override;
  70. rtc::SocketAddress GetLocalAddress() const {
  71. return socket_->GetLocalAddress();
  72. }
  73. const ServerAddresses& server_addresses() const { return server_addresses_; }
  74. void set_server_addresses(const ServerAddresses& addresses) {
  75. server_addresses_ = addresses;
  76. }
  77. void PrepareAddress() override;
  78. Connection* CreateConnection(const Candidate& address,
  79. CandidateOrigin origin) override;
  80. int SetOption(rtc::Socket::Option opt, int value) override;
  81. int GetOption(rtc::Socket::Option opt, int* value) override;
  82. int GetError() override;
  83. bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket,
  84. const char* data,
  85. size_t size,
  86. const rtc::SocketAddress& remote_addr,
  87. int64_t packet_time_us) override;
  88. bool SupportsProtocol(const std::string& protocol) const override;
  89. ProtocolType GetProtocol() const override;
  90. void GetStunStats(absl::optional<StunStats>* stats) override;
  91. void set_stun_keepalive_delay(const absl::optional<int>& delay);
  92. int stun_keepalive_delay() const { return stun_keepalive_delay_; }
  93. // Visible for testing.
  94. int stun_keepalive_lifetime() const { return stun_keepalive_lifetime_; }
  95. void set_stun_keepalive_lifetime(int lifetime) {
  96. stun_keepalive_lifetime_ = lifetime;
  97. }
  98. // Returns true if there is a pending request with type |msg_type|.
  99. bool HasPendingRequest(int msg_type) {
  100. return requests_.HasRequest(msg_type);
  101. }
  102. protected:
  103. UDPPort(rtc::Thread* thread,
  104. rtc::PacketSocketFactory* factory,
  105. rtc::Network* network,
  106. uint16_t min_port,
  107. uint16_t max_port,
  108. const std::string& username,
  109. const std::string& password,
  110. const std::string& origin,
  111. bool emit_local_for_anyaddress);
  112. UDPPort(rtc::Thread* thread,
  113. rtc::PacketSocketFactory* factory,
  114. rtc::Network* network,
  115. rtc::AsyncPacketSocket* socket,
  116. const std::string& username,
  117. const std::string& password,
  118. const std::string& origin,
  119. bool emit_local_for_anyaddress);
  120. bool Init();
  121. int SendTo(const void* data,
  122. size_t size,
  123. const rtc::SocketAddress& addr,
  124. const rtc::PacketOptions& options,
  125. bool payload) override;
  126. void UpdateNetworkCost() override;
  127. rtc::DiffServCodePoint StunDscpValue() const override;
  128. void OnLocalAddressReady(rtc::AsyncPacketSocket* socket,
  129. const rtc::SocketAddress& address);
  130. void PostAddAddress(bool is_final) override;
  131. void OnReadPacket(rtc::AsyncPacketSocket* socket,
  132. const char* data,
  133. size_t size,
  134. const rtc::SocketAddress& remote_addr,
  135. const int64_t& packet_time_us);
  136. void OnSentPacket(rtc::AsyncPacketSocket* socket,
  137. const rtc::SentPacket& sent_packet) override;
  138. void OnReadyToSend(rtc::AsyncPacketSocket* socket);
  139. // This method will send STUN binding request if STUN server address is set.
  140. void MaybePrepareStunCandidate();
  141. void SendStunBindingRequests();
  142. // Helper function which will set |addr|'s IP to the default local address if
  143. // |addr| is the "any" address and |emit_local_for_anyaddress_| is true. When
  144. // returning false, it indicates that the operation has failed and the
  145. // address shouldn't be used by any candidate.
  146. bool MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const;
  147. private:
  148. // A helper class which can be called repeatedly to resolve multiple
  149. // addresses, as opposed to rtc::AsyncResolverInterface, which can only
  150. // resolve one address per instance.
  151. class AddressResolver : public sigslot::has_slots<> {
  152. public:
  153. explicit AddressResolver(rtc::PacketSocketFactory* factory);
  154. ~AddressResolver() override;
  155. void Resolve(const rtc::SocketAddress& address);
  156. bool GetResolvedAddress(const rtc::SocketAddress& input,
  157. int family,
  158. rtc::SocketAddress* output) const;
  159. // The signal is sent when resolving the specified address is finished. The
  160. // first argument is the input address, the second argument is the error
  161. // or 0 if it succeeded.
  162. sigslot::signal2<const rtc::SocketAddress&, int> SignalDone;
  163. private:
  164. typedef std::map<rtc::SocketAddress, rtc::AsyncResolverInterface*>
  165. ResolverMap;
  166. void OnResolveResult(rtc::AsyncResolverInterface* resolver);
  167. rtc::PacketSocketFactory* socket_factory_;
  168. ResolverMap resolvers_;
  169. };
  170. // DNS resolution of the STUN server.
  171. void ResolveStunAddress(const rtc::SocketAddress& stun_addr);
  172. void OnResolveResult(const rtc::SocketAddress& input, int error);
  173. void SendStunBindingRequest(const rtc::SocketAddress& stun_addr);
  174. // Below methods handles binding request responses.
  175. void OnStunBindingRequestSucceeded(
  176. int rtt_ms,
  177. const rtc::SocketAddress& stun_server_addr,
  178. const rtc::SocketAddress& stun_reflected_addr);
  179. void OnStunBindingOrResolveRequestFailed(
  180. const rtc::SocketAddress& stun_server_addr,
  181. int error_code,
  182. const std::string& reason);
  183. // Sends STUN requests to the server.
  184. void OnSendPacket(const void* data, size_t size, StunRequest* req);
  185. // TODO(mallinaht) - Move this up to cricket::Port when SignalAddressReady is
  186. // changed to SignalPortReady.
  187. void MaybeSetPortCompleteOrError();
  188. bool HasCandidateWithAddress(const rtc::SocketAddress& addr) const;
  189. // If this is a low-cost network, it will keep on sending STUN binding
  190. // requests indefinitely to keep the NAT binding alive. Otherwise, stop
  191. // sending STUN binding requests after HIGH_COST_PORT_KEEPALIVE_LIFETIME.
  192. int GetStunKeepaliveLifetime() {
  193. return (network_cost() >= rtc::kNetworkCostHigh)
  194. ? HIGH_COST_PORT_KEEPALIVE_LIFETIME
  195. : INFINITE_LIFETIME;
  196. }
  197. ServerAddresses server_addresses_;
  198. ServerAddresses bind_request_succeeded_servers_;
  199. ServerAddresses bind_request_failed_servers_;
  200. StunRequestManager requests_;
  201. rtc::AsyncPacketSocket* socket_;
  202. int error_;
  203. int send_error_count_ = 0;
  204. std::unique_ptr<AddressResolver> resolver_;
  205. bool ready_;
  206. int stun_keepalive_delay_;
  207. int stun_keepalive_lifetime_ = INFINITE_LIFETIME;
  208. rtc::DiffServCodePoint dscp_;
  209. StunStats stats_;
  210. // This is true by default and false when
  211. // PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE is specified.
  212. bool emit_local_for_anyaddress_;
  213. friend class StunBindingRequest;
  214. };
  215. class StunPort : public UDPPort {
  216. public:
  217. static std::unique_ptr<StunPort> Create(
  218. rtc::Thread* thread,
  219. rtc::PacketSocketFactory* factory,
  220. rtc::Network* network,
  221. uint16_t min_port,
  222. uint16_t max_port,
  223. const std::string& username,
  224. const std::string& password,
  225. const ServerAddresses& servers,
  226. const std::string& origin,
  227. absl::optional<int> stun_keepalive_interval);
  228. void PrepareAddress() override;
  229. protected:
  230. StunPort(rtc::Thread* thread,
  231. rtc::PacketSocketFactory* factory,
  232. rtc::Network* network,
  233. uint16_t min_port,
  234. uint16_t max_port,
  235. const std::string& username,
  236. const std::string& password,
  237. const ServerAddresses& servers,
  238. const std::string& origin);
  239. };
  240. } // namespace cricket
  241. #endif // P2P_BASE_STUN_PORT_H_