]>
The Tcpdump Group git mirrors - libpcap/blob - filtertest.c
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 <arpa/inet.h>
43 #include <sys/types.h>
46 #ifndef HAVE___ATTRIBUTE__
47 #define __attribute__(x)
50 static char *program_name
;
53 static void usage(void) __attribute__((noreturn
));
54 static void error(const char *, ...)
55 __attribute__((noreturn
, format (printf
, 1, 2)));
62 * On Windows, we need to open the file in binary mode, so that
63 * we get all the bytes specified by the size we get from "fstat()".
64 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
65 * we define it as 0 if it's not defined, so it does nothing.
72 read_infile(char *fname
)
74 register int i
, fd
, cc
;
78 fd
= open(fname
, O_RDONLY
|O_BINARY
);
80 error("can't open %s: %s", fname
, pcap_strerror(errno
));
82 if (fstat(fd
, &buf
) < 0)
83 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
85 cp
= malloc((u_int
)buf
.st_size
+ 1);
87 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
88 fname
, pcap_strerror(errno
));
89 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
91 error("read %s: %s", fname
, pcap_strerror(errno
));
92 if (cc
!= buf
.st_size
)
93 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
96 /* replace "# comment" with spaces */
97 for (i
= 0; i
< cc
; i
++) {
99 while (i
< cc
&& cp
[i
] != '\n')
108 error(const char *fmt
, ...)
112 (void)fprintf(stderr
, "%s: ", program_name
);
114 (void)vfprintf(stderr
, fmt
, ap
);
119 (void)fputc('\n', stderr
);
126 * Copy arg vector into a new buffer, concatenating arguments with spaces.
129 copy_argv(register char **argv
)
132 register u_int len
= 0;
141 len
+= strlen(*p
++) + 1;
143 buf
= (char *)malloc(len
);
145 error("copy_argv: malloc");
149 while ((src
= *p
++) != NULL
) {
150 while ((*dst
++ = *src
++) != '\0')
160 main(int argc
, char **argv
)
169 bpf_u_int32 netmask
= PCAP_NETMASK_UNKNOWN
;
172 struct bpf_program fcode
;
175 if(wsockinit() != 0) return 1;
183 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
184 program_name
= cp
+ 1;
186 program_name
= argv
[0];
189 while ((op
= getopt(argc
, argv
, "dF:m:Os:")) != -1) {
207 addr
= inet_addr(optarg
);
208 if (addr
== INADDR_NONE
)
209 error("invalid netmask %s", optarg
);
217 snaplen
= strtol(optarg
, &end
, 0);
218 if (optarg
== end
|| *end
!= '\0'
219 || snaplen
< 0 || snaplen
> 65535)
220 error("invalid snaplen %s", optarg
);
221 else if (snaplen
== 0)
232 if (optind
>= argc
) {
237 dlt
= pcap_datalink_name_to_val(argv
[optind
]);
239 error("invalid data link type %s", argv
[optind
]);
242 cmdbuf
= read_infile(infile
);
244 cmdbuf
= copy_argv(&argv
[optind
+1]);
246 pd
= pcap_open_dead(dlt
, snaplen
);
248 error("Can't open fake pcap_t");
250 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
251 error("%s", pcap_geterr(pd
));
252 bpf_dump(&fcode
, dflag
);
260 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
262 (void)fprintf(stderr
,
263 "Usage: %s [-dO] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n",