blob: 633f12e3ca626254dded5d58fa22059e507c44bb [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"
[email protected]3cd17242009-06-23 02:59:0214#include "net/base/net_util.h"
eroman87c53d62015-04-02 06:51:0715#include "net/log/net_log.h"
[email protected]a796bcec2010-03-22 17:17:2616#include "net/socket/client_socket_handle.h"
[email protected]3cd17242009-06-23 02:59:0217
18namespace net {
19
20// Every SOCKS server requests a user-id from the client. It is optional
21// and we send an empty string.
22static const char kEmptyUserId[] = "";
23
[email protected]3cd17242009-06-23 02:59:0224// For SOCKS4, the client sends 8 bytes plus the size of the user-id.
[email protected]76a51ac82009-06-28 07:58:5825static const unsigned int kWriteHeaderSize = 8;
[email protected]3cd17242009-06-23 02:59:0226
[email protected]034d3892011-03-29 04:07:2127// For SOCKS4 the server sends 8 bytes for acknowledgement.
[email protected]76a51ac82009-06-28 07:58:5828static const unsigned int kReadHeaderSize = 8;
[email protected]3cd17242009-06-23 02:59:0229
30// Server Response codes for SOCKS.
Avi Drissman13fc8932015-12-20 04:40:4631static const uint8_t kServerResponseOk = 0x5A;
32static const uint8_t kServerResponseRejected = 0x5B;
33static const uint8_t kServerResponseNotReachable = 0x5C;
34static const uint8_t kServerResponseMismatchedUserId = 0x5D;
[email protected]3cd17242009-06-23 02:59:0235
Avi Drissman13fc8932015-12-20 04:40:4636static const uint8_t kSOCKSVersion4 = 0x04;
37static const uint8_t kSOCKSStreamRequest = 0x01;
[email protected]3cd17242009-06-23 02:59:0238
[email protected]034d3892011-03-29 04:07:2139// A struct holding the essential details of the SOCKS4 Server Request.
[email protected]3cd17242009-06-23 02:59:0240// The port in the header is stored in network byte order.
41struct SOCKS4ServerRequest {
Avi Drissman13fc8932015-12-20 04:40:4642 uint8_t version;
43 uint8_t command;
44 uint16_t nw_port;
45 uint8_t ip[4];
[email protected]3cd17242009-06-23 02:59:0246};
mostynb91e0da982015-01-20 19:17:2747static_assert(sizeof(SOCKS4ServerRequest) == kWriteHeaderSize,
48 "socks4 server request struct has incorrect size");
[email protected]3cd17242009-06-23 02:59:0249
[email protected]034d3892011-03-29 04:07:2150// A struct holding details of the SOCKS4 Server Response.
[email protected]3cd17242009-06-23 02:59:0251struct SOCKS4ServerResponse {
Avi Drissman13fc8932015-12-20 04:40:4652 uint8_t reserved_null;
53 uint8_t code;
54 uint16_t port;
55 uint8_t ip[4];
[email protected]3cd17242009-06-23 02:59:0256};
mostynb91e0da982015-01-20 19:17:2757static_assert(sizeof(SOCKS4ServerResponse) == kReadHeaderSize,
58 "socks4 server response struct has incorrect size");
[email protected]3cd17242009-06-23 02:59:0259
[email protected]18ccfdb2013-08-15 00:13:4460SOCKSClientSocket::SOCKSClientSocket(
61 scoped_ptr<ClientSocketHandle> transport_socket,
62 const HostResolver::RequestInfo& req_info,
[email protected]5109c1952013-08-20 18:44:1063 RequestPriority priority,
[email protected]18ccfdb2013-08-15 00:13:4464 HostResolver* host_resolver)
dchengc7eeda422015-12-26 03:56:4865 : transport_(std::move(transport_socket)),
[email protected]3cd17242009-06-23 02:59:0266 next_state_(STATE_NONE),
[email protected]3cd17242009-06-23 02:59:0267 completed_handshake_(false),
68 bytes_sent_(0),
69 bytes_received_(0),
[email protected]0dc88b32014-03-26 20:12:2870 was_ever_used_(false),
[email protected]76a51ac82009-06-28 07:58:5871 host_resolver_(host_resolver),
[email protected]a2006ece2010-04-23 16:44:0272 host_request_info_(req_info),
[email protected]5109c1952013-08-20 18:44:1073 priority_(priority),
74 net_log_(transport_->socket()->NetLog()) {}
[email protected]3cd17242009-06-23 02:59:0275
76SOCKSClientSocket::~SOCKSClientSocket() {
77 Disconnect();
78}
79
[email protected]83039bb2011-12-09 18:43:5580int SOCKSClientSocket::Connect(const CompletionCallback& callback) {
[email protected]3cd17242009-06-23 02:59:0281 DCHECK(transport_.get());
[email protected]a796bcec2010-03-22 17:17:2682 DCHECK(transport_->socket());
[email protected]3cd17242009-06-23 02:59:0283 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:5584 DCHECK(user_callback_.is_null());
[email protected]3cd17242009-06-23 02:59:0285
86 // If already connected, then just return OK.
87 if (completed_handshake_)
88 return OK;
89
90 next_state_ = STATE_RESOLVE_HOST;
[email protected]5a05c47a2009-11-02 23:25:1991
[email protected]3aa4af042012-06-14 21:02:3192 net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT);
[email protected]3cd17242009-06-23 02:59:0293
94 int rv = DoLoop(OK);
[email protected]5a05c47a2009-11-02 23:25:1995 if (rv == ERR_IO_PENDING) {
[email protected]83039bb2011-12-09 18:43:5596 user_callback_ = callback;
[email protected]5a05c47a2009-11-02 23:25:1997 } else {
[email protected]d7fd1782011-02-08 19:16:4398 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
[email protected]5a05c47a2009-11-02 23:25:1999 }
[email protected]3cd17242009-06-23 02:59:02100 return rv;
101}
102
103void SOCKSClientSocket::Disconnect() {
104 completed_handshake_ = false;
[email protected]16a02742010-01-07 22:50:10105 host_resolver_.Cancel();
[email protected]a796bcec2010-03-22 17:17:26106 transport_->socket()->Disconnect();
[email protected]16a02742010-01-07 22:50:10107
108 // Reset other states to make sure they aren't mistakenly used later.
109 // These are the states initialized by Connect().
110 next_state_ = STATE_NONE;
[email protected]dbf036f2011-12-06 23:33:24111 user_callback_.Reset();
[email protected]3cd17242009-06-23 02:59:02112}
113
114bool SOCKSClientSocket::IsConnected() const {
[email protected]a796bcec2010-03-22 17:17:26115 return completed_handshake_ && transport_->socket()->IsConnected();
[email protected]3cd17242009-06-23 02:59:02116}
117
118bool SOCKSClientSocket::IsConnectedAndIdle() const {
[email protected]a796bcec2010-03-22 17:17:26119 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle();
[email protected]3cd17242009-06-23 02:59:02120}
121
[email protected]e4be2dd2010-12-14 00:44:39122const BoundNetLog& SOCKSClientSocket::NetLog() const {
123 return net_log_;
124}
125
[email protected]9b5614a2010-08-25 20:29:45126void SOCKSClientSocket::SetSubresourceSpeculation() {
127 if (transport_.get() && transport_->socket()) {
128 transport_->socket()->SetSubresourceSpeculation();
129 } else {
130 NOTREACHED();
131 }
132}
133
134void SOCKSClientSocket::SetOmniboxSpeculation() {
135 if (transport_.get() && transport_->socket()) {
136 transport_->socket()->SetOmniboxSpeculation();
137 } else {
138 NOTREACHED();
139 }
140}
141
[email protected]0f873e82010-09-02 16:09:01142bool SOCKSClientSocket::WasEverUsed() const {
[email protected]0dc88b32014-03-26 20:12:28143 return was_ever_used_;
[email protected]0f873e82010-09-02 16:09:01144}
145
[email protected]7f7e92392010-10-26 18:29:29146bool SOCKSClientSocket::UsingTCPFastOpen() const {
147 if (transport_.get() && transport_->socket()) {
148 return transport_->socket()->UsingTCPFastOpen();
149 }
150 NOTREACHED();
151 return false;
152}
153
[email protected]2d88e7d2012-07-19 17:55:17154bool SOCKSClientSocket::WasNpnNegotiated() const {
155 if (transport_.get() && transport_->socket()) {
156 return transport_->socket()->WasNpnNegotiated();
157 }
158 NOTREACHED();
159 return false;
160}
161
[email protected]33661e482012-04-03 16:16:26162NextProto SOCKSClientSocket::GetNegotiatedProtocol() const {
163 if (transport_.get() && transport_->socket()) {
164 return transport_->socket()->GetNegotiatedProtocol();
165 }
166 NOTREACHED();
167 return kProtoUnknown;
168}
169
[email protected]2d88e7d2012-07-19 17:55:17170bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
171 if (transport_.get() && transport_->socket()) {
172 return transport_->socket()->GetSSLInfo(ssl_info);
173 }
174 NOTREACHED();
175 return false;
ttuttle23fdb7b2015-05-15 01:28:03176}
[email protected]2d88e7d2012-07-19 17:55:17177
ttuttle23fdb7b2015-05-15 01:28:03178void SOCKSClientSocket::GetConnectionAttempts(ConnectionAttempts* out) const {
179 out->clear();
[email protected]2d88e7d2012-07-19 17:55:17180}
181
tbansalf82cc8e2015-10-14 20:05:49182int64_t SOCKSClientSocket::GetTotalReceivedBytes() const {
183 return transport_->socket()->GetTotalReceivedBytes();
184}
185
[email protected]3cd17242009-06-23 02:59:02186// Read is called by the transport layer above to read. This can only be done
187// if the SOCKS handshake is complete.
188int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len,
[email protected]3f55aa12011-12-07 02:03:33189 const CompletionCallback& callback) {
190 DCHECK(completed_handshake_);
191 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55192 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28193 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02194
[email protected]0dc88b32014-03-26 20:12:28195 int rv = transport_->socket()->Read(
196 buf, buf_len,
197 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
198 base::Unretained(this), callback));
199 if (rv > 0)
200 was_ever_used_ = true;
201 return rv;
[email protected]3cd17242009-06-23 02:59:02202}
203
204// Write is called by the transport layer. This can only be done if the
205// SOCKS handshake is complete.
206int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55207 const CompletionCallback& callback) {
[email protected]3cd17242009-06-23 02:59:02208 DCHECK(completed_handshake_);
209 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55210 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28211 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02212
[email protected]0dc88b32014-03-26 20:12:28213 int rv = transport_->socket()->Write(
214 buf, buf_len,
215 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
216 base::Unretained(this), callback));
217 if (rv > 0)
218 was_ever_used_ = true;
219 return rv;
[email protected]3cd17242009-06-23 02:59:02220}
221
Avi Drissman13fc8932015-12-20 04:40:46222int SOCKSClientSocket::SetReceiveBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26223 return transport_->socket()->SetReceiveBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04224}
225
Avi Drissman13fc8932015-12-20 04:40:46226int SOCKSClientSocket::SetSendBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26227 return transport_->socket()->SetSendBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04228}
229
[email protected]3cd17242009-06-23 02:59:02230void SOCKSClientSocket::DoCallback(int result) {
231 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]83039bb2011-12-09 18:43:55232 DCHECK(!user_callback_.is_null());
[email protected]3cd17242009-06-23 02:59:02233
234 // Since Run() may result in Read being called,
235 // clear user_callback_ up front.
[email protected]83039bb2011-12-09 18:43:55236 DVLOG(1) << "Finished setting up SOCKS handshake";
[email protected]0dc88b32014-03-26 20:12:28237 base::ResetAndReturn(&user_callback_).Run(result);
[email protected]3cd17242009-06-23 02:59:02238}
239
240void SOCKSClientSocket::OnIOComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02241 DCHECK_NE(STATE_NONE, next_state_);
242 int rv = DoLoop(result);
[email protected]5a05c47a2009-11-02 23:25:19243 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43244 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
[email protected]3cd17242009-06-23 02:59:02245 DoCallback(rv);
[email protected]5a05c47a2009-11-02 23:25:19246 }
[email protected]3cd17242009-06-23 02:59:02247}
248
[email protected]0dc88b32014-03-26 20:12:28249void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback& callback,
250 int result) {
251 DCHECK_NE(ERR_IO_PENDING, result);
252 DCHECK(!callback.is_null());
253
254 if (result > 0)
255 was_ever_used_ = true;
256 callback.Run(result);
257}
258
[email protected]3cd17242009-06-23 02:59:02259int SOCKSClientSocket::DoLoop(int last_io_result) {
260 DCHECK_NE(next_state_, STATE_NONE);
261 int rv = last_io_result;
262 do {
263 State state = next_state_;
264 next_state_ = STATE_NONE;
265 switch (state) {
266 case STATE_RESOLVE_HOST:
267 DCHECK_EQ(OK, rv);
268 rv = DoResolveHost();
269 break;
270 case STATE_RESOLVE_HOST_COMPLETE:
271 rv = DoResolveHostComplete(rv);
272 break;
273 case STATE_HANDSHAKE_WRITE:
274 DCHECK_EQ(OK, rv);
275 rv = DoHandshakeWrite();
276 break;
277 case STATE_HANDSHAKE_WRITE_COMPLETE:
278 rv = DoHandshakeWriteComplete(rv);
279 break;
280 case STATE_HANDSHAKE_READ:
281 DCHECK_EQ(OK, rv);
282 rv = DoHandshakeRead();
283 break;
284 case STATE_HANDSHAKE_READ_COMPLETE:
285 rv = DoHandshakeReadComplete(rv);
286 break;
287 default:
288 NOTREACHED() << "bad state";
289 rv = ERR_UNEXPECTED;
290 break;
291 }
292 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
293 return rv;
294}
295
296int SOCKSClientSocket::DoResolveHost() {
[email protected]3cd17242009-06-23 02:59:02297 next_state_ = STATE_RESOLVE_HOST_COMPLETE;
[email protected]034d3892011-03-29 04:07:21298 // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4
299 // addresses for the target host.
300 host_request_info_.set_address_family(ADDRESS_FAMILY_IPV4);
[email protected]ec08bb22009-08-12 00:25:12301 return host_resolver_.Resolve(
[email protected]5109c1952013-08-20 18:44:10302 host_request_info_,
303 priority_,
304 &addresses_,
[email protected]aa22b242011-11-16 18:58:29305 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)),
306 net_log_);
[email protected]3cd17242009-06-23 02:59:02307}
308
309int SOCKSClientSocket::DoResolveHostComplete(int result) {
[email protected]034d3892011-03-29 04:07:21310 if (result != OK) {
311 // Resolving the hostname failed; fail the request rather than automatically
312 // falling back to SOCKS4a (since it can be confusing to see invalid IP
313 // addresses being sent to the SOCKS4 server when it doesn't support 4A.)
314 return result;
[email protected]3cd17242009-06-23 02:59:02315 }
316
[email protected]034d3892011-03-29 04:07:21317 next_state_ = STATE_HANDSHAKE_WRITE;
[email protected]3cd17242009-06-23 02:59:02318 return OK;
319}
320
321// Builds the buffer that is to be sent to the server.
[email protected]76a51ac82009-06-28 07:58:58322const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const {
[email protected]76a51ac82009-06-28 07:58:58323 SOCKS4ServerRequest request;
324 request.version = kSOCKSVersion4;
325 request.command = kSOCKSStreamRequest;
[email protected]9eb7b11b2012-03-28 20:19:31326 request.nw_port = base::HostToNet16(host_request_info_.port());
[email protected]3cd17242009-06-23 02:59:02327
[email protected]7054e78f2012-05-07 21:44:56328 DCHECK(!addresses_.empty());
329 const IPEndPoint& endpoint = addresses_.front();
[email protected]3cd17242009-06-23 02:59:02330
[email protected]034d3892011-03-29 04:07:21331 // We disabled IPv6 results when resolving the hostname, so none of the
332 // results in the list will be IPv6.
333 // TODO(eroman): we only ever use the first address in the list. It would be
334 // more robust to try all the IP addresses we have before
335 // failing the connect attempt.
[email protected]e466aaf2012-12-13 01:46:44336 CHECK_EQ(ADDRESS_FAMILY_IPV4, endpoint.GetFamily());
[email protected]7054e78f2012-05-07 21:44:56337 CHECK_LE(endpoint.address().size(), sizeof(request.ip));
martijn98acb9b2016-01-27 07:14:07338 memcpy(&request.ip, &endpoint.address().bytes()[0],
339 endpoint.address().size());
[email protected]034d3892011-03-29 04:07:21340
[email protected]7054e78f2012-05-07 21:44:56341 DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort();
[email protected]3cd17242009-06-23 02:59:02342
[email protected]76a51ac82009-06-28 07:58:58343 std::string handshake_data(reinterpret_cast<char*>(&request),
344 sizeof(request));
345 handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId));
[email protected]3cd17242009-06-23 02:59:02346
[email protected]76a51ac82009-06-28 07:58:58347 return handshake_data;
[email protected]3cd17242009-06-23 02:59:02348}
349
350// Writes the SOCKS handshake data to the underlying socket connection.
351int SOCKSClientSocket::DoHandshakeWrite() {
352 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE;
353
[email protected]76a51ac82009-06-28 07:58:58354 if (buffer_.empty()) {
355 buffer_ = BuildHandshakeWriteBuffer();
[email protected]3cd17242009-06-23 02:59:02356 bytes_sent_ = 0;
357 }
358
[email protected]76a51ac82009-06-28 07:58:58359 int handshake_buf_len = buffer_.size() - bytes_sent_;
360 DCHECK_GT(handshake_buf_len, 0);
361 handshake_buf_ = new IOBuffer(handshake_buf_len);
362 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_],
363 handshake_buf_len);
[email protected]83039bb2011-12-09 18:43:55364 return transport_->socket()->Write(
[email protected]90499482013-06-01 00:39:50365 handshake_buf_.get(),
366 handshake_buf_len,
[email protected]83039bb2011-12-09 18:43:55367 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
[email protected]3cd17242009-06-23 02:59:02368}
369
370int SOCKSClientSocket::DoHandshakeWriteComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02371 if (result < 0)
372 return result;
373
[email protected]76a51ac82009-06-28 07:58:58374 // We ignore the case when result is 0, since the underlying Write
375 // may return spurious writes while waiting on the socket.
376
[email protected]3cd17242009-06-23 02:59:02377 bytes_sent_ += result;
[email protected]76a51ac82009-06-28 07:58:58378 if (bytes_sent_ == buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02379 next_state_ = STATE_HANDSHAKE_READ;
[email protected]76a51ac82009-06-28 07:58:58380 buffer_.clear();
381 } else if (bytes_sent_ < buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02382 next_state_ = STATE_HANDSHAKE_WRITE;
383 } else {
384 return ERR_UNEXPECTED;
385 }
386
387 return OK;
388}
389
390int SOCKSClientSocket::DoHandshakeRead() {
[email protected]3cd17242009-06-23 02:59:02391 next_state_ = STATE_HANDSHAKE_READ_COMPLETE;
392
[email protected]76a51ac82009-06-28 07:58:58393 if (buffer_.empty()) {
[email protected]3cd17242009-06-23 02:59:02394 bytes_received_ = 0;
395 }
396
[email protected]76a51ac82009-06-28 07:58:58397 int handshake_buf_len = kReadHeaderSize - bytes_received_;
398 handshake_buf_ = new IOBuffer(handshake_buf_len);
[email protected]90499482013-06-01 00:39:50399 return transport_->socket()->Read(
400 handshake_buf_.get(),
401 handshake_buf_len,
402 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
[email protected]3cd17242009-06-23 02:59:02403}
404
405int SOCKSClientSocket::DoHandshakeReadComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02406 if (result < 0)
407 return result;
[email protected]76a51ac82009-06-28 07:58:58408
409 // The underlying socket closed unexpectedly.
410 if (result == 0)
411 return ERR_CONNECTION_CLOSED;
412
[email protected]d5a309592010-02-05 02:22:52413 if (bytes_received_ + result > kReadHeaderSize) {
[email protected]9e743cd2010-03-16 07:03:53414 // TODO(eroman): Describe failure in NetLog.
[email protected]d5a309592010-02-05 02:22:52415 return ERR_SOCKS_CONNECTION_FAILED;
416 }
[email protected]3cd17242009-06-23 02:59:02417
[email protected]76a51ac82009-06-28 07:58:58418 buffer_.append(handshake_buf_->data(), result);
[email protected]3cd17242009-06-23 02:59:02419 bytes_received_ += result;
[email protected]76a51ac82009-06-28 07:58:58420 if (bytes_received_ < kReadHeaderSize) {
[email protected]3cd17242009-06-23 02:59:02421 next_state_ = STATE_HANDSHAKE_READ;
422 return OK;
423 }
424
[email protected]76a51ac82009-06-28 07:58:58425 const SOCKS4ServerResponse* response =
426 reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data());
[email protected]3cd17242009-06-23 02:59:02427
428 if (response->reserved_null != 0x00) {
429 LOG(ERROR) << "Unknown response from SOCKS server.";
[email protected]d5a309592010-02-05 02:22:52430 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02431 }
432
[email protected]3cd17242009-06-23 02:59:02433 switch (response->code) {
434 case kServerResponseOk:
435 completed_handshake_ = true;
436 return OK;
437 case kServerResponseRejected:
438 LOG(ERROR) << "SOCKS request rejected or failed";
[email protected]d5a309592010-02-05 02:22:52439 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02440 case kServerResponseNotReachable:
441 LOG(ERROR) << "SOCKS request failed because client is not running "
442 << "identd (or not reachable from the server)";
[email protected]d5a309592010-02-05 02:22:52443 return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE;
[email protected]3cd17242009-06-23 02:59:02444 case kServerResponseMismatchedUserId:
445 LOG(ERROR) << "SOCKS request failed because client's identd could "
446 << "not confirm the user ID string in the request";
[email protected]d5a309592010-02-05 02:22:52447 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02448 default:
449 LOG(ERROR) << "SOCKS server sent unknown response";
[email protected]d5a309592010-02-05 02:22:52450 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02451 }
452
453 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol
454}
455
[email protected]a3528692012-06-08 00:11:42456int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const {
[email protected]a796bcec2010-03-22 17:17:26457 return transport_->socket()->GetPeerAddress(address);
[email protected]3cd17242009-06-23 02:59:02458}
[email protected]3cd17242009-06-23 02:59:02459
[email protected]e7f74da2011-04-19 23:49:35460int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const {
461 return transport_->socket()->GetLocalAddress(address);
462}
463
[email protected]3cd17242009-06-23 02:59:02464} // namespace net