OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "net/socket/socks_client_socket.h" | 5 #include "net/socket/socks_client_socket.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
13 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
14 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
15 #include "net/socket/client_socket_handle.h" | 15 #include "net/socket/client_socket_handle.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 // Every SOCKS server requests a user-id from the client. It is optional | 19 // Every SOCKS server requests a user-id from the client. It is optional |
20 // and we send an empty string. | 20 // and we send an empty string. |
21 static const char kEmptyUserId[] = ""; | 21 static const char kEmptyUserId[] = ""; |
22 | 22 |
23 // For SOCKS4, the client sends 8 bytes plus the size of the user-id. | 23 // For SOCKS4, the client sends 8 bytes plus the size of the user-id. |
24 static const unsigned int kWriteHeaderSize = 8; | 24 static const unsigned int kWriteHeaderSize = 8; |
25 | 25 |
26 // For SOCKS4 the server sends 8 bytes for acknowledgement. | 26 // For SOCKS4 the server sends 8 bytes for acknowledgement. |
27 static const unsigned int kReadHeaderSize = 8; | 27 static const unsigned int kReadHeaderSize = 8; |
28 | 28 |
29 // Server Response codes for SOCKS. | 29 // Server Response codes for SOCKS. |
30 static const uint8 kServerResponseOk = 0x5A; | 30 static const uint8 kServerResponseOk = 0x5A; |
31 static const uint8 kServerResponseRejected = 0x5B; | 31 static const uint8 kServerResponseRejected = 0x5B; |
32 static const uint8 kServerResponseNotReachable = 0x5C; | 32 static const uint8 kServerResponseNotReachable = 0x5C; |
33 static const uint8 kServerResponseMismatchedUserId = 0x5D; | 33 static const uint8 kServerResponseMismatchedUserId = 0x5D; |
34 | 34 |
35 static const uint8 kSOCKSVersion4 = 0x04; | 35 static const uint8 kSOCKSVersion4 = 0x04; |
36 static const uint8 kSOCKSStreamRequest = 0x01; | 36 static const uint8 kSOCKSStreamRequest = 0x01; |
37 | 37 |
38 // A struct holding the essential details of the SOCKS4 Server Request. | 38 // A struct holding the essential details of the SOCKS4 Server Request. |
39 // The port in the header is stored in network byte order. | 39 // The port in the header is stored in network byte order. |
40 struct SOCKS4ServerRequest { | 40 struct SOCKS4ServerRequest { |
(...skipping 22 matching lines...) Expand all Loading... |
63 HostResolver* host_resolver) | 63 HostResolver* host_resolver) |
64 : transport_(transport_socket.Pass()), | 64 : transport_(transport_socket.Pass()), |
65 next_state_(STATE_NONE), | 65 next_state_(STATE_NONE), |
66 completed_handshake_(false), | 66 completed_handshake_(false), |
67 bytes_sent_(0), | 67 bytes_sent_(0), |
68 bytes_received_(0), | 68 bytes_received_(0), |
69 was_ever_used_(false), | 69 was_ever_used_(false), |
70 host_resolver_(host_resolver), | 70 host_resolver_(host_resolver), |
71 host_request_info_(req_info), | 71 host_request_info_(req_info), |
72 priority_(priority), | 72 priority_(priority), |
73 net_log_(transport_->socket()->NetLog()) {} | 73 net_log_(transport_->socket()->NetLog()) { |
| 74 } |
74 | 75 |
75 SOCKSClientSocket::~SOCKSClientSocket() { | 76 SOCKSClientSocket::~SOCKSClientSocket() { |
76 Disconnect(); | 77 Disconnect(); |
77 } | 78 } |
78 | 79 |
79 int SOCKSClientSocket::Connect(const CompletionCallback& callback) { | 80 int SOCKSClientSocket::Connect(const CompletionCallback& callback) { |
80 DCHECK(transport_.get()); | 81 DCHECK(transport_.get()); |
81 DCHECK(transport_->socket()); | 82 DCHECK(transport_->socket()); |
82 DCHECK_EQ(STATE_NONE, next_state_); | 83 DCHECK_EQ(STATE_NONE, next_state_); |
83 DCHECK(user_callback_.is_null()); | 84 DCHECK(user_callback_.is_null()); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 NOTREACHED(); | 166 NOTREACHED(); |
166 return kProtoUnknown; | 167 return kProtoUnknown; |
167 } | 168 } |
168 | 169 |
169 bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | 170 bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
170 if (transport_.get() && transport_->socket()) { | 171 if (transport_.get() && transport_->socket()) { |
171 return transport_->socket()->GetSSLInfo(ssl_info); | 172 return transport_->socket()->GetSSLInfo(ssl_info); |
172 } | 173 } |
173 NOTREACHED(); | 174 NOTREACHED(); |
174 return false; | 175 return false; |
175 | |
176 } | 176 } |
177 | 177 |
178 // Read is called by the transport layer above to read. This can only be done | 178 // Read is called by the transport layer above to read. This can only be done |
179 // if the SOCKS handshake is complete. | 179 // if the SOCKS handshake is complete. |
180 int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, | 180 int SOCKSClientSocket::Read(IOBuffer* buf, |
| 181 int buf_len, |
181 const CompletionCallback& callback) { | 182 const CompletionCallback& callback) { |
182 DCHECK(completed_handshake_); | 183 DCHECK(completed_handshake_); |
183 DCHECK_EQ(STATE_NONE, next_state_); | 184 DCHECK_EQ(STATE_NONE, next_state_); |
184 DCHECK(user_callback_.is_null()); | 185 DCHECK(user_callback_.is_null()); |
185 DCHECK(!callback.is_null()); | 186 DCHECK(!callback.is_null()); |
186 | 187 |
187 int rv = transport_->socket()->Read( | 188 int rv = transport_->socket()->Read( |
188 buf, buf_len, | 189 buf, |
| 190 buf_len, |
189 base::Bind(&SOCKSClientSocket::OnReadWriteComplete, | 191 base::Bind(&SOCKSClientSocket::OnReadWriteComplete, |
190 base::Unretained(this), callback)); | 192 base::Unretained(this), |
| 193 callback)); |
191 if (rv > 0) | 194 if (rv > 0) |
192 was_ever_used_ = true; | 195 was_ever_used_ = true; |
193 return rv; | 196 return rv; |
194 } | 197 } |
195 | 198 |
196 // Write is called by the transport layer. This can only be done if the | 199 // Write is called by the transport layer. This can only be done if the |
197 // SOCKS handshake is complete. | 200 // SOCKS handshake is complete. |
198 int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len, | 201 int SOCKSClientSocket::Write(IOBuffer* buf, |
| 202 int buf_len, |
199 const CompletionCallback& callback) { | 203 const CompletionCallback& callback) { |
200 DCHECK(completed_handshake_); | 204 DCHECK(completed_handshake_); |
201 DCHECK_EQ(STATE_NONE, next_state_); | 205 DCHECK_EQ(STATE_NONE, next_state_); |
202 DCHECK(user_callback_.is_null()); | 206 DCHECK(user_callback_.is_null()); |
203 DCHECK(!callback.is_null()); | 207 DCHECK(!callback.is_null()); |
204 | 208 |
205 int rv = transport_->socket()->Write( | 209 int rv = transport_->socket()->Write( |
206 buf, buf_len, | 210 buf, |
| 211 buf_len, |
207 base::Bind(&SOCKSClientSocket::OnReadWriteComplete, | 212 base::Bind(&SOCKSClientSocket::OnReadWriteComplete, |
208 base::Unretained(this), callback)); | 213 base::Unretained(this), |
| 214 callback)); |
209 if (rv > 0) | 215 if (rv > 0) |
210 was_ever_used_ = true; | 216 was_ever_used_ = true; |
211 return rv; | 217 return rv; |
212 } | 218 } |
213 | 219 |
214 int SOCKSClientSocket::SetReceiveBufferSize(int32 size) { | 220 int SOCKSClientSocket::SetReceiveBufferSize(int32 size) { |
215 return transport_->socket()->SetReceiveBufferSize(size); | 221 return transport_->socket()->SetReceiveBufferSize(size); |
216 } | 222 } |
217 | 223 |
218 int SOCKSClientSocket::SetSendBufferSize(int32 size) { | 224 int SOCKSClientSocket::SetSendBufferSize(int32 size) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; | 349 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; |
344 | 350 |
345 if (buffer_.empty()) { | 351 if (buffer_.empty()) { |
346 buffer_ = BuildHandshakeWriteBuffer(); | 352 buffer_ = BuildHandshakeWriteBuffer(); |
347 bytes_sent_ = 0; | 353 bytes_sent_ = 0; |
348 } | 354 } |
349 | 355 |
350 int handshake_buf_len = buffer_.size() - bytes_sent_; | 356 int handshake_buf_len = buffer_.size() - bytes_sent_; |
351 DCHECK_GT(handshake_buf_len, 0); | 357 DCHECK_GT(handshake_buf_len, 0); |
352 handshake_buf_ = new IOBuffer(handshake_buf_len); | 358 handshake_buf_ = new IOBuffer(handshake_buf_len); |
353 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], | 359 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], handshake_buf_len); |
354 handshake_buf_len); | |
355 return transport_->socket()->Write( | 360 return transport_->socket()->Write( |
356 handshake_buf_.get(), | 361 handshake_buf_.get(), |
357 handshake_buf_len, | 362 handshake_buf_len, |
358 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this))); | 363 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this))); |
359 } | 364 } |
360 | 365 |
361 int SOCKSClientSocket::DoHandshakeWriteComplete(int result) { | 366 int SOCKSClientSocket::DoHandshakeWriteComplete(int result) { |
362 if (result < 0) | 367 if (result < 0) |
363 return result; | 368 return result; |
364 | 369 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 | 451 |
447 int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const { | 452 int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const { |
448 return transport_->socket()->GetPeerAddress(address); | 453 return transport_->socket()->GetPeerAddress(address); |
449 } | 454 } |
450 | 455 |
451 int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const { | 456 int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const { |
452 return transport_->socket()->GetLocalAddress(address); | 457 return transport_->socket()->GetLocalAddress(address); |
453 } | 458 } |
454 | 459 |
455 } // namespace net | 460 } // namespace net |
OLD | NEW |