blob: 97fa61a2784e2621449f5859f8b7f7ec822d8c90 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e0c27be2009-07-15 13:09:352// 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_SOCKET_SOCKS5_CLIENT_SOCKET_H_
6#define NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]e0c27be2009-07-15 13:09:358
9#include <string>
10
[email protected]1407b6e2010-08-27 21:39:4811#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
[email protected]e0c27be2009-07-15 13:09:3514#include "googleurl/src/gurl.h"
15#include "net/base/address_list.h"
16#include "net/base/completion_callback.h"
17#include "net/base/host_resolver.h"
18#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5319#include "net/base/net_log.h"
[email protected]3268023f2011-05-05 00:08:1020#include "net/socket/stream_socket.h"
[email protected]e0c27be2009-07-15 13:09:3521#include "testing/gtest/include/gtest/gtest_prod.h"
22
23namespace net {
24
[email protected]a796bcec2010-03-22 17:17:2625class ClientSocketHandle;
[email protected]9e743cd2010-03-16 07:03:5326class BoundNetLog;
[email protected]5a05c47a2009-11-02 23:25:1927
[email protected]3268023f2011-05-05 00:08:1028// This StreamSocket is used to setup a SOCKSv5 handshake with a socks proxy.
[email protected]e0c27be2009-07-15 13:09:3529// Currently no SOCKSv5 authentication is supported.
[email protected]172da1b2011-08-12 15:52:2630class NET_EXPORT_PRIVATE SOCKS5ClientSocket : public StreamSocket {
[email protected]e0c27be2009-07-15 13:09:3531 public:
32 // Takes ownership of the |transport_socket|, which should already be
33 // connected by the time Connect() is called.
34 //
35 // |req_info| contains the hostname and port to which the socket above will
36 // communicate to via the SOCKS layer.
[email protected]f209dba2009-12-18 00:24:3737 //
[email protected]20cbe23d2009-12-18 03:39:2138 // Although SOCKS 5 supports 3 different modes of addressing, we will
39 // always pass it a hostname. This means the DNS resolving is done
40 // proxy side.
[email protected]a796bcec2010-03-22 17:17:2641 SOCKS5ClientSocket(ClientSocketHandle* transport_socket,
42 const HostResolver::RequestInfo& req_info);
43
[email protected]3268023f2011-05-05 00:08:1044 // Deprecated constructor (http://crbug.com/37810) that takes a StreamSocket.
45 SOCKS5ClientSocket(StreamSocket* transport_socket,
[email protected]20cbe23d2009-12-18 03:39:2146 const HostResolver::RequestInfo& req_info);
[email protected]e0c27be2009-07-15 13:09:3547
48 // On destruction Disconnect() is called.
49 virtual ~SOCKS5ClientSocket();
50
[email protected]3268023f2011-05-05 00:08:1051 // StreamSocket methods:
[email protected]e0c27be2009-07-15 13:09:3552
53 // Does the SOCKS handshake and completes the protocol.
[email protected]a2006ece2010-04-23 16:44:0254 virtual int Connect(CompletionCallback* callback);
[email protected]e0c27be2009-07-15 13:09:3555 virtual void Disconnect();
56 virtual bool IsConnected() const;
57 virtual bool IsConnectedAndIdle() const;
[email protected]e4be2dd2010-12-14 00:44:3958 virtual const BoundNetLog& NetLog() const;
[email protected]9b5614a2010-08-25 20:29:4559 virtual void SetSubresourceSpeculation();
60 virtual void SetOmniboxSpeculation();
[email protected]0f873e82010-09-02 16:09:0161 virtual bool WasEverUsed() const;
[email protected]7f7e92392010-10-26 18:29:2962 virtual bool UsingTCPFastOpen() const;
[email protected]5e6efa52011-06-27 17:26:4163 virtual int64 NumBytesRead() const;
64 virtual base::TimeDelta GetConnectTimeMicros() const;
[email protected]e0c27be2009-07-15 13:09:3565
66 // Socket methods:
67 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
68 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
69
[email protected]d3f66572009-09-09 22:38:0470 virtual bool SetReceiveBufferSize(int32 size);
71 virtual bool SetSendBufferSize(int32 size);
72
[email protected]ac9eec62010-02-20 18:50:3873 virtual int GetPeerAddress(AddressList* address) const;
[email protected]e7f74da2011-04-19 23:49:3574 virtual int GetLocalAddress(IPEndPoint* address) const;
[email protected]e0c27be2009-07-15 13:09:3575
76 private:
[email protected]e0c27be2009-07-15 13:09:3577 enum State {
[email protected]e0c27be2009-07-15 13:09:3578 STATE_GREET_WRITE,
79 STATE_GREET_WRITE_COMPLETE,
80 STATE_GREET_READ,
81 STATE_GREET_READ_COMPLETE,
82 STATE_HANDSHAKE_WRITE,
83 STATE_HANDSHAKE_WRITE_COMPLETE,
84 STATE_HANDSHAKE_READ,
85 STATE_HANDSHAKE_READ_COMPLETE,
86 STATE_NONE,
87 };
88
[email protected]20cbe23d2009-12-18 03:39:2189 // Addressing type that can be specified in requests or responses.
[email protected]e0c27be2009-07-15 13:09:3590 enum SocksEndPointAddressType {
[email protected]20cbe23d2009-12-18 03:39:2191 kEndPointDomain = 0x03,
[email protected]e0c27be2009-07-15 13:09:3592 kEndPointResolvedIPv4 = 0x01,
93 kEndPointResolvedIPv6 = 0x04,
94 };
95
96 static const unsigned int kGreetReadHeaderSize;
97 static const unsigned int kWriteHeaderSize;
98 static const unsigned int kReadHeaderSize;
99 static const uint8 kSOCKS5Version;
100 static const uint8 kTunnelCommand;
101 static const uint8 kNullByte;
102
103 void DoCallback(int result);
104 void OnIOComplete(int result);
105
106 int DoLoop(int last_io_result);
[email protected]e0c27be2009-07-15 13:09:35107 int DoHandshakeRead();
108 int DoHandshakeReadComplete(int result);
109 int DoHandshakeWrite();
110 int DoHandshakeWriteComplete(int result);
111 int DoGreetRead();
112 int DoGreetReadComplete(int result);
113 int DoGreetWrite();
114 int DoGreetWriteComplete(int result);
115
116 // Writes the SOCKS handshake buffer into |handshake|
117 // and return OK on success.
118 int BuildHandshakeWriteBuffer(std::string* handshake) const;
119
120 CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_;
121
122 // Stores the underlying socket.
[email protected]a796bcec2010-03-22 17:17:26123 scoped_ptr<ClientSocketHandle> transport_;
[email protected]e0c27be2009-07-15 13:09:35124
125 State next_state_;
[email protected]e0c27be2009-07-15 13:09:35126
127 // Stores the callback to the layer above, called on completing Connect().
128 CompletionCallback* user_callback_;
129
130 // This IOBuffer is used by the class to read and write
131 // SOCKS handshake data. The length contains the expected size to
132 // read or write.
133 scoped_refptr<IOBuffer> handshake_buf_;
134
135 // While writing, this buffer stores the complete write handshake data.
136 // While reading, it stores the handshake information received so far.
137 std::string buffer_;
138
139 // This becomes true when the SOCKS handshake has completed and the
140 // overlying connection is free to communicate.
141 bool completed_handshake_;
142
143 // These contain the bytes sent / received by the SOCKS handshake.
144 size_t bytes_sent_;
145 size_t bytes_received_;
146
147 size_t read_header_size;
148
[email protected]e0c27be2009-07-15 13:09:35149 HostResolver::RequestInfo host_request_info_;
150
[email protected]9e743cd2010-03-16 07:03:53151 BoundNetLog net_log_;
[email protected]5a05c47a2009-11-02 23:25:19152
[email protected]e0c27be2009-07-15 13:09:35153 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket);
154};
155
156} // namespace net
157
158#endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_