xref: /openbsd/lib/libpcap/pcap.h (revision a878b819)
1*a878b819Sdjm /*	$OpenBSD: pcap.h,v 1.13 2005/11/18 11:05:39 djm Exp $	*/
2df930be7Sderaadt 
3df930be7Sderaadt /*
401efc7efSderaadt  * Copyright (c) 1993, 1994, 1995, 1996, 1997
5df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
6df930be7Sderaadt  *
7df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
8df930be7Sderaadt  * modification, are permitted provided that the following conditions
9df930be7Sderaadt  * are met:
10df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
11df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
12df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
14df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
15df930be7Sderaadt  * 3. All advertising materials mentioning features or use of this software
16df930be7Sderaadt  *    must display the following acknowledgement:
17df930be7Sderaadt  *	This product includes software developed by the Computer Systems
18df930be7Sderaadt  *	Engineering Group at Lawrence Berkeley Laboratory.
19df930be7Sderaadt  * 4. Neither the name of the University nor of the Laboratory may be used
20df930be7Sderaadt  *    to endorse or promote products derived from this software without
21df930be7Sderaadt  *    specific prior written permission.
22df930be7Sderaadt  *
23df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df930be7Sderaadt  * SUCH DAMAGE.
34df930be7Sderaadt  *
35*a878b819Sdjm  * @(#) $Header: /home/cvs/src/lib/libpcap/pcap.h,v 1.13 2005/11/18 11:05:39 djm Exp $ (LBL)
36df930be7Sderaadt  */
37df930be7Sderaadt 
38df930be7Sderaadt #ifndef lib_pcap_h
39df930be7Sderaadt #define lib_pcap_h
40df930be7Sderaadt 
41df930be7Sderaadt #include <sys/types.h>
42df930be7Sderaadt #include <sys/time.h>
43df930be7Sderaadt 
44df930be7Sderaadt #include <net/bpf.h>
45df930be7Sderaadt 
46df930be7Sderaadt #include <stdio.h>
47df930be7Sderaadt 
48df930be7Sderaadt #define PCAP_VERSION_MAJOR 2
49df930be7Sderaadt #define PCAP_VERSION_MINOR 4
50df930be7Sderaadt 
51df930be7Sderaadt #define PCAP_ERRBUF_SIZE 256
52df930be7Sderaadt 
53df930be7Sderaadt /*
54df930be7Sderaadt  * Compatibility for systems that have a bpf.h that
55df930be7Sderaadt  * predates the bpf typedefs for 64-bit support.
56df930be7Sderaadt  */
57df930be7Sderaadt #if BPF_RELEASE - 0 < 199406
589b113833Smickey typedef	int bpf_int32;
599b113833Smickey typedef	u_int bpf_u_int32;
60df930be7Sderaadt #endif
61df930be7Sderaadt 
62df930be7Sderaadt typedef struct pcap pcap_t;
63*a878b819Sdjm typedef struct pcap_if pcap_if_t;
64*a878b819Sdjm typedef struct pcap_addr pcap_addr_t;
65df930be7Sderaadt typedef struct pcap_dumper pcap_dumper_t;
66df930be7Sderaadt 
67df930be7Sderaadt /*
68df930be7Sderaadt  * The first record in the file contains saved values for some
69df930be7Sderaadt  * of the flags used in the printout phases of tcpdump.
709b113833Smickey  * Many fields here are 32 bit ints so compilers won't insert unwanted
71df930be7Sderaadt  * padding; these files need to be interchangeable across architectures.
72df930be7Sderaadt  */
73df930be7Sderaadt struct pcap_file_header {
74df930be7Sderaadt 	bpf_u_int32 magic;
75df930be7Sderaadt 	u_short version_major;
76df930be7Sderaadt 	u_short version_minor;
77df930be7Sderaadt 	bpf_int32 thiszone;	/* gmt to local correction */
78df930be7Sderaadt 	bpf_u_int32 sigfigs;	/* accuracy of timestamps */
79df930be7Sderaadt 	bpf_u_int32 snaplen;	/* max length saved portion of each pkt */
80df930be7Sderaadt 	bpf_u_int32 linktype;	/* data link type (DLT_*) */
81df930be7Sderaadt };
82df930be7Sderaadt 
83df930be7Sderaadt /*
84df930be7Sderaadt  * Each packet in the dump file is prepended with this generic header.
85df930be7Sderaadt  * This gets around the problem of different headers for different
86df930be7Sderaadt  * packet interfaces.
87df930be7Sderaadt  */
88df930be7Sderaadt struct pcap_pkthdr {
89eb867ec1Sderaadt 	struct bpf_timeval ts;	/* time stamp */
90df930be7Sderaadt 	bpf_u_int32 caplen;	/* length of portion present */
91df930be7Sderaadt 	bpf_u_int32 len;	/* length this packet (off wire) */
92df930be7Sderaadt };
93df930be7Sderaadt 
94df930be7Sderaadt /*
95df930be7Sderaadt  * As returned by the pcap_stats()
96df930be7Sderaadt  */
97df930be7Sderaadt struct pcap_stat {
98df930be7Sderaadt 	u_int ps_recv;		/* number of packets received */
99df930be7Sderaadt 	u_int ps_drop;		/* number of packets dropped */
100df930be7Sderaadt 	u_int ps_ifdrop;	/* drops by interface XXX not yet supported */
101df930be7Sderaadt };
102df930be7Sderaadt 
103*a878b819Sdjm /*
104*a878b819Sdjm  * Item in a list of interfaces.
105*a878b819Sdjm  */
106*a878b819Sdjm struct pcap_if {
107*a878b819Sdjm 	struct pcap_if *next;
108*a878b819Sdjm 	char *name;		/* name to hand to "pcap_open_live()" */
109*a878b819Sdjm 	char *description;	/* textual description of interface, or NULL */
110*a878b819Sdjm 	struct pcap_addr *addresses;
111*a878b819Sdjm 	bpf_u_int32 flags;	/* PCAP_IF_ interface flags */
112*a878b819Sdjm };
113*a878b819Sdjm 
114*a878b819Sdjm #define PCAP_IF_LOOPBACK	0x00000001	/* interface is loopback */
115*a878b819Sdjm 
116*a878b819Sdjm /*
117*a878b819Sdjm  * Representation of an interface address.
118*a878b819Sdjm  */
119*a878b819Sdjm struct pcap_addr {
120*a878b819Sdjm 	struct pcap_addr *next;
121*a878b819Sdjm 	struct sockaddr *addr;		/* address */
122*a878b819Sdjm 	struct sockaddr *netmask;	/* netmask for that address */
123*a878b819Sdjm 	struct sockaddr *broadaddr;	/* broadcast address for that address */
124*a878b819Sdjm 	struct sockaddr *dstaddr;	/* P2P destination address for that address */
125*a878b819Sdjm };
126*a878b819Sdjm 
127df930be7Sderaadt typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
128df930be7Sderaadt 			     const u_char *);
129df930be7Sderaadt 
130c6ae2b3fSnaddy __BEGIN_DECLS
131df930be7Sderaadt char	*pcap_lookupdev(char *);
1329b113833Smickey int	pcap_lookupnet(char *, bpf_u_int32 *, bpf_u_int32 *, char *);
133*a878b819Sdjm pcap_t	*pcap_open_live(const char *, int, int, int, char *);
13401efc7efSderaadt pcap_t	*pcap_open_offline(const char *, char *);
135*a878b819Sdjm pcap_t	*pcap_open_dead(int, int);
136df930be7Sderaadt void	pcap_close(pcap_t *);
137df930be7Sderaadt int	pcap_loop(pcap_t *, int, pcap_handler, u_char *);
138df930be7Sderaadt int	pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
139df930be7Sderaadt const u_char*
140df930be7Sderaadt 	pcap_next(pcap_t *, struct pcap_pkthdr *);
141df930be7Sderaadt int	pcap_stats(pcap_t *, struct pcap_stat *);
142e6ca656dSericj int	pcap_inject(pcap_t *, const void *, size_t);
143df930be7Sderaadt int	pcap_setfilter(pcap_t *, struct bpf_program *);
144*a878b819Sdjm int	pcap_getnonblock(pcap_t *, char *);
145*a878b819Sdjm int	pcap_setnonblock(pcap_t *, int, char *);
146df930be7Sderaadt void	pcap_perror(pcap_t *, char *);
147df930be7Sderaadt char	*pcap_strerror(int);
148df930be7Sderaadt char	*pcap_geterr(pcap_t *);
1499b113833Smickey int	pcap_compile(pcap_t *, struct bpf_program *, char *, int,
1509b113833Smickey 	    bpf_u_int32);
151a9b0695fSjakob int	pcap_compile_nopcap(int, int, struct bpf_program *,
152a9b0695fSjakob 	    char *, int, bpf_u_int32);
15378871eb8Sdugsong void	pcap_freecode(struct bpf_program *);
154*a878b819Sdjm void	pcap_breakloop(pcap_t *);
155df930be7Sderaadt int	pcap_datalink(pcap_t *);
156*a878b819Sdjm int	pcap_list_datalinks(pcap_t *, int **);
157*a878b819Sdjm int	pcap_set_datalink(pcap_t *, int);
158*a878b819Sdjm int	pcap_datalink_name_to_val(const char *);
159*a878b819Sdjm const char *pcap_datalink_val_to_name(int);
160*a878b819Sdjm const char *pcap_datalink_val_to_description(int);
161*a878b819Sdjm const char *pcap_lib_version(void);
162df930be7Sderaadt int	pcap_snapshot(pcap_t *);
163df930be7Sderaadt int	pcap_is_swapped(pcap_t *);
164df930be7Sderaadt int	pcap_major_version(pcap_t *);
165df930be7Sderaadt int	pcap_minor_version(pcap_t *);
166df930be7Sderaadt 
167df930be7Sderaadt /* XXX */
168df930be7Sderaadt FILE	*pcap_file(pcap_t *);
169df930be7Sderaadt int	pcap_fileno(pcap_t *);
170df930be7Sderaadt 
17101efc7efSderaadt pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
172df930be7Sderaadt void	pcap_dump_close(pcap_dumper_t *);
173df930be7Sderaadt void	pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
174df930be7Sderaadt 
175*a878b819Sdjm int	pcap_findalldevs(pcap_if_t **, char *);
176*a878b819Sdjm void	pcap_freealldevs(pcap_if_t *);
177*a878b819Sdjm 
178df930be7Sderaadt /* XXX this guy lives in the bpf tree */
179df930be7Sderaadt u_int	bpf_filter(struct bpf_insn *, u_char *, u_int, u_int);
180df930be7Sderaadt char	*bpf_image(struct bpf_insn *, int);
181c6ae2b3fSnaddy __END_DECLS
182df930be7Sderaadt #endif
183