blob: e26b6c11623f09fe0577981a2420517046f64b70 [file] [log] [blame]
[email protected]a2006ece2010-04-23 16:44:021// Copyright (c) 2010 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]e0c27be2009-07-15 13:09:3512#include "base/ref_counted.h"
13#include "base/scoped_ptr.h"
14#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]e0c27be2009-07-15 13:09:3520#include "net/socket/client_socket.h"
21#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]e0c27be2009-07-15 13:09:3528// This ClientSocket is used to setup a SOCKSv5 handshake with a socks proxy.
29// Currently no SOCKSv5 authentication is supported.
30class SOCKS5ClientSocket : public ClientSocket {
31 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
44 // Deprecated constructor (http://crbug.com/37810) that takes a ClientSocket.
[email protected]e0c27be2009-07-15 13:09:3545 SOCKS5ClientSocket(ClientSocket* 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
51 // ClientSocket methods:
52
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]e0c27be2009-07-15 13:09:3563
64 // Socket methods:
65 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
66 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
67
[email protected]d3f66572009-09-09 22:38:0468 virtual bool SetReceiveBufferSize(int32 size);
69 virtual bool SetSendBufferSize(int32 size);
70
[email protected]ac9eec62010-02-20 18:50:3871 virtual int GetPeerAddress(AddressList* address) const;
[email protected]e0c27be2009-07-15 13:09:3572
73 private:
[email protected]e0c27be2009-07-15 13:09:3574 enum State {
[email protected]e0c27be2009-07-15 13:09:3575 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,
84 };
85
[email protected]20cbe23d2009-12-18 03:39:2186 // Addressing type that can be specified in requests or responses.
[email protected]e0c27be2009-07-15 13:09:3587 enum SocksEndPointAddressType {
[email protected]20cbe23d2009-12-18 03:39:2188 kEndPointDomain = 0x03,
[email protected]e0c27be2009-07-15 13:09:3589 kEndPointResolvedIPv4 = 0x01,
90 kEndPointResolvedIPv6 = 0x04,
91 };
92
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;
99
100 void DoCallback(int result);
101 void OnIOComplete(int result);
102
103 int DoLoop(int last_io_result);
[email protected]e0c27be2009-07-15 13:09:35104 int DoHandshakeRead();
105 int DoHandshakeReadComplete(int result);
106 int DoHandshakeWrite();
107 int DoHandshakeWriteComplete(int result);
108 int DoGreetRead();
109 int DoGreetReadComplete(int result);
110 int DoGreetWrite();
111 int DoGreetWriteComplete(int result);
112
113 // Writes the SOCKS handshake buffer into |handshake|
114 // and return OK on success.
115 int BuildHandshakeWriteBuffer(std::string* handshake) const;
116
117 CompletionCallbackImpl<SOCKS5ClientSocket> io_callback_;
118
119 // Stores the underlying socket.
[email protected]a796bcec2010-03-22 17:17:26120 scoped_ptr<ClientSocketHandle> transport_;
[email protected]e0c27be2009-07-15 13:09:35121
122 State next_state_;
[email protected]e0c27be2009-07-15 13:09:35123
124 // Stores the callback to the layer above, called on completing Connect().
125 CompletionCallback* user_callback_;
126
127 // This IOBuffer is used by the class to read and write
128 // SOCKS handshake data. The length contains the expected size to
129 // read or write.
130 scoped_refptr<IOBuffer> handshake_buf_;
131
132 // While writing, this buffer stores the complete write handshake data.
133 // While reading, it stores the handshake information received so far.
134 std::string buffer_;
135
136 // This becomes true when the SOCKS handshake has completed and the
137 // overlying connection is free to communicate.
138 bool completed_handshake_;
139
140 // These contain the bytes sent / received by the SOCKS handshake.
141 size_t bytes_sent_;
142 size_t bytes_received_;
143
144 size_t read_header_size;
145
[email protected]e0c27be2009-07-15 13:09:35146 HostResolver::RequestInfo host_request_info_;
147
[email protected]9e743cd2010-03-16 07:03:53148 BoundNetLog net_log_;
[email protected]5a05c47a2009-11-02 23:25:19149
[email protected]e0c27be2009-07-15 13:09:35150 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket);
151};
152
153} // namespace net
154
155#endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_