--- libaitio/example/bpf.c 2013/06/25 16:53:15 1.1.2.5 +++ libaitio/example/bpf.c 2016/08/11 16:23:43 1.4.12.2 @@ -1,3 +1,10 @@ +#ifdef __linux__ +int main() +{ + return 0; +} +#else + #include #include #include @@ -61,7 +68,6 @@ static void * ShowPkt(void *buffer) { char Proto = 0, szStr[BUFSIZ], szLine[BUFSIZ], szWrk[BUFSIZ], szShow[USHRT_MAX] = { 0 }; - struct bpf_hdr *bpf = buffer; #ifdef __FreeBSD__ struct bpf_zbuf_header *bzh = buffer; struct icmphdr *icmp; @@ -78,15 +84,7 @@ ShowPkt(void *buffer) assert(buffer); -#ifdef __FreeBSD__ - snprintf(szLine, BUFSIZ, "#Packet length: %d\n>>> Ethernet ...\n", flg ? bzh->bzh_kernel_len : bpf->bh_datalen); - strlcat(szShow, szLine, USHRT_MAX); - eth = (struct ether_header *) (buffer + (flg ? bzh->bzh_kernel_len : bpf->bh_hdrlen)); -#else - snprintf(szLine, BUFSIZ, "#Packet length: %d\n>>> Ethernet ...\n", bpf->bh_datalen); - strlcat(szShow, szLine, USHRT_MAX); - eth = (struct ether_header *) (buffer + bpf->bh_hdrlen); -#endif + eth = (struct ether_header *) buffer; switch (ntohs(eth->ether_type)) { case ETHERTYPE_ARP: @@ -246,7 +244,7 @@ ShowPkt(void *buffer) } printf("%s===\n", szShow); - pthread_exit(NULL); + return NULL; } // ---------------------- @@ -264,6 +262,7 @@ main(int argc, char **argv) char ch, mode = 'R'; struct timespec ts_start, ts_end; void *bz = NULL; + ether_addr_t ea; while ((ch = getopt(argc, argv, "hvwzs:p:f:")) != -1) switch (ch) { @@ -300,11 +299,11 @@ main(int argc, char **argv) strlcpy(szStr, *argv, sizeof szStr); #ifdef __FreeBSD__ - dev = io_etherOpen(szStr, O_RDWR, 42, (u_int*) &siz, &bz); + dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, (flg) ? &bz : NULL); if (dev == -1) - dev = io_etherOpen(szStr, O_RDWR, 42, (u_int*) &siz, NULL); + dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, NULL); #else - dev = io_etherOpen(szStr, O_RDWR, 42, (u_int*) &siz, NULL); + dev = io_etherOpen(szStr, O_RDWR, 42, 0, (u_int*) &siz, NULL); #endif if (dev == -1) { printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError()); @@ -312,30 +311,28 @@ 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)"); io_etherClose(dev, &bz); return 1; } else - printf("BPF buffer len=%d\n", n); + printf("BPF buffer len=%d\n", siz); - if (!flg) { - if (*szMap) { - fd = open(szMap, O_RDWR); - if (fd == -1) { - perror("open(map)"); - io_etherClose(dev, &bz); - 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()"); + if (*szMap) { + fd = open(szMap, O_RDWR); + if (fd == -1) { + perror("open(map)"); io_etherClose(dev, &bz); 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()"); + io_etherClose(dev, &bz); + return 1; } pfd.fd = dev; @@ -343,31 +340,19 @@ main(int argc, char **argv) if (mode == 'R') { pfd.events = POLLIN; for (i = 0; i < count; i++) { - if (poll(&pfd, 1, -1) == -1) + if ((ret = poll(&pfd, 1, -1)) == -1) break; -#if 0 -#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) + continue; + 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); + ShowPkt(buffer); } -#endif -#endif } } else { pfd.events = POLLOUT; @@ -384,8 +369,7 @@ main(int argc, char **argv) } assert(!clock_gettime(CLOCK_REALTIME, &ts_end)); - if (!flg) - munmap(buffer, siz); + munmap(buffer, siz); io_etherClose(dev, &bz); time_spec_sub(&ts_end, &ts_start); @@ -394,3 +378,5 @@ main(int argc, char **argv) printf("0.%09lu per/iteration\n", ts_end.tv_sec + ts_end.tv_nsec / i); return 0; } + +#endif