blob: 8cbd379ef7b5cbf7f1a2d69759727698b9b72b23 [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]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(
danakj655b66c2016-04-16 00:51:3861 std::unique_ptr<ClientSocketHandle> transport_socket,
[email protected]18ccfdb2013-08-15 00:13:4462 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
mikecirone8b85c432016-09-08 19:11:0092 net_log_.BeginEvent(NetLogEventType::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 {
mikecirone8b85c432016-09-08 19:11:0098 net_log_.EndEventWithNetErrorCode(NetLogEventType::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;
maksim.sisov320d5962016-08-15 18:02:51105 request_.reset();
[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
tfarina428341112016-09-22 13:38:20122const NetLogWithSource& SOCKSClientSocket::NetLog() const {
[email protected]e4be2dd2010-12-14 00:44:39123 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]2d88e7d2012-07-19 17:55:17146bool SOCKSClientSocket::WasNpnNegotiated() const {
147 if (transport_.get() && transport_->socket()) {
148 return transport_->socket()->WasNpnNegotiated();
149 }
150 NOTREACHED();
151 return false;
152}
153
[email protected]33661e482012-04-03 16:16:26154NextProto SOCKSClientSocket::GetNegotiatedProtocol() const {
155 if (transport_.get() && transport_->socket()) {
156 return transport_->socket()->GetNegotiatedProtocol();
157 }
158 NOTREACHED();
159 return kProtoUnknown;
160}
161
[email protected]2d88e7d2012-07-19 17:55:17162bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
163 if (transport_.get() && transport_->socket()) {
164 return transport_->socket()->GetSSLInfo(ssl_info);
165 }
166 NOTREACHED();
167 return false;
ttuttle23fdb7b2015-05-15 01:28:03168}
[email protected]2d88e7d2012-07-19 17:55:17169
ttuttle23fdb7b2015-05-15 01:28:03170void SOCKSClientSocket::GetConnectionAttempts(ConnectionAttempts* out) const {
171 out->clear();
[email protected]2d88e7d2012-07-19 17:55:17172}
173
tbansalf82cc8e2015-10-14 20:05:49174int64_t SOCKSClientSocket::GetTotalReceivedBytes() const {
175 return transport_->socket()->GetTotalReceivedBytes();
176}
177
[email protected]3cd17242009-06-23 02:59:02178// Read is called by the transport layer above to read. This can only be done
179// if the SOCKS handshake is complete.
180int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len,
[email protected]3f55aa12011-12-07 02:03:33181 const CompletionCallback& callback) {
182 DCHECK(completed_handshake_);
183 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55184 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28185 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02186
[email protected]0dc88b32014-03-26 20:12:28187 int rv = transport_->socket()->Read(
188 buf, buf_len,
189 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
190 base::Unretained(this), callback));
191 if (rv > 0)
192 was_ever_used_ = true;
193 return rv;
[email protected]3cd17242009-06-23 02:59:02194}
195
196// Write is called by the transport layer. This can only be done if the
197// SOCKS handshake is complete.
198int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55199 const CompletionCallback& callback) {
[email protected]3cd17242009-06-23 02:59:02200 DCHECK(completed_handshake_);
201 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55202 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28203 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02204
[email protected]0dc88b32014-03-26 20:12:28205 int rv = transport_->socket()->Write(
206 buf, buf_len,
207 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
208 base::Unretained(this), callback));
209 if (rv > 0)
210 was_ever_used_ = true;
211 return rv;
[email protected]3cd17242009-06-23 02:59:02212}
213
Avi Drissman13fc8932015-12-20 04:40:46214int SOCKSClientSocket::SetReceiveBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26215 return transport_->socket()->SetReceiveBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04216}
217
Avi Drissman13fc8932015-12-20 04:40:46218int SOCKSClientSocket::SetSendBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26219 return transport_->socket()->SetSendBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04220}
221
[email protected]3cd17242009-06-23 02:59:02222void SOCKSClientSocket::DoCallback(int result) {
223 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]83039bb2011-12-09 18:43:55224 DCHECK(!user_callback_.is_null());
[email protected]3cd17242009-06-23 02:59:02225
226 // Since Run() may result in Read being called,
227 // clear user_callback_ up front.
[email protected]83039bb2011-12-09 18:43:55228 DVLOG(1) << "Finished setting up SOCKS handshake";
[email protected]0dc88b32014-03-26 20:12:28229 base::ResetAndReturn(&user_callback_).Run(result);
[email protected]3cd17242009-06-23 02:59:02230}
231
232void SOCKSClientSocket::OnIOComplete(int result) {
233 DCHECK_NE(STATE_NONE, next_state_);
234 int rv = DoLoop(result);
[email protected]5a05c47a2009-11-02 23:25:19235 if (rv != ERR_IO_PENDING) {
mikecirone8b85c432016-09-08 19:11:00236 net_log_.EndEventWithNetErrorCode(NetLogEventType::SOCKS_CONNECT, rv);
[email protected]3cd17242009-06-23 02:59:02237 DoCallback(rv);
[email protected]5a05c47a2009-11-02 23:25:19238 }
[email protected]3cd17242009-06-23 02:59:02239}
240
[email protected]0dc88b32014-03-26 20:12:28241void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback& callback,
242 int result) {
243 DCHECK_NE(ERR_IO_PENDING, result);
244 DCHECK(!callback.is_null());
245
246 if (result > 0)
247 was_ever_used_ = true;
248 callback.Run(result);
249}
250
[email protected]3cd17242009-06-23 02:59:02251int SOCKSClientSocket::DoLoop(int last_io_result) {
252 DCHECK_NE(next_state_, STATE_NONE);
253 int rv = last_io_result;
254 do {
255 State state = next_state_;
256 next_state_ = STATE_NONE;
257 switch (state) {
258 case STATE_RESOLVE_HOST:
259 DCHECK_EQ(OK, rv);
260 rv = DoResolveHost();
261 break;
262 case STATE_RESOLVE_HOST_COMPLETE:
263 rv = DoResolveHostComplete(rv);
264 break;
265 case STATE_HANDSHAKE_WRITE:
266 DCHECK_EQ(OK, rv);
267 rv = DoHandshakeWrite();
268 break;
269 case STATE_HANDSHAKE_WRITE_COMPLETE:
270 rv = DoHandshakeWriteComplete(rv);
271 break;
272 case STATE_HANDSHAKE_READ:
273 DCHECK_EQ(OK, rv);
274 rv = DoHandshakeRead();
275 break;
276 case STATE_HANDSHAKE_READ_COMPLETE:
277 rv = DoHandshakeReadComplete(rv);
278 break;
279 default:
280 NOTREACHED() << "bad state";
281 rv = ERR_UNEXPECTED;
282 break;
283 }
284 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
285 return rv;
286}
287
288int SOCKSClientSocket::DoResolveHost() {
[email protected]3cd17242009-06-23 02:59:02289 next_state_ = STATE_RESOLVE_HOST_COMPLETE;
[email protected]034d3892011-03-29 04:07:21290 // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4
291 // addresses for the target host.
292 host_request_info_.set_address_family(ADDRESS_FAMILY_IPV4);
maksim.sisov320d5962016-08-15 18:02:51293 return host_resolver_->Resolve(
294 host_request_info_, priority_, &addresses_,
[email protected]aa22b242011-11-16 18:58:29295 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)),
maksim.sisov320d5962016-08-15 18:02:51296 &request_, net_log_);
[email protected]3cd17242009-06-23 02:59:02297}
298
299int SOCKSClientSocket::DoResolveHostComplete(int result) {
[email protected]034d3892011-03-29 04:07:21300 if (result != OK) {
301 // Resolving the hostname failed; fail the request rather than automatically
302 // falling back to SOCKS4a (since it can be confusing to see invalid IP
303 // addresses being sent to the SOCKS4 server when it doesn't support 4A.)
304 return result;
[email protected]3cd17242009-06-23 02:59:02305 }
306
[email protected]034d3892011-03-29 04:07:21307 next_state_ = STATE_HANDSHAKE_WRITE;
[email protected]3cd17242009-06-23 02:59:02308 return OK;
309}
310
311// Builds the buffer that is to be sent to the server.
[email protected]76a51ac82009-06-28 07:58:58312const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const {
[email protected]76a51ac82009-06-28 07:58:58313 SOCKS4ServerRequest request;
314 request.version = kSOCKSVersion4;
315 request.command = kSOCKSStreamRequest;
[email protected]9eb7b11b2012-03-28 20:19:31316 request.nw_port = base::HostToNet16(host_request_info_.port());
[email protected]3cd17242009-06-23 02:59:02317
[email protected]7054e78f2012-05-07 21:44:56318 DCHECK(!addresses_.empty());
319 const IPEndPoint& endpoint = addresses_.front();
[email protected]3cd17242009-06-23 02:59:02320
[email protected]034d3892011-03-29 04:07:21321 // We disabled IPv6 results when resolving the hostname, so none of the
322 // results in the list will be IPv6.
323 // TODO(eroman): we only ever use the first address in the list. It would be
324 // more robust to try all the IP addresses we have before
325 // failing the connect attempt.
[email protected]e466aaf2012-12-13 01:46:44326 CHECK_EQ(ADDRESS_FAMILY_IPV4, endpoint.GetFamily());
[email protected]7054e78f2012-05-07 21:44:56327 CHECK_LE(endpoint.address().size(), sizeof(request.ip));
martijn98acb9b2016-01-27 07:14:07328 memcpy(&request.ip, &endpoint.address().bytes()[0],
329 endpoint.address().size());
[email protected]034d3892011-03-29 04:07:21330
[email protected]7054e78f2012-05-07 21:44:56331 DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort();
[email protected]3cd17242009-06-23 02:59:02332
[email protected]76a51ac82009-06-28 07:58:58333 std::string handshake_data(reinterpret_cast<char*>(&request),
334 sizeof(request));
335 handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId));
[email protected]3cd17242009-06-23 02:59:02336
[email protected]76a51ac82009-06-28 07:58:58337 return handshake_data;
[email protected]3cd17242009-06-23 02:59:02338}
339
340// Writes the SOCKS handshake data to the underlying socket connection.
341int SOCKSClientSocket::DoHandshakeWrite() {
342 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE;
343
[email protected]76a51ac82009-06-28 07:58:58344 if (buffer_.empty()) {
345 buffer_ = BuildHandshakeWriteBuffer();
[email protected]3cd17242009-06-23 02:59:02346 bytes_sent_ = 0;
347 }
348
[email protected]76a51ac82009-06-28 07:58:58349 int handshake_buf_len = buffer_.size() - bytes_sent_;
350 DCHECK_GT(handshake_buf_len, 0);
351 handshake_buf_ = new IOBuffer(handshake_buf_len);
352 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_],
353 handshake_buf_len);
[email protected]83039bb2011-12-09 18:43:55354 return transport_->socket()->Write(
[email protected]90499482013-06-01 00:39:50355 handshake_buf_.get(),
356 handshake_buf_len,
[email protected]83039bb2011-12-09 18:43:55357 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
[email protected]3cd17242009-06-23 02:59:02358}
359
360int SOCKSClientSocket::DoHandshakeWriteComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02361 if (result < 0)
362 return result;
363
[email protected]76a51ac82009-06-28 07:58:58364 // We ignore the case when result is 0, since the underlying Write
365 // may return spurious writes while waiting on the socket.
366
[email protected]3cd17242009-06-23 02:59:02367 bytes_sent_ += result;
[email protected]76a51ac82009-06-28 07:58:58368 if (bytes_sent_ == buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02369 next_state_ = STATE_HANDSHAKE_READ;
[email protected]76a51ac82009-06-28 07:58:58370 buffer_.clear();
371 } else if (bytes_sent_ < buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02372 next_state_ = STATE_HANDSHAKE_WRITE;
373 } else {
374 return ERR_UNEXPECTED;
375 }
376
377 return OK;
378}
379
380int SOCKSClientSocket::DoHandshakeRead() {
[email protected]3cd17242009-06-23 02:59:02381 next_state_ = STATE_HANDSHAKE_READ_COMPLETE;
382
[email protected]76a51ac82009-06-28 07:58:58383 if (buffer_.empty()) {
[email protected]3cd17242009-06-23 02:59:02384 bytes_received_ = 0;
385 }
386
[email protected]76a51ac82009-06-28 07:58:58387 int handshake_buf_len = kReadHeaderSize - bytes_received_;
388 handshake_buf_ = new IOBuffer(handshake_buf_len);
[email protected]90499482013-06-01 00:39:50389 return transport_->socket()->Read(
390 handshake_buf_.get(),
391 handshake_buf_len,
392 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
[email protected]3cd17242009-06-23 02:59:02393}
394
395int SOCKSClientSocket::DoHandshakeReadComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02396 if (result < 0)
397 return result;
[email protected]76a51ac82009-06-28 07:58:58398
399 // The underlying socket closed unexpectedly.
400 if (result == 0)
401 return ERR_CONNECTION_CLOSED;
402
[email protected]d5a309592010-02-05 02:22:52403 if (bytes_received_ + result > kReadHeaderSize) {
[email protected]9e743cd2010-03-16 07:03:53404 // TODO(eroman): Describe failure in NetLog.
[email protected]d5a309592010-02-05 02:22:52405 return ERR_SOCKS_CONNECTION_FAILED;
406 }
[email protected]3cd17242009-06-23 02:59:02407
[email protected]76a51ac82009-06-28 07:58:58408 buffer_.append(handshake_buf_->data(), result);
[email protected]3cd17242009-06-23 02:59:02409 bytes_received_ += result;
[email protected]76a51ac82009-06-28 07:58:58410 if (bytes_received_ < kReadHeaderSize) {
[email protected]3cd17242009-06-23 02:59:02411 next_state_ = STATE_HANDSHAKE_READ;
412 return OK;
413 }
414
[email protected]76a51ac82009-06-28 07:58:58415 const SOCKS4ServerResponse* response =
416 reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data());
[email protected]3cd17242009-06-23 02:59:02417
418 if (response->reserved_null != 0x00) {
mmenke99b57172016-04-14 20:44:33419 DVLOG(1) << "Unknown response from SOCKS server.";
[email protected]d5a309592010-02-05 02:22:52420 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02421 }
422
[email protected]3cd17242009-06-23 02:59:02423 switch (response->code) {
424 case kServerResponseOk:
425 completed_handshake_ = true;
426 return OK;
427 case kServerResponseRejected:
mmenke99b57172016-04-14 20:44:33428 DVLOG(1) << "SOCKS request rejected or failed";
[email protected]d5a309592010-02-05 02:22:52429 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02430 case kServerResponseNotReachable:
mmenke99b57172016-04-14 20:44:33431 DVLOG(1) << "SOCKS request failed because client is not running "
432 << "identd (or not reachable from the server)";
[email protected]d5a309592010-02-05 02:22:52433 return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE;
[email protected]3cd17242009-06-23 02:59:02434 case kServerResponseMismatchedUserId:
mmenke99b57172016-04-14 20:44:33435 DVLOG(1) << "SOCKS request failed because client's identd could "
436 << "not confirm the user ID string in the request";
[email protected]d5a309592010-02-05 02:22:52437 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02438 default:
mmenke99b57172016-04-14 20:44:33439 DVLOG(1) << "SOCKS server sent unknown response";
[email protected]d5a309592010-02-05 02:22:52440 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02441 }
442
443 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol
444}
445
[email protected]a3528692012-06-08 00:11:42446int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const {
[email protected]a796bcec2010-03-22 17:17:26447 return transport_->socket()->GetPeerAddress(address);
[email protected]3cd17242009-06-23 02:59:02448}
[email protected]3cd17242009-06-23 02:59:02449
[email protected]e7f74da2011-04-19 23:49:35450int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const {
451 return transport_->socket()->GetLocalAddress(address);
452}
453
[email protected]3cd17242009-06-23 02:59:02454} // namespace net