]>
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 #include <sys/select.h>
50 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
51 static void usage(void) __attribute__((noreturn
));
52 static void error(const char *, ...);
53 static void warning(const char *, ...);
54 static char *copy_argv(char **);
59 main(int argc
, char **argv
)
62 bpf_u_int32 localnet
, netmask
;
63 register char *cp
, *cmdbuf
, *device
;
64 int doselect
, dopoll
, dotimeout
, dononblock
;
65 struct bpf_program fcode
;
66 char ebuf
[PCAP_ERRBUF_SIZE
];
76 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
77 program_name
= cp
+ 1;
79 program_name
= argv
[0];
82 while ((op
= getopt(argc
, argv
, "i:sptn")) != -1) {
111 if (doselect
&& dopoll
) {
112 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
115 if (dotimeout
&& !doselect
&& !dopoll
) {
116 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
119 if (device
== NULL
) {
120 device
= pcap_lookupdev(ebuf
);
125 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
130 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
135 cmdbuf
= copy_argv(&argv
[optind
]);
137 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
138 error("%s", pcap_geterr(pd
));
140 if (pcap_setfilter(pd
, &fcode
) < 0)
141 error("%s", pcap_geterr(pd
));
142 if (pcap_get_selectable_fd(pd
) == -1)
143 error("pcap_get_selectable_fd() fails");
145 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
146 error("pcap_setnonblock failed: %s", ebuf
);
148 selectable_fd
= pcap_get_selectable_fd(pd
);
149 printf("Listening on %s\n", device
);
152 fd_set setread
, setexcept
;
153 struct timeval seltimeout
;
156 FD_SET(selectable_fd
, &setread
);
158 FD_SET(selectable_fd
, &setexcept
);
160 seltimeout
.tv_sec
= 0;
161 seltimeout
.tv_usec
= 1000;
162 status
= select(selectable_fd
+ 1, &setread
,
163 NULL
, &setexcept
, &seltimeout
);
165 status
= select(selectable_fd
+ 1, &setread
,
166 NULL
, &setexcept
, NULL
);
169 printf("Select returns error (%s)\n",
173 printf("Select timed out: ");
175 printf("Select returned a descriptor: ");
176 if (FD_ISSET(selectable_fd
, &setread
))
177 printf("readable, ");
179 printf("not readable, ");
180 if (FD_ISSET(selectable_fd
, &setexcept
))
181 printf("exceptional condition\n");
183 printf("no exceptional condition\n");
185 status
= pcap_dispatch(pd
, -1, countme
,
186 (u_char
*)&packet_count
);
189 printf("%d packets seen, %d packets counted after select returns\n",
190 status
, packet_count
);
198 fd
.fd
= selectable_fd
;
204 status
= poll(&fd
, 1, polltimeout
);
206 printf("Poll returns error (%s)\n",
210 printf("Poll timed out\n");
212 printf("Poll returned a descriptor: ");
213 if (fd
.revents
& POLLIN
)
214 printf("readable, ");
216 printf("not readable, ");
217 if (fd
.revents
& POLLERR
)
218 printf("exceptional condition, ");
220 printf("no exceptional condition, ");
221 if (fd
.revents
& POLLHUP
)
222 printf("disconnect, ");
224 printf("no disconnect, ");
225 if (fd
.revents
& POLLNVAL
)
228 printf("not invalid\n");
231 status
= pcap_dispatch(pd
, -1, countme
,
232 (u_char
*)&packet_count
);
235 printf("%d packets seen, %d packets counted after poll returns\n",
236 status
, packet_count
);
242 status
= pcap_dispatch(pd
, -1, countme
,
243 (u_char
*)&packet_count
);
246 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
247 status
, packet_count
);
252 * We got interrupted, so perhaps we didn't
253 * manage to finish a line we were printing.
254 * Print an extra newline, just in case.
258 (void)fflush(stdout
);
263 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
264 program_name
, pcap_geterr(pd
));
267 exit(status
== -1 ? 1 : 0);
271 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
273 int *counterp
= (int *)user
;
281 (void)fprintf(stderr
, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
288 error(const char *fmt
, ...)
292 (void)fprintf(stderr
, "%s: ", program_name
);
294 (void)vfprintf(stderr
, fmt
, ap
);
299 (void)fputc('\n', stderr
);
307 warning(const char *fmt
, ...)
311 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
313 (void)vfprintf(stderr
, fmt
, ap
);
318 (void)fputc('\n', stderr
);
323 * Copy arg vector into a new buffer, concatenating arguments with spaces.
326 copy_argv(register char **argv
)
329 register u_int len
= 0;
338 len
+= strlen(*p
++) + 1;
340 buf
= (char *)malloc(len
);
342 error("copy_argv: malloc");
346 while ((src
= *p
++) != NULL
) {
347 while ((*dst
++ = *src
++) != '\0')