[问题]我用libpcap编写抓包程序,找不到我的网卡?

软件和网站开发以及相关技术探讨
回复
ruben
帖子: 9
注册时间: 2008-04-29 22:21

[问题]我用libpcap编写抓包程序,找不到我的网卡?

#1

帖子 ruben » 2008-07-16 16:08

源代码如下:

代码: 全选

#include <stdio.h>
#include <pcap.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(void)
{
	char *net_dev;
	char *ip_addr;
	char *net_mask;
	char errbuf[PCAP_ERRBUF_SIZE];

	bpf_u_int32 netp;
	bpf_u_int32 maskp;
	struct in_addr addr;

	net_dev=pcap_lookupdev(errbuf);
	if(net_dev==NULL){
		printf("cannot get the network device info: %s\n",errbuf);
		return 1;
	}	
        printf("the network device is: %s\n",net_dev);

	if(pcap_lookupnet(net_dev,&netp,&maskp,errbuf)==-1){
		printf("cannot get the network device ip info:%s\n",errbuf);
		return 1;
	}

	addr.s_addr=netp;
	ip_addr=inet_ntoa(addr);

	if(ip_addr==NULL){
		printf("convert network address fail\n");
		return 1;
	}
        printf("ip address is : %s\n",ip_addr);

	addr.s_addr=maskp;
	net_mask=inet_ntoa(addr);

	if(net_mask==NULL){
		printf("convert network mask fail\n");
		return 1;
	}

	printf("network mask is : %s\n",net_mask);

	return 0;
}
运行结果如下:
cannot get the network device info: no suitable device found
高手能帮忙看一下什么问题吗?
hnbingpo
帖子: 13
注册时间: 2007-05-14 22:19

Re: [问题]我用libpcap编写抓包程序,找不到我的网卡?

#2

帖子 hnbingpo » 2008-07-16 23:50

pcap_lookupdev都已经是被winpcap官方明确标志为Deprecated的函数,为什么还是坚持要使用。
try pcap_findalldevs
wanghuaiye
帖子: 7
注册时间: 2007-10-21 23:34

#3

帖子 wanghuaiye » 2008-07-22 15:15

ms是权限问题,我也遇到了同样的问题
你在终端里输入sudo su,再去编译执行,就没问题了
ruben
帖子: 9
注册时间: 2008-04-29 22:21

#4

帖子 ruben » 2008-07-22 21:54

wanghuaiye 写了:ms是权限问题,我也遇到了同样的问题
你在终端里输入sudo su,再去编译执行,就没问题了
谢谢三楼,是这个问题。
myspacelhy
帖子: 1
注册时间: 2008-10-11 13:41

Re: [问题]我用libpcap编写抓包程序,找不到我的网卡?

#5

帖子 myspacelhy » 2008-10-19 13:45

呵呵,我也遇到这个问题了,谢谢3楼和楼主了! :em11
twinstudy
帖子: 2
注册时间: 2008-09-06 17:55

Re: [问题]我用libpcap编写抓包程序,找不到我的网卡?

#6

帖子 twinstudy » 2009-09-15 22:13

谢谢了
回复