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";
50 #include <sys/socket.h>
51 #include <arpa/inet.h>
53 #include <sys/types.h>
56 #include "pcap/funcattrs.h"
58 #define MAXIMUM_SNAPLEN 262144
62 * We have pcap_set_optimizer_debug() and pcap_set_print_dot_graph() in
63 * libpcap; declare them (they're not declared by any libpcap header,
64 * because they're special hacks, only available if libpcap was configured
65 * to include them, and only intended for use by libpcap developers trying
66 * to debug the optimizer for filter expressions).
68 PCAP_API
void pcap_set_optimizer_debug(int);
69 PCAP_API
void pcap_set_print_dot_graph(int);
73 #include <linux/filter.h>
74 #if defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT)
76 * pcap-int.h is a private header and should not be included by programs that
77 * use libpcap. This test program uses a special hack because it is the
78 * simplest way to test internal code paths that otherwise would require
79 * elevated privileges. Do not do this in normal code.
83 #endif // defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT)
86 static char *program_name
;
89 static void PCAP_NORETURN
usage(void);
90 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
91 static void warn(const char *, ...) PCAP_PRINTFLIKE(1, 2);
94 * On Windows, we need to open the file in binary mode, so that
95 * we get all the bytes specified by the size we get from "fstat()".
96 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
97 * we define it as 0 if it's not defined, so it does nothing.
104 read_infile(char *fname
)
106 register int i
, fd
, cc
;
110 fd
= open(fname
, O_RDONLY
|O_BINARY
);
112 error("can't open %s: %s", fname
, pcap_strerror(errno
));
114 if (fstat(fd
, &buf
) < 0)
115 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
118 * _read(), on Windows, has an unsigned int byte count and an
119 * int return value, so we can't handle a file bigger than
120 * INT_MAX - 1 bytes (and have no reason to do so; a filter *that*
121 * big will take forever to compile). (The -1 is for the '\0' at
122 * the end of the string.)
124 if (buf
.st_size
> INT_MAX
- 1)
125 error("%s is larger than %d bytes; that's too large", fname
,
127 cp
= malloc((u_int
)buf
.st_size
+ 1);
129 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
130 fname
, pcap_strerror(errno
));
131 cc
= (int)read(fd
, cp
, (u_int
)buf
.st_size
);
133 error("read %s: %s", fname
, pcap_strerror(errno
));
134 if (cc
!= buf
.st_size
)
135 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
138 /* replace "# comment" with spaces */
139 for (i
= 0; i
< cc
; i
++) {
141 while (i
< cc
&& cp
[i
] != '\n')
150 error(const char *fmt
, ...)
154 (void)fprintf(stderr
, "%s: ", program_name
);
156 (void)vfprintf(stderr
, fmt
, ap
);
161 (void)fputc('\n', stderr
);
169 warn(const char *fmt
, ...)
173 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
175 (void)vfprintf(stderr
, fmt
, ap
);
180 (void)fputc('\n', stderr
);
185 * Copy arg vector into a new buffer, concatenating arguments with spaces.
188 copy_argv(register char **argv
)
191 register size_t len
= 0;
200 len
+= strlen(*p
++) + 1;
202 buf
= (char *)malloc(len
);
204 error("copy_argv: malloc");
208 while ((src
= *p
++) != NULL
) {
209 while ((*dst
++ = *src
++) != '\0')
219 main(int argc
, char **argv
)
235 bpf_u_int32 netmask
= PCAP_NETMASK_UNKNOWN
;
238 struct bpf_program fcode
;
242 if (0 != WSAStartup(MAKEWORD(2, 2), &wsaData
))
253 snaplen
= MAXIMUM_SNAPLEN
;
255 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
256 program_name
= cp
+ 1;
258 program_name
= argv
[0];
261 while ((op
= getopt(argc
, argv
, "dF:gm:Os:l")) != -1) {
273 error("libpcap and filtertest not built with optimizer debugging enabled");
287 switch (inet_pton(AF_INET
, optarg
, &addr
)) {
290 error("invalid netmask %s", optarg
);
293 error("invalid netmask %s: %s", optarg
,
294 pcap_strerror(errno
));
307 long_snaplen
= strtol(optarg
, &end
, 0);
308 if (optarg
== end
|| *end
!= '\0'
310 || long_snaplen
> MAXIMUM_SNAPLEN
)
311 error("invalid snaplen %s", optarg
);
314 snaplen
= MAXIMUM_SNAPLEN
;
316 snaplen
= (int)long_snaplen
;
323 // Enable Linux BPF extensions.
327 error("libpcap and filtertest built without Linux BPF extensions");
336 if (optind
>= argc
) {
341 dlt
= pcap_datalink_name_to_val(argv
[optind
]);
343 dlt
= (int)strtol(argv
[optind
], &p
, 10);
344 if (p
== argv
[optind
] || *p
!= '\0')
345 error("invalid data link type %s", argv
[optind
]);
349 cmdbuf
= read_infile(infile
);
351 cmdbuf
= copy_argv(&argv
[optind
+1]);
354 pcap_set_optimizer_debug(dflag
);
355 pcap_set_print_dot_graph(gflag
);
358 pd
= pcap_open_dead(dlt
, snaplen
);
360 error("Can't open fake pcap_t");
364 pd
->bpf_codegen_flags
|= BPF_SPECIAL_VLAN_HANDLING
;
368 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
369 error("%s", pcap_geterr(pd
));
371 if (!bpf_validate(fcode
.bf_insns
, fcode
.bf_len
))
372 warn("Filter doesn't pass validation");
375 if (cmdbuf
!= NULL
) {
376 // replace line feed with space
377 for (cp
= cmdbuf
; *cp
!= '\0'; ++cp
) {
378 if (*cp
== '\r' || *cp
== '\n') {
382 // only show machine code if BDEBUG defined, since dflag > 3
383 printf("machine codes for filter: %s\n", cmdbuf
);
385 printf("machine codes for empty filter:\n");
388 bpf_dump(&fcode
, dflag
);
390 pcap_freecode (&fcode
);
401 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
403 (void)fprintf(stderr
,
412 "] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expression ]\n",