Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: webrtc/p2p/base/stunport.cc

Issue 1976683003: Update the type and cost of existing networks if its type is found later by network monitor (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Minor changes Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/p2p/base/stunport.h ('k') | webrtc/p2p/base/stunport_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 "webrtc/p2p/base/stunport.h" 11 #include "webrtc/p2p/base/stunport.h"
12 12
13 #include "webrtc/p2p/base/common.h" 13 #include "webrtc/p2p/base/common.h"
14 #include "webrtc/p2p/base/portallocator.h" 14 #include "webrtc/p2p/base/portallocator.h"
15 #include "webrtc/p2p/base/stun.h" 15 #include "webrtc/p2p/base/stun.h"
16 #include "webrtc/base/checks.h" 16 #include "webrtc/base/checks.h"
17 #include "webrtc/base/common.h" 17 #include "webrtc/base/common.h"
18 #include "webrtc/base/helpers.h" 18 #include "webrtc/base/helpers.h"
19 #include "webrtc/base/ipaddress.h" 19 #include "webrtc/base/ipaddress.h"
20 #include "webrtc/base/logging.h" 20 #include "webrtc/base/logging.h"
21 #include "webrtc/base/nethelpers.h" 21 #include "webrtc/base/nethelpers.h"
22 22
23 namespace cricket { 23 namespace cricket {
24 24
25 // TODO: Move these to a common place (used in relayport too) 25 // TODO: Move these to a common place (used in relayport too)
26 const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts 26 const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts
27 const int RETRY_TIMEOUT = 50 * 1000; // 50 seconds 27 const int RETRY_TIMEOUT = 50 * 1000; // 50 seconds
28 // Lifetime chosen for STUN ports on low-cost networks.
29 const int INFINITE_LIFETIME = -1;
30 // Lifetime for STUN ports on high-cost networks: 2 minutes
31 const int HIGH_COST_PORT_KEEPALIVE_LIFETIME = 2 * 60 * 1000;
32 28
33 // Handles a binding request sent to the STUN server. 29 // Handles a binding request sent to the STUN server.
34 class StunBindingRequest : public StunRequest { 30 class StunBindingRequest : public StunRequest {
35 public: 31 public:
36 StunBindingRequest(UDPPort* port, 32 StunBindingRequest(UDPPort* port,
37 const rtc::SocketAddress& addr, 33 const rtc::SocketAddress& addr,
38 int64_t start_time, 34 int64_t start_time)
39 int lifetime) 35 : port_(port), server_addr_(addr), start_time_(start_time) {}
40 : port_(port),
41 server_addr_(addr),
42 start_time_(start_time),
43 lifetime_(lifetime) {}
44 36
45 virtual ~StunBindingRequest() { 37 virtual ~StunBindingRequest() {
46 } 38 }
47 39
48 const rtc::SocketAddress& server_addr() const { return server_addr_; } 40 const rtc::SocketAddress& server_addr() const { return server_addr_; }
49 41
50 virtual void Prepare(StunMessage* request) override { 42 virtual void Prepare(StunMessage* request) override {
51 request->SetType(STUN_BINDING_REQUEST); 43 request->SetType(STUN_BINDING_REQUEST);
52 } 44 }
53 45
54 virtual void OnResponse(StunMessage* response) override { 46 virtual void OnResponse(StunMessage* response) override {
55 const StunAddressAttribute* addr_attr = 47 const StunAddressAttribute* addr_attr =
56 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS); 48 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
57 if (!addr_attr) { 49 if (!addr_attr) {
58 LOG(LS_ERROR) << "Binding response missing mapped address."; 50 LOG(LS_ERROR) << "Binding response missing mapped address.";
59 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 && 51 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 &&
60 addr_attr->family() != STUN_ADDRESS_IPV6) { 52 addr_attr->family() != STUN_ADDRESS_IPV6) {
61 LOG(LS_ERROR) << "Binding address has bad family"; 53 LOG(LS_ERROR) << "Binding address has bad family";
62 } else { 54 } else {
63 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port()); 55 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port());
64 port_->OnStunBindingRequestSucceeded(server_addr_, addr); 56 port_->OnStunBindingRequestSucceeded(server_addr_, addr);
65 } 57 }
66 58
67 // The keep-alive requests will be stopped after its lifetime has passed. 59 // The keep-alive requests will be stopped after its lifetime has passed.
68 if (WithinLifetime(rtc::TimeMillis())) { 60 if (WithinLifetime(rtc::TimeMillis())) {
69 port_->requests_.SendDelayed( 61 port_->requests_.SendDelayed(
70 new StunBindingRequest(port_, server_addr_, start_time_, lifetime_), 62 new StunBindingRequest(port_, server_addr_, start_time_),
71 port_->stun_keepalive_delay()); 63 port_->stun_keepalive_delay());
72 } 64 }
73 } 65 }
74 66
75 virtual void OnErrorResponse(StunMessage* response) override { 67 virtual void OnErrorResponse(StunMessage* response) override {
76 const StunErrorCodeAttribute* attr = response->GetErrorCode(); 68 const StunErrorCodeAttribute* attr = response->GetErrorCode();
77 if (!attr) { 69 if (!attr) {
78 LOG(LS_ERROR) << "Bad allocate response error code"; 70 LOG(LS_ERROR) << "Bad allocate response error code";
79 } else { 71 } else {
80 LOG(LS_ERROR) << "Binding error response:" 72 LOG(LS_ERROR) << "Binding error response:"
81 << " class=" << attr->eclass() 73 << " class=" << attr->eclass()
82 << " number=" << attr->number() << " reason='" 74 << " number=" << attr->number() << " reason='"
83 << attr->reason() << "'"; 75 << attr->reason() << "'";
84 } 76 }
85 77
86 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 78 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
87 79
88 int64_t now = rtc::TimeMillis(); 80 int64_t now = rtc::TimeMillis();
89 if (WithinLifetime(now) && 81 if (WithinLifetime(now) &&
90 rtc::TimeDiff(now, start_time_) < RETRY_TIMEOUT) { 82 rtc::TimeDiff(now, start_time_) < RETRY_TIMEOUT) {
91 port_->requests_.SendDelayed( 83 port_->requests_.SendDelayed(
92 new StunBindingRequest(port_, server_addr_, start_time_, lifetime_), 84 new StunBindingRequest(port_, server_addr_, start_time_),
93 port_->stun_keepalive_delay()); 85 port_->stun_keepalive_delay());
94 } 86 }
95 } 87 }
96 virtual void OnTimeout() override { 88 virtual void OnTimeout() override {
97 LOG(LS_ERROR) << "Binding request timed out from " 89 LOG(LS_ERROR) << "Binding request timed out from "
98 << port_->GetLocalAddress().ToSensitiveString() << " (" 90 << port_->GetLocalAddress().ToSensitiveString() << " ("
99 << port_->Network()->name() << ")"; 91 << port_->Network()->name() << ")";
100 92
101 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 93 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
102 } 94 }
103 95
104 private: 96 private:
105 // Returns true if |now| is within the lifetime of the request (a negative 97 // Returns true if |now| is within the lifetime of the request (a negative
106 // lifetime means infinite). 98 // lifetime means infinite).
107 bool WithinLifetime(int64_t now) const { 99 bool WithinLifetime(int64_t now) const {
108 return lifetime_ < 0 || rtc::TimeDiff(now, start_time_) <= lifetime_; 100 int lifetime = port_->stun_keepalive_lifetime();
101 return lifetime < 0 || rtc::TimeDiff(now, start_time_) <= lifetime;
109 } 102 }
110 UDPPort* port_; 103 UDPPort* port_;
111 const rtc::SocketAddress server_addr_; 104 const rtc::SocketAddress server_addr_;
112 105
113 int64_t start_time_; 106 int64_t start_time_;
114 // The time duration for which this request will be rescheduled.
115 int lifetime_;
116 }; 107 };
117 108
118 UDPPort::AddressResolver::AddressResolver( 109 UDPPort::AddressResolver::AddressResolver(
119 rtc::PacketSocketFactory* factory) 110 rtc::PacketSocketFactory* factory)
120 : socket_factory_(factory) {} 111 : socket_factory_(factory) {}
121 112
122 UDPPort::AddressResolver::~AddressResolver() { 113 UDPPort::AddressResolver::~AddressResolver() {
123 for (ResolverMap::iterator it = resolvers_.begin(); 114 for (ResolverMap::iterator it = resolvers_.begin();
124 it != resolvers_.end(); ++it) { 115 it != resolvers_.end(); ++it) {
125 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang 116 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 requests_(thread), 204 requests_(thread),
214 socket_(NULL), 205 socket_(NULL),
215 error_(0), 206 error_(0),
216 ready_(false), 207 ready_(false),
217 stun_keepalive_delay_(KEEPALIVE_DELAY), 208 stun_keepalive_delay_(KEEPALIVE_DELAY),
218 emit_local_for_anyaddress_(emit_local_for_anyaddress) { 209 emit_local_for_anyaddress_(emit_local_for_anyaddress) {
219 requests_.set_origin(origin); 210 requests_.set_origin(origin);
220 } 211 }
221 212
222 bool UDPPort::Init() { 213 bool UDPPort::Init() {
223 // If this is a zero-cost network, it will keep on sending STUN binding 214 stun_keepalive_lifetime_ = GetStunKeepaliveLifetime();
224 // requests indefinitely to keep the NAT binding alive. Otherwise, stop
225 // sending STUN binding requests after HIGH_COST_PORT_KEEPALIVE_LIFETIME.
226 stun_keepalive_lifetime_ = (network_cost() == 0)
227 ? INFINITE_LIFETIME
228 : HIGH_COST_PORT_KEEPALIVE_LIFETIME;
229 if (!SharedSocket()) { 215 if (!SharedSocket()) {
230 ASSERT(socket_ == NULL); 216 ASSERT(socket_ == NULL);
231 socket_ = socket_factory()->CreateUdpSocket( 217 socket_ = socket_factory()->CreateUdpSocket(
232 rtc::SocketAddress(ip(), 0), min_port(), max_port()); 218 rtc::SocketAddress(ip(), 0), min_port(), max_port());
233 if (!socket_) { 219 if (!socket_) {
234 LOG_J(LS_WARNING, this) << "UDP socket creation failed"; 220 LOG_J(LS_WARNING, this) << "UDP socket creation failed";
235 return false; 221 return false;
236 } 222 }
237 socket_->SignalReadPacket.connect(this, &UDPPort::OnReadPacket); 223 socket_->SignalReadPacket.connect(this, &UDPPort::OnReadPacket);
238 } 224 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 bool payload) { 278 bool payload) {
293 int sent = socket_->SendTo(data, size, addr, options); 279 int sent = socket_->SendTo(data, size, addr, options);
294 if (sent < 0) { 280 if (sent < 0) {
295 error_ = socket_->GetError(); 281 error_ = socket_->GetError();
296 LOG_J(LS_ERROR, this) << "UDP send of " << size 282 LOG_J(LS_ERROR, this) << "UDP send of " << size
297 << " bytes failed with error " << error_; 283 << " bytes failed with error " << error_;
298 } 284 }
299 return sent; 285 return sent;
300 } 286 }
301 287
288 void UDPPort::UpdateNetworkCost() {
289 Port::UpdateNetworkCost();
290 stun_keepalive_lifetime_ = GetStunKeepaliveLifetime();
291 }
292
302 int UDPPort::SetOption(rtc::Socket::Option opt, int value) { 293 int UDPPort::SetOption(rtc::Socket::Option opt, int value) {
303 return socket_->SetOption(opt, value); 294 return socket_->SetOption(opt, value);
304 } 295 }
305 296
306 int UDPPort::GetOption(rtc::Socket::Option opt, int* value) { 297 int UDPPort::GetOption(rtc::Socket::Option opt, int* value) {
307 return socket_->GetOption(opt, value); 298 return socket_->GetOption(opt, value);
308 } 299 }
309 300
310 int UDPPort::GetError() { 301 int UDPPort::GetError() {
311 return error_; 302 return error_;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 } 395 }
405 } 396 }
406 397
407 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) { 398 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) {
408 if (stun_addr.IsUnresolvedIP()) { 399 if (stun_addr.IsUnresolvedIP()) {
409 ResolveStunAddress(stun_addr); 400 ResolveStunAddress(stun_addr);
410 401
411 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { 402 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
412 // Check if |server_addr_| is compatible with the port's ip. 403 // Check if |server_addr_| is compatible with the port's ip.
413 if (IsCompatibleAddress(stun_addr)) { 404 if (IsCompatibleAddress(stun_addr)) {
414 requests_.Send(new StunBindingRequest(this, stun_addr, rtc::TimeMillis(), 405 requests_.Send(
415 stun_keepalive_lifetime_)); 406 new StunBindingRequest(this, stun_addr, rtc::TimeMillis()));
416 } else { 407 } else {
417 // Since we can't send stun messages to the server, we should mark this 408 // Since we can't send stun messages to the server, we should mark this
418 // port ready. 409 // port ready.
419 LOG(LS_WARNING) << "STUN server address is incompatible."; 410 LOG(LS_WARNING) << "STUN server address is incompatible.";
420 OnStunBindingOrResolveRequestFailed(stun_addr); 411 OnStunBindingOrResolveRequestFailed(stun_addr);
421 } 412 }
422 } 413 }
423 } 414 }
424 415
425 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const { 416 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 const std::vector<Candidate>& existing_candidates = Candidates(); 510 const std::vector<Candidate>& existing_candidates = Candidates();
520 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); 511 std::vector<Candidate>::const_iterator it = existing_candidates.begin();
521 for (; it != existing_candidates.end(); ++it) { 512 for (; it != existing_candidates.end(); ++it) {
522 if (it->address() == addr) 513 if (it->address() == addr)
523 return true; 514 return true;
524 } 515 }
525 return false; 516 return false;
526 } 517 }
527 518
528 } // namespace cricket 519 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/p2p/base/stunport.h ('k') | webrtc/p2p/base/stunport_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698