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/common.h" | 17 #include "webrtc/base/common.h" |
17 #include "webrtc/base/helpers.h" | 18 #include "webrtc/base/helpers.h" |
18 #include "webrtc/base/ipaddress.h" | 19 #include "webrtc/base/ipaddress.h" |
19 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/nethelpers.h" | 21 #include "webrtc/base/nethelpers.h" |
21 | 22 |
22 namespace cricket { | 23 namespace cricket { |
23 | 24 |
24 // TODO: Move these to a common place (used in relayport too) | 25 // TODO: Move these to a common place (used in relayport too) |
25 const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts | 26 const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 } | 160 } |
160 } | 161 } |
161 | 162 |
162 UDPPort::UDPPort(rtc::Thread* thread, | 163 UDPPort::UDPPort(rtc::Thread* thread, |
163 rtc::PacketSocketFactory* factory, | 164 rtc::PacketSocketFactory* factory, |
164 rtc::Network* network, | 165 rtc::Network* network, |
165 rtc::AsyncPacketSocket* socket, | 166 rtc::AsyncPacketSocket* socket, |
166 const std::string& username, | 167 const std::string& username, |
167 const std::string& password, | 168 const std::string& password, |
168 const std::string& origin, | 169 const std::string& origin, |
169 bool emit_localhost_for_anyaddress) | 170 bool emit_local_for_anyaddress) |
170 : Port(thread, factory, network, socket->GetLocalAddress().ipaddr(), | 171 : Port(thread, |
171 username, password), | 172 factory, |
173 network, | |
174 socket->GetLocalAddress().ipaddr(), | |
175 username, | |
176 password), | |
172 requests_(thread), | 177 requests_(thread), |
173 socket_(socket), | 178 socket_(socket), |
174 error_(0), | 179 error_(0), |
175 ready_(false), | 180 ready_(false), |
176 stun_keepalive_delay_(KEEPALIVE_DELAY), | 181 stun_keepalive_delay_(KEEPALIVE_DELAY), |
177 emit_localhost_for_anyaddress_(emit_localhost_for_anyaddress) { | 182 emit_local_for_anyaddress_(emit_local_for_anyaddress) { |
178 requests_.set_origin(origin); | 183 requests_.set_origin(origin); |
179 } | 184 } |
180 | 185 |
181 UDPPort::UDPPort(rtc::Thread* thread, | 186 UDPPort::UDPPort(rtc::Thread* thread, |
182 rtc::PacketSocketFactory* factory, | 187 rtc::PacketSocketFactory* factory, |
183 rtc::Network* network, | 188 rtc::Network* network, |
184 const rtc::IPAddress& ip, | 189 const rtc::IPAddress& ip, |
185 uint16_t min_port, | 190 uint16_t min_port, |
186 uint16_t max_port, | 191 uint16_t max_port, |
187 const std::string& username, | 192 const std::string& username, |
188 const std::string& password, | 193 const std::string& password, |
189 const std::string& origin, | 194 const std::string& origin, |
190 bool emit_localhost_for_anyaddress) | 195 bool emit_local_for_anyaddress) |
191 : Port(thread, | 196 : Port(thread, |
192 LOCAL_PORT_TYPE, | 197 LOCAL_PORT_TYPE, |
193 factory, | 198 factory, |
194 network, | 199 network, |
195 ip, | 200 ip, |
196 min_port, | 201 min_port, |
197 max_port, | 202 max_port, |
198 username, | 203 username, |
199 password), | 204 password), |
200 requests_(thread), | 205 requests_(thread), |
201 socket_(NULL), | 206 socket_(NULL), |
202 error_(0), | 207 error_(0), |
203 ready_(false), | 208 ready_(false), |
204 stun_keepalive_delay_(KEEPALIVE_DELAY), | 209 stun_keepalive_delay_(KEEPALIVE_DELAY), |
205 emit_localhost_for_anyaddress_(emit_localhost_for_anyaddress) { | 210 emit_local_for_anyaddress_(emit_local_for_anyaddress) { |
206 requests_.set_origin(origin); | 211 requests_.set_origin(origin); |
207 } | 212 } |
208 | 213 |
209 bool UDPPort::Init() { | 214 bool UDPPort::Init() { |
210 if (!SharedSocket()) { | 215 if (!SharedSocket()) { |
211 ASSERT(socket_ == NULL); | 216 ASSERT(socket_ == NULL); |
212 socket_ = socket_factory()->CreateUdpSocket( | 217 socket_ = socket_factory()->CreateUdpSocket( |
213 rtc::SocketAddress(ip(), 0), min_port(), max_port()); | 218 rtc::SocketAddress(ip(), 0), min_port(), max_port()); |
214 if (!socket_) { | 219 if (!socket_) { |
215 LOG_J(LS_WARNING, this) << "UDP socket creation failed"; | 220 LOG_J(LS_WARNING, this) << "UDP socket creation failed"; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 } | 293 } |
289 | 294 |
290 int UDPPort::GetError() { | 295 int UDPPort::GetError() { |
291 return error_; | 296 return error_; |
292 } | 297 } |
293 | 298 |
294 void UDPPort::OnLocalAddressReady(rtc::AsyncPacketSocket* socket, | 299 void UDPPort::OnLocalAddressReady(rtc::AsyncPacketSocket* socket, |
295 const rtc::SocketAddress& address) { | 300 const rtc::SocketAddress& address) { |
296 // When adapter enumeration is disabled and binding to the any address, the | 301 // When adapter enumeration is disabled and binding to the any address, the |
297 // loopback address will be issued as a candidate instead if | 302 // loopback address will be issued as a candidate instead if |
298 // |emit_localhost_for_anyaddress| is true. This is to allow connectivity on | 303 // |emit_local_for_anyaddress| is true. This is to allow connectivity on |
299 // demo pages without STUN/TURN to work. | 304 // demo pages without STUN/TURN to work. |
300 rtc::SocketAddress addr = address; | 305 rtc::SocketAddress addr = address; |
301 if (addr.IsAnyIP() && emit_localhost_for_anyaddress_) { | 306 if (addr.IsAnyIP() && emit_local_for_anyaddress_ && |
302 addr.SetIP(rtc::GetLoopbackIP(addr.family())); | 307 Network()->default_address_provider()) { |
pthatcher1
2015/11/10 18:52:34
Actually, not it seems like it would make sense to
guoweis_webrtc
2015/11/10 20:37:29
should not be a network property.
| |
308 rtc::IPAddress default_address; | |
309 bool result = Network()->default_address_provider()->GetDefaultLocalAddress( | |
310 address.family(), &default_address); | |
311 RTC_DCHECK(result && !default_address.IsNil()); | |
312 addr.SetIP(default_address); | |
303 } | 313 } |
304 | 314 |
305 AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "", | 315 AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "", |
306 LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false); | 316 LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false); |
307 MaybePrepareStunCandidate(); | 317 MaybePrepareStunCandidate(); |
308 } | 318 } |
309 | 319 |
310 void UDPPort::OnReadPacket( | 320 void UDPPort::OnReadPacket( |
311 rtc::AsyncPacketSocket* socket, const char* data, size_t size, | 321 rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
312 const rtc::SocketAddress& remote_addr, | 322 const rtc::SocketAddress& remote_addr, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 bind_request_succeeded_servers_.insert(stun_server_addr); | 418 bind_request_succeeded_servers_.insert(stun_server_addr); |
409 | 419 |
410 // If socket is shared and |stun_reflected_addr| is equal to local socket | 420 // If socket is shared and |stun_reflected_addr| is equal to local socket |
411 // address, or if the same address has been added by another STUN server, | 421 // address, or if the same address has been added by another STUN server, |
412 // then discarding the stun address. | 422 // then discarding the stun address. |
413 // For STUN, related address is the local socket address. | 423 // For STUN, related address is the local socket address. |
414 if ((!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) && | 424 if ((!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) && |
415 !HasCandidateWithAddress(stun_reflected_addr)) { | 425 !HasCandidateWithAddress(stun_reflected_addr)) { |
416 | 426 |
417 rtc::SocketAddress related_address = socket_->GetLocalAddress(); | 427 rtc::SocketAddress related_address = socket_->GetLocalAddress(); |
428 if (related_address.IsAnyIP() && emit_local_for_anyaddress_ && | |
429 Network()->default_address_provider()) { | |
430 rtc::IPAddress default_address; | |
431 bool result = | |
432 Network()->default_address_provider()->GetDefaultLocalAddress( | |
433 related_address.family(), &default_address); | |
434 RTC_DCHECK(result && !default_address.IsNil()); | |
435 related_address.SetIP(default_address); | |
pthatcher1
2015/11/10 18:52:34
Seems like we have duplicate logic here. Can we j
guoweis_webrtc
2015/11/10 20:37:30
Done.
| |
436 } | |
418 if (!(candidate_filter() & CF_HOST)) { | 437 if (!(candidate_filter() & CF_HOST)) { |
419 // If candidate filter doesn't have CF_HOST specified, empty raddr to | 438 // If candidate filter doesn't have CF_HOST specified, empty raddr to |
420 // avoid local address leakage. | 439 // avoid local address leakage. |
421 related_address = rtc::EmptySocketAddressWithFamily( | 440 related_address = rtc::EmptySocketAddressWithFamily( |
422 related_address.family()); | 441 related_address.family()); |
423 } | 442 } |
424 | 443 |
425 AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address, | 444 AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address, |
426 UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE, | 445 UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE, |
427 ICE_TYPE_PREFERENCE_SRFLX, 0, false); | 446 ICE_TYPE_PREFERENCE_SRFLX, 0, false); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
476 const std::vector<Candidate>& existing_candidates = Candidates(); | 495 const std::vector<Candidate>& existing_candidates = Candidates(); |
477 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); | 496 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); |
478 for (; it != existing_candidates.end(); ++it) { | 497 for (; it != existing_candidates.end(); ++it) { |
479 if (it->address() == addr) | 498 if (it->address() == addr) |
480 return true; | 499 return true; |
481 } | 500 } |
482 return false; | 501 return false; |
483 } | 502 } |
484 | 503 |
485 } // namespace cricket | 504 } // namespace cricket |
OLD | NEW |