blob: 7438a9b22f936ff95db07d14213d0ece09ab37f6 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "p2p/base/basicpacketsocketfactory.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012
johan57e13de2016-10-25 10:15:06 -070013#include <string>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "p2p/base/asyncstuntcpsocket.h"
16#include "p2p/base/stun.h"
17#include "rtc_base/asynctcpsocket.h"
18#include "rtc_base/asyncudpsocket.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
21#include "rtc_base/nethelpers.h"
22#include "rtc_base/physicalsocketserver.h"
23#include "rtc_base/socketadapters.h"
24#include "rtc_base/ssladapter.h"
25#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026
27namespace rtc {
28
29BasicPacketSocketFactory::BasicPacketSocketFactory()
30 : thread_(Thread::Current()),
31 socket_factory_(NULL) {
32}
33
34BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread)
35 : thread_(thread),
36 socket_factory_(NULL) {
37}
38
39BasicPacketSocketFactory::BasicPacketSocketFactory(
40 SocketFactory* socket_factory)
41 : thread_(NULL),
42 socket_factory_(socket_factory) {
43}
44
45BasicPacketSocketFactory::~BasicPacketSocketFactory() {
46}
47
48AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
Peter Boström0c4e06b2015-10-07 12:23:21 +020049 const SocketAddress& address,
50 uint16_t min_port,
51 uint16_t max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 // UDP sockets are simple.
johan57e13de2016-10-25 10:15:06 -070053 AsyncSocket* socket =
54 socket_factory()->CreateAsyncSocket(address.family(), SOCK_DGRAM);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 if (!socket) {
56 return NULL;
57 }
58 if (BindSocket(socket, address, min_port, max_port) < 0) {
59 LOG(LS_ERROR) << "UDP bind failed with error "
60 << socket->GetError();
61 delete socket;
62 return NULL;
63 }
johan57e13de2016-10-25 10:15:06 -070064 return new AsyncUDPSocket(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065}
66
67AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
Peter Boström0c4e06b2015-10-07 12:23:21 +020068 const SocketAddress& local_address,
69 uint16_t min_port,
70 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000071 int opts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072 // Fail if TLS is required.
73 if (opts & PacketSocketFactory::OPT_TLS) {
74 LOG(LS_ERROR) << "TLS support currently is not available.";
75 return NULL;
76 }
77
johan57e13de2016-10-25 10:15:06 -070078 AsyncSocket* socket =
79 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080 if (!socket) {
81 return NULL;
82 }
83
84 if (BindSocket(socket, local_address, min_port, max_port) < 0) {
85 LOG(LS_ERROR) << "TCP bind failed with error "
86 << socket->GetError();
87 delete socket;
88 return NULL;
89 }
90
hnsl04833622017-01-09 08:35:45 -080091 // If using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
92 if (opts & PacketSocketFactory::OPT_TLS_FAKE) {
nisseede5da42017-01-12 05:15:36 -080093 RTC_DCHECK(!(opts & PacketSocketFactory::OPT_TLS));
johan57e13de2016-10-25 10:15:06 -070094 socket = new AsyncSSLSocket(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095 }
96
97 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
98 // See http://go/gtalktcpnodelayexperiment
johan57e13de2016-10-25 10:15:06 -070099 socket->SetOption(Socket::OPT_NODELAY, 1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100
101 if (opts & PacketSocketFactory::OPT_STUN)
102 return new cricket::AsyncStunTCPSocket(socket, true);
103
johan57e13de2016-10-25 10:15:06 -0700104 return new AsyncTCPSocket(socket, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105}
106
107AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
Diogo Real1dca9d52017-08-29 12:18:32 -0700108 const SocketAddress& local_address,
109 const SocketAddress& remote_address,
110 const ProxyInfo& proxy_info,
111 const std::string& user_agent,
Steve Antoneae3e652017-10-25 14:46:18 -0700112 int opts) {
113 PacketSocketTcpOptions tcp_options;
114 tcp_options.opts = opts;
115 return CreateClientTcpSocket(local_address, remote_address, proxy_info,
116 user_agent, tcp_options);
117}
118
119AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
120 const SocketAddress& local_address,
121 const SocketAddress& remote_address,
122 const ProxyInfo& proxy_info,
123 const std::string& user_agent,
Diogo Real1dca9d52017-08-29 12:18:32 -0700124 const PacketSocketTcpOptions& tcp_options) {
johan57e13de2016-10-25 10:15:06 -0700125 AsyncSocket* socket =
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
127 if (!socket) {
128 return NULL;
129 }
130
131 if (BindSocket(socket, local_address, 0, 0) < 0) {
deadbeef1ee21252017-06-13 15:49:45 -0700132 // Allow BindSocket to fail if we're binding to the ANY address, since this
133 // is mostly redundant in the first place. The socket will be bound when we
134 // call Connect() instead.
135 if (local_address.IsAnyIP()) {
136 LOG(LS_WARNING) << "TCP bind failed with error " << socket->GetError()
137 << "; ignoring since socket is using 'any' address.";
138 } else {
139 LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
140 delete socket;
141 return NULL;
142 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 }
144
deadbeeff137e972017-03-23 15:45:49 -0700145 // If using a proxy, wrap the socket in a proxy socket.
146 if (proxy_info.type == PROXY_SOCKS5) {
147 socket = new AsyncSocksProxySocket(
148 socket, proxy_info.address, proxy_info.username, proxy_info.password);
149 } else if (proxy_info.type == PROXY_HTTPS) {
150 socket =
151 new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address,
152 proxy_info.username, proxy_info.password);
153 }
154
hnsl04833622017-01-09 08:35:45 -0800155 // Assert that at most one TLS option is used.
Diogo Real1dca9d52017-08-29 12:18:32 -0700156 int tlsOpts = tcp_options.opts & (PacketSocketFactory::OPT_TLS |
157 PacketSocketFactory::OPT_TLS_FAKE |
158 PacketSocketFactory::OPT_TLS_INSECURE);
nisseede5da42017-01-12 05:15:36 -0800159 RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160
hnsl04833622017-01-09 08:35:45 -0800161 if ((tlsOpts & PacketSocketFactory::OPT_TLS) ||
162 (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) {
163 // Using TLS, wrap the socket in an SSL adapter.
johan57e13de2016-10-25 10:15:06 -0700164 SSLAdapter* ssl_adapter = SSLAdapter::Create(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 if (!ssl_adapter) {
166 return NULL;
167 }
168
hnsl04833622017-01-09 08:35:45 -0800169 if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) {
Diogo Real1dca9d52017-08-29 12:18:32 -0700170 ssl_adapter->SetIgnoreBadCert(true);
hnsl04833622017-01-09 08:35:45 -0800171 }
172
Diogo Real1dca9d52017-08-29 12:18:32 -0700173 ssl_adapter->SetAlpnProtocols(tcp_options.tls_alpn_protocols);
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700174 ssl_adapter->SetEllipticCurves(tcp_options.tls_elliptic_curves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700175
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176 socket = ssl_adapter;
177
178 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) {
179 delete ssl_adapter;
180 return NULL;
181 }
182
hnsl04833622017-01-09 08:35:45 -0800183 } else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) {
184 // Using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
johan57e13de2016-10-25 10:15:06 -0700185 socket = new AsyncSSLSocket(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 }
187
188 if (socket->Connect(remote_address) < 0) {
189 LOG(LS_ERROR) << "TCP connect failed with error "
190 << socket->GetError();
191 delete socket;
192 return NULL;
193 }
194
195 // Finally, wrap that socket in a TCP or STUN TCP packet socket.
196 AsyncPacketSocket* tcp_socket;
Diogo Real1dca9d52017-08-29 12:18:32 -0700197 if (tcp_options.opts & PacketSocketFactory::OPT_STUN) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false);
199 } else {
johan57e13de2016-10-25 10:15:06 -0700200 tcp_socket = new AsyncTCPSocket(socket, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 }
202
203 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
204 // See http://go/gtalktcpnodelayexperiment
johan57e13de2016-10-25 10:15:06 -0700205 tcp_socket->SetOption(Socket::OPT_NODELAY, 1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206
207 return tcp_socket;
208}
209
210AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
johan57e13de2016-10-25 10:15:06 -0700211 return new AsyncResolver();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212}
213
Peter Boström0c4e06b2015-10-07 12:23:21 +0200214int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket,
215 const SocketAddress& local_address,
216 uint16_t min_port,
217 uint16_t max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218 int ret = -1;
219 if (min_port == 0 && max_port == 0) {
220 // If there's no port range, let the OS pick a port for us.
221 ret = socket->Bind(local_address);
222 } else {
223 // Otherwise, try to find a port in the provided range.
224 for (int port = min_port; ret < 0 && port <= max_port; ++port) {
johan57e13de2016-10-25 10:15:06 -0700225 ret = socket->Bind(SocketAddress(local_address.ipaddr(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226 }
227 }
228 return ret;
229}
230
231SocketFactory* BasicPacketSocketFactory::socket_factory() {
232 if (thread_) {
nisseede5da42017-01-12 05:15:36 -0800233 RTC_DCHECK(thread_ == Thread::Current());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 return thread_->socketserver();
235 } else {
236 return socket_factory_;
237 }
238}
239
240} // namespace rtc