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
60 #define MAX_STDIN (64 * 1024)
64 * We have pcap_set_optimizer_debug() and pcap_set_print_dot_graph() in
65 * libpcap; declare them (they're not declared by any libpcap header,
66 * because they're special hacks, only available if libpcap was configured
67 * to include them, and only intended for use by libpcap developers trying
68 * to debug the optimizer for filter expressions).
70 PCAP_API
void pcap_set_optimizer_debug(int);
71 PCAP_API
void pcap_set_print_dot_graph(int);
75 #include <linux/filter.h> // 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.
85 static char *program_name
;
88 static void PCAP_NORETURN
usage(FILE *);
89 static void PCAP_NORETURN
error(const int, const char *, ...) PCAP_PRINTFLIKE(2, 3);
90 static void warn(const char *, ...) PCAP_PRINTFLIKE(1, 2);
93 * On Windows, we need to open the file in binary mode, so that
94 * we get all the bytes specified by the size we get from "fstat()".
95 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
96 * we define it as 0 if it's not defined, so it does nothing.
102 // Replace "# comment" with spaces.
104 blank_comments(char *cp
, const size_t size
)
106 for (size_t i
= 0; i
< size
; i
++) {
108 while (i
< size
&& cp
[i
] != '\n')
114 read_infile(char *fname
)
120 fd
= open(fname
, O_RDONLY
|O_BINARY
);
122 error(EX_NOINPUT
, "can't open %s: %s", fname
, pcap_strerror(errno
));
124 if (fstat(fd
, &buf
) < 0)
125 error(EX_NOINPUT
, "can't stat %s: %s", fname
, pcap_strerror(errno
));
128 * _read(), on Windows, has an unsigned int byte count and an
129 * int return value, so we can't handle a file bigger than
130 * INT_MAX - 1 bytes (and have no reason to do so; a filter *that*
131 * big will take forever to compile). (The -1 is for the '\0' at
132 * the end of the string.)
134 if (buf
.st_size
> INT_MAX
- 1)
135 error(EX_DATAERR
, "%s is larger than %d bytes; that's too large", fname
,
137 cp
= malloc((u_int
)buf
.st_size
+ 1);
139 error(EX_OSERR
, "malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
140 fname
, pcap_strerror(errno
));
141 cc
= (int)read(fd
, cp
, (u_int
)buf
.st_size
);
143 error(EX_IOERR
, "read %s: %s", fname
, pcap_strerror(errno
));
144 if (cc
!= buf
.st_size
)
145 error(EX_IOERR
, "short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
148 blank_comments(cp
, (size_t)cc
);
153 // Copy stdin into a size-limited buffer.
157 char *buf
= calloc(1, MAX_STDIN
+ 1);
159 error(EX_OSERR
, "%s: calloc", __func__
);
160 size_t readsize
= fread(buf
, 1, MAX_STDIN
, stdin
);
163 error(EX_DATAERR
, "received more than %u bytes on stdin", MAX_STDIN
);
167 error(EX_IOERR
, "failed reading from stdin after %zd bytes", readsize
);
170 // No error, all data is within the buffer and NUL-terminated.
171 blank_comments(buf
, readsize
);
177 error(const int status
, const char *fmt
, ...)
181 (void)fprintf(stderr
, "%s: ", program_name
);
183 (void)vfprintf(stderr
, fmt
, ap
);
188 (void)fputc('\n', stderr
);
196 warn(const char *fmt
, ...)
200 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
202 (void)vfprintf(stderr
, fmt
, ap
);
207 (void)fputc('\n', stderr
);
212 * Copy arg vector into a new buffer, concatenating arguments with spaces.
215 copy_argv(register char **argv
)
218 register size_t len
= 0;
227 len
+= strlen(*p
++) + 1;
229 buf
= (char *)malloc(len
);
231 error(EX_OSERR
, "%s: malloc", __func__
);
235 while ((src
= *p
++) != NULL
) {
236 while ((*dst
++ = *src
++) != '\0')
246 main(int argc
, char **argv
)
255 char *insavefile
= NULL
;
260 int snaplen
= MAXIMUM_SNAPLEN
;
262 bpf_u_int32 netmask
= PCAP_NETMASK_UNKNOWN
;
265 struct bpf_program fcode
;
269 if (0 != WSAStartup(MAKEWORD(2, 2), &wsaData
))
273 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
274 program_name
= cp
+ 1;
276 program_name
= argv
[0];
279 while ((op
= getopt(argc
, argv
, "hdF:gm:Os:lr:")) != -1) {
295 error(EX_USAGE
, "libpcap and filtertest not built with optimizer debugging enabled");
313 switch (inet_pton(AF_INET
, optarg
, &addr
)) {
316 error(EX_DATAERR
, "invalid netmask %s", optarg
);
319 error(EX_DATAERR
, "invalid netmask %s: %s", optarg
,
320 pcap_strerror(errno
));
323 // inet_pton(): network byte order, pcap_compile(): host byte order.
324 netmask
= ntohl(addr
);
334 long_snaplen
= strtol(optarg
, &end
, 0);
335 if (optarg
== end
|| *end
!= '\0'
337 || long_snaplen
> MAXIMUM_SNAPLEN
)
338 error(EX_DATAERR
, "invalid snaplen %s", optarg
);
341 snaplen
= MAXIMUM_SNAPLEN
;
343 snaplen
= (int)long_snaplen
;
350 // Enable Linux BPF extensions.
354 error(EX_USAGE
, "libpcap and filtertest built without Linux BPF extensions");
365 warn("-d is a no-op with -r");
368 warn("-g is a no-op with -r");
372 warn("-l is a no-op with -r");
375 char errbuf
[PCAP_ERRBUF_SIZE
];
376 if (NULL
== (pd
= pcap_open_offline(insavefile
, errbuf
)))
377 error(EX_NOINPUT
, "Failed opening: %s", errbuf
);
379 // Must have at least one command-line argument for the DLT.
380 if (optind
>= argc
) {
384 int dlt
= pcap_datalink_name_to_val(argv
[optind
]);
386 dlt
= (int)strtol(argv
[optind
], &p
, 10);
387 if (p
== argv
[optind
] || *p
!= '\0')
388 error(EX_DATAERR
, "invalid data link type %s", argv
[optind
]);
392 pd
= pcap_open_dead(dlt
, snaplen
);
394 error(EX_SOFTWARE
, "Can't open fake pcap_t");
397 #ifdef SKF_AD_VLAN_TAG_PRESENT
399 * Generally speaking, the fact the header defines the
400 * symbol does not necessarily mean the running kernel
401 * supports what is known as [vlanp] and everything
402 * before it, but in this use case the filter program
403 * is not meant for the kernel.
405 pd
->bpf_codegen_flags
|= BPF_SPECIAL_VLAN_HANDLING
;
406 #endif // SKF_AD_VLAN_TAG_PRESENT
407 pd
->bpf_codegen_flags
|= BPF_SPECIAL_BASIC_HANDLING
;
411 pcap_set_optimizer_debug(dflag
);
412 pcap_set_print_dot_graph(gflag
);
417 cmdbuf
= strcmp(infile
, "-") ? read_infile(infile
) : read_stdin();
419 cmdbuf
= copy_argv(&argv
[optind
]);
421 if (pcap_compile(pd
, &fcode
, cmdbuf
, Oflag
, netmask
) < 0)
422 error(EX_DATAERR
, "%s", pcap_geterr(pd
));
424 if (!bpf_validate(fcode
.bf_insns
, fcode
.bf_len
))
425 warn("Filter doesn't pass validation");
429 // replace line feed with space
430 for (cp
= cmdbuf
; *cp
!= '\0'; ++cp
) {
431 if (*cp
== '\r' || *cp
== '\n') {
435 // only show machine code if BDEBUG defined, since dflag > 3
436 printf("machine codes for filter: %s\n", cmdbuf
);
438 bpf_dump(&fcode
, dflag
);
440 struct pcap_pkthdr
*h
;
443 while (PCAP_ERROR_BREAK
!= (ret
= pcap_next_ex(pd
, &h
, &d
))) {
444 if (ret
== PCAP_ERROR
)
445 error(EX_IOERR
, "pcap_next_ex() failed: %s", pcap_geterr(pd
));
447 printf("%d\n", pcap_offline_filter(&fcode
, h
, d
));
449 error(EX_IOERR
, "pcap_next_ex() failed: %d", ret
);
453 pcap_freecode (&fcode
);
464 (void)fprintf(f
, "%s, with %s\n", program_name
,
475 "] [ -F file ] [ -m netmask] [ -s snaplen ] dlt [ expr ]\n",
477 (void)fprintf(f
, " (print the filter program bytecode)\n");
479 " or: %s [-O] [ -F file ] [ -m netmask] -r file [ expression ]\n",
481 (void)fprintf(f
, " (print the filter program result for each packet)\n");
482 (void)fprintf(f
, " or: %s -h\n", program_name
);
483 (void)fprintf(f
, " (print the detailed help screen)\n");
485 (void)fprintf(f
, "\nOptions specific to %s:\n", program_name
);
486 (void)fprintf(f
, " <dlt> a valid DLT name, e.g. 'EN10MB'\n");
487 (void)fprintf(f
, " <expr> a valid filter expression, e.g. 'tcp port 80'\n");
489 (void)fprintf(f
, " -l allow the use of Linux BPF extensions\n");
492 (void)fprintf(f
, " -g print Graphviz dot graphs for the optimizer steps\n");
494 (void)fprintf(f
, " -m <netmask> use this netmask for pcap_compile(), e.g. 255.255.255.0\n");
495 (void)fprintf(f
, "\n");
496 (void)fprintf(f
, "Options common with tcpdump:\n");
497 (void)fprintf(f
, " -d change output format (accumulates, one -d is implicit)\n");
498 (void)fprintf(f
, " -O do not optimize the filter program\n");
499 (void)fprintf(f
, " -F <file> read the filter expression from the specified file\n");
500 (void)fprintf(f
, " (\"-\" means stdin and allows at most %u characters)\n", MAX_STDIN
);
501 (void)fprintf(f
, " -s <snaplen> set the snapshot length\n");
502 (void)fprintf(f
, " -r <file> read the packets from this savefile\n");
503 (void)fprintf(f
, "\nIf no filter expression is specified, it defaults to an empty string, which\n");
504 (void)fprintf(f
, "accepts all packets. If the -F option is in use, it replaces any filter\n");
505 (void)fprintf(f
, "expression specified as a command-line argument.\n");
507 exit(f
== stdout
? EX_OK
: EX_USAGE
);