]> The Tcpdump Group git mirrors - libpcap/commitdiff
Do the check for a valid direction value in pcap_setdirection().
authorGuy Harris <[email protected]>
Sun, 10 Nov 2019 03:44:33 +0000 (19:44 -0800)
committerGuy Harris <[email protected]>
Sun, 10 Nov 2019 03:44:33 +0000 (19:44 -0800)
That way, we don't have to do the check in the individual setdirection
op routines.

pcap-bpf.c
pcap-bt-linux.c
pcap-linux.c
pcap-usb-linux.c
pcap.c

index 1f2b96b995f199a22cc870690f617b9b1ed8ed00..0d8f2d6e9c207cb0c8c3bc69df154b9e86619e07 100644 (file)
@@ -3283,18 +3283,18 @@ pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
                direction_name = "\"outgoing only\"";
                break;
 
-       case PCAP_D_INOUT:
+       default:
                /*
                 * Incoming and outgoing, so accept both
                 * incoming and outgoing packets.
+                *
+                * It's guaranteed, at this point, that d is a valid
+                * direction value, so we know that this is PCAP_D_INOUT
+                * if it's not PCAP_D_IN or PCAP_D_OUT.
                 */
                direction = BPF_D_INOUT;
                direction_name = "\"incoming and outgoing\"";
                break;
-
-       default:
-               snprintf(p->errbuf, sizeof(p->errbuf), "Invalid direction");
-               return (-1);
        }
                
        if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
@@ -3337,18 +3337,18 @@ pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
                direction_name = "\"outgoing only\"";
                break;
 
-       case PCAP_D_INOUT:
+       default:
                /*
                 * Incoming and outgoing, so don't filter out
                 * any packets based on direction.
+                *
+                * It's guaranteed, at this point, that d is a valid
+                * direction value, so we know that this is PCAP_D_INOUT
+                * if it's not PCAP_D_IN or PCAP_D_OUT.
                 */
                dirfilt = 0;
                direction_name = "\"incoming and outgoing\"";
                break;
-
-       default:
-               snprintf(p->errbuf, sizeof(p->errbuf), "Invalid direction");
-               return (-1);
        }
        if (ioctl(p->fd, BIOCSDIRFILT, &dirfilt) == -1) {
                pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
@@ -3386,18 +3386,18 @@ pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
                    "Setting direction to \"outgoing only\" is not supported on this device");
                return (-1);
 
-       case PCAP_D_INOUT:
+       default:
                /*
                 * Incoming and outgoing, so we want to see transmitted
                 * packets.
+                *
+                * It's guaranteed, at this point, that d is a valid
+                * direction value, so we know that this is PCAP_D_INOUT
+                * if it's not PCAP_D_IN or PCAP_D_OUT.
                 */
                seesent = 1;
                direction_name = "\"incoming and outgoing\"";
                break;
-
-       default:
-               snprintf(p->errbuf, sizeof(p->errbuf), "Invalid direction");
-               return (-1);
        }
                
        if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
index 47ff969f9b9f95c1c7d5c428ad82eb8c82a97e94..dbaa92ea46ffa3a39567a50fd2929b7be6018dd3 100644 (file)
@@ -430,17 +430,10 @@ bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
 static int
 bt_setdirection_linux(pcap_t *p, pcap_direction_t d)
 {
-       switch (d) {
-
-       case PCAP_D_IN:
-       case PCAP_D_OUT:
-       case PCAP_D_INOUT:
-               p->direction = d;
-               break;
-
-       default:
-               snprintf(p->errbuf, sizeof(p->errbuf), "Invalid direction");
-               return (-1);
-       }
+       /*
+        * It's guaranteed, at this point, that d is a valid
+        * direction value.
+        */
+       p->direction = d;
        return 0;
 }
index e0da92f3b1d4b0f4f53f3ec5c90db7182198ff5c..83cf9777f30823a45b5153d003d902a39a8a0d71 100644 (file)
@@ -2610,19 +2610,11 @@ pcap_setfilter_linux(pcap_t *handle, struct bpf_program *filter)
 static int
 pcap_setdirection_linux(pcap_t *handle, pcap_direction_t d)
 {
-       switch (d) {
-
-       case PCAP_D_IN:
-       case PCAP_D_OUT:
-       case PCAP_D_INOUT:
-               handle->direction = d;
-               break;
-
-       default:
-               snprintf(handle->errbuf, sizeof(handle->errbuf),
-                   "Invalid direction");
-               return -1;
-       }
+       /*
+        * It's guaranteed, at this point, that d is a valid
+        * direction value.
+        */
+       handle->direction = d;
        return 0;
 }
 
index d4d189e03ba85763c1cfb3000f701d8898e13d75..952f22d03ef8d63af7ba06cc8d1dad273091678e 100644 (file)
@@ -1109,18 +1109,11 @@ usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
 static int
 usb_setdirection_linux(pcap_t *p, pcap_direction_t d)
 {
-       switch (d) {
-
-       case PCAP_D_IN:
-       case PCAP_D_OUT:
-       case PCAP_D_INOUT:
-               p->direction = d;
-               break;
-
-       default:
-               snprintf(p->errbuf, sizeof(p->errbuf), "Invalid direction");
-               return -1;
-       }
+       /*
+        * It's guaranteed, at this point, that d is a valid
+        * direction value.
+        */
+       p->direction = d;
        return 0;
 }
 
diff --git a/pcap.c b/pcap.c
index e4c0a1f3909790e2c9e57652c2e2442d69146ee5..363deb56ee44f794aacabfa71f1bbe290a378e81 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -3533,8 +3533,26 @@ pcap_setdirection(pcap_t *p, pcap_direction_t d)
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
                    "Setting direction is not supported on this device");
                return (-1);
-       } else
-               return (p->setdirection_op(p, d));
+       } else {
+               switch (d) {
+
+               case PCAP_D_IN:
+               case PCAP_D_OUT:
+               case PCAP_D_INOUT:
+                       /*
+                        * Valid direction.
+                        */
+                       return (p->setdirection_op(p, d));
+
+               default:
+                       /*
+                        * Invalid direction.
+                        */
+                       snprintf(p->errbuf, sizeof(p->errbuf),
+                           "Invalid direction");
+                       return (-1);
+               }
+       }       
 }
 
 int