]>
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 #include <sys/select.h>
49 #include "pcap/funcattrs.h"
51 static char *program_name
;
54 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
55 static void PCAP_NORETURN
usage(void);
56 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
57 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
58 static char *copy_argv(char **);
63 main(int argc
, char **argv
)
66 bpf_u_int32 localnet
, netmask
;
67 register char *cp
, *cmdbuf
, *device
;
68 int doselect
, dopoll
, dotimeout
, dononblock
, quiet
;
69 const char *mechanism
;
70 struct bpf_program fcode
;
71 char ebuf
[PCAP_ERRBUF_SIZE
];
73 int selectable_fd
= -1;
74 const struct timeval
*required_timeout
;
85 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
86 program_name
= cp
+ 1;
88 program_name
= argv
[0];
91 while ((op
= getopt(argc
, argv
, "i:sptnq")) != -1) {
100 mechanism
= "select() and pcap_dispatch()";
105 mechanism
= "poll() and pcap_dispatch()";
126 if (doselect
&& dopoll
) {
127 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
130 if (dotimeout
&& !doselect
&& !dopoll
) {
131 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
134 if (device
== NULL
) {
135 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
138 error("no interfaces available for capture");
139 device
= strdup(devlist
->name
);
140 pcap_freealldevs(devlist
);
143 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
148 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
153 cmdbuf
= copy_argv(&argv
[optind
]);
155 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
156 error("%s", pcap_geterr(pd
));
157 if (pcap_setfilter(pd
, &fcode
) < 0)
158 error("%s", pcap_geterr(pd
));
160 if (doselect
|| dopoll
) {
162 * We need either an FD on which to do select()/poll()
163 * or, if there isn't one, a timeout to use in select()/
166 selectable_fd
= pcap_get_selectable_fd(pd
);
167 if (selectable_fd
== -1) {
168 printf("Listening on %s, using %s, with a timeout\n",
170 required_timeout
= pcap_get_required_select_timeout(pd
);
171 if (required_timeout
== NULL
)
172 error("select()/poll() isn't supported on %s, even with a timeout",
176 * As we won't be notified by select() or poll()
177 * that a read can be done, we'll have to periodically
178 * try reading from the device every time the required
179 * timeout expires, and we don't want those attempts
180 * to block if nothing has arrived in that interval,
181 * so we want to force non-blocking mode.
185 printf("Listening on %s, using %s\n", device
,
187 required_timeout
= NULL
;
190 printf("Listening on %s, using pcap_dispatch()\n", device
);
193 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
194 error("pcap_setnonblock failed: %s", ebuf
);
198 fd_set setread
, setexcept
;
199 struct timeval seltimeout
;
200 struct timeval
*timeoutp
;
203 if (selectable_fd
!= -1) {
204 FD_SET(selectable_fd
, &setread
);
206 FD_SET(selectable_fd
, &setexcept
);
208 required_timeout
= pcap_get_required_select_timeout(pd
);
210 seltimeout
.tv_sec
= 0;
211 if (required_timeout
!= NULL
&&
212 required_timeout
->tv_usec
< 1000)
213 seltimeout
.tv_usec
= required_timeout
->tv_usec
;
215 seltimeout
.tv_usec
= 1000;
216 timeoutp
= &seltimeout
;
217 } else if (required_timeout
!= NULL
) {
218 seltimeout
= *required_timeout
;
219 timeoutp
= &seltimeout
;
223 status
= select((selectable_fd
== -1) ?
224 0 : selectable_fd
+ 1, &setread
, NULL
, &setexcept
,
227 printf("Select returns error (%s)\n",
232 printf("Select timed out: ");
234 printf("Select returned a descriptor: ");
235 if (FD_ISSET(selectable_fd
, &setread
))
236 printf("readable, ");
238 printf("not readable, ");
239 if (FD_ISSET(selectable_fd
, &setexcept
))
240 printf("exceptional condition\n");
242 printf("no exceptional condition\n");
246 status
= pcap_dispatch(pd
, -1, countme
,
247 (u_char
*)&packet_count
);
251 * Don't report this if we're using a
252 * required timeout and we got no packets,
253 * because that could be a very short timeout,
254 * and we don't want to spam the user with
255 * a ton of "no packets" reports.
257 if (status
!= 0 || packet_count
!= 0 ||
258 required_timeout
!= NULL
) {
259 printf("%d packets seen, %d packets counted after select returns\n",
260 status
, packet_count
);
269 fd
.fd
= selectable_fd
;
271 required_timeout
= pcap_get_required_select_timeout(pd
);
274 else if (required_timeout
!= NULL
&&
275 required_timeout
->tv_usec
>= 1000)
276 polltimeout
= (int)(required_timeout
->tv_usec
/1000);
279 status
= poll(&fd
, (selectable_fd
== -1) ? 0 : 1, polltimeout
);
281 printf("Poll returns error (%s)\n",
286 printf("Poll timed out\n");
288 printf("Poll returned a descriptor: ");
289 if (fd
.revents
& POLLIN
)
290 printf("readable, ");
292 printf("not readable, ");
293 if (fd
.revents
& POLLERR
)
294 printf("exceptional condition, ");
296 printf("no exceptional condition, ");
297 if (fd
.revents
& POLLHUP
)
298 printf("disconnect, ");
300 printf("no disconnect, ");
301 if (fd
.revents
& POLLNVAL
)
304 printf("not invalid\n");
308 status
= pcap_dispatch(pd
, -1, countme
,
309 (u_char
*)&packet_count
);
313 * Don't report this if we're using a
314 * required timeout and we got no packets,
315 * because that could be a very short timeout,
316 * and we don't want to spam the user with
317 * a ton of "no packets" reports.
319 if (status
!= 0 || packet_count
!= 0 ||
320 required_timeout
!= NULL
) {
321 printf("%d packets seen, %d packets counted after poll returns\n",
322 status
, packet_count
);
329 status
= pcap_dispatch(pd
, -1, countme
,
330 (u_char
*)&packet_count
);
333 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
334 status
, packet_count
);
339 * We got interrupted, so perhaps we didn't
340 * manage to finish a line we were printing.
341 * Print an extra newline, just in case.
345 (void)fflush(stdout
);
350 (void)fprintf(stderr
, "%s: pcap_dispatch: %s\n",
351 program_name
, pcap_geterr(pd
));
354 exit(status
== -1 ? 1 : 0);
358 countme(u_char
*user
, const struct pcap_pkthdr
*h _U_
, const u_char
*sp _U_
)
360 int *counterp
= (int *)user
;
368 (void)fprintf(stderr
, "Usage: %s [ -sptnq ] [ -i interface ] [expression]\n",
375 error(const char *fmt
, ...)
379 (void)fprintf(stderr
, "%s: ", program_name
);
381 (void)vfprintf(stderr
, fmt
, ap
);
386 (void)fputc('\n', stderr
);
394 warning(const char *fmt
, ...)
398 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
400 (void)vfprintf(stderr
, fmt
, ap
);
405 (void)fputc('\n', stderr
);
410 * Copy arg vector into a new buffer, concatenating arguments with spaces.
413 copy_argv(register char **argv
)
416 register size_t len
= 0;
425 len
+= strlen(*p
++) + 1;
427 buf
= (char *)malloc(len
);
429 error("copy_argv: malloc");
433 while ((src
= *p
++) != NULL
) {
434 while ((*dst
++ = *src
++) != '\0')