1 /* $OpenBSD: pcap.h,v 1.15 2012/05/25 01:58:08 lteo 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.15 2012/05/25 01:58:08 lteo 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 /* 134 * Error codes for the pcap API. 135 * These will all be negative, so you can check for the success or 136 * failure of a call that returns these codes by checking for a 137 * negative value. 138 */ 139 #define PCAP_ERROR -1 /* generic error code */ 140 #define PCAP_ERROR_BREAK -2 /* loop terminated by pcap_breakloop */ 141 #define PCAP_ERROR_NOT_ACTIVATED -3 /* the capture needs to be activated */ 142 #define PCAP_ERROR_ACTIVATED -4 /* the operation can't be performed on already activated captures */ 143 #define PCAP_ERROR_NO_SUCH_DEVICE -5 /* no such device exists */ 144 #define PCAP_ERROR_RFMON_NOTSUP -6 /* this device doesn't support rfmon (monitor) mode */ 145 #define PCAP_ERROR_NOT_RFMON -7 /* operation supported only in monitor mode */ 146 #define PCAP_ERROR_PERM_DENIED -8 /* no permission to open the device */ 147 #define PCAP_ERROR_IFACE_NOT_UP -9 /* interface isn't up */ 148 #define PCAP_ERROR_CANTSET_TSTAMP_TYPE -10 /* this device doesn't support setting the time stamp type */ 149 #define PCAP_ERROR_PROMISC_PERM_DENIED -11 /* you don't have permission to capture in promiscuous mode */ 150 151 /* 152 * Warning codes for the pcap API. 153 * These will all be positive and non-zero, so they won't look like 154 * errors. 155 */ 156 #define PCAP_WARNING 1 /* generic warning code */ 157 #define PCAP_WARNING_PROMISC_NOTSUP 2 /* this device doesn't support promiscuous mode */ 158 #define PCAP_WARNING_TSTAMP_TYPE_NOTSUP 3 /* the requested time stamp type is not supported */ 159 160 typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *, 161 const u_char *); 162 163 __BEGIN_DECLS 164 char *pcap_lookupdev(char *); 165 int pcap_lookupnet(const char *, bpf_u_int32 *, bpf_u_int32 *, char *); 166 167 pcap_t *pcap_create(const char *, char *); 168 int pcap_set_snaplen(pcap_t *, int); 169 int pcap_set_promisc(pcap_t *, int); 170 int pcap_can_set_rfmon(pcap_t *); 171 int pcap_set_rfmon(pcap_t *, int); 172 int pcap_set_timeout(pcap_t *, int); 173 int pcap_set_buffer_size(pcap_t *, int); 174 int pcap_activate(pcap_t *); 175 176 pcap_t *pcap_open_live(const char *, int, int, int, char *); 177 pcap_t *pcap_open_dead(int, int); 178 pcap_t *pcap_open_offline(const char *, char *); 179 pcap_t *pcap_fopen_offline(FILE *, char *); 180 void pcap_close(pcap_t *); 181 int pcap_loop(pcap_t *, int, pcap_handler, u_char *); 182 int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *); 183 const u_char* 184 pcap_next(pcap_t *, struct pcap_pkthdr *); 185 int pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **); 186 void pcap_breakloop(pcap_t *); 187 int pcap_stats(pcap_t *, struct pcap_stat *); 188 int pcap_setfilter(pcap_t *, struct bpf_program *); 189 int pcap_setdirection(pcap_t *, pcap_direction_t); 190 int pcap_getnonblock(pcap_t *, char *); 191 int pcap_setnonblock(pcap_t *, int, char *); 192 void pcap_perror(pcap_t *, char *); 193 int pcap_inject(pcap_t *, const void *, size_t); 194 int pcap_sendpacket(pcap_t *, const u_char *, int); 195 const char *pcap_statustostr(int); 196 char *pcap_strerror(int); 197 char *pcap_geterr(pcap_t *); 198 int pcap_compile(pcap_t *, struct bpf_program *, char *, int, 199 bpf_u_int32); 200 int pcap_compile_nopcap(int, int, struct bpf_program *, 201 char *, int, bpf_u_int32); 202 void pcap_freecode(struct bpf_program *); 203 int pcap_datalink(pcap_t *); 204 int pcap_list_datalinks(pcap_t *, int **); 205 int pcap_set_datalink(pcap_t *, int); 206 int pcap_datalink_name_to_val(const char *); 207 const char *pcap_datalink_val_to_name(int); 208 const char *pcap_datalink_val_to_description(int); 209 int pcap_snapshot(pcap_t *); 210 int pcap_is_swapped(pcap_t *); 211 int pcap_major_version(pcap_t *); 212 int pcap_minor_version(pcap_t *); 213 214 /* XXX */ 215 FILE *pcap_file(pcap_t *); 216 int pcap_fileno(pcap_t *); 217 218 pcap_dumper_t *pcap_dump_open(pcap_t *, const char *); 219 pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp); 220 FILE *pcap_dump_file(pcap_dumper_t *); 221 long pcap_dump_ftell(pcap_dumper_t *); 222 int pcap_dump_flush(pcap_dumper_t *); 223 void pcap_dump_close(pcap_dumper_t *); 224 void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *); 225 226 int pcap_findalldevs(pcap_if_t **, char *); 227 void pcap_freealldevs(pcap_if_t *); 228 229 const char *pcap_lib_version(void); 230 231 /* XXX this guy lives in the bpf tree */ 232 u_int bpf_filter(struct bpf_insn *, u_char *, u_int, u_int); 233 char *bpf_image(struct bpf_insn *, int); 234 235 int pcap_get_selectable_fd(pcap_t *); 236 237 __END_DECLS 238 #endif 239