From d7917c4382a6c32f21df5109fc2497c4b0567c37 Mon Sep 17 00:00:00 2001 From: Andrei Kurushin Date: Wed, 8 Feb 2017 15:52:23 +0300 Subject: [PATCH] consolidate address family checking (#1678) consolidate address family checking, fix no IPV6 windows and netlink builds --- src/enum_net.cpp | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/enum_net.cpp b/src/enum_net.cpp index 705909901..1663fb292 100644 --- a/src/enum_net.cpp +++ b/src/enum_net.cpp @@ -149,6 +149,15 @@ namespace libtorrent { namespace return address(); } + bool valid_addr_family(int family) + { + return (family == AF_INET +#if TORRENT_USE_IPV6 + || family == AF_INET6 +#endif + ); + } + #if TORRENT_USE_NETLINK int read_nl_sock(int sock, char *buf, int bufsize, int const seq, int const pid) @@ -190,7 +199,7 @@ namespace libtorrent { namespace { rtmsg* rt_msg = reinterpret_cast(NLMSG_DATA(nl_hdr)); - if((rt_msg->rtm_family != AF_INET && rt_msg->rtm_family != AF_INET6) || (rt_msg->rtm_table != RT_TABLE_MAIN + if (!valid_addr_family(rt_msg->rtm_family) || (rt_msg->rtm_table != RT_TABLE_MAIN && rt_msg->rtm_table != RT_TABLE_LOCAL)) return false; @@ -282,11 +291,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl if (sa == nullptr || rti_info[RTAX_DST] == nullptr || rti_info[RTAX_NETMASK] == nullptr - || (sa->sa_family != AF_INET -#if TORRENT_USE_IPV6 - && sa->sa_family != AF_INET6 -#endif - )) + || !valid_addr_family(sa->sa_family)) return false; rt_info->gateway = sockaddr_to_address(rti_info[RTAX_GATEWAY]); @@ -310,13 +315,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl #if TORRENT_USE_IFADDRS && !defined TORRENT_BUILD_SIMULATOR bool iface_from_ifaddrs(ifaddrs *ifa, ip_interface &rv) { - int family = ifa->ifa_addr->sa_family; - - if (family != AF_INET -#if TORRENT_USE_IPV6 - && family != AF_INET6 -#endif - ) + if (!valid_addr_family(ifa->ifa_addr->sa_family)) { return false; } @@ -472,12 +471,7 @@ namespace libtorrent if (ifa->ifa_addr == nullptr) 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 - ) + if (valid_addr_family(ifa->ifa_addr->sa_family)) { ip_interface iface; if (iface_from_ifaddrs(ifa, iface)) @@ -532,11 +526,7 @@ namespace libtorrent if (remaining < current_size) break; - if (item.ifr_addr.sa_family == AF_INET -#if TORRENT_USE_IPV6 - || item.ifr_addr.sa_family == AF_INET6 -#endif - ) + if (valid_addr_family(item.ifr_addr.sa_family)) { ip_interface iface; iface.interface_address = sockaddr_to_address(&item.ifr_addr); @@ -627,10 +617,11 @@ namespace libtorrent IP_ADAPTER_UNICAST_ADDRESS* unicast = adapter->FirstUnicastAddress; while (unicast) { - r.interface_address = sockaddr_to_address(unicast->Address.lpSockaddr); - - ret.push_back(r); - + if (valid_addr_family(unicast->Address.lpSockaddr->sa_family)) + { + r.interface_address = sockaddr_to_address(unicast->Address.lpSockaddr); + ret.push_back(r); + } unicast = unicast->Next; } }