]> The Tcpdump Group git mirrors - libpcap/blob - selpolltest.c
Clean up per-packet output a bit; don't output a blank line if no
[libpcap] / selpolltest.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
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";
26 #endif
27
28 #include <pcap.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <sys/select.h>
37 #include <poll.h>
38
39 char *program_name;
40
41 /* Forwards */
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 **);
47
48 static pcap_t *pd;
49
50 extern int optind;
51 extern int opterr;
52 extern char *optarg;
53
54 int
55 main(int argc, char **argv)
56 {
57 register int op;
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];
63 int selectable_fd;
64 int status;
65 int packet_count;
66
67 device = NULL;
68 doselect = 0;
69 dopoll = 0;
70 dotimeout = 0;
71 dononblock = 0;
72 if ((cp = strrchr(argv[0], '/')) != NULL)
73 program_name = cp + 1;
74 else
75 program_name = argv[0];
76
77 opterr = 0;
78 while ((op = getopt(argc, argv, "i:sptn")) != -1) {
79 switch (op) {
80
81 case 'i':
82 device = optarg;
83 break;
84
85 case 's':
86 doselect = 1;
87 break;
88
89 case 'p':
90 dopoll = 1;
91 break;
92
93 case 't':
94 dotimeout = 1;
95 break;
96
97 case 'n':
98 dononblock = 1;
99 break;
100
101 default:
102 usage();
103 /* NOTREACHED */
104 }
105 }
106
107 if (doselect && dopoll) {
108 fprintf(stderr, "selpolltest: choose select (-s) or poll (-p), but not both\n");
109 return 1;
110 }
111 if (dotimeout && !doselect && !dopoll) {
112 fprintf(stderr, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n");
113 return 1;
114 }
115 if (device == NULL) {
116 device = pcap_lookupdev(ebuf);
117 if (device == NULL)
118 error("%s", ebuf);
119 }
120 *ebuf = '\0';
121 pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
122 if (pd == NULL)
123 error("%s", ebuf);
124 else if (*ebuf)
125 warning("%s", ebuf);
126 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
127 localnet = 0;
128 netmask = 0;
129 warning("%s", ebuf);
130 }
131 cmdbuf = copy_argv(&argv[optind]);
132
133 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
134 error("%s", pcap_geterr(pd));
135
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");
140 if (dononblock) {
141 if (pcap_setnonblock(pd, 1, ebuf) == -1)
142 error("pcap_setnonblock failed: %s", ebuf);
143 }
144 selectable_fd = pcap_get_selectable_fd(pd);
145 printf("Listening on %s\n", device);
146 if (doselect) {
147 for (;;) {
148 fd_set setread, setexcept;
149 struct timeval seltimeout;
150
151 FD_ZERO(&setread);
152 FD_SET(selectable_fd, &setread);
153 FD_ZERO(&setexcept);
154 FD_SET(selectable_fd, &setexcept);
155 if (dotimeout) {
156 seltimeout.tv_sec = 0;
157 seltimeout.tv_usec = 1000;
158 status = select(selectable_fd + 1, &setread,
159 NULL, &setexcept, &seltimeout);
160 } else {
161 status = select(selectable_fd + 1, &setread,
162 NULL, &setexcept, NULL);
163 }
164 if (status == -1) {
165 printf("Select returns error (%s)\n",
166 strerror(errno));
167 } else {
168 if (status == 0)
169 printf("Select timed out: ");
170 else
171 printf("Select returned a descriptor: ");
172 if (FD_ISSET(selectable_fd, &setread))
173 printf("readable, ");
174 else
175 printf("not readable, ");
176 if (FD_ISSET(selectable_fd, &setexcept))
177 printf("exceptional condition\n");
178 else
179 printf("no exceptional condition\n");
180 packet_count = 0;
181 status = pcap_dispatch(pd, -1, printme,
182 (u_char *)&packet_count);
183 if (status < 0)
184 break;
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);
189 }
190 }
191 } else if (dopoll) {
192 for (;;) {
193 struct pollfd fd;
194 int polltimeout;
195
196 fd.fd = selectable_fd;
197 fd.events = POLLIN;
198 if (dotimeout)
199 polltimeout = 1;
200 else
201 polltimeout = -1;
202 status = poll(&fd, 1, polltimeout);
203 if (status == -1) {
204 printf("Poll returns error (%s)\n",
205 strerror(errno));
206 } else {
207 if (status == 0)
208 printf("Poll timed out\n");
209 else {
210 printf("Poll returned a descriptor: ");
211 if (fd.revents & POLLIN)
212 printf("readable, ");
213 else
214 printf("not readable, ");
215 if (fd.revents & POLLERR)
216 printf("exceptional condition, ");
217 else
218 printf("no exceptional condition, ");
219 if (fd.revents & POLLHUP)
220 printf("disconnect, ");
221 else
222 printf("no disconnect, ");
223 if (fd.revents & POLLNVAL)
224 printf("invalid\n");
225 else
226 printf("not invalid\n");
227 }
228 packet_count = 0;
229 status = pcap_dispatch(pd, -1, printme,
230 (u_char *)&packet_count);
231 if (status < 0)
232 break;
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);
237 }
238 }
239 } else {
240 for (;;) {
241 packet_count = 0;
242 status = pcap_dispatch(pd, -1, printme,
243 (u_char *)&packet_count);
244 if (status < 0)
245 break;
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);
250 }
251 }
252 if (status == -2) {
253 /*
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.
257 */
258 putchar('\n');
259 }
260 (void)fflush(stdout);
261 if (status == -1) {
262 /*
263 * Error. Report it.
264 */
265 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
266 program_name, pcap_geterr(pd));
267 }
268 pcap_close(pd);
269 exit(status == -1 ? 1 : 0);
270 }
271
272 static void
273 printme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
274 {
275 int *counterp = (int *)user;
276
277 printf(".");
278 (*counterp)++;
279 }
280
281 static void
282 usage(void)
283 {
284 (void)fprintf(stderr, "Usage: %s [ -stn ] [ -i interface ] [expression]\n",
285 program_name);
286 exit(1);
287 }
288
289 /* VARARGS */
290 static void
291 error(const char *fmt, ...)
292 {
293 va_list ap;
294
295 (void)fprintf(stderr, "%s: ", program_name);
296 va_start(ap, fmt);
297 (void)vfprintf(stderr, fmt, ap);
298 va_end(ap);
299 if (*fmt) {
300 fmt += strlen(fmt);
301 if (fmt[-1] != '\n')
302 (void)fputc('\n', stderr);
303 }
304 exit(1);
305 /* NOTREACHED */
306 }
307
308 /* VARARGS */
309 static void
310 warning(const char *fmt, ...)
311 {
312 va_list ap;
313
314 (void)fprintf(stderr, "%s: WARNING: ", program_name);
315 va_start(ap, fmt);
316 (void)vfprintf(stderr, fmt, ap);
317 va_end(ap);
318 if (*fmt) {
319 fmt += strlen(fmt);
320 if (fmt[-1] != '\n')
321 (void)fputc('\n', stderr);
322 }
323 }
324
325 /*
326 * Copy arg vector into a new buffer, concatenating arguments with spaces.
327 */
328 static char *
329 copy_argv(register char **argv)
330 {
331 register char **p;
332 register u_int len = 0;
333 char *buf;
334 char *src, *dst;
335
336 p = argv;
337 if (*p == 0)
338 return 0;
339
340 while (*p)
341 len += strlen(*p++) + 1;
342
343 buf = (char *)malloc(len);
344 if (buf == NULL)
345 error("copy_argv: malloc");
346
347 p = argv;
348 dst = buf;
349 while ((src = *p++) != NULL) {
350 while ((*dst++ = *src++) != '\0')
351 ;
352 dst[-1] = ' ';
353 }
354 dst[-1] = '\0';
355
356 return buf;
357 }