3

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?

Pirozek
  • 1,250
  • 4
  • 16
  • 25
  • How do you know the IPs are wrong? ifconfig? What if you run it a different way: http://stackoverflow.com/questions/4139405/how-to-know-ip-address-for-interfaces-in-c/4139893#4139893 – chrisaycock Nov 27 '10 at 23:42
  • I know they are wrong because of ifconfig :) Besides that, they change every time I run a program – Pirozek Nov 27 '10 at 23:47
  • Related: https://stackoverflow.com/a/9443991/7508077 – EsmaeelE Mar 30 '20 at 09:47

1 Answers1

2

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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Yes, that's what I was referring to when I said "if indeed the family is AF_INET." The family is of course just another field in the sockaddr. – John Zwinck Nov 28 '10 at 00:40
  • Looks like you can only retrieve the IP this way. How do you get the mac address? – T3rm1 Nov 28 '12 at 21:02
  • I'd try this approach: http://stackoverflow.com/questions/1779715/how-to-get-mac-address-of-your-machine-using-a-c-program to get the MAC address, once you have the IP address. This of course assumes that the system configuration doesn't change between capture time and analysis time. – John Zwinck Dec 10 '12 at 10:10