2009-08-20 05:19:12 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2009-2018, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-08-29 07:29:01 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2016-07-10 07:24:06 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
#include "libtorrent/i2p_stream.hpp"
|
2017-09-02 23:58:10 +02:00
|
|
|
#include "libtorrent/aux_/proxy_settings.hpp"
|
2009-08-20 05:19:12 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2014-05-05 00:40:30 +02:00
|
|
|
#include "libtorrent/string_util.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/settings_pack.hpp"
|
2017-04-24 02:52:37 +02:00
|
|
|
#include "libtorrent/random.hpp"
|
2016-06-04 16:01:43 +02:00
|
|
|
#include "libtorrent/hex.hpp" // for to_hex
|
2016-07-10 07:24:06 +02:00
|
|
|
#include "libtorrent/debug.hpp"
|
2013-08-29 07:29:01 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2016-07-10 07:24:06 +02:00
|
|
|
#include <cstring>
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2019-01-19 22:47:48 +01:00
|
|
|
struct i2p_error_category final : boost::system::error_category
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2016-07-10 02:10:38 +02:00
|
|
|
const char* name() const BOOST_SYSTEM_NOEXCEPT override
|
2014-02-02 05:07:36 +01:00
|
|
|
{ return "i2p error"; }
|
2017-11-10 00:46:03 +01:00
|
|
|
std::string message(int ev) const override
|
2014-02-02 05:07:36 +01:00
|
|
|
{
|
|
|
|
static char const* messages[] =
|
|
|
|
{
|
|
|
|
"no error",
|
|
|
|
"parse failed",
|
|
|
|
"cannot reach peer",
|
|
|
|
"i2p error",
|
|
|
|
"invalid key",
|
|
|
|
"invalid id",
|
|
|
|
"timeout",
|
|
|
|
"key not found",
|
|
|
|
"duplicated id"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ev < 0 || ev >= i2p_error::num_errors) return "unknown error";
|
|
|
|
return messages[ev];
|
|
|
|
}
|
2016-07-10 02:10:38 +02:00
|
|
|
boost::system::error_condition default_error_condition(
|
|
|
|
int ev) const BOOST_SYSTEM_NOEXCEPT override
|
2018-01-11 01:35:15 +01:00
|
|
|
{ return {ev, *this}; }
|
2014-02-02 05:07:36 +01:00
|
|
|
};
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2016-10-02 21:27:50 +02:00
|
|
|
boost::system::error_category& i2p_category()
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2014-02-02 05:07:36 +01:00
|
|
|
static i2p_error_category i2p_category;
|
|
|
|
return i2p_category;
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
2014-07-20 22:49:56 +02:00
|
|
|
|
|
|
|
namespace i2p_error
|
|
|
|
{
|
|
|
|
boost::system::error_code make_error_code(i2p_error_code e)
|
|
|
|
{
|
2018-01-11 01:35:15 +01:00
|
|
|
return {e, i2p_category()};
|
2014-07-20 22:49:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
i2p_connection::i2p_connection(io_service& ios)
|
2015-05-05 04:32:14 +02:00
|
|
|
: m_port(0)
|
|
|
|
, m_state(sam_idle)
|
2011-02-21 06:24:41 +01:00
|
|
|
, m_io_service(ios)
|
2014-02-02 05:07:36 +01:00
|
|
|
{}
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2016-07-10 13:34:45 +02:00
|
|
|
i2p_connection::~i2p_connection() = default;
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2009-09-16 05:46:36 +02:00
|
|
|
void i2p_connection::close(error_code& e)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2009-09-16 05:46:36 +02:00
|
|
|
if (m_sam_socket) m_sam_socket->close(e);
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings i2p_connection::proxy() const
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2015-08-25 04:18:10 +02:00
|
|
|
aux::proxy_settings ret;
|
2014-07-06 21:18:00 +02:00
|
|
|
ret.hostname = m_hostname;
|
2016-11-25 17:17:25 +01:00
|
|
|
ret.port = std::uint16_t(m_port);
|
2014-07-06 21:18:00 +02:00
|
|
|
ret.type = settings_pack::i2p_proxy;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void i2p_connection::open(std::string const& s, int port
|
2016-11-20 04:56:34 +01:00
|
|
|
, i2p_stream::handler_type handler)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
|
|
|
// we already seem to have a session to this SAM router
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_hostname == s
|
|
|
|
&& m_port == port
|
2013-10-27 20:56:37 +01:00
|
|
|
&& m_sam_socket
|
|
|
|
&& (is_open() || m_state == sam_connecting)) return;
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
m_hostname = s;
|
|
|
|
m_port = port;
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_hostname.empty()) return;
|
2011-03-07 08:02:30 +01:00
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
m_state = sam_connecting;
|
|
|
|
|
|
|
|
char tmp[20];
|
2017-04-24 02:52:37 +02:00
|
|
|
aux::random_bytes(tmp);
|
2009-08-20 05:19:12 +02:00
|
|
|
m_session_id.resize(sizeof(tmp)*2);
|
2016-07-29 08:36:15 +02:00
|
|
|
aux::to_hex(tmp, &m_session_id[0]);
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
m_sam_socket.reset(new i2p_stream(m_io_service));
|
2014-07-06 21:18:00 +02:00
|
|
|
m_sam_socket->set_proxy(m_hostname, m_port);
|
2009-08-20 05:19:12 +02:00
|
|
|
m_sam_socket->set_command(i2p_stream::cmd_create_session);
|
|
|
|
m_sam_socket->set_session_id(m_session_id.c_str());
|
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::on_sam_connect");
|
2009-08-20 05:19:12 +02:00
|
|
|
m_sam_socket->async_connect(tcp::endpoint()
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_connection::on_sam_connect, this, _1
|
|
|
|
, std::move(handler), m_sam_socket));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 23:08:40 +02:00
|
|
|
void i2p_connection::on_sam_connect(error_code const& ec
|
2016-11-20 04:56:34 +01:00
|
|
|
, i2p_stream::handler_type& h, std::shared_ptr<i2p_stream>)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("i2p_stream::on_sam_connect");
|
2009-08-20 05:19:12 +02:00
|
|
|
m_state = sam_idle;
|
2015-07-06 00:33:43 +02:00
|
|
|
|
2014-09-02 08:28:27 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
h(ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
do_name_lookup("ME", std::bind(&i2p_connection::set_local_endpoint
|
|
|
|
, this, _1, _2, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2014-09-02 08:28:27 +02:00
|
|
|
void i2p_connection::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
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
if (!ec && dest != nullptr)
|
2014-09-02 08:28:27 +02:00
|
|
|
m_i2p_local_endpoint = dest;
|
|
|
|
else
|
2009-08-20 05:19:12 +02:00
|
|
|
m_i2p_local_endpoint.clear();
|
2014-09-02 08:28:27 +02:00
|
|
|
|
|
|
|
h(ec);
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2p_connection::async_name_lookup(char const* name
|
|
|
|
, i2p_connection::name_lookup_handler handler)
|
|
|
|
{
|
2014-03-15 23:20:19 +01:00
|
|
|
if (m_state == sam_idle && m_name_lookup.empty() && is_open())
|
2016-11-20 04:56:34 +01:00
|
|
|
do_name_lookup(name, std::move(handler));
|
2009-08-20 05:19:12 +02:00
|
|
|
else
|
2018-01-11 01:35:15 +01:00
|
|
|
m_name_lookup.emplace_back(std::string(name)
|
|
|
|
, std::move(handler));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2p_connection::do_name_lookup(std::string const& name
|
2016-11-20 04:56:34 +01:00
|
|
|
, name_lookup_handler handler)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_state == sam_idle);
|
|
|
|
m_state = sam_name_lookup;
|
|
|
|
m_sam_socket->set_name_lookup(name.c_str());
|
2016-11-20 04:56:34 +01:00
|
|
|
m_sam_socket->send_name_lookup(std::bind(&i2p_connection::on_name_lookup
|
|
|
|
, this, _1, std::move(handler), m_sam_socket));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2p_connection::on_name_lookup(error_code const& ec
|
2016-11-20 04:56:34 +01:00
|
|
|
, name_lookup_handler& handler, std::shared_ptr<i2p_stream>)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
|
|
|
m_state = sam_idle;
|
|
|
|
|
|
|
|
std::string name = m_sam_socket->name_lookup();
|
|
|
|
if (!m_name_lookup.empty())
|
|
|
|
{
|
|
|
|
std::pair<std::string, name_lookup_handler>& nl = m_name_lookup.front();
|
2018-01-11 01:35:15 +01:00
|
|
|
do_name_lookup(nl.first, std::move(nl.second));
|
2009-08-20 05:19:12 +02:00
|
|
|
m_name_lookup.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
handler(ec, nullptr);
|
2009-08-20 05:19:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler(ec, name.c_str());
|
|
|
|
}
|
|
|
|
|
2013-10-27 20:56:37 +01:00
|
|
|
i2p_stream::i2p_stream(io_service& io_service)
|
|
|
|
: proxy_base(io_service)
|
2016-07-09 22:26:26 +02:00
|
|
|
, m_id(nullptr)
|
2013-10-27 20:56:37 +01:00
|
|
|
, m_command(cmd_create_session)
|
2017-12-02 19:33:56 +01:00
|
|
|
, m_state(read_hello_response)
|
2013-10-27 20:56:37 +01:00
|
|
|
{
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2013-10-27 20:56:37 +01:00
|
|
|
m_magic = 0x1337;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:35:15 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2013-10-27 20:56:37 +01:00
|
|
|
i2p_stream::~i2p_stream()
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
|
|
|
m_magic = 0;
|
|
|
|
}
|
2018-01-11 01:35:15 +01:00
|
|
|
#endif
|
2013-10-27 20:56:37 +01:00
|
|
|
|
2009-08-20 05:19:12 +02:00
|
|
|
void i2p_stream::do_connect(error_code const& e, tcp::resolver::iterator i
|
2016-11-20 04:56:34 +01:00
|
|
|
, handler_type h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2009-08-20 05:19:12 +02:00
|
|
|
if (e || i == tcp::resolver::iterator())
|
|
|
|
{
|
2016-11-20 04:56:34 +01:00
|
|
|
h(e);
|
2009-08-20 05:19:12 +02:00
|
|
|
error_code ec;
|
|
|
|
close(ec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::connected");
|
2016-05-25 06:31:52 +02:00
|
|
|
m_sock.async_connect(i->endpoint(), std::bind(
|
2016-11-20 04:56:34 +01:00
|
|
|
&i2p_stream::connected, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void i2p_stream::connected(error_code const& e, handler_type& h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("i2p_stream::connected");
|
2009-08-20 05:19:12 +02:00
|
|
|
if (handle_error(e, h)) return;
|
|
|
|
|
|
|
|
// send hello command
|
|
|
|
m_state = read_hello_response;
|
|
|
|
static const char cmd[] = "HELLO VERSION MIN=3.0 MAX=3.0\n";
|
2013-08-21 17:41:19 +02:00
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::start_read_line");
|
2015-06-06 07:22:53 +02:00
|
|
|
async_write(m_sock, boost::asio::buffer(cmd, sizeof(cmd) - 1)
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_stream::start_read_line, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void i2p_stream::start_read_line(error_code const& e, handler_type& h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("i2p_stream::start_read_line");
|
2009-08-20 05:19:12 +02:00
|
|
|
if (handle_error(e, h)) return;
|
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::read_line");
|
2009-08-20 05:19:12 +02:00
|
|
|
m_buffer.resize(1);
|
2015-06-06 07:22:53 +02:00
|
|
|
async_read(m_sock, boost::asio::buffer(m_buffer)
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_stream::read_line, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void i2p_stream::read_line(error_code const& e, handler_type& h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("i2p_stream::read_line");
|
2009-08-20 05:19:12 +02:00
|
|
|
if (handle_error(e, h)) return;
|
|
|
|
|
2018-01-11 01:35:15 +01:00
|
|
|
auto const read_pos = int(m_buffer.size());
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
// look for \n which means end of the response
|
|
|
|
if (m_buffer[read_pos - 1] != '\n')
|
|
|
|
{
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::read_line");
|
2009-08-20 05:19:12 +02:00
|
|
|
// read another byte from the socket
|
|
|
|
m_buffer.resize(read_pos + 1);
|
2015-06-06 07:22:53 +02:00
|
|
|
async_read(m_sock, boost::asio::buffer(&m_buffer[read_pos], 1)
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_stream::read_line, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_buffer[read_pos - 1] = 0;
|
|
|
|
|
|
|
|
if (m_command == cmd_incoming)
|
|
|
|
{
|
|
|
|
// this is the line containing the destination
|
|
|
|
// of the incoming connection in an accept call
|
|
|
|
m_dest = &m_buffer[0];
|
2016-11-20 04:56:34 +01:00
|
|
|
h(e);
|
2009-08-20 05:19:12 +02:00
|
|
|
std::vector<char>().swap(m_buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code invalid_response(i2p_error::parse_failed
|
2016-10-02 21:27:50 +02:00
|
|
|
, i2p_category());
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2017-03-18 17:19:49 +01:00
|
|
|
string_view expect1;
|
|
|
|
string_view expect2;
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
switch (m_state)
|
|
|
|
{
|
|
|
|
case read_hello_response:
|
2017-03-18 17:19:49 +01:00
|
|
|
expect1 = "HELLO"_sv;
|
|
|
|
expect2 = "REPLY"_sv;
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
case read_connect_response:
|
|
|
|
case read_accept_response:
|
2017-03-18 17:19:49 +01:00
|
|
|
expect1 = "STREAM"_sv;
|
|
|
|
expect2 = "STATUS"_sv;
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
case read_session_create_response:
|
2017-03-18 17:19:49 +01:00
|
|
|
expect1 = "SESSION"_sv;
|
|
|
|
expect2 = "STATUS"_sv;
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
case read_name_lookup_response:
|
2017-03-18 17:19:49 +01:00
|
|
|
expect1 = "NAMING"_sv;
|
|
|
|
expect2 = "REPLY"_sv;
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-02 00:17:21 +02:00
|
|
|
string_view remaining(m_buffer.data(), m_buffer.size());
|
|
|
|
string_view token;
|
|
|
|
|
|
|
|
std::tie(token, remaining) = split_string(remaining, ' ');
|
|
|
|
if (expect1.empty() || expect1 != token)
|
2016-07-10 07:24:06 +02:00
|
|
|
{ handle_error(invalid_response, h); return; }
|
2017-09-02 00:17:21 +02:00
|
|
|
|
|
|
|
std::tie(token, remaining) = split_string(remaining, ' ');
|
|
|
|
if (expect2.empty() || expect2 != token)
|
2016-07-10 07:24:06 +02:00
|
|
|
{ handle_error(invalid_response, h); return; }
|
2009-08-20 05:19:12 +02:00
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2017-09-02 00:17:21 +02:00
|
|
|
string_view name;
|
|
|
|
std::tie(name, remaining) = split_string(remaining, '=');
|
|
|
|
if (name.empty()) break;
|
|
|
|
string_view value;
|
|
|
|
std::tie(value, remaining) = split_string(remaining, ' ');
|
|
|
|
if (value.empty()) { handle_error(invalid_response, h); return; }
|
2009-08-20 05:19:12 +02:00
|
|
|
|
2017-03-18 17:19:49 +01:00
|
|
|
if ("RESULT"_sv == name)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2017-09-02 00:17:21 +02:00
|
|
|
if ("OK"_sv == value)
|
2009-08-20 05:19:12 +02:00
|
|
|
result = i2p_error::no_error;
|
2017-09-02 00:17:21 +02:00
|
|
|
else if ("CANT_REACH_PEER"_sv == value)
|
2009-08-20 05:19:12 +02:00
|
|
|
result = i2p_error::cant_reach_peer;
|
2017-09-02 00:17:21 +02:00
|
|
|
else if ("I2P_ERROR"_sv == value)
|
2009-08-20 05:19:12 +02:00
|
|
|
result = i2p_error::i2p_error;
|
2017-09-02 00:17:21 +02:00
|
|
|
else if ("INVALID_KEY"_sv == value)
|
2009-08-20 05:19:12 +02:00
|
|
|
result = i2p_error::invalid_key;
|
2017-09-02 00:17:21 +02:00
|
|
|
else if ("INVALID_ID"_sv == value)
|
2009-08-20 05:19:12 +02:00
|
|
|
result = i2p_error::invalid_id;
|
2017-09-02 00:17:21 +02:00
|
|
|
else if ("TIMEOUT"_sv == value)
|
2009-08-20 05:19:12 +02:00
|
|
|
result = i2p_error::timeout;
|
2017-09-02 00:17:21 +02:00
|
|
|
else if ("KEY_NOT_FOUND"_sv == value)
|
2009-08-20 05:19:12 +02:00
|
|
|
result = i2p_error::key_not_found;
|
2017-09-02 00:17:21 +02:00
|
|
|
else if ("DUPLICATED_ID"_sv == value)
|
2013-10-27 20:56:37 +01:00
|
|
|
result = i2p_error::duplicated_id;
|
2009-08-20 05:19:12 +02:00
|
|
|
else
|
|
|
|
result = i2p_error::num_errors; // unknown error
|
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
/*else if ("MESSAGE" == name)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
else if ("VERSION"_sv == name)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2016-07-10 07:24:06 +02:00
|
|
|
}*/
|
2017-03-18 17:19:49 +01:00
|
|
|
else if ("VALUE"_sv == name)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2017-09-02 00:17:21 +02:00
|
|
|
m_name_lookup = value.to_string();
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
2017-03-18 17:19:49 +01:00
|
|
|
else if ("DESTINATION"_sv == name)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2017-09-02 00:17:21 +02:00
|
|
|
m_dest = value.to_string();
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 21:27:50 +02:00
|
|
|
error_code ec(result, i2p_category());
|
2015-06-14 21:17:44 +02:00
|
|
|
switch (result)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2015-06-14 21:17:44 +02:00
|
|
|
case i2p_error::no_error:
|
|
|
|
case i2p_error::invalid_key:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
handle_error (ec, h);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (m_state)
|
|
|
|
{
|
|
|
|
case read_hello_response:
|
|
|
|
switch (m_command)
|
|
|
|
{
|
|
|
|
case cmd_create_session:
|
2016-11-20 04:56:34 +01:00
|
|
|
send_session_create(std::move(h));
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
case cmd_accept:
|
2016-11-20 04:56:34 +01:00
|
|
|
send_accept(std::move(h));
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
case cmd_connect:
|
2016-11-20 04:56:34 +01:00
|
|
|
send_connect(std::move(h));
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
2017-12-02 19:33:56 +01:00
|
|
|
case cmd_none:
|
|
|
|
case cmd_name_lookup:
|
|
|
|
case cmd_incoming:
|
2016-11-20 04:56:34 +01:00
|
|
|
h(e);
|
2009-08-20 05:19:12 +02:00
|
|
|
std::vector<char>().swap(m_buffer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case read_connect_response:
|
|
|
|
case read_session_create_response:
|
|
|
|
case read_name_lookup_response:
|
2016-11-20 04:56:34 +01:00
|
|
|
h(ec);
|
2009-08-20 05:19:12 +02:00
|
|
|
std::vector<char>().swap(m_buffer);
|
|
|
|
break;
|
|
|
|
case read_accept_response:
|
|
|
|
// the SAM bridge is waiting for an incoming
|
|
|
|
// connection.
|
|
|
|
// wait for one more line containing
|
|
|
|
// the destination of the remote peer
|
|
|
|
m_command = cmd_incoming;
|
|
|
|
m_buffer.resize(1);
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::read_line");
|
2015-06-06 07:22:53 +02:00
|
|
|
async_read(m_sock, boost::asio::buffer(m_buffer)
|
2016-05-25 06:31:52 +02:00
|
|
|
, std::bind(&i2p_stream::read_line, this, _1, h));
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void i2p_stream::send_connect(handler_type h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2009-08-20 05:19:12 +02:00
|
|
|
m_state = read_connect_response;
|
|
|
|
char cmd[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
int size = std::snprintf(cmd, sizeof(cmd), "STREAM CONNECT ID=%s DESTINATION=%s\n"
|
2009-08-20 05:19:12 +02:00
|
|
|
, m_id, m_dest.c_str());
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::start_read_line");
|
2017-01-17 03:51:49 +01:00
|
|
|
async_write(m_sock, boost::asio::buffer(cmd, std::size_t(size))
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_stream::start_read_line, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void i2p_stream::send_accept(handler_type h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2009-08-20 05:19:12 +02:00
|
|
|
m_state = read_accept_response;
|
|
|
|
char cmd[400];
|
2016-05-17 15:24:06 +02:00
|
|
|
int size = std::snprintf(cmd, sizeof(cmd), "STREAM ACCEPT ID=%s\n", m_id);
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::start_read_line");
|
2017-01-17 03:51:49 +01:00
|
|
|
async_write(m_sock, boost::asio::buffer(cmd, std::size_t(size))
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_stream::start_read_line, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void i2p_stream::send_session_create(handler_type h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2009-08-20 05:19:12 +02:00
|
|
|
m_state = read_session_create_response;
|
|
|
|
char cmd[400];
|
2016-05-17 15:24:06 +02:00
|
|
|
int size = std::snprintf(cmd, sizeof(cmd), "SESSION CREATE STYLE=STREAM ID=%s DESTINATION=TRANSIENT\n"
|
2009-08-20 05:19:12 +02:00
|
|
|
, m_id);
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::start_read_line");
|
2017-01-17 03:51:49 +01:00
|
|
|
async_write(m_sock, boost::asio::buffer(cmd, std::size_t(size))
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_stream::start_read_line, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 04:56:34 +01:00
|
|
|
void i2p_stream::send_name_lookup(handler_type h)
|
2009-08-20 05:19:12 +02:00
|
|
|
{
|
2013-10-27 20:56:37 +01:00
|
|
|
TORRENT_ASSERT(m_magic == 0x1337);
|
2009-08-20 05:19:12 +02:00
|
|
|
m_state = read_name_lookup_response;
|
|
|
|
char cmd[1024];
|
2016-05-17 15:24:06 +02:00
|
|
|
int size = std::snprintf(cmd, sizeof(cmd), "NAMING LOOKUP NAME=%s\n", m_name_lookup.c_str());
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("i2p_stream::start_read_line");
|
2017-01-17 03:51:49 +01:00
|
|
|
async_write(m_sock, boost::asio::buffer(cmd, std::size_t(size))
|
2016-11-20 04:56:34 +01:00
|
|
|
, std::bind(&i2p_stream::start_read_line, this, _1, std::move(h)));
|
2009-08-20 05:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|