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 countme(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, countme,
182 (u_char *)&packet_count);
185 printf("%d packets seen, %d packets counted after select returns\n",
186 status, packet_count);
194 fd.fd = selectable_fd;
200 status = poll(&fd, 1, polltimeout);
202 printf("Poll returns error (%s)\n",
206 printf("Poll timed out\n");
208 printf("Poll returned a descriptor: ");
209 if (fd.revents & POLLIN)
210 printf("readable, ");
212 printf("not readable, ");
213 if (fd.revents & POLLERR)
214 printf("exceptional condition, ");
216 printf("no exceptional condition, ");
217 if (fd.revents & POLLHUP)
218 printf("disconnect, ");
220 printf("no disconnect, ");
221 if (fd.revents & POLLNVAL)
224 printf("not invalid\n");
227 status = pcap_dispatch(pd, -1, countme,
228 (u_char *)&packet_count);
231 printf("%d packets seen, %d packets counted after poll returns\n",
232 status, packet_count);
238 status = pcap_dispatch(pd, -1, countme,
239 (u_char *)&packet_count);
242 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
243 status, packet_count);
248 * We got interrupted, so perhaps we didn't
249 * manage to finish a line we were printing.
250 * Print an extra newline, just in case.
254 (void)fflush(stdout);
259 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
260 program_name, pcap_geterr(pd));
263 exit(status == -1 ? 1 : 0);
267 countme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
269 int *counterp = (int *)user;
277 (void)fprintf(stderr, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
284 error(const char *fmt, ...)
288 (void)fprintf(stderr, "%s: ", program_name);
290 (void)vfprintf(stderr, fmt, ap);
295 (void)fputc('\n', stderr);
303 warning(const char *fmt, ...)
307 (void)fprintf(stderr, "%s: WARNING: ", program_name);
309 (void)vfprintf(stderr, fmt, ap);
314 (void)fputc('\n', stderr);
319 * Copy arg vector into a new buffer, concatenating arguments with spaces.
322 copy_argv(register char **argv)
325 register u_int len = 0;
334 len += strlen(*p++) + 1;
336 buf = (char *)malloc(len);
338 error("copy_argv: malloc");
342 while ((src = *p++) != NULL) {
343 while ((*dst++ = *src++) != '\0')