| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 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/turnport.h" | 11 #include "webrtc/p2p/base/turnport.h" |
| 12 | 12 |
| 13 #include <algorithm> |
| 13 #include <functional> | 14 #include <functional> |
| 14 | 15 |
| 15 #include "webrtc/p2p/base/common.h" | 16 #include "webrtc/p2p/base/common.h" |
| 16 #include "webrtc/p2p/base/stun.h" | 17 #include "webrtc/p2p/base/stun.h" |
| 17 #include "webrtc/rtc_base/asyncpacketsocket.h" | 18 #include "webrtc/rtc_base/asyncpacketsocket.h" |
| 18 #include "webrtc/rtc_base/byteorder.h" | 19 #include "webrtc/rtc_base/byteorder.h" |
| 19 #include "webrtc/rtc_base/checks.h" | 20 #include "webrtc/rtc_base/checks.h" |
| 20 #include "webrtc/rtc_base/logging.h" | 21 #include "webrtc/rtc_base/logging.h" |
| 21 #include "webrtc/rtc_base/nethelpers.h" | 22 #include "webrtc/rtc_base/nethelpers.h" |
| 22 #include "webrtc/rtc_base/ptr_util.h" | 23 #include "webrtc/rtc_base/ptr_util.h" |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 const std::string& username, | 188 const std::string& username, |
| 188 const std::string& password, | 189 const std::string& password, |
| 189 const ProtocolAddress& server_address, | 190 const ProtocolAddress& server_address, |
| 190 const RelayCredentials& credentials, | 191 const RelayCredentials& credentials, |
| 191 int server_priority, | 192 int server_priority, |
| 192 const std::string& origin) | 193 const std::string& origin) |
| 193 : Port(thread, | 194 : Port(thread, |
| 194 RELAY_PORT_TYPE, | 195 RELAY_PORT_TYPE, |
| 195 factory, | 196 factory, |
| 196 network, | 197 network, |
| 197 socket->GetLocalAddress().ipaddr(), | |
| 198 username, | 198 username, |
| 199 password), | 199 password), |
| 200 server_address_(server_address), | 200 server_address_(server_address), |
| 201 credentials_(credentials), | 201 credentials_(credentials), |
| 202 socket_(socket), | 202 socket_(socket), |
| 203 resolver_(NULL), | 203 resolver_(NULL), |
| 204 error_(0), | 204 error_(0), |
| 205 request_manager_(thread), | 205 request_manager_(thread), |
| 206 next_channel_number_(TURN_CHANNEL_NUMBER_START), | 206 next_channel_number_(TURN_CHANNEL_NUMBER_START), |
| 207 state_(STATE_CONNECTING), | 207 state_(STATE_CONNECTING), |
| 208 server_priority_(server_priority), | 208 server_priority_(server_priority), |
| 209 allocate_mismatch_retries_(0) { | 209 allocate_mismatch_retries_(0) { |
| 210 request_manager_.SignalSendPacket.connect(this, &TurnPort::OnSendStunPacket); | 210 request_manager_.SignalSendPacket.connect(this, &TurnPort::OnSendStunPacket); |
| 211 request_manager_.set_origin(origin); | 211 request_manager_.set_origin(origin); |
| 212 } | 212 } |
| 213 | 213 |
| 214 TurnPort::TurnPort(rtc::Thread* thread, | 214 TurnPort::TurnPort(rtc::Thread* thread, |
| 215 rtc::PacketSocketFactory* factory, | 215 rtc::PacketSocketFactory* factory, |
| 216 rtc::Network* network, | 216 rtc::Network* network, |
| 217 const rtc::IPAddress& ip, | |
| 218 uint16_t min_port, | 217 uint16_t min_port, |
| 219 uint16_t max_port, | 218 uint16_t max_port, |
| 220 const std::string& username, | 219 const std::string& username, |
| 221 const std::string& password, | 220 const std::string& password, |
| 222 const ProtocolAddress& server_address, | 221 const ProtocolAddress& server_address, |
| 223 const RelayCredentials& credentials, | 222 const RelayCredentials& credentials, |
| 224 int server_priority, | 223 int server_priority, |
| 225 const std::string& origin) | 224 const std::string& origin) |
| 226 : Port(thread, | 225 : Port(thread, |
| 227 RELAY_PORT_TYPE, | 226 RELAY_PORT_TYPE, |
| 228 factory, | 227 factory, |
| 229 network, | 228 network, |
| 230 ip, | |
| 231 min_port, | 229 min_port, |
| 232 max_port, | 230 max_port, |
| 233 username, | 231 username, |
| 234 password), | 232 password), |
| 235 server_address_(server_address), | 233 server_address_(server_address), |
| 236 credentials_(credentials), | 234 credentials_(credentials), |
| 237 socket_(NULL), | 235 socket_(NULL), |
| 238 resolver_(NULL), | 236 resolver_(NULL), |
| 239 error_(0), | 237 error_(0), |
| 240 request_manager_(thread), | 238 request_manager_(thread), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 server_address_.address.SetPort(TURN_DEFAULT_PORT); | 284 server_address_.address.SetPort(TURN_DEFAULT_PORT); |
| 287 } | 285 } |
| 288 | 286 |
| 289 if (server_address_.address.IsUnresolvedIP()) { | 287 if (server_address_.address.IsUnresolvedIP()) { |
| 290 ResolveTurnAddress(server_address_.address); | 288 ResolveTurnAddress(server_address_.address); |
| 291 } else { | 289 } else { |
| 292 // If protocol family of server address doesn't match with local, return. | 290 // If protocol family of server address doesn't match with local, return. |
| 293 if (!IsCompatibleAddress(server_address_.address)) { | 291 if (!IsCompatibleAddress(server_address_.address)) { |
| 294 LOG(LS_ERROR) << "IP address family does not match: " | 292 LOG(LS_ERROR) << "IP address family does not match: " |
| 295 << "server: " << server_address_.address.family() | 293 << "server: " << server_address_.address.family() |
| 296 << " local: " << ip().family(); | 294 << " local: " << Network()->GetBestIP().family(); |
| 297 OnAllocateError(); | 295 OnAllocateError(); |
| 298 return; | 296 return; |
| 299 } | 297 } |
| 300 | 298 |
| 301 // Insert the current address to prevent redirection pingpong. | 299 // Insert the current address to prevent redirection pingpong. |
| 302 attempted_server_addresses_.insert(server_address_.address); | 300 attempted_server_addresses_.insert(server_address_.address); |
| 303 | 301 |
| 304 LOG_J(LS_INFO, this) << "Trying to connect to TURN server via " | 302 LOG_J(LS_INFO, this) << "Trying to connect to TURN server via " |
| 305 << ProtoToString(server_address_.proto) << " @ " | 303 << ProtoToString(server_address_.proto) << " @ " |
| 306 << server_address_.address.ToSensitiveString(); | 304 << server_address_.address.ToSensitiveString(); |
| 307 if (!CreateTurnClientSocket()) { | 305 if (!CreateTurnClientSocket()) { |
| 308 LOG(LS_ERROR) << "Failed to create TURN client socket"; | 306 LOG(LS_ERROR) << "Failed to create TURN client socket"; |
| 309 OnAllocateError(); | 307 OnAllocateError(); |
| 310 return; | 308 return; |
| 311 } | 309 } |
| 312 if (server_address_.proto == PROTO_UDP) { | 310 if (server_address_.proto == PROTO_UDP) { |
| 313 // If its UDP, send AllocateRequest now. | 311 // If its UDP, send AllocateRequest now. |
| 314 // For TCP and TLS AllcateRequest will be sent by OnSocketConnect. | 312 // For TCP and TLS AllcateRequest will be sent by OnSocketConnect. |
| 315 SendRequest(new TurnAllocateRequest(this), 0); | 313 SendRequest(new TurnAllocateRequest(this), 0); |
| 316 } | 314 } |
| 317 } | 315 } |
| 318 } | 316 } |
| 319 | 317 |
| 320 bool TurnPort::CreateTurnClientSocket() { | 318 bool TurnPort::CreateTurnClientSocket() { |
| 321 RTC_DCHECK(!socket_ || SharedSocket()); | 319 RTC_DCHECK(!socket_ || SharedSocket()); |
| 322 | 320 |
| 323 if (server_address_.proto == PROTO_UDP && !SharedSocket()) { | 321 if (server_address_.proto == PROTO_UDP && !SharedSocket()) { |
| 324 socket_ = socket_factory()->CreateUdpSocket( | 322 socket_ = socket_factory()->CreateUdpSocket( |
| 325 rtc::SocketAddress(ip(), 0), min_port(), max_port()); | 323 rtc::SocketAddress(Network()->GetBestIP(), 0), min_port(), max_port()); |
| 326 } else if (server_address_.proto == PROTO_TCP || | 324 } else if (server_address_.proto == PROTO_TCP || |
| 327 server_address_.proto == PROTO_TLS) { | 325 server_address_.proto == PROTO_TLS) { |
| 328 RTC_DCHECK(!SharedSocket()); | 326 RTC_DCHECK(!SharedSocket()); |
| 329 int opts = rtc::PacketSocketFactory::OPT_STUN; | 327 int opts = rtc::PacketSocketFactory::OPT_STUN; |
| 330 | 328 |
| 331 // Apply server address TLS and insecure bits to options. | 329 // Apply server address TLS and insecure bits to options. |
| 332 if (server_address_.proto == PROTO_TLS) { | 330 if (server_address_.proto == PROTO_TLS) { |
| 333 if (tls_cert_policy_ == | 331 if (tls_cert_policy_ == |
| 334 TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK) { | 332 TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK) { |
| 335 opts |= rtc::PacketSocketFactory::OPT_TLS_INSECURE; | 333 opts |= rtc::PacketSocketFactory::OPT_TLS_INSECURE; |
| 336 } else { | 334 } else { |
| 337 opts |= rtc::PacketSocketFactory::OPT_TLS; | 335 opts |= rtc::PacketSocketFactory::OPT_TLS; |
| 338 } | 336 } |
| 339 } | 337 } |
| 340 | 338 |
| 341 socket_ = socket_factory()->CreateClientTcpSocket( | 339 socket_ = socket_factory()->CreateClientTcpSocket( |
| 342 rtc::SocketAddress(ip(), 0), server_address_.address, | 340 rtc::SocketAddress(Network()->GetBestIP(), 0), server_address_.address, |
| 343 proxy(), user_agent(), opts); | 341 proxy(), user_agent(), opts); |
| 344 } | 342 } |
| 345 | 343 |
| 346 if (!socket_) { | 344 if (!socket_) { |
| 347 error_ = SOCKET_ERROR; | 345 error_ = SOCKET_ERROR; |
| 348 return false; | 346 return false; |
| 349 } | 347 } |
| 350 | 348 |
| 351 // Apply options if any. | 349 // Apply options if any. |
| 352 for (SocketOptionsMap::iterator iter = socket_options_.begin(); | 350 for (SocketOptionsMap::iterator iter = socket_options_.begin(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 374 } | 372 } |
| 375 return true; | 373 return true; |
| 376 } | 374 } |
| 377 | 375 |
| 378 void TurnPort::OnSocketConnect(rtc::AsyncPacketSocket* socket) { | 376 void TurnPort::OnSocketConnect(rtc::AsyncPacketSocket* socket) { |
| 379 // This slot should only be invoked if we're using a connection-oriented | 377 // This slot should only be invoked if we're using a connection-oriented |
| 380 // protocol. | 378 // protocol. |
| 381 RTC_DCHECK(server_address_.proto == PROTO_TCP || | 379 RTC_DCHECK(server_address_.proto == PROTO_TCP || |
| 382 server_address_.proto == PROTO_TLS); | 380 server_address_.proto == PROTO_TLS); |
| 383 | 381 |
| 384 // Do not use this port if the socket bound to a different address than | 382 // Do not use this port if the socket bound to an address not associated with |
| 385 // the one we asked for. This is seen in Chrome, where TCP sockets cannot be | 383 // the desired network interface. This is seen in Chrome, where TCP sockets |
| 386 // given a binding address, and the platform is expected to pick the | 384 // cannot be given a binding address, and the platform is expected to pick |
| 387 // correct local address. | 385 // the correct local address. |
| 388 | 386 // |
| 389 // However, there are two situations in which we allow the bound address to | 387 // However, there are two situations in which we allow the bound address to |
| 390 // differ from the requested address: 1. The bound address is the loopback | 388 // not be one of the addresses of the requested interface: |
| 391 // address. This happens when a proxy forces TCP to bind to only the | 389 // 1. The bound address is the loopback address. This happens when a proxy |
| 392 // localhost address (see issue 3927). 2. The bound address is the "any | 390 // forces TCP to bind to only the localhost address (see issue 3927). |
| 393 // address". This happens when multiple_routes is disabled (see issue 4780). | 391 // 2. The bound address is the "any address". This happens when |
| 394 if (socket->GetLocalAddress().ipaddr() != ip()) { | 392 // multiple_routes is disabled (see issue 4780). |
| 393 // |
| 394 // Note that, aside from minor differences in log statements, this logic is |
| 395 // identical to that in TcpPort. |
| 396 const rtc::SocketAddress& socket_address = socket->GetLocalAddress(); |
| 397 const std::vector<rtc::InterfaceAddress>& desired_addresses = |
| 398 Network()->GetIPs(); |
| 399 if (std::find(desired_addresses.begin(), desired_addresses.end(), |
| 400 socket_address.ipaddr()) == desired_addresses.end()) { |
| 395 if (socket->GetLocalAddress().IsLoopbackIP()) { | 401 if (socket->GetLocalAddress().IsLoopbackIP()) { |
| 396 LOG(LS_WARNING) << "Socket is bound to a different address:" | 402 LOG(LS_WARNING) << "Socket is bound to the address:" |
| 397 << socket->GetLocalAddress().ipaddr().ToString() | 403 << socket_address.ipaddr().ToString() |
| 398 << ", rather then the local port:" << ip().ToString() | 404 << ", rather then an address associated with network:" |
| 405 << Network()->ToString() |
| 399 << ". Still allowing it since it's localhost."; | 406 << ". Still allowing it since it's localhost."; |
| 400 } else if (IPIsAny(ip())) { | 407 } else if (IPIsAny(Network()->GetBestIP())) { |
| 401 LOG(LS_WARNING) << "Socket is bound to a different address:" | 408 LOG(LS_WARNING) << "Socket is bound to the address:" |
| 402 << socket->GetLocalAddress().ipaddr().ToString() | 409 << socket_address.ipaddr().ToString() |
| 403 << ", rather then the local port:" << ip().ToString() | 410 << ", rather then an address associated with network:" |
| 404 << ". Still allowing it since it's any address" | 411 << Network()->ToString() |
| 412 << ". Still allowing it since it's the 'any' address" |
| 405 << ", possibly caused by multiple_routes being disabled."; | 413 << ", possibly caused by multiple_routes being disabled."; |
| 406 } else { | 414 } else { |
| 407 LOG(LS_WARNING) << "Socket is bound to a different address:" | 415 LOG(LS_WARNING) << "Socket is bound to the address:" |
| 408 << socket->GetLocalAddress().ipaddr().ToString() | 416 << socket_address.ipaddr().ToString() |
| 409 << ", rather then the local port:" << ip().ToString() | 417 << ", rather then an address associated with network:" |
| 410 << ". Discarding TURN port."; | 418 << Network()->ToString() << ". Discarding TURN port."; |
| 411 OnAllocateError(); | 419 OnAllocateError(); |
| 412 return; | 420 return; |
| 413 } | 421 } |
| 414 } | 422 } |
| 415 | 423 |
| 416 state_ = STATE_CONNECTED; // It is ready to send stun requests. | 424 state_ = STATE_CONNECTED; // It is ready to send stun requests. |
| 417 if (server_address_.address.IsUnresolvedIP()) { | 425 if (server_address_.address.IsUnresolvedIP()) { |
| 418 server_address_.address = socket_->GetRemoteAddress(); | 426 server_address_.address = socket_->GetRemoteAddress(); |
| 419 } | 427 } |
| 420 | 428 |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 if (!CreateTurnClientSocket()) { | 702 if (!CreateTurnClientSocket()) { |
| 695 OnAllocateError(); | 703 OnAllocateError(); |
| 696 } | 704 } |
| 697 return; | 705 return; |
| 698 } | 706 } |
| 699 | 707 |
| 700 // Copy the original server address in |resolved_address|. For TLS based | 708 // Copy the original server address in |resolved_address|. For TLS based |
| 701 // sockets we need hostname along with resolved address. | 709 // sockets we need hostname along with resolved address. |
| 702 rtc::SocketAddress resolved_address = server_address_.address; | 710 rtc::SocketAddress resolved_address = server_address_.address; |
| 703 if (resolver_->GetError() != 0 || | 711 if (resolver_->GetError() != 0 || |
| 704 !resolver_->GetResolvedAddress(ip().family(), &resolved_address)) { | 712 !resolver_->GetResolvedAddress(Network()->GetBestIP().family(), |
| 713 &resolved_address)) { |
| 705 LOG_J(LS_WARNING, this) << "TURN host lookup received error " | 714 LOG_J(LS_WARNING, this) << "TURN host lookup received error " |
| 706 << resolver_->GetError(); | 715 << resolver_->GetError(); |
| 707 error_ = resolver_->GetError(); | 716 error_ = resolver_->GetError(); |
| 708 OnAllocateError(); | 717 OnAllocateError(); |
| 709 return; | 718 return; |
| 710 } | 719 } |
| 711 // Signal needs both resolved and unresolved address. After signal is sent | 720 // Signal needs both resolved and unresolved address. After signal is sent |
| 712 // we can copy resolved address back into |server_address_|. | 721 // we can copy resolved address back into |server_address_|. |
| 713 SignalResolvedServerAddress(this, server_address_.address, | 722 SignalResolvedServerAddress(this, server_address_.address, |
| 714 resolved_address); | 723 resolved_address); |
| (...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1582 } else { | 1591 } else { |
| 1583 state_ = STATE_UNBOUND; | 1592 state_ = STATE_UNBOUND; |
| 1584 port_->FailAndPruneConnection(ext_addr_); | 1593 port_->FailAndPruneConnection(ext_addr_); |
| 1585 } | 1594 } |
| 1586 } | 1595 } |
| 1587 void TurnEntry::OnChannelBindTimeout() { | 1596 void TurnEntry::OnChannelBindTimeout() { |
| 1588 state_ = STATE_UNBOUND; | 1597 state_ = STATE_UNBOUND; |
| 1589 port_->FailAndPruneConnection(ext_addr_); | 1598 port_->FailAndPruneConnection(ext_addr_); |
| 1590 } | 1599 } |
| 1591 } // namespace cricket | 1600 } // namespace cricket |
| OLD | NEW |