]>
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 */
54 * This was introduced by Clang:
56 * http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
58 * in some version (which version?); it has been picked up by GCC 5.0.
60 #ifndef __has_attribute
62 * It's a macro, so you can check whether it's defined to check
63 * whether it's supported.
65 * If it's not, define it to always return 0, so that we move on to
66 * the fallback checks.
68 #define __has_attribute(x) 0
71 #if __has_attribute(noreturn) \
72 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
73 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) \
74 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
75 || (defined(__HP_aCC) && __HP_aCC >= 61000)
77 * Compiler with support for it, or GCC 2.5 and later, or Solaris Studio 12
78 * (Sun C 5.9) and later, or IBM XL C 10.1 and later (do any earlier
79 * versions of XL C support this?), or HP aCC A.06.10 and later.
81 #define PCAP_NORETURN __attribute((noreturn))
82 #elif defined( _MSC_VER )
83 #define PCAP_NORETURN __declspec(noreturn)
88 #if __has_attribute(__format__) \
89 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)) \
90 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
91 || (defined(__HP_aCC) && __HP_aCC >= 61000)
93 * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
94 * and later (do any earlier versions of XL C support this?),
95 * or HP aCC A.06.10 and later.
97 #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
99 #define PCAP_PRINTFLIKE(x,y)
103 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
104 static void PCAP_NORETURN
usage(void);
105 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
106 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
107 static char *copy_argv(char **);
112 main(int argc
, char **argv
)
115 bpf_u_int32 localnet
, netmask
;
116 register char *cp
, *cmdbuf
, *device
;
117 int doselect
, dopoll
, dotimeout
, dononblock
;
118 struct bpf_program fcode
;
119 char ebuf
[PCAP_ERRBUF_SIZE
];
129 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
130 program_name
= cp
+ 1;
132 program_name
= argv
[0];
135 while ((op
= getopt(argc
, argv
, "i:sptn")) != -1) {
164 if (doselect
&& dopoll
) {
165 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
168 if (dotimeout
&& !doselect
&& !dopoll
) {
169 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
172 if (device
== NULL
) {
173 device
= pcap_lookupdev(ebuf
);
178 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
183 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
188 cmdbuf
= copy_argv(&argv
[optind
]);
190 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
191 error("%s", pcap_geterr(pd
));
193 if (pcap_setfilter(pd
, &fcode
) < 0)
194 error("%s", pcap_geterr(pd
));
195 if (pcap_get_selectable_fd(pd
) == -1)
196 error("pcap_get_selectable_fd() fails");
198 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
199 error("pcap_setnonblock failed: %s", ebuf
);
201 selectable_fd
= pcap_get_selectable_fd(pd
);
202 printf("Listening on %s\n", device
);
205 fd_set setread
, setexcept
;
206 struct timeval seltimeout
;
209 FD_SET(selectable_fd
, &setread
);
211 FD_SET(selectable_fd
, &setexcept
);
213 seltimeout
.tv_sec
= 0;
214 seltimeout
.tv_usec
= 1000;
215 status
= select(selectable_fd
+ 1, &setread
,
216 NULL
, &setexcept
, &seltimeout
);
218 status
= select(selectable_fd
+ 1, &setread
,
219 NULL
, &setexcept
, NULL
);
222 printf("Select returns error (%s)\n",
226 printf("Select timed out: ");
228 printf("Select returned a descriptor: ");
229 if (FD_ISSET(selectable_fd
, &setread
))
230 printf("readable, ");
232 printf("not readable, ");
233 if (FD_ISSET(selectable_fd
, &setexcept
))
234 printf("exceptional condition\n");
236 printf("no exceptional condition\n");
238 status
= pcap_dispatch(pd
, -1, countme
,
239 (u_char
*)&packet_count
);
242 printf("%d packets seen, %d packets counted after select returns\n",
243 status
, packet_count
);
251 fd
.fd
= selectable_fd
;
257 status
= poll(&fd
, 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')