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

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

Issue 1737493004: Keep on sending stun binding requests on zero-cost networks. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Change back to uint32_t timestamp Created 4 years, 9 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; // ICE says 50 secs 27 const int RETRY_TIMEOUT = 50 * 1000; // 50 seconds
28 // Stop sending STUN binding requests after this amount of time 28 // Lifetime chosen for STUN ports on low-cost networks.
29 // (in milliseconds) because the connection binding requests should keep 29 const int INFINITE_LIFETIME = -1;
30 // the NAT binding alive. 30 // Lifetime for STUN ports on high-cost networks: 2 minutes
31 const int KEEP_ALIVE_TIMEOUT = 2 * 60 * 1000; // 2 minutes 31 const int HIGH_COST_PORT_KEEPALIVE_LIFETIME = 2 * 60 * 1000;
32 32
33 // Handles a binding request sent to the STUN server. 33 // Handles a binding request sent to the STUN server.
34 class StunBindingRequest : public StunRequest { 34 class StunBindingRequest : public StunRequest {
35 public: 35 public:
36 StunBindingRequest(UDPPort* port, 36 StunBindingRequest(UDPPort* port,
37 const rtc::SocketAddress& addr, 37 const rtc::SocketAddress& addr,
38 uint32_t deadline) 38 uint32_t start_time,
39 : port_(port), server_addr_(addr), deadline_(deadline) { 39 int lifetime)
40 start_time_ = rtc::Time(); 40 : port_(port),
41 } 41 server_addr_(addr),
42 start_time_(start_time),
43 lifetime_(lifetime) {}
42 44
43 virtual ~StunBindingRequest() { 45 virtual ~StunBindingRequest() {
44 } 46 }
45 47
46 const rtc::SocketAddress& server_addr() const { return server_addr_; } 48 const rtc::SocketAddress& server_addr() const { return server_addr_; }
47 49
48 virtual void Prepare(StunMessage* request) override { 50 virtual void Prepare(StunMessage* request) override {
49 request->SetType(STUN_BINDING_REQUEST); 51 request->SetType(STUN_BINDING_REQUEST);
50 } 52 }
51 53
52 virtual void OnResponse(StunMessage* response) override { 54 virtual void OnResponse(StunMessage* response) override {
53 const StunAddressAttribute* addr_attr = 55 const StunAddressAttribute* addr_attr =
54 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS); 56 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
55 if (!addr_attr) { 57 if (!addr_attr) {
56 LOG(LS_ERROR) << "Binding response missing mapped address."; 58 LOG(LS_ERROR) << "Binding response missing mapped address.";
57 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 && 59 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 &&
58 addr_attr->family() != STUN_ADDRESS_IPV6) { 60 addr_attr->family() != STUN_ADDRESS_IPV6) {
59 LOG(LS_ERROR) << "Binding address has bad family"; 61 LOG(LS_ERROR) << "Binding address has bad family";
60 } else { 62 } else {
61 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port()); 63 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port());
62 port_->OnStunBindingRequestSucceeded(server_addr_, addr); 64 port_->OnStunBindingRequestSucceeded(server_addr_, addr);
63 } 65 }
64 66
65 // We will do a keep-alive regardless of whether this request succeeds. 67 // The keep-alive requests will be stopped after its lifetime has passed.
66 // It will be stopped after |deadline_| mostly to conserve the battery life. 68 if (WithinLifetime(rtc::Time())) {
67 if (rtc::Time() <= deadline_) {
68 port_->requests_.SendDelayed( 69 port_->requests_.SendDelayed(
69 new StunBindingRequest(port_, server_addr_, deadline_), 70 new StunBindingRequest(port_, server_addr_, start_time_, lifetime_),
70 port_->stun_keepalive_delay()); 71 port_->stun_keepalive_delay());
71 } 72 }
72 } 73 }
73 74
74 virtual void OnErrorResponse(StunMessage* response) override { 75 virtual void OnErrorResponse(StunMessage* response) override {
75 const StunErrorCodeAttribute* attr = response->GetErrorCode(); 76 const StunErrorCodeAttribute* attr = response->GetErrorCode();
76 if (!attr) { 77 if (!attr) {
77 LOG(LS_ERROR) << "Bad allocate response error code"; 78 LOG(LS_ERROR) << "Bad allocate response error code";
78 } else { 79 } else {
79 LOG(LS_ERROR) << "Binding error response:" 80 LOG(LS_ERROR) << "Binding error response:"
80 << " class=" << attr->eclass() 81 << " class=" << attr->eclass()
81 << " number=" << attr->number() 82 << " number=" << attr->number() << " reason='"
82 << " reason='" << attr->reason() << "'"; 83 << attr->reason() << "'";
83 } 84 }
84 85
85 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 86 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
86 87
87 uint32_t now = rtc::Time(); 88 uint32_t now = rtc::Time();
88 if (now <= deadline_ && rtc::TimeDiff(now, start_time_) <= RETRY_TIMEOUT) { 89 if (WithinLifetime(now) &&
90 rtc::TimeDiff(now, start_time_) < RETRY_TIMEOUT) {
89 port_->requests_.SendDelayed( 91 port_->requests_.SendDelayed(
90 new StunBindingRequest(port_, server_addr_, deadline_), 92 new StunBindingRequest(port_, server_addr_, start_time_, lifetime_),
91 port_->stun_keepalive_delay()); 93 port_->stun_keepalive_delay());
92 } 94 }
93 } 95 }
94
95 virtual void OnTimeout() override { 96 virtual void OnTimeout() override {
96 LOG(LS_ERROR) << "Binding request timed out from " 97 LOG(LS_ERROR) << "Binding request timed out from "
97 << port_->GetLocalAddress().ToSensitiveString() 98 << port_->GetLocalAddress().ToSensitiveString() << " ("
98 << " (" << port_->Network()->name() << ")"; 99 << port_->Network()->name() << ")";
99 100
100 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 101 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
101 } 102 }
102 103
103 private: 104 private:
105 // Returns true if |now| is within the lifetime of the request (a negative
106 // lifetime means infinite).
107 bool WithinLifetime(uint32_t now) const {
108 return lifetime_ < 0 || rtc::TimeDiff(now, start_time_) <= lifetime_;
109 }
104 UDPPort* port_; 110 UDPPort* port_;
105 const rtc::SocketAddress server_addr_; 111 const rtc::SocketAddress server_addr_;
112
106 uint32_t start_time_; 113 uint32_t start_time_;
107 uint32_t deadline_; 114 // The time duration for which this request will be rescheduled.
115 int lifetime_;
108 }; 116 };
109 117
110 UDPPort::AddressResolver::AddressResolver( 118 UDPPort::AddressResolver::AddressResolver(
111 rtc::PacketSocketFactory* factory) 119 rtc::PacketSocketFactory* factory)
112 : socket_factory_(factory) {} 120 : socket_factory_(factory) {}
113 121
114 UDPPort::AddressResolver::~AddressResolver() { 122 UDPPort::AddressResolver::~AddressResolver() {
115 for (ResolverMap::iterator it = resolvers_.begin(); 123 for (ResolverMap::iterator it = resolvers_.begin();
116 it != resolvers_.end(); ++it) { 124 it != resolvers_.end(); ++it) {
117 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang 125 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 requests_(thread), 213 requests_(thread),
206 socket_(NULL), 214 socket_(NULL),
207 error_(0), 215 error_(0),
208 ready_(false), 216 ready_(false),
209 stun_keepalive_delay_(KEEPALIVE_DELAY), 217 stun_keepalive_delay_(KEEPALIVE_DELAY),
210 emit_local_for_anyaddress_(emit_local_for_anyaddress) { 218 emit_local_for_anyaddress_(emit_local_for_anyaddress) {
211 requests_.set_origin(origin); 219 requests_.set_origin(origin);
212 } 220 }
213 221
214 bool UDPPort::Init() { 222 bool UDPPort::Init() {
223 // If this is a zero-cost network, it will keep on sending STUN binding
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;
215 if (!SharedSocket()) { 229 if (!SharedSocket()) {
216 ASSERT(socket_ == NULL); 230 ASSERT(socket_ == NULL);
217 socket_ = socket_factory()->CreateUdpSocket( 231 socket_ = socket_factory()->CreateUdpSocket(
218 rtc::SocketAddress(ip(), 0), min_port(), max_port()); 232 rtc::SocketAddress(ip(), 0), min_port(), max_port());
219 if (!socket_) { 233 if (!socket_) {
220 LOG_J(LS_WARNING, this) << "UDP socket creation failed"; 234 LOG_J(LS_WARNING, this) << "UDP socket creation failed";
221 return false; 235 return false;
222 } 236 }
223 socket_->SignalReadPacket.connect(this, &UDPPort::OnReadPacket); 237 socket_->SignalReadPacket.connect(this, &UDPPort::OnReadPacket);
224 } 238 }
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 } 404 }
391 } 405 }
392 406
393 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) { 407 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) {
394 if (stun_addr.IsUnresolvedIP()) { 408 if (stun_addr.IsUnresolvedIP()) {
395 ResolveStunAddress(stun_addr); 409 ResolveStunAddress(stun_addr);
396 410
397 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { 411 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
398 // Check if |server_addr_| is compatible with the port's ip. 412 // Check if |server_addr_| is compatible with the port's ip.
399 if (IsCompatibleAddress(stun_addr)) { 413 if (IsCompatibleAddress(stun_addr)) {
400 requests_.Send(new StunBindingRequest(this, stun_addr, 414 requests_.Send(new StunBindingRequest(this, stun_addr, rtc::Time(),
401 rtc::Time() + KEEP_ALIVE_TIMEOUT)); 415 stun_keepalive_lifetime_));
402 } else { 416 } else {
403 // Since we can't send stun messages to the server, we should mark this 417 // Since we can't send stun messages to the server, we should mark this
404 // port ready. 418 // port ready.
405 LOG(LS_WARNING) << "STUN server address is incompatible."; 419 LOG(LS_WARNING) << "STUN server address is incompatible.";
406 OnStunBindingOrResolveRequestFailed(stun_addr); 420 OnStunBindingOrResolveRequestFailed(stun_addr);
407 } 421 }
408 } 422 }
409 } 423 }
410 424
411 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const { 425 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 const std::vector<Candidate>& existing_candidates = Candidates(); 519 const std::vector<Candidate>& existing_candidates = Candidates();
506 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); 520 std::vector<Candidate>::const_iterator it = existing_candidates.begin();
507 for (; it != existing_candidates.end(); ++it) { 521 for (; it != existing_candidates.end(); ++it) {
508 if (it->address() == addr) 522 if (it->address() == addr)
509 return true; 523 return true;
510 } 524 }
511 return false; 525 return false;
512 } 526 }
513 527
514 } // namespace cricket 528 } // 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