You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.5 KiB

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