]>
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.
23 static const char copyright
[] _U_
=
24 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
25 The Regents of the University of California. All rights reserved.\n";
29 * Tests how select() and poll() behave on the selectable file descriptor
32 * This would be significantly different on Windows, as it'd test
33 * how WaitForMultipleObjects() would work on the event handle for a
43 #include <sys/types.h>
44 #ifdef HAVE_SYS_SELECT_H
45 #include <sys/select.h>
47 #include <sys/time.h> /* older UN*Xes */
51 #include "pcap/funcattrs.h"
56 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
57 static void PCAP_NORETURN
usage(void);
58 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
59 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
60 static char *copy_argv(char **);
65 main(int argc
, char **argv
)
68 bpf_u_int32 localnet
, netmask
;
69 register char *cp
, *cmdbuf
, *device
;
70 int doselect
, dopoll
, dotimeout
, dononblock
;
71 struct bpf_program fcode
;
72 char ebuf
[PCAP_ERRBUF_SIZE
];
83 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
84 program_name
= cp
+ 1;
86 program_name
= argv
[0];
89 while ((op
= getopt(argc
, argv
, "i:sptn")) != -1) {
118 if (doselect
&& dopoll
) {
119 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
122 if (dotimeout
&& !doselect
&& !dopoll
) {
123 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
126 if (device
== NULL
) {
127 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
130 error("no interfaces available for capture");
131 device
= strdup(devlist
->name
);
132 pcap_freealldevs(devlist
);
135 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
140 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
145 cmdbuf
= copy_argv(&argv
[optind
]);
147 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
148 error("%s", pcap_geterr(pd
));
150 if (pcap_setfilter(pd
, &fcode
) < 0)
151 error("%s", pcap_geterr(pd
));
152 if (pcap_get_selectable_fd(pd
) == -1)
153 error("pcap_get_selectable_fd() fails");
155 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
156 error("pcap_setnonblock failed: %s", ebuf
);
158 selectable_fd
= pcap_get_selectable_fd(pd
);
159 printf("Listening on %s\n", device
);
162 fd_set setread
, setexcept
;
163 struct timeval seltimeout
;
166 FD_SET(selectable_fd
, &setread
);
168 FD_SET(selectable_fd
, &setexcept
);
170 seltimeout
.tv_sec
= 0;
171 seltimeout
.tv_usec
= 1000;
172 status
= select(selectable_fd
+ 1, &setread
,
173 NULL
, &setexcept
, &seltimeout
);
175 status
= select(selectable_fd
+ 1, &setread
,
176 NULL
, &setexcept
, NULL
);
179 printf("Select returns error (%s)\n",
183 printf("Select timed out: ");
185 printf("Select returned a descriptor: ");
186 if (FD_ISSET(selectable_fd
, &setread
))
187 printf("readable, ");
189 printf("not readable, ");
190 if (FD_ISSET(selectable_fd
, &setexcept
))
191 printf("exceptional condition\n");
193 printf("no exceptional condition\n");
195 status
= pcap_dispatch(pd
, -1, countme
,
196 (u_char
*)&packet_count
);
199 printf("%d packets seen, %d packets counted after select returns\n",
200 status
, packet_count
);
208 fd
.fd
= selectable_fd
;
214 status
= poll(&fd
, 1, polltimeout
);
216 printf("Poll returns error (%s)\n",
220 printf("Poll timed out\n");
222 printf("Poll returned a descriptor: ");
223 if (fd
.revents
& POLLIN
)
224 printf("readable, ");
226 printf("not readable, ");
227 if (fd
.revents
& POLLERR
)
228 printf("exceptional condition, ");
230 printf("no exceptional condition, ");
231 if (fd
.revents
& POLLHUP
)
232 printf("disconnect, ");
234 printf("no disconnect, ");
235 if (fd
.revents
& POLLNVAL
)
238 printf("not invalid\n");
241 status
= pcap_dispatch(pd
, -1, countme
,
242 (u_char
*)&packet_count
);
245 printf("%d packets seen, %d packets counted after poll returns\n",
246 status
, packet_count
);
252 status
= pcap_dispatch(pd
, -1, countme
,
253 (u_char
*)&packet_count
);
256 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
257 status
, packet_count
);
262 * We got interrupted, so perhaps we didn't
263 * manage to finish a line we were printing.
264 * Print an extra newline, just in case.
268 (void)fflush(stdout
);
273 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
274 program_name
, pcap_geterr(pd
));
277 exit(status
== -1 ? 1 : 0);
281 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
283 int *counterp
= (int *)user
;
291 (void)fprintf(stderr
, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
298 error(const char *fmt
, ...)
302 (void)fprintf(stderr
, "%s: ", program_name
);
304 (void)vfprintf(stderr
, fmt
, ap
);
309 (void)fputc('\n', stderr
);
317 warning(const char *fmt
, ...)
321 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
323 (void)vfprintf(stderr
, fmt
, ap
);
328 (void)fputc('\n', stderr
);
333 * Copy arg vector into a new buffer, concatenating arguments with spaces.
336 copy_argv(register char **argv
)
339 register u_int len
= 0;
348 len
+= strlen(*p
++) + 1;
350 buf
= (char *)malloc(len
);
352 error("copy_argv: malloc");
356 while ((src
= *p
++) != NULL
) {
357 while ((*dst
++ = *src
++) != '\0')