2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef WEBRTC_BASE_SOCKETADAPTERS_H_
12 #define WEBRTC_BASE_SOCKETADAPTERS_H_
17 #include "webrtc/base/asyncsocket.h"
18 #include "webrtc/base/cryptstring.h"
19 #include "webrtc/base/logging.h"
23 struct HttpAuthContext;
26 ///////////////////////////////////////////////////////////////////////////////
28 // Implements a socket adapter that can buffer and process data internally,
29 // as in the case of connecting to a proxy, where you must speak the proxy
30 // protocol before commencing normal socket behavior.
31 class BufferedReadAdapter : public AsyncSocketAdapter {
33 BufferedReadAdapter(AsyncSocket* socket, size_t buffer_size);
34 virtual ~BufferedReadAdapter();
36 virtual int Send(const void* pv, size_t cb);
37 virtual int Recv(void* pv, size_t cb);
40 int DirectSend(const void* pv, size_t cb) {
41 return AsyncSocketAdapter::Send(pv, cb);
44 void BufferInput(bool on = true);
45 virtual void ProcessInput(char* data, size_t* len) = 0;
47 virtual void OnReadEvent(AsyncSocket * socket);
51 size_t buffer_size_, data_len_;
53 DISALLOW_EVIL_CONSTRUCTORS(BufferedReadAdapter);
56 ///////////////////////////////////////////////////////////////////////////////
58 // Interface for implementing proxy server sockets.
59 class AsyncProxyServerSocket : public BufferedReadAdapter {
61 AsyncProxyServerSocket(AsyncSocket* socket, size_t buffer_size)
62 : BufferedReadAdapter(socket, buffer_size) {}
63 sigslot::signal2<AsyncProxyServerSocket*,
64 const SocketAddress&> SignalConnectRequest;
65 virtual void SendConnectResult(int err, const SocketAddress& addr) = 0;
68 ///////////////////////////////////////////////////////////////////////////////
70 // Implements a socket adapter that performs the client side of a
71 // fake SSL handshake. Used for "ssltcp" P2P functionality.
72 class AsyncSSLSocket : public BufferedReadAdapter {
74 explicit AsyncSSLSocket(AsyncSocket* socket);
76 virtual int Connect(const SocketAddress& addr);
79 virtual void OnConnectEvent(AsyncSocket* socket);
80 virtual void ProcessInput(char* data, size_t* len);
81 DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLSocket);
84 // Implements a socket adapter that performs the server side of a
85 // fake SSL handshake. Used when implementing a relay server that does "ssltcp".
86 class AsyncSSLServerSocket : public BufferedReadAdapter {
88 explicit AsyncSSLServerSocket(AsyncSocket* socket);
91 virtual void ProcessInput(char* data, size_t* len);
92 DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLServerSocket);
95 ///////////////////////////////////////////////////////////////////////////////
97 // Implements a socket adapter that speaks the HTTP/S proxy protocol.
98 class AsyncHttpsProxySocket : public BufferedReadAdapter {
100 AsyncHttpsProxySocket(AsyncSocket* socket, const std::string& user_agent,
101 const SocketAddress& proxy,
102 const std::string& username, const CryptString& password);
103 virtual ~AsyncHttpsProxySocket();
105 // If connect is forced, the adapter will always issue an HTTP CONNECT to the
106 // target address. Otherwise, it will connect only if the destination port
108 void SetForceConnect(bool force) { force_connect_ = force; }
110 virtual int Connect(const SocketAddress& addr);
111 virtual SocketAddress GetRemoteAddress() const;
113 virtual ConnState GetState() const;
116 virtual void OnConnectEvent(AsyncSocket* socket);
117 virtual void OnCloseEvent(AsyncSocket* socket, int err);
118 virtual void ProcessInput(char* data, size_t* len);
120 bool ShouldIssueConnect() const;
122 void ProcessLine(char* data, size_t len);
124 void Error(int error);
127 SocketAddress proxy_, dest_;
128 std::string agent_, user_, headers_;
131 size_t content_length_;
135 PS_INIT, PS_LEADER, PS_AUTHENTICATE, PS_SKIP_HEADERS, PS_ERROR_HEADERS,
136 PS_TUNNEL_HEADERS, PS_SKIP_BODY, PS_TUNNEL, PS_WAIT_CLOSE, PS_ERROR
138 HttpAuthContext * context_;
139 std::string unknown_mechanisms_;
140 DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxySocket);
143 /* TODO: Implement this.
144 class AsyncHttpsProxyServerSocket : public AsyncProxyServerSocket {
146 explicit AsyncHttpsProxyServerSocket(AsyncSocket* socket);
149 virtual void ProcessInput(char * data, size_t& len);
150 void Error(int error);
151 DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxyServerSocket);
155 ///////////////////////////////////////////////////////////////////////////////
157 // Implements a socket adapter that speaks the SOCKS proxy protocol.
158 class AsyncSocksProxySocket : public BufferedReadAdapter {
160 AsyncSocksProxySocket(AsyncSocket* socket, const SocketAddress& proxy,
161 const std::string& username, const CryptString& password);
163 virtual int Connect(const SocketAddress& addr);
164 virtual SocketAddress GetRemoteAddress() const;
166 virtual ConnState GetState() const;
169 virtual void OnConnectEvent(AsyncSocket* socket);
170 virtual void ProcessInput(char* data, size_t* len);
175 void Error(int error);
179 SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR
182 SocketAddress proxy_, dest_;
185 DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxySocket);
188 // Implements a proxy server socket for the SOCKS protocol.
189 class AsyncSocksProxyServerSocket : public AsyncProxyServerSocket {
191 explicit AsyncSocksProxyServerSocket(AsyncSocket* socket);
194 virtual void ProcessInput(char* data, size_t* len);
195 void DirectSend(const ByteBuffer& buf);
197 void HandleHello(ByteBuffer* request);
198 void SendHelloReply(uint8 method);
199 void HandleAuth(ByteBuffer* request);
200 void SendAuthReply(uint8 result);
201 void HandleConnect(ByteBuffer* request);
202 virtual void SendConnectResult(int result, const SocketAddress& addr);
204 void Error(int error);
206 static const int kBufferSize = 1024;
208 SS_HELLO, SS_AUTH, SS_CONNECT, SS_CONNECT_PENDING, SS_TUNNEL, SS_ERROR
211 DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxyServerSocket);
214 ///////////////////////////////////////////////////////////////////////////////
216 // Implements a socket adapter that logs everything that it sends and receives.
217 class LoggingSocketAdapter : public AsyncSocketAdapter {
219 LoggingSocketAdapter(AsyncSocket* socket, LoggingSeverity level,
220 const char * label, bool hex_mode = false);
222 virtual int Send(const void *pv, size_t cb);
223 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr);
224 virtual int Recv(void *pv, size_t cb);
225 virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr);
229 virtual void OnConnectEvent(AsyncSocket * socket);
230 virtual void OnCloseEvent(AsyncSocket * socket, int err);
233 LoggingSeverity level_;
236 LogMultilineState lms_;
237 DISALLOW_EVIL_CONSTRUCTORS(LoggingSocketAdapter);
240 ///////////////////////////////////////////////////////////////////////////////
244 #endif // WEBRTC_BASE_SOCKETADAPTERS_H_