clean up natpmp and upnp logging (#842)

clean up natpmp and upnp logging. remove custom logging macro and use regular logging via alerts. honor TORRENT_DISABLE_LOGGING
This commit is contained in:
Arvid Norberg 2016-06-23 01:23:59 -04:00 committed by GitHub
parent 3b2a527ffb
commit 21c1540ace
9 changed files with 280 additions and 290 deletions

View File

@ -99,7 +99,7 @@ script:
# if we are building with code coverage, report it as soon as possible # if we are building with code coverage, report it as soon as possible
- cd test - cd test
- 'if [ "$variant" != "" ]; then - 'if [ "$variant" != "" ]; then
bjam -j3 warnings-as-errors=on variant=$variant -l900 $toolset $target && bjam -j3 warnings-as-errors=on variant=$variant -l900 $toolset $target test_natpmp &&
if [ "$coverage" == "1" ]; then if [ "$coverage" == "1" ]; then
codecov --root .. --gcov-exec gcov-5; codecov --root .. --gcov-exec gcov-5;
fi; fi;

View File

@ -81,7 +81,7 @@ build_script:
# test # test
- cd %ROOT_DIRECTORY%\test - cd %ROOT_DIRECTORY%\test
- b2.exe --hash warnings-as-errors=on -j2 address-model=%model% win-tests %compiler% variant=%variant% picker-debugging=on link=shared linkflags=%linkflags% include=%include% testing.execute=off - b2.exe --hash warnings-as-errors=on -j2 address-model=%model% win-tests test_upnp test_natpmp %compiler% variant=%variant% picker-debugging=on link=shared linkflags=%linkflags% include=%include% testing.execute=off
# python binding # python binding
- cd %ROOT_DIRECTORY%\bindings\python - cd %ROOT_DIRECTORY%\bindings\python

View File

@ -154,7 +154,6 @@ my-python-extension libtorrent
<conditional>@libtorrent_linking <conditional>@libtorrent_linking
: # default-build : # default-build
<warnings>all <warnings>all
<warnings-as-errors>on
: # usage-requirements : # usage-requirements
<suppress-import-lib>false <suppress-import-lib>false
; ;

View File

@ -58,7 +58,7 @@ namespace libtorrent
typedef boost::function<void(int, address, int, int, error_code const&)> portmap_callback_t; typedef boost::function<void(int, address, int, int, error_code const&)> portmap_callback_t;
typedef boost::function<void(char const*)> log_callback_t; typedef boost::function<void(char const*)> log_callback_t;
struct natpmp struct TORRENT_EXTRA_EXPORT natpmp
: boost::enable_shared_from_this<natpmp> : boost::enable_shared_from_this<natpmp>
, single_threaded , single_threaded
{ {
@ -135,7 +135,9 @@ private:
}; };
portmap_callback_t m_callback; portmap_callback_t m_callback;
#ifndef TORRENT_DISABLE_LOGGING
log_callback_t m_log_callback; log_callback_t m_log_callback;
#endif
std::vector<mapping_t> m_mappings; std::vector<mapping_t> m_mappings;

View File

@ -220,7 +220,9 @@ private:
void disable(error_code const& ec); void disable(error_code const& ec);
void return_error(int mapping, int code); void return_error(int mapping, int code);
void log(char const* msg); #ifndef TORRENT_DISABLE_LOGGING
void log(char const* msg, ...) const TORRENT_FORMAT(2,3);
#endif
void get_ip_address(rootdevice& d); void get_ip_address(rootdevice& d);
void delete_port_mapping(rootdevice& d, int i); void delete_port_mapping(rootdevice& d, int i);
@ -364,7 +366,9 @@ private:
std::set<rootdevice> m_devices; std::set<rootdevice> m_devices;
portmap_callback_t m_callback; portmap_callback_t m_callback;
#ifndef TORRENT_DISABLE_LOGGING
log_callback_t m_log_callback; log_callback_t m_log_callback;
#endif
// current retry count // current retry count
int m_retry_count; int m_retry_count;

View File

@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/aux_/disable_warnings_pop.hpp"
#include <cstdio> // for snprintf #include <cstdio> // for snprintf
#include <cinttypes> // for PRId64 et.al.
#include <cstdarg> #include <cstdarg>
#include <functional> #include <functional>
@ -56,19 +57,15 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
#include "libtorrent/aux_/escape_string.hpp" #include "libtorrent/aux_/escape_string.hpp"
//#define NATPMP_LOG
#ifdef NATPMP_LOG
#include <iostream>
#endif
using namespace libtorrent; using namespace libtorrent;
using namespace std::placeholders; using namespace std::placeholders;
natpmp::natpmp(io_service& ios natpmp::natpmp(io_service& ios
, portmap_callback_t const& cb, log_callback_t const& lcb) , portmap_callback_t const& cb, log_callback_t const& lcb)
: m_callback(cb) : m_callback(cb)
#ifndef TORRENT_DISABLE_LOGGING
, m_log_callback(lcb) , m_log_callback(lcb)
#endif
, m_currently_mapping(-1) , m_currently_mapping(-1)
, m_retry_count(0) , m_retry_count(0)
, m_socket(ios) , m_socket(ios)
@ -78,6 +75,7 @@ natpmp::natpmp(io_service& ios
, m_disabled(false) , m_disabled(false)
, m_abort(false) , m_abort(false)
{ {
TORRENT_UNUSED(lcb);
// unfortunately async operations rely on the storage // unfortunately async operations rely on the storage
// for this array not to be reallocated, by passing // for this array not to be reallocated, by passing
// around pointers to its elements. so reserve size for now // around pointers to its elements. so reserve size for now
@ -225,7 +223,8 @@ void natpmp::delete_mapping(int index)
update_mapping(index); update_mapping(index);
} }
int natpmp::add_mapping(protocol_type p, int external_port, int local_port) int natpmp::add_mapping(protocol_type p, int const external_port
, int const local_port)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
@ -243,51 +242,33 @@ int natpmp::add_mapping(protocol_type p, int external_port, int local_port)
i->local_port = local_port; i->local_port = local_port;
i->action = mapping_t::action_add; i->action = mapping_t::action_add;
int mapping_index = i - m_mappings.begin(); int const mapping_index = i - m_mappings.begin();
#ifndef TORRENT_DISABLE_LOGGING
#ifdef NATPMP_LOG natpmp::mapping_t const& m = *i;
time_point now = aux::time_now(); log("add-mapping: proto: %s port: %d local-port: %d action: %s ttl: %" PRId64
for (std::vector<mapping_t>::iterator m = m_mappings.begin() , (m.protocol == none
, end(m_mappings.end()); m != end; ++m) ? "none" : m.protocol == tcp ? "tcp" : "udp")
{ , m.external_port
std::cout << " ADD MAPPING: " << mapping_index << " [ " , m.local_port
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp") , (m.action == mapping_t::action_none
<< " port: " << i->external_port ? "none" : m.action == mapping_t::action_add ? "add" : "delete")
<< " local-port: " << i->local_port , total_seconds(m.expires - aux::time_now()));
<< " action: " << (i->action == mapping_t::action_none ? "none" : i->action == mapping_t::action_add ? "add" : "delete")
<< " ttl: " << total_seconds(i->expires - now)
<< " ]" << std::endl;
}
#endif #endif
update_mapping(mapping_index); update_mapping(mapping_index);
return mapping_index; return mapping_index;
} }
void natpmp::try_next_mapping(int i) void natpmp::try_next_mapping(int const i)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
#ifdef NATPMP_LOG
time_point now = aux::time_now();
for (std::vector<mapping_t>::iterator m = m_mappings.begin()
, end(m_mappings.end()); m != end; ++m)
{
std::cout << " " << (m - m_mappings.begin()) << " [ "
"proto: " << (m->protocol == none ? "none" : m->protocol == tcp ? "tcp" : "udp")
<< " port: " << m->external_port
<< " local-port: " << m->local_port
<< " action: " << (m->action == mapping_t::action_none ? "none" : m->action == mapping_t::action_add ? "add" : "delete")
<< " ttl: " << total_seconds(m->expires - now)
<< " ]" << std::endl;
}
#endif
if (i < int(m_mappings.size()) - 1) if (i < int(m_mappings.size()) - 1)
{ {
update_mapping(i + 1); update_mapping(i + 1);
return; return;
} }
std::vector<mapping_t>::iterator m = std::find_if( std::vector<mapping_t>::iterator const m = std::find_if(
m_mappings.begin(), m_mappings.end() m_mappings.begin(), m_mappings.end()
, [] (mapping_t const& ma) { return ma.action != mapping_t::action_none; }); , [] (mapping_t const& ma) { return ma.action != mapping_t::action_none; });
@ -299,20 +280,13 @@ void natpmp::try_next_mapping(int i)
m_send_timer.cancel(ec); m_send_timer.cancel(ec);
m_socket.close(ec); m_socket.close(ec);
} }
#ifdef NATPMP_LOG
std::cout << " done" << (m_abort?" shutting down":"") << std::endl;
#endif
return; return;
} }
#ifdef NATPMP_LOG
std::cout << " updating " << (m - m_mappings.begin()) << std::endl;
#endif
update_mapping(m - m_mappings.begin()); update_mapping(m - m_mappings.begin());
} }
void natpmp::update_mapping(int i) void natpmp::update_mapping(int const i)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
if (i == int(m_mappings.size())) if (i == int(m_mappings.size()))
@ -323,13 +297,22 @@ void natpmp::update_mapping(int i)
m_send_timer.cancel(ec); m_send_timer.cancel(ec);
m_socket.close(ec); m_socket.close(ec);
} }
#ifdef NATPMP_LOG
std::cout << " done" << (m_abort?" shutting down":"") << std::endl;
#endif
return; return;
} }
natpmp::mapping_t& m = m_mappings[i]; natpmp::mapping_t const& m = m_mappings[i];
#ifndef TORRENT_DISABLE_LOGGING
log("update-mapping: proto: %s port: %d local-port: %d action: %s ttl: %" PRId64
, (m.protocol == none
? "none" : m.protocol == tcp ? "tcp" : "udp")
, m.external_port
, m.local_port
, (m.action == mapping_t::action_none
? "none" : m.action == mapping_t::action_add ? "add" : "delete")
, total_seconds(m.expires - aux::time_now()));
#endif
if (m.action == mapping_t::action_none if (m.action == mapping_t::action_none
|| m.protocol == none) || m.protocol == none)
{ {
@ -346,7 +329,7 @@ void natpmp::update_mapping(int i)
} }
} }
void natpmp::send_map_request(int i) void natpmp::send_map_request(int const i)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
using namespace libtorrent::detail; using namespace libtorrent::detail;
@ -397,7 +380,7 @@ void natpmp::send_map_request(int i)
} }
} }
void natpmp::resend_request(int i, error_code const& e) void natpmp::resend_request(int const i, error_code const& e)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
COMPLETE_ASYNC("natpmp::resend_request"); COMPLETE_ASYNC("natpmp::resend_request");
@ -594,20 +577,6 @@ void natpmp::update_expiration_timer()
if (m_abort) return; if (m_abort) return;
time_point now = aux::time_now() + milliseconds(100); time_point now = aux::time_now() + milliseconds(100);
#ifdef NATPMP_LOG
std::cout << time_now_string() << " update_expiration_timer " << std::endl;
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
, end(m_mappings.end()); i != end; ++i)
{
std::cout << " " << (i - m_mappings.begin()) << " [ "
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
<< " port: " << i->external_port
<< " local-port: " << i->local_port
<< " action: " << (i->action == mapping_t::action_none ? "none" : i->action == mapping_t::action_add ? "add" : "delete")
<< " ttl: " << total_seconds(i->expires - now)
<< " ]" << std::endl;
}
#endif
time_point min_expire = now + seconds(3600); time_point min_expire = now + seconds(3600);
int min_index = -1; int min_index = -1;
for (std::vector<mapping_t>::iterator i = m_mappings.begin() for (std::vector<mapping_t>::iterator i = m_mappings.begin()
@ -637,11 +606,9 @@ void natpmp::update_expiration_timer()
if (min_index >= 0) if (min_index >= 0)
{ {
#ifdef NATPMP_LOG #ifndef TORRENT_DISABLE_LOGGING
std::cout << time_now_string() << " next expiration [" log("next expiration [ idx: %d ttl: %" PRId64 " ]"
" i: " << min_index , min_index, total_seconds(min_expire - aux::time_now()));
<< " ttl: " << total_seconds(min_expire - aux::time_now())
<< " ]" << std::endl;
#endif #endif
error_code ec; error_code ec;
if (m_next_refresh >= 0) m_refresh_timer.cancel(ec); if (m_next_refresh >= 0) m_refresh_timer.cancel(ec);
@ -678,26 +645,12 @@ void natpmp::close_impl()
m_abort = true; m_abort = true;
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
log("closing"); log("closing");
#endif
#ifdef NATPMP_LOG
std::cout << time_now_string() << " close" << std::endl;
time_point now = aux::time_now();
#endif #endif
if (m_disabled) return; if (m_disabled) return;
for (std::vector<mapping_t>::iterator i = m_mappings.begin() for (auto& m : m_mappings)
, end(m_mappings.end()); i != end; ++i)
{ {
#ifdef NATPMP_LOG if (m.protocol == none) continue;
std::cout << " " << (i - m_mappings.begin()) << " [ " m.action = mapping_t::action_delete;
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
<< " port: " << i->external_port
<< " local-port: " << i->local_port
<< " action: " << (i->action == mapping_t::action_none ? "none" : i->action == mapping_t::action_add ? "add" : "delete")
<< " ttl: " << total_seconds(i->expires - now)
<< " ]" << std::endl;
#endif
if (i->protocol == none) continue;
i->action = mapping_t::action_delete;
} }
error_code ec; error_code ec;
m_refresh_timer.cancel(ec); m_refresh_timer.cancel(ec);

View File

@ -43,15 +43,14 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/ref.hpp> #include <boost/ref.hpp>
#include <boost/asio/ip/host_name.hpp> #include <boost/asio/ip/host_name.hpp>
#include <boost/asio/ip/multicast.hpp> #include <boost/asio/ip/multicast.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/aux_/disable_warnings_pop.hpp"
#include <cstdlib> #include <cstdlib>
#include <cstdio> // for snprintf #include <cstdio> // for snprintf
#include <cstdarg>
#include <functional> #include <functional>
using namespace std::placeholders; using namespace std::placeholders;
@ -78,7 +77,9 @@ upnp::upnp(io_service& ios
, bool ignore_nonrouters) , bool ignore_nonrouters)
: m_user_agent(user_agent) : m_user_agent(user_agent)
, m_callback(cb) , m_callback(cb)
#ifndef TORRENT_DISABLE_LOGGING
, m_log_callback(lcb) , m_log_callback(lcb)
#endif
, m_retry_count(0) , m_retry_count(0)
, m_io_service(ios) , m_io_service(ios)
, m_resolver(ios) , m_resolver(ios)
@ -92,6 +93,7 @@ upnp::upnp(io_service& ios
, m_ignore_non_routers(ignore_nonrouters) , m_ignore_non_routers(ignore_nonrouters)
, m_last_if_update(min_time()) , m_last_if_update(min_time())
{ {
TORRENT_UNUSED(lcb);
TORRENT_ASSERT(cb); TORRENT_ASSERT(cb);
} }
@ -111,17 +113,27 @@ upnp::~upnp() {}
void upnp::discover_device() void upnp::discover_device()
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
#ifndef TORRENT_DISABLE_LOGGING
if (m_socket.num_send_sockets() == 0) if (m_socket.num_send_sockets() == 0)
log("No network interfaces to broadcast to"); log("No network interfaces to broadcast to");
#endif
discover_device_impl(); discover_device_impl();
} }
void upnp::log(char const* msg) #ifndef TORRENT_DISABLE_LOGGING
TORRENT_FORMAT(2,3)
void upnp::log(char const* fmt, ...) const
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
va_list v;
va_start(v, fmt);
char msg[500];
std::vsnprintf(msg, sizeof(msg), fmt, v);
va_end(v);
m_log_callback(msg); m_log_callback(msg);
} }
#endif
void upnp::discover_device_impl() void upnp::discover_device_impl()
{ {
@ -143,10 +155,10 @@ void upnp::discover_device_impl()
if (ec) if (ec)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "broadcast failed: %s. Aborting." log("broadcast failed: %s. Aborting."
, convert_from_native(ec.message()).c_str()); , convert_from_native(ec.message()).c_str());
log(msg); #endif
disable(ec); disable(ec);
return; return;
} }
@ -157,7 +169,9 @@ void upnp::discover_device_impl()
m_broadcast_timer.async_wait(std::bind(&upnp::resend_request m_broadcast_timer.async_wait(std::bind(&upnp::resend_request
, self(), _1)); , self(), _1));
#ifndef TORRENT_DISABLE_LOGGING
log("broadcasting search for rootdevice"); log("broadcasting search for rootdevice");
#endif
} }
// returns a reference to a mapping or -1 on failure // returns a reference to a mapping or -1 on failure
@ -167,11 +181,11 @@ int upnp::add_mapping(upnp::protocol_type p, int external_port, int local_port)
// external port 0 means _every_ port // external port 0 means _every_ port
TORRENT_ASSERT(external_port != 0); TORRENT_ASSERT(external_port != 0);
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "adding port map: [ protocol: %s ext_port: %u " log("adding port map: [ protocol: %s ext_port: %u "
"local_port: %u ] %s", (p == tcp?"tcp":"udp"), external_port "local_port: %u ] %s", (p == tcp?"tcp":"udp"), external_port
, local_port, m_disabled ? "DISABLED": ""); , local_port, m_disabled ? "DISABLED": "");
log(msg); #endif
if (m_disabled) return -1; if (m_disabled) return -1;
std::vector<global_mapping_t>::iterator mapping_it = std::find_if( std::vector<global_mapping_t>::iterator mapping_it = std::find_if(
@ -188,12 +202,11 @@ int upnp::add_mapping(upnp::protocol_type p, int external_port, int local_port)
mapping_it->external_port = external_port; mapping_it->external_port = external_port;
mapping_it->local_port = local_port; mapping_it->local_port = local_port;
int mapping_index = mapping_it - m_mappings.begin(); int const mapping_index = mapping_it - m_mappings.begin();
for (std::set<rootdevice>::iterator i = m_devices.begin() for (auto const& dev : m_devices)
, end(m_devices.end()); i != end; ++i)
{ {
rootdevice& d = const_cast<rootdevice&>(*i); rootdevice& d = const_cast<rootdevice&>(dev);
TORRENT_ASSERT(d.magic == 1337); TORRENT_ASSERT(d.magic == 1337);
if (int(d.mapping.size()) <= mapping_index) if (int(d.mapping.size()) <= mapping_index)
@ -219,18 +232,17 @@ void upnp::delete_mapping(int mapping)
global_mapping_t& m = m_mappings[mapping]; global_mapping_t& m = m_mappings[mapping];
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "deleting port map: [ protocol: %s ext_port: %u " log("deleting port map: [ protocol: %s ext_port: %u "
"local_port: %u ]", (m.protocol == tcp?"tcp":"udp"), m.external_port "local_port: %u ]", (m.protocol == tcp?"tcp":"udp"), m.external_port
, m.local_port); , m.local_port);
log(msg); #endif
if (m.protocol == none) return; if (m.protocol == none) return;
for (std::set<rootdevice>::iterator i = m_devices.begin() for (auto const& dev : m_devices)
, end(m_devices.end()); i != end; ++i)
{ {
rootdevice& d = const_cast<rootdevice&>(*i); rootdevice& d = const_cast<rootdevice&>(dev);
TORRENT_ASSERT(d.magic == 1337); TORRENT_ASSERT(d.magic == 1337);
TORRENT_ASSERT(mapping < int(d.mapping.size())); TORRENT_ASSERT(mapping < int(d.mapping.size()));
@ -240,7 +252,10 @@ void upnp::delete_mapping(int mapping)
} }
} }
bool upnp::get_mapping(int index, int& local_port, int& external_port, int& protocol) const bool upnp::get_mapping(int const index
, int& local_port
, int& external_port
, int& protocol) const
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0); TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0);
@ -276,35 +291,39 @@ void upnp::resend_request(error_code const& ec)
return; return;
} }
for (std::set<rootdevice>::iterator i = m_devices.begin() for (auto const& dev : m_devices)
, end(m_devices.end()); i != end; ++i)
{ {
if (i->control_url.empty() && !i->upnp_connection && !i->disabled) if (!dev.control_url.empty()
|| dev.upnp_connection
|| dev.disabled)
{ {
// we don't have a WANIP or WANPPP url for this device, continue;
// ask for it }
rootdevice& d = const_cast<rootdevice&>(*i);
TORRENT_ASSERT(d.magic == 1337); // we don't have a WANIP or WANPPP url for this device,
TORRENT_TRY // ask for it
{ rootdevice& d = const_cast<rootdevice&>(dev);
char msg[500]; TORRENT_ASSERT(d.magic == 1337);
std::snprintf(msg, sizeof(msg), "connecting to: %s", d.url.c_str()); TORRENT_TRY
log(msg); {
if (d.upnp_connection) d.upnp_connection->close(); #ifndef TORRENT_DISABLE_LOGGING
d.upnp_connection.reset(new http_connection(m_io_service log("connecting to: %s", d.url.c_str());
, m_resolver #endif
, std::bind(&upnp::on_upnp_xml, self(), _1, _2 if (d.upnp_connection) d.upnp_connection->close();
, boost::ref(d), _5))); d.upnp_connection.reset(new http_connection(m_io_service
d.upnp_connection->get(d.url, seconds(30), 1); , m_resolver
} , std::bind(&upnp::on_upnp_xml, self(), _1, _2
TORRENT_CATCH (std::exception& exc) , boost::ref(d), _5)));
{ d.upnp_connection->get(d.url, seconds(30), 1);
TORRENT_DECLARE_DUMMY(std::exception, exc); }
char msg[500]; TORRENT_CATCH (std::exception& exc)
std::snprintf(msg, sizeof(msg), "connection failed to: %s %s", d.url.c_str(), exc.what()); {
log(msg); TORRENT_DECLARE_DUMMY(std::exception, exc);
d.disabled = true; TORRENT_UNUSED(exc);
} #ifndef TORRENT_DISABLE_LOGGING
log("connection failed to: %s %s", d.url.c_str(), exc.what());
#endif
d.disabled = true;
} }
} }
} }
@ -347,31 +366,32 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
if (clock_type::now() - seconds(60) > m_last_if_update) if (clock_type::now() - seconds(60) > m_last_if_update)
{ {
m_interfaces = enum_net_interfaces(m_io_service, ec); m_interfaces = enum_net_interfaces(m_io_service, ec);
#ifndef TORRENT_DISABLE_LOGGING
if (ec) if (ec)
{ {
char msg[500]; log("when receiving response from: %s: %s"
std::snprintf(msg, sizeof(msg), "when receiving response from: %s: %s"
, print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str()); , print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str());
log(msg);
} }
#endif
m_last_if_update = aux::time_now(); m_last_if_update = aux::time_now();
} }
if (!ec && !in_local_network(m_interfaces, from.address())) if (!ec && !in_local_network(m_interfaces, from.address()))
{ {
#ifndef TORRENT_DISABLE_LOGGING
char msg[400]; char msg[400];
int num_chars = std::snprintf(msg, sizeof(msg) int num_chars = std::snprintf(msg, sizeof(msg)
, "ignoring response from: %s. IP is not on local network. " , "ignoring response from: %s. IP is not on local network. "
, print_endpoint(from).c_str()); , print_endpoint(from).c_str());
std::vector<ip_interface> net = enum_net_interfaces(m_io_service, ec); for (auto const& iface : m_interfaces)
for (std::vector<ip_interface>::const_iterator i = net.begin()
, end(net.end()); i != end && num_chars < int(sizeof(msg)); ++i)
{ {
num_chars += std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "(%s,%s) " num_chars += std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "(%s,%s) "
, print_address(i->interface_address).c_str(), print_address(i->netmask).c_str()); , print_address(iface.interface_address).c_str(), print_address(iface.netmask).c_str());
if (num_chars >= int(sizeof(msg))) break;
} }
log(msg); log("%s", msg);
#endif
return; return;
} }
@ -386,25 +406,28 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
// list of configured routers // list of configured routers
if (ec) if (ec)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "failed to enumerate routes when " log("failed to enumerate routes when "
"receiving response from: %s: %s" "receiving response from: %s: %s"
, print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str()); , print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str());
log(msg); #endif
} }
else else
{ {
#ifndef TORRENT_DISABLE_LOGGING
char msg[400]; char msg[400];
int num_chars = std::snprintf(msg, sizeof(msg), "SSDP response from: " int num_chars = std::snprintf(msg, sizeof(msg), "SSDP response from: "
"%s: IP is not a router. " "%s: IP is not a router. "
, print_endpoint(from).c_str()); , print_endpoint(from).c_str());
for (std::vector<ip_route>::const_iterator i = routes.begin() for (auto const& route : routes)
, end(routes.end()); i != end && num_chars < int(sizeof(msg)); ++i)
{ {
num_chars += std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "(%s,%s) " num_chars += std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "(%s,%s) "
, print_address(i->gateway).c_str(), print_address(i->netmask).c_str()); , print_address(route.gateway).c_str()
, print_address(route.netmask).c_str());
if (num_chars >= int(sizeof(msg))) break;
} }
log(msg); log("%s", msg);
#endif
non_router = true; non_router = true;
} }
} }
@ -416,48 +439,46 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
, buffer + bytes_transferred), error); , buffer + bytes_transferred), error);
if (error) if (error)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "received malformed HTTP from: %s" log("received malformed HTTP from: %s"
, print_endpoint(from).c_str()); , print_endpoint(from).c_str());
log(msg); #endif
return; return;
} }
if (p.status_code() != 200 && p.method() != "notify") if (p.status_code() != 200 && p.method() != "notify")
{ {
#ifndef TORRENT_DISABLE_LOGGING
if (p.method().empty()) if (p.method().empty())
{ {
char msg[500]; log("HTTP status %u from %s"
std::snprintf(msg, sizeof(msg), "HTTP status %u from %s"
, p.status_code(), print_endpoint(from).c_str()); , p.status_code(), print_endpoint(from).c_str());
log(msg);
} }
else else
{ {
char msg[500]; log("HTTP method %s from %s"
std::snprintf(msg, sizeof(msg), "HTTP method %s from %s"
, p.method().c_str(), print_endpoint(from).c_str()); , p.method().c_str(), print_endpoint(from).c_str());
log(msg);
} }
#endif
return; return;
} }
if (!p.header_finished()) if (!p.header_finished())
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "incomplete HTTP packet from %s" log("incomplete HTTP packet from %s"
, print_endpoint(from).c_str()); , print_endpoint(from).c_str());
log(msg); #endif
return; return;
} }
std::string url = p.header("location"); std::string url = p.header("location");
if (url.empty()) if (url.empty())
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "missing location header from %s" log("missing location header from %s"
, print_endpoint(from).c_str()); , print_endpoint(from).c_str());
log(msg); #endif
return; return;
} }
@ -477,10 +498,10 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
if (ec) if (ec)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "invalid URL %s from %s: %s" log("invalid URL %s from %s: %s"
, d.url.c_str(), print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str()); , d.url.c_str(), print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str());
log(msg); #endif
return; return;
} }
@ -489,35 +510,34 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
if (protocol != "http") if (protocol != "http")
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "unsupported protocol %s from %s" log("unsupported protocol %s from %s"
, protocol.c_str(), print_endpoint(from).c_str()); , protocol.c_str(), print_endpoint(from).c_str());
log(msg); #endif
return; return;
} }
if (d.port == 0) if (d.port == 0)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "URL with port 0 from %s" log("URL with port 0 from %s", print_endpoint(from).c_str());
, print_endpoint(from).c_str()); #endif
log(msg);
return; return;
} }
#ifndef TORRENT_DISABLE_LOGGING
{ {
char msg[500]; log("found rootdevice: %s (%d)"
std::snprintf(msg, sizeof(msg), "found rootdevice: %s (%d)"
, d.url.c_str(), int(m_devices.size())); , d.url.c_str(), int(m_devices.size()));
log(msg);
} }
#endif
if (m_devices.size() >= 50) if (m_devices.size() >= 50)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "too many rootdevices: (%d). Ignoring %s" log("too many rootdevices: (%d). Ignoring %s"
, int(m_devices.size()), d.url.c_str()); , int(m_devices.size()), d.url.c_str());
log(msg); #endif
return; return;
} }
d.non_router = non_router; d.non_router = non_router;
@ -574,12 +594,12 @@ void upnp::try_map_upnp(bool timer)
// regardless of if they are not running at our gateway. // regardless of if they are not running at our gateway.
override_ignore_non_routers = std::none_of(m_devices.begin() override_ignore_non_routers = std::none_of(m_devices.begin()
, m_devices.end(), [](rootdevice const& d) { return d.non_router == false; }); , m_devices.end(), [](rootdevice const& d) { return d.non_router == false; });
#ifndef TORRENT_DISABLE_LOGGING
if (override_ignore_non_routers) if (override_ignore_non_routers)
{ {
char msg[500]; log("overriding ignore non-routers");
std::snprintf(msg, sizeof(msg), "overriding ignore non-routers");
log(msg);
} }
#endif
} }
for (std::set<rootdevice>::iterator i = m_devices.begin() for (std::set<rootdevice>::iterator i = m_devices.begin()
@ -601,10 +621,9 @@ void upnp::try_map_upnp(bool timer)
TORRENT_ASSERT(d.magic == 1337); TORRENT_ASSERT(d.magic == 1337);
TORRENT_TRY TORRENT_TRY
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "connecting to: %s" log("connecting to: %s", d.url.c_str());
, d.url.c_str()); #endif
log(msg);
if (d.upnp_connection) d.upnp_connection->close(); if (d.upnp_connection) d.upnp_connection->close();
d.upnp_connection.reset(new http_connection(m_io_service d.upnp_connection.reset(new http_connection(m_io_service
@ -616,10 +635,10 @@ void upnp::try_map_upnp(bool timer)
TORRENT_CATCH (std::exception& exc) TORRENT_CATCH (std::exception& exc)
{ {
TORRENT_DECLARE_DUMMY(std::exception, exc); TORRENT_DECLARE_DUMMY(std::exception, exc);
char msg[500]; TORRENT_UNUSED(exc);
std::snprintf(msg, sizeof(msg), "connection failed to: %s %s" #ifndef TORRENT_DISABLE_LOGGING
, d.url.c_str(), exc.what()); log("connection failed to: %s %s", d.url.c_str(), exc.what());
log(msg); #endif
d.disabled = true; d.disabled = true;
} }
} }
@ -646,9 +665,9 @@ void upnp::post(upnp::rootdevice const& d, char const* soap
d.upnp_connection->m_sendbuffer = header; d.upnp_connection->m_sendbuffer = header;
char msg[1024]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "sending: %s", header); log("sending: %s", header);
log(msg); #endif
} }
void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i) void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i)
@ -660,9 +679,9 @@ void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i)
if (!d.upnp_connection) if (!d.upnp_connection)
{ {
TORRENT_ASSERT(d.disabled); TORRENT_ASSERT(d.disabled);
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "mapping %u aborted", i); log("mapping %u aborted", i);
log(msg); #endif
return; return;
} }
@ -728,9 +747,9 @@ void upnp::update_map(rootdevice& d, int i)
if (m.action == mapping_t::action_none if (m.action == mapping_t::action_none
|| m.protocol == none) || m.protocol == none)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "mapping %u does not need updating, skipping", i); log("mapping %u does not need updating, skipping", i);
log(msg); #endif
m.action = mapping_t::action_none; m.action = mapping_t::action_none;
next(d, i); next(d, i);
return; return;
@ -739,9 +758,9 @@ void upnp::update_map(rootdevice& d, int i)
TORRENT_ASSERT(!d.upnp_connection); TORRENT_ASSERT(!d.upnp_connection);
TORRENT_ASSERT(d.service_namespace); TORRENT_ASSERT(d.service_namespace);
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "connecting to %s", d.hostname.c_str()); log("connecting to %s", d.hostname.c_str());
log(msg); #endif
if (m.action == mapping_t::action_add) if (m.action == mapping_t::action_add)
{ {
if (m.failcount > 5) if (m.failcount > 5)
@ -786,9 +805,9 @@ void upnp::delete_port_mapping(rootdevice& d, int i)
if (!d.upnp_connection) if (!d.upnp_connection)
{ {
TORRENT_ASSERT(d.disabled); TORRENT_ASSERT(d.disabled);
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "unmapping %u aborted", i); log("unmapping %u aborted", i);
log(msg); #endif
return; return;
} }
@ -860,7 +879,8 @@ TORRENT_EXTRA_EXPORT void find_control_url(int type, char const* string
state.in_service = true; state.in_service = true;
} }
} }
else if (state.control_url.empty() && state.in_service && state.top_tags("service", "controlurl") && strlen(string) > 0) else if (state.control_url.empty() && state.in_service
&& state.top_tags("service", "controlurl") && strlen(string) > 0)
{ {
// default to the first (or only) control url in the router's listing // default to the first (or only) control url in the router's listing
state.control_url.assign(string, str_len); state.control_url.assign(string, str_len);
@ -892,30 +912,30 @@ void upnp::on_upnp_xml(error_code const& e
if (e && e != boost::asio::error::eof) if (e && e != boost::asio::error::eof)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while fetching control url from: %s: %s" log("error while fetching control url from: %s: %s"
, d.url.c_str(), convert_from_native(e.message()).c_str()); , d.url.c_str(), convert_from_native(e.message()).c_str());
log(msg); #endif
d.disabled = true; d.disabled = true;
return; return;
} }
if (!p.header_finished()) if (!p.header_finished())
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while fetching control url from: %s: incomplete HTTP message" log("error while fetching control url from: %s: incomplete HTTP message"
, d.url.c_str()); , d.url.c_str());
log(msg); #endif
d.disabled = true; d.disabled = true;
return; return;
} }
if (p.status_code() != 200) if (p.status_code() != 200)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while fetching control url from: %s: %s" log("error while fetching control url from: %s: %s"
, d.url.c_str(), convert_from_native(p.message()).c_str()); , d.url.c_str(), convert_from_native(p.message()).c_str());
log(msg); #endif
d.disabled = true; d.disabled = true;
return; return;
} }
@ -925,10 +945,10 @@ void upnp::on_upnp_xml(error_code const& e
, std::bind(&find_control_url, _1, _2, _3, boost::ref(s))); , std::bind(&find_control_url, _1, _2, _3, boost::ref(s)));
if (s.control_url.empty()) if (s.control_url.empty())
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "could not find a port mapping interface in response from: %s" log("could not find a port mapping interface in response from: %s"
, d.url.c_str()); , d.url.c_str());
log(msg); #endif
d.disabled = true; d.disabled = true;
return; return;
} }
@ -960,14 +980,14 @@ void upnp::on_upnp_xml(error_code const& e
+ to_string(d.port).data() + s.control_url; + to_string(d.port).data() + s.control_url;
} }
#ifndef TORRENT_DISABLE_LOGGING
{ {
char msg[500]; log("found control URL: %s namespace %s "
std::snprintf(msg, sizeof(msg), "found control URL: %s namespace %s "
"urlbase: %s in response from %s" "urlbase: %s in response from %s"
, d.control_url.c_str(), d.service_namespace , d.control_url.c_str(), d.service_namespace
, s.url_base.c_str(), d.url.c_str()); , s.url_base.c_str(), d.url.c_str());
log(msg);
} }
#endif
std::tie(protocol, auth, d.hostname, d.port, d.path) std::tie(protocol, auth, d.hostname, d.port, d.path)
= parse_url_components(d.control_url, ec); = parse_url_components(d.control_url, ec);
@ -975,10 +995,10 @@ void upnp::on_upnp_xml(error_code const& e
if (ec) if (ec)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "failed to parse URL '%s': %s" log("failed to parse URL '%s': %s"
, d.control_url.c_str(), convert_from_native(ec.message()).c_str()); , d.control_url.c_str(), convert_from_native(ec.message()).c_str());
log(msg); #endif
d.disabled = true; d.disabled = true;
return; return;
} }
@ -1001,9 +1021,9 @@ void upnp::get_ip_address(rootdevice& d)
if (!d.upnp_connection) if (!d.upnp_connection)
{ {
TORRENT_ASSERT(d.disabled); TORRENT_ASSERT(d.disabled);
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "getting external IP address"); log("getting external IP address");
log(msg); #endif
return; return;
} }
@ -1176,27 +1196,29 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e
if (e && e != boost::asio::error::eof) if (e && e != boost::asio::error::eof)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while getting external IP address: %s" log("error while getting external IP address: %s"
, convert_from_native(e.message()).c_str()); , convert_from_native(e.message()).c_str());
log(msg); #endif
if (num_mappings() > 0) update_map(d, 0); if (num_mappings() > 0) update_map(d, 0);
return; return;
} }
if (!p.header_finished()) if (!p.header_finished())
{ {
#ifndef TORRENT_DISABLE_LOGGING
log("error while getting external IP address: incomplete http message"); log("error while getting external IP address: incomplete http message");
#endif
if (num_mappings() > 0) update_map(d, 0); if (num_mappings() > 0) update_map(d, 0);
return; return;
} }
if (p.status_code() != 200) if (p.status_code() != 200)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while getting external IP address: %s" log("error while getting external IP address: %s"
, convert_from_native(p.message()).c_str()); , convert_from_native(p.message()).c_str());
log(msg); #endif
if (num_mappings() > 0) update_map(d, 0); if (num_mappings() > 0) update_map(d, 0);
return; return;
} }
@ -1210,31 +1232,33 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e
// </s:Body> // </s:Body>
// </s:Envelope> // </s:Envelope>
{ #ifndef TORRENT_DISABLE_LOGGING
char msg[500]; log("get external IP address response: %s"
std::snprintf(msg, sizeof(msg), "get external IP address response: %s" , std::string(p.get_body().begin, p.get_body().end).c_str());
, std::string(p.get_body().begin, p.get_body().end).c_str()); #endif
log(msg);
}
ip_address_parse_state s; ip_address_parse_state s;
xml_parse(const_cast<char*>(p.get_body().begin), const_cast<char*>(p.get_body().end) xml_parse(const_cast<char*>(p.get_body().begin), const_cast<char*>(p.get_body().end)
, std::bind(&find_ip_address, _1, _2, boost::ref(s))); , std::bind(&find_ip_address, _1, _2, boost::ref(s)));
#ifndef TORRENT_DISABLE_LOGGING
if (s.error_code != -1) if (s.error_code != -1)
{ {
char msg[500]; log("error while getting external IP address, code: %u", s.error_code);
std::snprintf(msg, sizeof(msg), "error while getting external IP address, code: %u"
, s.error_code);
log(msg);
} }
#endif
if (!s.ip_address.empty()) { if (!s.ip_address.empty())
char msg[500]; {
std::snprintf(msg, sizeof(msg), "got router external IP address %s", s.ip_address.c_str()); #ifndef TORRENT_DISABLE_LOGGING
log(msg); log("got router external IP address %s", s.ip_address.c_str());
#endif
d.external_ip = address::from_string(s.ip_address.c_str(), ignore_error); d.external_ip = address::from_string(s.ip_address.c_str(), ignore_error);
} else { }
else
{
#ifndef TORRENT_DISABLE_LOGGING
log("failed to find external IP address in response"); log("failed to find external IP address in response");
#endif
} }
if (num_mappings() > 0) update_map(d, 0); if (num_mappings() > 0) update_map(d, 0);
@ -1256,10 +1280,10 @@ void upnp::on_upnp_map_response(error_code const& e
if (e && e != boost::asio::error::eof) if (e && e != boost::asio::error::eof)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while adding port map: %s" log("error while adding port map: %s"
, convert_from_native(e.message()).c_str()); , convert_from_native(e.message()).c_str());
log(msg); #endif
d.disabled = true; d.disabled = true;
return; return;
} }
@ -1285,7 +1309,9 @@ void upnp::on_upnp_map_response(error_code const& e
if (!p.header_finished()) if (!p.header_finished())
{ {
#ifndef TORRENT_DISABLE_LOGGING
log("error while adding port map: incomplete http message"); log("error while adding port map: incomplete http message");
#endif
next(d, mapping); next(d, mapping);
return; return;
} }
@ -1298,10 +1324,10 @@ void upnp::on_upnp_map_response(error_code const& e
&& ct.find_first_of("application/soap+xml") == std::string::npos && ct.find_first_of("application/soap+xml") == std::string::npos
) )
{ {
char msg[300]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while adding port map: invalid content-type, \"%s\". Expected text/xml or application/soap+xml" log("error while adding port map: invalid content-type, \"%s\". "
, ct.c_str()); "Expected text/xml or application/soap+xml", ct.c_str());
log(msg); #endif
next(d, mapping); next(d, mapping);
return; return;
} }
@ -1316,10 +1342,10 @@ void upnp::on_upnp_map_response(error_code const& e
if (s.error_code != -1) if (s.error_code != -1)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while adding port map, code: %u" log("error while adding port map, code: %u"
, s.error_code); , s.error_code);
log(msg); #endif
} }
mapping_t& m = d.mapping[mapping]; mapping_t& m = d.mapping[mapping];
@ -1353,10 +1379,10 @@ void upnp::on_upnp_map_response(error_code const& e
return_error(mapping, s.error_code); return_error(mapping, s.error_code);
} }
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "map response: %s" log("map response: %s"
, std::string(p.get_body().begin, p.get_body().end).c_str()); , std::string(p.get_body().begin, p.get_body().end).c_str());
log(msg); #endif
if (s.error_code == -1) if (s.error_code == -1)
{ {
@ -1422,28 +1448,30 @@ void upnp::on_upnp_unmap_response(error_code const& e
if (e && e != boost::asio::error::eof) if (e && e != boost::asio::error::eof)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while deleting portmap: %s" log("error while deleting portmap: %s"
, convert_from_native(e.message()).c_str()); , convert_from_native(e.message()).c_str());
log(msg); #endif
} }
else if (!p.header_finished()) else if (!p.header_finished())
{ {
#ifndef TORRENT_DISABLE_LOGGING
log("error while deleting portmap: incomplete http message"); log("error while deleting portmap: incomplete http message");
#endif
} }
else if (p.status_code() != 200) else if (p.status_code() != 200)
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error while deleting portmap: %s" log("error while deleting portmap: %s"
, convert_from_native(p.message()).c_str()); , convert_from_native(p.message()).c_str());
log(msg); #endif
} }
else else
{ {
char msg[500]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "unmap response: %s" log("unmap response: %s"
, std::string(p.get_body().begin, p.get_body().end).c_str()); , std::string(p.get_body().begin, p.get_body().end).c_str());
log(msg); #endif
} }
error_code_parse_state s; error_code_parse_state s;

