]> The Tcpdump Group git mirrors - libpcap/blob - tests/filtertest.c
Don't use non-blocking mode unless we have a timeout or use -n.
[libpcap] / tests / filtertest.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 #include "varattrs.h"
23
24 #ifndef lint
25 static const char copyright[] _U_ =
26 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27 The Regents of the University of California. All rights reserved.\n";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <pcap.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #ifdef _WIN32
40 #include "getopt.h"
41 #include "unix.h"
42 #else
43 #include <unistd.h>
44 #endif
45 #include <fcntl.h>
46 #include <errno.h>
47 #ifdef _WIN32
48 #include <winsock2.h>
49 #include <ws2tcpip.h>
50 #else
51 #include <sys/socket.h>
52 #include <arpa/inet.h>
53 #endif
54 #include <sys/types.h>
55 #include <sys/stat.h>
56
57 #include "pcap/funcattrs.h"
58
59 static char *program_name;
60
61 /* Forwards */
62 static void PCAP_NORETURN usage(void);
63 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
64 static void warn(const char *, ...) PCAP_PRINTFLIKE(1, 2);
65
66 #ifdef BDEBUG
67 int dflag;
68 #endif
69
70 /*
71 * On Windows, we need to open the file in binary mode, so that
72 * we get all the bytes specified by the size we get from "fstat()".
73 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
74 * we define it as 0 if it's not defined, so it does nothing.
75 */
76 #ifndef O_BINARY
77 #define O_BINARY 0
78 #endif
79
80 static char *
81 read_infile(char *fname)
82 {
83 register int i, fd, cc;
84 register char *cp;
85 struct stat buf;
86
87 fd = open(fname, O_RDONLY|O_BINARY);
88 if (fd < 0)
89 error("can't open %s: %s", fname, pcap_strerror(errno));
90
91 if (fstat(fd, &buf) < 0)
92 error("can't stat %s: %s", fname, pcap_strerror(errno));
93
94 cp = malloc((u_int)buf.st_size + 1);
95 if (cp == NULL)
96 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
97 fname, pcap_strerror(errno));
98 cc = read(fd, cp, (u_int)buf.st_size);
99 if (cc < 0)
100 error("read %s: %s", fname, pcap_strerror(errno));
101 if (cc != buf.st_size)
102 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
103
104 close(fd);
105 /* replace "# comment" with spaces */
106 for (i = 0; i < cc; i++) {
107 if (cp[i] == '#')
108 while (i < cc && cp[i] != '\n')
109 cp[i++] = ' ';
110 }
111 cp[cc] = '\0';
112 return (cp);
113 }
114
115 /* VARARGS */
116 static void
117 error(const char *fmt, ...)
118 {
119 va_list ap;
120
121 (void)fprintf(stderr, "%s: ", program_name);
122 va_start(ap, fmt);
123 (void)vfprintf(stderr, fmt, ap);
124 va_end(ap);
125 if (*fmt) {
126 fmt += strlen(fmt);
127 if (fmt[-1] != '\n')
128 (void)fputc('\n', stderr);
129 }
130 exit(1);
131 /* NOTREACHED */
132 }
133
134 /* VARARGS */
135 static void
136 warn(const char *fmt, ...)
137 {
138 va_list ap;
139
140 (void)fprintf(stderr, "%s: WARNING: ", program_name);
141 va_start(ap, fmt);
142 (void)vfprintf(stderr, fmt, ap);
143 va_end(ap);
144 if (*fmt) {
145 fmt += strlen(fmt);
146 if (fmt[-1] != '\n')
147 (void)fputc('\n', stderr);
148 }
149 }
150
151 /*
152 * Copy arg vector into a new buffer, concatenating arguments with spaces.
153 */
154 static char *
155 copy_argv(register char **argv)
156 {
157 register char **p;
158 register u_int len = 0;
159 char *buf;
160 char *src, *dst;
161
162 p = argv;
163 if (*p == 0)
164 return 0;
165
166 while (*p)
167 len += strlen(*p++) + 1;
168
169 buf = (char *)malloc(len);
170 if (buf == NULL)
171 error("copy_argv: malloc");
172
173 p = argv;
174 dst = buf;
175 while ((src = *p++) != NULL) {
176 while ((*dst++ = *src++) != '\0')
177 ;
178 dst[-1] = ' ';
179 }
180 dst[-1] = '\0';
181
182 return buf;
183 }
184
185 int
186 main(int argc, char **argv)
187 {
188 char *cp;
189 int op;
190 #ifndef BDEBUG
191 int dflag;
192 #endif
193 char *infile;
194 int Oflag;
195 long snaplen;
196 char *p;
197 int dlt;
198 int have_fcode = 0;
199 bpf_u_int32 netmask = PCAP_NETMASK_UNKNOWN;
200 char *cmdbuf;
201 pcap_t *pd;
202 struct bpf_program fcode;
203
204 #ifdef _WIN32
205 if (pcap_wsockinit() != 0)
206 return 1;
207 #endif /* _WIN32 */
208
209 #ifndef BDEBUG
210 dflag = 1;
211 #else
212 /* if optimizer debugging is enabled, output DOT graph
213 * `dflag=4' is equivalent to -dddd to follow -d/-dd/-ddd
214 * convention in tcpdump command line
215 */
216 dflag = 4;
217 #endif
218 infile = NULL;
219 Oflag = 1;
220 snaplen = 68;
221
222 if ((cp = strrchr(argv[0], '/')) != NULL)
223 program_name = cp + 1;
224 else
225 program_name = argv[0];
226
227 opterr = 0;
228 while ((op = getopt(argc, argv, "dF:m:Os:")) != -1) {
229 switch (op) {
230
231 case 'd':
232 ++dflag;
233 break;
234
235 case 'F':
236 infile = optarg;
237 break;
238
239 case 'O':
240 Oflag = 0;
241 break;
242
243 case 'm': {
244 bpf_u_int32 addr;
245
246 switch (inet_pton(AF_INET, optarg, &addr)) {
247
248 case 0:
249 error("invalid netmask %s", optarg);
250 break;
251
252 case -1:
253 error("invalid netmask %s: %s", optarg,
254 pcap_strerror(errno));
255 break;
256
257 case 1:
258 netmask = addr;
259 break;
260 }
261 break;
262 }
263
264 case 's': {
265 char *end;
266
267 snaplen = strtol(optarg, &end, 0);
268 if (optarg == end || *end != '\0'
269 || snaplen < 0 || snaplen > 65535)
270 error("invalid snaplen %s", optarg);
271 else if (snaplen == 0)
272 snaplen = 65535;
273 break;
274 }
275
276 default:
277 usage();
278 /* NOTREACHED */
279 }
280 }
281
282 if (optind >= argc) {
283 usage();
284 /* NOTREACHED */
285 }
286
287 dlt = pcap_datalink_name_to_val(argv[optind]);
288 if (dlt < 0) {
289 dlt = (int)strtol(argv[optind], &p, 10);
290 if (p == argv[optind] || *p != '\0')
291 error("invalid data link type %s", argv[optind]);
292 }
293
294 if (infile)
295 cmdbuf = read_infile(infile);
296 else
297 cmdbuf = copy_argv(&argv[optind+1]);
298
299 pd = pcap_open_dead(dlt, snaplen);
300 if (pd == NULL)
301 error("Can't open fake pcap_t");
302
303 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
304 error("%s", pcap_geterr(pd));
305
306 have_fcode = 1;
307 if (!bpf_validate(fcode.bf_insns, fcode.bf_len))
308 warn("Filter doesn't pass validation");
309
310 #ifdef BDEBUG
311 if (cmdbuf != NULL) {
312 // replace line feed with space
313 for (cp = cmdbuf; *cp != '\0'; ++cp) {
314 if (*cp == '\r' || *cp == '\n') {
315 *cp = ' ';
316 }
317 }
318 // only show machine code if BDEBUG defined, since dflag > 3
319 printf("machine codes for filter: %s\n", cmdbuf);
320 } else
321 printf("machine codes for empty filter:\n");
322 #endif
323
324 bpf_dump(&fcode, dflag);
325 free(cmdbuf);
326 if (have_fcode)
327 pcap_freecode (&fcode);
328 pcap_close(pd);
329 exit(0);
330 }
331
332 static void
333 usage(void)
334 {
335 (void)fprintf(stderr, "%s, with %s\n", program_name,
336 pcap_lib_version());
337 (void)fprintf(stderr,
338 "Usage: %s [-dO] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n",
339 program_name);
340 exit(1);
341 }