| 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> |
| 14 |
| 13 #include "webrtc/p2p/base/asyncstuntcpsocket.h" | 15 #include "webrtc/p2p/base/asyncstuntcpsocket.h" |
| 14 #include "webrtc/p2p/base/stun.h" | 16 #include "webrtc/p2p/base/stun.h" |
| 15 #include "webrtc/base/asynctcpsocket.h" | 17 #include "webrtc/base/asynctcpsocket.h" |
| 16 #include "webrtc/base/asyncudpsocket.h" | 18 #include "webrtc/base/asyncudpsocket.h" |
| 17 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
| 18 #include "webrtc/base/nethelpers.h" | 20 #include "webrtc/base/nethelpers.h" |
| 19 #include "webrtc/base/physicalsocketserver.h" | 21 #include "webrtc/base/physicalsocketserver.h" |
| 20 #include "webrtc/base/socketadapters.h" | 22 #include "webrtc/base/socketadapters.h" |
| 21 #include "webrtc/base/ssladapter.h" | 23 #include "webrtc/base/ssladapter.h" |
| 22 #include "webrtc/base/thread.h" | 24 #include "webrtc/base/thread.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 40 } | 42 } |
| 41 | 43 |
| 42 BasicPacketSocketFactory::~BasicPacketSocketFactory() { | 44 BasicPacketSocketFactory::~BasicPacketSocketFactory() { |
| 43 } | 45 } |
| 44 | 46 |
| 45 AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket( | 47 AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket( |
| 46 const SocketAddress& address, | 48 const SocketAddress& address, |
| 47 uint16_t min_port, | 49 uint16_t min_port, |
| 48 uint16_t max_port) { | 50 uint16_t max_port) { |
| 49 // UDP sockets are simple. | 51 // UDP sockets are simple. |
| 50 rtc::AsyncSocket* socket = | 52 AsyncSocket* socket = |
| 51 socket_factory()->CreateAsyncSocket( | 53 socket_factory()->CreateAsyncSocket(address.family(), SOCK_DGRAM); |
| 52 address.family(), SOCK_DGRAM); | |
| 53 if (!socket) { | 54 if (!socket) { |
| 54 return NULL; | 55 return NULL; |
| 55 } | 56 } |
| 56 if (BindSocket(socket, address, min_port, max_port) < 0) { | 57 if (BindSocket(socket, address, min_port, max_port) < 0) { |
| 57 LOG(LS_ERROR) << "UDP bind failed with error " | 58 LOG(LS_ERROR) << "UDP bind failed with error " |
| 58 << socket->GetError(); | 59 << socket->GetError(); |
| 59 delete socket; | 60 delete socket; |
| 60 return NULL; | 61 return NULL; |
| 61 } | 62 } |
| 62 return new rtc::AsyncUDPSocket(socket); | 63 return new AsyncUDPSocket(socket); |
| 63 } | 64 } |
| 64 | 65 |
| 65 AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket( | 66 AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket( |
| 66 const SocketAddress& local_address, | 67 const SocketAddress& local_address, |
| 67 uint16_t min_port, | 68 uint16_t min_port, |
| 68 uint16_t max_port, | 69 uint16_t max_port, |
| 69 int opts) { | 70 int opts) { |
| 70 // Fail if TLS is required. | 71 // Fail if TLS is required. |
| 71 if (opts & PacketSocketFactory::OPT_TLS) { | 72 if (opts & PacketSocketFactory::OPT_TLS) { |
| 72 LOG(LS_ERROR) << "TLS support currently is not available."; | 73 LOG(LS_ERROR) << "TLS support currently is not available."; |
| 73 return NULL; | 74 return NULL; |
| 74 } | 75 } |
| 75 | 76 |
| 76 rtc::AsyncSocket* socket = | 77 AsyncSocket* socket = |
| 77 socket_factory()->CreateAsyncSocket(local_address.family(), | 78 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM); |
| 78 SOCK_STREAM); | |
| 79 if (!socket) { | 79 if (!socket) { |
| 80 return NULL; | 80 return NULL; |
| 81 } | 81 } |
| 82 | 82 |
| 83 if (BindSocket(socket, local_address, min_port, max_port) < 0) { | 83 if (BindSocket(socket, local_address, min_port, max_port) < 0) { |
| 84 LOG(LS_ERROR) << "TCP bind failed with error " | 84 LOG(LS_ERROR) << "TCP bind failed with error " |
| 85 << socket->GetError(); | 85 << socket->GetError(); |
| 86 delete socket; | 86 delete socket; |
| 87 return NULL; | 87 return NULL; |
| 88 } | 88 } |
| 89 | 89 |
| 90 // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket. | 90 // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket. |
| 91 if (opts & PacketSocketFactory::OPT_SSLTCP) { | 91 if (opts & PacketSocketFactory::OPT_SSLTCP) { |
| 92 ASSERT(!(opts & PacketSocketFactory::OPT_TLS)); | 92 ASSERT(!(opts & PacketSocketFactory::OPT_TLS)); |
| 93 socket = new rtc::AsyncSSLSocket(socket); | 93 socket = new AsyncSSLSocket(socket); |
| 94 } | 94 } |
| 95 | 95 |
| 96 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance. | 96 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance. |
| 97 // See http://go/gtalktcpnodelayexperiment | 97 // See http://go/gtalktcpnodelayexperiment |
| 98 socket->SetOption(rtc::Socket::OPT_NODELAY, 1); | 98 socket->SetOption(Socket::OPT_NODELAY, 1); |
| 99 | 99 |
| 100 if (opts & PacketSocketFactory::OPT_STUN) | 100 if (opts & PacketSocketFactory::OPT_STUN) |
| 101 return new cricket::AsyncStunTCPSocket(socket, true); | 101 return new cricket::AsyncStunTCPSocket(socket, true); |
| 102 | 102 |
| 103 return new rtc::AsyncTCPSocket(socket, true); | 103 return new AsyncTCPSocket(socket, true); |
| 104 } | 104 } |
| 105 | 105 |
| 106 AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket( | 106 AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket( |
| 107 const SocketAddress& local_address, const SocketAddress& remote_address, | 107 const SocketAddress& local_address, const SocketAddress& remote_address, |
| 108 const ProxyInfo& proxy_info, const std::string& user_agent, int opts) { | 108 const ProxyInfo& proxy_info, const std::string& user_agent, int opts) { |
| 109 | 109 AsyncSocket* socket = |
| 110 rtc::AsyncSocket* socket = | |
| 111 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM); | 110 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM); |
| 112 if (!socket) { | 111 if (!socket) { |
| 113 return NULL; | 112 return NULL; |
| 114 } | 113 } |
| 115 | 114 |
| 116 if (BindSocket(socket, local_address, 0, 0) < 0) { | 115 if (BindSocket(socket, local_address, 0, 0) < 0) { |
| 117 LOG(LS_ERROR) << "TCP bind failed with error " | 116 LOG(LS_ERROR) << "TCP bind failed with error " |
| 118 << socket->GetError(); | 117 << socket->GetError(); |
| 119 delete socket; | 118 delete socket; |
| 120 return NULL; | 119 return NULL; |
| 121 } | 120 } |
| 122 | 121 |
| 123 // If using a proxy, wrap the socket in a proxy socket. | 122 // If using a proxy, wrap the socket in a proxy socket. |
| 124 if (proxy_info.type == rtc::PROXY_SOCKS5) { | 123 if (proxy_info.type == PROXY_SOCKS5) { |
| 125 socket = new rtc::AsyncSocksProxySocket( | 124 socket = new AsyncSocksProxySocket( |
| 126 socket, proxy_info.address, proxy_info.username, proxy_info.password); | 125 socket, proxy_info.address, proxy_info.username, proxy_info.password); |
| 127 } else if (proxy_info.type == rtc::PROXY_HTTPS) { | 126 } else if (proxy_info.type == PROXY_HTTPS) { |
| 128 socket = new rtc::AsyncHttpsProxySocket( | 127 socket = |
| 129 socket, user_agent, proxy_info.address, | 128 new AsyncHttpsProxySocket(socket, user_agent, proxy_info.address, |
| 130 proxy_info.username, proxy_info.password); | 129 proxy_info.username, proxy_info.password); |
| 131 } | 130 } |
| 132 | 131 |
| 133 // If using TLS, wrap the socket in an SSL adapter. | 132 // If using TLS, wrap the socket in an SSL adapter. |
| 134 if (opts & PacketSocketFactory::OPT_TLS) { | 133 if (opts & PacketSocketFactory::OPT_TLS) { |
| 135 ASSERT(!(opts & PacketSocketFactory::OPT_SSLTCP)); | 134 ASSERT(!(opts & PacketSocketFactory::OPT_SSLTCP)); |
| 136 | 135 |
| 137 rtc::SSLAdapter* ssl_adapter = rtc::SSLAdapter::Create(socket); | 136 SSLAdapter* ssl_adapter = SSLAdapter::Create(socket); |
| 138 if (!ssl_adapter) { | 137 if (!ssl_adapter) { |
| 139 return NULL; | 138 return NULL; |
| 140 } | 139 } |
| 141 | 140 |
| 142 socket = ssl_adapter; | 141 socket = ssl_adapter; |
| 143 | 142 |
| 144 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) { | 143 if (ssl_adapter->StartSSL(remote_address.hostname().c_str(), false) != 0) { |
| 145 delete ssl_adapter; | 144 delete ssl_adapter; |
| 146 return NULL; | 145 return NULL; |
| 147 } | 146 } |
| 148 | 147 |
| 149 // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket. | 148 // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket. |
| 150 } else if (opts & PacketSocketFactory::OPT_SSLTCP) { | 149 } else if (opts & PacketSocketFactory::OPT_SSLTCP) { |
| 151 ASSERT(!(opts & PacketSocketFactory::OPT_TLS)); | 150 ASSERT(!(opts & PacketSocketFactory::OPT_TLS)); |
| 152 socket = new rtc::AsyncSSLSocket(socket); | 151 socket = new AsyncSSLSocket(socket); |
| 153 } | 152 } |
| 154 | 153 |
| 155 if (socket->Connect(remote_address) < 0) { | 154 if (socket->Connect(remote_address) < 0) { |
| 156 LOG(LS_ERROR) << "TCP connect failed with error " | 155 LOG(LS_ERROR) << "TCP connect failed with error " |
| 157 << socket->GetError(); | 156 << socket->GetError(); |
| 158 delete socket; | 157 delete socket; |
| 159 return NULL; | 158 return NULL; |
| 160 } | 159 } |
| 161 | 160 |
| 162 // Finally, wrap that socket in a TCP or STUN TCP packet socket. | 161 // Finally, wrap that socket in a TCP or STUN TCP packet socket. |
| 163 AsyncPacketSocket* tcp_socket; | 162 AsyncPacketSocket* tcp_socket; |
| 164 if (opts & PacketSocketFactory::OPT_STUN) { | 163 if (opts & PacketSocketFactory::OPT_STUN) { |
| 165 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false); | 164 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false); |
| 166 } else { | 165 } else { |
| 167 tcp_socket = new rtc::AsyncTCPSocket(socket, false); | 166 tcp_socket = new AsyncTCPSocket(socket, false); |
| 168 } | 167 } |
| 169 | 168 |
| 170 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance. | 169 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance. |
| 171 // See http://go/gtalktcpnodelayexperiment | 170 // See http://go/gtalktcpnodelayexperiment |
| 172 tcp_socket->SetOption(rtc::Socket::OPT_NODELAY, 1); | 171 tcp_socket->SetOption(Socket::OPT_NODELAY, 1); |
| 173 | 172 |
| 174 return tcp_socket; | 173 return tcp_socket; |
| 175 } | 174 } |
| 176 | 175 |
| 177 AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() { | 176 AsyncResolverInterface* BasicPacketSocketFactory::CreateAsyncResolver() { |
| 178 return new rtc::AsyncResolver(); | 177 return new AsyncResolver(); |
| 179 } | 178 } |
| 180 | 179 |
| 181 int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket, | 180 int BasicPacketSocketFactory::BindSocket(AsyncSocket* socket, |
| 182 const SocketAddress& local_address, | 181 const SocketAddress& local_address, |
| 183 uint16_t min_port, | 182 uint16_t min_port, |
| 184 uint16_t max_port) { | 183 uint16_t max_port) { |
| 185 int ret = -1; | 184 int ret = -1; |
| 186 if (min_port == 0 && max_port == 0) { | 185 if (min_port == 0 && max_port == 0) { |
| 187 // If there's no port range, let the OS pick a port for us. | 186 // If there's no port range, let the OS pick a port for us. |
| 188 ret = socket->Bind(local_address); | 187 ret = socket->Bind(local_address); |
| 189 } else { | 188 } else { |
| 190 // Otherwise, try to find a port in the provided range. | 189 // Otherwise, try to find a port in the provided range. |
| 191 for (int port = min_port; ret < 0 && port <= max_port; ++port) { | 190 for (int port = min_port; ret < 0 && port <= max_port; ++port) { |
| 192 ret = socket->Bind(rtc::SocketAddress(local_address.ipaddr(), | 191 ret = socket->Bind(SocketAddress(local_address.ipaddr(), port)); |
| 193 port)); | |
| 194 } | 192 } |
| 195 } | 193 } |
| 196 return ret; | 194 return ret; |
| 197 } | 195 } |
| 198 | 196 |
| 199 SocketFactory* BasicPacketSocketFactory::socket_factory() { | 197 SocketFactory* BasicPacketSocketFactory::socket_factory() { |
| 200 if (thread_) { | 198 if (thread_) { |
| 201 ASSERT(thread_ == Thread::Current()); | 199 ASSERT(thread_ == Thread::Current()); |
| 202 return thread_->socketserver(); | 200 return thread_->socketserver(); |
| 203 } else { | 201 } else { |
| 204 return socket_factory_; | 202 return socket_factory_; |
| 205 } | 203 } |
| 206 } | 204 } |
| 207 | 205 |
| 208 } // namespace rtc | 206 } // namespace rtc |
| OLD | NEW |