| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | 11 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "webrtc/p2p/base/asyncstuntcpsocket.h" | 15 #include "webrtc/p2p/base/asyncstuntcpsocket.h" |
| 16 #include "webrtc/p2p/base/stun.h" | 16 #include "webrtc/p2p/base/stun.h" |
| 17 #include "webrtc/base/asynctcpsocket.h" | 17 #include "webrtc/base/asynctcpsocket.h" |
| 18 #include "webrtc/base/asyncudpsocket.h" | 18 #include "webrtc/base/asyncudpsocket.h" |
| 19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 21 #include "webrtc/base/nethelpers.h" | 21 #include "webrtc/base/nethelpers.h" |
| 22 #include "webrtc/base/physicalsocketserver.h" | 22 #include "webrtc/base/physicalsocketserver.h" |
| 23 #include "webrtc/base/socketadapters.h" | 23 #include "webrtc/base/socketadapters.h" |
| 24 #include "webrtc/base/ssladapter.h" | 24 #include "webrtc/base/ssladapter.h" |
| 25 #include "webrtc/base/thread.h" | 25 #include "webrtc/base/thread.h" |
| 26 | 26 |
| 27 namespace rtc { | 27 namespace rtc { |
| 28 | 28 |
| 29 BasicPacketSocketFactory::BasicPacketSocketFactory() | 29 BasicPacketSocketFactory::BasicPacketSocketFactory() |
| 30 : thread_(Thread::Current()), | 30 : thread_(Thread::Current()), socket_factory_(nullptr) {} |
| 31 socket_factory_(NULL) { | |
| 32 } | |
| 33 | 31 |
| 34 BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread) | 32 BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread) |
| 35 : thread_(thread), | 33 : thread_(thread), socket_factory_(nullptr) {} |
| 36 socket_factory_(NULL) { | |
| 37 } | |
| 38 | 34 |
| 39 BasicPacketSocketFactory::BasicPacketSocketFactory( | 35 BasicPacketSocketFactory::BasicPacketSocketFactory( |
| 40 SocketFactory* socket_factory) | 36 SocketFactory* socket_factory) |
| 41 : thread_(NULL), | 37 : thread_(nullptr), socket_factory_(socket_factory) {} |
| 42 socket_factory_(socket_factory) { | |
| 43 } | |
| 44 | 38 |
| 45 BasicPacketSocketFactory::~BasicPacketSocketFactory() { | 39 BasicPacketSocketFactory::~BasicPacketSocketFactory() { |
| 46 } | 40 } |
| 47 | 41 |
| 48 AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket( | 42 AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket( |
| 49 const SocketAddress& address, | 43 const SocketAddress& address, |
| 50 uint16_t min_port, | 44 uint16_t min_port, |
| 51 uint16_t max_port) { | 45 uint16_t max_port) { |
| 52 // UDP sockets are simple. | 46 // UDP sockets are simple. |
| 53 AsyncSocket* socket = | 47 AsyncSocket* socket = |
| 54 socket_factory()->CreateAsyncSocket(address.family(), SOCK_DGRAM); | 48 socket_factory()->CreateAsyncSocket(address.family(), SOCK_DGRAM); |
| 55 if (!socket) { | 49 if (!socket) { |
| 56 return NULL; | 50 return nullptr; |
| 57 } | 51 } |
| 58 if (BindSocket(socket, address, min_port, max_port) < 0) { | 52 if (BindSocket(socket, address, min_port, max_port) < 0) { |
| 59 LOG(LS_ERROR) << "UDP bind failed with error " | 53 LOG(LS_ERROR) << "UDP bind failed with error " |
| 60 << socket->GetError(); | 54 << socket->GetError(); |
| 61 delete socket; | 55 delete socket; |
| 62 return NULL; | 56 return nullptr; |
| 63 } | 57 } |
| 64 return new AsyncUDPSocket(socket); | 58 return new AsyncUDPSocket(socket); |
| 65 } | 59 } |
| 66 | 60 |
| 67 AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket( | 61 AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket( |
| 68 const SocketAddress& local_address, | 62 const SocketAddress& local_address, |
| 69 uint16_t min_port, | 63 uint16_t min_port, |
| 70 uint16_t max_port, | 64 uint16_t max_port, |
| 71 int opts) { | 65 int opts) { |
| 72 // Fail if TLS is required. | 66 // Fail if TLS is required. |
| 73 if (opts & PacketSocketFactory::OPT_TLS) { | 67 if (opts & PacketSocketFactory::OPT_TLS) { |
| 74 LOG(LS_ERROR) << "TLS support currently is not available."; | 68 LOG(LS_ERROR) << "TLS support currently is not available."; |
| 75 return NULL; | 69 return nullptr; |
| 76 } | 70 } |
| 77 | 71 |
| 78 AsyncSocket* socket = | 72 AsyncSocket* socket = |
| 79 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM); | 73 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM); |
| 80 if (!socket) { | 74 if (!socket) { |
| 81 return NULL; | 75 return nullptr; |
| 82 } | 76 } |
| 83 | 77 |
| 84 if (BindSocket(socket, local_address, min_port, max_port) < 0) { | 78 if (BindSocket(socket, local_address, min_port, max_port) < 0) { |
| 85 LOG(LS_ERROR) << "TCP bind failed with error " | 79 LOG(LS_ERROR) << "TCP bind failed with error " |
| 86 << socket->GetError(); | 80 << socket->GetError(); |
| 87 delete socket; | 81 delete socket; |
| 88 return NULL; | 82 return nullptr; |
| 89 } | 83 } |
| 90 | 84 |
| 91 // If using fake TLS, wrap the TCP socket in a pseudo-SSL socket. | 85 // If using fake TLS, wrap the TCP socket in a pseudo-SSL socket. |
| 92 if (opts & PacketSocketFactory::OPT_TLS_FAKE) { | 86 if (opts & PacketSocketFactory::OPT_TLS_FAKE) { |
| 93 RTC_DCHECK(!(opts & PacketSocketFactory::OPT_TLS)); | 87 RTC_DCHECK(!(opts & PacketSocketFactory::OPT_TLS)); |
| 94 socket = new AsyncSSLSocket(socket); | 88 socket = new AsyncSSLSocket(socket); |
| 95 } | 89 } |
| 96 | 90 |
| 97 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance. | 91 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance. |
| 98 // See http://go/gtalktcpnodelayexperiment | 92 // See http://go/gtalktcpnodelayexperiment |
| 99 socket->SetOption(Socket::OPT_NODELAY, 1); | 93 socket->SetOption(Socket::OPT_NODELAY, 1); |
| 100 | 94 |
| 101 if (opts & PacketSocketFactory::OPT_STUN) | 95 if (opts & PacketSocketFactory::OPT_STUN) |
| 102 return new cricket::AsyncStunTCPSocket(socket, true); | 96 return new cricket::AsyncStunTCPSocket(socket, true); |
| 103 | 97 |
| 104 return new AsyncTCPSocket(socket, true); | 98 return new AsyncTCPSocket(socket, true); |
| 105 } | 99 } |
| 106 | 100 |
| 107 AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket( | 101 AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket( |
| 108 const SocketAddress& local_address, const SocketAddress& remote_address, | 102 const SocketAddress& local_address, const SocketAddress& remote_address, |
| 109 const ProxyInfo& proxy_info, const std::string& user_agent, int opts) { | 103 const ProxyInfo& proxy_info, const std::string& user_agent, int opts) { |
| 110 AsyncSocket* socket = | 104 AsyncSocket* socket = |
| 111 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM); | 105 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM); |
| 112 if (!socket) { | 106 if (!socket) { |
| 113 return NULL; | 107 return nullptr; |
| 114 } | 108 } |
| 115 | 109 |
| 116 if (BindSocket(socket, local_address, 0, 0) < 0) { | 110 if (BindSocket(socket, local_address, 0, 0) < 0) { |
| 117 LOG(LS_ERROR) << "TCP bind failed with error " | 111 LOG(LS_ERROR) << "TCP bind failed with error " |
| 118 << socket->GetError(); | 112 << socket->GetError(); |
| 119 delete socket; | 113 delete socket; |
| 120 return NULL; | 114 return nullptr; |
| 121 } | 115 } |
| 122 | 116 |
| 123 // If using a proxy, wrap the socket in a proxy socket. | 117 // If using a proxy, wrap the socket in a proxy socket. |
| 124 if (proxy_info.type == PROXY_SOCKS5) { | 118 if (proxy_info.type == PROXY_SOCKS5) { |
| 125 socket = new AsyncSocksProxySocket( | 119 socket = new AsyncSocksProxySocket( |
| 126 socket, proxy_info.address, proxy_info.username, proxy_info.password); | 120 socket, proxy_info.address, proxy_info.username, proxy_info.password); |
| 127 } else if (proxy_info.type == PROXY_HTTPS) { | 121 } else if (proxy_info.type == PROXY_HTTPS) { |
| 128 socket = | 122 socket = |
| 129 new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address, | 123 new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address, |
| 130 proxy_info.username, proxy_info.password); | 124 proxy_info.username, proxy_info.password); |
| 131 } | 125 } |
| 132 | 126 |
| 133 // Assert that at most one TLS option is used. | 127 // Assert that at most one TLS option is used. |
| 134 int tlsOpts = | 128 int tlsOpts = |
| 135 opts & (PacketSocketFactory::OPT_TLS | PacketSocketFactory::OPT_TLS_FAKE | | 129 opts & (PacketSocketFactory::OPT_TLS | PacketSocketFactory::OPT_TLS_FAKE | |
| 136 PacketSocketFactory::OPT_TLS_INSECURE); | 130 PacketSocketFactory::OPT_TLS_INSECURE); |
| 137 RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0); | 131 RTC_DCHECK((tlsOpts & (tlsOpts - 1)) == 0); |
| 138 | 132 |
| 139 if ((tlsOpts & PacketSocketFactory::OPT_TLS) || | 133 if ((tlsOpts & PacketSocketFactory::OPT_TLS) || |
| 140 (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) { | 134 (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE)) { |
| 141 // Using TLS, wrap the socket in an SSL adapter. | 135 // Using TLS, wrap the socket in an SSL adapter. |
| 142 SSLAdapter* ssl_adapter = SSLAdapter::Create(socket); | 136 SSLAdapter* ssl_adapter = SSLAdapter::Create(socket); |
| 143 if (!ssl_adapter) { | 137 if (!ssl_adapter) { |
| 144 return NULL; | 138 return nullptr; |
| 145 } | 139 } |
| 146 | 140 |
| 147 if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) { | 141 if (tlsOpts & PacketSocketFactory::OPT_TLS_INSECURE) { |
| 148 ssl_adapter->set_ignore_bad_cert(true); | 142 ssl_adapter->set_ignore_bad_cert(true); |
| 149 } | 143 } |
| 150 | 144 |
| 151 socket = ssl_adapter; | 145 socket = ssl_adapter; |
| 152 | 146 |
| 153 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) { | 147 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) { |
| 154 delete ssl_adapter; | 148 delete ssl_adapter; |
| 155 return NULL; | 149 return nullptr; |
| 156 } | 150 } |
| 157 | 151 |
| 158 } else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) { | 152 } else if (tlsOpts & PacketSocketFactory::OPT_TLS_FAKE) { |
| 159 // Using fake TLS, wrap the TCP socket in a pseudo-SSL socket. | 153 // Using fake TLS, wrap the TCP socket in a pseudo-SSL socket. |
| 160 socket = new AsyncSSLSocket(socket); | 154 socket = new AsyncSSLSocket(socket); |
| 161 } | 155 } |
| 162 | 156 |
| 163 if (socket->Connect(remote_address) < 0) { | 157 if (socket->Connect(remote_address) < 0) { |
| 164 LOG(LS_ERROR) << "TCP connect failed with error " | 158 LOG(LS_ERROR) << "TCP connect failed with error " |
| 165 << socket->GetError(); | 159 << socket->GetError(); |
| 166 delete socket; | 160 delete socket; |
| 167 return NULL; | 161 return nullptr; |
| 168 } | 162 } |
| 169 | 163 |
| 170 // Finally, wrap that socket in a TCP or STUN TCP packet socket. | 164 // Finally, wrap that socket in a TCP or STUN TCP packet socket. |
| 171 AsyncPacketSocket* tcp_socket; | 165 AsyncPacketSocket* tcp_socket; |
| 172 if (opts & PacketSocketFactory::OPT_STUN) { | 166 if (opts & PacketSocketFactory::OPT_STUN) { |
| 173 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false); | 167 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false); |
| 174 } else { | 168 } else { |
| 175 tcp_socket = new AsyncTCPSocket(socket, false); | 169 tcp_socket = new AsyncTCPSocket(socket, false); |
| 176 } | 170 } |
| 177 | 171 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 206 SocketFactory* BasicPacketSocketFactory::socket_factory() { | 200 SocketFactory* BasicPacketSocketFactory::socket_factory() { |
| 207 if (thread_) { | 201 if (thread_) { |
| 208 RTC_DCHECK(thread_ == Thread::Current()); | 202 RTC_DCHECK(thread_ == Thread::Current()); |
| 209 return thread_->socketserver(); | 203 return thread_->socketserver(); |
| 210 } else { | 204 } else { |
| 211 return socket_factory_; | 205 return socket_factory_; |
| 212 } | 206 } |
| 213 } | 207 } |
| 214 | 208 |
| 215 } // namespace rtc | 209 } // namespace rtc |
| OLD | NEW |