如何在pcap vc ++中过滤传入的数据包

时间:2010-01-04 10:01:29

标签: visual-c++ packet-sniffers

hai ..在vc ++中使用pcap编写简单的tcp嗅探器。所以如何阻止或过滤来自任何ip的所有传入口袋

请提出您的建议

谢谢

1 个答案:

答案 0 :(得分:1)

 PcapHandle = pcap_open_live(device, 65535, 1, 0, errbuf);
  if (PcapHandle == NULL) {
// Error handling
  }
  /* Compile and apply the filter */
  sprintf(FilterExp, "dst host %s", sDestIp); // sDestIp your IP to filter
  if (pcap_compile(m_pPcapHandle, &fp, FilterExp, 1, mask) < 0) {
    printf("Couldn't parse filter '%s': %s\n", FilterExp, pcap_geterr(PcapHandle));
    pcap_close(PcapHandle);
// Error handling
  }
  if (pcap_setfilter(PcapHandle, &fp) < 0) {
    printf("Couldn't install filter '%s': %s\n", FilterExp, pcap_geterr(PcapHandle));
    pcap_freecode(&fp);
    pcap_close(m_pPcapHandle);
// Error handling
  }
  pcap_loop(...)