2007-03-16 06:29:23 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2007-2018, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
#ifndef TORRENT_NATPMP_HPP
|
|
|
|
#define TORRENT_NATPMP_HPP
|
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/io_service_fwd.hpp"
|
2007-04-05 00:27:36 +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"
|
|
|
|
#include "libtorrent/deadline_timer.hpp"
|
2016-05-01 00:54:23 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2016-05-01 08:21:30 +02:00
|
|
|
#include "libtorrent/debug.hpp"
|
2016-09-16 15:53:17 +02:00
|
|
|
#include "libtorrent/aux_/portmap.hpp"
|
2017-01-09 15:32:52 +01:00
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
2018-05-30 01:25:40 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
|
|
|
namespace errors
|
|
|
|
{
|
|
|
|
// See RFC 6887 Section 7.4
|
|
|
|
enum pcp_errors
|
|
|
|
{
|
|
|
|
pcp_success = 0,
|
|
|
|
pcp_unsupp_version,
|
|
|
|
pcp_not_authorized,
|
|
|
|
pcp_malformed_request,
|
|
|
|
pcp_unsupp_opcode,
|
|
|
|
pcp_unsupp_option,
|
|
|
|
pcp_malformed_option,
|
|
|
|
pcp_network_failure,
|
|
|
|
pcp_no_resources,
|
|
|
|
pcp_unsupp_protocol,
|
|
|
|
pcp_user_ex_quota,
|
|
|
|
pcp_cannot_provide_external,
|
|
|
|
pcp_address_mismatch,
|
|
|
|
pcp_excessive_remote_peers,
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::system::error_code make_error_code(pcp_errors e);
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::system::error_category& pcp_category();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace boost { namespace system {
|
|
|
|
template<> struct is_error_code_enum<libtorrent::errors::pcp_errors>
|
|
|
|
{ static const bool value = true; };
|
|
|
|
} }
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-06-23 07:23:59 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT natpmp
|
2016-08-29 14:31:23 +02:00
|
|
|
: std::enable_shared_from_this<natpmp>
|
2016-05-01 08:21:30 +02:00
|
|
|
, single_threaded
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2016-09-16 15:53:17 +02:00
|
|
|
natpmp(io_service& ios, aux::portmap_callback& cb);
|
2007-03-16 22:04:58 +01:00
|
|
|
|
2018-05-30 01:25:40 +02:00
|
|
|
void start(address local_address, std::string device);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
// maps the ports, if a port is set to 0
|
|
|
|
// it will not be mapped
|
2017-08-31 00:49:49 +02:00
|
|
|
port_mapping_t add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
|
|
|
|
void delete_mapping(port_mapping_t mapping_index);
|
|
|
|
bool get_mapping(port_mapping_t mapping_index, int& local_port, int& external_port
|
2017-08-30 20:47:49 +02:00
|
|
|
, portmap_protocol& protocol) const;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
void close();
|
|
|
|
|
|
|
|
private:
|
2018-05-30 01:25:40 +02:00
|
|
|
static error_code from_result_code(int version, int result);
|
2016-05-01 00:54:23 +02:00
|
|
|
|
2016-08-29 14:31:23 +02:00
|
|
|
std::shared_ptr<natpmp> self() { return shared_from_this(); }
|
2015-01-06 09:08:49 +01:00
|
|
|
|
2017-08-31 00:49:49 +02:00
|
|
|
void update_mapping(port_mapping_t i);
|
|
|
|
void send_map_request(port_mapping_t i);
|
2016-05-01 08:21:30 +02:00
|
|
|
void send_get_ip_address_request();
|
2018-11-08 01:01:12 +01:00
|
|
|
void on_resend_request(port_mapping_t i, error_code const& e);
|
|
|
|
void resend_request(port_mapping_t);
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_reply(error_code const& e
|
2007-03-15 23:03:56 +01:00
|
|
|
, std::size_t bytes_transferred);
|
2017-08-31 00:49:49 +02:00
|
|
|
void try_next_mapping(port_mapping_t i);
|
2016-05-01 08:21:30 +02:00
|
|
|
void update_expiration_timer();
|
2017-08-31 00:49:49 +02:00
|
|
|
void mapping_expired(error_code const& e, port_mapping_t i);
|
2016-05-01 08:21:30 +02:00
|
|
|
void close_impl();
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-05-01 08:21:30 +02:00
|
|
|
void disable(error_code const& ec);
|
2007-12-29 08:26:36 +01:00
|
|
|
|
2018-05-30 01:25:40 +02:00
|
|
|
enum protocol_version
|
|
|
|
{
|
|
|
|
version_natpmp = 0,
|
|
|
|
version_pcp = 2,
|
|
|
|
};
|
|
|
|
|
2018-06-10 00:04:38 +02:00
|
|
|
static char const* version_to_string(protocol_version version);
|
|
|
|
|
2018-05-30 01:25:40 +02:00
|
|
|
// See RFC 6887 Section 19.2
|
|
|
|
enum pcp_opcode
|
|
|
|
{
|
|
|
|
opcode_announce = 0,
|
|
|
|
opcode_map,
|
|
|
|
opcode_peer,
|
|
|
|
};
|
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
struct mapping_t : aux::base_mapping
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2018-05-30 01:25:40 +02:00
|
|
|
// random identifier, used by PCP
|
|
|
|
std::array<char, 12> nonce;
|
|
|
|
|
|
|
|
// only valid if the router supports PCP
|
|
|
|
address external_address;
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// the local port for this mapping. If this is set
|
|
|
|
// to 0, the mapping is not in use
|
2016-09-18 16:11:56 +02:00
|
|
|
int local_port = 0;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2008-11-16 03:11:04 +01:00
|
|
|
// set to true when the first map request is sent
|
2016-09-18 16:11:56 +02:00
|
|
|
bool map_sent = false;
|
2010-09-30 08:33:42 +02:00
|
|
|
|
|
|
|
// set to true while we're waiting for a response
|
2016-09-18 16:11:56 +02:00
|
|
|
bool outstanding_request = false;
|
2007-03-15 23:03:56 +01:00
|
|
|
};
|
|
|
|
|
2017-05-28 02:26:47 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
|
|
|
bool should_log() const;
|
|
|
|
void log(char const* fmt, ...) const TORRENT_FORMAT(2, 3);
|
|
|
|
void mapping_log(char const* op, mapping_t const& m) const;
|
|
|
|
#endif
|
|
|
|
|
2016-09-16 15:53:17 +02:00
|
|
|
aux::portmap_callback& m_callback;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2018-05-30 01:25:40 +02:00
|
|
|
protocol_version m_version = version_pcp;
|
|
|
|
|
2017-08-31 00:49:49 +02:00
|
|
|
aux::vector<mapping_t, port_mapping_t> m_mappings;
|
2016-05-01 00:54:23 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// the endpoint to the nat router
|
|
|
|
udp::endpoint m_nat_endpoint;
|
|
|
|
|
|
|
|
// this is the mapping that is currently
|
|
|
|
// being updated. It is -1 in case no
|
|
|
|
// mapping is being updated at the moment
|
2017-08-31 00:49:49 +02:00
|
|
|
port_mapping_t m_currently_mapping{-1};
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
// current retry count
|
2016-09-18 16:11:56 +02:00
|
|
|
int m_retry_count = 0;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
// used to receive responses in
|
2018-05-30 01:25:40 +02:00
|
|
|
// 1100 octets is the maximum size of a PCP packet
|
|
|
|
char m_response_buffer[1100];
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
// router external IP address
|
2018-05-30 01:25:40 +02:00
|
|
|
// this is only used if the router does not support PCP
|
|
|
|
// with PCP the external IP is stored with the mapping
|
2010-12-05 21:40:28 +01:00
|
|
|
address m_external_ip;
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// the endpoint we received the message from
|
|
|
|
udp::endpoint m_remote;
|
2016-05-01 00:54:23 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// the udp socket used to communicate
|
|
|
|
// with the NAT router
|
2015-06-06 19:49:18 +02:00
|
|
|
udp::socket m_socket;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
// used to resend udp packets in case
|
|
|
|
// they time out
|
|
|
|
deadline_timer m_send_timer;
|
|
|
|
|
|
|
|
// timer used to refresh mappings
|
|
|
|
deadline_timer m_refresh_timer;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
// the mapping index that will expire next
|
2017-08-31 00:49:49 +02:00
|
|
|
port_mapping_t m_next_refresh{-1};
|
2016-05-01 00:54:23 +02:00
|
|
|
|
2016-09-18 16:11:56 +02:00
|
|
|
bool m_disabled = false;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2016-09-18 16:11:56 +02:00
|
|
|
bool m_abort = false;
|
2007-03-15 23:03:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|