dect
/
libpcap
Archived
13
0
Fork 0

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.
This commit is contained in:
guy 2003-07-25 05:32:02 +00:00
parent 08658f1c93
commit cd0d893ef9
14 changed files with 61 additions and 62 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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;

View 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.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 *);

View File

@ -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.

View File

@ -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;

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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 <pcap-int.h>
@ -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;

27
pcap.c
View File

@ -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

View File

@ -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;