Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: webrtc/p2p/stunprober/stunprober.cc

Issue 1166013002: Better determination of Symmetric NAT (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 using NATTYPE = StunProber::NATTYPE;
26
25 namespace { 27 namespace {
26 28
27 void IncrementCounterByAddress(std::map<rtc::IPAddress, int>* counter_per_ip, 29 template <typename T>
28 const rtc::IPAddress& ip) { 30 void IncrementCounterByAddress(std::map<T, int>* counter_per_ip, const T& ip) {
29 counter_per_ip->insert(std::make_pair(ip, 0)).first->second++; 31 counter_per_ip->insert(std::make_pair(ip, 0)).first->second++;
30 } 32 }
31 33
34 bool is_behind_nat(NATTYPE nat_type) {
35 return nat_type > NATTYPE::NATTYPE_NO_NAT;
36 }
pthatcher2 2015/06/05 22:39:41 It's probably not worth a separate function if you
guoweis_webrtc 2015/06/07 17:29:40 I changed the name but I like it being separated a
37
32 } // namespace 38 } // namespace
33 39
34 // A requester tracks the requests and responses from a single socket to many 40 // A requester tracks the requests and responses from a single socket to many
35 // STUN servers 41 // STUN servers
36 class StunProber::Requester { 42 class StunProber::Requester {
37 public: 43 public:
38 // Each Request maps to a request and response. 44 // Each Request maps to a request and response.
39 struct Request { 45 struct Request {
40 // Actual time the STUN bind request was sent. 46 // Actual time the STUN bind request was sent.
41 int64 sent_time_ms = 0; 47 int64 sent_time_ms = 0;
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 if (!SendNextRequest()) { 417 if (!SendNextRequest()) {
412 End(GENERIC_FAILURE, 0); 418 End(GENERIC_FAILURE, 0);
413 return; 419 return;
414 } 420 }
415 next_request_time_ms_ = now + interval_ms_; 421 next_request_time_ms_ = now + interval_ms_;
416 } 422 }
417 task_runner_->PostTask( 423 task_runner_->PostTask(
418 rtc::Bind(&StunProber::MaybeScheduleStunRequests, this), 1 /* ms */); 424 rtc::Bind(&StunProber::MaybeScheduleStunRequests, this), 1 /* ms */);
419 } 425 }
420 426
421 bool StunProber::GetStats(StunProber::Stats* prob_stats) { 427 bool StunProber::GetStats(StunProber::Stats* prob_stats) const {
422 // No need to be on the same thread. 428 // No need to be on the same thread.
423 if (!prob_stats) { 429 if (!prob_stats) {
424 return false; 430 return false;
425 } 431 }
426 432
427 StunProber::Stats stats; 433 StunProber::Stats stats;
428 434
429 int rtt_sum = 0; 435 int rtt_sum = 0;
430 bool behind_nat_set = false;
431 int64 first_sent_time = 0; 436 int64 first_sent_time = 0;
432 int64 last_sent_time = 0; 437 int64 last_sent_time = 0;
438 NATTYPE nat_type = NATTYPE_UNKNOWN;
433 439
434 // Track of how many srflx IP that we have seen. 440 // Track of how many srflx IP that we have seen.
435 std::set<rtc::IPAddress> srflx_ips; 441 std::set<rtc::IPAddress> srflx_ips;
436 442
437 // If we're not receiving any response on a given IP, all requests sent to 443 // 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. 444 // that IP should be ignored as this could just be an DNS error.
439 std::map<rtc::IPAddress, int> num_response_per_ip; 445 std::map<rtc::IPAddress, int> num_response_per_server;
440 std::map<rtc::IPAddress, int> num_request_per_ip; 446 std::map<rtc::IPAddress, int> num_request_per_server;
441 447
442 for (auto* requester : requesters_) { 448 for (auto* requester : requesters_) {
449 std::map<rtc::SocketAddress, int> num_response_per_srflx_addr;
443 for (auto request : requester->requests()) { 450 for (auto request : requester->requests()) {
444 if (request->sent_time_ms <= 0) { 451 if (request->sent_time_ms <= 0) {
445 continue; 452 continue;
446 } 453 }
447 454
448 IncrementCounterByAddress(&num_request_per_ip, request->server_addr); 455 IncrementCounterByAddress(&num_request_per_server, request->server_addr);
449 456
450 if (!first_sent_time) { 457 if (!first_sent_time) {
451 first_sent_time = request->sent_time_ms; 458 first_sent_time = request->sent_time_ms;
452 } 459 }
453 last_sent_time = request->sent_time_ms; 460 last_sent_time = request->sent_time_ms;
454 461
455 if (request->received_time_ms < request->sent_time_ms) { 462 if (request->received_time_ms < request->sent_time_ms) {
456 continue; 463 continue;
457 } 464 }
458 465
459 IncrementCounterByAddress(&num_response_per_ip, request->server_addr); 466 IncrementCounterByAddress(&num_response_per_server, request->server_addr);
467 IncrementCounterByAddress(&num_response_per_srflx_addr,
468 request->srflx_addr);
460 469
461 rtt_sum += request->rtt(); 470 rtt_sum += request->rtt();
462 if (!behind_nat_set) { 471 if (nat_type == NATTYPE_UNKNOWN) {
463 stats.behind_nat = request->behind_nat; 472 nat_type =
464 behind_nat_set = true; 473 request->behind_nat ? NATTYPE_NAT_TYPE_UNKNOWN : NATTYPE_NO_NAT;
465 } else if (stats.behind_nat != request->behind_nat) { 474 } else if (is_behind_nat(nat_type) != request->behind_nat) {
466 // Detect the inconsistency in NAT presence. 475 // Detect the inconsistency in NAT presence.
467 return false; 476 return false;
468 } 477 }
469 stats.srflx_addrs.insert(request->srflx_addr.ToString()); 478 stats.srflx_addrs.insert(request->srflx_addr.ToString());
470 srflx_ips.insert(request->srflx_addr.ipaddr()); 479 srflx_ips.insert(request->srflx_addr.ipaddr());
471 } 480 }
481
482 // If we're using shared mode and seeing >1 srflx addresses for a single
483 // requester, it's symmetric NAT.
484 if (shared_socket_mode_ && num_response_per_srflx_addr.size() > 1) {
485 nat_type = NATTYPE_SYM_NAT;
486 }
472 } 487 }
473 488
474 // We're probably not behind a regular NAT. We have more than 1 distinct 489 // We're probably not behind a regular NAT. We have more than 1 distinct
475 // server reflexive IPs. 490 // server reflexive IPs.
476 if (srflx_ips.size() > 1) { 491 if (srflx_ips.size() > 1) {
477 return false; 492 return false;
478 } 493 }
479 494
480 int num_sent = 0; 495 int num_sent = 0;
481 int num_received = 0; 496 int num_received = 0;
482 int num_server_ip_with_response = 0; 497 int num_server_ip_with_response = 0;
483 498
484 for (const auto& kv : num_response_per_ip) { 499 for (const auto& kv : num_response_per_server) {
485 DCHECK_GT(kv.second, 0); 500 DCHECK_GT(kv.second, 0);
486 num_server_ip_with_response++; 501 num_server_ip_with_response++;
487 num_received += kv.second; 502 num_received += kv.second;
488 num_sent += num_request_per_ip[kv.first]; 503 num_sent += num_request_per_server[kv.first];
489 } 504 }
490 505
491 // Not receiving any response, the trial is inconclusive. 506 // Not receiving any response, the trial is inconclusive.
492 if (!num_received) { 507 if (!num_received) {
493 return false; 508 return false;
494 } 509 }
495 510
511 stats.nat_type = nat_type;
512
496 // Shared mode is only true if we use the shared socket and there are more 513 // Shared mode is only true if we use the shared socket and there are more
497 // than 1 responding servers. 514 // than 1 responding servers.
498 stats.shared_socket_mode = 515 stats.shared_socket_mode =
499 shared_socket_mode_ && (num_server_ip_with_response > 1); 516 shared_socket_mode_ && (num_server_ip_with_response > 1);
500 517
518 if (stats.shared_socket_mode && nat_type == NATTYPE_NAT_TYPE_UNKNOWN) {
519 stats.nat_type = NATTYPE_NON_SYM_NAT;
520 }
521
501 stats.host_ip = local_addr_.ToString(); 522 stats.host_ip = local_addr_.ToString();
502 stats.num_request_sent = num_sent; 523 stats.num_request_sent = num_sent;
503 stats.num_response_received = num_received; 524 stats.num_response_received = num_received;
504 stats.target_request_interval_ns = interval_ms_ * 1000; 525 stats.target_request_interval_ns = interval_ms_ * 1000;
505 stats.symmetric_nat =
506 stats.srflx_addrs.size() > static_cast<size_t>(GetTotalServerSockets());
507 526
508 if (num_sent) { 527 if (num_sent) {
509 stats.success_percent = static_cast<int>(100 * num_received / num_sent); 528 stats.success_percent = static_cast<int>(100 * num_received / num_sent);
510 } 529 }
511 530
512 if (num_sent > 1) { 531 if (num_sent > 1) {
513 stats.actual_request_interval_ns = 532 stats.actual_request_interval_ns =
514 (1000 * (last_sent_time - first_sent_time)) / (num_sent - 1); 533 (1000 * (last_sent_time - first_sent_time)) / (num_sent - 1);
515 } 534 }
516 535
(...skipping 10 matching lines...) Expand all
527 if (!finished_callback_.empty()) { 546 if (!finished_callback_.empty()) {
528 AsyncCallback callback = finished_callback_; 547 AsyncCallback callback = finished_callback_;
529 finished_callback_ = AsyncCallback(); 548 finished_callback_ = AsyncCallback();
530 549
531 // Callback at the last since the prober might be deleted in the callback. 550 // Callback at the last since the prober might be deleted in the callback.
532 callback(status); 551 callback(status);
533 } 552 }
534 } 553 }
535 554
536 } // namespace stunprober 555 } // namespace stunprober
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698