| 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 "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 const char kSOCKS5GreetWriteData[] = { 0x05, 0x01, 0x00 }; // no authentication | 216 const char kSOCKS5GreetWriteData[] = { 0x05, 0x01, 0x00 }; // no authentication |
| 217 const char kSOCKS5GreetReadData[] = { 0x05, 0x00 }; | 217 const char kSOCKS5GreetReadData[] = { 0x05, 0x00 }; |
| 218 | 218 |
| 219 int SOCKS5ClientSocket::DoGreetWrite() { | 219 int SOCKS5ClientSocket::DoGreetWrite() { |
| 220 // Since we only have 1 byte to send the hostname length in, if the | 220 // Since we only have 1 byte to send the hostname length in, if the |
| 221 // URL has a hostname longer than 255 characters we can't send it. | 221 // URL has a hostname longer than 255 characters we can't send it. |
| 222 if (0xFF < host_request_info_.hostname().size()) { | 222 if (0xFF < host_request_info_.hostname().size()) { |
| 223 LoadLog::AddStringLiteral(load_log_, | 223 LoadLog::AddStringLiteral(load_log_, |
| 224 "Failed sending request because hostname is " | 224 "Failed sending request because hostname is " |
| 225 "longer than 255 characters"); | 225 "longer than 255 characters"); |
| 226 return ERR_INVALID_URL; | 226 return ERR_SOCKS_CONNECTION_FAILED; |
| 227 } | 227 } |
| 228 | 228 |
| 229 if (buffer_.empty()) { | 229 if (buffer_.empty()) { |
| 230 buffer_ = std::string(kSOCKS5GreetWriteData, | 230 buffer_ = std::string(kSOCKS5GreetWriteData, |
| 231 arraysize(kSOCKS5GreetWriteData)); | 231 arraysize(kSOCKS5GreetWriteData)); |
| 232 bytes_sent_ = 0; | 232 bytes_sent_ = 0; |
| 233 } | 233 } |
| 234 | 234 |
| 235 next_state_ = STATE_GREET_WRITE_COMPLETE; | 235 next_state_ = STATE_GREET_WRITE_COMPLETE; |
| 236 size_t handshake_buf_len = buffer_.size() - bytes_sent_; | 236 size_t handshake_buf_len = buffer_.size() - bytes_sent_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 259 next_state_ = STATE_GREET_READ_COMPLETE; | 259 next_state_ = STATE_GREET_READ_COMPLETE; |
| 260 size_t handshake_buf_len = kGreetReadHeaderSize - bytes_received_; | 260 size_t handshake_buf_len = kGreetReadHeaderSize - bytes_received_; |
| 261 handshake_buf_ = new IOBuffer(handshake_buf_len); | 261 handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 262 return transport_->Read(handshake_buf_, handshake_buf_len, &io_callback_); | 262 return transport_->Read(handshake_buf_, handshake_buf_len, &io_callback_); |
| 263 } | 263 } |
| 264 | 264 |
| 265 int SOCKS5ClientSocket::DoGreetReadComplete(int result) { | 265 int SOCKS5ClientSocket::DoGreetReadComplete(int result) { |
| 266 if (result < 0) | 266 if (result < 0) |
| 267 return result; | 267 return result; |
| 268 | 268 |
| 269 if (result == 0) | 269 if (result == 0) { |
| 270 return ERR_CONNECTION_CLOSED; // Unexpected socket close | 270 LoadLog::AddStringLiteral( |
| 271 load_log_, "Connection unexpected closed while reading greeting."); |
| 272 return ERR_SOCKS_CONNECTION_FAILED; |
| 273 } |
| 271 | 274 |
| 272 bytes_received_ += result; | 275 bytes_received_ += result; |
| 273 buffer_.append(handshake_buf_->data(), result); | 276 buffer_.append(handshake_buf_->data(), result); |
| 274 if (bytes_received_ < kGreetReadHeaderSize) { | 277 if (bytes_received_ < kGreetReadHeaderSize) { |
| 275 next_state_ = STATE_GREET_READ; | 278 next_state_ = STATE_GREET_READ; |
| 276 return OK; | 279 return OK; |
| 277 } | 280 } |
| 278 | 281 |
| 279 // Got the greet data. | 282 // Got the greet data. |
| 280 if (buffer_[0] != kSOCKS5Version) { | 283 if (buffer_[0] != kSOCKS5Version) { |
| 281 LoadLog::AddStringLiteral(load_log_, "Unexpected SOCKS version"); | 284 LoadLog::AddStringLiteral(load_log_, "Unexpected SOCKS version"); |
| 282 LoadLog::AddString(load_log_, StringPrintf( | 285 LoadLog::AddString(load_log_, StringPrintf( |
| 283 "buffer_[0] = 0x%x", static_cast<int>(buffer_[0]))); | 286 "buffer_[0] = 0x%x", static_cast<int>(buffer_[0]))); |
| 284 return ERR_INVALID_RESPONSE; | 287 return ERR_SOCKS_CONNECTION_FAILED; |
| 285 } | 288 } |
| 286 if (buffer_[1] != 0x00) { | 289 if (buffer_[1] != 0x00) { |
| 287 LoadLog::AddStringLiteral(load_log_, "Unexpected authentication method"); | 290 LoadLog::AddStringLiteral(load_log_, "Unexpected authentication method"); |
| 288 LoadLog::AddString(load_log_, StringPrintf( | 291 LoadLog::AddString(load_log_, StringPrintf( |
| 289 "buffer_[1] = 0x%x", static_cast<int>(buffer_[1]))); | 292 "buffer_[1] = 0x%x", static_cast<int>(buffer_[1]))); |
| 290 return ERR_INVALID_RESPONSE; // Unknown error | 293 return ERR_SOCKS_CONNECTION_FAILED; |
| 291 } | 294 } |
| 292 | 295 |
| 293 buffer_.clear(); | 296 buffer_.clear(); |
| 294 next_state_ = STATE_HANDSHAKE_WRITE; | 297 next_state_ = STATE_HANDSHAKE_WRITE; |
| 295 return OK; | 298 return OK; |
| 296 } | 299 } |
| 297 | 300 |
| 298 int SOCKS5ClientSocket::BuildHandshakeWriteBuffer(std::string* handshake) | 301 int SOCKS5ClientSocket::BuildHandshakeWriteBuffer(std::string* handshake) |
| 299 const { | 302 const { |
| 300 DCHECK(handshake->empty()); | 303 DCHECK(handshake->empty()); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 int handshake_buf_len = read_header_size - bytes_received_; | 370 int handshake_buf_len = read_header_size - bytes_received_; |
| 368 handshake_buf_ = new IOBuffer(handshake_buf_len); | 371 handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 369 return transport_->Read(handshake_buf_, handshake_buf_len, &io_callback_); | 372 return transport_->Read(handshake_buf_, handshake_buf_len, &io_callback_); |
| 370 } | 373 } |
| 371 | 374 |
| 372 int SOCKS5ClientSocket::DoHandshakeReadComplete(int result) { | 375 int SOCKS5ClientSocket::DoHandshakeReadComplete(int result) { |
| 373 if (result < 0) | 376 if (result < 0) |
| 374 return result; | 377 return result; |
| 375 | 378 |
| 376 // The underlying socket closed unexpectedly. | 379 // The underlying socket closed unexpectedly. |
| 377 if (result == 0) | 380 if (result == 0) { |
| 378 return ERR_CONNECTION_CLOSED; | 381 LoadLog::AddStringLiteral( |
| 382 load_log_, "Connection unexpected closed while reading handshake."); |
| 383 return ERR_SOCKS_CONNECTION_FAILED; |
| 384 } |
| 379 | 385 |
| 380 buffer_.append(handshake_buf_->data(), result); | 386 buffer_.append(handshake_buf_->data(), result); |
| 381 bytes_received_ += result; | 387 bytes_received_ += result; |
| 382 | 388 |
| 383 // When the first few bytes are read, check how many more are required | 389 // When the first few bytes are read, check how many more are required |
| 384 // and accordingly increase them | 390 // and accordingly increase them |
| 385 if (bytes_received_ == kReadHeaderSize) { | 391 if (bytes_received_ == kReadHeaderSize) { |
| 386 if (buffer_[0] != kSOCKS5Version || buffer_[2] != kNullByte) { | 392 if (buffer_[0] != kSOCKS5Version || buffer_[2] != kNullByte) { |
| 387 LoadLog::AddStringLiteral(load_log_, "Unexpected SOCKS version."); | 393 LoadLog::AddStringLiteral(load_log_, "Unexpected SOCKS version."); |
| 388 LoadLog::AddString(load_log_, StringPrintf( | 394 LoadLog::AddString(load_log_, StringPrintf( |
| 389 "buffer_[0] = 0x%x; buffer_[2] = 0x%x", | 395 "buffer_[0] = 0x%x; buffer_[2] = 0x%x", |
| 390 static_cast<int>(buffer_[0]), | 396 static_cast<int>(buffer_[0]), |
| 391 static_cast<int>(buffer_[2]))); | 397 static_cast<int>(buffer_[2]))); |
| 392 return ERR_INVALID_RESPONSE; | 398 return ERR_SOCKS_CONNECTION_FAILED; |
| 393 } | 399 } |
| 394 if (buffer_[1] != 0x00) { | 400 if (buffer_[1] != 0x00) { |
| 395 LoadLog::AddStringLiteral(load_log_, | 401 LoadLog::AddStringLiteral(load_log_, |
| 396 "SOCKS server returned a failure code:"); | 402 "SOCKS server returned a failure code:"); |
| 397 const char* error_string = MapSOCKSReplyToErrorString(buffer_[1]); | 403 const char* error_string = MapSOCKSReplyToErrorString(buffer_[1]); |
| 398 if (error_string) { | 404 if (error_string) { |
| 399 LoadLog::AddStringLiteral(load_log_, error_string); | 405 LoadLog::AddStringLiteral(load_log_, error_string); |
| 400 } else { | 406 } else { |
| 401 LoadLog::AddString(load_log_, StringPrintf( | 407 LoadLog::AddString(load_log_, StringPrintf( |
| 402 "buffer_[1] = 0x%x", static_cast<int>(buffer_[1]))); | 408 "buffer_[1] = 0x%x", static_cast<int>(buffer_[1]))); |
| 403 } | 409 } |
| 404 return ERR_FAILED; | 410 return ERR_SOCKS_CONNECTION_FAILED; |
| 405 } | 411 } |
| 406 | 412 |
| 407 // We check the type of IP/Domain the server returns and accordingly | 413 // We check the type of IP/Domain the server returns and accordingly |
| 408 // increase the size of the response. For domains, we need to read the | 414 // increase the size of the response. For domains, we need to read the |
| 409 // size of the domain, so the initial request size is upto the domain | 415 // size of the domain, so the initial request size is upto the domain |
| 410 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is | 416 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is |
| 411 // read, we substract 1 byte from the additional request size. | 417 // read, we substract 1 byte from the additional request size. |
| 412 SocksEndPointAddressType address_type = | 418 SocksEndPointAddressType address_type = |
| 413 static_cast<SocksEndPointAddressType>(buffer_[3]); | 419 static_cast<SocksEndPointAddressType>(buffer_[3]); |
| 414 if (address_type == kEndPointDomain) | 420 if (address_type == kEndPointDomain) |
| 415 read_header_size += static_cast<uint8>(buffer_[4]); | 421 read_header_size += static_cast<uint8>(buffer_[4]); |
| 416 else if (address_type == kEndPointResolvedIPv4) | 422 else if (address_type == kEndPointResolvedIPv4) |
| 417 read_header_size += sizeof(struct in_addr) - 1; | 423 read_header_size += sizeof(struct in_addr) - 1; |
| 418 else if (address_type == kEndPointResolvedIPv6) | 424 else if (address_type == kEndPointResolvedIPv6) |
| 419 read_header_size += sizeof(struct in6_addr) - 1; | 425 read_header_size += sizeof(struct in6_addr) - 1; |
| 420 else { | 426 else { |
| 421 LoadLog::AddStringLiteral(load_log_, "Unknown address type in response"); | 427 LoadLog::AddStringLiteral(load_log_, "Unknown address type in response"); |
| 422 LoadLog::AddString(load_log_, StringPrintf( | 428 LoadLog::AddString(load_log_, StringPrintf( |
| 423 "buffer_[3] = 0x%x", static_cast<int>(buffer_[3]))); | 429 "buffer_[3] = 0x%x", static_cast<int>(buffer_[3]))); |
| 424 return ERR_INVALID_RESPONSE; | 430 return ERR_SOCKS_CONNECTION_FAILED; |
| 425 } | 431 } |
| 426 | 432 |
| 427 read_header_size += 2; // for the port. | 433 read_header_size += 2; // for the port. |
| 428 next_state_ = STATE_HANDSHAKE_READ; | 434 next_state_ = STATE_HANDSHAKE_READ; |
| 429 return OK; | 435 return OK; |
| 430 } | 436 } |
| 431 | 437 |
| 432 // When the final bytes are read, setup handshake. We ignore the rest | 438 // When the final bytes are read, setup handshake. We ignore the rest |
| 433 // of the response since they represent the SOCKSv5 endpoint and have | 439 // of the response since they represent the SOCKSv5 endpoint and have |
| 434 // no use when doing a tunnel connection. | 440 // no use when doing a tunnel connection. |
| 435 if (bytes_received_ == read_header_size) { | 441 if (bytes_received_ == read_header_size) { |
| 436 completed_handshake_ = true; | 442 completed_handshake_ = true; |
| 437 buffer_.clear(); | 443 buffer_.clear(); |
| 438 next_state_ = STATE_NONE; | 444 next_state_ = STATE_NONE; |
| 439 return OK; | 445 return OK; |
| 440 } | 446 } |
| 441 | 447 |
| 442 next_state_ = STATE_HANDSHAKE_READ; | 448 next_state_ = STATE_HANDSHAKE_READ; |
| 443 return OK; | 449 return OK; |
| 444 } | 450 } |
| 445 | 451 |
| 446 int SOCKS5ClientSocket::GetPeerName(struct sockaddr* name, | 452 int SOCKS5ClientSocket::GetPeerName(struct sockaddr* name, |
| 447 socklen_t* namelen) { | 453 socklen_t* namelen) { |
| 448 return transport_->GetPeerName(name, namelen); | 454 return transport_->GetPeerName(name, namelen); |
| 449 } | 455 } |
| 450 | 456 |
| 451 } // namespace net | 457 } // namespace net |
| OLD | NEW |