blob: 678af10bf23cfe98236ba7de4b43cbe20b5560af [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
[email protected]aa22b242011-11-16 18:58:297#include "base/bind.h"
[email protected]0dc88b32014-03-26 20:12:288#include "base/callback_helpers.h"
[email protected]3cd17242009-06-23 02:59:029#include "base/compiler_specific.h"
[email protected]9eb7b11b2012-03-28 20:19:3110#include "base/sys_byteorder.h"
[email protected]3cd17242009-06-23 02:59:0211#include "net/base/io_buffer.h"
[email protected]3cd17242009-06-23 02:59:0212#include "net/base/net_util.h"
eroman87c53d62015-04-02 06:51:0713#include "net/log/net_log.h"
[email protected]a796bcec2010-03-22 17:17:2614#include "net/socket/client_socket_handle.h"
[email protected]3cd17242009-06-23 02:59:0215
16namespace net {
17
18// Every SOCKS server requests a user-id from the client. It is optional
19// and we send an empty string.
20static const char kEmptyUserId[] = "";
21
[email protected]3cd17242009-06-23 02:59:0222// For SOCKS4, the client sends 8 bytes plus the size of the user-id.
[email protected]76a51ac82009-06-28 07:58:5823static const unsigned int kWriteHeaderSize = 8;
[email protected]3cd17242009-06-23 02:59:0224
[email protected]034d3892011-03-29 04:07:2125// For SOCKS4 the server sends 8 bytes for acknowledgement.
[email protected]76a51ac82009-06-28 07:58:5826static const unsigned int kReadHeaderSize = 8;
[email protected]3cd17242009-06-23 02:59:0227
28// Server Response codes for SOCKS.
Avi Drissman13fc8932015-12-20 04:40:4629static const uint8_t kServerResponseOk = 0x5A;
30static const uint8_t kServerResponseRejected = 0x5B;
31static const uint8_t kServerResponseNotReachable = 0x5C;
32static const uint8_t kServerResponseMismatchedUserId = 0x5D;
[email protected]3cd17242009-06-23 02:59:0233
Avi Drissman13fc8932015-12-20 04:40:4634static const uint8_t kSOCKSVersion4 = 0x04;
35static const uint8_t kSOCKSStreamRequest = 0x01;
[email protected]3cd17242009-06-23 02:59:0236
[email protected]034d3892011-03-29 04:07:2137// A struct holding the essential details of the SOCKS4 Server Request.
[email protected]3cd17242009-06-23 02:59:0238// The port in the header is stored in network byte order.
39struct SOCKS4ServerRequest {
Avi Drissman13fc8932015-12-20 04:40:4640 uint8_t version;
41 uint8_t command;
42 uint16_t nw_port;
43 uint8_t ip[4];
[email protected]3cd17242009-06-23 02:59:0244};
mostynb91e0da982015-01-20 19:17:2745static_assert(sizeof(SOCKS4ServerRequest) == kWriteHeaderSize,
46 "socks4 server request struct has incorrect size");
[email protected]3cd17242009-06-23 02:59:0247
[email protected]034d3892011-03-29 04:07:2148// A struct holding details of the SOCKS4 Server Response.
[email protected]3cd17242009-06-23 02:59:0249struct SOCKS4ServerResponse {
Avi Drissman13fc8932015-12-20 04:40:4650 uint8_t reserved_null;
51 uint8_t code;
52 uint16_t port;
53 uint8_t ip[4];
[email protected]3cd17242009-06-23 02:59:0254};
mostynb91e0da982015-01-20 19:17:2755static_assert(sizeof(SOCKS4ServerResponse) == kReadHeaderSize,
56 "socks4 server response struct has incorrect size");
[email protected]3cd17242009-06-23 02:59:0257
[email protected]18ccfdb2013-08-15 00:13:4458SOCKSClientSocket::SOCKSClientSocket(
59 scoped_ptr<ClientSocketHandle> transport_socket,
60 const HostResolver::RequestInfo& req_info,
[email protected]5109c1952013-08-20 18:44:1061 RequestPriority priority,
[email protected]18ccfdb2013-08-15 00:13:4462 HostResolver* host_resolver)
63 : transport_(transport_socket.Pass()),
[email protected]3cd17242009-06-23 02:59:0264 next_state_(STATE_NONE),
[email protected]3cd17242009-06-23 02:59:0265 completed_handshake_(false),
66 bytes_sent_(0),
67 bytes_received_(0),
[email protected]0dc88b32014-03-26 20:12:2868 was_ever_used_(false),
[email protected]76a51ac82009-06-28 07:58:5869 host_resolver_(host_resolver),
[email protected]a2006ece2010-04-23 16:44:0270 host_request_info_(req_info),
[email protected]5109c1952013-08-20 18:44:1071 priority_(priority),
72 net_log_(transport_->socket()->NetLog()) {}
[email protected]3cd17242009-06-23 02:59:0273
74SOCKSClientSocket::~SOCKSClientSocket() {
75 Disconnect();
76}
77
[email protected]83039bb2011-12-09 18:43:5578int SOCKSClientSocket::Connect(const CompletionCallback& callback) {
[email protected]3cd17242009-06-23 02:59:0279 DCHECK(transport_.get());
[email protected]a796bcec2010-03-22 17:17:2680 DCHECK(transport_->socket());
[email protected]3cd17242009-06-23 02:59:0281 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:5582 DCHECK(user_callback_.is_null());
[email protected]3cd17242009-06-23 02:59:0283
84 // If already connected, then just return OK.
85 if (completed_handshake_)
86 return OK;
87
88 next_state_ = STATE_RESOLVE_HOST;
[email protected]5a05c47a2009-11-02 23:25:1989
[email protected]3aa4af042012-06-14 21:02:3190 net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT);
[email protected]3cd17242009-06-23 02:59:0291
92 int rv = DoLoop(OK);
[email protected]5a05c47a2009-11-02 23:25:1993 if (rv == ERR_IO_PENDING) {
[email protected]83039bb2011-12-09 18:43:5594 user_callback_ = callback;
[email protected]5a05c47a2009-11-02 23:25:1995 } else {
[email protected]d7fd1782011-02-08 19:16:4396 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
[email protected]5a05c47a2009-11-02 23:25:1997 }
[email protected]3cd17242009-06-23 02:59:0298 return rv;
99}
100
101void SOCKSClientSocket::Disconnect() {
102 completed_handshake_ = false;
[email protected]16a02742010-01-07 22:50:10103 host_resolver_.Cancel();
[email protected]a796bcec2010-03-22 17:17:26104 transport_->socket()->Disconnect();
[email protected]16a02742010-01-07 22:50:10105
106 // Reset other states to make sure they aren't mistakenly used later.
107 // These are the states initialized by Connect().
108 next_state_ = STATE_NONE;
[email protected]dbf036f2011-12-06 23:33:24109 user_callback_.Reset();
[email protected]3cd17242009-06-23 02:59:02110}
111
112bool SOCKSClientSocket::IsConnected() const {
[email protected]a796bcec2010-03-22 17:17:26113 return completed_handshake_ && transport_->socket()->IsConnected();
[email protected]3cd17242009-06-23 02:59:02114}
115
116bool SOCKSClientSocket::IsConnectedAndIdle() const {
[email protected]a796bcec2010-03-22 17:17:26117 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle();
[email protected]3cd17242009-06-23 02:59:02118}
119
[email protected]e4be2dd2010-12-14 00:44:39120const BoundNetLog& SOCKSClientSocket::NetLog() const {
121 return net_log_;
122}
123
[email protected]9b5614a2010-08-25 20:29:45124void SOCKSClientSocket::SetSubresourceSpeculation() {
125 if (transport_.get() && transport_->socket()) {
126 transport_->socket()->SetSubresourceSpeculation();
127 } else {
128 NOTREACHED();
129 }
130}
131
132void SOCKSClientSocket::SetOmniboxSpeculation() {
133 if (transport_.get() && transport_->socket()) {
134 transport_->socket()->SetOmniboxSpeculation();
135 } else {
136 NOTREACHED();
137 }
138}
139
[email protected]0f873e82010-09-02 16:09:01140bool SOCKSClientSocket::WasEverUsed() const {
[email protected]0dc88b32014-03-26 20:12:28141 return was_ever_used_;
[email protected]0f873e82010-09-02 16:09:01142}
143
[email protected]7f7e92392010-10-26 18:29:29144bool SOCKSClientSocket::UsingTCPFastOpen() const {
145 if (transport_.get() && transport_->socket()) {
146 return transport_->socket()->UsingTCPFastOpen();
147 }
148 NOTREACHED();
149 return false;
150}
151
[email protected]2d88e7d2012-07-19 17:55:17152bool SOCKSClientSocket::WasNpnNegotiated() const {
153 if (transport_.get() && transport_->socket()) {
154 return transport_->socket()->WasNpnNegotiated();
155 }
156 NOTREACHED();
157 return false;
158}
159
[email protected]33661e482012-04-03 16:16:26160NextProto SOCKSClientSocket::GetNegotiatedProtocol() const {
161 if (transport_.get() && transport_->socket()) {
162 return transport_->socket()->GetNegotiatedProtocol();
163 }
164 NOTREACHED();
165 return kProtoUnknown;
166}
167
[email protected]2d88e7d2012-07-19 17:55:17168bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
169 if (transport_.get() && transport_->socket()) {
170 return transport_->socket()->GetSSLInfo(ssl_info);
171 }
172 NOTREACHED();
173 return false;
ttuttle23fdb7b2015-05-15 01:28:03174}
[email protected]2d88e7d2012-07-19 17:55:17175
ttuttle23fdb7b2015-05-15 01:28:03176void SOCKSClientSocket::GetConnectionAttempts(ConnectionAttempts* out) const {
177 out->clear();
[email protected]2d88e7d2012-07-19 17:55:17178}
179
tbansalf82cc8e2015-10-14 20:05:49180int64_t SOCKSClientSocket::GetTotalReceivedBytes() const {
181 return transport_->socket()->GetTotalReceivedBytes();
182}
183
[email protected]3cd17242009-06-23 02:59:02184// Read is called by the transport layer above to read. This can only be done
185// if the SOCKS handshake is complete.
186int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len,
[email protected]3f55aa12011-12-07 02:03:33187 const CompletionCallback& callback) {
188 DCHECK(completed_handshake_);
189 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55190 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28191 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02192
[email protected]0dc88b32014-03-26 20:12:28193 int rv = transport_->socket()->Read(
194 buf, buf_len,
195 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
196 base::Unretained(this), callback));
197 if (rv > 0)
198 was_ever_used_ = true;
199 return rv;
[email protected]3cd17242009-06-23 02:59:02200}
201
202// Write is called by the transport layer. This can only be done if the
203// SOCKS handshake is complete.
204int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55205 const CompletionCallback& callback) {
[email protected]3cd17242009-06-23 02:59:02206 DCHECK(completed_handshake_);
207 DCHECK_EQ(STATE_NONE, next_state_);
[email protected]83039bb2011-12-09 18:43:55208 DCHECK(user_callback_.is_null());
[email protected]0dc88b32014-03-26 20:12:28209 DCHECK(!callback.is_null());
[email protected]3cd17242009-06-23 02:59:02210
[email protected]0dc88b32014-03-26 20:12:28211 int rv = transport_->socket()->Write(
212 buf, buf_len,
213 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
214 base::Unretained(this), callback));
215 if (rv > 0)
216 was_ever_used_ = true;
217 return rv;
[email protected]3cd17242009-06-23 02:59:02218}
219
Avi Drissman13fc8932015-12-20 04:40:46220int SOCKSClientSocket::SetReceiveBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26221 return transport_->socket()->SetReceiveBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04222}
223
Avi Drissman13fc8932015-12-20 04:40:46224int SOCKSClientSocket::SetSendBufferSize(int32_t size) {
[email protected]a796bcec2010-03-22 17:17:26225 return transport_->socket()->SetSendBufferSize(size);
[email protected]d3f66572009-09-09 22:38:04226}
227
[email protected]3cd17242009-06-23 02:59:02228void SOCKSClientSocket::DoCallback(int result) {
229 DCHECK_NE(ERR_IO_PENDING, result);
[email protected]83039bb2011-12-09 18:43:55230 DCHECK(!user_callback_.is_null());
[email protected]3cd17242009-06-23 02:59:02231
232 // Since Run() may result in Read being called,
233 // clear user_callback_ up front.
[email protected]83039bb2011-12-09 18:43:55234 DVLOG(1) << "Finished setting up SOCKS handshake";
[email protected]0dc88b32014-03-26 20:12:28235 base::ResetAndReturn(&user_callback_).Run(result);
[email protected]3cd17242009-06-23 02:59:02236}
237
238void SOCKSClientSocket::OnIOComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02239 DCHECK_NE(STATE_NONE, next_state_);
240 int rv = DoLoop(result);
[email protected]5a05c47a2009-11-02 23:25:19241 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43242 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
[email protected]3cd17242009-06-23 02:59:02243 DoCallback(rv);
[email protected]5a05c47a2009-11-02 23:25:19244 }
[email protected]3cd17242009-06-23 02:59:02245}
246
[email protected]0dc88b32014-03-26 20:12:28247void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback& callback,
248 int result) {
249 DCHECK_NE(ERR_IO_PENDING, result);
250 DCHECK(!callback.is_null());
251
252 if (result > 0)
253 was_ever_used_ = true;
254 callback.Run(result);
255}
256
[email protected]3cd17242009-06-23 02:59:02257int SOCKSClientSocket::DoLoop(int last_io_result) {
258 DCHECK_NE(next_state_, STATE_NONE);
259 int rv = last_io_result;
260 do {
261 State state = next_state_;
262 next_state_ = STATE_NONE;
263 switch (state) {
264 case STATE_RESOLVE_HOST:
265 DCHECK_EQ(OK, rv);
266 rv = DoResolveHost();
267 break;
268 case STATE_RESOLVE_HOST_COMPLETE:
269 rv = DoResolveHostComplete(rv);
270 break;
271 case STATE_HANDSHAKE_WRITE:
272 DCHECK_EQ(OK, rv);
273 rv = DoHandshakeWrite();
274 break;
275 case STATE_HANDSHAKE_WRITE_COMPLETE:
276 rv = DoHandshakeWriteComplete(rv);
277 break;
278 case STATE_HANDSHAKE_READ:
279 DCHECK_EQ(OK, rv);
280 rv = DoHandshakeRead();
281 break;
282 case STATE_HANDSHAKE_READ_COMPLETE:
283 rv = DoHandshakeReadComplete(rv);
284 break;
285 default:
286 NOTREACHED() << "bad state";
287 rv = ERR_UNEXPECTED;
288 break;
289 }
290 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
291 return rv;
292}
293
294int SOCKSClientSocket::DoResolveHost() {
[email protected]3cd17242009-06-23 02:59:02295 next_state_ = STATE_RESOLVE_HOST_COMPLETE;
[email protected]034d3892011-03-29 04:07:21296 // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4
297 // addresses for the target host.
298 host_request_info_.set_address_family(ADDRESS_FAMILY_IPV4);
[email protected]ec08bb22009-08-12 00:25:12299 return host_resolver_.Resolve(
[email protected]5109c1952013-08-20 18:44:10300 host_request_info_,
301 priority_,
302 &addresses_,
[email protected]aa22b242011-11-16 18:58:29303 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)),
304 net_log_);
[email protected]3cd17242009-06-23 02:59:02305}
306
307int SOCKSClientSocket::DoResolveHostComplete(int result) {
[email protected]034d3892011-03-29 04:07:21308 if (result != OK) {
309 // Resolving the hostname failed; fail the request rather than automatically
310 // falling back to SOCKS4a (since it can be confusing to see invalid IP
311 // addresses being sent to the SOCKS4 server when it doesn't support 4A.)
312 return result;
[email protected]3cd17242009-06-23 02:59:02313 }
314
[email protected]034d3892011-03-29 04:07:21315 next_state_ = STATE_HANDSHAKE_WRITE;
[email protected]3cd17242009-06-23 02:59:02316 return OK;
317}
318
319// Builds the buffer that is to be sent to the server.
[email protected]76a51ac82009-06-28 07:58:58320const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const {
[email protected]76a51ac82009-06-28 07:58:58321 SOCKS4ServerRequest request;
322 request.version = kSOCKSVersion4;
323 request.command = kSOCKSStreamRequest;
[email protected]9eb7b11b2012-03-28 20:19:31324 request.nw_port = base::HostToNet16(host_request_info_.port());
[email protected]3cd17242009-06-23 02:59:02325
[email protected]7054e78f2012-05-07 21:44:56326 DCHECK(!addresses_.empty());
327 const IPEndPoint& endpoint = addresses_.front();
[email protected]3cd17242009-06-23 02:59:02328
[email protected]034d3892011-03-29 04:07:21329 // We disabled IPv6 results when resolving the hostname, so none of the
330 // results in the list will be IPv6.
331 // TODO(eroman): we only ever use the first address in the list. It would be
332 // more robust to try all the IP addresses we have before
333 // failing the connect attempt.
[email protected]e466aaf2012-12-13 01:46:44334 CHECK_EQ(ADDRESS_FAMILY_IPV4, endpoint.GetFamily());
[email protected]7054e78f2012-05-07 21:44:56335 CHECK_LE(endpoint.address().size(), sizeof(request.ip));
336 memcpy(&request.ip, &endpoint.address()[0], endpoint.address().size());
[email protected]034d3892011-03-29 04:07:21337
[email protected]7054e78f2012-05-07 21:44:56338 DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort();
[email protected]3cd17242009-06-23 02:59:02339
[email protected]76a51ac82009-06-28 07:58:58340 std::string handshake_data(reinterpret_cast<char*>(&request),
341 sizeof(request));
342 handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId));
[email protected]3cd17242009-06-23 02:59:02343
[email protected]76a51ac82009-06-28 07:58:58344 return handshake_data;
[email protected]3cd17242009-06-23 02:59:02345}
346
347// Writes the SOCKS handshake data to the underlying socket connection.
348int SOCKSClientSocket::DoHandshakeWrite() {
349 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE;
350
[email protected]76a51ac82009-06-28 07:58:58351 if (buffer_.empty()) {
352 buffer_ = BuildHandshakeWriteBuffer();
[email protected]3cd17242009-06-23 02:59:02353 bytes_sent_ = 0;
354 }
355
[email protected]76a51ac82009-06-28 07:58:58356 int handshake_buf_len = buffer_.size() - bytes_sent_;
357 DCHECK_GT(handshake_buf_len, 0);
358 handshake_buf_ = new IOBuffer(handshake_buf_len);
359 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_],
360 handshake_buf_len);
[email protected]83039bb2011-12-09 18:43:55361 return transport_->socket()->Write(
[email protected]90499482013-06-01 00:39:50362 handshake_buf_.get(),
363 handshake_buf_len,
[email protected]83039bb2011-12-09 18:43:55364 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
[email protected]3cd17242009-06-23 02:59:02365}
366
367int SOCKSClientSocket::DoHandshakeWriteComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02368 if (result < 0)
369 return result;
370
[email protected]76a51ac82009-06-28 07:58:58371 // We ignore the case when result is 0, since the underlying Write
372 // may return spurious writes while waiting on the socket.
373
[email protected]3cd17242009-06-23 02:59:02374 bytes_sent_ += result;
[email protected]76a51ac82009-06-28 07:58:58375 if (bytes_sent_ == buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02376 next_state_ = STATE_HANDSHAKE_READ;
[email protected]76a51ac82009-06-28 07:58:58377 buffer_.clear();
378 } else if (bytes_sent_ < buffer_.size()) {
[email protected]3cd17242009-06-23 02:59:02379 next_state_ = STATE_HANDSHAKE_WRITE;
380 } else {
381 return ERR_UNEXPECTED;
382 }
383
384 return OK;
385}
386
387int SOCKSClientSocket::DoHandshakeRead() {
[email protected]3cd17242009-06-23 02:59:02388 next_state_ = STATE_HANDSHAKE_READ_COMPLETE;
389
[email protected]76a51ac82009-06-28 07:58:58390 if (buffer_.empty()) {
[email protected]3cd17242009-06-23 02:59:02391 bytes_received_ = 0;
392 }
393
[email protected]76a51ac82009-06-28 07:58:58394 int handshake_buf_len = kReadHeaderSize - bytes_received_;
395 handshake_buf_ = new IOBuffer(handshake_buf_len);
[email protected]90499482013-06-01 00:39:50396 return transport_->socket()->Read(
397 handshake_buf_.get(),
398 handshake_buf_len,
399 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
[email protected]3cd17242009-06-23 02:59:02400}
401
402int SOCKSClientSocket::DoHandshakeReadComplete(int result) {
[email protected]3cd17242009-06-23 02:59:02403 if (result < 0)
404 return result;
[email protected]76a51ac82009-06-28 07:58:58405
406 // The underlying socket closed unexpectedly.
407 if (result == 0)
408 return ERR_CONNECTION_CLOSED;
409
[email protected]d5a309592010-02-05 02:22:52410 if (bytes_received_ + result > kReadHeaderSize) {
[email protected]9e743cd2010-03-16 07:03:53411 // TODO(eroman): Describe failure in NetLog.
[email protected]d5a309592010-02-05 02:22:52412 return ERR_SOCKS_CONNECTION_FAILED;
413 }
[email protected]3cd17242009-06-23 02:59:02414
[email protected]76a51ac82009-06-28 07:58:58415 buffer_.append(handshake_buf_->data(), result);
[email protected]3cd17242009-06-23 02:59:02416 bytes_received_ += result;
[email protected]76a51ac82009-06-28 07:58:58417 if (bytes_received_ < kReadHeaderSize) {
[email protected]3cd17242009-06-23 02:59:02418 next_state_ = STATE_HANDSHAKE_READ;
419 return OK;
420 }
421
[email protected]76a51ac82009-06-28 07:58:58422 const SOCKS4ServerResponse* response =
423 reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data());
[email protected]3cd17242009-06-23 02:59:02424
425 if (response->reserved_null != 0x00) {
426 LOG(ERROR) << "Unknown response from SOCKS server.";
[email protected]d5a309592010-02-05 02:22:52427 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02428 }
429
[email protected]3cd17242009-06-23 02:59:02430 switch (response->code) {
431 case kServerResponseOk:
432 completed_handshake_ = true;
433 return OK;
434 case kServerResponseRejected:
435 LOG(ERROR) << "SOCKS request rejected or failed";
[email protected]d5a309592010-02-05 02:22:52436 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02437 case kServerResponseNotReachable:
438 LOG(ERROR) << "SOCKS request failed because client is not running "
439 << "identd (or not reachable from the server)";
[email protected]d5a309592010-02-05 02:22:52440 return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE;
[email protected]3cd17242009-06-23 02:59:02441 case kServerResponseMismatchedUserId:
442 LOG(ERROR) << "SOCKS request failed because client's identd could "
443 << "not confirm the user ID string in the request";
[email protected]d5a309592010-02-05 02:22:52444 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02445 default:
446 LOG(ERROR) << "SOCKS server sent unknown response";
[email protected]d5a309592010-02-05 02:22:52447 return ERR_SOCKS_CONNECTION_FAILED;
[email protected]3cd17242009-06-23 02:59:02448 }
449
450 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol
451}
452
[email protected]a3528692012-06-08 00:11:42453int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const {
[email protected]a796bcec2010-03-22 17:17:26454 return transport_->socket()->GetPeerAddress(address);
[email protected]3cd17242009-06-23 02:59:02455}
[email protected]3cd17242009-06-23 02:59:02456
[email protected]e7f74da2011-04-19 23:49:35457int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const {
458 return transport_->socket()->GetLocalAddress(address);
459}
460
[email protected]3cd17242009-06-23 02:59:02461} // namespace net