I am trying to find address of devices in my computer. So far i managed to get list of devices(with pcap_findalldevs) but i can`t figure out how to get to those addresses. I saw this manpage - http://www.tcpdump.org/pcap3_man.html where is written something like this
addresses a pointer to the first element of a list of addresses for the interface
And then this
Each element of the list of addresses is of type pcap_addr_t, and has the following members:
So I have this code
pcap_if_开发者_运维技巧t *alldevsp , *device;
char *devname , **devs;
int count = 1 , n;
if(pcap_findalldevs(&alldevsp, errbuf))
{
printf("Error: %s" , errbuf);
exit(1);
}
device = alldevsp;
pcap_addr_t list;
printf("\nDevices:\n");
while(device != NULL)
{
printf("%d. %s - %s", count++ , device->name , device->description);
list = device->addresses[0];
printf("address: %s\n",(char *) inet_ntoa(list.addr));
device = device->next;
}
Compilation is OK, but when i try to run it i get this:
Devices: 1. eth0 - (null)addres: 144.208.30.8 2. wlan0 - (null)addres: 128.213.30.8 Segmentation fault
I can understand that segfault, because third device is usb and it doesnt have address, but those IP for eth0 and wlan0 are wrong, they doesnt match.
What am I doing wrong?
From your link:
Each element of the list of addresses is of type pcap_addr_t,
and has the following members:
...
addr
a pointer to a struct sockaddr containing an address
...
Now, what is a struct sockaddr
? See here:
http://www.retran.com/beej/sockaddr_inman.html
So where you are doing this:
printf("address: %s\n",(char *) inet_ntoa(list.addr));
You should be doing something like this:
printf("address: %s\n", inet_ntoa(((struct sockaddr_in*)list.addr)->sin_addr));
That is, you need to extract the "IP address" (if indeed the family is AF_INET
), else you are giving inet_ntoa
the wrong type of argument.
精彩评论