1 /* $NetBSD: selpolltest.c,v 1.2 2014/11/19 19:33:31 christos Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 __RCSID("$NetBSD: selpolltest.c,v 1.2 2014/11/19 19:33:31 christos Exp $");
28 static const char copyright
[] =
29 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
30 The Regents of the University of California. All rights reserved.\n";
40 #include <sys/types.h>
41 #include <sys/select.h>
47 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
48 static void usage(void) __attribute__((noreturn
));
49 static void error(const char *, ...);
50 static void warning(const char *, ...);
51 static char *copy_argv(char **);
60 main(int argc
, char **argv
)
63 bpf_u_int32 localnet
, netmask
;
64 register char *cp
, *cmdbuf
, *device
;
65 int doselect
, dopoll
, dotimeout
, dononblock
;
66 struct bpf_program fcode
;
67 char ebuf
[PCAP_ERRBUF_SIZE
];
77 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
78 program_name
= cp
+ 1;
80 program_name
= argv
[0];
83 while ((op
= getopt(argc
, argv
, "i:sptn")) != -1) {
112 if (doselect
&& dopoll
) {
113 fprintf(stderr
, "selpolltest: choose select (-s) or poll (-p), but not both\n");
116 if (dotimeout
&& !doselect
&& !dopoll
) {
117 fprintf(stderr
, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
120 if (device
== NULL
) {
121 device
= pcap_lookupdev(ebuf
);
126 pd
= pcap_open_live(device
, 65535, 0, 1000, ebuf
);
131 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
136 cmdbuf
= copy_argv(&argv
[optind
]);
138 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
139 error("%s", pcap_geterr(pd
));
141 if (pcap_setfilter(pd
, &fcode
) < 0)
142 error("%s", pcap_geterr(pd
));
143 if (pcap_get_selectable_fd(pd
) == -1)
144 error("pcap_get_selectable_fd() fails");
146 if (pcap_setnonblock(pd
, 1, ebuf
) == -1)
147 error("pcap_setnonblock failed: %s", ebuf
);
149 selectable_fd
= pcap_get_selectable_fd(pd
);
150 printf("Listening on %s\n", device
);
153 fd_set setread
, setexcept
;
154 struct timeval seltimeout
;
157 FD_SET(selectable_fd
, &setread
);
159 FD_SET(selectable_fd
, &setexcept
);
161 seltimeout
.tv_sec
= 0;
162 seltimeout
.tv_usec
= 1000;
163 status
= select(selectable_fd
+ 1, &setread
,
164 NULL
, &setexcept
, &seltimeout
);
166 status
= select(selectable_fd
+ 1, &setread
,
167 NULL
, &setexcept
, NULL
);
170 printf("Select returns error (%s)\n",
174 printf("Select timed out: ");
176 printf("Select returned a descriptor: ");
177 if (FD_ISSET(selectable_fd
, &setread
))
178 printf("readable, ");
180 printf("not readable, ");
181 if (FD_ISSET(selectable_fd
, &setexcept
))
182 printf("exceptional condition\n");
184 printf("no exceptional condition\n");
186 status
= pcap_dispatch(pd
, -1, countme
,
187 (u_char
*)&packet_count
);
190 printf("%d packets seen, %d packets counted after select returns\n",
191 status
, packet_count
);
199 fd
.fd
= selectable_fd
;
205 status
= poll(&fd
, 1, polltimeout
);
207 printf("Poll returns error (%s)\n",
211 printf("Poll timed out\n");
213 printf("Poll returned a descriptor: ");
214 if (fd
.revents
& POLLIN
)
215 printf("readable, ");
217 printf("not readable, ");
218 if (fd
.revents
& POLLERR
)
219 printf("exceptional condition, ");
221 printf("no exceptional condition, ");
222 if (fd
.revents
& POLLHUP
)
223 printf("disconnect, ");
225 printf("no disconnect, ");
226 if (fd
.revents
& POLLNVAL
)
229 printf("not invalid\n");
232 status
= pcap_dispatch(pd
, -1, countme
,
233 (u_char
*)&packet_count
);
236 printf("%d packets seen, %d packets counted after poll returns\n",
237 status
, packet_count
);
243 status
= pcap_dispatch(pd
, -1, countme
,
244 (u_char
*)&packet_count
);
247 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
248 status
, packet_count
);
253 * We got interrupted, so perhaps we didn't
254 * manage to finish a line we were printing.
255 * Print an extra newline, just in case.
259 (void)fflush(stdout
);
264 (void)fprintf(stderr
, "%s: pcap_loop: %s\n",
265 program_name
, pcap_geterr(pd
));
268 exit(status
== -1 ? 1 : 0);
272 countme(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
274 int *counterp
= (int *)user
;
282 (void)fprintf(stderr
, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n",
289 error(const char *fmt
, ...)
293 (void)fprintf(stderr
, "%s: ", program_name
);
295 (void)vfprintf(stderr
, fmt
, ap
);
300 (void)fputc('\n', stderr
);
308 warning(const char *fmt
, ...)
312 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
314 (void)vfprintf(stderr
, fmt
, ap
);
319 (void)fputc('\n', stderr
);
324 * Copy arg vector into a new buffer, concatenating arguments with spaces.
327 copy_argv(register char **argv
)
330 register u_int len
= 0;
339 len
+= strlen(*p
++) + 1;
341 buf
= (char *)malloc(len
);
343 error("copy_argv: malloc");
347 while ((src
= *p
++) != NULL
) {
348 while ((*dst
++ = *src
++) != '\0')