stun_prober.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2015 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_STUNPROBER_STUN_PROBER_H_
  11. #define P2P_STUNPROBER_STUN_PROBER_H_
  12. #include <set>
  13. #include <string>
  14. #include <vector>
  15. #include "rtc_base/async_invoker.h"
  16. #include "rtc_base/byte_buffer.h"
  17. #include "rtc_base/callback.h"
  18. #include "rtc_base/constructor_magic.h"
  19. #include "rtc_base/ip_address.h"
  20. #include "rtc_base/network.h"
  21. #include "rtc_base/socket_address.h"
  22. #include "rtc_base/system/rtc_export.h"
  23. #include "rtc_base/thread.h"
  24. #include "rtc_base/thread_checker.h"
  25. namespace rtc {
  26. class AsyncPacketSocket;
  27. class PacketSocketFactory;
  28. class Thread;
  29. class NetworkManager;
  30. class AsyncResolverInterface;
  31. } // namespace rtc
  32. namespace stunprober {
  33. class StunProber;
  34. static const int kMaxUdpBufferSize = 1200;
  35. typedef rtc::Callback2<void, StunProber*, int> AsyncCallback;
  36. enum NatType {
  37. NATTYPE_INVALID,
  38. NATTYPE_NONE, // Not behind a NAT.
  39. NATTYPE_UNKNOWN, // Behind a NAT but type can't be determine.
  40. NATTYPE_SYMMETRIC, // Behind a symmetric NAT.
  41. NATTYPE_NON_SYMMETRIC // Behind a non-symmetric NAT.
  42. };
  43. class RTC_EXPORT StunProber : public sigslot::has_slots<> {
  44. public:
  45. enum Status { // Used in UMA_HISTOGRAM_ENUMERATION.
  46. SUCCESS, // Successfully received bytes from the server.
  47. GENERIC_FAILURE, // Generic failure.
  48. RESOLVE_FAILED, // Host resolution failed.
  49. WRITE_FAILED, // Sending a message to the server failed.
  50. READ_FAILED, // Reading the reply from the server failed.
  51. };
  52. class Observer {
  53. public:
  54. virtual ~Observer() = default;
  55. virtual void OnPrepared(StunProber* prober, StunProber::Status status) = 0;
  56. virtual void OnFinished(StunProber* prober, StunProber::Status status) = 0;
  57. };
  58. struct RTC_EXPORT Stats {
  59. Stats();
  60. ~Stats();
  61. // |raw_num_request_sent| is the total number of requests
  62. // sent. |num_request_sent| is the count of requests against a server where
  63. // we see at least one response. |num_request_sent| is designed to protect
  64. // against DNS resolution failure or the STUN server is not responsive
  65. // which could skew the result.
  66. int raw_num_request_sent = 0;
  67. int num_request_sent = 0;
  68. int num_response_received = 0;
  69. NatType nat_type = NATTYPE_INVALID;
  70. int average_rtt_ms = -1;
  71. int success_percent = 0;
  72. int target_request_interval_ns = 0;
  73. int actual_request_interval_ns = 0;
  74. // Also report whether this trial can't be considered truly as shared
  75. // mode. Share mode only makes sense when we have multiple IP resolved and
  76. // successfully probed.
  77. bool shared_socket_mode = false;
  78. std::string host_ip;
  79. // If the srflx_addrs has more than 1 element, the NAT is symmetric.
  80. std::set<std::string> srflx_addrs;
  81. };
  82. StunProber(rtc::PacketSocketFactory* socket_factory,
  83. rtc::Thread* thread,
  84. const rtc::NetworkManager::NetworkList& networks);
  85. ~StunProber() override;
  86. // Begin performing the probe test against the |servers|. If
  87. // |shared_socket_mode| is false, each request will be done with a new socket.
  88. // Otherwise, a unique socket will be used for a single round of requests
  89. // against all resolved IPs. No single socket will be used against a given IP
  90. // more than once. The interval of requests will be as close to the requested
  91. // inter-probe interval |stun_ta_interval_ms| as possible. After sending out
  92. // the last scheduled request, the probe will wait |timeout_ms| for request
  93. // responses and then call |finish_callback|. |requests_per_ip| indicates how
  94. // many requests should be tried for each resolved IP address. In shared mode,
  95. // (the number of sockets to be created) equals to |requests_per_ip|. In
  96. // non-shared mode, (the number of sockets) equals to requests_per_ip * (the
  97. // number of resolved IP addresses). TODO(guoweis): Remove this once
  98. // everything moved to Prepare() and Run().
  99. bool Start(const std::vector<rtc::SocketAddress>& servers,
  100. bool shared_socket_mode,
  101. int stun_ta_interval_ms,
  102. int requests_per_ip,
  103. int timeout_ms,
  104. const AsyncCallback finish_callback);
  105. // TODO(guoweis): The combination of Prepare() and Run() are equivalent to the
  106. // Start() above. Remove Start() once everything is migrated.
  107. bool Prepare(const std::vector<rtc::SocketAddress>& servers,
  108. bool shared_socket_mode,
  109. int stun_ta_interval_ms,
  110. int requests_per_ip,
  111. int timeout_ms,
  112. StunProber::Observer* observer);
  113. // Start to send out the STUN probes.
  114. bool Start(StunProber::Observer* observer);
  115. // Method to retrieve the Stats once |finish_callback| is invoked. Returning
  116. // false when the result is inconclusive, for example, whether it's behind a
  117. // NAT or not.
  118. bool GetStats(Stats* stats) const;
  119. int estimated_execution_time() {
  120. return static_cast<int>(requests_per_ip_ * all_servers_addrs_.size() *
  121. interval_ms_);
  122. }
  123. private:
  124. // A requester tracks the requests and responses from a single socket to many
  125. // STUN servers.
  126. class Requester;
  127. // TODO(guoweis): Remove this once all dependencies move away from
  128. // AsyncCallback.
  129. class ObserverAdapter : public Observer {
  130. public:
  131. ObserverAdapter();
  132. ~ObserverAdapter() override;
  133. void set_callback(AsyncCallback callback) { callback_ = callback; }
  134. void OnPrepared(StunProber* stunprober, Status status) override;
  135. void OnFinished(StunProber* stunprober, Status status) override;
  136. private:
  137. AsyncCallback callback_;
  138. };
  139. bool ResolveServerName(const rtc::SocketAddress& addr);
  140. void OnServerResolved(rtc::AsyncResolverInterface* resolver);
  141. void OnSocketReady(rtc::AsyncPacketSocket* socket,
  142. const rtc::SocketAddress& addr);
  143. void CreateSockets();
  144. bool Done() {
  145. return num_request_sent_ >= requests_per_ip_ * all_servers_addrs_.size();
  146. }
  147. size_t total_socket_required() {
  148. return (shared_socket_mode_ ? 1 : all_servers_addrs_.size()) *
  149. requests_per_ip_;
  150. }
  151. bool should_send_next_request(int64_t now);
  152. int get_wake_up_interval_ms();
  153. bool SendNextRequest();
  154. // Will be invoked in 1ms intervals and schedule the next request from the
  155. // |current_requester_| if the time has passed for another request.
  156. void MaybeScheduleStunRequests();
  157. void ReportOnPrepared(StunProber::Status status);
  158. void ReportOnFinished(StunProber::Status status);
  159. Requester* CreateRequester();
  160. Requester* current_requester_ = nullptr;
  161. // The time when the next request should go out.
  162. int64_t next_request_time_ms_ = 0;
  163. // Total requests sent so far.
  164. uint32_t num_request_sent_ = 0;
  165. bool shared_socket_mode_ = false;
  166. // How many requests should be done against each resolved IP.
  167. uint32_t requests_per_ip_ = 0;
  168. // Milliseconds to pause between each STUN request.
  169. int interval_ms_;
  170. // Timeout period after the last request is sent.
  171. int timeout_ms_;
  172. // STUN server name to be resolved.
  173. std::vector<rtc::SocketAddress> servers_;
  174. // Weak references.
  175. rtc::PacketSocketFactory* socket_factory_;
  176. rtc::Thread* thread_;
  177. // Accumulate all resolved addresses.
  178. std::vector<rtc::SocketAddress> all_servers_addrs_;
  179. // The set of STUN probe sockets and their state.
  180. std::vector<Requester*> requesters_;
  181. rtc::ThreadChecker thread_checker_;
  182. // Temporary storage for created sockets.
  183. std::vector<rtc::AsyncPacketSocket*> sockets_;
  184. // This tracks how many of the sockets are ready.
  185. size_t total_ready_sockets_ = 0;
  186. rtc::AsyncInvoker invoker_;
  187. Observer* observer_ = nullptr;
  188. // TODO(guoweis): Remove this once all dependencies move away from
  189. // AsyncCallback.
  190. ObserverAdapter observer_adapter_;
  191. rtc::NetworkManager::NetworkList networks_;
  192. RTC_DISALLOW_COPY_AND_ASSIGN(StunProber);
  193. };
  194. } // namespace stunprober
  195. #endif // P2P_STUNPROBER_STUN_PROBER_H_