]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Move some code to better show how to handle pcap_activate() failure.
authorGuy Harris <[email protected]>
Sun, 24 Nov 2019 03:28:01 +0000 (19:28 -0800)
committerGuy Harris <[email protected]>
Sun, 24 Nov 2019 03:28:17 +0000 (19:28 -0800)
If you call pcap_activate() and it fails, you still have a pcap_t from
pcap_create(), and should close it.

Moving the code after the error() calls doesn't make a difference, as
error() exits, but it may make it clearer to those reading tcpdump.c for
help in figuring out how to use libpcap that you should close the pcap_t
if pcap_activate() fails.

(In the future, there may also be the option of changing some options
and trying again, e.g. changing the user name or password for a remote
capture.)

tcpdump.c

index a44baa29ccadd709ff3e4960779e133f9a14dfbc..dfa340b6113777cdbd99810c4f2527df7f2de223 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -1347,8 +1347,6 @@ open_interface(const char *device, netdissect_options *ndo, char *ebuf)
                         */
                        snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s\n(%s)",
                            device, pcap_statustostr(status), cp);
-                       pcap_close(pc);
-                       return (NULL);
                } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
                        error("%s: %s\n(%s)", device,
                            pcap_statustostr(status), cp);
@@ -1379,6 +1377,8 @@ open_interface(const char *device, netdissect_options *ndo, char *ebuf)
                else
                        error("%s: %s", device,
                            pcap_statustostr(status));
+               pcap_close(pc);
+               return (NULL);
        } else if (status > 0) {
                /*
                 * pcap_activate() succeeded, but it's warning us