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