2007-05-30 11:07:33 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Arvid Norberg
|
2007-05-30 11:07:33 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_PROXY_BASE_HPP_INCLUDED
|
|
|
|
#define TORRENT_PROXY_BASE_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/io.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/io_service_fwd.hpp"
|
2007-05-30 11:07:33 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/address.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2015-04-19 00:00:27 +02:00
|
|
|
|
2007-05-30 11:07:33 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-11-17 20:34:49 +01:00
|
|
|
class proxy_base
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-11-17 20:34:49 +01:00
|
|
|
using handler_type = std::function<void(error_code const&)>;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
typedef tcp::socket next_layer_type;
|
|
|
|
typedef tcp::socket::lowest_layer_type lowest_layer_type;
|
|
|
|
typedef tcp::socket::endpoint_type endpoint_type;
|
|
|
|
typedef tcp::socket::protocol_type protocol_type;
|
2007-05-30 11:07:33 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
explicit proxy_base(io_service& io_service);
|
|
|
|
~proxy_base();
|
2016-11-17 20:34:49 +01:00
|
|
|
proxy_base(proxy_base const&) = delete;
|
|
|
|
proxy_base& operator=(proxy_base const&) = delete;
|
2007-05-30 11:07:33 +02:00
|
|
|
|
|
|
|
void set_proxy(std::string hostname, int port)
|
|
|
|
{
|
|
|
|
m_hostname = hostname;
|
|
|
|
m_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Mutable_Buffers, class Handler>
|
|
|
|
void async_read_some(Mutable_Buffers const& buffers, Handler const& handler)
|
|
|
|
{
|
|
|
|
m_sock.async_read_some(buffers, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Mutable_Buffers>
|
2008-05-03 18:05:42 +02:00
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers, error_code& ec)
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
|
|
|
return m_sock.read_some(buffers, ec);
|
|
|
|
}
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
template <class Const_Buffers>
|
|
|
|
std::size_t write_some(Const_Buffers const& buffers, error_code& ec)
|
|
|
|
{
|
|
|
|
return m_sock.write_some(buffers, ec);
|
|
|
|
}
|
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
std::size_t available(error_code& ec) const
|
|
|
|
{ return m_sock.available(ec); }
|
|
|
|
|
2007-12-28 20:26:26 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-11-23 09:38:50 +01:00
|
|
|
std::size_t available() const
|
|
|
|
{ return m_sock.available(); }
|
|
|
|
|
2007-05-30 11:07:33 +02:00
|
|
|
template <class Mutable_Buffers>
|
|
|
|
std::size_t read_some(Mutable_Buffers const& buffers)
|
|
|
|
{
|
|
|
|
return m_sock.read_some(buffers);
|
|
|
|
}
|
|
|
|
|
2010-10-12 10:57:43 +02:00
|
|
|
template <class Const_Buffers>
|
|
|
|
std::size_t write_some(Const_Buffers const& buffers)
|
|
|
|
{
|
|
|
|
return m_sock.write_some(buffers);
|
|
|
|
}
|
|
|
|
|
2007-05-30 11:07:33 +02:00
|
|
|
template <class IO_Control_Command>
|
|
|
|
void io_control(IO_Control_Command& ioc)
|
|
|
|
{
|
|
|
|
m_sock.io_control(ioc);
|
|
|
|
}
|
2007-12-28 20:26:26 +01:00
|
|
|
#endif
|
2007-05-30 11:07:33 +02:00
|
|
|
|
|
|
|
template <class IO_Control_Command>
|
2008-05-03 18:05:42 +02:00
|
|
|
void io_control(IO_Control_Command& ioc, error_code& ec)
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
|
|
|
m_sock.io_control(ioc, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Const_Buffers, class Handler>
|
|
|
|
void async_write_some(Const_Buffers const& buffers, Handler const& handler)
|
|
|
|
{
|
|
|
|
m_sock.async_write_some(buffers, handler);
|
|
|
|
}
|
|
|
|
|
2008-03-12 17:58:23 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class SettableSocketOption>
|
|
|
|
void set_option(SettableSocketOption const& opt)
|
|
|
|
{
|
|
|
|
m_sock.set_option(opt);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class SettableSocketOption>
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code set_option(SettableSocketOption const& opt, error_code& ec)
|
2008-03-12 17:58:23 +01:00
|
|
|
{
|
|
|
|
return m_sock.set_option(opt, ec);
|
|
|
|
}
|
|
|
|
|
2013-09-03 10:39:30 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
template <class GettableSocketOption>
|
2013-09-03 18:02:24 +02:00
|
|
|
void get_option(GettableSocketOption& opt)
|
2013-09-03 10:39:30 +02:00
|
|
|
{
|
|
|
|
m_sock.get_option(opt);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class GettableSocketOption>
|
|
|
|
error_code get_option(GettableSocketOption& opt, error_code& ec)
|
|
|
|
{
|
|
|
|
return m_sock.get_option(opt, ec);
|
|
|
|
}
|
|
|
|
|
2007-12-28 20:26:26 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2014-07-07 08:28:48 +02:00
|
|
|
void bind(endpoint_type const& /* endpoint */)
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2010-08-03 11:08:37 +02:00
|
|
|
// m_sock.bind(endpoint);
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
2007-12-28 20:26:26 +01:00
|
|
|
#endif
|
2007-05-30 11:07:33 +02:00
|
|
|
|
2014-10-06 05:03:01 +02:00
|
|
|
error_code cancel(error_code& ec)
|
|
|
|
{
|
|
|
|
return m_sock.cancel(ec);
|
|
|
|
}
|
|
|
|
|
2014-07-07 08:28:48 +02:00
|
|
|
void bind(endpoint_type const& /* endpoint */, error_code& /* ec */)
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2010-08-03 11:08:37 +02:00
|
|
|
// the reason why we ignore binds here is because we don't
|
|
|
|
// (necessarily) yet know what address family the proxy
|
|
|
|
// will resolve to, and binding to the wrong one would
|
|
|
|
// break our connection attempt later. The caller here
|
|
|
|
// doesn't necessarily know that we're proxying, so this
|
|
|
|
// bind address is based on the final endpoint, not the
|
|
|
|
// proxy.
|
|
|
|
// TODO: it would be nice to remember the bind port and bind once we know where the proxy is
|
|
|
|
// m_sock.bind(endpoint, ec);
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 20:26:26 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2014-07-07 08:28:48 +02:00
|
|
|
void open(protocol_type const&)
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2010-08-03 11:08:37 +02:00
|
|
|
// m_sock.open(p);
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
2007-12-28 20:26:26 +01:00
|
|
|
#endif
|
2007-05-30 11:07:33 +02:00
|
|
|
|
2014-07-07 08:28:48 +02:00
|
|
|
void open(protocol_type const&, error_code&)
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2010-08-03 11:08:37 +02:00
|
|
|
// we need to ignore this for the same reason as stated
|
|
|
|
// for ignoring bind()
|
|
|
|
// m_sock.open(p, ec);
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 20:26:26 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2007-05-30 11:07:33 +02:00
|
|
|
void close()
|
|
|
|
{
|
|
|
|
m_remote_endpoint = endpoint_type();
|
|
|
|
m_sock.close();
|
2007-11-19 06:18:50 +01:00
|
|
|
m_resolver.cancel();
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
2007-12-28 20:26:26 +01:00
|
|
|
#endif
|
2007-05-30 11:07:33 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void close(error_code& ec)
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2010-08-03 11:08:37 +02:00
|
|
|
m_remote_endpoint = endpoint_type();
|
2007-10-22 12:54:48 +02:00
|
|
|
m_sock.close(ec);
|
2007-11-11 20:09:29 +01:00
|
|
|
m_resolver.cancel();
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 20:26:26 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2008-01-27 23:39:50 +01:00
|
|
|
endpoint_type remote_endpoint() const
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
|
|
|
return m_remote_endpoint;
|
|
|
|
}
|
2007-12-28 20:26:26 +01:00
|
|
|
#endif
|
2007-05-30 11:07:33 +02:00
|
|
|
|
2010-07-03 08:56:09 +02:00
|
|
|
endpoint_type remote_endpoint(error_code& ec) const
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2015-06-06 07:22:53 +02:00
|
|
|
if (!m_sock.is_open()) ec = boost::asio::error::not_connected;
|
2007-05-30 11:07:33 +02:00
|
|
|
return m_remote_endpoint;
|
|
|
|
}
|
|
|
|
|
2007-12-28 20:26:26 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2008-01-27 23:39:50 +01:00
|
|
|
endpoint_type local_endpoint() const
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
|
|
|
return m_sock.local_endpoint();
|
|
|
|
}
|
2007-12-28 20:26:26 +01:00
|
|
|
#endif
|
2007-05-30 11:07:33 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
endpoint_type local_endpoint(error_code& ec) const
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2007-10-22 12:54:48 +02:00
|
|
|
return m_sock.local_endpoint(ec);
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
io_service& get_io_service()
|
2007-05-30 11:07:33 +02:00
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
return m_sock.get_io_service();
|
2007-05-30 11:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lowest_layer_type& lowest_layer()
|
|
|
|
{
|
|
|
|
return m_sock.lowest_layer();
|
|
|
|
}
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2011-09-10 07:52:07 +02:00
|
|
|
next_layer_type& next_layer()
|
|
|
|
{
|
|
|
|
return m_sock;
|
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
bool is_open() const { return m_sock.is_open(); }
|
2015-06-06 07:22:53 +02:00
|
|
|
|
2007-05-30 11:07:33 +02:00
|
|
|
protected:
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
bool handle_error(error_code const& e, handler_type const& h);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-06-06 19:49:18 +02:00
|
|
|
tcp::socket m_sock;
|
2007-05-30 11:07:33 +02:00
|
|
|
std::string m_hostname;
|
|
|
|
int m_port;
|
|
|
|
|
|
|
|
endpoint_type m_remote_endpoint;
|
|
|
|
|
2014-10-23 02:19:11 +02:00
|
|
|
// TODO: 2 use the resolver interface that has a built-in cache
|
2007-05-30 11:07:33 +02:00
|
|
|
tcp::resolver m_resolver;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|