Chromecast Android buildfix: rework CommandLine initialization logic.
[chromium-blink-merge.git] / net / socket / socks5_client_socket.h
blob8da0b4da5ce42b4b21007fae01ce501374f28ca8
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_
8 #include <string>
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"
20 #include "url/gurl.h"
22 namespace net {
24 class ClientSocketHandle;
25 class BoundNetLog;
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 {
30 public:
31 // |req_info| contains the hostname and port to which the socket above will
32 // communicate to via the SOCKS layer.
34 // Although SOCKS 5 supports 3 different modes of addressing, we will
35 // always pass it a hostname. This means the DNS resolving is done
36 // proxy side.
37 SOCKS5ClientSocket(scoped_ptr<ClientSocketHandle> transport_socket,
38 const HostResolver::RequestInfo& req_info);
40 // On destruction Disconnect() is called.
41 virtual ~SOCKS5ClientSocket();
43 // StreamSocket implementation.
45 // Does the SOCKS handshake and completes the protocol.
46 virtual int Connect(const CompletionCallback& callback) OVERRIDE;
47 virtual void Disconnect() OVERRIDE;
48 virtual bool IsConnected() const OVERRIDE;
49 virtual bool IsConnectedAndIdle() const OVERRIDE;
50 virtual const BoundNetLog& NetLog() const OVERRIDE;
51 virtual void SetSubresourceSpeculation() OVERRIDE;
52 virtual void SetOmniboxSpeculation() OVERRIDE;
53 virtual bool WasEverUsed() const OVERRIDE;
54 virtual bool UsingTCPFastOpen() const OVERRIDE;
55 virtual bool WasNpnNegotiated() const OVERRIDE;
56 virtual NextProto GetNegotiatedProtocol() const OVERRIDE;
57 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
59 // Socket implementation.
60 virtual int Read(IOBuffer* buf,
61 int buf_len,
62 const CompletionCallback& callback) OVERRIDE;
63 virtual int Write(IOBuffer* buf,
64 int buf_len,
65 const CompletionCallback& callback) OVERRIDE;
67 virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
68 virtual int SetSendBufferSize(int32 size) OVERRIDE;
70 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
71 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
73 private:
74 enum State {
75 STATE_GREET_WRITE,
76 STATE_GREET_WRITE_COMPLETE,
77 STATE_GREET_READ,
78 STATE_GREET_READ_COMPLETE,
79 STATE_HANDSHAKE_WRITE,
80 STATE_HANDSHAKE_WRITE_COMPLETE,
81 STATE_HANDSHAKE_READ,
82 STATE_HANDSHAKE_READ_COMPLETE,
83 STATE_NONE,
86 // Addressing type that can be specified in requests or responses.
87 enum SocksEndPointAddressType {
88 kEndPointDomain = 0x03,
89 kEndPointResolvedIPv4 = 0x01,
90 kEndPointResolvedIPv6 = 0x04,
93 static const unsigned int kGreetReadHeaderSize;
94 static const unsigned int kWriteHeaderSize;
95 static const unsigned int kReadHeaderSize;
96 static const uint8 kSOCKS5Version;
97 static const uint8 kTunnelCommand;
98 static const uint8 kNullByte;
100 void DoCallback(int result);
101 void OnIOComplete(int result);
102 void OnReadWriteComplete(const CompletionCallback& callback, int result);
104 int DoLoop(int last_io_result);
105 int DoHandshakeRead();
106 int DoHandshakeReadComplete(int result);
107 int DoHandshakeWrite();
108 int DoHandshakeWriteComplete(int result);
109 int DoGreetRead();
110 int DoGreetReadComplete(int result);
111 int DoGreetWrite();
112 int DoGreetWriteComplete(int result);
114 // Writes the SOCKS handshake buffer into |handshake|
115 // and return OK on success.
116 int BuildHandshakeWriteBuffer(std::string* handshake) const;
118 CompletionCallback io_callback_;
120 // Stores the underlying socket.
121 scoped_ptr<ClientSocketHandle> transport_;
123 State next_state_;
125 // Stores the callback to the layer above, called on completing Connect().
126 CompletionCallback user_callback_;
128 // This IOBuffer is used by the class to read and write
129 // SOCKS handshake data. The length contains the expected size to
130 // read or write.
131 scoped_refptr<IOBuffer> handshake_buf_;
133 // While writing, this buffer stores the complete write handshake data.
134 // While reading, it stores the handshake information received so far.
135 std::string buffer_;
137 // This becomes true when the SOCKS handshake has completed and the
138 // overlying connection is free to communicate.
139 bool completed_handshake_;
141 // These contain the bytes sent / received by the SOCKS handshake.
142 size_t bytes_sent_;
143 size_t bytes_received_;
145 size_t read_header_size;
147 bool was_ever_used_;
149 HostResolver::RequestInfo host_request_info_;
151 BoundNetLog net_log_;
153 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket);
156 } // namespace net
158 #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_