-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_ips.c
51 lines (44 loc) · 1.53 KB
/
print_ips.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
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <arpa/inet.h>
int main(int arg, char *argv[])
{
char errbuf[PCAP_ERRBUF_SIZE] = {0};
pcap_if_t *all_interfaces = NULL;
pcap_if_t *d = NULL;
pcap_addr_t *a = NULL;
char ip[INET_ADDRSTRLEN] = {0};
char netmask[INET_ADDRSTRLEN] = {0};
char ipv6[INET6_ADDRSTRLEN] = {0};
if (pcap_findalldevs(&all_interfaces, errbuf) != 0)
{
printf("Error: %s\n", errbuf);
exit(1);
}
for (d = all_interfaces; d != NULL; d = d->next)
{
for (a = d->addresses; a != NULL; a = a->next)
{
if (a->addr->sa_family == AF_INET)
{
printf("Device: %s\n", d->name);
// get IP address
inet_ntop(AF_INET, &(((struct sockaddr_in*)a->addr)->sin_addr), ip, INET_ADDRSTRLEN);
printf("IP: %s\n", ip);
// get netmask
inet_ntop(AF_INET, &(((struct sockaddr_in*)a->netmask)->sin_addr), netmask, INET_ADDRSTRLEN);
printf("Netmask: %s\n", netmask);
}
if (a->addr->sa_family == AF_INET6)
{
// get IPv6 address
inet_ntop(AF_INET6, &(((struct sockaddr_in6*)a->addr)->sin6_addr), ipv6, INET6_ADDRSTRLEN);
// extra \n : separate interface data (many assumptions here)
printf("IPv6: %s\n\n", ipv6);
}
}
}
pcap_freealldevs(all_interfaces);
exit(0);
}