diff --git a/dlpisubs.c b/dlpisubs.c index 5f1a9bfc5f..671302f16e 100644 --- a/dlpisubs.c +++ b/dlpisubs.c @@ -193,7 +193,7 @@ pcap_process_pkts(pcap_t *p, pcap_handler callback, u_char *user, if (pkthdr.caplen > p->snapshot) pkthdr.caplen = p->snapshot; (*callback)(user, &pkthdr, pk); - if (++n >= count && count >= 0) { + if (++n >= count && !PACKET_COUNT_IS_UNLIMITED(count)) { p->cc = ep - bufp; p->bp = bufp; return (n); diff --git a/pcap-bpf.c b/pcap-bpf.c index bc37639ebd..250298a78d 100644 --- a/pcap-bpf.c +++ b/pcap-bpf.c @@ -1037,7 +1037,7 @@ pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) #endif (*callback)(user, &pkthdr, datap); bp += BPF_WORDALIGN(caplen + hdrlen); - if (++n >= cnt && cnt > 0) { + if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { p->bp = bp; p->cc = ep - bp; /* diff --git a/pcap-dag.c b/pcap-dag.c index 247bee8774..70a6d6c204 100644 --- a/pcap-dag.c +++ b/pcap-dag.c @@ -579,7 +579,7 @@ dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */ processed++; - if (processed == cnt && cnt > 0) + if (processed == cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { /* Reached the user-specified limit. */ return cnt; diff --git a/pcap-dos.c b/pcap-dos.c index 15a9a6803b..cecc73cede 100644 --- a/pcap-dos.c +++ b/pcap-dos.c @@ -335,7 +335,7 @@ pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback, u_char *data) struct pcap_dos *pd = p->priv; int rc, num = 0; - while (num <= cnt || (cnt < 0)) + while (num <= cnt || PACKET_COUNT_IS_UNLIMITED(cnt)) { if (p->fd <= 0) return (-1); diff --git a/pcap-int.h b/pcap-int.h index 08bbcc3d84..0c27ec7ac4 100644 --- a/pcap-int.h +++ b/pcap-int.h @@ -336,6 +336,12 @@ extern int snprintf (char *, size_t, const char *, ...); extern int vsnprintf (char *, size_t, const char *, va_list ap); #endif +/* + * Does the packet count argument to a module's read routine say + * "supply packets until you run out of packets"? + */ +#define PACKET_COUNT_IS_UNLIMITED(count) ((count) <= 0) + /* * Routines that most pcap implementations can use for non-blocking mode. */ diff --git a/pcap-linux.c b/pcap-linux.c index 565abd3ba8..7a05ff2931 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -4333,7 +4333,7 @@ pcap_read_linux_mmap_v1(pcap_t *handle, int max_packets, pcap_handler callback, /* non-positive values of max_packets are used to require all * packets currently available in the ring */ - while ((pkts < max_packets) || (max_packets <= 0)) { + while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) { union thdr h; h.raw = pcap_get_ring_frame(handle, TP_STATUS_USER); @@ -4391,7 +4391,7 @@ pcap_read_linux_mmap_v2(pcap_t *handle, int max_packets, pcap_handler callback, /* non-positive values of max_packets are used to require all * packets currently available in the ring */ - while ((pkts < max_packets) || (max_packets <= 0)) { + while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) { union thdr h; h.raw = pcap_get_ring_frame(handle, TP_STATUS_USER); @@ -4460,7 +4460,7 @@ pcap_read_linux_mmap_v3(pcap_t *handle, int max_packets, pcap_handler callback, /* non-positive values of max_packets are used to require all * packets currently available in the ring */ - while ((pkts < max_packets) || (max_packets <= 0)) { + while ((pkts < max_packets) || PACKET_COUNT_IS_UNLIMITED(max_packets)) { if (handlep->current_packet == NULL) { h.raw = pcap_get_ring_frame(handle, TP_STATUS_USER); if (!h.raw) @@ -4471,7 +4471,7 @@ pcap_read_linux_mmap_v3(pcap_t *handle, int max_packets, pcap_handler callback, } int packets_to_read = handlep->packets_left; - if (max_packets > 0 && packets_to_read > max_packets) { + if (!PACKET_COUNT_IS_UNLIMITED(max_packets) && packets_to_read > max_packets) { packets_to_read = max_packets; } diff --git a/pcap-nit.c b/pcap-nit.c index 60521a43ad..40e640cdc6 100644 --- a/pcap-nit.c +++ b/pcap-nit.c @@ -189,7 +189,7 @@ pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) h.len = nh->nh_wirelen; h.caplen = caplen; (*callback)(user, &h, cp); - if (++n >= cnt && cnt > 0) { + if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { p->cc = ep - bp; p->bp = bp; return (n); diff --git a/pcap-pf.c b/pcap-pf.c index 9a79bdf5f4..8de6e58f63 100644 --- a/pcap-pf.c +++ b/pcap-pf.c @@ -219,7 +219,7 @@ pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) buflen -= pad; h.caplen = buflen; (*callback)(user, &h, p); - if (++n >= cnt && cnt > 0) { + if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { pc->cc = cc; pc->bp = bp; return (n); diff --git a/pcap-snf.c b/pcap-snf.c index 3709d01cd0..ee6ffa4dcc 100644 --- a/pcap-snf.c +++ b/pcap-snf.c @@ -118,7 +118,7 @@ snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) return -1; n = 0; - while (n < cnt || cnt < 0) { + while (n < cnt || PACKET_COUNT_IS_UNLIMITED(cnt)) { /* * Has "pcap_breakloop()" been called? */ diff --git a/pcap-snit.c b/pcap-snit.c index 08c587252c..12656b8e5c 100644 --- a/pcap-snit.c +++ b/pcap-snit.c @@ -201,7 +201,7 @@ pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) h.len = nlp->nh_pktlen; h.caplen = caplen; (*callback)(user, &h, cp); - if (++n >= cnt && cnt > 0) { + if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { p->cc = ep - bp; p->bp = bp; return (n); diff --git a/pcap-usb-linux.c b/pcap-usb-linux.c index e5b76a6316..f0892a7bd9 100644 --- a/pcap-usb-linux.c +++ b/pcap-usb-linux.c @@ -908,8 +908,8 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch } } - /* with max_packets <= 0 we stop afer the first chunk*/ - if ((max_packets <= 0) || (packets == max_packets)) + /* with max_packets specifying "unlimited" we stop afer the first chunk*/ + if (PACKET_COUNT_IS_UNLIMITED(max_packets) || (packets == max_packets)) break; } diff --git a/pcap-win32.c b/pcap-win32.c index 5ea10c97ff..87b3fc6ac2 100644 --- a/pcap-win32.c +++ b/pcap-win32.c @@ -256,7 +256,7 @@ pcap_read_win32_npf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) */ (*callback)(user, (struct pcap_pkthdr*)bp, bp + hdrlen); bp += Packet_WORDALIGN(caplen + hdrlen); - if (++n >= cnt && cnt > 0) { + if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { p->bp = bp; p->cc = ep - bp; return (n); @@ -416,7 +416,7 @@ pcap_read_win32_dag(pcap_t *p, int cnt, pcap_handler callback, u_char *user) header = (dag_record_t*)((char*)header + erf_record_len); /* Stop if the number of packets requested by user has been reached*/ - if (++n >= cnt && cnt > 0) + if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { p->bp = (char*)header; p->cc = endofbuf - (char*)header; diff --git a/pcap.c b/pcap.c index faa9762b73..83d4bb1571 100644 --- a/pcap.c +++ b/pcap.c @@ -858,7 +858,7 @@ pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) } if (n <= 0) return (n); - if (cnt > 0) { + if (!PACKET_COUNT_IS_UNLIMITED(cnt)) { cnt -= n; if (cnt <= 0) return (0);