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";
39 #include <arpa/inet.h>
40 #include <sys/types.h>
43 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
44 /* BSD-flavored OS - use BPF */
47 /* Linux - use socket filters */
48 #define USE_SOCKET_FILTERS
50 #error "Unknown platform or platform that doesn't support Valgrind"
55 #include <sys/ioctl.h>
59 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
60 * native OS version, as we're going to be doing our own ioctls to
61 * make sure that, in the uninitialized-data tests, the filters aren't
62 * checked by libpcap before being handed to BPF.
64 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
66 #elif defined(USE_SOCKET_FILTERS)
68 #include <sys/socket.h>
69 #include <linux/types.h>
70 #include <linux/filter.h>
75 #ifndef HAVE___ATTRIBUTE__
76 #define __attribute__(x)
79 static char *program_name
;
82 static void usage(void) __attribute__((noreturn
));
83 static void error(const char *, ...)
84 __attribute__((noreturn
, format (printf
, 1, 2)));
85 static void warning(const char *, ...)
86 __attribute__((format (printf
, 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.
103 read_infile(char *fname
)
105 register int i
, fd
, cc
;
109 fd
= open(fname
, O_RDONLY
|O_BINARY
);
111 error("can't open %s: %s", fname
, pcap_strerror(errno
));
113 if (fstat(fd
, &buf
) < 0)
114 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
116 cp
= malloc((u_int
)buf
.st_size
+ 1);
118 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
119 fname
, pcap_strerror(errno
));
120 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
122 error("read %s: %s", fname
, pcap_strerror(errno
));
123 if (cc
!= buf
.st_size
)
124 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
127 /* replace "# comment" with spaces */
128 for (i
= 0; i
< cc
; i
++) {
130 while (i
< cc
&& cp
[i
] != '\n')
139 error(const char *fmt
, ...)
143 (void)fprintf(stderr
, "%s: ", program_name
);
145 (void)vfprintf(stderr
, fmt
, ap
);
150 (void)fputc('\n', stderr
);
158 warning(const char *fmt
, ...)
162 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
164 (void)vfprintf(stderr
, fmt
, ap
);
169 (void)fputc('\n', stderr
);
174 * Copy arg vector into a new buffer, concatenating arguments with spaces.
177 copy_argv(register char **argv
)
180 register u_int len
= 0;
189 len
+= strlen(*p
++) + 1;
191 buf
= (char *)malloc(len
);
193 error("copy_argv: malloc");
197 while ((src
= *p
++) != NULL
) {
198 while ((*dst
++ = *src
++) != '\0')
207 #define INSN_COUNT 17
210 main(int argc
, char **argv
)
214 int dorfmon
, useactivate
;
215 char ebuf
[PCAP_ERRBUF_SIZE
];
222 struct bpf_program bad_fcode
;
223 struct bpf_insn uninitialized
[INSN_COUNT
];
224 #elif defined(USE_SOCKET_FILTERS)
225 struct sock_fprog bad_fcode
;
226 struct sock_filter uninitialized
[INSN_COUNT
];
228 struct bpf_program fcode
;
235 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
236 program_name
= cp
+ 1;
238 program_name
= argv
[0];
241 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
258 useactivate
= 1; /* required for rfmon */
267 if (device
== NULL
) {
269 * No interface specified; get whatever pcap_lookupdev()
272 device
= pcap_lookupdev(ebuf
);
273 if (device
== NULL
) {
274 error("couldn't find interface to use: %s",
279 if (infile
!= NULL
) {
281 * Filter specified with "-F" and a file containing
284 cmdbuf
= read_infile(infile
);
288 * Filter specified with arguments on the
291 cmdbuf
= copy_argv(&argv
[optind
+1]);
294 * No filter specified; use an empty string, which
295 * compiles to an "accept all" filter.
302 pd
= pcap_create(device
, ebuf
);
304 error("%s: pcap_create() failed: %s", device
, ebuf
);
305 status
= pcap_set_snaplen(pd
, 65535);
307 error("%s: pcap_set_snaplen failed: %s",
308 device
, pcap_statustostr(status
));
309 status
= pcap_set_promisc(pd
, 1);
311 error("%s: pcap_set_promisc failed: %s",
312 device
, pcap_statustostr(status
));
314 status
= pcap_set_rfmon(pd
, 1);
316 error("%s: pcap_set_rfmon failed: %s",
317 device
, pcap_statustostr(status
));
319 status
= pcap_set_timeout(pd
, 1000);
321 error("%s: pcap_set_timeout failed: %s",
322 device
, pcap_statustostr(status
));
323 status
= pcap_activate(pd
);
326 * pcap_activate() failed.
328 error("%s: %s\n(%s)", device
,
329 pcap_statustostr(status
), pcap_geterr(pd
));
330 } else if (status
> 0) {
332 * pcap_activate() succeeded, but it's warning us
333 * of a problem it had.
335 warning("%s: %s\n(%s)", device
,
336 pcap_statustostr(status
), pcap_geterr(pd
));
340 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
347 pcap_fd
= pcap_fileno(pd
);
350 * Try setting a filter with an uninitialized bpf_program
351 * structure. This should cause valgrind to report a
354 * We don't check for errors, because it could get an
355 * error due to a bad pointer or count.
358 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
359 #elif defined(USE_SOCKET_FILTERS)
360 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
365 * Try setting a filter with an initialized bpf_program
366 * structure that points to an uninitialized program.
367 * That should also cause valgrind to report a problem.
369 * We don't check for errors, because it could get an
370 * error due to a bad pointer or count.
373 bad_fcode
.bf_len
= INSN_COUNT
;
374 bad_fcode
.bf_insns
= uninitialized
;
375 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
376 #elif defined(USE_SOCKET_FILTERS)
377 bad_fcode
.len
= INSN_COUNT
;
378 bad_fcode
.filter
= uninitialized
;
379 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
384 * Now compile a filter and set the filter with that.
385 * That should *not* cause valgrind to report a
388 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
389 error("can't compile filter: %s", pcap_geterr(pd
));
390 if (pcap_setfilter(pd
, &fcode
) < 0)
391 error("can't set filter: %s", pcap_geterr(pd
));
394 exit(status
< 0 ? 1 : 0);
400 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
402 (void)fprintf(stderr
,
403 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",