2007-03-27 09:04:31 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
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_UPNP_HPP
|
|
|
|
#define TORRENT_UPNP_HPP
|
|
|
|
|
|
|
|
#include "libtorrent/socket.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2007-09-10 01:52:34 +02:00
|
|
|
#include "libtorrent/broadcast_socket.hpp"
|
2007-03-27 09:04:31 +02:00
|
|
|
#include "libtorrent/http_connection.hpp"
|
2007-05-05 02:29:33 +02:00
|
|
|
#include "libtorrent/connection_queue.hpp"
|
2007-09-29 23:31:51 +02:00
|
|
|
#include "libtorrent/intrusive_ptr_base.hpp"
|
2009-10-20 04:49:56 +02:00
|
|
|
#include "libtorrent/thread.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/deadline_timer.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
#include <boost/function/function1.hpp>
|
2012-06-23 07:46:51 +02:00
|
|
|
#include <boost/function/function4.hpp>
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2007-04-05 03:08:21 +02:00
|
|
|
#include <set>
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-05-17 04:54:13 +02:00
|
|
|
|
|
|
|
#if defined(TORRENT_UPNP_LOGGING)
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <fstream>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
namespace upnp_errors
|
|
|
|
{
|
|
|
|
enum error_code_enum
|
|
|
|
{
|
|
|
|
no_error = 0,
|
|
|
|
invalid_argument = 402,
|
|
|
|
action_failed = 501,
|
|
|
|
value_not_in_array = 714,
|
|
|
|
source_ip_cannot_be_wildcarded = 715,
|
|
|
|
external_port_cannot_be_wildcarded = 716,
|
|
|
|
port_mapping_conflict = 718,
|
|
|
|
internal_port_must_match_external = 724,
|
|
|
|
only_permanent_leases_supported = 725,
|
|
|
|
remote_host_must_be_wildcard = 726,
|
2009-08-30 09:38:52 +02:00
|
|
|
external_port_must_be_wildcard = 727
|
2009-06-12 18:40:38 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#if BOOST_VERSION < 103500
|
|
|
|
extern asio::error::error_category upnp_category;
|
|
|
|
#else
|
|
|
|
|
|
|
|
struct TORRENT_EXPORT upnp_error_category : boost::system::error_category
|
|
|
|
{
|
|
|
|
virtual const char* name() const;
|
|
|
|
virtual std::string message(int ev) const;
|
|
|
|
virtual boost::system::error_condition default_error_condition(int ev) const
|
|
|
|
{ return boost::system::error_condition(ev, *this); }
|
|
|
|
};
|
|
|
|
|
|
|
|
extern TORRENT_EXPORT upnp_error_category upnp_category;
|
|
|
|
#endif
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
// int: port-mapping index
|
2010-12-05 21:40:28 +01:00
|
|
|
// address: external address as queried from router
|
2008-04-06 21:17:58 +02:00
|
|
|
// int: external port
|
2007-03-27 09:04:31 +02:00
|
|
|
// std::string: error message
|
2008-04-06 21:17:58 +02:00
|
|
|
// an empty string as error means success
|
2008-10-22 03:12:14 +02:00
|
|
|
// a port-mapping index of -1 means it's
|
|
|
|
// an informational log message
|
2010-12-05 21:40:28 +01:00
|
|
|
typedef boost::function<void(int, address, 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-27 09:04:31 +02:00
|
|
|
|
2009-04-30 19:30:14 +02:00
|
|
|
class TORRENT_EXPORT upnp : public intrusive_ptr_base<upnp>
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
|
|
|
public:
|
2007-05-05 02:29:33 +02:00
|
|
|
upnp(io_service& ios, connection_queue& cc
|
|
|
|
, address const& listen_interface, std::string const& user_agent
|
2009-06-12 18:40:38 +02:00
|
|
|
, portmap_callback_t const& cb, log_callback_t const& lcb
|
|
|
|
, bool ignore_nonrouters, void* state = 0);
|
2007-04-01 01:23:30 +02:00
|
|
|
~upnp();
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2008-06-20 18:14:10 +02:00
|
|
|
void* drain_state();
|
|
|
|
|
2008-04-07 01:18:35 +02:00
|
|
|
enum protocol_type { none = 0, udp = 1, tcp = 2 };
|
2008-04-06 21:17:58 +02:00
|
|
|
int add_mapping(protocol_type p, int external_port, int local_port);
|
2008-04-07 01:18:35 +02:00
|
|
|
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-27 09:04:31 +02:00
|
|
|
|
2007-12-24 09:18:53 +01:00
|
|
|
void discover_device();
|
2007-03-27 09:04:31 +02:00
|
|
|
void close();
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
std::string router_model()
|
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
return m_model;
|
|
|
|
}
|
2007-12-22 17:47:46 +01:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
private:
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void discover_device_impl(mutex::scoped_lock& l);
|
2007-05-04 08:47:32 +02:00
|
|
|
static address_v4 upnp_multicast_address;
|
|
|
|
static udp::endpoint upnp_multicast_endpoint;
|
|
|
|
|
2009-07-20 04:28:56 +02:00
|
|
|
// there are routers that's don't support timed
|
|
|
|
// port maps, without returning error 725. It seems
|
|
|
|
// safer to always assume that we have to ask for
|
|
|
|
// permanent leases
|
|
|
|
enum { default_lease_time = 0 };
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void resend_request(error_code const& e);
|
2007-09-10 01:52:34 +02:00
|
|
|
void on_reply(udp::endpoint const& from, char* buffer
|
2007-03-27 09:04:31 +02:00
|
|
|
, std::size_t bytes_transferred);
|
|
|
|
|
|
|
|
struct rootdevice;
|
2009-10-20 04:49:56 +02:00
|
|
|
void next(rootdevice& d, int i, mutex::scoped_lock& l);
|
|
|
|
void update_map(rootdevice& d, int i, mutex::scoped_lock& l);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_upnp_xml(error_code const& e
|
2008-05-17 02:27:26 +02:00
|
|
|
, libtorrent::http_parser const& p, rootdevice& d
|
|
|
|
, http_connection& c);
|
2010-12-05 21:40:28 +01:00
|
|
|
void on_upnp_get_ip_address_response(error_code const& e
|
|
|
|
, libtorrent::http_parser const& p, rootdevice& d
|
|
|
|
, http_connection& c);
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_upnp_map_response(error_code const& e
|
2007-03-30 18:57:55 +02:00
|
|
|
, libtorrent::http_parser const& p, rootdevice& d
|
2008-05-17 02:27:26 +02:00
|
|
|
, int mapping, http_connection& c);
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_upnp_unmap_response(error_code const& e
|
2007-04-01 01:23:30 +02:00
|
|
|
, libtorrent::http_parser const& p, rootdevice& d
|
2008-05-17 02:27:26 +02:00
|
|
|
, int mapping, http_connection& c);
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_expire(error_code const& e);
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void disable(error_code const& ec, mutex::scoped_lock& l);
|
|
|
|
void return_error(int mapping, int code, mutex::scoped_lock& l);
|
|
|
|
void log(char const* msg, mutex::scoped_lock& l);
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
void get_ip_address(rootdevice& d);
|
2007-09-10 01:52:34 +02:00
|
|
|
void delete_port_mapping(rootdevice& d, int i);
|
|
|
|
void create_port_mapping(http_connection& c, rootdevice& d, int i);
|
2009-04-13 06:22:03 +02:00
|
|
|
void post(upnp::rootdevice const& d, char const* soap
|
2009-10-20 04:49:56 +02:00
|
|
|
, char const* soap_action, mutex::scoped_lock& l);
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
int num_mappings() const { return int(m_mappings.size()); }
|
|
|
|
|
|
|
|
struct global_mapping_t
|
|
|
|
{
|
|
|
|
global_mapping_t()
|
|
|
|
: protocol(none)
|
|
|
|
, external_port(0)
|
|
|
|
, local_port(0)
|
|
|
|
{}
|
|
|
|
int protocol;
|
|
|
|
int external_port;
|
|
|
|
int local_port;
|
|
|
|
};
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
struct mapping_t
|
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
enum action_t { action_none, action_add, action_delete };
|
2007-03-27 09:04:31 +02:00
|
|
|
mapping_t()
|
2008-04-06 21:17:58 +02:00
|
|
|
: action(action_none)
|
2007-04-01 03:18:10 +02:00
|
|
|
, local_port(0)
|
2007-03-27 09:04:31 +02:00
|
|
|
, external_port(0)
|
2008-04-06 21:17:58 +02:00
|
|
|
, protocol(none)
|
2008-01-07 03:35:29 +01:00
|
|
|
, failcount(0)
|
2007-03-27 09:04:31 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
// the time the port mapping will expire
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime expires;
|
2007-04-01 03:18:10 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
int action;
|
2007-03-27 09:04:31 +02: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;
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
// 2 = udp, 1 = tcp
|
2007-03-27 09:04:31 +02:00
|
|
|
int protocol;
|
2008-01-07 03:35:29 +01:00
|
|
|
|
|
|
|
// the number of times this mapping has failed
|
|
|
|
int failcount;
|
2007-03-27 09:04:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rootdevice
|
|
|
|
{
|
2007-05-06 00:15:22 +02:00
|
|
|
rootdevice(): service_namespace(0)
|
2011-02-21 06:24:41 +01:00
|
|
|
, port(0)
|
2007-05-06 00:15:22 +02:00
|
|
|
, lease_duration(default_lease_time)
|
2007-04-01 01:23:30 +02:00
|
|
|
, supports_specific_external(true)
|
2007-05-14 09:31:01 +02:00
|
|
|
, disabled(false)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
2011-05-08 11:04:59 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2007-10-26 02:47:30 +02:00
|
|
|
magic = 1337;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-08 11:04:59 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2007-10-26 02:47:30 +02:00
|
|
|
~rootdevice()
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(magic == 1337);
|
|
|
|
magic = 0;
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
2007-10-26 02:47:30 +02:00
|
|
|
#endif
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
// the interface url, through which the list of
|
|
|
|
// supported interfaces are fetched
|
|
|
|
std::string url;
|
|
|
|
|
|
|
|
// the url to the WANIP or WANPPP interface
|
|
|
|
std::string control_url;
|
|
|
|
// either the WANIP namespace or the WANPPP namespace
|
2007-05-06 00:06:26 +02:00
|
|
|
char const* service_namespace;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
std::vector<mapping_t> mapping;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2008-10-14 09:19:46 +02:00
|
|
|
// this is the hostname, port and path
|
|
|
|
// component of the url or the control_url
|
|
|
|
// if it has been found
|
2007-03-27 09:04:31 +02:00
|
|
|
std::string hostname;
|
|
|
|
int port;
|
|
|
|
std::string path;
|
2010-12-05 21:40:28 +01:00
|
|
|
address external_ip;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
int lease_duration;
|
|
|
|
// true if the device supports specifying a
|
|
|
|
// specific external port, false if it doesn't
|
|
|
|
bool supports_specific_external;
|
2007-05-14 09:31:01 +02:00
|
|
|
|
|
|
|
bool disabled;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-05-14 09:31:01 +02:00
|
|
|
mutable boost::shared_ptr<http_connection> upnp_connection;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2011-05-08 11:04:59 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
2007-10-26 02:47:30 +02:00
|
|
|
int magic;
|
|
|
|
#endif
|
2007-05-14 09:31:01 +02:00
|
|
|
void close() const
|
|
|
|
{
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(magic == 1337);
|
2007-05-14 09:31:01 +02:00
|
|
|
if (!upnp_connection) return;
|
|
|
|
upnp_connection->close();
|
|
|
|
upnp_connection.reset();
|
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
bool operator<(rootdevice const& rhs) const
|
|
|
|
{ return url < rhs.url; }
|
|
|
|
};
|
2007-04-01 03:18:10 +02:00
|
|
|
|
2008-06-20 18:14:10 +02:00
|
|
|
struct upnp_state_t
|
|
|
|
{
|
|
|
|
std::vector<global_mapping_t> mappings;
|
|
|
|
std::set<rootdevice> devices;
|
|
|
|
};
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
std::vector<global_mapping_t> m_mappings;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
std::string const& m_user_agent;
|
|
|
|
|
|
|
|
// the set of devices we've found
|
|
|
|
std::set<rootdevice> m_devices;
|
|
|
|
|
|
|
|
portmap_callback_t m_callback;
|
2009-06-12 18:40:38 +02:00
|
|
|
log_callback_t m_log_callback;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
// current retry count
|
|
|
|
int m_retry_count;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
io_service& m_io_service;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
// the udp socket used to send and receive
|
|
|
|
// multicast messages on the network
|
2007-09-10 01:52:34 +02:00
|
|
|
broadcast_socket m_socket;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
// used to resend udp packets in case
|
|
|
|
// they time out
|
|
|
|
deadline_timer m_broadcast_timer;
|
|
|
|
|
|
|
|
// timer used to refresh mappings
|
|
|
|
deadline_timer m_refresh_timer;
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
bool m_disabled;
|
2007-04-01 01:23:30 +02:00
|
|
|
bool m_closing;
|
2008-05-13 06:59:56 +02:00
|
|
|
bool m_ignore_non_routers;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
connection_queue& m_cc;
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex m_mutex;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-12-22 17:47:46 +01:00
|
|
|
std::string m_model;
|
2007-03-27 09:04:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|