xref: /openbsd/lib/libpcap/pcap.h (revision ff78d6ed)
1 /*	$OpenBSD: pcap.h,v 1.22 2021/01/18 09:26:35 sthen 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 
36 #ifndef lib_pcap_h
37 #define lib_pcap_h
38 
39 #include <sys/types.h>
40 #include <sys/time.h>
41 
42 #include <net/bpf.h>
43 
44 #include <stdio.h>
45 
46 #define PCAP_VERSION_MAJOR 2
47 #define PCAP_VERSION_MINOR 4
48 
49 #define PCAP_ERRBUF_SIZE 256
50 
51 /*
52  * Compatibility for systems that have a bpf.h that
53  * predates the bpf typedefs for 64-bit support.
54  */
55 #if BPF_RELEASE - 0 < 199406
56 typedef	int bpf_int32;
57 typedef	u_int bpf_u_int32;
58 #endif
59 
60 typedef struct pcap pcap_t;
61 typedef struct pcap_if pcap_if_t;
62 typedef struct pcap_addr pcap_addr_t;
63 typedef struct pcap_dumper pcap_dumper_t;
64 
65 /*
66  * The first record in the file contains saved values for some
67  * of the flags used in the printout phases of tcpdump.
68  * Many fields here are 32 bit ints so compilers won't insert unwanted
69  * padding; these files need to be interchangeable across architectures.
70  */
71 struct pcap_file_header {
72 	bpf_u_int32 magic;
73 	u_short version_major;
74 	u_short version_minor;
75 	bpf_int32 thiszone;	/* gmt to local correction */
76 	bpf_u_int32 sigfigs;	/* accuracy of timestamps */
77 	bpf_u_int32 snaplen;	/* max length saved portion of each pkt */
78 	bpf_u_int32 linktype;	/* data link type (DLT_*) */
79 };
80 
81 typedef enum {
82        PCAP_D_INOUT = 0,
83        PCAP_D_IN,
84        PCAP_D_OUT
85 } pcap_direction_t;
86 
87 /*
88  * Each packet in the dump file is prepended with this generic header.
89  * This gets around the problem of different headers for different
90  * packet interfaces.
91  */
92 struct pcap_pkthdr {
93 	struct bpf_timeval ts;	/* time stamp */
94 	bpf_u_int32 caplen;	/* length of portion present */
95 	bpf_u_int32 len;	/* length of this packet (off wire) */
96 };
97 
98 /*
99  * As returned by the pcap_stats()
100  */
101 struct pcap_stat {
102 	u_int ps_recv;		/* number of packets received */
103 	u_int ps_drop;		/* number of packets dropped */
104 	u_int ps_ifdrop;	/* drops by interface XXX not yet supported */
105 };
106 
107 /*
108  * Item in a list of interfaces.
109  */
110 struct pcap_if {
111 	struct pcap_if *next;
112 	char *name;		/* name to hand to "pcap_open_live()" */
113 	char *description;	/* textual description of interface, or NULL */
114 	struct pcap_addr *addresses;
115 	bpf_u_int32 flags;	/* PCAP_IF_ interface flags */
116 };
117 
118 #define PCAP_IF_LOOPBACK	0x00000001	/* interface is loopback */
119 
120 /*
121  * Representation of an interface address.
122  */
123 struct pcap_addr {
124 	struct pcap_addr *next;
125 	struct sockaddr *addr;		/* address */
126 	struct sockaddr *netmask;	/* netmask for that address */
127 	struct sockaddr *broadaddr;	/* broadcast address for that address */
128 	struct sockaddr *dstaddr;	/* P2P destination address for that address */
129 };
130 
131 /*
132  * Error codes for the pcap API.
133  * These will all be negative, so you can check for the success or
134  * failure of a call that returns these codes by checking for a
135  * negative value.
136  */
137 #define PCAP_ERROR			-1	/* generic error code */
138 #define PCAP_ERROR_BREAK		-2	/* loop terminated by pcap_breakloop */
139 #define PCAP_ERROR_NOT_ACTIVATED	-3	/* the capture needs to be activated */
140 #define PCAP_ERROR_ACTIVATED		-4	/* the operation can't be performed on already activated captures */
141 #define PCAP_ERROR_NO_SUCH_DEVICE	-5	/* no such device exists */
142 #define PCAP_ERROR_RFMON_NOTSUP		-6	/* this device doesn't support rfmon (monitor) mode */
143 #define PCAP_ERROR_NOT_RFMON		-7	/* operation supported only in monitor mode */
144 #define PCAP_ERROR_PERM_DENIED		-8	/* no permission to open the device */
145 #define PCAP_ERROR_IFACE_NOT_UP		-9	/* interface isn't up */
146 #define PCAP_ERROR_CANTSET_TSTAMP_TYPE	-10	/* this device doesn't support setting the time stamp type */
147 #define PCAP_ERROR_PROMISC_PERM_DENIED	-11	/* you don't have permission to capture in promiscuous mode */
148 
149 /*
150  * Warning codes for the pcap API.
151  * These will all be positive and non-zero, so they won't look like
152  * errors.
153  */
154 #define PCAP_WARNING			1	/* generic warning code */
155 #define PCAP_WARNING_PROMISC_NOTSUP	2	/* this device doesn't support promiscuous mode */
156 #define PCAP_WARNING_TSTAMP_TYPE_NOTSUP	3	/* the requested time stamp type is not supported */
157 
158 /*
159  * Value to pass to pcap_compile() as the netmask if you don't know what
160  * the netmask is.
161  */
162 #define PCAP_NETMASK_UNKNOWN		0xffffffff
163 
164 typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
165 			     const u_char *);
166 
167 __BEGIN_DECLS
168 char	*pcap_lookupdev(char *);
169 int	pcap_lookupnet(const char *, bpf_u_int32 *, bpf_u_int32 *, char *);
170 
171 pcap_t	*pcap_create(const char *, char *);
172 int	pcap_set_snaplen(pcap_t *, int);
173 int	pcap_set_promisc(pcap_t *, int);
174 int	pcap_can_set_rfmon(pcap_t *);
175 int	pcap_set_rfmon(pcap_t *, int);
176 int	pcap_set_timeout(pcap_t *, int);
177 int	pcap_set_immediate_mode(pcap_t *, int);
178 int	pcap_set_buffer_size(pcap_t *, int);
179 int	pcap_activate(pcap_t *);
180 
181 pcap_t	*pcap_open_live(const char *, int, int, int, char *);
182 pcap_t	*pcap_open_dead(int, int);
183 pcap_t	*pcap_open_offline(const char *, char *);
184 pcap_t	*pcap_fopen_offline(FILE *, char *);
185 void	pcap_close(pcap_t *);
186 int	pcap_loop(pcap_t *, int, pcap_handler, u_char *);
187 int	pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
188 const u_char*
189 	pcap_next(pcap_t *, struct pcap_pkthdr *);
190 int 	pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
191 void	pcap_breakloop(pcap_t *);
192 int	pcap_stats(pcap_t *, struct pcap_stat *);
193 int	pcap_setfilter(pcap_t *, struct bpf_program *);
194 int 	pcap_setdirection(pcap_t *, pcap_direction_t);
195 int	pcap_getnonblock(pcap_t *, char *);
196 int	pcap_setnonblock(pcap_t *, int, char *);
197 void	pcap_perror(pcap_t *, const char *);
198 int	pcap_inject(pcap_t *, const void *, size_t);
199 int	pcap_sendpacket(pcap_t *, const u_char *, int);
200 const char *pcap_statustostr(int);
201 const char *pcap_strerror(int);
202 char	*pcap_geterr(pcap_t *);
203 int	pcap_compile(pcap_t *, struct bpf_program *, const char *, int,
204 	    bpf_u_int32);
205 int	pcap_compile_nopcap(int, int, struct bpf_program *,
206 	    const char *, int, bpf_u_int32);
207 void	pcap_freecode(struct bpf_program *);
208 int	pcap_offline_filter(const struct bpf_program *,
209 	    const struct pcap_pkthdr *, const u_char *);
210 int	pcap_datalink(pcap_t *);
211 int	pcap_list_datalinks(pcap_t *, int **);
212 int	pcap_set_datalink(pcap_t *, int);
213 void	pcap_free_datalinks(int *);
214 int	pcap_datalink_name_to_val(const char *);
215 const char *pcap_datalink_val_to_name(int);
216 const char *pcap_datalink_val_to_description(int);
217 int	pcap_snapshot(pcap_t *);
218 int	pcap_is_swapped(pcap_t *);
219 int	pcap_major_version(pcap_t *);
220 int	pcap_minor_version(pcap_t *);
221 
222 /* XXX */
223 FILE	*pcap_file(pcap_t *);
224 int	pcap_fileno(pcap_t *);
225 
226 pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
227 pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp);
228 FILE	*pcap_dump_file(pcap_dumper_t *);
229 long	pcap_dump_ftell(pcap_dumper_t *);
230 int	pcap_dump_flush(pcap_dumper_t *);
231 void	pcap_dump_close(pcap_dumper_t *);
232 void	pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
233 
234 int	pcap_findalldevs(pcap_if_t **, char *);
235 void	pcap_freealldevs(pcap_if_t *);
236 
237 const char *pcap_lib_version(void);
238 
239 char	*bpf_image(const struct bpf_insn *, int);
240 
241 int	pcap_get_selectable_fd(pcap_t *);
242 
243 __END_DECLS
244 #endif
245