[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |
| 6 | #define NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 12 | #include "base/ref_counted.h" |
| 13 | #include "base/scoped_ptr.h" |
| 14 | #include "googleurl/src/gurl.h" |
| 15 | #include "net/base/address_list.h" |
| 16 | #include "net/base/completion_callback.h" |
| 17 | #include "net/base/host_resolver.h" |
| 18 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 19 | #include "net/base/net_log.h" |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 20 | #include "net/socket/client_socket.h" |
| 21 | #include "testing/gtest/include/gtest/gtest_prod.h" |
| 22 | |
| 23 | namespace net { |
| 24 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 25 | class ClientSocketHandle; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 26 | class BoundNetLog; |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 27 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 28 | // This ClientSocket is used to setup a SOCKSv5 handshake with a socks proxy. |
| 29 | // Currently no SOCKSv5 authentication is supported. |
| 30 | class SOCKS5ClientSocket : public ClientSocket { |
| 31 | public: |
| 32 | // Takes ownership of the |transport_socket|, which should already be |
| 33 | // connected by the time Connect() is called. |
| 34 | // |
| 35 | // |req_info| contains the hostname and port to which the socket above will |
| 36 | // communicate to via the SOCKS layer. |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 37 | // |
[email protected] | 20cbe23d | 2009-12-18 03:39:21 | [diff] [blame] | 38 | // Although SOCKS 5 supports 3 different modes of addressing, we will |
| 39 | // always pass it a hostname. This means the DNS resolving is done |
| 40 | // proxy side. |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 41 | SOCKS5ClientSocket(ClientSocketHandle* transport_socket, |
| 42 | const HostResolver::RequestInfo& req_info); |
| 43 | |
| 44 | // Deprecated constructor (http://crbug.com/37810) that takes a ClientSocket. |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 45 | SOCKS5ClientSocket(ClientSocket* transport_socket, |
[email protected] | 20cbe23d | 2009-12-18 03:39:21 | [diff] [blame] | 46 | const HostResolver::RequestInfo& req_info); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 47 | |
| 48 | // On destruction Disconnect() is called. |
| 49 | virtual ~SOCKS5ClientSocket(); |
| 50 | |
| 51 | // ClientSocket methods: |
| 52 | |
| 53 | // Does the SOCKS handshake and completes the protocol. |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 54 | virtual int Connect(CompletionCallback* callback); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 55 | virtual void Disconnect(); |
| 56 | virtual bool IsConnected() const; |
| 57 | virtual bool IsConnectedAndIdle() const; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 58 | virtual const BoundNetLog& NetLog() const { return net_log_; } |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 59 | virtual void SetSubresourceSpeculation(); |
| 60 | virtual void SetOmniboxSpeculation(); |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame^] | 61 | virtual bool WasEverUsed() const; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 62 | |
| 63 | // Socket methods: |
| 64 | virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 65 | virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 66 | |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 67 | virtual bool SetReceiveBufferSize(int32 size); |
| 68 | virtual bool SetSendBufferSize(int32 size); |
| 69 | |
[email protected] | ac9eec6 | 2010-02-20 18:50:38 | [diff] [blame] | 70 | virtual int GetPeerAddress(AddressList* address) const; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 71 | |
| 72 | private: |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 73 | enum State { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 74 | STATE_GREET_WRITE, |
| 75 | STATE_GREET_WRITE_COMPLETE, |
| 76 | STATE_GREET_READ, |
| 77 | STATE_GREET_READ_COMPLETE, |
| 78 | STATE_HANDSHAKE_WRITE, |
| 79 | STATE_HANDSHAKE_WRITE_COMPLETE, |
| 80 | STATE_HANDSHAKE_READ, |
| 81 | STATE_HANDSHAKE_READ_COMPLETE, |
| 82 | STATE_NONE, |
| 83 | }; |
| 84 | |
[email protected] | 20cbe23d | 2009-12-18 03:39:21 | [diff] [blame] | 85 | // Addressing type that can be specified in requests or responses. |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 86 | enum SocksEndPointAddressType { |
[email protected] | 20cbe23d | 2009-12-18 03:39:21 | [diff] [blame] | 87 | kEndPointDomain = 0x03, |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 88 | kEndPointResolvedIPv4 = 0x01, |
| 89 | kEndPointResolvedIPv6 = 0x04, |
| 90 | }; |
| 91 | |
| 92 | static const unsigned int kGreetReadHeaderSize; |
| 93 | static const unsigned int kWriteHeaderSize; |
| 94 | static const unsigned int kReadHeaderSize; |
| 95 | static const uint8 kSOCKS5Version; |
| 96 | static const uint8 kTunnelCommand; |
| 97 | static const uint8 kNullByte; |
| 98 | |
| 99 | void DoCallback(int result); |
| 100 | void OnIOComplete(int result); |
| 101 | |
| 102 | int DoLoop(int last_io_result); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 103 | int DoHandshakeRead(); |
| 104 | int DoHandshakeReadComplete(int result); |
| 105 | int DoHandshakeWrite(); |
| 106 | int DoHandshakeWriteComplete(int result); |
| 107 | int DoGreetRead(); |
| 108 | int DoGreetReadComplete(int result); |
| 109 | int DoGreetWrite(); |
| 110 | int DoGreetWriteComplete(int result); |
| 111 | |
| 112 | // Writes the SOCKS handshake buffer into |handshake| |
| 113 | // and return OK on success. |
| 114 | int BuildHandshakeWriteBuffer(std::string* handshake) const; |
| 115 | |
| 116 | CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_; |
| 117 | |
| 118 | // Stores the underlying socket. |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 119 | scoped_ptr<ClientSocketHandle> transport_; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 120 | |
| 121 | State next_state_; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 122 | |
| 123 | // Stores the callback to the layer above, called on completing Connect(). |
| 124 | CompletionCallback* user_callback_; |
| 125 | |
| 126 | // This IOBuffer is used by the class to read and write |
| 127 | // SOCKS handshake data. The length contains the expected size to |
| 128 | // read or write. |
| 129 | scoped_refptr<IOBuffer> handshake_buf_; |
| 130 | |
| 131 | // While writing, this buffer stores the complete write handshake data. |
| 132 | // While reading, it stores the handshake information received so far. |
| 133 | std::string buffer_; |
| 134 | |
| 135 | // This becomes true when the SOCKS handshake has completed and the |
| 136 | // overlying connection is free to communicate. |
| 137 | bool completed_handshake_; |
| 138 | |
| 139 | // These contain the bytes sent / received by the SOCKS handshake. |
| 140 | size_t bytes_sent_; |
| 141 | size_t bytes_received_; |
| 142 | |
| 143 | size_t read_header_size; |
| 144 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 145 | HostResolver::RequestInfo host_request_info_; |
| 146 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 147 | BoundNetLog net_log_; |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 148 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 149 | DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket); |
| 150 | }; |
| 151 | |
| 152 | } // namespace net |
| 153 | |
| 154 | #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |