• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 namespace rtc {
22 
23 struct HttpAuthContext;
24 class 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.
31 class BufferedReadAdapter : public AsyncSocketAdapter {
32  public:
33   BufferedReadAdapter(AsyncSocket* socket, size_t buffer_size);
34   ~BufferedReadAdapter() override;
35 
36   int Send(const void* pv, size_t cb) override;
37   int Recv(void* pv, size_t cb) override;
38 
39  protected:
DirectSend(const void * pv,size_t cb)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   void OnReadEvent(AsyncSocket* socket) override;
48 
49  private:
50   char * buffer_;
51   size_t buffer_size_, data_len_;
52   bool buffering_;
53   RTC_DISALLOW_COPY_AND_ASSIGN(BufferedReadAdapter);
54 };
55 
56 ///////////////////////////////////////////////////////////////////////////////
57 
58 // Interface for implementing proxy server sockets.
59 class AsyncProxyServerSocket : public BufferedReadAdapter {
60  public:
61   AsyncProxyServerSocket(AsyncSocket* socket, size_t buffer_size);
62   ~AsyncProxyServerSocket() override;
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.
72 class AsyncSSLSocket : public BufferedReadAdapter {
73  public:
74   explicit AsyncSSLSocket(AsyncSocket* socket);
75 
76   int Connect(const SocketAddress& addr) override;
77 
78  protected:
79   void OnConnectEvent(AsyncSocket* socket) override;
80   void ProcessInput(char* data, size_t* len) override;
81   RTC_DISALLOW_COPY_AND_ASSIGN(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".
86 class AsyncSSLServerSocket : public BufferedReadAdapter {
87  public:
88   explicit AsyncSSLServerSocket(AsyncSocket* socket);
89 
90  protected:
91   void ProcessInput(char* data, size_t* len) override;
92   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSSLServerSocket);
93 };
94 
95 ///////////////////////////////////////////////////////////////////////////////
96 
97 // Implements a socket adapter that speaks the HTTP/S proxy protocol.
98 class 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   ~AsyncHttpsProxySocket() override;
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.
SetForceConnect(bool force)108   void SetForceConnect(bool force) { force_connect_ = force; }
109 
110   int Connect(const SocketAddress& addr) override;
111   SocketAddress GetRemoteAddress() const override;
112   int Close() override;
113   ConnState GetState() const override;
114 
115  protected:
116   void OnConnectEvent(AsyncSocket* socket) override;
117   void OnCloseEvent(AsyncSocket* socket, int err) override;
118   void ProcessInput(char* data, size_t* len) override;
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   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncHttpsProxySocket);
141 };
142 
143 /* TODO: Implement this.
144 class 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   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncHttpsProxyServerSocket);
152 };
153 */
154 
155 ///////////////////////////////////////////////////////////////////////////////
156 
157 // Implements a socket adapter that speaks the SOCKS proxy protocol.
158 class AsyncSocksProxySocket : public BufferedReadAdapter {
159  public:
160   AsyncSocksProxySocket(AsyncSocket* socket, const SocketAddress& proxy,
161     const std::string& username, const CryptString& password);
162   ~AsyncSocksProxySocket() override;
163 
164   int Connect(const SocketAddress& addr) override;
165   SocketAddress GetRemoteAddress() const override;
166   int Close() override;
167   ConnState GetState() const override;
168 
169  protected:
170   void OnConnectEvent(AsyncSocket* socket) override;
171   void ProcessInput(char* data, size_t* len) override;
172 
173   void SendHello();
174   void SendConnect();
175   void SendAuth();
176   void Error(int error);
177 
178  private:
179   enum State {
180     SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR
181   };
182   State state_;
183   SocketAddress proxy_, dest_;
184   std::string user_;
185   CryptString pass_;
186   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSocksProxySocket);
187 };
188 
189 // Implements a proxy server socket for the SOCKS protocol.
190 class AsyncSocksProxyServerSocket : public AsyncProxyServerSocket {
191  public:
192   explicit AsyncSocksProxyServerSocket(AsyncSocket* socket);
193 
194  private:
195   void ProcessInput(char* data, size_t* len) override;
196   void DirectSend(const ByteBuffer& buf);
197 
198   void HandleHello(ByteBuffer* request);
199   void SendHelloReply(uint8_t method);
200   void HandleAuth(ByteBuffer* request);
201   void SendAuthReply(uint8_t result);
202   void HandleConnect(ByteBuffer* request);
203   void SendConnectResult(int result, const SocketAddress& addr) override;
204 
205   void Error(int error);
206 
207   static const int kBufferSize = 1024;
208   enum State {
209     SS_HELLO, SS_AUTH, SS_CONNECT, SS_CONNECT_PENDING, SS_TUNNEL, SS_ERROR
210   };
211   State state_;
212   RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSocksProxyServerSocket);
213 };
214 
215 ///////////////////////////////////////////////////////////////////////////////
216 
217 // Implements a socket adapter that logs everything that it sends and receives.
218 class LoggingSocketAdapter : public AsyncSocketAdapter {
219  public:
220   LoggingSocketAdapter(AsyncSocket* socket, LoggingSeverity level,
221                  const char * label, bool hex_mode = false);
222 
223   int Send(const void* pv, size_t cb) override;
224   int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
225   int Recv(void* pv, size_t cb) override;
226   int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) override;
227   int Close() override;
228 
229  protected:
230   void OnConnectEvent(AsyncSocket* socket) override;
231   void OnCloseEvent(AsyncSocket* socket, int err) override;
232 
233  private:
234   LoggingSeverity level_;
235   std::string label_;
236   bool hex_mode_;
237   LogMultilineState lms_;
238   RTC_DISALLOW_COPY_AND_ASSIGN(LoggingSocketAdapter);
239 };
240 
241 ///////////////////////////////////////////////////////////////////////////////
242 
243 }  // namespace rtc
244 
245 #endif  // WEBRTC_BASE_SOCKETADAPTERS_H_
246