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