]>
The Tcpdump Group git mirrors - libpcap/blob - tests/filtertest.c
e45db21eabbfd8f78806ac3e7b3bd39e19bf7d25
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.
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
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.
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";
40 #include <arpa/inet.h>
41 #include <sys/types.h>
44 #ifndef HAVE___ATTRIBUTE__
45 #define __attribute__(x)
48 static char *program_name
;
51 static void usage(void) __attribute__((noreturn
));
52 static void error(const char *, ...)
53 __attribute__((noreturn
, format (printf
, 1, 2)));
54 static void warn(const char *, ...)
55 __attribute__((format (printf
, 1, 2)));
65 * On Windows, we need to open the file in binary mode, so that
66 * we get all the bytes specified by the size we get from "fstat()".
67 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
68 * we define it as 0 if it's not defined, so it does nothing.
75 read_infile(char *fname
)
77 register int i
, fd
, cc
;
81 fd
= open(fname
, O_RDONLY
|O_BINARY
);
83 error("can't open %s: %s", fname
, pcap_strerror(errno
));
85 if (fstat(fd
, &buf
) < 0)
86 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
88 cp
= malloc((u_int
)buf
.st_size
+ 1);
90 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
91 fname
, pcap_strerror(errno
));
92 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
94 error("read %s: %s", fname
, pcap_strerror(errno
));
95 if (cc
!= buf
.st_size
)
96 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
99 /* replace "# comment" with spaces */
100 for (i
= 0; i
< cc
; i
++) {
102 while (i
< cc
&& cp
[i
] != '\n')
111 error(const char *fmt
, ...)
115 (void)fprintf(stderr
, "%s: ", program_name
);
117 (void)vfprintf(stderr
, fmt
, ap
);
122 (void)fputc('\n', stderr
);
130 warn(const char *fmt
, ...)
134 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
136 (void)vfprintf(stderr
, fmt
, ap
);
141 (void)fputc('\n', stderr
);
146 * Copy arg vector into a new buffer, concatenating arguments with spaces.
149 copy_argv(register char **argv
)
152 register u_int len
= 0;
161 len
+= strlen(*p
++) + 1;
163 buf
= (char *)malloc(len
);
165 error("copy_argv: malloc");
169 while ((src
= *p
++) != NULL
) {
170 while ((*dst
++ = *src
++) != '\0')
180 main(int argc
, char **argv
)
192 bpf_u_int32 netmask
= PCAP_NETMASK_UNKNOWN
;
195 struct bpf_program fcode
;
198 if(wsockinit() != 0) return 1;
204 /* if optimizer debugging is enabled, output DOT graph
205 * `dflag=4' is equivalent to -dddd to follow -d/-dd/-ddd
206 * convention in tcpdump command line
214 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
215 program_name
= cp
+ 1;
217 program_name
= argv
[0];
220 while ((op
= getopt(argc
, argv
, "dF:m:Os:")) != -1) {
238 addr
= inet_addr(optarg
);
239 if (addr
== INADDR_NONE
)
240 error("invalid netmask %s", optarg
);
248 snaplen
= strtol(optarg
, &end
, 0);
249 if (optarg
== end
|| *end
!= '\0'
250 || snaplen
< 0 || snaplen
> 65535)
251 error("invalid snaplen %s", optarg
);
252 else if (snaplen
== 0)
263 if (optind
>= argc
) {
268 dlt
= pcap_datalink_name_to_val(argv
[optind
]);
270 dlt
= (int)strtol(argv
[optind
], &p
, 10);
271 if (p
== argv
[optind
] || *p
!= '\0')
272 error("invalid data link type %s", argv
[optind
]);
276 cmdbuf
= read_infile(infile
);
278 cmdbuf
= copy_argv(&argv
[optind
+1]);
280 pd
= pcap_open_dead(dlt
, snaplen
);
282 error("Can't open fake pcap_t");
284 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
285 error("%s", pcap_geterr(pd
));
287 if (!bpf_validate(fcode
.bf_insns
, fcode
.bf_len
))
288 warn("Filter doesn't pass validation");
291 // replace line feed with space
292 for (cp
= cmdbuf
; *cp
!= '\0'; ++cp
) {
293 if (*cp
== '\r' || *cp
== '\n') {
297 // only show machine code if BDEBUG defined, since dflag > 3
298 printf("machine codes for filter: %s\n", cmdbuf
);
301 bpf_dump(&fcode
, dflag
);
309 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
311 (void)fprintf(stderr
,
312 "Usage: %s [-dO] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n",