]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/filtertest.c
7e2d6d6e186d4989f4c694c5a0f7bf5d4f99eb0c
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.
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";
51 #include <sys/socket.h>
52 #include <arpa/inet.h>
54 #include <sys/types.h>
57 #include "pcap/funcattrs.h"
61 * We have pcap_set_optimizer_debug() and pcap_set_print_dot_graph() in
62 * libpcap; declare them (they're not declared by any libpcap header,
63 * because they're special hacks, only available if libpcap was configured
64 * to include them, and only intended for use by libpcap developers trying
65 * to debug the optimizer for filter expressions).
67 PCAP_API
void pcap_set_optimizer_debug(int);
68 PCAP_API
void pcap_set_print_dot_graph(int);
71 static char *program_name
;
74 static void PCAP_NORETURN
usage(void);
75 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
76 static void warn(const char *, ...) PCAP_PRINTFLIKE(1, 2);
79 * On Windows, we need to open the file in binary mode, so that
80 * we get all the bytes specified by the size we get from "fstat()".
81 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
82 * we define it as 0 if it's not defined, so it does nothing.
89 read_infile(char *fname
)
91 register int i
, fd
, cc
;
95 fd
= open(fname
, O_RDONLY
|O_BINARY
);
97 error("can't open %s: %s", fname
, pcap_strerror(errno
));
99 if (fstat(fd
, &buf
) < 0)
100 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
102 cp
= malloc((u_int
)buf
.st_size
+ 1);
104 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
105 fname
, pcap_strerror(errno
));
106 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
108 error("read %s: %s", fname
, pcap_strerror(errno
));
109 if (cc
!= buf
.st_size
)
110 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
113 /* replace "# comment" with spaces */
114 for (i
= 0; i
< cc
; i
++) {
116 while (i
< cc
&& cp
[i
] != '\n')
125 error(const char *fmt
, ...)
129 (void)fprintf(stderr
, "%s: ", program_name
);
131 (void)vfprintf(stderr
, fmt
, ap
);
136 (void)fputc('\n', stderr
);
144 warn(const char *fmt
, ...)
148 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
150 (void)vfprintf(stderr
, fmt
, ap
);
155 (void)fputc('\n', stderr
);
160 * Copy arg vector into a new buffer, concatenating arguments with spaces.
163 copy_argv(register char **argv
)
166 register u_int len
= 0;
175 len
+= strlen(*p
++) + 1;
177 buf
= (char *)malloc(len
);
179 error("copy_argv: malloc");
183 while ((src
= *p
++) != NULL
) {
184 while ((*dst
++ = *src
++) != '\0')
194 main(int argc
, char **argv
)
206 bpf_u_int32 netmask
= PCAP_NETMASK_UNKNOWN
;
209 struct bpf_program fcode
;
212 if (pcap_wsockinit() != 0)
223 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
224 program_name
= cp
+ 1;
226 program_name
= argv
[0];
229 while ((op
= getopt(argc
, argv
, "dF:gm:Os:")) != -1) {
240 error("libpcap and filtertest not built with optimizer debugging enabled");
255 switch (inet_pton(AF_INET
, optarg
, &addr
)) {
258 error("invalid netmask %s", optarg
);
262 error("invalid netmask %s: %s", optarg
,
263 pcap_strerror(errno
));
276 snaplen
= strtol(optarg
, &end
, 0);
277 if (optarg
== end
|| *end
!= '\0'
278 || snaplen
< 0 || snaplen
> 65535)
279 error("invalid snaplen %s", optarg
);
280 else if (snaplen
== 0)
291 if (optind
>= argc
) {
296 dlt
= pcap_datalink_name_to_val(argv
[optind
]);
298 dlt
= (int)strtol(argv
[optind
], &p
, 10);
299 if (p
== argv
[optind
] || *p
!= '\0')
300 error("invalid data link type %s", argv
[optind
]);
304 cmdbuf
= read_infile(infile
);
306 cmdbuf
= copy_argv(&argv
[optind
+1]);
309 pcap_set_optimizer_debug(dflag
);
310 pcap_set_print_dot_graph(gflag
);
313 pd
= pcap_open_dead(dlt
, snaplen
);
315 error("Can't open fake pcap_t");
317 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
318 error("%s", pcap_geterr(pd
));
321 if (!bpf_validate(fcode
.bf_insns
, fcode
.bf_len
))
322 warn("Filter doesn't pass validation");
325 if (cmdbuf
!= NULL
) {
326 // replace line feed with space
327 for (cp
= cmdbuf
; *cp
!= '\0'; ++cp
) {
328 if (*cp
== '\r' || *cp
== '\n') {
332 // only show machine code if BDEBUG defined, since dflag > 3
333 printf("machine codes for filter: %s\n", cmdbuf
);
335 printf("machine codes for empty filter:\n");
338 bpf_dump(&fcode
, dflag
);
341 pcap_freecode (&fcode
);
349 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
351 (void)fprintf(stderr
,
353 "Usage: %s [-dgO] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n",
355 "Usage: %s [-dO] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n",