diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | Makefile.in | 2 | ||||
-rw-r--r-- | config.h.in | 3 | ||||
-rwxr-xr-x | configure | 2 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | dlpisubs.c | 5 | ||||
-rw-r--r-- | fad-getad.c | 4 | ||||
-rw-r--r-- | fad-gifc.c | 43 | ||||
-rw-r--r-- | fad-glifc.c | 51 | ||||
-rw-r--r-- | fmtutils.c | 129 | ||||
-rw-r--r-- | fmtutils.h | 50 | ||||
-rw-r--r-- | gencode.c | 3 | ||||
-rw-r--r-- | optimize.c | 4 | ||||
-rw-r--r-- | pcap-bpf.c | 285 | ||||
-rw-r--r-- | pcap-bt-linux.c | 50 | ||||
-rw-r--r-- | pcap-bt-monitor-linux.c | 20 | ||||
-rw-r--r-- | pcap-dag.c | 45 | ||||
-rw-r--r-- | pcap-dlpi.c | 83 | ||||
-rw-r--r-- | pcap-int.h | 2 | ||||
-rw-r--r-- | pcap-libdlpi.c | 8 | ||||
-rw-r--r-- | pcap-linux.c | 342 | ||||
-rw-r--r-- | pcap-netfilter-linux.c | 45 | ||||
-rw-r--r-- | pcap-netmap.c | 6 | ||||
-rw-r--r-- | pcap-new.c | 20 | ||||
-rw-r--r-- | pcap-nit.c | 23 | ||||
-rw-r--r-- | pcap-npf.c | 7 | ||||
-rw-r--r-- | pcap-pf.c | 64 | ||||
-rw-r--r-- | pcap-rpcap.c | 31 | ||||
-rw-r--r-- | pcap-septel.c | 4 | ||||
-rw-r--r-- | pcap-sita.c | 41 | ||||
-rw-r--r-- | pcap-snf.c | 49 | ||||
-rw-r--r-- | pcap-snit.c | 64 | ||||
-rw-r--r-- | pcap-snoop.c | 40 | ||||
-rw-r--r-- | pcap-tc.c | 4 | ||||
-rw-r--r-- | pcap-usb-linux.c | 47 | ||||
-rw-r--r-- | pcap.c | 110 | ||||
-rwxr-xr-x | rpcapd/daemon.c | 32 | ||||
-rwxr-xr-x | rpcapd/rpcapd.c | 10 | ||||
-rw-r--r-- | savefile.c | 15 | ||||
-rw-r--r-- | sf-pcap-ng.c | 15 | ||||
-rw-r--r-- | sf-pcap.c | 52 |
41 files changed, 1043 insertions, 772 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 29486df4..11da3374 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -327,6 +327,8 @@ endif() # Functions. # check_function_exists(strerror HAVE_STRERROR) +check_function_exists(strerror_r HAVE_STRERROR_R) +check_function_exists(strerror_s HAVE_STRERROR_S) check_function_exists(strlcpy HAVE_STRLCPY) check_function_exists(strlcat HAVE_STRLCAT) check_function_exists(snprintf HAVE_SNPRINTF) @@ -552,6 +554,7 @@ set(PROJECT_SOURCE_LIST_C bpf_dump.c bpf_image.c etherent.c + fmtutils.c fopen.c gencode.c nametoaddr.c diff --git a/Makefile.in b/Makefile.in index f6b8b56f..136e84e0 100644 --- a/Makefile.in +++ b/Makefile.in @@ -89,6 +89,7 @@ PSRC = pcap-@V_PCAP@.c @USB_SRC@ @BT_SRC@ @BT_MONITOR_SRC@ @NETFILTER_SRC@ @DBUS FSRC = @V_FINDALLDEVS@ SSRC = @SSRC@ CSRC = pcap.c gencode.c optimize.c nametoaddr.c etherent.c \ + fmtutils.c \ savefile.c sf-pcap.c sf-pcap-ng.c fopen.c pcap-common.c \ bpf_image.c bpf_dump.c GENSRC = scanner.c grammar.c bpf_filter.c @@ -123,6 +124,7 @@ HDR = $(PUBHDR) \ atmuni31.h \ ethertype.h \ extract.h \ + fmtutils.h \ fopen.h \ ftmacros.h \ gencode.h \ diff --git a/config.h.in b/config.h.in index 277918ff..a5413c40 100644 --- a/config.h.in +++ b/config.h.in @@ -168,6 +168,9 @@ /* Define to 1 if you have the `strerror' function. */ #undef HAVE_STRERROR +/* Define to 1 if you have the `strerror_r' function. */ +#undef HAVE_STRERROR_R + /* Define to 1 if you have the <strings.h> header file. */ #undef HAVE_STRINGS_H @@ -4736,7 +4736,7 @@ $as_echo "$ac_cv_lbl_gcc_fixincludes" >&6; } fi fi -for ac_func in strerror strlcpy strlcat +for ac_func in strerror strerror_r strlcpy strlcat do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/configure.ac b/configure.ac index 1f608ee2..a20f376a 100644 --- a/configure.ac +++ b/configure.ac @@ -83,7 +83,7 @@ esac AC_LBL_FIXINCLUDES -AC_CHECK_FUNCS(strerror strlcpy strlcat) +AC_CHECK_FUNCS(strerror strerror_r strlcpy strlcat) needsnprintf=no AC_CHECK_FUNCS(vsnprintf snprintf,, @@ -358,7 +358,8 @@ pcap_alloc_databuf(pcap_t *p) p->bufsize = PKTBUFSIZE; p->buffer = malloc(p->bufsize + p->offset); if (p->buffer == NULL) { - strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, ""); return (-1); } @@ -392,6 +393,6 @@ strioctl(int fd, int cmd, int len, char *dp) static void pcap_stream_err(const char *func, int err, char *errbuf) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", func, pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, err, "%s", func); } #endif diff --git a/fad-getad.c b/fad-getad.c index c2c3e43d..75797450 100644 --- a/fad-getad.c +++ b/fad-getad.c @@ -168,8 +168,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, * those. */ if (getifaddrs(&ifap) != 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "getifaddrs: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "getifaddrs"); return (-1); } for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { @@ -160,8 +160,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, */ fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); return (-1); } @@ -186,8 +186,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, } buf = malloc(buf_size); if (buf == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); (void)close(fd); return (-1); } @@ -197,8 +197,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, memset(buf, 0, buf_size); if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 && errno != EINVAL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFCONF: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFCONF"); (void)close(fd); free(buf); return (-1); @@ -270,11 +270,10 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) { if (errno == ENXIO) continue; - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFFLAGS: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFFLAGS: %.*s", (int)sizeof(ifrflags.ifr_name), - ifrflags.ifr_name, - pcap_strerror(errno)); + ifrflags.ifr_name); ret = -1; break; } @@ -294,11 +293,11 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, netmask = NULL; netmask_size = 0; } else { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFNETMASK: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCGIFNETMASK: %.*s", (int)sizeof(ifrnetmask.ifr_name), - ifrnetmask.ifr_name, - pcap_strerror(errno)); + ifrnetmask.ifr_name); ret = -1; break; } @@ -325,11 +324,11 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, broadaddr = NULL; broadaddr_size = 0; } else { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFBRDADDR: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCGIFBRDADDR: %.*s", (int)sizeof(ifrbroadaddr.ifr_name), - ifrbroadaddr.ifr_name, - pcap_strerror(errno)); + ifrbroadaddr.ifr_name); ret = -1; break; } @@ -364,11 +363,11 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, dstaddr = NULL; dstaddr_size = 0; } else { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFDSTADDR: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCGIFDSTADDR: %.*s", (int)sizeof(ifrdstaddr.ifr_name), - ifrdstaddr.ifr_name, - pcap_strerror(errno)); + ifrdstaddr.ifr_name); ret = -1; break; } diff --git a/fad-glifc.c b/fad-glifc.c index e89279e9..1dbd1c91 100644 --- a/fad-glifc.c +++ b/fad-glifc.c @@ -97,8 +97,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, */ fd4 = socket(AF_INET, SOCK_DGRAM, 0); if (fd4 < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "socket: AF_INET"); return (-1); } @@ -107,8 +107,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, */ fd6 = socket(AF_INET6, SOCK_DGRAM, 0); if (fd6 < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "socket: AF_INET6"); (void)close(fd4); return (-1); } @@ -120,8 +120,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, ifn.lifn_flags = 0; ifn.lifn_count = 0; if (ioctl(fd4, SIOCGLIFNUM, (char *)&ifn) < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGLIFNUM: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGLIFNUM"); (void)close(fd6); (void)close(fd4); return (-1); @@ -133,8 +133,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, buf_size = ifn.lifn_count * sizeof (struct lifreq); buf = malloc(buf_size); if (buf == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); (void)close(fd6); (void)close(fd4); return (-1); @@ -149,8 +149,8 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, ifc.lifc_flags = 0; memset(buf, 0, buf_size); if (ioctl(fd4, SIOCGLIFCONF, (char *)&ifc) < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGLIFCONF: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGLIFCONF"); (void)close(fd6); (void)close(fd4); free(buf); @@ -198,11 +198,10 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, if (ioctl(fd, SIOCGLIFFLAGS, (char *)&ifrflags) < 0) { if (errno == ENXIO) continue; - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGLIFFLAGS: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGLIFFLAGS: %.*s", (int)sizeof(ifrflags.lifr_name), - ifrflags.lifr_name, - pcap_strerror(errno)); + ifrflags.lifr_name); ret = -1; break; } @@ -221,11 +220,11 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, */ netmask = NULL; } else { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGLIFNETMASK: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCGLIFNETMASK: %.*s", (int)sizeof(ifrnetmask.lifr_name), - ifrnetmask.lifr_name, - pcap_strerror(errno)); + ifrnetmask.lifr_name); ret = -1; break; } @@ -249,11 +248,11 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, */ broadaddr = NULL; } else { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGLIFBRDADDR: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCGLIFBRDADDR: %.*s", (int)sizeof(ifrbroadaddr.lifr_name), - ifrbroadaddr.lifr_name, - pcap_strerror(errno)); + ifrbroadaddr.lifr_name); ret = -1; break; } @@ -284,11 +283,11 @@ pcap_findalldevs_interfaces(pcap_if_list_t *devlistp, char *errbuf, */ dstaddr = NULL; } else { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGLIFDSTADDR: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCGLIFDSTADDR: %.*s", (int)sizeof(ifrdstaddr.lifr_name), - ifrdstaddr.lifr_name, - pcap_strerror(errno)); + ifrdstaddr.lifr_name); ret = -1; break; } diff --git a/fmtutils.c b/fmtutils.c new file mode 100644 index 00000000..9f1acab6 --- /dev/null +++ b/fmtutils.c @@ -0,0 +1,129 @@ +/* + * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the Computer Systems + * Engineering Group at Lawrence Berkeley Laboratory. + * 4. Neither the name of the University nor of the Laboratory may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Utilities for message formatting used both by libpcap and rpcapd. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stddef.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include <pcap/pcap.h> + +#include "portability.h" + +#include "fmtutils.h" + +/* + * Generate an error message based on a format, arguments, and an + * errno, with a message for the errno after the formatted output. + */ +void +pcap_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum, + const char *fmt, ...) +{ + va_list ap; + size_t msglen; + char *p; + size_t errbuflen_remaining; +#if defined(HAVE_STRERROR_S) + errno_t err; +#elif defined(HAVE_STRERROR_R) + int err; +#endif + + va_start(ap, fmt); + pcap_vsnprintf(errbuf, errbuflen, fmt, ap); + va_end(ap); + msglen = strlen(errbuf); + + /* + * Do we have enough space to append ": "? + * Including the terminating '\0', that's 3 bytes. + */ + if (msglen + 3 > errbuflen) { + /* No - just give them what we've produced. */ + return; + } + p = errbuf + msglen; + errbuflen_remaining = errbuflen - msglen; + *p++ = ':'; + *p++ = ' '; + *p = '\0'; + msglen += 2; + errbuflen_remaining -= 2; + + /* + * Now append the string for the error code. + */ +#if defined(HAVE_STRERROR_S) + err = strerror_s(p, errbuflen_remaining, errnum); + if (err != 0) { + /* + * It doesn't appear to be documented anywhere obvious + * what the error returns from strerror_s(). + */ + pcap_snprintf(p, errbuflen_remaining, "Error %d", errnum); + } +#elif defined(HAVE_STRERROR_R) + err = strerror_r(errnum, p, errbuflen_remaining); + if (err == EINVAL) { + /* + * UNIX 03 says this isn't guaranteed to produce a + * fallback error message. + */ + pcap_snprintf(p, errbuflen_remaining, "Unknown error: %d", + errnum); + } else if (err == ERANGE) { + /* + * UNIX 03 says this isn't guaranteed to produce a + * fallback error message. + */ + pcap_snprintf(p, errbuflen_remaining, + "Message for error %d is too long", errnum); + } +#else + /* + * We have neither strerror_s() nor strerror_r(), so we're + * stuck with using pcap_strerror(). + */ + pcap_snprintf(p, errbuflen_remaining, "%s", pcap_strerror(errnum)); +#endif +} diff --git a/fmtutils.h b/fmtutils.h new file mode 100644 index 00000000..62c78fdb --- /dev/null +++ b/fmtutils.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 1994, 1995, 1996 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the Computer Systems + * Engineering Group at Lawrence Berkeley Laboratory. + * 4. Neither the name of the University nor of the Laboratory may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef fmtutils_h +#define fmtutils_h + +#include "pcap/funcattrs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void pcap_fmt_errmsg_for_errno(char *, size_t, int, + PCAP_FORMAT_STRING(const char *), ...) PCAP_PRINTFLIKE(4, 5); + +#ifdef __cplusplus +} +#endif + +#endif @@ -734,7 +734,8 @@ pcap_compile(pcap_t *p, struct bpf_program *program, } if (pcap_lex_init(&scanner) != 0) - bpf_error(&cstate, "can't initialize scanner: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't initialize scanner"); in_buffer = pcap__scan_string(xbuf ? xbuf : "", scanner); /* @@ -2304,8 +2304,8 @@ install_bpf_program(pcap_t *p, struct bpf_program *fp) p->fcode.bf_len = fp->bf_len; p->fcode.bf_insns = (struct bpf_insn *)malloc(prog_size); if (p->fcode.bf_insns == NULL) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "malloc"); return (-1); } memcpy(p->fcode.bf_insns, fp->bf_insns, prog_size); @@ -368,8 +368,8 @@ pcap_next_zbuf(pcap_t *p, int *cc) if (data) return (data); if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { - (void) pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCROTZBUF: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, "BIOCROTZBUF"); return (PCAP_ERROR); } return (pcap_next_zbuf_shm(p, cc)); @@ -396,8 +396,8 @@ pcap_next_zbuf(pcap_t *p, int *cc) } return (0); } else if (r < 0) { - (void) pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "select: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "select"); return (PCAP_ERROR); } } @@ -415,8 +415,8 @@ pcap_next_zbuf(pcap_t *p, int *cc) * data. */ if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { - (void) pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCROTZBUF: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCROTZBUF"); return (PCAP_ERROR); } return (pcap_next_zbuf_shm(p, cc)); @@ -458,8 +458,8 @@ pcap_create_interface(const char *device _U_, char *ebuf) p->tstamp_precision_count = 2; p->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (p->tstamp_precision_list == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, + "malloc"); free(p); return (NULL); } @@ -507,9 +507,8 @@ bpf_open(char *errbuf) fd = PCAP_ERROR_PERM_DENIED; else fd = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "(cannot open device) %s: %s", cloning_device, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "(cannot open device) %s", cloning_device); return (fd); } no_cloning_bpf = 1; @@ -578,9 +577,8 @@ bpf_open(char *errbuf) * if any. */ fd = PCAP_ERROR_PERM_DENIED; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "(cannot open BPF device) %s: %s", device, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "(cannot open BPF device) %s", device); break; default: @@ -588,9 +586,8 @@ bpf_open(char *errbuf) * Some other problem. */ fd = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "(cannot open BPF device) %s: %s", device, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "(cannot open BPF device) %s", device); break; } } @@ -645,8 +642,8 @@ bpf_open_and_bind(const char *name, char *errbuf) return (PCAP_ERROR_IFACE_NOT_UP); default: - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "BIOCSETIF: %s: %s", name, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETIF: %s", name); close(fd); return (PCAP_ERROR); } @@ -669,14 +666,14 @@ get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf) bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1)); if (bdlp->bfl_list == NULL) { - (void)pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (PCAP_ERROR); } if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) { - (void)pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "BIOCGDLTLIST: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "BIOCGDLTLIST"); free(bdlp->bfl_list); return (PCAP_ERROR); } @@ -730,8 +727,8 @@ get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf) * this device"; don't treat it as an error. */ if (errno != EINVAL) { - (void)pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "BIOCGDLTLIST: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "BIOCGDLTLIST"); return (PCAP_ERROR); } } @@ -797,8 +794,8 @@ pcap_can_set_rfmon_bpf(pcap_t *p) } fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) { - (void)pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); return (PCAP_ERROR); } strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name)); @@ -852,9 +849,8 @@ pcap_can_set_rfmon_bpf(pcap_t *p) return (PCAP_ERROR_IFACE_NOT_UP); default: - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCSETIF: %s: %s", - p->opt.device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETIF: %s", p->opt.device); close(fd); return (PCAP_ERROR); } @@ -916,8 +912,8 @@ pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps) * by libpcap, and thus not yet seen by the application. */ if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCGSTATS"); return (PCAP_ERROR); } @@ -1044,8 +1040,8 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) /* fall through */ #endif } - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "read"); return (PCAP_ERROR); } bp = (u_char *)p->buffer; @@ -1218,9 +1214,8 @@ pcap_inject_bpf(pcap_t *p, const void *buf, size_t size) u_int spoof_eth_src = 0; if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { - (void)pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "send: can't turn off BIOCSHDRCMPLT: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send: can't turn off BIOCSHDRCMPLT"); return (PCAP_ERROR); } @@ -1231,8 +1226,8 @@ pcap_inject_bpf(pcap_t *p, const void *buf, size_t size) } #endif /* __APPLE__ */ if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (PCAP_ERROR); } return (ret); @@ -1320,8 +1315,8 @@ bpf_load(char *errbuf) major = genmajor(BPF_NAME); if (major == -1) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "bpf_load: genmajor failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "bpf_load: genmajor failed"); (void)bpf_odmcleanup(NULL); return (PCAP_ERROR); } @@ -1330,9 +1325,8 @@ bpf_load(char *errbuf) if (!minors) { minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1); if (!minors) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "bpf_load: genminor failed: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "bpf_load: genminor failed"); (void)bpf_odmcleanup(NULL); return (PCAP_ERROR); } @@ -1343,9 +1337,8 @@ bpf_load(char *errbuf) rc = stat(BPF_NODE "0", &sbuf); if (rc == -1 && errno != ENOENT) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "bpf_load: can't stat %s: %s", - BPF_NODE "0", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "bpf_load: can't stat %s", BPF_NODE "0"); return (PCAP_ERROR); } @@ -1354,9 +1347,9 @@ bpf_load(char *errbuf) sprintf(buf, "%s%d", BPF_NODE, i); unlink(buf); if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "bpf_load: can't mknod %s: %s", - buf, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "bpf_load: can't mknod %s", buf); return (PCAP_ERROR); } } @@ -1370,9 +1363,8 @@ bpf_load(char *errbuf) (cfg_ld.kmid == 0)) { /* Driver isn't loaded, load it now */ if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "bpf_load: could not load driver: %s", - strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "bpf_load: could not load driver"); return (PCAP_ERROR); } } @@ -1385,9 +1377,8 @@ bpf_load(char *errbuf) for (i = 0; i < BPF_MINORS; i++) { cfg_bpf.devno = domakedev(major, i); if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "bpf_load: could not configure driver: %s", - strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "bpf_load: could not configure driver"); return (PCAP_ERROR); } } @@ -1551,9 +1542,10 @@ check_setif_failure(pcap_t *p, int error) * exist. */ err = PCAP_ERROR_NO_SUCH_DEVICE; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFFLAGS on %s failed: %s", - ifr.ifr_name, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCGIFFLAGS on %s failed", + ifr.ifr_name); } else { /* * The underlying "enN" device @@ -1575,9 +1567,9 @@ check_setif_failure(pcap_t *p, int error) * just report "no such device". */ err = PCAP_ERROR_NO_SUCH_DEVICE; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "socket() failed: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + errno, PCAP_ERRBUF_SIZE, + "socket() failed"); } return (err); } @@ -1585,8 +1577,8 @@ check_setif_failure(pcap_t *p, int error) /* * No such device. */ - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF failed: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETIF failed"); return (PCAP_ERROR_NO_SUCH_DEVICE); } else if (errno == ENETDOWN) { /* @@ -1602,8 +1594,8 @@ check_setif_failure(pcap_t *p, int error) * Some other error; fill in the error string, and * return PCAP_ERROR. */ - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", - p->opt.device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETIF: %s", p->opt.device); return (PCAP_ERROR); } } @@ -1675,8 +1667,8 @@ pcap_activate_bpf(pcap_t *p) p->fd = fd; if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCVERSION"); status = PCAP_ERROR; goto bad; } @@ -1704,8 +1696,8 @@ pcap_activate_bpf(pcap_t *p) * Retrieve the zoneid of the zone we are currently executing in. */ if ((ifr.lifr_zoneid = getzoneid()) == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "getzoneid(): %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "getzoneid()"); status = PCAP_ERROR; goto bad; } @@ -1731,16 +1723,15 @@ pcap_activate_bpf(pcap_t *p) (void) strlcpy(path_zname, p->opt.device, znamelen + 1); ifr.lifr_zoneid = getzoneidbyname(path_zname); if (ifr.lifr_zoneid == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "getzoneidbyname(%s): %s", path_zname, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "getzoneidbyname(%s)", path_zname); status = PCAP_ERROR; goto bad; } lnamep = strdup(zonesep + 1); if (lnamep == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "strdup"); status = PCAP_ERROR; goto bad; } @@ -1751,8 +1742,8 @@ pcap_activate_bpf(pcap_t *p) pb->device = strdup(p->opt.device); if (pb->device == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "strdup"); status = PCAP_ERROR; goto bad; } @@ -1806,10 +1797,10 @@ pcap_activate_bpf(pcap_t *p) * exist. */ status = PCAP_ERROR_NO_SUCH_DEVICE; - pcap_snprintf(p->errbuf, + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFFLAGS failed: %s", - pcap_strerror(errno)); + errno, + "SIOCGIFFLAGS failed"); } else status = PCAP_ERROR_RFMON_NOTSUP; close(sockfd); @@ -1820,18 +1811,17 @@ pcap_activate_bpf(pcap_t *p) * report "no such device". */ status = PCAP_ERROR_NO_SUCH_DEVICE; - pcap_snprintf(p->errbuf, - PCAP_ERRBUF_SIZE, - "socket() failed: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "socket() failed"); } goto bad; } wltdev = malloc(strlen(p->opt.device) + 2); if (wltdev == NULL) { - (void)pcap_snprintf(p->errbuf, - PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); status = PCAP_ERROR; goto bad; } @@ -1871,9 +1861,9 @@ pcap_activate_bpf(pcap_t *p) */ s = socket(AF_LOCAL, SOCK_DGRAM, 0); if (s < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "Can't open socket: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errnoB(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Can't open socket"); status = PCAP_ERROR; goto bad; } @@ -1904,9 +1894,10 @@ pcap_activate_bpf(pcap_t *p) "Invalid USB bus interface %s", p->opt.device); } else { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "Can't create interface for %s: %s", - p->opt.device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Can't create interface for %s", + p->opt.device); } close(s); status = PCAP_ERROR; @@ -1949,8 +1940,8 @@ pcap_activate_bpf(pcap_t *p) * size. */ if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGETZMAX: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCGETZMAX"); status = PCAP_ERROR; goto bad; } @@ -1976,8 +1967,8 @@ pcap_activate_bpf(pcap_t *p) pb->zbuf2 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); if (pb->zbuf1 == MAP_FAILED || pb->zbuf2 == MAP_FAILED) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "mmap: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "mmap"); status = PCAP_ERROR; goto bad; } @@ -1986,15 +1977,15 @@ pcap_activate_bpf(pcap_t *p) bz.bz_bufb = pb->zbuf2; bz.bz_buflen = pb->zbufsize; if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETZBUF: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETZBUF"); status = PCAP_ERROR; goto bad; } (void)strncpy(ifrname, p->opt.device, ifnamsiz); if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", - p->opt.device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETIF: %s", p->opt.device); status = PCAP_ERROR; goto bad; } @@ -2012,9 +2003,9 @@ pcap_activate_bpf(pcap_t *p) */ if (ioctl(fd, BIOCSBLEN, (caddr_t)&p->opt.buffer_size) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCSBLEN: %s: %s", p->opt.device, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "BIOCSBLEN: %s", p->opt.device); status = PCAP_ERROR; goto bad; } @@ -2081,8 +2072,8 @@ pcap_activate_bpf(pcap_t *p) /* Get the data link layer type. */ if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCGDLT"); status = PCAP_ERROR; goto bad; } @@ -2326,8 +2317,8 @@ pcap_activate_bpf(pcap_t *p) * BSDs - check CVS log for "bpf.c"? */ if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { - (void)pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCSHDRCMPLT: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSHDRCMPLT"); status = PCAP_ERROR; goto bad; } @@ -2368,8 +2359,8 @@ pcap_activate_bpf(pcap_t *p) bpf_to.tv_sec = p->opt.timeout / 1000; bpf_to.tv_usec = (p->opt.timeout * 1000) % 1000000; if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCSRTIMEOUT: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT"); status = PCAP_ERROR; goto bad; } @@ -2378,8 +2369,8 @@ pcap_activate_bpf(pcap_t *p) to.tv_sec = p->opt.timeout / 1000; to.tv_usec = (p->opt.timeout * 1000) % 1000000; if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCSRTIMEOUT: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT"); status = PCAP_ERROR; goto bad; } @@ -2413,8 +2404,8 @@ pcap_activate_bpf(pcap_t *p) #endif /* _AIX */ v = 1; if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "BIOCIMMEDIATE: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCIMMEDIATE"); status = PCAP_ERROR; goto bad; } @@ -2435,8 +2426,8 @@ pcap_activate_bpf(pcap_t *p) if (p->opt.promisc) { /* set promiscuous mode, just warn if it fails */ if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCPROMISC"); status = PCAP_WARNING_PROMISC_NOTSUP; } } @@ -2444,16 +2435,16 @@ pcap_activate_bpf(pcap_t *p) #ifdef BIOCSTSTAMP v = BPF_T_BINTIME; if (ioctl(p->fd, BIOCSTSTAMP, &v) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSTSTAMP: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSTSTAMP"); status = PCAP_ERROR; goto bad; } #endif /* BIOCSTSTAMP */ if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCGBLEN"); status = PCAP_ERROR; goto bad; } @@ -2463,8 +2454,8 @@ pcap_activate_bpf(pcap_t *p) #endif p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); status = PCAP_ERROR; goto bad; } @@ -2494,8 +2485,8 @@ pcap_activate_bpf(pcap_t *p) total_prog.bf_len = 1; total_prog.bf_insns = &total_insn; if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETF"); status = PCAP_ERROR; goto bad; } @@ -2605,8 +2596,8 @@ check_bpf_bindable(const char *name) en_name_len = strlen(name) - 1; en_name = malloc(en_name_len + 1); if (en_name == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (-1); } strcpy(en_name, "en"); @@ -2754,8 +2745,8 @@ monitor_mode(pcap_t *p, int set) sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't open socket: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't open socket"); return (PCAP_ERROR); } @@ -2786,8 +2777,8 @@ monitor_mode(pcap_t *p, int set) return (PCAP_ERROR_RFMON_NOTSUP); default: - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFMEDIA 1: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFMEDIA 1"); close(sock); return (PCAP_ERROR); } @@ -2806,15 +2797,15 @@ monitor_mode(pcap_t *p, int set) */ media_list = malloc(req.ifm_count * sizeof(*media_list)); if (media_list == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); close(sock); return (PCAP_ERROR); } req.ifm_ulist = media_list; if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMEDIA: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFMEDIA"); free(media_list); close(sock); return (PCAP_ERROR); @@ -2874,8 +2865,8 @@ monitor_mode(pcap_t *p, int set) sizeof(ifr.ifr_name)); ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR; if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "SIOCSIFMEDIA: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, "SIOCSIFMEDIA"); close(sock); return (PCAP_ERROR); } @@ -3099,8 +3090,8 @@ pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp) * some kernels. */ if (errno != EINVAL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "BIOCSETF"); return (-1); } @@ -3128,11 +3119,10 @@ pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d) direction = (d == PCAP_D_IN) ? BPF_D_IN : ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT); if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) { - (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "Cannot set direction to %s: %s", + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "Cannot set direction to %s", (d == PCAP_D_IN) ? "PCAP_D_IN" : - ((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"), - strerror(errno)); + ((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT")); return (-1); } return (0); @@ -3150,10 +3140,9 @@ pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d) seesent = (d == PCAP_D_INOUT); if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) { - (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "Cannot set direction to %s: %s", - (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN", - strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "Cannot set direction to %s", + (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN"); return (-1); } return (0); @@ -3169,8 +3158,8 @@ pcap_set_datalink_bpf(pcap_t *p, int dlt) { #ifdef BIOCSDLT if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) { - (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "Cannot set DLT %d: %s", dlt, strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "Cannot set DLT %d", dlt); return (-1); } #endif diff --git a/pcap-bt-linux.c b/pcap-bt-linux.c index 2486bf23..1b82110b 100644 --- a/pcap-bt-linux.c +++ b/pcap-bt-linux.c @@ -40,10 +40,6 @@ #include "pcap-bt-linux.h" #include "pcap/bluetooth.h" -#ifdef NEED_STRERROR_H -#include "strerror.h" -#endif - #include <errno.h> #include <stdlib.h> #include <unistd.h> @@ -87,8 +83,8 @@ bt_findalldevs(pcap_if_list_t *devlistp, char *err_str) /* if bluetooth is not supported this this is not fatal*/ if (errno == EAFNOSUPPORT) return 0; - pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, - "Can't open raw Bluetooth socket: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, + errno, "Can't open raw Bluetooth socket"); return -1; } @@ -105,9 +101,8 @@ bt_findalldevs(pcap_if_list_t *devlistp, char *err_str) if (ioctl(sock, HCIGETDEVLIST, (void *) dev_list) < 0) { - pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, - "Can't get Bluetooth device list via ioctl: %s", - strerror(errno)); + pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, + errno, "Can't get Bluetooth device list via ioctl"); ret = -1; goto free; } @@ -225,29 +220,29 @@ bt_activate(pcap_t* handle) /* Create HCI socket */ handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (handle->fd < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't create raw socket: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't create raw socket"); return PCAP_ERROR; } handle->buffer = malloc(handle->bufsize); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't allocate dump buffer"); goto close_fail; } opt = 1; if (setsockopt(handle->fd, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't enable data direction info: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't enable data direction info"); goto close_fail; } opt = 1; if (setsockopt(handle->fd, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't enable time stamp: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't enable time stamp"); goto close_fail; } @@ -257,8 +252,8 @@ bt_activate(pcap_t* handle) memset((void *) &flt.type_mask, 0xff, sizeof(flt.type_mask)); memset((void *) &flt.event_mask, 0xff, sizeof(flt.event_mask)); if (setsockopt(handle->fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't set filter: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't set filter"); goto close_fail; } @@ -270,9 +265,8 @@ bt_activate(pcap_t* handle) addr.hci_channel = HCI_CHANNEL_RAW; #endif if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't attach to device %d: %s", handlep->dev_id, - strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't attach to device %d", handlep->dev_id); goto close_fail; } @@ -291,8 +285,8 @@ bt_activate(pcap_t* handle) if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SO_RCVBUF: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + errno, PCAP_ERRBUF_SIZE, "SO_RCVBUF"); goto close_fail; } } @@ -339,8 +333,8 @@ bt_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *us } while ((ret == -1) && (errno == EINTR)); if (ret < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't receive packet: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't receive packet"); return -1; } @@ -399,8 +393,8 @@ bt_stats_linux(pcap_t *handle, struct pcap_stat *stats) } while ((ret == -1) && (errno == EINTR)); if (ret < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't get stats via ioctl: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't get stats via ioctl"); return (-1); } diff --git a/pcap-bt-monitor-linux.c b/pcap-bt-monitor-linux.c index 8dd9b42e..cee34bcd 100644 --- a/pcap-bt-monitor-linux.c +++ b/pcap-bt-monitor-linux.c @@ -110,8 +110,8 @@ bt_monitor_read(pcap_t *handle, int max_packets _U_, pcap_handler callback, u_ch } while ((ret == -1) && (errno == EINTR)); if (ret < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't receive packet: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't receive packet"); return -1; } @@ -198,15 +198,15 @@ bt_monitor_activate(pcap_t* handle) handle->fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (handle->fd < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't create raw socket: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't create raw socket"); return PCAP_ERROR; } handle->buffer = malloc(handle->bufsize); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't allocate dump buffer"); goto close_fail; } @@ -216,15 +216,15 @@ bt_monitor_activate(pcap_t* handle) addr.hci_channel = HCI_CHANNEL_MONITOR; if (bind(handle->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't attach to interface: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't attach to interface"); goto close_fail; } opt = 1; if (setsockopt(handle->fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't enable time stamp: %s", strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't enable time stamp"); goto close_fail; } @@ -749,20 +749,22 @@ static int dag_activate(pcap_t* p) struct timeval poll; if (device == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno)); + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL"); return -1; } /* Initialize some components of the pcap structure. */ newDev = (char *)malloc(strlen(device) + 16); if (newDev == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't allocate string for device name"); goto fail; } /* Parse input name to get dag device and stream number if provided */ if (dag_parse_name(device, newDev, strlen(device) + 16, &pd->dag_stream) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_parse_name"); goto fail; } device = newDev; @@ -774,13 +776,15 @@ static int dag_activate(pcap_t* p) /* setup device parameters */ if((p->fd = dag_open((char *)device)) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_open %s", device); goto fail; } /* Open requested stream. Can fail if already locked or on error */ if (dag_attach_stream64(p->fd, pd->dag_stream, 0, 0) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_attach_stream"); goto failclose; } @@ -789,7 +793,8 @@ static int dag_activate(pcap_t* p) */ if (dag_get_stream_poll64(p->fd, pd->dag_stream, &mindata, &maxwait, &poll) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_get_stream_poll"); goto faildetach; } @@ -825,7 +830,8 @@ static int dag_activate(pcap_t* p) if (dag_set_stream_poll64(p->fd, pd->dag_stream, mindata, &maxwait, &poll) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_set_stream_poll"); goto faildetach; } @@ -846,7 +852,8 @@ static int dag_activate(pcap_t* p) #endif if(dag_start_stream(p->fd, pd->dag_stream) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s", device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_start_stream %s", device); goto faildetach; } @@ -908,7 +915,8 @@ static int dag_activate(pcap_t* p) p->bufsize = 0; if (new_pcap_dag(p) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s", device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "new_pcap_dag %s", device); goto failstop; } @@ -1021,8 +1029,8 @@ pcap_t *dag_create(const char *device, char *ebuf, int *is_ours) p->tstamp_precision_count = 2; p->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (p->tstamp_precision_list == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); pcap_close(p); return NULL; } @@ -1158,7 +1166,8 @@ dag_setnonblock(pcap_t *p, int nonblock) if (dag_get_stream_poll64(p->fd, pd->dag_stream, &mindata, &maxwait, &poll) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_get_stream_poll"); return -1; } @@ -1173,7 +1182,8 @@ dag_setnonblock(pcap_t *p, int nonblock) if (dag_set_stream_poll64(p->fd, pd->dag_stream, mindata, &maxwait, &poll) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "dag_set_stream_poll"); return -1; } @@ -1195,7 +1205,8 @@ dag_get_datalink(pcap_t *p) memset(types, 0, 255); if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) { - (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "malloc"); return (-1); } @@ -1204,7 +1215,8 @@ dag_get_datalink(pcap_t *p) #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES /* Get list of possible ERF types for this card */ if (dag_get_stream_erf_types(p->fd, pd->dag_stream, types, 255) < 0) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_stream_erf_types: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "dag_get_stream_erf_types"); return (-1); } @@ -1213,7 +1225,8 @@ dag_get_datalink(pcap_t *p) #elif defined HAVE_DAG_GET_ERF_TYPES /* Get list of possible ERF types for this card */ if (dag_get_erf_types(p->fd, types, 255) < 0) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "dag_get_erf_types"); return (-1); } diff --git a/pcap-dlpi.c b/pcap-dlpi.c index 619e70e2..60612d8e 100644 --- a/pcap-dlpi.c +++ b/pcap-dlpi.c @@ -238,8 +238,8 @@ pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user) case EAGAIN: return (0); } - strlcpy(p->errbuf, pcap_strerror(errno), - sizeof(p->errbuf)); + pcap_fmt_errmsg_for_errno(p->errbuf, + sizeof(p->errbuf), errno, "getmsg"); return (-1); } cc = data.len; @@ -262,8 +262,8 @@ pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size) #if defined(DLIOCRAW) ret = write(p->fd, buf, size); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } #elif defined(DL_HP_RAWDLS) @@ -274,8 +274,8 @@ pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size) } ret = dlrawdatareq(pd->send_fd, buf, size); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } /* @@ -393,8 +393,8 @@ open_dlpi_device(const char *name, u_int *ppa, char *errbuf) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "%s: %s", cp, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "%s", cp); return (status); } @@ -442,8 +442,8 @@ open_dlpi_device(const char *name, u_int *ppa, char *errbuf) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "%s", dname); return (status); } @@ -482,8 +482,8 @@ open_dlpi_device(const char *name, u_int *ppa, char *errbuf) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", - dname2, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "%s", dname2); } return (status); } @@ -666,8 +666,8 @@ pcap_activate_dlpi(pcap_t *p) */ if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) { status = PCAP_ERROR; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "A_PROMISCON_REQ: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "A_PROMISCON_REQ"); goto bad; } } else @@ -784,8 +784,8 @@ pcap_activate_dlpi(pcap_t *p) */ if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { status = PCAP_ERROR; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "DLIOCRAW"); goto bad; } #endif @@ -825,8 +825,8 @@ pcap_activate_dlpi(pcap_t *p) */ if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { status = PCAP_ERROR; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "FLUSHR"); goto bad; } @@ -1086,8 +1086,8 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) } if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "A_GET_UNITS"); return (-1); } for (i = 0; i < buf.nunits; i++) { @@ -1112,9 +1112,8 @@ send_request(int fd, char *ptr, int len, char *what, char *ebuf) flags = 0; if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "send_request: putmsg \"%s\": %s", - what, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "send_request: putmsg \"%s\"", what); return (-1); } return (0); @@ -1142,8 +1141,8 @@ recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror flags = 0; if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s", - what, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "recv_ack: %s getmsg", what); return (PCAP_ERROR); } @@ -1165,9 +1164,9 @@ recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror case DL_SYSERR: if (uerror != NULL) *uerror = dlp->error_ack.dl_unix_errno; - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "recv_ack: %s: UNIX error - %s", - what, pcap_strerror(dlp->error_ack.dl_unix_errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + dlp->error_ack.dl_unix_errno, + "recv_ack: %s: UNIX error", what); if (dlp->error_ack.dl_unix_errno == EPERM || dlp->error_ack.dl_unix_errno == EACCES) return (PCAP_ERROR_PERM_DENIED); @@ -1618,8 +1617,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, */ /* get the head first */ if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "get_dlpi_ppa: hpppa getmsg"); return (PCAP_ERROR); } if (ctl.len == -1) { @@ -1645,8 +1644,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, /* allocate buffer */ if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "get_dlpi_ppa: hpppa malloc"); return (PCAP_ERROR); } ctl.maxlen = dlp->dl_length; @@ -1654,8 +1653,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, ctl.buf = (char *)ppa_data_buf; /* get the data */ if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "get_dlpi_ppa: hpppa getmsg"); free(ppa_data_buf); return (PCAP_ERROR); } @@ -1724,8 +1723,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, */ pcap_snprintf(dname, sizeof(dname), "/dev/%s%u", device, unit); if (stat(dname, &statbuf) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s", - dname, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "stat: %s", dname); return (PCAP_ERROR); } majdev = major(statbuf.st_rdev); @@ -1797,8 +1796,8 @@ get_dlpi_ppa(register int fd, register const char *ifname, register u_int unit, } kd = open("/dev/kmem", O_RDONLY); if (kd < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "kmem open"); return (PCAP_ERROR); } if (dlpi_kread(kd, nl[NL_IFNET].n_value, @@ -1832,14 +1831,14 @@ dlpi_kread(register int fd, register off_t addr, register int cc; if (lseek(fd, addr, SEEK_SET) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "lseek"); return (-1); } cc = read(fd, buf, len); if (cc < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "read"); return (-1); } else if (cc != len) { pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc, @@ -38,6 +38,8 @@ #include <pcap/pcap.h> +#include "fmtutils.h" + /* * Version string. * Uses PACKAGE_VERSION from config.h. diff --git a/pcap-libdlpi.c b/pcap-libdlpi.c index c013bfc0..99ca9be7 100644 --- a/pcap-libdlpi.c +++ b/pcap-libdlpi.c @@ -220,8 +220,8 @@ pcap_activate_libdlpi(pcap_t *p) */ if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { status = PCAP_ERROR; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "FLUSHR"); goto bad; } @@ -320,8 +320,8 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) dlpi_walk(list_interfaces, &lw, 0); if (lw.lw_err != 0) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "dlpi_walk: %s", pcap_strerror(lw.lw_err)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + lw.lw_err, "dlpi_walk"); retv = -1; goto done; } diff --git a/pcap-linux.c b/pcap-linux.c index 9abe9d29..b1c7ecf4 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -488,8 +488,8 @@ pcap_create_interface(const char *device, char *ebuf) handle->tstamp_precision_count = 2; handle->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (handle->tstamp_precision_list == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); pcap_close(handle); return NULL; } @@ -575,9 +575,8 @@ get_mac80211_phydev(pcap_t *handle, const char *device, char *phydev_path, free(pathstr); return 0; } - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: Can't readlink %s: %s", device, pathstr, - strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "%s: Can't readlink %s", device, pathstr); free(pathstr); return PCAP_ERROR; } @@ -772,8 +771,8 @@ add_mon_if(pcap_t *handle, int sock_fd, struct nl80211_state *state, */ handlep->mondevice = strdup(mondevice); if (handlep->mondevice == NULL) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "strdup"); /* * Get rid of the monitor device. */ @@ -938,9 +937,9 @@ added: memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, handlep->mondevice, sizeof(ifr.ifr_name)); if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: Can't get flags for %s: %s", device, - handlep->mondevice, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "%s: Can't get flags for %s", device, + handlep->mondevice); del_mon_if(handle, sock_fd, &nlstate, device, handlep->mondevice); nl80211_cleanup(&nlstate); @@ -948,9 +947,9 @@ added: } ifr.ifr_flags |= IFF_UP|IFF_RUNNING; if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: Can't set flags for %s: %s", device, - handlep->mondevice, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "%s: Can't set flags for %s", device, + handlep->mondevice); del_mon_if(handle, sock_fd, &nlstate, device, handlep->mondevice); nl80211_cleanup(&nlstate); @@ -1072,8 +1071,8 @@ pcap_can_set_rfmon_linux(pcap_t *handle) */ sock_fd = socket(PF_PACKET, SOCK_RAW, pcap_protocol(handle)); if (sock_fd == -1) { - (void)pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); return PCAP_ERROR; } @@ -1097,8 +1096,8 @@ pcap_can_set_rfmon_linux(pcap_t *handle) } if (errno == ENODEV) { /* The device doesn't even exist. */ - (void)pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIWMODE failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIWMODE failed"); close(sock_fd); return PCAP_ERROR_NO_SUCH_DEVICE; } @@ -1495,8 +1494,8 @@ pcap_activate_linux(pcap_t *handle) handlep->device = strdup(device); if (handlep->device == NULL) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s", - pcap_strerror(errno) ); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "strdup"); return PCAP_ERROR; } @@ -1592,8 +1591,8 @@ pcap_activate_linux(pcap_t *handle) if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SO_RCVBUF: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "SO_RCVBUF"); status = PCAP_ERROR; goto fail; } @@ -1603,8 +1602,8 @@ pcap_activate_linux(pcap_t *handle) handle->buffer = malloc(handle->bufsize + handle->offset); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); status = PCAP_ERROR; goto fail; } @@ -1821,8 +1820,8 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata) return PCAP_ERROR; default: - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "recvfrom: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "recvfrom"); return PCAP_ERROR; } } @@ -1986,16 +1985,16 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata) #if defined(SIOCGSTAMPNS) && defined(SO_TIMESTAMPNS) if (handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) { if (ioctl(handle->fd, SIOCGSTAMPNS, &pcap_header.ts) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SIOCGSTAMPNS: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "SIOCGSTAMPNS"); return PCAP_ERROR; } } else #endif { if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SIOCGSTAMP: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "SIOCGSTAMP"); return PCAP_ERROR; } } @@ -2092,8 +2091,8 @@ pcap_inject_linux(pcap_t *handle, const void *buf, size_t size) ret = send(handle->fd, buf, size, 0); if (ret == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } return (ret); @@ -2214,8 +2213,8 @@ pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats) * is built on a system without "struct tpacket_stats". */ if (errno != EOPNOTSUPP) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "pcap_stats: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "pcap_stats"); return -1; } } @@ -2300,11 +2299,10 @@ add_linux_if(pcap_if_list_t *devlistp, const char *ifname, int fd, char *errbuf) if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifrflags) < 0) { if (errno == ENXIO || errno == ENODEV) return (0); /* device doesn't actually exist - ignore it */ - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFFLAGS: %.*s: %s", + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFFLAGS: %.*s", (int)sizeof(ifrflags.ifr_name), - ifrflags.ifr_name, - pcap_strerror(errno)); + ifrflags.ifr_name); return (-1); } @@ -2360,8 +2358,8 @@ scan_sys_class_net(pcap_if_list_t *devlistp, char *errbuf) /* * Fail if we got some other error. */ - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "Can't open /sys/class/net: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't open /sys/class/net"); return (-1); } @@ -2370,8 +2368,8 @@ scan_sys_class_net(pcap_if_list_t *devlistp, char *errbuf) */ fd = socket(PF_UNIX, SOCK_RAW, 0); if (fd < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); (void)closedir(sys_class_net_d); return (-1); } @@ -2438,9 +2436,8 @@ scan_sys_class_net(pcap_if_list_t *devlistp, char *errbuf) * fail due to an error reading the directory? */ if (errno != 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "Error reading /sys/class/net: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "Error reading /sys/class/net"); ret = -1; } } @@ -2478,8 +2475,8 @@ scan_proc_net_dev(pcap_if_list_t *devlistp, char *errbuf) /* * Fail if we got some other error. */ - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "Can't open /proc/net/dev: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't open /proc/net/dev"); return (-1); } @@ -2489,7 +2486,7 @@ scan_proc_net_dev(pcap_if_list_t *devlistp, char *errbuf) fd = socket(PF_UNIX, SOCK_RAW, 0); if (fd < 0) { (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + errno, "socket"); (void)fclose(proc_net_f); return (-1); } @@ -2527,9 +2524,8 @@ scan_proc_net_dev(pcap_if_list_t *devlistp, char *errbuf) * fail due to an error reading the file? */ if (ferror(proc_net_f)) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "Error reading /proc/net/dev: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "Error reading /proc/net/dev"); ret = -1; } } @@ -2747,9 +2743,9 @@ pcap_setfilter_linux_common(pcap_t *handle, struct bpf_program *filter, */ if (handlep->filter_in_userland) { if (reset_kernel_filter(handle) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't remove kernel filter: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "can't remove kernel filter"); err = -2; /* fatal error */ } } @@ -3377,8 +3373,8 @@ activate_new(pcap_t *handle) return 0; } - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "socket: %s", - pcap_strerror(errno) ); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); if (errno == EPERM || errno == EACCES) { /* * You don't have permission to open the @@ -3479,14 +3475,14 @@ activate_new(pcap_t *handle) * kernels) - reopen in cooked mode. */ if (close(sock_fd) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "close: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "close"); return PCAP_ERROR; } sock_fd = socket(PF_PACKET, SOCK_DGRAM, protocol); if (sock_fd == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "socket"); if (errno == EPERM || errno == EACCES) { /* * You don't have permission to @@ -3610,8 +3606,8 @@ activate_new(pcap_t *handle) mr.mr_type = PACKET_MR_PROMISC; if (setsockopt(sock_fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "setsockopt: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "setsockopt"); close(sock_fd); return PCAP_ERROR; } @@ -3623,8 +3619,8 @@ activate_new(pcap_t *handle) val = 1; if (setsockopt(sock_fd, SOL_PACKET, PACKET_AUXDATA, &val, sizeof(val)) == -1 && errno != ENOPROTOOPT) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "setsockopt: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "setsockopt"); close(sock_fd); return PCAP_ERROR; } @@ -3746,9 +3742,8 @@ activate_mmap(pcap_t *handle, int *status) */ handlep->oneshot_buffer = malloc(handle->snapshot); if (handlep->oneshot_buffer == NULL) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't allocate oneshot buffer: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't allocate oneshot buffer"); *status = PCAP_ERROR; return -1; } @@ -3851,10 +3846,9 @@ init_tpacket(pcap_t *handle, int version, const char *version_str) return 1; /* no */ /* Failed to even find out; this is a fatal error. */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't get %s header len on packet socket: %s", - version_str, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't get %s header len on packet socket", + version_str); return -1; } handlep->tp_hdrlen = val; @@ -3862,10 +3856,8 @@ init_tpacket(pcap_t *handle, int version, const char *version_str) val = version; if (setsockopt(handle->fd, SOL_PACKET, PACKET_VERSION, &val, sizeof(val)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't activate %s on packet socket: %s", - version_str, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't activate %s on packet socket", version_str); return -1; } handlep->tp_version = version; @@ -3874,9 +3866,8 @@ init_tpacket(pcap_t *handle, int version, const char *version_str) val = VLAN_TAG_LEN; if (setsockopt(handle->fd, SOL_PACKET, PACKET_RESERVE, &val, sizeof(val)) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't set up reserve on packet socket: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't set up reserve on packet socket"); return -1; } @@ -4006,8 +3997,8 @@ prepare_tpacket_socket(pcap_t *handle) /* * Failed. */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "uname failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "uname failed"); return -1; } if (strcmp(utsname.machine, ISA_64_BIT) == 0) { @@ -4137,8 +4128,8 @@ create_ring(pcap_t *handle, int *status) len = sizeof(sk_type); if (getsockopt(handle->fd, SOL_SOCKET, SO_TYPE, &sk_type, &len) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "getsockopt: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "getsockopt"); *status = PCAP_ERROR; return -1; } @@ -4152,8 +4143,8 @@ create_ring(pcap_t *handle, int *status) * PACKET_RESERVE", in which case we fall back * as best we can. */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "getsockopt: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "getsockopt"); *status = PCAP_ERROR; return -1; } @@ -4312,9 +4303,9 @@ create_ring(pcap_t *handle, int *status) break; default: - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SIOCSHWTSTAMP failed: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "SIOCSHWTSTAMP failed"); *status = PCAP_ERROR; return -1; } @@ -4340,9 +4331,9 @@ create_ring(pcap_t *handle, int *status) } if (setsockopt(handle->fd, SOL_PACKET, PACKET_TIMESTAMP, (void *)×ource, sizeof(timesource))) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't set PACKET_TIMESTAMP: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "can't set PACKET_TIMESTAMP"); *status = PCAP_ERROR; return -1; } @@ -4390,9 +4381,8 @@ retry: */ return 0; } - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't create rx ring on packet socket: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't create rx ring on packet socket"); *status = PCAP_ERROR; return -1; } @@ -4402,8 +4392,8 @@ retry: handlep->mmapbuf = mmap(0, handlep->mmapbuflen, PROT_READ|PROT_WRITE, MAP_SHARED, handle->fd, 0); if (handlep->mmapbuf == MAP_FAILED) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't mmap rx ring: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't mmap rx ring"); /* clear the allocated ring on error*/ destroy_ring(handle); @@ -4415,9 +4405,8 @@ retry: handle->cc = req.tp_frame_nr; handle->buffer = malloc(handle->cc * sizeof(union thdr *)); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't allocate ring of frame headers: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "can't allocate ring of frame headers"); destroy_ring(handle); *status = PCAP_ERROR; @@ -4607,9 +4596,9 @@ static int pcap_wait_for_frames_mmap(pcap_t *handle) */ ret = poll(&pollinfo, 1, handlep->poll_timeout); if (ret < 0 && errno != EINTR) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't poll on packet socket: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "can't poll on packet socket"); return PCAP_ERROR; } else if (ret > 0 && (pollinfo.revents & (POLLHUP|POLLRDHUP|POLLERR|POLLNVAL))) { @@ -4647,10 +4636,9 @@ static int pcap_wait_for_frames_mmap(pcap_t *handle) PCAP_ERRBUF_SIZE, "The interface went down"); } else { - pcap_snprintf(handle->errbuf, - PCAP_ERRBUF_SIZE, - "Error condition on packet socket: %s", - strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Error condition on packet socket"); } return PCAP_ERROR; } @@ -5314,8 +5302,8 @@ iface_get_id(int fd, const char *device, char *ebuf) strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "SIOCGIFINDEX: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFINDEX"); return -1; } @@ -5350,8 +5338,8 @@ iface_bind(int fd, int ifindex, char *ebuf, int protocol) */ return PCAP_ERROR_IFACE_NOT_UP; } else { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "bind: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "bind"); return PCAP_ERROR; } } @@ -5359,8 +5347,8 @@ iface_bind(int fd, int ifindex, char *ebuf, int protocol) /* Any pending errors, e.g., network is down? */ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "getsockopt: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "getsockopt"); return 0; } @@ -5374,8 +5362,8 @@ iface_bind(int fd, int ifindex, char *ebuf, int protocol) */ return PCAP_ERROR_IFACE_NOT_UP; } else if (err > 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "bind: %s", pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + err, "bind"); return 0; } @@ -5400,8 +5388,8 @@ has_wext(int sock_fd, const char *device, char *ebuf) sizeof ireq.ifr_ifrn.ifrn_name); if (ioctl(sock_fd, SIOCGIWNAME, &ireq) >= 0) return 1; /* yes */ - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "%s: SIOCGIWNAME: %s", device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, + "%s: SIOCGIWNAME", device); if (errno == ENODEV) return PCAP_ERROR_NO_SUCH_DEVICE; return 0; @@ -5552,9 +5540,8 @@ enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device) /* * Failed. */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: SIOCGIWPRIV: %s", device, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "%s: SIOCGIWPRIV", device); return PCAP_ERROR; } @@ -5563,15 +5550,14 @@ enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device) */ priv = malloc(ireq.u.data.length * sizeof (struct iw_priv_args)); if (priv == NULL) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "malloc"); return PCAP_ERROR; } ireq.u.data.pointer = (void *)priv; if (ioctl(sock_fd, SIOCGIWPRIV, &ireq) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: SIOCGIWPRIV: %s", device, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "%s: SIOCGIWPRIV", device); free(priv); return PCAP_ERROR; } @@ -5825,8 +5811,8 @@ enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device) memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: Can't get flags: %s", device, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "%s: Can't get flags", device); return PCAP_ERROR; } oldflags = 0; @@ -5834,8 +5820,9 @@ enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device) oldflags = ifr.ifr_flags; ifr.ifr_flags &= ~IFF_UP; if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: Can't set flags: %s", device, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "%s: Can't set flags", + device); return PCAP_ERROR; } } @@ -5853,8 +5840,9 @@ enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device) */ ifr.ifr_flags = oldflags; if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: Can't set flags: %s", device, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "%s: Can't set flags", + device); return PCAP_ERROR; } return PCAP_ERROR_RFMON_NOTSUP; @@ -5937,9 +5925,8 @@ enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device) strlcpy(ireq.ifr_ifrn.ifrn_name, device, sizeof ireq.ifr_ifrn.ifrn_name); if (ioctl(sock_fd, SIOCGIWFREQ, &ireq) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: SIOCGIWFREQ: %s", device, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "%s: SIOCGIWFREQ", device); return PCAP_ERROR; } channel = ireq.u.freq.m; @@ -6014,8 +6001,9 @@ enter_rfmon_mode_wext(pcap_t *handle, int sock_fd, const char *device) if (oldflags != 0) { ifr.ifr_flags = oldflags; if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: Can't set flags: %s", device, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "%s: Can't set flags", + device); /* * At least try to restore the old mode on the @@ -6141,8 +6129,8 @@ iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) */ fd = socket(PF_UNIX, SOCK_RAW, 0); if (fd < 0) { - (void)pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "socket for SIOCETHTOOL(ETHTOOL_GET_TS_INFO): %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "socket for SIOCETHTOOL(ETHTOOL_GET_TS_INFO)"); return -1; } @@ -6181,9 +6169,10 @@ iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) /* * Other error. */ - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "%s: SIOCETHTOOL(ETHTOOL_GET_TS_INFO) ioctl failed: %s", device, - strerror(save_errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + save_errno, + "%s: SIOCETHTOOL(ETHTOOL_GET_TS_INFO) ioctl failed", + device); return -1; } } @@ -6283,9 +6272,9 @@ iface_ethtool_flag_ioctl(pcap_t *handle, int cmd, const char *cmdname) */ return 0; } - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "%s: SIOCETHTOOL(%s) ioctl failed: %s", handle->opt.device, - cmdname, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "%s: SIOCETHTOOL(%s) ioctl failed", + handle->opt.device, cmdname); return -1; } return eval.data; @@ -6374,6 +6363,7 @@ static int activate_old(pcap_t *handle) { struct pcap_linux *handlep = handle->priv; + int err; int arptype; struct ifreq ifr; const char *device = handle->opt.device; @@ -6384,9 +6374,10 @@ activate_old(pcap_t *handle) handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL)); if (handle->fd == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); - if (errno == EPERM || errno == EACCES) { + err = errno; + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + err, "socket"); + if (err == EPERM || err == EACCES) { /* * You don't have permission to open the * socket. @@ -6440,8 +6431,8 @@ activate_old(pcap_t *handle) memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFFLAGS: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "SIOCGIFFLAGS"); return PCAP_ERROR; } if ((ifr.ifr_flags & IFF_PROMISC) == 0) { @@ -6468,9 +6459,8 @@ activate_old(pcap_t *handle) ifr.ifr_flags |= IFF_PROMISC; if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "SIOCSIFFLAGS: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "SIOCSIFFLAGS"); return PCAP_ERROR; } handlep->must_do_on_close |= MUST_CLEAR_PROMISC; @@ -6578,22 +6568,22 @@ iface_bind_old(int fd, const char *device, char *ebuf) memset(&saddr, 0, sizeof(saddr)); strlcpy(saddr.sa_data, device, sizeof(saddr.sa_data)); if (bind(fd, &saddr, sizeof(saddr)) == -1) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "bind: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "bind"); return -1; } /* Any pending errors, e.g., network is down? */ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "getsockopt: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "getsockopt"); return -1; } if (err > 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "bind: %s", pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + err, "bind"); return -1; } @@ -6618,8 +6608,8 @@ iface_get_mtu(int fd, const char *device, char *ebuf) strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "SIOCGIFMTU: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFMTU"); return -1; } @@ -6638,8 +6628,8 @@ iface_get_arptype(int fd, const char *device, char *ebuf) strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "SIOCGIFHWADDR: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFHWADDR"); if (errno == ENODEV) { /* * No such device. @@ -6671,8 +6661,8 @@ fix_program(pcap_t *handle, struct sock_fprog *fcode, int is_mmapped) len = handle->fcode.bf_len; f = (struct bpf_insn *)malloc(prog_size); if (f == NULL) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } memcpy(f, handle->fcode.bf_insns, prog_size); @@ -6848,15 +6838,15 @@ set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode) */ save_mode = fcntl(handle->fd, F_GETFL, 0); if (save_mode == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't get FD flags when changing filter: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "can't get FD flags when changing filter"); return -2; } if (fcntl(handle->fd, F_SETFL, save_mode | O_NONBLOCK) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't set nonblocking mode when changing filter: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "can't set nonblocking mode when changing filter"); return -2; } while (recv(handle->fd, &drain, sizeof drain, MSG_TRUNC) >= 0) @@ -6871,15 +6861,15 @@ set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode) */ (void)fcntl(handle->fd, F_SETFL, save_mode); (void)reset_kernel_filter(handle); - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "recv failed when changing filter: %s", - pcap_strerror(save_errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, save_errno, + "recv failed when changing filter"); return -2; } if (fcntl(handle->fd, F_SETFL, save_mode) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't restore FD flags when changing filter: %s", - pcap_strerror(save_errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "can't restore FD flags when changing filter"); return -2; } } @@ -6908,9 +6898,9 @@ set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode) * Report it as a fatal error. */ if (reset_kernel_filter(handle) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "can't remove kernel total filter: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "can't remove kernel total filter"); return -2; /* fatal error */ } diff --git a/pcap-netfilter-linux.c b/pcap-netfilter-linux.c index 7092b135..785cd2a0 100644 --- a/pcap-netfilter-linux.c +++ b/pcap-netfilter-linux.c @@ -127,7 +127,8 @@ netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_c } while ((len == -1) && (errno == EINTR || errno == ENOBUFS)); if (len < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't receive packet %d:%s", errno, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "Can't receive packet"); return PCAP_ERROR; } @@ -570,7 +571,8 @@ netfilter_activate(pcap_t* handle) /* Create netlink socket */ handle->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER); if (handle->fd < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't create raw socket %d:%s", errno, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't create raw socket"); return PCAP_ERROR; } @@ -588,54 +590,68 @@ netfilter_activate(pcap_t* handle) handle->buffer = malloc(handle->bufsize); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate dump buffer: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't allocate dump buffer"); goto close_fail; } if (type == NFLOG) { if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "NFULNL_CFG_CMD_PF_UNBIND"); goto close_fail; } if (nflog_send_config_cmd(handle, 0, NFULNL_CFG_CMD_PF_BIND, AF_INET) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "NFULNL_CFG_CMD_PF_BIND"); goto close_fail; } /* Bind socket to the nflog groups */ for (i = 0; i < group_count; i++) { if (nflog_send_config_cmd(handle, groups[i], NFULNL_CFG_CMD_BIND, AF_UNSPEC) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Can't listen on group group index"); goto close_fail; } if (nflog_send_config_mode(handle, groups[i], NFULNL_COPY_PACKET, handle->snapshot) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFULNL_COPY_PACKET: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "NFULNL_COPY_PACKET"); goto close_fail; } } } else { if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_UNBIND, AF_INET) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_UNBIND: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "NFQNL_CFG_CMD_PF_UNBIND"); goto close_fail; } if (nfqueue_send_config_cmd(handle, 0, NFQNL_CFG_CMD_PF_BIND, AF_INET) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_CFG_CMD_PF_BIND: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "NFQNL_CFG_CMD_PF_BIND"); goto close_fail; } /* Bind socket to the nfqueue groups */ for (i = 0; i < group_count; i++) { if (nfqueue_send_config_cmd(handle, groups[i], NFQNL_CFG_CMD_BIND, AF_UNSPEC) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't listen on group group index: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Can't listen on group group index"); goto close_fail; } if (nfqueue_send_config_mode(handle, groups[i], NFQNL_COPY_PACKET, handle->snapshot) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "NFQNL_COPY_PACKET: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "NFQNL_COPY_PACKET"); goto close_fail; } } @@ -654,7 +670,8 @@ netfilter_activate(pcap_t* handle) * Set the socket buffer size to the specified value. */ if (setsockopt(handle->fd, SOL_SOCKET, SO_RCVBUF, &handle->opt.buffer_size, sizeof(handle->opt.buffer_size)) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "SO_RCVBUF: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "SO_RCVBUF"); goto close_fail; } } @@ -720,8 +737,8 @@ netfilter_findalldevs(pcap_if_list_t *devlistp, char *err_str) /* if netlink is not supported this is not fatal */ if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) return 0; - pcap_snprintf(err_str, PCAP_ERRBUF_SIZE, "Can't open netlink socket %d:%s", - errno, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(err_str, PCAP_ERRBUF_SIZE, + errno, "Can't open netlink socket"); return -1; } close(sock); diff --git a/pcap-netmap.c b/pcap-netmap.c index 00e6e414..ee8d8b3a 100644 --- a/pcap-netmap.c +++ b/pcap-netmap.c @@ -193,9 +193,9 @@ pcap_netmap_activate(pcap_t *p) d = nm_open(p->opt.device, NULL, 0, NULL); if (d == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "netmap open: cannot access %s: %s\n", - p->opt.device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "netmap open: cannot access %s", + p->opt.device); pcap_cleanup_live_common(p); return (PCAP_ERROR); } @@ -134,7 +134,9 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all dev->name = strdup(tmpstring); if (dev->name == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc() failed"); pcap_freealldevs(*alldevs); return -1; } @@ -154,7 +156,9 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all dev->description = strdup(tmpstring); if (dev->description == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc() failed"); pcap_freealldevs(*alldevs); return -1; } @@ -247,7 +251,9 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all dev = (pcap_if_t *)malloc(sizeof(pcap_if_t)); if (dev == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc() failed"); pcap_freealldevs(*alldevs); return -1; } @@ -286,7 +292,9 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all dev->name = (char *)malloc(stringlen + 1); if (dev->name == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc() failed"); pcap_freealldevs(*alldevs); return -1; } @@ -305,7 +313,9 @@ int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **all if (dev->description == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc() failed"); pcap_freealldevs(*alldevs); return -1; } @@ -114,8 +114,8 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) if (cc < 0) { if (errno == EWOULDBLOCK) return (0); - pcap_snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "pcap_read"); return (-1); } bp = (u_char *)p->buffer; @@ -206,8 +206,8 @@ pcap_inject_nit(pcap_t *p, const void *buf, size_t size) strncpy(sa.sa_data, device, sizeof(sa.sa_data)); ret = sendto(p->fd, buf, size, 0, &sa, sizeof(sa)); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } return (ret); @@ -249,8 +249,8 @@ nit_setflags(pcap_t *p) nioc.nioc_flags |= NF_PROMISC; if (ioctl(p->fd, SIOCSNIT, &nioc) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCSNIT"); return (-1); } return (0); @@ -291,8 +291,8 @@ pcap_activate_nit(pcap_t *p) memset(p, 0, sizeof(*p)); p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW); if (fd < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "socket: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); goto bad; } snit.snit_family = AF_NIT; @@ -306,8 +306,8 @@ pcap_activate_nit(pcap_t *p) * they might be the same error, if they both end up * meaning "NIT doesn't know about that device". */ - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "bind: %s", snit.snit_ifname); goto bad; } if (nit_setflags(p) < 0) @@ -321,7 +321,8 @@ pcap_activate_nit(pcap_t *p) p->bufsize = BUFSPACE; p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { - strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); goto bad; } @@ -1042,7 +1042,8 @@ pcap_activate_win32(pcap_t *p) p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); goto bad; } @@ -1289,8 +1290,8 @@ pcap_setfilter_win32_dag(pcap_t *p, struct bpf_program *fp) { /* Install a user level filter */ if (install_bpf_program(p, fp) < 0) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "setfilter, unable to install the filter: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "setfilter, unable to install the filter"); return (-1); } @@ -127,8 +127,8 @@ pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) (void)lseek(pc->fd, 0L, SEEK_SET); goto again; } - pcap_snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(pc->errbuf, + sizeof(pc->errbuf), errno, "pf read"); return (-1); } bp = (u_char *)pc->buffer + pc->offset; @@ -232,8 +232,8 @@ pcap_inject_pf(pcap_t *p, const void *buf, size_t size) ret = write(p->fd, buf, size); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } return (ret); @@ -302,6 +302,7 @@ pcap_activate_pf(pcap_t *p) int backlog = -1; /* request the most */ struct enfilter Filter; struct endevp devparams; + int err; /* * Initially try a read/write open (to allow the inject @@ -329,9 +330,17 @@ pcap_activate_pf(pcap_t *p) if (p->fd == -1 && errno == EACCES) p->fd = pfopen(p->opt.device, O_RDONLY); if (p->fd < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\ -your system may not be properly configured; see the packetfilter(4) man page\n", - p->opt.device, pcap_strerror(errno)); + if (errno == EACCES) { + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, + "pf open: %s: Permission denied\n" +"your system may not be properly configured; see the packetfilter(4) man page", + p->opt.device); + err = PCAP_ERROR_PERM_DENIED; + } else { + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "pf open: %s", p->opt.device); + err = PCAP_ERROR; + } goto bad; } @@ -353,8 +362,9 @@ your system may not be properly configured; see the packetfilter(4) man page\n", if (p->opt.promisc) enmode |= ENPROMISC; if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "EIOCMBIS"); + err = PCAP_ERROR; goto bad; } #ifdef ENCOPYALL @@ -364,14 +374,16 @@ your system may not be properly configured; see the packetfilter(4) man page\n", #endif /* set the backlog */ if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "EIOCSETW"); + err = PCAP_ERROR; goto bad; } /* discover interface type */ if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "EIOCDEVP"); + err = PCAP_ERROR; goto bad; } /* HACK: to compile prior to Ultrix 4.2 */ @@ -454,6 +466,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n", */ pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown data-link type %u", devparams.end_dev_type); + err = PCAP_ERROR; goto bad; } /* set truncation */ @@ -465,8 +478,9 @@ your system may not be properly configured; see the packetfilter(4) man page\n", } else p->fddipad = 0; if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "EIOCTRUNCATE"); + err = PCAP_ERROR; goto bad; } /* accept all packets */ @@ -474,8 +488,9 @@ your system may not be properly configured; see the packetfilter(4) man page\n", Filter.enf_Priority = 37; /* anything > 2 */ Filter.enf_FilterLen = 0; /* means "always true" */ if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "EIOCSETF"); + err = PCAP_ERROR; goto bad; } @@ -484,8 +499,9 @@ your system may not be properly configured; see the packetfilter(4) man page\n", timeout.tv_sec = p->opt.timeout / 1000; timeout.tv_usec = (p->opt.timeout * 1000) % 1000000; if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "EIOCSRTIMEOUT"); + err = PCAP_ERROR; goto bad; } } @@ -493,7 +509,9 @@ your system may not be properly configured; see the packetfilter(4) man page\n", p->bufsize = BUFSPACE; p->buffer = malloc(p->bufsize + p->offset); if (p->buffer == NULL) { - strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); + err = PCAP_ERROR; goto bad; } @@ -514,7 +532,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n", return (0); bad: pcap_cleanup_live_common(p); - return (PCAP_ERROR); + return (err); } pcap_t * @@ -572,8 +590,8 @@ pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp) * Yes. Try to install the filter. */ if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "BIOCSETF: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + sizeof(p->errbuf), errno, "BIOCSETF"); return (-1); } diff --git a/pcap-rpcap.c b/pcap-rpcap.c index e08f89ee..f4d330c9 100644 --- a/pcap-rpcap.c +++ b/pcap-rpcap.c @@ -292,7 +292,8 @@ rpcap_deseraddr(struct rpcap_sockaddr *sockaddrin, struct sockaddr_storage **soc (*sockaddrout) = (struct sockaddr_storage *) malloc(sizeof(struct sockaddr_in)); if ((*sockaddrout) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc() failed"); return -1; } sockaddrin_ipv4 = (struct rpcap_sockaddr_in *) sockaddrin; @@ -321,7 +322,8 @@ rpcap_deseraddr(struct rpcap_sockaddr *sockaddrin, struct sockaddr_storage **soc (*sockaddrout) = (struct sockaddr_storage *) malloc(sizeof(struct sockaddr_in6)); if ((*sockaddrout) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc() failed"); return -1; } sockaddrin_ipv6 = (struct rpcap_sockaddr_in6 *) sockaddrin; @@ -943,7 +945,8 @@ rpcap_remoteact_getsock(const char *host, int *error, char *errbuf) retval = getaddrinfo(host, "0", &hints, &addrinfo); if (retval != 0) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "getaddrinfo() %s", gai_strerror(retval)); + pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "getaddrinfo() %s", + gai_strerror(retval)); *error = 1; return NULL; } @@ -1305,7 +1308,8 @@ static int pcap_startcapture_remote(pcap_t *fp) fp->buffer = (u_char *)malloc(fp->bufsize); if (fp->buffer == NULL) { - pcap_snprintf(fp->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(fp->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); goto error; } @@ -2090,8 +2094,8 @@ pcap_t *pcap_open_rpcap(const char *source, int snaplen, int flags, int read_tim } source_str = strdup(source); if (source_str == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return NULL; } @@ -2435,7 +2439,8 @@ pcap_findalldevs_ex_remote(char *source, struct pcap_rmtauth *auth, pcap_if_t ** dev = (pcap_if_t *)malloc(sizeof(pcap_if_t)); if (dev == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc() failed"); goto error; } @@ -2486,7 +2491,8 @@ pcap_findalldevs_ex_remote(char *source, struct pcap_rmtauth *auth, pcap_if_t ** dev->name = (char *)malloc(stringlen + 1); if (dev->name == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "malloc() failed"); goto error; } @@ -2518,7 +2524,8 @@ pcap_findalldevs_ex_remote(char *source, struct pcap_rmtauth *auth, pcap_if_t ** if (dev->description == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "malloc() failed"); goto error; } @@ -2546,7 +2553,8 @@ pcap_findalldevs_ex_remote(char *source, struct pcap_rmtauth *auth, pcap_if_t ** addr = (struct pcap_addr *) malloc(sizeof(struct pcap_addr)); if (addr == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "malloc() failed"); goto error; } addr->next = NULL; @@ -2787,7 +2795,8 @@ SOCKET pcap_remoteact_accept(const char *address, const char *port, const char * if (temp == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc() failed"); rpcap_senderror(sockctrl, protocol_version, PCAP_ERR_REMOTEACCEPT, errbuf, NULL); sock_close(sockctrl, NULL, 0); return -1; diff --git a/pcap-septel.c b/pcap-septel.c index dce84a1e..b8b4e3ee 100644 --- a/pcap-septel.c +++ b/pcap-septel.c @@ -298,8 +298,8 @@ static int septel_setfilter(pcap_t *p, struct bpf_program *fp) { /* Make our private copy of the filter */ if (install_bpf_program(p, fp) < 0) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), errno, + "malloc"); return -1; } diff --git a/pcap-sita.c b/pcap-sita.c index 8a4f6d8b..7c42791a 100644 --- a/pcap-sita.c +++ b/pcap-sita.c @@ -293,7 +293,8 @@ int acn_parse_hosts_file(char *errbuf) { /* returns: -1 = error, 0 = OK */ continue; /* and ignore the entry */ } if ((ptr2 = (char *)malloc(strlen(ptr) + 1)) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); continue; } strcpy(ptr2, ptr); /* copy the IP address into our malloc'ed memory */ @@ -582,7 +583,8 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr = u->imsg; /* point to the start of the msg for this IOP */ while (ptr < (u->imsg + u->len)) { if ((iff = malloc(sizeof(pcap_if_t))) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "malloc"); return -1; } memset((char *)iff, 0, sizeof(pcap_if_t)); /* bzero() is deprecated, replaced with memset() */ @@ -591,7 +593,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 if (*ptr) { /* if there is a count for the name */ if ((iff->name = malloc(*ptr + 1)) == NULL) { /* get that amount of space */ - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); return -1; } memcpy(iff->name, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */ @@ -602,7 +606,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 if (*ptr) { /* if there is a count for the description */ if ((iff->description = malloc(*ptr + 1)) == NULL) { /* get that amount of space */ - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); return -1; } memcpy(iff->description, (ptr + 1), *ptr); /* copy the name into the malloc'ed space */ @@ -622,7 +628,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 prev_addr = 0; while (address_count--) { if ((addr = malloc(sizeof(pcap_addr_t))) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); return -1; } memset((char *)addr, 0, sizeof(pcap_addr_t)); /* bzero() is deprecated, replaced with memset() */ @@ -630,7 +638,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 if (prev_addr) prev_addr->next = addr; /* insert a forward link */ if (*ptr) { /* if there is a count for the address */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { /* get that amount of space */ - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } memset((char *)s, 0, sizeof(struct sockaddr_in)); /* bzero() is deprecated, replaced with memset() */ @@ -642,7 +652,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr++; /* then forwards one more for the 'length of the address' field */ if (*ptr) { /* process any netmask */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } /* bzero() is deprecated, replaced with memset() */ @@ -656,7 +668,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr++; if (*ptr) { /* process any broadcast address */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } /* bzero() is deprecated, replaced with memset() */ @@ -670,7 +684,9 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 ptr++; if (*ptr) { /* process any destination address */ if ((s = malloc(sizeof(struct sockaddr_in))) == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, + errno, "malloc"); return -1; } /* bzero() is deprecated, replaced with memset() */ @@ -689,7 +705,8 @@ static int process_client_data (char *errbuf) { /* returns: -1 = error, 0 newname = translate_IOP_to_pcap_name(u, iff->name, interfaceType); /* add a translation entry and get a point to the mangled name */ bigger_buffer = realloc(iff->name, strlen(newname) + 1)); if (bigger_buffer == NULL) { /* we now re-write the name stored in the interface list */ - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "realloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "realloc"); return -1; } iff->name = bigger_buffer; @@ -1011,8 +1028,8 @@ static int pcap_activate_sita(pcap_t *handle) { handle->buffer = malloc(handle->bufsize + handle->offset); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); pcap_cleanup_acn(handle); return PCAP_ERROR; } @@ -55,8 +55,8 @@ snf_pcap_stats(pcap_t *p, struct pcap_stat *ps) int rc; if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_get_stats: %s", - pcap_strerror(rc)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + rc, "snf_get_stats"); return -1; } ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow; @@ -166,8 +166,8 @@ snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) continue; } else { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_read: %s", - pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, err, "snf_read"); return -1; } } @@ -221,8 +221,8 @@ snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_) if (ps->snf_inj == NULL) { rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj); if (rc) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "snf_inject_open: %s", pcap_strerror(rc)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + rc, "snf_inject_open"); return (-1); } } @@ -232,8 +232,8 @@ snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_) return (size); } else { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_inject_send: %s", - pcap_strerror(rc)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + rc, "snf_inject_send"); return (-1); } #else @@ -253,8 +253,7 @@ snf_activate(pcap_t* p) int flags = -1, ring_id = -1; if (device == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "device is NULL: %s", pcap_strerror(errno)); + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL"); return -1; } @@ -281,8 +280,8 @@ snf_activate(pcap_t* p) flags, /* may want pshared */ &ps->snf_handle); if (err != 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "snf_open failed: %s", pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + err, "snf_open failed"); return -1; } @@ -291,9 +290,8 @@ snf_activate(pcap_t* p) } err = snf_ring_open_id(ps->snf_handle, ring_id, &ps->snf_ring); if (err != 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "snf_ring_open_id(ring=%d) failed: %s", - ring_id, pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + err, "snf_ring_open_id(ring=%d) failed", ring_id); return -1; } @@ -315,8 +313,8 @@ snf_activate(pcap_t* p) err = snf_start(ps->snf_handle); if (err != 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "snf_start failed: %s", pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + err, "snf_start failed"); return -1; } @@ -364,8 +362,8 @@ snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf) if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "snf_getifaddrs: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "snf_getifaddrs"); return (-1); } if ((nr = getenv("SNF_FLAGS")) && *nr) { @@ -431,8 +429,9 @@ snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf) desc_str = strdup(desc); if (desc_str == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "snf_findalldevs strdup: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "snf_findalldevs strdup"); return -1; } free(dev->description); @@ -463,7 +462,9 @@ snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf) /* * Error. */ - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,"sinf_findalldevs inet_pton: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "sinf_findalldevs inet_pton"); return -1; } #endif _WIN32 @@ -553,8 +554,8 @@ snf_create(const char *device, char *ebuf, int *is_ours) p->tstamp_precision_count = 2; p->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (p->tstamp_precision_list == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, + "malloc"); pcap_close(p); return NULL; } diff --git a/pcap-snit.c b/pcap-snit.c index 24368f6e..9da8573d 100644 --- a/pcap-snit.c +++ b/pcap-snit.c @@ -130,8 +130,8 @@ pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) if (cc < 0) { if (errno == EWOULDBLOCK) return (0); - pcap_snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "pcap_read"); return (-1); } bp = (u_char *)p->buffer; @@ -223,8 +223,8 @@ pcap_inject_snit(pcap_t *p, const void *buf, size_t size) data.len = size; ret = putmsg(p->fd, &ctl, &data); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } return (ret); @@ -247,8 +247,8 @@ nit_setflags(pcap_t *p) si.ic_len = sizeof(zero); si.ic_dp = (char *)&zero; if (ioctl(p->fd, I_STR, (char *)&si) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "NIOCSCHUNK"); return (-1); } } @@ -260,8 +260,8 @@ nit_setflags(pcap_t *p) si.ic_len = sizeof(timeout); si.ic_dp = (char *)&timeout; if (ioctl(p->fd, I_STR, (char *)&si) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "NIOCSTIME"); return (-1); } } @@ -272,8 +272,8 @@ nit_setflags(pcap_t *p) si.ic_len = sizeof(flags); si.ic_dp = (char *)&flags; if (ioctl(p->fd, I_STR, (char *)&si) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "NIOCSFLAGS"); return (-1); } return (0); @@ -287,6 +287,7 @@ pcap_activate_snit(pcap_t *p) int chunksize = CHUNKSIZE; int fd; static const char dev[] = "/dev/nit"; + int err; if (p->opt.rfmon) { /* @@ -331,20 +332,26 @@ pcap_activate_snit(pcap_t *p) if (fd < 0 && errno == EACCES) p->fd = fd = open(dev, O_RDONLY); if (fd < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dev, - pcap_strerror(errno)); + if (errno == EACCES) + err = PCAP_ERROR_PERM_DENIED; + else + err = PCAP_ERROR; + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "%s", dev); goto bad; } /* arrange to get discrete messages from the STREAM and use NIT_BUF */ if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "I_SRDOPT"); + err = PCAP_ERROR; goto bad; } if (ioctl(fd, I_PUSH, "nbuf") < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "push nbuf: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "push nbuf"); + err = PCAP_ERROR; goto bad; } /* set the chunksize */ @@ -353,8 +360,9 @@ pcap_activate_snit(pcap_t *p) si.ic_len = sizeof(chunksize); si.ic_dp = (char *)&chunksize; if (ioctl(fd, I_STR, (char *)&si) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "NIOCSCHUNK"); + err = PCAP_ERROR; goto bad; } @@ -370,8 +378,9 @@ pcap_activate_snit(pcap_t *p) * Is there one that means "that device doesn't support * STREAMS NIT"? */ - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s", - ifr.ifr_name, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "NIOCBIND: %s", ifr.ifr_name); + err = PCAP_ERROR; goto bad; } @@ -380,12 +389,15 @@ pcap_activate_snit(pcap_t *p) si.ic_len = sizeof(p->snapshot); si.ic_dp = (char *)&p->snapshot; if (ioctl(fd, I_STR, (char *)&si) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "NIOCSSNAP"); + err = PCAP_ERROR; goto bad; } - if (nit_setflags(p) < 0) + if (nit_setflags(p) < 0) { + err = PCAP_ERROR; goto bad; + } (void)ioctl(fd, I_FLUSH, (char *)FLUSHR); /* @@ -396,7 +408,9 @@ pcap_activate_snit(pcap_t *p) p->bufsize = BUFSPACE; p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { - strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); + err = PCAP_ERROR; goto bad; } @@ -438,7 +452,7 @@ pcap_activate_snit(pcap_t *p) return (0); bad: pcap_cleanup_live_common(p); - return (PCAP_ERROR); + return (err); } pcap_t * diff --git a/pcap-snoop.c b/pcap-snoop.c index b5fbeba2..2fe7715b 100644 --- a/pcap-snoop.c +++ b/pcap-snoop.c @@ -95,8 +95,8 @@ again: case EWOULDBLOCK: return (0); /* XXX */ } - pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "read: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "read"); return (-1); } sh = (struct snoopheader *)p->buffer; @@ -150,8 +150,8 @@ pcap_inject_snoop(pcap_t *p, const void *buf, size_t size) */ ret = write(p->fd, buf, size); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } return (ret); @@ -167,8 +167,8 @@ pcap_stats_snoop(pcap_t *p, struct pcap_stat *ps) rs = &rawstats; memset(rs, 0, sizeof(*rs)); if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "SIOCRAWSTATS: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "SIOCRAWSTATS"); return (-1); } @@ -212,8 +212,8 @@ pcap_activate_snoop(pcap_t *p) fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP); if (fd < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snoop socket: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "snoop socket"); goto bad; } p->fd = fd; @@ -228,14 +228,14 @@ pcap_activate_snoop(pcap_t *p) * they might be the same error, if they both end up * meaning "snoop doesn't know about that device". */ - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snoop bind: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "snoop bind"); goto bad; } memset(&sf, 0, sizeof(sf)); if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCADDSNOOP: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCADDSNOOP"); goto bad; } if (p->opt.buffer_size != 0) @@ -343,8 +343,8 @@ pcap_activate_snoop(pcap_t *p) */ (void)strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name)); if (ioctl(fd, SIOCGIFMTU, (char *)&ifr) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMTU: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFMTU"); goto bad; } /* @@ -377,22 +377,22 @@ pcap_activate_snoop(pcap_t *p) if (snooplen < 0) snooplen = 0; if (ioctl(fd, SIOCSNOOPLEN, &snooplen) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPLEN: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCSNOOPLEN"); goto bad; } v = 1; if (ioctl(fd, SIOCSNOOPING, &v) < 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPING: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCSNOOPING"); goto bad; } p->bufsize = 4096; /* XXX */ p->buffer = malloc(p->bufsize); if (p->buffer == NULL) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); goto bad; } @@ -1130,8 +1130,8 @@ TcSetFilter(pcap_t *p, struct bpf_program *fp) /* Install a user level filter */ if (install_bpf_program(p, fp) < 0) { - pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "setfilter, unable to install the filter: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "setfilter, unable to install the filter"); return -1; } diff --git a/pcap-usb-linux.c b/pcap-usb-linux.c index 12501624..802842b7 100644 --- a/pcap-usb-linux.c +++ b/pcap-usb-linux.c @@ -567,8 +567,9 @@ usb_activate(pcap_t* handle) /* * Something went wrong. */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't open USB bus file %s: %s", full_path, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Can't open USB bus file %s", full_path); return PCAP_ERROR; } } @@ -646,8 +647,10 @@ usb_activate(pcap_t* handle) /* * Yes - return *that* error. */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't open USB bus file %s: %s", full_path, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Can't open USB bus file %s", + full_path); return PCAP_ERROR; } @@ -683,8 +686,8 @@ usb_activate(pcap_t* handle) * buffer */ handle->buffer = malloc(handle->bufsize); if (!handle->buffer) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); close(handle->fd); return PCAP_ERROR; } @@ -734,8 +737,8 @@ usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u if (errno == EAGAIN) return 0; /* no data there */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't read from fd %d: %s", handle->fd, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't read from fd %d", handle->fd); return -1; } @@ -761,9 +764,8 @@ usb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *u /* don't use usbmon provided timestamp, since it have low precision*/ if (gettimeofday(&pkth.ts, NULL) < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't get timestamp for message '%s' %d:%s", - string, errno, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't get timestamp for message '%s'"); return -1; } uhdr->ts_sec = pkth.ts.tv_sec; @@ -932,9 +934,9 @@ usb_stats_linux(pcap_t *handle, struct pcap_stat *stats) fd = open(string, O_RDONLY, 0); } if (fd < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't open USB stats file %s: %s", - string, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, + "Can't open USB stats file %s", string); return -1; } } @@ -1001,8 +1003,8 @@ usb_stats_linux_bin(pcap_t *handle, struct pcap_stat *stats) ret = ioctl(handle->fd, MON_IOCG_STATS, &st); if (ret < 0) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't read stats from fd %d:%s ", handle->fd, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't read stats from fd %d", handle->fd); return -1; } @@ -1044,8 +1046,8 @@ usb_read_linux_bin(pcap_t *handle, int max_packets, pcap_handler callback, u_cha if (errno == EAGAIN) return 0; /* no data there */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't read from fd %d: %s", handle->fd, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't read from fd %d", handle->fd); return -1; } @@ -1115,8 +1117,9 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch if (errno == EAGAIN) return 0; /* no data there */ - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't mfetch fd %d: %s", handle->fd, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, + PCAP_ERRBUF_SIZE, errno, "Can't mfetch fd %d", + handle->fd); return -1; } @@ -1156,8 +1159,8 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch /* flush pending events*/ if (ioctl(handle->fd, MON_IOCH_MFLUSH, nflush) == -1) { - pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, - "Can't mflush fd %d: %s", handle->fd, strerror(errno)); + pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't mflush fd %d", handle->fd); return -1; } return packets; @@ -295,8 +295,8 @@ pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp) *tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp), p->tstamp_type_count); if (*tstamp_typesp == NULL) { - (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "malloc"); return (PCAP_ERROR); } (void)memcpy(*tstamp_typesp, p->tstamp_type_list, @@ -865,8 +865,8 @@ add_addr_to_dev(pcap_if_t *curdev, */ curaddr = (pcap_addr_t *)malloc(sizeof(pcap_addr_t)); if (curaddr == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (-1); } @@ -874,8 +874,8 @@ add_addr_to_dev(pcap_if_t *curdev, if (addr != NULL && addr_size != 0) { curaddr->addr = (struct sockaddr *)dup_sockaddr(addr, addr_size); if (curaddr->addr == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); free(curaddr); return (-1); } @@ -885,8 +885,8 @@ add_addr_to_dev(pcap_if_t *curdev, if (netmask != NULL && netmask_size != 0) { curaddr->netmask = (struct sockaddr *)dup_sockaddr(netmask, netmask_size); if (curaddr->netmask == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); if (curaddr->addr != NULL) free(curaddr->addr); free(curaddr); @@ -898,8 +898,8 @@ add_addr_to_dev(pcap_if_t *curdev, if (broadaddr != NULL && broadaddr_size != 0) { curaddr->broadaddr = (struct sockaddr *)dup_sockaddr(broadaddr, broadaddr_size); if (curaddr->broadaddr == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); if (curaddr->netmask != NULL) free(curaddr->netmask); if (curaddr->addr != NULL) @@ -913,8 +913,8 @@ add_addr_to_dev(pcap_if_t *curdev, if (dstaddr != NULL && dstaddr_size != 0) { curaddr->dstaddr = (struct sockaddr *)dup_sockaddr(dstaddr, dstaddr_size); if (curaddr->dstaddr == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); if (curaddr->broadaddr != NULL) free(curaddr->broadaddr); if (curaddr->netmask != NULL) @@ -1035,8 +1035,8 @@ add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags, curdev = malloc(sizeof(pcap_if_t)); if (curdev == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (NULL); } @@ -1046,8 +1046,8 @@ add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags, curdev->next = NULL; curdev->name = strdup(name); if (curdev->name == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); free(curdev); return (NULL); } @@ -1062,8 +1062,8 @@ add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags, */ curdev->description = strdup(description); if (curdev->description == NULL) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); free(curdev->name); free(curdev); return (NULL); @@ -1316,8 +1316,8 @@ pcap_lookupnet(device, netp, maskp, errbuf) fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "socket: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "socket"); return (-1); } memset(&ifr, 0, sizeof(ifr)); @@ -1331,9 +1331,8 @@ pcap_lookupnet(device, netp, maskp, errbuf) (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: no IPv4 address assigned", device); } else { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFADDR: %s: %s", - device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFADDR: %s", device); } (void)close(fd); return (-1); @@ -1347,8 +1346,8 @@ pcap_lookupnet(device, netp, maskp, errbuf) #endif (void)strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) { - (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "SIOCGIFNETMASK: %s: %s", device, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "SIOCGIFNETMASK: %s", device); (void)close(fd); return (-1); } @@ -1385,8 +1384,8 @@ get_substring(const char *p, size_t len, char *ebuf) token = malloc(len + 1); if (token == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (NULL); } memcpy(token, p, len); @@ -1480,8 +1479,8 @@ pcap_parse_source(const char *source, char **schemep, char **userinfop, */ *pathp = strdup(source); if (*pathp == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (-1); } return (0); @@ -1505,8 +1504,8 @@ pcap_parse_source(const char *source, char **schemep, char **userinfop, */ *pathp = strdup(source); if (*pathp == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (-1); } return (0); @@ -1523,8 +1522,8 @@ pcap_parse_source(const char *source, char **schemep, char **userinfop, scheme_len = colonp - source; scheme = malloc(scheme_len + 1); if (scheme == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (-1); } memcpy(scheme, source, scheme_len); @@ -1538,8 +1537,8 @@ pcap_parse_source(const char *source, char **schemep, char **userinfop, *schemep = scheme; *pathp = strdup(colonp + 3); if (*pathp == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (-1); } return (0); @@ -1564,8 +1563,8 @@ pcap_parse_source(const char *source, char **schemep, char **userinfop, free(scheme); *pathp = strdup(colonp + 3); if (*pathp == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (-1); } return (0); @@ -1742,8 +1741,8 @@ pcap_parse_source(const char *source, char **schemep, char **userinfop, else path = strdup(endp + 1); if (path == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); free(port); free(host); free(userinfo); @@ -1950,8 +1949,9 @@ pcap_create(const char *device, char *errbuf) length = wcslen((wchar_t *)device); device_str = (char *)malloc(length + 1); if (device_str == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, + "malloc"); return (NULL); } @@ -1962,8 +1962,8 @@ pcap_create(const char *device, char *errbuf) device_str = strdup(device); } if (device_str == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (NULL); } @@ -2083,8 +2083,8 @@ pcap_alloc_pcap_t(char *ebuf, size_t size) */ chunk = malloc(sizeof (pcap_t) + size); if (chunk == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); return (NULL); } memset(chunk, 0, sizeof (pcap_t) + size); @@ -2578,8 +2578,8 @@ pcap_list_datalinks(pcap_t *p, int **dlt_buffer) */ *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); if (*dlt_buffer == NULL) { - (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "malloc"); return (PCAP_ERROR); } **dlt_buffer = p->linktype; @@ -2587,8 +2587,8 @@ pcap_list_datalinks(pcap_t *p, int **dlt_buffer) } else { *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count); if (*dlt_buffer == NULL) { - (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), - "malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), + errno, "malloc"); return (PCAP_ERROR); } (void)memcpy(*dlt_buffer, p->dlt_list, @@ -3111,8 +3111,8 @@ pcap_getnonblock_fd(pcap_t *p) fdflags = fcntl(p->fd, F_GETFL, 0); if (fdflags == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "F_GETFL"); return (-1); } if (fdflags & O_NONBLOCK) @@ -3157,8 +3157,8 @@ pcap_setnonblock_fd(pcap_t *p, int nonblock) fdflags = fcntl(p->fd, F_GETFL, 0); if (fdflags == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "F_GETFL"); return (-1); } if (nonblock) @@ -3166,8 +3166,8 @@ pcap_setnonblock_fd(pcap_t *p, int nonblock) else fdflags &= ~O_NONBLOCK; if (fcntl(p->fd, F_SETFL, fdflags) == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "F_SETFL"); return (-1); } return (0); diff --git a/rpcapd/daemon.c b/rpcapd/daemon.c index 8a47f384..2cce9aeb 100755 --- a/rpcapd/daemon.c +++ b/rpcapd/daemon.c @@ -39,6 +39,7 @@ #include <errno.h> // for the errno variable #include <stdlib.h> // for malloc(), free(), ... #include <string.h> // for strlen(), ... +#include "fmtutils.h" #include "sockutils.h" // for socket calls #include "portability.h" #include "rpcap-protocol.h" @@ -984,7 +985,8 @@ int daemon_msg_auth_req(SOCKET sockctrl, uint8 ver, uint32 plen, int nullAuthAll username = (char *) malloc (usernamelen + 1); if (username == NULL) { - pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errmsgbuf, + PCAP_ERRBUF_SIZE, errno, "malloc() failed"); goto error; } status = rpcapd_recv(sockctrl, username, usernamelen, &plen, errmsgbuf); @@ -1004,7 +1006,8 @@ int daemon_msg_auth_req(SOCKET sockctrl, uint8 ver, uint32 plen, int nullAuthAll passwd = (char *) malloc (passwdlen + 1); if (passwd == NULL) { - pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errmsgbuf, + PCAP_ERRBUF_SIZE, errno, "malloc() failed"); free(username); goto error; } @@ -1205,14 +1208,15 @@ int daemon_AuthUserPwd(char *username, char *password, char *errbuf) if (setuid(user->pw_uid)) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "setuid"); return -1; } /* if (setgid(user->pw_gid)) { - SOCK_ASSERT("setgid failed", 1); - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "setgid"); return -1; } */ @@ -1751,10 +1755,8 @@ static int daemon_msg_startcap_req(struct daemon_slpars *pars, uint32 plen, int daemon_thrdatamain, (void *) session); if (ret != 0) { - char thread_errbuf[PCAP_ERRBUF_SIZE]; - - (void)strerror_r(ret, thread_errbuf, PCAP_ERRBUF_SIZE); - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error creating the data thread: %s", thread_errbuf); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + ret, "Error creating the data thread"); pthread_attr_destroy(&detachedAttribute); goto error; } @@ -1977,7 +1979,8 @@ static int daemon_unpackapplyfilter(SOCKET sockctrl, struct session *session, ui bf_insn = (struct bpf_insn *) malloc (sizeof(struct bpf_insn) * bf_prog.bf_len); if (bf_insn == NULL) { - pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errmsgbuf, PCAP_ERRBUF_SIZE, + errno, "malloc() failed"); return -2; } @@ -2239,14 +2242,17 @@ daemon_thrdatamain(void *ptr) retval = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); if (retval != 0) { - (void)strerror_r(retval, errbuf, PCAP_ERRBUF_SIZE); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + retval, "pthread_setcancelstate"); rpcapd_log(LOGPRIO_ERROR, "Can't set cancel state on data thread: %s", errbuf); goto error; } - if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) + retval = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + if (retval != 0) { - (void)strerror_r(retval, errbuf, PCAP_ERRBUF_SIZE); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + retval, "pthread_setcanceltype"); rpcapd_log(LOGPRIO_ERROR, "Can't set cancel type on data thread: %s", errbuf); goto error; diff --git a/rpcapd/rpcapd.c b/rpcapd/rpcapd.c index 128e8105..523f0d3a 100755 --- a/rpcapd/rpcapd.c +++ b/rpcapd/rpcapd.c @@ -42,6 +42,7 @@ #include <pcap.h> // for PCAP_ERRBUF_SIZE #include <signal.h> // for signal() +#include "fmtutils.h" #include "sockutils.h" // for socket calls #include "portability.h" #include "rpcapd.h" @@ -604,7 +605,8 @@ main_passive(void *ptr) pars = (struct daemon_slpars *) malloc (sizeof(struct daemon_slpars)); if (pars == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc() failed"); rpcap_senderror(sockctrl, 0, PCAP_ERR_OPEN, errbuf, NULL); sock_close(sockctrl, NULL, 0); continue; @@ -632,7 +634,8 @@ main_passive(void *ptr) pars = (struct daemon_slpars *) malloc (sizeof(struct daemon_slpars)); if (pars == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "malloc() failed"); exit(0); } @@ -727,7 +730,8 @@ main_active(void *ptr) pars = (struct daemon_slpars *) malloc (sizeof(struct daemon_slpars)); if (pars == NULL) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "malloc() failed: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "malloc() failed"); continue; } @@ -275,8 +275,8 @@ pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision, err = pcap_fopen(&fp, fname, "rb"); if (err != 0) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname, - pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + err, "%s", fname); return (NULL); } } @@ -305,14 +305,16 @@ pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision, fd = _open_osfhandle(osfd, _O_RDONLY); if ( fd < 0 ) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "_open_osfhandle"); return NULL; } file = _fdopen(fd, "rb"); if ( file == NULL ) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "_fdopen"); return NULL; } @@ -357,9 +359,8 @@ pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision, amt_read = fread((char *)&magic, 1, sizeof(magic), fp); if (amt_read != sizeof(magic)) { if (ferror(fp)) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "error reading dump file"); } else { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %lu file header bytes, only got %lu", diff --git a/sf-pcap-ng.c b/sf-pcap-ng.c index 52613123..205275b3 100644 --- a/sf-pcap-ng.c +++ b/sf-pcap-ng.c @@ -255,9 +255,8 @@ read_bytes(FILE *fp, void *buf, size_t bytes_to_read, int fail_on_eof, amt_read = fread(buf, 1, bytes_to_read, fp); if (amt_read != bytes_to_read) { if (ferror(fp)) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "error reading dump file"); } else { if (amt_read == 0 && !fail_on_eof) return (0); /* EOF */ @@ -777,9 +776,8 @@ pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf, amt_read = fread(&total_length, 1, sizeof(total_length), fp); if (amt_read < sizeof(total_length)) { if (ferror(fp)) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "error reading dump file"); *err = 1; return (NULL); /* fail */ } @@ -793,9 +791,8 @@ pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf, amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp); if (amt_read < sizeof(byte_order_magic)) { if (ferror(fp)) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "error reading dump file"); *err = 1; return (NULL); /* fail */ } @@ -174,9 +174,8 @@ pcap_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf, sizeof(hdr) - sizeof(hdr.magic), fp); if (amt_read != sizeof(hdr) - sizeof(hdr.magic)) { if (ferror(fp)) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "error reading dump file"); } else { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %lu file header bytes, only got %lu", @@ -447,9 +446,8 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) amt_read = fread(&sf_hdr, 1, ps->hdrsize, fp); if (amt_read != ps->hdrsize) { if (ferror(fp)) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "error reading dump file"); return (-1); } else { if (amt_read != 0) { @@ -595,9 +593,9 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) amt_read = fread(p->buffer, 1, p->bufsize, fp); if (amt_read != p->bufsize) { if (ferror(fp)) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "error reading dump file"); } else { /* * Yes, this uses hdr->caplen; technically, @@ -626,9 +624,9 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) bytes_read += amt_read; if (amt_read != bytes_to_read) { if (ferror(fp)) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "error reading dump file"); } else { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %u captured bytes, only got %lu", @@ -675,9 +673,9 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data) amt_read = fread(p->buffer, 1, hdr->caplen, fp); if (amt_read != hdr->caplen) { if (ferror(fp)) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "error reading dump file: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, + PCAP_ERRBUF_SIZE, errno, + "error reading dump file"); } else { pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "truncated dump file; tried to read %u captured bytes, only got %lu", @@ -751,8 +749,8 @@ pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname) setvbuf(f, NULL, _IONBF, 0); #endif if (sf_write_header(p, f, linktype, p->tzoff, p->snapshot) == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s", - fname, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't write to %s", fname); if (f != stdout) (void)fclose(f); return (NULL); @@ -801,8 +799,8 @@ pcap_dump_open(pcap_t *p, const char *fname) err = pcap_fopen(&f, fname, "wb"); if (err != 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", - fname, pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + err, "%s", fname); return (NULL); } } @@ -856,8 +854,8 @@ pcap_dump_open_append(pcap_t *p, const char *fname) err = pcap_fopen(&f, fname, "rb+"); if (err != 0) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", - fname, pcap_strerror(err)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + err, "%s", fname); return (NULL); } @@ -867,8 +865,8 @@ pcap_dump_open_append(pcap_t *p, const char *fname) amt_read = fread(&ph, 1, sizeof (ph), f); if (amt_read != sizeof (ph)) { if (ferror(f)) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", - fname, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "%s", fname); fclose(f); return (NULL); } else if (feof(f) && amt_read > 0) { @@ -975,8 +973,8 @@ pcap_dump_open_append(pcap_t *p, const char *fname) * A header isn't present; attempt to write it. */ if (sf_write_header(p, f, linktype, p->tzoff, p->snapshot) == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s", - fname, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't write to %s", fname); (void)fclose(f); return (NULL); } @@ -986,8 +984,8 @@ pcap_dump_open_append(pcap_t *p, const char *fname) * Start writing at the end of the file. */ if (fseek(f, 0, SEEK_END) == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't seek to end of %s: %s", - fname, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "Can't seek to end of %s", fname); (void)fclose(f); return (NULL); } |