blob: e911f2fe222769a3eaf27f56742b5171a77eeddc [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(
108 const SocketAddress& local_address, const SocketAddress& remote_address,
109 const ProxyInfo& proxy_info, const std::string& user_agent, int opts) {
johan57e13de2016-10-25 10:15:06 -0700110 AsyncSocket* socket =
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
112 if (!socket) {
113 return NULL;
114 }
115
116 if (BindSocket(socket, local_address, 0, 0) < 0) {
deadbeef1ee21252017-06-13 15:49:45 -0700117 // Allow BindSocket to fail if we're binding to the ANY address, since this
118 // is mostly redundant in the first place. The socket will be bound when we
119 // call Connect() instead.
120 if (local_address.IsAnyIP()) {
121 LOG(LS_WARNING) << "TCP bind failed with error " << socket->GetError()
122 << "; ignoring since socket is using 'any' address.";
123 } else {
124 LOG(LS_ERROR) << "TCP bind failed with error " << socket->GetError();
125 delete socket;
126 return NULL;
127 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 }
129
deadbeeff137e972017-03-23 15:45:49 -0700130 // If using a proxy, wrap the socket in a proxy socket.
131 if (proxy_info.type == PROXY_SOCKS5) {
132 socket = new AsyncSocksProxySocket(
133 socket, proxy_info.address, proxy_info.username, proxy_info.password);
134 } else if (proxy_info.type == PROXY_HTTPS) {
135 socket =
136 new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address,
137 proxy_info.username, proxy_info.password);
138 }
139
hnsl04833622017-01-09 08:35:45 -0800140 // Assert that at most one TLS option is used.
141 int tlsOpts =
142 opts & (PacketSocketFactory::OPT_TLS | PacketSocketFactory::OPT_TLS_FAKE |
143 PacketSocketFactory::OPT_TLS_INSECURE);
nisseede5da42017-01-12 05:15:36 -0800144 RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145
hnsl04833622017-01-09 08:35:45 -0800146 if ((tlsOpts & PacketSocketFactory::OPT_TLS) ||
147 (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) {
148 // Using TLS, wrap the socket in an SSL adapter.
johan57e13de2016-10-25 10:15:06 -0700149 SSLAdapter* ssl_adapter = SSLAdapter::Create(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150 if (!ssl_adapter) {
151 return NULL;
152 }
153
hnsl04833622017-01-09 08:35:45 -0800154 if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) {
155 ssl_adapter->set_ignore_bad_cert(true);
156 }
157
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 socket = ssl_adapter;
159
160 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) {
161 delete ssl_adapter;
162 return NULL;
163 }
164
hnsl04833622017-01-09 08:35:45 -0800165 } else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) {
166 // Using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
johan57e13de2016-10-25 10:15:06 -0700167 socket = new AsyncSSLSocket(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168 }
169
170 if (socket->Connect(remote_address) < 0) {
171 LOG(LS_ERROR) << "TCP connect failed with error "
172 << socket->GetError();
173 delete socket;
174 return NULL;
175 }
176
177 // Finally, wrap that socket in a TCP or STUN TCP packet socket.
178 AsyncPacketSocket* tcp_socket;
179 if (opts & PacketSocketFactory::OPT_STUN) {
180 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false);
181 } else {
johan57e13de2016-10-25 10:15:06 -0700182 tcp_socket = new AsyncTCPSocket(socket, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183 }
184
185 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
186 // See http://go/gtalktcpnodelayexperiment
johan57e13de2016-10-25 10:15:06 -0700187 tcp_socket->SetOption(Socket::OPT_NODELAY, 1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188
189 return tcp_socket;
190}
191
192AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
johan57e13de2016-10-25 10:15:06 -0700193 return new AsyncResolver();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194}
195
Peter Boström0c4e06b2015-10-07 12:23:21 +0200196int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket,
197 const SocketAddress& local_address,
198 uint16_t min_port,
199 uint16_t max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 int ret = -1;
201 if (min_port == 0 && max_port == 0) {
202 // If there's no port range, let the OS pick a port for us.
203 ret = socket->Bind(local_address);
204 } else {
205 // Otherwise, try to find a port in the provided range.
206 for (int port = min_port; ret < 0 && port <= max_port; ++port) {
johan57e13de2016-10-25 10:15:06 -0700207 ret = socket->Bind(SocketAddress(local_address.ipaddr(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 }
209 }
210 return ret;
211}
212
213SocketFactory* BasicPacketSocketFactory::socket_factory() {
214 if (thread_) {
nisseede5da42017-01-12 05:15:36 -0800215 RTC_DCHECK(thread_ == Thread::Current());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 return thread_->socketserver();
217 } else {
218 return socket_factory_;
219 }
220}
221
222} // namespace rtc