diff options
author | Guy Harris <guy@alum.mit.edu> | 2017-11-15 10:47:24 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2017-11-15 10:47:24 -0800 |
commit | 7d787482040aa327ef2345eca2e9f980ce76cc28 (patch) | |
tree | a4756446c343f763ab12c0c55b78801d3c28a3f4 /pcap-nit.c | |
parent | 3299e855cb1cf7b6426d70e52cc51a022b9d9141 (diff) |
Add a routine to format error messages with an errno-based message at the end.
That routine will use strerror_s() or strerror_r() if available, in a
fashion that's thread-safe. Otherwise, it falls back on
pcap_strerror().
Use it in both libpcap and rpcapd.
Given that we check for errors in strerror_r(), hopefully this will
squelch warnings with newer version of GCC and GNU libc; whilst the
macOS (and other BSD-flavored?) strerror_r() always fills in a message,
that's not required by the Single UNIX Specification, as far as I can
tell, so we apparently really *do* need to check for errors.
Diffstat (limited to 'pcap-nit.c')
-rw-r--r-- | pcap-nit.c | 23 |
1 files changed, 12 insertions, 11 deletions
@@ -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; } |