OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" | |
8 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
9 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
10 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
11 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
12 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
13 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
14 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
15 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
16 #include "net/log/net_log.h" | 15 #include "net/log/net_log.h" |
17 #include "net/socket/client_socket_handle.h" | 16 #include "net/socket/client_socket_handle.h" |
18 | 17 |
19 namespace net { | 18 namespace net { |
20 | 19 |
21 const unsigned int SOCKS5ClientSocket::kGreetReadHeaderSize = 2; | 20 const unsigned int SOCKS5ClientSocket::kGreetReadHeaderSize = 2; |
22 const unsigned int SOCKS5ClientSocket::kWriteHeaderSize = 10; | 21 const unsigned int SOCKS5ClientSocket::kWriteHeaderSize = 10; |
23 const unsigned int SOCKS5ClientSocket::kReadHeaderSize = 5; | 22 const unsigned int SOCKS5ClientSocket::kReadHeaderSize = 5; |
24 const uint8 SOCKS5ClientSocket::kSOCKS5Version = 0x05; | 23 const uint8_t SOCKS5ClientSocket::kSOCKS5Version = 0x05; |
25 const uint8 SOCKS5ClientSocket::kTunnelCommand = 0x01; | 24 const uint8_t SOCKS5ClientSocket::kTunnelCommand = 0x01; |
26 const uint8 SOCKS5ClientSocket::kNullByte = 0x00; | 25 const uint8_t SOCKS5ClientSocket::kNullByte = 0x00; |
27 | 26 |
28 static_assert(sizeof(struct in_addr) == 4, "incorrect system size of IPv4"); | 27 static_assert(sizeof(struct in_addr) == 4, "incorrect system size of IPv4"); |
29 static_assert(sizeof(struct in6_addr) == 16, "incorrect system size of IPv6"); | 28 static_assert(sizeof(struct in6_addr) == 16, "incorrect system size of IPv6"); |
30 | 29 |
31 SOCKS5ClientSocket::SOCKS5ClientSocket( | 30 SOCKS5ClientSocket::SOCKS5ClientSocket( |
32 scoped_ptr<ClientSocketHandle> transport_socket, | 31 scoped_ptr<ClientSocketHandle> transport_socket, |
33 const HostResolver::RequestInfo& req_info) | 32 const HostResolver::RequestInfo& req_info) |
34 : io_callback_(base::Bind(&SOCKS5ClientSocket::OnIOComplete, | 33 : io_callback_(base::Bind(&SOCKS5ClientSocket::OnIOComplete, |
35 base::Unretained(this))), | 34 base::Unretained(this))), |
36 transport_(transport_socket.Pass()), | 35 transport_(transport_socket.Pass()), |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 182 |
184 int rv = transport_->socket()->Write( | 183 int rv = transport_->socket()->Write( |
185 buf, buf_len, | 184 buf, buf_len, |
186 base::Bind(&SOCKS5ClientSocket::OnReadWriteComplete, | 185 base::Bind(&SOCKS5ClientSocket::OnReadWriteComplete, |
187 base::Unretained(this), callback)); | 186 base::Unretained(this), callback)); |
188 if (rv > 0) | 187 if (rv > 0) |
189 was_ever_used_ = true; | 188 was_ever_used_ = true; |
190 return rv; | 189 return rv; |
191 } | 190 } |
192 | 191 |
193 int SOCKS5ClientSocket::SetReceiveBufferSize(int32 size) { | 192 int SOCKS5ClientSocket::SetReceiveBufferSize(int32_t size) { |
194 return transport_->socket()->SetReceiveBufferSize(size); | 193 return transport_->socket()->SetReceiveBufferSize(size); |
195 } | 194 } |
196 | 195 |
197 int SOCKS5ClientSocket::SetSendBufferSize(int32 size) { | 196 int SOCKS5ClientSocket::SetSendBufferSize(int32_t size) { |
198 return transport_->socket()->SetSendBufferSize(size); | 197 return transport_->socket()->SetSendBufferSize(size); |
199 } | 198 } |
200 | 199 |
201 void SOCKS5ClientSocket::DoCallback(int result) { | 200 void SOCKS5ClientSocket::DoCallback(int result) { |
202 DCHECK_NE(ERR_IO_PENDING, result); | 201 DCHECK_NE(ERR_IO_PENDING, result); |
203 DCHECK(!user_callback_.is_null()); | 202 DCHECK(!user_callback_.is_null()); |
204 | 203 |
205 // Since Run() may result in Read being called, | 204 // Since Run() may result in Read being called, |
206 // clear user_callback_ up front. | 205 // clear user_callback_ up front. |
207 base::ResetAndReturn(&user_callback_).Run(result); | 206 base::ResetAndReturn(&user_callback_).Run(result); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 | 370 |
372 handshake->push_back(kEndPointDomain); // The type of the address. | 371 handshake->push_back(kEndPointDomain); // The type of the address. |
373 | 372 |
374 DCHECK_GE(static_cast<size_t>(0xFF), host_request_info_.hostname().size()); | 373 DCHECK_GE(static_cast<size_t>(0xFF), host_request_info_.hostname().size()); |
375 | 374 |
376 // First add the size of the hostname, followed by the hostname. | 375 // First add the size of the hostname, followed by the hostname. |
377 handshake->push_back(static_cast<unsigned char>( | 376 handshake->push_back(static_cast<unsigned char>( |
378 host_request_info_.hostname().size())); | 377 host_request_info_.hostname().size())); |
379 handshake->append(host_request_info_.hostname()); | 378 handshake->append(host_request_info_.hostname()); |
380 | 379 |
381 uint16 nw_port = base::HostToNet16(host_request_info_.port()); | 380 uint16_t nw_port = base::HostToNet16(host_request_info_.port()); |
382 handshake->append(reinterpret_cast<char*>(&nw_port), sizeof(nw_port)); | 381 handshake->append(reinterpret_cast<char*>(&nw_port), sizeof(nw_port)); |
383 return OK; | 382 return OK; |
384 } | 383 } |
385 | 384 |
386 // Writes the SOCKS handshake data to the underlying socket connection. | 385 // Writes the SOCKS handshake data to the underlying socket connection. |
387 int SOCKS5ClientSocket::DoHandshakeWrite() { | 386 int SOCKS5ClientSocket::DoHandshakeWrite() { |
388 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; | 387 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; |
389 | 388 |
390 if (buffer_.empty()) { | 389 if (buffer_.empty()) { |
391 int rv = BuildHandshakeWriteBuffer(&buffer_); | 390 int rv = BuildHandshakeWriteBuffer(&buffer_); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 } | 464 } |
466 | 465 |
467 // We check the type of IP/Domain the server returns and accordingly | 466 // We check the type of IP/Domain the server returns and accordingly |
468 // increase the size of the response. For domains, we need to read the | 467 // increase the size of the response. For domains, we need to read the |
469 // size of the domain, so the initial request size is upto the domain | 468 // size of the domain, so the initial request size is upto the domain |
470 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is | 469 // size. Since for IPv4/IPv6 the size is fixed and hence no 'size' is |
471 // read, we substract 1 byte from the additional request size. | 470 // read, we substract 1 byte from the additional request size. |
472 SocksEndPointAddressType address_type = | 471 SocksEndPointAddressType address_type = |
473 static_cast<SocksEndPointAddressType>(buffer_[3]); | 472 static_cast<SocksEndPointAddressType>(buffer_[3]); |
474 if (address_type == kEndPointDomain) | 473 if (address_type == kEndPointDomain) |
475 read_header_size += static_cast<uint8>(buffer_[4]); | 474 read_header_size += static_cast<uint8_t>(buffer_[4]); |
476 else if (address_type == kEndPointResolvedIPv4) | 475 else if (address_type == kEndPointResolvedIPv4) |
477 read_header_size += sizeof(struct in_addr) - 1; | 476 read_header_size += sizeof(struct in_addr) - 1; |
478 else if (address_type == kEndPointResolvedIPv6) | 477 else if (address_type == kEndPointResolvedIPv6) |
479 read_header_size += sizeof(struct in6_addr) - 1; | 478 read_header_size += sizeof(struct in6_addr) - 1; |
480 else { | 479 else { |
481 net_log_.AddEvent(NetLog::TYPE_SOCKS_UNKNOWN_ADDRESS_TYPE, | 480 net_log_.AddEvent(NetLog::TYPE_SOCKS_UNKNOWN_ADDRESS_TYPE, |
482 NetLog::IntCallback("address_type", buffer_[3])); | 481 NetLog::IntCallback("address_type", buffer_[3])); |
483 return ERR_SOCKS_CONNECTION_FAILED; | 482 return ERR_SOCKS_CONNECTION_FAILED; |
484 } | 483 } |
485 | 484 |
(...skipping 18 matching lines...) Expand all Loading... |
504 | 503 |
505 int SOCKS5ClientSocket::GetPeerAddress(IPEndPoint* address) const { | 504 int SOCKS5ClientSocket::GetPeerAddress(IPEndPoint* address) const { |
506 return transport_->socket()->GetPeerAddress(address); | 505 return transport_->socket()->GetPeerAddress(address); |
507 } | 506 } |
508 | 507 |
509 int SOCKS5ClientSocket::GetLocalAddress(IPEndPoint* address) const { | 508 int SOCKS5ClientSocket::GetLocalAddress(IPEndPoint* address) const { |
510 return transport_->socket()->GetLocalAddress(address); | 509 return transport_->socket()->GetLocalAddress(address); |
511 } | 510 } |
512 | 511 |
513 } // namespace net | 512 } // namespace net |
OLD | NEW |