From: guy Date: Fri, 25 Jul 2003 05:32:02 +0000 (+0000) Subject: Add a "read" function pointer to the pcap_t structure, which handles X-Git-Tag: libpcap-0.8-bp~27 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/cd0d893ef99fbd6e06311242d8631b8a5cf9502e Add a "read" function pointer to the pcap_t structure, which handles reading packets from a pcap_t, and make "pcap_read()" call it. That removes the last place where we have to check for a pcap_t that refers to a DAG card rather than a live capture, so get rid of the "is_dag" flag. --- diff --git a/pcap-bpf.c b/pcap-bpf.c index eeda8500..eb31e342 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.65 2003-07-25 05:07:01 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.66 2003-07-25 05:32:02 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -135,19 +135,13 @@ pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps) return (0); } -int -pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) +static int +pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { int cc; int n = 0; register u_char *bp, *ep; -#ifdef HAVE_DAG_API - if (p->md.is_dag) { - return dag_read(p, cnt, callback, user); - } -#endif /* HAVE_DAG_API */ - again: cc = p->cc; if (p->cc == 0) { @@ -733,6 +727,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, memset(p->buffer, 0x0, p->bufsize); #endif + p->read_op = pcap_read_bpf; p->setfilter_op = pcap_setfilter_bpf; p->set_datalink_op = pcap_set_datalink_bpf; p->stats_op = pcap_stats_bpf; diff --git a/pcap-dag.c b/pcap-dag.c index d4536597..618ef279 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.5 2003-07-25 05:07:01 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.6 2003-07-25 05:32:02 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -59,7 +59,6 @@ static int atexit_handler_installed = 0; #include "pcap-dag.h" /* Replace dag function names with pcap equivalent. */ -#define dag_read pcap_read #define dag_open_live pcap_open_live #define dag_platform_finddevs pcap_platform_finddevs #endif /* DAG_ONLY */ @@ -201,7 +200,7 @@ static unsigned long long swapll(unsigned long long ull) { * for each of them. Returns the number of packets handled or -1 if an * error occured. A blocking */ -int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { +static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { u_char *dp = NULL; int packet_len = 0, caplen = 0; struct pcap_pkthdr pcap_header; @@ -336,7 +335,6 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c */ handle->md.dag_mem_bottom = 0; handle->md.dag_mem_top = 0; - handle->md.is_dag = 1; handle->snapshot = snaplen; /*handle->md.timeout = to_ms; */ @@ -386,6 +384,7 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c return NULL; } + handle->read_op = dag_read; handle->setfilter_op = dag_setfilter; handle->set_datalink_op = dag_set_datalink; handle->stats_op = dag_stats; diff --git a/pcap-dag.h b/pcap-dag.h index 0776f593..440311cd 100644 --- a/pcap-dag.h +++ b/pcap-dag.h @@ -7,10 +7,8 @@ * * Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com) * - * @(#) $Header: /tcpdump/master/libpcap/pcap-dag.h,v 1.2 2003-07-25 04:04:57 guy Exp $ (LBL) + * @(#) $Header: /tcpdump/master/libpcap/pcap-dag.h,v 1.3 2003-07-25 05:32:03 guy Exp $ (LBL) */ -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); diff --git a/pcap-dlpi.c b/pcap-dlpi.c index 0b6f06f9..46bf7082 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.89 2003-07-25 05:07:01 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.90 2003-07-25 05:32:03 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -184,8 +184,8 @@ static struct strbuf ctl = { (char *)ctlbuf }; -int -pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) +static int +pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { register int cc, n, caplen, origlen; register u_char *bp, *ep, *pk; @@ -672,6 +672,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->read_op = pcap_read_dlpi; p->setfilter_op = install_bpf_program; /* no kernel filtering */ p->set_datalink_op = NULL; /* can't change data link type */ p->stats_op = pcap_stats_dlpi; diff --git a/pcap-int.h b/pcap-int.h index 13191829..3a0fbe6a 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.51 2003-07-25 05:07:02 guy Exp $ (LBL) + * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.52 2003-07-25 05:32:03 guy Exp $ (LBL) */ #ifndef pcap_int_h @@ -78,7 +78,6 @@ struct pcap_md { #endif #ifdef HAVE_DAG_API - int is_dag; /* this is a dag card handle */ void *dag_mem_base; /* DAG card memory base address */ u_int dag_mem_bottom; /* DAG card current memory bottom pointer */ u_int dag_mem_top; /* DAG card current memory top pointer */ @@ -118,6 +117,7 @@ struct pcap { /* * Methods. */ + int (*read_op)(pcap_t *, int cnt, pcap_handler, u_char *); int (*setfilter_op)(pcap_t *, struct bpf_program *); int (*set_datalink_op)(pcap_t *, int); int (*stats_op)(pcap_t *, struct pcap_stat *); diff --git a/pcap-linux.c b/pcap-linux.c index 5a732121..3931caac 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.94 2003-07-25 05:07:02 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.95 2003-07-25 05:32:04 guy Exp $ (LBL)"; #endif /* @@ -186,6 +186,7 @@ typedef int socklen_t; 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_linux(pcap_t *, int, pcap_handler, u_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 *); @@ -396,6 +397,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, return NULL; } + handle->read_op = pcap_read_linux; handle->setfilter_op = pcap_setfilter_linux; handle->set_datalink_op = NULL; /* can't change data link type */ handle->stats_op = pcap_stats_linux; @@ -409,15 +411,9 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, * for each of them. Returns the number of packets handled or -1 if an * error occured. */ -int -pcap_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) +static int +pcap_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) { -#ifdef HAVE_DAG_API - if (handle->md.is_dag) { - return dag_read(handle, max_packets, callback, user); - } -#endif /* HAVE_DAG_API */ - /* * Currently, on Linux only one packet is delivered per read, * so we don't loop. diff --git a/pcap-nit.c b/pcap-nit.c index c0cd9ee9..44db9aff 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.48 2003-07-25 05:07:02 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.49 2003-07-25 05:32:04 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -95,8 +95,8 @@ pcap_stats_nit(pcap_t *p, struct pcap_stat *ps) return (0); } -int -pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) +static int +pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { register int cc, n; register struct bpf_insn *fcode = p->fcode.bf_insns; @@ -260,6 +260,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->read_op = pcap_read_nit; p->setfilter_op = install_bpf_program; /* no kernel filtering */ p->set_datalink_op = NULL; /* can't change data link type */ p->stats_op = pcap_stats_nit; diff --git a/pcap-null.c b/pcap-null.c index 013185a2..2a80c0ac 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.19 2003-07-25 05:07:02 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-null.c,v 1.20 2003-07-25 05:32:04 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_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) -{ - (void)snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s", nosup); - return (-1); -} - pcap_t * pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) diff --git a/pcap-pf.c b/pcap-pf.c index 0a77426e..d3f3b7f1 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.77 2003-07-25 05:07:03 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.78 2003-07-25 05:32:05 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -84,8 +84,8 @@ static int pcap_setfilter_pf(pcap_t *, struct bpf_program *); */ #define BUFSPACE (200 * 256) -int -pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) +static int +pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) { register u_char *p, *bp; struct bpf_insn *fcode; @@ -415,6 +415,7 @@ your system may not be properly configured; see the packetfilter(4) man page\n", goto bad; } + p->read_op = pcap_read_pf; p->setfilter_op = pcap_setfilter_pf; p->set_datalink_op = NULL; /* can't change data link type */ p->stats_op = pcap_stats_pf; diff --git a/pcap-snit.c b/pcap-snit.c index fa0dc8c8..1b7bae51 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.64 2003-07-25 05:07:03 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.65 2003-07-25 05:32:05 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -109,8 +109,8 @@ pcap_stats_snit(pcap_t *p, struct pcap_stat *ps) return (0); } -int -pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) +static int +pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { register int cc, n; register struct bpf_insn *fcode = p->fcode.bf_insns; @@ -318,6 +318,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->read_op = pcap_read_snit; p->setfilter_op = install_bpf_program; /* no kernel filtering */ p->set_datalink_op = NULL; /* can't change data link type */ p->stats_op = pcap_stats_snit; diff --git a/pcap-snoop.c b/pcap-snoop.c index f852a782..69c6569f 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.43 2003-07-25 05:07:03 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.44 2003-07-25 05:32:05 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -58,8 +58,8 @@ static const char rcsid[] = #include "os-proto.h" #endif -int -pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) +static int +pcap_read_snoop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { int cc; register struct snoopheader *sh; @@ -294,6 +294,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, goto bad; } + p->read_op = pcap_read_snoop; p->setfilter_op = install_bpf_program; /* no kernel filtering */ p->set_datalink_op = NULL; /* can't change data link type */ p->stats_op = pcap_stats_snoop; diff --git a/pcap-win32.c b/pcap-win32.c index a2d29c7a..1289f32e 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.12 2003-07-25 05:07:03 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.13 2003-07-25 05:32:05 guy Exp $ (LBL)"; #endif #include @@ -89,8 +89,8 @@ pcap_stats_win32(pcap_t *p, struct pcap_stat *ps) return 0; } -int -pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) +static int +pcap_read_win32(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { int cc; int n = 0; @@ -256,6 +256,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, PacketSetReadTimeout(p->adapter, to_ms); + p->read_op = pcap_read_win32; p->setfilter_op = pcap_setfilter_win32; p->set_datalink_op = NULL; /* can't change data link type */ p->stats_op = pcap_stats_win32; diff --git a/pcap.c b/pcap.c index e6fabebc..0845933e 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.60 2003-07-25 05:07:04 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.61 2003-07-25 05:32:05 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -65,9 +65,17 @@ int pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { - if (p->sf.rfile != NULL) - return (pcap_offline_read(p, cnt, callback, user)); - return (pcap_read(p, cnt, callback, user)); + return p->read_op(p, cnt, callback, user); +} + +/* + * XXX - is this necessary? + */ +int +pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) +{ + + return p->read_op(p, cnt, callback, user); } int @@ -76,15 +84,18 @@ pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) register int n; for (;;) { - if (p->sf.rfile != NULL) + if (p->sf.rfile != NULL) { + /* + * 0 means EOF, so don't loop if we get 0. + */ n = pcap_offline_read(p, cnt, callback, user); - else { + } else { /* * XXX keep reading until we get something * (or an error occurs) */ do { - n = pcap_read(p, cnt, callback, user); + n = p->read_op(p, cnt, callback, user); } while (n == 0); } if (n <= 0) @@ -182,7 +193,7 @@ pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, * The first one ('0') conflicts with the return code of 0 from * pcap_offline_read() meaning "end of file". */ - return (pcap_read(p, 1, pcap_fakecallback, (u_char *)&s)); + return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s)); } int diff --git a/savefile.c b/savefile.c index 51eb4cd1..4c145d8c 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.85 2003-07-25 05:07:04 guy Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.86 2003-07-25 05:32:06 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->read_op = pcap_offline_read; p->setfilter_op = install_bpf_program; p->set_datalink_op = NULL; /* we don't support munging link-layer headers */ p->stats_op = sf_stats;