1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* 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 "p2p/base/basic_packet_socket_factory.h"
#include <stddef.h>
#include <cstdint>
#include <memory>
#include <string>
#include "absl/memory/memory.h"
#include "api/async_dns_resolver.h"
#include "api/packet_socket_factory.h"
#include "p2p/base/async_stun_tcp_socket.h"
#include "rtc_base/async_dns_resolver.h"
#include "rtc_base/async_packet_socket.h"
#include "rtc_base/async_tcp_socket.h"
#include "rtc_base/async_udp_socket.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/socket.h"
#include "rtc_base/socket_adapters.h"
#include "rtc_base/socket_address.h"
#include "rtc_base/socket_factory.h"
#include "rtc_base/ssl_adapter.h"
namespace webrtc {
BasicPacketSocketFactory::BasicPacketSocketFactory(
SocketFactory* socket_factory)
: socket_factory_(socket_factory) {}
BasicPacketSocketFactory::~BasicPacketSocketFactory() {}
AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
const SocketAddress& address,
uint16_t min_port,
uint16_t max_port) {
// UDP sockets are simple.
Socket* socket = socket_factory_->CreateSocket(address.family(), SOCK_DGRAM);
if (!socket) {
return nullptr;
}
if (BindSocket(socket, address, min_port, max_port) < 0) {
RTC_LOG(LS_ERROR) << "UDP bind failed with error " << socket->GetError();
delete socket;
return nullptr;
}
return new AsyncUDPSocket(socket);
}
AsyncListenSocket* 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) {
RTC_LOG(LS_ERROR) << "TLS support currently is not available.";
return nullptr;
}
if (opts & PacketSocketFactory::OPT_TLS_FAKE) {
RTC_LOG(LS_ERROR) << "Fake TLS not supported.";
return nullptr;
}
Socket* socket =
socket_factory_->CreateSocket(local_address.family(), SOCK_STREAM);
if (!socket) {
return nullptr;
}
if (BindSocket(socket, local_address, min_port, max_port) < 0) {
RTC_LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
delete socket;
return nullptr;
}
RTC_CHECK(!(opts & PacketSocketFactory::OPT_STUN));
return new AsyncTcpListenSocket(absl::WrapUnique(socket));
}
AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
const SocketAddress& local_address,
const SocketAddress& remote_address,
const PacketSocketTcpOptions& tcp_options) {
Socket* socket =
socket_factory_->CreateSocket(local_address.family(), SOCK_STREAM);
if (!socket) {
return nullptr;
}
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()) {
RTC_LOG(LS_WARNING) << "TCP bind failed with error " << socket->GetError()
<< "; ignoring since socket is using 'any' address.";
} else {
RTC_LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
delete socket;
return nullptr;
}
}
// 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();
}
// Assert that at most one TLS option is used.
int tlsOpts = tcp_options.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 nullptr;
}
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);
ssl_adapter->SetCertVerifier(tcp_options.tls_cert_verifier);
socket = ssl_adapter;
if (ssl_adapter->StartSSL(remote_address.hostname().c_str()) != 0) {
delete ssl_adapter;
return nullptr;
}
} 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) {
RTC_LOG(LS_ERROR) << "TCP connect failed with error " << socket->GetError();
delete socket;
return nullptr;
}
// Finally, wrap that socket in a TCP or STUN TCP packet socket.
AsyncPacketSocket* tcp_socket;
if (tcp_options.opts & PacketSocketFactory::OPT_STUN) {
tcp_socket = new AsyncStunTCPSocket(socket);
} else {
tcp_socket = new AsyncTCPSocket(socket);
}
return tcp_socket;
}
std::unique_ptr<AsyncDnsResolverInterface>
BasicPacketSocketFactory::CreateAsyncDnsResolver() {
return std::make_unique<AsyncDnsResolver>();
}
int BasicPacketSocketFactory::BindSocket(Socket* 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;
}
} // namespace webrtc
|