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"
|
|
|
|
#include "libtorrent/http_connection.hpp"
|
2007-05-05 02:29:33 +02:00
|
|
|
#include "libtorrent/connection_queue.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2007-04-01 01:23:30 +02:00
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#include <boost/thread/condition.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
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
|
2007-05-17 04:54:13 +02:00
|
|
|
#define TORRENT_UPNP_LOGGING
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(TORRENT_UPNP_LOGGING)
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <fstream>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2007-05-16 23:36:27 +02:00
|
|
|
bool is_local(address const& a);
|
2007-05-17 04:54:13 +02:00
|
|
|
address_v4 guess_local_address(asio::io_service&);
|
2007-05-16 23:36:27 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
// int: external tcp port
|
|
|
|
// int: external udp port
|
|
|
|
// std::string: error message
|
|
|
|
typedef boost::function<void(int, int, std::string const&)> portmap_callback_t;
|
|
|
|
|
|
|
|
class upnp : boost::noncopyable
|
|
|
|
{
|
|
|
|
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
|
|
|
|
, portmap_callback_t const& cb);
|
2007-04-01 01:23:30 +02:00
|
|
|
~upnp();
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
void rebind(address const& listen_interface);
|
|
|
|
|
|
|
|
// maps the ports, if a port is set to 0
|
|
|
|
// it will not be mapped
|
|
|
|
void set_mappings(int tcp, int udp);
|
|
|
|
|
|
|
|
void close();
|
|
|
|
|
|
|
|
private:
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2007-05-04 08:47:32 +02:00
|
|
|
static address_v4 upnp_multicast_address;
|
|
|
|
static udp::endpoint upnp_multicast_endpoint;
|
|
|
|
|
2007-04-01 01:23:30 +02:00
|
|
|
enum { num_mappings = 2 };
|
|
|
|
enum { default_lease_time = 3600 };
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
void update_mapping(int i, int port);
|
|
|
|
void resend_request(asio::error_code const& e);
|
|
|
|
void on_reply(asio::error_code const& e
|
|
|
|
, std::size_t bytes_transferred);
|
|
|
|
void discover_device();
|
|
|
|
|
|
|
|
struct rootdevice;
|
|
|
|
|
|
|
|
void on_upnp_xml(asio::error_code const& e
|
|
|
|
, libtorrent::http_parser const& p, rootdevice& d);
|
|
|
|
void on_upnp_map_response(asio::error_code const& e
|
2007-03-30 18:57:55 +02:00
|
|
|
, libtorrent::http_parser const& p, rootdevice& d
|
|
|
|
, int mapping);
|
2007-04-01 01:23:30 +02:00
|
|
|
void on_upnp_unmap_response(asio::error_code const& e
|
|
|
|
, libtorrent::http_parser const& p, rootdevice& d
|
|
|
|
, int mapping);
|
|
|
|
void on_expire(asio::error_code const& e);
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
void post(rootdevice& d, std::stringstream const& s
|
|
|
|
, std::string const& soap_action);
|
|
|
|
void map_port(rootdevice& d, int i);
|
2007-04-01 01:23:30 +02:00
|
|
|
void unmap_port(rootdevice& d, int i);
|
2007-05-14 09:31:01 +02:00
|
|
|
void disable();
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
struct mapping_t
|
|
|
|
{
|
|
|
|
mapping_t()
|
2007-04-01 03:18:10 +02:00
|
|
|
: need_update(false)
|
|
|
|
, local_port(0)
|
2007-03-27 09:04:31 +02:00
|
|
|
, external_port(0)
|
|
|
|
, protocol(1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
bool need_update;
|
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;
|
|
|
|
|
|
|
|
// 1 = udp, 0 = tcp
|
|
|
|
int protocol;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rootdevice
|
|
|
|
{
|
2007-05-06 00:15:22 +02:00
|
|
|
rootdevice(): service_namespace(0)
|
|
|
|
, 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
|
|
|
{
|
|
|
|
mapping[0].protocol = 0;
|
|
|
|
mapping[1].protocol = 1;
|
|
|
|
}
|
2007-04-01 17:39:08 +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
|
|
|
|
2007-04-01 01:23:30 +02:00
|
|
|
mapping_t mapping[num_mappings];
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
std::string hostname;
|
|
|
|
int port;
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-05-14 09:31:01 +02:00
|
|
|
void close() const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
int m_udp_local_port;
|
|
|
|
int m_tcp_local_port;
|
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;
|
|
|
|
|
|
|
|
// current retry count
|
|
|
|
int m_retry_count;
|
|
|
|
|
|
|
|
// used to receive responses in
|
|
|
|
char m_receive_buffer[1024];
|
|
|
|
|
|
|
|
// the endpoint we received the message from
|
|
|
|
udp::endpoint m_remote;
|
|
|
|
|
|
|
|
// the local address we're listening on
|
|
|
|
address_v4 m_local_ip;
|
|
|
|
|
|
|
|
// the udp socket used to send and receive
|
|
|
|
// multicast messages on the network
|
|
|
|
datagram_socket m_socket;
|
|
|
|
|
|
|
|
// 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-10 07:14:37 +02:00
|
|
|
|
|
|
|
asio::strand m_strand;
|
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;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
connection_queue& m_cc;
|
|
|
|
|
2007-05-04 22:55:39 +02:00
|
|
|
#ifdef TORRENT_UPNP_LOGGING
|
2007-03-27 09:04:31 +02:00
|
|
|
std::ofstream m_log;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|