Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 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 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 15 | #include <map> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 16 | #include <memory> |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 17 | #include <sstream> // no-presubmit-check TODO(webrtc:8982) |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "p2p/base/basicpacketsocketfactory.h" |
| 20 | #include "p2p/stunprober/stunprober.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/flags.h" |
| 23 | #include "rtc_base/helpers.h" |
| 24 | #include "rtc_base/logging.h" |
| 25 | #include "rtc_base/nethelpers.h" |
| 26 | #include "rtc_base/network.h" |
| 27 | #include "rtc_base/ssladapter.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 28 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/stringutils.h" |
| 30 | #include "rtc_base/thread.h" |
| 31 | #include "rtc_base/timeutils.h" |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 32 | |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 33 | using stunprober::StunProber; |
| 34 | using stunprober::AsyncCallback; |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 35 | |
Mirko Bonadei | c538fc7 | 2018-10-19 15:04:07 +0000 | [diff] [blame^] | 36 | DEFINE_bool(help, false, "Prints this message"); |
| 37 | DEFINE_int(interval, 10, "Interval of consecutive stun pings in milliseconds"); |
| 38 | DEFINE_bool(shared_socket, false, "Share socket mode for different remote IPs"); |
| 39 | DEFINE_int(pings_per_ip, |
| 40 | 10, |
| 41 | "Number of consecutive stun pings to send for each IP"); |
| 42 | DEFINE_int(timeout, |
| 43 | 1000, |
| 44 | "Milliseconds of wait after the last ping sent before exiting"); |
| 45 | DEFINE_string( |
Guo-wei Shieh | 1ab67ae | 2015-05-18 21:36:20 -0700 | [diff] [blame] | 46 | servers, |
| 47 | "stun.l.google.com:19302,stun1.l.google.com:19302,stun2.l.google.com:19302", |
| 48 | "Comma separated STUN server addresses with ports"); |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 49 | |
| 50 | namespace { |
| 51 | |
Guo-wei Shieh | 72e9f04 | 2015-06-08 21:03:48 -0700 | [diff] [blame] | 52 | const char* PrintNatType(stunprober::NatType type) { |
| 53 | switch (type) { |
| 54 | case stunprober::NATTYPE_NONE: |
| 55 | return "Not behind a NAT"; |
| 56 | case stunprober::NATTYPE_UNKNOWN: |
| 57 | return "Unknown NAT type"; |
| 58 | case stunprober::NATTYPE_SYMMETRIC: |
| 59 | return "Symmetric NAT"; |
| 60 | case stunprober::NATTYPE_NON_SYMMETRIC: |
| 61 | return "Non-Symmetric NAT"; |
| 62 | default: |
| 63 | return "Invalid"; |
| 64 | } |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void PrintStats(StunProber* prober) { |
| 68 | StunProber::Stats stats; |
| 69 | if (!prober->GetStats(&stats)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 70 | RTC_LOG(LS_WARNING) << "Results are inconclusive."; |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 74 | RTC_LOG(LS_INFO) << "Shared Socket Mode: " << stats.shared_socket_mode; |
| 75 | RTC_LOG(LS_INFO) << "Requests sent: " << stats.num_request_sent; |
| 76 | RTC_LOG(LS_INFO) << "Responses received: " << stats.num_response_received; |
| 77 | RTC_LOG(LS_INFO) << "Target interval (ns): " |
| 78 | << stats.target_request_interval_ns; |
| 79 | RTC_LOG(LS_INFO) << "Actual interval (ns): " |
| 80 | << stats.actual_request_interval_ns; |
| 81 | RTC_LOG(LS_INFO) << "NAT Type: " << PrintNatType(stats.nat_type); |
| 82 | RTC_LOG(LS_INFO) << "Host IP: " << stats.host_ip; |
| 83 | RTC_LOG(LS_INFO) << "Server-reflexive ips: "; |
Guo-wei Shieh | 1ab67ae | 2015-05-18 21:36:20 -0700 | [diff] [blame] | 84 | for (auto& ip : stats.srflx_addrs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 85 | RTC_LOG(LS_INFO) << "\t" << ip; |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 88 | RTC_LOG(LS_INFO) << "Success Precent: " << stats.success_percent; |
| 89 | RTC_LOG(LS_INFO) << "Response Latency:" << stats.average_rtt_ms; |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void StopTrial(rtc::Thread* thread, StunProber* prober, int result) { |
| 93 | thread->Quit(); |
| 94 | if (prober) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 95 | RTC_LOG(LS_INFO) << "Result: " << result; |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 96 | if (result == StunProber::SUCCESS) { |
| 97 | PrintStats(prober); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
Robin Raymond | 1c62ffa | 2017-12-03 16:45:56 -0500 | [diff] [blame] | 104 | int main(int argc, char* argv[]) { |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 105 | rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
| 106 | if (FLAG_help) { |
| 107 | rtc::FlagList::Print(nullptr, false); |
| 108 | return 0; |
| 109 | } |
| 110 | |
Guo-wei Shieh | 1ab67ae | 2015-05-18 21:36:20 -0700 | [diff] [blame] | 111 | std::vector<rtc::SocketAddress> server_addresses; |
| 112 | std::istringstream servers(FLAG_servers); |
| 113 | std::string server; |
| 114 | while (getline(servers, server, ',')) { |
| 115 | rtc::SocketAddress addr; |
| 116 | if (!addr.FromString(server)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 117 | RTC_LOG(LS_ERROR) << "Parsing " << server << " failed."; |
Guo-wei Shieh | 1ab67ae | 2015-05-18 21:36:20 -0700 | [diff] [blame] | 118 | return -1; |
| 119 | } |
| 120 | server_addresses.push_back(addr); |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | rtc::InitializeSSL(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 124 | rtc::InitRandom(rtc::Time32()); |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 125 | rtc::Thread* thread = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 126 | std::unique_ptr<rtc::BasicPacketSocketFactory> socket_factory( |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 127 | new rtc::BasicPacketSocketFactory()); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 128 | std::unique_ptr<rtc::BasicNetworkManager> network_manager( |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 129 | new rtc::BasicNetworkManager()); |
| 130 | rtc::NetworkManager::NetworkList networks; |
| 131 | network_manager->GetNetworks(&networks); |
| 132 | StunProber* prober = |
| 133 | new StunProber(socket_factory.get(), rtc::Thread::Current(), networks); |
| 134 | auto finish_callback = [thread](StunProber* prober, int result) { |
| 135 | StopTrial(thread, prober, result); |
| 136 | }; |
Guo-wei Shieh | 1ab67ae | 2015-05-18 21:36:20 -0700 | [diff] [blame] | 137 | prober->Start(server_addresses, FLAG_shared_socket, FLAG_interval, |
Guo-wei Shieh | 37931c4 | 2015-05-15 10:26:52 -0700 | [diff] [blame] | 138 | FLAG_pings_per_ip, FLAG_timeout, |
| 139 | AsyncCallback(finish_callback)); |
| 140 | thread->Run(); |
| 141 | delete prober; |
| 142 | return 0; |
| 143 | } |