]>
The Tcpdump Group git mirrors - libpcap/blob - tests/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
;
85 mechanism
= "pcap_dispatch()";
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
));
157 if (pcap_setfilter(pd
, &fcode
) < 0)
158 error("%s", pcap_geterr(pd
));
159 selectable_fd
= pcap_get_selectable_fd(pd
);
160 if (selectable_fd
== -1) {
161 printf("Listening on %s, using %s, with a timeout\n",
163 required_timeout
= pcap_get_required_select_timeout(pd
);
164 if (required_timeout
== NULL
)
165 if (doselect
|| dopoll
)
166 error("select()/poll() isn't supported on %s, even with a timeout",
169 * As we won't be notified by select() or poll() that
170 * a read can be done, we'll have to periodically try
171 * reading from the device every time the required
172 * timeout expires, and we don't want those attempts
173 * to block if nothing has arrived in that interval,
174 * so we want to force non-blocking mode.
178 printf("Listening on %s, using %s\n", device
, mechanism
);
179 required_timeout
= NULL
;
182 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
183 error("pcap_setnonblock failed: %s", ebuf
);
187 fd_set setread
, setexcept
;
188 struct timeval seltimeout
;
191 if (selectable_fd
!= -1) {
192 FD_SET(selectable_fd
, &setread
);
194 FD_SET(selectable_fd
, &setexcept
);
197 seltimeout
.tv_sec
= 0;
198 if (required_timeout
!= NULL
&&
199 required_timeout
->tv_usec
< 1000)
200 seltimeout
.tv_usec
= required_timeout
->tv_usec
;
202 seltimeout
.tv_usec
= 1000;
203 status
= select(selectable_fd
+ 1, &setread
,
204 NULL
, &setexcept
, &seltimeout
);
205 } else if (required_timeout
!= NULL
) {
206 seltimeout
= *required_timeout
;
207 status
= select(selectable_fd
+ 1, &setread
,
208 NULL
, &setexcept
, &seltimeout
);
210 status
= select((selectable_fd
== -1) ?
211 0 : selectable_fd
+ 1, &setread
,
212 NULL
, &setexcept
, NULL
);
215 printf("Select returns error (%s)\n",
219 printf("Select timed out: ");
221 printf("Select returned a descriptor: ");
222 if (selectable_fd
== -1)
223 printf("couldn't do select() on FD\n");
225 if (FD_ISSET(selectable_fd
, &setread
))
226 printf("readable, ");
228 printf("not readable, ");
229 if (FD_ISSET(selectable_fd
, &setexcept
))
230 printf("exceptional condition\n");
232 printf("no exceptional condition\n");
235 status
= pcap_dispatch(pd
, -1, countme
,
236 (u_char
*)&packet_count
);
239 printf("%d packets seen, %d packets counted after select returns\n",
240 status
, packet_count
);
248 fd
.fd
= selectable_fd
;
252 else if (required_timeout
!= NULL
&&
253 required_timeout
->tv_usec
>= 1000)
254 polltimeout
= required_timeout
->tv_usec
/1000;
257 status
= poll(&fd
, (selectable_fd
== -1) ? 0 : 1, polltimeout
);
259 printf("Poll returns error (%s)\n",
263 printf("Poll timed out\n");
265 printf("Poll returned a descriptor: ");
266 if (fd
.revents
& POLLIN
)
267 printf("readable, ");
269 printf("not readable, ");
270 if (fd
.revents
& POLLERR
)
271 printf("exceptional condition, ");
273 printf("no exceptional condition, ");
274 if (fd
.revents
& POLLHUP
)
275 printf("disconnect, ");
277 printf("no disconnect, ");
278 if (fd
.revents
& POLLNVAL
)
281 printf("not invalid\n");
284 status
= pcap_dispatch(pd
, -1, countme
,
285 (u_char
*)&packet_count
);
288 printf("%d packets seen, %d packets counted after poll returns\n",
289 status
, packet_count
);
295 status
= pcap_dispatch(pd
, -1, countme
,
296 (u_char
*)&packet_count
);
299 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
300 status
, packet_count
);
305 * We got interrupted, so perhaps we didn't
306 * manage to finish a line we were printing.
307 * Print an extra newline, just in case.
311 (void)fflush(stdout
);
316 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
317 program_name
, pcap_geterr(pd
));
320 exit(status
== -1 ? 1 : 0);
324 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
326 int *counterp
= (int *)user
;
334 (void)fprintf(stderr
, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
341 error(const char *fmt
, ...)
345 (void)fprintf(stderr
, "%s: ", program_name
);
347 (void)vfprintf(stderr
, fmt
, ap
);
352 (void)fputc('\n', stderr
);
360 warning(const char *fmt
, ...)
364 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
366 (void)vfprintf(stderr
, fmt
, ap
);
371 (void)fputc('\n', stderr
);
376 * Copy arg vector into a new buffer, concatenating arguments with spaces.
379 copy_argv(register char **argv
)
382 register u_int len
= 0;
391 len
+= strlen(*p
++) + 1;
393 buf
= (char *)malloc(len
);
395 error("copy_argv: malloc");
399 while ((src
= *p
++) != NULL
) {
400 while ((*dst
++ = *src
++) != '\0')