blob: 3e5d6eb0aa41f84b6acf71c5828e38c9bbebcf5f [file] [log] [blame]
[email protected]9eb7b11b2012-03-28 20:19:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]3cd17242009-06-23 02:59:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/socket/socks_client_socket.h"
6
dchengc7eeda422015-12-26 03:56:487#include <utility>
8
[email protected]aa22b242011-11-16 18:58:299#include "base/bind.h"
[email protected]0dc88b32014-03-26 20:12:2810#include "base/callback_helpers.h"
[email protected]3cd17242009-06-23 02:59:0211#include "base/compiler_specific.h"
[email protected]9eb7b11b2012-03-28 20:19:3112#include "base/sys_byteorder.h"
[email protected]3cd17242009-06-23 02:59:0213#include "net/base/io_buffer.h"
eroman87c53d62015-04-02 06:51:0714#include "net/log/net_log.h"
mikecirone8b85c432016-09-08 19:11:0015#include "net/log/net_log_event_type.h"
[email protected]a796bcec2010-03-22 17:17:2616#include "net/socket/client_socket_handle.h"
[email protected]a2b2cfc2017-12-06 09:06:0817#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]3cd17242009-06-23 02:59:0218
19namespace net {
20
21// Every SOCKS server requests a user-id from the client. It is optional
22// and we send an empty string.
23static const char kEmptyUserId[] = "";
24
[email protected]3cd17242009-06-23 02:59:0225// For SOCKS4, the client sends 8 bytes plus the size of the user-id.
[email protected]76a51ac82009-06-28 07:58:5826static const unsigned int kWriteHeaderSize = 8;
[email protected]3cd17242009-06-23 02:59:0227
[email protected]034d3892011-03-29 04:07:2128// For SOCKS4 the server sends 8 bytes for acknowledgement.
[email protected]76a51ac82009-06-28 07:58:5829static const unsigned int kReadHeaderSize = 8;
[email protected]3cd17242009-06-23 02:59:0230
31// Server Response codes for SOCKS.
Avi Drissman13fc8932015-12-20 04:40:4632static const uint8_t kServerResponseOk = 0x5A;
33static const uint8_t kServerResponseRejected = 0x5B;
34static const uint8_t kServerResponseNotReachable = 0x5C;
35static const uint8_t kServerResponseMismatchedUserId = 0x5D;
[email protected]3cd17242009-06-23 02:59:0236
Avi Drissman13fc8932015-12-20 04:40:4637static const uint8_t kSOCKSVersion4 = 0x04;
38static const uint8_t kSOCKSStreamRequest = 0x01;
[email protected]3cd17242009-06-23 02:59:0239
[email protected]034d3892011-03-29 04:07:2140// A struct holding the essential details of the SOCKS4 Server Request.
[email protected]3cd17242009-06-23 02:59:0241// The port in the header is stored in network byte order.
42struct SOCKS4ServerRequest {
Avi Drissman13fc8932015-12-20 04:40:4643 uint8_t version;
44 uint8_t command;
45 uint16_t nw_port;
46 uint8_t ip[4];
[email protected]3cd17242009-06-23 02:59:0247};
mostynb91e0da982015-01-20 19:17:2748static_assert(sizeof(SOCKS4ServerRequest) == kWriteHeaderSize,
49 "socks4 server request struct has incorrect size");
[email protected]3cd17242009-06-23 02:59:0250
[email protected]034d3892011-03-29 04:07:2151// A struct holding details of the SOCKS4 Server Response.
[email protected]3cd17242009-06-23 02:59:0252struct SOCKS4ServerResponse {
Avi Drissman13fc8932015-12-20 04:40:4653 uint8_t reserved_null;
54 uint8_t code;
55 uint16_t port;
56 uint8_t ip[4];
[email protected]3cd17242009-06-23 02:59:0257};
mostynb91e0da982015-01-20 19:17:2758static_assert(sizeof(SOCKS4ServerResponse) == kReadHeaderSize,
59 "socks4 server response struct has incorrect size");
[email protected]3cd17242009-06-23 02:59:0260
[email protected]18ccfdb2013-08-15 00:13:4461SOCKSClientSocket::SOCKSClientSocket(
danakj655b66c2016-04-16 00:51:3862 std::unique_ptr<ClientSocketHandle> transport_socket,
[email protected]18ccfdb2013-08-15 00:13:4463 const HostResolver::RequestInfo& req_info,
[email protected]5109c1952013-08-20 18:44:1064 RequestPriority priority,
[email protected]27fb73c2018-01-11 13:27:2465 HostResolver* host_resolver,
66 const NetworkTrafficAnnotationTag& traffic_annotation)
dchengc7eeda422015-12-26 03:56:4867 : transport_(std::move(transport_socket)),
[email protected]3cd17242009-06-23 02:59:0268 next_state_(STATE_NONE),
[email protected]3cd17242009-06-23 02:59:0269 completed_handshake_(false),
70 bytes_sent_(0),
71 bytes_received_(0),
[email protected]0dc88b32014-03-26 20:12:2872 was_ever_used_(false),
[email protected]76a51ac82009-06-28 07:58:5873 host_resolver_(host_resolver),
[email protected]a2006ece2010-04-23 16:44:0274 host_request_info_(req_info),
[email protected]5109c1952013-08-20 18:44:1075 priority_(priority),
[email protected]27fb73c2018-01-11 13:27:2476 net_log_(transport_->socket()->NetLog()),
77 traffic_annotation_(traffic_annotation) {}
[email protected]3cd17242009-06-23 02:59:0278
79SOCKSClientSocket::~SOCKSClientSocket() {
80 Disconnect();
81}
82
[email protected]83039bb2011-12-09 18:43:5583int SOCKSClientSocket::Connect(const CompletionCallback& callback) {
[email protected]3cd17242009-06-23 02:59:0284 DCHECK(transport_.get());
[email protected]a796bcec2010-03-22 17:17:2685 DCHECK(transport_->socket());
[email protected]3cd17242009-06-23 02:59:0286 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:5587 DCHECK(user_callback_.is_null());
[email protected]3cd17242009-06-23 02:59:0288
89 // If already connected, then just return OK.
90 if (completed_handshake_)
91 return OK;
92
93 next_state_ = STATE_RESOLVE_HOST;
[email protected]5a05c47a2009-11-02 23:25:1994
mikecirone8b85c432016-09-08 19:11:0095 net_log_.BeginEvent(NetLogEventType::SOCKS_CONNECT);
[email protected]3cd17242009-06-23 02:59:0296
97 int rv = DoLoop(OK);
[email protected]5a05c47a2009-11-02 23:25:1998 if (rv == ERR_IO_PENDING) {
[email protected]83039bb2011-12-09 18:43:5599 user_callback_ = callback;
[email protected]5a05c47a2009-11-02 23:25:19100 } else {
mikecirone8b85c432016-09-08 19:11:00101 net_log_.EndEventWithNetErrorCode(NetLogEventType::SOCKS_CONNECT, rv);
[email protected]5a05c47a2009-11-02 23:25:19102 }
[email protected]3cd17242009-06-23 02:59:02103 return rv;
104}
105
106void SOCKSClientSocket::Disconnect() {
107 completed_handshake_ = false;
maksim.sisov320d5962016-08-15 18:02:51108 request_.reset();
[email protected]a796bcec2010-03-22 17:17:26109 transport_->socket()->Disconnect();
[email protected]16a02742010-01-07 22:50:10110
111 // Reset other states to make sure they aren't mistakenly used later.
112 // These are the states initialized by Connect().
113 next_state_ = STATE_NONE;
[email protected]dbf036f2011-12-06 23:33:24114 user_callback_.Reset();
[email protected]3cd17242009-06-23 02:59:02115}
116
117bool SOCKSClientSocket::IsConnected() const {
[email protected]a796bcec2010-03-22 17:17:26118 return completed_handshake_ && transport_->socket()->IsConnected();
[email protected]3cd17242009-06-23 02:59:02119}
120
121bool SOCKSClientSocket::IsConnectedAndIdle() const {
[email protected]a796bcec2010-03-22 17:17:26122 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle();
[email protected]3cd17242009-06-23 02:59:02123}
124
tfarina428341112016-09-22 13:38:20125const NetLogWithSource& SOCKSClientSocket::NetLog() const {
[email protected]e4be2dd2010-12-14 00:44:39126 return net_log_;
127}
128
[email protected]9b5614a2010-08-25 20:29:45129void SOCKSClientSocket::SetSubresourceSpeculation() {
130 if (transport_.get() && transport_->socket()) {
131 transport_->socket()->SetSubresourceSpeculation();
132 } else {
133 NOTREACHED();
134 }
135}
136
137void SOCKSClientSocket::SetOmniboxSpeculation() {
138 if (transport_.get() && transport_->socket()) {
139 transport_->socket()->SetOmniboxSpeculation();
140 } else {
141 NOTREACHED();
142 }
143}
144
[email protected]0f873e82010-09-02 16:09:01145bool SOCKSClientSocket::WasEverUsed() const {
[email protected]0dc88b32014-03-26 20:12:28146 return was_ever_used_;
[email protected]0f873e82010-09-02 16:09:01147}
148
tfarina2846404c2016-12-25 14:31:37149bool SOCKSClientSocket::WasAlpnNegotiated() const {
[email protected]2d88e7d2012-07-19 17:55:17150 if (transport_.get() && transport_->socket()) {
tfarina2846404c2016-12-25 14:31:37151 return transport_->socket()->WasAlpnNegotiated();
[email protected]2d88e7d2012-07-19 17:55:17152 }
153 NOTREACHED();
154 return false;
155}
156
[email protected]33661e482012-04-03 16:16:26157NextProto SOCKSClientSocket::GetNegotiatedProtocol() const {
158 if (transport_.get() && transport_->socket()) {
159 return transport_->socket()->GetNegotiatedProtocol();
160 }
161 NOTREACHED();
162 return kProtoUnknown;
163}
164
[email protected]2d88e7d2012-07-19 17:55:17165bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
166 if (transport_.get() && transport_->socket()) {
167 return transport_->socket()->GetSSLInfo(ssl_info);
168 }
169 NOTREACHED();
170 return false;
ttuttle23fdb7b2015-05-15 01:28:03171}
[email protected]2d88e7d2012-07-19 17:55:17172
ttuttle23fdb7b2015-05-15 01:28:03173void SOCKSClientSocket::GetConnectionAttempts(ConnectionAttempts* out) const {
174 out->clear();
[email protected]2d88e7d2012-07-19 17:55:17175}
176
tbansalf82cc8e2015-10-14 20:05:49177int64_t SOCKSClientSocket::GetTotalReceivedBytes() const {
178 return transport_->socket()->GetTotalReceivedBytes();
179}
180
Paul Jensen0f49dec2017-12-12 23:39:58181void SOCKSClientSocket::ApplySocketTag(const SocketTag& tag) {
182 return transport_->socket()->ApplySocketTag(tag);
183}
184
[email protected]3cd17242009-06-23 02:59:02185// Read is called by the transport layer above to read. This can only be done
186// if the SOCKS handshake is complete.
187int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len,
[email protected]3f55aa12011-12-07 02:03:33188 const CompletionCallback& callback) {
189 DCHECK(completed_handshake_);
190 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55191 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28192 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02193
[email protected]0dc88b32014-03-26 20:12:28194 int rv = transport_->socket()->Read(
195 buf, buf_len,
196 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
197 base::Unretained(this), callback));
198 if (rv > 0)
199 was_ever_used_ = true;
200 return rv;
[email protected]3cd17242009-06-23 02:59:02201}
202
203// Write is called by the transport layer. This can only be done if the
204// SOCKS handshake is complete.
[email protected]a2b2cfc2017-12-06 09:06:08205int SOCKSClientSocket::Write(
206 IOBuffer* buf,
207 int buf_len,
208 const CompletionCallback& callback,
209 const NetworkTrafficAnnotationTag& traffic_annotation) {
[email protected]3cd17242009-06-23 02:59:02210 DCHECK(completed_handshake_);
211 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55212 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28213 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02214
[email protected]0dc88b32014-03-26 20:12:28215 int rv = transport_->socket()->Write(
216 buf, buf_len,
217 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
[email protected]a2b2cfc2017-12-06 09:06:08218 base::Unretained(this), callback),
219 traffic_annotation);
[email protected]0dc88b32014-03-26 20:12:28220 if (rv > 0)
221 was_ever_used_ = true;
222 return rv;
[email protected]3cd17242009-06-23 02:59:02223}
224
Avi Drissman13fc8932015-12-20 04:40:46225int SOCKSClientSocket::SetReceiveBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26226 return transport_->socket()->SetReceiveBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04227}
228
Avi Drissman13fc8932015-12-20 04:40:46229int SOCKSClientSocket::SetSendBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26230 return transport_->socket()->SetSendBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04231}
232
[email protected]3cd17242009-06-23 02:59:02233void SOCKSClientSocket::DoCallback(int result) {
234 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]83039bb2011-12-09 18:43:55235 DCHECK(!user_callback_.is_null());
[email protected]3cd17242009-06-23 02:59:02236
237 // Since Run() may result in Read being called,
238 // clear user_callback_ up front.
[email protected]83039bb2011-12-09 18:43:55239 DVLOG(1) << "Finished setting up SOCKS handshake";
[email protected]0dc88b32014-03-26 20:12:28240 base::ResetAndReturn(&user_callback_).Run(result);
[email protected]3cd17242009-06-23 02:59:02241}
242
243void SOCKSClientSocket::OnIOComplete(int result) {
244 DCHECK_NE(STATE_NONE, next_state_);
245 int rv = DoLoop(result);
[email protected]5a05c47a2009-11-02 23:25:19246 if (rv != ERR_IO_PENDING) {
mikecirone8b85c432016-09-08 19:11:00247 net_log_.EndEventWithNetErrorCode(NetLogEventType::SOCKS_CONNECT, rv);
[email protected]3cd17242009-06-23 02:59:02248 DoCallback(rv);
[email protected]5a05c47a2009-11-02 23:25:19249 }
[email protected]3cd17242009-06-23 02:59:02250}
251
[email protected]0dc88b32014-03-26 20:12:28252void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback& callback,
253 int result) {
254 DCHECK_NE(ERR_IO_PENDING, result);
255 DCHECK(!callback.is_null());
256
257 if (result > 0)
258 was_ever_used_ = true;
259 callback.Run(result);
260}
261
[email protected]3cd17242009-06-23 02:59:02262int SOCKSClientSocket::DoLoop(int last_io_result) {
263 DCHECK_NE(next_state_, STATE_NONE);
264 int rv = last_io_result;
265 do {
266 State state = next_state_;
267 next_state_ = STATE_NONE;
268 switch (state) {
269 case STATE_RESOLVE_HOST:
270 DCHECK_EQ(OK, rv);
271 rv = DoResolveHost();
272 break;
273 case STATE_RESOLVE_HOST_COMPLETE:
274 rv = DoResolveHostComplete(rv);
275 break;
276 case STATE_HANDSHAKE_WRITE:
277 DCHECK_EQ(OK, rv);
278 rv = DoHandshakeWrite();
279 break;
280 case STATE_HANDSHAKE_WRITE_COMPLETE:
281 rv = DoHandshakeWriteComplete(rv);
282 break;
283 case STATE_HANDSHAKE_READ:
284 DCHECK_EQ(OK, rv);
285 rv = DoHandshakeRead();
286 break;
287 case STATE_HANDSHAKE_READ_COMPLETE:
288 rv = DoHandshakeReadComplete(rv);
289 break;
290 default:
291 NOTREACHED() << "bad state";
292 rv = ERR_UNEXPECTED;
293 break;
294 }
295 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
296 return rv;
297}
298
299int SOCKSClientSocket::DoResolveHost() {
[email protected]3cd17242009-06-23 02:59:02300 next_state_ = STATE_RESOLVE_HOST_COMPLETE;
[email protected]034d3892011-03-29 04:07:21301 // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4
302 // addresses for the target host.
303 host_request_info_.set_address_family(ADDRESS_FAMILY_IPV4);
maksim.sisov320d5962016-08-15 18:02:51304 return host_resolver_->Resolve(
305 host_request_info_, priority_, &addresses_,
[email protected]aa22b242011-11-16 18:58:29306 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)),
maksim.sisov320d5962016-08-15 18:02:51307 &request_, net_log_);
[email protected]3cd17242009-06-23 02:59:02308}
309
310int SOCKSClientSocket::DoResolveHostComplete(int result) {
[email protected]034d3892011-03-29 04:07:21311 if (result != OK) {
312 // Resolving the hostname failed; fail the request rather than automatically
313 // falling back to SOCKS4a (since it can be confusing to see invalid IP
314 // addresses being sent to the SOCKS4 server when it doesn't support 4A.)
315 return result;
[email protected]3cd17242009-06-23 02:59:02316 }
317
[email protected]034d3892011-03-29 04:07:21318 next_state_ = STATE_HANDSHAKE_WRITE;
[email protected]3cd17242009-06-23 02:59:02319 return OK;
320}
321
322// Builds the buffer that is to be sent to the server.
[email protected]76a51ac82009-06-28 07:58:58323const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const {
[email protected]76a51ac82009-06-28 07:58:58324 SOCKS4ServerRequest request;
325 request.version = kSOCKSVersion4;
326 request.command = kSOCKSStreamRequest;
[email protected]9eb7b11b2012-03-28 20:19:31327 request.nw_port = base::HostToNet16(host_request_info_.port());
[email protected]3cd17242009-06-23 02:59:02328
[email protected]7054e78f2012-05-07 21:44:56329 DCHECK(!addresses_.empty());
330 const IPEndPoint& endpoint = addresses_.front();
[email protected]3cd17242009-06-23 02:59:02331
[email protected]034d3892011-03-29 04:07:21332 // We disabled IPv6 results when resolving the hostname, so none of the
333 // results in the list will be IPv6.
334 // TODO(eroman): we only ever use the first address in the list. It would be
335 // more robust to try all the IP addresses we have before
336 // failing the connect attempt.
[email protected]e466aaf2012-12-13 01:46:44337 CHECK_EQ(ADDRESS_FAMILY_IPV4, endpoint.GetFamily());
[email protected]7054e78f2012-05-07 21:44:56338 CHECK_LE(endpoint.address().size(), sizeof(request.ip));
martijn98acb9b2016-01-27 07:14:07339 memcpy(&request.ip, &endpoint.address().bytes()[0],
340 endpoint.address().size());
[email protected]034d3892011-03-29 04:07:21341
[email protected]7054e78f2012-05-07 21:44:56342 DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort();
[email protected]3cd17242009-06-23 02:59:02343
[email protected]76a51ac82009-06-28 07:58:58344 std::string handshake_data(reinterpret_cast<char*>(&request),
345 sizeof(request));
346 handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId));
[email protected]3cd17242009-06-23 02:59:02347
[email protected]76a51ac82009-06-28 07:58:58348 return handshake_data;
[email protected]3cd17242009-06-23 02:59:02349}
350
351// Writes the SOCKS handshake data to the underlying socket connection.
352int SOCKSClientSocket::DoHandshakeWrite() {
353 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE;
354
[email protected]76a51ac82009-06-28 07:58:58355 if (buffer_.empty()) {
356 buffer_ = BuildHandshakeWriteBuffer();
[email protected]3cd17242009-06-23 02:59:02357 bytes_sent_ = 0;
358 }
359
[email protected]76a51ac82009-06-28 07:58:58360 int handshake_buf_len = buffer_.size() - bytes_sent_;
361 DCHECK_GT(handshake_buf_len, 0);
362 handshake_buf_ = new IOBuffer(handshake_buf_len);
363 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_],
364 handshake_buf_len);
[email protected]83039bb2011-12-09 18:43:55365 return transport_->socket()->Write(
[email protected]a2b2cfc2017-12-06 09:06:08366 handshake_buf_.get(), handshake_buf_len,
[email protected]27fb73c2018-01-11 13:27:24367 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)),
368 traffic_annotation_);
[email protected]3cd17242009-06-23 02:59:02369}
370
371int SOCKSClientSocket::DoHandshakeWriteComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02372 if (result < 0)
373 return result;
374
[email protected]76a51ac82009-06-28 07:58:58375 // We ignore the case when result is 0, since the underlying Write
376 // may return spurious writes while waiting on the socket.
377
[email protected]3cd17242009-06-23 02:59:02378 bytes_sent_ += result;
[email protected]76a51ac82009-06-28 07:58:58379 if (bytes_sent_ == buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02380 next_state_ = STATE_HANDSHAKE_READ;
[email protected]76a51ac82009-06-28 07:58:58381 buffer_.clear();
382 } else if (bytes_sent_ < buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02383 next_state_ = STATE_HANDSHAKE_WRITE;
384 } else {
385 return ERR_UNEXPECTED;
386 }
387
388 return OK;
389}
390
391int SOCKSClientSocket::DoHandshakeRead() {
[email protected]3cd17242009-06-23 02:59:02392 next_state_ = STATE_HANDSHAKE_READ_COMPLETE;
393
[email protected]76a51ac82009-06-28 07:58:58394 if (buffer_.empty()) {
[email protected]3cd17242009-06-23 02:59:02395 bytes_received_ = 0;
396 }
397
[email protected]76a51ac82009-06-28 07:58:58398 int handshake_buf_len = kReadHeaderSize - bytes_received_;
399 handshake_buf_ = new IOBuffer(handshake_buf_len);
[email protected]90499482013-06-01 00:39:50400 return transport_->socket()->Read(
401 handshake_buf_.get(),
402 handshake_buf_len,
403 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
[email protected]3cd17242009-06-23 02:59:02404}
405
406int SOCKSClientSocket::DoHandshakeReadComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02407 if (result < 0)
408 return result;
[email protected]76a51ac82009-06-28 07:58:58409
410 // The underlying socket closed unexpectedly.
411 if (result == 0)
412 return ERR_CONNECTION_CLOSED;
413
[email protected]d5a309592010-02-05 02:22:52414 if (bytes_received_ + result > kReadHeaderSize) {
[email protected]9e743cd2010-03-16 07:03:53415 // TODO(eroman): Describe failure in NetLog.
[email protected]d5a309592010-02-05 02:22:52416 return ERR_SOCKS_CONNECTION_FAILED;
417 }
[email protected]3cd17242009-06-23 02:59:02418
[email protected]76a51ac82009-06-28 07:58:58419 buffer_.append(handshake_buf_->data(), result);
[email protected]3cd17242009-06-23 02:59:02420 bytes_received_ += result;
[email protected]76a51ac82009-06-28 07:58:58421 if (bytes_received_ < kReadHeaderSize) {
[email protected]3cd17242009-06-23 02:59:02422 next_state_ = STATE_HANDSHAKE_READ;
423 return OK;
424 }
425
[email protected]76a51ac82009-06-28 07:58:58426 const SOCKS4ServerResponse* response =
427 reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data());
[email protected]3cd17242009-06-23 02:59:02428
429 if (response->reserved_null != 0x00) {
mmenke99b57172016-04-14 20:44:33430 DVLOG(1) << "Unknown response from SOCKS server.";
[email protected]d5a309592010-02-05 02:22:52431 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02432 }
433
[email protected]3cd17242009-06-23 02:59:02434 switch (response->code) {
435 case kServerResponseOk:
436 completed_handshake_ = true;
437 return OK;
438 case kServerResponseRejected:
mmenke99b57172016-04-14 20:44:33439 DVLOG(1) << "SOCKS request rejected or failed";
[email protected]d5a309592010-02-05 02:22:52440 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02441 case kServerResponseNotReachable:
mmenke99b57172016-04-14 20:44:33442 DVLOG(1) << "SOCKS request failed because client is not running "
443 << "identd (or not reachable from the server)";
[email protected]d5a309592010-02-05 02:22:52444 return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE;
[email protected]3cd17242009-06-23 02:59:02445 case kServerResponseMismatchedUserId:
mmenke99b57172016-04-14 20:44:33446 DVLOG(1) << "SOCKS request failed because client's identd could "
447 << "not confirm the user ID string in the request";
[email protected]d5a309592010-02-05 02:22:52448 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02449 default:
mmenke99b57172016-04-14 20:44:33450 DVLOG(1) << "SOCKS server sent unknown response";
[email protected]d5a309592010-02-05 02:22:52451 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02452 }
453
454 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol
455}
456
[email protected]a3528692012-06-08 00:11:42457int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const {
[email protected]a796bcec2010-03-22 17:17:26458 return transport_->socket()->GetPeerAddress(address);
[email protected]3cd17242009-06-23 02:59:02459}
[email protected]3cd17242009-06-23 02:59:02460
[email protected]e7f74da2011-04-19 23:49:35461int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const {
462 return transport_->socket()->GetLocalAddress(address);
463}
464
[email protected]3cd17242009-06-23 02:59:02465} // namespace net