使用libpcap查找设备地址
我正在尝试查找计算机中设备的地址。到目前为止,我设法获取设备列表(使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的链接:
现在,什么是 struct sockaddr ?参见这里:
http://www.retran.com/beej/sockaddr_inman.html
那么你在哪里这样做:
你应该做这样的事情:
也就是说,你需要提取“IP地址”(如果确实是
AF_INET
),否则你将给出inet_ntoa
> 论证类型错误。From your link:
Now, what is a
struct sockaddr
? See here:http://www.retran.com/beej/sockaddr_inman.html
So where you are doing this:
You should be doing something like this:
That is, you need to extract the "IP address" (if indeed the family is
AF_INET
), else you are givinginet_ntoa
the wrong type of argument.