| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <stdlib.h> | 12 #include <stdlib.h> |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include <iostream> | 15 #include <iostream> |
| 16 #include <map> | 16 #include <map> |
| 17 | |
| 18 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/flags.h" | 18 #include "webrtc/base/flags.h" |
| 20 #include "webrtc/base/helpers.h" | 19 #include "webrtc/base/helpers.h" |
| 21 #include "webrtc/base/nethelpers.h" | 20 #include "webrtc/base/nethelpers.h" |
| 21 #include "webrtc/base/network.h" |
| 22 #include "webrtc/base/logging.h" | 22 #include "webrtc/base/logging.h" |
| 23 #include "webrtc/base/scoped_ptr.h" | 23 #include "webrtc/base/scoped_ptr.h" |
| 24 #include "webrtc/base/ssladapter.h" | 24 #include "webrtc/base/ssladapter.h" |
| 25 #include "webrtc/base/stringutils.h" | 25 #include "webrtc/base/stringutils.h" |
| 26 #include "webrtc/base/thread.h" | 26 #include "webrtc/base/thread.h" |
| 27 #include "webrtc/base/timeutils.h" | 27 #include "webrtc/base/timeutils.h" |
| 28 #include "webrtc/p2p/base/basicpacketsocketfactory.cc" |
| 28 #include "webrtc/p2p/stunprober/stunprober.h" | 29 #include "webrtc/p2p/stunprober/stunprober.h" |
| 29 #include "webrtc/p2p/stunprober/stunprober_dependencies.h" | |
| 30 | 30 |
| 31 using stunprober::HostNameResolverInterface; | |
| 32 using stunprober::TaskRunner; | |
| 33 using stunprober::SocketFactory; | |
| 34 using stunprober::StunProber; | 31 using stunprober::StunProber; |
| 35 using stunprober::AsyncCallback; | 32 using stunprober::AsyncCallback; |
| 36 using stunprober::ClientSocketInterface; | |
| 37 using stunprober::ServerSocketInterface; | |
| 38 using stunprober::SocketFactory; | |
| 39 using stunprober::TaskRunner; | |
| 40 | 33 |
| 41 DEFINE_bool(help, false, "Prints this message"); | 34 DEFINE_bool(help, false, "Prints this message"); |
| 42 DEFINE_int(interval, 10, "Interval of consecutive stun pings in milliseconds"); | 35 DEFINE_int(interval, 10, "Interval of consecutive stun pings in milliseconds"); |
| 43 DEFINE_bool(shared_socket, false, "Share socket mode for different remote IPs"); | 36 DEFINE_bool(shared_socket, false, "Share socket mode for different remote IPs"); |
| 44 DEFINE_int(pings_per_ip, | 37 DEFINE_int(pings_per_ip, |
| 45 10, | 38 10, |
| 46 "Number of consecutive stun pings to send for each IP"); | 39 "Number of consecutive stun pings to send for each IP"); |
| 47 DEFINE_int(timeout, | 40 DEFINE_int(timeout, |
| 48 1000, | 41 1000, |
| 49 "Milliseconds of wait after the last ping sent before exiting"); | 42 "Milliseconds of wait after the last ping sent before exiting"); |
| 50 DEFINE_string( | 43 DEFINE_string( |
| 51 servers, | 44 servers, |
| 52 "stun.l.google.com:19302,stun1.l.google.com:19302,stun2.l.google.com:19302", | 45 "stun.l.google.com:19302,stun1.l.google.com:19302,stun2.l.google.com:19302", |
| 53 "Comma separated STUN server addresses with ports"); | 46 "Comma separated STUN server addresses with ports"); |
| 54 | 47 |
| 55 namespace { | 48 namespace { |
| 56 | 49 |
| 57 class HostNameResolver : public HostNameResolverInterface, | |
| 58 public sigslot::has_slots<> { | |
| 59 public: | |
| 60 HostNameResolver() {} | |
| 61 virtual ~HostNameResolver() {} | |
| 62 | |
| 63 void Resolve(const rtc::SocketAddress& addr, | |
| 64 std::vector<rtc::SocketAddress>* addresses, | |
| 65 AsyncCallback callback) override { | |
| 66 resolver_ = new rtc::AsyncResolver(); | |
| 67 DCHECK(callback_.empty()); | |
| 68 addr_ = addr; | |
| 69 callback_ = callback; | |
| 70 result_ = addresses; | |
| 71 resolver_->SignalDone.connect(this, &HostNameResolver::OnResolveResult); | |
| 72 resolver_->Start(addr); | |
| 73 } | |
| 74 | |
| 75 void OnResolveResult(rtc::AsyncResolverInterface* resolver) { | |
| 76 DCHECK(resolver); | |
| 77 int rv = resolver_->GetError(); | |
| 78 LOG(LS_INFO) << "ResolveResult for " << addr_.ToString() << " : " << rv; | |
| 79 if (rv == 0 && result_) { | |
| 80 for (auto addr : resolver_->addresses()) { | |
| 81 rtc::SocketAddress ip(addr, addr_.port()); | |
| 82 result_->push_back(ip); | |
| 83 LOG(LS_INFO) << "\t" << ip.ToString(); | |
| 84 } | |
| 85 } | |
| 86 if (!callback_.empty()) { | |
| 87 // Need to be the last statement as the object could be deleted by the | |
| 88 // callback_ in the failure case. | |
| 89 AsyncCallback callback = callback_; | |
| 90 callback_ = AsyncCallback(); | |
| 91 | |
| 92 // rtc::AsyncResolver inherits from SignalThread which requires explicit | |
| 93 // Release(). | |
| 94 resolver_->Release(); | |
| 95 resolver_ = nullptr; | |
| 96 callback(rv); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 AsyncCallback callback_; | |
| 102 rtc::SocketAddress addr_; | |
| 103 std::vector<rtc::SocketAddress>* result_; | |
| 104 | |
| 105 // Not using smart ptr here as this requires specific release pattern. | |
| 106 rtc::AsyncResolver* resolver_; | |
| 107 }; | |
| 108 | |
| 109 const char* PrintNatType(stunprober::NatType type) { | 50 const char* PrintNatType(stunprober::NatType type) { |
| 110 switch (type) { | 51 switch (type) { |
| 111 case stunprober::NATTYPE_NONE: | 52 case stunprober::NATTYPE_NONE: |
| 112 return "Not behind a NAT"; | 53 return "Not behind a NAT"; |
| 113 case stunprober::NATTYPE_UNKNOWN: | 54 case stunprober::NATTYPE_UNKNOWN: |
| 114 return "Unknown NAT type"; | 55 return "Unknown NAT type"; |
| 115 case stunprober::NATTYPE_SYMMETRIC: | 56 case stunprober::NATTYPE_SYMMETRIC: |
| 116 return "Symmetric NAT"; | 57 return "Symmetric NAT"; |
| 117 case stunprober::NATTYPE_NON_SYMMETRIC: | 58 case stunprober::NATTYPE_NON_SYMMETRIC: |
| 118 return "Non-Symmetric NAT"; | 59 return "Non-Symmetric NAT"; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 if (!addr.FromString(server)) { | 112 if (!addr.FromString(server)) { |
| 172 LOG(LS_ERROR) << "Parsing " << server << " failed."; | 113 LOG(LS_ERROR) << "Parsing " << server << " failed."; |
| 173 return -1; | 114 return -1; |
| 174 } | 115 } |
| 175 server_addresses.push_back(addr); | 116 server_addresses.push_back(addr); |
| 176 } | 117 } |
| 177 | 118 |
| 178 rtc::InitializeSSL(); | 119 rtc::InitializeSSL(); |
| 179 rtc::InitRandom(rtc::Time()); | 120 rtc::InitRandom(rtc::Time()); |
| 180 rtc::Thread* thread = rtc::ThreadManager::Instance()->WrapCurrentThread(); | 121 rtc::Thread* thread = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 181 StunProber* prober = new StunProber(new HostNameResolver(), | 122 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> socket_factory( |
| 182 new SocketFactory(), new TaskRunner()); | 123 new rtc::BasicPacketSocketFactory()); |
| 183 auto finish_callback = | 124 rtc::scoped_ptr<rtc::BasicNetworkManager> network_manager( |
| 184 [thread, prober](int result) { StopTrial(thread, prober, result); }; | 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 }; |
| 185 prober->Start(server_addresses, FLAG_shared_socket, FLAG_interval, | 133 prober->Start(server_addresses, FLAG_shared_socket, FLAG_interval, |
| 186 FLAG_pings_per_ip, FLAG_timeout, | 134 FLAG_pings_per_ip, FLAG_timeout, |
| 187 AsyncCallback(finish_callback)); | 135 AsyncCallback(finish_callback)); |
| 188 thread->Run(); | 136 thread->Run(); |
| 189 delete prober; | 137 delete prober; |
| 190 return 0; | 138 return 0; |
| 191 } | 139 } |
| OLD | NEW |