View File

@ -34,17 +34,20 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket.hpp" #include "libtorrent/socket.hpp"
#include "libtorrent/socket_io.hpp" #include "libtorrent/socket_io.hpp"
#include <functional> #include <functional>
#include <boost/ref.hpp>
#include <boost/intrusive_ptr.hpp>
#include <iostream> #include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/ref.hpp>
using namespace libtorrent; using namespace libtorrent;
void callback(int mapping, address extip, int port, error_code const& err) void callback(int mapping, address extip, int port, int protocol, error_code const& err)
{ {
std::cerr std::cerr
<< "mapping: " << mapping << "mapping: " << mapping
<< ", port: " << port << ", port: " << port
<< ", protocol: " << protocol
<< ", external-IP: " << print_address(extip) << ", external-IP: " << print_address(extip)
<< ", error: \"" << err.message() << "\"\n"; << ", error: \"" << err.message() << "\"\n";
} }
@ -65,24 +68,24 @@ int main(int argc, char* argv[])
return 1; return 1;
} }
boost::intrusive_ptr<natpmp> natpmp_handler = new natpmp(ios, address_v4() boost::shared_ptr<natpmp> natpmp_handler(new natpmp(
, &callback, &log_callback); ios, &callback, &log_callback));
deadline_timer timer(ios); deadline_timer timer(ios);
int tcp_map = natpmp_handler->add_mapping(natpmp::tcp, atoi(argv[1]), atoi(argv[1])); int const tcp_map = natpmp_handler->add_mapping(natpmp::tcp, atoi(argv[1]), atoi(argv[1]));
int udp_map = natpmp_handler->add_mapping(natpmp::udp, atoi(argv[2]), atoi(argv[2])); natpmp_handler->add_mapping(natpmp::udp, atoi(argv[2]), atoi(argv[2]));
error_code ec; error_code ec;
timer.expires_from_now(seconds(2), ec); timer.expires_from_now(seconds(2), ec);
timer.async_wait(std::bind(&io_service::stop, boost::ref(ios))); timer.async_wait([&] (error_code const&) { ios.io_service::stop(); });
std::cerr << "mapping ports TCP: " << argv[1] std::cerr << "mapping ports TCP: " << argv[1]
<< " UDP: " << argv[2] << std::endl; << " UDP: " << argv[2] << std::endl;
ios.reset(); ios.reset();
ios.run(ec); ios.run(ec);
timer.expires_from_now(seconds(2), ec); timer.expires_from_now(seconds(2), ec);
timer.async_wait(std::bind(&io_service::stop, boost::ref(ios))); timer.async_wait([&] (error_code const&) { ios.io_service::stop(); });
std::cerr << "removing mapping " << tcp_map << std::endl; std::cerr << "removing mapping " << tcp_map << std::endl;
natpmp_handler->delete_mapping(tcp_map); natpmp_handler->delete_mapping(tcp_map);

View File

@ -37,10 +37,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include "setup_transfer.hpp" #include "setup_transfer.hpp"
#include <fstream> #include <fstream>
#include <functional> #include <functional>
#include <boost/ref.hpp>
#include <boost/smart_ptr.hpp>
#include <iostream> #include <iostream>
#include <boost/smart_ptr.hpp>
#include <boost/ref.hpp>
using namespace libtorrent; using namespace libtorrent;
broadcast_socket* sock = 0; broadcast_socket* sock = 0;
@ -159,8 +160,8 @@ void run_upnp_test(char const* root_filename, char const* router_model, char con
std::string user_agent = "test agent"; std::string user_agent = "test agent";
boost::shared_ptr<upnp> upnp_handler = boost::make_shared<upnp>(boost::ref(ios) boost::shared_ptr<upnp> upnp_handler(new upnp(boost::ref(ios)
, user_agent, &callback, &log_callback, false); , user_agent, &callback, &log_callback, false));
upnp_handler->start(); upnp_handler->start();
upnp_handler->discover_device(); upnp_handler->discover_device();