From: Jakub Zawadzki Date: Wed, 1 May 2013 17:10:17 +0000 (+0200) Subject: [Linux] add new function linux_check_direction() X-Git-Tag: libpcap-1.5.0~56^2~21 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/43a4a4d17dab8168a08f30cbbbfed87e9e274d1f [Linux] add new function linux_check_direction() We're right now have the same code in two places, split it into new inline function: linux_check_direction() Merged-with-my-pcap_t-changes-by: Guy Harris --- diff --git a/pcap-linux.c b/pcap-linux.c index e49af804..9fe97f30 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -1377,6 +1377,42 @@ pcap_set_datalink_linux(pcap_t *handle, int dlt) return 0; } +/* + * linux_check_direction() + * + * Do checks based on packet direction. + */ +static inline int +linux_check_direction(const pcap_t *handle, const struct sockaddr_ll *sll) +{ + struct pcap_linux *handlep = handle->private; + + if (sll->sll_pkttype == PACKET_OUTGOING) { + /* + * Outgoing packet. + * If this is from the loopback device, reject it; + * we'll see the packet as an incoming packet as well, + * and we don't want to see it twice. + */ + if (sll->sll_ifindex == handlep->lo_ifindex) + return 0; + + /* + * If the user only wants incoming packets, reject it. + */ + if (handle->direction == PCAP_D_IN) + return 0; + } else { + /* + * Incoming packet. + * If the user only wants outgoing packets, reject it. + */ + if (handle->direction == PCAP_D_OUT) + return 0; + } + return 1; +} + /* * Read a packet from the socket calling the handler provided by * the user. Returns the number of packets received or -1 if an @@ -1531,29 +1567,8 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata) * address returned for SOCK_PACKET is a "sockaddr_pkt" * which lacks the relevant packet type information. */ - if (from.sll_pkttype == PACKET_OUTGOING) { - /* - * Outgoing packet. - * If this is from the loopback device, reject it; - * we'll see the packet as an incoming packet as well, - * and we don't want to see it twice. - */ - if (from.sll_ifindex == handlep->lo_ifindex) - return 0; - - /* - * If the user only wants incoming packets, reject it. - */ - if (handle->direction == PCAP_D_IN) - return 0; - } else { - /* - * Incoming packet. - * If the user only wants outgoing packets, reject it. - */ - if (handle->direction == PCAP_D_OUT) - return 0; - } + if (!linux_check_direction(handle, &from)) + return 0; } #endif @@ -4000,33 +4015,9 @@ pcap_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, tp_len, tp_snaplen) == 0)) goto skip; - /* - * Do checks based on packet direction. - */ sll = (void *)h.raw + TPACKET_ALIGN(handlep->tp_hdrlen); - if (sll->sll_pkttype == PACKET_OUTGOING) { - /* - * Outgoing packet. - * If this is from the loopback device, reject it; - * we'll see the packet as an incoming packet as well, - * and we don't want to see it twice. - */ - if (sll->sll_ifindex == handlep->lo_ifindex) - goto skip; - - /* - * If the user only wants incoming packets, reject it. - */ - if (handle->direction == PCAP_D_IN) - goto skip; - } else { - /* - * Incoming packet. - * If the user only wants outgoing packets, reject it. - */ - if (handle->direction == PCAP_D_OUT) - goto skip; - } + if (!linux_check_direction(handle, sll)) + goto skip; /* get required packet info from ring header */ pcaphdr.ts.tv_sec = tp_sec;