My C is terribly rusty, and structs with members containing unions (like struct sockaddr_in6
) no longer fit in my brain, so in the best cut and paste traditions I adapted chrisaycock's answer (which enumerates the system's IPv4 addresses) to use getnameinfo()
instead, with some help from the getifaddrs()
man page (which has a better example):
#define _GNU_SOURCE # required for NI_NUMERICHOST
#include <arpa/inet.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
int main ()
{
struct ifaddrs *ifap, *ifa;
struct sockaddr_in6 *sa;
char addr[INET6_ADDRSTRLEN];
if (getifaddrs(&ifap) == -1) {
perror("getifaddrs");
exit(1);
}
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_INET6) {
sa = (struct sockaddr_in6 *) ifa->ifa_addr;
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), addr,
sizeof(addr), NULL, 0, NI_NUMERICHOST);
printf("Interface: %sAddress: %s
", ifa->ifa_name, addr);
}
}
freeifaddrs(ifap);
return 0;
}
The output on my system:
Interface: lo Address: ::1
Interface: br0 Address: fdbf:e684:d5fb:6:6e62:6dff:fed1:dfad
Interface: br0 Address: 2001:db8:1f80:81c6:6e62:6dff:fed1:dfad
Interface: br0 Address: fe80::6e62:6dff:fed1:dfad%br0
Interface: virbr1 Address: fe80::5054:ff:fece:bfec%virbr1
Interface: virbr0 Address: fe80::5054:ff:fef9:c92e%virbr0
Interface: virbr2 Address: fe80::5054:ff:fedd:ea18%virbr2
Interface: vnet0 Address: fe80::fc54:ff:fe90:de19%vnet0
Interface: vnet1 Address: fe80::fc54:ff:fede:b69c%vnet1
Remember to add error checking to everything; some error checks have been omitted from this example.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…