2014-10-28 22:20:11 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/basic_packet_socket_factory.h"
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <stddef.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2016-10-25 10:15:06 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/async_stun_tcp_socket.h"
|
|
|
|
|
#include "rtc_base/async_tcp_socket.h"
|
|
|
|
|
#include "rtc_base/async_udp_socket.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/net_helpers.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "rtc_base/socket.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/socket_adapters.h"
|
|
|
|
|
#include "rtc_base/socket_server.h"
|
|
|
|
|
#include "rtc_base/ssl_adapter.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/thread.h"
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
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(
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
const SocketAddress& address,
|
|
|
|
|
uint16_t min_port,
|
|
|
|
|
uint16_t max_port) {
|
2014-10-28 22:20:11 +00:00
|
|
|
// UDP sockets are simple.
|
2016-10-25 10:15:06 -07:00
|
|
|
AsyncSocket* socket =
|
|
|
|
|
socket_factory()->CreateAsyncSocket(address.family(), SOCK_DGRAM);
|
2014-10-28 22:20:11 +00:00
|
|
|
if (!socket) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (BindSocket(socket, address, min_port, max_port) < 0) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "UDP bind failed with error " << socket->GetError();
|
2014-10-28 22:20:11 +00:00
|
|
|
delete socket;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-10-25 10:15:06 -07:00
|
|
|
return new AsyncUDPSocket(socket);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
const SocketAddress& local_address,
|
|
|
|
|
uint16_t min_port,
|
|
|
|
|
uint16_t max_port,
|
2014-11-06 20:19:22 +00:00
|
|
|
int opts) {
|
2014-10-28 22:20:11 +00:00
|
|
|
// Fail if TLS is required.
|
|
|
|
|
if (opts & PacketSocketFactory::OPT_TLS) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "TLS support currently is not available.";
|
2014-10-28 22:20:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-25 10:15:06 -07:00
|
|
|
AsyncSocket* socket =
|
|
|
|
|
socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
|
2014-10-28 22:20:11 +00:00
|
|
|
if (!socket) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BindSocket(socket, local_address, min_port, max_port) < 0) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
|
2014-10-28 22:20:11 +00:00
|
|
|
delete socket;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 08:35:45 -08:00
|
|
|
// If using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
|
|
|
|
|
if (opts & PacketSocketFactory::OPT_TLS_FAKE) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(!(opts & PacketSocketFactory::OPT_TLS));
|
2016-10-25 10:15:06 -07:00
|
|
|
socket = new AsyncSSLSocket(socket);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
|
|
|
|
|
// See http://go/gtalktcpnodelayexperiment
|
2016-10-25 10:15:06 -07:00
|
|
|
socket->SetOption(Socket::OPT_NODELAY, 1);
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
if (opts & PacketSocketFactory::OPT_STUN)
|
|
|
|
|
return new cricket::AsyncStunTCPSocket(socket, true);
|
|
|
|
|
|
2016-10-25 10:15:06 -07:00
|
|
|
return new AsyncTCPSocket(socket, true);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
|
2017-08-29 12:18:32 -07:00
|
|
|
const SocketAddress& local_address,
|
|
|
|
|
const SocketAddress& remote_address,
|
|
|
|
|
const ProxyInfo& proxy_info,
|
|
|
|
|
const std::string& user_agent,
|
|
|
|
|
const PacketSocketTcpOptions& tcp_options) {
|
2016-10-25 10:15:06 -07:00
|
|
|
AsyncSocket* socket =
|
2014-10-28 22:20:11 +00:00
|
|
|
socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
|
|
|
|
|
if (!socket) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BindSocket(socket, local_address, 0, 0) < 0) {
|
2017-06-13 15:49:45 -07:00
|
|
|
// 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()) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "TCP bind failed with error " << socket->GetError()
|
|
|
|
|
<< "; ignoring since socket is using 'any' address.";
|
2017-06-13 15:49:45 -07:00
|
|
|
} else {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
|
2017-06-13 15:49:45 -07:00
|
|
|
delete socket;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2017-03-23 15:45:49 -07:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 08:35:45 -08:00
|
|
|
// Assert that at most one TLS option is used.
|
2017-08-29 12:18:32 -07:00
|
|
|
int tlsOpts = tcp_options.opts & (PacketSocketFactory::OPT_TLS |
|
|
|
|
|
PacketSocketFactory::OPT_TLS_FAKE |
|
|
|
|
|
PacketSocketFactory::OPT_TLS_INSECURE);
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0);
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2017-01-09 08:35:45 -08:00
|
|
|
if ((tlsOpts & PacketSocketFactory::OPT_TLS) ||
|
|
|
|
|
(tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) {
|
|
|
|
|
// Using TLS, wrap the socket in an SSL adapter.
|
2016-10-25 10:15:06 -07:00
|
|
|
SSLAdapter* ssl_adapter = SSLAdapter::Create(socket);
|
2014-10-28 22:20:11 +00:00
|
|
|
if (!ssl_adapter) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 10:45:38 +00:00
|
|
|
if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) {
|
|
|
|
|
ssl_adapter->SetIgnoreBadCert(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ssl_adapter->SetAlpnProtocols(tcp_options.tls_alpn_protocols);
|
|
|
|
|
ssl_adapter->SetEllipticCurves(tcp_options.tls_elliptic_curves);
|
2018-05-08 13:12:25 -07:00
|
|
|
ssl_adapter->SetCertVerifier(tcp_options.tls_cert_verifier);
|
2017-08-29 12:18:32 -07:00
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
socket = ssl_adapter;
|
|
|
|
|
|
|
|
|
|
if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) {
|
|
|
|
|
delete ssl_adapter;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 08:35:45 -08:00
|
|
|
} else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) {
|
|
|
|
|
// Using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
|
2016-10-25 10:15:06 -07:00
|
|
|
socket = new AsyncSSLSocket(socket);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (socket->Connect(remote_address) < 0) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "TCP connect failed with error " << socket->GetError();
|
2014-10-28 22:20:11 +00:00
|
|
|
delete socket;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finally, wrap that socket in a TCP or STUN TCP packet socket.
|
|
|
|
|
AsyncPacketSocket* tcp_socket;
|
2017-08-29 12:18:32 -07:00
|
|
|
if (tcp_options.opts & PacketSocketFactory::OPT_STUN) {
|
2014-10-28 22:20:11 +00:00
|
|
|
tcp_socket = new cricket::AsyncStunTCPSocket(socket, false);
|
|
|
|
|
} else {
|
2016-10-25 10:15:06 -07:00
|
|
|
tcp_socket = new AsyncTCPSocket(socket, false);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
|
|
|
|
|
// See http://go/gtalktcpnodelayexperiment
|
2016-10-25 10:15:06 -07:00
|
|
|
tcp_socket->SetOption(Socket::OPT_NODELAY, 1);
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
return tcp_socket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
|
2016-10-25 10:15:06 -07:00
|
|
|
return new AsyncResolver();
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket,
|
|
|
|
|
const SocketAddress& local_address,
|
|
|
|
|
uint16_t min_port,
|
|
|
|
|
uint16_t max_port) {
|
2014-10-28 22:20:11 +00:00
|
|
|
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) {
|
2016-10-25 10:15:06 -07:00
|
|
|
ret = socket->Bind(SocketAddress(local_address.ipaddr(), port));
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SocketFactory* BasicPacketSocketFactory::socket_factory() {
|
|
|
|
|
if (thread_) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(thread_ == Thread::Current());
|
2014-10-28 22:20:11 +00:00
|
|
|
return thread_->socketserver();
|
|
|
|
|
} else {
|
|
|
|
|
return socket_factory_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|