diff options
author | guy <guy> | 2003-07-25 04:42:02 +0000 |
---|---|---|
committer | guy <guy> | 2003-07-25 04:42:02 +0000 |
commit | cd2807e08d02de68b766ed88acf3bfd3b54b2a24 (patch) | |
tree | 32510d11551eab43465b88894a38d7b627eb1413 | |
parent | 9792990eb80889004b39ee70caa98b3d91ac8012 (diff) | |
download | libpcap-cd2807e08d02de68b766ed88acf3bfd3b54b2a24.tar.gz |
Add a "setfilter" function pointer to the pcap_t structure, which
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).
-rw-r--r-- | pcap-bpf.c | 20 | ||||
-rw-r--r-- | pcap-dag.c | 8 | ||||
-rw-r--r-- | pcap-dlpi.c | 12 | ||||
-rw-r--r-- | pcap-int.h | 3 | ||||
-rw-r--r-- | pcap-linux.c | 21 | ||||
-rw-r--r-- | pcap-nit.c | 12 | ||||
-rw-r--r-- | pcap-null.c | 15 | ||||
-rw-r--r-- | pcap-pf.c | 9 | ||||
-rw-r--r-- | pcap-snit.c | 12 | ||||
-rw-r--r-- | pcap-snoop.c | 12 | ||||
-rw-r--r-- | pcap-win32.c | 16 | ||||
-rw-r--r-- | pcap.c | 8 | ||||
-rw-r--r-- | savefile.c | 3 |
13 files changed, 49 insertions, 102 deletions
@@ -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)); @@ -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) { diff --git a/pcap-dlpi.c b/pcap-dlpi.c index 5b5af565..1d3ae022 100644 --- a/pcap-dlpi.c +++ b/pcap-dlpi.c @@ -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) { @@ -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 *); diff --git a/pcap-linux.c b/pcap-linux.c index 5925c7da..f838a92d 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -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 @@ -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; @@ -278,15 +279,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) } 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) { return (0); diff --git a/pcap-null.c b/pcap-null.c index 67f2a604..6f6788d9 100644 --- a/pcap-null.c +++ b/pcap-null.c @@ -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 @@ -61,19 +61,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) } 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) { return (0); @@ -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 diff --git a/pcap-snit.c b/pcap-snit.c index f3280bd7..c05ebbbe 100644 --- a/pcap-snit.c +++ b/pcap-snit.c @@ -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; @@ -336,15 +337,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) } 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) { return (0); diff --git a/pcap-snoop.c b/pcap-snoop.c index ef2fbc5f..d2a8c486 100644 --- a/pcap-snoop.c +++ b/pcap-snoop.c @@ -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; @@ -311,15 +312,6 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) } 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) { return (0); diff --git a/pcap-win32.c b/pcap-win32.c index c71e86c3..da205265 100644 --- a/pcap-win32.c +++ b/pcap-win32.c @@ -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); @@ -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 @@ -611,6 +611,12 @@ pcap_strerror(int errnum) } 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) { return p->stats_op(p, ps); @@ -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; |