OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/socks_client_socket.h" | 5 #include "net/socket/socks_client_socket.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/trace_event.h" | 9 #include "base/trace_event.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
11 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 : ALLOW_THIS_IN_INITIALIZER_LIST( | 66 : ALLOW_THIS_IN_INITIALIZER_LIST( |
67 io_callback_(this, &SOCKSClientSocket::OnIOComplete)), | 67 io_callback_(this, &SOCKSClientSocket::OnIOComplete)), |
68 transport_(transport_socket), | 68 transport_(transport_socket), |
69 next_state_(STATE_NONE), | 69 next_state_(STATE_NONE), |
70 socks_version_(kSOCKS4Unresolved), | 70 socks_version_(kSOCKS4Unresolved), |
71 user_callback_(NULL), | 71 user_callback_(NULL), |
72 completed_handshake_(false), | 72 completed_handshake_(false), |
73 bytes_sent_(0), | 73 bytes_sent_(0), |
74 bytes_received_(0), | 74 bytes_received_(0), |
75 host_resolver_(host_resolver), | 75 host_resolver_(host_resolver), |
76 host_request_info_(req_info) { | 76 host_request_info_(req_info), |
| 77 net_log_(transport_socket->socket()->NetLog()) { |
77 } | 78 } |
78 | 79 |
79 SOCKSClientSocket::SOCKSClientSocket(ClientSocket* transport_socket, | 80 SOCKSClientSocket::SOCKSClientSocket(ClientSocket* transport_socket, |
80 const HostResolver::RequestInfo& req_info, | 81 const HostResolver::RequestInfo& req_info, |
81 HostResolver* host_resolver) | 82 HostResolver* host_resolver) |
82 : ALLOW_THIS_IN_INITIALIZER_LIST( | 83 : ALLOW_THIS_IN_INITIALIZER_LIST( |
83 io_callback_(this, &SOCKSClientSocket::OnIOComplete)), | 84 io_callback_(this, &SOCKSClientSocket::OnIOComplete)), |
84 transport_(new ClientSocketHandle()), | 85 transport_(new ClientSocketHandle()), |
85 next_state_(STATE_NONE), | 86 next_state_(STATE_NONE), |
86 socks_version_(kSOCKS4Unresolved), | 87 socks_version_(kSOCKS4Unresolved), |
87 user_callback_(NULL), | 88 user_callback_(NULL), |
88 completed_handshake_(false), | 89 completed_handshake_(false), |
89 bytes_sent_(0), | 90 bytes_sent_(0), |
90 bytes_received_(0), | 91 bytes_received_(0), |
91 host_resolver_(host_resolver), | 92 host_resolver_(host_resolver), |
92 host_request_info_(req_info) { | 93 host_request_info_(req_info), |
| 94 net_log_(transport_socket->NetLog()) { |
93 transport_->set_socket(transport_socket); | 95 transport_->set_socket(transport_socket); |
94 } | 96 } |
95 | 97 |
96 SOCKSClientSocket::~SOCKSClientSocket() { | 98 SOCKSClientSocket::~SOCKSClientSocket() { |
97 Disconnect(); | 99 Disconnect(); |
98 } | 100 } |
99 | 101 |
100 int SOCKSClientSocket::Connect(CompletionCallback* callback, | 102 int SOCKSClientSocket::Connect(CompletionCallback* callback) { |
101 const BoundNetLog& net_log) { | |
102 DCHECK(transport_.get()); | 103 DCHECK(transport_.get()); |
103 DCHECK(transport_->socket()); | 104 DCHECK(transport_->socket()); |
104 DCHECK(transport_->socket()->IsConnected()); | 105 DCHECK(transport_->socket()->IsConnected()); |
105 DCHECK_EQ(STATE_NONE, next_state_); | 106 DCHECK_EQ(STATE_NONE, next_state_); |
106 DCHECK(!user_callback_); | 107 DCHECK(!user_callback_); |
107 | 108 |
108 // If already connected, then just return OK. | 109 // If already connected, then just return OK. |
109 if (completed_handshake_) | 110 if (completed_handshake_) |
110 return OK; | 111 return OK; |
111 | 112 |
112 next_state_ = STATE_RESOLVE_HOST; | 113 next_state_ = STATE_RESOLVE_HOST; |
113 net_log_ = net_log; | |
114 | 114 |
115 net_log.BeginEvent(NetLog::TYPE_SOCKS_CONNECT); | 115 net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT); |
116 | 116 |
117 int rv = DoLoop(OK); | 117 int rv = DoLoop(OK); |
118 if (rv == ERR_IO_PENDING) { | 118 if (rv == ERR_IO_PENDING) { |
119 user_callback_ = callback; | 119 user_callback_ = callback; |
120 } else { | 120 } else { |
121 net_log.EndEvent(NetLog::TYPE_SOCKS_CONNECT); | 121 net_log_.EndEvent(NetLog::TYPE_SOCKS_CONNECT); |
122 net_log_ = BoundNetLog(); | |
123 } | 122 } |
124 return rv; | 123 return rv; |
125 } | 124 } |
126 | 125 |
127 void SOCKSClientSocket::Disconnect() { | 126 void SOCKSClientSocket::Disconnect() { |
128 completed_handshake_ = false; | 127 completed_handshake_ = false; |
129 host_resolver_.Cancel(); | 128 host_resolver_.Cancel(); |
130 transport_->socket()->Disconnect(); | 129 transport_->socket()->Disconnect(); |
131 | 130 |
132 // Reset other states to make sure they aren't mistakenly used later. | 131 // Reset other states to make sure they aren't mistakenly used later. |
133 // These are the states initialized by Connect(). | 132 // These are the states initialized by Connect(). |
134 next_state_ = STATE_NONE; | 133 next_state_ = STATE_NONE; |
135 user_callback_ = NULL; | 134 user_callback_ = NULL; |
136 net_log_ = BoundNetLog(); | |
137 } | 135 } |
138 | 136 |
139 bool SOCKSClientSocket::IsConnected() const { | 137 bool SOCKSClientSocket::IsConnected() const { |
140 return completed_handshake_ && transport_->socket()->IsConnected(); | 138 return completed_handshake_ && transport_->socket()->IsConnected(); |
141 } | 139 } |
142 | 140 |
143 bool SOCKSClientSocket::IsConnectedAndIdle() const { | 141 bool SOCKSClientSocket::IsConnectedAndIdle() const { |
144 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle(); | 142 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle(); |
145 } | 143 } |
146 | 144 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 user_callback_ = NULL; | 182 user_callback_ = NULL; |
185 DLOG(INFO) << "Finished setting up SOCKS handshake"; | 183 DLOG(INFO) << "Finished setting up SOCKS handshake"; |
186 c->Run(result); | 184 c->Run(result); |
187 } | 185 } |
188 | 186 |
189 void SOCKSClientSocket::OnIOComplete(int result) { | 187 void SOCKSClientSocket::OnIOComplete(int result) { |
190 DCHECK_NE(STATE_NONE, next_state_); | 188 DCHECK_NE(STATE_NONE, next_state_); |
191 int rv = DoLoop(result); | 189 int rv = DoLoop(result); |
192 if (rv != ERR_IO_PENDING) { | 190 if (rv != ERR_IO_PENDING) { |
193 net_log_.EndEvent(NetLog::TYPE_SOCKS_CONNECT); | 191 net_log_.EndEvent(NetLog::TYPE_SOCKS_CONNECT); |
194 net_log_ = BoundNetLog(); | |
195 DoCallback(rv); | 192 DoCallback(rv); |
196 } | 193 } |
197 } | 194 } |
198 | 195 |
199 int SOCKSClientSocket::DoLoop(int last_io_result) { | 196 int SOCKSClientSocket::DoLoop(int last_io_result) { |
200 DCHECK_NE(next_state_, STATE_NONE); | 197 DCHECK_NE(next_state_, STATE_NONE); |
201 int rv = last_io_result; | 198 int rv = last_io_result; |
202 do { | 199 do { |
203 State state = next_state_; | 200 State state = next_state_; |
204 next_state_ = STATE_NONE; | 201 next_state_ = STATE_NONE; |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 414 } |
418 | 415 |
419 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol | 416 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol |
420 } | 417 } |
421 | 418 |
422 int SOCKSClientSocket::GetPeerAddress(AddressList* address) const { | 419 int SOCKSClientSocket::GetPeerAddress(AddressList* address) const { |
423 return transport_->socket()->GetPeerAddress(address); | 420 return transport_->socket()->GetPeerAddress(address); |
424 } | 421 } |
425 | 422 |
426 } // namespace net | 423 } // namespace net |
OLD | NEW |