2009-08-20 05:19:12 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2009-2016, Arvid Norberg
|
2009-08-20 05:19:12 +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_I2P_STREAM_HPP_INCLUDED
|
|
|
|
#define TORRENT_I2P_STREAM_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#include "libtorrent/proxy_base.hpp"
|
|
|
|
#include "libtorrent/session_settings.hpp"
|
2015-03-15 00:10:20 +01:00
|
|
|
#include "libtorrent/string_util.hpp"
|
2017-01-17 03:51:49 +01:00
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
namespace libtorrent {
|
|
|
|
|
|
|
|
namespace i2p_error {
|
|
|
|
|
2014-02-02 10:33:19 +01:00
|
|
|
// error values for the i2p_category error_category.
|
2009-08-20 05:19:12 +02:00
|
|
|
enum i2p_error_code
|
|
|
|
{
|
|
|
|
no_error = 0,
|
|
|
|
parse_failed,
|
|
|
|
cant_reach_peer,
|
|
|
|
i2p_error,
|
|
|
|
invalid_key,
|
|
|
|
invalid_id,
|
|
|
|
timeout,
|
|
|
|
key_not_found,
|
2013-10-27 20:56:37 +01:00
|
|
|
duplicated_id,
|
2009-08-20 05:19:12 +02:00
|
|
|
num_errors
|
|
|
|
};
|
2014-07-20 22:49:56 +02:00
|
|
|
|
2014-07-20 22:52:39 +02:00
|
|
|
// hidden
|
2014-07-20 22:49:56 +02:00
|
|
|
TORRENT_EXPORT boost::system::error_code make_error_code(i2p_error_code e);
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2014-02-02 05:07:36 +01:00
|
|
|
// returns the error category for I2P errors
|
2016-10-02 21:27:50 +02:00
|
|
|
TORRENT_EXPORT boost::system::error_category& i2p_category();
|
|
|
|
|
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
TORRENT_DEPRECATED
|
|
|
|
inline boost::system::error_category& get_i2p_category()
|
|
|
|
{ return i2p_category(); }
|
|
|
|
#endif
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
class i2p_stream : public proxy_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-10-27 20:56:37 +01:00
|
|
|
explicit i2p_stream(io_service& io_service);
|
|
|
|
~i2p_stream();
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
enum command_t
|
|
|
|
{
|
|
|
|
cmd_none,
|
|
|
|
cmd_create_session,
|
|
|
|
cmd_connect,
|
|
|
|
cmd_accept,
|
|
|
|
cmd_name_lookup,
|
|
|
|
cmd_incoming
|
|
|
|
};
|
|
|
|
|
|
|
|
void set_command(command_t c) { m_command = c; }
|
|
|
|
|
|
|
|
void set_session_id(char const* id) { m_id = id; }
|
|
|
|
|
2017-08-25 09:42:46 +02:00
|
|
|
void set_destination(string_view d) { m_dest = d.to_string(); }
|
2009-08-20 05:19:12 +02:00
|
|
|
std::string const& destination() { return m_dest; }
|
|
|
|
|
|
|
|
template <class Handler>
|
2014-07-07 08:28:48 +02:00
|
|
|
void async_connect(endpoint_type const&, Handler const& handler)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
|
|
|
// since we don't support regular endpoints, just ignore the one
|
|
|
|
// provided and use m_dest.
|
|
|
|
|
|
|
|
// the connect is split up in the following steps:
|
|
|
|
// 1. resolve name of proxy server
|
|
|
|
// 2. connect to SAM bridge
|
|
|
|
// 4 send command message (CONNECT/ACCEPT)
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using std::placeholders::_1;
|
|
|
|
using std::placeholders::_2;
|
2016-05-01 05:10:47 +02:00
|
|
|
tcp::resolver::query q(m_hostname, to_string(m_port).data());
|
2016-05-25 06:31:52 +02:00
|
|
|
m_resolver.async_resolve(q, std::bind(
|
2016-11-20 04:56:34 +01:00
|
|
|
&i2p_stream::do_connect, this, _1, _2, handler_type(std::move(handler))));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string name_lookup() const { return m_name_lookup; }
|
|
|
|
void set_name_lookup(char const* name) { m_name_lookup = name; }
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void send_name_lookup(handler_type h);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
private:
|
2015-04-27 04:21:12 +02:00
|
|
|
// explicitly disallow assignment, to silence msvc warning
|
|
|
|
i2p_stream& operator=(i2p_stream const&);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
void do_connect(error_code const& e, tcp::resolver::iterator i
|
2016-11-20 04:56:34 +01:00
|
|
|
, handler_type h);
|
|
|
|
void connected(error_code const& e, handler_type& h);
|
|
|
|
void start_read_line(error_code const& e, handler_type& h);
|
|
|
|
void read_line(error_code const& e, handler_type& h);
|
|
|
|
void send_connect(handler_type h);
|
|
|
|
void send_accept(handler_type h);
|
|
|
|
void send_session_create(handler_type h);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
// send and receive buffer
|
2017-01-17 03:51:49 +01:00
|
|
|
aux::vector<char> m_buffer;
|
2009-08-20 05:19:12 +02:00
|
|
|
char const* m_id;
|
|
|
|
int m_command; // 0 = connect, 1 = accept
|
|
|
|
std::string m_dest;
|
|
|
|
std::string m_name_lookup;
|
|
|
|
|
|
|
|
enum state_t
|
|
|
|
{
|
|
|
|
read_hello_response,
|
|
|
|
read_connect_response,
|
|
|
|
read_accept_response,
|
|
|
|
read_session_create_response,
|
2009-08-30 09:38:52 +02:00
|
|
|
read_name_lookup_response
|
2009-08-20 05:19:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int m_state;
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2013-10-27 20:56:37 +01:00
|
|
|
int m_magic;
|
|
|
|
#endif
|
2009-08-20 05:19:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class i2p_connection
|
|
|
|
{
|
|
|
|
public:
|
2016-10-08 20:07:11 +02:00
|
|
|
explicit i2p_connection(io_service& ios);
|
2009-08-20 05:19:12 +02:00
|
|
|
~i2p_connection();
|
|
|
|
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings proxy() const;
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
bool is_open() const
|
|
|
|
{
|
|
|
|
return m_sam_socket
|
|
|
|
&& m_sam_socket->is_open()
|
|
|
|
&& m_state != sam_connecting;
|
|
|
|
}
|
2016-11-20 04:56:34 +01:00
|
|
|
void open(std::string const& hostname, int port, i2p_stream::handler_type h);
|
2009-09-16 05:46:36 +02:00
|
|
|
void close(error_code&);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
char const* session_id() const { return m_session_id.c_str(); }
|
|
|
|
std::string const& local_endpoint() const { return m_i2p_local_endpoint; }
|
|
|
|
|
2016-08-02 23:58:15 +02:00
|
|
|
typedef std::function<void(error_code const&, char const*)> name_lookup_handler;
|
2009-08-20 05:19:12 +02:00
|
|
|
void async_name_lookup(char const* name, name_lookup_handler handler);
|
|
|
|
|
|
|
|
private:
|
2015-04-27 04:21:12 +02:00
|
|
|
// explicitly disallow assignment, to silence msvc warning
|
|
|
|
i2p_connection& operator=(i2p_connection const&);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void on_sam_connect(error_code const& ec, i2p_stream::handler_type& h
|
2016-08-18 23:08:40 +02:00
|
|
|
, std::shared_ptr<i2p_stream>);
|
2009-08-20 05:19:12 +02:00
|
|
|
void do_name_lookup(std::string const& name
|
2016-11-20 04:56:34 +01:00
|
|
|
, name_lookup_handler h);
|
2009-08-20 05:19:12 +02:00
|
|
|
void on_name_lookup(error_code const& ec
|
2016-11-20 04:56:34 +01:00
|
|
|
, name_lookup_handler& handler
|
2016-08-18 23:08:40 +02:00
|
|
|
, std::shared_ptr<i2p_stream>);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2014-09-02 08:28:27 +02:00
|
|
|
void set_local_endpoint(error_code const& ec, char const* dest
|
2016-11-20 04:56:34 +01:00
|
|
|
, i2p_stream::handler_type& h);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
// to talk to i2p SAM bridge
|
2016-08-18 23:08:40 +02:00
|
|
|
std::shared_ptr<i2p_stream> m_sam_socket;
|
2014-07-06 21:18:00 +02:00
|
|
|
std::string m_hostname;
|
|
|
|
int m_port;
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
// our i2p endpoint key
|
|
|
|
std::string m_i2p_local_endpoint;
|
|
|
|
std::string m_session_id;
|
|
|
|
|
2016-08-02 23:58:15 +02:00
|
|
|
std::list<std::pair<std::string, name_lookup_handler>> m_name_lookup;
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
enum state_t
|
|
|
|
{
|
|
|
|
sam_connecting,
|
|
|
|
sam_name_lookup,
|
|
|
|
sam_idle
|
|
|
|
};
|
|
|
|
|
|
|
|
state_t m_state;
|
|
|
|
|
|
|
|
io_service& m_io_service;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2014-07-20 22:49:56 +02:00
|
|
|
|
|
|
|
namespace boost { namespace system {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct is_error_code_enum<libtorrent::i2p_error::i2p_error_code>
|
2014-07-21 05:19:58 +02:00
|
|
|
{ static const bool value = true; };
|
|
|
|
|
2014-07-20 22:49:56 +02:00
|
|
|
} }
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#endif // TORRENT_USE_I2P
|
|
|
|
|
|
|
|
#endif
|