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>
|
|
|
|
|
|
2021-10-19 10:11:02 +02:00
|
|
|
#include "absl/memory/memory.h"
|
2021-10-14 13:57:07 +00:00
|
|
|
#include "api/async_dns_resolver.h"
|
|
|
|
|
#include "api/wrapping_async_dns_resolver.h"
|
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"
|
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/ssl_adapter.h"
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
BasicPacketSocketFactory::BasicPacketSocketFactory(
|
|
|
|
|
SocketFactory* socket_factory)
|
2021-09-07 09:16:49 +02:00
|
|
|
: socket_factory_(socket_factory) {}
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
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.
|
2021-09-07 09:16:49 +02:00
|
|
|
Socket* socket = socket_factory_->CreateSocket(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
|
|
|
}
|
|
|
|
|
|
2021-10-06 11:19:03 +02:00
|
|
|
AsyncListenSocket* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 16:55:13 +02:00
|
|
|
if (opts & PacketSocketFactory::OPT_TLS_FAKE) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Fake TLS not supported.";
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2021-08-12 10:32:30 +02:00
|
|
|
Socket* socket =
|
2021-09-07 09:16:49 +02:00
|
|
|
socket_factory_->CreateSocket(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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 16:27:56 +02:00
|
|
|
RTC_CHECK(!(opts & PacketSocketFactory::OPT_STUN));
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2021-10-19 10:11:02 +02:00
|
|
|
return new AsyncTcpListenSocket(absl::WrapUnique(socket));
|
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) {
|
2021-08-12 10:32:30 +02:00
|
|
|
Socket* socket =
|
2021-09-07 09:16:49 +02:00
|
|
|
socket_factory_->CreateSocket(local_address.family(), SOCK_STREAM);
|
2014-10-28 22:20:11 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-11-25 15:24:53 -08:00
|
|
|
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance; this causes
|
|
|
|
|
// small media packets to be sent immediately rather than being buffered up,
|
|
|
|
|
// reducing latency.
|
|
|
|
|
//
|
|
|
|
|
// Must be done before calling Connect, otherwise it may fail.
|
|
|
|
|
if (socket->SetOption(Socket::OPT_NODELAY, 1) != 0) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Setting TCP_NODELAY option failed with error "
|
|
|
|
|
<< socket->GetError();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2020-04-11 00:01:43 +02:00
|
|
|
if (ssl_adapter->StartSSL(remote_address.hostname().c_str()) != 0) {
|
2014-10-28 22:20:11 +00:00
|
|
|
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) {
|
2021-09-17 16:27:56 +02:00
|
|
|
tcp_socket = new cricket::AsyncStunTCPSocket(socket);
|
2014-10-28 22:20:11 +00:00
|
|
|
} else {
|
2021-10-19 10:11:02 +02:00
|
|
|
tcp_socket = new AsyncTCPSocket(socket);
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tcp_socket;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 15:05:33 +00:00
|
|
|
AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
|
|
|
|
|
return new AsyncResolver();
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-14 13:57:07 +00:00
|
|
|
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
|
|
|
|
|
BasicPacketSocketFactory::CreateAsyncDnsResolver() {
|
|
|
|
|
return std::make_unique<webrtc::WrappingAsyncDnsResolver>(
|
|
|
|
|
new AsyncResolver());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 10:32:30 +02:00
|
|
|
int BasicPacketSocketFactory::BindSocket(Socket* socket,
|
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-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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|