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