From 7b39f7adf53065b2deff2b726316a8f7a7246627 Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Tue, 8 Jan 2008 05:59:58 +0000 Subject: [PATCH] lt sync 1912 --- .../include/libtorrent/session_settings.hpp | 6 ++++ libtorrent/include/libtorrent/upnp.hpp | 4 ++- libtorrent/src/session_impl.cpp | 3 +- libtorrent/src/upnp.cpp | 30 ++++++++++++++++++- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/libtorrent/include/libtorrent/session_settings.hpp b/libtorrent/include/libtorrent/session_settings.hpp index 7cc7d26da..2817d27d2 100644 --- a/libtorrent/include/libtorrent/session_settings.hpp +++ b/libtorrent/include/libtorrent/session_settings.hpp @@ -117,6 +117,7 @@ namespace libtorrent , use_dht_as_fallback(true) #endif , free_torrent_hashes(true) + , upnp_ignore_nonrouters(true) {} // this is the user agent that will be sent to the tracker @@ -292,6 +293,11 @@ namespace libtorrent // make the get_torrent_info() function to return an incomplete // torrent object that cannot be passed back to add_torrent() bool free_torrent_hashes; + + // when this is true, the upnp port mapper will ignore + // any upnp devices that don't have an address that matches + // our currently configured router. + bool upnp_ignore_nonrouters; }; #ifndef TORRENT_DISABLE_DHT diff --git a/libtorrent/include/libtorrent/upnp.hpp b/libtorrent/include/libtorrent/upnp.hpp index 9c4b2ce54..be8ec15cb 100644 --- a/libtorrent/include/libtorrent/upnp.hpp +++ b/libtorrent/include/libtorrent/upnp.hpp @@ -68,7 +68,7 @@ class upnp : public intrusive_ptr_base public: upnp(io_service& ios, connection_queue& cc , address const& listen_interface, std::string const& user_agent - , portmap_callback_t const& cb); + , portmap_callback_t const& cb, bool ignore_nonrouters); ~upnp(); // maps the ports, if a port is set to 0 @@ -233,6 +233,8 @@ private: connection_queue& m_cc; + std::vector
m_filter; + #ifdef TORRENT_UPNP_LOGGING std::ofstream m_log; #endif diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index 8851f7579..055bf38db 100755 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -2350,7 +2350,8 @@ namespace detail , m_listen_interface.address() , m_settings.user_agent , bind(&session_impl::on_port_mapping - , this, _1, _2, _3)); + , this, _1, _2, _3) + , m_settings.upnp_ignore_nonrouters); m_upnp->discover_device(); m_upnp->set_mappings(m_listen_interface.port(), diff --git a/libtorrent/src/upnp.cpp b/libtorrent/src/upnp.cpp index 70588b5ad..4f6ac7fbf 100644 --- a/libtorrent/src/upnp.cpp +++ b/libtorrent/src/upnp.cpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/http_tracker_connection.hpp" #include "libtorrent/xml_parse.hpp" #include "libtorrent/connection_queue.hpp" +#include "libtorrent/enum_net.hpp" #include #include @@ -61,7 +62,7 @@ namespace libtorrent upnp::upnp(io_service& ios, connection_queue& cc , address const& listen_interface, std::string const& user_agent - , portmap_callback_t const& cb) + , portmap_callback_t const& cb, bool ignore_nonrouters) : m_udp_local_port(0) , m_tcp_local_port(0) , m_user_agent(user_agent) @@ -81,6 +82,21 @@ upnp::upnp(io_service& ios, connection_queue& cc m_log.open("upnp.log", std::ios::in | std::ios::out | std::ios::trunc); #endif m_retry_count = 0; + + if (ignore_nonrouters) + { + asio::error_code ec; + std::vector
const& net = enum_net_interfaces(m_io_service, ec); + m_filter.reserve(net.size()); + for (std::vector
::const_iterator i = net.begin() + , end(net.end()); i != end; ++i) + { + asio::error_code e; + address a = router_for_interface(*i, e); + if (e || is_loopback(a)) continue; + m_filter.push_back(a); + } + } } upnp::~upnp() @@ -265,6 +281,18 @@ try Server:Microsoft-Windows-NT/5.1 UPnP/1.0 UPnP-Device-Host/1.0 */ + if (!m_filter.empty() && std::find(m_filter.begin(), m_filter.end() + , from.address()) == m_filter.end()) + { + // this upnp device is filtered because it's not in the + // list of configured routers +#ifdef TORRENT_UPNP_LOGGING + m_log << time_now_string() << " <== (" << from << ") Rootdevice " + "ignored because it's not out router" << std::endl; +#endif + return; + } + http_parser p; try {