OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_BASE_SOCKS_CLIENT_SOCKET_H_ |
| 6 #define NET_BASE_SOCKS_CLIENT_SOCKET_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/ref_counted.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/base/address_list.h" |
| 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/host_resolver.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/socket/client_socket.h" |
| 19 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 20 |
| 21 namespace net { |
| 22 |
| 23 // The SOCKS client socket implementation |
| 24 class SOCKSClientSocket : public ClientSocket { |
| 25 public: |
| 26 // Takes ownership of the |transport_socket|, which should already be |
| 27 // connected by the time Connect() is called. |
| 28 // |
| 29 // |req_info| contains the hostname and port to which the socket above will |
| 30 // communicate to via the socks layer. For testing the referrer is optional. |
| 31 SOCKSClientSocket(ClientSocket* transport_socket, |
| 32 const HostResolver::RequestInfo& req_info, |
| 33 HostResolver* host_resolver); |
| 34 |
| 35 // On destruction Disconnect() is called. |
| 36 virtual ~SOCKSClientSocket(); |
| 37 |
| 38 // ClientSocket methods: |
| 39 |
| 40 // Does the SOCKS handshake and completes the protocol. |
| 41 virtual int Connect(CompletionCallback* callback); |
| 42 virtual void Disconnect(); |
| 43 virtual bool IsConnected() const; |
| 44 virtual bool IsConnectedAndIdle() const; |
| 45 |
| 46 // Socket methods: |
| 47 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 48 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 49 |
| 50 #if defined(OS_LINUX) |
| 51 // Identical to posix system call getpeername(). |
| 52 // Needed by ssl_client_socket_nss. |
| 53 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen); |
| 54 #endif |
| 55 |
| 56 private: |
| 57 enum State { |
| 58 STATE_RESOLVE_HOST, |
| 59 STATE_RESOLVE_HOST_COMPLETE, |
| 60 STATE_HANDSHAKE_WRITE, |
| 61 STATE_HANDSHAKE_WRITE_COMPLETE, |
| 62 STATE_HANDSHAKE_READ, |
| 63 STATE_HANDSHAKE_READ_COMPLETE, |
| 64 STATE_NONE, |
| 65 }; |
| 66 |
| 67 // The SOCKS proxy connection either has the hostname resolved via the |
| 68 // client or via the server. This enum stores the state of the SOCKS |
| 69 // connection. If the client can resolve the hostname, the connection is |
| 70 // SOCKS4, otherwise it is SOCKS4A. |
| 71 enum SocksVersion { |
| 72 kSOCKS4Unresolved, |
| 73 kSOCKS4, |
| 74 kSOCKS4a, |
| 75 }; |
| 76 |
| 77 void DoCallback(int result); |
| 78 void OnIOComplete(int result); |
| 79 |
| 80 int DoLoop(int last_io_result); |
| 81 int DoResolveHost(); |
| 82 int DoResolveHostComplete(int result); |
| 83 int DoHandshakeRead(); |
| 84 int DoHandshakeReadComplete(int result); |
| 85 int DoHandshakeWrite(); |
| 86 int DoHandshakeWriteComplete(int result); |
| 87 |
| 88 void BuildHandshakeWriteBuffer(); |
| 89 |
| 90 CompletionCallbackImpl<SOCKSClientSocket> io_callback_; |
| 91 |
| 92 // Stores the underlying socket. |
| 93 scoped_ptr<ClientSocket> transport_; |
| 94 |
| 95 State next_state_; |
| 96 SocksVersion socks_version_; |
| 97 |
| 98 // Stores the callback to the layer above, called on completing Connect(). |
| 99 CompletionCallback* user_callback_; |
| 100 |
| 101 // This IOBuffer is used by the class to read and write |
| 102 // SOCKS handshake data. The length contains the expected size to |
| 103 // read or write. |
| 104 scoped_refptr<IOBuffer> handshake_buf_; |
| 105 int handshake_buf_len_; |
| 106 |
| 107 // While writing, this buffer stores the complete write handshake data. |
| 108 // While reading, it stores the handshake information received so far. |
| 109 scoped_array<char> buffer_; |
| 110 int buffer_len_; |
| 111 |
| 112 // This becomes true when the SOCKS handshake has completed and the |
| 113 // overlying connection is free to communicate. |
| 114 bool completed_handshake_; |
| 115 |
| 116 // These contain the bytes sent / received by the SOCKS handshake. |
| 117 int bytes_sent_; |
| 118 int bytes_received_; |
| 119 |
| 120 // Used to resolve the hostname to which the SOCKS proxy will connect. |
| 121 SingleRequestHostResolver resolver_; |
| 122 AddressList addresses_; |
| 123 HostResolver::RequestInfo host_request_info_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocket); |
| 126 }; |
| 127 |
| 128 } // namespace net |
| 129 |
| 130 #endif // NET_BASE_SOCKS_CLIENT_SOCKET_H_ |
| 131 |
OLD | NEW |