-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_ips2.c
79 lines (65 loc) · 2.31 KB
/
print_ips2.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct ifaddrs *ifaddr, *ifa;
int count = 0, i = 0, j = 0;
struct Interface
{
char name[50];
char ip[INET_ADDRSTRLEN];
char netmask[INET_ADDRSTRLEN];
char ipv6[INET6_ADDRSTRLEN];
} interface[10];
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs() error");
exit(-1);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family == AF_PACKET)
{
continue;
}
// addl check to prevent results like 1.0.0.0 (from AF_PACKET)
if (ifa->ifa_addr->sa_family == AF_INET)
{
// get interface name
strncpy(interface[count].name, ifa->ifa_name, sizeof(&(ifa->ifa_name)));
// get ip addr
inet_ntop(AF_INET, &(((struct sockaddr_in*)ifa->ifa_addr)->sin_addr), interface[count].ip, INET_ADDRSTRLEN);
// get netmask
inet_ntop(AF_INET, &(((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr), interface[count].netmask, INET_ADDRSTRLEN);
// only increment count for each ipv4 interface
count++;
}
if (ifa->ifa_addr->sa_family == AF_INET6)
{
// find correct struct (assumes that there is a corresponding ipv4 int for each ipv6 int)
for (j = 0; j < count; j++)
{
if (strcmp(ifa->ifa_name, interface[j].name) == 0)
{
// set ipv6 addr
inet_ntop(AF_INET6, &(((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr), interface[j].ipv6, INET6_ADDRSTRLEN);
}
}
}
}
// print out interface info
// count is set while looping through the interfaces and using it here prevents garbage output
for (i = 0; i < count; i++)
{
printf("Device: %s\n", interface[i].name);
printf("IP: %s\n", interface[i].ip);
printf("Netmask: %s\n", interface[i].netmask);
printf("IPv6: %s\n", interface[i].ipv6);
printf("\n");
}
freeifaddrs(ifaddr);
exit(0);
}