winpcap pcap_compile MAC 地址过滤

 

使用Winpcap编写了一个按照mac地址过滤的函数,刚开始怎么设置MAC地址都不正确,最后发现是格式不正确,争取的Filter的格式如下:特别编写如下:

char filter =   "ether   dst   00:90:41:C0:C1:C3";

具体的一段代码如下:

char filter[512]; 

sprintf(filter,"ether proto 0x1111 and ether dst %02X",m_macAddress[0]);
   for(int i=1;i<6;i++)
   {
    sprintf(tempChar,":%02X",m_macAddress[i]);
    strcat(filter,tempChar);
   }

  if ( (m_pcapAdhandle= pcap_open_live(this->m_ifName,          // name of the device
                 65536,  // portion of the packet to      capture.  PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
  10,             // read timeout
  errbuf            // error buffer
  ) ) == NULL)
    {
   DEBUG_HELPER->log("netcomm",LM_ERROR,"Unable to open the adapter. %s is not supported by WinPcap\n",m_ifName);
         return -1;
    }
 
 if (filter != NULL)
 {
  // We should loop through the adapters returned by the pcap_findalldevs_ex()
  // in order to locate the correct one.
  //
  // Let's do things simpler: we suppose to be in a C class network ;-)
  NetMask=0xffffff;

  //compile the filter
  if(pcap_compile(m_pcapAdhandle, &fcode, filter, 1, NetMask) < 0)
  {
   char* reterror = pcap_geterr(m_pcapAdhandle);
   DEBUG_HELPER->log("netcomm",LM_ERROR,"Error compiling filter: wrong syntax.%s\n",reterror);
   pcap_close(m_pcapAdhandle);
   return -3;
  }

  //set the filter
  if(pcap_setfilter(m_pcapAdhandle, &fcode)<0)
  {
   
   DEBUG_HELPER->log("netcomm",LM_ERROR,"Error setting the filter\n");
   pcap_close(m_pcapAdhandle);
   return -4;
  }
 }
 m_ifIsOpen=true;

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值