blob: 42ee0a1a9a2fe9cb3a62e1fecdd50f9c5e457839 [file] [log] [blame]
Guo-wei Shieh37931c42015-05-15 10:26:52 -07001/*
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 Shieh37931c42015-05-15 10:26:52 -070015#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070016#include <memory>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/basicpacketsocketfactory.h"
19#include "p2p/stunprober/stunprober.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/flags.h"
22#include "rtc_base/helpers.h"
23#include "rtc_base/logging.h"
24#include "rtc_base/nethelpers.h"
25#include "rtc_base/network.h"
26#include "rtc_base/ssladapter.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020027#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/stringutils.h"
29#include "rtc_base/thread.h"
30#include "rtc_base/timeutils.h"
Guo-wei Shieh37931c42015-05-15 10:26:52 -070031
Guo-wei Shieh37931c42015-05-15 10:26:52 -070032using stunprober::StunProber;
33using stunprober::AsyncCallback;
Guo-wei Shieh37931c42015-05-15 10:26:52 -070034
35DEFINE_bool(help, false, "Prints this message");
36DEFINE_int(interval, 10, "Interval of consecutive stun pings in milliseconds");
37DEFINE_bool(shared_socket, false, "Share socket mode for different remote IPs");
38DEFINE_int(pings_per_ip,
39 10,
40 "Number of consecutive stun pings to send for each IP");
41DEFINE_int(timeout,
42 1000,
43 "Milliseconds of wait after the last ping sent before exiting");
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -070044DEFINE_string(
45 servers,
46 "stun.l.google.com:19302,stun1.l.google.com:19302,stun2.l.google.com:19302",
47 "Comma separated STUN server addresses with ports");
Guo-wei Shieh37931c42015-05-15 10:26:52 -070048
49namespace {
50
Guo-wei Shieh72e9f042015-06-08 21:03:48 -070051const char* PrintNatType(stunprober::NatType type) {
52 switch (type) {
53 case stunprober::NATTYPE_NONE:
54 return "Not behind a NAT";
55 case stunprober::NATTYPE_UNKNOWN:
56 return "Unknown NAT type";
57 case stunprober::NATTYPE_SYMMETRIC:
58 return "Symmetric NAT";
59 case stunprober::NATTYPE_NON_SYMMETRIC:
60 return "Non-Symmetric NAT";
61 default:
62 return "Invalid";
63 }
Guo-wei Shieh37931c42015-05-15 10:26:52 -070064}
65
66void PrintStats(StunProber* prober) {
67 StunProber::Stats stats;
68 if (!prober->GetStats(&stats)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010069 RTC_LOG(LS_WARNING) << "Results are inconclusive.";
Guo-wei Shieh37931c42015-05-15 10:26:52 -070070 return;
71 }
72
Mirko Bonadei675513b2017-11-09 11:09:25 +010073 RTC_LOG(LS_INFO) << "Shared Socket Mode: " << stats.shared_socket_mode;
74 RTC_LOG(LS_INFO) << "Requests sent: " << stats.num_request_sent;
75 RTC_LOG(LS_INFO) << "Responses received: " << stats.num_response_received;
76 RTC_LOG(LS_INFO) << "Target interval (ns): "
77 << stats.target_request_interval_ns;
78 RTC_LOG(LS_INFO) << "Actual interval (ns): "
79 << stats.actual_request_interval_ns;
80 RTC_LOG(LS_INFO) << "NAT Type: " << PrintNatType(stats.nat_type);
81 RTC_LOG(LS_INFO) << "Host IP: " << stats.host_ip;
82 RTC_LOG(LS_INFO) << "Server-reflexive ips: ";
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -070083 for (auto& ip : stats.srflx_addrs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010084 RTC_LOG(LS_INFO) << "\t" << ip;
Guo-wei Shieh37931c42015-05-15 10:26:52 -070085 }
86
Mirko Bonadei675513b2017-11-09 11:09:25 +010087 RTC_LOG(LS_INFO) << "Success Precent: " << stats.success_percent;
88 RTC_LOG(LS_INFO) << "Response Latency:" << stats.average_rtt_ms;
Guo-wei Shieh37931c42015-05-15 10:26:52 -070089}
90
91void StopTrial(rtc::Thread* thread, StunProber* prober, int result) {
92 thread->Quit();
93 if (prober) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_INFO) << "Result: " << result;
Guo-wei Shieh37931c42015-05-15 10:26:52 -070095 if (result == StunProber::SUCCESS) {
96 PrintStats(prober);
97 }
98 }
99}
100
101} // namespace
102
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500103int main(int argc, char* argv[]) {
Guo-wei Shieh37931c42015-05-15 10:26:52 -0700104 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
105 if (FLAG_help) {
106 rtc::FlagList::Print(nullptr, false);
107 return 0;
108 }
109
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -0700110 std::vector<rtc::SocketAddress> server_addresses;
111 std::istringstream servers(FLAG_servers);
112 std::string server;
113 while (getline(servers, server, ',')) {
114 rtc::SocketAddress addr;
115 if (!addr.FromString(server)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_ERROR) << "Parsing " << server << " failed.";
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -0700117 return -1;
118 }
119 server_addresses.push_back(addr);
Guo-wei Shieh37931c42015-05-15 10:26:52 -0700120 }
121
122 rtc::InitializeSSL();
honghaiz34b11eb2016-03-16 08:55:44 -0700123 rtc::InitRandom(rtc::Time32());
Guo-wei Shieh37931c42015-05-15 10:26:52 -0700124 rtc::Thread* thread = rtc::ThreadManager::Instance()->WrapCurrentThread();
kwiberg3ec46792016-04-27 07:22:53 -0700125 std::unique_ptr<rtc::BasicPacketSocketFactory> socket_factory(
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700126 new rtc::BasicPacketSocketFactory());
kwiberg3ec46792016-04-27 07:22:53 -0700127 std::unique_ptr<rtc::BasicNetworkManager> network_manager(
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700128 new rtc::BasicNetworkManager());
129 rtc::NetworkManager::NetworkList networks;
130 network_manager->GetNetworks(&networks);
131 StunProber* prober =
132 new StunProber(socket_factory.get(), rtc::Thread::Current(), networks);
133 auto finish_callback = [thread](StunProber* prober, int result) {
134 StopTrial(thread, prober, result);
135 };
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -0700136 prober->Start(server_addresses, FLAG_shared_socket, FLAG_interval,
Guo-wei Shieh37931c42015-05-15 10:26:52 -0700137 FLAG_pings_per_ip, FLAG_timeout,
138 AsyncCallback(finish_callback));
139 thread->Run();
140 delete prober;
141 return 0;
142}