From 0a32fffdfd387e1c0fa1e5f43ca961293deaaaf2 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 14 Apr 2007 15:51:36 +0000 Subject: [PATCH] exception fixes for UPnP and NAT-PMP. port fix for lsd --- src/natpmp.cpp | 26 +++++++++--------- src/session_impl.cpp | 3 ++- src/upnp.cpp | 64 ++++++++++++++++++-------------------------- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/src/natpmp.cpp b/src/natpmp.cpp index 532fae6b4..3895a6ce9 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -60,7 +60,7 @@ natpmp::natpmp(io_service& ios, address const& listen_interface, portmap_callbac rebind(listen_interface); } -void natpmp::rebind(address const& listen_interface) +void natpmp::rebind(address const& listen_interface) try { address_v4 local; if (listen_interface.is_v4() && listen_interface != address_v4::from_string("0.0.0.0")) @@ -79,12 +79,8 @@ void natpmp::rebind(address const& listen_interface) if (i == udp::resolver_iterator()) { - #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << "local host name did not resolve to an IPv4 address. " - "disabling NAT-PMP" << std::endl; - #endif - m_disabled = true; - return; + throw std::runtime_error("local host name did not resolve to an " + "IPv4 address. disabling NAT-PMP"); } local = i->endpoint().address().to_v4(); @@ -104,11 +100,7 @@ void natpmp::rebind(address const& listen_interface) { // the local address seems to be an external // internet address. Assume it is not behind a NAT -#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << "not on a NAT. disabling NAT-PMP" << std::endl; -#endif - m_disabled = true; - return; + throw std::runtime_error("local IP is not on a local network"); } m_disabled = false; @@ -134,6 +126,16 @@ void natpmp::rebind(address const& listen_interface) refresh_mapping(i); } } +catch (std::exception& e) +{ + m_disabled = true; + std::stringstream msg; + msg << "NAT-PMP disabled: " << e.what(); +#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) + m_log << msg.str() << std::endl; +#endif + m_callback(0, 0, msg.str()); +} void natpmp::set_mappings(int tcp, int udp) { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 38891ec31..561366b42 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1503,7 +1503,8 @@ namespace libtorrent { namespace detail void session_impl::announce_lsd(sha1_hash const& ih) { mutex_t::scoped_lock l(m_mutex); - m_lsd.announce(ih, m_external_listen_port); + // use internal listen port for local peers + m_lsd.announce(ih, m_listen_interface.port()); } void session_impl::on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih) diff --git a/src/upnp.cpp b/src/upnp.cpp index 859113254..daf74bd3f 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -78,7 +78,7 @@ upnp::~upnp() { } -void upnp::rebind(address const& listen_interface) +void upnp::rebind(address const& listen_interface) try { if (listen_interface.is_v4() && listen_interface != address_v4::from_string("0.0.0.0")) { @@ -96,12 +96,8 @@ void upnp::rebind(address const& listen_interface) if (i == udp::resolver_iterator()) { - #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << "local host name did not resolve to an IPv4 address. " - "disabling UPnP" << std::endl; - #endif - m_disabled = true; - return; + throw std::runtime_error("local host name did not resolve to an " + "IPv4 address. disabling NAT-PMP"); } m_local_ip = i->endpoint().address().to_v4(); @@ -118,45 +114,37 @@ void upnp::rebind(address const& listen_interface) { // the local address seems to be an external // internet address. Assume it is not behind a NAT -#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << "not on a NAT. disabling UPnP" << std::endl; -#endif - m_disabled = true; - return; + throw std::runtime_error("local IP is not on a local network"); } - try - { - // the local interface hasn't changed - if (m_socket.is_open() - && m_socket.local_endpoint().address() == m_local_ip) - return; - - m_socket.close(); - - using namespace asio::ip::multicast; - - m_socket.open(udp::v4()); - m_socket.set_option(datagram_socket::reuse_address(true)); - m_socket.bind(udp::endpoint(m_local_ip, 0)); - - m_socket.set_option(join_group(upnp_multicast_address)); - m_socket.set_option(outbound_interface(m_local_ip)); - m_socket.set_option(hops(255)); - } - catch (std::exception& e) - { -#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << "socket multicast error " << e.what() << ". disabling UPnP" << std::endl; -#endif - m_disabled = true; + // the local interface hasn't changed + if (m_socket.is_open() + && m_socket.local_endpoint().address() == m_local_ip) return; - } + + m_socket.close(); + + using namespace asio::ip::multicast; + + m_socket.open(udp::v4()); + m_socket.set_option(datagram_socket::reuse_address(true)); + m_socket.bind(udp::endpoint(m_local_ip, 0)); + + m_socket.set_option(join_group(upnp_multicast_address)); + m_socket.set_option(outbound_interface(m_local_ip)); + m_socket.set_option(hops(255)); m_disabled = false; m_retry_count = 0; discover_device(); } +catch (std::exception& e) +{ + m_disabled = true; + std::stringstream msg; + msg << "UPnP portmapping disabled: " << e.what(); + m_callback(0, 0, msg.str()); +} void upnp::discover_device() {