]> The Tcpdump Group git mirrors - libpcap/blob - tests/selpolltest.c
Define EXTRA_NETWORK_LIBS, because we use it.
[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 struct bpf_program fcode;
74 char ebuf[PCAP_ERRBUF_SIZE];
75 pcap_if_t *devlist;
76 int selectable_fd;
77 int status;
78 int packet_count;
79
80 device = NULL;
81 doselect = 0;
82 dopoll = 0;
83 dotimeout = 0;
84 dononblock = 0;
85 if ((cp = strrchr(argv[0], '/')) != NULL)
86 program_name = cp + 1;
87 else
88 program_name = argv[0];
89
90 opterr = 0;
91 while ((op = getopt(argc, argv, "i:sptn")) != -1) {
92 switch (op) {
93
94 case 'i':
95 device = optarg;
96 break;
97
98 case 's':
99 doselect = 1;
100 break;
101
102 case 'p':
103 dopoll = 1;
104 break;
105
106 case 't':
107 dotimeout = 1;
108 break;
109
110 case 'n':
111 dononblock = 1;
112 break;
113
114 default:
115 usage();
116 /* NOTREACHED */
117 }
118 }
119
120 if (doselect && dopoll) {
121 fprintf(stderr, "selpolltest: choose select (-s) or poll (-p), but not both\n");
122 return 1;
123 }
124 if (dotimeout && !doselect && !dopoll) {
125 fprintf(stderr, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
126 return 1;
127 }
128 if (device == NULL) {
129 if (pcap_findalldevs(&devlist, ebuf) == -1)
130 error("%s", ebuf);
131 if (devlist == NULL)
132 error("no interfaces available for capture");
133 device = strdup(devlist->name);
134 pcap_freealldevs(devlist);
135 }
136 *ebuf = '\0';
137 pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
138 if (pd == NULL)
139 error("%s", ebuf);
140 else if (*ebuf)
141 warning("%s", ebuf);
142 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
143 localnet = 0;
144 netmask = 0;
145 warning("%s", ebuf);
146 }
147 cmdbuf = copy_argv(&argv[optind]);
148
149 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
150 error("%s", pcap_geterr(pd));
151
152 if (pcap_setfilter(pd, &fcode) < 0)
153 error("%s", pcap_geterr(pd));
154 if (pcap_get_selectable_fd(pd) == -1)
155 error("pcap_get_selectable_fd() fails");
156 if (dononblock) {
157 if (pcap_setnonblock(pd, 1, ebuf) == -1)
158 error("pcap_setnonblock failed: %s", ebuf);
159 }
160 selectable_fd = pcap_get_selectable_fd(pd);
161 printf("Listening on %s\n", device);
162 if (doselect) {
163 for (;;) {
164 fd_set setread, setexcept;
165 struct timeval seltimeout;
166
167 FD_ZERO(&setread);
168 FD_SET(selectable_fd, &setread);
169 FD_ZERO(&setexcept);
170 FD_SET(selectable_fd, &setexcept);
171 if (dotimeout) {
172 seltimeout.tv_sec = 0;
173 seltimeout.tv_usec = 1000;
174 status = select(selectable_fd + 1, &setread,
175 NULL, &setexcept, &seltimeout);
176 } else {
177 status = select(selectable_fd + 1, &setread,
178 NULL, &setexcept, NULL);
179 }
180 if (status == -1) {
181 printf("Select returns error (%s)\n",
182 strerror(errno));
183 } else {
184 if (status == 0)
185 printf("Select timed out: ");
186 else
187 printf("Select returned a descriptor: ");
188 if (FD_ISSET(selectable_fd, &setread))
189 printf("readable, ");
190 else
191 printf("not readable, ");
192 if (FD_ISSET(selectable_fd, &setexcept))
193 printf("exceptional condition\n");
194 else
195 printf("no exceptional condition\n");
196 packet_count = 0;
197 status = pcap_dispatch(pd, -1, countme,
198 (u_char *)&packet_count);
199 if (status < 0)
200 break;
201 printf("%d packets seen, %d packets counted after select returns\n",
202 status, packet_count);
203 }
204 }
205 } else if (dopoll) {
206 for (;;) {
207 struct pollfd fd;
208 int polltimeout;
209
210 fd.fd = selectable_fd;
211 fd.events = POLLIN;
212 if (dotimeout)
213 polltimeout = 1;
214 else
215 polltimeout = -1;
216 status = poll(&fd, 1, polltimeout);
217 if (status == -1) {
218 printf("Poll returns error (%s)\n",
219 strerror(errno));
220 } else {
221 if (status == 0)
222 printf("Poll timed out\n");
223 else {
224 printf("Poll returned a descriptor: ");
225 if (fd.revents & POLLIN)
226 printf("readable, ");
227 else
228 printf("not readable, ");
229 if (fd.revents & POLLERR)
230 printf("exceptional condition, ");
231 else
232 printf("no exceptional condition, ");
233 if (fd.revents & POLLHUP)
234 printf("disconnect, ");
235 else
236 printf("no disconnect, ");
237 if (fd.revents & POLLNVAL)
238 printf("invalid\n");
239 else
240 printf("not invalid\n");
241 }
242 packet_count = 0;
243 status = pcap_dispatch(pd, -1, countme,
244 (u_char *)&packet_count);
245 if (status < 0)
246 break;
247 printf("%d packets seen, %d packets counted after poll returns\n",
248 status, packet_count);
249 }
250 }
251 } else {
252 for (;;) {
253 packet_count = 0;
254 status = pcap_dispatch(pd, -1, countme,
255 (u_char *)&packet_count);
256 if (status < 0)
257 break;
258 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
259 status, packet_count);
260 }
261 }
262 if (status == -2) {
263 /*
264 * We got interrupted, so perhaps we didn't
265 * manage to finish a line we were printing.
266 * Print an extra newline, just in case.
267 */
268 putchar('\n');
269 }
270 (void)fflush(stdout);
271 if (status == -1) {
272 /*
273 * Error. Report it.
274 */
275 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
276 program_name, pcap_geterr(pd));
277 }
278 pcap_close(pd);
279 exit(status == -1 ? 1 : 0);
280 }
281
282 static void
283 countme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
284 {
285 int *counterp = (int *)user;
286
287 (*counterp)++;
288 }
289
290 static void
291 usage(void)
292 {
293 (void)fprintf(stderr, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
294 program_name);
295 exit(1);
296 }
297
298 /* VARARGS */
299 static void
300 error(const char *fmt, ...)
301 {
302 va_list ap;
303
304 (void)fprintf(stderr, "%s: ", program_name);
305 va_start(ap, fmt);
306 (void)vfprintf(stderr, fmt, ap);
307 va_end(ap);
308 if (*fmt) {
309 fmt += strlen(fmt);
310 if (fmt[-1] != '\n')
311 (void)fputc('\n', stderr);
312 }
313 exit(1);
314 /* NOTREACHED */
315 }
316
317 /* VARARGS */
318 static void
319 warning(const char *fmt, ...)
320 {
321 va_list ap;
322
323 (void)fprintf(stderr, "%s: WARNING: ", program_name);
324 va_start(ap, fmt);
325 (void)vfprintf(stderr, fmt, ap);
326 va_end(ap);
327 if (*fmt) {
328 fmt += strlen(fmt);
329 if (fmt[-1] != '\n')
330 (void)fputc('\n', stderr);
331 }
332 }
333
334 /*
335 * Copy arg vector into a new buffer, concatenating arguments with spaces.
336 */
337 static char *
338 copy_argv(register char **argv)
339 {
340 register char **p;
341 register u_int len = 0;
342 char *buf;
343 char *src, *dst;
344
345 p = argv;
346 if (*p == 0)
347 return 0;
348
349 while (*p)
350 len += strlen(*p++) + 1;
351
352 buf = (char *)malloc(len);
353 if (buf == NULL)
354 error("copy_argv: malloc");
355
356 p = argv;
357 dst = buf;
358 while ((src = *p++) != NULL) {
359 while ((*dst++ = *src++) != '\0')
360 ;
361 dst[-1] = ' ';
362 }
363 dst[-1] = '\0';
364
365 return buf;
366 }