static int match_interfaces(char *host) { struct in6_addr **lif = local_interfaces(); struct hostent *hp; int rc = 0; int errnum; /* are there any local interfaces */ if (lif == NULL) return (0); /* cycle through the host db addresses */ hp = getipnodebyname(host, AF_INET6, AI_ALL|AI_V4MAPPED, &errnum); if (hp != NULL) { struct in6_addr **tmp = (struct in6_addr **)hp->h_addr_list; int i; for (i = 0; ((rc == 0) && (tmp[i] != NULL)); i++) { int j; for (j = 0; ((rc == 0) && (lif[j] != NULL)); j++) if (memcmp(tmp[i], lif[j], sizeof (struct in6_addr)) == 0) rc = 1; } } free(lif); return (rc); }
static boost::asio::ip::address local_address() { boost::system::error_code ec; boost::asio::ip::address ret = boost::asio::ip::address_v4::any(); const std::vector<interface_t> & interfaces = local_interfaces(ec) ; std::vector<interface_t>::const_iterator it = interfaces.begin() ; for (; it != interfaces.end(); ++it) { const boost::asio::ip::address & a = (*it).destination; /** * Skip loopback, multicast and any. */ if ( address_is_loopback(a)|| address_is_multicast(a) || address_is_any(a) ) { continue; } // Other flags, IFF_UP, etc... /** * Prefer an ipv4 address over v6. */ if (a.is_v4()) { ret = a; break; } /** * If this one is not any then return it. */ if (ret != boost::asio::ip::address_v4::any()) { ret = a; } } return ret; }