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 <memory>
12 #include <set>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 
17 #include "absl/flags/flag.h"
18 #include "absl/flags/parse.h"
19 #include "p2p/base/basic_packet_socket_factory.h"
20 #include "p2p/stunprober/stun_prober.h"
21 #include "rtc_base/helpers.h"
22 #include "rtc_base/logging.h"
23 #include "rtc_base/network.h"
24 #include "rtc_base/socket_address.h"
25 #include "rtc_base/ssl_adapter.h"
26 #include "rtc_base/thread.h"
27 #include "rtc_base/time_utils.h"
28 
29 using stunprober::AsyncCallback;
30 using stunprober::StunProber;
31 
32 ABSL_FLAG(int,
33           interval,
34           10,
35           "Interval of consecutive stun pings in milliseconds");
36 ABSL_FLAG(bool,
37           shared_socket,
38           false,
39           "Share socket mode for different remote IPs");
40 ABSL_FLAG(int,
41           pings_per_ip,
42           10,
43           "Number of consecutive stun pings to send for each IP");
44 ABSL_FLAG(int,
45           timeout,
46           1000,
47           "Milliseconds of wait after the last ping sent before exiting");
48 ABSL_FLAG(
49     std::string,
50     servers,
51     "stun.l.google.com:19302,stun1.l.google.com:19302,stun2.l.google.com:19302",
52     "Comma separated STUN server addresses with ports");
53 
54 namespace {
55 
PrintNatType(stunprober::NatType type)56 const char* PrintNatType(stunprober::NatType type) {
57   switch (type) {
58     case stunprober::NATTYPE_NONE:
59       return "Not behind a NAT";
60     case stunprober::NATTYPE_UNKNOWN:
61       return "Unknown NAT type";
62     case stunprober::NATTYPE_SYMMETRIC:
63       return "Symmetric NAT";
64     case stunprober::NATTYPE_NON_SYMMETRIC:
65       return "Non-Symmetric NAT";
66     default:
67       return "Invalid";
68   }
69 }
70 
PrintStats(StunProber * prober)71 void PrintStats(StunProber* prober) {
72   StunProber::Stats stats;
73   if (!prober->GetStats(&stats)) {
74     RTC_LOG(LS_WARNING) << "Results are inconclusive.";
75     return;
76   }
77 
78   RTC_LOG(LS_INFO) << "Shared Socket Mode: " << stats.shared_socket_mode;
79   RTC_LOG(LS_INFO) << "Requests sent: " << stats.num_request_sent;
80   RTC_LOG(LS_INFO) << "Responses received: " << stats.num_response_received;
81   RTC_LOG(LS_INFO) << "Target interval (ns): "
82                    << stats.target_request_interval_ns;
83   RTC_LOG(LS_INFO) << "Actual interval (ns): "
84                    << stats.actual_request_interval_ns;
85   RTC_LOG(LS_INFO) << "NAT Type: " << PrintNatType(stats.nat_type);
86   RTC_LOG(LS_INFO) << "Host IP: " << stats.host_ip;
87   RTC_LOG(LS_INFO) << "Server-reflexive ips: ";
88   for (auto& ip : stats.srflx_addrs) {
89     RTC_LOG(LS_INFO) << "\t" << ip;
90   }
91 
92   RTC_LOG(LS_INFO) << "Success Precent: " << stats.success_percent;
93   RTC_LOG(LS_INFO) << "Response Latency:" << stats.average_rtt_ms;
94 }
95 
StopTrial(rtc::Thread * thread,StunProber * prober,int result)96 void StopTrial(rtc::Thread* thread, StunProber* prober, int result) {
97   thread->Quit();
98   if (prober) {
99     RTC_LOG(LS_INFO) << "Result: " << result;
100     if (result == StunProber::SUCCESS) {
101       PrintStats(prober);
102     }
103   }
104 }
105 
106 }  // namespace
107 
main(int argc,char * argv[])108 int main(int argc, char* argv[]) {
109   absl::ParseCommandLine(argc, argv);
110 
111   std::vector<rtc::SocketAddress> server_addresses;
112   std::istringstream servers(absl::GetFlag(FLAGS_servers));
113   std::string server;
114   while (getline(servers, server, ',')) {
115     rtc::SocketAddress addr;
116     if (!addr.FromString(server)) {
117       RTC_LOG(LS_ERROR) << "Parsing " << server << " failed.";
118       return -1;
119     }
120     server_addresses.push_back(addr);
121   }
122 
123   rtc::InitializeSSL();
124   rtc::InitRandom(rtc::Time32());
125   rtc::Thread* thread = rtc::ThreadManager::Instance()->WrapCurrentThread();
126   std::unique_ptr<rtc::BasicPacketSocketFactory> socket_factory(
127       new rtc::BasicPacketSocketFactory());
128   std::unique_ptr<rtc::BasicNetworkManager> network_manager(
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   };
137   prober->Start(server_addresses, absl::GetFlag(FLAGS_shared_socket),
138                 absl::GetFlag(FLAGS_interval),
139                 absl::GetFlag(FLAGS_pings_per_ip), absl::GetFlag(FLAGS_timeout),
140                 AsyncCallback(finish_callback));
141   thread->Run();
142   delete prober;
143   return 0;
144 }
145