3
* Copyright 2011, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/basicpacketsocketfactory.h"
30
#include "talk/base/asyncudpsocket.h"
31
#include "talk/base/asynctcpsocket.h"
32
#include "talk/base/logging.h"
33
#include "talk/base/socketadapters.h"
34
#include "talk/base/thread.h"
38
BasicPacketSocketFactory::BasicPacketSocketFactory()
39
: thread_(Thread::Current()),
40
socket_factory_(NULL) {
43
BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread)
45
socket_factory_(NULL) {
48
BasicPacketSocketFactory::BasicPacketSocketFactory(
49
SocketFactory* socket_factory)
51
socket_factory_(socket_factory) {
54
BasicPacketSocketFactory::~BasicPacketSocketFactory() {
57
AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
58
const SocketAddress& address, int min_port, int max_port) {
59
// UDP sockets are simple.
60
talk_base::AsyncSocket* socket =
61
socket_factory()->CreateAsyncSocket(SOCK_DGRAM);
65
if (BindSocket(socket, address, min_port, max_port) < 0) {
66
LOG(LS_ERROR) << "UDP bind failed with error "
67
<< socket->GetError();
71
return new talk_base::AsyncUDPSocket(socket);
74
AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
75
const SocketAddress& local_address, int min_port, int max_port, bool ssl) {
76
talk_base::AsyncSocket* socket =
77
socket_factory()->CreateAsyncSocket(SOCK_STREAM);
82
if (BindSocket(socket, local_address, min_port, max_port) < 0) {
83
LOG(LS_ERROR) << "TCP bind failed with error "
84
<< socket->GetError();
89
// If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
91
socket = new talk_base::AsyncSSLSocket(socket);
94
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
95
// See http://go/gtalktcpnodelayexperiment
96
socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
98
return new talk_base::AsyncTCPSocket(socket, true);
101
AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
102
const SocketAddress& local_address, const SocketAddress& remote_address,
103
const ProxyInfo& proxy_info, const std::string& user_agent, bool ssl) {
104
talk_base::AsyncSocket* socket =
105
socket_factory()->CreateAsyncSocket(SOCK_STREAM);
110
if (BindSocket(socket, local_address, 0, 0) < 0) {
111
LOG(LS_ERROR) << "TCP bind failed with error "
112
<< socket->GetError();
117
// If using a proxy, wrap the socket in a proxy socket.
118
if (proxy_info.type == talk_base::PROXY_SOCKS5) {
119
socket = new talk_base::AsyncSocksProxySocket(
120
socket, proxy_info.address, proxy_info.username, proxy_info.password);
121
} else if (proxy_info.type == talk_base::PROXY_HTTPS) {
122
socket = new talk_base::AsyncHttpsProxySocket(
123
socket, user_agent, proxy_info.address,
124
proxy_info.username, proxy_info.password);
127
// If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
129
socket = new talk_base::AsyncSSLSocket(socket);
132
if (socket->Connect(remote_address) < 0) {
133
LOG(LS_ERROR) << "TCP connect failed with error "
134
<< socket->GetError();
139
// Finally, wrap that socket in a TCP packet socket.
140
talk_base::AsyncTCPSocket* tcp_socket =
141
new talk_base::AsyncTCPSocket(socket, false);
143
// Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
144
// See http://go/gtalktcpnodelayexperiment
145
tcp_socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
150
int BasicPacketSocketFactory::BindSocket(
151
AsyncSocket* socket, const SocketAddress& local_address,
152
int min_port, int max_port) {
154
if (min_port == 0 && max_port == 0) {
155
// If there's no port range, let the OS pick a port for us.
156
ret = socket->Bind(local_address);
158
// Otherwise, try to find a port in the provided range.
159
for (int port = min_port; ret < 0 && port <= max_port; ++port) {
160
ret = socket->Bind(talk_base::SocketAddress(local_address.ipaddr(),
167
SocketFactory* BasicPacketSocketFactory::socket_factory() {
169
ASSERT(thread_ == Thread::Current());
170
return thread_->socketserver();
172
return socket_factory_;
176
} // namespace talk_base