OLD | NEW |
---|---|
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; // ICE says 50 secs |
28 // Stop sending STUN binding requests after this amount of time | 28 const int KEEPALIVE_LIFETIME = 2 * 60 * 1000; // 2 minutes |
pthatcher1
2016/02/26 22:33:52
This could use a comment explaining what it is.
P
honghaiz3
2016/02/29 19:34:11
Done.
| |
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 | |
32 | 29 |
33 // Handles a binding request sent to the STUN server. | 30 // Handles a binding request sent to the STUN server. |
34 class StunBindingRequest : public StunRequest { | 31 class StunBindingRequest : public StunRequest { |
35 public: | 32 public: |
36 StunBindingRequest(UDPPort* port, | 33 StunBindingRequest(UDPPort* port, |
37 const rtc::SocketAddress& addr, | 34 const rtc::SocketAddress& addr, |
38 uint32_t deadline) | 35 int lifetime) |
39 : port_(port), server_addr_(addr), deadline_(deadline) { | 36 : port_(port), server_addr_(addr), lifetime_(lifetime) { |
40 start_time_ = rtc::Time(); | 37 start_time_ = rtc::Time(); |
41 } | 38 } |
42 | 39 |
43 virtual ~StunBindingRequest() { | 40 virtual ~StunBindingRequest() { |
44 } | 41 } |
45 | 42 |
46 const rtc::SocketAddress& server_addr() const { return server_addr_; } | 43 const rtc::SocketAddress& server_addr() const { return server_addr_; } |
47 | 44 |
48 virtual void Prepare(StunMessage* request) override { | 45 virtual void Prepare(StunMessage* request) override { |
49 request->SetType(STUN_BINDING_REQUEST); | 46 request->SetType(STUN_BINDING_REQUEST); |
50 } | 47 } |
51 | 48 |
52 virtual void OnResponse(StunMessage* response) override { | 49 virtual void OnResponse(StunMessage* response) override { |
53 const StunAddressAttribute* addr_attr = | 50 const StunAddressAttribute* addr_attr = |
54 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS); | 51 response->GetAddress(STUN_ATTR_MAPPED_ADDRESS); |
55 if (!addr_attr) { | 52 if (!addr_attr) { |
56 LOG(LS_ERROR) << "Binding response missing mapped address."; | 53 LOG(LS_ERROR) << "Binding response missing mapped address."; |
57 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 && | 54 } else if (addr_attr->family() != STUN_ADDRESS_IPV4 && |
58 addr_attr->family() != STUN_ADDRESS_IPV6) { | 55 addr_attr->family() != STUN_ADDRESS_IPV6) { |
59 LOG(LS_ERROR) << "Binding address has bad family"; | 56 LOG(LS_ERROR) << "Binding address has bad family"; |
60 } else { | 57 } else { |
61 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port()); | 58 rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port()); |
62 port_->OnStunBindingRequestSucceeded(server_addr_, addr); | 59 port_->OnStunBindingRequestSucceeded(server_addr_, addr); |
63 } | 60 } |
64 | 61 |
65 // We will do a keep-alive regardless of whether this request succeeds. | 62 // We will do a keep-alive regardless of whether this request succeeds. |
66 // It will be stopped after |deadline_| mostly to conserve the battery life. | 63 // It will be stopped after its lifetime has passed (note that a negative |
67 if (rtc::Time() <= deadline_) { | 64 // lifetime indicates it will be sent indefinitely). |
65 if (lifetime_ < 0 || rtc::TimeDiff(rtc::Time(), start_time_) <= lifetime_) { | |
68 port_->requests_.SendDelayed( | 66 port_->requests_.SendDelayed( |
69 new StunBindingRequest(port_, server_addr_, deadline_), | 67 new StunBindingRequest(port_, server_addr_, lifetime_), |
70 port_->stun_keepalive_delay()); | 68 port_->stun_keepalive_delay()); |
71 } | 69 } |
72 } | 70 } |
73 | 71 |
74 virtual void OnErrorResponse(StunMessage* response) override { | 72 virtual void OnErrorResponse(StunMessage* response) override { |
75 const StunErrorCodeAttribute* attr = response->GetErrorCode(); | 73 const StunErrorCodeAttribute* attr = response->GetErrorCode(); |
76 if (!attr) { | 74 if (!attr) { |
77 LOG(LS_ERROR) << "Bad allocate response error code"; | 75 LOG(LS_ERROR) << "Bad allocate response error code"; |
78 } else { | 76 } else { |
79 LOG(LS_ERROR) << "Binding error response:" | 77 LOG(LS_ERROR) << "Binding error response:" |
80 << " class=" << attr->eclass() | 78 << " class=" << attr->eclass() |
81 << " number=" << attr->number() | 79 << " number=" << attr->number() |
82 << " reason='" << attr->reason() << "'"; | 80 << " reason='" << attr->reason() << "'"; |
83 } | 81 } |
84 | 82 |
85 port_->OnStunBindingOrResolveRequestFailed(server_addr_); | 83 port_->OnStunBindingOrResolveRequestFailed(server_addr_); |
86 | 84 |
87 uint32_t now = rtc::Time(); | 85 uint32_t now = rtc::Time(); |
88 if (now <= deadline_ && rtc::TimeDiff(now, start_time_) <= RETRY_TIMEOUT) { | 86 if ((lifetime_ < 0 || rtc::TimeDiff(now, start_time_) <= lifetime_) && |
pthatcher1
2016/02/26 22:33:52
It seems like we could use a helper method of Beyo
honghaiz3
2016/02/29 19:34:11
Done, although this will lose the benefit of not r
| |
87 rtc::TimeDiff(now, start_time_) <= RETRY_TIMEOUT) { | |
89 port_->requests_.SendDelayed( | 88 port_->requests_.SendDelayed( |
90 new StunBindingRequest(port_, server_addr_, deadline_), | 89 new StunBindingRequest(port_, server_addr_, lifetime_), |
91 port_->stun_keepalive_delay()); | 90 port_->stun_keepalive_delay()); |
92 } | 91 } |
93 } | 92 } |
94 | 93 |
95 virtual void OnTimeout() override { | 94 virtual void OnTimeout() override { |
96 LOG(LS_ERROR) << "Binding request timed out from " | 95 LOG(LS_ERROR) << "Binding request timed out from " |
97 << port_->GetLocalAddress().ToSensitiveString() | 96 << port_->GetLocalAddress().ToSensitiveString() |
98 << " (" << port_->Network()->name() << ")"; | 97 << " (" << port_->Network()->name() << ")"; |
99 | 98 |
100 port_->OnStunBindingOrResolveRequestFailed(server_addr_); | 99 port_->OnStunBindingOrResolveRequestFailed(server_addr_); |
101 } | 100 } |
102 | 101 |
103 private: | 102 private: |
104 UDPPort* port_; | 103 UDPPort* port_; |
105 const rtc::SocketAddress server_addr_; | 104 const rtc::SocketAddress server_addr_; |
106 uint32_t start_time_; | 105 uint32_t start_time_; |
107 uint32_t deadline_; | 106 // The time duration for which this request will be rescheduled. |
107 // A negative value means this request will be rescheduled indefinitely. | |
108 int lifetime_; | |
108 }; | 109 }; |
109 | 110 |
110 UDPPort::AddressResolver::AddressResolver( | 111 UDPPort::AddressResolver::AddressResolver( |
111 rtc::PacketSocketFactory* factory) | 112 rtc::PacketSocketFactory* factory) |
112 : socket_factory_(factory) {} | 113 : socket_factory_(factory) {} |
113 | 114 |
114 UDPPort::AddressResolver::~AddressResolver() { | 115 UDPPort::AddressResolver::~AddressResolver() { |
115 for (ResolverMap::iterator it = resolvers_.begin(); | 116 for (ResolverMap::iterator it = resolvers_.begin(); |
116 it != resolvers_.end(); ++it) { | 117 it != resolvers_.end(); ++it) { |
117 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang | 118 // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 } | 391 } |
391 } | 392 } |
392 | 393 |
393 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) { | 394 void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) { |
394 if (stun_addr.IsUnresolvedIP()) { | 395 if (stun_addr.IsUnresolvedIP()) { |
395 ResolveStunAddress(stun_addr); | 396 ResolveStunAddress(stun_addr); |
396 | 397 |
397 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { | 398 } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { |
398 // Check if |server_addr_| is compatible with the port's ip. | 399 // Check if |server_addr_| is compatible with the port's ip. |
399 if (IsCompatibleAddress(stun_addr)) { | 400 if (IsCompatibleAddress(stun_addr)) { |
400 requests_.Send(new StunBindingRequest(this, stun_addr, | 401 // If this is a zero-cost network, it will keep on sending STUN binding |
401 rtc::Time() + KEEP_ALIVE_TIMEOUT)); | 402 // requests indefinitely to keep the NAT binding alive. Otherwise, |
403 // stop sending STUN binding requests after KEEPALIVE_LIFETIME. | |
404 int keepalive_lifetime = (network_cost() == 0) ? -1 : KEEPALIVE_LIFETIME; | |
pthatcher1
2016/02/26 22:33:52
I think we should make a constant for UNLIMITED_LI
honghaiz3
2016/02/29 19:34:11
Done. Used INIFINTE_LIFETIME. Also the logic chang
| |
405 requests_.Send( | |
406 new StunBindingRequest(this, stun_addr, keepalive_lifetime)); | |
402 } else { | 407 } else { |
403 // 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 |
404 // port ready. | 409 // port ready. |
405 LOG(LS_WARNING) << "STUN server address is incompatible."; | 410 LOG(LS_WARNING) << "STUN server address is incompatible."; |
406 OnStunBindingOrResolveRequestFailed(stun_addr); | 411 OnStunBindingOrResolveRequestFailed(stun_addr); |
407 } | 412 } |
408 } | 413 } |
409 } | 414 } |
410 | 415 |
411 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 Loading... | |
505 const std::vector<Candidate>& existing_candidates = Candidates(); | 510 const std::vector<Candidate>& existing_candidates = Candidates(); |
506 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); | 511 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); |
507 for (; it != existing_candidates.end(); ++it) { | 512 for (; it != existing_candidates.end(); ++it) { |
508 if (it->address() == addr) | 513 if (it->address() == addr) |
509 return true; | 514 return true; |
510 } | 515 } |
511 return false; | 516 return false; |
512 } | 517 } |
513 | 518 |
514 } // namespace cricket | 519 } // namespace cricket |
OLD | NEW |