]> The Tcpdump Group git mirrors - libpcap/commitdiff
Add a "setfilter" function pointer to the pcap_t structure, which
authorguy <guy>
Fri, 25 Jul 2003 04:42:02 +0000 (04:42 +0000)
committerguy <guy>
Fri, 25 Jul 2003 04:42:02 +0000 (04:42 +0000)
handles setting a filter for a pcap_t.  Have "pcap_setfilter()" call it,
rather than being a per-platform function.  The per-platform functions
don't need to check for an offline capture any more, as they're not
called for an offline capture (and the ones that just call
"install_bpf_program()" don't need to exist at all).

13 files changed:
pcap-bpf.c
pcap-dag.c
pcap-dlpi.c
pcap-int.h
pcap-linux.c
pcap-nit.c
pcap-null.c
pcap-pf.c
pcap-snit.c
pcap-snoop.c
pcap-win32.c
pcap.c
savefile.c

index 8a4a8dc4527c1ed8fe3217d44c7411a12325eaa1..8b4091c4ee0ccb906290d122431fdc94a25a2287 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.63 2003-07-25 04:04:56 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.64 2003-07-25 04:42:02 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -101,7 +101,9 @@ static int odmlockid = 0;
 #include "os-proto.h"
 #endif
 
-#include "gencode.h"
+#include "gencode.h"   /* for "no_optimize" */
+
+static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
 
 static int
 pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
@@ -730,6 +732,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        memset(p->buffer, 0x0, p->bufsize);
 #endif
 
+       p->setfilter_op = pcap_setfilter_bpf;
        p->stats_op = pcap_stats_bpf;
        p->close_op = pcap_close_bpf;
 
@@ -755,15 +758,9 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        return (0);
 }
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
+static int
+pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
 {
-#ifdef HAVE_DAG_API
-       if (p->md.is_dag) {
-               return dag_setfilter(p, fp);
-       }
-#endif /* HAVE_DAG_API */
-
        /*
         * It looks that BPF code generated by gen_protochain() is not
         * compatible with some of kernel BPF code (for example BSD/OS 3.1).
@@ -772,9 +769,6 @@ pcap_setfilter(pcap_t *p, struct bpf_program *fp)
        if (no_optimize) {
                if (install_bpf_program(p, fp) < 0)
                        return (-1);
-       } else if (p->sf.rfile != NULL) {
-               if (install_bpf_program(p, fp) < 0)
-                       return (-1);
        } else if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
                    pcap_strerror(errno));
index c03625e22c25e2de4080fffd769199c5ae5c9bd2..1d0243b9e3ed6afc662a3182875539f4c86c1c1e 100644 (file)
@@ -19,7 +19,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.3 2003-07-25 04:04:57 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.4 2003-07-25 04:42:02 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -59,14 +59,13 @@ static int atexit_handler_installed = 0;
 #include "pcap-dag.h"
 
 /* Replace dag function names with pcap equivalent. */
-#define dag_stats pcap_stats
 #define dag_read pcap_read
 #define dag_open_live pcap_open_live
 #define dag_platform_finddevs pcap_platform_finddevs
-#define dag_setfilter pcap_setfilter
 #define dag_set_datalink_platform pcap_set_datalink_platform
 #endif /* DAG_ONLY */
 
+static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
 
 static void delete_pcap_dag(pcap_t *p) {
@@ -387,6 +386,7 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c
     return NULL;
   }
 
+  handle->setfilter_op = dag_setfilter;
   handle->stats_op = dag_stats;
   handle->close_op = dag_platform_close;
 
@@ -507,7 +507,7 @@ dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
  * no attempt to store the filter in kernel memory as that is not supported
  * with DAG cards.
  */
-int dag_setfilter(pcap_t *p, struct bpf_program *fp) {
+static int dag_setfilter(pcap_t *p, struct bpf_program *fp) {
   if (!p)
     return -1;
   if (!fp) {
index 5b5af5651a77cd286c9990afb9b702c38afcd329..1d3ae022f71082bc2d81004acdf9eaaccc0f58b4 100644 (file)
@@ -38,7 +38,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.87 2003-07-25 04:04:57 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.88 2003-07-25 04:42:02 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -672,6 +672,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                goto bad;
        }
 
+       p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->stats_op = pcap_stats_dlpi;
        p->close_op = pcap_close_dlpi;
 
@@ -764,15 +765,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        return (0);
 }
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
-{
-
-       if (install_bpf_program(p, fp) < 0)
-               return (-1);
-       return (0);
-}
-
 static int
 send_request(int fd, char *ptr, int len, char *what, char *ebuf)
 {
index 355e5036e60f1b1671bfc6dcfc8c2051efafb2c9..d346250247f65902aaf864eccaf5f3f3c18521b0 100644 (file)
@@ -30,7 +30,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.49 2003-07-25 04:04:57 guy Exp $ (LBL)
+ * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.50 2003-07-25 04:42:03 guy Exp $ (LBL)
  */
 
 #ifndef pcap_int_h
@@ -118,6 +118,7 @@ struct pcap {
        /*
         * Methods.
         */
+       int     (*setfilter_op)(pcap_t *, struct bpf_program *);
        int     (*stats_op)(pcap_t *, struct pcap_stat *);
        void    (*close_op)(pcap_t *);
 
index 5925c7daf8ab41d6a7e55b2c776f0c866aace6c6..f838a92d33fd2159a2f9e2262f9f9091e3647d03 100644 (file)
@@ -27,7 +27,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.92 2003-07-25 04:04:58 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.93 2003-07-25 04:42:03 guy Exp $ (LBL)";
 #endif
 
 /*
@@ -188,6 +188,7 @@ static int live_open_old(pcap_t *, const char *, int, int, char *);
 static int live_open_new(pcap_t *, const char *, int, int, char *);
 static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
 static int pcap_stats_linux(pcap_t *, struct pcap_stat *);
+static int pcap_setfilter_linux(pcap_t *, struct bpf_program *);
 static void pcap_close_linux(pcap_t *);
 
 /*
@@ -395,6 +396,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                return NULL;
        }
 
+       handle->setfilter_op = pcap_setfilter_linux;
        handle->stats_op = pcap_stats_linux;
        handle->close_op = pcap_close_linux;
 
@@ -773,8 +775,8 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
 /*
  *  Attach the given BPF code to the packet capture device.
  */
-int
-pcap_setfilter(pcap_t *handle, struct bpf_program *filter)
+static int
+pcap_setfilter_linux(pcap_t *handle, struct bpf_program *filter)
 {
 #ifdef SO_ATTACH_FILTER
        struct sock_fprog       fcode;
@@ -782,12 +784,6 @@ pcap_setfilter(pcap_t *handle, struct bpf_program *filter)
        int                     err = 0;
 #endif
 
-#ifdef HAVE_DAG_API
-       if (handle->md.is_dag) {
-               return dag_setfilter(handle, filter);
-       }
-#endif /* HAVE_DAG_API */
-
        if (!handle)
                return -1;
        if (!filter) {
@@ -808,13 +804,6 @@ pcap_setfilter(pcap_t *handle, struct bpf_program *filter)
         */
        handle->md.use_bpf = 0;
 
-       /*
-        * If we're reading from a savefile, don't try to install
-        * a kernel filter.
-        */
-       if (handle->sf.rfile != NULL)
-               return 0;
-
        /* Install kernel level filter if possible */
 
 #ifdef SO_ATTACH_FILTER
index 2be8e79ad7b1e8c6cb75f2e286f0938e729dbe2d..883fc9f3c3f013a29049d01ccc4192acecd307a2 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.46 2003-07-25 04:04:58 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.47 2003-07-25 04:42:03 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -260,6 +260,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                goto bad;
        }
 
+       p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->stats_op = pcap_stats_nit;
        p->close_op = pcap_close_nit;
 
@@ -277,15 +278,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        return (0);
 }
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
-{
-
-       if (install_bpf_program(p, fp) < 0)
-               return (-1);
-       return (0);
-}
-
 int
 pcap_set_datalink_platform(pcap_t *p, int dlt)
 {
index 67f2a6046da3ddac61653c56ae106b7abcc3cc89..6f6788d96e3bf151278e4cbafd5956de10fc9ba7 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-null.c,v 1.17 2003-07-25 04:04:58 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-null.c,v 1.18 2003-07-25 04:42:03 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -60,19 +60,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        return (0);
 }
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
-{
-       if (p->sf.rfile == NULL) {
-               (void)snprintf(p->errbuf, sizeof(p->errbuf),
-                   "pcap_setfilter: %s", nosup);
-               return (-1);
-       }
-       if (install_bpf_program(p, fp) < 0)
-               return (-1);
-       return (0);
-}
-
 int
 pcap_set_datalink_platform(pcap_t *p, int dlt)
 {
index 8669df8ccf3cac3004df05079436d60b031af5e8..9d6e26acc250027f41a47743b3b8011645753089 100644 (file)
--- a/pcap-pf.c
+++ b/pcap-pf.c
@@ -24,7 +24,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.75 2003-07-25 04:04:59 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.76 2003-07-25 04:42:04 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -74,6 +74,8 @@ struct rtentry;
 #include "os-proto.h"
 #endif
 
+static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
+
 /*
  * BUFSPACE is the size in bytes of the packet read buffer.  Most tcpdump
  * applications aren't going to need more than 200 bytes of packet header
@@ -413,6 +415,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n",
                goto bad;
        }
 
+       p->setfilter_op = pcap_setfilter_pf;
        p->stats_op = pcap_stats_pf;
        p->close_op = pcap_close_pf;
 
@@ -430,8 +433,8 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        return (0);
 }
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
+static int
+pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
 {
        /*
         * See if BIOCSETF works.  If it does, the kernel supports
index f3280bd7dac9fa0047ae3d1b7bc77f39a840d70c..c05ebbbe20d4c48d7626ce7f810d1303c49037c4 100644 (file)
@@ -25,7 +25,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.62 2003-07-25 04:04:59 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.63 2003-07-25 04:42:04 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -318,6 +318,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                goto bad;
        }
 
+       p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->stats_op = pcap_stats_snit;
        p->close_op = pcap_close_snit;
 
@@ -335,15 +336,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        return (0);
 }
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
-{
-
-       if (install_bpf_program(p, fp) < 0)
-               return (-1);
-       return (0);
-}
-
 int
 pcap_set_datalink_platform(pcap_t *p, int dlt)
 {
index ef2fbc5f0f07bf044832ebffccefcdd71f3ffde0..d2a8c4866d889dda06531083c8c670d974b78a06 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.41 2003-07-25 04:04:59 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.42 2003-07-25 04:42:04 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -294,6 +294,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                goto bad;
        }
 
+       p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->stats_op = pcap_stats_snoop;
        p->close_op = pcap_close_snoop;
 
@@ -310,15 +311,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
        return (0);
 }
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
-{
-
-       if (install_bpf_program(p, fp) < 0)
-               return (-1);
-       return (0);
-}
-
 int
 pcap_set_datalink_platform(pcap_t *p, int dlt)
 {
index c71e86c35f6770cd08e073e1f59a2d1b5101ce5f..da20526544970ce4a45fd3f32057fa879b9cc34e 100644 (file)
@@ -32,7 +32,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.10 2003-07-25 04:04:59 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.11 2003-07-25 04:42:04 guy Exp $ (LBL)";
 #endif
 
 #include <pcap-int.h>
@@ -43,6 +43,8 @@ int* _errno();
 #define errno (*_errno())
 #endif /* __MINGW32__ */
 
+static int pcap_setfilter_win32(pcap_t *, struct bpf_program *);
+
 #define        PcapBufSize 256000      /*dimension of the buffer in the pcap_t structure*/
 #define        SIZE_BUF 1000000
 
@@ -254,6 +256,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
 
        PacketSetReadTimeout(p->adapter, to_ms);
 
+       p->setfilter_op = pcap_setfilter_win32;
        p->stats_op = pcap_stats_win32;
        p->close_op = pcap_close_win32;
 
@@ -268,15 +271,10 @@ bad:
 }
 
 
-int
-pcap_setfilter(pcap_t *p, struct bpf_program *fp)
+static int
+pcap_setfilter_win32(pcap_t *p, struct bpf_program *fp)
 {
-       if(p->adapter==NULL){
-               /* Offline capture: make our own copy of the filter */
-               if (install_bpf_program(p, fp) < 0)
-                       return (-1);
-       }
-       else if(PacketSetBpf(p->adapter,fp)==FALSE){
+       if(PacketSetBpf(p->adapter,fp)==FALSE){
                /* kernel filter not installed. */
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Driver error: cannot set bpf filter: %s", pcap_win32strerror());
                return (-1);
diff --git a/pcap.c b/pcap.c
index a0aeb36789f9d4c511f8c0d431b411274f96e7a9..51e3fe5fdf2f84ba1758939a4de880452abde520 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -33,7 +33,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.58 2003-07-25 04:05:00 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.59 2003-07-25 04:42:04 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -610,6 +610,12 @@ pcap_strerror(int errnum)
 #endif
 }
 
+int
+pcap_setfilter(pcap_t *p, struct bpf_program *fp)
+{
+       return p->setfilter_op(p, fp);
+}
+
 int
 pcap_stats(pcap_t *p, struct pcap_stat *ps)
 {
index 9b9baf83035635ee2585f8e7332b0e466667e707..53cc2157678800afc36e056e35613ef508fff8aa 100644 (file)
@@ -30,7 +30,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.83 2003-07-25 04:05:00 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.84 2003-07-25 04:42:05 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -532,6 +532,7 @@ pcap_open_offline(const char *fname, char *errbuf)
        pcap_fddipad = 0;
 #endif
 
+       p->setfilter_op = install_bpf_program;
        p->stats_op = sf_stats;
        p->close_op = sf_close;