getifaddrs
does support IPv6. Here's an example on how to print the name and IP address of all interfaces on the system:
struct ifaddrs *ifa, *ifa_tmp;
char addr[INET6_ADDRSTRLEN];
if (getifaddrs(&ifa) == -1) {
perror("getifaddrs failed");
exit(1);
}
ifa_tmp = ifa;
while (ifa_tmp) {
if ((ifa_tmp->ifa_addr) && ((ifa_tmp->ifa_addr->sa_family == AF_INET) ||
(ifa_tmp->ifa_addr->sa_family == AF_INET6))) {
if (ifa_tmp->ifa_addr->sa_family == AF_INET) {
// create IPv4 string
struct sockaddr_in *in = (struct sockaddr_in*) ifa_tmp->ifa_addr;
inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
} else { // AF_INET6
// create IPv6 string
struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa_tmp->ifa_addr;
inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
}
printf("name = %s\n", ifa_tmp->ifa_name);
printf("addr = %s\n", addr);
}
ifa_tmp = ifa_tmp->ifa_next;
}
freeifaddrs(ifa);