使用pcap_compile将网络掩码的过滤器设置为零

时间:2015-04-13 11:04:10

标签: wireshark pcap packet-sniffers

我在winpcap的帮助下实现了一个嗅探器。现在我正在获取数据包并使用后台工作程序更新UI。现在我试图对数据包应用过滤器,所以我决定使用pcap_compile()和pcap_setfilter()API。但pcap_Compile()需要一个网络掩码,所以我使用以下代码

for(pIf=pIfList,i=0; i<num-1; pIf=pIf->next,i++);

 // Open the device.
if((pPcap= pcap_open(
    pIf->name,                  // name of the device
    65536,                      // portion of the packet to capture
    PCAP_OPENFLAG_PROMISCUOUS,  // promiscuous mode
    1000,                       // read timeout
    NULL,                       // authentication on the remote machine
    err                         // error buffer
    )) == NULL)
{
    printf("\nUnable to open the adapter. %s is not supported by WinPcap\n",pIf->name);
    //goto Exit;  //one function is nedded*/
}
gPcap = pPcap;

if (pIf->addresses != NULL)
    /* Retrieve the mask of the first address of the interface */
    net=((struct sockaddr_in *)(pIf->addresses->netmask))->sin_addr.S_un.S_addr;
  else
    /* If the interface is without an address we suppose to be in a C class network */
    net=0xffffffff; 


   //compile the filter
if (pcap_compile(gPcap, &fcode, "type ctl subtype rts", 0, net) < 0)
{
    fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
    /* Free the device list */
    pcap_freealldevs(alldevs);
    return -1;
}
//set the filter
if (pcap_setfilter(gPcap, &fcode) < 0)
{
    fprintf(stderr,"\nError setting the filter.\n");
    /* Free the device list */
    pcap_freealldevs(alldevs);
    return -1;
}

我的网络掩码值为零。我使用了不同的过滤器表达式,如“类型mgt”,“类型ctl”,类型数据“,”ip“等...但过滤器操作不起作用,它提供所有数据包。我不明白为什么过滤器是不工作。你能建议我吗?

我使用以下API来获取数据包:

restart: 
status = pcap_next_ex( pPcap, &header, &pkt_data);
   {    
    if(status == 0)// Timeout elapsed 
        goto restart;
   }

上面的代码我在无限循环中运行。 你能告诉我为什么我的过滤器不工作吗?

谢谢,  Sathish所在

0 个答案:

没有答案