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";
26 static const char rcsid[] _U_ =
27 "@(#) $Header: /tcpdump/master/libpcap/filtertest.c,v 1.2 2005/08/08 17:50:13 guy Exp $ (LBL)";
42 #include <sys/types.h>
45 #ifndef HAVE___ATTRIBUTE__
46 #define __attribute__(x)
49 static char *program_name;
52 static void usage(void) __attribute__((noreturn));
53 static void error(const char *, ...)
54 __attribute__((noreturn, format (printf, 1, 2)));
61 * On Windows, we need to open the file in binary mode, so that
62 * we get all the bytes specified by the size we get from "fstat()".
63 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
64 * we define it as 0 if it's not defined, so it does nothing.
71 read_infile(char *fname)
73 register int i, fd, cc;
77 fd = open(fname, O_RDONLY|O_BINARY);
79 error("can't open %s: %s", fname, pcap_strerror(errno));
81 if (fstat(fd, &buf) < 0)
82 error("can't stat %s: %s", fname, pcap_strerror(errno));
84 cp = malloc((u_int)buf.st_size + 1);
86 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
87 fname, pcap_strerror(errno));
88 cc = read(fd, cp, (u_int)buf.st_size);
90 error("read %s: %s", fname, pcap_strerror(errno));
91 if (cc != buf.st_size)
92 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
95 /* replace "# comment" with spaces */
96 for (i = 0; i < cc; i++) {
98 while (i < cc && cp[i] != '\n')
107 error(const char *fmt, ...)
111 (void)fprintf(stderr, "%s: ", program_name);
113 (void)vfprintf(stderr, fmt, ap);
118 (void)fputc('\n', stderr);
125 * Copy arg vector into a new buffer, concatenating arguments with spaces.
128 copy_argv(register char **argv)
131 register u_int len = 0;
140 len += strlen(*p++) + 1;
142 buf = (char *)malloc(len);
144 error("copy_argv: malloc");
148 while ((src = *p++) != NULL) {
149 while ((*dst++ = *src++) != '\0')
159 main(int argc, char **argv)
170 struct bpf_program fcode;
173 if(wsockinit() != 0) return 1;
181 if ((cp = strrchr(argv[0], '/')) != NULL)
182 program_name = cp + 1;
184 program_name = argv[0];
187 while ((op = getopt(argc, argv, "dF:Os:")) != -1) {
205 snaplen = strtol(optarg, &end, 0);
206 if (optarg == end || *end != '\0'
207 || snaplen < 0 || snaplen > 65535)
208 error("invalid snaplen %s", optarg);
209 else if (snaplen == 0)
220 if (optind >= argc) {
225 dlt = pcap_datalink_name_to_val(argv[optind]);
227 error("invalid data link type %s", argv[optind]);
230 cmdbuf = read_infile(infile);
232 cmdbuf = copy_argv(&argv[optind+1]);
234 pd = pcap_open_dead(dlt, snaplen);
236 error("Can't open fake pcap_t");
238 if (pcap_compile(pd, &fcode, cmdbuf, Oflag, 0) < 0)
239 error("%s", pcap_geterr(pd));
240 bpf_dump(&fcode, dflag);
248 (void)fprintf(stderr, "%s, with %s\n", program_name,
250 (void)fprintf(stderr,
251 "Usage: %s [-dO] [ -F file ] [ -s snaplen ] dlt [ expression ]\n",