-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
32 lines (27 loc) · 800 Bytes
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
struct ifaddrs *addresses;
if (getifaddrs(&addresses) == -1) {
printf("getofaddrs call failed\n");
return -1;
}
struct ifaddrs *address = addresses;
while(address) {
int family = address->ifa_addr->sa_family;
if (family == AF_INET || family == AF_INET6) {
printf("%s\t", address->ifa_name);
printf("%s\t", family == AF_INET ? "IPv4" : "IPv6");
char ap[100];
const int family_size = family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
getnameinfo(address->ifa_addr, family_size, ap, sizeof(ap), 0, 0, NI_NUMERICHOST);
printf("\t%s\n", ap);
}
address = address->ifa_next;
}
freeifaddrs(addresses);
return 0;
}