]> The Tcpdump Group git mirrors - libpcap/commitdiff
Don't use non-blocking mode unless we have a timeout or use -n.
authorGuy Harris <[email protected]>
Thu, 1 Feb 2018 04:18:19 +0000 (20:18 -0800)
committerGuy Harris <[email protected]>
Thu, 1 Feb 2018 04:18:19 +0000 (20:18 -0800)
We only need it if we're using select()/poll() and don't have a
descriptor on which we can block in select()/poll() waiting for packets
to be readable, so that we have to use a timeout and try reading when
the timeout expires; if we can block on the descriptor, that'll let us
know when the read won't block, and if we aren't using select()/poll(),
pcap_dispatch() will wait until packets are available (or the packet
buffer timeout expires).

tests/selpolltest.c

index 614056d9a09c5d4319ef2ce8559ecdc405872f80..5d47b3d5cbe91f59c7904165f27f34b35f193058 100644 (file)
@@ -82,7 +82,7 @@ main(int argc, char **argv)
        device = NULL;
        doselect = 0;
        dopoll = 0;
-       mechanism = "pcap_dispatch()";
+       mechanism = NULL;
        dotimeout = 0;
        dononblock = 0;
        if ((cp = strrchr(argv[0], '/')) != NULL)
@@ -153,31 +153,41 @@ main(int argc, char **argv)
 
        if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
                error("%s", pcap_geterr(pd));
-
        if (pcap_setfilter(pd, &fcode) < 0)
                error("%s", pcap_geterr(pd));
-       selectable_fd = pcap_get_selectable_fd(pd);
-       if (selectable_fd == -1) {
-               printf("Listening on %s, using %s, with a timeout\n",
-                   device, mechanism);
-               required_timeout = pcap_get_required_select_timeout(pd);
-               if (required_timeout == NULL)
-                       if (doselect || dopoll)
-                               error("select()/poll() isn't supported on %s, even with a timeout",
-                                   device);
+
+       if (doselect || dopoll) {
                /*
-                * As we won't be notified by select() or poll() that
-                * a read can be done, we'll have to periodically try
-                * reading from the device every time the required
-                * timeout expires, and we don't want those attempts
-                * to block if nothing has arrived in that interval,
-                * so we want to force non-blocking mode.
+                * We need either an FD on which to do select()/poll()
+                * or, if there isn't one, a timeout to use in select()/
+                * poll().
                 */
-               dononblock = 1;
-       } else {
-               printf("Listening on %s, using %s\n", device, mechanism);
-               required_timeout = NULL;
-       }
+               selectable_fd = pcap_get_selectable_fd(pd);
+               if (selectable_fd == -1) {
+                       printf("Listening on %s, using %s, with a timeout\n",
+                           device, mechanism);
+                       required_timeout = pcap_get_required_select_timeout(pd);
+                       if (required_timeout == NULL)
+                               error("select()/poll() isn't supported on %s, even with a timeout",
+                                   device);
+
+                       /*
+                        * As we won't be notified by select() or poll()
+                        * that a read can be done, we'll have to periodically
+                        * try reading from the device every time the required
+                        * timeout expires, and we don't want those attempts
+                        * to block if nothing has arrived in that interval,
+                        * so we want to force non-blocking mode.
+                        */
+                       dononblock = 1;
+               } else {
+                       printf("Listening on %s, using %s\n", device,
+                           mechanism);
+                       required_timeout = NULL;
+               }
+       } else
+               printf("Listening on %s, using pcap_dispatch()\n", device);
+
        if (dononblock) {
                if (pcap_setnonblock(pd, 1, ebuf) == -1)
                        error("pcap_setnonblock failed: %s", ebuf);
@@ -245,7 +255,7 @@ main(int argc, char **argv)
                                 * a ton of "no packets" reports.
                                 */
                                if (status != 0 || packet_count != 0 ||
-                                   selectable_fd != -1) {
+                                   required_timeout != NULL) {
                                        printf("%d packets seen, %d packets counted after select returns\n",
                                            status, packet_count);
                                }
@@ -309,7 +319,7 @@ main(int argc, char **argv)
                                 * a ton of "no packets" reports.
                                 */
                                if (status != 0 || packet_count != 0 ||
-                                   selectable_fd != -1) {
+                                   required_timeout != NULL) {
                                        printf("%d packets seen, %d packets counted after poll returns\n",
                                            status, packet_count);
                                }