使用libpcap查找设备地址

发布于 2024-10-05 12:32:12 字数 1085 浏览 3 评论 0原文

我正在尝试查找计算机中设备的地址。到目前为止,我设法获取设备列表(使用 pcap_findalldevs),但我不知道如何到达这些地址。我看到了这个手册页 - http://www.tcpdump.org/pcap3_man.html 哪里写着这样的东西

地址 指向接口地址列表第一个元素的指针

然后这个

地址列表的每个元素 是 pcap_addr_t 类型,并且具有 以下成员:

所以我有这个代码

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; 
 }

编译没问题,但是当我尝试运行它时我得到这个:

设备: 1. eth0 -(空)地址:144.208.30.8 2. wlan0 -(空)地址:128.213.30.8 分段错误

我可以理解分段错误,因为第三个设备是USB并且它没有地址,但是eth0和wlan0的那些IP是错误的,它们不匹配。

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-10-12 12:32:12

从您的链接:

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
    ...

现在,什么是 struct sockaddr ?参见这里:
http://www.retran.com/beej/sockaddr_inman.html

那么你在哪里这样做:

printf("address: %s\n",(char *) inet_ntoa(list.addr));

你应该做这样的事情:

printf("address: %s\n", inet_ntoa(((struct sockaddr_in*)list.addr)->sin_addr));

也就是说,你需要提取“IP地址”(如果确实是AF_INET),否则你将给出inet_ntoa > 论证类型错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文