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

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

Issue 1465843004: Stop sending stun binding requests after certain amount of time. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Created 5 years 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 | « no previous file | no next file » | 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_DELAY = 50; // 50ms, from ICE spec
28 const int RETRY_TIMEOUT = 50 * 1000; // ICE says 50 secs 27 const int RETRY_TIMEOUT = 50 * 1000; // ICE says 50 secs
28 // Stop sending STUN binding requests after this amount of time
29 // (in milliseconds) because the connection binding requests should keep
30 // the NAT binding alive.
31 const int KEEP_ALIVE_TIMEOUT = 2 * 60 * 1000; // 2 minutes
29 32
30 // Handles a binding request sent to the STUN server. 33 // Handles a binding request sent to the STUN server.
31 class StunBindingRequest : public StunRequest { 34 class StunBindingRequest : public StunRequest {
32 public: 35 public:
33 StunBindingRequest(UDPPort* port, bool keep_alive, 36 StunBindingRequest(UDPPort* port,
34 const rtc::SocketAddress& addr) 37 const rtc::SocketAddress& addr,
35 : port_(port), keep_alive_(keep_alive), server_addr_(addr) { 38 uint32_t deadline)
39 : port_(port), server_addr_(addr), deadline_(deadline) {
36 start_time_ = rtc::Time(); 40 start_time_ = rtc::Time();
37 } 41 }
38 42
39 virtual ~StunBindingRequest() { 43 virtual ~StunBindingRequest() {
40 } 44 }
41 45
42 const rtc::SocketAddress& server_addr() const { return server_addr_; } 46 const rtc::SocketAddress& server_addr() const { return server_addr_; }
43 47
44 virtual void Prepare(StunMessage* request) override { 48 virtual void Prepare(StunMessage* request) override {
45 request->SetType(STUN_BINDING_REQUEST); 49 request->SetType(STUN_BINDING_REQUEST);
46 } 50 }
47 51
48 virtual void OnResponse(StunMessage* response) override { 52 virtual void OnResponse(StunMessage* response) override {
49 const StunAddressAttribute* addr_attr = 53 const StunAddressAttribute* addr_attr =
50 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS); 54 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
51 if (!addr_attr) { 55 if (!addr_attr) {
52 LOG(LS_ERROR) << "Binding response missing mapped address."; 56 LOG(LS_ERROR) << "Binding response missing mapped address.";
53 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 && 57 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 &&
54 addr_attr->family() != STUN_ADDRESS_IPV6) { 58 addr_attr->family() != STUN_ADDRESS_IPV6) {
55 LOG(LS_ERROR) << "Binding address has bad family"; 59 LOG(LS_ERROR) << "Binding address has bad family";
56 } else { 60 } else {
57 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port()); 61 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port());
58 port_->OnStunBindingRequestSucceeded(server_addr_, addr); 62 port_->OnStunBindingRequestSucceeded(server_addr_, addr);
59 } 63 }
60 64
61 // We will do a keep-alive regardless of whether this request succeeds. 65 // We will do a keep-alive regardless of whether this request succeeds.
62 // This should have almost no impact on network usage. 66 // It will be stopped after |deadline_| mostly to conserve the battery life.
63 if (keep_alive_) { 67 if (rtc::Time() <= deadline_) {
64 port_->requests_.SendDelayed( 68 port_->requests_.SendDelayed(
65 new StunBindingRequest(port_, true, server_addr_), 69 new StunBindingRequest(port_, server_addr_, deadline_),
66 port_->stun_keepalive_delay()); 70 port_->stun_keepalive_delay());
67 } 71 }
68 } 72 }
69 73
70 virtual void OnErrorResponse(StunMessage* response) override { 74 virtual void OnErrorResponse(StunMessage* response) override {
71 const StunErrorCodeAttribute* attr = response->GetErrorCode(); 75 const StunErrorCodeAttribute* attr = response->GetErrorCode();
72 if (!attr) { 76 if (!attr) {
73 LOG(LS_ERROR) << "Bad allocate response error code"; 77 LOG(LS_ERROR) << "Bad allocate response error code";
74 } else { 78 } else {
75 LOG(LS_ERROR) << "Binding error response:" 79 LOG(LS_ERROR) << "Binding error response:"
76 << " class=" << attr->eclass() 80 << " class=" << attr->eclass()
77 << " number=" << attr->number() 81 << " number=" << attr->number()
78 << " reason='" << attr->reason() << "'"; 82 << " reason='" << attr->reason() << "'";
79 } 83 }
80 84
81 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 85 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
82 86
83 if (keep_alive_ 87 uint32_t now = rtc::Time();
84 && (rtc::TimeSince(start_time_) <= RETRY_TIMEOUT)) { 88 if (now <= deadline_ && rtc::TimeDiff(now, start_time_) <= RETRY_TIMEOUT) {
85 port_->requests_.SendDelayed( 89 port_->requests_.SendDelayed(
86 new StunBindingRequest(port_, true, server_addr_), 90 new StunBindingRequest(port_, server_addr_, deadline_),
87 port_->stun_keepalive_delay()); 91 port_->stun_keepalive_delay());
88 } 92 }
89 } 93 }
90 94
91 virtual void OnTimeout() override { 95 virtual void OnTimeout() override {
92 LOG(LS_ERROR) << "Binding request timed out from " 96 LOG(LS_ERROR) << "Binding request timed out from "
93 << port_->GetLocalAddress().ToSensitiveString() 97 << port_->GetLocalAddress().ToSensitiveString()
94 << " (" << port_->Network()->name() << ")"; 98 << " (" << port_->Network()->name() << ")";
95 99
96 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 100 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
97
98 if (keep_alive_
99 && (rtc::TimeSince(start_time_) <= RETRY_TIMEOUT)) {
100 port_->requests_.SendDelayed(
101 new StunBindingRequest(port_, true, server_addr_),
102 RETRY_DELAY);
103 }
104 } 101 }
105 102
106 private: 103 private:
107 UDPPort* port_; 104 UDPPort* port_;
108 bool keep_alive_;
109 const rtc::SocketAddress server_addr_; 105 const rtc::SocketAddress server_addr_;
110 uint32_t start_time_; 106 uint32_t start_time_;
107 uint32_t deadline_;
111 }; 108 };
112 109
113 UDPPort::AddressResolver::AddressResolver( 110 UDPPort::AddressResolver::AddressResolver(
114 rtc::PacketSocketFactory* factory) 111 rtc::PacketSocketFactory* factory)
115 : socket_factory_(factory) {} 112 : socket_factory_(factory) {}
116 113
117 UDPPort::AddressResolver::~AddressResolver() { 114 UDPPort::AddressResolver::~AddressResolver() {
118 for (ResolverMap::iterator it = resolvers_.begin(); 115 for (ResolverMap::iterator it = resolvers_.begin();
119 it != resolvers_.end(); ++it) { 116 it != resolvers_.end(); ++it) {
120 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang 117 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 const rtc::SentPacket& sent_packet) { 341 const rtc::SentPacket& sent_packet) {
345 Port::OnSentPacket(sent_packet); 342 Port::OnSentPacket(sent_packet);
346 } 343 }
347 344
348 void UDPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) { 345 void UDPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
349 Port::OnReadyToSend(); 346 Port::OnReadyToSend();
350 } 347 }
351 348
352 void UDPPort::SendStunBindingRequests() { 349 void UDPPort::SendStunBindingRequests() {
353 // We will keep pinging the stun server to make sure our NAT pin-hole stays 350 // We will keep pinging the stun server to make sure our NAT pin-hole stays
354 // open during the call. 351 // open until the deadline (specified in SendStunBindingRequest).
355 ASSERT(requests_.empty()); 352 ASSERT(requests_.empty());
356 353
357 for (ServerAddresses::const_iterator it = server_addresses_.begin(); 354 for (ServerAddresses::const_iterator it = server_addresses_.begin();
358 it != server_addresses_.end(); ++it) { 355 it != server_addresses_.end(); ++it) {
359 SendStunBindingRequest(*it); 356 SendStunBindingRequest(*it);
360 } 357 }
361 } 358 }
362 359
363 void UDPPort::ResolveStunAddress(const rtc::SocketAddress& stun_addr) { 360 void UDPPort::ResolveStunAddress(const rtc::SocketAddress& stun_addr) {
364 if (!resolver_) { 361 if (!resolver_) {
(...skipping 25 matching lines...) Expand all
390 } 387 }
391 } 388 }
392 389
393 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) { 390 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) {
394 if (stun_addr.IsUnresolvedIP()) { 391 if (stun_addr.IsUnresolvedIP()) {
395 ResolveStunAddress(stun_addr); 392 ResolveStunAddress(stun_addr);
396 393
397 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { 394 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
398 // Check if |server_addr_| is compatible with the port's ip. 395 // Check if |server_addr_| is compatible with the port's ip.
399 if (IsCompatibleAddress(stun_addr)) { 396 if (IsCompatibleAddress(stun_addr)) {
400 requests_.Send(new StunBindingRequest(this, true, stun_addr)); 397 requests_.Send(new StunBindingRequest(this, stun_addr,
398 rtc::Time() + KEEP_ALIVE_TIMEOUT));
401 } else { 399 } else {
402 // Since we can't send stun messages to the server, we should mark this 400 // Since we can't send stun messages to the server, we should mark this
403 // port ready. 401 // port ready.
404 LOG(LS_WARNING) << "STUN server address is incompatible."; 402 LOG(LS_WARNING) << "STUN server address is incompatible.";
405 OnStunBindingOrResolveRequestFailed(stun_addr); 403 OnStunBindingOrResolveRequestFailed(stun_addr);
406 } 404 }
407 } 405 }
408 } 406 }
409 407
410 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const { 408 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 const std::vector<Candidate>& existing_candidates = Candidates(); 502 const std::vector<Candidate>& existing_candidates = Candidates();
505 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); 503 std::vector<Candidate>::const_iterator it = existing_candidates.begin();
506 for (; it != existing_candidates.end(); ++it) { 504 for (; it != existing_candidates.end(); ++it) {
507 if (it->address() == addr) 505 if (it->address() == addr)
508 return true; 506 return true;
509 } 507 }
510 return false; 508 return false;
511 } 509 }
512 510
513 } // namespace cricket 511 } // namespace cricket
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698