]> The Tcpdump Group git mirrors - libpcap/blob - tests/valgrindtest.c
Clean up the ether_hostton() stuff.
[libpcap] / tests / valgrindtest.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 */
21
22 /*
23 * This doesn't actually test libpcap itself; it tests whether
24 * valgrind properly handles the APIs libpcap uses. If it doesn't,
25 * we end up getting patches submitted to "fix" references that
26 * valgrind claims are being made to uninitialized data, when, in
27 * fact, the OS isn't making any such references - or we get
28 * valgrind *not* detecting *actual* incorrect references.
29 *
30 * Both BPF and Linux socket filters aren't handled correctly
31 * by some versions of valgrind. See valgrind bug 318203 for
32 * Linux:
33 *
34 * https://bugs.kde.org/show_bug.cgi?id=318203
35 *
36 * and valgrind bug 312989 for OS X:
37 *
38 * https://bugs.kde.org/show_bug.cgi?id=312989
39 *
40 * The fixes for both of those are checked into the official valgrind
41 * repository.
42 *
43 * The unofficial FreeBSD port has similar issues to the official OS X
44 * port, for similar reasons.
45 */
46 #ifndef lint
47 static const char copyright[] _U_ =
48 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
49 The Regents of the University of California. All rights reserved.\n";
50 #endif
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <stdarg.h>
60 #include <unistd.h>
61 #include <fcntl.h>
62 #include <errno.h>
63 #include <arpa/inet.h>
64 #include <sys/types.h>
65 #include <sys/stat.h>
66
67 #include "pcap/funcattrs.h"
68
69 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
70 /* BSD-flavored OS - use BPF */
71 #define USE_BPF
72 #elif defined(linux)
73 /* Linux - use socket filters */
74 #define USE_SOCKET_FILTERS
75 #else
76 #error "Unknown platform or platform that doesn't support Valgrind"
77 #endif
78
79 #if defined(USE_BPF)
80
81 #include <sys/ioctl.h>
82 #include <net/bpf.h>
83
84 /*
85 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
86 * native OS version, as we're going to be doing our own ioctls to
87 * make sure that, in the uninitialized-data tests, the filters aren't
88 * checked by libpcap before being handed to BPF.
89 */
90 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
91
92 #elif defined(USE_SOCKET_FILTERS)
93
94 #include <sys/socket.h>
95 #include <linux/types.h>
96 #include <linux/filter.h>
97
98 #endif
99
100 #include <pcap.h>
101
102 static char *program_name;
103
104 /* Forwards */
105 static void PCAP_NORETURN usage(void);
106 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
107 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
108
109 /*
110 * On Windows, we need to open the file in binary mode, so that
111 * we get all the bytes specified by the size we get from "fstat()".
112 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
113 * we define it as 0 if it's not defined, so it does nothing.
114 */
115 #ifndef O_BINARY
116 #define O_BINARY 0
117 #endif
118
119 static char *
120 read_infile(char *fname)
121 {
122 register int i, fd, cc;
123 register char *cp;
124 struct stat buf;
125
126 fd = open(fname, O_RDONLY|O_BINARY);
127 if (fd < 0)
128 error("can't open %s: %s", fname, pcap_strerror(errno));
129
130 if (fstat(fd, &buf) < 0)
131 error("can't stat %s: %s", fname, pcap_strerror(errno));
132
133 cp = malloc((u_int)buf.st_size + 1);
134 if (cp == NULL)
135 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
136 fname, pcap_strerror(errno));
137 cc = read(fd, cp, (u_int)buf.st_size);
138 if (cc < 0)
139 error("read %s: %s", fname, pcap_strerror(errno));
140 if (cc != buf.st_size)
141 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
142
143 close(fd);
144 /* replace "# comment" with spaces */
145 for (i = 0; i < cc; i++) {
146 if (cp[i] == '#')
147 while (i < cc && cp[i] != '\n')
148 cp[i++] = ' ';
149 }
150 cp[cc] = '\0';
151 return (cp);
152 }
153
154 /* VARARGS */
155 static void
156 error(const char *fmt, ...)
157 {
158 va_list ap;
159
160 (void)fprintf(stderr, "%s: ", program_name);
161 va_start(ap, fmt);
162 (void)vfprintf(stderr, fmt, ap);
163 va_end(ap);
164 if (*fmt) {
165 fmt += strlen(fmt);
166 if (fmt[-1] != '\n')
167 (void)fputc('\n', stderr);
168 }
169 exit(1);
170 /* NOTREACHED */
171 }
172
173 /* VARARGS */
174 static void
175 warning(const char *fmt, ...)
176 {
177 va_list ap;
178
179 (void)fprintf(stderr, "%s: WARNING: ", program_name);
180 va_start(ap, fmt);
181 (void)vfprintf(stderr, fmt, ap);
182 va_end(ap);
183 if (*fmt) {
184 fmt += strlen(fmt);
185 if (fmt[-1] != '\n')
186 (void)fputc('\n', stderr);
187 }
188 }
189
190 /*
191 * Copy arg vector into a new buffer, concatenating arguments with spaces.
192 */
193 static char *
194 copy_argv(register char **argv)
195 {
196 register char **p;
197 register u_int len = 0;
198 char *buf;
199 char *src, *dst;
200
201 p = argv;
202 if (*p == 0)
203 return 0;
204
205 while (*p)
206 len += strlen(*p++) + 1;
207
208 buf = (char *)malloc(len);
209 if (buf == NULL)
210 error("copy_argv: malloc");
211
212 p = argv;
213 dst = buf;
214 while ((src = *p++) != NULL) {
215 while ((*dst++ = *src++) != '\0')
216 ;
217 dst[-1] = ' ';
218 }
219 dst[-1] = '\0';
220
221 return buf;
222 }
223
224 #define INSN_COUNT 17
225
226 int
227 main(int argc, char **argv)
228 {
229 char *cp, *device;
230 int op;
231 int dorfmon, useactivate;
232 char ebuf[PCAP_ERRBUF_SIZE];
233 char *infile;
234 char *cmdbuf;
235 pcap_if_t *devlist;
236 pcap_t *pd;
237 int status = 0;
238 int pcap_fd;
239 #if defined(USE_BPF)
240 struct bpf_program bad_fcode;
241 struct bpf_insn uninitialized[INSN_COUNT];
242 #elif defined(USE_SOCKET_FILTERS)
243 struct sock_fprog bad_fcode;
244 struct sock_filter uninitialized[INSN_COUNT];
245 #endif
246 struct bpf_program fcode;
247
248 device = NULL;
249 dorfmon = 0;
250 useactivate = 0;
251 infile = NULL;
252
253 if ((cp = strrchr(argv[0], '/')) != NULL)
254 program_name = cp + 1;
255 else
256 program_name = argv[0];
257
258 opterr = 0;
259 while ((op = getopt(argc, argv, "aF:i:I")) != -1) {
260 switch (op) {
261
262 case 'a':
263 useactivate = 1;
264 break;
265
266 case 'F':
267 infile = optarg;
268 break;
269
270 case 'i':
271 device = optarg;
272 break;
273
274 case 'I':
275 dorfmon = 1;
276 useactivate = 1; /* required for rfmon */
277 break;
278
279 default:
280 usage();
281 /* NOTREACHED */
282 }
283 }
284
285 if (device == NULL) {
286 /*
287 * No interface specified; get whatever pcap_lookupdev()
288 * finds.
289 */
290 if (pcap_findalldevs(&devlist, ebuf) == -1)
291 error("%s", ebuf);
292 if (devlist == NULL)
293 error("no interfaces available for capture");
294 device = strdup(devlist->name);
295 pcap_freealldevs(devlist);
296 }
297
298 if (infile != NULL) {
299 /*
300 * Filter specified with "-F" and a file containing
301 * a filter.
302 */
303 cmdbuf = read_infile(infile);
304 } else {
305 if (optind < argc) {
306 /*
307 * Filter specified with arguments on the
308 * command line.
309 */
310 cmdbuf = copy_argv(&argv[optind+1]);
311 } else {
312 /*
313 * No filter specified; use an empty string, which
314 * compiles to an "accept all" filter.
315 */
316 cmdbuf = "";
317 }
318 }
319
320 if (useactivate) {
321 pd = pcap_create(device, ebuf);
322 if (pd == NULL)
323 error("%s: pcap_create() failed: %s", device, ebuf);
324 status = pcap_set_snaplen(pd, 65535);
325 if (status != 0)
326 error("%s: pcap_set_snaplen failed: %s",
327 device, pcap_statustostr(status));
328 status = pcap_set_promisc(pd, 1);
329 if (status != 0)
330 error("%s: pcap_set_promisc failed: %s",
331 device, pcap_statustostr(status));
332 if (dorfmon) {
333 status = pcap_set_rfmon(pd, 1);
334 if (status != 0)
335 error("%s: pcap_set_rfmon failed: %s",
336 device, pcap_statustostr(status));
337 }
338 status = pcap_set_timeout(pd, 1000);
339 if (status != 0)
340 error("%s: pcap_set_timeout failed: %s",
341 device, pcap_statustostr(status));
342 status = pcap_activate(pd);
343 if (status < 0) {
344 /*
345 * pcap_activate() failed.
346 */
347 error("%s: %s\n(%s)", device,
348 pcap_statustostr(status), pcap_geterr(pd));
349 } else if (status > 0) {
350 /*
351 * pcap_activate() succeeded, but it's warning us
352 * of a problem it had.
353 */
354 warning("%s: %s\n(%s)", device,
355 pcap_statustostr(status), pcap_geterr(pd));
356 }
357 } else {
358 *ebuf = '\0';
359 pd = pcap_open_live(device, 65535, 1, 1000, ebuf);
360 if (pd == NULL)
361 error("%s", ebuf);
362 else if (*ebuf)
363 warning("%s", ebuf);
364 }
365
366 pcap_fd = pcap_fileno(pd);
367
368 /*
369 * Try setting a filter with an uninitialized bpf_program
370 * structure. This should cause valgrind to report a
371 * problem.
372 *
373 * We don't check for errors, because it could get an
374 * error due to a bad pointer or count.
375 */
376 #if defined(USE_BPF)
377 ioctl(pcap_fd, BIOCSETF, &bad_fcode);
378 #elif defined(USE_SOCKET_FILTERS)
379 setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
380 sizeof(bad_fcode));
381 #endif
382
383 /*
384 * Try setting a filter with an initialized bpf_program
385 * structure that points to an uninitialized program.
386 * That should also cause valgrind to report a problem.
387 *
388 * We don't check for errors, because it could get an
389 * error due to a bad pointer or count.
390 */
391 #if defined(USE_BPF)
392 bad_fcode.bf_len = INSN_COUNT;
393 bad_fcode.bf_insns = uninitialized;
394 ioctl(pcap_fd, BIOCSETF, &bad_fcode);
395 #elif defined(USE_SOCKET_FILTERS)
396 bad_fcode.len = INSN_COUNT;
397 bad_fcode.filter = uninitialized;
398 setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
399 sizeof(bad_fcode));
400 #endif
401
402 /*
403 * Now compile a filter and set the filter with that.
404 * That should *not* cause valgrind to report a
405 * problem.
406 */
407 if (pcap_compile(pd, &fcode, cmdbuf, 1, 0) < 0)
408 error("can't compile filter: %s", pcap_geterr(pd));
409 if (pcap_setfilter(pd, &fcode) < 0)
410 error("can't set filter: %s", pcap_geterr(pd));
411
412 pcap_close(pd);
413 exit(status < 0 ? 1 : 0);
414 }
415
416 static void
417 usage(void)
418 {
419 (void)fprintf(stderr, "%s, with %s\n", program_name,
420 pcap_lib_version());
421 (void)fprintf(stderr,
422 "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",
423 program_name);
424 exit(1);
425 }