| 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/socks_client_socket.h" | 5 #include "net/socket/socks_client_socket.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_log.h" | 12 #include "net/base/net_log.h" |
| 13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 14 #include "net/base/sys_addrinfo.h" | |
| 15 #include "net/socket/client_socket_handle.h" | 14 #include "net/socket/client_socket_handle.h" |
| 16 | 15 |
| 17 namespace net { | 16 namespace net { |
| 18 | 17 |
| 19 // Every SOCKS server requests a user-id from the client. It is optional | 18 // Every SOCKS server requests a user-id from the client. It is optional |
| 20 // and we send an empty string. | 19 // and we send an empty string. |
| 21 static const char kEmptyUserId[] = ""; | 20 static const char kEmptyUserId[] = ""; |
| 22 | 21 |
| 23 // For SOCKS4, the client sends 8 bytes plus the size of the user-id. | 22 // For SOCKS4, the client sends 8 bytes plus the size of the user-id. |
| 24 static const unsigned int kWriteHeaderSize = 8; | 23 static const unsigned int kWriteHeaderSize = 8; |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 return OK; | 299 return OK; |
| 301 } | 300 } |
| 302 | 301 |
| 303 // Builds the buffer that is to be sent to the server. | 302 // Builds the buffer that is to be sent to the server. |
| 304 const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const { | 303 const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const { |
| 305 SOCKS4ServerRequest request; | 304 SOCKS4ServerRequest request; |
| 306 request.version = kSOCKSVersion4; | 305 request.version = kSOCKSVersion4; |
| 307 request.command = kSOCKSStreamRequest; | 306 request.command = kSOCKSStreamRequest; |
| 308 request.nw_port = base::HostToNet16(host_request_info_.port()); | 307 request.nw_port = base::HostToNet16(host_request_info_.port()); |
| 309 | 308 |
| 310 const struct addrinfo* ai = addresses_.head(); | 309 DCHECK(!addresses_.empty()); |
| 311 DCHECK(ai); | 310 const IPEndPoint& endpoint = addresses_.front(); |
| 312 | 311 |
| 313 // We disabled IPv6 results when resolving the hostname, so none of the | 312 // We disabled IPv6 results when resolving the hostname, so none of the |
| 314 // results in the list will be IPv6. | 313 // results in the list will be IPv6. |
| 315 // TODO(eroman): we only ever use the first address in the list. It would be | 314 // TODO(eroman): we only ever use the first address in the list. It would be |
| 316 // more robust to try all the IP addresses we have before | 315 // more robust to try all the IP addresses we have before |
| 317 // failing the connect attempt. | 316 // failing the connect attempt. |
| 318 CHECK_EQ(AF_INET, ai->ai_addr->sa_family); | 317 CHECK_EQ(AF_INET, endpoint.GetFamily()); |
| 319 struct sockaddr_in* ipv4_host = | 318 CHECK_LE(endpoint.address().size(), sizeof(request.ip)); |
| 320 reinterpret_cast<struct sockaddr_in*>(ai->ai_addr); | 319 memcpy(&request.ip, &endpoint.address()[0], endpoint.address().size()); |
| 321 memcpy(&request.ip, &ipv4_host->sin_addr, sizeof(ipv4_host->sin_addr)); | |
| 322 | 320 |
| 323 DVLOG(1) << "Resolved Host is : " << NetAddressToString(ai); | 321 DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort(); |
| 324 | 322 |
| 325 std::string handshake_data(reinterpret_cast<char*>(&request), | 323 std::string handshake_data(reinterpret_cast<char*>(&request), |
| 326 sizeof(request)); | 324 sizeof(request)); |
| 327 handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId)); | 325 handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId)); |
| 328 | 326 |
| 329 return handshake_data; | 327 return handshake_data; |
| 330 } | 328 } |
| 331 | 329 |
| 332 // Writes the SOCKS handshake data to the underlying socket connection. | 330 // Writes the SOCKS handshake data to the underlying socket connection. |
| 333 int SOCKSClientSocket::DoHandshakeWrite() { | 331 int SOCKSClientSocket::DoHandshakeWrite() { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 433 |
| 436 int SOCKSClientSocket::GetPeerAddress(AddressList* address) const { | 434 int SOCKSClientSocket::GetPeerAddress(AddressList* address) const { |
| 437 return transport_->socket()->GetPeerAddress(address); | 435 return transport_->socket()->GetPeerAddress(address); |
| 438 } | 436 } |
| 439 | 437 |
| 440 int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const { | 438 int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const { |
| 441 return transport_->socket()->GetLocalAddress(address); | 439 return transport_->socket()->GetLocalAddress(address); |
| 442 } | 440 } |
| 443 | 441 |
| 444 } // namespace net | 442 } // namespace net |
| OLD | NEW |