added missing file error_code.cpp
[libtorrent.git] / src / socks5_stream.cpp
blob3fb9ec666eaed6a188099afbcdcdd1281c16aa32
1 /*
3 Copyright (c) 2007, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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"
38 namespace libtorrent
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())
46 (*h)(e);
47 error_code ec;
48 close(ec);
49 return;
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)
58 if (e)
60 (*h)(e);
61 error_code ec;
62 close(ec);
63 return;
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
71 if (m_user.empty())
73 write_uint8(1, p); // 1 authentication method (no auth)
74 write_uint8(0, p); // no authentication
76 else
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)
88 if (e)
90 (*h)(e);
91 error_code ec;
92 close(ec);
93 return;
96 m_buffer.resize(2);
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)
103 if (e)
105 (*h)(e);
106 error_code ec;
107 close(ec);
108 return;
111 using namespace libtorrent::detail;
113 char* p = &m_buffer[0];
114 int version = read_uint8(p);
115 int method = read_uint8(p);
117 if (version < 5)
119 (*h)(asio::error::operation_not_supported);
120 error_code ec;
121 close(ec);
122 return;
125 if (method == 0)
127 socks_connect(h);
129 else if (method == 2)
131 if (m_user.empty())
133 (*h)(asio::error::operation_not_supported);
134 error_code ec;
135 close(ec);
136 return;
139 // start sub-negotiation
140 m_buffer.resize(m_user.size() + m_password.size() + 3);
141 char* p = &m_buffer[0];
142 write_uint8(1, p);
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));
150 else
152 (*h)(asio::error::operation_not_supported);
153 error_code ec;
154 close(ec);
155 return;
159 void socks5_stream::handshake3(error_code const& e
160 , boost::shared_ptr<handler_type> h)
162 if (e)
164 (*h)(e);
165 error_code ec;
166 close(ec);
167 return;
170 m_buffer.resize(2);
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)
178 if (e)
180 (*h)(e);
181 error_code ec;
182 close(ec);
183 return;
186 using namespace libtorrent::detail;
188 char* p = &m_buffer[0];
189 int version = read_uint8(p);
190 int status = read_uint8(p);
192 if (version != 1)
194 (*h)(asio::error::operation_not_supported);
195 error_code ec;
196 close(ec);
197 return;
200 if (status != 0)
202 (*h)(asio::error::operation_not_supported);
203 error_code ec;
204 close(ec);
205 return;
208 std::vector<char>().swap(m_buffer);
209 socks_connect(h);
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)
232 if (e)
234 (*h)(e);
235 error_code ec;
236 close(ec);
237 return;
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)
247 if (e)
249 (*h)(e);
250 error_code ec;
251 close(ec);
252 return;
255 using namespace libtorrent::detail;
257 // send SOCKS5 connect command
258 char* p = &m_buffer[0];
259 int version = read_uint8(p);
260 if (version < 5)
262 (*h)(asio::error::operation_not_supported);
263 error_code ec;
264 close(ec);
265 return;
267 int response = read_uint8(p);
268 if (response != 0)
270 error_code e = asio::error::fault;
271 switch (response)
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;
282 (*h)(e);
283 error_code ec;
284 close(ec);
285 return;
287 p += 1; // reserved
288 int atyp = read_uint8(p);
289 // we ignore the proxy IP it was bound to
290 if (atyp == 1)
292 std::vector<char>().swap(m_buffer);
293 (*h)(e);
294 return;
296 int skip_bytes = 0;
297 if (atyp == 4)
299 skip_bytes = 12;
301 else if (atyp == 3)
303 skip_bytes = read_uint8(p) - 3;
305 else
307 (*h)(asio::error::operation_not_supported);
308 error_code ec;
309 close(ec);
310 return;
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)
320 if (e)
322 (*h)(e);
323 error_code ec;
324 close(ec);
325 return;
328 std::vector<char>().swap(m_buffer);
329 (*h)(e);