]>
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"
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
;
74 struct bpf_program fcode
;
75 char ebuf
[PCAP_ERRBUF_SIZE
];
78 struct timeval
*required_timeout
;
88 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
89 program_name
= cp
+ 1;
91 program_name
= argv
[0];
94 while ((op
= getopt(argc
, argv
, "i:sptn")) != -1) {
103 mechanism
= "select() and pcap_dispatch()";
108 mechanism
= "poll() and pcap_dispatch()";
125 if (doselect
&& dopoll
) {
126 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
129 if (dotimeout
&& !doselect
&& !dopoll
) {
130 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
133 if (device
== NULL
) {
134 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
137 error("no interfaces available for capture");
138 device
= strdup(devlist
->name
);
139 pcap_freealldevs(devlist
);
142 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
147 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
152 cmdbuf
= copy_argv(&argv
[optind
]);
154 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
155 error("%s", pcap_geterr(pd
));
156 if (pcap_setfilter(pd
, &fcode
) < 0)
157 error("%s", pcap_geterr(pd
));
159 if (doselect
|| dopoll
) {
161 * We need either an FD on which to do select()/poll()
162 * or, if there isn't one, a timeout to use in select()/
165 selectable_fd
= pcap_get_selectable_fd(pd
);
166 if (selectable_fd
== -1) {
167 printf("Listening on %s, using %s, with a timeout\n",
169 required_timeout
= pcap_get_required_select_timeout(pd
);
170 if (required_timeout
== NULL
)
171 error("select()/poll() isn't supported on %s, even with a timeout",
175 * As we won't be notified by select() or poll()
176 * that a read can be done, we'll have to periodically
177 * try reading from the device every time the required
178 * timeout expires, and we don't want those attempts
179 * to block if nothing has arrived in that interval,
180 * so we want to force non-blocking mode.
184 printf("Listening on %s, using %s\n", device
,
186 required_timeout
= NULL
;
189 printf("Listening on %s, using pcap_dispatch()\n", device
);
192 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
193 error("pcap_setnonblock failed: %s", ebuf
);
197 fd_set setread
, setexcept
;
198 struct timeval seltimeout
;
201 if (selectable_fd
!= -1) {
202 FD_SET(selectable_fd
, &setread
);
204 FD_SET(selectable_fd
, &setexcept
);
207 seltimeout
.tv_sec
= 0;
208 if (required_timeout
!= NULL
&&
209 required_timeout
->tv_usec
< 1000)
210 seltimeout
.tv_usec
= required_timeout
->tv_usec
;
212 seltimeout
.tv_usec
= 1000;
213 status
= select(selectable_fd
+ 1, &setread
,
214 NULL
, &setexcept
, &seltimeout
);
215 } else if (required_timeout
!= NULL
) {
216 seltimeout
= *required_timeout
;
217 status
= select(selectable_fd
+ 1, &setread
,
218 NULL
, &setexcept
, &seltimeout
);
220 status
= select((selectable_fd
== -1) ?
221 0 : selectable_fd
+ 1, &setread
,
222 NULL
, &setexcept
, NULL
);
225 printf("Select returns error (%s)\n",
228 if (selectable_fd
== -1) {
230 printf("Select returned a descriptor\n");
233 printf("Select timed out: ");
235 printf("Select returned a descriptor: ");
236 if (FD_ISSET(selectable_fd
, &setread
))
237 printf("readable, ");
239 printf("not readable, ");
240 if (FD_ISSET(selectable_fd
, &setexcept
))
241 printf("exceptional condition\n");
243 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
;
273 else if (required_timeout
!= NULL
&&
274 required_timeout
->tv_usec
>= 1000)
275 polltimeout
= required_timeout
->tv_usec
/1000;
278 status
= poll(&fd
, (selectable_fd
== -1) ? 0 : 1, polltimeout
);
280 printf("Poll returns error (%s)\n",
283 if (selectable_fd
== -1) {
285 printf("Poll returned a descriptor\n");
288 printf("Poll timed out\n");
290 printf("Poll returned a descriptor: ");
291 if (fd
.revents
& POLLIN
)
292 printf("readable, ");
294 printf("not readable, ");
295 if (fd
.revents
& POLLERR
)
296 printf("exceptional condition, ");
298 printf("no exceptional condition, ");
299 if (fd
.revents
& POLLHUP
)
300 printf("disconnect, ");
302 printf("no disconnect, ");
303 if (fd
.revents
& POLLNVAL
)
306 printf("not invalid\n");
310 status
= pcap_dispatch(pd
, -1, countme
,
311 (u_char
*)&packet_count
);
315 * Don't report this if we're using a
316 * required timeout and we got no packets,
317 * because that could be a very short timeout,
318 * and we don't want to spam the user with
319 * a ton of "no packets" reports.
321 if (status
!= 0 || packet_count
!= 0 ||
322 required_timeout
!= NULL
) {
323 printf("%d packets seen, %d packets counted after poll returns\n",
324 status
, packet_count
);
331 status
= pcap_dispatch(pd
, -1, countme
,
332 (u_char
*)&packet_count
);
335 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
336 status
, packet_count
);
341 * We got interrupted, so perhaps we didn't
342 * manage to finish a line we were printing.
343 * Print an extra newline, just in case.
347 (void)fflush(stdout
);
352 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
353 program_name
, pcap_geterr(pd
));
356 exit(status
== -1 ? 1 : 0);
360 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
362 int *counterp
= (int *)user
;
370 (void)fprintf(stderr
, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
377 error(const char *fmt
, ...)
381 (void)fprintf(stderr
, "%s: ", program_name
);
383 (void)vfprintf(stderr
, fmt
, ap
);
388 (void)fputc('\n', stderr
);
396 warning(const char *fmt
, ...)
400 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
402 (void)vfprintf(stderr
, fmt
, ap
);
407 (void)fputc('\n', stderr
);
412 * Copy arg vector into a new buffer, concatenating arguments with spaces.
415 copy_argv(register char **argv
)
418 register u_int len
= 0;
427 len
+= strlen(*p
++) + 1;
429 buf
= (char *)malloc(len
);
431 error("copy_argv: malloc");
435 while ((src
= *p
++) != NULL
) {
436 while ((*dst
++ = *src
++) != '\0')