From 2d6e537e609bad84996847ab5ea6b59bf4bc6773 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Fri, 14 Aug 2020 18:29:33 -0700 Subject: [PATCH] Improve error reporting. The return value of pcap_activate() is: 0, if activation succeeded, with no warnings; a non-zero positive number, if activation succeded with a warning - the number is a PCAP_WARNING_ indication of the warning; a negative number, if activation failed - the number is a PCAP_ERROR_ indication of the error. We should not treat non-zero positive numbers as errors. (Printing a warning might be useful.) We should treat non-zero negative numbers as errors, and translate it to an error message using libpcap's own pcap_statustostr(), rather than attempting to duplicate its mapping ourselves, as new error codes may appear in the future, and using pcap_statustostr() future-proofs us against that. In addition, if the error is PCAP_ERROR, we should report the return value of pcap_geterr(handle), as that will provide additional information about that "generic" error. --- src/init.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/init.c b/src/init.c index 587b0d6..c26ec7a 100644 --- a/src/init.c +++ b/src/init.c @@ -143,30 +143,27 @@ pcap_t *capture_init(char *capture_source) pcap_set_timeout(handle, 50); pcap_set_rfmon(handle, activate_rfmon); pcap_set_promisc(handle, 1); - if(!(status = pcap_activate(handle))) + status = pcap_activate(handle); + if(status >= 0) { + // Complete success, or success with warning. + // XXX - report warning? return handle; + } if(status == PCAP_ERROR_RFMON_NOTSUP) { pcap_set_rfmon(handle, 0); status = pcap_activate(handle); - if(!status) return handle; + if(status >= 0) { + // Complete success, or success with warning. + // XXX - report warning? + return handle; + } + } + if(status < 0) { + if(status == PCAP_ERROR) + cprintf(CRITICAL, "[X] ERROR: pcap_activate status %d - %s, %s\n", status, pcap_statustostr(status), pcap_geterr(handle)); + else + cprintf(CRITICAL, "[X] ERROR: pcap_activate status %d - %s\n", status, pcap_statustostr(status)); } - cprintf(CRITICAL, "[X] ERROR: pcap_activate status %d\n", status); - static const char *pcap_errmsg[] = { - [1] = "generic error code", - [2] = "loop terminated by pcap_breakloop", - [3] = "the capture needs to be activated", - [4] = "the operation can't be performed on already activated captures", - [5] = "no such device exists", - [6] = "this device doesn't support rfmon (monitor) mode", - [7] = "operation supported only in monitor mode", - [8] = "no permission to open the device", - [9] = "interface isn't up", - [10]= "this device doesn't support setting the time stamp type", - [11]= "you don't have permission to capture in promiscuous mode", - [12]= "the requested time stamp precision is not supported", - }; - if(status < 0 && status > -13) - cprintf(CRITICAL, "[X] PCAP: %s\n", pcap_errmsg[-status]); pcap_close(handle); handle = 0; }