Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pcap_循环只是等待_C_Network Programming_Pcap - Fatal编程技术网

pcap_循环只是等待

pcap_循环只是等待,c,network-programming,pcap,C,Network Programming,Pcap,我正试图用C语言中的pcap做一个小嗅探器,如图所示 我的问题是pcap_循环绝对不捕获任何数据包和/或什么也不做,我的回调函数从未被调用。我的猜测是超时值,但即使我将其设置为20(毫秒),也没有任何变化。我希望这只是一个我看不到的简单错误,但我会让你们试着找出它,因为它已经把我的大脑弄得太乱了 谢谢 日航 编辑:我选择wlan0作为接口,它与链接中给出的程序一起工作 我的主要意见是: int main(int argc, char* argv[]) { // interface &

我正试图用C语言中的pcap做一个小嗅探器,如图所示 我的问题是pcap_循环绝对不捕获任何数据包和/或什么也不做,我的回调函数从未被调用。我的猜测是超时值,但即使我将其设置为20(毫秒),也没有任何变化。我希望这只是一个我看不到的简单错误,但我会让你们试着找出它,因为它已经把我的大脑弄得太乱了

谢谢

日航

编辑:我选择wlan0作为接口,它与链接中给出的程序一起工作 我的主要意见是:

int main(int argc, char* argv[]) {

// interface & err buff
char *dev, errbuff[PCAP_ERRBUF_SIZE];
int i = 0,inum= -1;
// it filters out only packet from this machin
char filter_exp[] = "ip host localhost"; 
struct bpf_program fp;  /* compiled filter program (expression) */
/* typedef, no need for struct ... */
bpf_u_int32 mask;
bpf_u_int32 net;

int num_packets = 10;

// 1.0+ API pcap version
pcap_if_t * alldevs;    
pcap_if_t * pdev;
pcap_t * handle;
// 1st argument interface
if(argc == 2) {
    dev = argv[1];
    printf("Chosen interface : %s\n",dev);
}



//+1.0 api version
if(pcap_findalldevs(&alldevs,errbuff)){
    fprintf(stderr,"findalldev failed to retrieve interface\n %s",errbuff);
    return(2);
}
if(alldevs == NULL){
    fprintf(stderr,"Retrieved interface is null\n");
    return(2);
}
// select all interfaces
for(pdev = alldevs; pdev != NULL;pdev = pdev->next) {
    printf("Device %d : ",++i);
    print_pcap_if_t(pdev);
    //print_pcap_addr(pdev->addresses);

}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
if(inum < 1 || inum > i){
    fprintf(stderr,"Device %d not in list.\n",i);
    return(2);
}
/* Jump to the selected adapter */
for(pdev=alldevs, i=0; i< inum - 1 ;pdev=pdev->next, i++);
printf("\n-------------------------------------------------\n");    

//printf("Chosen device : %s",pdev->name);
//print_pcap_if_t(pdev);    

/* activate device */   
printf("activating\n");
handle = pcap_open_live(pdev->name,SNAP_LEN,1,1000,errbuff);
if(handle == NULL){
    fprintf(stderr,"Could not open device for sniffing");
    return(2);
}

/* compile filter */
if(pcap_compile(handle,&fp,filter_exp,0,net) == -1) {
    fprintf(stderr,"Could not compile filtering rules");
    return(EXIT_FAILURE);
}

/* apply filter */
if(pcap_setfilter(handle,&fp) == -1) {
    fprintf(stderr,"Could not set filtering rules");
    return(EXIT_FAILURE);
}

printf("Waiting for packets to come in your hands");
fflush(stdout);

pcap_loop(handle,num_packets,got_packet,NULL);

pcap_freecode(&fp);
pcap_close(handle);

pcap_freealldevs(alldevs);
return(0);
intmain(intargc,char*argv[]){
//接口和错误buff
char*dev,errbuff[PCAP_ERRBUF_SIZE];
int i=0,inum=-1;
//它只过滤掉这台机器上的数据包
char filter_exp[]=“ip主机本地主机”;
struct bpf_program fp;/*编译的筛选程序(表达式)*/
/*typedef,不需要结构*/
bpf_____int32掩模;
bpf____int32网络;
int num_数据包=10;
//1.0+API pcap版本
pcap_if_t*所有开发者;
pcap_if_t*pdev;
pcap_t*手柄;
//第一参数接口
如果(argc==2){
dev=argv[1];
printf(“所选接口:%s\n”,dev);
}
//+1.0 api版本
如果(pcap_findalldevs(&alldevs,errbuff)){
fprintf(stderr,“findalldev检索接口失败\n%s”,errbuff);
返回(2);
}
if(alldevs==NULL){
fprintf(stderr,“检索到的接口为空\n”);
返回(2);
}
//选择所有接口
对于(pdev=alldevs;pdev!=NULL;pdev=pdev->next){
printf(“设备%d:,++i”);
打印pcap if(pdev);
//打印pcap地址(pdev->地址);
}
printf(“输入接口编号(1-%d):”,i);
scanf(“%d”和“inum”);
如果(inum<1 | | inum>i){
fprintf(stderr,“设备%d不在列表中。\n”,i);
返回(2);
}
/*跳转到所选适配器*/
对于(pdev=alldevs,i=0;inext,i++);
printf(“\n-------------------------------------------------------------\n”);
//printf(“所选设备:%s”,pdev->name);
//打印pcap if(pdev);
/*激活设备*/
printf(“激活”);
handle=pcap\u open\u live(pdev->name,SNAP\u LEN,11000,errbuff);
if(handle==NULL){
fprintf(stderr,“无法打开设备进行嗅探”);
返回(2);
}
/*编译过滤器*/
if(pcap_compile(handle,&fp,filter_exp,0,net)=-1){
fprintf(stderr,“无法编译筛选规则”);
返回(退出失败);
}
/*应用过滤器*/
if(pcap_setfilter(handle,&fp)=-1){
fprintf(stderr,“无法设置筛选规则”);
返回(退出失败);
}
printf(“等待数据包到达您手中”);
fflush(stdout);
pcap_循环(句柄,num_数据包,got_数据包,NULL);
pcap_自由代码(&fp);
pcap_关闭(手柄);
pcap_freealldevs(alldevs);
返回(0);
}好的,我发现了问题: 我的过滤器“ip主机本地主机”是原因。我把它改成了“ip”,它就在那里=)

不过我真的不明白。我要做的是启动程序,然后刷新网页。所以我的第一个请求是GET或stg,source=localhost,不是吗?响应将在目的地字段中包含我的地址。根据手册页:

如果数据包的IPv4/v6源或目标为 主持人

是否在设置过滤器时不翻译“localhost”

无论如何,我希望它能帮助其他人

ip主机本地主机

“localhost”是IP地址127.0.0.1的名称;它不是您的计算机在Internet上的IP地址,而是一个特殊的IP地址,用于将IPv4数据包从您的计算机发送到自身(例如,“ftp localhost”,如果您想通过从计算机上的命令提示符连接到您的计算机上测试ftp服务器)

与其他主机之间的通信不会来自127.0.0.1,也不会发送到127.0.0.1


例如,如果您的计算机具有IP地址10.0.1.2,请尝试“IP主机10.0.1.2”。

got_数据包的定义是什么?尝试设置
num\u packets=-1
,这将允许
pcap\u循环无限期运行。请参阅我的答案。简而言之,“那么我的第一个请求是GET或stg,source=localhost,no?”的答案实际上是“no”