From: Guy Harris Date: Mon, 1 Jul 2019 03:32:38 +0000 (-0700) Subject: Return an appropriate error message for ERROR_GEN_FAILURE when capturing. X-Git-Tag: libpcap-1.10-bp~473 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/f15b254ab33d805e68a8a7cfad37c8231fe6b71e Return an appropriate error message for ERROR_GEN_FAILURE when capturing. That's the error returned if the capture stops working because either 1) the device was removed (unplugged) or 2) the device becomes unusable for capturing due to a suspend/resume. --- diff --git a/pcap-npf.c b/pcap-npf.c index a9a1967f..15018da9 100644 --- a/pcap-npf.c +++ b/pcap-npf.c @@ -523,7 +523,34 @@ pcap_read_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) */ PacketInitPacket(&Packet, (BYTE *)p->buffer, p->bufsize); if (!PacketReceivePacket(pw->adapter, &Packet, TRUE)) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read error: PacketReceivePacket failed"); + /* + * Did the device go away? + * If so, the error we get is ERROR_GEN_FAILURE. + */ + DWORD errcode = GetLastError(); + + if (errcode == ERROR_GEN_FAILURE) { + /* + * The device on which we're capturing + * went away, or it became unusable + * by NPF due to a suspend/resume. + * + * XXX - hopefully no other error + * conditions are indicated by this. + * + * XXX - we really should return an + * appropriate error for that, but + * pcap_dispatch() etc. aren't + * documented as having error returns + * other than PCAP_ERROR or PCAP_ERROR_BREAK. + */ + pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, + "The interface disappeared"); + } else { + pcap_fmt_errmsg_for_win32_err(p->errbuf, + PCAP_ERRBUF_SIZE, errcode, + "PacketReceivePacket error"); + } return (PCAP_ERROR); }