-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetifaddr.c
40 lines (35 loc) · 946 Bytes
/
getifaddr.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
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <linux/if_link.h>
int main(int argc, char *argv[])
{
struct ifaddrs *ifaddr, *ifa;
int family, s, n;
if (getifaddrs(&ifaddr) == -1)
return -1;
for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr) {
char host[100];
printf("ifname %s ", ifa->ifa_name);
printf("\tfamily ");
s = -1;
if (ifa->ifa_addr->sa_family == AF_INET) {
printf("AF_INET ");
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
printf("AF_INET6 ");
} else {
printf("??? ");
}
if (s == 0)
printf("\t\taddress %s", host);
printf("\n");
}
}
freeifaddrs(ifaddr);
return 0;
}