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