2007-03-16 06:29:23 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Arvid Norberg
|
2007-03-16 06:29:23 +01: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.
|
|
|
|
|
|
|
|
*/
|
2012-09-24 18:13:57 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2015-08-20 01:33:20 +02:00
|
|
|
|
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
|
|
|
|
2012-09-24 18:13:57 +02:00
|
|
|
#if defined TORRENT_OS2
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2007-03-16 06:29:23 +01:00
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
#include <boost/asio/ip/host_name.hpp>
|
2008-05-20 18:43:09 +02:00
|
|
|
|
2015-08-20 01:33:20 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
#include <cstdio> // for snprintf
|
2016-06-23 07:23:59 +02:00
|
|
|
#include <cinttypes> // for PRId64 et.al.
|
2016-05-18 07:54:37 +02:00
|
|
|
#include <cstdarg>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2016-05-17 15:24:06 +02:00
|
|
|
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/natpmp.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
2007-10-01 19:21:19 +02:00
|
|
|
#include "libtorrent/enum_net.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/io_service.hpp"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp"
|
2016-04-23 23:29:25 +02:00
|
|
|
#include "libtorrent/debug.hpp"
|
2015-03-15 00:10:20 +01:00
|
|
|
#include "libtorrent/aux_/escape_string.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
using namespace libtorrent;
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
natpmp::natpmp(io_service& ios
|
2016-09-16 15:53:17 +02:00
|
|
|
, aux::portmap_callback& cb)
|
2007-03-15 23:03:56 +01:00
|
|
|
: m_callback(cb)
|
|
|
|
, m_currently_mapping(-1)
|
|
|
|
, m_retry_count(0)
|
|
|
|
, m_socket(ios)
|
|
|
|
, m_send_timer(ios)
|
|
|
|
, m_refresh_timer(ios)
|
2008-04-06 21:17:58 +02:00
|
|
|
, m_next_refresh(-1)
|
2007-03-15 23:03:56 +01:00
|
|
|
, m_disabled(false)
|
2008-12-19 20:27:58 +01:00
|
|
|
, m_abort(false)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2010-12-24 04:30:52 +01:00
|
|
|
// unfortunately async operations rely on the storage
|
|
|
|
// for this array not to be reallocated, by passing
|
|
|
|
// around pointers to its elements. so reserve size for now
|
|
|
|
m_mappings.reserve(10);
|
2007-03-16 22:04:58 +01:00
|
|
|
}
|
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
void natpmp::start()
|
2007-03-16 22:04:58 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-08-09 21:01:01 +02:00
|
|
|
address gateway = get_default_gateway(m_socket.get_io_service(), ec);
|
2008-01-11 10:38:05 +01:00
|
|
|
if (ec)
|
2007-03-16 06:29:23 +01:00
|
|
|
{
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
log("failed to find default route: %s"
|
|
|
|
, convert_from_native(ec.message()).c_str());
|
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2016-05-01 08:21:30 +02:00
|
|
|
disable(ec);
|
2007-12-29 08:26:36 +01:00
|
|
|
return;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2007-03-16 22:04:58 +01:00
|
|
|
m_disabled = false;
|
|
|
|
|
2008-01-11 10:38:05 +01:00
|
|
|
udp::endpoint nat_endpoint(gateway, 5351);
|
2007-03-16 22:04:58 +01:00
|
|
|
if (nat_endpoint == m_nat_endpoint) return;
|
|
|
|
m_nat_endpoint = nat_endpoint;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
log("found router at: %s"
|
|
|
|
, print_address(m_nat_endpoint.address()).c_str());
|
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2007-12-29 08:26:36 +01:00
|
|
|
m_socket.open(udp::v4(), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
disable(ec);
|
2007-12-29 08:26:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_socket.bind(udp::endpoint(address_v4::any(), 0), ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
disable(ec);
|
2007-12-29 08:26:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-05-16 23:36:27 +02:00
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("natpmp::on_reply");
|
2016-05-24 04:46:46 +02:00
|
|
|
m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer[0]
|
|
|
|
, sizeof(m_response_buffer))
|
2016-05-25 06:31:52 +02:00
|
|
|
, m_remote, std::bind(&natpmp::on_reply, self(), _1, _2));
|
2016-05-01 08:21:30 +02:00
|
|
|
send_get_ip_address_request();
|
2008-08-20 10:34:32 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
|
|
|
, end(m_mappings.end()); i != end; ++i)
|
2007-03-16 22:04:58 +01:00
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
if (i->protocol != none
|
|
|
|
|| i->action != mapping_t::action_none)
|
2007-03-16 22:04:58 +01:00
|
|
|
continue;
|
2008-04-06 21:17:58 +02:00
|
|
|
i->action = mapping_t::action_add;
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(i - m_mappings.begin());
|
2007-03-16 22:04:58 +01:00
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2007-12-29 08:26:36 +01:00
|
|
|
|
2016-05-01 08:21:30 +02:00
|
|
|
void natpmp::send_get_ip_address_request()
|
2010-12-05 21:40:28 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2010-12-05 21:40:28 +01:00
|
|
|
using namespace libtorrent::detail;
|
|
|
|
|
|
|
|
char buf[2];
|
|
|
|
char* out = buf;
|
|
|
|
write_uint8(0, out); // NAT-PMP version
|
|
|
|
write_uint8(0, out); // public IP address request opcode
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-05-01 08:21:30 +02:00
|
|
|
log("==> get public IP address");
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2010-12-05 21:40:28 +01:00
|
|
|
|
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
m_socket.send_to(boost::asio::buffer(buf, sizeof(buf)), m_nat_endpoint, 0, ec);
|
2010-12-05 21:40:28 +01:00
|
|
|
}
|
|
|
|
|
2008-11-16 03:11:04 +01:00
|
|
|
bool natpmp::get_mapping(int index, int& local_port, int& external_port, int& protocol) const
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2009-06-13 10:04:53 +02:00
|
|
|
|
2008-11-16 03:11:04 +01:00
|
|
|
TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0);
|
|
|
|
if (index >= int(m_mappings.size()) || index < 0) return false;
|
|
|
|
mapping_t const& m = m_mappings[index];
|
|
|
|
if (m.protocol == none) return false;
|
|
|
|
local_port = m.local_port;
|
|
|
|
external_port = m.external_port;
|
|
|
|
protocol = m.protocol;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
bool natpmp::should_log() const
|
|
|
|
{
|
|
|
|
return m_callback.should_log_portmap(aux::portmap_transport::natpmp);
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
TORRENT_FORMAT(2, 3)
|
|
|
|
void natpmp::log(char const* fmt, ...) const
|
2008-10-22 09:06:58 +02:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-09-16 15:53:17 +02:00
|
|
|
if (!should_log()) return;
|
2016-05-18 07:54:37 +02:00
|
|
|
char msg[200];
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
|
|
|
std::vsnprintf(msg, sizeof(msg), fmt, v);
|
|
|
|
va_end(v);
|
2016-09-16 15:53:17 +02:00
|
|
|
m_callback.log_portmap(aux::portmap_transport::natpmp, msg);
|
2008-10-22 09:06:58 +02:00
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2008-10-22 09:06:58 +02:00
|
|
|
|
2016-05-01 08:21:30 +02:00
|
|
|
void natpmp::disable(error_code const& ec)
|
2007-04-14 17:51:36 +02:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2007-04-14 17:51:36 +02:00
|
|
|
m_disabled = true;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
|
|
|
, end(m_mappings.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->protocol == none) continue;
|
2016-02-22 01:16:00 +01:00
|
|
|
int const proto = i->protocol;
|
2008-04-06 21:17:58 +02:00
|
|
|
i->protocol = none;
|
2009-06-13 10:04:53 +02:00
|
|
|
int index = i - m_mappings.begin();
|
2016-09-16 15:53:17 +02:00
|
|
|
m_callback.on_port_mapping(index, address(), 0, proto, ec
|
|
|
|
, aux::portmap_transport::natpmp);
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
2016-05-01 08:21:30 +02:00
|
|
|
close_impl();
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
2008-11-16 03:11:04 +01:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
void natpmp::delete_mapping(int index)
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2009-06-13 10:04:53 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0);
|
|
|
|
if (index >= int(m_mappings.size()) || index < 0) return;
|
|
|
|
mapping_t& m = m_mappings[index];
|
|
|
|
|
|
|
|
if (m.protocol == none) return;
|
2008-11-16 03:11:04 +01:00
|
|
|
if (!m.map_sent)
|
|
|
|
{
|
|
|
|
m.action = mapping_t::action_none;
|
|
|
|
m.protocol = none;
|
|
|
|
return;
|
|
|
|
}
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
m.action = mapping_t::action_delete;
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(index);
|
2007-12-29 08:26:36 +01:00
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
int natpmp::add_mapping(protocol_type p, int const external_port
|
|
|
|
, int const local_port)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
if (m_disabled) return -1;
|
|
|
|
|
|
|
|
std::vector<mapping_t>::iterator i = std::find_if(m_mappings.begin()
|
2016-05-25 06:31:52 +02:00
|
|
|
, m_mappings.end(), [] (mapping_t const& m) { return m.protocol == none; });
|
2008-04-06 21:17:58 +02:00
|
|
|
if (i == m_mappings.end())
|
|
|
|
{
|
|
|
|
m_mappings.push_back(mapping_t());
|
|
|
|
i = m_mappings.end() - 1;
|
|
|
|
}
|
|
|
|
i->protocol = p;
|
|
|
|
i->external_port = external_port;
|
|
|
|
i->local_port = local_port;
|
|
|
|
i->action = mapping_t::action_add;
|
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
int const mapping_index = i - m_mappings.begin();
|
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
natpmp::mapping_t const& m = *i;
|
|
|
|
log("add-mapping: proto: %s port: %d local-port: %d action: %s ttl: %" PRId64
|
|
|
|
, (m.protocol == none
|
|
|
|
? "none" : m.protocol == tcp ? "tcp" : "udp")
|
|
|
|
, m.external_port
|
|
|
|
, m.local_port
|
|
|
|
, (m.action == mapping_t::action_none
|
|
|
|
? "none" : m.action == mapping_t::action_add ? "add" : "delete")
|
|
|
|
, total_seconds(m.expires - aux::time_now()));
|
|
|
|
}
|
2010-09-30 08:33:42 +02:00
|
|
|
#endif
|
|
|
|
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(mapping_index);
|
2008-04-06 21:17:58 +02:00
|
|
|
return mapping_index;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
void natpmp::try_next_mapping(int const i)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2008-04-06 21:17:58 +02:00
|
|
|
if (i < int(m_mappings.size()) - 1)
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(i + 1);
|
2008-04-06 21:17:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
std::vector<mapping_t>::iterator const m = std::find_if(
|
2008-04-06 21:17:58 +02:00
|
|
|
m_mappings.begin(), m_mappings.end()
|
2016-05-25 06:31:52 +02:00
|
|
|
, [] (mapping_t const& ma) { return ma.action != mapping_t::action_none; });
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
if (m == m_mappings.end())
|
|
|
|
{
|
|
|
|
if (m_abort)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-04-06 21:17:58 +02:00
|
|
|
m_send_timer.cancel(ec);
|
|
|
|
m_socket.close(ec);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(m - m_mappings.begin());
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
void natpmp::update_mapping(int const i)
|
2008-04-06 21:17:58 +02:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2011-02-15 11:05:25 +01:00
|
|
|
if (i == int(m_mappings.size()))
|
2008-09-24 04:20:13 +02:00
|
|
|
{
|
|
|
|
if (m_abort)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
m_send_timer.cancel(ec);
|
|
|
|
m_socket.close(ec);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
natpmp::mapping_t const& m = m_mappings[i];
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
log("update-mapping: proto: %s port: %d local-port: %d action: %s ttl: %" PRId64
|
|
|
|
, (m.protocol == none
|
|
|
|
? "none" : m.protocol == tcp ? "tcp" : "udp")
|
|
|
|
, m.external_port
|
|
|
|
, m.local_port
|
|
|
|
, (m.action == mapping_t::action_none
|
|
|
|
? "none" : m.action == mapping_t::action_add ? "add" : "delete")
|
|
|
|
, total_seconds(m.expires - aux::time_now()));
|
|
|
|
}
|
2016-06-23 07:23:59 +02:00
|
|
|
#endif
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
if (m.action == mapping_t::action_none
|
|
|
|
|| m.protocol == none)
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
try_next_mapping(i);
|
2008-04-06 21:17:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
if (m_currently_mapping == -1)
|
|
|
|
{
|
|
|
|
// the socket is not currently in use
|
|
|
|
// send out a mapping request
|
|
|
|
m_retry_count = 0;
|
2016-05-01 08:21:30 +02:00
|
|
|
send_map_request(i);
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
void natpmp::send_map_request(int const i)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2007-03-15 23:03:56 +01:00
|
|
|
using namespace libtorrent::detail;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_currently_mapping == -1
|
2007-03-15 23:03:56 +01:00
|
|
|
|| m_currently_mapping == i);
|
|
|
|
m_currently_mapping = i;
|
2008-04-06 21:17:58 +02:00
|
|
|
mapping_t& m = m_mappings[i];
|
|
|
|
TORRENT_ASSERT(m.action != mapping_t::action_none);
|
2007-03-15 23:03:56 +01:00
|
|
|
char buf[12];
|
|
|
|
char* out = buf;
|
|
|
|
write_uint8(0, out); // NAT-PMP version
|
|
|
|
write_uint8(m.protocol, out); // map "protocol"
|
|
|
|
write_uint16(0, out); // reserved
|
|
|
|
write_uint16(m.local_port, out); // private port
|
|
|
|
write_uint16(m.external_port, out); // requested public port
|
2008-04-06 21:17:58 +02:00
|
|
|
int ttl = m.action == mapping_t::action_add ? 3600 : 0;
|
2007-03-15 23:03:56 +01:00
|
|
|
write_uint32(ttl, out); // port mapping lifetime
|
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
log("==> port map [ mapping: %d action: %s"
|
|
|
|
" proto: %s local: %u external: %u ttl: %u ]"
|
|
|
|
, i, m.action == mapping_t::action_add ? "add" : "delete"
|
|
|
|
, m.protocol == udp ? "udp" : "tcp"
|
|
|
|
, m.local_port, m.external_port, ttl);
|
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-06-06 07:22:53 +02:00
|
|
|
m_socket.send_to(boost::asio::buffer(buf, sizeof(buf)), m_nat_endpoint, 0, ec);
|
2008-11-16 03:11:04 +01:00
|
|
|
m.map_sent = true;
|
2010-09-30 08:33:42 +02:00
|
|
|
m.outstanding_request = true;
|
2008-12-07 21:25:04 +01:00
|
|
|
if (m_abort)
|
|
|
|
{
|
2008-12-19 20:27:58 +01:00
|
|
|
// when we're shutting down, ignore the
|
|
|
|
// responses and just remove all mappings
|
|
|
|
// immediately
|
|
|
|
m_currently_mapping = -1;
|
|
|
|
m.action = mapping_t::action_none;
|
2016-05-01 08:21:30 +02:00
|
|
|
try_next_mapping(i);
|
2008-12-07 21:25:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("natpmp::resend_request");
|
2008-12-19 20:27:58 +01:00
|
|
|
// linear back-off instead of exponential
|
2008-12-07 21:25:04 +01:00
|
|
|
++m_retry_count;
|
|
|
|
m_send_timer.expires_from_now(milliseconds(250 * m_retry_count), ec);
|
2016-05-25 06:31:52 +02:00
|
|
|
m_send_timer.async_wait(std::bind(&natpmp::resend_request, self(), i, _1));
|
2008-12-07 21:25:04 +01:00
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
void natpmp::resend_request(int const i, error_code const& e)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("natpmp::resend_request");
|
2007-03-15 23:03:56 +01:00
|
|
|
if (e) return;
|
2007-04-04 10:56:58 +02:00
|
|
|
if (m_currently_mapping != i) return;
|
2008-08-20 10:34:32 +02:00
|
|
|
|
|
|
|
// if we're shutting down, don't retry, just move on
|
|
|
|
// to the next mapping
|
|
|
|
if (m_retry_count >= 9 || m_abort)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
m_currently_mapping = -1;
|
|
|
|
m_mappings[i].action = mapping_t::action_none;
|
2007-03-15 23:03:56 +01:00
|
|
|
// try again in two hours
|
2015-03-12 05:34:54 +01:00
|
|
|
m_mappings[i].expires = aux::time_now() + hours(2);
|
2016-05-01 08:21:30 +02:00
|
|
|
try_next_mapping(i);
|
2007-03-15 23:03:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-01 08:21:30 +02:00
|
|
|
send_map_request(i);
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void natpmp::on_reply(error_code const& e
|
2007-03-15 23:03:56 +01:00
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2009-06-13 10:04:53 +02:00
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("natpmp::on_reply");
|
2010-11-28 02:47:30 +01:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
using namespace libtorrent::detail;
|
2008-08-20 10:34:32 +02:00
|
|
|
if (e)
|
|
|
|
{
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
log("error on receiving reply: %s"
|
|
|
|
, convert_from_native(e.message()).c_str());
|
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2008-08-20 10:34:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("natpmp::on_reply");
|
2010-12-12 19:16:35 +01:00
|
|
|
// make a copy of the response packet buffer
|
|
|
|
// to avoid overwriting it in the next receive call
|
2016-05-24 04:46:46 +02:00
|
|
|
char msg_buf[sizeof(m_response_buffer)];
|
2010-12-12 19:16:35 +01:00
|
|
|
memcpy(msg_buf, m_response_buffer, bytes_transferred);
|
|
|
|
|
2016-05-24 04:46:46 +02:00
|
|
|
m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer[0]
|
|
|
|
, sizeof(m_response_buffer))
|
2016-05-25 06:31:52 +02:00
|
|
|
, m_remote, std::bind(&natpmp::on_reply, self(), _1, _2));
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2007-12-29 08:26:36 +01:00
|
|
|
if (m_remote != m_nat_endpoint)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
log("received packet from wrong IP: %s"
|
|
|
|
, print_endpoint(m_remote).c_str());
|
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2007-12-29 08:26:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-29 08:26:36 +01:00
|
|
|
m_send_timer.cancel(ec);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
if (bytes_transferred < 12)
|
|
|
|
{
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
log("received packet of invalid size: %d", int(bytes_transferred));
|
|
|
|
#endif
|
2010-12-05 21:40:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-12 19:16:35 +01:00
|
|
|
char* in = msg_buf;
|
2007-12-29 08:26:36 +01:00
|
|
|
int version = read_uint8(in);
|
|
|
|
int cmd = read_uint8(in);
|
|
|
|
int result = read_uint16(in);
|
|
|
|
int time = read_uint32(in);
|
2016-05-18 07:54:37 +02:00
|
|
|
TORRENT_UNUSED(version);
|
|
|
|
TORRENT_UNUSED(time);
|
2010-12-05 21:40:28 +01:00
|
|
|
|
2010-12-12 19:16:35 +01:00
|
|
|
if (cmd == 128)
|
2010-12-05 21:40:28 +01:00
|
|
|
{
|
|
|
|
// public IP request response
|
2010-12-12 19:16:35 +01:00
|
|
|
m_external_ip = read_v4_address(in);
|
2010-12-05 21:40:28 +01:00
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-16 15:53:17 +02:00
|
|
|
if (should_log())
|
|
|
|
{
|
|
|
|
log("<== public IP address [ %s ]", print_address(m_external_ip).c_str());
|
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2010-12-05 21:40:28 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-24 04:46:46 +02:00
|
|
|
if (bytes_transferred != 16)
|
2010-12-05 21:40:28 +01:00
|
|
|
{
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
log("received packet of invalid size: %d", int(bytes_transferred));
|
|
|
|
#endif
|
2010-12-05 21:40:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
int const private_port = read_uint16(in);
|
|
|
|
int const public_port = read_uint16(in);
|
|
|
|
int const lifetime = read_uint32(in);
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
int const protocol = (cmd - 128 == 1)?udp:tcp;
|
2008-12-02 09:20:29 +01:00
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2009-04-13 06:22:03 +02:00
|
|
|
char msg[200];
|
2016-05-17 15:24:06 +02:00
|
|
|
int num_chars = std::snprintf(msg, sizeof(msg), "<== port map ["
|
2009-04-13 06:22:03 +02:00
|
|
|
" protocol: %s local: %u external: %u ttl: %u ]"
|
|
|
|
, (cmd - 128 == 1 ? "udp" : "tcp")
|
|
|
|
, private_port, public_port, lifetime);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2007-12-29 08:26:36 +01:00
|
|
|
if (version != 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "unexpected version: %u"
|
2009-04-13 06:22:03 +02:00
|
|
|
, version);
|
2016-05-18 07:54:37 +02:00
|
|
|
log("%s", msg);
|
2007-12-29 08:26:36 +01:00
|
|
|
}
|
2016-05-18 07:54:37 +02:00
|
|
|
#endif
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
mapping_t* m = nullptr;
|
2008-12-02 09:20:29 +01:00
|
|
|
int index = -1;
|
|
|
|
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
|
|
|
, end(m_mappings.end()); i != end; ++i)
|
2007-12-29 08:26:36 +01:00
|
|
|
{
|
2008-12-02 09:20:29 +01:00
|
|
|
if (private_port != i->local_port) continue;
|
|
|
|
if (protocol != i->protocol) continue;
|
|
|
|
if (!i->map_sent) continue;
|
2010-09-30 08:33:42 +02:00
|
|
|
if (!i->outstanding_request) continue;
|
2008-12-02 09:20:29 +01:00
|
|
|
m = &*i;
|
|
|
|
index = i - m_mappings.begin();
|
|
|
|
break;
|
2007-12-29 08:26:36 +01:00
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-07-09 22:26:26 +02:00
|
|
|
if (m == nullptr)
|
2007-12-29 08:26:36 +01:00
|
|
|
{
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
snprintf(msg + num_chars, sizeof(msg) - num_chars, " not found in map table");
|
|
|
|
log("%s", msg);
|
|
|
|
#endif
|
2008-12-02 09:20:29 +01:00
|
|
|
return;
|
2007-12-29 08:26:36 +01:00
|
|
|
}
|
2010-09-30 08:33:42 +02:00
|
|
|
m->outstanding_request = false;
|
|
|
|
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
log("%s", msg);
|
|
|
|
#endif
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2007-12-29 08:26:36 +01:00
|
|
|
if (public_port == 0 || lifetime == 0)
|
|
|
|
{
|
|
|
|
// this means the mapping was
|
|
|
|
// successfully closed
|
2008-12-02 09:20:29 +01:00
|
|
|
m->protocol = none;
|
2007-12-29 08:26:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
m->expires = aux::time_now() + seconds(int(lifetime * 0.7f));
|
2008-12-02 09:20:29 +01:00
|
|
|
m->external_port = public_port;
|
2007-12-29 08:26:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result != 0)
|
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
int errors[] =
|
2007-03-17 00:28:26 +01:00
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
errors::unsupported_protocol_version,
|
|
|
|
errors::natpmp_not_authorized,
|
|
|
|
errors::network_failure,
|
|
|
|
errors::no_resources,
|
|
|
|
errors::unsupported_opcode,
|
2009-04-13 06:22:03 +02:00
|
|
|
};
|
2009-06-12 18:40:38 +02:00
|
|
|
int ev = errors::no_error;
|
|
|
|
if (result >= 1 && result <= 5) ev = errors[result - 1];
|
2009-04-13 06:22:03 +02:00
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
m->expires = aux::time_now() + hours(2);
|
2016-02-22 01:16:00 +01:00
|
|
|
int const proto = m->protocol;
|
2016-09-16 15:53:17 +02:00
|
|
|
m_callback.on_port_mapping(index, address(), 0, proto
|
|
|
|
, error_code(ev, get_libtorrent_category())
|
|
|
|
, aux::portmap_transport::natpmp);
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2008-12-02 09:20:29 +01:00
|
|
|
else if (m->action == mapping_t::action_add)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-02-22 01:16:00 +01:00
|
|
|
int const proto = m->protocol;
|
2016-09-16 15:53:17 +02:00
|
|
|
m_callback.on_port_mapping(index, m_external_ip, m->external_port, proto
|
|
|
|
, error_code(errors::no_error, get_libtorrent_category())
|
|
|
|
, aux::portmap_transport::natpmp);
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2007-12-29 08:26:36 +01:00
|
|
|
|
2009-11-28 04:15:06 +01:00
|
|
|
if (m_abort) return;
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
m_currently_mapping = -1;
|
2008-12-02 09:20:29 +01:00
|
|
|
m->action = mapping_t::action_none;
|
2007-12-29 08:26:36 +01:00
|
|
|
m_send_timer.cancel(ec);
|
2016-05-01 08:21:30 +02:00
|
|
|
update_expiration_timer();
|
|
|
|
try_next_mapping(index);
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
2016-05-01 08:21:30 +02:00
|
|
|
void natpmp::update_expiration_timer()
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2008-04-06 21:17:58 +02:00
|
|
|
if (m_abort) return;
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point now = aux::time_now() + milliseconds(100);
|
|
|
|
time_point min_expire = now + seconds(3600);
|
2007-03-15 23:03:56 +01:00
|
|
|
int min_index = -1;
|
2008-04-06 21:17:58 +02:00
|
|
|
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
|
|
|
|
, end(m_mappings.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->protocol == none
|
|
|
|
|| i->action != mapping_t::action_none) continue;
|
2010-06-18 06:43:20 +02:00
|
|
|
int index = i - m_mappings.begin();
|
|
|
|
if (i->expires < now)
|
|
|
|
{
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
log("mapping %u expired", index);
|
|
|
|
#endif
|
2010-06-18 06:43:20 +02:00
|
|
|
i->action = mapping_t::action_add;
|
|
|
|
if (m_next_refresh == index) m_next_refresh = -1;
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(index);
|
2010-06-18 06:43:20 +02:00
|
|
|
}
|
|
|
|
else if (i->expires < min_expire)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
min_expire = i->expires;
|
2010-06-18 06:43:20 +02:00
|
|
|
min_index = index;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is already the mapping we're waiting for
|
|
|
|
if (m_next_refresh == min_index) return;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
if (min_index >= 0)
|
|
|
|
{
|
2016-06-23 07:23:59 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
log("next expiration [ idx: %d ttl: %" PRId64 " ]"
|
|
|
|
, min_index, total_seconds(min_expire - aux::time_now()));
|
2008-04-06 21:17:58 +02:00
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-04-06 21:17:58 +02:00
|
|
|
if (m_next_refresh >= 0) m_refresh_timer.cancel(ec);
|
2010-11-28 02:47:30 +01:00
|
|
|
|
2016-04-23 23:29:25 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("natpmp::mapping_expired");
|
2007-12-29 08:26:36 +01:00
|
|
|
m_refresh_timer.expires_from_now(min_expire - now, ec);
|
2016-05-25 06:31:52 +02:00
|
|
|
m_refresh_timer.async_wait(std::bind(&natpmp::mapping_expired, self(), _1, min_index));
|
2008-04-06 21:17:58 +02:00
|
|
|
m_next_refresh = min_index;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void natpmp::mapping_expired(error_code const& e, int i)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2016-04-23 23:29:25 +02:00
|
|
|
COMPLETE_ASYNC("natpmp::mapping_expired");
|
2007-03-15 23:03:56 +01:00
|
|
|
if (e) return;
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
log("mapping %u expired", i);
|
|
|
|
#endif
|
2008-04-06 21:17:58 +02:00
|
|
|
m_mappings[i].action = mapping_t::action_add;
|
|
|
|
if (m_next_refresh == i) m_next_refresh = -1;
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(i);
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void natpmp::close()
|
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
|
|
|
close_impl();
|
2009-09-22 06:01:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 08:21:30 +02:00
|
|
|
void natpmp::close_impl()
|
2009-09-22 06:01:01 +02:00
|
|
|
{
|
2016-05-01 08:21:30 +02:00
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2008-04-06 21:17:58 +02:00
|
|
|
m_abort = true;
|
2016-05-18 07:54:37 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-05-01 08:21:30 +02:00
|
|
|
log("closing");
|
2008-04-06 21:17:58 +02:00
|
|
|
#endif
|
2007-03-15 23:03:56 +01:00
|
|
|
if (m_disabled) return;
|
2016-06-23 07:23:59 +02:00
|
|
|
for (auto& m : m_mappings)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-06-23 07:23:59 +02:00
|
|
|
if (m.protocol == none) continue;
|
|
|
|
m.action = mapping_t::action_delete;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2008-11-16 03:11:04 +01:00
|
|
|
error_code ec;
|
2007-12-29 08:26:36 +01:00
|
|
|
m_refresh_timer.cancel(ec);
|
2009-12-07 05:40:36 +01:00
|
|
|
m_currently_mapping = -1;
|
2016-05-01 08:21:30 +02:00
|
|
|
update_mapping(0);
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|