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 */
51 #error "Unknown platform or platform that doesn't support Valgrind"
55 #include <sys/ioctl.h>
57 #elif defined(USE_SOCKET_FILTERS)
58 #include <sys/socket.h>
59 #include <linux/types.h>
60 #include <linux/filter.h>
64 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
65 * native OS version, as we're going to be doing our own ioctls to
66 * make sure that, in the uninitialized-data tests, the filters aren't
67 * checked by libpcap before being handed to BPF.
69 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
72 #ifndef HAVE___ATTRIBUTE__
73 #define __attribute__(x)
76 static char *program_name
;
79 static void usage(void) __attribute__((noreturn
));
80 static void error(const char *, ...)
81 __attribute__((noreturn
, format (printf
, 1, 2)));
82 static void warning(const char *, ...)
83 __attribute__((format (printf
, 1, 2)));
90 * On Windows, we need to open the file in binary mode, so that
91 * we get all the bytes specified by the size we get from "fstat()".
92 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
93 * we define it as 0 if it's not defined, so it does nothing.
100 read_infile(char *fname
)
102 register int i
, fd
, cc
;
106 fd
= open(fname
, O_RDONLY
|O_BINARY
);
108 error("can't open %s: %s", fname
, pcap_strerror(errno
));
110 if (fstat(fd
, &buf
) < 0)
111 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
113 cp
= malloc((u_int
)buf
.st_size
+ 1);
115 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
116 fname
, pcap_strerror(errno
));
117 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
119 error("read %s: %s", fname
, pcap_strerror(errno
));
120 if (cc
!= buf
.st_size
)
121 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
124 /* replace "# comment" with spaces */
125 for (i
= 0; i
< cc
; i
++) {
127 while (i
< cc
&& cp
[i
] != '\n')
136 error(const char *fmt
, ...)
140 (void)fprintf(stderr
, "%s: ", program_name
);
142 (void)vfprintf(stderr
, fmt
, ap
);
147 (void)fputc('\n', stderr
);
155 warning(const char *fmt
, ...)
159 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
161 (void)vfprintf(stderr
, fmt
, ap
);
166 (void)fputc('\n', stderr
);
171 * Copy arg vector into a new buffer, concatenating arguments with spaces.
174 copy_argv(register char **argv
)
177 register u_int len
= 0;
186 len
+= strlen(*p
++) + 1;
188 buf
= (char *)malloc(len
);
190 error("copy_argv: malloc");
194 while ((src
= *p
++) != NULL
) {
195 while ((*dst
++ = *src
++) != '\0')
204 #define INSN_COUNT 17
207 main(int argc
, char **argv
)
211 int dorfmon
, useactivate
;
212 char ebuf
[PCAP_ERRBUF_SIZE
];
219 struct bpf_program bad_fcode
;
220 struct bpf_insn uninitialized
[INSN_COUNT
];
221 #elif define(USE_SOCKET_FILTERS)
222 struct sock_fprog bad_fcode
;
223 struct sock_filter uninitialized
[INSN_COUNT
];
225 struct bpf_program fcode
;
232 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
233 program_name
= cp
+ 1;
235 program_name
= argv
[0];
238 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
255 useactivate
= 1; /* required for rfmon */
264 if (device
== NULL
) {
266 * No interface specified; get whatever pcap_lookupdev()
269 device
= pcap_lookupdev(ebuf
);
270 if (device
== NULL
) {
271 error("couldn't find interface to use: %s",
276 if (infile
!= NULL
) {
278 * Filter specified with "-F" and a file containing
281 cmdbuf
= read_infile(infile
);
285 * Filter specified with arguments on the
288 cmdbuf
= copy_argv(&argv
[optind
+1]);
291 * No filter specified; use an empty string, which
292 * compiles to an "accept all" filter.
299 pd
= pcap_create(device
, ebuf
);
301 error("%s: pcap_create() failed: %s", device
, ebuf
);
302 status
= pcap_set_snaplen(pd
, 65535);
304 error("%s: pcap_set_snaplen failed: %s",
305 device
, pcap_statustostr(status
));
306 status
= pcap_set_promisc(pd
, 1);
308 error("%s: pcap_set_promisc failed: %s",
309 device
, pcap_statustostr(status
));
311 status
= pcap_set_rfmon(pd
, 1);
313 error("%s: pcap_set_rfmon failed: %s",
314 device
, pcap_statustostr(status
));
316 status
= pcap_set_timeout(pd
, 1000);
318 error("%s: pcap_set_timeout failed: %s",
319 device
, pcap_statustostr(status
));
320 status
= pcap_activate(pd
);
323 * pcap_activate() failed.
325 error("%s: %s\n(%s)", device
,
326 pcap_statustostr(status
), pcap_geterr(pd
));
327 } else if (status
> 0) {
329 * pcap_activate() succeeded, but it's warning us
330 * of a problem it had.
332 warning("%s: %s\n(%s)", device
,
333 pcap_statustostr(status
), pcap_geterr(pd
));
337 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
344 pcap_fd
= pcap_fileno(pd
);
347 * Try setting a filter with an uninitialized bpf_program
348 * structure. This should cause valgrind to report a
351 * We don't check for errors, because it could get an
352 * error due to a bad pointer or count.
355 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
356 #elif defined(USE_SOCKET_FILTERS)
357 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
362 * Try setting a filter with an initialized bpf_program
363 * structure that points to an uninitialized program.
364 * That should also cause valgrind to report a problem.
366 * We don't check for errors, because it could get an
367 * error due to a bad pointer or count.
370 bad_fcode
.bf_len
= INSN_COUNT
;
371 bad_fcode
.bf_insns
= uninitialized
;
372 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
373 #elif defined(USE_SOCKET_FILTERS)
374 bad_fcode
.len
= INSN_COUNT
;
375 bad_fcode
.filter
= uninitialized
;
376 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
381 * Now compile a filter and set the filter with that.
382 * That should *not* cause valgrind to report a
385 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
386 error("can't compile filter: %s", pcap_geterr(pd
));
387 if (pcap_setfilter(pd
, &fcode
) < 0)
388 error("can't set filter: %s", pcap_geterr(pd
));
391 exit(status
< 0 ? 1 : 0);
397 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
399 (void)fprintf(stderr
,
400 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",