Annotation of libaitio/example/bpf.c, revision 1.5

1.2       misho       1: #include <stdio.h>
                      2: #include <stdlib.h>
                      3: #include <fcntl.h>
                      4: #include <unistd.h>
                      5: #include <string.h>
                      6: #include <errno.h>
                      7: #include <poll.h>
                      8: #include <getopt.h>
                      9: #include <pthread.h>
                     10: #include <assert.h>
                     11: #include <sys/types.h>
                     12: #include <sys/param.h>
                     13: #include <sys/socket.h>
                     14: #include <sys/ioctl.h>
                     15: #include <sys/mman.h>
                     16: #include <net/if.h>
1.5     ! misho      17: #ifndef __linux__
        !            18: #include <sys/limits.h>
1.2       misho      19: #include <net/bpf.h>
                     20: #include <machine/atomic.h>
1.5     ! misho      21: #else
        !            22: #include <linux/if_ether.h>
        !            23: #endif
1.2       misho      24: 
                     25: #ifdef __FreeBSD__
                     26: #include <net/ethernet.h>
                     27: #endif
                     28: #ifdef __OpenBSD__
                     29: #include <net/ethertypes.h>
                     30: #include <netinet/in_systm.h>
                     31: #endif
                     32: #include <net/if_arp.h>
                     33: #include <netinet/in.h>
                     34: #include <netinet/ip.h>
                     35: #include <netinet/ip6.h>
                     36: #include <netinet/udp.h>
                     37: #include <netinet/tcp.h>
                     38: #include <netinet/ip_icmp.h>
                     39: #include <arpa/inet.h>
                     40: #ifdef __OpenBSD__
                     41: #include <netinet/if_ether.h>
                     42: #endif
                     43: 
                     44: #include <aitio.h>
                     45: 
                     46: #ifndef roundup
                     47: #define roundup(x, y)   ((((x) + ((y) - 1)) / (y)) * (y))  /* to any y */
                     48: #endif
                     49: 
                     50: #define time_spec_sub(vvp, uvp)                                           \
                     51:         do {                                                            \
                     52:                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
                     53:                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
                     54:                 if ((vvp)->tv_nsec < 0) {                               \
                     55:                         (vvp)->tv_sec--;                                \
                     56:                         (vvp)->tv_nsec += 1000000000;                   \
                     57:                 }                                                       \
                     58:         } while (0)
                     59: 
                     60: 
                     61: int Verbose, flg;
                     62: 
                     63: 
                     64: static void *
                     65: ShowPkt(void *buffer)
                     66: {
                     67:        char Proto = 0, szStr[BUFSIZ], szLine[BUFSIZ], szWrk[BUFSIZ], szShow[USHRT_MAX] = { 0 };
1.5     ! misho      68: #ifndef __linux__
1.2       misho      69: #ifdef __FreeBSD__
                     70:        struct bpf_zbuf_header *bzh = buffer;
                     71:        struct icmphdr *icmp;
                     72: #endif
                     73: #ifdef __OpenBSD__
                     74:        struct icmp *icmp;
                     75: #endif
1.5     ! misho      76: 
1.2       misho      77:        struct ether_header *eth;
1.5     ! misho      78: #else
        !            79:        struct icmphdr *icmp;
        !            80:        struct ethhdr *eth;
        !            81: #endif
1.2       misho      82:        struct ip *ip;
                     83:        struct ip6_hdr *ipv6;
                     84:        struct arphdr *arp;
                     85:        struct udphdr *udp;
                     86:        struct tcphdr *tcp;
                     87: 
                     88:        assert(buffer);
                     89: 
1.5     ! misho      90: #ifndef __linux__
1.3       misho      91:        eth = (struct ether_header *) buffer;
1.2       misho      92: 
                     93:        switch (ntohs(eth->ether_type)) {
1.5     ! misho      94: #else
        !            95: #define ETHERTYPE_ARP 0x0806
        !            96: #define ETHERTYPE_IP 0x0800
        !            97: #define ETHERTYPE_IPV6 0x86dd
        !            98: #define ETHER_HDR_LEN sizeof(struct ethhdr)
        !            99: 
        !           100:        eth = (struct ethhdr *) buffer;
        !           101: 
        !           102:        switch (ntohs(eth->h_proto)) {
        !           103: #endif
1.2       misho     104:                case ETHERTYPE_ARP:
                    105:                        strlcpy(szWrk, "(ARP)", sizeof szWrk);
                    106:                        break;
                    107:                case ETHERTYPE_IP:
                    108:                        strlcpy(szWrk, "(IP)", sizeof szWrk);
                    109:                        break;
                    110:                case ETHERTYPE_IPV6:
                    111:                        strlcpy(szWrk, "(IPv6)", sizeof szWrk);
                    112:                        break;
                    113:                default:
                    114:                        memset(szWrk, 0, sizeof szWrk);
                    115:        }
1.5     ! misho     116: #ifndef __linux__
1.2       misho     117:        snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_shost[0], eth->ether_shost[1], 
                    118:                        eth->ether_shost[2], eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]);
                    119:        snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->ether_type), szWrk, szStr);
                    120:        strlcat(szShow, szLine, USHRT_MAX);
                    121:        snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_dhost[0], eth->ether_dhost[1], 
                    122:                        eth->ether_dhost[2], eth->ether_dhost[3], eth->ether_dhost[4], eth->ether_dhost[5]);
