--- libaitio/example/bpf.c 2013/06/25 09:08:10 1.1.2.1 +++ libaitio/example/bpf.c 2013/06/26 12:27:40 1.1.2.9 @@ -37,6 +37,8 @@ #include #endif +#include + #ifndef roundup #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */ #endif @@ -77,9 +79,10 @@ ShowPkt(void *buffer) assert(buffer); #ifdef __FreeBSD__ - snprintf(szLine, BUFSIZ, "#Packet length: %d\n>>> Ethernet ...\n", flg ? bzh->bzh_kernel_len : bpf->bh_datalen); + bpf = (struct bpf_hdr*) (buffer + (flg ? sizeof(struct bpf_zbuf_header) : 0)); + snprintf(szLine, BUFSIZ, "#Packet length: %d/%d\n>>> Ethernet ...\n", flg ? bzh->bzh_kernel_len : bpf->bh_datalen, bpf->bh_datalen); strlcat(szShow, szLine, USHRT_MAX); - eth = (struct ether_header *) (buffer + (flg ? bzh->bzh_kernel_len : bpf->bh_hdrlen)); + eth = (struct ether_header *) (buffer + (flg ? sizeof(struct bpf_zbuf_header) : 0) + bpf->bh_hdrlen); #else snprintf(szLine, BUFSIZ, "#Packet length: %d\n>>> Ethernet ...\n", bpf->bh_datalen); strlcat(szShow, szLine, USHRT_MAX); @@ -261,9 +264,7 @@ main(int argc, char **argv) pthread_t tid; char ch, mode = 'R'; struct timespec ts_start, ts_end; -#ifdef __FreeBSD__ - struct bpf_zbuf *bz; -#endif + void *bz = NULL; while ((ch = getopt(argc, argv, "hvwzs:p:f:")) != -1) switch (ch) { @@ -299,10 +300,12 @@ main(int argc, char **argv) else strlcpy(szStr, *argv, sizeof szStr); -#ifdef __FreeBSD_ - dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, &siz, &bz); +#ifdef __FreeBSD__ + dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, (u_int*) &siz, (flg) ? &bz : NULL); + if (dev == -1) + dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, (u_int*) &siz, NULL); #else - dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, &siz, NULL); + dev = io_etherOpen(szStr, O_RDWR, 42, (u_int*) &siz, NULL); #endif if (dev == -1) { printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError()); @@ -310,114 +313,29 @@ main(int argc, char **argv) } else printf("dev=%d(%s)\n", dev, szStr); - if (ioctl(dev, BIOCGBLEN, &n) == -1) { + if (ioctl(dev, BIOCGBLEN, &siz) == -1) { perror("ioctl(BIOCGBLEN)"); - close(dev); + io_etherClose(dev, &bz); return 1; } else - printf("BPF buffer len=%d\n", n); + printf("BPF buffer len=%d\n", siz); -#if 0 -#ifdef __FreeBSD__ - ret = BPF_BUFMODE_ZBUF; - if (flg && !ioctl(dev, BIOCSETBUFMODE, (u_int*) &ret)) { - if (ioctl(dev, BIOCGETZMAX, &ret) == -1) { - perror("ioctl(BIOCGETZMAX)"); - close(dev); + if (*szMap) { + fd = open(szMap, O_RDWR); + if (fd == -1) { + perror("open(map)"); + io_etherClose(dev, &bz); return 1; } - memset(&bz, 0, sizeof bz); - bz.bz_buflen = roundup(MIN(n, ret), getpagesize()); - bz.bz_bufa = mmap(NULL, bz.bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); - bz.bz_bufb = mmap(NULL, bz.bz_buflen, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); - if (bz.bz_bufa == MAP_FAILED || bz.bz_bufb == MAP_FAILED) { - perror("mmap()"); - close(dev); - return 1; - } - - if (ioctl(dev, BIOCSETZBUF, &bz) == -1) { - perror("ioctl(BIOCSETZBUF)"); - munmap(bz.bz_bufa, bz.bz_buflen); - munmap(bz.bz_bufb, bz.bz_buflen); - close(dev); - return 1; - } else - siz = bz.bz_buflen; - } else { -#endif - flg = 0; - if (siz) { - if (ioctl(dev, BIOCSBLEN, &siz) == -1) { - perror("ioctl(BIOCSBLEN)"); - close(dev); - return 1; - } - } else - siz = n; -#ifdef __FreeBSD__ - } -#endif - printf("Set buffer len to %d\n", siz); - - if (!flg) { - if (*szMap) { - fd = open(szMap, O_RDWR); - if (fd == -1) { - perror("open(map)"); - close(dev); - return 1; - } - buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); - close(fd); - } else - buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); - if (buffer == MAP_FAILED) { - perror("mmap()"); - close(dev); - return 1; - } - } - - memset(&ifr, 0, sizeof ifr); - strlcpy(ifr.ifr_name, szStr, sizeof ifr.ifr_name); - if (ioctl(dev, BIOCSETIF, (char*) &ifr) == -1) { - perror("ioctl(BIOCSETIF)"); - close(dev); -#ifdef __FreeBSD__ - if (flg) { - munmap(bz.bz_bufa, bz.bz_buflen); - munmap(bz.bz_bufb, bz.bz_buflen); - } else -#endif - munmap(buffer, siz); + buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); + close(fd); + } else + buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); + if (buffer == MAP_FAILED) { + perror("mmap()"); + io_etherClose(dev, &bz); return 1; } - n = 1; - if (ioctl(dev, BIOCSHDRCMPLT, &n) == -1) { - perror("ioctl(BIOCSHDRCMPLT)"); - close(dev); -#ifdef __FreeBSD__ - if (flg) { - munmap(bz.bz_bufa, bz.bz_buflen); - munmap(bz.bz_bufb, bz.bz_buflen); - } else -#endif - munmap(buffer, siz); - return 1; - } - if (ioctl(dev, BIOCIMMEDIATE, &n) == -1) { - perror("ioctl(BIOCIMMEDIATE)"); - close(dev); -#ifdef __FreeBSD__ - if (flg) { - munmap(bz.bz_bufa, bz.bz_buflen); - munmap(bz.bz_bufb, bz.bz_buflen); - } else -#endif - munmap(buffer, siz); - return 1; - } pfd.fd = dev; assert(!clock_gettime(CLOCK_REALTIME, &ts_start)); @@ -427,46 +345,32 @@ main(int argc, char **argv) if (poll(&pfd, 1, -1) == -1) break; -#ifdef __FreeBSD__ - if (flg) { - if (!NEXT_zbuf((void**) &buffer, &bz, &ret)) - continue; - if (Verbose) { - printf("+readed %d bytes\n", ret); - pthread_create(&tid, NULL, ShowPkt, buffer); - } - } else { -#endif - ret = read(dev, buffer, siz); - if (ret == -1) - printf("%d) read(%d) #%d - %s\n", i, ret, errno, strerror(errno)); - if (Verbose) { - printf("%d) +readed %d bytes\n", i, ret); - pthread_create(&tid, NULL, ShowPkt, buffer); - } -#ifdef __FreeBSD__ + ret = io_etherRecv(dev, buffer, siz, bz); + if (ret == -1) + printf("%d) io_etherRecv(%d) #%d - %s\n", i, ret, + io_GetErrno(), io_GetError()); + if (Verbose) { + printf("%d) +readed %d bytes\n", i, ret); + pthread_create(&tid, NULL, ShowPkt, buffer); + pthread_join(tid, NULL); } -#endif } } else { pfd.events = POLLOUT; for (i = 0; i < count; i++) { -#ifdef __FreeBSD__ if (poll(&pfd, 1, -1) == -1) break; -#endif - ret = write(dev, buffer, siz); + ret = io_etherSend(dev, buffer, siz); if (ret == -1) - printf("write(%d) #%d - %s\n", ret, errno, strerror(errno)); + printf("io_etherSend(%d) #%d - %s\n", ret, io_GetErrno(), io_GetError()); if (Verbose) printf("+writed %d bytes\n", ret); } } assert(!clock_gettime(CLOCK_REALTIME, &ts_end)); -#endif -// munmap(buffer, siz); + munmap(buffer, siz); io_etherClose(dev, &bz); time_spec_sub(&ts_end, &ts_start);