blob: 714210470a646055ab876aebf341f5f1c815eab6 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#include "talk/p2p/base/basicpacketsocketfactory.h"
29
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"
35#include "talk/p2p/base/asyncstuntcpsocket.h"
36#include "talk/p2p/base/stun.h"
37
38namespace talk_base {
39
40BasicPacketSocketFactory::BasicPacketSocketFactory()
41 : thread_(Thread::Current()),
42 socket_factory_(NULL) {
43}
44
45BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread)
46 : thread_(thread),
47 socket_factory_(NULL) {
48}
49
50BasicPacketSocketFactory::BasicPacketSocketFactory(
51 SocketFactory* socket_factory)
52 : thread_(NULL),
53 socket_factory_(socket_factory) {
54}
55
56BasicPacketSocketFactory::~BasicPacketSocketFactory() {
57}
58
59AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
60 const SocketAddress& address, int min_port, int max_port) {
61 // UDP sockets are simple.
62 talk_base::AsyncSocket* socket =
63 socket_factory()->CreateAsyncSocket(
64 address.family(), SOCK_DGRAM);
65 if (!socket) {
66 return NULL;
67 }
68 if (BindSocket(socket, address, min_port, max_port) < 0) {
69 LOG(LS_ERROR) << "UDP bind failed with error "
70 << socket->GetError();
71 delete socket;
72 return NULL;
73 }
74 return new talk_base::AsyncUDPSocket(socket);
75}
76
77AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
78 const SocketAddress& local_address, int min_port, int max_port, int opts) {
79 talk_base::AsyncSocket* socket =
80 socket_factory()->CreateAsyncSocket(local_address.family(),
81 SOCK_STREAM);
82 if (!socket) {
83 return NULL;
84 }
85
86 if (BindSocket(socket, local_address, min_port, max_port) < 0) {
87 LOG(LS_ERROR) << "TCP bind failed with error "
88 << socket->GetError();
89 delete socket;
90 return NULL;
91 }
92
93 // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
94 if (opts & PacketSocketFactory::OPT_SSLTCP) {
95 socket = new talk_base::AsyncSSLSocket(socket);
96 }
97
98 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
99 // See http://go/gtalktcpnodelayexperiment
100 socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
101
102 if (opts & PacketSocketFactory::OPT_STUN)
103 return new cricket::AsyncStunTCPSocket(socket, true);
104
105 return new talk_base::AsyncTCPSocket(socket, true);
106}
107
108AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
109 const SocketAddress& local_address, const SocketAddress& remote_address,
110 const ProxyInfo& proxy_info, const std::string& user_agent, int opts) {
111 talk_base::AsyncSocket* socket =
112 socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
113 if (!socket) {
114 return NULL;
115 }
116
117 if (BindSocket(socket, local_address, 0, 0) < 0) {
118 LOG(LS_ERROR) << "TCP bind failed with error "
119 << socket->GetError();
120 delete socket;
121 return NULL;
122 }
123
124 // If using a proxy, wrap the socket in a proxy socket.
125 if (proxy_info.type == talk_base::PROXY_SOCKS5) {
126 socket = new talk_base::AsyncSocksProxySocket(
127 socket, proxy_info.address, proxy_info.username, proxy_info.password);
128 } else if (proxy_info.type == talk_base::PROXY_HTTPS) {
129 socket = new talk_base::AsyncHttpsProxySocket(
130 socket, user_agent, proxy_info.address,
131 proxy_info.username, proxy_info.password);
132 }
133
134 // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
135 if (opts & PacketSocketFactory::OPT_SSLTCP) {
136 socket = new talk_base::AsyncSSLSocket(socket);
137 }
138
139 if (socket->Connect(remote_address) < 0) {
140 LOG(LS_ERROR) << "TCP connect failed with error "
141 << socket->GetError();
142 delete socket;
143 return NULL;
144 }
145
146 // Finally, wrap that socket in a TCP or STUN TCP packet socket.
147 AsyncPacketSocket* tcp_socket;
148 if (opts & PacketSocketFactory::OPT_STUN) {
149 tcp_socket = new cricket::AsyncStunTCPSocket(socket, false);
150 } else {
151 tcp_socket = new talk_base::AsyncTCPSocket(socket, false);
152 }
153
154 // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
155 // See http://go/gtalktcpnodelayexperiment
156 tcp_socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
157
158 return tcp_socket;
159}
160
161int BasicPacketSocketFactory::BindSocket(
162 AsyncSocket* socket, const SocketAddress& local_address,
163 int min_port, int max_port) {
164 int ret = -1;
165 if (min_port == 0 && max_port == 0) {
166 // If there's no port range, let the OS pick a port for us.
167 ret = socket->Bind(local_address);
168 } else {
169 // Otherwise, try to find a port in the provided range.
170 for (int port = min_port; ret < 0 && port <= max_port; ++port) {
171 ret = socket->Bind(talk_base::SocketAddress(local_address.ipaddr(),
172 port));
173 }
174 }
175 return ret;
176}
177
178SocketFactory* BasicPacketSocketFactory::socket_factory() {
179 if (thread_) {
180 ASSERT(thread_ == Thread::Current());
181 return thread_->socketserver();
182 } else {
183 return socket_factory_;
184 }
185}
186
187} // namespace talk_base