exception fixes for UPnP and NAT-PMP. port fix for lsd

This commit is contained in:
Arvid Norberg 2007-04-14 15:51:36 +00:00
parent a22ad5ad33
commit 0a32fffdfd
3 changed files with 42 additions and 51 deletions

View File

@ -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)
{

View File

@ -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)

View File

@ -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()
{