OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/socks5_client_socket.h" | 5 #include "net/socket/socks5_client_socket.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/callback_helpers.h" |
8 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
9 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
10 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
15 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
16 #include "net/socket/client_socket_handle.h" | 17 #include "net/socket/client_socket_handle.h" |
17 | 18 |
(...skipping 13 matching lines...) Expand all Loading... |
31 scoped_ptr<ClientSocketHandle> transport_socket, | 32 scoped_ptr<ClientSocketHandle> transport_socket, |
32 const HostResolver::RequestInfo& req_info) | 33 const HostResolver::RequestInfo& req_info) |
33 : io_callback_(base::Bind(&SOCKS5ClientSocket::OnIOComplete, | 34 : io_callback_(base::Bind(&SOCKS5ClientSocket::OnIOComplete, |
34 base::Unretained(this))), | 35 base::Unretained(this))), |
35 transport_(transport_socket.Pass()), | 36 transport_(transport_socket.Pass()), |
36 next_state_(STATE_NONE), | 37 next_state_(STATE_NONE), |
37 completed_handshake_(false), | 38 completed_handshake_(false), |
38 bytes_sent_(0), | 39 bytes_sent_(0), |
39 bytes_received_(0), | 40 bytes_received_(0), |
40 read_header_size(kReadHeaderSize), | 41 read_header_size(kReadHeaderSize), |
| 42 was_ever_used_(false), |
41 host_request_info_(req_info), | 43 host_request_info_(req_info), |
42 net_log_(transport_->socket()->NetLog()) { | 44 net_log_(transport_->socket()->NetLog()) { |
43 } | 45 } |
44 | 46 |
45 SOCKS5ClientSocket::~SOCKS5ClientSocket() { | 47 SOCKS5ClientSocket::~SOCKS5ClientSocket() { |
46 Disconnect(); | 48 Disconnect(); |
47 } | 49 } |
48 | 50 |
49 int SOCKS5ClientSocket::Connect(const CompletionCallback& callback) { | 51 int SOCKS5ClientSocket::Connect(const CompletionCallback& callback) { |
50 DCHECK(transport_.get()); | 52 DCHECK(transport_.get()); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 104 |
103 void SOCKS5ClientSocket::SetOmniboxSpeculation() { | 105 void SOCKS5ClientSocket::SetOmniboxSpeculation() { |
104 if (transport_.get() && transport_->socket()) { | 106 if (transport_.get() && transport_->socket()) { |
105 transport_->socket()->SetOmniboxSpeculation(); | 107 transport_->socket()->SetOmniboxSpeculation(); |
106 } else { | 108 } else { |
107 NOTREACHED(); | 109 NOTREACHED(); |
108 } | 110 } |
109 } | 111 } |
110 | 112 |
111 bool SOCKS5ClientSocket::WasEverUsed() const { | 113 bool SOCKS5ClientSocket::WasEverUsed() const { |
112 if (transport_.get() && transport_->socket()) { | 114 return was_ever_used_; |
113 return transport_->socket()->WasEverUsed(); | |
114 } | |
115 NOTREACHED(); | |
116 return false; | |
117 } | 115 } |
118 | 116 |
119 bool SOCKS5ClientSocket::UsingTCPFastOpen() const { | 117 bool SOCKS5ClientSocket::UsingTCPFastOpen() const { |
120 if (transport_.get() && transport_->socket()) { | 118 if (transport_.get() && transport_->socket()) { |
121 return transport_->socket()->UsingTCPFastOpen(); | 119 return transport_->socket()->UsingTCPFastOpen(); |
122 } | 120 } |
123 NOTREACHED(); | 121 NOTREACHED(); |
124 return false; | 122 return false; |
125 } | 123 } |
126 | 124 |
(...skipping 22 matching lines...) Expand all Loading... |
149 | 147 |
150 } | 148 } |
151 | 149 |
152 // Read is called by the transport layer above to read. This can only be done | 150 // Read is called by the transport layer above to read. This can only be done |
153 // if the SOCKS handshake is complete. | 151 // if the SOCKS handshake is complete. |
154 int SOCKS5ClientSocket::Read(IOBuffer* buf, int buf_len, | 152 int SOCKS5ClientSocket::Read(IOBuffer* buf, int buf_len, |
155 const CompletionCallback& callback) { | 153 const CompletionCallback& callback) { |
156 DCHECK(completed_handshake_); | 154 DCHECK(completed_handshake_); |
157 DCHECK_EQ(STATE_NONE, next_state_); | 155 DCHECK_EQ(STATE_NONE, next_state_); |
158 DCHECK(user_callback_.is_null()); | 156 DCHECK(user_callback_.is_null()); |
| 157 DCHECK(!callback.is_null()); |
159 | 158 |
160 return transport_->socket()->Read(buf, buf_len, callback); | 159 int rv = transport_->socket()->Read( |
| 160 buf, buf_len, |
| 161 base::Bind(&SOCKS5ClientSocket::OnReadWriteComplete, |
| 162 base::Unretained(this), callback)); |
| 163 if (rv > 0) |
| 164 was_ever_used_ = true; |
| 165 return rv; |
161 } | 166 } |
162 | 167 |
163 // Write is called by the transport layer. This can only be done if the | 168 // Write is called by the transport layer. This can only be done if the |
164 // SOCKS handshake is complete. | 169 // SOCKS handshake is complete. |
165 int SOCKS5ClientSocket::Write(IOBuffer* buf, int buf_len, | 170 int SOCKS5ClientSocket::Write(IOBuffer* buf, int buf_len, |
166 const CompletionCallback& callback) { | 171 const CompletionCallback& callback) { |
167 DCHECK(completed_handshake_); | 172 DCHECK(completed_handshake_); |
168 DCHECK_EQ(STATE_NONE, next_state_); | 173 DCHECK_EQ(STATE_NONE, next_state_); |
169 DCHECK(user_callback_.is_null()); | 174 DCHECK(user_callback_.is_null()); |
| 175 DCHECK(!callback.is_null()); |
170 | 176 |
171 return transport_->socket()->Write(buf, buf_len, callback); | 177 int rv = transport_->socket()->Write( |
| 178 buf, buf_len, |
| 179 base::Bind(&SOCKS5ClientSocket::OnReadWriteComplete, |
| 180 base::Unretained(this), callback)); |
| 181 if (rv > 0) |
| 182 was_ever_used_ = true; |
| 183 return rv; |
172 } | 184 } |
173 | 185 |
174 bool SOCKS5ClientSocket::SetReceiveBufferSize(int32 size) { | 186 bool SOCKS5ClientSocket::SetReceiveBufferSize(int32 size) { |
175 return transport_->socket()->SetReceiveBufferSize(size); | 187 return transport_->socket()->SetReceiveBufferSize(size); |
176 } | 188 } |
177 | 189 |
178 bool SOCKS5ClientSocket::SetSendBufferSize(int32 size) { | 190 bool SOCKS5ClientSocket::SetSendBufferSize(int32 size) { |
179 return transport_->socket()->SetSendBufferSize(size); | 191 return transport_->socket()->SetSendBufferSize(size); |
180 } | 192 } |
181 | 193 |
182 void SOCKS5ClientSocket::DoCallback(int result) { | 194 void SOCKS5ClientSocket::DoCallback(int result) { |
183 DCHECK_NE(ERR_IO_PENDING, result); | 195 DCHECK_NE(ERR_IO_PENDING, result); |
184 DCHECK(!user_callback_.is_null()); | 196 DCHECK(!user_callback_.is_null()); |
185 | 197 |
186 // Since Run() may result in Read being called, | 198 // Since Run() may result in Read being called, |
187 // clear user_callback_ up front. | 199 // clear user_callback_ up front. |
188 CompletionCallback c = user_callback_; | 200 base::ResetAndReturn(&user_callback_).Run(result); |
189 user_callback_.Reset(); | |
190 c.Run(result); | |
191 } | 201 } |
192 | 202 |
193 void SOCKS5ClientSocket::OnIOComplete(int result) { | 203 void SOCKS5ClientSocket::OnIOComplete(int result) { |
194 DCHECK_NE(STATE_NONE, next_state_); | 204 DCHECK_NE(STATE_NONE, next_state_); |
195 int rv = DoLoop(result); | 205 int rv = DoLoop(result); |
196 if (rv != ERR_IO_PENDING) { | 206 if (rv != ERR_IO_PENDING) { |
197 net_log_.EndEvent(NetLog::TYPE_SOCKS5_CONNECT); | 207 net_log_.EndEvent(NetLog::TYPE_SOCKS5_CONNECT); |
198 DoCallback(rv); | 208 DoCallback(rv); |
199 } | 209 } |
200 } | 210 } |
201 | 211 |
| 212 void SOCKS5ClientSocket::OnReadWriteComplete(const CompletionCallback& callback, |
| 213 int result) { |
| 214 DCHECK_NE(ERR_IO_PENDING, result); |
| 215 DCHECK(!callback.is_null()); |
| 216 |
| 217 if (result > 0) |
| 218 was_ever_used_ = true; |
| 219 callback.Run(result); |
| 220 } |
| 221 |
202 int SOCKS5ClientSocket::DoLoop(int last_io_result) { | 222 int SOCKS5ClientSocket::DoLoop(int last_io_result) { |
203 DCHECK_NE(next_state_, STATE_NONE); | 223 DCHECK_NE(next_state_, STATE_NONE); |
204 int rv = last_io_result; | 224 int rv = last_io_result; |
205 do { | 225 do { |
206 State state = next_state_; | 226 State state = next_state_; |
207 next_state_ = STATE_NONE; | 227 next_state_ = STATE_NONE; |
208 switch (state) { | 228 switch (state) { |
209 case STATE_GREET_WRITE: | 229 case STATE_GREET_WRITE: |
210 DCHECK_EQ(OK, rv); | 230 DCHECK_EQ(OK, rv); |
211 net_log_.BeginEvent(NetLog::TYPE_SOCKS5_GREET_WRITE); | 231 net_log_.BeginEvent(NetLog::TYPE_SOCKS5_GREET_WRITE); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 | 497 |
478 int SOCKS5ClientSocket::GetPeerAddress(IPEndPoint* address) const { | 498 int SOCKS5ClientSocket::GetPeerAddress(IPEndPoint* address) const { |
479 return transport_->socket()->GetPeerAddress(address); | 499 return transport_->socket()->GetPeerAddress(address); |
480 } | 500 } |
481 | 501 |
482 int SOCKS5ClientSocket::GetLocalAddress(IPEndPoint* address) const { | 502 int SOCKS5ClientSocket::GetLocalAddress(IPEndPoint* address) const { |
483 return transport_->socket()->GetLocalAddress(address); | 503 return transport_->socket()->GetLocalAddress(address); |
484 } | 504 } |
485 | 505 |
486 } // namespace net | 506 } // namespace net |
OLD | NEW |