diff --git a/.travis.yml b/.travis.yml index 9ff149fad..a891626d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -99,7 +99,7 @@ script: # if we are building with code coverage, report it as soon as possible - cd test - '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 codecov --root .. --gcov-exec gcov-5; fi; diff --git a/appveyor.yml b/appveyor.yml index acf404a59..70b93b137 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -81,7 +81,7 @@ build_script: # 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 - cd %ROOT_DIRECTORY%\bindings\python diff --git a/bindings/python/Jamfile b/bindings/python/Jamfile index dbbb4dea5..2760fa575 100644 --- a/bindings/python/Jamfile +++ b/bindings/python/Jamfile @@ -154,7 +154,6 @@ my-python-extension libtorrent @libtorrent_linking : # default-build all - on : # usage-requirements false ; diff --git a/include/libtorrent/natpmp.hpp b/include/libtorrent/natpmp.hpp index 5a157d610..ac6994bc7 100644 --- a/include/libtorrent/natpmp.hpp +++ b/include/libtorrent/natpmp.hpp @@ -58,7 +58,7 @@ namespace libtorrent typedef boost::function portmap_callback_t; typedef boost::function log_callback_t; -struct natpmp +struct TORRENT_EXTRA_EXPORT natpmp : boost::enable_shared_from_this , single_threaded { @@ -135,7 +135,9 @@ private: }; portmap_callback_t m_callback; +#ifndef TORRENT_DISABLE_LOGGING log_callback_t m_log_callback; +#endif std::vector m_mappings; diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index 0f784f901..923825dba 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -220,7 +220,9 @@ private: void disable(error_code const& ec); 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 delete_port_mapping(rootdevice& d, int i); @@ -364,7 +366,9 @@ private: std::set m_devices; portmap_callback_t m_callback; +#ifndef TORRENT_DISABLE_LOGGING log_callback_t m_log_callback; +#endif // current retry count int m_retry_count; diff --git a/src/natpmp.cpp b/src/natpmp.cpp index 8b91e1aa7..a1b931ad8 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_pop.hpp" #include // for snprintf +#include // for PRId64 et.al. #include #include @@ -56,19 +57,15 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/debug.hpp" #include "libtorrent/aux_/escape_string.hpp" -//#define NATPMP_LOG - -#ifdef NATPMP_LOG -#include -#endif - using namespace libtorrent; using namespace std::placeholders; natpmp::natpmp(io_service& ios , portmap_callback_t const& cb, log_callback_t const& lcb) : m_callback(cb) +#ifndef TORRENT_DISABLE_LOGGING , m_log_callback(lcb) +#endif , m_currently_mapping(-1) , m_retry_count(0) , m_socket(ios) @@ -78,6 +75,7 @@ natpmp::natpmp(io_service& ios , m_disabled(false) , m_abort(false) { + TORRENT_UNUSED(lcb); // unfortunately async operations rely on the storage // for this array not to be reallocated, by passing // around pointers to its elements. so reserve size for now @@ -225,7 +223,8 @@ void natpmp::delete_mapping(int 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()); @@ -243,51 +242,33 @@ int natpmp::add_mapping(protocol_type p, int external_port, int local_port) i->local_port = local_port; i->action = mapping_t::action_add; - int mapping_index = i - m_mappings.begin(); - -#ifdef NATPMP_LOG - time_point now = aux::time_now(); - for (std::vector::iterator m = m_mappings.begin() - , end(m_mappings.end()); m != end; ++m) - { - std::cout << " ADD MAPPING: " << mapping_index << " [ " - "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; - } + int const mapping_index = i - m_mappings.begin(); +#ifndef TORRENT_DISABLE_LOGGING + natpmp::mapping_t const& m = *i; + log("add-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 update_mapping(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()); -#ifdef NATPMP_LOG - time_point now = aux::time_now(); - for (std::vector::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) { update_mapping(i + 1); return; } - std::vector::iterator m = std::find_if( + std::vector::iterator const m = std::find_if( m_mappings.begin(), m_mappings.end() , [] (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_socket.close(ec); } -#ifdef NATPMP_LOG - std::cout << " done" << (m_abort?" shutting down":"") << std::endl; -#endif return; } -#ifdef NATPMP_LOG - std::cout << " updating " << (m - m_mappings.begin()) << std::endl; -#endif - update_mapping(m - m_mappings.begin()); } -void natpmp::update_mapping(int i) +void natpmp::update_mapping(int const i) { TORRENT_ASSERT(is_single_thread()); if (i == int(m_mappings.size())) @@ -323,13 +297,22 @@ void natpmp::update_mapping(int i) m_send_timer.cancel(ec); m_socket.close(ec); } -#ifdef NATPMP_LOG - std::cout << " done" << (m_abort?" shutting down":"") << std::endl; -#endif 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 || 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()); 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()); COMPLETE_ASYNC("natpmp::resend_request"); @@ -594,20 +577,6 @@ void natpmp::update_expiration_timer() if (m_abort) return; time_point now = aux::time_now() + milliseconds(100); -#ifdef NATPMP_LOG - std::cout << time_now_string() << " update_expiration_timer " << std::endl; - for (std::vector::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); int min_index = -1; for (std::vector::iterator i = m_mappings.begin() @@ -637,11 +606,9 @@ void natpmp::update_expiration_timer() if (min_index >= 0) { -#ifdef NATPMP_LOG - std::cout << time_now_string() << " next expiration [" - " i: " << min_index - << " ttl: " << total_seconds(min_expire - aux::time_now()) - << " ]" << std::endl; +#ifndef TORRENT_DISABLE_LOGGING + log("next expiration [ idx: %d ttl: %" PRId64 " ]" + , min_index, total_seconds(min_expire - aux::time_now())); #endif error_code ec; if (m_next_refresh >= 0) m_refresh_timer.cancel(ec); @@ -678,26 +645,12 @@ void natpmp::close_impl() m_abort = true; #ifndef TORRENT_DISABLE_LOGGING log("closing"); -#endif -#ifdef NATPMP_LOG - std::cout << time_now_string() << " close" << std::endl; - time_point now = aux::time_now(); #endif if (m_disabled) return; - for (std::vector::iterator i = m_mappings.begin() - , end(m_mappings.end()); i != end; ++i) + for (auto& m : m_mappings) { -#ifdef NATPMP_LOG - 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 - if (i->protocol == none) continue; - i->action = mapping_t::action_delete; + if (m.protocol == none) continue; + m.action = mapping_t::action_delete; } error_code ec; m_refresh_timer.cancel(ec); diff --git a/src/upnp.cpp b/src/upnp.cpp index 4af96233a..f4444e554 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -43,15 +43,14 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/debug.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" - #include #include #include - #include "libtorrent/aux_/disable_warnings_pop.hpp" #include #include // for snprintf +#include #include using namespace std::placeholders; @@ -78,7 +77,9 @@ upnp::upnp(io_service& ios , bool ignore_nonrouters) : m_user_agent(user_agent) , m_callback(cb) +#ifndef TORRENT_DISABLE_LOGGING , m_log_callback(lcb) +#endif , m_retry_count(0) , m_io_service(ios) , m_resolver(ios) @@ -92,6 +93,7 @@ upnp::upnp(io_service& ios , m_ignore_non_routers(ignore_nonrouters) , m_last_if_update(min_time()) { + TORRENT_UNUSED(lcb); TORRENT_ASSERT(cb); } @@ -111,17 +113,27 @@ upnp::~upnp() {} void upnp::discover_device() { TORRENT_ASSERT(is_single_thread()); +#ifndef TORRENT_DISABLE_LOGGING if (m_socket.num_send_sockets() == 0) log("No network interfaces to broadcast to"); +#endif 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()); + va_list v; + va_start(v, fmt); + char msg[500]; + std::vsnprintf(msg, sizeof(msg), fmt, v); + va_end(v); m_log_callback(msg); } +#endif void upnp::discover_device_impl() { @@ -143,10 +155,10 @@ void upnp::discover_device_impl() if (ec) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "broadcast failed: %s. Aborting." +#ifndef TORRENT_DISABLE_LOGGING + log("broadcast failed: %s. Aborting." , convert_from_native(ec.message()).c_str()); - log(msg); +#endif disable(ec); return; } @@ -157,7 +169,9 @@ void upnp::discover_device_impl() m_broadcast_timer.async_wait(std::bind(&upnp::resend_request , self(), _1)); +#ifndef TORRENT_DISABLE_LOGGING log("broadcasting search for rootdevice"); +#endif } // 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 TORRENT_ASSERT(external_port != 0); - char msg[500]; - std::snprintf(msg, sizeof(msg), "adding port map: [ protocol: %s ext_port: %u " +#ifndef TORRENT_DISABLE_LOGGING + log("adding port map: [ protocol: %s ext_port: %u " "local_port: %u ] %s", (p == tcp?"tcp":"udp"), external_port , local_port, m_disabled ? "DISABLED": ""); - log(msg); +#endif if (m_disabled) return -1; std::vector::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->local_port = local_port; - int mapping_index = mapping_it - m_mappings.begin(); + int const mapping_index = mapping_it - m_mappings.begin(); - for (std::set::iterator i = m_devices.begin() - , end(m_devices.end()); i != end; ++i) + for (auto const& dev : m_devices) { - rootdevice& d = const_cast(*i); + rootdevice& d = const_cast(dev); TORRENT_ASSERT(d.magic == 1337); if (int(d.mapping.size()) <= mapping_index) @@ -219,18 +232,17 @@ void upnp::delete_mapping(int mapping) global_mapping_t& m = m_mappings[mapping]; - char msg[500]; - std::snprintf(msg, sizeof(msg), "deleting port map: [ protocol: %s ext_port: %u " +#ifndef TORRENT_DISABLE_LOGGING + log("deleting port map: [ protocol: %s ext_port: %u " "local_port: %u ]", (m.protocol == tcp?"tcp":"udp"), m.external_port , m.local_port); - log(msg); +#endif if (m.protocol == none) return; - for (std::set::iterator i = m_devices.begin() - , end(m_devices.end()); i != end; ++i) + for (auto const& dev : m_devices) { - rootdevice& d = const_cast(*i); + rootdevice& d = const_cast(dev); TORRENT_ASSERT(d.magic == 1337); 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(index < int(m_mappings.size()) && index >= 0); @@ -276,35 +291,39 @@ void upnp::resend_request(error_code const& ec) return; } - for (std::set::iterator i = m_devices.begin() - , end(m_devices.end()); i != end; ++i) + for (auto const& dev : m_devices) { - 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, - // ask for it - rootdevice& d = const_cast(*i); - TORRENT_ASSERT(d.magic == 1337); - TORRENT_TRY - { - char msg[500]; - std::snprintf(msg, sizeof(msg), "connecting to: %s", d.url.c_str()); - log(msg); - if (d.upnp_connection) d.upnp_connection->close(); - d.upnp_connection.reset(new http_connection(m_io_service - , m_resolver - , std::bind(&upnp::on_upnp_xml, self(), _1, _2 - , boost::ref(d), _5))); - d.upnp_connection->get(d.url, seconds(30), 1); - } - TORRENT_CATCH (std::exception& exc) - { - TORRENT_DECLARE_DUMMY(std::exception, exc); - char msg[500]; - std::snprintf(msg, sizeof(msg), "connection failed to: %s %s", d.url.c_str(), exc.what()); - log(msg); - d.disabled = true; - } + continue; + } + + // we don't have a WANIP or WANPPP url for this device, + // ask for it + rootdevice& d = const_cast(dev); + TORRENT_ASSERT(d.magic == 1337); + TORRENT_TRY + { +#ifndef TORRENT_DISABLE_LOGGING + log("connecting to: %s", d.url.c_str()); +#endif + if (d.upnp_connection) d.upnp_connection->close(); + d.upnp_connection.reset(new http_connection(m_io_service + , m_resolver + , std::bind(&upnp::on_upnp_xml, self(), _1, _2 + , boost::ref(d), _5))); + d.upnp_connection->get(d.url, seconds(30), 1); + } + TORRENT_CATCH (std::exception& exc) + { + TORRENT_DECLARE_DUMMY(std::exception, exc); + 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) { m_interfaces = enum_net_interfaces(m_io_service, ec); +#ifndef TORRENT_DISABLE_LOGGING if (ec) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "when receiving response from: %s: %s" + log("when receiving response from: %s: %s" , print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str()); - log(msg); } +#endif m_last_if_update = aux::time_now(); } if (!ec && !in_local_network(m_interfaces, from.address())) { +#ifndef TORRENT_DISABLE_LOGGING char msg[400]; int num_chars = std::snprintf(msg, sizeof(msg) , "ignoring response from: %s. IP is not on local network. " , print_endpoint(from).c_str()); - std::vector net = enum_net_interfaces(m_io_service, ec); - for (std::vector::const_iterator i = net.begin() - , end(net.end()); i != end && num_chars < int(sizeof(msg)); ++i) + for (auto const& iface : m_interfaces) { 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; } @@ -386,25 +406,28 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer // list of configured routers if (ec) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "failed to enumerate routes when " +#ifndef TORRENT_DISABLE_LOGGING + log("failed to enumerate routes when " "receiving response from: %s: %s" , print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str()); - log(msg); +#endif } else { +#ifndef TORRENT_DISABLE_LOGGING char msg[400]; int num_chars = std::snprintf(msg, sizeof(msg), "SSDP response from: " "%s: IP is not a router. " , print_endpoint(from).c_str()); - for (std::vector::const_iterator i = routes.begin() - , end(routes.end()); i != end && num_chars < int(sizeof(msg)); ++i) + for (auto const& route : routes) { 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; } } @@ -416,48 +439,46 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer , buffer + bytes_transferred), error); if (error) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "received malformed HTTP from: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("received malformed HTTP from: %s" , print_endpoint(from).c_str()); - log(msg); +#endif return; } if (p.status_code() != 200 && p.method() != "notify") { +#ifndef TORRENT_DISABLE_LOGGING if (p.method().empty()) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "HTTP status %u from %s" + log("HTTP status %u from %s" , p.status_code(), print_endpoint(from).c_str()); - log(msg); } else { - char msg[500]; - std::snprintf(msg, sizeof(msg), "HTTP method %s from %s" + log("HTTP method %s from %s" , p.method().c_str(), print_endpoint(from).c_str()); - log(msg); } +#endif return; } if (!p.header_finished()) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "incomplete HTTP packet from %s" +#ifndef TORRENT_DISABLE_LOGGING + log("incomplete HTTP packet from %s" , print_endpoint(from).c_str()); - log(msg); +#endif return; } std::string url = p.header("location"); if (url.empty()) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "missing location header from %s" +#ifndef TORRENT_DISABLE_LOGGING + log("missing location header from %s" , print_endpoint(from).c_str()); - log(msg); +#endif return; } @@ -477,10 +498,10 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer if (ec) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "invalid URL %s from %s: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("invalid URL %s from %s: %s" , d.url.c_str(), print_endpoint(from).c_str(), convert_from_native(ec.message()).c_str()); - log(msg); +#endif return; } @@ -489,35 +510,34 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer if (protocol != "http") { - char msg[500]; - std::snprintf(msg, sizeof(msg), "unsupported protocol %s from %s" +#ifndef TORRENT_DISABLE_LOGGING + log("unsupported protocol %s from %s" , protocol.c_str(), print_endpoint(from).c_str()); - log(msg); +#endif return; } if (d.port == 0) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "URL with port 0 from %s" - , print_endpoint(from).c_str()); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("URL with port 0 from %s", print_endpoint(from).c_str()); +#endif return; } +#ifndef TORRENT_DISABLE_LOGGING { - char msg[500]; - std::snprintf(msg, sizeof(msg), "found rootdevice: %s (%d)" + log("found rootdevice: %s (%d)" , d.url.c_str(), int(m_devices.size())); - log(msg); } +#endif if (m_devices.size() >= 50) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "too many rootdevices: (%d). Ignoring %s" +#ifndef TORRENT_DISABLE_LOGGING + log("too many rootdevices: (%d). Ignoring %s" , int(m_devices.size()), d.url.c_str()); - log(msg); +#endif return; } 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. override_ignore_non_routers = std::none_of(m_devices.begin() , m_devices.end(), [](rootdevice const& d) { return d.non_router == false; }); +#ifndef TORRENT_DISABLE_LOGGING if (override_ignore_non_routers) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "overriding ignore non-routers"); - log(msg); + log("overriding ignore non-routers"); } +#endif } for (std::set::iterator i = m_devices.begin() @@ -601,10 +621,9 @@ void upnp::try_map_upnp(bool timer) TORRENT_ASSERT(d.magic == 1337); TORRENT_TRY { - char msg[500]; - std::snprintf(msg, sizeof(msg), "connecting to: %s" - , d.url.c_str()); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("connecting to: %s", d.url.c_str()); +#endif if (d.upnp_connection) d.upnp_connection->close(); 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_DECLARE_DUMMY(std::exception, exc); - char msg[500]; - std::snprintf(msg, sizeof(msg), "connection failed to: %s %s" - , d.url.c_str(), exc.what()); - log(msg); + TORRENT_UNUSED(exc); +#ifndef TORRENT_DISABLE_LOGGING + log("connection failed to: %s %s", d.url.c_str(), exc.what()); +#endif d.disabled = true; } } @@ -646,9 +665,9 @@ void upnp::post(upnp::rootdevice const& d, char const* soap d.upnp_connection->m_sendbuffer = header; - char msg[1024]; - std::snprintf(msg, sizeof(msg), "sending: %s", header); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("sending: %s", header); +#endif } 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) { TORRENT_ASSERT(d.disabled); - char msg[500]; - std::snprintf(msg, sizeof(msg), "mapping %u aborted", i); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("mapping %u aborted", i); +#endif return; } @@ -728,9 +747,9 @@ void upnp::update_map(rootdevice& d, int i) if (m.action == mapping_t::action_none || m.protocol == none) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "mapping %u does not need updating, skipping", i); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("mapping %u does not need updating, skipping", i); +#endif m.action = mapping_t::action_none; next(d, i); return; @@ -739,9 +758,9 @@ void upnp::update_map(rootdevice& d, int i) TORRENT_ASSERT(!d.upnp_connection); TORRENT_ASSERT(d.service_namespace); - char msg[500]; - std::snprintf(msg, sizeof(msg), "connecting to %s", d.hostname.c_str()); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("connecting to %s", d.hostname.c_str()); +#endif if (m.action == mapping_t::action_add) { if (m.failcount > 5) @@ -786,9 +805,9 @@ void upnp::delete_port_mapping(rootdevice& d, int i) if (!d.upnp_connection) { TORRENT_ASSERT(d.disabled); - char msg[500]; - std::snprintf(msg, sizeof(msg), "unmapping %u aborted", i); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("unmapping %u aborted", i); +#endif return; } @@ -860,7 +879,8 @@ TORRENT_EXTRA_EXPORT void find_control_url(int type, char const* string 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 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) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while fetching control url from: %s: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("error while fetching control url from: %s: %s" , d.url.c_str(), convert_from_native(e.message()).c_str()); - log(msg); +#endif d.disabled = true; return; } if (!p.header_finished()) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while fetching control url from: %s: incomplete HTTP message" +#ifndef TORRENT_DISABLE_LOGGING + log("error while fetching control url from: %s: incomplete HTTP message" , d.url.c_str()); - log(msg); +#endif d.disabled = true; return; } if (p.status_code() != 200) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while fetching control url from: %s: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("error while fetching control url from: %s: %s" , d.url.c_str(), convert_from_native(p.message()).c_str()); - log(msg); +#endif d.disabled = true; 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))); if (s.control_url.empty()) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "could not find a port mapping interface in response from: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("could not find a port mapping interface in response from: %s" , d.url.c_str()); - log(msg); +#endif d.disabled = true; return; } @@ -960,14 +980,14 @@ void upnp::on_upnp_xml(error_code const& e + to_string(d.port).data() + s.control_url; } +#ifndef TORRENT_DISABLE_LOGGING { - char msg[500]; - std::snprintf(msg, sizeof(msg), "found control URL: %s namespace %s " + log("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); } +#endif std::tie(protocol, auth, d.hostname, d.port, d.path) = parse_url_components(d.control_url, ec); @@ -975,10 +995,10 @@ void upnp::on_upnp_xml(error_code const& e if (ec) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "failed to parse URL '%s': %s" +#ifndef TORRENT_DISABLE_LOGGING + log("failed to parse URL '%s': %s" , d.control_url.c_str(), convert_from_native(ec.message()).c_str()); - log(msg); +#endif d.disabled = true; return; } @@ -1001,9 +1021,9 @@ void upnp::get_ip_address(rootdevice& d) if (!d.upnp_connection) { TORRENT_ASSERT(d.disabled); - char msg[500]; - std::snprintf(msg, sizeof(msg), "getting external IP address"); - log(msg); +#ifndef TORRENT_DISABLE_LOGGING + log("getting external IP address"); +#endif return; } @@ -1176,27 +1196,29 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e if (e && e != boost::asio::error::eof) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while getting external IP address: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("error while getting external IP address: %s" , convert_from_native(e.message()).c_str()); - log(msg); +#endif if (num_mappings() > 0) update_map(d, 0); return; } if (!p.header_finished()) { +#ifndef TORRENT_DISABLE_LOGGING log("error while getting external IP address: incomplete http message"); +#endif if (num_mappings() > 0) update_map(d, 0); return; } if (p.status_code() != 200) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while getting external IP address: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("error while getting external IP address: %s" , convert_from_native(p.message()).c_str()); - log(msg); +#endif if (num_mappings() > 0) update_map(d, 0); return; } @@ -1210,31 +1232,33 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e // // - { - char msg[500]; - std::snprintf(msg, sizeof(msg), "get external IP address response: %s" - , std::string(p.get_body().begin, p.get_body().end).c_str()); - log(msg); - } +#ifndef TORRENT_DISABLE_LOGGING + log("get external IP address response: %s" + , std::string(p.get_body().begin, p.get_body().end).c_str()); +#endif ip_address_parse_state s; xml_parse(const_cast(p.get_body().begin), const_cast(p.get_body().end) , std::bind(&find_ip_address, _1, _2, boost::ref(s))); +#ifndef TORRENT_DISABLE_LOGGING if (s.error_code != -1) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while getting external IP address, code: %u" - , s.error_code); - log(msg); + log("error while getting external IP address, code: %u", s.error_code); } +#endif - if (!s.ip_address.empty()) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "got router external IP address %s", s.ip_address.c_str()); - log(msg); + if (!s.ip_address.empty()) + { +#ifndef TORRENT_DISABLE_LOGGING + 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); - } else { + } + else + { +#ifndef TORRENT_DISABLE_LOGGING log("failed to find external IP address in response"); +#endif } 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) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while adding port map: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("error while adding port map: %s" , convert_from_native(e.message()).c_str()); - log(msg); +#endif d.disabled = true; return; } @@ -1285,7 +1309,9 @@ void upnp::on_upnp_map_response(error_code const& e if (!p.header_finished()) { +#ifndef TORRENT_DISABLE_LOGGING log("error while adding port map: incomplete http message"); +#endif next(d, mapping); 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 ) { - char msg[300]; - std::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); +#ifndef TORRENT_DISABLE_LOGGING + log("error while adding port map: invalid content-type, \"%s\". " + "Expected text/xml or application/soap+xml", ct.c_str()); +#endif next(d, mapping); return; } @@ -1316,10 +1342,10 @@ void upnp::on_upnp_map_response(error_code const& e if (s.error_code != -1) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while adding port map, code: %u" +#ifndef TORRENT_DISABLE_LOGGING + log("error while adding port map, code: %u" , s.error_code); - log(msg); +#endif } 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); } - char msg[500]; - std::snprintf(msg, sizeof(msg), "map response: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("map response: %s" , std::string(p.get_body().begin, p.get_body().end).c_str()); - log(msg); +#endif 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) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while deleting portmap: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("error while deleting portmap: %s" , convert_from_native(e.message()).c_str()); - log(msg); +#endif } else if (!p.header_finished()) { +#ifndef TORRENT_DISABLE_LOGGING log("error while deleting portmap: incomplete http message"); +#endif } else if (p.status_code() != 200) { - char msg[500]; - std::snprintf(msg, sizeof(msg), "error while deleting portmap: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("error while deleting portmap: %s" , convert_from_native(p.message()).c_str()); - log(msg); +#endif } else { - char msg[500]; - std::snprintf(msg, sizeof(msg), "unmap response: %s" +#ifndef TORRENT_DISABLE_LOGGING + log("unmap response: %s" , std::string(p.get_body().begin, p.get_body().end).c_str()); - log(msg); +#endif } error_code_parse_state s; diff --git a/test/test_natpmp.cpp b/test/test_natpmp.cpp index fd233dfd8..4b34b8b4c 100644 --- a/test/test_natpmp.cpp +++ b/test/test_natpmp.cpp @@ -34,17 +34,20 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/socket_io.hpp" #include -#include -#include #include +#include +#include +#include + 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 << "mapping: " << mapping << ", port: " << port + << ", protocol: " << protocol << ", external-IP: " << print_address(extip) << ", error: \"" << err.message() << "\"\n"; } @@ -65,24 +68,24 @@ int main(int argc, char* argv[]) return 1; } - boost::intrusive_ptr natpmp_handler = new natpmp(ios, address_v4() - , &callback, &log_callback); + boost::shared_ptr natpmp_handler(new natpmp( + ios, &callback, &log_callback)); deadline_timer timer(ios); - int 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])); + int const tcp_map = natpmp_handler->add_mapping(natpmp::tcp, atoi(argv[1]), atoi(argv[1])); + natpmp_handler->add_mapping(natpmp::udp, atoi(argv[2]), atoi(argv[2])); error_code 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] << " UDP: " << argv[2] << std::endl; ios.reset(); ios.run(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; natpmp_handler->delete_mapping(tcp_map); diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index 903d56bca..d274a1042 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -37,10 +37,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "setup_transfer.hpp" #include #include -#include -#include #include +#include +#include + using namespace libtorrent; 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"; - boost::shared_ptr upnp_handler = boost::make_shared(boost::ref(ios) - , user_agent, &callback, &log_callback, false); + boost::shared_ptr upnp_handler(new upnp(boost::ref(ios) + , user_agent, &callback, &log_callback, false)); upnp_handler->start(); upnp_handler->discover_device();