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