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