]> The Tcpdump Group git mirrors - libpcap/blobdiff - tests/valgrindtest.c
Clean up the ether_hostton() stuff.
[libpcap] / tests / valgrindtest.c
index f74f8ea29c63699c92c82ba39228b3dd53dbf381..6bb6d4f6d839c6fedc408585f5ab4337f0b9461a 100644 (file)
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
+/*
+ * This doesn't actually test libpcap itself; it tests whether
+ * valgrind properly handles the APIs libpcap uses.  If it doesn't,
+ * we end up getting patches submitted to "fix" references that
+ * valgrind claims are being made to uninitialized data, when, in
+ * fact, the OS isn't making any such references - or we get
+ * valgrind *not* detecting *actual* incorrect references.
+ *
+ * Both BPF and Linux socket filters aren't handled correctly
+ * by some versions of valgrind.  See valgrind bug 318203 for
+ * Linux:
+ *
+ *     https://bugs.kde.org/show_bug.cgi?id=318203
+ *
+ * and valgrind bug 312989 for OS X:
+ *
+ *     https://bugs.kde.org/show_bug.cgi?id=312989
+ *
+ * The fixes for both of those are checked into the official valgrind
+ * repository.
+ *
+ * The unofficial FreeBSD port has similar issues to the official OS X
+ * port, for similar reasons.
+ */
 #ifndef lint
 static const char copyright[] _U_ =
     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
 The Regents of the University of California.  All rights reserved.\n";
-static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/filtertest.c,v 1.2 2005-08-08 17:50:13 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif
 
 #include <stdio.h>
@@ -42,11 +64,14 @@ static const char rcsid[] _U_ =
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include "pcap/funcattrs.h"
+
 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
 /* BSD-flavored OS - use BPF */
 #define USE_BPF
 #elif defined(linux)
 /* Linux - use socket filters */
+#define USE_SOCKET_FILTERS
 #else
 #error "Unknown platform or platform that doesn't support Valgrind"
 #endif
@@ -73,22 +98,13 @@ static const char rcsid[] _U_ =
 #endif
 
 #include <pcap.h>
-#ifndef HAVE___ATTRIBUTE__
-#define __attribute__(x)
-#endif
 
 static char *program_name;
 
 /* Forwards */
-static void usage(void) __attribute__((noreturn));
-static void error(const char *, ...)
-    __attribute__((noreturn, format (printf, 1, 2)));
-static void warning(const char *, ...)
-    __attribute__((format (printf, 1, 2)));
-
-extern int optind;
-extern int opterr;
-extern char *optarg;
+static void PCAP_NORETURN usage(void);
+static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
+static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
 
 /*
  * On Windows, we need to open the file in binary mode, so that
@@ -216,13 +232,14 @@ main(int argc, char **argv)
        char ebuf[PCAP_ERRBUF_SIZE];
        char *infile;
        char *cmdbuf;
+       pcap_if_t *devlist;
        pcap_t *pd;
        int status = 0;
        int pcap_fd;
 #if defined(USE_BPF)
        struct bpf_program bad_fcode;
        struct bpf_insn uninitialized[INSN_COUNT];
-#elif define(USE_SOCKET_FILTERS)
+#elif defined(USE_SOCKET_FILTERS)
        struct sock_fprog bad_fcode;
        struct sock_filter uninitialized[INSN_COUNT];
 #endif
@@ -232,7 +249,7 @@ main(int argc, char **argv)
        dorfmon = 0;
        useactivate = 0;
        infile = NULL;
-  
+
        if ((cp = strrchr(argv[0], '/')) != NULL)
                program_name = cp + 1;
        else
@@ -270,11 +287,12 @@ main(int argc, char **argv)
                 * No interface specified; get whatever pcap_lookupdev()
                 * finds.
                 */
-               device = pcap_lookupdev(ebuf);
-               if (device == NULL) {
-                       error("couldn't find interface to use: %s",
-                           ebuf);
-               }
+               if (pcap_findalldevs(&devlist, ebuf) == -1)
+                       error("%s", ebuf);
+               if (devlist == NULL)
+                       error("no interfaces available for capture");
+               device = strdup(devlist->name);
+               pcap_freealldevs(devlist);
        }
 
        if (infile != NULL) {