| 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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 } | 163 } |
| 163 } | 164 } |
| 164 | 165 |
| 165 UDPPort::UDPPort(rtc::Thread* thread, | 166 UDPPort::UDPPort(rtc::Thread* thread, |
| 166 rtc::PacketSocketFactory* factory, | 167 rtc::PacketSocketFactory* factory, |
| 167 rtc::Network* network, | 168 rtc::Network* network, |
| 168 rtc::AsyncPacketSocket* socket, | 169 rtc::AsyncPacketSocket* socket, |
| 169 const std::string& username, | 170 const std::string& username, |
| 170 const std::string& password, | 171 const std::string& password, |
| 171 const std::string& origin, | 172 const std::string& origin, |
| 172 bool emit_localhost_for_anyaddress) | 173 bool emit_local_for_anyaddress) |
| 173 : Port(thread, factory, network, socket->GetLocalAddress().ipaddr(), | 174 : Port(thread, |
| 174 username, password), | 175 factory, |
| 176 network, |
| 177 socket->GetLocalAddress().ipaddr(), |
| 178 username, |
| 179 password), |
| 175 requests_(thread), | 180 requests_(thread), |
| 176 socket_(socket), | 181 socket_(socket), |
| 177 error_(0), | 182 error_(0), |
| 178 ready_(false), | 183 ready_(false), |
| 179 stun_keepalive_delay_(KEEPALIVE_DELAY), | 184 stun_keepalive_delay_(KEEPALIVE_DELAY), |
| 180 emit_localhost_for_anyaddress_(emit_localhost_for_anyaddress) { | 185 emit_local_for_anyaddress_(emit_local_for_anyaddress) { |
| 181 requests_.set_origin(origin); | 186 requests_.set_origin(origin); |
| 182 } | 187 } |
| 183 | 188 |
| 184 UDPPort::UDPPort(rtc::Thread* thread, | 189 UDPPort::UDPPort(rtc::Thread* thread, |
| 185 rtc::PacketSocketFactory* factory, | 190 rtc::PacketSocketFactory* factory, |
| 186 rtc::Network* network, | 191 rtc::Network* network, |
| 187 const rtc::IPAddress& ip, | 192 const rtc::IPAddress& ip, |
| 188 uint16_t min_port, | 193 uint16_t min_port, |
| 189 uint16_t max_port, | 194 uint16_t max_port, |
| 190 const std::string& username, | 195 const std::string& username, |
| 191 const std::string& password, | 196 const std::string& password, |
| 192 const std::string& origin, | 197 const std::string& origin, |
| 193 bool emit_localhost_for_anyaddress) | 198 bool emit_local_for_anyaddress) |
| 194 : Port(thread, | 199 : Port(thread, |
| 195 LOCAL_PORT_TYPE, | 200 LOCAL_PORT_TYPE, |
| 196 factory, | 201 factory, |
| 197 network, | 202 network, |
| 198 ip, | 203 ip, |
| 199 min_port, | 204 min_port, |
| 200 max_port, | 205 max_port, |
| 201 username, | 206 username, |
| 202 password), | 207 password), |
| 203 requests_(thread), | 208 requests_(thread), |
| 204 socket_(NULL), | 209 socket_(NULL), |
| 205 error_(0), | 210 error_(0), |
| 206 ready_(false), | 211 ready_(false), |
| 207 stun_keepalive_delay_(KEEPALIVE_DELAY), | 212 stun_keepalive_delay_(KEEPALIVE_DELAY), |
| 208 emit_localhost_for_anyaddress_(emit_localhost_for_anyaddress) { | 213 emit_local_for_anyaddress_(emit_local_for_anyaddress) { |
| 209 requests_.set_origin(origin); | 214 requests_.set_origin(origin); |
| 210 } | 215 } |
| 211 | 216 |
| 212 bool UDPPort::Init() { | 217 bool UDPPort::Init() { |
| 213 if (!SharedSocket()) { | 218 if (!SharedSocket()) { |
| 214 ASSERT(socket_ == NULL); | 219 ASSERT(socket_ == NULL); |
| 215 socket_ = socket_factory()->CreateUdpSocket( | 220 socket_ = socket_factory()->CreateUdpSocket( |
| 216 rtc::SocketAddress(ip(), 0), min_port(), max_port()); | 221 rtc::SocketAddress(ip(), 0), min_port(), max_port()); |
| 217 if (!socket_) { | 222 if (!socket_) { |
| 218 LOG_J(LS_WARNING, this) << "UDP socket creation failed"; | 223 LOG_J(LS_WARNING, this) << "UDP socket creation failed"; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 return socket_->GetOption(opt, value); | 295 return socket_->GetOption(opt, value); |
| 291 } | 296 } |
| 292 | 297 |
| 293 int UDPPort::GetError() { | 298 int UDPPort::GetError() { |
| 294 return error_; | 299 return error_; |
| 295 } | 300 } |
| 296 | 301 |
| 297 void UDPPort::OnLocalAddressReady(rtc::AsyncPacketSocket* socket, | 302 void UDPPort::OnLocalAddressReady(rtc::AsyncPacketSocket* socket, |
| 298 const rtc::SocketAddress& address) { | 303 const rtc::SocketAddress& address) { |
| 299 // When adapter enumeration is disabled and binding to the any address, the | 304 // When adapter enumeration is disabled and binding to the any address, the |
| 300 // loopback address will be issued as a candidate instead if | 305 // default local address will be issued as a candidate instead if |
| 301 // |emit_localhost_for_anyaddress| is true. This is to allow connectivity on | 306 // |emit_local_for_anyaddress| is true. This is to allow connectivity for |
| 302 // demo pages without STUN/TURN to work. | 307 // applications which absolutely requires a HOST candidate. |
| 303 rtc::SocketAddress addr = address; | 308 rtc::SocketAddress addr = address; |
| 304 if (addr.IsAnyIP() && emit_localhost_for_anyaddress_) { | 309 MaybeSetDefaultLocalAddress(&addr); |
| 305 addr.SetIP(rtc::GetLoopbackIP(addr.family())); | |
| 306 } | |
| 307 | 310 |
| 308 AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "", | 311 AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "", |
| 309 LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false); | 312 LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false); |
| 310 MaybePrepareStunCandidate(); | 313 MaybePrepareStunCandidate(); |
| 311 } | 314 } |
| 312 | 315 |
| 313 void UDPPort::OnReadPacket(rtc::AsyncPacketSocket* socket, | 316 void UDPPort::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 314 const char* data, | 317 const char* data, |
| 315 size_t size, | 318 size_t size, |
| 316 const rtc::SocketAddress& remote_addr, | 319 const rtc::SocketAddress& remote_addr, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 requests_.Send(new StunBindingRequest(this, true, stun_addr)); | 397 requests_.Send(new StunBindingRequest(this, true, stun_addr)); |
| 395 } else { | 398 } else { |
| 396 // Since we can't send stun messages to the server, we should mark this | 399 // Since we can't send stun messages to the server, we should mark this |
| 397 // port ready. | 400 // port ready. |
| 398 LOG(LS_WARNING) << "STUN server address is incompatible."; | 401 LOG(LS_WARNING) << "STUN server address is incompatible."; |
| 399 OnStunBindingOrResolveRequestFailed(stun_addr); | 402 OnStunBindingOrResolveRequestFailed(stun_addr); |
| 400 } | 403 } |
| 401 } | 404 } |
| 402 } | 405 } |
| 403 | 406 |
| 407 void UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const { |
| 408 if (!addr->IsAnyIP() || !emit_local_for_anyaddress_ || |
| 409 !Network()->default_local_address_provider()) { |
| 410 return; |
| 411 } |
| 412 rtc::IPAddress default_address; |
| 413 bool result = |
| 414 Network()->default_local_address_provider()->GetDefaultLocalAddress( |
| 415 addr->family(), &default_address); |
| 416 RTC_DCHECK(result && !default_address.IsNil()); |
| 417 addr->SetIP(default_address); |
| 418 } |
| 419 |
| 404 void UDPPort::OnStunBindingRequestSucceeded( | 420 void UDPPort::OnStunBindingRequestSucceeded( |
| 405 const rtc::SocketAddress& stun_server_addr, | 421 const rtc::SocketAddress& stun_server_addr, |
| 406 const rtc::SocketAddress& stun_reflected_addr) { | 422 const rtc::SocketAddress& stun_reflected_addr) { |
| 407 if (bind_request_succeeded_servers_.find(stun_server_addr) != | 423 if (bind_request_succeeded_servers_.find(stun_server_addr) != |
| 408 bind_request_succeeded_servers_.end()) { | 424 bind_request_succeeded_servers_.end()) { |
| 409 return; | 425 return; |
| 410 } | 426 } |
| 411 bind_request_succeeded_servers_.insert(stun_server_addr); | 427 bind_request_succeeded_servers_.insert(stun_server_addr); |
| 412 | 428 |
| 413 // If socket is shared and |stun_reflected_addr| is equal to local socket | 429 // If socket is shared and |stun_reflected_addr| is equal to local socket |
| 414 // address, or if the same address has been added by another STUN server, | 430 // address, or if the same address has been added by another STUN server, |
| 415 // then discarding the stun address. | 431 // then discarding the stun address. |
| 416 // For STUN, related address is the local socket address. | 432 // For STUN, related address is the local socket address. |
| 417 if ((!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) && | 433 if ((!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) && |
| 418 !HasCandidateWithAddress(stun_reflected_addr)) { | 434 !HasCandidateWithAddress(stun_reflected_addr)) { |
| 419 | 435 |
| 420 rtc::SocketAddress related_address = socket_->GetLocalAddress(); | 436 rtc::SocketAddress related_address = socket_->GetLocalAddress(); |
| 437 MaybeSetDefaultLocalAddress(&related_address); |
| 421 if (!(candidate_filter() & CF_HOST)) { | 438 if (!(candidate_filter() & CF_HOST)) { |
| 422 // If candidate filter doesn't have CF_HOST specified, empty raddr to | 439 // If candidate filter doesn't have CF_HOST specified, empty raddr to |
| 423 // avoid local address leakage. | 440 // avoid local address leakage. |
| 424 related_address = rtc::EmptySocketAddressWithFamily( | 441 related_address = rtc::EmptySocketAddressWithFamily( |
| 425 related_address.family()); | 442 related_address.family()); |
| 426 } | 443 } |
| 427 | 444 |
| 428 AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address, | 445 AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address, |
| 429 UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE, | 446 UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE, |
| 430 ICE_TYPE_PREFERENCE_SRFLX, 0, false); | 447 ICE_TYPE_PREFERENCE_SRFLX, 0, false); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 const std::vector<Candidate>& existing_candidates = Candidates(); | 496 const std::vector<Candidate>& existing_candidates = Candidates(); |
| 480 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); | 497 std::vector<Candidate>::const_iterator it = existing_candidates.begin(); |
| 481 for (; it != existing_candidates.end(); ++it) { | 498 for (; it != existing_candidates.end(); ++it) { |
| 482 if (it->address() == addr) | 499 if (it->address() == addr) |
| 483 return true; | 500 return true; |
| 484 } | 501 } |
| 485 return false; | 502 return false; |
| 486 } | 503 } |
| 487 | 504 |
| 488 } // namespace cricket | 505 } // namespace cricket |
| OLD | NEW |