diff --git a/include/libtorrent/socks5_stream.hpp b/include/libtorrent/socks5_stream.hpp index 2099f77eb..b1cd19b0c 100644 --- a/include/libtorrent/socks5_stream.hpp +++ b/include/libtorrent/socks5_stream.hpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include "libtorrent/proxy_base.hpp" +#include "libtorrent/assert.hpp" #if defined TORRENT_ASIO_DEBUGGING #include "libtorrent/debug.hpp" #endif @@ -73,16 +74,27 @@ class socks5_stream : public proxy_base { public: + // commands + enum { + socks5_connect = 1, + socks5_bind = 2, + socks5_udp_associate = 3 + }; + explicit socks5_stream(io_service& io_service) : proxy_base(io_service) , m_version(5) - , m_command(1) + , m_command(socks5_connect) , m_listen(0) {} void set_version(int v) { m_version = v; } - void set_command(int c) { m_command = c; } + void set_command(int c) + { + TORRENT_ASSERT(c >= socks5_connect && c <=socks5_udp_associate); + m_command = c; + } void set_username(std::string const& user , std::string const& password) @@ -114,11 +126,17 @@ public: } #endif -//#error fix error messages to use custom error_code category -//#error add async_connect() that takes a hostname and port as well + // TODO: 2 fix error messages to use custom error_code category + // TODO: 2 add async_connect() that takes a hostname and port as well template void async_connect(endpoint_type const& endpoint, Handler const& handler) { + // make sure we don't try to connect to INADDR_ANY. binding is fine, + // and using a hostname is fine on SOCKS version 5. + TORRENT_ASSERT(m_command == socks5_bind + || endpoint.address() != address() + || (!m_dst_name.empty() && m_version == 5)); + m_remote_endpoint = endpoint; // the connect is split up in the following steps: @@ -163,7 +181,11 @@ private: std::string m_password; std::string m_dst_name; int m_version; + + // the socks command to send for this connection (connect, bind, + // udp associate) int m_command; + // set to one when we're waiting for the // second message to accept an incoming connection int m_listen; diff --git a/src/socks5_stream.cpp b/src/socks5_stream.cpp index 230d33e5c..a58476e55 100644 --- a/src/socks5_stream.cpp +++ b/src/socks5_stream.cpp @@ -294,6 +294,10 @@ namespace libtorrent } else { + // we either need a hostname or a valid endpoint + TORRENT_ASSERT(m_command == socks5_bind + || m_remote_endpoint.address() != address()); + write_uint8(m_remote_endpoint.address().is_v4()?1:4, p); // address type write_address(m_remote_endpoint.address(), p); } @@ -398,7 +402,7 @@ namespace libtorrent // we ignore the proxy IP it was bound to if (atyp == 1) { - if (m_command == 2) + if (m_command == socks5_bind) { if (m_listen == 0) { @@ -459,7 +463,7 @@ namespace libtorrent // access granted if (response == 90) { - if (m_command == 2) + if (m_command == socks5_bind) { if (m_listen == 0) { @@ -505,7 +509,7 @@ namespace libtorrent if (handle_error(e, h)) return; - if (m_command == 2) + if (m_command == socks5_bind) { if (m_listen == 0) {