From: Luyao Huang Date: Tue, 10 Mar 2015 00:04:58 +0000 (-0400) Subject: util: Update virNetDevGetIPAddress to get IPv6 addresses X-Git-Tag: v1.2.15-rc1~236 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2605089c0450c47dc2e696c9be2bb75183d93b91;p=thirdparty%2Flibvirt.git util: Update virNetDevGetIPAddress to get IPv6 addresses Add static virNetDevGetifaddrsAddress to attempt to get the interface IP address. If getifaddrs is not supported, fall back to virNetDevGetIPv4AddressIoctl to get the IP address. This allows IPv6 addresses to be used for ifa_next) { + int family = ifa->ifa_addr->sa_family; + + if (STRNEQ_NULLABLE(ifa->ifa_name, ifname)) + continue; + if (family != AF_INET6 && family != AF_INET) + continue; + + if (family == AF_INET6) { + addr->len = sizeof(addr->data.inet6); + memcpy(&addr->data.inet6, ifa->ifa_addr, addr->len); + } else { + addr->len = sizeof(addr->data.inet4); + memcpy(&addr->data.inet4, ifa->ifa_addr, addr->len); + } + addr->data.stor.ss_family = family; + ret = 0; + goto cleanup; + } + + virReportError(VIR_ERR_INTERNAL_ERROR, + _("no IP address found for interface '%s'"), + ifname); + cleanup: + freeifaddrs(ifap); + return ret; +} + +#else /* ! HAVE_GETIFADDRS */ + +static int +virNetDevGetifaddrsAddress(const char *ifname ATTRIBUTE_UNUSED, + virSocketAddrPtr addr ATTRIBUTE_UNUSED) +{ + return -2; +} + +#endif + /** * virNetDevGetIPAddress: * @ifname: name of the interface whose IP address we want @@ -1454,6 +1522,9 @@ virNetDevGetIPAddress(const char *ifname, memset(addr, 0, sizeof(*addr)); addr->data.stor.ss_family = AF_UNSPEC; + if ((ret = virNetDevGetifaddrsAddress(ifname, addr)) != -2) + return ret; + if ((ret = virNetDevGetIPv4AddressIoctl(ifname, addr)) != -2) return ret;