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_MS = 10 * 1000; // 10 seconds - sort timeouts |
27 const int RETRY_TIMEOUT = 50 * 1000; // ICE says 50 secs | 27 const int RETRY_TIMEOUT_US = 50 * 1000000; // 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 int32_t HIGH_COST_PORT_KEEPALIVE_LIFETIME_MS = 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 uint64_t start_time_us, |
39 : port_(port), server_addr_(addr), deadline_(deadline) { | 39 int64_t lifetime_us) |
40 start_time_ = rtc::Time(); | 40 : port_(port), |
41 } | 41 server_addr_(addr), |
42 start_time_us_(start_time_us), | |
43 lifetime_us_(lifetime_us) {} | |
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::TimeMicros())) { |
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_us_, |
71 lifetime_us_), | |
70 port_->stun_keepalive_delay()); | 72 port_->stun_keepalive_delay()); |
71 } | 73 } |
72 } | 74 } |
73 | 75 |
74 virtual void OnErrorResponse(StunMessage* response) override { | 76 virtual void OnErrorResponse(StunMessage* response) override { |
75 const StunErrorCodeAttribute* attr = response->GetErrorCode(); | 77 const StunErrorCodeAttribute* attr = response->GetErrorCode(); |
76 if (!attr) { | 78 if (!attr) { |
77 LOG(LS_ERROR) << "Bad allocate response error code"; | 79 LOG(LS_ERROR) << "Bad allocate response error code"; |
78 } else { | 80 } else { |
79 LOG(LS_ERROR) << "Binding error response:" | 81 LOG(LS_ERROR) << "Binding error response:" |
80 << " class=" << attr->eclass() | 82 << " class=" << attr->eclass() |
81 << " number=" << attr->number() | 83 << " number=" << attr->number() << " reason='" |
82 << " reason='" << attr->reason() << "'"; | 84 << attr->reason() << "'"; |
83 } | 85 } |
84 | 86 |
85 port_->OnStunBindingOrResolveRequestFailed(server_addr_); | 87 port_->OnStunBindingOrResolveRequestFailed(server_addr_); |
86 | 88 |
87 uint32_t now = rtc::Time(); | 89 uint64_t now = rtc::TimeMicros(); |
88 if (now <= deadline_ && rtc::TimeDiff(now, start_time_) <= RETRY_TIMEOUT) { | 90 if (WithinLifetime(now) && now - start_time_us_ <= RETRY_TIMEOUT_US) { |
89 port_->requests_.SendDelayed( | 91 port_->requests_.SendDelayed( |
90 new StunBindingRequest(port_, server_addr_, deadline_), | 92 new StunBindingRequest(port_, server_addr_, start_time_us_, |
93 lifetime_us_), | |
91 port_->stun_keepalive_delay()); | 94 port_->stun_keepalive_delay()); |
92 } | 95 } |
93 } | 96 } |
94 | |
95 virtual void OnTimeout() override { | 97 virtual void OnTimeout() override { |
96 LOG(LS_ERROR) << "Binding request timed out from " | 98 LOG(LS_ERROR) << "Binding request timed out from " |
97 << port_->GetLocalAddress().ToSensitiveString() | 99 << port_->GetLocalAddress().ToSensitiveString() << " (" |
98 << " (" << port_->Network()->name() << ")"; | 100 << port_->Network()->name() << ")"; |
99 | 101 |
100 port_->OnStunBindingOrResolveRequestFailed(server_addr_); | 102 port_->OnStunBindingOrResolveRequestFailed(server_addr_); |
101 } | 103 } |
102 | 104 |
103 private: | 105 private: |
106 // Returns true if |now| is within the lifetime of the request (a negative | |
107 // lifetime means infinite). | |
108 bool WithinLifetime(uint64_t now) const { | |
109 return lifetime_us_ < 0 || now <= start_time_us_ + lifetime_us_; | |
honghaiz3
2016/03/02 20:20:41
Used the negative check instead of exactly -1 so t
| |
110 } | |
104 UDPPort* port_; | 111 UDPPort* port_; |
105 const rtc::SocketAddress server_addr_; | 112 const rtc::SocketAddress server_addr_; |
106 uint32_t start_time_; | 113 uint64_t start_time_us_; |
107 uint32_t deadline_; | 114 // The time duration for which this request will be rescheduled. |
115 int64_t lifetime_us_; | |
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 : Port(thread, | 179 : Port(thread, |
172 factory, | 180 factory, |
173 network, | 181 network, |
174 socket->GetLocalAddress().ipaddr(), | 182 socket->GetLocalAddress().ipaddr(), |
175 username, | 183 username, |
176 password), | 184 password), |
177 requests_(thread), | 185 requests_(thread), |
178 socket_(socket), | 186 socket_(socket), |
179 error_(0), | 187 error_(0), |
180 ready_(false), | 188 ready_(false), |
181 stun_keepalive_delay_(KEEPALIVE_DELAY), | 189 stun_keepalive_delay_(KEEPALIVE_DELAY_MS), |
182 emit_local_for_anyaddress_(emit_local_for_anyaddress) { | 190 emit_local_for_anyaddress_(emit_local_for_anyaddress) { |
183 requests_.set_origin(origin); | 191 requests_.set_origin(origin); |
184 } | 192 } |
185 | 193 |
186 UDPPort::UDPPort(rtc::Thread* thread, | 194 UDPPort::UDPPort(rtc::Thread* thread, |
187 rtc::PacketSocketFactory* factory, | 195 rtc::PacketSocketFactory* factory, |
188 rtc::Network* network, | 196 rtc::Network* network, |
189 const rtc::IPAddress& ip, | 197 const rtc::IPAddress& ip, |
190 uint16_t min_port, | 198 uint16_t min_port, |
191 uint16_t max_port, | 199 uint16_t max_port, |
192 const std::string& username, | 200 const std::string& username, |
193 const std::string& password, | 201 const std::string& password, |
194 const std::string& origin, | 202 const std::string& origin, |
195 bool emit_local_for_anyaddress) | 203 bool emit_local_for_anyaddress) |
196 : Port(thread, | 204 : Port(thread, |
197 LOCAL_PORT_TYPE, | 205 LOCAL_PORT_TYPE, |
198 factory, | 206 factory, |
199 network, | 207 network, |
200 ip, | 208 ip, |
201 min_port, | 209 min_port, |
202 max_port, | 210 max_port, |
203 username, | 211 username, |
204 password), | 212 password), |
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_MS), |
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_MS. | |
226 stun_keepalive_lifetime_ = (network_cost() == 0) | |
227 ? INFINITE_LIFETIME | |
228 : HIGH_COST_PORT_KEEPALIVE_LIFETIME_MS; | |
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 Loading... | |
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 // The |stun_keepalive_lifetime_| is in milliseconds while the lifetime |
401 rtc::Time() + KEEP_ALIVE_TIMEOUT)); | 415 // in the StunBindingRequest is in microseconds. |
pthatcher1
2016/03/02 20:25:59
Can we just make everything in this file milliseco
honghaiz3
2016/03/02 23:22:51
It is hard to all use milliseconds or microseconds
| |
416 requests_.Send(new StunBindingRequest(this, stun_addr, rtc::TimeMicros(), | |
417 stun_keepalive_lifetime_ * 1000)); | |
402 } else { | 418 } else { |
403 // Since we can't send stun messages to the server, we should mark this | 419 // Since we can't send stun messages to the server, we should mark this |
404 // port ready. | 420 // port ready. |
405 LOG(LS_WARNING) << "STUN server address is incompatible."; | 421 LOG(LS_WARNING) << "STUN server address is incompatible."; |
406 OnStunBindingOrResolveRequestFailed(stun_addr); | 422 OnStunBindingOrResolveRequestFailed(stun_addr); |
407 } | 423 } |
408 } | 424 } |
409 } | 425 } |
410 | 426 |
411 bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const { | 427 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(); | 521 const std::vector<Candidate>& existing_candidates = Candidates(); |
506 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); | 522 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); |
507 for (; it != existing_candidates.end(); ++it) { | 523 for (; it != existing_candidates.end(); ++it) { |
508 if (it->address() == addr) | 524 if (it->address() == addr) |
509 return true; | 525 return true; |
510 } | 526 } |
511 return false; | 527 return false; |
512 } | 528 } |
513 | 529 |
514 } // namespace cricket | 530 } // namespace cricket |
OLD | NEW |