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