ctdb-common: Replace pcap_open_live() by lower level calls
authorMartin Schwenke <mschwenke@ddn.com>
Tue, 15 Aug 2023 00:57:59 +0000 (10:57 +1000)
committerAmitay Isaacs <amitay@samba.org>
Tue, 15 Aug 2023 09:49:38 +0000 (09:49 +0000)
A subsequent commit will insert an additional call before
pcap_activate().

This sequence of calls is taken from the source for pcap_open_live(),
so there should be no change in behaviour.

Given the defaults set by pcap_create_common(), it would be possible
to omit the calls to pcap_set_promisc() and pcap_set_timeout().
However, those defaults don't seem to be well documented, so continue
to explicitly set everything that was set before.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15451

Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/common/system_socket.c

index 3a7b6eb41de6057383a73796070981ba87989f17..16463af5a335d39da32875bd9d3121c4e899befc 100644 (file)
@@ -980,14 +980,38 @@ int ctdb_sys_open_capture_socket(const char *iface, void **private_data)
        int pcap_packet_type;
        const char *t = NULL;
        int fd;
+       int ret;
 
-       pt = pcap_open_live(iface, 100, 0, 0, errbuf);
+       pt = pcap_create(iface, errbuf);
        if (pt == NULL) {
                DBG_ERR("Failed to open pcap capture device %s (%s)\n",
                        iface,
                        errbuf);
                return -1;
        }
+       /*
+        * pcap isn't very clear about defaults...
+        */
+       ret = pcap_set_snaplen(pt, 100);
+       if (ret < 0) {
+               DBG_ERR("Failed to set snaplen for pcap capture\n");
+               goto fail;
+       }
+       ret = pcap_set_promisc(pt, 0);
+       if (ret < 0) {
+               DBG_ERR("Failed to unset promiscuous mode for pcap capture\n");
+               goto fail;
+       }
+       ret = pcap_set_timeout(pt, 0);
+       if (ret < 0) {
+               DBG_ERR("Failed to set timeout for pcap capture\n");
+               goto fail;
+       }
+       ret = pcap_activate(pt);
+       if (ret < 0) {
+               DBG_ERR("Failed to activate pcap capture\n");
+               goto fail;
+       }
 
        pcap_packet_type = pcap_datalink(pt);
        switch (pcap_packet_type) {