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