]>
The Tcpdump Group git mirrors - libpcap/blob - 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
[] =
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";
35 #include <sys/types.h>
36 #include <sys/select.h>
42 static void printme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
43 static void usage(void) __attribute__((noreturn
));
44 static void error(const char *, ...);
45 static void warning(const char *, ...);
46 static char *copy_argv(char **);
55 main(int argc
, char **argv
)
58 bpf_u_int32 localnet
, netmask
;
59 register char *cp
, *cmdbuf
, *device
;
60 int doselect
, dopoll
, dotimeout
, dononblock
;
61 struct bpf_program fcode
;
62 char ebuf
[PCAP_ERRBUF_SIZE
];
72 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
73 program_name
= cp
+ 1;
75 program_name
= argv
[0];
78 while ((op
= getopt(argc
, argv
, "i:sptn")) != -1) {
107 if (doselect
&& dopoll
) {
108 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
111 if (dotimeout
&& !doselect
&& !dopoll
) {
112 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
115 if (device
== NULL
) {
116 device
= pcap_lookupdev(ebuf
);
121 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
126 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
131 cmdbuf
= copy_argv(&argv
[optind
]);
133 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
134 error("%s", pcap_geterr(pd
));
136 if (pcap_setfilter(pd
, &fcode
) < 0)
137 error("%s", pcap_geterr(pd
));
138 if (pcap_get_selectable_fd(pd
) == -1)
139 error("pcap_get_selectable_fd() fails");
141 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
142 error("pcap_setnonblock failed: %s", ebuf
);
144 selectable_fd
= pcap_get_selectable_fd(pd
);
145 printf("Listening on %s\n", device
);
148 fd_set setread
, setexcept
;
149 struct timeval seltimeout
;
152 FD_SET(selectable_fd
, &setread
);
154 FD_SET(selectable_fd
, &setexcept
);
156 seltimeout
.tv_sec
= 0;
157 seltimeout
.tv_usec
= 1000;
158 status
= select(selectable_fd
+ 1, &setread
,
159 NULL
, &setexcept
, &seltimeout
);
161 status
= select(selectable_fd
+ 1, &setread
,
162 NULL
, &setexcept
, NULL
);
165 printf("Select returns error (%s)\n",
169 printf("Select timed out: ");
171 printf("Select returned a descriptor: ");
172 if (FD_ISSET(selectable_fd
, &setread
))
173 printf("readable, ");
175 printf("not readable, ");
176 if (FD_ISSET(selectable_fd
, &setexcept
))
177 printf("exceptional condition\n");
179 printf("no exceptional condition\n");
181 status
= pcap_dispatch(pd
, -1, printme
,
182 (u_char
*)&packet_count
);
185 if (packet_count
!= 0)
186 putchar('\n'); /* finish line of .'s */
187 printf("%d packets seen, %d packets counted after select returns\n",
188 status
, packet_count
);
196 fd
.fd
= selectable_fd
;
202 status
= poll(&fd
, 1, polltimeout
);
204 printf("Poll returns error (%s)\n",
208 printf("Poll timed out\n");
210 printf("Poll returned a descriptor: ");
211 if (fd
.revents
& POLLIN
)
212 printf("readable, ");
214 printf("not readable, ");
215 if (fd
.revents
& POLLERR
)
216 printf("exceptional condition, ");
218 printf("no exceptional condition, ");
219 if (fd
.revents
& POLLHUP
)
220 printf("disconnect, ");
222 printf("no disconnect, ");
223 if (fd
.revents
& POLLNVAL
)
226 printf("not invalid\n");
229 status
= pcap_dispatch(pd
, -1, printme
,
230 (u_char
*)&packet_count
);
233 if (packet_count
!= 0)
234 putchar('\n'); /* finish line of .'s */
235 printf("%d packets seen, %d packets counted after poll returns\n",
236 status
, packet_count
);
242 status
= pcap_dispatch(pd
, -1, printme
,
243 (u_char
*)&packet_count
);
246 if (packet_count
!= 0)
247 putchar('\n'); /* finish line of .'s */
248 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
249 status
, packet_count
);
254 * We got interrupted, so perhaps we didn't
255 * manage to finish a line we were printing.
256 * Print an extra newline, just in case.
260 (void)fflush(stdout
);
265 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
266 program_name
, pcap_geterr(pd
));
269 exit(status
== -1 ? 1 : 0);
273 printme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
275 int *counterp
= (int *)user
;
284 (void)fprintf(stderr
, "Usage: %s [ -stn ] [ -i interface ] [expression]\n",
291 error(const char *fmt
, ...)
295 (void)fprintf(stderr
, "%s: ", program_name
);
297 (void)vfprintf(stderr
, fmt
, ap
);
302 (void)fputc('\n', stderr
);
310 warning(const char *fmt
, ...)
314 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
316 (void)vfprintf(stderr
, fmt
, ap
);
321 (void)fputc('\n', stderr
);
326 * Copy arg vector into a new buffer, concatenating arguments with spaces.
329 copy_argv(register char **argv
)
332 register u_int len
= 0;
341 len
+= strlen(*p
++) + 1;
343 buf
= (char *)malloc(len
);
345 error("copy_argv: malloc");
349 while ((src
= *p
++) != NULL
) {
350 while ((*dst
++ = *src
++) != '\0')