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

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

Issue 1411253008: WebRTC should generate default private address even when adapter enumeration is disabled. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: address Peter's comments. Created 5 years, 1 month 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
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/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
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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return socket_->GetOption(opt, value); 292 return socket_->GetOption(opt, value);
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 // default local 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 for
299 // demo pages without STUN/TURN to work. 304 // applications which absolutely requires a HOST candidate.
300 rtc::SocketAddress addr = address; 305 rtc::SocketAddress addr = address;
301 if (addr.IsAnyIP() && emit_localhost_for_anyaddress_) { 306 MaybeSetDefaultLocalAddress(&addr);
302 addr.SetIP(rtc::GetLoopbackIP(addr.family()));
303 }
304 307
305 AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "", 308 AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "",
306 LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false); 309 LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false);
307 MaybePrepareStunCandidate(); 310 MaybePrepareStunCandidate();
308 } 311 }
309 312
310 void UDPPort::OnReadPacket( 313 void UDPPort::OnReadPacket(
311 rtc::AsyncPacketSocket* socket, const char* data, size_t size, 314 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
312 const rtc::SocketAddress& remote_addr, 315 const rtc::SocketAddress& remote_addr,
313 const rtc::PacketTime& packet_time) { 316 const rtc::PacketTime& packet_time) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 requests_.Send(new StunBindingRequest(this, true, stun_addr)); 394 requests_.Send(new StunBindingRequest(this, true, stun_addr));
392 } else { 395 } else {
393 // Since we can't send stun messages to the server, we should mark this 396 // Since we can't send stun messages to the server, we should mark this
394 // port ready. 397 // port ready.
395 LOG(LS_WARNING) << "STUN server address is incompatible."; 398 LOG(LS_WARNING) << "STUN server address is incompatible.";
396 OnStunBindingOrResolveRequestFailed(stun_addr); 399 OnStunBindingOrResolveRequestFailed(stun_addr);
397 } 400 }
398 } 401 }
399 } 402 }
400 403
404 void UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const {
405 if (!addr->IsAnyIP() || !emit_local_for_anyaddress_ ||
406 !Network()->default_local_address_provider()) {
407 return;
408 }
409 rtc::IPAddress default_address;
410 bool result =
411 Network()->default_local_address_provider()->GetDefaultLocalAddress(
412 addr->family(), &default_address);
413 RTC_DCHECK(result && !default_address.IsNil());
414 addr->SetIP(default_address);
415 }
416
401 void UDPPort::OnStunBindingRequestSucceeded( 417 void UDPPort::OnStunBindingRequestSucceeded(
402 const rtc::SocketAddress& stun_server_addr, 418 const rtc::SocketAddress& stun_server_addr,
403 const rtc::SocketAddress& stun_reflected_addr) { 419 const rtc::SocketAddress& stun_reflected_addr) {
404 if (bind_request_succeeded_servers_.find(stun_server_addr) != 420 if (bind_request_succeeded_servers_.find(stun_server_addr) !=
405 bind_request_succeeded_servers_.end()) { 421 bind_request_succeeded_servers_.end()) {
406 return; 422 return;
407 } 423 }
408 bind_request_succeeded_servers_.insert(stun_server_addr); 424 bind_request_succeeded_servers_.insert(stun_server_addr);
409 425
410 // If socket is shared and |stun_reflected_addr| is equal to local socket 426 // 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, 427 // address, or if the same address has been added by another STUN server,
412 // then discarding the stun address. 428 // then discarding the stun address.
413 // For STUN, related address is the local socket address. 429 // For STUN, related address is the local socket address.
414 if ((!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) && 430 if ((!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) &&
415 !HasCandidateWithAddress(stun_reflected_addr)) { 431 !HasCandidateWithAddress(stun_reflected_addr)) {
416 432
417 rtc::SocketAddress related_address = socket_->GetLocalAddress(); 433 rtc::SocketAddress related_address = socket_->GetLocalAddress();
434 MaybeSetDefaultLocalAddress(&related_address);
418 if (!(candidate_filter() & CF_HOST)) { 435 if (!(candidate_filter() & CF_HOST)) {
419 // If candidate filter doesn't have CF_HOST specified, empty raddr to 436 // If candidate filter doesn't have CF_HOST specified, empty raddr to
420 // avoid local address leakage. 437 // avoid local address leakage.
421 related_address = rtc::EmptySocketAddressWithFamily( 438 related_address = rtc::EmptySocketAddressWithFamily(
422 related_address.family()); 439 related_address.family());
423 } 440 }
424 441
425 AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address, 442 AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address,
426 UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE, 443 UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE,
427 ICE_TYPE_PREFERENCE_SRFLX, 0, false); 444 ICE_TYPE_PREFERENCE_SRFLX, 0, false);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 const std::vector<Candidate>& existing_candidates = Candidates(); 493 const std::vector<Candidate>& existing_candidates = Candidates();
477 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); 494 std::vector<Candidate>::const_iterator it = existing_candidates.begin();
478 for (; it != existing_candidates.end(); ++it) { 495 for (; it != existing_candidates.end(); ++it) {
479 if (it->address() == addr) 496 if (it->address() == addr)
480 return true; 497 return true;
481 } 498 }
482 return false; 499 return false;
483 } 500 }
484 501
485 } // namespace cricket 502 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698