1/*
2 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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.
9 */
10
11#ifndef WEBRTC_BASE_SOCKETADAPTERS_H_
12#define WEBRTC_BASE_SOCKETADAPTERS_H_
13
14#include <map>
15#include <string>
16
17#include "webrtc/base/asyncsocket.h"
18#include "webrtc/base/cryptstring.h"
19#include "webrtc/base/logging.h"
20
21namespace rtc {
22
23struct HttpAuthContext;
24class ByteBuffer;
25
26///////////////////////////////////////////////////////////////////////////////
27
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.
31class BufferedReadAdapter : public AsyncSocketAdapter {
32 public:
33  BufferedReadAdapter(AsyncSocket* socket, size_t buffer_size);
34  virtual ~BufferedReadAdapter();
35
36  virtual int Send(const void* pv, size_t cb);
37  virtual int Recv(void* pv, size_t cb);
38
39 protected:
40  int DirectSend(const void* pv, size_t cb) {
41    return AsyncSocketAdapter::Send(pv, cb);
42  }
43
44  void BufferInput(bool on = true);
45  virtual void ProcessInput(char* data, size_t* len) = 0;
46
47  virtual void OnReadEvent(AsyncSocket * socket);
48
49 private:
50  char * buffer_;
51  size_t buffer_size_, data_len_;
52  bool buffering_;
53  DISALLOW_EVIL_CONSTRUCTORS(BufferedReadAdapter);
54};
55
56///////////////////////////////////////////////////////////////////////////////
57
58// Interface for implementing proxy server sockets.
59class AsyncProxyServerSocket : public BufferedReadAdapter {
60 public:
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;
66};
67
68///////////////////////////////////////////////////////////////////////////////
69
70// Implements a socket adapter that performs the client side of a
71// fake SSL handshake. Used for "ssltcp" P2P functionality.
72class AsyncSSLSocket : public BufferedReadAdapter {
73 public:
74  explicit AsyncSSLSocket(AsyncSocket* socket);
75
76  virtual int Connect(const SocketAddress& addr);
77
78 protected:
79  virtual void OnConnectEvent(AsyncSocket* socket);
80  virtual void ProcessInput(char* data, size_t* len);
81  DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLSocket);
82};
83
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".
86class AsyncSSLServerSocket : public BufferedReadAdapter {
87 public:
88  explicit AsyncSSLServerSocket(AsyncSocket* socket);
89
90 protected:
91  virtual void ProcessInput(char* data, size_t* len);
92  DISALLOW_EVIL_CONSTRUCTORS(AsyncSSLServerSocket);
93};
94
95///////////////////////////////////////////////////////////////////////////////
96
97// Implements a socket adapter that speaks the HTTP/S proxy protocol.
98class AsyncHttpsProxySocket : public BufferedReadAdapter {
99 public:
100  AsyncHttpsProxySocket(AsyncSocket* socket, const std::string& user_agent,
101    const SocketAddress& proxy,
102    const std::string& username, const CryptString& password);
103  virtual ~AsyncHttpsProxySocket();
104
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
107  // is not port 80.
108  void SetForceConnect(bool force) { force_connect_ = force; }
109
110  virtual int Connect(const SocketAddress& addr);
111  virtual SocketAddress GetRemoteAddress() const;
112  virtual int Close();
113  virtual ConnState GetState() const;
114
115 protected:
116  virtual void OnConnectEvent(AsyncSocket* socket);
117  virtual void OnCloseEvent(AsyncSocket* socket, int err);
118  virtual void ProcessInput(char* data, size_t* len);
119
120  bool ShouldIssueConnect() const;
121  void SendRequest();
122  void ProcessLine(char* data, size_t len);
123  void EndResponse();
124  void Error(int error);
125
126 private:
127  SocketAddress proxy_, dest_;
128  std::string agent_, user_, headers_;
129  CryptString pass_;
130  bool force_connect_;
131  size_t content_length_;
132  int defer_error_;
133  bool expect_close_;
134  enum ProxyState {
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
137  } state_;
138  HttpAuthContext * context_;
139  std::string unknown_mechanisms_;
140  DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxySocket);
141};
142
143/* TODO: Implement this.
144class AsyncHttpsProxyServerSocket : public AsyncProxyServerSocket {
145 public:
146  explicit AsyncHttpsProxyServerSocket(AsyncSocket* socket);
147
148 private:
149  virtual void ProcessInput(char * data, size_t& len);
150  void Error(int error);
151  DISALLOW_EVIL_CONSTRUCTORS(AsyncHttpsProxyServerSocket);
152};
153*/
154
155///////////////////////////////////////////////////////////////////////////////
156
157// Implements a socket adapter that speaks the SOCKS proxy protocol.
158class AsyncSocksProxySocket : public BufferedReadAdapter {
159 public:
160  AsyncSocksProxySocket(AsyncSocket* socket, const SocketAddress& proxy,
161    const std::string& username, const CryptString& password);
162
163  virtual int Connect(const SocketAddress& addr);
164  virtual SocketAddress GetRemoteAddress() const;
165  virtual int Close();
166  virtual ConnState GetState() const;
167
168 protected:
169  virtual void OnConnectEvent(AsyncSocket* socket);
170  virtual void ProcessInput(char* data, size_t* len);
171
172  void SendHello();
173  void SendConnect();
174  void SendAuth();
175  void Error(int error);
176
177 private:
178  enum State {
179    SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR
180  };
181  State state_;
182  SocketAddress proxy_, dest_;
183  std::string user_;
184  CryptString pass_;
185  DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxySocket);
186};
187
188// Implements a proxy server socket for the SOCKS protocol.
189class AsyncSocksProxyServerSocket : public AsyncProxyServerSocket {
190 public:
191  explicit AsyncSocksProxyServerSocket(AsyncSocket* socket);
192
193 private:
194  virtual void ProcessInput(char* data, size_t* len);
195  void DirectSend(const ByteBuffer& buf);
196
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);
203
204  void Error(int error);
205
206  static const int kBufferSize = 1024;
207  enum State {
208    SS_HELLO, SS_AUTH, SS_CONNECT, SS_CONNECT_PENDING, SS_TUNNEL, SS_ERROR
209  };
210  State state_;
211  DISALLOW_EVIL_CONSTRUCTORS(AsyncSocksProxyServerSocket);
212};
213
214///////////////////////////////////////////////////////////////////////////////
215
216// Implements a socket adapter that logs everything that it sends and receives.
217class LoggingSocketAdapter : public AsyncSocketAdapter {
218 public:
219  LoggingSocketAdapter(AsyncSocket* socket, LoggingSeverity level,
220                 const char * label, bool hex_mode = false);
221
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);
226  virtual int Close();
227
228 protected:
229  virtual void OnConnectEvent(AsyncSocket * socket);
230  virtual void OnCloseEvent(AsyncSocket * socket, int err);
231
232 private:
233  LoggingSeverity level_;
234  std::string label_;
235  bool hex_mode_;
236  LogMultilineState lms_;
237  DISALLOW_EVIL_CONSTRUCTORS(LoggingSocketAdapter);
238};
239
240///////////////////////////////////////////////////////////////////////////////
241
242}  // namespace rtc
243
244#endif  // WEBRTC_BASE_SOCKETADAPTERS_H_
245