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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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-10-20 04:49:56 +02:00
|
|
|
#include "libtorrent/thread.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
|
|
|
#include "libtorrent/deadline_timer.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2015-04-19 00:00:27 +02:00
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/function/function1.hpp>
|
2016-02-22 01:16:00 +01:00
|
|
|
#include <boost/function/function5.hpp>
|
2015-01-06 09:08:49 +01:00
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2015-04-19 00:00:27 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
// int: port mapping index
|
|
|
|
// int: external port
|
2007-03-15 23:03:56 +01:00
|
|
|
// std::string: error message
|
2016-02-22 01:16:00 +01:00
|
|
|
typedef boost::function<void(int, address, int, int, error_code const&)> portmap_callback_t;
|
2009-06-12 18:40:38 +02:00
|
|
|
typedef boost::function<void(char const*)> log_callback_t;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
class natpmp : public boost::enable_shared_from_this<natpmp>
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
|
|
|
public:
|
2015-01-06 09:08:49 +01:00
|
|
|
natpmp(io_service& ios, portmap_callback_t const& cb
|
2009-06-12 18:40:38 +02:00
|
|
|
, log_callback_t const& lcb);
|
2007-03-16 22:04:58 +01:00
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
void start();
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
// maps the ports, if a port is set to 0
|
|
|
|
// it will not be mapped
|
2008-04-06 21:17:58 +02:00
|
|
|
enum protocol_type { none = 0, udp = 1, tcp = 2 };
|
|
|
|
int add_mapping(protocol_type p, int external_port, int local_port);
|
|
|
|
void delete_mapping(int mapping_index);
|
2008-11-16 03:11:04 +01:00
|
|
|
bool get_mapping(int mapping_index, int& local_port, int& external_port, int& protocol) const;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
void close();
|
|
|
|
|
|
|
|
private:
|
2017-05-08 02:43:55 +02:00
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<natpmp> self() { return shared_from_this(); }
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void update_mapping(int i, mutex::scoped_lock& l);
|
|
|
|
void send_map_request(int i, mutex::scoped_lock& l);
|
2010-12-05 21:40:28 +01:00
|
|
|
void send_get_ip_address_request(mutex::scoped_lock& l);
|
2008-05-03 18:05:42 +02:00
|
|
|
void resend_request(int i, error_code const& e);
|
|
|
|
void on_reply(error_code const& e
|
2007-03-15 23:03:56 +01:00
|
|
|
, std::size_t bytes_transferred);
|
2009-10-20 04:49:56 +02:00
|
|
|
void try_next_mapping(int i, mutex::scoped_lock& l);
|
2010-06-18 06:43:20 +02:00
|
|
|
void update_expiration_timer(mutex::scoped_lock& l);
|
2008-05-03 18:05:42 +02:00
|
|
|
void mapping_expired(error_code const& e, int i);
|
2009-10-20 04:49:56 +02:00
|
|
|
void close_impl(mutex::scoped_lock& l);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void log(char const* msg, mutex::scoped_lock& l);
|
|
|
|
void disable(error_code const& ec, mutex::scoped_lock& l);
|
2007-12-29 08:26:36 +01:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
struct mapping_t
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
enum action_t { action_none, action_add, action_delete };
|
|
|
|
mapping_t()
|
|
|
|
: action(action_none)
|
2007-03-15 23:03:56 +01:00
|
|
|
, local_port(0)
|
|
|
|
, external_port(0)
|
2008-04-06 21:17:58 +02:00
|
|
|
, protocol(none)
|
2008-11-16 03:11:04 +01:00
|
|
|
, map_sent(false)
|
2010-09-30 08:33:42 +02:00
|
|
|
, outstanding_request(false)
|
2007-03-15 23:03:56 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
// indicates that the mapping has changed
|
|
|
|
// and needs an update
|
2008-04-06 21:17:58 +02:00
|
|
|
int action;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
// the time the port mapping will expire
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point expires;
|
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
|
|
|
|
int local_port;
|
|
|
|
|
|
|
|
// the external (on the NAT router) port
|
|
|
|
// for the mapping. This is the port we
|
|
|
|
// should announce to others
|
|
|
|
int external_port;
|
|
|
|
|
|
|
|
int protocol;
|
2008-11-16 03:11:04 +01:00
|
|
|
|
|
|
|
// set to true when the first map request is sent
|
|
|
|
bool map_sent;
|
2010-09-30 08:33:42 +02:00
|
|
|
|
|
|
|
// set to true while we're waiting for a response
|
|
|
|
bool outstanding_request;
|
2007-03-15 23:03:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
portmap_callback_t m_callback;
|
2009-06-12 18:40:38 +02:00
|
|
|
log_callback_t m_log_callback;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
std::vector<mapping_t> m_mappings;
|
2016-05-24 04:46:46 +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
|
|
|
|
int m_currently_mapping;
|
|
|
|
|
|
|
|
// current retry count
|
|
|
|
int m_retry_count;
|
|
|
|
|
2016-05-24 04:46:46 +02:00
|
|
|
// used to receive responses in
|
2007-03-15 23:03:56 +01:00
|
|
|
char m_response_buffer[16];
|
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
// router external IP address
|
|
|
|
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-24 04:46:46 +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
|
|
|
|
int m_next_refresh;
|
2016-05-24 04:46:46 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
bool m_disabled;
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
bool m_abort;
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutable mutex m_mutex;
|
2007-03-15 23:03:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|