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