--- libaitio/src/bpf.c 2013/10/18 14:21:01 1.3.8.1 +++ libaitio/src/bpf.c 2013/10/21 16:12:57 1.3.8.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: bpf.c,v 1.3.8.1 2013/10/18 14:21:01 misho Exp $ +* $Id: bpf.c,v 1.3.8.3 2013/10/21 16:12:57 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -169,20 +169,37 @@ io_etherOpen(const char *csIface, int flags, int whdr, return -1; } + if (csIface) + strlcpy(szStr, csIface, sizeof szStr); + else if (io_get1stiface(szStr, sizeof szStr) == -1) { + close(eth); + return -1; + } + + n = 1; + if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) { + LOGERR; + close(eth); + return -1; + } + if (wdlt && ioctl(eth, BIOCSDLT, &wdlt) == -1) { + LOGERR; + close(eth); + return -1; + } + if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) { + LOGERR; + close(eth); + return -1; + } + if (!zcbuf) { - if (ioctl(eth, BIOCGBLEN, &n) == -1) { - LOGERR; - close(eth); - return -1; - } else - n = (buflen && *buflen) ? *buflen : n; + n = (buflen && *buflen) ? *buflen : getpagesize(); if (ioctl(eth, BIOCSBLEN, &n) == -1) { LOGERR; close(eth); return -1; } - if (buflen && *buflen) - *buflen = n; } else { #ifdef __FreeBSD__ n = BPF_BUFMODE_ZBUF; @@ -211,12 +228,6 @@ io_etherOpen(const char *csIface, int flags, int whdr, #endif } - if (csIface) - strlcpy(szStr, csIface, sizeof szStr); - else if (io_get1stiface(szStr, sizeof szStr) == -1) { - io_etherClose(eth, zcbuf); - return -1; - } memset(&ifr, 0, sizeof ifr); strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name); if (ioctl(eth, BIOCSETIF, &ifr) == -1) { @@ -225,22 +236,14 @@ io_etherOpen(const char *csIface, int flags, int whdr, return -1; } - n = 1; - if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) { - LOGERR; - io_etherClose(eth, zcbuf); - return -1; + if (buflen && *buflen) { + if (ioctl(eth, BIOCGBLEN, &n) == -1) { + LOGERR; + io_etherClose(eth, zcbuf); + return -1; + } else + *buflen = n; } - if (wdlt && ioctl(eth, BIOCSDLT, &wdlt) == -1) { - LOGERR; - io_etherClose(eth, zcbuf); - return -1; - } - if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) { - LOGERR; - io_etherClose(eth, zcbuf); - return -1; - } return eth; } @@ -337,8 +340,10 @@ io_etherRecv(int eth, void * __restrict buf, size_t bu if (!zcbuf) { rlen = read(eth, buf, buflen); - if (rlen == -1) + if (rlen == -1) { LOGERR; + return -1; + } } else { #ifdef __FreeBSD__ rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen);