3 * Copyright 2004--2005, 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 #ifndef TALK_BASE_SOCKETADAPTERS_H_
29 #define TALK_BASE_SOCKETADAPTERS_H_
34 #include "talk/base/asyncsocket.h"
35 #include "talk/base/cryptstring.h"
36 #include "talk/base/logging.h"
40 struct HttpAuthContext;
43 ///////////////////////////////////////////////////////////////////////////////
45 // Implements a socket adapter that can buffer and process data internally,
46 // as in the case of connecting to a proxy, where you must speak the proxy
47 // protocol before commencing normal socket behavior.
48 class BufferedReadAdapter : public AsyncSocketAdapter {
50 BufferedReadAdapter(AsyncSocket* socket, size_t buffer_size);
51 virtual ~BufferedReadAdapter();
53 virtual int Send(const void* pv, size_t cb);
54 virtual int Recv(void* pv, size_t cb);
57 int DirectSend(const void* pv, size_t cb) {
58 return AsyncSocketAdapter::Send(pv, cb);
61 void BufferInput(bool on = true);
62 virtual void ProcessInput(char* data, size_t* len) = 0;
64 virtual void OnReadEvent(AsyncSocket * socket);
68 size_t buffer_size_, data_len_;
70 DISALLOW_EVIL_CONSTRUCTORS(BufferedReadAdapter);
73 ///////////////////////////////////////////////////////////////////////////////
75 // Interface for implementing proxy server sockets.
76 class AsyncProxyServerSocket : public BufferedReadAdapter {
78 AsyncProxyServerSocket(AsyncSocket* socket, size_t buffer_size)
79 : BufferedReadAdapter(socket, buffer_size) {}
80 sigslot::signal2<AsyncProxyServerSocket*,
81 const SocketAddress&> SignalConnectRequest;
82 virtual void SendConnectResult(int err, const SocketAddress& addr) = 0;
85 ///////////////////////////////////////////////////////////////////////////////
87 // Implements a socket adapter that performs the client side of a
88 // fake SSL handshake. Used for "ssltcp" P2P functionality.
89 class AsyncSSLSocket : public BufferedReadAdapter {
91 explicit AsyncSSLSocket(AsyncSocket* socket);
93 virtual int Connect(const SocketAddress& addr);
96 virtual void OnConnectEvent(AsyncSocket* socket);
97 virtual void ProcessInput(char* data, size_t* len);
98 DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLSocket);
101 // Implements a socket adapter that performs the server side of a
102 // fake SSL handshake. Used when implementing a relay server that does "ssltcp".
103 class AsyncSSLServerSocket : public BufferedReadAdapter {
105 explicit AsyncSSLServerSocket(AsyncSocket* socket);
108 virtual void ProcessInput(char* data, size_t* len);
109 DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLServerSocket);
112 ///////////////////////////////////////////////////////////////////////////////
114 // Implements a socket adapter that speaks the HTTP/S proxy protocol.
115 class AsyncHttpsProxySocket : public BufferedReadAdapter {
117 AsyncHttpsProxySocket(AsyncSocket* socket, const std::string& user_agent,
118 const SocketAddress& proxy,
119 const std::string& username, const CryptString& password);
120 virtual ~AsyncHttpsProxySocket();
122 // If connect is forced, the adapter will always issue an HTTP CONNECT to the
123 // target address. Otherwise, it will connect only if the destination port
125 void SetForceConnect(bool force) { force_connect_ = force; }
127 virtual int Connect(const SocketAddress& addr);
128 virtual SocketAddress GetRemoteAddress() const;
130 virtual ConnState GetState() const;
133 virtual void OnConnectEvent(AsyncSocket* socket);
134 virtual void OnCloseEvent(AsyncSocket* socket, int err);
135 virtual void ProcessInput(char* data, size_t* len);
137 bool ShouldIssueConnect() const;
139 void ProcessLine(char* data, size_t len);
141 void Error(int error);
144 SocketAddress proxy_, dest_;
145 std::string agent_, user_, headers_;
148 size_t content_length_;
152 PS_INIT, PS_LEADER, PS_AUTHENTICATE, PS_SKIP_HEADERS, PS_ERROR_HEADERS,
153 PS_TUNNEL_HEADERS, PS_SKIP_BODY, PS_TUNNEL, PS_WAIT_CLOSE, PS_ERROR
155 HttpAuthContext * context_;
156 std::string unknown_mechanisms_;
157 DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxySocket);
160 /* TODO: Implement this.
161 class AsyncHttpsProxyServerSocket : public AsyncProxyServerSocket {
163 explicit AsyncHttpsProxyServerSocket(AsyncSocket* socket);
166 virtual void ProcessInput(char * data, size_t& len);
167 void Error(int error);
168 DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxyServerSocket);
172 ///////////////////////////////////////////////////////////////////////////////
174 // Implements a socket adapter that speaks the SOCKS proxy protocol.
175 class AsyncSocksProxySocket : public BufferedReadAdapter {
177 AsyncSocksProxySocket(AsyncSocket* socket, const SocketAddress& proxy,
178 const std::string& username, const CryptString& password);
180 virtual int Connect(const SocketAddress& addr);
181 virtual SocketAddress GetRemoteAddress() const;
183 virtual ConnState GetState() const;
186 virtual void OnConnectEvent(AsyncSocket* socket);
187 virtual void ProcessInput(char* data, size_t* len);
192 void Error(int error);
196 SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR
199 SocketAddress proxy_, dest_;
202 DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxySocket);
205 // Implements a proxy server socket for the SOCKS protocol.
206 class AsyncSocksProxyServerSocket : public AsyncProxyServerSocket {
208 explicit AsyncSocksProxyServerSocket(AsyncSocket* socket);
211 virtual void ProcessInput(char* data, size_t* len);
212 void DirectSend(const ByteBuffer& buf);
214 void HandleHello(ByteBuffer* request);
215 void SendHelloReply(int method);
216 void HandleAuth(ByteBuffer* request);
217 void SendAuthReply(int result);
218 void HandleConnect(ByteBuffer* request);
219 virtual void SendConnectResult(int result, const SocketAddress& addr);
221 void Error(int error);
223 static const int kBufferSize = 1024;
225 SS_HELLO, SS_AUTH, SS_CONNECT, SS_CONNECT_PENDING, SS_TUNNEL, SS_ERROR
228 DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxyServerSocket);
231 ///////////////////////////////////////////////////////////////////////////////
233 // Implements a socket adapter that logs everything that it sends and receives.
234 class LoggingSocketAdapter : public AsyncSocketAdapter {
236 LoggingSocketAdapter(AsyncSocket* socket, LoggingSeverity level,
237 const char * label, bool hex_mode = false);
239 virtual int Send(const void *pv, size_t cb);
240 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr);
241 virtual int Recv(void *pv, size_t cb);
242 virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr);
246 virtual void OnConnectEvent(AsyncSocket * socket);
247 virtual void OnCloseEvent(AsyncSocket * socket, int err);
250 LoggingSeverity level_;
253 LogMultilineState lms_;
254 DISALLOW_EVIL_CONSTRUCTORS(LoggingSocketAdapter);
257 ///////////////////////////////////////////////////////////////////////////////
259 } // namespace talk_base
261 #endif // TALK_BASE_SOCKETADAPTERS_H_