blob: 8c3b424f82424b54c9822d010fca3d9e766839b5 [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
15#include <iostream>
16#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070017#include <memory>
18
wjywbs5a601d92016-08-31 14:03:52 -070019#include "webrtc/p2p/base/basicpacketsocketfactory.h"
Guo-wei Shieh37931c42015-05-15 10:26:52 -070020#include "webrtc/p2p/stunprober/stunprober.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/checks.h"
22#include "webrtc/rtc_base/flags.h"
23#include "webrtc/rtc_base/helpers.h"
24#include "webrtc/rtc_base/logging.h"
25#include "webrtc/rtc_base/nethelpers.h"
26#include "webrtc/rtc_base/network.h"
27#include "webrtc/rtc_base/ssladapter.h"
28#include "webrtc/rtc_base/stringutils.h"
29#include "webrtc/rtc_base/thread.h"
30#include "webrtc/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)) {
69 LOG(LS_WARNING) << "Results are inconclusive.";
70 return;
71 }
72
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -070073 LOG(LS_INFO) << "Shared Socket Mode: " << stats.shared_socket_mode;
Guo-wei Shieh37931c42015-05-15 10:26:52 -070074 LOG(LS_INFO) << "Requests sent: " << stats.num_request_sent;
75 LOG(LS_INFO) << "Responses received: " << stats.num_response_received;
76 LOG(LS_INFO) << "Target interval (ns): " << stats.target_request_interval_ns;
77 LOG(LS_INFO) << "Actual interval (ns): " << stats.actual_request_interval_ns;
Guo-wei Shieh72e9f042015-06-08 21:03:48 -070078 LOG(LS_INFO) << "NAT Type: " << PrintNatType(stats.nat_type);
Guo-wei Shieh37931c42015-05-15 10:26:52 -070079 LOG(LS_INFO) << "Host IP: " << stats.host_ip;
80 LOG(LS_INFO) << "Server-reflexive ips: ";
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -070081 for (auto& ip : stats.srflx_addrs) {
Guo-wei Shieh37931c42015-05-15 10:26:52 -070082 LOG(LS_INFO) << "\t" << ip;
83 }
84
Guo-wei Shieh72e9f042015-06-08 21:03:48 -070085 LOG(LS_INFO) << "Success Precent: " << stats.success_percent;
86 LOG(LS_INFO) << "Response Latency:" << stats.average_rtt_ms;
Guo-wei Shieh37931c42015-05-15 10:26:52 -070087}
88
89void StopTrial(rtc::Thread* thread, StunProber* prober, int result) {
90 thread->Quit();
91 if (prober) {
92 LOG(LS_INFO) << "Result: " << result;
93 if (result == StunProber::SUCCESS) {
94 PrintStats(prober);
95 }
96 }
97}
98
99} // namespace
100
101int main(int argc, char** argv) {
102 rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
103 if (FLAG_help) {
104 rtc::FlagList::Print(nullptr, false);
105 return 0;
106 }
107
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -0700108 std::vector<rtc::SocketAddress> server_addresses;
109 std::istringstream servers(FLAG_servers);
110 std::string server;
111 while (getline(servers, server, ',')) {
112 rtc::SocketAddress addr;
113 if (!addr.FromString(server)) {
114 LOG(LS_ERROR) << "Parsing " << server << " failed.";
115 return -1;
116 }
117 server_addresses.push_back(addr);
Guo-wei Shieh37931c42015-05-15 10:26:52 -0700118 }
119
120 rtc::InitializeSSL();
honghaiz34b11eb2016-03-16 08:55:44 -0700121 rtc::InitRandom(rtc::Time32());
Guo-wei Shieh37931c42015-05-15 10:26:52 -0700122 rtc::Thread* thread = rtc::ThreadManager::Instance()->WrapCurrentThread();
kwiberg3ec46792016-04-27 07:22:53 -0700123 std::unique_ptr<rtc::BasicPacketSocketFactory> socket_factory(
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700124 new rtc::BasicPacketSocketFactory());
kwiberg3ec46792016-04-27 07:22:53 -0700125 std::unique_ptr<rtc::BasicNetworkManager> network_manager(
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700126 new rtc::BasicNetworkManager());
127 rtc::NetworkManager::NetworkList networks;
128 network_manager->GetNetworks(&networks);
129 StunProber* prober =
130 new StunProber(socket_factory.get(), rtc::Thread::Current(), networks);
131 auto finish_callback = [thread](StunProber* prober, int result) {
132 StopTrial(thread, prober, result);
133 };
Guo-wei Shieh1ab67ae2015-05-18 21:36:20 -0700134 prober->Start(server_addresses, FLAG_shared_socket, FLAG_interval,
Guo-wei Shieh37931c42015-05-15 10:26:52 -0700135 FLAG_pings_per_ip, FLAG_timeout,
136 AsyncCallback(finish_callback));
137 thread->Run();
138 delete prober;
139 return 0;
140}