blob: c478d6311202108bbaff642768508171aa1f9f06 [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"
17#include "webrtc/base/asynctcpsocket.h"
18#include "webrtc/base/asyncudpsocket.h"
nisseede5da42017-01-12 05:15:36 -080019#include "webrtc/base/checks.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020#include "webrtc/base/logging.h"
21#include "webrtc/base/nethelpers.h"
22#include "webrtc/base/physicalsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000023#include "webrtc/base/socketadapters.h"
24#include "webrtc/base/ssladapter.h"
25#include "webrtc/base/thread.h"
26
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) {
117 LOG(LS_ERROR) << "TCP bind failed with error "
118 << socket->GetError();
119 delete socket;
120 return NULL;
121 }
122
deadbeeff137e972017-03-23 15:45:49 -0700123 // If using a proxy, wrap the socket in a proxy socket.
124 if (proxy_info.type == PROXY_SOCKS5) {
125 socket = new AsyncSocksProxySocket(
126 socket, proxy_info.address, proxy_info.username, proxy_info.password);
127 } else if (proxy_info.type == PROXY_HTTPS) {
128 socket =
129 new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address,
130 proxy_info.username, proxy_info.password);
131 }
132
hnsl04833622017-01-09 08:35:45 -0800133 // Assert that at most one TLS option is used.
134 int tlsOpts =
135 opts & (PacketSocketFactory::OPT_TLS | PacketSocketFactory::OPT_TLS_FAKE |
136 PacketSocketFactory::OPT_TLS_INSECURE);
nisseede5da42017-01-12 05:15:36 -0800137 RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138
hnsl04833622017-01-09 08:35:45 -0800139 if ((tlsOpts & PacketSocketFactory::OPT_TLS) ||
140 (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) {
141 // Using TLS, wrap the socket in an SSL adapter.
johan57e13de2016-10-25 10:15:06 -0700142 SSLAdapter* ssl_adapter = SSLAdapter::Create(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 if (!ssl_adapter) {
144 return NULL;
145 }
146
hnsl04833622017-01-09 08:35:45 -0800147 if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) {
148 ssl_adapter->set_ignore_bad_cert(true);
149 }
150
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 socket = ssl_adapter;
152
153 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) {
154 delete ssl_adapter;
155 return NULL;
156 }
157
hnsl04833622017-01-09 08:35:45 -0800158 } else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) {
159 // Using fake TLS, wrap the TCP socket in a pseudo-SSL socket.
johan57e13de2016-10-25 10:15:06 -0700160 socket = new AsyncSSLSocket(socket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 }
162
163 if (socket->Connect(remote_address) < 0) {
164 LOG(LS_ERROR) << "TCP connect failed with error "
165 << socket->GetError();
166 delete socket;
167 return NULL;
168 }
169
170 // Finally, wrap that socket in a TCP or STUN TCP packet socket.
171 AsyncPacketSocket* tcp_socket;
172 if (opts & PacketSocketFactory::OPT_STUN) {
173 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false);
174 } else {
johan57e13de2016-10-25 10:15:06 -0700175 tcp_socket = new AsyncTCPSocket(socket, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176 }
177
178 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
179 // See http://go/gtalktcpnodelayexperiment
johan57e13de2016-10-25 10:15:06 -0700180 tcp_socket->SetOption(Socket::OPT_NODELAY, 1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181
182 return tcp_socket;
183}
184
185AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() {
johan57e13de2016-10-25 10:15:06 -0700186 return new AsyncResolver();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187}
188
Peter Boström0c4e06b2015-10-07 12:23:21 +0200189int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket,
190 const SocketAddress& local_address,
191 uint16_t min_port,
192 uint16_t max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 int ret = -1;
194 if (min_port == 0 && max_port == 0) {
195 // If there's no port range, let the OS pick a port for us.
196 ret = socket->Bind(local_address);
197 } else {
198 // Otherwise, try to find a port in the provided range.
199 for (int port = min_port; ret < 0 && port <= max_port; ++port) {
johan57e13de2016-10-25 10:15:06 -0700200 ret = socket->Bind(SocketAddress(local_address.ipaddr(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 }
202 }
203 return ret;
204}
205
206SocketFactory* BasicPacketSocketFactory::socket_factory() {
207 if (thread_) {
nisseede5da42017-01-12 05:15:36 -0800208 RTC_DCHECK(thread_ == Thread::Current());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 return thread_->socketserver();
210 } else {
211 return socket_factory_;
212 }
213}
214
215} // namespace rtc