]> The Tcpdump Group git mirrors - libpcap/blob - seltest.c
Use pcap_get_selectable_fd() to get the FD on which to do a select().
[libpcap] / seltest.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
37 char *program_name;
38
39 /* Forwards */
40 static void printme(u_char *, const struct pcap_pkthdr *, const u_char *);
41 static void usage(void) __attribute__((noreturn));
42 static void error(const char *, ...);
43 static void warning(const char *, ...);
44 static char *copy_argv(char **);
45
46 static pcap_t *pd;
47
48 extern int optind;
49 extern int opterr;
50 extern char *optarg;
51
52 int
53 main(int argc, char **argv)
54 {
55 register int op;
56 bpf_u_int32 localnet, netmask;
57 register char *cp, *cmdbuf, *device;
58 int doselect, dotimeout, dononblock;
59 struct bpf_program fcode;
60 char ebuf[PCAP_ERRBUF_SIZE];
61 int status;
62
63 device = NULL;
64 doselect = 0;
65 dotimeout = 0;
66 dononblock = 0;
67 if ((cp = strrchr(argv[0], '/')) != NULL)
68 program_name = cp + 1;
69 else
70 program_name = argv[0];
71
72 opterr = 0;
73 while ((op = getopt(argc, argv, "i:stn")) != -1)
74 switch (op) {
75
76 case 'i':
77 device = optarg;
78 break;
79
80 case 's':
81 doselect = 1;
82 break;
83
84 case 't':
85 dotimeout = 1;
86 break;
87
88 case 'n':
89 dononblock = 1;
90 break;
91
92 default:
93 usage();
94 /* NOTREACHED */
95 }
96
97 if (device == NULL) {
98 device = pcap_lookupdev(ebuf);
99 if (device == NULL)
100 error("%s", ebuf);
101 }
102 *ebuf = '\0';
103 pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
104 if (pd == NULL)
105 error("%s", ebuf);
106 else if (*ebuf)
107 warning("%s", ebuf);
108 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
109 localnet = 0;
110 netmask = 0;
111 warning("%s", ebuf);
112 }
113 cmdbuf = copy_argv(&argv[optind]);
114
115 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
116 error("%s", pcap_geterr(pd));
117
118 if (pcap_setfilter(pd, &fcode) < 0)
119 error("%s", pcap_geterr(pd));
120 if (pcap_get_selectable_fd(pd) == -1)
121 error("pcap_get_selectable_fd() fails");
122 if (dononblock) {
123 if (pcap_setnonblock(pd, 1, ebuf) == -1)
124 error("pcap_setnonblock failed: %s", ebuf);
125 }
126 printf("Listening on %s\n", device);
127 if (doselect) {
128 for (;;) {
129 int selectable_fd;
130 fd_set setread, setexcept;
131 struct timeval timeout;
132
133 selectable_fd = pcap_get_selectable_fd(pd);
134 FD_ZERO(&setread);
135 FD_SET(selectable_fd, &setread);
136 FD_ZERO(&setexcept);
137 FD_SET(selectable_fd, &setexcept);
138 if (dotimeout) {
139 timeout.tv_sec = 0;
140 timeout.tv_usec = 1000;
141 select(selectable_fd + 1, &setread, NULL,
142 &setexcept, &timeout);
143 } else
144 select(selectable_fd + 1, &setread, NULL,
145 &setexcept, NULL);
146 if (FD_ISSET(selectable_fd, &setread))
147 printf("Select says descriptor is readable\n");
148 else
149 printf("Select doesn't say descriptor is readable\n");
150 if (FD_ISSET(selectable_fd, &setexcept))
151 printf("Select says descriptor has exceptional condition\n");
152 else
153 printf("Select doesn't say descriptor has exceptional condition\n");
154 status = pcap_dispatch(pd, -1, printme, NULL);
155 if (status < 0)
156 break;
157 else if (status == 0)
158 printf("No packets seen after select returns\n");
159 else
160 printf("%d packets seen after select returns\n",
161 status);
162 }
163 } else
164 status = pcap_loop(pd, -1, printme, NULL);
165 if (status == -2) {
166 /*
167 * We got interrupted, so perhaps we didn't
168 * manage to finish a line we were printing.
169 * Print an extra newline, just in case.
170 */
171 putchar('\n');
172 }
173 (void)fflush(stdout);
174 if (status == -1) {
175 /*
176 * Error. Report it.
177 */
178 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
179 program_name, pcap_geterr(pd));
180 }
181 pcap_close(pd);
182 exit(status == -1 ? 1 : 0);
183 }
184
185 static void
186 printme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
187 {
188 printf("Saw a packet\n");
189 }
190
191 static void
192 usage(void)
193 {
194 (void)fprintf(stderr, "Usage: %s [ -stn ] [ -i interface ] [expression]\n",
195 program_name);
196 exit(1);
197 }
198
199 /* VARARGS */
200 static void
201 error(const char *fmt, ...)
202 {
203 va_list ap;
204
205 (void)fprintf(stderr, "%s: ", program_name);
206 va_start(ap, fmt);
207 (void)vfprintf(stderr, fmt, ap);
208 va_end(ap);
209 if (*fmt) {
210 fmt += strlen(fmt);
211 if (fmt[-1] != '\n')
212 (void)fputc('\n', stderr);
213 }
214 exit(1);
215 /* NOTREACHED */
216 }
217
218 /* VARARGS */
219 static void
220 warning(const char *fmt, ...)
221 {
222 va_list ap;
223
224 (void)fprintf(stderr, "%s: WARNING: ", program_name);
225 va_start(ap, fmt);
226 (void)vfprintf(stderr, fmt, ap);
227 va_end(ap);
228 if (*fmt) {
229 fmt += strlen(fmt);
230 if (fmt[-1] != '\n')
231 (void)fputc('\n', stderr);
232 }
233 }
234
235 /*
236 * Copy arg vector into a new buffer, concatenating arguments with spaces.
237 */
238 static char *
239 copy_argv(register char **argv)
240 {
241 register char **p;
242 register u_int len = 0;
243 char *buf;
244 char *src, *dst;
245
246 p = argv;
247 if (*p == 0)
248 return 0;
249
250 while (*p)
251 len += strlen(*p++) + 1;
252
253 buf = (char *)malloc(len);
254 if (buf == NULL)
255 error("copy_argv: malloc");
256
257 p = argv;
258 dst = buf;
259 while ((src = *p++) != NULL) {
260 while ((*dst++ = *src++) != '\0')
261 ;
262 dst[-1] = ' ';
263 }
264 dst[-1] = '\0';
265
266 return buf;
267 }