Move some code to better show how to handle pcap_activate() failure.

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.)
This commit is contained in:
Guy Harris 2019-11-23 19:28:01 -08:00
parent 6f53130273
commit cbd0d78fce

View File

@ -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