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: /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 #ifndef DLT_IEEE802_11_RADIO 63 #define DLT_IEEE802_11_RADIO 255 /* notyet */ 64 #endif 65 66 typedef struct pcap pcap_t; 67 typedef struct pcap_if pcap_if_t; 68 typedef struct pcap_addr pcap_addr_t; 69 typedef struct pcap_dumper pcap_dumper_t; 70 71 /* 72 * The first record in the file contains saved values for some 73 * of the flags used in the printout phases of tcpdump. 74 * Many fields here are 32 bit ints so compilers won't insert unwanted 75 * padding; these files need to be interchangeable across architectures. 76 */ 77 struct pcap_file_header { 78 bpf_u_int32 magic; 79 u_short version_major; 80 u_short version_minor; 81 bpf_int32 thiszone; /* gmt to local correction */ 82 bpf_u_int32 sigfigs; /* accuracy of timestamps */ 83 bpf_u_int32 snaplen; /* max length saved portion of each pkt */ 84 bpf_u_int32 linktype; /* data link type (DLT_*) */ 85 }; 86 87 typedef enum { 88 PCAP_D_INOUT = 0, 89 PCAP_D_IN, 90 PCAP_D_OUT 91 } pcap_direction_t; 92 93 /* 94 * Each packet in the dump file is prepended with this generic header. 95 * This gets around the problem of different headers for different 96 * packet interfaces. 97 */ 98 struct pcap_pkthdr { 99 struct bpf_timeval ts; /* time stamp */ 100 bpf_u_int32 caplen; /* length of portion present */ 101 bpf_u_int32 len; /* length this packet (off wire) */ 102 }; 103 104 /* 105 * As returned by the pcap_stats() 106 */ 107 struct pcap_stat { 108 u_int ps_recv; /* number of packets received */ 109 u_int ps_drop; /* number of packets dropped */ 110 u_int ps_ifdrop; /* drops by interface XXX not yet supported */ 111 }; 112 113 /* 114 * Item in a list of interfaces. 115 */ 116 struct pcap_if { 117 struct pcap_if *next; 118 char *name; /* name to hand to "pcap_open_live()" */ 119 char *description; /* textual description of interface, or NULL */ 120 struct pcap_addr *addresses; 121 bpf_u_int32 flags; /* PCAP_IF_ interface flags */ 122 }; 123 124 #define PCAP_IF_LOOPBACK 0x00000001 /* interface is loopback */ 125 126 /* 127 * Representation of an interface address. 128 */ 129 struct pcap_addr { 130 struct pcap_addr *next; 131 struct sockaddr *addr; /* address */ 132 struct sockaddr *netmask; /* netmask for that address */ 133 struct sockaddr *broadaddr; /* broadcast address for that address */ 134 struct sockaddr *dstaddr; /* P2P destination address for that address */ 135 }; 136 137 typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *, 138 const u_char *); 139 140 __BEGIN_DECLS 141 char *pcap_lookupdev(char *); 142 int pcap_lookupnet(const char *, bpf_u_int32 *, bpf_u_int32 *, char *); 143 pcap_t *pcap_open_live(const char *, int, int, int, char *); 144 pcap_t *pcap_open_dead(int, int); 145 pcap_t *pcap_open_offline(const char *, char *); 146 pcap_t *pcap_fopen_offline(FILE *, char *); 147 void pcap_close(pcap_t *); 148 int pcap_loop(pcap_t *, int, pcap_handler, u_char *); 149 int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *); 150 const u_char* 151 pcap_next(pcap_t *, struct pcap_pkthdr *); 152 int pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **); 153 void pcap_breakloop(pcap_t *); 154 int pcap_stats(pcap_t *, struct pcap_stat *); 155 int pcap_setfilter(pcap_t *, struct bpf_program *); 156 int pcap_setdirection(pcap_t *, pcap_direction_t); 157 int pcap_getnonblock(pcap_t *, char *); 158 int pcap_setnonblock(pcap_t *, int, char *); 159 void pcap_perror(pcap_t *, char *); 160 int pcap_inject(pcap_t *, const void *, size_t); 161 int pcap_sendpacket(pcap_t *, const u_char *, int); 162 char *pcap_strerror(int); 163 char *pcap_geterr(pcap_t *); 164 int pcap_compile(pcap_t *, struct bpf_program *, char *, int, 165 bpf_u_int32); 166 int pcap_compile_nopcap(int, int, struct bpf_program *, 167 char *, int, bpf_u_int32); 168 void pcap_freecode(struct bpf_program *); 169 int pcap_datalink(pcap_t *); 170 int pcap_list_datalinks(pcap_t *, int **); 171 int pcap_set_datalink(pcap_t *, int); 172 int pcap_datalink_name_to_val(const char *); 173 const char *pcap_datalink_val_to_name(int); 174 const char *pcap_datalink_val_to_description(int); 175 int pcap_snapshot(pcap_t *); 176 int pcap_is_swapped(pcap_t *); 177 int pcap_major_version(pcap_t *); 178 int pcap_minor_version(pcap_t *); 179 180 /* XXX */ 181 FILE *pcap_file(pcap_t *); 182 int pcap_fileno(pcap_t *); 183 184 pcap_dumper_t *pcap_dump_open(pcap_t *, const char *); 185 pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp); 186 FILE *pcap_dump_file(pcap_dumper_t *); 187 long pcap_dump_ftell(pcap_dumper_t *); 188 int pcap_dump_flush(pcap_dumper_t *); 189 void pcap_dump_close(pcap_dumper_t *); 190 void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *); 191 192 int pcap_findalldevs(pcap_if_t **, char *); 193 void pcap_freealldevs(pcap_if_t *); 194 195 const char *pcap_lib_version(void); 196 197 /* XXX this guy lives in the bpf tree */ 198 u_int bpf_filter(struct bpf_insn *, u_char *, u_int, u_int); 199 char *bpf_image(struct bpf_insn *, int); 200 201 int pcap_get_selectable_fd(pcap_t *); 202 203 __END_DECLS 204 #endif 205