]>
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
;
73 struct bpf_program fcode
;
74 char ebuf
[PCAP_ERRBUF_SIZE
];
85 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
86 program_name
= cp
+ 1;
88 program_name
= argv
[0];
91 while ((op
= getopt(argc
, argv
, "i:sptn")) != -1) {
120 if (doselect
&& dopoll
) {
121 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
124 if (dotimeout
&& !doselect
&& !dopoll
) {
125 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
128 if (device
== NULL
) {
129 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
132 error("no interfaces available for capture");
133 device
= strdup(devlist
->name
);
134 pcap_freealldevs(devlist
);
137 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
142 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
147 cmdbuf
= copy_argv(&argv
[optind
]);
149 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
150 error("%s", pcap_geterr(pd
));
152 if (pcap_setfilter(pd
, &fcode
) < 0)
153 error("%s", pcap_geterr(pd
));
154 if (pcap_get_selectable_fd(pd
) == -1)
155 error("pcap_get_selectable_fd() fails");
157 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
158 error("pcap_setnonblock failed: %s", ebuf
);
160 selectable_fd
= pcap_get_selectable_fd(pd
);
161 printf("Listening on %s\n", device
);
164 fd_set setread
, setexcept
;
165 struct timeval seltimeout
;
168 FD_SET(selectable_fd
, &setread
);
170 FD_SET(selectable_fd
, &setexcept
);
172 seltimeout
.tv_sec
= 0;
173 seltimeout
.tv_usec
= 1000;
174 status
= select(selectable_fd
+ 1, &setread
,
175 NULL
, &setexcept
, &seltimeout
);
177 status
= select(selectable_fd
+ 1, &setread
,
178 NULL
, &setexcept
, NULL
);
181 printf("Select returns error (%s)\n",
185 printf("Select timed out: ");
187 printf("Select returned a descriptor: ");
188 if (FD_ISSET(selectable_fd
, &setread
))
189 printf("readable, ");
191 printf("not readable, ");
192 if (FD_ISSET(selectable_fd
, &setexcept
))
193 printf("exceptional condition\n");
195 printf("no exceptional condition\n");
197 status
= pcap_dispatch(pd
, -1, countme
,
198 (u_char
*)&packet_count
);
201 printf("%d packets seen, %d packets counted after select returns\n",
202 status
, packet_count
);
210 fd
.fd
= selectable_fd
;
216 status
= poll(&fd
, 1, polltimeout
);
218 printf("Poll returns error (%s)\n",
222 printf("Poll timed out\n");
224 printf("Poll returned a descriptor: ");
225 if (fd
.revents
& POLLIN
)
226 printf("readable, ");
228 printf("not readable, ");
229 if (fd
.revents
& POLLERR
)
230 printf("exceptional condition, ");
232 printf("no exceptional condition, ");
233 if (fd
.revents
& POLLHUP
)
234 printf("disconnect, ");
236 printf("no disconnect, ");
237 if (fd
.revents
& POLLNVAL
)
240 printf("not invalid\n");
243 status
= pcap_dispatch(pd
, -1, countme
,
244 (u_char
*)&packet_count
);
247 printf("%d packets seen, %d packets counted after poll returns\n",
248 status
, packet_count
);
254 status
= pcap_dispatch(pd
, -1, countme
,
255 (u_char
*)&packet_count
);
258 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
259 status
, packet_count
);
264 * We got interrupted, so perhaps we didn't
265 * manage to finish a line we were printing.
266 * Print an extra newline, just in case.
270 (void)fflush(stdout
);
275 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
276 program_name
, pcap_geterr(pd
));
279 exit(status
== -1 ? 1 : 0);
283 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
285 int *counterp
= (int *)user
;
293 (void)fprintf(stderr
, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
300 error(const char *fmt
, ...)
304 (void)fprintf(stderr
, "%s: ", program_name
);
306 (void)vfprintf(stderr
, fmt
, ap
);
311 (void)fputc('\n', stderr
);
319 warning(const char *fmt
, ...)
323 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
325 (void)vfprintf(stderr
, fmt
, ap
);
330 (void)fputc('\n', stderr
);
335 * Copy arg vector into a new buffer, concatenating arguments with spaces.
338 copy_argv(register char **argv
)
341 register u_int len
= 0;
350 len
+= strlen(*p
++) + 1;
352 buf
= (char *)malloc(len
);
354 error("copy_argv: malloc");
358 while ((src
= *p
++) != NULL
) {
359 while ((*dst
++ = *src
++) != '\0')