2007-03-27 09:04:31 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Arvid Norberg
|
2007-03-27 09:04:31 +02: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-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
2007-03-27 09:04:31 +02:00
|
|
|
#include "libtorrent/upnp.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
2008-05-17 16:19:34 +02:00
|
|
|
#include "libtorrent/parse_url.hpp"
|
2007-03-27 09:04:31 +02:00
|
|
|
#include "libtorrent/xml_parse.hpp"
|
2008-01-07 06:48:28 +01:00
|
|
|
#include "libtorrent/enum_net.hpp"
|
2011-02-26 08:55:51 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp" // for aux::time_now()
|
2015-03-15 00:10:20 +01:00
|
|
|
#include "libtorrent/aux_/escape_string.hpp" // for convert_from_native
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
#include "libtorrent/debug.hpp"
|
|
|
|
#endif
|
|
|
|
|
2015-08-02 21:55:05 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/ref.hpp>
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <boost/asio/ip/host_name.hpp>
|
|
|
|
#include <boost/asio/ip/multicast.hpp>
|
2015-08-02 21:55:05 +02:00
|
|
|
|
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2014-02-02 04:05:55 +01:00
|
|
|
namespace libtorrent {
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2014-07-21 05:19:58 +02:00
|
|
|
namespace upnp_errors
|
|
|
|
{
|
|
|
|
boost::system::error_code make_error_code(error_code_enum e)
|
|
|
|
{
|
|
|
|
return error_code(e, get_upnp_category());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // upnp_errors namespace
|
|
|
|
|
2015-08-16 18:17:23 +02:00
|
|
|
static error_code ignore_error;
|
2008-11-05 06:39:18 +01:00
|
|
|
|
2014-10-03 22:56:57 +02:00
|
|
|
upnp::upnp(io_service& ios
|
2007-05-05 02:29:33 +02:00
|
|
|
, 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
|
2015-01-06 09:08:49 +01:00
|
|
|
, bool ignore_nonrouters)
|
2009-04-13 06:22:03 +02:00
|
|
|
: m_user_agent(user_agent)
|
2007-03-27 09:04:31 +02:00
|
|
|
, m_callback(cb)
|
2009-06-12 18:40:38 +02:00
|
|
|
, m_log_callback(lcb)
|
2007-03-27 09:04:31 +02:00
|
|
|
, m_retry_count(0)
|
2007-09-10 01:52:34 +02:00
|
|
|
, m_io_service(ios)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_resolver(ios)
|
2015-08-16 18:17:23 +02:00
|
|
|
, m_socket(udp::endpoint(address_v4::from_string("239.255.255.250"
|
|
|
|
, ignore_error), 1900))
|
2007-03-27 09:04:31 +02:00
|
|
|
, m_broadcast_timer(ios)
|
|
|
|
, m_refresh_timer(ios)
|
2014-03-10 07:47:01 +01:00
|
|
|
, m_map_timer(ios)
|
2007-03-27 09:04:31 +02:00
|
|
|
, m_disabled(false)
|
2007-04-01 01:23:30 +02:00
|
|
|
, m_closing(false)
|
2008-05-13 06:59:56 +02:00
|
|
|
, m_ignore_non_routers(ignore_nonrouters)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_last_if_update(min_time())
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2008-10-22 03:12:14 +02:00
|
|
|
TORRENT_ASSERT(cb);
|
2015-05-18 03:30:32 +02:00
|
|
|
|
|
|
|
// TODO: 3 listen_interface is not used. It's meant to bind the broadcast
|
|
|
|
// socket. it would probably have to be changed to a vector of interfaces to
|
|
|
|
// bind to though, since the broadcast socket opens one socket per local
|
|
|
|
// interface by default
|
|
|
|
TORRENT_UNUSED(listen_interface);
|
2015-01-06 09:08:49 +01:00
|
|
|
}
|
2008-06-20 18:14:10 +02:00
|
|
|
|
2015-08-16 18:21:39 +02:00
|
|
|
void upnp::start()
|
2015-01-06 09:08:49 +01:00
|
|
|
{
|
2012-10-09 06:16:37 +02:00
|
|
|
error_code ec;
|
2015-01-06 09:08:49 +01:00
|
|
|
m_socket.open(boost::bind(&upnp::on_reply, self(), _1, _2, _3)
|
|
|
|
, m_refresh_timer.get_io_service(), ec);
|
2012-10-09 06:16:37 +02:00
|
|
|
|
2010-12-24 04:30:52 +01:00
|
|
|
m_mappings.reserve(10);
|
2008-06-20 18:14:10 +02:00
|
|
|
}
|
|
|
|
|
2007-04-01 01:23:30 +02:00
|
|
|
upnp::~upnp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-12-30 00:14:59 +01:00
|
|
|
void upnp::discover_device()
|
2008-04-06 21:17:58 +02:00
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-10-22 03:12:14 +02:00
|
|
|
if (m_socket.num_send_sockets() == 0)
|
2009-06-13 10:04:53 +02:00
|
|
|
log("No network interfaces to broadcast to", l);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
discover_device_impl(l);
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void upnp::log(char const* msg, mutex::scoped_lock& l)
|
2008-10-22 03:12:14 +02:00
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
l.unlock();
|
2009-06-12 18:40:38 +02:00
|
|
|
m_log_callback(msg);
|
2009-06-13 10:04:53 +02:00
|
|
|
l.lock();
|
2008-10-22 03:12:14 +02:00
|
|
|
}
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void upnp::discover_device_impl(mutex::scoped_lock& l)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2015-07-06 14:33:29 +02:00
|
|
|
const char msearch[] =
|
2007-03-27 09:04:31 +02:00
|
|
|
"M-SEARCH * HTTP/1.1\r\n"
|
|
|
|
"HOST: 239.255.255.250:1900\r\n"
|
|
|
|
"ST:upnp:rootdevice\r\n"
|
|
|
|
"MAN:\"ssdp:discover\"\r\n"
|
|
|
|
"MX:3\r\n"
|
|
|
|
"\r\n\r\n";
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-05-14 09:31:01 +02:00
|
|
|
#ifdef TORRENT_DEBUG_UPNP
|
|
|
|
// simulate packet loss
|
|
|
|
if (m_retry_count & 1)
|
|
|
|
#endif
|
2007-09-10 01:52:34 +02:00
|
|
|
m_socket.send(msearch, sizeof(msearch) - 1, ec);
|
2007-05-14 09:31:01 +02:00
|
|
|
|
|
|
|
if (ec)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2013-01-19 07:39:32 +01:00
|
|
|
snprintf(msg, sizeof(msg), "broadcast failed: %s. Aborting."
|
|
|
|
, convert_from_native(ec.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
|
|
|
disable(ec, l);
|
2007-05-14 09:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("upnp::resend_request");
|
|
|
|
#endif
|
2007-03-27 09:04:31 +02:00
|
|
|
++m_retry_count;
|
2008-10-14 04:41:22 +02:00
|
|
|
m_broadcast_timer.expires_from_now(seconds(2 * m_retry_count), ec);
|
2010-04-30 21:08:16 +02:00
|
|
|
m_broadcast_timer.async_wait(boost::bind(&upnp::resend_request
|
2008-01-08 06:47:43 +01:00
|
|
|
, self(), _1));
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
log("broadcasting search for rootdevice", l);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
// returns a reference to a mapping or -1 on failure
|
|
|
|
int upnp::add_mapping(upnp::protocol_type p, int external_port, int local_port)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2014-06-15 06:20:39 +02:00
|
|
|
// external port 0 means _every_ port
|
|
|
|
TORRENT_ASSERT(external_port != 0);
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "adding port map: [ protocol: %s ext_port: %u "
|
|
|
|
"local_port: %u ] %s", (p == tcp?"tcp":"udp"), external_port
|
|
|
|
, local_port, m_disabled ? "DISABLED": "");
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-04-06 21:17:58 +02:00
|
|
|
if (m_disabled) return -1;
|
|
|
|
|
2015-08-18 13:55:50 +02:00
|
|
|
std::vector<global_mapping_t>::iterator mapping_it = std::find_if(
|
2008-04-06 21:17:58 +02:00
|
|
|
m_mappings.begin(), m_mappings.end()
|
|
|
|
, boost::bind(&global_mapping_t::protocol, _1) == int(none));
|
|
|
|
|
2015-08-18 13:55:50 +02:00
|
|
|
if (mapping_it == m_mappings.end())
|
2008-04-06 21:17:58 +02:00
|
|
|
{
|
|
|
|
m_mappings.push_back(global_mapping_t());
|
2015-08-18 13:55:50 +02:00
|
|
|
mapping_it = m_mappings.end() - 1;
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
2007-05-14 10:56:46 +02:00
|
|
|
|
2015-08-18 13:55:50 +02:00
|
|
|
mapping_it->protocol = p;
|
|
|
|
mapping_it->external_port = external_port;
|
|
|
|
mapping_it->local_port = local_port;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2015-08-18 13:55:50 +02:00
|
|
|
int mapping_index = mapping_it - m_mappings.begin();
|
2007-04-01 03:18:10 +02:00
|
|
|
|
|
|
|
for (std::set<rootdevice>::iterator i = m_devices.begin()
|
|
|
|
, end(m_devices.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
rootdevice& d = const_cast<rootdevice&>(*i);
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
if (int(d.mapping.size()) <= mapping_index)
|
|
|
|
d.mapping.resize(mapping_index + 1);
|
|
|
|
mapping_t& m = d.mapping[mapping_index];
|
|
|
|
|
|
|
|
m.action = mapping_t::action_add;
|
|
|
|
m.protocol = p;
|
|
|
|
m.external_port = external_port;
|
|
|
|
m.local_port = local_port;
|
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
if (d.service_namespace) update_map(d, mapping_index, l);
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return mapping_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void upnp::delete_mapping(int mapping)
|
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2009-03-11 01:42:46 +01:00
|
|
|
if (mapping >= int(m_mappings.size())) return;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
global_mapping_t& m = m_mappings[mapping];
|
|
|
|
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "deleting port map: [ protocol: %s ext_port: %u "
|
|
|
|
"local_port: %u ]", (m.protocol == tcp?"tcp":"udp"), m.external_port
|
|
|
|
, m.local_port);
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
if (m.protocol == none) return;
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
for (std::set<rootdevice>::iterator i = m_devices.begin()
|
|
|
|
, end(m_devices.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
rootdevice& d = const_cast<rootdevice&>(*i);
|
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(mapping < int(d.mapping.size()));
|
|
|
|
d.mapping[mapping].action = mapping_t::action_delete;
|
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
if (d.service_namespace) update_map(d, mapping, l);
|
2007-04-01 03:18:10 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2008-11-16 03:11:04 +01:00
|
|
|
bool upnp::get_mapping(int index, int& local_port, int& external_port, int& protocol) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0);
|
|
|
|
if (index >= int(m_mappings.size()) || index < 0) return false;
|
|
|
|
global_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;
|
|
|
|
}
|
|
|
|
|
2011-02-24 05:25:35 +01:00
|
|
|
void upnp::resend_request(error_code const& ec)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("upnp::resend_request");
|
|
|
|
#endif
|
2011-02-24 05:25:35 +01:00
|
|
|
if (ec) return;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<upnp> me(self());
|
2009-07-01 06:02:16 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2008-12-07 21:25:04 +01:00
|
|
|
if (m_closing) return;
|
|
|
|
|
2008-10-14 04:41:22 +02:00
|
|
|
if (m_retry_count < 12
|
2007-05-04 07:34:03 +02:00
|
|
|
&& (m_devices.empty() || m_retry_count < 4))
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
discover_device_impl(l);
|
2007-05-04 19:39:44 +02:00
|
|
|
return;
|
2007-05-04 07:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_devices.empty())
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disable(errors::no_router, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2007-05-04 07:34:03 +02:00
|
|
|
for (std::set<rootdevice>::iterator i = m_devices.begin()
|
|
|
|
, end(m_devices.end()); i != end; ++i)
|
|
|
|
{
|
2007-05-14 09:31:01 +02:00
|
|
|
if (i->control_url.empty() && !i->upnp_connection && !i->disabled)
|
2007-05-04 07:34:03 +02:00
|
|
|
{
|
|
|
|
// we don't have a WANIP or WANPPP url for this device,
|
|
|
|
// ask for it
|
|
|
|
rootdevice& d = const_cast<rootdevice&>(*i);
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY
|
2007-05-14 09:31:01 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "connecting to: %s", d.url.c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-05-17 02:27:26 +02:00
|
|
|
if (d.upnp_connection) d.upnp_connection->close();
|
2007-09-10 01:52:34 +02:00
|
|
|
d.upnp_connection.reset(new http_connection(m_io_service
|
2014-10-03 22:56:57 +02:00
|
|
|
, m_resolver
|
2014-07-06 21:18:00 +02:00
|
|
|
, boost::bind(&upnp::on_upnp_xml, self(), _1, _2
|
2008-05-17 02:27:26 +02:00
|
|
|
, boost::ref(d), _5)));
|
2008-03-12 08:44:27 +01:00
|
|
|
d.upnp_connection->get(d.url, seconds(30), 1);
|
2007-05-14 09:31:01 +02:00
|
|
|
}
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_CATCH (std::exception& exc)
|
2007-05-14 09:31:01 +02:00
|
|
|
{
|
2011-03-10 06:01:36 +01:00
|
|
|
TORRENT_DECLARE_DUMMY(std::exception, exc);
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2011-02-24 05:25:35 +01:00
|
|
|
snprintf(msg, sizeof(msg), "connection failed to: %s %s", d.url.c_str(), exc.what());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-05-14 09:31:01 +02:00
|
|
|
d.disabled = true;
|
|
|
|
}
|
2007-05-04 07:34:03 +02:00
|
|
|
}
|
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
void upnp::on_reply(udp::endpoint const& from, char* buffer
|
2007-05-14 09:31:01 +02:00
|
|
|
, std::size_t bytes_transferred)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<upnp> me(self());
|
2009-07-01 06:02:16 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
using namespace libtorrent::detail;
|
|
|
|
|
|
|
|
// parse out the url for the device
|
|
|
|
|
|
|
|
/*
|
|
|
|
the response looks like this:
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
ST:upnp:rootdevice
|
|
|
|
USN:uuid:000f-66d6-7296000099dc::upnp:rootdevice
|
|
|
|
Location: http://192.168.1.1:5431/dyndev/uuid:000f-66d6-7296000099dc
|
|
|
|
Server: Custom/1.0 UPnP/1.0 Proc/Ver
|
|
|
|
EXT:
|
|
|
|
Cache-Control:max-age=180
|
|
|
|
DATE: Fri, 02 Jan 1970 08:10:38 GMT
|
2007-09-21 01:25:40 +02:00
|
|
|
|
|
|
|
a notification looks like this:
|
|
|
|
|
|
|
|
NOTIFY * HTTP/1.1
|
|
|
|
Host:239.255.255.250:1900
|
|
|
|
NT:urn:schemas-upnp-org:device:MediaServer:1
|
|
|
|
NTS:ssdp:alive
|
|
|
|
Location:http://10.0.3.169:2869/upnphost/udhisapi.dll?content=uuid:c17f0c32-d19b-4938-ae94-65f945c3a26e
|
|
|
|
USN:uuid:c17f0c32-d19b-4938-ae94-65f945c3a26e::urn:schemas-upnp-org:device:MediaServer:1
|
|
|
|
Cache-Control:max-age=900
|
|
|
|
Server:Microsoft-Windows-NT/5.1 UPnP/1.0 UPnP-Device-Host/1.0
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
*/
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2015-03-12 05:34:54 +01:00
|
|
|
if (clock_type::now() - seconds(60) > m_last_if_update)
|
2008-01-07 06:48:28 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
m_interfaces = enum_net_interfaces(m_io_service, ec);
|
2008-02-19 19:50:04 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "when receiving response from: %s: %s"
|
2013-01-19 07:39:32 +01:00
|
|
|
, print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-02-19 19:50:04 +01:00
|
|
|
}
|
2015-03-12 05:34:54 +01:00
|
|
|
m_last_if_update = aux::time_now();
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2008-10-22 03:12:14 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!ec && !in_local_network(m_interfaces, from.address()))
|
|
|
|
{
|
|
|
|
char msg[400];
|
|
|
|
int num_chars = snprintf(msg, sizeof(msg)
|
|
|
|
, "ignoring response from: %s. IP is not on local network. "
|
|
|
|
, print_endpoint(from).c_str());
|
|
|
|
|
|
|
|
std::vector<ip_interface> net = enum_net_interfaces(m_io_service, ec);
|
|
|
|
for (std::vector<ip_interface>::const_iterator i = net.begin()
|
|
|
|
, end(net.end()); i != end && num_chars < int(sizeof(msg)); ++i)
|
|
|
|
{
|
|
|
|
num_chars += snprintf(msg + num_chars, sizeof(msg) - num_chars, "(%s,%s) "
|
|
|
|
, print_address(i->interface_address).c_str(), print_address(i->netmask).c_str());
|
2008-02-19 19:50:04 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
log(msg, l);
|
|
|
|
return;
|
2015-05-18 07:04:55 +02:00
|
|
|
}
|
2008-05-13 06:59:56 +02:00
|
|
|
|
2014-03-10 07:47:01 +01:00
|
|
|
bool non_router = false;
|
2008-12-04 07:24:57 +01:00
|
|
|
if (m_ignore_non_routers)
|
2008-05-13 06:59:56 +02:00
|
|
|
{
|
2015-08-09 21:01:01 +02:00
|
|
|
std::vector<ip_route> routes = enum_routes(m_io_service, ec);
|
2008-12-04 07:24:57 +01:00
|
|
|
if (std::find_if(routes.begin(), routes.end()
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&ip_route::gateway, _1) == from.address()) == routes.end())
|
2008-05-13 06:59:56 +02:00
|
|
|
{
|
2008-12-04 07:24:57 +01:00
|
|
|
// this upnp device is filtered because it's not in the
|
|
|
|
// list of configured routers
|
|
|
|
if (ec)
|
2008-05-13 06:59:56 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2014-03-10 07:47:01 +01:00
|
|
|
snprintf(msg, sizeof(msg), "failed to enumerate routes when "
|
|
|
|
"receiving response from: %s: %s"
|
2013-01-19 07:39:32 +01:00
|
|
|
, print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-05-13 06:59:56 +02:00
|
|
|
}
|
2008-12-04 07:24:57 +01:00
|
|
|
else
|
|
|
|
{
|
2010-10-20 07:14:50 +02:00
|
|
|
char msg[400];
|
2014-03-10 07:47:01 +01:00
|
|
|
int num_chars = snprintf(msg, sizeof(msg), "SSDP response from: "
|
|
|
|
"%s: IP is not a router. "
|
2009-04-13 06:22:03 +02:00
|
|
|
, print_endpoint(from).c_str());
|
2008-12-04 07:24:57 +01:00
|
|
|
for (std::vector<ip_route>::const_iterator i = routes.begin()
|
2014-07-05 16:10:25 +02:00
|
|
|
, end(routes.end()); i != end && num_chars < int(sizeof(msg)); ++i)
|
2008-12-04 07:24:57 +01:00
|
|
|
{
|
2009-04-13 06:22:03 +02:00
|
|
|
num_chars += snprintf(msg + num_chars, sizeof(msg) - num_chars, "(%s,%s) "
|
|
|
|
, print_address(i->gateway).c_str(), print_address(i->netmask).c_str());
|
2008-12-04 07:24:57 +01:00
|
|
|
}
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2014-03-10 07:47:01 +01:00
|
|
|
non_router = true;
|
2008-12-04 07:24:57 +01:00
|
|
|
}
|
2008-05-13 06:59:56 +02:00
|
|
|
}
|
2008-01-07 06:48:28 +01:00
|
|
|
}
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
http_parser p;
|
2007-12-29 19:24:50 +01:00
|
|
|
bool error = false;
|
|
|
|
p.incoming(buffer::const_interval(buffer
|
|
|
|
, buffer + bytes_transferred), error);
|
|
|
|
if (error)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "received malformed HTTP from: %s"
|
|
|
|
, print_endpoint(from).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-21 01:25:40 +02:00
|
|
|
if (p.status_code() != 200 && p.method() != "notify")
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2007-09-21 01:25:40 +02:00
|
|
|
if (p.method().empty())
|
2008-10-22 03:12:14 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "HTTP status %u from %s"
|
|
|
|
, p.status_code(), print_endpoint(from).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-10-22 03:12:14 +02:00
|
|
|
}
|
2007-09-21 01:25:40 +02:00
|
|
|
else
|
2008-10-22 03:12:14 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "HTTP method %s from %s"
|
|
|
|
, p.method().c_str(), print_endpoint(from).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-10-22 03:12:14 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p.header_finished())
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "incomplete HTTP packet from %s"
|
|
|
|
, print_endpoint(from).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-25 05:14:05 +02:00
|
|
|
std::string url = p.header("location");
|
2007-03-27 09:04:31 +02:00
|
|
|
if (url.empty())
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "missing location header from %s"
|
|
|
|
, print_endpoint(from).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rootdevice d;
|
|
|
|
d.url = url;
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
std::set<rootdevice>::iterator i = m_devices.find(d);
|
|
|
|
|
|
|
|
if (i == m_devices.end())
|
|
|
|
{
|
|
|
|
std::string protocol;
|
2007-05-22 22:44:18 +02:00
|
|
|
std::string auth;
|
2007-03-27 09:04:31 +02:00
|
|
|
// we don't have this device in our list. Add it
|
2009-06-12 18:40:38 +02:00
|
|
|
boost::tie(protocol, auth, d.hostname, d.port, d.path)
|
|
|
|
= parse_url_components(d.url, ec);
|
2013-02-26 06:57:29 +01:00
|
|
|
if (d.port == -1) d.port = protocol == "http" ? 80 : 443;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
if (ec)
|
2008-05-17 16:19:34 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "invalid URL %s from %s: %s"
|
2013-01-19 07:39:32 +01:00
|
|
|
, d.url.c_str(), print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-05-17 16:19:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-22 22:44:18 +02:00
|
|
|
// ignore the auth here. It will be re-parsed
|
|
|
|
// by the http connection later
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
if (protocol != "http")
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "unsupported protocol %s from %s"
|
|
|
|
, protocol.c_str(), print_endpoint(from).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.port == 0)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "URL with port 0 from %s"
|
|
|
|
, print_endpoint(from).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-10-22 03:12:14 +02:00
|
|
|
|
2015-08-18 13:55:50 +02:00
|
|
|
{
|
|
|
|
char msg[500];
|
|
|
|
snprintf(msg, sizeof(msg), "found rootdevice: %s (%d)"
|
|
|
|
, d.url.c_str(), int(m_devices.size()));
|
|
|
|
log(msg, l);
|
|
|
|
}
|
2007-10-26 02:47:30 +02:00
|
|
|
|
|
|
|
if (m_devices.size() >= 50)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-05-25 10:55:43 +02:00
|
|
|
snprintf(msg, sizeof(msg), "too many rootdevices: (%d). Ignoring %s"
|
|
|
|
, int(m_devices.size()), d.url.c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-10-26 02:47:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-10 07:47:01 +01:00
|
|
|
d.non_router = non_router;
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
TORRENT_ASSERT(d.mapping.empty());
|
|
|
|
for (std::vector<global_mapping_t>::iterator j = m_mappings.begin()
|
|
|
|
, end(m_mappings.end()); j != end; ++j)
|
2007-04-01 03:18:10 +02:00
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
mapping_t m;
|
|
|
|
m.action = mapping_t::action_add;
|
|
|
|
m.local_port = j->local_port;
|
|
|
|
m.external_port = j->external_port;
|
|
|
|
m.protocol = j->protocol;
|
|
|
|
d.mapping.push_back(m);
|
2007-04-01 03:18:10 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
boost::tie(i, boost::tuples::ignore) = m_devices.insert(d);
|
|
|
|
}
|
2007-05-08 20:02:10 +02:00
|
|
|
|
|
|
|
|
2014-03-10 07:47:01 +01:00
|
|
|
// iterate over the devices we know and connect and issue the mappings
|
|
|
|
try_map_upnp(l);
|
|
|
|
|
|
|
|
if (m_ignore_non_routers)
|
2007-05-08 20:02:10 +02:00
|
|
|
{
|
2014-03-19 06:28:54 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("upnp::map_timer");
|
|
|
|
#endif
|
2014-03-10 07:47:01 +01:00
|
|
|
// check back in in a little bit to see if we have seen any
|
|
|
|
// devices at one of our default routes. If not, we want to override
|
|
|
|
// ignoring them and use them instead (better than not working).
|
|
|
|
m_map_timer.expires_from_now(seconds(1), ec);
|
|
|
|
m_map_timer.async_wait(boost::bind(&upnp::map_timer
|
|
|
|
, self(), _1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void upnp::map_timer(error_code const& ec)
|
|
|
|
{
|
2014-03-19 06:28:54 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("upnp::map_timer");
|
|
|
|
#endif
|
2014-03-10 07:47:01 +01:00
|
|
|
if (ec) return;
|
2014-03-19 06:28:54 +01:00
|
|
|
if (m_closing) return;
|
2014-03-10 07:47:01 +01:00
|
|
|
|
|
|
|
mutex::scoped_lock l(m_mutex);
|
|
|
|
try_map_upnp(l, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void upnp::try_map_upnp(mutex::scoped_lock& l, bool timer)
|
|
|
|
{
|
|
|
|
if (m_devices.empty()) return;
|
|
|
|
|
|
|
|
bool override_ignore_non_routers = false;
|
|
|
|
if (m_ignore_non_routers && timer)
|
|
|
|
{
|
|
|
|
// if we don't ave any devices that match our default route, we
|
|
|
|
// should try to map with the ones we did hear from anyway,
|
|
|
|
// regardless of if they are not running at our gateway.
|
|
|
|
override_ignore_non_routers = std::find_if(m_devices.begin()
|
|
|
|
, m_devices.end(), boost::bind(&rootdevice::non_router, _1) == false)
|
|
|
|
== m_devices.end();
|
|
|
|
if (override_ignore_non_routers)
|
2007-05-08 20:02:10 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2014-03-10 07:47:01 +01:00
|
|
|
snprintf(msg, sizeof(msg), "overriding ignore non-routers");
|
|
|
|
log(msg, l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::set<rootdevice>::iterator i = m_devices.begin()
|
|
|
|
, end(m_devices.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
// if we're ignoring non-routers, skip them. If on_timer is
|
|
|
|
// set, we expect to have received all responses and if we don't
|
|
|
|
// have any devices at our default route, then issue requests
|
|
|
|
// to any device we found.
|
|
|
|
if (m_ignore_non_routers && i->non_router
|
|
|
|
&& !override_ignore_non_routers)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i->control_url.empty() && !i->upnp_connection && !i->disabled)
|
|
|
|
{
|
|
|
|
// we don't have a WANIP or WANPPP url for this device,
|
|
|
|
// ask for it
|
|
|
|
rootdevice& d = const_cast<rootdevice&>(*i);
|
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
|
|
|
TORRENT_TRY
|
2007-05-08 20:02:10 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2014-03-10 07:47:01 +01:00
|
|
|
snprintf(msg, sizeof(msg), "connecting to: %s"
|
|
|
|
, d.url.c_str());
|
|
|
|
log(msg, l);
|
|
|
|
|
|
|
|
if (d.upnp_connection) d.upnp_connection->close();
|
|
|
|
d.upnp_connection.reset(new http_connection(m_io_service
|
2014-10-03 22:56:57 +02:00
|
|
|
, m_resolver
|
2014-07-06 21:18:00 +02:00
|
|
|
, boost::bind(&upnp::on_upnp_xml, self(), _1, _2
|
2014-03-10 07:47:01 +01:00
|
|
|
, boost::ref(d), _5)));
|
|
|
|
d.upnp_connection->get(d.url, seconds(30), 1);
|
|
|
|
}
|
|
|
|
TORRENT_CATCH (std::exception& exc)
|
|
|
|
{
|
|
|
|
TORRENT_DECLARE_DUMMY(std::exception, exc);
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2014-03-10 07:47:01 +01:00
|
|
|
snprintf(msg, sizeof(msg), "connection failed to: %s %s"
|
|
|
|
, d.url.c_str(), exc.what());
|
|
|
|
log(msg, l);
|
|
|
|
d.disabled = true;
|
2007-05-08 20:02:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
void upnp::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-03-27 09:04:31 +02:00
|
|
|
{
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-03-15 17:07:49 +01:00
|
|
|
TORRENT_ASSERT(d.upnp_connection);
|
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
char header[2048];
|
2014-12-30 10:02:20 +01:00
|
|
|
snprintf(header, sizeof(header), "POST %s HTTP/1.1\r\n"
|
2009-04-13 06:22:03 +02:00
|
|
|
"Host: %s:%u\r\n"
|
2007-03-27 09:04:31 +02:00
|
|
|
"Content-Type: text/xml; charset=\"utf-8\"\r\n"
|
2009-05-25 10:55:43 +02:00
|
|
|
"Content-Length: %d\r\n"
|
2009-04-13 06:22:03 +02:00
|
|
|
"Soapaction: \"%s#%s\"\r\n\r\n"
|
|
|
|
"%s"
|
|
|
|
, d.path.c_str(), d.hostname.c_str(), d.port
|
2009-05-25 10:55:43 +02:00
|
|
|
, int(strlen(soap)), d.service_namespace, soap_action
|
2009-04-13 06:22:03 +02:00
|
|
|
, soap);
|
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
d.upnp_connection->m_sendbuffer = header;
|
2009-04-13 06:22:03 +02:00
|
|
|
|
2009-06-29 10:09:32 +02:00
|
|
|
char msg[1024];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "sending: %s", header);
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i)
|
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-03-15 17:07:49 +01:00
|
|
|
|
|
|
|
if (!d.upnp_connection)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(d.disabled);
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "mapping %u aborted", i);
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-03-15 17:07:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
char const* soap_action = "AddPortMapping";
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2015-08-16 18:17:23 +02:00
|
|
|
error_code ec;
|
2009-07-19 04:20:38 +02:00
|
|
|
std::string local_endpoint = print_address(c.socket().local_endpoint(ec).address());
|
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
char soap[2048];
|
|
|
|
snprintf(soap, sizeof(soap), "<?xml version=\"1.0\"?>\n"
|
2007-09-10 01:52:34 +02:00
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
2009-04-13 06:22:03 +02:00
|
|
|
"<s:Body><u:%s xmlns:u=\"%s\">"
|
|
|
|
"<NewRemoteHost></NewRemoteHost>"
|
|
|
|
"<NewExternalPort>%u</NewExternalPort>"
|
|
|
|
"<NewProtocol>%s</NewProtocol>"
|
|
|
|
"<NewInternalPort>%u</NewInternalPort>"
|
|
|
|
"<NewInternalClient>%s</NewInternalClient>"
|
2007-09-10 01:52:34 +02:00
|
|
|
"<NewEnabled>1</NewEnabled>"
|
2009-07-19 04:20:38 +02:00
|
|
|
"<NewPortMappingDescription>%s at %s:%d</NewPortMappingDescription>"
|
2009-04-13 06:22:03 +02:00
|
|
|
"<NewLeaseDuration>%u</NewLeaseDuration>"
|
|
|
|
"</u:%s></s:Body></s:Envelope>"
|
|
|
|
, soap_action, d.service_namespace, d.mapping[i].external_port
|
|
|
|
, (d.mapping[i].protocol == udp ? "UDP" : "TCP")
|
|
|
|
, d.mapping[i].local_port
|
2009-07-19 04:20:38 +02:00
|
|
|
, local_endpoint.c_str()
|
|
|
|
, m_user_agent.c_str(), local_endpoint.c_str(), d.mapping[i].local_port
|
|
|
|
, d.lease_duration, soap_action);
|
2009-04-13 06:22:03 +02:00
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
post(d, soap, soap_action, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void upnp::next(rootdevice& d, int i, mutex::scoped_lock& l)
|
2008-04-06 21:17:58 +02:00
|
|
|
{
|
|
|
|
if (i < num_mappings() - 1)
|
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
update_map(d, i + 1, l);
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-02-24 05:25:35 +01:00
|
|
|
std::vector<mapping_t>::iterator j
|
2008-04-06 21:17:58 +02:00
|
|
|
= std::find_if(d.mapping.begin(), d.mapping.end()
|
|
|
|
, boost::bind(&mapping_t::action, _1) != int(mapping_t::action_none));
|
2011-02-24 05:25:35 +01:00
|
|
|
if (j == d.mapping.end()) return;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2011-02-24 05:25:35 +01:00
|
|
|
update_map(d, j - d.mapping.begin(), l);
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void upnp::update_map(rootdevice& d, int i, mutex::scoped_lock& l)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-04-06 21:17:58 +02:00
|
|
|
TORRENT_ASSERT(i < int(d.mapping.size()));
|
|
|
|
TORRENT_ASSERT(d.mapping.size() == m_mappings.size());
|
|
|
|
|
2007-04-04 02:08:00 +02:00
|
|
|
if (d.upnp_connection) return;
|
|
|
|
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<upnp> me(self());
|
2009-07-01 06:02:16 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
mapping_t& m = d.mapping[i];
|
|
|
|
|
|
|
|
if (m.action == mapping_t::action_none
|
|
|
|
|| m.protocol == none)
|
2007-04-01 03:18:10 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "mapping %u does not need updating, skipping", i);
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2009-02-18 04:46:36 +01:00
|
|
|
m.action = mapping_t::action_none;
|
2009-06-13 10:04:53 +02:00
|
|
|
next(d, i, l);
|
2007-04-01 03:18:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!d.upnp_connection);
|
|
|
|
TORRENT_ASSERT(d.service_namespace);
|
2007-05-06 00:06:26 +02:00
|
|
|
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "connecting to %s", d.hostname.c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-04-06 21:17:58 +02:00
|
|
|
if (m.action == mapping_t::action_add)
|
|
|
|
{
|
|
|
|
if (m.failcount > 5)
|
|
|
|
{
|
2009-02-18 04:46:36 +01:00
|
|
|
m.action = mapping_t::action_none;
|
2008-04-06 21:17:58 +02:00
|
|
|
// giving up
|
2009-06-13 10:04:53 +02:00
|
|
|
next(d, i, l);
|
2008-04-06 21:17:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-17 02:27:26 +02:00
|
|
|
if (d.upnp_connection) d.upnp_connection->close();
|
2008-04-06 21:17:58 +02:00
|
|
|
d.upnp_connection.reset(new http_connection(m_io_service
|
2014-10-03 22:56:57 +02:00
|
|
|
, m_resolver
|
2014-07-06 21:18:00 +02:00
|
|
|
, boost::bind(&upnp::on_upnp_map_response, self(), _1, _2
|
2012-10-19 03:28:47 +02:00
|
|
|
, boost::ref(d), i, _5), true, default_max_bottled_buffer_size
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&upnp::create_port_mapping, self(), _1, boost::ref(d), i)));
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
d.upnp_connection->start(d.hostname, d.port
|
2008-04-06 21:17:58 +02:00
|
|
|
, seconds(10), 1);
|
|
|
|
}
|
|
|
|
else if (m.action == mapping_t::action_delete)
|
|
|
|
{
|
2008-05-17 02:27:26 +02:00
|
|
|
if (d.upnp_connection) d.upnp_connection->close();
|
2008-04-06 21:17:58 +02:00
|
|
|
d.upnp_connection.reset(new http_connection(m_io_service
|
2014-10-03 22:56:57 +02:00
|
|
|
, m_resolver
|
2014-07-06 21:18:00 +02:00
|
|
|
, boost::bind(&upnp::on_upnp_unmap_response, self(), _1, _2
|
2012-10-19 03:28:47 +02:00
|
|
|
, boost::ref(d), i, _5), true, default_max_bottled_buffer_size
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&upnp::delete_port_mapping, self(), boost::ref(d), i)));
|
2014-07-06 21:18:00 +02:00
|
|
|
d.upnp_connection->start(d.hostname, d.port
|
2008-04-06 21:17:58 +02:00
|
|
|
, seconds(10), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
m.action = mapping_t::action_none;
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
void upnp::delete_port_mapping(rootdevice& d, int i)
|
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-03-15 17:07:49 +01:00
|
|
|
|
|
|
|
if (!d.upnp_connection)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(d.disabled);
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "unmapping %u aborted", i);
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-03-15 17:07:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
char const* soap_action = "DeletePortMapping";
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2009-04-13 06:22:03 +02:00
|
|
|
char soap[2048];
|
|
|
|
error_code ec;
|
|
|
|
snprintf(soap, sizeof(soap), "<?xml version=\"1.0\"?>\n"
|
2007-03-27 09:04:31 +02:00
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
2009-04-13 06:22:03 +02:00
|
|
|
"<s:Body><u:%s xmlns:u=\"%s\">"
|
2010-09-30 09:06:54 +02:00
|
|
|
"<NewRemoteHost></NewRemoteHost>"
|
2009-04-13 06:22:03 +02:00
|
|
|
"<NewExternalPort>%u</NewExternalPort>"
|
|
|
|
"<NewProtocol>%s</NewProtocol>"
|
|
|
|
"</u:%s></s:Body></s:Envelope>"
|
|
|
|
, soap_action, d.service_namespace
|
|
|
|
, d.mapping[i].external_port
|
|
|
|
, (d.mapping[i].protocol == udp ? "UDP" : "TCP")
|
|
|
|
, soap_action);
|
2015-06-06 08:10:53 +02:00
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
post(d, soap, soap_action, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2015-09-06 01:31:47 +02:00
|
|
|
void copy_tolower(std::string& dst, char const* src, int len)
|
2008-10-28 10:49:26 +01:00
|
|
|
{
|
|
|
|
dst.clear();
|
2015-09-06 01:31:47 +02:00
|
|
|
dst.reserve(len);
|
|
|
|
while (len-- > 0)
|
|
|
|
{
|
|
|
|
dst.push_back(to_lower(*src++));
|
|
|
|
}
|
2008-10-28 10:49:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 01:31:47 +02:00
|
|
|
TORRENT_EXTRA_EXPORT void find_control_url(int type, char const* string
|
|
|
|
, int str_len, parse_state& state)
|
2008-10-28 10:49:26 +01:00
|
|
|
{
|
|
|
|
if (type == xml_start_tag)
|
|
|
|
{
|
|
|
|
std::string tag;
|
2015-09-06 01:31:47 +02:00
|
|
|
copy_tolower(tag, string, str_len);
|
2008-10-28 10:49:26 +01:00
|
|
|
state.tag_stack.push_back(tag);
|
|
|
|
// std::copy(state.tag_stack.begin(), state.tag_stack.end(), std::ostream_iterator<std::string>(std::cout, " "));
|
|
|
|
// std::cout << std::endl;
|
|
|
|
}
|
|
|
|
else if (type == xml_end_tag)
|
|
|
|
{
|
|
|
|
if (!state.tag_stack.empty())
|
2008-11-02 22:11:00 +01:00
|
|
|
{
|
|
|
|
if (state.in_service && state.tag_stack.back() == "service")
|
|
|
|
state.in_service = false;
|
2008-10-28 10:49:26 +01:00
|
|
|
state.tag_stack.pop_back();
|
2008-11-02 22:11:00 +01:00
|
|
|
}
|
2008-10-28 10:49:26 +01:00
|
|
|
}
|
|
|
|
else if (type == xml_string)
|
|
|
|
{
|
|
|
|
if (state.tag_stack.empty()) return;
|
2015-07-06 14:33:29 +02:00
|
|
|
// std::cout << " " << string << std::endl;}
|
2008-11-02 22:11:00 +01:00
|
|
|
if (!state.in_service && state.top_tags("service", "servicetype"))
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2015-09-06 01:31:47 +02:00
|
|
|
std::string name(string, str_len);
|
|
|
|
if (string_equal_no_case(name.c_str(), "urn:schemas-upnp-org:service:WANIPConnection:1")
|
|
|
|
|| string_equal_no_case(name.c_str(), "urn:schemas-upnp-org:service:WANIPConnection:2")
|
|
|
|
|| string_equal_no_case(name.c_str(), "urn:schemas-upnp-org:service:WANPPPConnection:1"))
|
2015-07-06 14:33:29 +02:00
|
|
|
{
|
2015-09-06 01:31:47 +02:00
|
|
|
state.service_type.assign(string, str_len);
|
2008-11-02 22:11:00 +01:00
|
|
|
state.in_service = true;
|
2015-07-06 14:33:29 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2015-07-06 14:33:29 +02:00
|
|
|
else if (state.control_url.empty() && state.in_service && state.top_tags("service", "controlurl") && strlen(string) > 0)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2011-09-18 22:28:06 +02:00
|
|
|
// default to the first (or only) control url in the router's listing
|
2015-09-06 01:31:47 +02:00
|
|
|
state.control_url.assign(string, str_len);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2008-11-02 22:11:00 +01:00
|
|
|
else if (state.model.empty() && state.top_tags("device", "modelname"))
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2015-09-06 01:31:47 +02:00
|
|
|
state.model.assign(string, str_len);
|
2008-10-28 10:49:26 +01:00
|
|
|
}
|
|
|
|
else if (state.tag_stack.back() == "urlbase")
|
|
|
|
{
|
2015-09-06 01:31:47 +02:00
|
|
|
state.url_base.assign(string, str_len);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void upnp::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)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<upnp> me(self());
|
2009-07-01 06:02:16 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-05-17 02:27:26 +02:00
|
|
|
if (d.upnp_connection && d.upnp_connection.get() == &c)
|
2007-04-01 17:39:08 +02:00
|
|
|
{
|
|
|
|
d.upnp_connection->close();
|
|
|
|
d.upnp_connection.reset();
|
|
|
|
}
|
2007-03-30 18:57:55 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e && e != boost::asio::error::eof)
|
2007-03-30 18:57:55 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "error while fetching control url from: %s: %s"
|
2013-01-19 07:39:32 +01:00
|
|
|
, d.url.c_str(), convert_from_native(e.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-10-26 02:47:30 +02:00
|
|
|
d.disabled = true;
|
2007-03-30 18:57:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-01 17:39:08 +02:00
|
|
|
if (!p.header_finished())
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "error while fetching control url from: %s: incomplete HTTP message"
|
|
|
|
, d.url.c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-10-26 02:47:30 +02:00
|
|
|
d.disabled = true;
|
2007-08-07 09:59:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.status_code() != 200)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "error while fetching control url from: %s: %s"
|
2013-01-19 07:39:32 +01:00
|
|
|
, d.url.c_str(), convert_from_native(p.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-10-26 02:47:30 +02:00
|
|
|
d.disabled = true;
|
2007-04-01 17:39:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
parse_state s;
|
2015-09-06 01:31:47 +02:00
|
|
|
xml_parse(p.get_body().begin, p.get_body().end
|
|
|
|
, boost::bind(&find_control_url, _1, _2, _3, boost::ref(s)));
|
2015-07-06 14:33:29 +02:00
|
|
|
if (s.control_url.empty())
|
2007-05-06 00:06:26 +02:00
|
|
|
{
|
2015-07-06 14:33:29 +02:00
|
|
|
char msg[500];
|
|
|
|
snprintf(msg, sizeof(msg), "could not find a port mapping interface in response from: %s"
|
|
|
|
, d.url.c_str());
|
|
|
|
log(msg, l);
|
|
|
|
d.disabled = true;
|
|
|
|
return;
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2015-07-06 14:33:29 +02:00
|
|
|
static std::string service_type;
|
|
|
|
service_type.swap(s.service_type);
|
|
|
|
d.service_namespace = service_type.c_str();
|
|
|
|
if (!s.model.empty()) m_model = s.model;
|
|
|
|
|
2009-05-31 04:41:59 +02:00
|
|
|
if (!s.url_base.empty() && s.control_url.substr(0, 7) != "http://")
|
2008-11-21 19:23:44 +01:00
|
|
|
{
|
|
|
|
// avoid double slashes in path
|
|
|
|
if (s.url_base[s.url_base.size()-1] == '/'
|
|
|
|
&& !s.control_url.empty()
|
|
|
|
&& s.control_url[0] == '/')
|
|
|
|
s.url_base.erase(s.url_base.end()-1);
|
|
|
|
d.control_url = s.url_base + s.control_url;
|
|
|
|
}
|
|
|
|
else d.control_url = s.control_url;
|
2008-10-28 10:49:26 +01:00
|
|
|
|
2008-10-14 09:19:46 +02:00
|
|
|
std::string protocol;
|
|
|
|
std::string auth;
|
2009-06-12 18:40:38 +02:00
|
|
|
error_code ec;
|
2008-10-29 02:17:19 +01:00
|
|
|
if (!d.control_url.empty() && d.control_url[0] == '/')
|
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
boost::tie(protocol, auth, d.hostname, d.port, d.path)
|
|
|
|
= parse_url_components(d.url, ec);
|
2013-02-26 06:57:29 +01:00
|
|
|
if (d.port == -1) d.port = protocol == "http" ? 80 : 443;
|
2008-10-29 02:17:19 +01:00
|
|
|
d.control_url = protocol + "://" + d.hostname + ":"
|
2009-01-27 07:17:55 +01:00
|
|
|
+ to_string(d.port).elems + s.control_url;
|
2008-10-29 02:17:19 +01:00
|
|
|
}
|
|
|
|
|
2015-08-18 13:55:50 +02:00
|
|
|
{
|
|
|
|
char msg[500];
|
|
|
|
snprintf(msg, sizeof(msg), "found control URL: %s namespace %s "
|
|
|
|
"urlbase: %s in response from %s"
|
|
|
|
, d.control_url.c_str(), d.service_namespace
|
|
|
|
, s.url_base.c_str(), d.url.c_str());
|
|
|
|
log(msg, l);
|
|
|
|
}
|
2008-10-29 02:17:19 +01:00
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
boost::tie(protocol, auth, d.hostname, d.port, d.path)
|
|
|
|
= parse_url_components(d.control_url, ec);
|
2013-02-26 06:57:29 +01:00
|
|
|
if (d.port == -1) d.port = protocol == "http" ? 80 : 443;
|
2008-10-14 09:19:46 +02:00
|
|
|
|
2009-06-12 18:40:38 +02:00
|
|
|
if (ec)
|
2008-10-14 09:19:46 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "failed to parse URL '%s': %s"
|
2013-01-19 07:39:32 +01:00
|
|
|
, d.control_url.c_str(), convert_from_native(ec.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-10-14 09:19:46 +02:00
|
|
|
d.disabled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
d.upnp_connection.reset(new http_connection(m_io_service
|
2014-10-03 22:56:57 +02:00
|
|
|
, m_resolver
|
2014-07-06 21:18:00 +02:00
|
|
|
, boost::bind(&upnp::on_upnp_get_ip_address_response, self(), _1, _2
|
2015-07-06 14:33:29 +02:00
|
|
|
, boost::ref(d), _5), true, default_max_bottled_buffer_size
|
2010-12-05 21:40:28 +01:00
|
|
|
, boost::bind(&upnp::get_ip_address, self(), boost::ref(d))));
|
2014-07-06 21:18:00 +02:00
|
|
|
d.upnp_connection->start(d.hostname, d.port
|
2010-12-05 21:40:28 +01:00
|
|
|
, seconds(10), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void upnp::get_ip_address(rootdevice& d)
|
|
|
|
{
|
|
|
|
mutex::scoped_lock l(m_mutex);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
|
|
|
|
|
|
|
if (!d.upnp_connection)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(d.disabled);
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2010-12-05 21:40:28 +01:00
|
|
|
snprintf(msg, sizeof(msg), "getting external IP address");
|
|
|
|
log(msg, l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* soap_action = "GetExternalIPAddress";
|
|
|
|
|
|
|
|
char soap[2048];
|
|
|
|
error_code ec;
|
|
|
|
snprintf(soap, sizeof(soap), "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<s:Body><u:%s xmlns:u=\"%s\">"
|
|
|
|
"</u:%s></s:Body></s:Envelope>"
|
|
|
|
, soap_action, d.service_namespace
|
|
|
|
, soap_action);
|
|
|
|
|
|
|
|
post(d, soap, soap_action, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2007-05-14 09:31:01 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void upnp::disable(error_code const& ec, mutex::scoped_lock& l)
|
2007-05-10 20:00:10 +02:00
|
|
|
{
|
|
|
|
m_disabled = true;
|
2008-04-06 21:17:58 +02:00
|
|
|
|
|
|
|
// kill all mappings
|
|
|
|
for (std::vector<global_mapping_t>::iterator i = m_mappings.begin()
|
|
|
|
, end(m_mappings.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->protocol == none) continue;
|
|
|
|
i->protocol = none;
|
2009-06-13 10:04:53 +02:00
|
|
|
l.unlock();
|
2010-12-05 21:40:28 +01:00
|
|
|
m_callback(i - m_mappings.begin(), address(), 0, ec);
|
2009-06-13 10:04:53 +02:00
|
|
|
l.lock();
|
2008-04-06 21:17:58 +02:00
|
|
|
}
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2009-03-01 03:25:46 +01:00
|
|
|
// we cannot clear the devices since there
|
|
|
|
// might be outstanding requests relying on
|
|
|
|
// the device entry being present when they
|
|
|
|
// complete
|
2009-06-12 18:40:38 +02:00
|
|
|
error_code e;
|
|
|
|
m_broadcast_timer.cancel(e);
|
|
|
|
m_refresh_timer.cancel(e);
|
2014-03-10 07:47:01 +01:00
|
|
|
m_map_timer.cancel(e);
|
2007-05-14 09:31:01 +02:00
|
|
|
m_socket.close();
|
2007-05-10 20:00:10 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct error_code_parse_state
|
|
|
|
{
|
|
|
|
error_code_parse_state(): in_error_code(false), exit(false), error_code(-1) {}
|
|
|
|
bool in_error_code;
|
|
|
|
bool exit;
|
|
|
|
int error_code;
|
|
|
|
};
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
void find_error_code(int type, char const* string, error_code_parse_state& state)
|
|
|
|
{
|
|
|
|
if (state.exit) return;
|
2009-01-27 09:24:48 +01:00
|
|
|
if (type == xml_start_tag && !std::strcmp("errorCode", string))
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
|
|
|
state.in_error_code = true;
|
|
|
|
}
|
|
|
|
else if (type == xml_string && state.in_error_code)
|
|
|
|
{
|
|
|
|
state.error_code = std::atoi(string);
|
|
|
|
state.exit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
struct ip_address_parse_state: public error_code_parse_state
|
|
|
|
{
|
|
|
|
ip_address_parse_state(): in_ip_address(false) {}
|
|
|
|
bool in_ip_address;
|
|
|
|
std::string ip_address;
|
|
|
|
};
|
|
|
|
|
|
|
|
void find_ip_address(int type, char const* string, ip_address_parse_state& state)
|
|
|
|
{
|
|
|
|
find_error_code(type, string, state);
|
|
|
|
if (state.exit) return;
|
|
|
|
|
|
|
|
if (type == xml_start_tag && !std::strcmp("NewExternalIPAddress", string))
|
|
|
|
{
|
|
|
|
state.in_ip_address = true;
|
|
|
|
}
|
|
|
|
else if (type == xml_string && state.in_ip_address)
|
|
|
|
{
|
|
|
|
state.ip_address = string;
|
|
|
|
state.exit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-07 02:12:33 +02:00
|
|
|
struct error_code_t
|
|
|
|
{
|
|
|
|
int code;
|
|
|
|
char const* msg;
|
|
|
|
};
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2007-05-07 02:12:33 +02:00
|
|
|
error_code_t error_codes[] =
|
|
|
|
{
|
2009-06-12 18:40:38 +02:00
|
|
|
{0, "no error"}
|
|
|
|
, {402, "Invalid Arguments"}
|
2007-05-07 02:12:33 +02:00
|
|
|
, {501, "Action Failed"}
|
|
|
|
, {714, "The specified value does not exist in the array"}
|
|
|
|
, {715, "The source IP address cannot be wild-carded"}
|
|
|
|
, {716, "The external port cannot be wild-carded"}
|
|
|
|
, {718, "The port mapping entry specified conflicts with "
|
|
|
|
"a mapping assigned previously to another client"}
|
|
|
|
, {724, "Internal and External port values must be the same"}
|
|
|
|
, {725, "The NAT implementation only supports permanent "
|
|
|
|
"lease times on port mappings"}
|
|
|
|
, {726, "RemoteHost must be a wildcard and cannot be a "
|
|
|
|
"specific IP address or DNS name"}
|
|
|
|
, {727, "ExternalPort must be a wildcard and cannot be a specific port "}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-02 04:05:55 +01:00
|
|
|
struct upnp_error_category : boost::system::error_category
|
2009-06-12 18:40:38 +02:00
|
|
|
{
|
2014-02-02 04:05:55 +01:00
|
|
|
virtual const char* name() const BOOST_SYSTEM_NOEXCEPT
|
|
|
|
{
|
|
|
|
return "UPnP error";
|
|
|
|
}
|
2009-06-12 18:40:38 +02:00
|
|
|
|
2014-02-02 04:05:55 +01:00
|
|
|
virtual std::string message(int ev) const BOOST_SYSTEM_NOEXCEPT
|
2009-06-12 18:40:38 +02:00
|
|
|
{
|
2014-02-02 04:05:55 +01:00
|
|
|
int num_errors = sizeof(error_codes) / sizeof(error_codes[0]);
|
|
|
|
error_code_t* end = error_codes + num_errors;
|
|
|
|
error_code_t tmp = {ev, 0};
|
|
|
|
error_code_t* e = std::lower_bound(error_codes, end, tmp
|
|
|
|
, boost::bind(&error_code_t::code, _1) < boost::bind(&error_code_t::code, _2));
|
|
|
|
if (e != end && e->code == ev)
|
|
|
|
{
|
|
|
|
return e->msg;
|
|
|
|
}
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2014-02-02 04:05:55 +01:00
|
|
|
snprintf(msg, sizeof(msg), "unknown UPnP error (%d)", ev);
|
|
|
|
return msg;
|
2009-06-12 18:40:38 +02:00
|
|
|
}
|
|
|
|
|
2014-02-02 04:05:55 +01:00
|
|
|
virtual boost::system::error_condition default_error_condition(
|
|
|
|
int ev) const BOOST_SYSTEM_NOEXCEPT
|
|
|
|
{
|
|
|
|
return boost::system::error_condition(ev, *this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::system::error_category& get_upnp_category()
|
2009-06-12 18:40:38 +02:00
|
|
|
{
|
2014-02-02 04:05:55 +01:00
|
|
|
static upnp_error_category cat;
|
|
|
|
return cat;
|
2009-06-12 18:40:38 +02:00
|
|
|
}
|
|
|
|
|
2010-12-05 21:40:28 +01:00
|
|
|
void upnp::on_upnp_get_ip_address_response(error_code const& e
|
|
|
|
, libtorrent::http_parser const& p, rootdevice& d
|
|
|
|
, http_connection& c)
|
|
|
|
{
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<upnp> me(self());
|
2010-12-05 21:40:28 +01:00
|
|
|
|
|
|
|
mutex::scoped_lock l(m_mutex);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
|
|
|
if (d.upnp_connection && d.upnp_connection.get() == &c)
|
|
|
|
{
|
|
|
|
d.upnp_connection->close();
|
|
|
|
d.upnp_connection.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_closing) return;
|
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e && e != boost::asio::error::eof)
|
2010-12-05 21:40:28 +01:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2013-01-19 07:39:32 +01:00
|
|
|
snprintf(msg, sizeof(msg), "error while getting external IP address: %s"
|
|
|
|
, convert_from_native(e.message()).c_str());
|
2010-12-05 21:40:28 +01:00
|
|
|
log(msg, l);
|
|
|
|
if (num_mappings() > 0) update_map(d, 0, l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p.header_finished())
|
|
|
|
{
|
|
|
|
log("error while getting external IP address: incomplete http message", l);
|
|
|
|
if (num_mappings() > 0) update_map(d, 0, l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.status_code() != 200)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2013-01-19 07:39:32 +01:00
|
|
|
snprintf(msg, sizeof(msg), "error while getting external IP address: %s"
|
|
|
|
, convert_from_native(p.message()).c_str());
|
2010-12-05 21:40:28 +01:00
|
|
|
log(msg, l);
|
|
|
|
if (num_mappings() > 0) update_map(d, 0, l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// response may look like
|
|
|
|
// <?xml version="1.0"?>
|
|
|
|
// <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
|
|
|
// <s:Body><u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
|
|
|
|
// <NewExternalIPAddress>192.168.160.19</NewExternalIPAddress>
|
|
|
|
// </u:GetExternalIPAddressResponse>
|
|
|
|
// </s:Body>
|
|
|
|
// </s:Envelope>
|
|
|
|
|
2015-08-18 13:55:50 +02:00
|
|
|
{
|
|
|
|
char msg[500];
|
|
|
|
snprintf(msg, sizeof(msg), "get external IP address response: %s"
|
|
|
|
, std::string(p.get_body().begin, p.get_body().end).c_str());
|
|
|
|
log(msg, l);
|
|
|
|
}
|
2010-12-05 21:40:28 +01:00
|
|
|
|
|
|
|
ip_address_parse_state s;
|
2015-08-18 13:55:50 +02:00
|
|
|
xml_parse(const_cast<char*>(p.get_body().begin), const_cast<char*>(p.get_body().end)
|
2010-12-05 21:40:28 +01:00
|
|
|
, boost::bind(&find_ip_address, _1, _2, boost::ref(s)));
|
|
|
|
if (s.error_code != -1)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2010-12-05 21:40:28 +01:00
|
|
|
snprintf(msg, sizeof(msg), "error while getting external IP address, code: %u"
|
|
|
|
, s.error_code);
|
|
|
|
log(msg, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s.ip_address.empty()) {
|
2015-08-18 13:55:50 +02:00
|
|
|
char msg[500];
|
2010-12-05 21:40:28 +01:00
|
|
|
snprintf(msg, sizeof(msg), "got router external IP address %s", s.ip_address.c_str());
|
|
|
|
log(msg, l);
|
2015-08-16 18:17:23 +02:00
|
|
|
d.external_ip = address::from_string(s.ip_address.c_str(), ignore_error);
|
2010-12-05 21:40:28 +01:00
|
|
|
} else {
|
|
|
|
log("failed to find external IP address in response", l);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_mappings() > 0) update_map(d, 0, l);
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void upnp::on_upnp_map_response(error_code const& e
|
2008-05-17 02:27:26 +02:00
|
|
|
, libtorrent::http_parser const& p, rootdevice& d, int mapping
|
|
|
|
, http_connection& c)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<upnp> me(self());
|
2009-07-01 06:02:16 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-05-17 02:27:26 +02:00
|
|
|
if (d.upnp_connection && d.upnp_connection.get() == &c)
|
2007-04-01 17:39:08 +02:00
|
|
|
{
|
|
|
|
d.upnp_connection->close();
|
|
|
|
d.upnp_connection.reset();
|
|
|
|
}
|
2007-03-30 18:57:55 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e && e != boost::asio::error::eof)
|
2007-03-27 09:04:31 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "error while adding port map: %s"
|
2013-01-19 07:39:32 +01:00
|
|
|
, convert_from_native(e.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-10-26 02:47:30 +02:00
|
|
|
d.disabled = true;
|
2007-03-27 09:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2007-04-01 01:23:30 +02:00
|
|
|
if (m_closing) return;
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
// error code response may look like this:
|
|
|
|
// <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
|
|
|
|
// s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
|
|
|
// <s:Body>
|
|
|
|
// <s:Fault>
|
|
|
|
// <faultcode>s:Client</faultcode>
|
|
|
|
// <faultstring>UPnPError</faultstring>
|
|
|
|
// <detail>
|
|
|
|
// <UPnPErrorxmlns="urn:schemas-upnp-org:control-1-0">
|
|
|
|
// <errorCode>402</errorCode>
|
|
|
|
// <errorDescription>Invalid Args</errorDescription>
|
|
|
|
// </UPnPError>
|
|
|
|
// </detail>
|
|
|
|
// </s:Fault>
|
|
|
|
// </s:Body>
|
|
|
|
// </s:Envelope>
|
|
|
|
|
2007-04-01 17:39:08 +02:00
|
|
|
if (!p.header_finished())
|
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
log("error while adding port map: incomplete http message", l);
|
|
|
|
next(d, mapping, l);
|
2013-11-08 08:23:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ct = p.header("content-type");
|
|
|
|
if (!ct.empty()
|
|
|
|
&& ct.find_first_of("text/xml") == std::string::npos
|
|
|
|
&& ct.find_first_of("text/soap+xml") == std::string::npos
|
|
|
|
&& ct.find_first_of("application/xml") == std::string::npos
|
|
|
|
&& ct.find_first_of("application/soap+xml") == std::string::npos
|
|
|
|
)
|
|
|
|
{
|
|
|
|
char msg[300];
|
|
|
|
snprintf(msg, sizeof(msg), "error while adding port map: invalid content-type, \"%s\". Expected text/xml or application/soap+xml"
|
|
|
|
, ct.c_str());
|
|
|
|
log(msg, l);
|
|
|
|
next(d, mapping, l);
|
2007-04-01 17:39:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-09-17 11:55:48 +02:00
|
|
|
|
|
|
|
// We don't want to ignore responses with return codes other than 200
|
|
|
|
// since those might contain valid UPnP error codes
|
2007-08-07 09:59:51 +02:00
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
error_code_parse_state s;
|
2015-08-18 13:55:50 +02:00
|
|
|
xml_parse(const_cast<char*>(p.get_body().begin)
|
|
|
|
, const_cast<char*>(p.get_body().end)
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&find_error_code, _1, _2, boost::ref(s)));
|
2007-03-27 09:04:31 +02:00
|
|
|
|
|
|
|
if (s.error_code != -1)
|
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "error while adding port map, code: %u"
|
|
|
|
, s.error_code);
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2015-07-06 14:33:29 +02:00
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
mapping_t& m = d.mapping[mapping];
|
|
|
|
|
2007-03-27 09:04:31 +02:00
|
|
|
if (s.error_code == 725)
|
|
|
|
{
|
2007-03-30 18:57:55 +02:00
|
|
|
// only permanent leases supported
|
2007-03-27 09:04:31 +02:00
|
|
|
d.lease_duration = 0;
|
2008-04-06 21:17:58 +02:00
|
|
|
m.action = mapping_t::action_add;
|
|
|
|
++m.failcount;
|
2009-06-13 10:04:53 +02:00
|
|
|
update_map(d, mapping, l);
|
2007-04-01 17:39:08 +02:00
|
|
|
return;
|
2007-03-30 18:57:55 +02:00
|
|
|
}
|
2014-06-15 06:20:39 +02:00
|
|
|
else if (s.error_code == 727)
|
2008-01-07 03:35:29 +01:00
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
return_error(mapping, s.error_code, l);
|
2008-01-07 03:35:29 +01:00
|
|
|
}
|
2014-06-15 06:20:39 +02:00
|
|
|
else if ((s.error_code == 718 || s.error_code == 501) && m.failcount < 4)
|
2007-03-30 18:57:55 +02:00
|
|
|
{
|
2011-07-12 10:33:05 +02:00
|
|
|
// some routers return 501 action failed, instead of 716
|
2014-06-15 06:20:39 +02:00
|
|
|
// The external port conflicts with another mapping
|
2008-01-07 03:35:29 +01:00
|
|
|
// pick a random port
|
2011-02-26 08:55:51 +01:00
|
|
|
m.external_port = 40000 + (random() % 10000);
|
2008-04-06 21:17:58 +02:00
|
|
|
m.action = mapping_t::action_add;
|
|
|
|
++m.failcount;
|
2009-06-13 10:04:53 +02:00
|
|
|
update_map(d, mapping, l);
|
2007-04-01 17:39:08 +02:00
|
|
|
return;
|
2007-03-30 18:57:55 +02:00
|
|
|
}
|
|
|
|
else if (s.error_code != -1)
|
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
return_error(mapping, s.error_code, l);
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2009-12-25 23:21:39 +01:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "map response: %s"
|
|
|
|
, std::string(p.get_body().begin, p.get_body().end).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2007-03-30 18:57:55 +02:00
|
|
|
if (s.error_code == -1)
|
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
l.unlock();
|
2010-12-05 21:40:28 +01:00
|
|
|
m_callback(mapping, d.external_ip, m.external_port, error_code());
|
2009-06-13 10:04:53 +02:00
|
|
|
l.lock();
|
2007-04-01 01:23:30 +02:00
|
|
|
if (d.lease_duration > 0)
|
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
m.expires = aux::time_now()
|
2007-04-01 17:39:08 +02:00
|
|
|
+ seconds(int(d.lease_duration * 0.75f));
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point next_expire = m_refresh_timer.expires_at();
|
|
|
|
if (next_expire < aux::time_now()
|
2008-04-06 21:17:58 +02:00
|
|
|
|| next_expire > m.expires)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("upnp::on_expire");
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-04-06 21:17:58 +02:00
|
|
|
m_refresh_timer.expires_at(m.expires, ec);
|
2010-04-30 21:08:16 +02:00
|
|
|
m_refresh_timer.async_wait(boost::bind(&upnp::on_expire, self(), _1));
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-06 21:17:58 +02:00
|
|
|
m.expires = max_time();
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
2008-04-06 21:17:58 +02:00
|
|
|
m.failcount = 0;
|
2007-03-30 18:57:55 +02:00
|
|
|
}
|
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
next(d, mapping, l);
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
void upnp::return_error(int mapping, int code, mutex::scoped_lock& l)
|
2008-01-07 03:35:29 +01:00
|
|
|
{
|
|
|
|
int num_errors = sizeof(error_codes) / sizeof(error_codes[0]);
|
|
|
|
error_code_t* end = error_codes + num_errors;
|
|
|
|
error_code_t tmp = {code, 0};
|
|
|
|
error_code_t* e = std::lower_bound(error_codes, end, tmp
|
2010-04-30 21:08:16 +02:00
|
|
|
, boost::bind(&error_code_t::code, _1) < boost::bind(&error_code_t::code, _2));
|
2008-01-07 03:35:29 +01:00
|
|
|
std::string error_string = "UPnP mapping error ";
|
2009-01-27 07:17:55 +01:00
|
|
|
error_string += to_string(code).elems;
|
2008-01-07 03:35:29 +01:00
|
|
|
if (e != end && e->code == code)
|
|
|
|
{
|
|
|
|
error_string += ": ";
|
|
|
|
error_string += e->msg;
|
|
|
|
}
|
2009-06-13 10:04:53 +02:00
|
|
|
l.unlock();
|
2014-02-02 04:05:55 +01:00
|
|
|
m_callback(mapping, address(), 0, error_code(code, get_upnp_category()));
|
2009-06-13 10:04:53 +02:00
|
|
|
l.lock();
|
2008-01-07 03:35:29 +01:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void upnp::on_upnp_unmap_response(error_code const& e
|
2008-05-17 02:27:26 +02:00
|
|
|
, libtorrent::http_parser const& p, rootdevice& d, int mapping
|
|
|
|
, http_connection& c)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
2015-01-06 09:08:49 +01:00
|
|
|
boost::shared_ptr<upnp> me(self());
|
2009-07-01 06:02:16 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-05-17 02:27:26 +02:00
|
|
|
if (d.upnp_connection && d.upnp_connection.get() == &c)
|
2007-04-01 17:39:08 +02:00
|
|
|
{
|
|
|
|
d.upnp_connection->close();
|
|
|
|
d.upnp_connection.reset();
|
|
|
|
}
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2015-06-06 07:22:53 +02:00
|
|
|
if (e && e != boost::asio::error::eof)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2013-01-19 07:39:32 +01:00
|
|
|
snprintf(msg, sizeof(msg), "error while deleting portmap: %s"
|
|
|
|
, convert_from_native(e.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2008-10-22 03:12:14 +02:00
|
|
|
}
|
|
|
|
else if (!p.header_finished())
|
2007-04-01 17:39:08 +02:00
|
|
|
{
|
2009-06-13 10:04:53 +02:00
|
|
|
log("error while deleting portmap: incomplete http message", l);
|
2007-04-01 17:39:08 +02:00
|
|
|
}
|
2008-04-06 21:17:58 +02:00
|
|
|
else if (p.status_code() != 200)
|
2007-08-07 09:59:51 +02:00
|
|
|
{
|
2014-03-23 23:49:09 +01:00
|
|
|
char msg[500];
|
2013-01-19 07:39:32 +01:00
|
|
|
snprintf(msg, sizeof(msg), "error while deleting portmap: %s"
|
|
|
|
, convert_from_native(p.message()).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-08-07 09:59:51 +02:00
|
|
|
}
|
2008-04-06 21:17:58 +02:00
|
|
|
else
|
|
|
|
{
|
2010-09-30 09:06:54 +02:00
|
|
|
char msg[500];
|
2009-04-13 06:22:03 +02:00
|
|
|
snprintf(msg, sizeof(msg), "unmap response: %s"
|
|
|
|
, std::string(p.get_body().begin, p.get_body().end).c_str());
|
2009-06-13 10:04:53 +02:00
|
|
|
log(msg, l);
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2013-09-03 06:41:49 +02:00
|
|
|
error_code_parse_state s;
|
2013-10-20 04:40:43 +02:00
|
|
|
if (p.header_finished())
|
|
|
|
{
|
2015-08-18 13:55:50 +02:00
|
|
|
xml_parse(const_cast<char*>(p.get_body().begin)
|
|
|
|
, const_cast<char*>(p.get_body().end)
|
2013-10-20 04:40:43 +02:00
|
|
|
, boost::bind(&find_error_code, _1, _2, boost::ref(s)));
|
|
|
|
}
|
2013-09-03 06:41:49 +02:00
|
|
|
|
|
|
|
l.unlock();
|
|
|
|
m_callback(mapping, address(), 0, p.status_code() != 200
|
|
|
|
? error_code(p.status_code(), get_http_category())
|
2014-02-02 04:05:55 +01:00
|
|
|
: error_code(s.error_code, get_upnp_category()));
|
2013-09-03 06:41:49 +02:00
|
|
|
l.lock();
|
|
|
|
|
2008-04-06 21:17:58 +02:00
|
|
|
d.mapping[mapping].protocol = none;
|
|
|
|
|
2009-06-13 10:04:53 +02:00
|
|
|
next(d, mapping, l);
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
|
|
|
|
2011-02-24 05:25:35 +01:00
|
|
|
void upnp::on_expire(error_code const& ec)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("upnp::on_expire");
|
|
|
|
#endif
|
2011-02-24 05:25:35 +01:00
|
|
|
if (ec) return;
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point now = aux::time_now();
|
|
|
|
time_point next_expire = max_time();
|
2007-04-01 01:23:30 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2007-04-01 01:23:30 +02:00
|
|
|
for (std::set<rootdevice>::iterator i = m_devices.begin()
|
|
|
|
, end(m_devices.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
rootdevice& d = const_cast<rootdevice&>(*i);
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
2008-04-06 21:17:58 +02:00
|
|
|
for (int m = 0; m < num_mappings(); ++m)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
2007-04-05 00:27:36 +02:00
|
|
|
if (d.mapping[m].expires != max_time())
|
2007-04-01 01:23:30 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (d.mapping[m].expires < now)
|
|
|
|
{
|
2007-04-05 00:27:36 +02:00
|
|
|
d.mapping[m].expires = max_time();
|
2009-06-13 10:04:53 +02:00
|
|
|
update_map(d, m, l);
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
2007-04-05 00:27:36 +02:00
|
|
|
else if (d.mapping[m].expires < next_expire)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
|
|
|
next_expire = d.mapping[m].expires;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-05 00:27:36 +02:00
|
|
|
if (next_expire != max_time())
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("upnp::on_expire");
|
|
|
|
#endif
|
2011-03-30 18:03:35 +02:00
|
|
|
error_code e;
|
|
|
|
m_refresh_timer.expires_at(next_expire, e);
|
2010-04-30 21:08:16 +02:00
|
|
|
m_refresh_timer.async_wait(boost::bind(&upnp::on_expire, self(), _1));
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void upnp::close()
|
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
mutex::scoped_lock l(m_mutex);
|
2008-04-06 21:17:58 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-30 00:14:59 +01:00
|
|
|
m_refresh_timer.cancel(ec);
|
|
|
|
m_broadcast_timer.cancel(ec);
|
2014-03-10 07:47:01 +01:00
|
|
|
m_map_timer.cancel(ec);
|
2007-04-01 01:23:30 +02:00
|
|
|
m_closing = true;
|
2007-04-02 22:09:48 +02:00
|
|
|
m_socket.close();
|
|
|
|
|
2007-04-01 01:23:30 +02:00
|
|
|
for (std::set<rootdevice>::iterator i = m_devices.begin()
|
2007-10-26 02:47:30 +02:00
|
|
|
, end(m_devices.end()); i != end; ++i)
|
2007-04-01 01:23:30 +02:00
|
|
|
{
|
|
|
|
rootdevice& d = const_cast<rootdevice&>(*i);
|
2007-10-26 02:47:30 +02:00
|
|
|
TORRENT_ASSERT(d.magic == 1337);
|
|
|
|
if (d.control_url.empty()) continue;
|
2008-04-06 21:17:58 +02:00
|
|
|
for (std::vector<mapping_t>::iterator j = d.mapping.begin()
|
2015-08-18 13:55:50 +02:00
|
|
|
, end2(d.mapping.end()); j != end2; ++j)
|
2008-04-06 21:17:58 +02:00
|
|
|
{
|
|
|
|
if (j->protocol == none) continue;
|
|
|
|
if (j->action == mapping_t::action_add)
|
|
|
|
{
|
|
|
|
j->action = mapping_t::action_none;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
j->action = mapping_t::action_delete;
|
|
|
|
m_mappings[j - d.mapping.begin()].protocol = none;
|
|
|
|
}
|
2009-06-13 10:04:53 +02:00
|
|
|
if (num_mappings() > 0) update_map(d, 0, l);
|
2007-04-01 01:23:30 +02:00
|
|
|
}
|
2007-03-27 09:04:31 +02:00
|
|
|
}
|
|
|
|
|
2014-02-02 04:05:55 +01:00
|
|
|
}
|
|
|
|
|