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 13 matching lines...) Expand all Loading... |
24 | 24 |
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 // |
| 35 // Although SOCKS 5 supports 3 different modes of addressing, we will |
| 36 // always pass it a hostname. This means the DNS resolving is done |
| 37 // proxy side. |
34 SOCKS5ClientSocket(ClientSocket* transport_socket, | 38 SOCKS5ClientSocket(ClientSocket* transport_socket, |
35 const HostResolver::RequestInfo& req_info, | 39 const HostResolver::RequestInfo& req_info); |
36 HostResolver* host_resolver); | |
37 | 40 |
38 // On destruction Disconnect() is called. | 41 // On destruction Disconnect() is called. |
39 virtual ~SOCKS5ClientSocket(); | 42 virtual ~SOCKS5ClientSocket(); |
40 | 43 |
41 // ClientSocket methods: | 44 // ClientSocket methods: |
42 | 45 |
43 // Does the SOCKS handshake and completes the protocol. | 46 // Does the SOCKS handshake and completes the protocol. |
44 virtual int Connect(CompletionCallback* callback, LoadLog* load_log); | 47 virtual int Connect(CompletionCallback* callback, LoadLog* load_log); |
45 virtual void Disconnect(); | 48 virtual void Disconnect(); |
46 virtual bool IsConnected() const; | 49 virtual bool IsConnected() const; |
47 virtual bool IsConnectedAndIdle() const; | 50 virtual bool IsConnectedAndIdle() const; |
48 | 51 |
49 // Socket methods: | 52 // Socket methods: |
50 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); | 53 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
51 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); | 54 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
52 | 55 |
53 virtual bool SetReceiveBufferSize(int32 size); | 56 virtual bool SetReceiveBufferSize(int32 size); |
54 virtual bool SetSendBufferSize(int32 size); | 57 virtual bool SetSendBufferSize(int32 size); |
55 | 58 |
56 #if defined(OS_LINUX) | 59 #if defined(OS_LINUX) |
57 virtual int GetPeerName(struct sockaddr* name, socklen_t* namelen); | 60 virtual int GetPeerName(struct sockaddr* name, socklen_t* namelen); |
58 #endif | 61 #endif |
59 | 62 |
60 private: | 63 private: |
61 FRIEND_TEST(SOCKS5ClientSocketTest, IPv6Domain); | |
62 FRIEND_TEST(SOCKS5ClientSocketTest, FailedDNS); | |
63 FRIEND_TEST(SOCKS5ClientSocketTest, CompleteHandshake); | |
64 | |
65 enum State { | 64 enum State { |
66 STATE_RESOLVE_HOST, | |
67 STATE_RESOLVE_HOST_COMPLETE, | |
68 STATE_GREET_WRITE, | 65 STATE_GREET_WRITE, |
69 STATE_GREET_WRITE_COMPLETE, | 66 STATE_GREET_WRITE_COMPLETE, |
70 STATE_GREET_READ, | 67 STATE_GREET_READ, |
71 STATE_GREET_READ_COMPLETE, | 68 STATE_GREET_READ_COMPLETE, |
72 STATE_HANDSHAKE_WRITE, | 69 STATE_HANDSHAKE_WRITE, |
73 STATE_HANDSHAKE_WRITE_COMPLETE, | 70 STATE_HANDSHAKE_WRITE_COMPLETE, |
74 STATE_HANDSHAKE_READ, | 71 STATE_HANDSHAKE_READ, |
75 STATE_HANDSHAKE_READ_COMPLETE, | 72 STATE_HANDSHAKE_READ_COMPLETE, |
76 STATE_NONE, | 73 STATE_NONE, |
77 }; | 74 }; |
78 | 75 |
79 // State of the SOCKSv5 handshake. Before host resolution all connections | 76 // Addressing type that can be specified in requests or responses. |
80 // are kEndPointFailedDomain. If DNS lookup fails, we move to | |
81 // kEndPointFailedDomain, otherwise the IPv4/IPv6 address as resolved. | |
82 enum SocksEndPointAddressType { | 77 enum SocksEndPointAddressType { |
83 kEndPointUnresolved, | 78 kEndPointDomain = 0x03, |
84 kEndPointFailedDomain = 0x03, | |
85 kEndPointResolvedIPv4 = 0x01, | 79 kEndPointResolvedIPv4 = 0x01, |
86 kEndPointResolvedIPv6 = 0x04, | 80 kEndPointResolvedIPv6 = 0x04, |
87 }; | 81 }; |
88 | 82 |
89 static const unsigned int kGreetReadHeaderSize; | 83 static const unsigned int kGreetReadHeaderSize; |
90 static const unsigned int kWriteHeaderSize; | 84 static const unsigned int kWriteHeaderSize; |
91 static const unsigned int kReadHeaderSize; | 85 static const unsigned int kReadHeaderSize; |
92 static const uint8 kSOCKS5Version; | 86 static const uint8 kSOCKS5Version; |
93 static const uint8 kTunnelCommand; | 87 static const uint8 kTunnelCommand; |
94 static const uint8 kNullByte; | 88 static const uint8 kNullByte; |
95 | 89 |
96 void DoCallback(int result); | 90 void DoCallback(int result); |
97 void OnIOComplete(int result); | 91 void OnIOComplete(int result); |
98 | 92 |
99 int DoLoop(int last_io_result); | 93 int DoLoop(int last_io_result); |
100 int DoResolveHost(); | |
101 int DoResolveHostComplete(int result); | |
102 int DoHandshakeRead(); | 94 int DoHandshakeRead(); |
103 int DoHandshakeReadComplete(int result); | 95 int DoHandshakeReadComplete(int result); |
104 int DoHandshakeWrite(); | 96 int DoHandshakeWrite(); |
105 int DoHandshakeWriteComplete(int result); | 97 int DoHandshakeWriteComplete(int result); |
106 int DoGreetRead(); | 98 int DoGreetRead(); |
107 int DoGreetReadComplete(int result); | 99 int DoGreetReadComplete(int result); |
108 int DoGreetWrite(); | 100 int DoGreetWrite(); |
109 int DoGreetWriteComplete(int result); | 101 int DoGreetWriteComplete(int result); |
110 | 102 |
111 // Writes the SOCKS handshake buffer into |handshake| | 103 // Writes the SOCKS handshake buffer into |handshake| |
112 // and return OK on success. | 104 // and return OK on success. |
113 int BuildHandshakeWriteBuffer(std::string* handshake) const; | 105 int BuildHandshakeWriteBuffer(std::string* handshake) const; |
114 | 106 |
115 CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_; | 107 CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_; |
116 | 108 |
117 // Stores the underlying socket. | 109 // Stores the underlying socket. |
118 scoped_ptr<ClientSocket> transport_; | 110 scoped_ptr<ClientSocket> transport_; |
119 | 111 |
120 State next_state_; | 112 State next_state_; |
121 SocksEndPointAddressType address_type_; | |
122 | 113 |
123 // Stores the callback to the layer above, called on completing Connect(). | 114 // Stores the callback to the layer above, called on completing Connect(). |
124 CompletionCallback* user_callback_; | 115 CompletionCallback* user_callback_; |
125 | 116 |
126 // This IOBuffer is used by the class to read and write | 117 // This IOBuffer is used by the class to read and write |
127 // SOCKS handshake data. The length contains the expected size to | 118 // SOCKS handshake data. The length contains the expected size to |
128 // read or write. | 119 // read or write. |
129 scoped_refptr<IOBuffer> handshake_buf_; | 120 scoped_refptr<IOBuffer> handshake_buf_; |
130 | 121 |
131 // While writing, this buffer stores the complete write handshake data. | 122 // While writing, this buffer stores the complete write handshake data. |
132 // While reading, it stores the handshake information received so far. | 123 // While reading, it stores the handshake information received so far. |
133 std::string buffer_; | 124 std::string buffer_; |
134 | 125 |
135 // This becomes true when the SOCKS handshake has completed and the | 126 // This becomes true when the SOCKS handshake has completed and the |
136 // overlying connection is free to communicate. | 127 // overlying connection is free to communicate. |
137 bool completed_handshake_; | 128 bool completed_handshake_; |
138 | 129 |
139 // These contain the bytes sent / received by the SOCKS handshake. | 130 // These contain the bytes sent / received by the SOCKS handshake. |
140 size_t bytes_sent_; | 131 size_t bytes_sent_; |
141 size_t bytes_received_; | 132 size_t bytes_received_; |
142 | 133 |
143 size_t read_header_size; | 134 size_t read_header_size; |
144 | 135 |
145 // Used to resolve the hostname to which the SOCKS proxy will connect. | |
146 SingleRequestHostResolver host_resolver_; | |
147 AddressList addresses_; | |
148 HostResolver::RequestInfo host_request_info_; | 136 HostResolver::RequestInfo host_request_info_; |
149 | 137 |
150 scoped_refptr<LoadLog> load_log_; | 138 scoped_refptr<LoadLog> load_log_; |
151 | 139 |
152 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket); | 140 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket); |
153 }; | 141 }; |
154 | 142 |
155 } // namespace net | 143 } // namespace net |
156 | 144 |
157 #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ | 145 #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_ |
OLD | NEW |