1.5     ! misho     123: #else
        !           124:        snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->h_source[0], eth->h_source[1], 
        !           125:                        eth->h_source[2], eth->h_source[3], eth->h_source[4], eth->h_source[5]);
        !           126:        snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->h_proto), szWrk, szStr);
        !           127:        strlcat(szShow, szLine, USHRT_MAX);
        !           128:        snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->h_dest[0], eth->h_dest[1], 
        !           129:                        eth->h_dest[2], eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
        !           130: #endif
1.2       misho     131:        snprintf(szLine, BUFSIZ, "dst_mac: %s\n", szStr);
                    132:        strlcat(szShow, szLine, USHRT_MAX);
                    133: 
1.5     ! misho     134: #ifndef __linux__
1.2       misho     135:        switch (ntohs(eth->ether_type)) {
1.5     ! misho     136: #else
        !           137: #define ARPOP_REVREQUEST 3
        !           138: #define ARPOP_REVREPLY 4
        !           139: #define ARPOP_INVREQUEST 8
        !           140: #define ARPOP_INVREPLY 9
        !           141: #define IPV6_FLOWLABEL_MASK 0x000fffff
        !           142: 
        !           143:        switch (ntohs(eth->h_proto)) {
        !           144: #endif
1.2       misho     145:                case ETHERTYPE_ARP:
                    146:                        arp = (struct arphdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
                    147:                        strlcat(szShow, "\t>>> ARP ...\n", USHRT_MAX);
                    148: 
                    149:                        switch (ntohs(arp->ar_op)) {
                    150:                                case ARPOP_REQUEST:
                    151:                                        strlcpy(szStr, "Request", sizeof szStr);
                    152:                                        break;
                    153:                                case ARPOP_REPLY:
                    154:                                        strlcpy(szStr, "Reply", sizeof szStr);
                    155:                                        break;
                    156:                                case ARPOP_REVREQUEST:
                    157:                                        strlcpy(szStr, "RevRequest", sizeof szStr);
                    158:                                        break;
                    159:                                case ARPOP_REVREPLY:
                    160:                                        strlcpy(szStr, "RevReply", sizeof szStr);
                    161:                                        break;
                    162:                                case ARPOP_INVREQUEST:
                    163:                                        strlcpy(szStr, "InvRequest", sizeof szStr);
                    164:                                        break;
                    165:                                case ARPOP_INVREPLY:
                    166:                                        strlcpy(szStr, "InvReply", sizeof szStr);
                    167:                                        break;
                    168:                                default:
                    169:                                        strlcpy(szStr, "*Unknown*", sizeof szStr);
                    170:                        }
                    171:                        snprintf(szLine, BUFSIZ, "\tAddr_fmt: %d, Proto_fmt: %d, Addr_len: %d, Proto_len: %d, %s\n", 
                    172:                                        ntohs(arp->ar_hrd), ntohs(arp->ar_pro), arp->ar_hln, arp->ar_pln, szStr);
                    173:                        strlcat(szShow, szLine, USHRT_MAX);
                    174:                        break;
                    175:                case ETHERTYPE_IP:
                    176:                        ip = (struct ip*) (((caddr_t) eth) + ETHER_HDR_LEN);
                    177:                        strlcat(szShow, "\t>>> IP ...\n", USHRT_MAX);
                    178: 
                    179:                        snprintf(szStr, BUFSIZ, "Frag_off: %d", ntohs(ip->ip_off) & IP_OFFMASK);
                    180:                        if (ntohs(ip->ip_off) & IP_RF)
                    181:                                strlcat(szStr, "|RF", sizeof szStr);
                    182:                        if (ntohs(ip->ip_off) & IP_DF)
                    183:                                strlcat(szStr, "|DF", sizeof szStr);
                    184:                        if (ntohs(ip->ip_off) & IP_MF)
                    185:                                strlcat(szStr, "|MF", sizeof szStr);
                    186:                        snprintf(szLine, BUFSIZ, "\tTOS: %02x, TTL: %d, ID: %04x, Cksum: %04x, Len: %d, %s\n", 
                    187:                                        ip->ip_tos, ip->ip_ttl, ntohs(ip->ip_id), ntohs(ip->ip_sum), 
                    188:                                        ntohs(ip->ip_len), szStr);
                    189:                        strlcat(szShow, szLine, USHRT_MAX);
                    190:                        snprintf(szLine, BUFSIZ, "\tProto: %d, Src_addr: %s, Dst_addr: %s\n", 
                    191:                                        ip->ip_p, inet_ntop(AF_INET, &ip->ip_src, szStr, BUFSIZ), 
                    192:                                        inet_ntop(AF_INET, &ip->ip_dst, szWrk, BUFSIZ));
                    193:                        strlcat(szShow, szLine, USHRT_MAX);
                    194: 
                    195:                        Proto = ip->ip_p;
                    196:                        break;
                    197:                case ETHERTYPE_IPV6:
                    198:                        ipv6 = (struct ip6_hdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
                    199:                        strlcat(szShow, "\t>>> IPv6 ...\n", USHRT_MAX);
                    200: 
                    201:                        snprintf(szLine, BUFSIZ, "\tHLim: %d, Payload_len: %d, Flow: %u\n", ipv6->ip6_hlim, 
                    202:                                        htons(ipv6->ip6_plen), htonl(ipv6->ip6_flow) & IPV6_FLOWLABEL_MASK);
                    203:                        strlcat(szShow, szLine, USHRT_MAX);
                    204:                        inet_ntop(AF_INET6, &ipv6->ip6_src, szStr, BUFSIZ);
                    205:                        inet_ntop(AF_INET6, &ipv6->ip6_dst, szWrk, BUFSIZ);
                    206:                        snprintf(szLine, BUFSIZ, "\tNext: %d, Src_addr: %s, Dst_addr: %s\n", ipv6->ip6_nxt, szStr, szWrk);
                    207:                        strlcat(szShow, szLine, USHRT_MAX);
                    208: 
                    209:                        Proto = ipv6->ip6_nxt;
                    210:                        break;
                    211:        }
                    212:        
1.5     ! misho     213: #ifndef __linux__
1.2       misho     214:        if (Proto && (ntohs(eth->ether_type) == ETHERTYPE_IP || ntohs(eth->ether_type) == ETHERTYPE_IPV6))
1.5     ! misho     215: #else
        !           216:        if (Proto && (ntohs(eth->h_proto) == ETHERTYPE_IP || ntohs(eth->h_proto) == ETHERTYPE_IPV6))
        !           217: #endif
1.2       misho     218:                switch (Proto) {
                    219:                        case IPPROTO_TCP:
                    220:                                strlcat(szShow, "\t\t>>> TCP ...\n", USHRT_MAX);
1.5     ! misho     221: #ifndef __linux__
1.2       misho     222:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
1.5     ! misho     223: #else
        !           224:                                if (ntohs(eth->h_proto) == ETHERTYPE_IPV6)
        !           225: #endif
1.2       misho     226:                                        tcp = (struct tcphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    227:                                else
                    228:                                        tcp = (struct tcphdr*) (((caddr_t) ip) + sizeof(struct ip));
                    229: 
1.5     ! misho     230: #ifndef __linux__
1.2       misho     231:                                snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n", 
                    232:                                                ntohs(tcp->th_sport), ntohs(tcp->th_dport), tcp->th_seq, tcp->th_ack);
                    233:                                strlcat(szShow, szLine, USHRT_MAX);
                    234:                                snprintf(szWrk, BUFSIZ, "%d", (u_char) tcp->th_off >> 4);
                    235:                                if (tcp->th_flags & TH_FIN)
                    236:                                        strlcat(szWrk, "|FIN", sizeof szWrk);
                    237:                                if (tcp->th_flags & TH_SYN)
                    238:                                        strlcat(szWrk, "|SYN", sizeof szWrk);
                    239:                                if (tcp->th_flags & TH_RST)
                    240:                                        strlcat(szWrk, "|RST", sizeof szWrk);
                    241:                                if (tcp->th_flags & TH_PUSH)
                    242:                                        strlcat(szWrk, "|PUSH", sizeof szWrk);
                    243:                                if (tcp->th_flags & TH_ACK)
                    244:                                        strlcat(szWrk, "|ACK", sizeof szWrk);
                    245:                                if (tcp->th_flags & TH_URG)
                    246:                                        strlcat(szWrk, "|URG", sizeof szWrk);
                    247:                                if (tcp->th_flags & TH_ECE)
                    248:                                        strlcat(szWrk, "|ECE", sizeof szWrk);
                    249:                                if (tcp->th_flags & TH_CWR)
                    250:                                        strlcat(szWrk, "|CWR", sizeof szWrk);
                    251:                                snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n", 
                    252:                                                szWrk, ntohs(tcp->th_win), ntohs(tcp->th_urp), ntohs(tcp->th_sum));
1.5     ! misho     253: #else
        !           254:                                snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n", 
        !           255:                                                ntohs(tcp->source), ntohs(tcp->dest), tcp->seq, tcp->ack);
        !           256:                                strlcat(szShow, szLine, USHRT_MAX);
        !           257:                                snprintf(szWrk, BUFSIZ, "%d", (u_char) tcp->doff);
        !           258:                                if (tcp->fin)
        !           259:                                        strlcat(szWrk, "|FIN", sizeof szWrk);
        !           260:                                if (tcp->syn)
        !           261:                                        strlcat(szWrk, "|SYN", sizeof szWrk);
        !           262:                                if (tcp->rst)
        !           263:                                        strlcat(szWrk, "|RST", sizeof szWrk);
        !           264:                                if (tcp->psh)
        !           265:                                        strlcat(szWrk, "|PUSH", sizeof szWrk);
        !           266:                                if (tcp->ack)
        !           267:                                        strlcat(szWrk, "|ACK", sizeof szWrk);
        !           268:                                if (tcp->urg)
        !           269:                                        strlcat(szWrk, "|URG", sizeof szWrk);
        !           270:                                /*
        !           271:                                if (tcp->ece)
        !           272:                                        strlcat(szWrk, "|ECE", sizeof szWrk);
        !           273:                                if (tcp->cwr)
        !           274:                                        strlcat(szWrk, "|CWR", sizeof szWrk);
        !           275:                                        */
        !           276:                                snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n", 
        !           277:                                                szWrk, ntohs(tcp->window), ntohs(tcp->urg_ptr), ntohs(tcp->check));
        !           278: #endif
1.2       misho     279:                                strlcat(szShow, szLine, USHRT_MAX);
                    280:                                break;
                    281:                        case IPPROTO_UDP:
                    282:                                strlcat(szShow, "\t\t>>> UDP ...\n", USHRT_MAX);
1.5     ! misho     283: #ifndef __linux__
1.2       misho     284:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
1.5     ! misho     285: #else
        !           286:                                if (ntohs(eth->h_proto) == ETHERTYPE_IPV6)
        !           287: #endif
1.2       misho     288:                                        udp = (struct udphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    289:                                else
                    290:                                        udp = (struct udphdr*) (((caddr_t) ip) + sizeof(struct ip));
                    291: 
1.5     ! misho     292: #ifndef __linux__
1.2       misho     293:                                snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n", 
                    294:                                                ntohs(udp->uh_sport), ntohs(udp->uh_dport), 
                    295:                                                ntohs(udp->uh_ulen), ntohs(udp->uh_sum));
1.5     ! misho     296: #else
        !           297:                                snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n", 
        !           298:                                                ntohs(udp->source), ntohs(udp->dest), 
        !           299:                                                ntohs(udp->len), ntohs(udp->check));
        !           300: #endif
