pcap-int.h revision 127667
117683Spst/*
217683Spst * Copyright (c) 1994, 1995, 1996
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that the following conditions
717683Spst * are met:
817683Spst * 1. Redistributions of source code must retain the above copyright
917683Spst *    notice, this list of conditions and the following disclaimer.
1017683Spst * 2. Redistributions in binary form must reproduce the above copyright
1117683Spst *    notice, this list of conditions and the following disclaimer in the
1217683Spst *    documentation and/or other materials provided with the distribution.
1317683Spst * 3. All advertising materials mentioning features or use of this software
1417683Spst *    must display the following acknowledgement:
1517683Spst *	This product includes software developed by the Computer Systems
1617683Spst *	Engineering Group at Lawrence Berkeley Laboratory.
1717683Spst * 4. Neither the name of the University nor of the Laboratory may be used
1817683Spst *    to endorse or promote products derived from this software without
1917683Spst *    specific prior written permission.
2017683Spst *
2117683Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2217683Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2317683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2417683Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2517683Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2617683Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2717683Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2817683Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2917683Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3017683Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3117683Spst * SUCH DAMAGE.
3217683Spst *
3356891Sfenner * $FreeBSD: head/contrib/libpcap/pcap-int.h 127667 2004-03-31 09:15:09Z bms $
34127667Sbms * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.55.2.4 2003/12/15 01:42:24 guy Exp $ (LBL)
3517683Spst */
3617683Spst
3717683Spst#ifndef pcap_int_h
3817683Spst#define pcap_int_h
3917683Spst
4038151Sphk#ifdef __cplusplus
4138151Sphkextern "C" {
4238151Sphk#endif
4338151Sphk
4417683Spst#include <pcap.h>
4517683Spst
46127667Sbms#ifdef WIN32
47127667Sbms#include <packet32.h>
48127667Sbms#endif /* WIN32 */
49127667Sbms
5017683Spst/*
5117683Spst * Savefile
5217683Spst */
53127667Sbmstypedef enum {
54127667Sbms	NOT_SWAPPED,
55127667Sbms	SWAPPED,
56127667Sbms	MAYBE_SWAPPED
57127667Sbms} swapped_type_t;
58127667Sbms
5917683Spststruct pcap_sf {
6017683Spst	FILE *rfile;
6117683Spst	int swapped;
6275110Sfenner	int hdrsize;
63127667Sbms	swapped_type_t lengths_swapped;
6417683Spst	int version_major;
6517683Spst	int version_minor;
6617683Spst	u_char *base;
6717683Spst};
6817683Spst
6917683Spststruct pcap_md {
7017683Spst	struct pcap_stat stat;
7117683Spst	/*XXX*/
7275110Sfenner	int use_bpf;		/* using kernel filter */
7317683Spst	u_long	TotPkts;	/* can't oflow for 79 hrs on ether */
7417683Spst	u_long	TotAccepted;	/* count accepted by filter */
7517683Spst	u_long	TotDrops;	/* count of dropped packets */
7617683Spst	long	TotMissed;	/* missed by i/f during this run */
7717683Spst	long	OrigMissed;	/* missed by i/f before this run */
7826175Sfenner#ifdef linux
7975110Sfenner	int	sock_packet;	/* using Linux 2.0 compatible interface */
8075110Sfenner	int	timeout;	/* timeout specified to pcap_open_live */
8175110Sfenner	int	clear_promisc;	/* must clear promiscuous mode when we close */
8275110Sfenner	int	cooked;		/* using SOCK_DGRAM rather than SOCK_RAW */
8375110Sfenner	int	lo_ifindex;	/* interface index of the loopback device */
84127667Sbms	char	*device;	/* device name */
8575110Sfenner	struct pcap *next;	/* list of open promiscuous sock_packet pcaps */
8626175Sfenner#endif
87127667Sbms
88127667Sbms#ifdef HAVE_DAG_API
89127667Sbms	void	*dag_mem_base;	/* DAG card memory base address */
90127667Sbms	u_int	dag_mem_bottom;	/* DAG card current memory bottom pointer */
91127667Sbms	u_int	dag_mem_top;	/* DAG card current memory top pointer */
92127667Sbms	int	dag_fcs_bits;	/* Number of checksum bits from link layer */
93127667Sbms	int dag_offset_flags; /* Flags to pass to dag_offset(). */
94127667Sbms#endif
9517683Spst};
9617683Spst
9717683Spststruct pcap {
98127667Sbms#ifdef WIN32
99127667Sbms	ADAPTER *adapter;
100127667Sbms	LPPACKET Packet;
101127667Sbms	int timeout;
102127667Sbms	int nonblock;
103127667Sbms#else
10417683Spst	int fd;
105127667Sbms	int selectable_fd;
106127667Sbms#endif /* WIN32 */
10717683Spst	int snapshot;
10817683Spst	int linktype;
10917683Spst	int tzoff;		/* timezone offset */
11017683Spst	int offset;		/* offset for proper alignment */
11117683Spst
112127667Sbms	int break_loop;		/* flag set to force break from packet-reading loop */
113127667Sbms
11417683Spst	struct pcap_sf sf;
11517683Spst	struct pcap_md md;
11617683Spst
11717683Spst	/*
11817683Spst	 * Read buffer.
11917683Spst	 */
12017683Spst	int bufsize;
12117683Spst	u_char *buffer;
12217683Spst	u_char *bp;
12317683Spst	int cc;
12417683Spst
12517683Spst	/*
12617683Spst	 * Place holder for pcap_next().
12717683Spst	 */
12817683Spst	u_char *pkt;
12917683Spst
13017683Spst	/*
131127667Sbms	 * Methods.
132127667Sbms	 */
133127667Sbms	int	(*read_op)(pcap_t *, int cnt, pcap_handler, u_char *);
134127667Sbms	int	(*setfilter_op)(pcap_t *, struct bpf_program *);
135127667Sbms	int	(*set_datalink_op)(pcap_t *, int);
136127667Sbms	int	(*getnonblock_op)(pcap_t *, char *);
137127667Sbms	int	(*setnonblock_op)(pcap_t *, int, char *);
138127667Sbms	int	(*stats_op)(pcap_t *, struct pcap_stat *);
139127667Sbms	void	(*close_op)(pcap_t *);
140127667Sbms
141127667Sbms	/*
14217683Spst	 * Placeholder for filter code if bpf not in kernel.
14317683Spst	 */
14417683Spst	struct bpf_program fcode;
14517683Spst
146127667Sbms	char errbuf[PCAP_ERRBUF_SIZE + 1];
147109841Sfenner	int dlt_count;
148109841Sfenner	int *dlt_list;
149127667Sbms
150127667Sbms	struct pcap_pkthdr pcap_header;	/* This is needed for the pcap_next_ex() to work */
15117683Spst};
15217683Spst
15356891Sfenner/*
15456891Sfenner * This is a timeval as stored in disk in a dumpfile.
15556891Sfenner * It has to use the same types everywhere, independent of the actual
15656891Sfenner * `struct timeval'
15756891Sfenner */
15856891Sfenner
15956891Sfennerstruct pcap_timeval {
16056891Sfenner    bpf_int32 tv_sec;		/* seconds */
16156891Sfenner    bpf_int32 tv_usec;		/* microseconds */
16256891Sfenner};
16356891Sfenner
16456891Sfenner/*
16556891Sfenner * How a `pcap_pkthdr' is actually stored in the dumpfile.
16675110Sfenner *
16775110Sfenner * Do not change the format of this structure, in any way (this includes
16875110Sfenner * changes that only affect the length of fields in this structure),
16975110Sfenner * and do not make the time stamp anything other than seconds and
17075110Sfenner * microseconds (e.g., seconds and nanoseconds).  Instead:
17175110Sfenner *
17275110Sfenner *	introduce a new structure for the new format;
17375110Sfenner *
17475110Sfenner *	send mail to "tcpdump-workers@tcpdump.org", requesting a new
17575110Sfenner *	magic number for your new capture file format, and, when
17675110Sfenner *	you get the new magic number, put it in "savefile.c";
17775110Sfenner *
17875110Sfenner *	use that magic number for save files with the changed record
17975110Sfenner *	header;
18075110Sfenner *
18175110Sfenner *	make the code in "savefile.c" capable of reading files with
18275110Sfenner *	the old record header as well as files with the new record header
18375110Sfenner *	(using the magic number to determine the header format).
18475110Sfenner *
18575110Sfenner * Then supply the changes to "patches@tcpdump.org", so that future
18675110Sfenner * versions of libpcap and programs that use it (such as tcpdump) will
18775110Sfenner * be able to read your new capture file format.
18856891Sfenner */
18956891Sfenner
19056891Sfennerstruct pcap_sf_pkthdr {
19156891Sfenner    struct pcap_timeval ts;	/* time stamp */
19256891Sfenner    bpf_u_int32 caplen;		/* length of portion present */
19356891Sfenner    bpf_u_int32 len;		/* length this packet (off wire) */
19456891Sfenner};
19556891Sfenner
19675110Sfenner/*
19775110Sfenner * How a `pcap_pkthdr' is actually stored in dumpfiles written
19875110Sfenner * by some patched versions of libpcap (e.g. the ones in Red
19975110Sfenner * Hat Linux 6.1 and 6.2).
20075110Sfenner *
20175110Sfenner * Do not change the format of this structure, in any way (this includes
20275110Sfenner * changes that only affect the length of fields in this structure).
20375110Sfenner * Instead, introduce a new structure, as per the above.
20475110Sfenner */
20575110Sfenner
20675110Sfennerstruct pcap_sf_patched_pkthdr {
20775110Sfenner    struct pcap_timeval ts;	/* time stamp */
20875110Sfenner    bpf_u_int32 caplen;		/* length of portion present */
20975110Sfenner    bpf_u_int32 len;		/* length this packet (off wire) */
21075110Sfenner    int		index;
21175110Sfenner    unsigned short protocol;
21275110Sfenner    unsigned char pkt_type;
21375110Sfenner};
21475110Sfenner
21517683Spstint	yylex(void);
21617683Spst
21717683Spst#ifndef min
21817683Spst#define min(a, b) ((a) > (b) ? (b) : (a))
21917683Spst#endif
22017683Spst
22117683Spst/* XXX should these be in pcap.h? */
22217683Spstint	pcap_offline_read(pcap_t *, int, pcap_handler, u_char *);
22317683Spstint	pcap_read(pcap_t *, int cnt, pcap_handler, u_char *);
22417683Spst
225127667Sbms
22675110Sfenner/*
22775110Sfenner * Ultrix, DEC OSF/1^H^H^H^H^H^H^H^H^HDigital UNIX^H^H^H^H^H^H^H^H^H^H^H^H
22875110Sfenner * Tru64 UNIX, and NetBSD pad to make everything line up on a nice boundary.
22975110Sfenner */
23075110Sfenner#if defined(ultrix) || defined(__osf__) || defined(__NetBSD__)
23117683Spst#define       PCAP_FDDIPAD 3
23217683Spst#endif
23317683Spst
23475110Sfenner#ifndef HAVE_STRLCPY
23575110Sfenner#define strlcpy(x, y, z) \
23675110Sfenner	(strncpy((x), (y), (z)), \
23775110Sfenner	 ((z) <= 0 ? 0 : ((x)[(z) - 1] = '\0')), \
23875110Sfenner	 strlen((y)))
23975110Sfenner#endif
24075110Sfenner
241127667Sbms#include <stdarg.h>
242127667Sbms
243127667Sbms#if !defined(HAVE_SNPRINTF)
244127667Sbms#define snprintf pcap_snprintf
245127667Sbmsextern int snprintf (char *, size_t, const char *, ...);
24675110Sfenner#endif
24775110Sfenner
248127667Sbms#if !defined(HAVE_VSNPRINTF)
249127667Sbms#define vsnprintf pcap_vsnprintf
250127667Sbmsextern int vsnprintf (char *, size_t, const char *, va_list ap);
251127667Sbms#endif
252127667Sbms
253127667Sbms/*
254127667Sbms * Routines that most pcap implementations can use for non-blocking mode.
255127667Sbms */
256127667Sbms#ifndef WIN32
257127667Sbmsint	pcap_getnonblock_fd(pcap_t *, char *);
258127667Sbmsint	pcap_setnonblock_fd(pcap_t *p, int, char *);
259127667Sbms#endif
260127667Sbms
261127667Sbms/*
262127667Sbms * Internal interfaces for "pcap_findalldevs()".
263127667Sbms *
264127667Sbms * "pcap_platform_finddevs()" is a platform-dependent routine to
265127667Sbms * add devices not found by the "standard" mechanisms (SIOCGIFCONF,
266127667Sbms * "getifaddrs()", etc..
267127667Sbms *
268127667Sbms * "pcap_add_if()" adds an interface to the list of interfaces.
269127667Sbms */
270127667Sbmsint	pcap_platform_finddevs(pcap_if_t **, char *);
271127667Sbmsint	add_addr_to_iflist(pcap_if_t **, char *, u_int, struct sockaddr *,
272127667Sbms	    size_t, struct sockaddr *, size_t, struct sockaddr *, size_t,
273127667Sbms	    struct sockaddr *, size_t, char *);
274127667Sbmsint	pcap_add_if(pcap_if_t **, char *, u_int, const char *, char *);
275127667Sbmsstruct sockaddr *dup_sockaddr(struct sockaddr *, size_t);
276127667Sbmsint	add_or_find_if(pcap_if_t **, pcap_if_t **, const char *, u_int,
277127667Sbms	    const char *, char *);
278127667Sbms
279127667Sbms#ifdef WIN32
280127667Sbmschar	*pcap_win32strerror(void);
281127667Sbms#endif
282127667Sbms
28317683Spst/* XXX */
28417683Spstextern	int pcap_fddipad;
28538151Sphk
28675110Sfennerint	install_bpf_program(pcap_t *, struct bpf_program *);
28775110Sfenner
288127667Sbmsint	pcap_strcasecmp(const char *, const char *);
289127667Sbms
29038151Sphk#ifdef __cplusplus
29138151Sphk}
29217683Spst#endif
29338151Sphk
29438151Sphk#endif
295