2017-10-04 48 views
0

我想將一個基於libpcap的程序移植到macos,它似乎是爲windows和linux編寫的。在pcap_open_live函數中,讀取超時設置爲-1,與PacketOpen相同,在macOS上,當嘗試打開接口BIOCSRTIMEOUT時會導致錯誤:無效的參數。我無法找到什麼-1讀取超時實際上做任何文檔。另外,有沒有一個版本可以讓我在基於BPF的libpcap上做同樣的事情?在winpcap和linux libpcap上做什麼to_ms = -1?

回答

1

在winpcap和linux libpcap上做什麼to_ms = -1?

無法預測。引用主分支pcap(3pcap)手冊頁:

packet buffer timeout 
     If, when capturing, packets are delivered as soon as they 
     arrive, the application capturing the packets will be woken up 
     for each packet as it arrives, and might have to make one or 
     more calls to the operating system to fetch each packet. 

     If, instead, packets are not delivered as soon as they arrive, 
     but are delivered after a short delay (called a "packet buffer 
     timeout"), more than one packet can be accumulated before the 
     packets are delivered, so that a single wakeup would be done for 
     multiple packets, and each set of calls made to the operating 
     system would supply multiple packets, rather than a single 
     packet. This reduces the per‐packet CPU overhead if packets are 
     arriving at a high rate, increasing the number of packets per 
     second that can be captured. 

     The packet buffer timeout is required so that an application 
     won’t wait for the operating system’s capture buffer to fill up 
     before packets are delivered; if packets are arriving slowly, 
     that wait could take an arbitrarily long period of time. 

     Not all platforms support a packet buffer timeout; on platforms 
     that don’t, the packet buffer timeout is ignored. A zero value 
     for the timeout, on platforms that support a packet buffer time‐ 
     out, will cause a read to wait forever to allow enough packets 
     to arrive, with no timeout. 

     NOTE: the packet buffer timeout cannot be used to cause calls 
     that read packets to return within a limited period of time, 
     because, on some platforms, the packet buffer timeout isn’t sup‐ 
     ported, and, on other platforms, the timer doesn’t start until 
     at least one packet arrives. This means that the packet buffer 
     timeout should NOT be used, for example, in an interactive 
     application to allow the packet capture loop to ‘‘poll’’ for 
     user input periodically, as there’s no guarantee that a call 
     reading packets will return after the timeout expires even if no 
     packets have arrived. 

沒有關於負超時的說法;我會更新它明確地說不應該使用負值。 (不是在Windows上,不在MacOS上,不在Linux上,不在* BSD上,不在Solaris上,不在AIX上,不在HP-UX上,不在Tru64 UNIX上,不在IRIX上,而不在上,任何東西

通過將超時設置爲-1,它們可能是意圖pcap_t置於「非阻塞模式」,其中嘗試讀取將在沒有數據包等待讀取時立即返回,而不是等待數據包到達。因此,請在pcap_open_live()調用之後提供例如100(表示1/10秒)的超時並使用pcap_setnonblock()pcap_t置於非阻塞模式。這應該在全部平臺上工作。