| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <iostream> | 11 #include <iostream> |
| 12 #include <map> | 12 #include <map> |
| 13 #include <set> | 13 #include <set> |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "webrtc/base/bind.h" | 16 #include "webrtc/base/bind.h" |
| 17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 18 #include "webrtc/base/helpers.h" | 18 #include "webrtc/base/helpers.h" |
| 19 #include "webrtc/base/timeutils.h" | 19 #include "webrtc/base/timeutils.h" |
| 20 #include "webrtc/p2p/base/stun.h" | 20 #include "webrtc/p2p/base/stun.h" |
| 21 #include "webrtc/p2p/stunprober/stunprober.h" | 21 #include "webrtc/p2p/stunprober/stunprober.h" |
| 22 | 22 |
| 23 namespace stunprober { | 23 namespace stunprober { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 void IncrementCounterByAddress(std::map<rtc::IPAddress, int>* counter_per_ip, | 27 template <typename T> |
| 28 const rtc::IPAddress& ip) { | 28 void IncrementCounterByAddress(std::map<T, int>* counter_per_ip, const T& ip) { |
| 29 counter_per_ip->insert(std::make_pair(ip, 0)).first->second++; | 29 counter_per_ip->insert(std::make_pair(ip, 0)).first->second++; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool behind_nat(NatType nat_type) { |
| 33 return nat_type > stunprober::NATTYPE_NONE; |
| 34 } |
| 35 |
| 32 } // namespace | 36 } // namespace |
| 33 | 37 |
| 34 // A requester tracks the requests and responses from a single socket to many | 38 // A requester tracks the requests and responses from a single socket to many |
| 35 // STUN servers | 39 // STUN servers |
| 36 class StunProber::Requester { | 40 class StunProber::Requester { |
| 37 public: | 41 public: |
| 38 // Each Request maps to a request and response. | 42 // Each Request maps to a request and response. |
| 39 struct Request { | 43 struct Request { |
| 40 // Actual time the STUN bind request was sent. | 44 // Actual time the STUN bind request was sent. |
| 41 int64 sent_time_ms = 0; | 45 int64 sent_time_ms = 0; |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 if (!SendNextRequest()) { | 415 if (!SendNextRequest()) { |
| 412 End(GENERIC_FAILURE, 0); | 416 End(GENERIC_FAILURE, 0); |
| 413 return; | 417 return; |
| 414 } | 418 } |
| 415 next_request_time_ms_ = now + interval_ms_; | 419 next_request_time_ms_ = now + interval_ms_; |
| 416 } | 420 } |
| 417 task_runner_->PostTask( | 421 task_runner_->PostTask( |
| 418 rtc::Bind(&StunProber::MaybeScheduleStunRequests, this), 1 /* ms */); | 422 rtc::Bind(&StunProber::MaybeScheduleStunRequests, this), 1 /* ms */); |
| 419 } | 423 } |
| 420 | 424 |
| 421 bool StunProber::GetStats(StunProber::Stats* prob_stats) { | 425 bool StunProber::GetStats(StunProber::Stats* prob_stats) const { |
| 422 // No need to be on the same thread. | 426 // No need to be on the same thread. |
| 423 if (!prob_stats) { | 427 if (!prob_stats) { |
| 424 return false; | 428 return false; |
| 425 } | 429 } |
| 426 | 430 |
| 427 StunProber::Stats stats; | 431 StunProber::Stats stats; |
| 428 | 432 |
| 429 int rtt_sum = 0; | 433 int rtt_sum = 0; |
| 430 bool behind_nat_set = false; | |
| 431 int64 first_sent_time = 0; | 434 int64 first_sent_time = 0; |
| 432 int64 last_sent_time = 0; | 435 int64 last_sent_time = 0; |
| 436 NatType nat_type = NATTYPE_INVALID; |
| 433 | 437 |
| 434 // Track of how many srflx IP that we have seen. | 438 // Track of how many srflx IP that we have seen. |
| 435 std::set<rtc::IPAddress> srflx_ips; | 439 std::set<rtc::IPAddress> srflx_ips; |
| 436 | 440 |
| 437 // If we're not receiving any response on a given IP, all requests sent to | 441 // If we're not receiving any response on a given IP, all requests sent to |
| 438 // that IP should be ignored as this could just be an DNS error. | 442 // that IP should be ignored as this could just be an DNS error. |
| 439 std::map<rtc::IPAddress, int> num_response_per_ip; | 443 std::map<rtc::IPAddress, int> num_response_per_server; |
| 440 std::map<rtc::IPAddress, int> num_request_per_ip; | 444 std::map<rtc::IPAddress, int> num_request_per_server; |
| 441 | 445 |
| 442 for (auto* requester : requesters_) { | 446 for (auto* requester : requesters_) { |
| 447 std::map<rtc::SocketAddress, int> num_response_per_srflx_addr; |
| 443 for (auto request : requester->requests()) { | 448 for (auto request : requester->requests()) { |
| 444 if (request->sent_time_ms <= 0) { | 449 if (request->sent_time_ms <= 0) { |
| 445 continue; | 450 continue; |
| 446 } | 451 } |
| 447 | 452 |
| 448 IncrementCounterByAddress(&num_request_per_ip, request->server_addr); | 453 IncrementCounterByAddress(&num_request_per_server, request->server_addr); |
| 449 | 454 |
| 450 if (!first_sent_time) { | 455 if (!first_sent_time) { |
| 451 first_sent_time = request->sent_time_ms; | 456 first_sent_time = request->sent_time_ms; |
| 452 } | 457 } |
| 453 last_sent_time = request->sent_time_ms; | 458 last_sent_time = request->sent_time_ms; |
| 454 | 459 |
| 455 if (request->received_time_ms < request->sent_time_ms) { | 460 if (request->received_time_ms < request->sent_time_ms) { |
| 456 continue; | 461 continue; |
| 457 } | 462 } |
| 458 | 463 |
| 459 IncrementCounterByAddress(&num_response_per_ip, request->server_addr); | 464 IncrementCounterByAddress(&num_response_per_server, request->server_addr); |
| 465 IncrementCounterByAddress(&num_response_per_srflx_addr, |
| 466 request->srflx_addr); |
| 460 | 467 |
| 461 rtt_sum += request->rtt(); | 468 rtt_sum += request->rtt(); |
| 462 if (!behind_nat_set) { | 469 if (nat_type == NATTYPE_INVALID) { |
| 463 stats.behind_nat = request->behind_nat; | 470 nat_type = request->behind_nat ? NATTYPE_UNKNOWN : NATTYPE_NONE; |
| 464 behind_nat_set = true; | 471 } else if (behind_nat(nat_type) != request->behind_nat) { |
| 465 } else if (stats.behind_nat != request->behind_nat) { | |
| 466 // Detect the inconsistency in NAT presence. | 472 // Detect the inconsistency in NAT presence. |
| 467 return false; | 473 return false; |
| 468 } | 474 } |
| 469 stats.srflx_addrs.insert(request->srflx_addr.ToString()); | 475 stats.srflx_addrs.insert(request->srflx_addr.ToString()); |
| 470 srflx_ips.insert(request->srflx_addr.ipaddr()); | 476 srflx_ips.insert(request->srflx_addr.ipaddr()); |
| 471 } | 477 } |
| 478 |
| 479 // If we're using shared mode and seeing >1 srflx addresses for a single |
| 480 // requester, it's symmetric NAT. |
| 481 if (shared_socket_mode_ && num_response_per_srflx_addr.size() > 1) { |
| 482 nat_type = NATTYPE_SYMMETRIC; |
| 483 } |
| 472 } | 484 } |
| 473 | 485 |
| 474 // We're probably not behind a regular NAT. We have more than 1 distinct | 486 // We're probably not behind a regular NAT. We have more than 1 distinct |
| 475 // server reflexive IPs. | 487 // server reflexive IPs. |
| 476 if (srflx_ips.size() > 1) { | 488 if (srflx_ips.size() > 1) { |
| 477 return false; | 489 return false; |
| 478 } | 490 } |
| 479 | 491 |
| 480 int num_sent = 0; | 492 int num_sent = 0; |
| 481 int num_received = 0; | 493 int num_received = 0; |
| 482 int num_server_ip_with_response = 0; | 494 int num_server_ip_with_response = 0; |
| 483 | 495 |
| 484 for (const auto& kv : num_response_per_ip) { | 496 for (const auto& kv : num_response_per_server) { |
| 485 DCHECK_GT(kv.second, 0); | 497 DCHECK_GT(kv.second, 0); |
| 486 num_server_ip_with_response++; | 498 num_server_ip_with_response++; |
| 487 num_received += kv.second; | 499 num_received += kv.second; |
| 488 num_sent += num_request_per_ip[kv.first]; | 500 num_sent += num_request_per_server[kv.first]; |
| 489 } | 501 } |
| 490 | 502 |
| 491 // Not receiving any response, the trial is inconclusive. | 503 // Not receiving any response, the trial is inconclusive. |
| 492 if (!num_received) { | 504 if (!num_received) { |
| 493 return false; | 505 return false; |
| 494 } | 506 } |
| 495 | 507 |
| 508 stats.nat_type = nat_type; |
| 509 |
| 496 // Shared mode is only true if we use the shared socket and there are more | 510 // Shared mode is only true if we use the shared socket and there are more |
| 497 // than 1 responding servers. | 511 // than 1 responding servers. |
| 498 stats.shared_socket_mode = | 512 stats.shared_socket_mode = |
| 499 shared_socket_mode_ && (num_server_ip_with_response > 1); | 513 shared_socket_mode_ && (num_server_ip_with_response > 1); |
| 500 | 514 |
| 515 if (stats.shared_socket_mode && nat_type == NATTYPE_UNKNOWN) { |
| 516 stats.nat_type = NATTYPE_NON_SYMMETRIC; |
| 517 } |
| 518 |
| 501 stats.host_ip = local_addr_.ToString(); | 519 stats.host_ip = local_addr_.ToString(); |
| 502 stats.num_request_sent = num_sent; | 520 stats.num_request_sent = num_sent; |
| 503 stats.num_response_received = num_received; | 521 stats.num_response_received = num_received; |
| 504 stats.target_request_interval_ns = interval_ms_ * 1000; | 522 stats.target_request_interval_ns = interval_ms_ * 1000; |
| 505 stats.symmetric_nat = | |
| 506 stats.srflx_addrs.size() > static_cast<size_t>(GetTotalServerSockets()); | |
| 507 | 523 |
| 508 if (num_sent) { | 524 if (num_sent) { |
| 509 stats.success_percent = static_cast<int>(100 * num_received / num_sent); | 525 stats.success_percent = static_cast<int>(100 * num_received / num_sent); |
| 510 } | 526 } |
| 511 | 527 |
| 512 if (num_sent > 1) { | 528 if (num_sent > 1) { |
| 513 stats.actual_request_interval_ns = | 529 stats.actual_request_interval_ns = |
| 514 (1000 * (last_sent_time - first_sent_time)) / (num_sent - 1); | 530 (1000 * (last_sent_time - first_sent_time)) / (num_sent - 1); |
| 515 } | 531 } |
| 516 | 532 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 527 if (!finished_callback_.empty()) { | 543 if (!finished_callback_.empty()) { |
| 528 AsyncCallback callback = finished_callback_; | 544 AsyncCallback callback = finished_callback_; |
| 529 finished_callback_ = AsyncCallback(); | 545 finished_callback_ = AsyncCallback(); |
| 530 | 546 |
| 531 // Callback at the last since the prober might be deleted in the callback. | 547 // Callback at the last since the prober might be deleted in the callback. |
| 532 callback(status); | 548 callback(status); |
| 533 } | 549 } |
| 534 } | 550 } |
| 535 | 551 |
| 536 } // namespace stunprober | 552 } // namespace stunprober |
| OLD | NEW |