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"
59 #define MAXIMUM_SNAPLEN 262144
63 * We have pcap_set_optimizer_debug() and pcap_set_print_dot_graph() in
64 * libpcap; declare them (they're not declared by any libpcap header,
65 * because they're special hacks, only available if libpcap was configured
66 * to include them, and only intended for use by libpcap developers trying
67 * to debug the optimizer for filter expressions).
69 PCAP_API
void pcap_set_optimizer_debug(int);
70 PCAP_API
void pcap_set_print_dot_graph(int);
74 #include <linux/filter.h>
75 #if defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT)
77 * pcap-int.h is a private header and should not be included by programs that
78 * use libpcap. This test program uses a special hack because it is the
79 * simplest way to test internal code paths that otherwise would require
80 * elevated privileges. Do not do this in normal code.
84 #endif // defined(SO_BPF_EXTENSIONS) && defined(SKF_AD_VLAN_TAG_PRESENT)
87 static char *program_name
;
90 static void PCAP_NORETURN
usage(FILE *);
91 static void PCAP_NORETURN
error(const int, const char *, ...) PCAP_PRINTFLIKE(2, 3);
92 static void warn(const char *, ...) PCAP_PRINTFLIKE(1, 2);
95 * On Windows, we need to open the file in binary mode, so that
96 * we get all the bytes specified by the size we get from "fstat()".
97 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
98 * we define it as 0 if it's not defined, so it does nothing.
105 read_infile(char *fname
)
107 register int i
, fd
, cc
;
111 fd
= open(fname
, O_RDONLY
|O_BINARY
);
113 error(EX_NOINPUT
, "can't open %s: %s", fname
, pcap_strerror(errno
));
115 if (fstat(fd
, &buf
) < 0)
116 error(EX_NOINPUT
, "can't stat %s: %s", fname
, pcap_strerror(errno
));
119 * _read(), on Windows, has an unsigned int byte count and an
120 * int return value, so we can't handle a file bigger than
121 * INT_MAX - 1 bytes (and have no reason to do so; a filter *that*
122 * big will take forever to compile). (The -1 is for the '\0' at
123 * the end of the string.)
125 if (buf
.st_size
> INT_MAX
- 1)
126 error(EX_DATAERR
, "%s is larger than %d bytes; that's too large", fname
,
128 cp
= malloc((u_int
)buf
.st_size
+ 1);
130 error(EX_OSERR
, "malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
131 fname
, pcap_strerror(errno
));
132 cc
= (int)read(fd
, cp
, (u_int
)buf
.st_size
);
134 error(EX_IOERR
, "read %s: %s", fname
, pcap_strerror(errno
));
135 if (cc
!= buf
.st_size
)
136 error(EX_IOERR
, "short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
139 /* replace "# comment" with spaces */
140 for (i
= 0; i
< cc
; i
++) {
142 while (i
< cc
&& cp
[i
] != '\n')
151 error(const int status
, const char *fmt
, ...)
155 (void)fprintf(stderr
, "%s: ", program_name
);
157 (void)vfprintf(stderr
, fmt
, ap
);
162 (void)fputc('\n', stderr
);
170 warn(const char *fmt
, ...)
174 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
176 (void)vfprintf(stderr
, fmt
, ap
);
181 (void)fputc('\n', stderr
);
186 * Copy arg vector into a new buffer, concatenating arguments with spaces.
189 copy_argv(register char **argv
)
192 register size_t len
= 0;
201 len
+= strlen(*p
++) + 1;
203 buf
= (char *)malloc(len
);
205 error(EX_OSERR
, "%s: malloc", __func__
);
209 while ((src
= *p
++) != NULL
) {
210 while ((*dst
++ = *src
++) != '\0')
220 main(int argc
, char **argv
)
233 int snaplen
= MAXIMUM_SNAPLEN
;
236 bpf_u_int32 netmask
= PCAP_NETMASK_UNKNOWN
;
239 struct bpf_program fcode
;
243 if (0 != WSAStartup(MAKEWORD(2, 2), &wsaData
))
247 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
248 program_name
= cp
+ 1;
250 program_name
= argv
[0];
253 while ((op
= getopt(argc
, argv
, "hdF:gm:Os:l")) != -1) {
269 error(EX_USAGE
, "libpcap and filtertest not built with optimizer debugging enabled");
283 switch (inet_pton(AF_INET
, optarg
, &addr
)) {
286 error(EX_DATAERR
, "invalid netmask %s", optarg
);
289 error(EX_DATAERR
, "invalid netmask %s: %s", optarg
,
290 pcap_strerror(errno
));
303 long_snaplen
= strtol(optarg
, &end
, 0);
304 if (optarg
== end
|| *end
!= '\0'
306 || long_snaplen
> MAXIMUM_SNAPLEN
)
307 error(EX_DATAERR
, "invalid snaplen %s", optarg
);
310 snaplen
= MAXIMUM_SNAPLEN
;
312 snaplen
= (int)long_snaplen
;
319 // Enable Linux BPF extensions.
323 error(EX_USAGE
, "libpcap and filtertest built without Linux BPF extensions");
332 if (optind
>= argc
) {
337 dlt
= pcap_datalink_name_to_val(argv
[optind
]);
339 dlt
= (int)strtol(argv
[optind
], &p
, 10);
340 if (p
== argv
[optind
] || *p
!= '\0')
341 error(EX_DATAERR
, "invalid data link type %s", argv
[optind
]);
345 cmdbuf
= read_infile(infile
);
347 cmdbuf
= copy_argv(&argv
[optind
+1]);
350 pcap_set_optimizer_debug(dflag
);
351 pcap_set_print_dot_graph(gflag
);
354 pd
= pcap_open_dead(dlt
, snaplen
);
356 error(EX_SOFTWARE
, "Can't open fake pcap_t");
360 pd
->bpf_codegen_flags
|= BPF_SPECIAL_VLAN_HANDLING
;
361 pd
->bpf_codegen_flags
|= BPF_SPECIAL_BASIC_HANDLING
;
365 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
366 error(EX_DATAERR
, "%s", pcap_geterr(pd
));
368 if (!bpf_validate(fcode
.bf_insns
, fcode
.bf_len
))
369 warn("Filter doesn't pass validation");
372 if (cmdbuf
!= NULL
) {
373 // replace line feed with space
374 for (cp
= cmdbuf
; *cp
!= '\0'; ++cp
) {
375 if (*cp
== '\r' || *cp
== '\n') {
379 // only show machine code if BDEBUG defined, since dflag > 3
380 printf("machine codes for filter: %s\n", cmdbuf
);
382 printf("machine codes for empty filter:\n");
385 bpf_dump(&fcode
, dflag
);
387 pcap_freecode (&fcode
);
398 (void)fprintf(f
, "%s, with %s\n", program_name
,
409 "] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expr ]\n",
411 (void)fprintf(f
, " (print the filter program bytecode)\n");
412 (void)fprintf(f
, " or: %s -h\n", program_name
);
413 (void)fprintf(f
, " (print the detailed help screen)\n");
415 (void)fprintf(f
, "\nOptions specific to %s:\n", program_name
);
416 (void)fprintf(f
, " <dlt> a valid DLT name, e.g. 'EN10MB'\n");
417 (void)fprintf(f
, " <expr> a valid filter expression, e.g. 'tcp port 80'\n");
419 (void)fprintf(f
, " -l allow the use of Linux BPF extensions\n");
422 (void)fprintf(f
, " -g print Graphviz dot graphs for the optimizer steps\n");
424 (void)fprintf(f
, " -m <netmask> use this netmask for pcap_compile(), e.g. 255.255.255.0\n");
425 (void)fprintf(f
, "\n");
426 (void)fprintf(f
, "Options common with tcpdump:\n");
427 (void)fprintf(f
, " -d change output format (accumulates, one -d is implicit)\n");
428 (void)fprintf(f
, " -O do not optimize the filter program\n");
429 (void)fprintf(f
, " -F <file> read the filter expression from the specified file\n");
430 (void)fprintf(f
, " -s <snaplen> set the snapshot length\n");
431 (void)fprintf(f
, "\nIf no filter expression is specified, it defaults to an empty string, which\n");
432 (void)fprintf(f
, "accepts all packets. If the -F option is in use, it replaces any filter\n");
433 (void)fprintf(f
, "expression specified as a command-line argument.\n");
435 exit(f
== stdout
? EX_OK
: EX_USAGE
);