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(asio::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(asio::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
asio::async_write(m_sock, asio::buffer(m_buffer)
83
, boost::bind(&socks5_stream::handshake1, this, _1, h));
86
void socks5_stream::handshake1(asio::error_code const& e, boost::shared_ptr<handler_type> h)
97
asio::async_read(m_sock, asio::buffer(m_buffer)
98
, boost::bind(&socks5_stream::handshake2, this, _1, h));
101
void socks5_stream::handshake2(asio::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
asio::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(asio::error_code const& e
160
, boost::shared_ptr<handler_type> h)
171
asio::async_read(m_sock, asio::buffer(m_buffer)
172
, boost::bind(&socks5_stream::handshake4, this, _1, h));
175
void socks5_stream::handshake4(asio::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_address(m_remote_endpoint.address(), p);
224
write_uint16(m_remote_endpoint.port(), p);
225
TORRENT_ASSERT(p - &m_buffer[0] == int(m_buffer.size()));
227
asio::async_write(m_sock, asio::buffer(m_buffer)
228
, boost::bind(&socks5_stream::connect1, this, _1, h));
231
void socks5_stream::connect1(asio::error_code const& e, boost::shared_ptr<handler_type> h)
241
m_buffer.resize(6 + 4); // assume an IPv4 address
242
asio::async_read(m_sock, asio::buffer(m_buffer)
243
, boost::bind(&socks5_stream::connect2, this, _1, h));
246
void socks5_stream::connect2(asio::error_code const& e, boost::shared_ptr<handler_type> h)
256
using namespace libtorrent::detail;
258
// send SOCKS5 connect command
259
char* p = &m_buffer[0];
260
int version = read_uint8(p);
263
(*h)(asio::error::operation_not_supported);
268
int response = read_uint8(p);
271
asio::error_code e = asio::error::fault;
274
case 1: e = asio::error::fault; break;
275
case 2: e = asio::error::no_permission; break;
276
case 3: e = asio::error::network_unreachable; break;
277
case 4: e = asio::error::host_unreachable; break;
278
case 5: e = asio::error::connection_refused; break;
279
case 6: e = asio::error::timed_out; break;
280
case 7: e = asio::error::operation_not_supported; break;
281
case 8: e = asio::error::address_family_not_supported; break;
289
int atyp = read_uint8(p);
290
// we ignore the proxy IP it was bound to
293
std::vector<char>().swap(m_buffer);
304
skip_bytes = read_uint8(p) - 3;
308
(*h)(asio::error::operation_not_supported);
313
m_buffer.resize(skip_bytes);
315
asio::async_read(m_sock, asio::buffer(m_buffer)
316
, boost::bind(&socks5_stream::connect3, this, _1, h));
319
void socks5_stream::connect3(asio::error_code const& e, boost::shared_ptr<handler_type> h)
329
std::vector<char>().swap(m_buffer);