| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/socks5_client_socket.h" | 5 #include "net/socket/socks5_client_socket.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <ws2tcpip.h> | 10 #include <ws2tcpip.h> |
| 11 #elif defined(OS_POSIX) | 11 #elif defined(OS_POSIX) |
| 12 #include <netdb.h> | 12 #include <netdb.h> |
| 13 #endif | 13 #endif |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/trace_event.h" | 15 #include "base/trace_event.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/base/load_log.h" |
| 17 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 | 21 |
| 21 const unsigned int SOCKS5ClientSocket::kGreetReadHeaderSize = 2; | 22 const unsigned int SOCKS5ClientSocket::kGreetReadHeaderSize = 2; |
| 22 const unsigned int SOCKS5ClientSocket::kWriteHeaderSize = 10; | 23 const unsigned int SOCKS5ClientSocket::kWriteHeaderSize = 10; |
| 23 const unsigned int SOCKS5ClientSocket::kReadHeaderSize = 5; | 24 const unsigned int SOCKS5ClientSocket::kReadHeaderSize = 5; |
| 24 const uint8 SOCKS5ClientSocket::kSOCKS5Version = 0x05; | 25 const uint8 SOCKS5ClientSocket::kSOCKS5Version = 0x05; |
| 25 const uint8 SOCKS5ClientSocket::kTunnelCommand = 0x01; | 26 const uint8 SOCKS5ClientSocket::kTunnelCommand = 0x01; |
| 26 const uint8 SOCKS5ClientSocket::kNullByte = 0x00; | 27 const uint8 SOCKS5ClientSocket::kNullByte = 0x00; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 42 bytes_received_(0), | 43 bytes_received_(0), |
| 43 read_header_size(kReadHeaderSize), | 44 read_header_size(kReadHeaderSize), |
| 44 host_resolver_(host_resolver), | 45 host_resolver_(host_resolver), |
| 45 host_request_info_(req_info) { | 46 host_request_info_(req_info) { |
| 46 } | 47 } |
| 47 | 48 |
| 48 SOCKS5ClientSocket::~SOCKS5ClientSocket() { | 49 SOCKS5ClientSocket::~SOCKS5ClientSocket() { |
| 49 Disconnect(); | 50 Disconnect(); |
| 50 } | 51 } |
| 51 | 52 |
| 52 int SOCKS5ClientSocket::Connect(CompletionCallback* callback) { | 53 int SOCKS5ClientSocket::Connect(CompletionCallback* callback, |
| 54 LoadLog* load_log) { |
| 53 DCHECK(transport_.get()); | 55 DCHECK(transport_.get()); |
| 54 DCHECK(transport_->IsConnected()); | 56 DCHECK(transport_->IsConnected()); |
| 55 DCHECK_EQ(STATE_NONE, next_state_); | 57 DCHECK_EQ(STATE_NONE, next_state_); |
| 56 DCHECK(!user_callback_); | 58 DCHECK(!user_callback_); |
| 57 | 59 |
| 58 // If already connected, then just return OK. | 60 // If already connected, then just return OK. |
| 59 if (completed_handshake_) | 61 if (completed_handshake_) |
| 60 return OK; | 62 return OK; |
| 61 | 63 |
| 62 next_state_ = STATE_RESOLVE_HOST; | 64 next_state_ = STATE_RESOLVE_HOST; |
| 65 load_log_ = load_log; |
| 66 |
| 67 LoadLog::BeginEvent(load_log, LoadLog::TYPE_SOCKS5_CONNECT); |
| 63 | 68 |
| 64 int rv = DoLoop(OK); | 69 int rv = DoLoop(OK); |
| 65 if (rv == ERR_IO_PENDING) | 70 if (rv == ERR_IO_PENDING) { |
| 66 user_callback_ = callback; | 71 user_callback_ = callback; |
| 72 } else { |
| 73 LoadLog::EndEvent(load_log, LoadLog::TYPE_SOCKS5_CONNECT); |
| 74 load_log_ = NULL; |
| 75 } |
| 67 return rv; | 76 return rv; |
| 68 } | 77 } |
| 69 | 78 |
| 70 void SOCKS5ClientSocket::Disconnect() { | 79 void SOCKS5ClientSocket::Disconnect() { |
| 71 completed_handshake_ = false; | 80 completed_handshake_ = false; |
| 72 transport_->Disconnect(); | 81 transport_->Disconnect(); |
| 73 } | 82 } |
| 74 | 83 |
| 75 bool SOCKS5ClientSocket::IsConnected() const { | 84 bool SOCKS5ClientSocket::IsConnected() const { |
| 76 return completed_handshake_ && transport_->IsConnected(); | 85 return completed_handshake_ && transport_->IsConnected(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 // clear user_callback_ up front. | 127 // clear user_callback_ up front. |
| 119 CompletionCallback* c = user_callback_; | 128 CompletionCallback* c = user_callback_; |
| 120 user_callback_ = NULL; | 129 user_callback_ = NULL; |
| 121 DLOG(INFO) << "Finished setting up SOCKSv5 handshake"; | 130 DLOG(INFO) << "Finished setting up SOCKSv5 handshake"; |
| 122 c->Run(result); | 131 c->Run(result); |
| 123 } | 132 } |
| 124 | 133 |
| 125 void SOCKS5ClientSocket::OnIOComplete(int result) { | 134 void SOCKS5ClientSocket::OnIOComplete(int result) { |
| 126 DCHECK_NE(STATE_NONE, next_state_); | 135 DCHECK_NE(STATE_NONE, next_state_); |
| 127 int rv = DoLoop(result); | 136 int rv = DoLoop(result); |
| 128 if (rv != ERR_IO_PENDING) | 137 if (rv != ERR_IO_PENDING) { |
| 138 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKS5_CONNECT); |
| 139 load_log_ = NULL; |
| 129 DoCallback(rv); | 140 DoCallback(rv); |
| 141 } |
| 130 } | 142 } |
| 131 | 143 |
| 132 int SOCKS5ClientSocket::DoLoop(int last_io_result) { | 144 int SOCKS5ClientSocket::DoLoop(int last_io_result) { |
| 133 DCHECK_NE(next_state_, STATE_NONE); | 145 DCHECK_NE(next_state_, STATE_NONE); |
| 134 int rv = last_io_result; | 146 int rv = last_io_result; |
| 135 do { | 147 do { |
| 136 State state = next_state_; | 148 State state = next_state_; |
| 137 next_state_ = STATE_NONE; | 149 next_state_ = STATE_NONE; |
| 138 switch (state) { | 150 switch (state) { |
| 139 case STATE_RESOLVE_HOST: | 151 case STATE_RESOLVE_HOST: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 190 } |
| 179 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 191 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 180 return rv; | 192 return rv; |
| 181 } | 193 } |
| 182 | 194 |
| 183 int SOCKS5ClientSocket::DoResolveHost() { | 195 int SOCKS5ClientSocket::DoResolveHost() { |
| 184 DCHECK_EQ(kEndPointUnresolved, address_type_); | 196 DCHECK_EQ(kEndPointUnresolved, address_type_); |
| 185 | 197 |
| 186 next_state_ = STATE_RESOLVE_HOST_COMPLETE; | 198 next_state_ = STATE_RESOLVE_HOST_COMPLETE; |
| 187 return host_resolver_.Resolve( | 199 return host_resolver_.Resolve( |
| 188 host_request_info_, &addresses_, &io_callback_, NULL); | 200 host_request_info_, &addresses_, &io_callback_, load_log_); |
| 189 } | 201 } |
| 190 | 202 |
| 191 int SOCKS5ClientSocket::DoResolveHostComplete(int result) { | 203 int SOCKS5ClientSocket::DoResolveHostComplete(int result) { |
| 192 DCHECK_EQ(kEndPointUnresolved, address_type_); | 204 DCHECK_EQ(kEndPointUnresolved, address_type_); |
| 193 | 205 |
| 194 bool ok = (result == OK); | 206 bool ok = (result == OK); |
| 195 next_state_ = STATE_GREET_WRITE; | 207 next_state_ = STATE_GREET_WRITE; |
| 196 if (ok) { | 208 if (ok) { |
| 197 DCHECK(addresses_.head()); | 209 DCHECK(addresses_.head()); |
| 198 struct sockaddr* host_info = addresses_.head()->ai_addr; | 210 struct sockaddr* host_info = addresses_.head()->ai_addr; |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 } | 441 } |
| 430 | 442 |
| 431 #if defined(OS_LINUX) | 443 #if defined(OS_LINUX) |
| 432 int SOCKS5ClientSocket::GetPeerName(struct sockaddr* name, | 444 int SOCKS5ClientSocket::GetPeerName(struct sockaddr* name, |
| 433 socklen_t* namelen) { | 445 socklen_t* namelen) { |
| 434 return transport_->GetPeerName(name, namelen); | 446 return transport_->GetPeerName(name, namelen); |
| 435 } | 447 } |
| 436 #endif | 448 #endif |
| 437 | 449 |
| 438 } // namespace net | 450 } // namespace net |
| OLD | NEW |