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