1.2       misho     301:                                strlcat(szShow, szLine, USHRT_MAX);
                    302:                                break;
                    303:                        case IPPROTO_ICMP:
                    304:                                strlcat(szShow, "\t\t>>> ICMP ...\n", USHRT_MAX);
                    305: #ifdef __FreeBSD__
                    306:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
                    307:                                        icmp = (struct icmphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    308:                                else
                    309:                                        icmp = (struct icmphdr*) (((caddr_t) ip) + sizeof(struct ip));
                    310: #endif
                    311: #ifdef __OpenBSD__
                    312:                                if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
                    313:                                        icmp = (struct icmp*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
                    314:                                else
                    315:                                        icmp = (struct icmp*) (((caddr_t) ip) + sizeof(struct ip));
                    316: #endif
                    317: 
1.5     ! misho     318: #ifndef __linux__
1.2       misho     319:                                snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n", 
                    320:                                                icmp->icmp_type, icmp->icmp_code, ntohs(icmp->icmp_cksum));
1.5     ! misho     321: #else
        !           322:                                snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n", 
        !           323:                                                icmp->type, icmp->code, ntohs(icmp->checksum));
        !           324: #endif
1.2       misho     325:                                strlcat(szShow, szLine, USHRT_MAX);
                    326:                                break;
                    327:                }
                    328: 
                    329:        printf("%s===\n", szShow);
                    330:        return NULL;
                    331: }
                    332: 
                    333: // ----------------------
                    334: 
                    335: int
                    336: main(int argc, char **argv)
                    337: {
                    338:        u_int n, count = (u_int) -1;
                    339:        register int i;
                    340:        int dev, fd, ret, siz = 0;
1.5     ! misho     341:        char szStr[BUFSIZ], szMap[MAXPATHLEN] = { 0 }, *buffer = NULL;
1.2       misho     342:        struct ifreq ifr;
                    343:        struct pollfd pfd = { 0 };
                    344:        pthread_t tid;
                    345:        char ch, mode = 'R';
                    346:        struct timespec ts_start, ts_end;
                    347:        void *bz = NULL;
1.4       misho     348:        ether_addr_t ea;
1.2       misho     349: 
                    350:        while ((ch = getopt(argc, argv, "hvwzs:p:f:")) != -1)
                    351:                switch (ch) {
                    352:                        case 'w':
                    353:                                mode = 'W';
                    354:                                break;
                    355:                        case 's':
                    356:                                siz = strtol(optarg, NULL, 0);
                    357:                                break;
                    358:                        case 'p':
                    359:                                count = strtol(optarg, NULL, 0);
                    360:                                break;
                    361:                        case 'f':
                    362:                                strlcpy(szMap, optarg, sizeof szMap);
                    363:                                break;
                    364:                        case 'z':
                    365:                                flg++;
                    366:                                break;
                    367:                        case 'v':
                    368:                                Verbose++;
                    369:                                break;
                    370:                        case 'h':
                    371:                        default:
                    372:                                return 1;
                    373:                }
                    374:        argc -= optind;
                    375:        argv += optind;
                    376:        if (argc < 1)
                    377:                return 1;
                    378: 
                    379:        if (**argv == '/')
                    380:                strlcpy(szStr, strrchr(*argv, '/') + 1, sizeof szStr);
                    381:        else
                    382:                strlcpy(szStr, *argv, sizeof szStr);
                    383: 
1.5     ! misho     384: #ifndef __linux__
1.2       misho     385: #ifdef __FreeBSD__
1.3       misho     386:        dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, (flg) ? &bz : NULL);
1.2       misho     387:        if (dev == -1)
1.3       misho     388:                dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, NULL);
1.2       misho     389: #else
1.3       misho     390:        dev = io_etherOpen(szStr, O_RDWR, 42, 0, (u_int*) &siz, NULL);
1.2       misho     391: #endif
1.5     ! misho     392: #else
        !           393:        dev = io_etherOpen(szStr, 0, 42, 0, (u_int*) &siz, NULL);
        !           394: #endif
