improved network interface enumeration and upnp device filtering

This commit is contained in:
Arvid Norberg 2008-01-11 06:49:37 +00:00
parent b5dedccb70
commit 72edfc2ee7
7 changed files with 131 additions and 66 deletions

View File

@ -9,16 +9,19 @@ int main()
{
io_service ios;
asio::error_code ec;
std::vector<address> const& net = enum_net_interfaces(ios, ec);
std::vector<ip_interface> const& net = enum_net_interfaces(ios, ec);
for (std::vector<address>::const_iterator i = net.begin()
for (std::vector<ip_interface>::const_iterator i = net.begin()
, end(net.end()); i != end; ++i)
{
std::cout << *i << " ";
if (is_multicast(*i)) std::cout << "multicast ";
if (is_local(*i)) std::cout << "local ";
if (is_loopback(*i)) std::cout << "loopback ";
std::cout << "router: " << router_for_interface(*i, ec);
std::cout << "address: " << i->interface_address << std::endl
<< " mask: " << i->netmask << std::endl
<< " flags: ";
if (is_multicast(i->interface_address)) std::cout << "multicast ";
if (is_local(i->interface_address)) std::cout << "local ";
if (is_loopback(i->interface_address)) std::cout << "loopback ";
std::cout << std::endl;
std::cout << " router: " << router_for_interface(i->interface_address, ec);
std::cout << std::endl;
}

View File

@ -37,7 +37,26 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
std::vector<address> enum_net_interfaces(asio::io_service& ios, asio::error_code& ec);
struct ip_interface
{
address interface_address;
address netmask;
};
// returns a list of the configured IP interfaces
// on the machine
std::vector<ip_interface> enum_net_interfaces(asio::io_service& ios
, asio::error_code& ec);
// returns true if the specified address is on the same
// local network as the specified interface
bool in_subnet(address const& addr, ip_interface const& iface);
// returns true if the specified address is on the same
// local network as us
bool in_local_network(asio::io_service& ios, address const& addr, asio::error_code& ec);
address router_for_interface(address const interface, asio::error_code& ec);
}

View File

@ -235,13 +235,12 @@ private:
bool m_disabled;
bool m_closing;
bool m_ignore_outside_network;
connection_queue& m_cc;
std::string m_model;
std::vector<address> m_filter;
#ifdef TORRENT_UPNP_LOGGING
std::ofstream m_log;
#endif

View File

@ -79,12 +79,12 @@ namespace libtorrent
{
// make a best guess of the interface we're using and its IP
asio::error_code ec;
std::vector<address> const& interfaces = enum_net_interfaces(ios, ec);
std::vector<ip_interface> const& interfaces = enum_net_interfaces(ios, ec);
address ret = address_v4::any();
for (std::vector<address>::const_iterator i = interfaces.begin()
for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
, end(interfaces.end()); i != end; ++i)
{
address const& a = *i;
address const& a = i->interface_address;
if (is_loopback(a)
|| is_multicast(a)
|| is_any(a)) continue;
@ -111,28 +111,28 @@ namespace libtorrent
using namespace asio::ip::multicast;
asio::error_code ec;
std::vector<address> interfaces = enum_net_interfaces(ios, ec);
std::vector<ip_interface> interfaces = enum_net_interfaces(ios, ec);
if (multicast_endpoint.address().is_v4())
open_multicast_socket(ios, address_v4::any(), loopback);
else
open_multicast_socket(ios, address_v6::any(), loopback);
for (std::vector<address>::const_iterator i = interfaces.begin()
for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
, end(interfaces.end()); i != end; ++i)
{
// only broadcast to IPv4 addresses that are not local
if (!is_local(*i)) continue;
if (!is_local(i->interface_address)) continue;
// only multicast on compatible networks
if (i->is_v4() != multicast_endpoint.address().is_v4()) continue;
if (i->interface_address.is_v4() != multicast_endpoint.address().is_v4()) continue;
// ignore any loopback interface
if (is_loopback(*i)) continue;
if (is_loopback(i->interface_address)) continue;
#ifndef NDEBUG
// std::cerr << "broadcast socket [ if: " << i->to_v4().to_string()
// << " group: " << multicast_endpoint.address() << " ]" << std::endl;
#endif
open_unicast_socket(ios, *i);
open_unicast_socket(ios, i->interface_address);
}
}

View File

@ -30,7 +30,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#if defined __linux__ || defined __MACH__
#if defined __linux__ || defined BSD
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
@ -46,11 +46,58 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
std::vector<address> enum_net_interfaces(asio::io_service& ios, asio::error_code& ec)
namespace
{
std::vector<address> ret;
address sockaddr_to_address(sockaddr const* sin)
{
if (sin->sa_family == AF_INET)
{
typedef asio::ip::address_v4::bytes_type bytes_t;
bytes_t b;
memcpy(&b[0], &((sockaddr_in const*)sin)->sin_addr, b.size());
return address_v4(b);
}
else if (sin->sa_family == AF_INET6)
{
typedef asio::ip::address_v6::bytes_type bytes_t;
bytes_t b;
memcpy(&b[0], &((sockaddr_in6 const*)sin)->sin6_addr, b.size());
return address_v6(b);
}
return address();
}
}
bool in_subnet(address const& addr, ip_interface const& iface)
{
if (addr.is_v4() != iface.interface_address.is_v4()) return false;
// since netmasks seems unreliable for IPv6 interfaces
// (MacOS X returns AF_INET addresses as bitmasks) assume
// that any IPv6 address belongs to the subnet of any
// interface with an IPv6 address
if (addr.is_v6()) return true;
#if defined __linux__ || defined __MACH__ || defined(__FreeBSD__)
return (addr.to_v4().to_ulong() & iface.netmask.to_v4().to_ulong())
== (iface.interface_address.to_v4().to_ulong() & iface.netmask.to_v4().to_ulong());
}
bool in_local_network(asio::io_service& ios, address const& addr, asio::error_code& ec)
{
std::vector<ip_interface> const& net = enum_net_interfaces(ios, ec);
if (ec) return false;
for (std::vector<ip_interface>::const_iterator i = net.begin()
, end(net.end()); i != end; ++i)
{
if (in_subnet(addr, *i)) return true;
}
return false;
}
std::vector<ip_interface> enum_net_interfaces(asio::io_service& ios, asio::error_code& ec)
{
std::vector<ip_interface> ret;
#if defined __linux__ || defined BSD
int s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
@ -64,10 +111,9 @@ namespace libtorrent
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
{
close(s);
ec = asio::error::fault;
ec = asio::error_code(errno, asio::error::system_category);
return ret;
}
close(s);
char *ifr = (char*)ifc.ifc_req;
int remaining = ifc.ifc_len;
@ -75,22 +121,25 @@ namespace libtorrent
while (remaining)
{
ifreq const& item = *reinterpret_cast<ifreq*>(ifr);
if (item.ifr_addr.sa_family == AF_INET)
if (item.ifr_addr.sa_family == AF_INET
|| item.ifr_addr.sa_family == AF_INET6)
{
typedef asio::ip::address_v4::bytes_type bytes_t;
bytes_t b;
memcpy(&b[0], &((sockaddr_in const*)&item.ifr_addr)->sin_addr, b.size());
ret.push_back(address_v4(b));
}
else if (item.ifr_addr.sa_family == AF_INET6)
{
typedef asio::ip::address_v6::bytes_type bytes_t;
bytes_t b;
memcpy(&b[0], &((sockaddr_in6 const*)&item.ifr_addr)->sin6_addr, b.size());
ret.push_back(address_v6(b));
ip_interface iface;
iface.interface_address = sockaddr_to_address(&item.ifr_addr);
ifreq netmask = item;
if (ioctl(s, SIOCGIFNETMASK, &netmask) < 0)
{
close(s);
ec = asio::error_code(errno, asio::error::system_category);
return ret;
}
iface.netmask = sockaddr_to_address(&netmask.ifr_addr);
ret.push_back(iface);
}
#if defined __MACH__ || defined(__FreeBSD__)
#if defined BSD
int current_size = item.ifr_addr.sa_len + IFNAMSIZ;
#elif defined __linux__
int current_size = sizeof(ifreq);
@ -98,6 +147,7 @@ namespace libtorrent
ifr += current_size;
remaining -= current_size;
}
close(s);
#elif defined WIN32
@ -122,21 +172,27 @@ namespace libtorrent
int n = size / sizeof(INTERFACE_INFO);
ip_interface iface;
for (int i = 0; i < n; ++i)
{
sockaddr_in *sockaddr = (sockaddr_in*)&buffer[i].iiAddress;
address a(address::from_string(inet_ntoa(sockaddr->sin_addr)));
if (a == address_v4::any()) continue;
ret.push_back(a);
iface.interface_address = sockaddr_to_address(&buffer[i].iiAddress.Address);
iface.netmask = sockaddr_to_address(&buffer[i].iiNetmask.Address);
if (iface.interface_address == address_v4::any()) continue;
ret.push_back(iface);
}
#else
#warning THIS OS IS NOT RECOGNIZED, enum_net_interfaces WILL PROBABLY NOT WORK
// make a best guess of the interface we're using and its IP
udp::resolver r(ios);
udp::resolver::iterator i = r.resolve(udp::resolver::query(asio::ip::host_name(), "0"));
ip_interface iface;
for (;i != udp::resolver_iterator(); ++i)
{
ret.push_back(i->endpoint().address());
iface.interface_address = i->endpoint().address();
if (iface.interface_address.is_v4())
iface.netmask = iface.interface_address.to_v4().netmask();
ret.push_back(iface);
}
#endif
return ret;

View File

@ -959,12 +959,14 @@ namespace detail
{
// if we're listening on any IPv6 address, enumerate them and
// pick the first non-local address
std::vector<address> const& ifs = enum_net_interfaces(m_io_service, ec);
for (std::vector<address>::const_iterator i = ifs.begin()
std::vector<ip_interface> const& ifs = enum_net_interfaces(m_io_service, ec);
for (std::vector<ip_interface>::const_iterator i = ifs.begin()
, end(ifs.end()); i != end; ++i)
{
if (i->is_v4() || i->to_v6().is_link_local() || i->to_v6().is_loopback()) continue;
m_ipv6_interface = tcp::endpoint(*i, ep.port());
if (i->interface_address.is_v4()
|| i->interface_address.to_v6().is_link_local()
|| i->interface_address.to_v6().is_loopback()) continue;
m_ipv6_interface = tcp::endpoint(i->interface_address, ep.port());
break;
}
break;

View File

@ -75,27 +75,13 @@ upnp::upnp(io_service& ios, connection_queue& cc
, m_refresh_timer(ios)
, m_disabled(false)
, m_closing(false)
, m_ignore_outside_network(ignore_nonrouters)
, m_cc(cc)
{
#ifdef TORRENT_UPNP_LOGGING
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<address> const& net = enum_net_interfaces(m_io_service, ec);
m_filter.reserve(net.size());
for (std::vector<address>::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()
@ -264,14 +250,14 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
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())
asio::error_code ec;
if (m_ignore_outside_network && !in_local_network(m_io_service, from.address(), ec))
{
// 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;
m_log << time_now_string() << " <== (" << from << ") UPnP device "
"ignored because it's not on our network" << std::endl;
#endif
return;
}