OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 | 301 |
302 bytes_received_ += result; | 302 bytes_received_ += result; |
303 buffer_.append(handshake_buf_->data(), result); | 303 buffer_.append(handshake_buf_->data(), result); |
304 if (bytes_received_ < kGreetReadHeaderSize) { | 304 if (bytes_received_ < kGreetReadHeaderSize) { |
305 next_state_ = STATE_GREET_READ; | 305 next_state_ = STATE_GREET_READ; |
306 return OK; | 306 return OK; |
307 } | 307 } |
308 | 308 |
309 // Got the greet data. | 309 // Got the greet data. |
310 if (buffer_[0] != kSOCKS5Version) { | 310 if (buffer_[0] != kSOCKS5Version) { |
311 net_log_.AddEvent(NetLog::TYPE_SOCKS_UNEXPECTED_VERSION, | 311 net_log_.AddEvent( |
312 new NetLogIntegerParameter("version", buffer_[0])); | 312 NetLog::TYPE_SOCKS_UNEXPECTED_VERSION, |
| 313 make_scoped_refptr(new NetLogIntegerParameter("version", buffer_[0]))); |
313 return ERR_SOCKS_CONNECTION_FAILED; | 314 return ERR_SOCKS_CONNECTION_FAILED; |
314 } | 315 } |
315 if (buffer_[1] != 0x00) { | 316 if (buffer_[1] != 0x00) { |
316 net_log_.AddEvent(NetLog::TYPE_SOCKS_UNEXPECTED_AUTH, | 317 net_log_.AddEvent( |
317 new NetLogIntegerParameter("method", buffer_[1])); | 318 NetLog::TYPE_SOCKS_UNEXPECTED_AUTH, |
| 319 make_scoped_refptr(new NetLogIntegerParameter("method", buffer_[1]))); |
318 return ERR_SOCKS_CONNECTION_FAILED; | 320 return ERR_SOCKS_CONNECTION_FAILED; |
319 } | 321 } |
320 | 322 |
321 buffer_.clear(); | 323 buffer_.clear(); |
322 next_state_ = STATE_HANDSHAKE_WRITE; | 324 next_state_ = STATE_HANDSHAKE_WRITE; |
323 return OK; | 325 return OK; |
324 } | 326 } |
325 | 327 |
326 int SOCKS5ClientSocket::BuildHandshakeWriteBuffer(std::string* handshake) | 328 int SOCKS5ClientSocket::BuildHandshakeWriteBuffer(std::string* handshake) |
327 const { | 329 const { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 return ERR_SOCKS_CONNECTION_FAILED; | 412 return ERR_SOCKS_CONNECTION_FAILED; |
411 } | 413 } |
412 | 414 |
413 buffer_.append(handshake_buf_->data(), result); | 415 buffer_.append(handshake_buf_->data(), result); |
414 bytes_received_ += result; | 416 bytes_received_ += result; |
415 | 417 |
416 // When the first few bytes are read, check how many more are required | 418 // When the first few bytes are read, check how many more are required |
417 // and accordingly increase them | 419 // and accordingly increase them |
418 if (bytes_received_ == kReadHeaderSize) { | 420 if (bytes_received_ == kReadHeaderSize) { |
419 if (buffer_[0] != kSOCKS5Version || buffer_[2] != kNullByte) { | 421 if (buffer_[0] != kSOCKS5Version || buffer_[2] != kNullByte) { |
420 net_log_.AddEvent(NetLog::TYPE_SOCKS_UNEXPECTED_VERSION, | 422 net_log_.AddEvent( |
421 new NetLogIntegerParameter("version", buffer_[0])); | 423 NetLog::TYPE_SOCKS_UNEXPECTED_VERSION, |
| 424 make_scoped_refptr( |
| 425 new NetLogIntegerParameter("version", buffer_[0]))); |
422 return ERR_SOCKS_CONNECTION_FAILED; | 426 return ERR_SOCKS_CONNECTION_FAILED; |
423 } | 427 } |
424 if (buffer_[1] != 0x00) { | 428 if (buffer_[1] != 0x00) { |
425 net_log_.AddEvent(NetLog::TYPE_SOCKS_SERVER_ERROR, | 429 net_log_.AddEvent( |
426 new NetLogIntegerParameter("error_code", buffer_[1])); | 430 NetLog::TYPE_SOCKS_SERVER_ERROR, |
| 431 make_scoped_refptr( |
| 432 new NetLogIntegerParameter("error_code", buffer_[1]))); |
427 return ERR_SOCKS_CONNECTION_FAILED; | 433 return ERR_SOCKS_CONNECTION_FAILED; |
428 } | 434 } |
429 | 435 |
430 // We check the type of IP/Domain the server returns and accordingly | 436 // We check the type of IP/Domain the server returns and accordingly |
431 // increase the size of the response. For domains, we need to read the | 437 // increase the size of the response. For domains, we need to read the |
432 // size of the domain, so the initial request size is upto the domain | 438 // size of the domain, so the initial request size is upto the domain |
433 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is | 439 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is |
434 // read, we substract 1 byte from the additional request size. | 440 // read, we substract 1 byte from the additional request size. |
435 SocksEndPointAddressType address_type = | 441 SocksEndPointAddressType address_type = |
436 static_cast<SocksEndPointAddressType>(buffer_[3]); | 442 static_cast<SocksEndPointAddressType>(buffer_[3]); |
437 if (address_type == kEndPointDomain) | 443 if (address_type == kEndPointDomain) |
438 read_header_size += static_cast<uint8>(buffer_[4]); | 444 read_header_size += static_cast<uint8>(buffer_[4]); |
439 else if (address_type == kEndPointResolvedIPv4) | 445 else if (address_type == kEndPointResolvedIPv4) |
440 read_header_size += sizeof(struct in_addr) - 1; | 446 read_header_size += sizeof(struct in_addr) - 1; |
441 else if (address_type == kEndPointResolvedIPv6) | 447 else if (address_type == kEndPointResolvedIPv6) |
442 read_header_size += sizeof(struct in6_addr) - 1; | 448 read_header_size += sizeof(struct in6_addr) - 1; |
443 else { | 449 else { |
444 net_log_.AddEvent(NetLog::TYPE_SOCKS_UNKNOWN_ADDRESS_TYPE, | 450 net_log_.AddEvent( |
445 new NetLogIntegerParameter("address_type", buffer_[3])); | 451 NetLog::TYPE_SOCKS_UNKNOWN_ADDRESS_TYPE, |
| 452 make_scoped_refptr( |
| 453 new NetLogIntegerParameter("address_type", buffer_[3]))); |
446 return ERR_SOCKS_CONNECTION_FAILED; | 454 return ERR_SOCKS_CONNECTION_FAILED; |
447 } | 455 } |
448 | 456 |
449 read_header_size += 2; // for the port. | 457 read_header_size += 2; // for the port. |
450 next_state_ = STATE_HANDSHAKE_READ; | 458 next_state_ = STATE_HANDSHAKE_READ; |
451 return OK; | 459 return OK; |
452 } | 460 } |
453 | 461 |
454 // When the final bytes are read, setup handshake. We ignore the rest | 462 // When the final bytes are read, setup handshake. We ignore the rest |
455 // of the response since they represent the SOCKSv5 endpoint and have | 463 // of the response since they represent the SOCKSv5 endpoint and have |
456 // no use when doing a tunnel connection. | 464 // no use when doing a tunnel connection. |
457 if (bytes_received_ == read_header_size) { | 465 if (bytes_received_ == read_header_size) { |
458 completed_handshake_ = true; | 466 completed_handshake_ = true; |
459 buffer_.clear(); | 467 buffer_.clear(); |
460 next_state_ = STATE_NONE; | 468 next_state_ = STATE_NONE; |
461 return OK; | 469 return OK; |
462 } | 470 } |
463 | 471 |
464 next_state_ = STATE_HANDSHAKE_READ; | 472 next_state_ = STATE_HANDSHAKE_READ; |
465 return OK; | 473 return OK; |
466 } | 474 } |
467 | 475 |
468 int SOCKS5ClientSocket::GetPeerAddress(AddressList* address) const { | 476 int SOCKS5ClientSocket::GetPeerAddress(AddressList* address) const { |
469 return transport_->socket()->GetPeerAddress(address); | 477 return transport_->socket()->GetPeerAddress(address); |
470 } | 478 } |
471 | 479 |
472 } // namespace net | 480 } // namespace net |
OLD | NEW |