From 9792990eb80889004b39ee70caa98b3d91ac8012 Mon Sep 17 00:00:00 2001 From: guy Date: Fri, 25 Jul 2003 04:04:56 +0000 Subject: [PATCH] Add a "stats" function pointer to the pcap_t structure, which handles getting statistics for a pcap_t. Have "pcap_stats()" call it, rather than being a per-platform function; have stats routines for non-live pcap_t's that return an error. --- pcap-bpf.c | 13 ++++--------- pcap-dag.c | 7 +++++-- pcap-dag.h | 4 +--- pcap-dlpi.c | 7 ++++--- pcap-int.h | 3 ++- pcap-linux.c | 16 ++++++---------- pcap-nit.c | 7 ++++--- pcap-null.c | 9 +-------- pcap-pf.c | 7 ++++--- pcap-snit.c | 7 ++++--- pcap-snoop.c | 7 ++++--- pcap-win32.c | 7 ++++--- pcap.c | 17 ++++++++++++++++- savefile.c | 11 ++++++++++- 14 files changed, 69 insertions(+), 53 deletions(-) diff --git a/pcap-bpf.c b/pcap-bpf.c index 1af0d8f..8a4a8dc 100644 --- a/pcap-bpf.c +++ b/pcap-bpf.c @@ -20,7 +20,7 @@ */ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.62 2003-07-25 03:25:45 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.63 2003-07-25 04:04:56 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -103,17 +103,11 @@ static int odmlockid = 0; #include "gencode.h" -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) +static int +pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps) { struct bpf_stat s; -#ifdef HAVE_DAG_API - if (p->md.is_dag) { - return dag_stats(p, ps); - } -#endif /* HAVE_DAG_API */ - /* * "ps_recv" counts packets handed to the filter, not packets * that passed the filter. This includes packets later dropped @@ -736,6 +730,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, memset(p->buffer, 0x0, p->bufsize); #endif + p->stats_op = pcap_stats_bpf; p->close_op = pcap_close_bpf; return (p); diff --git a/pcap-dag.c b/pcap-dag.c index 9c5bb41..c03625e 100644 --- a/pcap-dag.c +++ b/pcap-dag.c @@ -19,7 +19,7 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.2 2003-07-25 03:25:45 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.3 2003-07-25 04:04:57 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -67,6 +67,8 @@ static int atexit_handler_installed = 0; #define dag_set_datalink_platform pcap_set_datalink_platform #endif /* DAG_ONLY */ +static int dag_stats(pcap_t *p, struct pcap_stat *ps); + static void delete_pcap_dag(pcap_t *p) { pcap_dag_node_t *curr = NULL, *prev = NULL; @@ -385,12 +387,13 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c return NULL; } + handle->stats_op = dag_stats; handle->close_op = dag_platform_close; return handle; } -int dag_stats(pcap_t *p, struct pcap_stat *ps) { +static int dag_stats(pcap_t *p, struct pcap_stat *ps) { /* This needs to be filled out correctly. Hopefully a dagapi call will provide all necessary information. */ diff --git a/pcap-dag.h b/pcap-dag.h index b486907..0776f59 100644 --- a/pcap-dag.h +++ b/pcap-dag.h @@ -7,12 +7,10 @@ * * Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com) * - * @(#) $Header: /tcpdump/master/libpcap/pcap-dag.h,v 1.1 2003-07-23 05:29:21 guy Exp $ (LBL) + * @(#) $Header: /tcpdump/master/libpcap/pcap-dag.h,v 1.2 2003-07-25 04:04:57 guy Exp $ (LBL) */ -int dag_stats(pcap_t *p, struct pcap_stat *ps); int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user); pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf); int dag_setfilter(pcap_t *p, struct bpf_program *fp); -void dag_platform_close(pcap_t *p); diff --git a/pcap-dlpi.c b/pcap-dlpi.c index c5b0062..5b5af56 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.86 2003-07-25 03:25:45 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.87 2003-07-25 04:04:57 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -151,8 +151,8 @@ static int dlpi_kread(int, off_t, void *, u_int, char *); static int get_dlpi_ppa(int, const char *, int, char *); #endif -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) +static int +pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps) { /* @@ -672,6 +672,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->stats_op = pcap_stats_dlpi; p->close_op = pcap_close_dlpi; return (p); diff --git a/pcap-int.h b/pcap-int.h index 23363cd..355e503 100644 --- a/pcap-int.h +++ b/pcap-int.h @@ -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.48 2003-07-25 03:25:46 guy Exp $ (LBL) + * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.49 2003-07-25 04:04:57 guy Exp $ (LBL) */ #ifndef pcap_int_h @@ -118,6 +118,7 @@ struct pcap { /* * Methods. */ + int (*stats_op)(pcap_t *, struct pcap_stat *); void (*close_op)(pcap_t *); /* diff --git a/pcap-linux.c b/pcap-linux.c index e03070b..5925c7d 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.91 2003-07-25 03:25:46 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.92 2003-07-25 04:04:58 guy Exp $ (LBL)"; #endif /* @@ -187,7 +187,8 @@ static void map_arphrd_to_dlt(pcap_t *, int, int); 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 void pcap_close_linux(pcap_t *handle); +static int pcap_stats_linux(pcap_t *, struct pcap_stat *); +static void pcap_close_linux(pcap_t *); /* * Wrap some ioctl calls @@ -394,6 +395,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, return NULL; } + handle->stats_op = pcap_stats_linux; handle->close_op = pcap_close_linux; return handle; @@ -659,20 +661,14 @@ pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata) * patches); otherwise, that information isn't available, and we lie * and report 0 as the count of dropped packets. */ -int -pcap_stats(pcap_t *handle, struct pcap_stat *stats) +static int +pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats) { #ifdef HAVE_TPACKET_STATS struct tpacket_stats kstats; socklen_t len = sizeof (struct tpacket_stats); #endif -#ifdef HAVE_DAG_API - if (handle->md.is_dag) { - return dag_stats(handle, stats); - } -#endif /* HAVE_DAG_API */ - #ifdef HAVE_TPACKET_STATS /* * Try to get the packet counts from the kernel. diff --git a/pcap-nit.c b/pcap-nit.c index 1c0117f..2be8e79 100644 --- a/pcap-nit.c +++ b/pcap-nit.c @@ -20,7 +20,7 @@ */ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.45 2003-07-25 03:25:46 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.46 2003-07-25 04:04:58 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -71,8 +71,8 @@ static const char rcsid[] = /* Forwards */ static int nit_setflags(int, int, int, char *); -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) +static int +pcap_stats_nit(pcap_t *p, struct pcap_stat *ps) { /* @@ -260,6 +260,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->stats_op = pcap_stats_nit; p->close_op = pcap_close_nit; return (p); diff --git a/pcap-null.c b/pcap-null.c index 6be5ab4..67f2a60 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.16 2002-12-22 02:36:49 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-null.c,v 1.17 2003-07-25 04:04:58 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -39,13 +39,6 @@ static const char rcsid[] = static char nosup[] = "live packet capture not supported on this system"; -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) -{ - (void)snprintf(p->errbuf, sizeof(p->errbuf), "pcap_stats: %s", nosup); - return (-1); -} - int pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { diff --git a/pcap-pf.c b/pcap-pf.c index 3fa123e..8669df8 100644 --- 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.74 2003-07-25 03:25:47 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.75 2003-07-25 04:04:59 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -198,8 +198,8 @@ pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) return (n); } -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) +static int +pcap_stats_pf(pcap_t *p, struct pcap_stat *ps) { /* @@ -413,6 +413,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n", goto bad; } + p->stats_op = pcap_stats_pf; p->close_op = pcap_close_pf; return (p); diff --git a/pcap-snit.c b/pcap-snit.c index 3426c0d..f3280bd 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.61 2003-07-25 03:25:47 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.62 2003-07-25 04:04:59 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -84,8 +84,8 @@ static const char rcsid[] = /* Forwards */ static int nit_setflags(int, int, int, char *); -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) +static int +pcap_stats_snit(pcap_t *p, struct pcap_stat *ps) { /* @@ -318,6 +318,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->stats_op = pcap_stats_snit; p->close_op = pcap_close_snit; return (p); diff --git a/pcap-snoop.c b/pcap-snoop.c index 83cb6d2..ef2fbc5 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.40 2003-07-25 03:25:47 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.41 2003-07-25 04:04:59 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -102,8 +102,8 @@ again: return (0); } -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) +static int +pcap_stats_snoop(pcap_t *p, struct pcap_stat *ps) { register struct rawstats *rs; struct rawstats rawstats; @@ -294,6 +294,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->stats_op = pcap_stats_snoop; p->close_op = pcap_close_snoop; return (p); diff --git a/pcap-win32.c b/pcap-win32.c index 8722517..c71e86c 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.9 2003-07-25 03:25:47 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.10 2003-07-25 04:04:59 guy Exp $ (LBL)"; #endif #include @@ -75,8 +75,8 @@ wsockinit() } -int -pcap_stats(pcap_t *p, struct pcap_stat *ps) +static int +pcap_stats_win32(pcap_t *p, struct pcap_stat *ps) { if(PacketGetStats(p->adapter, (struct bpf_stat*)ps) != TRUE){ @@ -254,6 +254,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, PacketSetReadTimeout(p->adapter, to_ms); + p->stats_op = pcap_stats_win32; p->close_op = pcap_close_win32; return (p); diff --git a/pcap.c b/pcap.c index 0b483b5..a0aeb36 100644 --- a/pcap.c +++ b/pcap.c @@ -33,7 +33,7 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.57 2003-07-25 03:25:48 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.58 2003-07-25 04:05:00 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -610,6 +610,20 @@ pcap_strerror(int errnum) #endif } +int +pcap_stats(pcap_t *p, struct pcap_stat *ps) +{ + return p->stats_op(p, ps); +} + +static int +pcap_stats_dead(pcap_t *p, struct pcap_stat *ps) +{ + snprintf(p->errbuf, PCAP_ERRBUF_SIZE, + "Statistics aren't available from a pcap_open_dead pcap_t"); + return (-1); +} + static void pcap_close_dead(pcap_t *p) { @@ -627,6 +641,7 @@ pcap_open_dead(int linktype, int snaplen) memset (p, 0, sizeof(*p)); p->snapshot = snaplen; p->linktype = linktype; + p->stats_op = pcap_stats_dead; p->close_op = pcap_close_dead; return p; } diff --git a/savefile.c b/savefile.c index 2a8bc90..9b9baf8 100644 --- a/savefile.c +++ b/savefile.c @@ -30,7 +30,7 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.82 2003-07-25 03:25:48 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.83 2003-07-25 04:05:00 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -410,6 +410,14 @@ swap_hdr(struct pcap_file_header *hp) hp->linktype = SWAPLONG(hp->linktype); } +static int +sf_stats(pcap_t *p, struct pcap_stat *ps) +{ + snprintf(p->errbuf, PCAP_ERRBUF_SIZE, + "Statistics aren't available from savefiles"); + return (-1); +} + static void sf_close(pcap_t *p) { @@ -524,6 +532,7 @@ pcap_open_offline(const char *fname, char *errbuf) pcap_fddipad = 0; #endif + p->stats_op = sf_stats; p->close_op = sf_close; return (p);