]> The Tcpdump Group git mirrors - libpcap/blob - tests/selpolltest.c
Clean up the ether_hostton() stuff.
[libpcap] / tests / selpolltest.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char copyright[] _U_ =
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
25 The Regents of the University of California. All rights reserved.\n";
26 #endif
27
28 /*
29 * Tests how select() and poll() behave on the selectable file descriptor
30 * for a pcap_t.
31 *
32 * This would be significantly different on Windows, as it'd test
33 * how WaitForMultipleObjects() would work on the event handle for a
34 * pcap_t.
35 */
36 #include <pcap.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #ifdef HAVE_SYS_SELECT_H
45 #include <sys/select.h>
46 #else
47 #include <sys/time.h> /* older UN*Xes */
48 #endif
49 #include <poll.h>
50
51 #include "pcap/funcattrs.h"
52
53 char *program_name;
54
55 /* Forwards */
56 static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
57 static void PCAP_NORETURN usage(void);
58 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
59 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
60 static char *copy_argv(char **);
61
62 static pcap_t *pd;
63
64 int
65 main(int argc, char **argv)
66 {
67 register int op;
68 bpf_u_int32 localnet, netmask;
69 register char *cp, *cmdbuf, *device;
70 int doselect, dopoll, dotimeout, dononblock;
71 struct bpf_program fcode;
72 char ebuf[PCAP_ERRBUF_SIZE];
73 pcap_if_t *devlist;
74 int selectable_fd;
75 int status;
76 int packet_count;
77
78 device = NULL;
79 doselect = 0;
80 dopoll = 0;
81 dotimeout = 0;
82 dononblock = 0;
83 if ((cp = strrchr(argv[0], '/')) != NULL)
84 program_name = cp + 1;
85 else
86 program_name = argv[0];
87
88 opterr = 0;
89 while ((op = getopt(argc, argv, "i:sptn")) != -1) {
90 switch (op) {
91
92 case 'i':
93 device = optarg;
94 break;
95
96 case 's':
97 doselect = 1;
98 break;
99
100 case 'p':
101 dopoll = 1;
102 break;
103
104 case 't':
105 dotimeout = 1;
106 break;
107
108 case 'n':
109 dononblock = 1;
110 break;
111
112 default:
113 usage();
114 /* NOTREACHED */
115 }
116 }
117
118 if (doselect && dopoll) {
119 fprintf(stderr, "selpolltest: choose select (-s) or poll (-p), but not both\n");
120 return 1;
121 }
122 if (dotimeout && !doselect && !dopoll) {
123 fprintf(stderr, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
124 return 1;
125 }
126 if (device == NULL) {
127 if (pcap_findalldevs(&devlist, ebuf) == -1)
128 error("%s", ebuf);
129 if (devlist == NULL)
130 error("no interfaces available for capture");
131 device = strdup(devlist->name);
132 pcap_freealldevs(devlist);
133 }
134 *ebuf = '\0';
135 pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
136 if (pd == NULL)
137 error("%s", ebuf);
138 else if (*ebuf)
139 warning("%s", ebuf);
140 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
141 localnet = 0;
142 netmask = 0;
143 warning("%s", ebuf);
144 }
145 cmdbuf = copy_argv(&argv[optind]);
146
147 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
148 error("%s", pcap_geterr(pd));
149
150 if (pcap_setfilter(pd, &fcode) < 0)
151 error("%s", pcap_geterr(pd));
152 if (pcap_get_selectable_fd(pd) == -1)
153 error("pcap_get_selectable_fd() fails");
154 if (dononblock) {
155 if (pcap_setnonblock(pd, 1, ebuf) == -1)
156 error("pcap_setnonblock failed: %s", ebuf);
157 }
158 selectable_fd = pcap_get_selectable_fd(pd);
159 printf("Listening on %s\n", device);
160 if (doselect) {
161 for (;;) {
162 fd_set setread, setexcept;
163 struct timeval seltimeout;
164
165 FD_ZERO(&setread);
166 FD_SET(selectable_fd, &setread);
167 FD_ZERO(&setexcept);
168 FD_SET(selectable_fd, &setexcept);
169 if (dotimeout) {
170 seltimeout.tv_sec = 0;
171 seltimeout.tv_usec = 1000;
172 status = select(selectable_fd + 1, &setread,
173 NULL, &setexcept, &seltimeout);
174 } else {
175 status = select(selectable_fd + 1, &setread,
176 NULL, &setexcept, NULL);
177 }
178 if (status == -1) {
179 printf("Select returns error (%s)\n",
180 strerror(errno));
181 } else {
182 if (status == 0)
183 printf("Select timed out: ");
184 else
185 printf("Select returned a descriptor: ");
186 if (FD_ISSET(selectable_fd, &setread))
187 printf("readable, ");
188 else
189 printf("not readable, ");
190 if (FD_ISSET(selectable_fd, &setexcept))
191 printf("exceptional condition\n");
192 else
193 printf("no exceptional condition\n");
194 packet_count = 0;
195 status = pcap_dispatch(pd, -1, countme,
196 (u_char *)&packet_count);
197 if (status < 0)
198 break;
199 printf("%d packets seen, %d packets counted after select returns\n",
200 status, packet_count);
201 }
202 }
203 } else if (dopoll) {
204 for (;;) {
205 struct pollfd fd;
206 int polltimeout;
207
208 fd.fd = selectable_fd;
209 fd.events = POLLIN;
210 if (dotimeout)
211 polltimeout = 1;
212 else
213 polltimeout = -1;
214 status = poll(&fd, 1, polltimeout);
215 if (status == -1) {
216 printf("Poll returns error (%s)\n",
217 strerror(errno));
218 } else {
219 if (status == 0)
220 printf("Poll timed out\n");
221 else {
222 printf("Poll returned a descriptor: ");
223 if (fd.revents & POLLIN)
224 printf("readable, ");
225 else
226 printf("not readable, ");
227 if (fd.revents & POLLERR)
228 printf("exceptional condition, ");
229 else
230 printf("no exceptional condition, ");
231 if (fd.revents & POLLHUP)
232 printf("disconnect, ");
233 else
234 printf("no disconnect, ");
235 if (fd.revents & POLLNVAL)
236 printf("invalid\n");
237 else
238 printf("not invalid\n");
239 }
240 packet_count = 0;
241 status = pcap_dispatch(pd, -1, countme,
242 (u_char *)&packet_count);
243 if (status < 0)
244 break;
245 printf("%d packets seen, %d packets counted after poll returns\n",
246 status, packet_count);
247 }
248 }
249 } else {
250 for (;;) {
251 packet_count = 0;
252 status = pcap_dispatch(pd, -1, countme,
253 (u_char *)&packet_count);
254 if (status < 0)
255 break;
256 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
257 status, packet_count);
258 }
259 }
260 if (status == -2) {
261 /*
262 * We got interrupted, so perhaps we didn't
263 * manage to finish a line we were printing.
264 * Print an extra newline, just in case.
265 */
266 putchar('\n');
267 }
268 (void)fflush(stdout);
269 if (status == -1) {
270 /*
271 * Error. Report it.
272 */
273 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
274 program_name, pcap_geterr(pd));
275 }
276 pcap_close(pd);
277 exit(status == -1 ? 1 : 0);
278 }
279
280 static void
281 countme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
282 {
283 int *counterp = (int *)user;
284
285 (*counterp)++;
286 }
287
288 static void
289 usage(void)
290 {
291 (void)fprintf(stderr, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
292 program_name);
293 exit(1);
294 }
295
296 /* VARARGS */
297 static void
298 error(const char *fmt, ...)
299 {
300 va_list ap;
301
302 (void)fprintf(stderr, "%s: ", program_name);
303 va_start(ap, fmt);
304 (void)vfprintf(stderr, fmt, ap);
305 va_end(ap);
306 if (*fmt) {
307 fmt += strlen(fmt);
308 if (fmt[-1] != '\n')
309 (void)fputc('\n', stderr);
310 }
311 exit(1);
312 /* NOTREACHED */
313 }
314
315 /* VARARGS */
316 static void
317 warning(const char *fmt, ...)
318 {
319 va_list ap;
320
321 (void)fprintf(stderr, "%s: WARNING: ", program_name);
322 va_start(ap, fmt);
323 (void)vfprintf(stderr, fmt, ap);
324 va_end(ap);
325 if (*fmt) {
326 fmt += strlen(fmt);
327 if (fmt[-1] != '\n')
328 (void)fputc('\n', stderr);
329 }
330 }
331
332 /*
333 * Copy arg vector into a new buffer, concatenating arguments with spaces.
334 */
335 static char *
336 copy_argv(register char **argv)
337 {
338 register char **p;
339 register u_int len = 0;
340 char *buf;
341 char *src, *dst;
342
343 p = argv;
344 if (*p == 0)
345 return 0;
346
347 while (*p)
348 len += strlen(*p++) + 1;
349
350 buf = (char *)malloc(len);
351 if (buf == NULL)
352 error("copy_argv: malloc");
353
354 p = argv;
355 dst = buf;
356 while ((src = *p++) != NULL) {
357 while ((*dst++ = *src++) != '\0')
358 ;
359 dst[-1] = ' ';
360 }
361 dst[-1] = '\0';
362
363 return buf;
364 }