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)";
41 #include <arpa/inet.h>
42 #include <sys/types.h>
45 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
46 /* BSD-flavored OS - use BPF */
49 /* Linux - use socket filters */
50 #define USE_SOCKET_FILTERS
52 #error "Unknown platform or platform that doesn't support Valgrind"
57 #include <sys/ioctl.h>
61 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
62 * native OS version, as we're going to be doing our own ioctls to
63 * make sure that, in the uninitialized-data tests, the filters aren't
64 * checked by libpcap before being handed to BPF.
66 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
68 #elif defined(USE_SOCKET_FILTERS)
70 #include <sys/socket.h>
71 #include <linux/types.h>
72 #include <linux/filter.h>
77 #ifndef HAVE___ATTRIBUTE__
78 #define __attribute__(x)
81 static char *program_name
;
84 static void usage(void) __attribute__((noreturn
));
85 static void error(const char *, ...)
86 __attribute__((noreturn
, format (printf
, 1, 2)));
87 static void warning(const char *, ...)
88 __attribute__((format (printf
, 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("can't open %s: %s", fname
, pcap_strerror(errno
));
115 if (fstat(fd
, &buf
) < 0)
116 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
118 cp
= malloc((u_int
)buf
.st_size
+ 1);
120 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
121 fname
, pcap_strerror(errno
));
122 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
124 error("read %s: %s", fname
, pcap_strerror(errno
));
125 if (cc
!= buf
.st_size
)
126 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
129 /* replace "# comment" with spaces */
130 for (i
= 0; i
< cc
; i
++) {
132 while (i
< cc
&& cp
[i
] != '\n')
141 error(const char *fmt
, ...)
145 (void)fprintf(stderr
, "%s: ", program_name
);
147 (void)vfprintf(stderr
, fmt
, ap
);
152 (void)fputc('\n', stderr
);
160 warning(const char *fmt
, ...)
164 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
166 (void)vfprintf(stderr
, fmt
, ap
);
171 (void)fputc('\n', stderr
);
176 * Copy arg vector into a new buffer, concatenating arguments with spaces.
179 copy_argv(register char **argv
)
182 register u_int len
= 0;
191 len
+= strlen(*p
++) + 1;
193 buf
= (char *)malloc(len
);
195 error("copy_argv: malloc");
199 while ((src
= *p
++) != NULL
) {
200 while ((*dst
++ = *src
++) != '\0')
209 #define INSN_COUNT 17
212 main(int argc
, char **argv
)
216 int dorfmon
, useactivate
;
217 char ebuf
[PCAP_ERRBUF_SIZE
];
224 struct bpf_program bad_fcode
;
225 struct bpf_insn uninitialized
[INSN_COUNT
];
226 #elif defined(USE_SOCKET_FILTERS)
227 struct sock_fprog bad_fcode
;
228 struct sock_filter uninitialized
[INSN_COUNT
];
230 struct bpf_program fcode
;
237 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
238 program_name
= cp
+ 1;
240 program_name
= argv
[0];
243 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
260 useactivate
= 1; /* required for rfmon */
269 if (device
== NULL
) {
271 * No interface specified; get whatever pcap_lookupdev()
274 device
= pcap_lookupdev(ebuf
);
275 if (device
== NULL
) {
276 error("couldn't find interface to use: %s",
281 if (infile
!= NULL
) {
283 * Filter specified with "-F" and a file containing
286 cmdbuf
= read_infile(infile
);
290 * Filter specified with arguments on the
293 cmdbuf
= copy_argv(&argv
[optind
+1]);
296 * No filter specified; use an empty string, which
297 * compiles to an "accept all" filter.
304 pd
= pcap_create(device
, ebuf
);
306 error("%s: pcap_create() failed: %s", device
, ebuf
);
307 status
= pcap_set_snaplen(pd
, 65535);
309 error("%s: pcap_set_snaplen failed: %s",
310 device
, pcap_statustostr(status
));
311 status
= pcap_set_promisc(pd
, 1);
313 error("%s: pcap_set_promisc failed: %s",
314 device
, pcap_statustostr(status
));
316 status
= pcap_set_rfmon(pd
, 1);
318 error("%s: pcap_set_rfmon failed: %s",
319 device
, pcap_statustostr(status
));
321 status
= pcap_set_timeout(pd
, 1000);
323 error("%s: pcap_set_timeout failed: %s",
324 device
, pcap_statustostr(status
));
325 status
= pcap_activate(pd
);
328 * pcap_activate() failed.
330 error("%s: %s\n(%s)", device
,
331 pcap_statustostr(status
), pcap_geterr(pd
));
332 } else if (status
> 0) {
334 * pcap_activate() succeeded, but it's warning us
335 * of a problem it had.
337 warning("%s: %s\n(%s)", device
,
338 pcap_statustostr(status
), pcap_geterr(pd
));
342 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
349 pcap_fd
= pcap_fileno(pd
);
352 * Try setting a filter with an uninitialized bpf_program
353 * structure. This should cause valgrind to report a
356 * We don't check for errors, because it could get an
357 * error due to a bad pointer or count.
360 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
361 #elif defined(USE_SOCKET_FILTERS)
362 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
367 * Try setting a filter with an initialized bpf_program
368 * structure that points to an uninitialized program.
369 * That should also cause valgrind to report a problem.
371 * We don't check for errors, because it could get an
372 * error due to a bad pointer or count.
375 bad_fcode
.bf_len
= INSN_COUNT
;
376 bad_fcode
.bf_insns
= uninitialized
;
377 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
378 #elif defined(USE_SOCKET_FILTERS)
379 bad_fcode
.len
= INSN_COUNT
;
380 bad_fcode
.filter
= uninitialized
;
381 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
386 * Now compile a filter and set the filter with that.
387 * That should *not* cause valgrind to report a
390 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
391 error("can't compile filter: %s", pcap_geterr(pd
));
392 if (pcap_setfilter(pd
, &fcode
) < 0)
393 error("can't set filter: %s", pcap_geterr(pd
));
396 exit(status
< 0 ? 1 : 0);
402 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
404 (void)fprintf(stderr
,
405 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",