forked from premiere/premiere-libtorrent
improved network interface enumeration and upnp device filtering
This commit is contained in:
parent
b5dedccb70
commit
72edfc2ee7
|
@ -9,16 +9,19 @@ int main()
|
||||||
{
|
{
|
||||||
io_service ios;
|
io_service ios;
|
||||||
asio::error_code ec;
|
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)
|
, end(net.end()); i != end; ++i)
|
||||||
{
|
{
|
||||||
std::cout << *i << " ";
|
std::cout << "address: " << i->interface_address << std::endl
|
||||||
if (is_multicast(*i)) std::cout << "multicast ";
|
<< " mask: " << i->netmask << std::endl
|
||||||
if (is_local(*i)) std::cout << "local ";
|
<< " flags: ";
|
||||||
if (is_loopback(*i)) std::cout << "loopback ";
|
if (is_multicast(i->interface_address)) std::cout << "multicast ";
|
||||||
std::cout << "router: " << router_for_interface(*i, ec);
|
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;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,26 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
namespace libtorrent
|
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);
|
address router_for_interface(address const interface, asio::error_code& ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -235,13 +235,12 @@ private:
|
||||||
|
|
||||||
bool m_disabled;
|
bool m_disabled;
|
||||||
bool m_closing;
|
bool m_closing;
|
||||||
|
bool m_ignore_outside_network;
|
||||||
|
|
||||||
connection_queue& m_cc;
|
connection_queue& m_cc;
|
||||||
|
|
||||||
std::string m_model;
|
std::string m_model;
|
||||||
|
|
||||||
std::vector<address> m_filter;
|
|
||||||
|
|
||||||
#ifdef TORRENT_UPNP_LOGGING
|
#ifdef TORRENT_UPNP_LOGGING
|
||||||
std::ofstream m_log;
|
std::ofstream m_log;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -79,12 +79,12 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
// make a best guess of the interface we're using and its IP
|
// make a best guess of the interface we're using and its IP
|
||||||
asio::error_code ec;
|
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();
|
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)
|
, end(interfaces.end()); i != end; ++i)
|
||||||
{
|
{
|
||||||
address const& a = *i;
|
address const& a = i->interface_address;
|
||||||
if (is_loopback(a)
|
if (is_loopback(a)
|
||||||
|| is_multicast(a)
|
|| is_multicast(a)
|
||||||
|| is_any(a)) continue;
|
|| is_any(a)) continue;
|
||||||
|
@ -111,28 +111,28 @@ namespace libtorrent
|
||||||
using namespace asio::ip::multicast;
|
using namespace asio::ip::multicast;
|
||||||
|
|
||||||
asio::error_code ec;
|
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())
|
if (multicast_endpoint.address().is_v4())
|
||||||
open_multicast_socket(ios, address_v4::any(), loopback);
|
open_multicast_socket(ios, address_v4::any(), loopback);
|
||||||
else
|
else
|
||||||
open_multicast_socket(ios, address_v6::any(), loopback);
|
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)
|
, end(interfaces.end()); i != end; ++i)
|
||||||
{
|
{
|
||||||
// only broadcast to IPv4 addresses that are not local
|
// 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
|
// 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
|
// ignore any loopback interface
|
||||||
if (is_loopback(*i)) continue;
|
if (is_loopback(i->interface_address)) continue;
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
// std::cerr << "broadcast socket [ if: " << i->to_v4().to_string()
|
// std::cerr << "broadcast socket [ if: " << i->to_v4().to_string()
|
||||||
// << " group: " << multicast_endpoint.address() << " ]" << std::endl;
|
// << " group: " << multicast_endpoint.address() << " ]" << std::endl;
|
||||||
#endif
|
#endif
|
||||||
open_unicast_socket(ios, *i);
|
open_unicast_socket(ios, i->interface_address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
104
src/enum_net.cpp
104
src/enum_net.cpp
|
@ -30,7 +30,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined __linux__ || defined __MACH__
|
#if defined __linux__ || defined BSD
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
@ -46,11 +46,58 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
namespace libtorrent
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if defined __linux__ || defined __MACH__ || defined(__FreeBSD__)
|
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;
|
||||||
|
|
||||||
|
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);
|
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
{
|
{
|
||||||
|
@ -64,10 +111,9 @@ namespace libtorrent
|
||||||
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
|
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
|
||||||
{
|
{
|
||||||
close(s);
|
close(s);
|
||||||
ec = asio::error::fault;
|
ec = asio::error_code(errno, asio::error::system_category);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
close(s);
|
|
||||||
|
|
||||||
char *ifr = (char*)ifc.ifc_req;
|
char *ifr = (char*)ifc.ifc_req;
|
||||||
int remaining = ifc.ifc_len;
|
int remaining = ifc.ifc_len;
|
||||||
|
@ -75,22 +121,25 @@ namespace libtorrent
|
||||||
while (remaining)
|
while (remaining)
|
||||||
{
|
{
|
||||||
ifreq const& item = *reinterpret_cast<ifreq*>(ifr);
|
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;
|
ip_interface iface;
|
||||||
bytes_t b;
|
iface.interface_address = sockaddr_to_address(&item.ifr_addr);
|
||||||
memcpy(&b[0], &((sockaddr_in const*)&item.ifr_addr)->sin_addr, b.size());
|
|
||||||
ret.push_back(address_v4(b));
|
ifreq netmask = item;
|
||||||
}
|
if (ioctl(s, SIOCGIFNETMASK, &netmask) < 0)
|
||||||
else if (item.ifr_addr.sa_family == AF_INET6)
|
{
|
||||||
{
|
close(s);
|
||||||
typedef asio::ip::address_v6::bytes_type bytes_t;
|
ec = asio::error_code(errno, asio::error::system_category);
|
||||||
bytes_t b;
|
return ret;
|
||||||
memcpy(&b[0], &((sockaddr_in6 const*)&item.ifr_addr)->sin6_addr, b.size());
|
}
|
||||||
ret.push_back(address_v6(b));
|
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;
|
int current_size = item.ifr_addr.sa_len + IFNAMSIZ;
|
||||||
#elif defined __linux__
|
#elif defined __linux__
|
||||||
int current_size = sizeof(ifreq);
|
int current_size = sizeof(ifreq);
|
||||||
|
@ -98,6 +147,7 @@ namespace libtorrent
|
||||||
ifr += current_size;
|
ifr += current_size;
|
||||||
remaining -= current_size;
|
remaining -= current_size;
|
||||||
}
|
}
|
||||||
|
close(s);
|
||||||
|
|
||||||
#elif defined WIN32
|
#elif defined WIN32
|
||||||
|
|
||||||
|
@ -122,21 +172,27 @@ namespace libtorrent
|
||||||
|
|
||||||
int n = size / sizeof(INTERFACE_INFO);
|
int n = size / sizeof(INTERFACE_INFO);
|
||||||
|
|
||||||
|
ip_interface iface;
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i)
|
||||||
{
|
{
|
||||||
sockaddr_in *sockaddr = (sockaddr_in*)&buffer[i].iiAddress;
|
iface.interface_address = sockaddr_to_address(&buffer[i].iiAddress.Address);
|
||||||
address a(address::from_string(inet_ntoa(sockaddr->sin_addr)));
|
iface.netmask = sockaddr_to_address(&buffer[i].iiNetmask.Address);
|
||||||
if (a == address_v4::any()) continue;
|
if (iface.interface_address == address_v4::any()) continue;
|
||||||
ret.push_back(a);
|
ret.push_back(iface);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#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
|
// make a best guess of the interface we're using and its IP
|
||||||
udp::resolver r(ios);
|
udp::resolver r(ios);
|
||||||
udp::resolver::iterator i = r.resolve(udp::resolver::query(asio::ip::host_name(), "0"));
|
udp::resolver::iterator i = r.resolve(udp::resolver::query(asio::ip::host_name(), "0"));
|
||||||
|
ip_interface iface;
|
||||||
for (;i != udp::resolver_iterator(); ++i)
|
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
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -959,12 +959,14 @@ namespace detail
|
||||||
{
|
{
|
||||||
// if we're listening on any IPv6 address, enumerate them and
|
// if we're listening on any IPv6 address, enumerate them and
|
||||||
// pick the first non-local address
|
// pick the first non-local address
|
||||||
std::vector<address> const& ifs = enum_net_interfaces(m_io_service, ec);
|
std::vector<ip_interface> const& ifs = enum_net_interfaces(m_io_service, ec);
|
||||||
for (std::vector<address>::const_iterator i = ifs.begin()
|
for (std::vector<ip_interface>::const_iterator i = ifs.begin()
|
||||||
, end(ifs.end()); i != end; ++i)
|
, end(ifs.end()); i != end; ++i)
|
||||||
{
|
{
|
||||||
if (i->is_v4() || i->to_v6().is_link_local() || i->to_v6().is_loopback()) continue;
|
if (i->interface_address.is_v4()
|
||||||
m_ipv6_interface = tcp::endpoint(*i, ep.port());
|
|| 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;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
24
src/upnp.cpp
24
src/upnp.cpp
|
@ -75,27 +75,13 @@ upnp::upnp(io_service& ios, connection_queue& cc
|
||||||
, m_refresh_timer(ios)
|
, m_refresh_timer(ios)
|
||||||
, m_disabled(false)
|
, m_disabled(false)
|
||||||
, m_closing(false)
|
, m_closing(false)
|
||||||
|
, m_ignore_outside_network(ignore_nonrouters)
|
||||||
, m_cc(cc)
|
, m_cc(cc)
|
||||||
{
|
{
|
||||||
#ifdef TORRENT_UPNP_LOGGING
|
#ifdef TORRENT_UPNP_LOGGING
|
||||||
m_log.open("upnp.log", std::ios::in | std::ios::out | std::ios::trunc);
|
m_log.open("upnp.log", std::ios::in | std::ios::out | std::ios::trunc);
|
||||||
#endif
|
#endif
|
||||||
m_retry_count = 0;
|
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()
|
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
|
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()
|
asio::error_code ec;
|
||||||
, from.address()) == m_filter.end())
|
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
|
// this upnp device is filtered because it's not in the
|
||||||
// list of configured routers
|
// list of configured routers
|
||||||
#ifdef TORRENT_UPNP_LOGGING
|
#ifdef TORRENT_UPNP_LOGGING
|
||||||
m_log << time_now_string() << " <== (" << from << ") Rootdevice "
|
m_log << time_now_string() << " <== (" << from << ") UPnP device "
|
||||||
"ignored because it's not out router" << std::endl;
|
"ignored because it's not on our network" << std::endl;
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue