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>
76 static char *program_name
;
79 * This was introduced by Clang:
81 * http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
83 * in some version (which version?); it has been picked up by GCC 5.0.
85 #ifndef __has_attribute
87 * It's a macro, so you can check whether it's defined to check
88 * whether it's supported.
90 * If it's not, define it to always return 0, so that we move on to
91 * the fallback checks.
93 #define __has_attribute(x) 0
96 #if __has_attribute(noreturn) \
97 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
98 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) \
99 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
100 || (defined(__HP_aCC) && __HP_aCC >= 61000)
102 * Compiler with support for it, or GCC 2.5 and later, or Solaris Studio 12
103 * (Sun C 5.9) and later, or IBM XL C 10.1 and later (do any earlier
104 * versions of XL C support this?), or HP aCC A.06.10 and later.
106 #define PCAP_NORETURN __attribute((noreturn))
107 #elif defined( _MSC_VER )
108 #define PCAP_NORETURN __declspec(noreturn)
110 #define PCAP_NORETURN
113 #if __has_attribute(__format__) \
114 || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)) \
115 || (defined(__xlC__) && __xlC__ >= 0x0A01) \
116 || (defined(__HP_aCC) && __HP_aCC >= 61000)
118 * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
119 * and later (do any earlier versions of XL C support this?),
120 * or HP aCC A.06.10 and later.
122 #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
124 #define PCAP_PRINTFLIKE(x,y)
128 static void PCAP_NORETURN
usage(void);
129 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
130 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
133 * On Windows, we need to open the file in binary mode, so that
134 * we get all the bytes specified by the size we get from "fstat()".
135 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
136 * we define it as 0 if it's not defined, so it does nothing.
143 read_infile(char *fname
)
145 register int i
, fd
, cc
;
149 fd
= open(fname
, O_RDONLY
|O_BINARY
);
151 error("can't open %s: %s", fname
, pcap_strerror(errno
));
153 if (fstat(fd
, &buf
) < 0)
154 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
156 cp
= malloc((u_int
)buf
.st_size
+ 1);
158 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
159 fname
, pcap_strerror(errno
));
160 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
162 error("read %s: %s", fname
, pcap_strerror(errno
));
163 if (cc
!= buf
.st_size
)
164 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
167 /* replace "# comment" with spaces */
168 for (i
= 0; i
< cc
; i
++) {
170 while (i
< cc
&& cp
[i
] != '\n')
179 error(const char *fmt
, ...)
183 (void)fprintf(stderr
, "%s: ", program_name
);
185 (void)vfprintf(stderr
, fmt
, ap
);
190 (void)fputc('\n', stderr
);
198 warning(const char *fmt
, ...)
202 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
204 (void)vfprintf(stderr
, fmt
, ap
);
209 (void)fputc('\n', stderr
);
214 * Copy arg vector into a new buffer, concatenating arguments with spaces.
217 copy_argv(register char **argv
)
220 register u_int len
= 0;
229 len
+= strlen(*p
++) + 1;
231 buf
= (char *)malloc(len
);
233 error("copy_argv: malloc");
237 while ((src
= *p
++) != NULL
) {
238 while ((*dst
++ = *src
++) != '\0')
247 #define INSN_COUNT 17
250 main(int argc
, char **argv
)
254 int dorfmon
, useactivate
;
255 char ebuf
[PCAP_ERRBUF_SIZE
];
262 struct bpf_program bad_fcode
;
263 struct bpf_insn uninitialized
[INSN_COUNT
];
264 #elif defined(USE_SOCKET_FILTERS)
265 struct sock_fprog bad_fcode
;
266 struct sock_filter uninitialized
[INSN_COUNT
];
268 struct bpf_program fcode
;
275 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
276 program_name
= cp
+ 1;
278 program_name
= argv
[0];
281 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
298 useactivate
= 1; /* required for rfmon */
307 if (device
== NULL
) {
309 * No interface specified; get whatever pcap_lookupdev()
312 device
= pcap_lookupdev(ebuf
);
313 if (device
== NULL
) {
314 error("couldn't find interface to use: %s",
319 if (infile
!= NULL
) {
321 * Filter specified with "-F" and a file containing
324 cmdbuf
= read_infile(infile
);
328 * Filter specified with arguments on the
331 cmdbuf
= copy_argv(&argv
[optind
+1]);
334 * No filter specified; use an empty string, which
335 * compiles to an "accept all" filter.
342 pd
= pcap_create(device
, ebuf
);
344 error("%s: pcap_create() failed: %s", device
, ebuf
);
345 status
= pcap_set_snaplen(pd
, 65535);
347 error("%s: pcap_set_snaplen failed: %s",
348 device
, pcap_statustostr(status
));
349 status
= pcap_set_promisc(pd
, 1);
351 error("%s: pcap_set_promisc failed: %s",
352 device
, pcap_statustostr(status
));
354 status
= pcap_set_rfmon(pd
, 1);
356 error("%s: pcap_set_rfmon failed: %s",
357 device
, pcap_statustostr(status
));
359 status
= pcap_set_timeout(pd
, 1000);
361 error("%s: pcap_set_timeout failed: %s",
362 device
, pcap_statustostr(status
));
363 status
= pcap_activate(pd
);
366 * pcap_activate() failed.
368 error("%s: %s\n(%s)", device
,
369 pcap_statustostr(status
), pcap_geterr(pd
));
370 } else if (status
> 0) {
372 * pcap_activate() succeeded, but it's warning us
373 * of a problem it had.
375 warning("%s: %s\n(%s)", device
,
376 pcap_statustostr(status
), pcap_geterr(pd
));
380 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
387 pcap_fd
= pcap_fileno(pd
);
390 * Try setting a filter with an uninitialized bpf_program
391 * structure. This should cause valgrind to report a
394 * We don't check for errors, because it could get an
395 * error due to a bad pointer or count.
398 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
399 #elif defined(USE_SOCKET_FILTERS)
400 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
405 * Try setting a filter with an initialized bpf_program
406 * structure that points to an uninitialized program.
407 * That should also cause valgrind to report a problem.
409 * We don't check for errors, because it could get an
410 * error due to a bad pointer or count.
413 bad_fcode
.bf_len
= INSN_COUNT
;
414 bad_fcode
.bf_insns
= uninitialized
;
415 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
416 #elif defined(USE_SOCKET_FILTERS)
417 bad_fcode
.len
= INSN_COUNT
;
418 bad_fcode
.filter
= uninitialized
;
419 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
424 * Now compile a filter and set the filter with that.
425 * That should *not* cause valgrind to report a
428 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
429 error("can't compile filter: %s", pcap_geterr(pd
));
430 if (pcap_setfilter(pd
, &fcode
) < 0)
431 error("can't set filter: %s", pcap_geterr(pd
));
434 exit(status
< 0 ? 1 : 0);
440 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
442 (void)fprintf(stderr
,
443 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",