]> The Tcpdump Group git mirrors - libpcap/blob - tests/selpolltest.c
Fix handling of the no-selectable-fd case.
[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 #include "varattrs.h"
23
24 #ifndef lint
25 static const char copyright[] _U_ =
26 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27 The Regents of the University of California. All rights reserved.\n";
28 #endif
29
30 /*
31 * Tests how select() and poll() behave on the selectable file descriptor
32 * for a pcap_t.
33 *
34 * This would be significantly different on Windows, as it'd test
35 * how WaitForMultipleObjects() would work on the event handle for a
36 * pcap_t.
37 */
38 #include <pcap.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_SELECT_H
47 #include <sys/select.h>
48 #else
49 #include <sys/time.h> /* older UN*Xes */
50 #endif
51 #include <poll.h>
52
53 #include "pcap/funcattrs.h"
54
55 char *program_name;
56
57 /* Forwards */
58 static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
59 static void PCAP_NORETURN usage(void);
60 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
61 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
62 static char *copy_argv(char **);
63
64 static pcap_t *pd;
65
66 int
67 main(int argc, char **argv)
68 {
69 register int op;
70 bpf_u_int32 localnet, netmask;
71 register char *cp, *cmdbuf, *device;
72 int doselect, dopoll, dotimeout, dononblock;
73 char *mechanism;
74 struct bpf_program fcode;
75 char ebuf[PCAP_ERRBUF_SIZE];
76 pcap_if_t *devlist;
77 int selectable_fd;
78 struct timeval *required_timeout;
79 int status;
80 int packet_count;
81
82 device = NULL;
83 doselect = 0;
84 dopoll = 0;
85 mechanism = "pcap_dispatch()";
86 dotimeout = 0;
87 dononblock = 0;
88 if ((cp = strrchr(argv[0], '/')) != NULL)
89 program_name = cp + 1;
90 else
91 program_name = argv[0];
92
93 opterr = 0;
94 while ((op = getopt(argc, argv, "i:sptn")) != -1) {
95 switch (op) {
96
97 case 'i':
98 device = optarg;
99 break;
100
101 case 's':
102 doselect = 1;
103 mechanism = "select() and pcap_dispatch()";
104 break;
105
106 case 'p':
107 dopoll = 1;
108 mechanism = "poll() and pcap_dispatch()";
109 break;
110
111 case 't':
112 dotimeout = 1;
113 break;
114
115 case 'n':
116 dononblock = 1;
117 break;
118
119 default:
120 usage();
121 /* NOTREACHED */
122 }
123 }
124
125 if (doselect && dopoll) {
126 fprintf(stderr, "selpolltest: choose select (-s) or poll (-p), but not both\n");
127 return 1;
128 }
129 if (dotimeout && !doselect && !dopoll) {
130 fprintf(stderr, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
131 return 1;
132 }
133 if (device == NULL) {
134 if (pcap_findalldevs(&devlist, ebuf) == -1)
135 error("%s", ebuf);
136 if (devlist == NULL)
137 error("no interfaces available for capture");
138 device = strdup(devlist->name);
139 pcap_freealldevs(devlist);
140 }
141 *ebuf = '\0';
142 pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
143 if (pd == NULL)
144 error("%s", ebuf);
145 else if (*ebuf)
146 warning("%s", ebuf);
147 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
148 localnet = 0;
149 netmask = 0;
150 warning("%s", ebuf);
151 }
152 cmdbuf = copy_argv(&argv[optind]);
153
154 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
155 error("%s", pcap_geterr(pd));
156
157 if (pcap_setfilter(pd, &fcode) < 0)
158 error("%s", pcap_geterr(pd));
159 selectable_fd = pcap_get_selectable_fd(pd);
160 if (selectable_fd == -1) {
161 printf("Listening on %s, using %s, with a timeout\n",
162 device, mechanism);
163 required_timeout = pcap_get_required_select_timeout(pd);
164 if (required_timeout == NULL)
165 if (doselect || dopoll)
166 error("select()/poll() isn't supported on %s, even with a timeout",
167 device);
168 /*
169 * As we won't be notified by select() or poll() that
170 * a read can be done, we'll have to periodically try
171 * reading from the device every time the required
172 * timeout expires, and we don't want those attempts
173 * to block if nothing has arrived in that interval,
174 * so we want to force non-blocking mode.
175 */
176 dononblock = 1;
177 } else {
178 printf("Listening on %s, using %s\n", device, mechanism);
179 required_timeout = NULL;
180 }
181 if (dononblock) {
182 if (pcap_setnonblock(pd, 1, ebuf) == -1)
183 error("pcap_setnonblock failed: %s", ebuf);
184 }
185 if (doselect) {
186 for (;;) {
187 fd_set setread, setexcept;
188 struct timeval seltimeout;
189
190 FD_ZERO(&setread);
191 if (selectable_fd != -1) {
192 FD_SET(selectable_fd, &setread);
193 FD_ZERO(&setexcept);
194 FD_SET(selectable_fd, &setexcept);
195 }
196 if (dotimeout) {
197 seltimeout.tv_sec = 0;
198 if (required_timeout != NULL &&
199 required_timeout->tv_usec < 1000)
200 seltimeout.tv_usec = required_timeout->tv_usec;
201 else
202 seltimeout.tv_usec = 1000;
203 status = select(selectable_fd + 1, &setread,
204 NULL, &setexcept, &seltimeout);
205 } else if (required_timeout != NULL) {
206 seltimeout = *required_timeout;
207 status = select(selectable_fd + 1, &setread,
208 NULL, &setexcept, &seltimeout);
209 } else {
210 status = select((selectable_fd == -1) ?
211 0 : selectable_fd + 1, &setread,
212 NULL, &setexcept, NULL);
213 }
214 if (status == -1) {
215 printf("Select returns error (%s)\n",
216 strerror(errno));
217 } else {
218 if (status == 0)
219 printf("Select timed out: ");
220 else
221 printf("Select returned a descriptor: ");
222 if (selectable_fd == -1)
223 printf("couldn't do select() on FD\n");
224 else {
225 if (FD_ISSET(selectable_fd, &setread))
226 printf("readable, ");
227 else
228 printf("not readable, ");
229 if (FD_ISSET(selectable_fd, &setexcept))
230 printf("exceptional condition\n");
231 else
232 printf("no exceptional condition\n");
233 }
234 packet_count = 0;
235 status = pcap_dispatch(pd, -1, countme,
236 (u_char *)&packet_count);
237 if (status < 0)
238 break;
239 printf("%d packets seen, %d packets counted after select returns\n",
240 status, packet_count);
241 }
242 }
243 } else if (dopoll) {
244 for (;;) {
245 struct pollfd fd;
246 int polltimeout;
247
248 fd.fd = selectable_fd;
249 fd.events = POLLIN;
250 if (dotimeout)
251 polltimeout = 1;
252 else if (required_timeout != NULL &&
253 required_timeout->tv_usec >= 1000)
254 polltimeout = required_timeout->tv_usec/1000;
255 else
256 polltimeout = -1;
257 status = poll(&fd, (selectable_fd == -1) ? 0 : 1, polltimeout);
258 if (status == -1) {
259 printf("Poll returns error (%s)\n",
260 strerror(errno));
261 } else {
262 if (status == 0)
263 printf("Poll timed out\n");
264 else {
265 printf("Poll returned a descriptor: ");
266 if (fd.revents & POLLIN)
267 printf("readable, ");
268 else
269 printf("not readable, ");
270 if (fd.revents & POLLERR)
271 printf("exceptional condition, ");
272 else
273 printf("no exceptional condition, ");
274 if (fd.revents & POLLHUP)
275 printf("disconnect, ");
276 else
277 printf("no disconnect, ");
278 if (fd.revents & POLLNVAL)
279 printf("invalid\n");
280 else
281 printf("not invalid\n");
282 }
283 packet_count = 0;
284 status = pcap_dispatch(pd, -1, countme,
285 (u_char *)&packet_count);
286 if (status < 0)
287 break;
288 printf("%d packets seen, %d packets counted after poll returns\n",
289 status, packet_count);
290 }
291 }
292 } else {
293 for (;;) {
294 packet_count = 0;
295 status = pcap_dispatch(pd, -1, countme,
296 (u_char *)&packet_count);
297 if (status < 0)
298 break;
299 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
300 status, packet_count);
301 }
302 }
303 if (status == -2) {
304 /*
305 * We got interrupted, so perhaps we didn't
306 * manage to finish a line we were printing.
307 * Print an extra newline, just in case.
308 */
309 putchar('\n');
310 }
311 (void)fflush(stdout);
312 if (status == -1) {
313 /*
314 * Error. Report it.
315 */
316 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
317 program_name, pcap_geterr(pd));
318 }
319 pcap_close(pd);
320 exit(status == -1 ? 1 : 0);
321 }
322
323 static void
324 countme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
325 {
326 int *counterp = (int *)user;
327
328 (*counterp)++;
329 }
330
331 static void
332 usage(void)
333 {
334 (void)fprintf(stderr, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
335 program_name);
336 exit(1);
337 }
338
339 /* VARARGS */
340 static void
341 error(const char *fmt, ...)
342 {
343 va_list ap;
344
345 (void)fprintf(stderr, "%s: ", program_name);
346 va_start(ap, fmt);
347 (void)vfprintf(stderr, fmt, ap);
348 va_end(ap);
349 if (*fmt) {
350 fmt += strlen(fmt);
351 if (fmt[-1] != '\n')
352 (void)fputc('\n', stderr);
353 }
354 exit(1);
355 /* NOTREACHED */
356 }
357
358 /* VARARGS */
359 static void
360 warning(const char *fmt, ...)
361 {
362 va_list ap;
363
364 (void)fprintf(stderr, "%s: WARNING: ", program_name);
365 va_start(ap, fmt);
366 (void)vfprintf(stderr, fmt, ap);
367 va_end(ap);
368 if (*fmt) {
369 fmt += strlen(fmt);
370 if (fmt[-1] != '\n')
371 (void)fputc('\n', stderr);
372 }
373 }
374
375 /*
376 * Copy arg vector into a new buffer, concatenating arguments with spaces.
377 */
378 static char *
379 copy_argv(register char **argv)
380 {
381 register char **p;
382 register u_int len = 0;
383 char *buf;
384 char *src, *dst;
385
386 p = argv;
387 if (*p == 0)
388 return 0;
389
390 while (*p)
391 len += strlen(*p++) + 1;
392
393 buf = (char *)malloc(len);
394 if (buf == NULL)
395 error("copy_argv: malloc");
396
397 p = argv;
398 dst = buf;
399 while ((src = *p++) != NULL) {
400 while ((*dst++ = *src++) != '\0')
401 ;
402 dst[-1] = ' ';
403 }
404 dst[-1] = '\0';
405
406 return buf;
407 }