henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef RTC_BASE_SOCKETADAPTERS_H_ |
| 12 | #define RTC_BASE_SOCKETADAPTERS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "rtc_base/asyncsocket.h" |
| 18 | #include "rtc_base/constructormagic.h" |
| 19 | #include "rtc_base/cryptstring.h" |
| 20 | #include "rtc_base/logging.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | struct HttpAuthContext; |
| 25 | class ByteBufferReader; |
| 26 | class ByteBufferWriter; |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | // Implements a socket adapter that can buffer and process data internally, |
| 31 | // as in the case of connecting to a proxy, where you must speak the proxy |
| 32 | // protocol before commencing normal socket behavior. |
| 33 | class BufferedReadAdapter : public AsyncSocketAdapter { |
| 34 | public: |
| 35 | BufferedReadAdapter(AsyncSocket* socket, size_t buffer_size); |
| 36 | ~BufferedReadAdapter() override; |
| 37 | |
| 38 | int Send(const void* pv, size_t cb) override; |
| 39 | int Recv(void* pv, size_t cb, int64_t* timestamp) override; |
| 40 | |
| 41 | protected: |
| 42 | int DirectSend(const void* pv, size_t cb) { |
| 43 | return AsyncSocketAdapter::Send(pv, cb); |
| 44 | } |
| 45 | |
| 46 | void BufferInput(bool on = true); |
| 47 | virtual void ProcessInput(char* data, size_t* len) = 0; |
| 48 | |
| 49 | void OnReadEvent(AsyncSocket* socket) override; |
| 50 | |
| 51 | private: |
| 52 | char * buffer_; |
| 53 | size_t buffer_size_, data_len_; |
| 54 | bool buffering_; |
| 55 | RTC_DISALLOW_COPY_AND_ASSIGN(BufferedReadAdapter); |
| 56 | }; |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | // Interface for implementing proxy server sockets. |
| 61 | class AsyncProxyServerSocket : public BufferedReadAdapter { |
| 62 | public: |
| 63 | AsyncProxyServerSocket(AsyncSocket* socket, size_t buffer_size); |
| 64 | ~AsyncProxyServerSocket() override; |
| 65 | sigslot::signal2<AsyncProxyServerSocket*, |
| 66 | const SocketAddress&> SignalConnectRequest; |
| 67 | virtual void SendConnectResult(int err, const SocketAddress& addr) = 0; |
| 68 | }; |
| 69 | |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | // Implements a socket adapter that performs the client side of a |
| 73 | // fake SSL handshake. Used for "ssltcp" P2P functionality. |
| 74 | class AsyncSSLSocket : public BufferedReadAdapter { |
| 75 | public: |
| 76 | explicit AsyncSSLSocket(AsyncSocket* socket); |
| 77 | |
| 78 | int Connect(const SocketAddress& addr) override; |
| 79 | |
| 80 | protected: |
| 81 | void OnConnectEvent(AsyncSocket* socket) override; |
| 82 | void ProcessInput(char* data, size_t* len) override; |
| 83 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSSLSocket); |
| 84 | }; |
| 85 | |
| 86 | // Implements a socket adapter that performs the server side of a |
| 87 | // fake SSL handshake. Used when implementing a relay server that does "ssltcp". |
| 88 | class AsyncSSLServerSocket : public BufferedReadAdapter { |
| 89 | public: |
| 90 | explicit AsyncSSLServerSocket(AsyncSocket* socket); |
| 91 | |
| 92 | protected: |
| 93 | void ProcessInput(char* data, size_t* len) override; |
| 94 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSSLServerSocket); |
| 95 | }; |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | // Implements a socket adapter that speaks the HTTP/S proxy protocol. |
| 100 | class AsyncHttpsProxySocket : public BufferedReadAdapter { |
| 101 | public: |
| 102 | AsyncHttpsProxySocket(AsyncSocket* socket, const std::string& user_agent, |
| 103 | const SocketAddress& proxy, |
| 104 | const std::string& username, const CryptString& password); |
| 105 | ~AsyncHttpsProxySocket() override; |
| 106 | |
| 107 | // If connect is forced, the adapter will always issue an HTTP CONNECT to the |
| 108 | // target address. Otherwise, it will connect only if the destination port |
| 109 | // is not port 80. |
| 110 | void SetForceConnect(bool force) { force_connect_ = force; } |
| 111 | |
| 112 | int Connect(const SocketAddress& addr) override; |
| 113 | SocketAddress GetRemoteAddress() const override; |
| 114 | int Close() override; |
| 115 | ConnState GetState() const override; |
| 116 | |
| 117 | protected: |
| 118 | void OnConnectEvent(AsyncSocket* socket) override; |
| 119 | void OnCloseEvent(AsyncSocket* socket, int err) override; |
| 120 | void ProcessInput(char* data, size_t* len) override; |
| 121 | |
| 122 | bool ShouldIssueConnect() const; |
| 123 | void SendRequest(); |
| 124 | void ProcessLine(char* data, size_t len); |
| 125 | void EndResponse(); |
| 126 | void Error(int error); |
| 127 | |
| 128 | private: |
| 129 | SocketAddress proxy_, dest_; |
| 130 | std::string agent_, user_, headers_; |
| 131 | CryptString pass_; |
| 132 | bool force_connect_; |
| 133 | size_t content_length_; |
| 134 | int defer_error_; |
| 135 | bool expect_close_; |
| 136 | enum ProxyState { |
| 137 | PS_INIT, PS_LEADER, PS_AUTHENTICATE, PS_SKIP_HEADERS, PS_ERROR_HEADERS, |
| 138 | PS_TUNNEL_HEADERS, PS_SKIP_BODY, PS_TUNNEL, PS_WAIT_CLOSE, PS_ERROR |
| 139 | } state_; |
| 140 | HttpAuthContext * context_; |
| 141 | std::string unknown_mechanisms_; |
| 142 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncHttpsProxySocket); |
| 143 | }; |
| 144 | |
| 145 | /////////////////////////////////////////////////////////////////////////////// |
| 146 | |
| 147 | // Implements a socket adapter that speaks the SOCKS proxy protocol. |
| 148 | class AsyncSocksProxySocket : public BufferedReadAdapter { |
| 149 | public: |
| 150 | AsyncSocksProxySocket(AsyncSocket* socket, const SocketAddress& proxy, |
| 151 | const std::string& username, const CryptString& password); |
| 152 | ~AsyncSocksProxySocket() override; |
| 153 | |
| 154 | int Connect(const SocketAddress& addr) override; |
| 155 | SocketAddress GetRemoteAddress() const override; |
| 156 | int Close() override; |
| 157 | ConnState GetState() const override; |
| 158 | |
| 159 | protected: |
| 160 | void OnConnectEvent(AsyncSocket* socket) override; |
| 161 | void ProcessInput(char* data, size_t* len) override; |
| 162 | |
| 163 | void SendHello(); |
| 164 | void SendConnect(); |
| 165 | void SendAuth(); |
| 166 | void Error(int error); |
| 167 | |
| 168 | private: |
| 169 | enum State { |
| 170 | SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR |
| 171 | }; |
| 172 | State state_; |
| 173 | SocketAddress proxy_, dest_; |
| 174 | std::string user_; |
| 175 | CryptString pass_; |
| 176 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSocksProxySocket); |
| 177 | }; |
| 178 | |
| 179 | // Implements a proxy server socket for the SOCKS protocol. |
| 180 | class AsyncSocksProxyServerSocket : public AsyncProxyServerSocket { |
| 181 | public: |
| 182 | explicit AsyncSocksProxyServerSocket(AsyncSocket* socket); |
| 183 | |
| 184 | private: |
| 185 | void ProcessInput(char* data, size_t* len) override; |
| 186 | void DirectSend(const ByteBufferWriter& buf); |
| 187 | |
| 188 | void HandleHello(ByteBufferReader* request); |
| 189 | void SendHelloReply(uint8_t method); |
| 190 | void HandleAuth(ByteBufferReader* request); |
| 191 | void SendAuthReply(uint8_t result); |
| 192 | void HandleConnect(ByteBufferReader* request); |
| 193 | void SendConnectResult(int result, const SocketAddress& addr) override; |
| 194 | |
| 195 | void Error(int error); |
| 196 | |
| 197 | static const int kBufferSize = 1024; |
| 198 | enum State { |
| 199 | SS_HELLO, SS_AUTH, SS_CONNECT, SS_CONNECT_PENDING, SS_TUNNEL, SS_ERROR |
| 200 | }; |
| 201 | State state_; |
| 202 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSocksProxyServerSocket); |
| 203 | }; |
| 204 | |
| 205 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 206 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 207 | #endif // RTC_BASE_SOCKETADAPTERS_H_ |