2007-09-10 01:52:34 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-01-11 10:38:05 +01:00
|
|
|
#if defined __linux__ || (defined __APPLE__ && __MACH__) || defined __FreeBSD__ || defined __NetBSD__ \
|
|
|
|
|| defined __OpenBSD__ || defined __bsdi__ || defined __DragonFly__
|
2007-09-10 01:52:34 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
2008-01-11 10:38:05 +01:00
|
|
|
#include <net/route.h>
|
2007-10-01 19:21:19 +02:00
|
|
|
#elif defined WIN32
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#include <windows.h>
|
|
|
|
#include <iphlpapi.h>
|
2007-09-10 01:52:34 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "libtorrent/enum_net.hpp"
|
2008-01-11 11:18:14 +01:00
|
|
|
#include "libtorrent/broadcast_socket.hpp"
|
2008-01-11 08:56:52 +01:00
|
|
|
#include <asio/ip/host_name.hpp>
|
2007-09-10 01:52:34 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2008-01-11 07:49:37 +01:00
|
|
|
namespace
|
2007-09-10 01:52:34 +02:00
|
|
|
{
|
2008-01-11 07:49:37 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2008-01-11 07:49:37 +01:00
|
|
|
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;
|
2008-01-11 08:56:52 +01:00
|
|
|
// covers linux, MacOS X and BSD distributions
|
|
|
|
#if defined __linux__ || (defined __APPLE__ && __MACH__) || defined __FreeBSD__ || defined __NetBSD__ \
|
|
|
|
|| defined __OpenBSD__ || defined __bsdi__ || defined __DragonFly__
|
2007-09-10 01:52:34 +02:00
|
|
|
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
ec = asio::error::fault;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ifconf ifc;
|
|
|
|
char buf[1024];
|
|
|
|
ifc.ifc_len = sizeof(buf);
|
|
|
|
ifc.ifc_buf = buf;
|
|
|
|
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
|
|
|
|
{
|
|
|
|
close(s);
|
2008-01-11 07:49:37 +01:00
|
|
|
ec = asio::error_code(errno, asio::error::system_category);
|
2007-09-10 01:52:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ifr = (char*)ifc.ifc_req;
|
|
|
|
int remaining = ifc.ifc_len;
|
|
|
|
|
|
|
|
while (remaining)
|
|
|
|
{
|
|
|
|
ifreq const& item = *reinterpret_cast<ifreq*>(ifr);
|
2008-01-11 07:49:37 +01:00
|
|
|
|
|
|
|
if (item.ifr_addr.sa_family == AF_INET
|
|
|
|
|| item.ifr_addr.sa_family == AF_INET6)
|
2007-09-20 18:25:07 +02:00
|
|
|
{
|
2008-01-11 07:49:37 +01:00
|
|
|
ip_interface iface;
|
|
|
|
iface.interface_address = sockaddr_to_address(&item.ifr_addr);
|
|
|
|
|
|
|
|
ifreq netmask = item;
|
|
|
|
if (ioctl(s, SIOCGIFNETMASK, &netmask) < 0)
|
|
|
|
{
|
2008-01-11 08:56:52 +01:00
|
|
|
if (iface.interface_address.is_v6())
|
|
|
|
{
|
|
|
|
// this is expected to fail (at least on MacOS X)
|
|
|
|
iface.netmask = address_v6::any();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close(s);
|
|
|
|
ec = asio::error_code(errno, asio::error::system_category);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iface.netmask = sockaddr_to_address(&netmask.ifr_addr);
|
2008-01-11 07:49:37 +01:00
|
|
|
}
|
|
|
|
ret.push_back(iface);
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
2008-01-11 08:56:52 +01:00
|
|
|
#if (defined __APPLE__ && __MACH__) || defined __FreeBSD__ || defined __NetBSD__ \
|
|
|
|
|| defined __OpenBSD__ || defined __bsdi__ || defined __DragonFly__
|
2007-09-10 01:52:34 +02:00
|
|
|
int current_size = item.ifr_addr.sa_len + IFNAMSIZ;
|
|
|
|
#elif defined __linux__
|
|
|
|
int current_size = sizeof(ifreq);
|
|
|
|
#endif
|
|
|
|
ifr += current_size;
|
|
|
|
remaining -= current_size;
|
|
|
|
}
|
2008-01-11 07:49:37 +01:00
|
|
|
close(s);
|
2007-09-10 01:52:34 +02:00
|
|
|
|
|
|
|
#elif defined WIN32
|
|
|
|
|
|
|
|
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s == SOCKET_ERROR)
|
|
|
|
{
|
2008-01-11 08:56:52 +01:00
|
|
|
ec = asio::error_code(WSAGetLastError(), asio::error::system_category);
|
2007-09-10 01:52:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERFACE_INFO buffer[30];
|
|
|
|
DWORD size;
|
|
|
|
|
|
|
|
if (WSAIoctl(s, SIO_GET_INTERFACE_LIST, 0, 0, buffer,
|
|
|
|
sizeof(buffer), &size, 0, 0) != 0)
|
|
|
|
{
|
2008-01-11 08:56:52 +01:00
|
|
|
ec = asio::error_code(WSAGetLastError(), asio::error::system_category);
|
2007-09-10 01:52:34 +02:00
|
|
|
closesocket(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
closesocket(s);
|
|
|
|
|
|
|
|
int n = size / sizeof(INTERFACE_INFO);
|
|
|
|
|
2008-01-11 07:49:37 +01:00
|
|
|
ip_interface iface;
|
2007-09-10 01:52:34 +02:00
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
2008-01-11 07:49:37 +01:00
|
|
|
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);
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2008-01-11 07:49:37 +01:00
|
|
|
#warning THIS OS IS NOT RECOGNIZED, enum_net_interfaces WILL PROBABLY NOT WORK
|
2007-09-10 01:52:34 +02:00
|
|
|
// make a best guess of the interface we're using and its IP
|
|
|
|
udp::resolver r(ios);
|
2008-01-11 08:56:52 +01:00
|
|
|
udp::resolver::iterator i = r.resolve(udp::resolver::query(asio::ip::host_name(ec), "0"), ec);
|
|
|
|
if (ec) return ret;
|
2008-01-11 07:49:37 +01:00
|
|
|
ip_interface iface;
|
2007-09-10 01:52:34 +02:00
|
|
|
for (;i != udp::resolver_iterator(); ++i)
|
|
|
|
{
|
2008-01-11 07:49:37 +01:00
|
|
|
iface.interface_address = i->endpoint().address();
|
|
|
|
if (iface.interface_address.is_v4())
|
2008-01-11 08:56:52 +01:00
|
|
|
iface.netmask = address_v4::netmask(iface.interface_address.to_v4());
|
2008-01-11 07:49:37 +01:00
|
|
|
ret.push_back(iface);
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-11 10:38:05 +01:00
|
|
|
address get_default_gateway(asio::io_service& ios, address const& interface, asio::error_code& ec)
|
2007-10-01 19:21:19 +02:00
|
|
|
{
|
2008-01-11 10:38:05 +01:00
|
|
|
|
2008-01-11 11:18:14 +01:00
|
|
|
#if (defined __APPLE__ && __MACH__) || defined __FreeBSD__ || defined __NetBSD__ \
|
2008-01-11 10:38:05 +01:00
|
|
|
|| defined __OpenBSD__ || defined __bsdi__ || defined __DragonFly__
|
|
|
|
|
2008-01-11 11:18:14 +01:00
|
|
|
bool verify_sockaddr(sockaddr_in* sin)
|
|
|
|
{
|
|
|
|
return (sin->sin_len == sizeof(sockaddr_in)
|
|
|
|
&& sin->sin_family == AF_INET)
|
|
|
|
|| (sin->sin_len == sizeof(sockaddr_in6)
|
|
|
|
&& sin->sin_family == AF_INET6);
|
|
|
|
}
|
|
|
|
|
2008-01-11 10:38:05 +01:00
|
|
|
struct rt_msg
|
|
|
|
{
|
|
|
|
rt_msghdr m_rtm;
|
|
|
|
char buf[512];
|
|
|
|
};
|
|
|
|
|
|
|
|
rt_msg m;
|
|
|
|
int len = sizeof(rt_msg);
|
|
|
|
bzero(&m, len);
|
|
|
|
m.m_rtm.rtm_type = RTM_GET;
|
|
|
|
m.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY;
|
|
|
|
m.m_rtm.rtm_version = RTM_VERSION;
|
|
|
|
m.m_rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
|
|
|
|
m.m_rtm.rtm_seq = 0;
|
|
|
|
m.m_rtm.rtm_msglen = len;
|
|
|
|
|
|
|
|
int s = socket(PF_ROUTE, SOCK_RAW, AF_INET);
|
|
|
|
if (s == -1)
|
|
|
|
{
|
|
|
|
ec = asio::error_code(errno, asio::error::system_category);
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = write(s, &m, len);
|
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
ec = asio::error_code(errno, asio::error::system_category);
|
|
|
|
close(s);
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
else if (n != len)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
close(s);
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
bzero(&m, len);
|
|
|
|
|
|
|
|
n = read(s, &m, len);
|
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
ec = asio::error_code(errno, asio::error::system_category);
|
|
|
|
close(s);
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(m.m_rtm.rtm_seq == 0);
|
|
|
|
TORRENT_ASSERT(m.m_rtm.rtm_pid == getpid());
|
|
|
|
if (m.m_rtm.rtm_errno)
|
|
|
|
{
|
|
|
|
ec = asio::error_code(m.m_rtm.rtm_errno, asio::error::system_category);
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
if (m.m_rtm.rtm_flags & RTF_UP == 0
|
|
|
|
|| m.m_rtm.rtm_flags & RTF_GATEWAY == 0)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
if (m.m_rtm.rtm_addrs & RTA_DST == 0
|
|
|
|
|| m.m_rtm.rtm_addrs & RTA_GATEWAY == 0)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
if (m.m_rtm.rtm_msglen > len)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
int min_len = sizeof(rt_msghdr) + 2 * sizeof(struct sockaddr_in);
|
|
|
|
if (m.m_rtm.rtm_msglen < min_len)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
// default route
|
|
|
|
char* p = m.buf;
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)p;
|
|
|
|
if (!verify_sockaddr(sin))
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
// default gateway
|
|
|
|
p += sin->sin_len;
|
|
|
|
sin = (sockaddr_in*)p;
|
|
|
|
if (!verify_sockaddr(sin))
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sockaddr_to_address((sockaddr*)sin);
|
|
|
|
|
|
|
|
#elif defined WIN32
|
2007-10-01 19:21:19 +02:00
|
|
|
|
|
|
|
// Load Iphlpapi library
|
|
|
|
HMODULE iphlp = LoadLibraryA("Iphlpapi.dll");
|
|
|
|
if (!iphlp)
|
|
|
|
{
|
2008-01-11 10:38:05 +01:00
|
|
|
ec = asio::error::operation_not_supported;
|
2007-10-01 19:21:19 +02:00
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get GetAdaptersInfo() pointer
|
|
|
|
typedef DWORD (WINAPI *GetAdaptersInfo_t)(PIP_ADAPTER_INFO, PULONG);
|
|
|
|
GetAdaptersInfo_t GetAdaptersInfo = (GetAdaptersInfo_t)GetProcAddress(iphlp, "GetAdaptersInfo");
|
|
|
|
if (!GetAdaptersInfo)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
2008-01-11 10:38:05 +01:00
|
|
|
ec = asio::error::operation_not_supported;
|
2007-10-01 19:21:19 +02:00
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
PIP_ADAPTER_INFO adapter_info = 0;
|
|
|
|
ULONG out_buf_size = 0;
|
|
|
|
if (GetAdaptersInfo(adapter_info, &out_buf_size) != ERROR_BUFFER_OVERFLOW)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
2008-01-11 10:38:05 +01:00
|
|
|
ec = asio::error::operation_not_supported;
|
2007-10-01 19:21:19 +02:00
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter_info = (IP_ADAPTER_INFO*)malloc(out_buf_size);
|
|
|
|
if (!adapter_info)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
2008-01-11 10:38:05 +01:00
|
|
|
ec = asio::error::no_memory;
|
2007-10-01 19:21:19 +02:00
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
|
|
|
|
address ret;
|
|
|
|
if (GetAdaptersInfo(adapter_info, &out_buf_size) == NO_ERROR)
|
|
|
|
{
|
2008-01-11 10:38:05 +01:00
|
|
|
|
|
|
|
for (PIP_ADAPTER_INFO adapter = adapter_info;
|
|
|
|
adapter != 0; adapter = adapter->Next)
|
2007-10-01 19:21:19 +02:00
|
|
|
{
|
2008-01-11 10:38:05 +01:00
|
|
|
address iface = address::from_string(adapter->IpAddressList.IpAddress.String, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
ec = asio::error_code();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_loopback(iface) || is_any(iface)) continue;
|
|
|
|
if (interface == address() || interface == iface)
|
2007-10-01 19:21:19 +02:00
|
|
|
{
|
|
|
|
ret = address::from_string(adapter->GatewayList.IpAddress.String, ec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
free(adapter_info);
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2008-01-11 11:18:14 +01:00
|
|
|
//#elif defined __linux__
|
|
|
|
// No linux implementation yet
|
2007-10-01 19:21:19 +02:00
|
|
|
#else
|
|
|
|
if (!interface.is_v4())
|
|
|
|
{
|
2008-01-11 10:38:05 +01:00
|
|
|
ec = asio::error::operation_not_supported;
|
2007-10-01 19:21:19 +02:00
|
|
|
return address_v4::any();
|
|
|
|
}
|
|
|
|
return address_v4((interface.to_v4().to_ulong() & 0xffffff00) | 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|