| 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> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 const unsigned int SOCKS5ClientSocket::kWriteHeaderSize = 10; | 23 const unsigned int SOCKS5ClientSocket::kWriteHeaderSize = 10; |
| 24 const unsigned int SOCKS5ClientSocket::kReadHeaderSize = 5; | 24 const unsigned int SOCKS5ClientSocket::kReadHeaderSize = 5; |
| 25 const uint8 SOCKS5ClientSocket::kSOCKS5Version = 0x05; | 25 const uint8 SOCKS5ClientSocket::kSOCKS5Version = 0x05; |
| 26 const uint8 SOCKS5ClientSocket::kTunnelCommand = 0x01; | 26 const uint8 SOCKS5ClientSocket::kTunnelCommand = 0x01; |
| 27 const uint8 SOCKS5ClientSocket::kNullByte = 0x00; | 27 const uint8 SOCKS5ClientSocket::kNullByte = 0x00; |
| 28 | 28 |
| 29 COMPILE_ASSERT(sizeof(struct in_addr) == 4, incorrect_system_size_of_IPv4); | 29 COMPILE_ASSERT(sizeof(struct in_addr) == 4, incorrect_system_size_of_IPv4); |
| 30 COMPILE_ASSERT(sizeof(struct in6_addr) == 16, incorrect_system_size_of_IPv6); | 30 COMPILE_ASSERT(sizeof(struct in6_addr) == 16, incorrect_system_size_of_IPv6); |
| 31 | 31 |
| 32 SOCKS5ClientSocket::SOCKS5ClientSocket(ClientSocket* transport_socket, | 32 SOCKS5ClientSocket::SOCKS5ClientSocket(ClientSocket* transport_socket, |
| 33 const HostResolver::RequestInfo& req_info, | 33 const HostResolver::RequestInfo& req_info) |
| 34 HostResolver* host_resolver) | |
| 35 : ALLOW_THIS_IN_INITIALIZER_LIST( | 34 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 36 io_callback_(this, &SOCKS5ClientSocket::OnIOComplete)), | 35 io_callback_(this, &SOCKS5ClientSocket::OnIOComplete)), |
| 37 transport_(transport_socket), | 36 transport_(transport_socket), |
| 38 next_state_(STATE_NONE), | 37 next_state_(STATE_NONE), |
| 39 address_type_(kEndPointUnresolved), | |
| 40 user_callback_(NULL), | 38 user_callback_(NULL), |
| 41 completed_handshake_(false), | 39 completed_handshake_(false), |
| 42 bytes_sent_(0), | 40 bytes_sent_(0), |
| 43 bytes_received_(0), | 41 bytes_received_(0), |
| 44 read_header_size(kReadHeaderSize), | 42 read_header_size(kReadHeaderSize), |
| 45 host_resolver_(host_resolver), | |
| 46 host_request_info_(req_info) { | 43 host_request_info_(req_info) { |
| 47 } | 44 } |
| 48 | 45 |
| 49 SOCKS5ClientSocket::~SOCKS5ClientSocket() { | 46 SOCKS5ClientSocket::~SOCKS5ClientSocket() { |
| 50 Disconnect(); | 47 Disconnect(); |
| 51 } | 48 } |
| 52 | 49 |
| 53 int SOCKS5ClientSocket::Connect(CompletionCallback* callback, | 50 int SOCKS5ClientSocket::Connect(CompletionCallback* callback, |
| 54 LoadLog* load_log) { | 51 LoadLog* load_log) { |
| 55 DCHECK(transport_.get()); | 52 DCHECK(transport_.get()); |
| 56 DCHECK(transport_->IsConnected()); | 53 DCHECK(transport_->IsConnected()); |
| 57 DCHECK_EQ(STATE_NONE, next_state_); | 54 DCHECK_EQ(STATE_NONE, next_state_); |
| 58 DCHECK(!user_callback_); | 55 DCHECK(!user_callback_); |
| 59 | 56 |
| 60 // If already connected, then just return OK. | 57 // If already connected, then just return OK. |
| 61 if (completed_handshake_) | 58 if (completed_handshake_) |
| 62 return OK; | 59 return OK; |
| 63 | 60 |
| 64 next_state_ = STATE_RESOLVE_HOST; | |
| 65 load_log_ = load_log; | 61 load_log_ = load_log; |
| 62 LoadLog::BeginEvent(load_log, LoadLog::TYPE_SOCKS5_CONNECT); |
| 66 | 63 |
| 67 LoadLog::BeginEvent(load_log, LoadLog::TYPE_SOCKS5_CONNECT); | 64 next_state_ = STATE_GREET_WRITE; |
| 65 buffer_.clear(); |
| 68 | 66 |
| 69 int rv = DoLoop(OK); | 67 int rv = DoLoop(OK); |
| 70 if (rv == ERR_IO_PENDING) { | 68 if (rv == ERR_IO_PENDING) { |
| 71 user_callback_ = callback; | 69 user_callback_ = callback; |
| 72 } else { | 70 } else { |
| 73 LoadLog::EndEvent(load_log, LoadLog::TYPE_SOCKS5_CONNECT); | 71 LoadLog::EndEvent(load_log, LoadLog::TYPE_SOCKS5_CONNECT); |
| 74 load_log_ = NULL; | 72 load_log_ = NULL; |
| 75 } | 73 } |
| 76 return rv; | 74 return rv; |
| 77 } | 75 } |
| 78 | 76 |
| 79 void SOCKS5ClientSocket::Disconnect() { | 77 void SOCKS5ClientSocket::Disconnect() { |
| 80 completed_handshake_ = false; | 78 completed_handshake_ = false; |
| 81 transport_->Disconnect(); | 79 transport_->Disconnect(); |
| 82 } | 80 } |
| 83 | 81 |
| 84 bool SOCKS5ClientSocket::IsConnected() const { | 82 bool SOCKS5ClientSocket::IsConnected() const { |
| 85 return completed_handshake_ && transport_->IsConnected(); | 83 return completed_handshake_ && transport_->IsConnected(); |
| 86 } | 84 } |
| 87 | 85 |
| 88 bool SOCKS5ClientSocket::IsConnectedAndIdle() const { | 86 bool SOCKS5ClientSocket::IsConnectedAndIdle() const { |
| 89 return completed_handshake_ && transport_->IsConnectedAndIdle(); | 87 return completed_handshake_ && transport_->IsConnectedAndIdle(); |
| 90 } | 88 } |
| 91 | 89 |
| 92 // Read is called by the transport layer above to read. This can only be done | 90 // Read is called by the transport layer above to read. This can only be done |
| 93 // if the SOCKS handshake is complete. | 91 // if the SOCKS handshake is complete. |
| 94 int SOCKS5ClientSocket::Read(IOBuffer* buf, int buf_len, | 92 int SOCKS5ClientSocket::Read(IOBuffer* buf, int buf_len, |
| 95 CompletionCallback* callback) { | 93 CompletionCallback* callback) { |
| 96 DCHECK(completed_handshake_); | 94 DCHECK(completed_handshake_); |
| 97 DCHECK_EQ(STATE_NONE, next_state_); | 95 DCHECK_EQ(STATE_NONE, next_state_); |
| 98 DCHECK(!user_callback_); | 96 DCHECK(!user_callback_); |
| 99 | 97 |
| 100 return transport_->Read(buf, buf_len, callback); | 98 return transport_->Read(buf, buf_len, callback); |
| 101 } | 99 } |
| 102 | 100 |
| 103 // Write is called by the transport layer. This can only be done if the | 101 // Write is called by the transport layer. This can only be done if the |
| 104 // SOCKS handshake is complete. | 102 // SOCKS handshake is complete. |
| 105 int SOCKS5ClientSocket::Write(IOBuffer* buf, int buf_len, | 103 int SOCKS5ClientSocket::Write(IOBuffer* buf, int buf_len, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 } | 139 } |
| 142 } | 140 } |
| 143 | 141 |
| 144 int SOCKS5ClientSocket::DoLoop(int last_io_result) { | 142 int SOCKS5ClientSocket::DoLoop(int last_io_result) { |
| 145 DCHECK_NE(next_state_, STATE_NONE); | 143 DCHECK_NE(next_state_, STATE_NONE); |
| 146 int rv = last_io_result; | 144 int rv = last_io_result; |
| 147 do { | 145 do { |
| 148 State state = next_state_; | 146 State state = next_state_; |
| 149 next_state_ = STATE_NONE; | 147 next_state_ = STATE_NONE; |
| 150 switch (state) { | 148 switch (state) { |
| 151 case STATE_RESOLVE_HOST: | |
| 152 DCHECK_EQ(OK, rv); | |
| 153 rv = DoResolveHost(); | |
| 154 break; | |
| 155 case STATE_RESOLVE_HOST_COMPLETE: | |
| 156 rv = DoResolveHostComplete(rv); | |
| 157 break; | |
| 158 case STATE_GREET_WRITE: | 149 case STATE_GREET_WRITE: |
| 159 DCHECK_EQ(OK, rv); | 150 DCHECK_EQ(OK, rv); |
| 160 rv = DoGreetWrite(); | 151 rv = DoGreetWrite(); |
| 161 break; | 152 break; |
| 162 case STATE_GREET_WRITE_COMPLETE: | 153 case STATE_GREET_WRITE_COMPLETE: |
| 163 rv = DoGreetWriteComplete(rv); | 154 rv = DoGreetWriteComplete(rv); |
| 164 break; | 155 break; |
| 165 case STATE_GREET_READ: | 156 case STATE_GREET_READ: |
| 166 DCHECK_EQ(OK, rv); | 157 DCHECK_EQ(OK, rv); |
| 167 rv = DoGreetRead(); | 158 rv = DoGreetRead(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 185 break; | 176 break; |
| 186 default: | 177 default: |
| 187 NOTREACHED() << "bad state"; | 178 NOTREACHED() << "bad state"; |
| 188 rv = ERR_UNEXPECTED; | 179 rv = ERR_UNEXPECTED; |
| 189 break; | 180 break; |
| 190 } | 181 } |
| 191 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 182 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 192 return rv; | 183 return rv; |
| 193 } | 184 } |
| 194 | 185 |
| 195 int SOCKS5ClientSocket::DoResolveHost() { | |
| 196 DCHECK_EQ(kEndPointUnresolved, address_type_); | |
| 197 | |
| 198 next_state_ = STATE_RESOLVE_HOST_COMPLETE; | |
| 199 return host_resolver_.Resolve( | |
| 200 host_request_info_, &addresses_, &io_callback_, load_log_); | |
| 201 } | |
| 202 | |
| 203 int SOCKS5ClientSocket::DoResolveHostComplete(int result) { | |
| 204 DCHECK_EQ(kEndPointUnresolved, address_type_); | |
| 205 | |
| 206 bool ok = (result == OK); | |
| 207 next_state_ = STATE_GREET_WRITE; | |
| 208 if (ok) { | |
| 209 DCHECK(addresses_.head()); | |
| 210 struct sockaddr* host_info = addresses_.head()->ai_addr; | |
| 211 if (host_info->sa_family == AF_INET) { | |
| 212 address_type_ = kEndPointResolvedIPv4; | |
| 213 } else if (host_info->sa_family == AF_INET6) { | |
| 214 address_type_ = kEndPointResolvedIPv6; | |
| 215 } | |
| 216 } else { | |
| 217 address_type_ = kEndPointFailedDomain; | |
| 218 } | |
| 219 | |
| 220 buffer_.clear(); | |
| 221 | |
| 222 // Even if DNS resolution fails, we send OK since the server | |
| 223 // resolves the domain. | |
| 224 return OK; | |
| 225 } | |
| 226 | |
| 227 const char kSOCKS5GreetWriteData[] = { 0x05, 0x01, 0x00 }; // no authentication | 186 const char kSOCKS5GreetWriteData[] = { 0x05, 0x01, 0x00 }; // no authentication |
| 228 const char kSOCKS5GreetReadData[] = { 0x05, 0x00 }; | 187 const char kSOCKS5GreetReadData[] = { 0x05, 0x00 }; |
| 229 | 188 |
| 230 int SOCKS5ClientSocket::DoGreetWrite() { | 189 int SOCKS5ClientSocket::DoGreetWrite() { |
| 190 // Since we only have 1 byte to send the hostname length in, if the |
| 191 // URL has a hostname longer than 255 characters we can't send it. |
| 192 if (0xFF < host_request_info_.hostname().size()) |
| 193 return ERR_INVALID_URL; |
| 194 |
| 231 if (buffer_.empty()) { | 195 if (buffer_.empty()) { |
| 232 buffer_ = std::string(kSOCKS5GreetWriteData, | 196 buffer_ = std::string(kSOCKS5GreetWriteData, |
| 233 arraysize(kSOCKS5GreetWriteData)); | 197 arraysize(kSOCKS5GreetWriteData)); |
| 234 bytes_sent_ = 0; | 198 bytes_sent_ = 0; |
| 235 } | 199 } |
| 236 | 200 |
| 237 next_state_ = STATE_GREET_WRITE_COMPLETE; | 201 next_state_ = STATE_GREET_WRITE_COMPLETE; |
| 238 size_t handshake_buf_len = buffer_.size() - bytes_sent_; | 202 size_t handshake_buf_len = buffer_.size() - bytes_sent_; |
| 239 handshake_buf_ = new IOBuffer(handshake_buf_len); | 203 handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 240 memcpy(handshake_buf_->data(), &buffer_.data()[bytes_sent_], | 204 memcpy(handshake_buf_->data(), &buffer_.data()[bytes_sent_], |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 if (buffer_[0] != kSOCKS5Version || buffer_[1] != 0x00) | 246 if (buffer_[0] != kSOCKS5Version || buffer_[1] != 0x00) |
| 283 return ERR_INVALID_RESPONSE; // Unknown error | 247 return ERR_INVALID_RESPONSE; // Unknown error |
| 284 | 248 |
| 285 buffer_.clear(); | 249 buffer_.clear(); |
| 286 next_state_ = STATE_HANDSHAKE_WRITE; | 250 next_state_ = STATE_HANDSHAKE_WRITE; |
| 287 return OK; | 251 return OK; |
| 288 } | 252 } |
| 289 | 253 |
| 290 int SOCKS5ClientSocket::BuildHandshakeWriteBuffer(std::string* handshake) | 254 int SOCKS5ClientSocket::BuildHandshakeWriteBuffer(std::string* handshake) |
| 291 const { | 255 const { |
| 292 DCHECK_NE(kEndPointUnresolved, address_type_); | |
| 293 DCHECK(handshake->empty()); | 256 DCHECK(handshake->empty()); |
| 294 | 257 |
| 295 handshake->push_back(kSOCKS5Version); | 258 handshake->push_back(kSOCKS5Version); |
| 296 handshake->push_back(kTunnelCommand); // Connect command | 259 handshake->push_back(kTunnelCommand); // Connect command |
| 297 handshake->push_back(kNullByte); // Reserved null | 260 handshake->push_back(kNullByte); // Reserved null |
| 298 | 261 |
| 299 handshake->push_back(address_type_); // The type of connection | 262 handshake->push_back(kEndPointDomain); // The type of the address. |
| 300 if (address_type_ == kEndPointFailedDomain) { | |
| 301 if(256 <= host_request_info_.hostname().size()) | |
| 302 return ERR_ADDRESS_INVALID; | |
| 303 | 263 |
| 304 // First add the size of the hostname, followed by the hostname. | 264 DCHECK_GE(static_cast<size_t>(0xFF), host_request_info_.hostname().size()); |
| 305 handshake->push_back(static_cast<unsigned char>( | |
| 306 host_request_info_.hostname().size())); | |
| 307 handshake->append(host_request_info_.hostname()); | |
| 308 | 265 |
| 309 } else if (address_type_ == kEndPointResolvedIPv4) { | 266 // First add the size of the hostname, followed by the hostname. |
| 310 struct sockaddr_in* ipv4_host = | 267 handshake->push_back(static_cast<unsigned char>( |
| 311 reinterpret_cast<struct sockaddr_in*>(addresses_.head()->ai_addr); | 268 host_request_info_.hostname().size())); |
| 312 handshake->append(reinterpret_cast<char*>(&ipv4_host->sin_addr), | 269 handshake->append(host_request_info_.hostname()); |
| 313 sizeof(ipv4_host->sin_addr)); | |
| 314 | |
| 315 } else if (address_type_ == kEndPointResolvedIPv6) { | |
| 316 struct sockaddr_in6* ipv6_host = | |
| 317 reinterpret_cast<struct sockaddr_in6*>(addresses_.head()->ai_addr); | |
| 318 handshake->append(reinterpret_cast<char*>(&ipv6_host->sin6_addr), | |
| 319 sizeof(ipv6_host->sin6_addr)); | |
| 320 | |
| 321 } else { | |
| 322 NOTREACHED(); | |
| 323 } | |
| 324 | 270 |
| 325 uint16 nw_port = htons(host_request_info_.port()); | 271 uint16 nw_port = htons(host_request_info_.port()); |
| 326 handshake->append(reinterpret_cast<char*>(&nw_port), sizeof(nw_port)); | 272 handshake->append(reinterpret_cast<char*>(&nw_port), sizeof(nw_port)); |
| 327 return OK; | 273 return OK; |
| 328 } | 274 } |
| 329 | 275 |
| 330 // Writes the SOCKS handshake data to the underlying socket connection. | 276 // Writes the SOCKS handshake data to the underlying socket connection. |
| 331 int SOCKS5ClientSocket::DoHandshakeWrite() { | 277 int SOCKS5ClientSocket::DoHandshakeWrite() { |
| 332 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; | 278 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; |
| 333 | 279 |
| 334 if (buffer_.empty()) { | 280 if (buffer_.empty()) { |
| 335 int rv = BuildHandshakeWriteBuffer(&buffer_); | 281 int rv = BuildHandshakeWriteBuffer(&buffer_); |
| 336 if (rv != OK) | 282 if (rv != OK) |
| 337 return rv; | 283 return rv; |
| 338 bytes_sent_ = 0; | 284 bytes_sent_ = 0; |
| 339 } | 285 } |
| 340 | 286 |
| 341 int handshake_buf_len = buffer_.size() - bytes_sent_; | 287 int handshake_buf_len = buffer_.size() - bytes_sent_; |
| 342 DCHECK_LT(0, handshake_buf_len); | 288 DCHECK_LT(0, handshake_buf_len); |
| 343 handshake_buf_ = new IOBuffer(handshake_buf_len); | 289 handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 344 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], | 290 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], |
| 345 handshake_buf_len); | 291 handshake_buf_len); |
| 346 return transport_->Write(handshake_buf_, handshake_buf_len, &io_callback_); | 292 return transport_->Write(handshake_buf_, handshake_buf_len, &io_callback_); |
| 347 } | 293 } |
| 348 | 294 |
| 349 int SOCKS5ClientSocket::DoHandshakeWriteComplete(int result) { | 295 int SOCKS5ClientSocket::DoHandshakeWriteComplete(int result) { |
| 350 DCHECK_NE(kEndPointUnresolved, address_type_); | |
| 351 | |
| 352 if (result < 0) | 296 if (result < 0) |
| 353 return result; | 297 return result; |
| 354 | 298 |
| 355 // We ignore the case when result is 0, since the underlying Write | 299 // We ignore the case when result is 0, since the underlying Write |
| 356 // may return spurious writes while waiting on the socket. | 300 // may return spurious writes while waiting on the socket. |
| 357 | 301 |
| 358 bytes_sent_ += result; | 302 bytes_sent_ += result; |
| 359 if (bytes_sent_ == buffer_.size()) { | 303 if (bytes_sent_ == buffer_.size()) { |
| 360 next_state_ = STATE_HANDSHAKE_READ; | 304 next_state_ = STATE_HANDSHAKE_READ; |
| 361 buffer_.clear(); | 305 buffer_.clear(); |
| 362 } else if (bytes_sent_ < buffer_.size()) { | 306 } else if (bytes_sent_ < buffer_.size()) { |
| 363 next_state_ = STATE_HANDSHAKE_WRITE; | 307 next_state_ = STATE_HANDSHAKE_WRITE; |
| 364 } else { | 308 } else { |
| 365 NOTREACHED(); | 309 NOTREACHED(); |
| 366 } | 310 } |
| 367 | 311 |
| 368 return OK; | 312 return OK; |
| 369 } | 313 } |
| 370 | 314 |
| 371 int SOCKS5ClientSocket::DoHandshakeRead() { | 315 int SOCKS5ClientSocket::DoHandshakeRead() { |
| 372 DCHECK_NE(kEndPointUnresolved, address_type_); | |
| 373 | |
| 374 next_state_ = STATE_HANDSHAKE_READ_COMPLETE; | 316 next_state_ = STATE_HANDSHAKE_READ_COMPLETE; |
| 375 | 317 |
| 376 if (buffer_.empty()) { | 318 if (buffer_.empty()) { |
| 377 bytes_received_ = 0; | 319 bytes_received_ = 0; |
| 378 read_header_size = kReadHeaderSize; | 320 read_header_size = kReadHeaderSize; |
| 379 } | 321 } |
| 380 | 322 |
| 381 int handshake_buf_len = read_header_size - bytes_received_; | 323 int handshake_buf_len = read_header_size - bytes_received_; |
| 382 handshake_buf_ = new IOBuffer(handshake_buf_len); | 324 handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 383 return transport_->Read(handshake_buf_, handshake_buf_len, &io_callback_); | 325 return transport_->Read(handshake_buf_, handshake_buf_len, &io_callback_); |
| 384 } | 326 } |
| 385 | 327 |
| 386 int SOCKS5ClientSocket::DoHandshakeReadComplete(int result) { | 328 int SOCKS5ClientSocket::DoHandshakeReadComplete(int result) { |
| 387 DCHECK_NE(kEndPointUnresolved, address_type_); | |
| 388 | |
| 389 if (result < 0) | 329 if (result < 0) |
| 390 return result; | 330 return result; |
| 391 | 331 |
| 392 // The underlying socket closed unexpectedly. | 332 // The underlying socket closed unexpectedly. |
| 393 if (result == 0) | 333 if (result == 0) |
| 394 return ERR_CONNECTION_CLOSED; | 334 return ERR_CONNECTION_CLOSED; |
| 395 | 335 |
| 396 buffer_.append(handshake_buf_->data(), result); | 336 buffer_.append(handshake_buf_->data(), result); |
| 397 bytes_received_ += result; | 337 bytes_received_ += result; |
| 398 | 338 |
| 399 // When the first few bytes are read, check how many more are required | 339 // When the first few bytes are read, check how many more are required |
| 400 // and accordingly increase them | 340 // and accordingly increase them |
| 401 if (bytes_received_ == kReadHeaderSize) { | 341 if (bytes_received_ == kReadHeaderSize) { |
| 402 // TODO(arindam): add error codes to net/error_list.h | 342 // TODO(arindam): add error codes to net/error_list.h |
| 403 if (buffer_[0] != kSOCKS5Version || buffer_[2] != kNullByte) | 343 if (buffer_[0] != kSOCKS5Version || buffer_[2] != kNullByte) |
| 404 return ERR_INVALID_RESPONSE; | 344 return ERR_INVALID_RESPONSE; |
| 405 if (buffer_[1] != 0x00) | 345 if (buffer_[1] != 0x00) |
| 406 return ERR_FAILED; | 346 return ERR_FAILED; |
| 407 | 347 |
| 408 // We check the type of IP/Domain the server returns and accordingly | 348 // We check the type of IP/Domain the server returns and accordingly |
| 409 // increase the size of the response. For domains, we need to read the | 349 // increase the size of the response. For domains, we need to read the |
| 410 // size of the domain, so the initial request size is upto the domain | 350 // size of the domain, so the initial request size is upto the domain |
| 411 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is | 351 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is |
| 412 // read, we substract 1 byte from the additional request size. | 352 // read, we substract 1 byte from the additional request size. |
| 413 SocksEndPointAddressType address_type = | 353 SocksEndPointAddressType address_type = |
| 414 static_cast<SocksEndPointAddressType>(buffer_[3]); | 354 static_cast<SocksEndPointAddressType>(buffer_[3]); |
| 415 if (address_type == kEndPointFailedDomain) | 355 if (address_type == kEndPointDomain) |
| 416 read_header_size += static_cast<uint8>(buffer_[4]); | 356 read_header_size += static_cast<uint8>(buffer_[4]); |
| 417 else if (address_type == kEndPointResolvedIPv4) | 357 else if (address_type == kEndPointResolvedIPv4) |
| 418 read_header_size += sizeof(struct in_addr) - 1; | 358 read_header_size += sizeof(struct in_addr) - 1; |
| 419 else if (address_type == kEndPointResolvedIPv6) | 359 else if (address_type == kEndPointResolvedIPv6) |
| 420 read_header_size += sizeof(struct in6_addr) - 1; | 360 read_header_size += sizeof(struct in6_addr) - 1; |
| 421 else | 361 else |
| 422 return ERR_INVALID_RESPONSE; | 362 return ERR_INVALID_RESPONSE; |
| 423 | 363 |
| 424 read_header_size += 2; // for the port. | 364 read_header_size += 2; // for the port. |
| 425 next_state_ = STATE_HANDSHAKE_READ; | 365 next_state_ = STATE_HANDSHAKE_READ; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 441 } | 381 } |
| 442 | 382 |
| 443 #if defined(OS_LINUX) | 383 #if defined(OS_LINUX) |
| 444 int SOCKS5ClientSocket::GetPeerName(struct sockaddr* name, | 384 int SOCKS5ClientSocket::GetPeerName(struct sockaddr* name, |
| 445 socklen_t* namelen) { | 385 socklen_t* namelen) { |
| 446 return transport_->GetPeerName(name, namelen); | 386 return transport_->GetPeerName(name, namelen); |
| 447 } | 387 } |
| 448 #endif | 388 #endif |
| 449 | 389 |
| 450 } // namespace net | 390 } // namespace net |
| OLD | NEW |