From: Guy Harris Date: Sun, 26 Jul 2009 20:55:35 +0000 (-0700) Subject: Add poll() support to seltest, and rename it to selpolltest. X-Git-Tag: libpcap-1.1.0~101 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/559ebc322fd145d1566131970be62d76e0466687 Add poll() support to seltest, and rename it to selpolltest. Just print "."s for packets seen, and count the packets. Print out the information from the file descriptor bitsets/poll FDs on one line. --- diff --git a/Makefile.in b/Makefile.in index 06fe6ece..c40cd1d7 100644 --- a/Makefile.in +++ b/Makefile.in @@ -123,7 +123,7 @@ GENHDR = \ TAGFILES = \ $(SRC) $(HDR) -CLEANFILES = $(OBJ) libpcap.* filtertest findalldevstest seltest \ +CLEANFILES = $(OBJ) libpcap.* filtertest findalldevstest selpolltest \ $(PROG)-`cat VERSION`.tar.gz $(GENSRC) $(GENHDR) lex.yy.c \ pcap-config @@ -288,7 +288,7 @@ EXTRA_DIST = \ pcap-win32.c \ runlex.sh \ scanner.l \ - seltest.c \ + selpolltest.c \ Win32/Include/Gnuc.h \ Win32/Include/addrinfo.h \ Win32/Include/bittypes.h \ @@ -468,8 +468,8 @@ filtertest: filtertest.c libpcap.a findalldevstest: findalldevstest.c libpcap.a $(CC) $(CFLAGS) -I. -L. -o findalldevstest findalldevstest.c libpcap.a $(LIBS) -seltest: seltest.c libpcap.a - $(CC) $(CFLAGS) -I. -L. -o seltest seltest.c libpcap.a $(LIBS) +selpolltest: selpolltest.c libpcap.a + $(CC) $(CFLAGS) -I. -L. -o selpolltest selpolltest.c libpcap.a $(LIBS) install: install-shared install-archive pcap-config [ -d $(DESTDIR)$(libdir) ] || \ diff --git a/seltest.c b/selpolltest.c similarity index 65% rename from seltest.c rename to selpolltest.c index c16afcb5..03e9395c 100644 --- a/seltest.c +++ b/selpolltest.c @@ -33,6 +33,8 @@ The Regents of the University of California. All rights reserved.\n"; #include #include #include +#include +#include char *program_name; @@ -55,13 +57,16 @@ main(int argc, char **argv) register int op; bpf_u_int32 localnet, netmask; register char *cp, *cmdbuf, *device; - int doselect, dotimeout, dononblock; + int doselect, dopoll, dotimeout, dononblock; struct bpf_program fcode; char ebuf[PCAP_ERRBUF_SIZE]; + int selectable_fd; int status; + int packet_count; device = NULL; doselect = 0; + dopoll = 0; dotimeout = 0; dononblock = 0; if ((cp = strrchr(argv[0], '/')) != NULL) @@ -70,7 +75,7 @@ main(int argc, char **argv) program_name = argv[0]; opterr = 0; - while ((op = getopt(argc, argv, "i:stn")) != -1) + while ((op = getopt(argc, argv, "i:sptn")) != -1) { switch (op) { case 'i': @@ -81,6 +86,10 @@ main(int argc, char **argv) doselect = 1; break; + case 'p': + dopoll = 1; + break; + case 't': dotimeout = 1; break; @@ -93,7 +102,16 @@ main(int argc, char **argv) usage(); /* NOTREACHED */ } + } + if (doselect && dopoll) { + fprintf(stderr, "selpolltest: choose select (-s) or poll (-p), but not both\n"); + return 1; + } + if (dotimeout && !doselect && !dopoll) { + fprintf(stderr, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n"); + return 1; + } if (device == NULL) { device = pcap_lookupdev(ebuf); if (device == NULL) @@ -123,45 +141,101 @@ main(int argc, char **argv) if (pcap_setnonblock(pd, 1, ebuf) == -1) error("pcap_setnonblock failed: %s", ebuf); } + selectable_fd = pcap_get_selectable_fd(pd); printf("Listening on %s\n", device); if (doselect) { for (;;) { - int selectable_fd; fd_set setread, setexcept; - struct timeval timeout; + struct timeval seltimeout; - selectable_fd = pcap_get_selectable_fd(pd); FD_ZERO(&setread); FD_SET(selectable_fd, &setread); FD_ZERO(&setexcept); FD_SET(selectable_fd, &setexcept); if (dotimeout) { - timeout.tv_sec = 0; - timeout.tv_usec = 1000; - select(selectable_fd + 1, &setread, NULL, - &setexcept, &timeout); - } else - select(selectable_fd + 1, &setread, NULL, - &setexcept, NULL); - if (FD_ISSET(selectable_fd, &setread)) - printf("Select says descriptor is readable\n"); - else - printf("Select doesn't say descriptor is readable\n"); - if (FD_ISSET(selectable_fd, &setexcept)) - printf("Select says descriptor has exceptional condition\n"); + seltimeout.tv_sec = 0; + seltimeout.tv_usec = 1000; + status = select(selectable_fd + 1, &setread, + NULL, &setexcept, &seltimeout); + } else { + status = select(selectable_fd + 1, &setread, + NULL, &setexcept, NULL); + } + if (status == -1) { + printf("Select returns error (%s)\n", + strerror(errno)); + } else { + if (status == 0) + printf("Select timed out: "); + else + printf("Select returned a descriptor: "); + if (FD_ISSET(selectable_fd, &setread)) + printf("readable, "); + else + printf("not readable, "); + if (FD_ISSET(selectable_fd, &setexcept)) + printf("exceptional condition\n"); + else + printf("no exceptional condition\n"); + packet_count = 0; + status = pcap_dispatch(pd, -1, printme, + (u_char *)&packet_count); + if (status < 0) + break; + printf("\n%d packets seen, %d packets counted after select returns\n", + status, packet_count); + } + } + } else if (dopoll) { + for (;;) { + struct pollfd fd; + int polltimeout; + + fd.fd = selectable_fd; + fd.events = POLLIN; + if (dotimeout) + polltimeout = 1; else - printf("Select doesn't say descriptor has exceptional condition\n"); - status = pcap_dispatch(pd, -1, printme, NULL); - if (status < 0) - break; - else if (status == 0) - printf("No packets seen after select returns\n"); - else - printf("%d packets seen after select returns\n", - status); + polltimeout = -1; + status = poll(&fd, 1, polltimeout); + if (status == -1) { + printf("Poll returns error (%s)\n", + strerror(errno)); + } else { + if (status == 0) + printf("Poll timed out\n"); + else { + printf("Poll returned a descriptor: "); + if (fd.revents & POLLIN) + printf("readable, "); + else + printf("not readable, "); + if (fd.revents & POLLERR) + printf("exceptional condition, "); + else + printf("no exceptional condition, "); + if (fd.revents & POLLHUP) + printf("disconnect\n"); + else + printf("no disconnect\n"); + } + packet_count = 0; + status = pcap_dispatch(pd, -1, printme, + (u_char *)&packet_count); + if (status < 0) + break; + printf("\n%d packets seen, %d packets counted after poll returns\n", + status, packet_count); + } } - } else - status = pcap_loop(pd, -1, printme, NULL); + } else { + packet_count = 0; + status = pcap_loop(pd, -1, printme, (u_char *)&packet_count); + if (status >= 0) { + printf("\n%d packets seen, %d packets counted by pcap_loop\n", + status, packet_count); + } + } if (status == -2) { /* * We got interrupted, so perhaps we didn't @@ -185,7 +259,11 @@ main(int argc, char **argv) static void printme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) { - printf("Saw a packet\n"); + int *counterp = (int *)user; + + printf("."); + fflush(stdout); + (*counterp)++; } static void