| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ | 5 #ifndef NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |
| 6 #define NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ | 6 #define NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // This ClientSocket is used to setup a SOCKSv5 handshake with a socks proxy. | 25 // This ClientSocket is used to setup a SOCKSv5 handshake with a socks proxy. |
| 26 // Currently no SOCKSv5 authentication is supported. | 26 // Currently no SOCKSv5 authentication is supported. |
| 27 class SOCKS5ClientSocket : public ClientSocket { | 27 class SOCKS5ClientSocket : public ClientSocket { |
| 28 public: | 28 public: |
| 29 // Takes ownership of the |transport_socket|, which should already be | 29 // Takes ownership of the |transport_socket|, which should already be |
| 30 // connected by the time Connect() is called. | 30 // connected by the time Connect() is called. |
| 31 // | 31 // |
| 32 // |req_info| contains the hostname and port to which the socket above will | 32 // |req_info| contains the hostname and port to which the socket above will |
| 33 // communicate to via the SOCKS layer. | 33 // communicate to via the SOCKS layer. |
| 34 // | 34 // |
| 35 // SOCKS5 supports three modes of specifying connection endpoints: | 35 // Although SOCKS 5 supports 3 different modes of addressing, we will |
| 36 // (1) as an IPv4 address. | 36 // always pass it a hostname. This means the DNS resolving is done |
| 37 // (2) as an IPv6 address. | 37 // proxy side. |
| 38 // (3) as a hostname string. | |
| 39 // | |
| 40 // To select mode (3), pass NULL for |host_resolver|. | |
| 41 // | |
| 42 // Otherwise if a non-NULL |host_resolver| is given, Connect() will first | |
| 43 // try to resolve the hostname using |host_resolver|, and pass that | |
| 44 // resolved address to the proxy server. If the resolve failed, Connect() | |
| 45 // will fall-back to mode (3) and simply send the unresolved hosname string | |
| 46 // to the SOCKS v5 proxy server. | |
| 47 // | |
| 48 // Passing NULL for |host_resolver| is the recommended default. | |
| 49 SOCKS5ClientSocket(ClientSocket* transport_socket, | 38 SOCKS5ClientSocket(ClientSocket* transport_socket, |
| 50 const HostResolver::RequestInfo& req_info, | 39 const HostResolver::RequestInfo& req_info); |
| 51 HostResolver* host_resolver); | |
| 52 | 40 |
| 53 // On destruction Disconnect() is called. | 41 // On destruction Disconnect() is called. |
| 54 virtual ~SOCKS5ClientSocket(); | 42 virtual ~SOCKS5ClientSocket(); |
| 55 | 43 |
| 56 // ClientSocket methods: | 44 // ClientSocket methods: |
| 57 | 45 |
| 58 // Does the SOCKS handshake and completes the protocol. | 46 // Does the SOCKS handshake and completes the protocol. |
| 59 virtual int Connect(CompletionCallback* callback, LoadLog* load_log); | 47 virtual int Connect(CompletionCallback* callback, LoadLog* load_log); |
| 60 virtual void Disconnect(); | 48 virtual void Disconnect(); |
| 61 virtual bool IsConnected() const; | 49 virtual bool IsConnected() const; |
| 62 virtual bool IsConnectedAndIdle() const; | 50 virtual bool IsConnectedAndIdle() const; |
| 63 | 51 |
| 64 // Socket methods: | 52 // Socket methods: |
| 65 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); | 53 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 66 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); | 54 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 67 | 55 |
| 68 virtual bool SetReceiveBufferSize(int32 size); | 56 virtual bool SetReceiveBufferSize(int32 size); |
| 69 virtual bool SetSendBufferSize(int32 size); | 57 virtual bool SetSendBufferSize(int32 size); |
| 70 | 58 |
| 71 #if defined(OS_LINUX) | 59 #if defined(OS_LINUX) |
| 72 virtual int GetPeerName(struct sockaddr* name, socklen_t* namelen); | 60 virtual int GetPeerName(struct sockaddr* name, socklen_t* namelen); |
| 73 #endif | 61 #endif |
| 74 | 62 |
| 75 private: | 63 private: |
| 76 FRIEND_TEST(SOCKS5ClientSocketTest, IPv6Domain); | |
| 77 FRIEND_TEST(SOCKS5ClientSocketTest, FailedDNS); | |
| 78 FRIEND_TEST(SOCKS5ClientSocketTest, CompleteHandshake); | |
| 79 | |
| 80 enum State { | 64 enum State { |
| 81 STATE_RESOLVE_HOST, | |
| 82 STATE_RESOLVE_HOST_COMPLETE, | |
| 83 STATE_GREET_WRITE, | 65 STATE_GREET_WRITE, |
| 84 STATE_GREET_WRITE_COMPLETE, | 66 STATE_GREET_WRITE_COMPLETE, |
| 85 STATE_GREET_READ, | 67 STATE_GREET_READ, |
| 86 STATE_GREET_READ_COMPLETE, | 68 STATE_GREET_READ_COMPLETE, |
| 87 STATE_HANDSHAKE_WRITE, | 69 STATE_HANDSHAKE_WRITE, |
| 88 STATE_HANDSHAKE_WRITE_COMPLETE, | 70 STATE_HANDSHAKE_WRITE_COMPLETE, |
| 89 STATE_HANDSHAKE_READ, | 71 STATE_HANDSHAKE_READ, |
| 90 STATE_HANDSHAKE_READ_COMPLETE, | 72 STATE_HANDSHAKE_READ_COMPLETE, |
| 91 STATE_NONE, | 73 STATE_NONE, |
| 92 }; | 74 }; |
| 93 | 75 |
| 94 // State of the SOCKSv5 handshake. Before host resolution all connections | 76 // Addressing type that can be specified in requests or responses. |
| 95 // are kEndPointFailedDomain. If DNS lookup fails, we move to | |
| 96 // kEndPointFailedDomain, otherwise the IPv4/IPv6 address as resolved. | |
| 97 enum SocksEndPointAddressType { | 77 enum SocksEndPointAddressType { |
| 98 kEndPointUnresolved, | 78 kEndPointDomain = 0x03, |
| 99 kEndPointFailedDomain = 0x03, | |
| 100 kEndPointResolvedIPv4 = 0x01, | 79 kEndPointResolvedIPv4 = 0x01, |
| 101 kEndPointResolvedIPv6 = 0x04, | 80 kEndPointResolvedIPv6 = 0x04, |
| 102 }; | 81 }; |
| 103 | 82 |
| 104 static const unsigned int kGreetReadHeaderSize; | 83 static const unsigned int kGreetReadHeaderSize; |
| 105 static const unsigned int kWriteHeaderSize; | 84 static const unsigned int kWriteHeaderSize; |
| 106 static const unsigned int kReadHeaderSize; | 85 static const unsigned int kReadHeaderSize; |
| 107 static const uint8 kSOCKS5Version; | 86 static const uint8 kSOCKS5Version; |
| 108 static const uint8 kTunnelCommand; | 87 static const uint8 kTunnelCommand; |
| 109 static const uint8 kNullByte; | 88 static const uint8 kNullByte; |
| 110 | 89 |
| 111 void DoCallback(int result); | 90 void DoCallback(int result); |
| 112 void OnIOComplete(int result); | 91 void OnIOComplete(int result); |
| 113 | 92 |
| 114 int DoLoop(int last_io_result); | 93 int DoLoop(int last_io_result); |
| 115 int DoResolveHost(); | |
| 116 int DoResolveHostComplete(int result); | |
| 117 int DoHandshakeRead(); | 94 int DoHandshakeRead(); |
| 118 int DoHandshakeReadComplete(int result); | 95 int DoHandshakeReadComplete(int result); |
| 119 int DoHandshakeWrite(); | 96 int DoHandshakeWrite(); |
| 120 int DoHandshakeWriteComplete(int result); | 97 int DoHandshakeWriteComplete(int result); |
| 121 int DoGreetRead(); | 98 int DoGreetRead(); |
| 122 int DoGreetReadComplete(int result); | 99 int DoGreetReadComplete(int result); |
| 123 int DoGreetWrite(); | 100 int DoGreetWrite(); |
| 124 int DoGreetWriteComplete(int result); | 101 int DoGreetWriteComplete(int result); |
| 125 | 102 |
| 126 // Writes the SOCKS handshake buffer into |handshake| | 103 // Writes the SOCKS handshake buffer into |handshake| |
| 127 // and return OK on success. | 104 // and return OK on success. |
| 128 int BuildHandshakeWriteBuffer(std::string* handshake) const; | 105 int BuildHandshakeWriteBuffer(std::string* handshake) const; |
| 129 | 106 |
| 130 CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_; | 107 CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_; |
| 131 | 108 |
| 132 // Stores the underlying socket. | 109 // Stores the underlying socket. |
| 133 scoped_ptr<ClientSocket> transport_; | 110 scoped_ptr<ClientSocket> transport_; |
| 134 | 111 |
| 135 State next_state_; | 112 State next_state_; |
| 136 SocksEndPointAddressType address_type_; | |
| 137 | 113 |
| 138 // Stores the callback to the layer above, called on completing Connect(). | 114 // Stores the callback to the layer above, called on completing Connect(). |
| 139 CompletionCallback* user_callback_; | 115 CompletionCallback* user_callback_; |
| 140 | 116 |
| 141 // This IOBuffer is used by the class to read and write | 117 // This IOBuffer is used by the class to read and write |
| 142 // SOCKS handshake data. The length contains the expected size to | 118 // SOCKS handshake data. The length contains the expected size to |
| 143 // read or write. | 119 // read or write. |
| 144 scoped_refptr<IOBuffer> handshake_buf_; | 120 scoped_refptr<IOBuffer> handshake_buf_; |
| 145 | 121 |
| 146 // While writing, this buffer stores the complete write handshake data. | 122 // While writing, this buffer stores the complete write handshake data. |
| 147 // While reading, it stores the handshake information received so far. | 123 // While reading, it stores the handshake information received so far. |
| 148 std::string buffer_; | 124 std::string buffer_; |
| 149 | 125 |
| 150 // This becomes true when the SOCKS handshake has completed and the | 126 // This becomes true when the SOCKS handshake has completed and the |
| 151 // overlying connection is free to communicate. | 127 // overlying connection is free to communicate. |
| 152 bool completed_handshake_; | 128 bool completed_handshake_; |
| 153 | 129 |
| 154 // These contain the bytes sent / received by the SOCKS handshake. | 130 // These contain the bytes sent / received by the SOCKS handshake. |
| 155 size_t bytes_sent_; | 131 size_t bytes_sent_; |
| 156 size_t bytes_received_; | 132 size_t bytes_received_; |
| 157 | 133 |
| 158 size_t read_header_size; | 134 size_t read_header_size; |
| 159 | 135 |
| 160 // If non-NULL, we will use this host resolver to resolve DNS client-side | |
| 161 // (and fall back to proxy-side resolving if it fails). | |
| 162 // Otherwise, we will do proxy-side DNS resolving. | |
| 163 scoped_ptr<SingleRequestHostResolver> host_resolver_; | |
| 164 AddressList addresses_; | |
| 165 HostResolver::RequestInfo host_request_info_; | 136 HostResolver::RequestInfo host_request_info_; |
| 166 | 137 |
| 167 scoped_refptr<LoadLog> load_log_; | 138 scoped_refptr<LoadLog> load_log_; |
| 168 | 139 |
| 169 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket); | 140 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket); |
| 170 }; | 141 }; |
| 171 | 142 |
| 172 } // namespace net | 143 } // namespace net |
| 173 | 144 |
| 174 #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ | 145 #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |
| OLD | NEW |