pcap_loop和pcap_dispatch的区别

时间:2011-02-06 23:54:18

标签: networking wireshark libpcap packet-sniffers sniffing

pcap_loop和pcap_dispatch之间究竟有什么区别?

1 个答案:

答案 0 :(得分:18)

手册对此描述得非常好(我说的是直面,承诺)。来自man pcap_loop

   pcap_loop() processes packets from a  live  capture  or  ``savefile''
   until  cnt  packets  are  processed,  the  end of the ``savefile'' is
   reached when reading from a ``savefile'', pcap_breakloop() is called,
   or  an  error  occurs.   It  does  not return when live read timeouts
   occur.  A value of -1 or 0 for cnt is equivalent to infinity, so that
   packets are processed until another ending condition occurs.

   pcap_dispatch() processes packets from a live capture or ``savefile''
   until cnt packets are processed, the end of the current bufferful  of
   packets  is reached when doing a live capture, the end of the ``save‐
   file'' is reached when reading from a ``savefile'',  pcap_breakloop()
   is  called, or an error occurs.  Thus, when doing a live capture, cnt
   is the maximum number of packets to process before returning, but  is
   not a minimum number; when reading a live capture, only one bufferful
   of packets is read at a time, so fewer than cnt packets may  be  pro‐
   cessed. A value of -1 or 0 for cnt causes all the packets received in
   one buffer to be processed when reading a live  capture,  and  causes
   all  the  packets  in the file to be processed when reading a ``save‐
   file''.

我知道你真的不想阅读和理解所有这些,所以让我们把它分解。

这两项功能

  • 处理来自实时捕获或“savefile”的数据包,直到出现以下任何情况:
    • 达到指定的数量
    • 到达“savefile”的结尾
    • 调用pcap_breakloop()
    • 发生错误
  • 考虑-1或0实质上意味着“处理无限数量的数据包” - 也就是说,直到另一个结束条件发生。 (建议使用-1与旧版本的互操作性,稍后在手册中)

pcap_dispatch()

  • 在达到当前缓冲数据包结束后返回,当进行实时捕获时(换句话说,可以更频繁地返回,因为指定的计数不是最小值)