Skip to content

Commit

Permalink
Do the check for a valid direction value in pcap_setdirection().
Browse files Browse the repository at this point in the history
That way, we don't have to do the check in the individual setdirection
op routines.
  • Loading branch information
guyharris committed Nov 10, 2019
1 parent c24390a commit ddbd328
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 54 deletions.
30 changes: 15 additions & 15 deletions pcap-bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 5 additions & 12 deletions pcap-bt-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 5 additions & 13 deletions pcap-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
17 changes: 5 additions & 12 deletions pcap-usb-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 20 additions & 2 deletions pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ddbd328

Please sign in to comment.