157e22627SCy Schubert /*
257e22627SCy Schubert * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
357e22627SCy Schubert * The Regents of the University of California. All rights reserved.
457e22627SCy Schubert *
557e22627SCy Schubert * Redistribution and use in source and binary forms, with or without
657e22627SCy Schubert * modification, are permitted provided that: (1) source code distributions
757e22627SCy Schubert * retain the above copyright notice and this paragraph in its entirety, (2)
857e22627SCy Schubert * distributions including binary code include the above copyright notice and
957e22627SCy Schubert * this paragraph in its entirety in the documentation or other materials
1057e22627SCy Schubert * provided with the distribution, and (3) all advertising materials mentioning
1157e22627SCy Schubert * features or use of this software display the following acknowledgement:
1257e22627SCy Schubert * ``This product includes software developed by the University of California,
1357e22627SCy Schubert * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1457e22627SCy Schubert * the University nor the names of its contributors may be used to endorse
1557e22627SCy Schubert * or promote products derived from this software without specific prior
1657e22627SCy Schubert * written permission.
1757e22627SCy Schubert * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1857e22627SCy Schubert * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1957e22627SCy Schubert * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2057e22627SCy Schubert */
2157e22627SCy Schubert
2257e22627SCy Schubert #include "varattrs.h"
2357e22627SCy Schubert
2457e22627SCy Schubert /*
2557e22627SCy Schubert * This doesn't actually test libpcap itself; it tests whether
2657e22627SCy Schubert * valgrind properly handles the APIs libpcap uses. If it doesn't,
2757e22627SCy Schubert * we end up getting patches submitted to "fix" references that
2857e22627SCy Schubert * valgrind claims are being made to uninitialized data, when, in
2957e22627SCy Schubert * fact, the OS isn't making any such references - or we get
3057e22627SCy Schubert * valgrind *not* detecting *actual* incorrect references.
3157e22627SCy Schubert *
3257e22627SCy Schubert * Both BPF and Linux socket filters aren't handled correctly
3357e22627SCy Schubert * by some versions of valgrind. See valgrind bug 318203 for
3457e22627SCy Schubert * Linux:
3557e22627SCy Schubert *
3657e22627SCy Schubert * https://bugs.kde.org/show_bug.cgi?id=318203
3757e22627SCy Schubert *
3857e22627SCy Schubert * and valgrind bug 312989 for macOS:
3957e22627SCy Schubert *
4057e22627SCy Schubert * https://bugs.kde.org/show_bug.cgi?id=312989
4157e22627SCy Schubert *
4257e22627SCy Schubert * The fixes for both of those are checked into the official valgrind
4357e22627SCy Schubert * repository.
4457e22627SCy Schubert *
4557e22627SCy Schubert * The unofficial FreeBSD port has similar issues to the official macOS
4657e22627SCy Schubert * port, for similar reasons.
4757e22627SCy Schubert */
4857e22627SCy Schubert #ifndef lint
4957e22627SCy Schubert static const char copyright[] _U_ =
5057e22627SCy Schubert "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
5157e22627SCy Schubert The Regents of the University of California. All rights reserved.\n";
5257e22627SCy Schubert #endif
5357e22627SCy Schubert
5457e22627SCy Schubert #include <config.h>
5557e22627SCy Schubert
5657e22627SCy Schubert #include <stdio.h>
5757e22627SCy Schubert #include <stdlib.h>
5857e22627SCy Schubert #include <string.h>
5957e22627SCy Schubert #include <stdarg.h>
606f9cba8fSJoseph Mingrone #include <limits.h>
6157e22627SCy Schubert #include <unistd.h>
6257e22627SCy Schubert #include <fcntl.h>
6357e22627SCy Schubert #include <errno.h>
6457e22627SCy Schubert #include <arpa/inet.h>
6557e22627SCy Schubert #include <sys/types.h>
6657e22627SCy Schubert #include <sys/stat.h>
6757e22627SCy Schubert
6857e22627SCy Schubert #include "pcap/funcattrs.h"
6957e22627SCy Schubert
7057e22627SCy Schubert #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(_AIX) || defined(sun)
7157e22627SCy Schubert /* OS with BPF - use BPF */
7257e22627SCy Schubert #define USE_BPF
73*afdbf109SJoseph Mingrone #elif defined(__linux__)
7457e22627SCy Schubert /* Linux - use socket filters */
7557e22627SCy Schubert #define USE_SOCKET_FILTERS
7657e22627SCy Schubert #else
7757e22627SCy Schubert #error "Unknown platform or platform that doesn't support Valgrind"
7857e22627SCy Schubert #endif
7957e22627SCy Schubert
8057e22627SCy Schubert #if defined(USE_BPF)
8157e22627SCy Schubert
8257e22627SCy Schubert #include <sys/ioctl.h>
8357e22627SCy Schubert #include <net/bpf.h>
8457e22627SCy Schubert
8557e22627SCy Schubert /*
8657e22627SCy Schubert * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
8757e22627SCy Schubert * native OS version, as we're going to be doing our own ioctls to
8857e22627SCy Schubert * make sure that, in the uninitialized-data tests, the filters aren't
8957e22627SCy Schubert * checked by libpcap before being handed to BPF.
9057e22627SCy Schubert */
9157e22627SCy Schubert #define PCAP_DONT_INCLUDE_PCAP_BPF_H
9257e22627SCy Schubert
9357e22627SCy Schubert #elif defined(USE_SOCKET_FILTERS)
9457e22627SCy Schubert
9557e22627SCy Schubert #include <sys/socket.h>
9657e22627SCy Schubert #include <linux/types.h>
9757e22627SCy Schubert #include <linux/filter.h>
9857e22627SCy Schubert
9957e22627SCy Schubert #endif
10057e22627SCy Schubert
1016f9cba8fSJoseph Mingrone /*
1026f9cba8fSJoseph Mingrone * Squelch a warning.
1036f9cba8fSJoseph Mingrone *
1046f9cba8fSJoseph Mingrone * We include system headers to be able to directly set the filter to
1056f9cba8fSJoseph Mingrone * a program with uninitialized content, to make sure what we're testing
1066f9cba8fSJoseph Mingrone * is Valgrind's checking of the system call to set the filter, and we
1076f9cba8fSJoseph Mingrone * also include <pcap.h> to open the device in the first place, and that
1086f9cba8fSJoseph Mingrone * means that we may get collisions between their definitions of
1096f9cba8fSJoseph Mingrone * BPF_STMT and BPF_JUMP - and do, in fact, get them on Linux (the
1106f9cba8fSJoseph Mingrone * definitions may be semantically the same, but that's not sufficient to
1116f9cba8fSJoseph Mingrone * avoid the warnings, as the preprocessor doesn't know that u_short is
1126f9cba8fSJoseph Mingrone * just unsigned short).
1136f9cba8fSJoseph Mingrone *
1146f9cba8fSJoseph Mingrone * So we undefine BPF_STMT and BPF_JUMP to avoid the warning.
1156f9cba8fSJoseph Mingrone */
1166f9cba8fSJoseph Mingrone #undef BPF_STMT
1176f9cba8fSJoseph Mingrone #undef BPF_JUMP
11857e22627SCy Schubert #include <pcap.h>
11957e22627SCy Schubert
12057e22627SCy Schubert static char *program_name;
12157e22627SCy Schubert
12257e22627SCy Schubert /* Forwards */
12357e22627SCy Schubert static void PCAP_NORETURN usage(void);
12457e22627SCy Schubert static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
12557e22627SCy Schubert static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
12657e22627SCy Schubert
12757e22627SCy Schubert /*
12857e22627SCy Schubert * On Windows, we need to open the file in binary mode, so that
12957e22627SCy Schubert * we get all the bytes specified by the size we get from "fstat()".
13057e22627SCy Schubert * On UNIX, that's not necessary. O_BINARY is defined on Windows;
13157e22627SCy Schubert * we define it as 0 if it's not defined, so it does nothing.
13257e22627SCy Schubert */
13357e22627SCy Schubert #ifndef O_BINARY
13457e22627SCy Schubert #define O_BINARY 0
13557e22627SCy Schubert #endif
13657e22627SCy Schubert
13757e22627SCy Schubert static char *
read_infile(char * fname)13857e22627SCy Schubert read_infile(char *fname)
13957e22627SCy Schubert {
14057e22627SCy Schubert register int i, fd, cc;
14157e22627SCy Schubert register char *cp;
14257e22627SCy Schubert struct stat buf;
14357e22627SCy Schubert
14457e22627SCy Schubert fd = open(fname, O_RDONLY|O_BINARY);
14557e22627SCy Schubert if (fd < 0)
14657e22627SCy Schubert error("can't open %s: %s", fname, pcap_strerror(errno));
14757e22627SCy Schubert
14857e22627SCy Schubert if (fstat(fd, &buf) < 0)
14957e22627SCy Schubert error("can't stat %s: %s", fname, pcap_strerror(errno));
15057e22627SCy Schubert
1516f9cba8fSJoseph Mingrone /*
1526f9cba8fSJoseph Mingrone * _read(), on Windows, has an unsigned int byte count and an
1536f9cba8fSJoseph Mingrone * int return value, so we can't handle a file bigger than
1546f9cba8fSJoseph Mingrone * INT_MAX - 1 bytes (and have no reason to do so; a filter *that*
1556f9cba8fSJoseph Mingrone * big will take forever to compile). (The -1 is for the '\0' at
1566f9cba8fSJoseph Mingrone * the end of the string.)
1576f9cba8fSJoseph Mingrone */
1586f9cba8fSJoseph Mingrone if (buf.st_size > INT_MAX - 1)
1596f9cba8fSJoseph Mingrone error("%s is larger than %d bytes; that's too large", fname,
1606f9cba8fSJoseph Mingrone INT_MAX - 1);
16157e22627SCy Schubert cp = malloc((u_int)buf.st_size + 1);
16257e22627SCy Schubert if (cp == NULL)
16357e22627SCy Schubert error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
16457e22627SCy Schubert fname, pcap_strerror(errno));
1656f9cba8fSJoseph Mingrone cc = (int)read(fd, cp, (u_int)buf.st_size);
16657e22627SCy Schubert if (cc < 0)
16757e22627SCy Schubert error("read %s: %s", fname, pcap_strerror(errno));
16857e22627SCy Schubert if (cc != buf.st_size)
16957e22627SCy Schubert error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
17057e22627SCy Schubert
17157e22627SCy Schubert close(fd);
17257e22627SCy Schubert /* replace "# comment" with spaces */
17357e22627SCy Schubert for (i = 0; i < cc; i++) {
17457e22627SCy Schubert if (cp[i] == '#')
17557e22627SCy Schubert while (i < cc && cp[i] != '\n')
17657e22627SCy Schubert cp[i++] = ' ';
17757e22627SCy Schubert }
17857e22627SCy Schubert cp[cc] = '\0';
17957e22627SCy Schubert return (cp);
18057e22627SCy Schubert }
18157e22627SCy Schubert
18257e22627SCy Schubert /* VARARGS */
18357e22627SCy Schubert static void
error(const char * fmt,...)18457e22627SCy Schubert error(const char *fmt, ...)
18557e22627SCy Schubert {
18657e22627SCy Schubert va_list ap;
18757e22627SCy Schubert
18857e22627SCy Schubert (void)fprintf(stderr, "%s: ", program_name);
18957e22627SCy Schubert va_start(ap, fmt);
19057e22627SCy Schubert (void)vfprintf(stderr, fmt, ap);
19157e22627SCy Schubert va_end(ap);
19257e22627SCy Schubert if (*fmt) {
19357e22627SCy Schubert fmt += strlen(fmt);
19457e22627SCy Schubert if (fmt[-1] != '\n')
19557e22627SCy Schubert (void)fputc('\n', stderr);
19657e22627SCy Schubert }
19757e22627SCy Schubert exit(1);
19857e22627SCy Schubert /* NOTREACHED */
19957e22627SCy Schubert }
20057e22627SCy Schubert
20157e22627SCy Schubert /* VARARGS */
20257e22627SCy Schubert static void
warning(const char * fmt,...)20357e22627SCy Schubert warning(const char *fmt, ...)
20457e22627SCy Schubert {
20557e22627SCy Schubert va_list ap;
20657e22627SCy Schubert
20757e22627SCy Schubert (void)fprintf(stderr, "%s: WARNING: ", program_name);
20857e22627SCy Schubert va_start(ap, fmt);
20957e22627SCy Schubert (void)vfprintf(stderr, fmt, ap);
21057e22627SCy Schubert va_end(ap);
21157e22627SCy Schubert if (*fmt) {
21257e22627SCy Schubert fmt += strlen(fmt);
21357e22627SCy Schubert if (fmt[-1] != '\n')
21457e22627SCy Schubert (void)fputc('\n', stderr);
21557e22627SCy Schubert }
21657e22627SCy Schubert }
21757e22627SCy Schubert
21857e22627SCy Schubert /*
21957e22627SCy Schubert * Copy arg vector into a new buffer, concatenating arguments with spaces.
22057e22627SCy Schubert */
22157e22627SCy Schubert static char *
copy_argv(register char ** argv)22257e22627SCy Schubert copy_argv(register char **argv)
22357e22627SCy Schubert {
22457e22627SCy Schubert register char **p;
2256f9cba8fSJoseph Mingrone register size_t len = 0;
22657e22627SCy Schubert char *buf;
22757e22627SCy Schubert char *src, *dst;
22857e22627SCy Schubert
22957e22627SCy Schubert p = argv;
23057e22627SCy Schubert if (*p == 0)
23157e22627SCy Schubert return 0;
23257e22627SCy Schubert
23357e22627SCy Schubert while (*p)
23457e22627SCy Schubert len += strlen(*p++) + 1;
23557e22627SCy Schubert
23657e22627SCy Schubert buf = (char *)malloc(len);
23757e22627SCy Schubert if (buf == NULL)
23857e22627SCy Schubert error("copy_argv: malloc");
23957e22627SCy Schubert
24057e22627SCy Schubert p = argv;
24157e22627SCy Schubert dst = buf;
24257e22627SCy Schubert while ((src = *p++) != NULL) {
24357e22627SCy Schubert while ((*dst++ = *src++) != '\0')
24457e22627SCy Schubert ;
24557e22627SCy Schubert dst[-1] = ' ';
24657e22627SCy Schubert }
24757e22627SCy Schubert dst[-1] = '\0';
24857e22627SCy Schubert
24957e22627SCy Schubert return buf;
25057e22627SCy Schubert }
25157e22627SCy Schubert
25257e22627SCy Schubert #define INSN_COUNT 17
25357e22627SCy Schubert
25457e22627SCy Schubert int
main(int argc,char ** argv)25557e22627SCy Schubert main(int argc, char **argv)
25657e22627SCy Schubert {
25757e22627SCy Schubert char *cp, *device;
25857e22627SCy Schubert int op;
25957e22627SCy Schubert int dorfmon, useactivate;
26057e22627SCy Schubert char ebuf[PCAP_ERRBUF_SIZE];
26157e22627SCy Schubert char *infile;
26257e22627SCy Schubert const char *cmdbuf;
26357e22627SCy Schubert pcap_if_t *devlist;
26457e22627SCy Schubert pcap_t *pd;
26557e22627SCy Schubert int status = 0;
26657e22627SCy Schubert int pcap_fd;
26757e22627SCy Schubert #if defined(USE_BPF)
26857e22627SCy Schubert struct bpf_program bad_fcode;
26957e22627SCy Schubert struct bpf_insn uninitialized[INSN_COUNT];
27057e22627SCy Schubert #elif defined(USE_SOCKET_FILTERS)
27157e22627SCy Schubert struct sock_fprog bad_fcode;
27257e22627SCy Schubert struct sock_filter uninitialized[INSN_COUNT];
27357e22627SCy Schubert #endif
27457e22627SCy Schubert struct bpf_program fcode;
27557e22627SCy Schubert
27657e22627SCy Schubert device = NULL;
27757e22627SCy Schubert dorfmon = 0;
27857e22627SCy Schubert useactivate = 0;
27957e22627SCy Schubert infile = NULL;
28057e22627SCy Schubert
28157e22627SCy Schubert if ((cp = strrchr(argv[0], '/')) != NULL)
28257e22627SCy Schubert program_name = cp + 1;
28357e22627SCy Schubert else
28457e22627SCy Schubert program_name = argv[0];
28557e22627SCy Schubert
28657e22627SCy Schubert opterr = 0;
28757e22627SCy Schubert while ((op = getopt(argc, argv, "aF:i:I")) != -1) {
28857e22627SCy Schubert switch (op) {
28957e22627SCy Schubert
29057e22627SCy Schubert case 'a':
29157e22627SCy Schubert useactivate = 1;
29257e22627SCy Schubert break;
29357e22627SCy Schubert
29457e22627SCy Schubert case 'F':
29557e22627SCy Schubert infile = optarg;
29657e22627SCy Schubert break;
29757e22627SCy Schubert
29857e22627SCy Schubert case 'i':
29957e22627SCy Schubert device = optarg;
30057e22627SCy Schubert break;
30157e22627SCy Schubert
30257e22627SCy Schubert case 'I':
30357e22627SCy Schubert dorfmon = 1;
30457e22627SCy Schubert useactivate = 1; /* required for rfmon */
30557e22627SCy Schubert break;
30657e22627SCy Schubert
30757e22627SCy Schubert default:
30857e22627SCy Schubert usage();
30957e22627SCy Schubert /* NOTREACHED */
31057e22627SCy Schubert }
31157e22627SCy Schubert }
31257e22627SCy Schubert
31357e22627SCy Schubert if (device == NULL) {
31457e22627SCy Schubert /*
31557e22627SCy Schubert * No interface specified; get whatever pcap_lookupdev()
31657e22627SCy Schubert * finds.
31757e22627SCy Schubert */
31857e22627SCy Schubert if (pcap_findalldevs(&devlist, ebuf) == -1)
31957e22627SCy Schubert error("%s", ebuf);
32057e22627SCy Schubert if (devlist == NULL)
32157e22627SCy Schubert error("no interfaces available for capture");
32257e22627SCy Schubert device = strdup(devlist->name);
32357e22627SCy Schubert pcap_freealldevs(devlist);
32457e22627SCy Schubert }
32557e22627SCy Schubert
32657e22627SCy Schubert if (infile != NULL) {
32757e22627SCy Schubert /*
32857e22627SCy Schubert * Filter specified with "-F" and a file containing
32957e22627SCy Schubert * a filter.
33057e22627SCy Schubert */
33157e22627SCy Schubert cmdbuf = read_infile(infile);
33257e22627SCy Schubert } else {
33357e22627SCy Schubert if (optind < argc) {
33457e22627SCy Schubert /*
33557e22627SCy Schubert * Filter specified with arguments on the
33657e22627SCy Schubert * command line.
33757e22627SCy Schubert */
33857e22627SCy Schubert cmdbuf = copy_argv(&argv[optind+1]);
33957e22627SCy Schubert } else {
34057e22627SCy Schubert /*
34157e22627SCy Schubert * No filter specified; use an empty string, which
34257e22627SCy Schubert * compiles to an "accept all" filter.
34357e22627SCy Schubert */
34457e22627SCy Schubert cmdbuf = "";
34557e22627SCy Schubert }
34657e22627SCy Schubert }
34757e22627SCy Schubert
34857e22627SCy Schubert if (useactivate) {
34957e22627SCy Schubert pd = pcap_create(device, ebuf);
35057e22627SCy Schubert if (pd == NULL)
35157e22627SCy Schubert error("%s: pcap_create() failed: %s", device, ebuf);
35257e22627SCy Schubert status = pcap_set_snaplen(pd, 65535);
35357e22627SCy Schubert if (status != 0)
35457e22627SCy Schubert error("%s: pcap_set_snaplen failed: %s",
35557e22627SCy Schubert device, pcap_statustostr(status));
35657e22627SCy Schubert status = pcap_set_promisc(pd, 1);
35757e22627SCy Schubert if (status != 0)
35857e22627SCy Schubert error("%s: pcap_set_promisc failed: %s",
35957e22627SCy Schubert device, pcap_statustostr(status));
36057e22627SCy Schubert if (dorfmon) {
36157e22627SCy Schubert status = pcap_set_rfmon(pd, 1);
36257e22627SCy Schubert if (status != 0)
36357e22627SCy Schubert error("%s: pcap_set_rfmon failed: %s",
36457e22627SCy Schubert device, pcap_statustostr(status));
36557e22627SCy Schubert }
36657e22627SCy Schubert status = pcap_set_timeout(pd, 1000);
36757e22627SCy Schubert if (status != 0)
36857e22627SCy Schubert error("%s: pcap_set_timeout failed: %s",
36957e22627SCy Schubert device, pcap_statustostr(status));
37057e22627SCy Schubert status = pcap_activate(pd);
37157e22627SCy Schubert if (status < 0) {
37257e22627SCy Schubert /*
37357e22627SCy Schubert * pcap_activate() failed.
37457e22627SCy Schubert */
37557e22627SCy Schubert error("%s: %s\n(%s)", device,
37657e22627SCy Schubert pcap_statustostr(status), pcap_geterr(pd));
37757e22627SCy Schubert } else if (status > 0) {
37857e22627SCy Schubert /*
37957e22627SCy Schubert * pcap_activate() succeeded, but it's warning us
38057e22627SCy Schubert * of a problem it had.
38157e22627SCy Schubert */
38257e22627SCy Schubert warning("%s: %s\n(%s)", device,
38357e22627SCy Schubert pcap_statustostr(status), pcap_geterr(pd));
38457e22627SCy Schubert }
38557e22627SCy Schubert } else {
38657e22627SCy Schubert *ebuf = '\0';
38757e22627SCy Schubert pd = pcap_open_live(device, 65535, 1, 1000, ebuf);
38857e22627SCy Schubert if (pd == NULL)
38957e22627SCy Schubert error("%s", ebuf);
39057e22627SCy Schubert else if (*ebuf)
39157e22627SCy Schubert warning("%s", ebuf);
39257e22627SCy Schubert }
39357e22627SCy Schubert
39457e22627SCy Schubert pcap_fd = pcap_fileno(pd);
39557e22627SCy Schubert
39657e22627SCy Schubert /*
39757e22627SCy Schubert * Try setting a filter with an uninitialized bpf_program
39857e22627SCy Schubert * structure. This should cause valgrind to report a
39957e22627SCy Schubert * problem.
40057e22627SCy Schubert *
40157e22627SCy Schubert * We don't check for errors, because it could get an
40257e22627SCy Schubert * error due to a bad pointer or count.
40357e22627SCy Schubert */
40457e22627SCy Schubert #if defined(USE_BPF)
40557e22627SCy Schubert ioctl(pcap_fd, BIOCSETF, &bad_fcode);
40657e22627SCy Schubert #elif defined(USE_SOCKET_FILTERS)
40757e22627SCy Schubert setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
40857e22627SCy Schubert sizeof(bad_fcode));
40957e22627SCy Schubert #endif
41057e22627SCy Schubert
41157e22627SCy Schubert /*
41257e22627SCy Schubert * Try setting a filter with an initialized bpf_program
41357e22627SCy Schubert * structure that points to an uninitialized program.
41457e22627SCy Schubert * That should also cause valgrind to report a problem.
41557e22627SCy Schubert *
41657e22627SCy Schubert * We don't check for errors, because it could get an
41757e22627SCy Schubert * error due to a bad pointer or count.
41857e22627SCy Schubert */
41957e22627SCy Schubert #if defined(USE_BPF)
42057e22627SCy Schubert bad_fcode.bf_len = INSN_COUNT;
42157e22627SCy Schubert bad_fcode.bf_insns = uninitialized;
42257e22627SCy Schubert ioctl(pcap_fd, BIOCSETF, &bad_fcode);
42357e22627SCy Schubert #elif defined(USE_SOCKET_FILTERS)
42457e22627SCy Schubert bad_fcode.len = INSN_COUNT;
42557e22627SCy Schubert bad_fcode.filter = uninitialized;
42657e22627SCy Schubert setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
42757e22627SCy Schubert sizeof(bad_fcode));
42857e22627SCy Schubert #endif
42957e22627SCy Schubert
43057e22627SCy Schubert /*
43157e22627SCy Schubert * Now compile a filter and set the filter with that.
43257e22627SCy Schubert * That should *not* cause valgrind to report a
43357e22627SCy Schubert * problem.
43457e22627SCy Schubert */
43557e22627SCy Schubert if (pcap_compile(pd, &fcode, cmdbuf, 1, 0) < 0)
43657e22627SCy Schubert error("can't compile filter: %s", pcap_geterr(pd));
43757e22627SCy Schubert if (pcap_setfilter(pd, &fcode) < 0)
43857e22627SCy Schubert error("can't set filter: %s", pcap_geterr(pd));
43957e22627SCy Schubert
44057e22627SCy Schubert pcap_close(pd);
44157e22627SCy Schubert exit(status < 0 ? 1 : 0);
44257e22627SCy Schubert }
44357e22627SCy Schubert
44457e22627SCy Schubert static void
usage(void)44557e22627SCy Schubert usage(void)
44657e22627SCy Schubert {
44757e22627SCy Schubert (void)fprintf(stderr, "%s, with %s\n", program_name,
44857e22627SCy Schubert pcap_lib_version());
44957e22627SCy Schubert (void)fprintf(stderr,
4506f9cba8fSJoseph Mingrone "Usage: %s [-aI] [ -F file ] [ -i interface ] [ expression ]\n",
45157e22627SCy Schubert program_name);
45257e22627SCy Schubert exit(1);
45357e22627SCy Schubert }
454