msantos / epcap

Erlang packet capture interface using pcap
http://listincomprehension.com/2009/12/erlang-packet-sniffer-using-ei-and.html
BSD 3-Clause "New" or "Revised" License
178 stars 56 forks source link

Add immediate mode #28

Closed erlbeck closed 3 years ago

erlbeck commented 3 years ago

Currently the test suite does not run successfully on Linux 5.4.0 / libpcap 1.9.1 due to timeouts.

There is also no way to force the immediate mode in a portable way (the closest would be {timeout, 1}).

Add an option 'immediate' ('-I' in epcap's CLI) to explicitly set libpcap's immediate mode and use it in the test suite instead of relying on the default timeout settings.

msantos commented 3 years ago

Older versions of libpcap don't seem to have PCAP_IF_CONNECTION_STATUS

diff --git a/c_src/epcap.c b/c_src/epcap.c
index b5edf49..3d8dd7c 100644
--- a/c_src/epcap.c
+++ b/c_src/epcap.c
@@ -396,8 +396,10 @@ static int epcap_open(EPCAP_STATE *ep) {
         if (!(flags & PCAP_IF_UP) || !(flags & PCAP_IF_RUNNING))
           continue;

+#ifdef PCAP_IF_CONNECTION_STATUS_DISCONNECTED
         if ((flags & PCAP_IF_CONNECTION_STATUS) == PCAP_IF_CONNECTION_STATUS_DISCONNECTED)
           continue;
+#endif

         if (!(flags & PCAP_IF_LOOPBACK))
           /* found */
erlbeck commented 3 years ago

Oops, I didn't want to include that chunk into the commit. Even if keeping that, it should go into another commit.

erlbeck commented 3 years ago

Other than intended, this PR changes the behaviour on non-Linux platforms which I wanted to avoid.

erlbeck commented 3 years ago

I have changed the first commit to set the immediate mode by default. The -I epcap CLI option now has a numeric value which is used to disable (0) or enable (1) the immediate mode. The value of the 'immediate' option is translated accordingly.

The chunk related to pcap_findalldevs is now removed.

Documentation on the 'timeout' option has been added.