This CL adds the flag "PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS", which will force the creation of ports not bound to any specific network interface. These are normally only used when network enumeration fails or is disabled, but in some circumstances (such as the one the test case adds), they're the only thing that works. This will result in extra ports being gathered, which is why it's only enabled behind a flag for now. In the future, we could probably introduce more sophisticated "pruning" logic that would lessen the impact of the extra ports when they're redundant, and make the flag the default. Some other minor changes that were required to make this use case work: * Allow a TCPPort to be used for outgoing connections even if it tries and fails to create a server socket. * Allow Bind to fail if being called before Connect, and the IP is an "any" address (0.0.0.0 or ::), since this bind would have been mostly pointless anyway. * Prevent P2PTransprotChannel from keeping a "backup" candidate pair using an "any address" network; we only want this for actual networks. BUG=webrtc:7798 Review-Url: https://codereview.webrtc.org/2936553003 Cr-Commit-Position: refs/heads/master@{#18578}
223 lines
6.9 KiB
C++
223 lines
6.9 KiB
C++
/*
|
|
* Copyright 2011 The WebRTC Project Authors. All rights reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "webrtc/p2p/base/basicpacketsocketfactory.h"
|
|
|
|
#include <string>
|
|
|
|
#include "webrtc/p2p/base/asyncstuntcpsocket.h"
|
|
#include "webrtc/p2p/base/stun.h"
|
|
#include "webrtc/base/asynctcpsocket.h"
|
|
#include "webrtc/base/asyncudpsocket.h"
|
|
#include "webrtc/base/checks.h"
|
|
#include "webrtc/base/logging.h"
|
|
#include "webrtc/base/nethelpers.h"
|
|
#include "webrtc/base/physicalsocketserver.h"
|
|
#include "webrtc/base/socketadapters.h"
|
|
#include "webrtc/base/ssladapter.h"
|
|
#include "webrtc/base/thread.h"
|
|
|
|
namespace rtc {
|
|
|
|
BasicPacketSocketFactory::BasicPacketSocketFactory()
|
|
: thread_(Thread::Current()),
|
|
socket_factory_(NULL) {
|
|
}
|
|
|
|
BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread)
|
|
: thread_(thread),
|
|
socket_factory_(NULL) {
|
|
}
|
|
|
|
BasicPacketSocketFactory::BasicPacketSocketFactory(
|
|
SocketFactory* socket_factory)
|
|
: thread_(NULL),
|
|
socket_factory_(socket_factory) {
|
|
}
|
|
|
|
BasicPacketSocketFactory::~BasicPacketSocketFactory() {
|
|
}
|
|
|
|
AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
|
|
const SocketAddress& address,
|
|
uint16_t min_port,
|
|
uint16_t max_port) {
|
|
// UDP sockets are simple.
|
|
AsyncSocket* socket =
|
|
socket_factory()->CreateAsyncSocket(address.family(), SOCK_DGRAM);
|
|
if (!socket) {
|
|
return NULL;
|
|
}
|
|
if (BindSocket(socket, address, min_port, max_port) < 0) {
|
|
LOG(LS_ERROR) << "UDP bind failed with error "
|
|
<< socket->GetError();
|
|
delete socket;
|
|
return NULL;
|
|
}
|
|
return new AsyncUDPSocket(socket);
|
|
}
|
|
|
|
AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
|
|
const SocketAddress& local_address,
|
|
uint16_t min_port,
|
|
uint16_t max_port,
|
|
int opts) {
|
|
// Fail if TLS is required.
|
|
if (opts & PacketSocketFactory::OPT_TLS) {
|
|
LOG(LS_ERROR) << "TLS support currently is not available.";
|
|
return NULL;
|
|
}
|
|
|
|
AsyncSocket* socket =
|
|
socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
|
|
if (!socket) {
|
|
return NULL;
|
|
}
|
|
|
|
if (BindSocket(socket, local_address, min_port, max_port) < 0) {
|
|
LOG(LS_ERROR) << "TCP bind failed with error "
|
|
<< socket->GetError();
|
|
delete socket;
|
|
return NULL;
|
|
}
|
|
|
|
// If using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
|
|
if (opts & PacketSocketFactory::OPT_TLS_FAKE) {
|
|
RTC_DCHECK(!(opts & PacketSocketFactory::OPT_TLS));
|
|
socket = new AsyncSSLSocket(socket);
|
|
}
|
|
|
|
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
|
|
// See http://go/gtalktcpnodelayexperiment
|
|
socket->SetOption(Socket::OPT_NODELAY, 1);
|
|
|
|
if (opts & PacketSocketFactory::OPT_STUN)
|
|
return new cricket::AsyncStunTCPSocket(socket, true);
|
|
|
|
return new AsyncTCPSocket(socket, true);
|
|
}
|
|
|
|
AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
|
|
const SocketAddress& local_address, const SocketAddress& remote_address,
|
|
const ProxyInfo& proxy_info, const std::string& user_agent, int opts) {
|
|
AsyncSocket* socket =
|
|
socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
|
|
if (!socket) {
|
|
return NULL;
|
|
}
|
|
|
|
if (BindSocket(socket, local_address, 0, 0) < 0) {
|
|
// Allow BindSocket to fail if we're binding to the ANY address, since this
|
|
// is mostly redundant in the first place. The socket will be bound when we
|
|
// call Connect() instead.
|
|
if (local_address.IsAnyIP()) {
|
|
LOG(LS_WARNING) << "TCP bind failed with error " << socket->GetError()
|
|
<< "; ignoring since socket is using 'any' address.";
|
|
} else {
|
|
LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
|
|
delete socket;
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
// If using a proxy, wrap the socket in a proxy socket.
|
|
if (proxy_info.type == PROXY_SOCKS5) {
|
|
socket = new AsyncSocksProxySocket(
|
|
socket, proxy_info.address, proxy_info.username, proxy_info.password);
|
|
} else if (proxy_info.type == PROXY_HTTPS) {
|
|
socket =
|
|
new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address,
|
|
proxy_info.username, proxy_info.password);
|
|
}
|
|
|
|
// Assert that at most one TLS option is used.
|
|
int tlsOpts =
|
|
opts & (PacketSocketFactory::OPT_TLS | PacketSocketFactory::OPT_TLS_FAKE |
|
|
PacketSocketFactory::OPT_TLS_INSECURE);
|
|
RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0);
|
|
|
|
if ((tlsOpts & PacketSocketFactory::OPT_TLS) ||
|
|
(tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) {
|
|
// Using TLS, wrap the socket in an SSL adapter.
|
|
SSLAdapter* ssl_adapter = SSLAdapter::Create(socket);
|
|
if (!ssl_adapter) {
|
|
return NULL;
|
|
}
|
|
|
|
if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) {
|
|
ssl_adapter->set_ignore_bad_cert(true);
|
|
}
|
|
|
|
socket = ssl_adapter;
|
|
|
|
if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) {
|
|
delete ssl_adapter;
|
|
return NULL;
|
|
}
|
|
|
|
} else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) {
|
|
// Using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
|
|
socket = new AsyncSSLSocket(socket);
|
|
}
|
|
|
|
if (socket->Connect(remote_address) < 0) {
|
|
LOG(LS_ERROR) << "TCP connect failed with error "
|
|
<< socket->GetError();
|
|
delete socket;
|
|
return NULL;
|
|
}
|
|
|
|
// Finally, wrap that socket in a TCP or STUN TCP packet socket.
|
|
AsyncPacketSocket* tcp_socket;
|
|
if (opts & PacketSocketFactory::OPT_STUN) {
|
|
tcp_socket = new cricket::AsyncStunTCPSocket(socket, false);
|
|
} else {
|
|
tcp_socket = new AsyncTCPSocket(socket, false);
|
|
}
|
|
|
|
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
|
|
// See http://go/gtalktcpnodelayexperiment
|
|
tcp_socket->SetOption(Socket::OPT_NODELAY, 1);
|
|
|
|
return tcp_socket;
|
|
}
|
|
|
|
AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
|
|
return new AsyncResolver();
|
|
}
|
|
|
|
int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket,
|
|
const SocketAddress& local_address,
|
|
uint16_t min_port,
|
|
uint16_t max_port) {
|
|
int ret = -1;
|
|
if (min_port == 0 && max_port == 0) {
|
|
// If there's no port range, let the OS pick a port for us.
|
|
ret = socket->Bind(local_address);
|
|
} else {
|
|
// Otherwise, try to find a port in the provided range.
|
|
for (int port = min_port; ret < 0 && port <= max_port; ++port) {
|
|
ret = socket->Bind(SocketAddress(local_address.ipaddr(), port));
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
SocketFactory* BasicPacketSocketFactory::socket_factory() {
|
|
if (thread_) {
|
|
RTC_DCHECK(thread_ == Thread::Current());
|
|
return thread_->socketserver();
|
|
} else {
|
|
return socket_factory_;
|
|
}
|
|
}
|
|
|
|
} // namespace rtc
|