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