1 /* $NetBSD: valgrindtest.c,v 1.3 2015/03/31 21:39:43 christos Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * 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";
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: valgrindtest.c,v 1.3 2015/03/31 21:39:43 christos Exp $");
44 #include <arpa/inet.h>
45 #include <sys/types.h>
48 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
49 /* BSD-flavored OS - use BPF */
52 /* Linux - use socket filters */
53 #define USE_SOCKET_FILTERS
55 #error "Unknown platform or platform that doesn't support Valgrind"
60 #include <sys/ioctl.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
71 #elif defined(USE_SOCKET_FILTERS)
73 #include <sys/socket.h>
74 #include <linux/types.h>
75 #include <linux/filter.h>
80 #ifndef HAVE___ATTRIBUTE__
81 #define __attribute__(x)
84 static char *program_name
;
87 static void usage(void) __attribute__((noreturn
));
88 static void error(const char *, ...)
89 __attribute__((noreturn
, format (printf
, 1, 2)));
90 static void warning(const char *, ...)
91 __attribute__((format (printf
, 1, 2)));
98 * On Windows, we need to open the file in binary mode, so that
99 * we get all the bytes specified by the size we get from "fstat()".
100 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
101 * we define it as 0 if it's not defined, so it does nothing.
108 read_infile(char *fname
)
110 register int i
, fd
, cc
;
114 fd
= open(fname
, O_RDONLY
|O_BINARY
);
116 error("can't open %s: %s", fname
, pcap_strerror(errno
));
118 if (fstat(fd
, &buf
) < 0)
119 error("can't stat %s: %s", fname
, pcap_strerror(errno
));
121 cp
= malloc((u_int
)buf
.st_size
+ 1);
123 error("malloc(%d) for %s: %s", (u_int
)buf
.st_size
+ 1,
124 fname
, pcap_strerror(errno
));
125 cc
= read(fd
, cp
, (u_int
)buf
.st_size
);
127 error("read %s: %s", fname
, pcap_strerror(errno
));
128 if (cc
!= buf
.st_size
)
129 error("short read %s (%d != %d)", fname
, cc
, (int)buf
.st_size
);
132 /* replace "# comment" with spaces */
133 for (i
= 0; i
< cc
; i
++) {
135 while (i
< cc
&& cp
[i
] != '\n')
144 error(const char *fmt
, ...)
148 (void)fprintf(stderr
, "%s: ", program_name
);
150 (void)vfprintf(stderr
, fmt
, ap
);
155 (void)fputc('\n', stderr
);
163 warning(const char *fmt
, ...)
167 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
169 (void)vfprintf(stderr
, fmt
, ap
);
174 (void)fputc('\n', stderr
);
179 * Copy arg vector into a new buffer, concatenating arguments with spaces.
182 copy_argv(register char **argv
)
185 register u_int len
= 0;
194 len
+= strlen(*p
++) + 1;
196 buf
= (char *)malloc(len
);
198 error("copy_argv: malloc");
202 while ((src
= *p
++) != NULL
) {
203 while ((*dst
++ = *src
++) != '\0')
212 #define INSN_COUNT 17
215 main(int argc
, char **argv
)
219 int dorfmon
, useactivate
;
220 char ebuf
[PCAP_ERRBUF_SIZE
];
227 struct bpf_program bad_fcode
;
228 struct bpf_insn uninitialized
[INSN_COUNT
];
229 #elif defined(USE_SOCKET_FILTERS)
230 struct sock_fprog bad_fcode
;
231 struct sock_filter uninitialized
[INSN_COUNT
];
233 struct bpf_program fcode
;
240 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
241 program_name
= cp
+ 1;
243 program_name
= argv
[0];
246 while ((op
= getopt(argc
, argv
, "aF:i:I")) != -1) {
263 useactivate
= 1; /* required for rfmon */
272 if (device
== NULL
) {
274 * No interface specified; get whatever pcap_lookupdev()
277 device
= pcap_lookupdev(ebuf
);
278 if (device
== NULL
) {
279 error("couldn't find interface to use: %s",
284 if (infile
!= NULL
) {
286 * Filter specified with "-F" and a file containing
289 cmdbuf
= read_infile(infile
);
293 * Filter specified with arguments on the
296 cmdbuf
= copy_argv(&argv
[optind
+1]);
299 * No filter specified; use an empty string, which
300 * compiles to an "accept all" filter.
307 pd
= pcap_create(device
, ebuf
);
309 error("%s: pcap_create() failed: %s", device
, ebuf
);
310 status
= pcap_set_snaplen(pd
, 65535);
312 error("%s: pcap_set_snaplen failed: %s",
313 device
, pcap_statustostr(status
));
314 status
= pcap_set_promisc(pd
, 1);
316 error("%s: pcap_set_promisc failed: %s",
317 device
, pcap_statustostr(status
));
319 status
= pcap_set_rfmon(pd
, 1);
321 error("%s: pcap_set_rfmon failed: %s",
322 device
, pcap_statustostr(status
));
324 status
= pcap_set_timeout(pd
, 1000);
326 error("%s: pcap_set_timeout failed: %s",
327 device
, pcap_statustostr(status
));
328 status
= pcap_activate(pd
);
331 * pcap_activate() failed.
333 error("%s: %s\n(%s)", device
,
334 pcap_statustostr(status
), pcap_geterr(pd
));
335 } else if (status
> 0) {
337 * pcap_activate() succeeded, but it's warning us
338 * of a problem it had.
340 warning("%s: %s\n(%s)", device
,
341 pcap_statustostr(status
), pcap_geterr(pd
));
345 pd
= pcap_open_live(device
, 65535, 1, 1000, ebuf
);
352 pcap_fd
= pcap_fileno(pd
);
355 * Try setting a filter with an uninitialized bpf_program
356 * structure. This should cause valgrind to report a
359 * We don't check for errors, because it could get an
360 * error due to a bad pointer or count.
363 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
364 #elif defined(USE_SOCKET_FILTERS)
365 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
370 * Try setting a filter with an initialized bpf_program
371 * structure that points to an uninitialized program.
372 * That should also cause valgrind to report a problem.
374 * We don't check for errors, because it could get an
375 * error due to a bad pointer or count.
378 bad_fcode
.bf_len
= INSN_COUNT
;
379 bad_fcode
.bf_insns
= uninitialized
;
380 ioctl(pcap_fd
, BIOCSETF
, &bad_fcode
);
381 #elif defined(USE_SOCKET_FILTERS)
382 bad_fcode
.len
= INSN_COUNT
;
383 bad_fcode
.filter
= uninitialized
;
384 setsockopt(pcap_fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bad_fcode
,
389 * Now compile a filter and set the filter with that.
390 * That should *not* cause valgrind to report a
393 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, 0) < 0)
394 error("can't compile filter: %s", pcap_geterr(pd
));
395 if (pcap_setfilter(pd
, &fcode
) < 0)
396 error("can't set filter: %s", pcap_geterr(pd
));
399 exit(status
< 0 ? 1 : 0);
405 (void)fprintf(stderr
, "%s, with %s\n", program_name
,
407 (void)fprintf(stderr
,
408 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",