1.2       misho     395:        if (dev == -1) {
                    396:                printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
                    397:                return 1;
                    398:        } else
                    399:                printf("dev=%d(%s)\n", dev, szStr);
                    400: 
1.5     ! misho     401: #ifndef __linux__
1.2       misho     402:        if (ioctl(dev, BIOCGBLEN, &siz) == -1) {
                    403:                perror("ioctl(BIOCGBLEN)");
                    404:                io_etherClose(dev, &bz);
                    405:                return 1;
                    406:        } else
                    407:                printf("BPF buffer len=%d\n", siz);
1.5     ! misho     408: #endif
1.2       misho     409: 
                    410:        if (*szMap) {
                    411:                fd = open(szMap, O_RDWR);
                    412:                if (fd == -1) {
                    413:                        perror("open(map)");
                    414:                        io_etherClose(dev, &bz);
                    415:                        return 1;
                    416:                }
                    417:                buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
                    418:                close(fd);
                    419:        } else
1.5     ! misho     420:                buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
1.2       misho     421:        if (buffer == MAP_FAILED) {
                    422:                perror("mmap()");
                    423:                io_etherClose(dev, &bz);
                    424:                return 1;
                    425:        }
                    426: 
                    427:        pfd.fd = dev;
                    428:        assert(!clock_gettime(CLOCK_REALTIME, &ts_start));
                    429:        if (mode == 'R') {
                    430:                pfd.events = POLLIN;
                    431:                for (i = 0; i < count; i++) {
                    432:                        if ((ret = poll(&pfd, 1, -1)) == -1)
                    433:                                break;
                    434: 
                    435:                        ret = io_etherRecv(dev, buffer, siz, bz);
                    436:                        if (!ret)
                    437:                                continue;
                    438:                        if (ret == -1)
                    439:                                printf("%d) io_etherRecv(%d) #%d - %s\n", i, ret, 
                    440:                                                io_GetErrno(), io_GetError());
                    441:                        if (Verbose) {
                    442:                                printf("%d) +readed %d bytes\n", i, ret);
                    443:                                ShowPkt(buffer);
                    444:                        }
                    445:                }
                    446:        } else {
                    447:                pfd.events = POLLOUT;
                    448:                for (i = 0; i < count; i++) {
                    449:                        if (poll(&pfd, 1, -1) == -1)
                    450:                                break;
                    451: 
                    452:                        ret = io_etherSend(dev, buffer, siz);
                    453:                        if (ret == -1)
                    454:                                printf("io_etherSend(%d) #%d - %s\n", ret, io_GetErrno(), io_GetError());
                    455:                        if (Verbose)
                    456:                                printf("+writed %d bytes\n", ret);
                    457:                }
                    458:        }
                    459:        assert(!clock_gettime(CLOCK_REALTIME, &ts_end));
                    460: 
                    461:        munmap(buffer, siz);
                    462:        io_etherClose(dev, &bz);
                    463: 
                    464:        time_spec_sub(&ts_end, &ts_start);
                    465:        printf("%d.%09lu for %d iterations\n", ts_end.tv_sec, ts_end.tv_nsec, i);
                    466:        ts_end.tv_sec *= 1000000000 / i;
                    467:        printf("0.%09lu per/iteration\n", ts_end.tv_sec + ts_end.tv_nsec / i);
                    468:        return 0;
                    469: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>