]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/selpolltest.c
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.
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
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.
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";
31 * Tests how select() and poll() behave on the selectable file descriptor
34 * This would be significantly different on Windows, as it'd test
35 * how WaitForMultipleObjects() would work on the event handle for a
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_SELECT_H
47 #include <sys/select.h>
49 #include <sys/time.h> /* older UN*Xes */
53 #include "pcap/funcattrs.h"
55 static char *program_name
;
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 **);
67 main(int argc
, char **argv
)
70 bpf_u_int32 localnet
, netmask
;
71 register char *cp
, *cmdbuf
, *device
;
72 int doselect
, dopoll
, dotimeout
, dononblock
, quiet
;
73 const char *mechanism
;
74 struct bpf_program fcode
;
75 char ebuf
[PCAP_ERRBUF_SIZE
];
77 int selectable_fd
= -1;
78 const struct timeval
*required_timeout
;
89 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
90 program_name
= cp
+ 1;
92 program_name
= argv
[0];
95 while ((op
= getopt(argc
, argv
, "i:sptnq")) != -1) {
104 mechanism
= "select() and pcap_dispatch()";
109 mechanism
= "poll() and pcap_dispatch()";
130 if (doselect
&& dopoll
) {
131 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
134 if (dotimeout
&& !doselect
&& !dopoll
) {
135 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
138 if (device
== NULL
) {
139 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
142 error("no interfaces available for capture");
143 device
= strdup(devlist
->name
);
144 pcap_freealldevs(devlist
);
147 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
152 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
157 cmdbuf
= copy_argv(&argv
[optind
]);
159 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
160 error("%s", pcap_geterr(pd
));
161 if (pcap_setfilter(pd
, &fcode
) < 0)
162 error("%s", pcap_geterr(pd
));
164 if (doselect
|| dopoll
) {
166 * We need either an FD on which to do select()/poll()
167 * or, if there isn't one, a timeout to use in select()/
170 selectable_fd
= pcap_get_selectable_fd(pd
);
171 if (selectable_fd
== -1) {
172 printf("Listening on %s, using %s, with a timeout\n",
174 required_timeout
= pcap_get_required_select_timeout(pd
);
175 if (required_timeout
== NULL
)
176 error("select()/poll() isn't supported on %s, even with a timeout",
180 * As we won't be notified by select() or poll()
181 * that a read can be done, we'll have to periodically
182 * try reading from the device every time the required
183 * timeout expires, and we don't want those attempts
184 * to block if nothing has arrived in that interval,
185 * so we want to force non-blocking mode.
189 printf("Listening on %s, using %s\n", device
,
191 required_timeout
= NULL
;
194 printf("Listening on %s, using pcap_dispatch()\n", device
);
197 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
198 error("pcap_setnonblock failed: %s", ebuf
);
202 fd_set setread
, setexcept
;
203 struct timeval seltimeout
;
204 struct timeval
*timeoutp
;
207 if (selectable_fd
!= -1) {
208 FD_SET(selectable_fd
, &setread
);
210 FD_SET(selectable_fd
, &setexcept
);
212 required_timeout
= pcap_get_required_select_timeout(pd
);
214 seltimeout
.tv_sec
= 0;
215 if (required_timeout
!= NULL
&&
216 required_timeout
->tv_usec
< 1000)
217 seltimeout
.tv_usec
= required_timeout
->tv_usec
;
219 seltimeout
.tv_usec
= 1000;
220 timeoutp
= &seltimeout
;
221 } else if (required_timeout
!= NULL
) {
222 seltimeout
= *required_timeout
;
223 timeoutp
= &seltimeout
;
227 status
= select((selectable_fd
== -1) ?
228 0 : selectable_fd
+ 1, &setread
, NULL
, &setexcept
,
231 printf("Select returns error (%s)\n",
236 printf("Select timed out: ");
238 printf("Select returned a descriptor: ");
239 if (FD_ISSET(selectable_fd
, &setread
))
240 printf("readable, ");
242 printf("not readable, ");
243 if (FD_ISSET(selectable_fd
, &setexcept
))
244 printf("exceptional condition\n");
246 printf("no exceptional condition\n");
250 status
= pcap_dispatch(pd
, -1, countme
,
251 (u_char
*)&packet_count
);
255 * Don't report this if we're using a
256 * required timeout and we got no packets,
257 * because that could be a very short timeout,
258 * and we don't want to spam the user with
259 * a ton of "no packets" reports.
261 if (status
!= 0 || packet_count
!= 0 ||
262 required_timeout
!= NULL
) {
263 printf("%d packets seen, %d packets counted after select returns\n",
264 status
, packet_count
);
273 fd
.fd
= selectable_fd
;
275 required_timeout
= pcap_get_required_select_timeout(pd
);
278 else if (required_timeout
!= NULL
&&
279 required_timeout
->tv_usec
>= 1000)
280 polltimeout
= (int)(required_timeout
->tv_usec
/1000);
283 status
= poll(&fd
, (selectable_fd
== -1) ? 0 : 1, polltimeout
);
285 printf("Poll returns error (%s)\n",
290 printf("Poll timed out\n");
292 printf("Poll returned a descriptor: ");
293 if (fd
.revents
& POLLIN
)
294 printf("readable, ");
296 printf("not readable, ");
297 if (fd
.revents
& POLLERR
)
298 printf("exceptional condition, ");
300 printf("no exceptional condition, ");
301 if (fd
.revents
& POLLHUP
)
302 printf("disconnect, ");
304 printf("no disconnect, ");
305 if (fd
.revents
& POLLNVAL
)
308 printf("not invalid\n");
312 status
= pcap_dispatch(pd
, -1, countme
,
313 (u_char
*)&packet_count
);
317 * Don't report this if we're using a
318 * required timeout and we got no packets,
319 * because that could be a very short timeout,
320 * and we don't want to spam the user with
321 * a ton of "no packets" reports.
323 if (status
!= 0 || packet_count
!= 0 ||
324 required_timeout
!= NULL
) {
325 printf("%d packets seen, %d packets counted after poll returns\n",
326 status
, packet_count
);
333 status
= pcap_dispatch(pd
, -1, countme
,
334 (u_char
*)&packet_count
);
337 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
338 status
, packet_count
);
343 * We got interrupted, so perhaps we didn't
344 * manage to finish a line we were printing.
345 * Print an extra newline, just in case.
349 (void)fflush(stdout
);
354 (void)fprintf(stderr
, "%s: pcap_dispatch: %s\n",
355 program_name
, pcap_geterr(pd
));
358 exit(status
== -1 ? 1 : 0);
362 countme(u_char
*user
, const struct pcap_pkthdr
*h _U_
, const u_char
*sp _U_
)
364 int *counterp
= (int *)user
;
372 (void)fprintf(stderr
, "Usage: %s [ -sptnq ] [ -i interface ] [expression]\n",
379 error(const char *fmt
, ...)
383 (void)fprintf(stderr
, "%s: ", program_name
);
385 (void)vfprintf(stderr
, fmt
, ap
);
390 (void)fputc('\n', stderr
);
398 warning(const char *fmt
, ...)
402 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
404 (void)vfprintf(stderr
, fmt
, ap
);
409 (void)fputc('\n', stderr
);
414 * Copy arg vector into a new buffer, concatenating arguments with spaces.
417 copy_argv(register char **argv
)
420 register size_t len
= 0;
429 len
+= strlen(*p
++) + 1;
431 buf
= (char *)malloc(len
);
433 error("copy_argv: malloc");
437 while ((src
= *p
++) != NULL
) {
438 while ((*dst
++ = *src
++) != '\0')