Skip to content

Commit

Permalink
Add a PACKET_COUNT_IS_UNLIMITED() to test for a packet count <= 0.
Browse files Browse the repository at this point in the history
In read routines, a packet count <= 0 means "keep supplying packets
until you run out of packets in the buffer", and it means "keep supply
packets until the loop is broken out of or you get an error" in
pcap_loop().

Use the macro in all tests for that, so the right test is always done
(i.e., a count of 0 means "unlimited", not "supply zero packets"); this
fixes some cases where we weren't doing the right test (and hopefully
encourages programmers to use it and get the test right in new modules).
  • Loading branch information
guyharris committed Dec 15, 2013
1 parent 1a52c9a commit 86b47f1
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dlpisubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion pcap-bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/*
Expand Down
2 changes: 1 addition & 1 deletion pcap-dag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pcap-dos.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions pcap-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
8 changes: 4 additions & 4 deletions pcap-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion pcap-nit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion pcap-pf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion pcap-snf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*/
Expand Down
2 changes: 1 addition & 1 deletion pcap-snit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions pcap-usb-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions pcap-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 86b47f1

Please sign in to comment.