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

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 | « webrtc/p2p/base/stunport.h ('k') | 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 request after this amount of time (in milliseconds)
pthatcher1 2015/12/03 21:00:19 request => requests
honghaiz3 2015/12/03 22:21:21 Done.
29 // because the connection binding requests should keep the NAT binding alive.
30 const int KEEP_ALIVE_DEADLINE = 120 * 1000;
pthatcher1 2015/12/03 21:00:19 2 * 60 * 1000; // 2 minutes Might be more clear
honghaiz3 2015/12/03 22:21:21 Done.
29 31
30 // Handles a binding request sent to the STUN server. 32 // Handles a binding request sent to the STUN server.
31 class StunBindingRequest : public StunRequest { 33 class StunBindingRequest : public StunRequest {
32 public: 34 public:
33 StunBindingRequest(UDPPort* port, bool keep_alive, 35 StunBindingRequest(UDPPort* port,
36 int keep_alive_deadline,
34 const rtc::SocketAddress& addr) 37 const rtc::SocketAddress& addr)
35 : port_(port), keep_alive_(keep_alive), server_addr_(addr) { 38 : port_(port),
39 keep_alive_deadline_(keep_alive_deadline),
40 server_addr_(addr) {
36 start_time_ = rtc::Time(); 41 start_time_ = rtc::Time();
37 } 42 }
38 43
39 virtual ~StunBindingRequest() { 44 virtual ~StunBindingRequest() {
40 } 45 }
41 46
42 const rtc::SocketAddress& server_addr() const { return server_addr_; } 47 const rtc::SocketAddress& server_addr() const { return server_addr_; }
43 48
44 virtual void Prepare(StunMessage* request) override { 49 virtual void Prepare(StunMessage* request) override {
45 request->SetType(STUN_BINDING_REQUEST); 50 request->SetType(STUN_BINDING_REQUEST);
46 } 51 }
47 52
48 virtual void OnResponse(StunMessage* response) override { 53 virtual void OnResponse(StunMessage* response) override {
49 const StunAddressAttribute* addr_attr = 54 const StunAddressAttribute* addr_attr =
50 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS); 55 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
51 if (!addr_attr) { 56 if (!addr_attr) {
52 LOG(LS_ERROR) << "Binding response missing mapped address."; 57 LOG(LS_ERROR) << "Binding response missing mapped address.";
53 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 && 58 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 &&
54 addr_attr->family() != STUN_ADDRESS_IPV6) { 59 addr_attr->family() != STUN_ADDRESS_IPV6) {
55 LOG(LS_ERROR) << "Binding address has bad family"; 60 LOG(LS_ERROR) << "Binding address has bad family";
56 } else { 61 } else {
57 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port()); 62 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port());
58 port_->OnStunBindingRequestSucceeded(server_addr_, addr); 63 port_->OnStunBindingRequestSucceeded(server_addr_, addr);
59 } 64 }
60 65
61 // We will do a keep-alive regardless of whether this request succeeds. 66 // We will do a keep-alive regardless of whether this request succeeds.
62 // This should have almost no impact on network usage. 67 // This should have almost no impact on network usage.
63 if (keep_alive_) { 68 if (rtc::TimeSince(port_->start_time()) <= keep_alive_deadline_) {
pthatcher1 2015/12/03 21:00:19 I was thinking that "deadline" would be more an ab
honghaiz3 2015/12/03 22:21:21 Done.
64 port_->requests_.SendDelayed( 69 port_->requests_.SendDelayed(
65 new StunBindingRequest(port_, true, server_addr_), 70 new StunBindingRequest(port_, true, server_addr_),
66 port_->stun_keepalive_delay()); 71 port_->stun_keepalive_delay());
67 } 72 }
68 } 73 }
69 74
70 virtual void OnErrorResponse(StunMessage* response) override { 75 virtual void OnErrorResponse(StunMessage* response) override {
71 const StunErrorCodeAttribute* attr = response->GetErrorCode(); 76 const StunErrorCodeAttribute* attr = response->GetErrorCode();
72 if (!attr) { 77 if (!attr) {
73 LOG(LS_ERROR) << "Bad allocate response error code"; 78 LOG(LS_ERROR) << "Bad allocate response error code";
74 } else { 79 } else {
75 LOG(LS_ERROR) << "Binding error response:" 80 LOG(LS_ERROR) << "Binding error response:"
76 << " class=" << attr->eclass() 81 << " class=" << attr->eclass()
77 << " number=" << attr->number() 82 << " number=" << attr->number()
78 << " reason='" << attr->reason() << "'"; 83 << " reason='" << attr->reason() << "'";
79 } 84 }
80 85
81 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 86 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
82 87
83 if (keep_alive_ 88 if (rtc::TimeSince(port_->start_time()) <= keep_alive_deadline_ &&
84 && (rtc::TimeSince(start_time_) <= RETRY_TIMEOUT)) { 89 rtc::TimeSince(start_time_) <= RETRY_TIMEOUT) {
85 port_->requests_.SendDelayed( 90 port_->requests_.SendDelayed(
86 new StunBindingRequest(port_, true, server_addr_), 91 new StunBindingRequest(port_, true, server_addr_),
87 port_->stun_keepalive_delay()); 92 port_->stun_keepalive_delay());
88 } 93 }
89 } 94 }
90 95
91 virtual void OnTimeout() override { 96 virtual void OnTimeout() override {
92 LOG(LS_ERROR) << "Binding request timed out from " 97 LOG(LS_ERROR) << "Binding request timed out from "
93 << port_->GetLocalAddress().ToSensitiveString() 98 << port_->GetLocalAddress().ToSensitiveString()
94 << " (" << port_->Network()->name() << ")"; 99 << " (" << port_->Network()->name() << ")";
95 100
96 port_->OnStunBindingOrResolveRequestFailed(server_addr_); 101 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 } 102 }
105 103
106 private: 104 private:
107 UDPPort* port_; 105 UDPPort* port_;
108 bool keep_alive_; 106 int keep_alive_deadline_;
109 const rtc::SocketAddress server_addr_; 107 const rtc::SocketAddress server_addr_;
110 uint32_t start_time_; 108 uint32_t start_time_;
111 }; 109 };
112 110
113 UDPPort::AddressResolver::AddressResolver( 111 UDPPort::AddressResolver::AddressResolver(
114 rtc::PacketSocketFactory* factory) 112 rtc::PacketSocketFactory* factory)
115 : socket_factory_(factory) {} 113 : socket_factory_(factory) {}
116 114
117 UDPPort::AddressResolver::~AddressResolver() { 115 UDPPort::AddressResolver::~AddressResolver() {
118 for (ResolverMap::iterator it = resolvers_.begin(); 116 for (ResolverMap::iterator it = resolvers_.begin();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 factory, 173 factory,
176 network, 174 network,
177 socket->GetLocalAddress().ipaddr(), 175 socket->GetLocalAddress().ipaddr(),
178 username, 176 username,
179 password), 177 password),
180 requests_(thread), 178 requests_(thread),
181 socket_(socket), 179 socket_(socket),
182 error_(0), 180 error_(0),
183 ready_(false), 181 ready_(false),
184 stun_keepalive_delay_(KEEPALIVE_DELAY), 182 stun_keepalive_delay_(KEEPALIVE_DELAY),
183 start_time_(rtc::Time()),
185 emit_local_for_anyaddress_(emit_local_for_anyaddress) { 184 emit_local_for_anyaddress_(emit_local_for_anyaddress) {
186 requests_.set_origin(origin); 185 requests_.set_origin(origin);
187 } 186 }
188 187
189 UDPPort::UDPPort(rtc::Thread* thread, 188 UDPPort::UDPPort(rtc::Thread* thread,
190 rtc::PacketSocketFactory* factory, 189 rtc::PacketSocketFactory* factory,
191 rtc::Network* network, 190 rtc::Network* network,
192 const rtc::IPAddress& ip, 191 const rtc::IPAddress& ip,
193 uint16_t min_port, 192 uint16_t min_port,
194 uint16_t max_port, 193 uint16_t max_port,
195 const std::string& username, 194 const std::string& username,
196 const std::string& password, 195 const std::string& password,
197 const std::string& origin, 196 const std::string& origin,
198 bool emit_local_for_anyaddress) 197 bool emit_local_for_anyaddress)
199 : Port(thread, 198 : Port(thread,
200 LOCAL_PORT_TYPE, 199 LOCAL_PORT_TYPE,
201 factory, 200 factory,
202 network, 201 network,
203 ip, 202 ip,
204 min_port, 203 min_port,
205 max_port, 204 max_port,
206 username, 205 username,
207 password), 206 password),
208 requests_(thread), 207 requests_(thread),
209 socket_(NULL), 208 socket_(NULL),
210 error_(0), 209 error_(0),
211 ready_(false), 210 ready_(false),
212 stun_keepalive_delay_(KEEPALIVE_DELAY), 211 stun_keepalive_delay_(KEEPALIVE_DELAY),
212 start_time_(rtc::Time()),
213 emit_local_for_anyaddress_(emit_local_for_anyaddress) { 213 emit_local_for_anyaddress_(emit_local_for_anyaddress) {
214 requests_.set_origin(origin); 214 requests_.set_origin(origin);
215 } 215 }
216 216
217 bool UDPPort::Init() { 217 bool UDPPort::Init() {
218 if (!SharedSocket()) { 218 if (!SharedSocket()) {
219 ASSERT(socket_ == NULL); 219 ASSERT(socket_ == NULL);
220 socket_ = socket_factory()->CreateUdpSocket( 220 socket_ = socket_factory()->CreateUdpSocket(
221 rtc::SocketAddress(ip(), 0), min_port(), max_port()); 221 rtc::SocketAddress(ip(), 0), min_port(), max_port());
222 if (!socket_) { 222 if (!socket_) {
(...skipping 16 matching lines...) Expand all
239 239
240 void UDPPort::PrepareAddress() { 240 void UDPPort::PrepareAddress() {
241 ASSERT(requests_.empty()); 241 ASSERT(requests_.empty());
242 if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { 242 if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
243 OnLocalAddressReady(socket_, socket_->GetLocalAddress()); 243 OnLocalAddressReady(socket_, socket_->GetLocalAddress());
244 } 244 }
245 } 245 }
246 246
247 void UDPPort::MaybePrepareStunCandidate() { 247 void UDPPort::MaybePrepareStunCandidate() {
248 // Sending binding request to the STUN server if address is available to 248 // Sending binding request to the STUN server if address is available to
249 // prepare STUN candidate. 249 // prepare STUN candidate.
pthatcher1 2015/12/03 21:00:57 This comment is no longer true.
honghaiz3 2015/12/03 22:21:21 I don't understand. why?
250 if (!server_addresses_.empty()) { 250 if (!server_addresses_.empty()) {
251 SendStunBindingRequests(); 251 SendStunBindingRequests();
252 } else { 252 } else {
253 // Port is done allocating candidates. 253 // Port is done allocating candidates.
254 MaybeSetPortCompleteOrError(); 254 MaybeSetPortCompleteOrError();
255 } 255 }
256 } 256 }
257 257
258 Connection* UDPPort::CreateConnection(const Candidate& address, 258 Connection* UDPPort::CreateConnection(const Candidate& address,
259 CandidateOrigin origin) { 259 CandidateOrigin origin) {
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 } 390 }
391 } 391 }
392 392
393 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) { 393 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) {
394 if (stun_addr.IsUnresolvedIP()) { 394 if (stun_addr.IsUnresolvedIP()) {
395 ResolveStunAddress(stun_addr); 395 ResolveStunAddress(stun_addr);
396 396
397 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { 397 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
398 // Check if |server_addr_| is compatible with the port's ip. 398 // Check if |server_addr_| is compatible with the port's ip.
399 if (IsCompatibleAddress(stun_addr)) { 399 if (IsCompatibleAddress(stun_addr)) {
400 requests_.Send(new StunBindingRequest(this, true, stun_addr)); 400 requests_.Send(
401 new StunBindingRequest(this, KEEP_ALIVE_DEADLINE, stun_addr));
401 } else { 402 } else {
402 // Since we can't send stun messages to the server, we should mark this 403 // Since we can't send stun messages to the server, we should mark this
403 // port ready. 404 // port ready.
404 LOG(LS_WARNING) << "STUN server address is incompatible."; 405 LOG(LS_WARNING) << "STUN server address is incompatible.";
405 OnStunBindingOrResolveRequestFailed(stun_addr); 406 OnStunBindingOrResolveRequestFailed(stun_addr);
406 } 407 }
407 } 408 }
408 } 409 }
409 410
410 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const { 411 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(); 505 const std::vector<Candidate>& existing_candidates = Candidates();
505 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); 506 std::vector<Candidate>::const_iterator it = existing_candidates.begin();
506 for (; it != existing_candidates.end(); ++it) { 507 for (; it != existing_candidates.end(); ++it) {
507 if (it->address() == addr) 508 if (it->address() == addr)
508 return true; 509 return true;
509 } 510 }
510 return false; 511 return false;
511 } 512 }
512 513
513 } // namespace cricket 514 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/p2p/base/stunport.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698