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-02-18 01:36:17 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2008-04-28 02:20:59 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <vector>
|
2010-11-29 02:33:05 +01:00
|
|
|
#include <stdlib.h> // for wcstombscstombs
|
2008-04-28 02:20:59 +02:00
|
|
|
#include "libtorrent/enum_net.hpp"
|
|
|
|
#include "libtorrent/broadcast_socket.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2012-05-12 18:15:36 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2008-05-20 08:03:46 +02:00
|
|
|
#if BOOST_VERSION < 103500
|
|
|
|
#include <asio/ip/host_name.hpp>
|
|
|
|
#else
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <boost/asio/ip/host_name.hpp>
|
2008-05-20 08:03:46 +02:00
|
|
|
#endif
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2011-04-10 05:45:13 +02:00
|
|
|
#if TORREN_USE_IFCONF
|
2007-09-10 01:52:34 +02:00
|
|
|
#include <sys/ioctl.h>
|
2008-03-04 05:29:03 +01:00
|
|
|
#include <sys/socket.h>
|
2007-09-10 01:52:34 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
2009-01-27 09:24:48 +01:00
|
|
|
#include <string.h>
|
2008-04-28 02:20:59 +02:00
|
|
|
#endif
|
|
|
|
|
2011-04-09 05:49:46 +02:00
|
|
|
#if TORRENT_USE_SYSCTL
|
2009-01-27 09:24:48 +01:00
|
|
|
#include <sys/sysctl.h>
|
2011-04-10 05:45:13 +02:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <boost/scoped_array.hpp>
|
2009-01-27 09:24:48 +01:00
|
|
|
#endif
|
|
|
|
|
2011-04-10 05:45:13 +02:00
|
|
|
#if TORRENT_USE_GETIPFORWARDTABLE || TORRENT_USE_GETADAPTERSADDRESSES
|
2007-10-01 19:21:19 +02:00
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#include <windows.h>
|
|
|
|
#include <iphlpapi.h>
|
2008-04-28 02:20:59 +02:00
|
|
|
#endif
|
|
|
|
|
2011-04-10 05:45:13 +02:00
|
|
|
#if TORRENT_USE_NETLINK
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
2008-04-25 07:51:21 +02:00
|
|
|
#include <asm/types.h>
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2011-02-15 07:29:44 +01:00
|
|
|
#include <sys/types.h>
|
2011-02-15 08:42:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TORRENT_USE_IFADDRS
|
2011-02-15 07:29:44 +01:00
|
|
|
#include <ifaddrs.h>
|
2008-04-28 02:20:59 +02:00
|
|
|
#endif
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
namespace libtorrent { namespace
|
2008-04-25 07:51:21 +02:00
|
|
|
{
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
address inaddr_to_address(in_addr const* ina, int len = 4)
|
2008-04-28 02:20:59 +02:00
|
|
|
{
|
|
|
|
typedef asio::ip::address_v4::bytes_type bytes_t;
|
|
|
|
bytes_t b;
|
2010-11-29 02:33:05 +01:00
|
|
|
std::memset(&b[0], 0, b.size());
|
|
|
|
if (len > 0) std::memcpy(&b[0], ina, (std::min)(len, int(b.size())));
|
2008-04-28 02:20:59 +02:00
|
|
|
return address_v4(b);
|
|
|
|
}
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2009-03-31 09:45:54 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2010-11-29 02:33:05 +01:00
|
|
|
address inaddr6_to_address(in6_addr const* ina6, int len = 16)
|
2008-04-28 02:20:59 +02:00
|
|
|
{
|
|
|
|
typedef asio::ip::address_v6::bytes_type bytes_t;
|
|
|
|
bytes_t b;
|
2010-11-29 02:33:05 +01:00
|
|
|
std::memset(&b[0], 0, b.size());
|
|
|
|
if (len > 0) std::memcpy(&b[0], ina6, (std::min)(len, int(b.size())));
|
2008-04-28 02:20:59 +02:00
|
|
|
return address_v6(b);
|
|
|
|
}
|
2009-03-31 09:45:54 +02:00
|
|
|
#endif
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
int sockaddr_len(sockaddr const* sin)
|
2008-04-25 07:51:21 +02:00
|
|
|
{
|
2011-04-09 05:49:46 +02:00
|
|
|
#if TORRENT_HAS_SALEN
|
2010-11-29 02:33:05 +01:00
|
|
|
return sin->sa_len;
|
2011-04-09 05:49:46 +02:00
|
|
|
#else
|
|
|
|
return sin->sa_family == AF_INET ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
|
2010-11-29 02:33:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
address sockaddr_to_address(sockaddr const* sin, int assume_family = -1)
|
|
|
|
{
|
|
|
|
if (sin->sa_family == AF_INET || assume_family == AF_INET)
|
|
|
|
return inaddr_to_address(&((sockaddr_in const*)sin)->sin_addr
|
|
|
|
, sockaddr_len(sin) - offsetof(sockaddr, sa_data));
|
2009-03-31 09:45:54 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2010-11-29 02:33:05 +01:00
|
|
|
else if (sin->sa_family == AF_INET6 || assume_family == AF_INET6)
|
|
|
|
return inaddr6_to_address(&((sockaddr_in6 const*)sin)->sin6_addr
|
|
|
|
, sockaddr_len(sin) - offsetof(sockaddr, sa_data));
|
2009-03-31 09:45:54 +02:00
|
|
|
#endif
|
2008-04-28 02:20:59 +02:00
|
|
|
return address();
|
|
|
|
}
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2011-04-09 05:49:46 +02:00
|
|
|
#if TORRENT_USE_NETLINK
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
int read_nl_sock(int sock, char *buf, int bufsize, int seq, int pid)
|
|
|
|
{
|
|
|
|
nlmsghdr* nl_hdr;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
int msg_len = 0;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
int read_len = recv(sock, buf, bufsize - msg_len, 0);
|
|
|
|
if (read_len < 0) return -1;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
nl_hdr = (nlmsghdr*)buf;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
if ((NLMSG_OK(nl_hdr, read_len) == 0) || (nl_hdr->nlmsg_type == NLMSG_ERROR))
|
|
|
|
return -1;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
if (nl_hdr->nlmsg_type == NLMSG_DONE) break;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
buf += read_len;
|
|
|
|
msg_len += read_len;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
if ((nl_hdr->nlmsg_flags & NLM_F_MULTI) == 0) break;
|
|
|
|
|
|
|
|
} while((nl_hdr->nlmsg_seq != seq) || (nl_hdr->nlmsg_pid != pid));
|
|
|
|
return msg_len;
|
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
bool parse_route(int s, nlmsghdr* nl_hdr, ip_route* rt_info)
|
2008-04-25 07:51:21 +02:00
|
|
|
{
|
2008-04-28 02:20:59 +02:00
|
|
|
rtmsg* rt_msg = (rtmsg*)NLMSG_DATA(nl_hdr);
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
if((rt_msg->rtm_family != AF_INET && rt_msg->rtm_family != AF_INET6) || (rt_msg->rtm_table != RT_TABLE_MAIN
|
|
|
|
&& rt_msg->rtm_table != RT_TABLE_LOCAL))
|
2008-04-28 03:23:11 +02:00
|
|
|
return false;
|
2008-04-28 02:20:59 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
int if_index = 0;
|
2008-04-28 02:20:59 +02:00
|
|
|
int rt_len = RTM_PAYLOAD(nl_hdr);
|
|
|
|
for (rtattr* rt_attr = (rtattr*)RTM_RTA(rt_msg);
|
|
|
|
RTA_OK(rt_attr,rt_len); rt_attr = RTA_NEXT(rt_attr,rt_len))
|
2008-04-25 07:51:21 +02:00
|
|
|
{
|
2008-04-28 02:20:59 +02:00
|
|
|
switch(rt_attr->rta_type)
|
|
|
|
{
|
|
|
|
case RTA_OIF:
|
2010-11-29 02:33:05 +01:00
|
|
|
if_index = *(int*)RTA_DATA(rt_attr);
|
2008-04-28 02:20:59 +02:00
|
|
|
break;
|
|
|
|
case RTA_GATEWAY:
|
2010-11-29 02:33:05 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (rt_msg->rtm_family == AF_INET6)
|
|
|
|
{
|
|
|
|
rt_info->gateway = inaddr6_to_address((in6_addr*)RTA_DATA(rt_attr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
rt_info->gateway = inaddr_to_address((in_addr*)RTA_DATA(rt_attr));
|
|
|
|
}
|
2008-04-28 02:20:59 +02:00
|
|
|
break;
|
|
|
|
case RTA_DST:
|
2010-11-29 02:33:05 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (rt_msg->rtm_family == AF_INET6)
|
|
|
|
{
|
|
|
|
rt_info->destination = inaddr6_to_address((in6_addr*)RTA_DATA(rt_attr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
rt_info->destination = inaddr_to_address((in_addr*)RTA_DATA(rt_attr));
|
|
|
|
}
|
2008-04-28 02:20:59 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-04-25 07:51:21 +02:00
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
if_indextoname(if_index, rt_info->name);
|
|
|
|
ifreq req;
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
if_indextoname(if_index, req.ifr_name);
|
|
|
|
ioctl(s, SIOCGIFMTU, &req);
|
|
|
|
rt_info->mtu = req.ifr_mtu;
|
|
|
|
// obviously this doesn't work correctly. How do you get the netmask for a route?
|
|
|
|
// if (ioctl(s, SIOCGIFNETMASK, &req) == 0) {
|
|
|
|
// rt_info->netmask = sockaddr_to_address(&req.ifr_addr, req.ifr_addr.sa_family);
|
|
|
|
// }
|
2008-04-28 03:23:11 +02:00
|
|
|
return true;
|
2008-04-25 07:51:21 +02:00
|
|
|
}
|
2007-09-10 01:52:34 +02:00
|
|
|
#endif
|
|
|
|
|
2011-04-09 05:49:46 +02:00
|
|
|
#if TORRENT_USE_SYSCTL
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
bool parse_route(int s, rt_msghdr* rtm, ip_route* rt_info)
|
2007-09-10 01:52:34 +02:00
|
|
|
{
|
2008-04-28 02:20:59 +02:00
|
|
|
sockaddr* rti_info[RTAX_MAX];
|
|
|
|
sockaddr* sa = (sockaddr*)(rtm + 1);
|
|
|
|
for (int i = 0; i < RTAX_MAX; ++i)
|
2008-04-25 07:51:21 +02:00
|
|
|
{
|
2008-04-28 02:20:59 +02:00
|
|
|
if ((rtm->rtm_addrs & (1 << i)) == 0)
|
|
|
|
{
|
|
|
|
rti_info[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rti_info[i] = sa;
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
#define ROUNDUP(a) \
|
|
|
|
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
|
2008-04-25 07:51:21 +02:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
sa = (sockaddr*)((char*)(sa) + ROUNDUP(sa->sa_len));
|
|
|
|
|
|
|
|
#undef ROUNDUP
|
2008-01-11 07:49:37 +01:00
|
|
|
}
|
2008-01-12 10:35:11 +01:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
sa = rti_info[RTAX_GATEWAY];
|
|
|
|
if (sa == 0
|
|
|
|
|| rti_info[RTAX_DST] == 0
|
|
|
|
|| rti_info[RTAX_NETMASK] == 0
|
2009-03-31 09:45:54 +02:00
|
|
|
|| (sa->sa_family != AF_INET
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
&& sa->sa_family != AF_INET6
|
|
|
|
#endif
|
|
|
|
))
|
2008-04-28 02:20:59 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
rt_info->gateway = sockaddr_to_address(rti_info[RTAX_GATEWAY]);
|
|
|
|
rt_info->destination = sockaddr_to_address(rti_info[RTAX_DST]);
|
2010-11-29 02:33:05 +01:00
|
|
|
rt_info->netmask = sockaddr_to_address(rti_info[RTAX_NETMASK]
|
|
|
|
, rt_info->destination.is_v4() ? AF_INET : AF_INET6);
|
2008-04-28 02:20:59 +02:00
|
|
|
if_indextoname(rtm->rtm_index, rt_info->name);
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
ifreq req;
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
if_indextoname(rtm->rtm_index, req.ifr_name);
|
|
|
|
if (ioctl(s, SIOCGIFMTU, &req) < 0) return false;
|
|
|
|
rt_info->mtu = req.ifr_mtu;
|
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-15 08:42:46 +01:00
|
|
|
#if TORRENT_USE_IFADDRS
|
2011-02-15 07:29:44 +01:00
|
|
|
bool iface_from_ifaddrs(ifaddrs *ifa, ip_interface &rv, error_code& ec)
|
|
|
|
{
|
|
|
|
int family = ifa->ifa_addr->sa_family;
|
|
|
|
|
|
|
|
if (family != AF_INET
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
&& family != AF_INET6
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(rv.name, ifa->ifa_name, sizeof(rv.name));
|
|
|
|
rv.name[sizeof(rv.name)-1] = 0;
|
|
|
|
|
|
|
|
// determine address
|
|
|
|
rv.interface_address = sockaddr_to_address(ifa->ifa_addr);
|
|
|
|
// determine netmask
|
|
|
|
if (ifa->ifa_netmask != NULL)
|
|
|
|
{
|
|
|
|
rv.netmask = sockaddr_to_address(ifa->ifa_netmask);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
}} // <anonymous>
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2008-01-11 07:49:37 +01:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// return (a1 & mask) == (a2 & mask)
|
|
|
|
bool match_addr_mask(address const& a1, address const& a2, address const& mask)
|
2008-01-11 07:49:37 +01:00
|
|
|
{
|
2010-11-29 02:33:05 +01:00
|
|
|
// all 3 addresses needs to belong to the same family
|
|
|
|
if (a1.is_v4() != a2.is_v4()) return false;
|
|
|
|
if (a1.is_v4() != mask.is_v4()) return false;
|
|
|
|
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (a1.is_v6())
|
|
|
|
{
|
|
|
|
address_v6::bytes_type b1;
|
|
|
|
address_v6::bytes_type b2;
|
|
|
|
address_v6::bytes_type m;
|
|
|
|
b1 = a1.to_v6().to_bytes();
|
|
|
|
b2 = a2.to_v6().to_bytes();
|
|
|
|
m = mask.to_v6().to_bytes();
|
2011-02-21 06:24:41 +01:00
|
|
|
for (int i = 0; i < int(b1.size()); ++i)
|
2010-11-29 02:33:05 +01:00
|
|
|
b1[i] &= m[i];
|
2011-02-15 07:29:44 +01:00
|
|
|
return memcmp(&b1[0], &b2[0], b1.size()) == 0;
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (a1.to_v4().to_ulong() & mask.to_v4().to_ulong())
|
|
|
|
== (a2.to_v4().to_ulong() & mask.to_v4().to_ulong());
|
2008-01-11 07:49:37 +01:00
|
|
|
}
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
bool in_local_network(io_service& ios, address const& addr, error_code& ec)
|
2008-01-11 07:49:37 +01:00
|
|
|
{
|
2008-08-16 23:26:23 +02:00
|
|
|
std::vector<ip_interface> net = enum_net_interfaces(ios, ec);
|
2008-01-11 07:49:37 +01:00
|
|
|
if (ec) return false;
|
2008-08-16 23:26:23 +02:00
|
|
|
for (std::vector<ip_interface>::iterator i = net.begin()
|
2008-01-11 07:49:37 +01:00
|
|
|
, end(net.end()); i != end; ++i)
|
|
|
|
{
|
2010-11-29 02:33:05 +01:00
|
|
|
if (match_addr_mask(addr, i->interface_address, i->netmask)) return true;
|
2008-01-11 07:49:37 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2011-04-09 05:49:46 +02:00
|
|
|
#if TORRENT_USE_GETIPFORWARDTABLE
|
2010-11-29 02:33:05 +01:00
|
|
|
address build_netmask(int bits, int family)
|
|
|
|
{
|
|
|
|
if (family == AF_INET)
|
|
|
|
{
|
|
|
|
typedef asio::ip::address_v4::bytes_type bytes_t;
|
|
|
|
bytes_t b;
|
|
|
|
std::memset(&b[0], 0xff, b.size());
|
|
|
|
for (int i = sizeof(bytes_t)/8-1; i > 0; --i)
|
|
|
|
{
|
|
|
|
if (bits < 8)
|
|
|
|
{
|
|
|
|
b[i] <<= bits;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
b[i] = 0;
|
|
|
|
bits -= 8;
|
|
|
|
}
|
|
|
|
return address_v4(b);
|
|
|
|
}
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
else if (family == AF_INET6)
|
|
|
|
{
|
|
|
|
typedef asio::ip::address_v6::bytes_type bytes_t;
|
|
|
|
bytes_t b;
|
|
|
|
std::memset(&b[0], 0xff, b.size());
|
|
|
|
for (int i = sizeof(bytes_t)/8-1; i > 0; --i)
|
|
|
|
{
|
|
|
|
if (bits < 8)
|
|
|
|
{
|
|
|
|
b[i] <<= bits;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
b[i] = 0;
|
|
|
|
bits -= 8;
|
|
|
|
}
|
|
|
|
return address_v6(b);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return address();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-01-11 07:49:37 +01:00
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
std::vector<ip_interface> enum_net_interfaces(io_service& ios, error_code& ec)
|
2008-01-11 07:49:37 +01:00
|
|
|
{
|
|
|
|
std::vector<ip_interface> ret;
|
2011-02-15 08:42:46 +01:00
|
|
|
#if TORRENT_USE_IFADDRS
|
2011-02-15 07:29:44 +01:00
|
|
|
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
ec = error_code(errno, asio::error::system_category);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaddrs *ifaddr;
|
|
|
|
if (getifaddrs(&ifaddr) == -1)
|
|
|
|
{
|
|
|
|
ec = error_code(errno, asio::error::system_category);
|
|
|
|
close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next)
|
|
|
|
{
|
|
|
|
if (ifa->ifa_addr == 0) continue;
|
|
|
|
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
|
|
|
|
|
|
|
|
int family = ifa->ifa_addr->sa_family;
|
|
|
|
if (family == AF_INET
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
|| family == AF_INET6
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ip_interface iface;
|
|
|
|
if (iface_from_ifaddrs(ifa, iface, ec))
|
|
|
|
{
|
|
|
|
ifreq req;
|
|
|
|
memset(&req, 0, sizeof(req));
|
2011-02-24 05:25:35 +01:00
|
|
|
// -1 to leave a null terminator
|
|
|
|
strncpy(req.ifr_name, iface.name, IF_NAMESIZE - 1);
|
2011-02-15 07:29:44 +01:00
|
|
|
if (ioctl(s, SIOCGIFMTU, &req) < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
iface.mtu = req.ifr_mtu;
|
|
|
|
ret.push_back(iface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
freeifaddrs(ifaddr);
|
|
|
|
// MacOS X, BSD and solaris
|
2011-04-09 05:49:46 +02:00
|
|
|
#elif TORRENT_USE_IFCONF
|
2007-09-10 01:52:34 +02:00
|
|
|
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
2009-01-27 09:24:48 +01:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2007-09-10 01:52:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ifconf ifc;
|
2012-05-12 18:15:36 +02:00
|
|
|
// make sure the buffer is aligned to hold ifreq structs
|
2012-05-13 06:43:22 +02:00
|
|
|
ifreq buf[40];
|
2007-09-10 01:52:34 +02:00
|
|
|
ifc.ifc_len = sizeof(buf);
|
2012-05-12 18:15:36 +02:00
|
|
|
ifc.ifc_buf = (char*)buf;
|
2007-09-10 01:52:34 +02:00
|
|
|
if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-02-19 19:50:04 +01:00
|
|
|
close(s);
|
2007-09-10 01:52:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ifr = (char*)ifc.ifc_req;
|
|
|
|
int remaining = ifc.ifc_len;
|
|
|
|
|
2012-05-12 18:15:36 +02:00
|
|
|
while (remaining > sizeof(ifreq))
|
2007-09-10 01:52:34 +02:00
|
|
|
{
|
|
|
|
ifreq const& item = *reinterpret_cast<ifreq*>(ifr);
|
2008-01-11 07:49:37 +01:00
|
|
|
|
|
|
|
if (item.ifr_addr.sa_family == AF_INET
|
2009-03-31 09:45:54 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
|| item.ifr_addr.sa_family == AF_INET6
|
|
|
|
#endif
|
|
|
|
)
|
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);
|
2008-04-28 02:20:59 +02:00
|
|
|
strcpy(iface.name, item.ifr_name);
|
2008-01-11 07:49:37 +01:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
ifreq req;
|
|
|
|
memset(&req, 0, sizeof(req));
|
2011-02-24 05:25:35 +01:00
|
|
|
// -1 to leave a null terminator
|
|
|
|
strncpy(req.ifr_name, item.ifr_name, IF_NAMESIZE - 1);
|
2010-11-29 02:33:05 +01:00
|
|
|
if (ioctl(s, SIOCGIFMTU, &req) < 0)
|
|
|
|
{
|
|
|
|
ec = error_code(errno, asio::error::system_category);
|
|
|
|
close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
iface.mtu = req.ifr_mtu;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
2011-02-24 05:25:35 +01:00
|
|
|
strncpy(req.ifr_name, item.ifr_name, IF_NAMESIZE - 1);
|
2010-11-29 02:33:05 +01:00
|
|
|
if (ioctl(s, SIOCGIFNETMASK, &req) < 0)
|
2008-01-11 07:49:37 +01:00
|
|
|
{
|
2009-11-27 19:46:29 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
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
|
2009-11-27 19:46:29 +01:00
|
|
|
#endif
|
2008-01-11 08:56:52 +01:00
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-02-19 19:50:04 +01:00
|
|
|
close(s);
|
2008-01-11 08:56:52 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-29 02:33:05 +01:00
|
|
|
iface.netmask = sockaddr_to_address(&req.ifr_addr, item.ifr_addr.sa_family);
|
2008-01-11 07:49:37 +01:00
|
|
|
}
|
|
|
|
ret.push_back(iface);
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
2012-05-12 18:15:36 +02:00
|
|
|
#ifdef _SIZEOF_ADDR_IFREQ
|
|
|
|
int current_size = _SIZEOF_ADDR_IFREQ(item);
|
|
|
|
#elif defined TORRENT_BSD
|
2007-09-10 01:52:34 +02:00
|
|
|
int current_size = item.ifr_addr.sa_len + IFNAMSIZ;
|
2011-02-15 08:42:46 +01:00
|
|
|
#else
|
2007-09-10 01:52:34 +02:00
|
|
|
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
|
|
|
|
2011-04-09 05:49:46 +02:00
|
|
|
#elif TORRENT_USE_GETADAPTERSADDRESSES
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2010-12-01 06:47:18 +01:00
|
|
|
#if _WIN32_WINNT >= 0x0501
|
2010-11-29 02:33:05 +01:00
|
|
|
// Load Iphlpapi library
|
|
|
|
HMODULE iphlp = LoadLibraryA("Iphlpapi.dll");
|
|
|
|
if (iphlp)
|
|
|
|
{
|
|
|
|
// Get GetAdaptersAddresses() pointer
|
|
|
|
typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG,ULONG,PVOID,PIP_ADAPTER_ADDRESSES,PULONG);
|
|
|
|
GetAdaptersAddresses_t GetAdaptersAddresses = (GetAdaptersAddresses_t)GetProcAddress(
|
|
|
|
iphlp, "GetAdaptersAddresses");
|
|
|
|
|
|
|
|
if (GetAdaptersAddresses)
|
|
|
|
{
|
|
|
|
PIP_ADAPTER_ADDRESSES adapter_addresses = 0;
|
|
|
|
ULONG out_buf_size = 0;
|
|
|
|
if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER
|
|
|
|
| GAA_FLAG_SKIP_ANYCAST, NULL, adapter_addresses, &out_buf_size) != ERROR_BUFFER_OVERFLOW)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_interface>();
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter_addresses = (IP_ADAPTER_ADDRESSES*)malloc(out_buf_size);
|
|
|
|
if (!adapter_addresses)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
ec = asio::error::no_memory;
|
|
|
|
return std::vector<ip_interface>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER
|
|
|
|
| GAA_FLAG_SKIP_ANYCAST, NULL, adapter_addresses, &out_buf_size) == NO_ERROR)
|
|
|
|
{
|
|
|
|
for (PIP_ADAPTER_ADDRESSES adapter = adapter_addresses;
|
|
|
|
adapter != 0; adapter = adapter->Next)
|
|
|
|
{
|
|
|
|
ip_interface r;
|
|
|
|
strncpy(r.name, adapter->AdapterName, sizeof(r.name));
|
|
|
|
r.name[sizeof(r.name)-1] = 0;
|
|
|
|
r.mtu = adapter->Mtu;
|
|
|
|
IP_ADAPTER_UNICAST_ADDRESS* unicast = adapter->FirstUnicastAddress;
|
|
|
|
while (unicast)
|
|
|
|
{
|
|
|
|
r.interface_address = sockaddr_to_address(unicast->Address.lpSockaddr);
|
|
|
|
|
|
|
|
ret.push_back(r);
|
|
|
|
|
|
|
|
unicast = unicast->Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
free(adapter_addresses);
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
}
|
2010-12-01 06:47:18 +01:00
|
|
|
#endif
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s == SOCKET_ERROR)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = 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-05-03 18:05:42 +02:00
|
|
|
ec = 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);
|
|
|
|
if (iface.interface_address == address_v4::any()) continue;
|
2010-11-29 02:33:05 +01:00
|
|
|
iface.netmask = sockaddr_to_address(&buffer[i].iiNetmask.Address
|
|
|
|
, iface.interface_address.is_v4() ? AF_INET : AF_INET6);
|
|
|
|
iface.name[0] = 0;
|
|
|
|
iface.mtu = 1500; // how to get the MTU?
|
2008-01-11 07:49:37 +01:00
|
|
|
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();
|
2010-11-29 02:33:05 +01:00
|
|
|
iface.mtu = 1500;
|
2008-01-11 07:49:37 +01:00
|
|
|
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-05-03 18:05:42 +02:00
|
|
|
address get_default_gateway(io_service& ios, error_code& ec)
|
2007-10-01 19:21:19 +02:00
|
|
|
{
|
2008-04-28 02:20:59 +02:00
|
|
|
std::vector<ip_route> ret = enum_routes(ios, ec);
|
2009-07-01 01:03:54 +02:00
|
|
|
#if defined TORRENT_WINDOWS || defined TORRENT_MINGW
|
2008-05-06 21:13:59 +02:00
|
|
|
std::vector<ip_route>::iterator i = std::find_if(ret.begin(), ret.end()
|
|
|
|
, boost::bind(&is_loopback, boost::bind(&ip_route::destination, _1)));
|
|
|
|
#else
|
2008-04-28 02:20:59 +02:00
|
|
|
std::vector<ip_route>::iterator i = std::find_if(ret.begin(), ret.end()
|
|
|
|
, boost::bind(&ip_route::destination, _1) == address());
|
2008-05-06 21:13:59 +02:00
|
|
|
#endif
|
2008-04-28 02:20:59 +02:00
|
|
|
if (i == ret.end()) return address();
|
|
|
|
return i->gateway;
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
std::vector<ip_route> enum_routes(io_service& ios, error_code& ec)
|
2008-04-28 02:20:59 +02:00
|
|
|
{
|
|
|
|
std::vector<ip_route> ret;
|
2008-01-11 10:38:05 +01:00
|
|
|
|
2011-04-09 05:49:46 +02:00
|
|
|
#if TORRENT_USE_SYSCTL
|
2008-04-28 02:20:59 +02:00
|
|
|
/*
|
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;
|
2008-04-28 02:20:59 +02:00
|
|
|
m.m_rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
|
2008-01-11 10:38:05 +01:00
|
|
|
m.m_rtm.rtm_seq = 0;
|
|
|
|
m.m_rtm.rtm_msglen = len;
|
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
int s = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
|
2008-01-11 10:38:05 +01:00
|
|
|
if (s == -1)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2008-01-11 10:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int n = write(s, &m, len);
|
|
|
|
if (n == -1)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-01-11 10:38:05 +01:00
|
|
|
close(s);
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2008-01-11 10:38:05 +01:00
|
|
|
}
|
|
|
|
else if (n != len)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
close(s);
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2008-01-11 10:38:05 +01:00
|
|
|
}
|
|
|
|
bzero(&m, len);
|
|
|
|
|
|
|
|
n = read(s, &m, len);
|
|
|
|
if (n == -1)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-01-11 10:38:05 +01:00
|
|
|
close(s);
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2008-01-11 10:38:05 +01:00
|
|
|
}
|
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
for (rt_msghdr* ptr = &m.m_rtm; (char*)ptr < ((char*)&m.m_rtm) + n; ptr = (rt_msghdr*)(((char*)ptr) + ptr->rtm_msglen))
|
2008-01-11 10:38:05 +01:00
|
|
|
{
|
2008-04-28 02:20:59 +02:00
|
|
|
std::cout << " rtm_msglen: " << ptr->rtm_msglen << std::endl;
|
|
|
|
std::cout << " rtm_type: " << ptr->rtm_type << std::endl;
|
|
|
|
if (ptr->rtm_errno)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(ptr->rtm_errno, asio::error::system_category);
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
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 (ptr->rtm_addrs & RTA_DST == 0
|
|
|
|
|| ptr->rtm_addrs & RTA_GATEWAY == 0
|
|
|
|
|| ptr->rtm_addrs & RTA_NETMASK == 0)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
if (ptr->rtm_msglen > len - ((char*)ptr - ((char*)&m.m_rtm)))
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
int min_len = sizeof(rt_msghdr) + 2 * sizeof(sockaddr_in);
|
|
|
|
if (m.m_rtm.rtm_msglen < min_len)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
2008-01-11 10:38:05 +01:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
ip_route r;
|
|
|
|
// destination
|
|
|
|
char* p = m.buf;
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)p;
|
|
|
|
r.destination = sockaddr_to_address((sockaddr*)p);
|
2008-01-11 10:38:05 +01:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
// gateway
|
|
|
|
p += sin->sin_len;
|
|
|
|
sin = (sockaddr_in*)p;
|
|
|
|
r.gateway = sockaddr_to_address((sockaddr*)p);
|
|
|
|
|
|
|
|
// netmask
|
|
|
|
p += sin->sin_len;
|
|
|
|
sin = (sockaddr_in*)p;
|
|
|
|
r.netmask = sockaddr_to_address((sockaddr*)p);
|
|
|
|
ret.push_back(r);
|
2008-01-11 10:38:05 +01:00
|
|
|
}
|
2008-04-28 02:20:59 +02:00
|
|
|
close(s);
|
|
|
|
*/
|
|
|
|
int mib[6] = { CTL_NET, PF_ROUTE, 0, AF_UNSPEC, NET_RT_DUMP, 0};
|
2008-01-11 10:38:05 +01:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
size_t needed = 0;
|
|
|
|
if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
2008-01-11 10:38:05 +01:00
|
|
|
|
2008-04-28 02:20:59 +02:00
|
|
|
if (needed <= 0)
|
|
|
|
{
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::scoped_array<char> buf(new (std::nothrow) char[needed]);
|
|
|
|
if (buf.get() == 0)
|
|
|
|
{
|
|
|
|
ec = asio::error::no_memory;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sysctl(mib, 6, buf.get(), &needed, 0, 0) < 0)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
|
|
|
char* end = buf.get() + needed;
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
ec = error_code(errno, asio::error::system_category);
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
2008-04-28 02:20:59 +02:00
|
|
|
rt_msghdr* rtm;
|
|
|
|
for (char* next = buf.get(); next < end; next += rtm->rtm_msglen)
|
|
|
|
{
|
|
|
|
rtm = (rt_msghdr*)next;
|
|
|
|
if (rtm->rtm_version != RTM_VERSION)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ip_route r;
|
2010-11-29 02:33:05 +01:00
|
|
|
if (parse_route(s, rtm, &r)) ret.push_back(r);
|
2008-04-28 02:20:59 +02:00
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
close(s);
|
2008-04-28 02:20:59 +02:00
|
|
|
|
2011-04-09 05:49:46 +02:00
|
|
|
#elif TORRENT_USE_GETIPFORWARDTABLE
|
2010-11-29 02:33:05 +01:00
|
|
|
/*
|
|
|
|
move this to enum_net_interfaces
|
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;
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2007-10-01 19:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2007-10-01 19:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2007-10-01 19:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2008-04-28 02:20:59 +02:00
|
|
|
return std::vector<ip_route>();
|
2007-10-01 19:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-28 02:20:59 +02:00
|
|
|
|
|
|
|
ip_route r;
|
2008-05-06 21:13:59 +02:00
|
|
|
r.destination = address::from_string(adapter->IpAddressList.IpAddress.String, ec);
|
2008-04-28 02:20:59 +02:00
|
|
|
r.gateway = address::from_string(adapter->GatewayList.IpAddress.String, ec);
|
|
|
|
r.netmask = address::from_string(adapter->IpAddressList.IpMask.String, ec);
|
2008-05-06 21:13:59 +02:00
|
|
|
strncpy(r.name, adapter->AdapterName, sizeof(r.name));
|
2008-04-28 02:20:59 +02:00
|
|
|
|
2008-01-11 10:38:05 +01:00
|
|
|
if (ec)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code();
|
2008-01-11 10:38:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-04-28 02:20:59 +02:00
|
|
|
ret.push_back(r);
|
2007-10-01 19:21:19 +02:00
|
|
|
}
|
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2007-10-01 19:21:19 +02:00
|
|
|
// Free memory
|
|
|
|
free(adapter_info);
|
|
|
|
FreeLibrary(iphlp);
|
2010-11-29 02:33:05 +01:00
|
|
|
*/
|
2007-10-01 19:21:19 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// Load Iphlpapi library
|
|
|
|
HMODULE iphlp = LoadLibraryA("Iphlpapi.dll");
|
|
|
|
if (!iphlp)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
2011-04-10 05:45:13 +02:00
|
|
|
typedef DWORD (WINAPI *GetIfEntry_t)(PMIB_IFROW pIfRow);
|
|
|
|
GetIfEntry_t GetIfEntry = (GetIfEntry_t)GetProcAddress(iphlp, "GetIfEntry");
|
|
|
|
if (!GetIfEntry)
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
2010-12-01 06:47:18 +01:00
|
|
|
#if _WIN32_WINNT >= 0x0600
|
2010-11-29 02:33:05 +01:00
|
|
|
typedef DWORD (WINAPI *GetIpForwardTable2_t)(
|
|
|
|
ADDRESS_FAMILY, PMIB_IPFORWARD_TABLE2*);
|
|
|
|
typedef void (WINAPI *FreeMibTable_t)(PVOID Memory);
|
|
|
|
|
|
|
|
GetIpForwardTable2_t GetIpForwardTable2 = (GetIpForwardTable2_t)GetProcAddress(
|
|
|
|
iphlp, "GetIpForwardTable2");
|
|
|
|
FreeMibTable_t FreeMibTable = (FreeMibTable_t)GetProcAddress(
|
|
|
|
iphlp, "FreeMibTable");
|
|
|
|
if (GetIpForwardTable2 && FreeMibTable)
|
|
|
|
{
|
|
|
|
MIB_IPFORWARD_TABLE2* routes = NULL;
|
|
|
|
int res = GetIpForwardTable2(AF_UNSPEC, &routes);
|
|
|
|
if (res == NO_ERROR)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < routes->NumEntries; ++i)
|
|
|
|
{
|
|
|
|
ip_route r;
|
|
|
|
r.gateway = sockaddr_to_address((const sockaddr*)&routes->Table[i].NextHop);
|
|
|
|
r.destination = sockaddr_to_address(
|
|
|
|
(const sockaddr*)&routes->Table[i].DestinationPrefix.Prefix);
|
|
|
|
r.netmask = build_netmask(routes->Table[i].SitePrefixLength
|
|
|
|
, routes->Table[i].DestinationPrefix.Prefix.si_family);
|
|
|
|
MIB_IFROW ifentry;
|
|
|
|
ifentry.dwIndex = routes->Table[i].InterfaceIndex;
|
|
|
|
if (GetIfEntry(&ifentry) == NO_ERROR)
|
|
|
|
{
|
|
|
|
wcstombs(r.name, ifentry.wszName, sizeof(r.name));
|
|
|
|
r.mtu = ifentry.dwMtu;
|
|
|
|
ret.push_back(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (routes) FreeMibTable(routes);
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-01 06:47:18 +01:00
|
|
|
#endif
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
// Get GetIpForwardTable() pointer
|
|
|
|
typedef DWORD (WINAPI *GetIpForwardTable_t)(PMIB_IPFORWARDTABLE pIpForwardTable,PULONG pdwSize,BOOL bOrder);
|
|
|
|
|
|
|
|
GetIpForwardTable_t GetIpForwardTable = (GetIpForwardTable_t)GetProcAddress(
|
|
|
|
iphlp, "GetIpForwardTable");
|
|
|
|
if (!GetIpForwardTable)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
|
|
|
MIB_IPFORWARDTABLE* routes = NULL;
|
|
|
|
ULONG out_buf_size = 0;
|
|
|
|
if (GetIpForwardTable(routes, &out_buf_size, FALSE) != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
ec = asio::error::operation_not_supported;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
|
|
|
routes = (MIB_IPFORWARDTABLE*)malloc(out_buf_size);
|
|
|
|
if (!routes)
|
|
|
|
{
|
|
|
|
FreeLibrary(iphlp);
|
|
|
|
ec = asio::error::no_memory;
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetIpForwardTable(routes, &out_buf_size, FALSE) == NO_ERROR)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < routes->dwNumEntries; ++i)
|
|
|
|
{
|
|
|
|
ip_route r;
|
|
|
|
r.destination = inaddr_to_address((in_addr const*)&routes->table[i].dwForwardDest);
|
|
|
|
r.netmask = inaddr_to_address((in_addr const*)&routes->table[i].dwForwardMask);
|
|
|
|
r.gateway = inaddr_to_address((in_addr const*)&routes->table[i].dwForwardNextHop);
|
|
|
|
MIB_IFROW ifentry;
|
|
|
|
ifentry.dwIndex = routes->table[i].dwForwardIfIndex;
|
|
|
|
if (GetIfEntry(&ifentry) == NO_ERROR)
|
|
|
|
{
|
|
|
|
wcstombs(r.name, ifentry.wszName, sizeof(r.name));
|
|
|
|
r.name[sizeof(r.name)-1] = 0;
|
|
|
|
r.mtu = ifentry.dwMtu;
|
|
|
|
ret.push_back(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
free(routes);
|
|
|
|
FreeLibrary(iphlp);
|
2011-04-09 05:49:46 +02:00
|
|
|
#elif TORRENT_USE_NETLINK
|
2008-04-25 07:51:21 +02:00
|
|
|
enum { BUFSIZE = 8192 };
|
|
|
|
|
|
|
|
int sock = socket(PF_ROUTE, SOCK_DGRAM, NETLINK_ROUTE);
|
|
|
|
if (sock < 0)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-04-28 03:23:11 +02:00
|
|
|
return std::vector<ip_route>();
|
2008-04-25 07:51:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int seq = 0;
|
|
|
|
|
|
|
|
char msg[BUFSIZE];
|
|
|
|
memset(msg, 0, BUFSIZE);
|
|
|
|
nlmsghdr* nl_msg = (nlmsghdr*)msg;
|
|
|
|
|
2008-04-27 06:51:28 +02:00
|
|
|
nl_msg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg));
|
2008-04-25 07:51:21 +02:00
|
|
|
nl_msg->nlmsg_type = RTM_GETROUTE;
|
|
|
|
nl_msg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
|
|
|
|
nl_msg->nlmsg_seq = seq++;
|
|
|
|
nl_msg->nlmsg_pid = getpid();
|
|
|
|
|
|
|
|
if (send(sock, nl_msg, nl_msg->nlmsg_len, 0) < 0)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-04-25 07:51:21 +02:00
|
|
|
close(sock);
|
2008-04-28 03:23:11 +02:00
|
|
|
return std::vector<ip_route>();
|
2008-04-25 07:51:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int len = read_nl_sock(sock, msg, BUFSIZE, seq, getpid());
|
|
|
|
if (len < 0)
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
ec = error_code(errno, asio::error::system_category);
|
2008-04-25 07:51:21 +02:00
|
|
|
close(sock);
|
2008-04-28 03:23:11 +02:00
|
|
|
return std::vector<ip_route>();
|
2008-04-25 07:51:21 +02:00
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
ec = error_code(errno, asio::error::system_category);
|
|
|
|
return std::vector<ip_route>();
|
|
|
|
}
|
2008-04-25 07:51:21 +02:00
|
|
|
for (; NLMSG_OK(nl_msg, len); nl_msg = NLMSG_NEXT(nl_msg, len))
|
|
|
|
{
|
2008-04-28 02:20:59 +02:00
|
|
|
ip_route r;
|
2010-11-29 02:33:05 +01:00
|
|
|
if (parse_route(s, nl_msg, &r)) ret.push_back(r);
|
2008-04-25 07:51:21 +02:00
|
|
|
}
|
2010-11-29 02:33:05 +01:00
|
|
|
close(s);
|
2008-04-25 07:51:21 +02:00
|
|
|
close(sock);
|
|
|
|
|
2007-10-01 19:21:19 +02:00
|
|
|
#endif
|
2008-04-28 02:20:59 +02:00
|
|
|
return ret;
|
2007-10-01 19:21:19 +02:00
|
|
|
}
|
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|