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
3 // found in the LICENSE file.
5 #ifndef NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_
6 #define NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/address_list.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_log.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/socket/stream_socket.h"
24 class ClientSocketHandle
;
27 // This StreamSocket is used to setup a SOCKSv5 handshake with a socks proxy.
28 // Currently no SOCKSv5 authentication is supported.
29 class NET_EXPORT_PRIVATE SOCKS5ClientSocket
: public StreamSocket
{
31 // Takes ownership of the |transport_socket|, which should already be
32 // connected by the time Connect() is called.
34 // |req_info| contains the hostname and port to which the socket above will
35 // communicate to via the SOCKS layer.
37 // Although SOCKS 5 supports 3 different modes of addressing, we will
38 // always pass it a hostname. This means the DNS resolving is done
40 SOCKS5ClientSocket(ClientSocketHandle
* transport_socket
,
41 const HostResolver::RequestInfo
& req_info
);
43 // Deprecated constructor (http://crbug.com/37810) that takes a StreamSocket.
44 SOCKS5ClientSocket(StreamSocket
* transport_socket
,
45 const HostResolver::RequestInfo
& req_info
);
47 // On destruction Disconnect() is called.
48 virtual ~SOCKS5ClientSocket();
50 // StreamSocket implementation.
52 // Does the SOCKS handshake and completes the protocol.
53 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
54 virtual void Disconnect() OVERRIDE
;
55 virtual bool IsConnected() const OVERRIDE
;
56 virtual bool IsConnectedAndIdle() const OVERRIDE
;
57 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
58 virtual void SetSubresourceSpeculation() OVERRIDE
;
59 virtual void SetOmniboxSpeculation() OVERRIDE
;
60 virtual bool WasEverUsed() const OVERRIDE
;
61 virtual bool UsingTCPFastOpen() const OVERRIDE
;
62 virtual bool WasNpnNegotiated() const OVERRIDE
;
63 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
64 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
66 // Socket implementation.
67 virtual int Read(IOBuffer
* buf
,
69 const CompletionCallback
& callback
) OVERRIDE
;
70 virtual int Write(IOBuffer
* buf
,
72 const CompletionCallback
& callback
) OVERRIDE
;
74 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
75 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
77 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
78 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
83 STATE_GREET_WRITE_COMPLETE
,
85 STATE_GREET_READ_COMPLETE
,
86 STATE_HANDSHAKE_WRITE
,
87 STATE_HANDSHAKE_WRITE_COMPLETE
,
89 STATE_HANDSHAKE_READ_COMPLETE
,
93 // Addressing type that can be specified in requests or responses.
94 enum SocksEndPointAddressType
{
95 kEndPointDomain
= 0x03,
96 kEndPointResolvedIPv4
= 0x01,
97 kEndPointResolvedIPv6
= 0x04,
100 static const unsigned int kGreetReadHeaderSize
;
101 static const unsigned int kWriteHeaderSize
;
102 static const unsigned int kReadHeaderSize
;
103 static const uint8 kSOCKS5Version
;
104 static const uint8 kTunnelCommand
;
105 static const uint8 kNullByte
;
107 void DoCallback(int result
);
108 void OnIOComplete(int result
);
110 int DoLoop(int last_io_result
);
111 int DoHandshakeRead();
112 int DoHandshakeReadComplete(int result
);
113 int DoHandshakeWrite();
114 int DoHandshakeWriteComplete(int result
);
116 int DoGreetReadComplete(int result
);
118 int DoGreetWriteComplete(int result
);
120 // Writes the SOCKS handshake buffer into |handshake|
121 // and return OK on success.
122 int BuildHandshakeWriteBuffer(std::string
* handshake
) const;
124 CompletionCallback io_callback_
;
126 // Stores the underlying socket.
127 scoped_ptr
<ClientSocketHandle
> transport_
;
131 // Stores the callback to the layer above, called on completing Connect().
132 CompletionCallback user_callback_
;
134 // This IOBuffer is used by the class to read and write
135 // SOCKS handshake data. The length contains the expected size to
137 scoped_refptr
<IOBuffer
> handshake_buf_
;
139 // While writing, this buffer stores the complete write handshake data.
140 // While reading, it stores the handshake information received so far.
143 // This becomes true when the SOCKS handshake has completed and the
144 // overlying connection is free to communicate.
145 bool completed_handshake_
;
147 // These contain the bytes sent / received by the SOCKS handshake.
149 size_t bytes_received_
;
151 size_t read_header_size
;
153 HostResolver::RequestInfo host_request_info_
;
155 BoundNetLog net_log_
;
157 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket
);
162 #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_