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.

517 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. #include <string>
  3. #include "talk/p2p/client/connectivitychecker.h"
  4. #include "talk/base/asynchttprequest.h"
  5. #include "talk/base/autodetectproxy.h"
  6. #include "talk/base/basicpacketsocketfactory.h"
  7. #include "talk/base/helpers.h"
  8. #include "talk/base/httpcommon.h"
  9. #include "talk/base/httpcommon-inl.h"
  10. #include "talk/base/logging.h"
  11. #include "talk/base/proxydetect.h"
  12. #include "talk/base/thread.h"
  13. #include "talk/p2p/base/candidate.h"
  14. #include "talk/p2p/base/constants.h"
  15. #include "talk/p2p/base/common.h"
  16. #include "talk/p2p/base/port.h"
  17. #include "talk/p2p/base/relayport.h"
  18. #include "talk/p2p/base/stunport.h"
  19. namespace cricket {
  20. static const char kSessionTypeVideo[] =
  21. "http://www.google.com/session/video";
  22. static const char kSessionNameRtp[] = "rtp";
  23. static const char kDefaultStunHostname[] = "stun.l.google.com";
  24. static const int kDefaultStunPort = 19302;
  25. // Default maximum time in milliseconds we will wait for connections.
  26. static const uint32 kDefaultTimeoutMs = 3000;
  27. enum {
  28. MSG_START = 1,
  29. MSG_STOP = 2,
  30. MSG_TIMEOUT = 3,
  31. MSG_SIGNAL_RESULTS = 4
  32. };
  33. class TestHttpPortAllocator : public HttpPortAllocator {
  34. public:
  35. TestHttpPortAllocator(talk_base::NetworkManager* network_manager,
  36. const std::string& user_agent,
  37. const std::string& relay_token) :
  38. HttpPortAllocator(network_manager, user_agent) {
  39. SetRelayToken(relay_token);
  40. }
  41. PortAllocatorSession* CreateSessionInternal(
  42. const std::string& content_name,
  43. int component,
  44. const std::string& ice_ufrag,
  45. const std::string& ice_pwd) {
  46. return new TestHttpPortAllocatorSession(this, content_name, component,
  47. ice_ufrag, ice_pwd,
  48. stun_hosts(), relay_hosts(),
  49. relay_token(), user_agent());
  50. }
  51. };
  52. void TestHttpPortAllocatorSession::ConfigReady(PortConfiguration* config) {
  53. SignalConfigReady(username(), password(), config, proxy_);
  54. }
  55. void TestHttpPortAllocatorSession::OnRequestDone(
  56. talk_base::SignalThread* data) {
  57. talk_base::AsyncHttpRequest* request =
  58. static_cast<talk_base::AsyncHttpRequest*>(data);
  59. // Tell the checker that the request is complete.
  60. SignalRequestDone(request);
  61. // Pass on the response to super class.
  62. HttpPortAllocatorSession::OnRequestDone(data);
  63. }
  64. ConnectivityChecker::ConnectivityChecker(
  65. talk_base::Thread* worker,
  66. const std::string& jid,
  67. const std::string& session_id,
  68. const std::string& user_agent,
  69. const std::string& relay_token,
  70. const std::string& connection)
  71. : worker_(worker),
  72. jid_(jid),
  73. session_id_(session_id),
  74. user_agent_(user_agent),
  75. relay_token_(relay_token),
  76. connection_(connection),
  77. proxy_detect_(NULL),
  78. timeout_ms_(kDefaultTimeoutMs),
  79. stun_address_(kDefaultStunHostname, kDefaultStunPort),
  80. started_(false) {
  81. }
  82. ConnectivityChecker::~ConnectivityChecker() {
  83. if (started_) {
  84. // We try to clear the TIMEOUT below. But worker may still handle it and
  85. // cause SignalCheckDone to happen on main-thread. So we finally clear any
  86. // pending SIGNAL_RESULTS.
  87. worker_->Clear(this, MSG_TIMEOUT);
  88. worker_->Send(this, MSG_STOP);
  89. nics_.clear();
  90. main_->Clear(this, MSG_SIGNAL_RESULTS);
  91. }
  92. }
  93. bool ConnectivityChecker::Initialize() {
  94. network_manager_.reset(CreateNetworkManager());
  95. socket_factory_.reset(CreateSocketFactory(worker_));
  96. port_allocator_.reset(CreatePortAllocator(network_manager_.get(),
  97. user_agent_, relay_token_));
  98. uint32 new_allocator_flags = port_allocator_->flags();
  99. new_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG;
  100. port_allocator_->set_flags(new_allocator_flags);
  101. return true;
  102. }
  103. void ConnectivityChecker::Start() {
  104. main_ = talk_base::Thread::Current();
  105. worker_->Post(this, MSG_START);
  106. started_ = true;
  107. }
  108. void ConnectivityChecker::CleanUp() {
  109. ASSERT(worker_ == talk_base::Thread::Current());
  110. if (proxy_detect_) {
  111. proxy_detect_->Release();
  112. proxy_detect_ = NULL;
  113. }
  114. for (uint32 i = 0; i < sessions_.size(); ++i) {
  115. delete sessions_[i];
  116. }
  117. sessions_.clear();
  118. for (uint32 i = 0; i < ports_.size(); ++i) {
  119. delete ports_[i];
  120. }
  121. ports_.clear();
  122. }
  123. bool ConnectivityChecker::AddNic(const talk_base::IPAddress& ip,
  124. const talk_base::SocketAddress& proxy_addr) {
  125. NicMap::iterator i = nics_.find(NicId(ip, proxy_addr));
  126. if (i != nics_.end()) {
  127. // Already have it.
  128. return false;
  129. }
  130. uint32 now = talk_base::Time();
  131. NicInfo info;
  132. info.ip = ip;
  133. info.proxy_info = GetProxyInfo();
  134. info.stun.start_time_ms = now;
  135. nics_.insert(std::pair<NicId, NicInfo>(NicId(ip, proxy_addr), info));
  136. return true;
  137. }
  138. void ConnectivityChecker::SetProxyInfo(const talk_base::ProxyInfo& proxy_info) {
  139. port_allocator_->set_proxy(user_agent_, proxy_info);
  140. AllocatePorts();
  141. }
  142. talk_base::ProxyInfo ConnectivityChecker::GetProxyInfo() const {
  143. talk_base::ProxyInfo proxy_info;
  144. if (proxy_detect_) {
  145. proxy_info = proxy_detect_->proxy();
  146. }
  147. return proxy_info;
  148. }
  149. void ConnectivityChecker::CheckNetworks() {
  150. network_manager_->SignalNetworksChanged.connect(
  151. this, &ConnectivityChecker::OnNetworksChanged);
  152. network_manager_->StartUpdating();
  153. }
  154. void ConnectivityChecker::OnMessage(talk_base::Message *msg) {
  155. switch (msg->message_id) {
  156. case MSG_START:
  157. ASSERT(worker_ == talk_base::Thread::Current());
  158. worker_->PostDelayed(timeout_ms_, this, MSG_TIMEOUT);
  159. CheckNetworks();
  160. break;
  161. case MSG_STOP:
  162. // We're being stopped, free resources.
  163. CleanUp();
  164. break;
  165. case MSG_TIMEOUT:
  166. // We need to signal results on the main thread.
  167. main_->Post(this, MSG_SIGNAL_RESULTS);
  168. break;
  169. case MSG_SIGNAL_RESULTS:
  170. ASSERT(main_ == talk_base::Thread::Current());
  171. SignalCheckDone(this);
  172. break;
  173. default:
  174. LOG(LS_ERROR) << "Unknown message: " << msg->message_id;
  175. }
  176. }
  177. void ConnectivityChecker::OnProxyDetect(talk_base::SignalThread* thread) {
  178. ASSERT(worker_ == talk_base::Thread::Current());
  179. if (proxy_detect_->proxy().type != talk_base::PROXY_NONE) {
  180. SetProxyInfo(proxy_detect_->proxy());
  181. }
  182. }
  183. void ConnectivityChecker::OnRequestDone(talk_base::AsyncHttpRequest* request) {
  184. ASSERT(worker_ == talk_base::Thread::Current());
  185. // Since we don't know what nic were actually used for the http request,
  186. // for now, just use the first one.
  187. std::vector<talk_base::Network*> networks;
  188. network_manager_->GetNetworks(&networks);
  189. if (networks.empty()) {
  190. LOG(LS_ERROR) << "No networks while registering http start.";
  191. return;
  192. }
  193. talk_base::ProxyInfo proxy_info = request->proxy();
  194. NicMap::iterator i = nics_.find(NicId(networks[0]->ip(), proxy_info.address));
  195. if (i != nics_.end()) {
  196. int port = request->port();
  197. uint32 now = talk_base::Time();
  198. NicInfo* nic_info = &i->second;
  199. if (port == talk_base::HTTP_DEFAULT_PORT) {
  200. nic_info->http.rtt = now - nic_info->http.start_time_ms;
  201. } else if (port == talk_base::HTTP_SECURE_PORT) {
  202. nic_info->https.rtt = now - nic_info->https.start_time_ms;
  203. } else {
  204. LOG(LS_ERROR) << "Got response with unknown port: " << port;
  205. }
  206. } else {
  207. LOG(LS_ERROR) << "No nic info found while receiving response.";
  208. }
  209. }
  210. void ConnectivityChecker::OnConfigReady(
  211. const std::string& username, const std::string& password,
  212. const PortConfiguration* config, const talk_base::ProxyInfo& proxy_info) {
  213. ASSERT(worker_ == talk_base::Thread::Current());
  214. // Since we send requests on both HTTP and HTTPS we will get two
  215. // configs per nic. Results from the second will overwrite the
  216. // result from the first.
  217. // TODO: Handle multiple pings on one nic.
  218. CreateRelayPorts(username, password, config, proxy_info);
  219. }
  220. void ConnectivityChecker::OnRelayAddressReady(Port* port) {
  221. ASSERT(worker_ == talk_base::Thread::Current());
  222. RelayPort* relay_port = reinterpret_cast<RelayPort*>(port);
  223. const ProtocolAddress* address = relay_port->ServerAddress(0);
  224. talk_base::IPAddress ip = port->Network()->ip();
  225. NicMap::iterator i = nics_.find(NicId(ip, port->proxy().address));
  226. if (i != nics_.end()) {
  227. // We have it already, add the new information.
  228. NicInfo* nic_info = &i->second;
  229. ConnectInfo* connect_info = NULL;
  230. if (address) {
  231. switch (address->proto) {
  232. case PROTO_UDP:
  233. connect_info = &nic_info->udp;
  234. break;
  235. case PROTO_TCP:
  236. connect_info = &nic_info->tcp;
  237. break;
  238. case PROTO_SSLTCP:
  239. connect_info = &nic_info->ssltcp;
  240. break;
  241. default:
  242. LOG(LS_ERROR) << " relay address with bad protocol added";
  243. }
  244. if (connect_info) {
  245. connect_info->rtt =
  246. talk_base::TimeSince(connect_info->start_time_ms);
  247. }
  248. }
  249. } else {
  250. LOG(LS_ERROR) << " got relay address for non-existing nic";
  251. }
  252. }
  253. void ConnectivityChecker::OnStunAddressReady(Port* port) {
  254. ASSERT(worker_ == talk_base::Thread::Current());
  255. const std::vector<Candidate> candidates = port->Candidates();
  256. Candidate c = candidates[0];
  257. talk_base::IPAddress ip = port->Network()->ip();
  258. NicMap::iterator i = nics_.find(NicId(ip, port->proxy().address));
  259. if (i != nics_.end()) {
  260. // We have it already, add the new information.
  261. uint32 now = talk_base::Time();
  262. NicInfo* nic_info = &i->second;
  263. nic_info->external_address = c.address();
  264. nic_info->stun_server_address = static_cast<StunPort*>(port)->server_addr();
  265. nic_info->stun.rtt = now - nic_info->stun.start_time_ms;
  266. } else {
  267. LOG(LS_ERROR) << "Got stun address for non-existing nic";
  268. }
  269. }
  270. void ConnectivityChecker::OnStunAddressError(Port* port) {
  271. ASSERT(worker_ == talk_base::Thread::Current());
  272. LOG(LS_ERROR) << "Stun address error.";
  273. talk_base::IPAddress ip = port->Network()->ip();
  274. NicMap::iterator i = nics_.find(NicId(ip, port->proxy().address));
  275. if (i != nics_.end()) {
  276. // We have it already, add the new information.
  277. NicInfo* nic_info = &i->second;
  278. nic_info->stun_server_address = static_cast<StunPort*>(port)->server_addr();
  279. }
  280. }
  281. void ConnectivityChecker::OnRelayAddressError(Port* port) {
  282. ASSERT(worker_ == talk_base::Thread::Current());
  283. LOG(LS_ERROR) << "Relay address error.";
  284. }
  285. void ConnectivityChecker::OnNetworksChanged() {
  286. ASSERT(worker_ == talk_base::Thread::Current());
  287. std::vector<talk_base::Network*> networks;
  288. network_manager_->GetNetworks(&networks);
  289. if (networks.empty()) {
  290. LOG(LS_ERROR) << "Machine has no networks; nothing to do";
  291. return;
  292. }
  293. AllocatePorts();
  294. }
  295. HttpPortAllocator* ConnectivityChecker::CreatePortAllocator(
  296. talk_base::NetworkManager* network_manager,
  297. const std::string& user_agent,
  298. const std::string& relay_token) {
  299. return new TestHttpPortAllocator(network_manager, user_agent, relay_token);
  300. }
  301. StunPort* ConnectivityChecker::CreateStunPort(
  302. const std::string& username, const std::string& password,
  303. const PortConfiguration* config, talk_base::Network* network) {
  304. return StunPort::Create(worker_, socket_factory_.get(),
  305. network, network->ip(), 0, 0,
  306. username, password, config->stun_address);
  307. }
  308. RelayPort* ConnectivityChecker::CreateRelayPort(
  309. const std::string& username, const std::string& password,
  310. const PortConfiguration* config, talk_base::Network* network) {
  311. return RelayPort::Create(worker_, socket_factory_.get(),
  312. network, network->ip(),
  313. port_allocator_->min_port(),
  314. port_allocator_->max_port(),
  315. username, password);
  316. }
  317. void ConnectivityChecker::CreateRelayPorts(
  318. const std::string& username, const std::string& password,
  319. const PortConfiguration* config, const talk_base::ProxyInfo& proxy_info) {
  320. PortConfiguration::RelayList::const_iterator relay;
  321. std::vector<talk_base::Network*> networks;
  322. network_manager_->GetNetworks(&networks);
  323. if (networks.empty()) {
  324. LOG(LS_ERROR) << "Machine has no networks; no relay ports created.";
  325. return;
  326. }
  327. for (relay = config->relays.begin();
  328. relay != config->relays.end(); ++relay) {
  329. for (uint32 i = 0; i < networks.size(); ++i) {
  330. NicMap::iterator iter = nics_.find(NicId(networks[i]->ip(),
  331. proxy_info.address));
  332. if (iter != nics_.end()) {
  333. // TODO: Now setting the same start time for all protocols.
  334. // This might affect accuracy, but since we are mainly looking for
  335. // connect failures or number that stick out, this is good enough.
  336. uint32 now = talk_base::Time();
  337. NicInfo* nic_info = &iter->second;
  338. nic_info->udp.start_time_ms = now;
  339. nic_info->tcp.start_time_ms = now;
  340. nic_info->ssltcp.start_time_ms = now;
  341. // Add the addresses of this protocol.
  342. PortList::const_iterator relay_port;
  343. for (relay_port = relay->ports.begin();
  344. relay_port != relay->ports.end();
  345. ++relay_port) {
  346. RelayPort* port = CreateRelayPort(username, password,
  347. config, networks[i]);
  348. port->AddServerAddress(*relay_port);
  349. port->AddExternalAddress(*relay_port);
  350. nic_info->media_server_address = port->ServerAddress(0)->address;
  351. // Listen to network events.
  352. port->SignalAddressReady.connect(
  353. this, &ConnectivityChecker::OnRelayAddressReady);
  354. port->SignalAddressError.connect(
  355. this, &ConnectivityChecker::OnRelayAddressError);
  356. port->set_proxy(user_agent_, proxy_info);
  357. // Start fetching an address for this port.
  358. port->PrepareAddress();
  359. ports_.push_back(port);
  360. }
  361. } else {
  362. LOG(LS_ERROR) << "Failed to find nic info when creating relay ports.";
  363. }
  364. }
  365. }
  366. }
  367. void ConnectivityChecker::AllocatePorts() {
  368. const std::string username = talk_base::CreateRandomString(ICE_UFRAG_LENGTH);
  369. const std::string password = talk_base::CreateRandomString(ICE_PWD_LENGTH);
  370. PortConfiguration config(stun_address_, username, password);
  371. std::vector<talk_base::Network*> networks;
  372. network_manager_->GetNetworks(&networks);
  373. if (networks.empty()) {
  374. LOG(LS_ERROR) << "Machine has no networks; no ports will be allocated";
  375. return;
  376. }
  377. talk_base::ProxyInfo proxy_info = GetProxyInfo();
  378. bool allocate_relay_ports = false;
  379. for (uint32 i = 0; i < networks.size(); ++i) {
  380. if (AddNic(networks[i]->ip(), proxy_info.address)) {
  381. Port* port = CreateStunPort(username, password, &config, networks[i]);
  382. if (port) {
  383. // Listen to network events.
  384. port->SignalAddressReady.connect(
  385. this, &ConnectivityChecker::OnStunAddressReady);
  386. port->SignalAddressError.connect(
  387. this, &ConnectivityChecker::OnStunAddressError);
  388. port->set_proxy(user_agent_, proxy_info);
  389. port->PrepareAddress();
  390. ports_.push_back(port);
  391. allocate_relay_ports = true;
  392. }
  393. }
  394. }
  395. // If any new ip/proxy combinations were added, send a relay allocate.
  396. if (allocate_relay_ports) {
  397. AllocateRelayPorts();
  398. }
  399. // Initiate proxy detection.
  400. InitiateProxyDetection();
  401. }
  402. void ConnectivityChecker::InitiateProxyDetection() {
  403. // Only start if we haven't been started before.
  404. if (!proxy_detect_) {
  405. proxy_detect_ = new talk_base::AutoDetectProxy(user_agent_);
  406. talk_base::Url<char> host_url("/", "relay.google.com",
  407. talk_base::HTTP_DEFAULT_PORT);
  408. host_url.set_secure(true);
  409. proxy_detect_->set_server_url(host_url.url());
  410. proxy_detect_->SignalWorkDone.connect(
  411. this, &ConnectivityChecker::OnProxyDetect);
  412. proxy_detect_->Start();
  413. }
  414. }
  415. void ConnectivityChecker::AllocateRelayPorts() {
  416. // Currently we are using the 'default' nic for http(s) requests.
  417. TestHttpPortAllocatorSession* allocator_session =
  418. reinterpret_cast<TestHttpPortAllocatorSession*>(
  419. port_allocator_->CreateSessionInternal(
  420. "connectivity checker test content",
  421. ICE_CANDIDATE_COMPONENT_RTP,
  422. talk_base::CreateRandomString(ICE_UFRAG_LENGTH),
  423. talk_base::CreateRandomString(ICE_PWD_LENGTH)));
  424. allocator_session->set_proxy(port_allocator_->proxy());
  425. allocator_session->SignalConfigReady.connect(
  426. this, &ConnectivityChecker::OnConfigReady);
  427. allocator_session->SignalRequestDone.connect(
  428. this, &ConnectivityChecker::OnRequestDone);
  429. // Try both http and https.
  430. RegisterHttpStart(talk_base::HTTP_SECURE_PORT);
  431. allocator_session->SendSessionRequest("relay.l.google.com",
  432. talk_base::HTTP_SECURE_PORT);
  433. RegisterHttpStart(talk_base::HTTP_DEFAULT_PORT);
  434. allocator_session->SendSessionRequest("relay.l.google.com",
  435. talk_base::HTTP_DEFAULT_PORT);
  436. sessions_.push_back(allocator_session);
  437. }
  438. void ConnectivityChecker::RegisterHttpStart(int port) {
  439. // Since we don't know what nic were actually used for the http request,
  440. // for now, just use the first one.
  441. std::vector<talk_base::Network*> networks;
  442. network_manager_->GetNetworks(&networks);
  443. if (networks.empty()) {
  444. LOG(LS_ERROR) << "No networks while registering http start.";
  445. return;
  446. }
  447. talk_base::ProxyInfo proxy_info = GetProxyInfo();
  448. NicMap::iterator i = nics_.find(NicId(networks[0]->ip(), proxy_info.address));
  449. if (i != nics_.end()) {
  450. uint32 now = talk_base::Time();
  451. NicInfo* nic_info = &i->second;
  452. if (port == talk_base::HTTP_DEFAULT_PORT) {
  453. nic_info->http.start_time_ms = now;
  454. } else if (port == talk_base::HTTP_SECURE_PORT) {
  455. nic_info->https.start_time_ms = now;
  456. } else {
  457. LOG(LS_ERROR) << "Registering start time for unknown port: " << port;
  458. }
  459. } else {
  460. LOG(LS_ERROR) << "Error, no nic info found while registering http start.";
  461. }
  462. }
  463. } // namespace talk_base