From dd41206f2a5127871fc4c9911a748f7f4e3b6c51 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Fri, 26 Mar 2010 19:02:32 -0700 Subject: tweak the ifaddrs address discovery --- host/lib/transport/if_addrs.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'host') diff --git a/host/lib/transport/if_addrs.cpp b/host/lib/transport/if_addrs.cpp index eb0e56b3a..5c8c8a176 100644 --- a/host/lib/transport/if_addrs.cpp +++ b/host/lib/transport/if_addrs.cpp @@ -16,6 +16,9 @@ // #include +#include +#include +#include uhd::transport::if_addrs_t::if_addrs_t(void){ /* NOP */ @@ -26,17 +29,11 @@ uhd::transport::if_addrs_t::if_addrs_t(void){ **********************************************************************/ #ifdef HAVE_IFADDRS_H #include -#include static boost::asio::ip::address_v4 sockaddr_to_ip_addr(sockaddr *addr){ - if (addr->sa_family == AF_INET) return boost::asio::ip::address_v4(ntohl( + return boost::asio::ip::address_v4(ntohl( reinterpret_cast(addr)->sin_addr.s_addr )); - return boost::asio::ip::address_v4::any(); -} - -static bool ifaddrs_valid(const struct ifaddrs *ifaddrs){ - return ifaddrs->ifa_addr->sa_family == AF_INET; } std::vector uhd::transport::get_if_addrs(void){ @@ -44,7 +41,12 @@ std::vector uhd::transport::get_if_addrs(void){ struct ifaddrs *ifap; if (getifaddrs(&ifap) == 0){ for (struct ifaddrs *iter = ifap; iter != NULL; iter = iter->ifa_next){ - if (not ifaddrs_valid(iter)) continue; + //ensure that the entries are valid + if (iter->ifa_addr->sa_family != AF_INET) continue; + if (iter->ifa_netmask->sa_family != AF_INET) continue; + if (iter->ifa_broadaddr->sa_family != AF_INET) continue; + + //append a new set of interface addresses if_addrs_t if_addr; if_addr.inet = sockaddr_to_ip_addr(iter->ifa_addr).to_string(); if_addr.mask = sockaddr_to_ip_addr(iter->ifa_netmask).to_string(); @@ -60,10 +62,7 @@ std::vector uhd::transport::get_if_addrs(void){ * Interface address discovery through windows api (TODO) **********************************************************************/ #elif HAVE_WINSOCK2_H -#include -#include #include -#include std::vector uhd::transport::get_if_addrs(void){ std::vector if_addrs; -- cgit v1.2.3