3 Copyright (c) 2007, Arvid Norberg
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/pch.hpp"
35 #include "libtorrent/socks5_stream.hpp"
36 #include "libtorrent/assert.hpp"
41 void socks5_stream::name_lookup(error_code
const& e
, tcp::resolver::iterator i
42 , boost::shared_ptr
<handler_type
> h
)
44 if (e
|| i
== tcp::resolver::iterator())
52 m_sock
.async_connect(i
->endpoint(), boost::bind(
53 &socks5_stream::connected
, this, _1
, h
));
56 void socks5_stream::connected(error_code
const& e
, boost::shared_ptr
<handler_type
> h
)
66 using namespace libtorrent::detail
;
67 // send SOCKS5 authentication methods
68 m_buffer
.resize(m_user
.empty()?3:4);
69 char* p
= &m_buffer
[0];
70 write_uint8(5, p
); // SOCKS VERSION 5
73 write_uint8(1, p
); // 1 authentication method (no auth)
74 write_uint8(0, p
); // no authentication
78 write_uint8(2, p
); // 2 authentication methods
79 write_uint8(0, p
); // no authentication
80 write_uint8(2, p
); // username/password
82 async_write(m_sock
, asio::buffer(m_buffer
)
83 , boost::bind(&socks5_stream::handshake1
, this, _1
, h
));
86 void socks5_stream::handshake1(error_code
const& e
, boost::shared_ptr
<handler_type
> h
)
97 async_read(m_sock
, asio::buffer(m_buffer
)
98 , boost::bind(&socks5_stream::handshake2
, this, _1
, h
));
101 void socks5_stream::handshake2(error_code
const& e
, boost::shared_ptr
<handler_type
> h
)
111 using namespace libtorrent::detail
;
113 char* p
= &m_buffer
[0];
114 int version
= read_uint8(p
);
115 int method
= read_uint8(p
);
119 (*h
)(asio::error::operation_not_supported
);
129 else if (method
== 2)
133 (*h
)(asio::error::operation_not_supported
);
139 // start sub-negotiation
140 m_buffer
.resize(m_user
.size() + m_password
.size() + 3);
141 char* p
= &m_buffer
[0];
143 write_uint8(m_user
.size(), p
);
144 write_string(m_user
, p
);
145 write_uint8(m_password
.size(), p
);
146 write_string(m_password
, p
);
147 async_write(m_sock
, asio::buffer(m_buffer
)
148 , boost::bind(&socks5_stream::handshake3
, this, _1
, h
));
152 (*h
)(asio::error::operation_not_supported
);
159 void socks5_stream::handshake3(error_code
const& e
160 , boost::shared_ptr
<handler_type
> h
)
171 async_read(m_sock
, asio::buffer(m_buffer
)
172 , boost::bind(&socks5_stream::handshake4
, this, _1
, h
));
175 void socks5_stream::handshake4(error_code
const& e
176 , boost::shared_ptr
<handler_type
> h
)
186 using namespace libtorrent::detail
;
188 char* p
= &m_buffer
[0];
189 int version
= read_uint8(p
);
190 int status
= read_uint8(p
);
194 (*h
)(asio::error::operation_not_supported
);
202 (*h
)(asio::error::operation_not_supported
);
208 std::vector
<char>().swap(m_buffer
);
212 void socks5_stream::socks_connect(boost::shared_ptr
<handler_type
> h
)
214 using namespace libtorrent::detail
;
216 // send SOCKS5 connect command
217 m_buffer
.resize(6 + (m_remote_endpoint
.address().is_v4()?4:16));
218 char* p
= &m_buffer
[0];
219 write_uint8(5, p
); // SOCKS VERSION 5
220 write_uint8(1, p
); // CONNECT command
221 write_uint8(0, p
); // reserved
222 write_uint8(m_remote_endpoint
.address().is_v4()?1:4, p
); // address type
223 write_endpoint(m_remote_endpoint
, p
);
224 TORRENT_ASSERT(p
- &m_buffer
[0] == int(m_buffer
.size()));
226 async_write(m_sock
, asio::buffer(m_buffer
)
227 , boost::bind(&socks5_stream::connect1
, this, _1
, h
));
230 void socks5_stream::connect1(error_code
const& e
, boost::shared_ptr
<handler_type
> h
)
240 m_buffer
.resize(6 + 4); // assume an IPv4 address
241 async_read(m_sock
, asio::buffer(m_buffer
)
242 , boost::bind(&socks5_stream::connect2
, this, _1
, h
));
245 void socks5_stream::connect2(error_code
const& e
, boost::shared_ptr
<handler_type
> h
)
255 using namespace libtorrent::detail
;
257 // send SOCKS5 connect command
258 char* p
= &m_buffer
[0];
259 int version
= read_uint8(p
);
262 (*h
)(asio::error::operation_not_supported
);
267 int response
= read_uint8(p
);
270 error_code e
= asio::error::fault
;
273 case 1: e
= asio::error::fault
; break;
274 case 2: e
= asio::error::no_permission
; break;
275 case 3: e
= asio::error::network_unreachable
; break;
276 case 4: e
= asio::error::host_unreachable
; break;
277 case 5: e
= asio::error::connection_refused
; break;
278 case 6: e
= asio::error::timed_out
; break;
279 case 7: e
= asio::error::operation_not_supported
; break;
280 case 8: e
= asio::error::address_family_not_supported
; break;
288 int atyp
= read_uint8(p
);
289 // we ignore the proxy IP it was bound to
292 std::vector
<char>().swap(m_buffer
);
303 skip_bytes
= read_uint8(p
) - 3;
307 (*h
)(asio::error::operation_not_supported
);
312 m_buffer
.resize(skip_bytes
);
314 async_read(m_sock
, asio::buffer(m_buffer
)
315 , boost::bind(&socks5_stream::connect3
, this, _1
, h
));
318 void socks5_stream::connect3(error_code
const& e
, boost::shared_ptr
<handler_type
> h
)
328 std::vector
<char>().swap(m_buffer
);