[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 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] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 20 | #include "net/socket/stream_socket.h" |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 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] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 28 | // This StreamSocket is used to setup a SOCKSv5 handshake with a socks proxy. |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 29 | // Currently no SOCKSv5 authentication is supported. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame^] | 30 | class NET_EXPORT_PRIVATE SOCKS5ClientSocket : public StreamSocket { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 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 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 44 | // Deprecated constructor (http://crbug.com/37810) that takes a StreamSocket. |
| 45 | SOCKS5ClientSocket(StreamSocket* 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 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 51 | // StreamSocket methods: |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 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] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 58 | virtual const BoundNetLog& NetLog() const; |
[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] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame] | 62 | virtual bool UsingTCPFastOpen() const; |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame] | 63 | virtual int64 NumBytesRead() const; |
| 64 | virtual base::TimeDelta GetConnectTimeMicros() const; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 65 | |
| 66 | // Socket methods: |
| 67 | virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 68 | virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 69 | |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 70 | virtual bool SetReceiveBufferSize(int32 size); |
| 71 | virtual bool SetSendBufferSize(int32 size); |
| 72 | |
[email protected] | ac9eec6 | 2010-02-20 18:50:38 | [diff] [blame] | 73 | virtual int GetPeerAddress(AddressList* address) const; |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 74 | virtual int GetLocalAddress(IPEndPoint* address) const; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 75 | |
| 76 | private: |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 77 | enum State { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 78 | STATE_GREET_WRITE, |
| 79 | STATE_GREET_WRITE_COMPLETE, |
| 80 | STATE_GREET_READ, |
| 81 | STATE_GREET_READ_COMPLETE, |
| 82 | STATE_HANDSHAKE_WRITE, |
| 83 | STATE_HANDSHAKE_WRITE_COMPLETE, |
| 84 | STATE_HANDSHAKE_READ, |
| 85 | STATE_HANDSHAKE_READ_COMPLETE, |
| 86 | STATE_NONE, |
| 87 | }; |
| 88 | |
[email protected] | 20cbe23d | 2009-12-18 03:39:21 | [diff] [blame] | 89 | // Addressing type that can be specified in requests or responses. |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 90 | enum SocksEndPointAddressType { |
[email protected] | 20cbe23d | 2009-12-18 03:39:21 | [diff] [blame] | 91 | kEndPointDomain = 0x03, |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 92 | kEndPointResolvedIPv4 = 0x01, |
| 93 | kEndPointResolvedIPv6 = 0x04, |
| 94 | }; |
| 95 | |
| 96 | static const unsigned int kGreetReadHeaderSize; |
| 97 | static const unsigned int kWriteHeaderSize; |
| 98 | static const unsigned int kReadHeaderSize; |
| 99 | static const uint8 kSOCKS5Version; |
| 100 | static const uint8 kTunnelCommand; |
| 101 | static const uint8 kNullByte; |
| 102 | |
| 103 | void DoCallback(int result); |
| 104 | void OnIOComplete(int result); |
| 105 | |
| 106 | int DoLoop(int last_io_result); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 107 | int DoHandshakeRead(); |
| 108 | int DoHandshakeReadComplete(int result); |
| 109 | int DoHandshakeWrite(); |
| 110 | int DoHandshakeWriteComplete(int result); |
| 111 | int DoGreetRead(); |
| 112 | int DoGreetReadComplete(int result); |
| 113 | int DoGreetWrite(); |
| 114 | int DoGreetWriteComplete(int result); |
| 115 | |
| 116 | // Writes the SOCKS handshake buffer into |handshake| |
| 117 | // and return OK on success. |
| 118 | int BuildHandshakeWriteBuffer(std::string* handshake) const; |
| 119 | |
| 120 | CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_; |
| 121 | |
| 122 | // Stores the underlying socket. |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 123 | scoped_ptr<ClientSocketHandle> transport_; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 124 | |
| 125 | State next_state_; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 126 | |
| 127 | // Stores the callback to the layer above, called on completing Connect(). |
| 128 | CompletionCallback* user_callback_; |
| 129 | |
| 130 | // This IOBuffer is used by the class to read and write |
| 131 | // SOCKS handshake data. The length contains the expected size to |
| 132 | // read or write. |
| 133 | scoped_refptr<IOBuffer> handshake_buf_; |
| 134 | |
| 135 | // While writing, this buffer stores the complete write handshake data. |
| 136 | // While reading, it stores the handshake information received so far. |
| 137 | std::string buffer_; |
| 138 | |
| 139 | // This becomes true when the SOCKS handshake has completed and the |
| 140 | // overlying connection is free to communicate. |
| 141 | bool completed_handshake_; |
| 142 | |
| 143 | // These contain the bytes sent / received by the SOCKS handshake. |
| 144 | size_t bytes_sent_; |
| 145 | size_t bytes_received_; |
| 146 | |
| 147 | size_t read_header_size; |
| 148 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 149 | HostResolver::RequestInfo host_request_info_; |
| 150 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 151 | BoundNetLog net_log_; |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 152 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket); |
| 154 | }; |
| 155 | |
| 156 | } // namespace net |
| 157 | |
| 158 | #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |