blob: b0f464b494db4978cce62044f52e7f48a5f37189 [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
11#include "webrtc/p2p/base/basicpacketsocketfactory.h"
12
johan57e13de2016-10-25 10:15:06 -070013#include <string>
14
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015#include "webrtc/p2p/base/asyncstuntcpsocket.h"
16#include "webrtc/p2p/base/stun.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/asynctcpsocket.h"
18#include "webrtc/rtc_base/asyncudpsocket.h"
19#include "webrtc/rtc_base/checks.h"
20#include "webrtc/rtc_base/logging.h"
21#include "webrtc/rtc_base/nethelpers.h"
22#include "webrtc/rtc_base/physicalsocketserver.h"
23#include "webrtc/rtc_base/socketadapters.h"
24#include "webrtc/rtc_base/ssladapter.h"
25#include "webrtc/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,
112 const PacketSocketTcpOptions& tcp_options) {
johan57e13de2016-10-25 10:15:06 -0700113 AsyncSocket* socket =
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
115 if (!socket) {
116 return NULL;
117 }
118
119 if (BindSocket(socket, local_address, 0, 0) < 0) {
deadbeef1ee21252017-06-13 15:49:45 -0700120 // Allow BindSocket to fail if we're binding to the ANY address, since this
121 // is mostly redundant in the first place. The socket will be bound when we
122 // call Connect() instead.
123 if (local_address.IsAnyIP()) {
124 LOG(LS_WARNING) << "TCP bind failed with error " << socket->GetError()
125 << "; ignoring since socket is using 'any' address.";
126 } else {
127 LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
128 delete socket;
129 return NULL;
130 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131 }
132
deadbeeff137e972017-03-23 15:45:49 -0700133 // If using a proxy, wrap the socket in a proxy socket.
134 if (proxy_info.type == PROXY_SOCKS5) {
135 socket = new AsyncSocksProxySocket(
136 socket, proxy_info.address, proxy_info.username, proxy_info.password);
137 } else if (proxy_info.type == PROXY_HTTPS) {
138 socket =
139 new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address,
140 proxy_info.username, proxy_info.password);
141 }
142
hnsl04833622017-01-09 08:35:45 -0800143 // Assert that at most one TLS option is used.
Diogo Real1dca9d52017-08-29 12:18:32 -0700144 int tlsOpts = tcp_options.opts & (PacketSocketFactory::OPT_TLS |
145 PacketSocketFactory::OPT_TLS_FAKE |
146 PacketSocketFactory::OPT_TLS_INSECURE);
nisseede5da42017-01-12 05:15:36 -0800147 RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148
hnsl04833622017-01-09 08:35:45 -0800149 if ((tlsOpts & PacketSocketFactory::OPT_TLS) ||
150 (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) {
151 // Using TLS, wrap the socket in an SSL adapter.
johan57e13de2016-10-25 10:15:06 -0700152 SSLAdapter* ssl_adapter = SSLAdapter::Create(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153 if (!ssl_adapter) {
154 return NULL;
155 }
156
hnsl04833622017-01-09 08:35:45 -0800157 if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) {
Diogo Real1dca9d52017-08-29 12:18:32 -0700158 ssl_adapter->SetIgnoreBadCert(true);
hnsl04833622017-01-09 08:35:45 -0800159 }
160
Diogo Real1dca9d52017-08-29 12:18:32 -0700161 ssl_adapter->SetAlpnProtocols(tcp_options.tls_alpn_protocols);
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700162 ssl_adapter->SetEllipticCurves(tcp_options.tls_elliptic_curves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700163
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000164 socket = ssl_adapter;
165
166 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) {
167 delete ssl_adapter;
168 return NULL;
169 }
170
hnsl04833622017-01-09 08:35:45 -0800171 } else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) {
172 // Using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
johan57e13de2016-10-25 10:15:06 -0700173 socket = new AsyncSSLSocket(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000174 }
175
176 if (socket->Connect(remote_address) < 0) {
177 LOG(LS_ERROR) << "TCP connect failed with error "
178 << socket->GetError();
179 delete socket;
180 return NULL;
181 }
182
183 // Finally, wrap that socket in a TCP or STUN TCP packet socket.
184 AsyncPacketSocket* tcp_socket;
Diogo Real1dca9d52017-08-29 12:18:32 -0700185 if (tcp_options.opts & PacketSocketFactory::OPT_STUN) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false);
187 } else {
johan57e13de2016-10-25 10:15:06 -0700188 tcp_socket = new AsyncTCPSocket(socket, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 }
190
191 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
192 // See http://go/gtalktcpnodelayexperiment
johan57e13de2016-10-25 10:15:06 -0700193 tcp_socket->SetOption(Socket::OPT_NODELAY, 1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194
195 return tcp_socket;
196}
197
198AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
johan57e13de2016-10-25 10:15:06 -0700199 return new AsyncResolver();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200}
201
Peter Boström0c4e06b2015-10-07 12:23:21 +0200202int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket,
203 const SocketAddress& local_address,
204 uint16_t min_port,
205 uint16_t max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 int ret = -1;
207 if (min_port == 0 && max_port == 0) {
208 // If there's no port range, let the OS pick a port for us.
209 ret = socket->Bind(local_address);
210 } else {
211 // Otherwise, try to find a port in the provided range.
212 for (int port = min_port; ret < 0 && port <= max_port; ++port) {
johan57e13de2016-10-25 10:15:06 -0700213 ret = socket->Bind(SocketAddress(local_address.ipaddr(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 }
215 }
216 return ret;
217}
218
219SocketFactory* BasicPacketSocketFactory::socket_factory() {
220 if (thread_) {
nisseede5da42017-01-12 05:15:36 -0800221 RTC_DCHECK(thread_ == Thread::Current());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 return thread_->socketserver();
223 } else {
224 return socket_factory_;
225 }
226}
227
228} // namespace rtc