source: lib/libtrace.h.in @ 16c598d

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 16c598d was 16c598d, checked in by Shane Alcock <salcock@…>, 9 years ago
  • Set TRACE_RT_LAST to a value that won't overflow a 32-bit integer.
  • Property mode set to 100644
File size: 117.6 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34#ifndef LIBTRACE_H
35#define LIBTRACE_H
36
37/** @file
38 *
39 * @brief Trace file processing library header
40 *
41 * @author Daniel Lawson
42 * @author Perry Lorier
43 * @author Shane Alcock
44 *
45 * @version $Id$
46 *
47 * This library provides a per packet interface into a trace file, or a live
48 * captures.  It supports ERF, DAG cards, PCAP, Linux and BSD native sockets,
49 * legacy ERF formats etc.
50 *
51 * @par Usage
52 * See the example/ directory in the source distribution for some simple
53 * examples
54 *
55 * @par Linking
56 * To use this library you need to link against libtrace by passing -ltrace
57 * to your linker. You may also need to link against a version of libpcap
58 * and of zlib which are compiled for largefile support (if you wish to access
59 * traces larger than 2 GB). This is left as an exercise for the reader. Debian
60 * Woody, at least, does not support large file offsets.
61 *
62 */
63
64#include <sys/types.h>
65#ifndef WIN32
66#include <sys/time.h>
67#endif
68
69#ifdef _MSC_VER
70    /* define the following from MSVC's internal types */
71    typedef             __int8  int8_t;
72    typedef             __int16 int16_t;
73    typedef             __int32 int32_t;
74    typedef             __int64 int64_t;
75    typedef unsigned    __int8  uint8_t;
76    typedef unsigned    __int16 uint16_t;
77    typedef unsigned    __int32 uint32_t;
78    typedef unsigned    __int64 uint64_t;
79    #ifdef LT_BUILDING_DLL
80        #define DLLEXPORT __declspec(dllexport)
81    #else
82        #define DLLEXPORT __declspec(dllimport)
83    #endif
84    #define DLLLOCAL
85    /* Windows pads bitfields out to to the size of their parent type
86     * however gcc warns that this doesn't meet with the iso C specification
87     * so produces warnings for this behaviour.  sigh.
88     */
89    #define LT_BITFIELD8        uint8_t
90    #define LT_BITFIELD16       uint16_t
91    #define LT_BITFIELD32       uint32_t
92    #define LT_BITFIELD64       uint64_t
93#else
94    #ifdef HAVE_STDINT_H
95        #   include <stdint.h>
96    #endif
97    #if __GNUC__ >= 4
98        #ifdef LT_BUILDING_DLL
99                #define DLLEXPORT __attribute__ ((visibility("default")))
100                #define DLLLOCAL __attribute__ ((visibility("hidden")))
101        #else
102                #define DLLEXPORT
103                #define DLLLOCAL
104        #endif
105    #else
106        #define DLLEXPORT
107        #define DLLLOCAL
108    #endif
109    /* GCC warns if the bitfield type is not "unsigned int", however windows
110     * generates incorrect code for this (see above), so we define these
111     * macros.  How Hideous.  So much for C's portability.
112     */
113    #define LT_BITFIELD8        unsigned int
114    #define LT_BITFIELD16       unsigned int
115    #define LT_BITFIELD32       unsigned int
116    #define LT_BITFIELD64       unsigned int
117#endif
118
119#ifdef WIN32
120#   include <winsock2.h>
121#   include <ws2tcpip.h>
122    typedef short sa_family_t;
123    /* Make up for a lack of stdbool.h */
124#    define bool signed char
125#    define false 0
126#    define true 1
127#    if !defined(ssize_t)
128     /* XXX: Not 64-bit safe! */
129#    define ssize_t int
130#    endif   
131#else
132#    include <netinet/in.h>
133
134#ifndef __cplusplus
135#    include <stdbool.h>
136#endif
137
138#    include <sys/types.h>
139#    include <sys/socket.h>
140#endif
141
142/** API version as 2 byte hex digits, eg 0xXXYYZZ */
143#define LIBTRACE_API_VERSION \
144            ((@LIBTRACE_MAJOR@<<16)|(@LIBTRACE_MID@<<8)|(@LIBTRACE_MINOR@))
145
146/** Replaced with the current SVN revision number when 'make dist' is invoked
147 *  to create a distributable tarball */
148#define LIBTRACE_SVN_REVISION 0
149
150/** DAG driver version installed on the current system */
151#define DAG_DRIVER_V "@DAG_VERSION_NUM@"
152   
153#ifdef __cplusplus
154extern "C" {
155#endif
156
157/* Function does not depend on anything but its
158 * parameters, used to hint gcc's optimisations
159 */
160#if __GNUC__ >= 3
161#  define DEPRECATED __attribute__((deprecated))
162#  define SIMPLE_FUNCTION __attribute__((pure))
163#  define UNUSED __attribute__((unused))
164#  define PACKED __attribute__((packed))
165#  define PRINTF(formatpos,argpos) __attribute__((format(printf,formatpos,argpos)))
166#else
167#  define DEPRECATED
168#  define SIMPLE_FUNCTION
169#  define UNUSED
170#  define PACKED
171#  define PRINTF(formatpos,argpos)
172#endif
173       
174/** Opaque structure holding information about an output trace */
175typedef struct libtrace_out_t libtrace_out_t;
176       
177/** Opaque structure holding information about a trace */
178typedef struct libtrace_t libtrace_t;
179       
180/** Opaque structure holding information about a bpf filter */
181typedef struct libtrace_filter_t libtrace_filter_t;
182
183/** If the packet has allocated its own memory the buffer_control should be
184 * set to TRACE_CTRL_PACKET, so that the memory will be freed when the packet
185 * is destroyed. If the packet has been zerocopied out of memory owned by
186 * something else, e.g. a DAG card, it should be TRACE_CTRL_EXTERNAL.
187 *
188 * @note The letters p and e are magic numbers used to detect if the packet
189 * wasn't created properly.
190 */
191typedef enum {
192        TRACE_CTRL_PACKET='p',  /**< Buffer memory is owned by the packet */
193        TRACE_CTRL_EXTERNAL='e' /**< Buffer memory is owned by an external source */
194} buf_control_t;
195
196/** The size of a packet's buffer when managed by libtrace */
197#define LIBTRACE_PACKET_BUFSIZE 65536
198
199/** Libtrace error information */
200typedef struct trace_err_t{
201        int err_num;            /**< error code */
202        char problem[255];      /**< the format, uri etc that caused the error for reporting purposes */
203} libtrace_err_t;
204
205/** Enumeration of error codes */
206enum {
207        /** No Error has occured.... yet. */
208        TRACE_ERR_NOERROR       = 0,
209        /** The URI passed to trace_create() is unsupported, or badly formed */
210        TRACE_ERR_BAD_FORMAT    = -1,
211        /** The trace failed to initialise */
212        TRACE_ERR_INIT_FAILED   = -2,
213        /** Unknown config option */
214        TRACE_ERR_UNKNOWN_OPTION= -3,
215        /** This output uri cannot write packets of this type */
216        TRACE_ERR_NO_CONVERSION = -4,
217        /** This packet is corrupt, or unusable for the action required */
218        TRACE_ERR_BAD_PACKET    = -5,
219        /** Option known, but unsupported by this format */
220        TRACE_ERR_OPTION_UNAVAIL= -6,
221        /** This feature is unsupported */
222        TRACE_ERR_UNSUPPORTED   = -7,
223        /** Illegal use of the API */
224        TRACE_ERR_BAD_STATE     = -8,
225        /** Failed to compile a BPF filter */
226        TRACE_ERR_BAD_FILTER    = -9,
227        /** RT communication breakdown */
228        TRACE_ERR_RT_FAILURE    = -10,
229        /** Compression format unsupported */
230        TRACE_ERR_UNSUPPORTED_COMPRESS  = -11
231};
232
233/** Enumeration of DLTs supported by libtrace
234 */
235typedef enum {
236        /** pcap documents this as having the Address Family value in host byte order as the
237          * framing.  Ugly? Yes.
238          */
239        TRACE_DLT_NULL = 0,
240        TRACE_DLT_EN10MB = 1,
241        TRACE_DLT_PPP = 9,
242        TRACE_DLT_ATM_RFC1483 = 11,
243       
244        /** Ok, so OpenBSD has a different value for DLT_RAW as the rest of the planet, so detect
245          * this.  When reading to/from files we should be using TRACE_DLT_LINKTYPE_RAW instead.
246          * When talking about DLT's inside libtrace tho, we should be using /these/ DLT's.
247          */
248#ifdef __OpenBSD__
249        TRACE_DLT_RAW = 14,     
250#else
251        TRACE_DLT_RAW = 12,
252#endif
253        TRACE_DLT_PPP_SERIAL = 50,
254        TRACE_DLT_LINKTYPE_RAW = 101, /**< See TRACE_DLT_RAW for explainations of pain. */
255        TRACE_DLT_C_HDLC = 104,
256        TRACE_DLT_IEEE802_11 = 105,
257        TRACE_DLT_LINUX_SLL = 113,
258        TRACE_DLT_PFLOG = 117,
259        TRACE_DLT_IEEE802_11_RADIO = 127 /**< Radiotap */
260} libtrace_dlt_t ;
261
262/** Enumeration of link layer types supported by libtrace */
263typedef enum {
264    /* TRACE_TYPE_LEGACY = 0            Obsolete */
265       TRACE_TYPE_HDLC_POS = 1,         /**< HDLC over POS */
266       TRACE_TYPE_ETH = 2,              /**< 802.3 style Ethernet */
267       TRACE_TYPE_ATM = 3,              /**< ATM frame */
268       TRACE_TYPE_80211 = 4,            /**< 802.11 frames */
269       TRACE_TYPE_NONE = 5,             /**< Raw IP frames */
270       TRACE_TYPE_LINUX_SLL = 6,        /**< Linux "null" framing */
271       TRACE_TYPE_PFLOG = 7,            /**< FreeBSD's PFlog */
272    /* TRACE_TYPE_LEGACY_DEFAULT        Obsolete */
273       TRACE_TYPE_POS = 9,              /**< Packet-over-SONET */
274    /* TRACE_TYPE_LEGACY_ATM            Obsolete */
275    /* TRACE_TYPE_LEGACY_ETH            Obsolete */
276       TRACE_TYPE_80211_PRISM = 12,     /**< 802.11 Prism frames */
277       TRACE_TYPE_AAL5 = 13,            /**< ATM Adaptation Layer 5 frames */
278       TRACE_TYPE_DUCK = 14,         /**< Pseudo link layer for DUCK packets */
279       TRACE_TYPE_80211_RADIO = 15,  /**< Radiotap + 802.11 */
280       TRACE_TYPE_LLCSNAP = 16,      /**< Raw LLC/SNAP */
281       TRACE_TYPE_PPP = 17,          /**< PPP frames */
282       TRACE_TYPE_METADATA = 18,        /**< WDCAP-style meta-data */
283       TRACE_TYPE_NONDATA = 19          /**< Not a data packet */
284} libtrace_linktype_t;
285
286/** RT protocol base format identifiers.
287 * This is used to describe the capture format of the packet is being sent
288 * using the RT protocol.
289 */
290enum base_format_t {
291        TRACE_FORMAT_ERF          =1,   /**< ERF (DAG capture format) */
292        TRACE_FORMAT_PCAP         =2,   /**< Live PCAP capture */
293        TRACE_FORMAT_PCAPFILE     =3,   /**< PCAP trace file */
294        TRACE_FORMAT_WAG          =4,   /**< WAG live capture (Obsolete) */
295        TRACE_FORMAT_RT           =5,   /**< RT network protocol */
296        TRACE_FORMAT_LEGACY_ATM   =6,   /**< Legacy ERF for ATM capture */
297        TRACE_FORMAT_LEGACY_POS   =7,   /**< Legacy ERF for POS capture */
298        TRACE_FORMAT_LEGACY_ETH   =8,   /**< Legacy ERF for ETH capture */
299        TRACE_FORMAT_LINUX_NATIVE =9,   /**< Linux native interface capture */
300        TRACE_FORMAT_DUCK         =10,  /**< DAG Clock information */
301        TRACE_FORMAT_BPF          =11,  /**< BSD native interface capture */
302        TRACE_FORMAT_TSH          =12,  /**< TSH trace format */
303        TRACE_FORMAT_ATMHDR       =13,  /**< Legacy ATM header capture */
304        TRACE_FORMAT_LEGACY_NZIX  =14   /**< Legacy format used for NZIX traces */
305};
306
307/** RT protocol packet types */
308typedef enum {
309        TRACE_RT_HELLO          =1, /**< Connection accepted */
310        TRACE_RT_START          =2, /**< Request for data transmission to begin
311                                    */
312        TRACE_RT_ACK            =3, /**< Data acknowledgement */
313        TRACE_RT_STATUS         =4, /**< Fifo status packet */
314        TRACE_RT_DUCK           =5, /**< Dag duck info packet */
315        TRACE_RT_END_DATA       =6, /**< Server is exiting message */
316        TRACE_RT_CLOSE          =7, /**< Client is exiting message */
317        TRACE_RT_DENY_CONN      =8, /**< Connection has been denied */
318        TRACE_RT_PAUSE          =9, /**< Request server to suspend sending data
319                                     */
320        TRACE_RT_PAUSE_ACK      =10,/**< Server is paused message */
321        TRACE_RT_OPTION         =11,/**< Option request */
322        TRACE_RT_KEYCHANGE      =12,/**< Anonymisation key has changed */
323        TRACE_RT_DUCK_2_4       =13,/**< Dag 2.4 Duck */
324        TRACE_RT_DUCK_2_5       =14,/**< Dag 2.5 Duck */
325        TRACE_RT_LOSTCONN       =15,/**< Lost connection to server */
326        TRACE_RT_SERVERSTART    =16,/**< Reliable server has been restarted */
327        TRACE_RT_CLIENTDROP     =17,/**< Reliable client was lost */
328        TRACE_RT_METADATA       =18,/**< Packet contains server meta-data */
329
330        /** Not actually used - all DATA types begin from this value */
331        TRACE_RT_DATA_SIMPLE    = 1000,
332       
333        /** RT is encapsulating an ERF capture record */
334        TRACE_RT_DATA_ERF       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_ERF,
335        /** RT is encapsulating a WAG capture record */
336        TRACE_RT_DATA_WAG       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_WAG,
337        /** RT is encapsulating a Legacy ATM capture record */
338        TRACE_RT_DATA_LEGACY_ATM=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_ATM,
339        /** RT is encapsulating a Legacy POS capture record */
340        TRACE_RT_DATA_LEGACY_POS=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_POS,
341        /** RT is encapsulating a Legacy ETH capture record */
342        TRACE_RT_DATA_LEGACY_ETH=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_ETH,
343        /** RT is encapsulating a Linux native capture record */
344        TRACE_RT_DATA_LINUX_NATIVE=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LINUX_NATIVE,
345        /** RT is encapsulating a BSD native capture record */
346        TRACE_RT_DATA_BPF       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_BPF,
347        /** RT is encapsulating a TSH capture record */
348        TRACE_RT_DATA_TSH       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_TSH,
349        /** RT is encapsulating an ATM header capture record */
350        TRACE_RT_DATA_ATMHDR = TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_ATMHDR,
351        /** RT is encapsulating a Legacy NZIX capture record */
352        TRACE_RT_DATA_LEGACY_NZIX=TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_LEGACY_NZIX,
353
354        /** As PCAP does not store the linktype with the packet, we need to
355         * create a separate RT type for each supported DLT, starting from
356         * this value */
357        TRACE_RT_DATA_DLT               = 2000,
358        /** RT is encapsulating a PCAP capture record with a NULL linktype */
359        TRACE_RT_DLT_NULL               =TRACE_RT_DATA_DLT+TRACE_DLT_NULL,
360        /** RT is encapsulating a PCAP capture record with an Ethernet
361         * linktype */
362        TRACE_RT_DLT_EN10MB             =TRACE_RT_DATA_DLT+TRACE_DLT_EN10MB,
363        /** RT is encapsulating a PCAP capture record with an 802.11
364         * linktype */
365        TRACE_RT_DLT_IEEE802_11         =TRACE_RT_DATA_DLT+TRACE_DLT_IEEE802_11,
366        /** RT is encapsulating a PCAP capture record with a Linux SLL
367         * linktype */
368        TRACE_RT_DLT_LINUX_SLL          =TRACE_RT_DATA_DLT+TRACE_DLT_LINUX_SLL,
369        /** RT is encapsulating a PCAP capture record with a PFlog linktype */
370        TRACE_RT_DLT_PFLOG              =TRACE_RT_DATA_DLT+TRACE_DLT_PFLOG,
371        /** RT is encapsulating a PCAP capture record with an AAL5 linktype */
372        TRACE_RT_DLT_ATM_RFC1483        =TRACE_RT_DATA_DLT+TRACE_DLT_ATM_RFC1483,
373        /** Unused value marking the end of the valid range for PCAP RT
374         * encapsulation */
375        TRACE_RT_DATA_DLT_END           = 2999,
376        /** Unused value marking the end of the valid range for all RT packet
377         * types */
378        TRACE_RT_LAST                   = 3000
379} libtrace_rt_types_t;
380
381/** IP Protocol values */
382typedef enum {
383        TRACE_IPPROTO_IP        = 0,    /**< IP pseudo protocol number */
384        TRACE_IPPROTO_ICMP      = 1,    /**< Internet Control Message protocol */
385        TRACE_IPPROTO_IGMP      = 2,    /**< Internet Group Management Protocol */
386        TRACE_IPPROTO_IPIP      = 4,    /**< IP encapsulated in IP */
387        TRACE_IPPROTO_TCP       = 6,    /**< Transmission Control Protocol */
388        TRACE_IPPROTO_UDP       = 17,   /**< User Datagram Protocol */
389        TRACE_IPPROTO_IPV6      = 41,   /**< IPv6 over IPv4 */
390        TRACE_IPPROTO_ROUTING   = 43,   /**< IPv6 Routing header */
391        TRACE_IPPROTO_FRAGMENT  = 44,   /**< IPv6 Fragmentation header */
392        TRACE_IPPROTO_RSVP      = 46,   /**< Resource Reservation Protocol */
393        TRACE_IPPROTO_GRE       = 47,   /**< General Routing Encapsulation */
394        TRACE_IPPROTO_ESP       = 50,   /**< Encapsulated Security Payload [RFC2406] */
395        TRACE_IPPROTO_AH        = 51,   /**< Authentication Header [RFC2402] */
396        TRACE_IPPROTO_ICMPV6    = 58,   /**< ICMPv6 */
397        TRACE_IPPROTO_NONE      = 59,   /**< IPv6 no next header */
398        TRACE_IPPROTO_DSTOPTS   = 60,   /**< IPv6 destination options */
399        TRACE_IPPROTO_OSPF      = 89,   /**< Open Shortest Path First routing protocol */
400        TRACE_IPPROTO_PIM       = 103,  /**< Protocol Independant Multicast */
401        TRACE_IPPROTO_SCTP      = 132   /**< Stream Control Transmission Protocol */
402} libtrace_ipproto_t;
403
404/** Ethertypes supported by Libtrace */
405typedef enum {
406        /* Numbers <=1500 are of course, LLC/SNAP */
407        TRACE_ETHERTYPE_IP      = 0x0800,       /**< IPv4 */
408        TRACE_ETHERTYPE_ARP     = 0x0806,       /**< Address resolution protocol */
409        TRACE_ETHERTYPE_RARP    = 0x8035,       /**< Reverse ARP */
410        TRACE_ETHERTYPE_8021Q   = 0x8100,       /**< 802.1q VLAN Extended Header */
411        TRACE_ETHERTYPE_IPV6    = 0x86DD,       /**< IPv6 */
412        TRACE_ETHERTYPE_MPLS    = 0x8847,       /**< MPLS Unicast traffic */
413        TRACE_ETHERTYPE_MPLS_MC = 0x8848,       /**< MPLS Multicast traffic */
414        TRACE_ETHERTYPE_PPP_DISC= 0x8863,       /**< PPPoE Service Discovery */
415        TRACE_ETHERTYPE_PPP_SES = 0x8864        /**< PPPoE Session Messages */
416} libtrace_ethertype_t;
417
418/** The libtrace packet structure. Applications shouldn't be
419 * meddling around in here
420 */
421typedef struct libtrace_packet_t {
422        struct libtrace_t *trace;       /**< Pointer to the trace */
423        void *header;                   /**< Pointer to the framing header */
424        void *payload;                  /**< Pointer to the link layer */
425        void *buffer;                   /**< Allocated buffer */
426        libtrace_rt_types_t  type;      /**< RT protocol type for the packet */
427        buf_control_t buf_control;      /**< Describes memory ownership */
428        int capture_length;             /**< Cached capture length */
429        int wire_length;                /**< Cached wire length */
430        int payload_length;             /**< Cached payload length */
431        void *l2_header;                /**< Cached link header */
432        libtrace_linktype_t link_type;  /**< Cached link type */
433        uint32_t l2_remaining;          /**< Cached link remaining */
434        void *l3_header;                /**< Cached l3 header */
435        uint16_t l3_ethertype;          /**< Cached l3 ethertype */
436        uint32_t l3_remaining;          /**< Cached l3 remaining */
437        void *l4_header;                /**< Cached transport header */
438        uint8_t transport_proto;        /**< Cached transport protocol */
439        uint32_t l4_remaining;          /**< Cached transport remaining */
440} libtrace_packet_t;
441
442
443/** Trace directions.
444 * Note that these are the directions used by convention. More directions
445 * are possible, not just these 3, and that they may not conform to this
446 * convention.
447 */
448typedef enum {
449        TRACE_DIR_OUTGOING = 0,         /**< Packets originating "inside" */
450        TRACE_DIR_INCOMING = 1,         /**< Packets originating "outside" */
451        TRACE_DIR_OTHER    = 2          /**< Packets with an unknown direction, or one that's unknown */
452} libtrace_direction_t;
453
454/** Enumeration of Radiotap fields */
455typedef enum {
456    TRACE_RADIOTAP_TSFT = 0, /**< Timer synchronisation function, in microseconds (uint64) */
457    TRACE_RADIOTAP_FLAGS = 1, /**< Wireless flags (uint8) */
458    TRACE_RADIOTAP_RATE = 2, /**< Bitrate in units of 500kbps (uint8) */
459    TRACE_RADIOTAP_CHANNEL = 3, /**< Frequency in MHz (uint16) and channel flags (uint16) */
460    TRACE_RADIOTAP_FHSS = 4, /**< FHSS hop set (uint8) and hopping pattern (uint8) */
461    TRACE_RADIOTAP_DBM_ANTSIGNAL = 5, /**< Signal power in dBm (int8) */
462    TRACE_RADIOTAP_DBM_ANTNOISE = 6, /**< Noise power in dBm (int8) */
463    TRACE_RADIOTAP_LOCK_QUALITY = 7, /**< Barker Code lock quality (uint16) */
464    TRACE_RADIOTAP_TX_ATTENUATION = 8, /**< TX attenuation as unitless distance from max power (uint16) */
465    TRACE_RADIOTAP_DB_TX_ATTENUATION = 9, /**< TX attenutation as dB from max power (uint16) */
466    TRACE_RADIOTAP_DBM_TX_POWER = 10, /**< TX Power in dBm (int8) */
467    TRACE_RADIOTAP_ANTENNA = 11, /**< Antenna frame was rx'd or tx'd on (uint8) */
468    TRACE_RADIOTAP_DB_ANTSIGNAL = 12, /**< Signal power in dB from a fixed reference (uint8) */
469    TRACE_RADIOTAP_DB_ANTNOISE = 13, /**< Noise power in dB from a fixed reference (uint8) */
470    TRACE_RADIOTAP_RX_FLAGS = 14, /** Properties of received frame (uint16) */
471    TRACE_RADIOTAP_TX_FLAGS = 15, /** Properties of transmitted frame (uint16) */
472    TRACE_RADIOTAP_RTS_RETRIES = 16, /** Number of rts retries frame used (uint8) */
473    TRACE_RADIOTAP_DATA_RETRIES = 17, /** Number of unicast retries a transmitted frame used (uint8) */
474    TRACE_RADIOTAP_EXT = 31
475} libtrace_radiotap_field_t;
476
477
478/** @name Protocol structures
479 * These convenience structures provide portable versions of the headers
480 * for a variety of protocols.
481 * @{
482 */
483
484#ifdef WIN32
485#pragma pack(push)
486#pragma pack(1)
487#endif
488
489/** Generic IP header structure */
490typedef struct libtrace_ip
491{
492#if BYTE_ORDER == LITTLE_ENDIAN
493    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
494    LT_BITFIELD8 ip_v:4;                /**< Version */
495#elif BYTE_ORDER == BIG_ENDIAN
496    LT_BITFIELD8 ip_v:4;                /**< Version */
497    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
498#else
499#   error "Adjust your <bits/endian.h> defines"
500#endif
501    uint8_t  ip_tos;                    /**< Type of Service */
502    uint16_t ip_len;                    /**< Total Length */
503    int16_t  ip_id;                     /**< Identification */
504    uint16_t ip_off;                    /**< IP Fragment offset (and flags) */
505    uint8_t  ip_ttl;                    /**< Time to Live */
506    uint8_t  ip_p;                      /**< Protocol */
507    uint16_t ip_sum;                    /**< Checksum */
508    struct in_addr ip_src;              /**< Source Address */
509    struct in_addr ip_dst;              /**< Destination Address */
510} PACKED libtrace_ip_t;
511
512/** IPv6 header extension structure */
513typedef struct libtrace_ip6_ext
514{
515        uint8_t nxt;    /**< Next header */
516        uint8_t len;    /**< Length of the current header */
517} PACKED libtrace_ip6_ext_t;
518
519typedef struct libtrace_ip6_frag
520{
521        uint8_t nxt;    /**< Next header */
522        uint8_t res;    /**< Reserved */
523        uint16_t frag_off;      /**< Fragment Offset (includes M flag) */
524        uint32_t ident; /** Fragment identification */
525} PACKED libtrace_ip6_frag_t;
526
527/** Generic IPv6 header structure
528 *
529 * @note The flow label field also includes the Version and Traffic Class
530 * fields, because we haven't figured out a nice way to deal with fields
531 * crossing byte boundaries on both Big and Little Endian machines */
532typedef struct libtrace_ip6
533{
534    uint32_t flow;                      /**< Flow label */
535    uint16_t plen;                      /**< Payload length */
536    uint8_t nxt;                        /**< Next header */
537    uint8_t hlim;                       /**< Hop limit */
538    struct in6_addr ip_src;             /**< Source address */
539    struct in6_addr ip_dst;             /**< Destination address */
540} PACKED libtrace_ip6_t;
541
542/** Generic TCP header structure */
543typedef struct libtrace_tcp
544  {
545    uint16_t source;            /**< Source Port */
546    uint16_t dest;              /**< Destination port */
547    uint32_t seq;               /**< Sequence number */
548    uint32_t ack_seq;           /**< Acknowledgement Number */
549#  if BYTE_ORDER == LITTLE_ENDIAN
550    LT_BITFIELD8 ecn_ns:1;      /**< ECN Nonce Sum */
551    LT_BITFIELD8 res1:3;        /**< Reserved bits */
552    LT_BITFIELD8 doff:4;        /**< Data Offset */
553    LT_BITFIELD8 fin:1;         /**< FIN */
554    LT_BITFIELD8 syn:1;         /**< SYN flag */
555    LT_BITFIELD8 rst:1;         /**< RST flag */
556    LT_BITFIELD8 psh:1;         /**< PuSH flag */
557    LT_BITFIELD8 ack:1;         /**< ACK flag */
558    LT_BITFIELD8 urg:1;         /**< URG flag */
559    LT_BITFIELD8 ece:1;         /**< ECN Echo */
560    LT_BITFIELD8 cwr:1;         /**< ECN CWR */
561#  elif BYTE_ORDER == BIG_ENDIAN
562    LT_BITFIELD8 doff:4;        /**< Data offset */
563    LT_BITFIELD8 res1:3;        /**< Reserved bits */
564    LT_BITFIELD8 ecn_ns:1;      /**< ECN Nonce Sum */
565    LT_BITFIELD8 cwr:1;         /**< ECN CWR */
566    LT_BITFIELD8 ece:1;         /**< ECN Echo */
567    LT_BITFIELD8 urg:1;         /**< URG flag */
568    LT_BITFIELD8 ack:1;         /**< ACK flag */
569    LT_BITFIELD8 psh:1;         /**< PuSH flag */
570    LT_BITFIELD8 rst:1;         /**< RST flag */
571    LT_BITFIELD8 syn:1;         /**< SYN flag */
572    LT_BITFIELD8 fin:1;         /**< FIN flag */
573#  else
574#   error "Adjust your <bits/endian.h> defines"
575#  endif
576    uint16_t window;            /**< Window Size */
577    uint16_t check;             /**< Checksum */
578    uint16_t urg_ptr;           /**< Urgent Pointer */
579} PACKED libtrace_tcp_t;
580
581/** Generic UDP header structure */
582typedef struct libtrace_udp {
583  uint16_t      source;         /**< Source port */
584  uint16_t      dest;           /**< Destination port */
585  uint16_t      len;            /**< Length */
586  uint16_t      check;          /**< Checksum */
587} PACKED libtrace_udp_t;
588
589/** Generic ICMP header structure */
590typedef struct libtrace_icmp
591{
592  uint8_t type;         /**< Message Type */
593  uint8_t code;         /**< Type Sub-code */
594  uint16_t checksum;            /**< Checksum */
595  union
596  {
597    struct
598    {
599      uint16_t  id;             /**< ID of the Echo request */
600      uint16_t  sequence;       /**< Sequence number of the Echo request */
601    } echo;                     /**< Echo Datagram */
602    uint32_t    gateway;        /**< Gateway Address */
603    struct
604    {
605      uint16_t  unused;         /**< Unused */
606      uint16_t  mtu;            /**< Next-hop MTU */
607    } frag;                     /**< Path MTU Discovery */
608  } un;                         /**< Union for Payloads of Various ICMP Codes */
609} PACKED libtrace_icmp_t;
610
611/** Generic LLC/SNAP header structure */
612typedef struct libtrace_llcsnap
613{
614/* LLC */
615  uint8_t dsap;                 /**< Destination Service Access Point */
616  uint8_t ssap;                 /**< Source Service Access Point */
617  uint8_t control;              /**< Control field */
618/* SNAP */
619  LT_BITFIELD32 oui:24;         /**< Organisationally Unique Identifier (scope)*/
620  uint16_t type;                /**< Protocol within OUI */
621} PACKED libtrace_llcsnap_t;
622
623/** 802.3 frame */
624typedef struct libtrace_ether
625{
626  uint8_t ether_dhost[6];       /**< Destination Ether Addr */
627  uint8_t ether_shost[6];       /**< Source Ether Addr */
628  uint16_t ether_type;          /**< Packet Type ID Field (next-header) */
629} PACKED libtrace_ether_t;
630
631/** 802.1Q frame */
632typedef struct libtrace_8021q
633{
634  LT_BITFIELD16 vlan_pri:3;      /**< VLAN User Priority */
635  LT_BITFIELD16 vlan_cfi:1;      /**< VLAN Format Indicator,
636                                   * 0 for ethernet, 1 for token ring */
637  LT_BITFIELD16 vlan_id:12;      /**< VLAN Id */
638  uint16_t vlan_ether_type;      /**< VLAN Sub-packet Type ID Field
639                                   * (next-header)*/
640} PACKED libtrace_8021q_t;
641
642/** ATM User Network Interface (UNI) Cell. */
643typedef struct libtrace_atm_cell
644{
645  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
646  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
647  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
648  LT_BITFIELD32 pt:3;           /**< Payload Type */
649  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
650  LT_BITFIELD32 hec:8;          /**< Header Error Control */
651} PACKED libtrace_atm_cell_t;
652
653/** ATM Network Node/Network Interface (NNI) Cell. */
654typedef struct libtrace_atm_nni_cell
655{
656  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
657  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
658  LT_BITFIELD32 pt:3;           /**< Payload Type */
659  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
660  LT_BITFIELD32 hec:8;          /**< Header Error Control */
661} PACKED libtrace_atm_nni_cell_t;
662
663/** Captured UNI cell.
664 *
665 * Endace don't capture the HEC, presumably to keep alignment.  This
666 * version of the \ref libtrace_atm_cell is used when dealing with DAG
667 * captures of uni cells.
668 *
669 */
670typedef struct libtrace_atm_capture_cell
671{
672  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
673  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
674  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
675  LT_BITFIELD32 pt:3;           /**< Payload Type */
676  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
677} PACKED libtrace_atm_capture_cell_t;
678
679/** Captured NNI cell.
680 *
681 * Endace don't capture the HEC, presumably to keep alignment.  This
682 * version of the \ref libtrace_atm_nni_cell is used when dealing with DAG
683 * captures of nni cells.
684 *
685 */
686typedef struct libtrace_atm_nni_capture_cell
687{
688  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
689  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
690  LT_BITFIELD32 pt:3;           /**< Payload Type */
691  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
692  LT_BITFIELD32 hec:8;          /**< Header Error Control */
693} PACKED libtrace_atm_nni_capture_cell_t;
694
695/** PPP header */
696typedef struct libtrace_ppp
697{
698 /* I can't figure out where the hell these two variables come from. They're
699  * definitely not in RFC 1661 which defines PPP. Probably some weird thing
700  * relating to the lack of distinction between PPP, HDLC and CHDLC */
701       
702/* uint8_t address; */          /**< PPP Address (0xFF - All stations) */
703/* uint8_t header;  */          /**< PPP Control (0x03 - Unnumbered info) */
704 uint16_t protocol;             /**< PPP Protocol (htons(0x0021) - IPv4 */
705} PACKED libtrace_ppp_t;
706
707/** PPPoE header */
708typedef struct libtrace_pppoe
709{
710 LT_BITFIELD8 version:4;        /**< Protocol version number */
711 LT_BITFIELD8 type:4;           /**< PPPoE Type */
712 uint8_t code;                  /**< PPPoE Code */
713 uint16_t session_id;           /**< Session Identifier */
714 uint16_t length;               /**< Total Length of the PPP packet */
715} PACKED libtrace_pppoe_t;
716
717/** 802.11 header */
718typedef struct libtrace_80211_t {
719#if BYTE_ORDER == LITTLE_ENDIAN
720        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
721        LT_BITFIELD32      type:2;      /**< Frame Type */
722        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
723#else
724        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
725        LT_BITFIELD32      type:2;      /**< Frame Type */
726        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
727#endif
728
729#if BYTE_ORDER == LITTLE_ENDIAN
730        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
731        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
732        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
733        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
734        LT_BITFIELD32      power:1;     /**< Power Management mode */
735        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
736        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
737        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
738#else
739        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
740        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
741        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
742        LT_BITFIELD32      power:1;     /**< Power Management mode */
743        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
744        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
745        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
746        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
747#endif
748       
749        uint16_t     duration;  /**< Duration value for NAV calculation */
750        uint8_t      mac1[6];   /**< MAC Address 1 */
751        uint8_t      mac2[6];   /**< MAC Address 2 */
752        uint8_t      mac3[6];   /**< MAC Address 3 */
753        uint16_t     SeqCtl;    /**< Sequence Control */       
754        uint8_t      mac4[6];   /**< MAC Address 4 */
755} PACKED libtrace_80211_t;
756
757/** The Radiotap header pre-amble
758 *
759 * All Radiotap headers start with this pre-amble, followed by the fields
760 * specified in the it_present bitmask. If bit 31 of it_present is set, then
761 * another bitmask follows.
762 * @note All of the radiotap data fields are in little-endian byte-order.
763 */
764typedef struct libtrace_radiotap_t {
765    uint8_t     it_version; /**< Radiotap version */
766    uint8_t     it_pad; /**< Padding for natural alignment */
767    uint16_t    it_len; /**< Length in bytes of the entire Radiotap header */
768    uint32_t    it_present; /**< Which Radiotap fields are present */
769} PACKED libtrace_radiotap_t;
770
771/** OSPF header */
772typedef struct libtrace_ospf_v2_t
773{
774        uint8_t ospf_v;         /**< OSPF Version, should be 2 */
775        uint8_t type;           /**< OSPF Packet Type */
776        uint16_t len;           /**< Packet length, including OSPF header */
777        struct in_addr router;  /**< Router ID of the packet source */
778        struct in_addr area;    /**< Area the packet belongs to */
779        uint16_t sum;           /**< Checksum */
780        uint16_t au_type;       /**< Authentication procedure */
781        uint16_t zero;          /**< Always zero */
782        uint8_t au_key_id;      /**< Authentication Key ID */
783        uint8_t au_data_len;    /**< Authentication Data Length */
784        uint32_t au_seq_num;    /**< Cryptographic Sequence Number */
785} PACKED libtrace_ospf_v2_t;
786
787/** Options Field present in some OSPFv2 packets */
788typedef struct libtrace_ospf_options_t {
789#if BYTE_ORDER == LITTLE_ENDIAN
790        LT_BITFIELD8 unused1:1;
791        LT_BITFIELD8 e_bit:1;
792        LT_BITFIELD8 mc_bit:1;
793        LT_BITFIELD8 np_bit:1;
794        LT_BITFIELD8 ea_bit:1;
795        LT_BITFIELD8 dc_bit:1;
796        LT_BITFIELD8 unused2:2;
797#elif BYTE_ORDER == BIG_ENDIAN
798        LT_BITFIELD8 unused2:2;
799        LT_BITFIELD8 dc_bit:1;
800        LT_BITFIELD8 ea_bit:1;
801        LT_BITFIELD8 np_bit:1;
802        LT_BITFIELD8 mc_bit:1;
803        LT_BITFIELD8 e_bit:1;
804        LT_BITFIELD8 unused1:1;
805#endif
806} PACKED libtrace_ospf_options_t;
807
808/** LSA Header for OSPFv2 */
809typedef struct libtrace_ospf_lsa_v2_t
810{
811        uint16_t age;           /**< Time in seconds since LSA originated */
812        libtrace_ospf_options_t lsa_options;    /**< Options */
813        uint8_t lsa_type;       /**< LSA type */
814        struct in_addr ls_id;   /**< Link State ID */
815        struct in_addr adv_router; /**< Router that originated this LSA */
816        uint32_t seq;           /**< LS sequence number */
817        uint16_t checksum;      /**< Checksum */
818        uint16_t length;        /**< Length of the LSA including LSA header */
819} PACKED libtrace_ospf_lsa_v2_t;
820
821/** OSPFv2 Hello Packet */
822typedef struct libtrace_ospf_hello_v2_t
823{
824        struct in_addr mask;    /**< Network mask for this interface */
825        uint16_t interval;      /**< Interval between Hello packets (secs) */
826        libtrace_ospf_options_t hello_options;  /**< Options */
827        uint8_t priority;       /**< Router Priority */
828        uint32_t deadint;       /**< Interval before declaring a router down */
829        struct in_addr designated;      /**< Designated router for the network */
830        struct in_addr backup;  /**< Backup designated router */
831
832        /** Neighbors follow from here, but there can be anywhere from 1 to N
833         * neighbors so I can't include that here */
834} PACKED libtrace_ospf_hello_v2_t;
835
836/** OSPFv2 Database Description packet */
837typedef struct libtrace_ospf_db_desc_v2_t
838{
839        uint16_t mtu;           /**< Interface MTU */
840        libtrace_ospf_options_t db_desc_options;        /**< Options */
841#if BYTE_ORDER == LITTLE_ENDIAN
842        LT_BITFIELD8 db_desc_ms:1;      /**< If set, this router is the master */
843        LT_BITFIELD8 db_desc_m:1;       /**< If set, more packets to follow */
844        LT_BITFIELD8 db_desc_i:1;       /**< If set, this is the first packet in sequence */
845        LT_BITFIELD8 zero:5;
846#elif BYTE_ORDER == BIG_ENDIAN
847        LT_BITFIELD8 zero:5;
848        LT_BITFIELD8 db_desc_i:1;       /**< If set, this is the first packet in sequence */
849        LT_BITFIELD8 db_desc_m:1;       /**< If set, more packets to follow */
850        LT_BITFIELD8 db_desc_ms:1;      /**< If set, this router is the master */
851#endif
852        uint32_t seq;           /**< Sequence number for DD packets */
853} PACKED libtrace_ospf_db_desc_v2_t;
854
855/** OSPF Link State Request Packet */
856typedef struct libtrace_ospf_ls_req_t
857{
858        uint32_t ls_type;       /**< Link State Type */
859        uint32_t ls_id;         /**< Link State Id */
860        uint32_t advertising_router;    /**< Advertising Router */
861} PACKED libtrace_ospf_ls_req_t;
862
863/** OSPF Link State Update Packet */
864typedef struct libtrace_ospf_ls_update_t
865{
866        uint32_t ls_num_adv;    /**< Number of LSAs in this packet */
867
868        /* Followed by LSAs, use API functions to access these */
869} PACKED libtrace_ospf_ls_update_t;
870
871/** OSPFv2 AS External LSA Body */
872typedef struct libtrace_ospf_as_external_lsa_t
873{
874        struct in_addr netmask; /**< Netmask for the destination */
875#if BYTE_ORDER == LITTLE_ENDIAN
876        LT_BITFIELD8 tos:7;
877        LT_BITFIELD8 e:1;       /**< If set, metric is Type 2. Else, Type 1 */
878#elif BYTE_ORDER == BIG_ENDIAN
879        LT_BITFIELD8 e:1;       /**< If set, metric is Type 2. Else, Type 1 */
880        LT_BITFIELD8 tos:7;
881#endif
882        uint8_t metric_a;       /**< Byte 1 of the Metric field */
883        uint8_t metric_b;       /**< Byte 2 of the Metric field */
884        uint8_t metric_c;       /**< Byte 3 of the Metric field */
885        struct in_addr forwarding;      /**< Forwarding address */
886        uint32_t external_tag;          /**< External Route Tag */
887} PACKED libtrace_ospf_as_external_lsa_v2_t;
888
889/** OSPFv2 Summary LSA Body */
890typedef struct libtrace_ospf_summary_lsa
891{
892        struct in_addr netmask; /**< Netmask for the destination */
893        uint8_t zero;           /**< Always zero */
894        uint8_t metric_a;       /**< Byte 1 of the Metric field */
895        uint8_t metric_b;       /**< Byte 2 of the Metric field */
896        uint8_t metric_c;       /**< Byte 3 of the Metric field */
897       
898} PACKED libtrace_ospf_summary_lsa_v2_t;
899
900/** OSPFv2 Network LSA Body */
901typedef struct libtrace_ospf_network_lsa_t
902{
903        struct in_addr netmask; /**< Netmask for the network */
904        /* Followed by IDs of attached routers */
905} PACKED libtrace_ospf_network_lsa_v2_t;
906
907/** OSPFv2 Router Link structure */
908typedef struct libtrace_ospf_link_t
909{
910        struct in_addr link_id;         /**< Object that link connects to */
911        struct in_addr link_data;       /**< Link Data field */
912        uint8_t type;                   /**< Link Type */
913        uint8_t num_tos;                /**< Number of TOS metrics */
914        uint16_t tos_metric;            /**< Cost of router link */
915} PACKED libtrace_ospf_link_v2_t;
916
917/** OSPFv2 Router LSA */
918typedef struct libtrace_ospf_router_lsa_t
919{
920#if BYTE_ORDER == LITTLE_ENDIAN
921        LT_BITFIELD8 b:1;       /**< Area Border Router Flag */
922        LT_BITFIELD8 e:1;       /**< External Router Flag */
923        LT_BITFIELD8 v:1;       /**< Virtual Endpoint Flag */
924        LT_BITFIELD8 zero:5;
925#elif BYTE_ORDER == BIG_ENDIAN
926        LT_BITFIELD8 zero:5;
927        LT_BITFIELD8 v:1;       /**< Virtual Endpoint Flag */
928        LT_BITFIELD8 e:1;       /**< External Router Flag */
929        LT_BITFIELD8 b:1;       /**< Area Border Router Flag */
930#endif
931        uint8_t zero2;          /**< Always zero */
932        uint16_t num_links;     /**< Number of links in LSA */
933} PACKED libtrace_ospf_router_lsa_v2_t;
934
935typedef enum {
936        TRACE_OSPF_HELLO = 1,           /**< OSPF Hello */
937        TRACE_OSPF_DATADESC = 2,        /**< OSPF Database Description */
938        TRACE_OSPF_LSREQ = 3,           /**< OSPF Link State Request */
939        TRACE_OSPF_LSUPDATE = 4,        /**< OSPF Link State Update */
940        TRACE_OSPF_LSACK = 5            /**< OSPF Link State Acknowledgement */
941} libtrace_ospf_types_t;
942
943typedef enum {
944        TRACE_OSPF_LS_ROUTER = 1,       /**< OSPF Router LSA */
945        TRACE_OSPF_LS_NETWORK = 2,      /**< OSPF Network LSA */
946        TRACE_OSPF_LS_SUMMARY = 3,      /**< OSPF Summary LSA */
947        TRACE_OSPF_LS_ASBR_SUMMARY = 4, /**< OSPF Summary LSA (ASBR) */
948        TRACE_OSPF_LS_EXTERNAL = 5      /**< OSPF AS External LSA */
949} libtrace_ospf_ls_types_t;
950
951/** A local definition of an SLL header */
952typedef struct libtrace_sll_header_t {
953        uint16_t pkttype;               /**< Packet type */
954        uint16_t hatype;                /**< Link-layer address type */
955        uint16_t halen;                 /**< Link-layer address length */
956        unsigned char addr[8];          /**< Link-layer address */
957        uint16_t protocol;              /**< Protocol */
958} libtrace_sll_header_t;
959
960
961/* SLL packet types */
962
963/** Packet was addressed for the local host */
964#define TRACE_SLL_HOST          0
965/** Packet was addressed for a broadcast address */
966#define TRACE_SLL_BROADCAST     1
967/** Packet was addressed for a multicast address */
968#define TRACE_SLL_MULTICAST     2
969/** Packet was addressed for another host but was captured by a promiscuous
970 * device */
971#define TRACE_SLL_OTHERHOST     3
972/** Packet originated from the local host */
973#define TRACE_SLL_OUTGOING      4
974
975
976#ifdef WIN32
977#pragma pack(pop)
978#endif
979
980
981/*@}*/
982
983/** Prints help information for libtrace
984 *
985 * Function prints out some basic help information regarding libtrace,
986 * and then prints out the help() function registered with each input module
987 */
988DLLEXPORT void trace_help(void);
989
990/** Causes a libtrace reader to stop blocking whilst waiting on new packets
991 * and immediately return EOF.
992 *
993 * This function is useful if you are handling signals within your libtrace
994 * program. If a live source is not receiving any packets (or they are being
995 * filtered), a call to trace_read_packet will result in an infinite loop as
996 * it will block until a packet is received. Normally, a SIGINT would cause the
997 * program to end and thus break the loop, but if you are handling the signal
998 * yourself then that signal will never reach libtrace.
999 *
1000 * Instead this function sets a global variable within libtrace that will
1001 * cause a blocking live capture to break on the next internal timeout,
1002 * allowing control to be returned to the user and their own signal handling
1003 * to kick in.
1004 */
1005DLLEXPORT void trace_interrupt(void);
1006
1007/** @name Trace management
1008 * These members deal with creating, configuring, starting, pausing and
1009 * cleaning up a trace object
1010 *@{
1011 */
1012
1013/** Takes a uri and splits it into a format and uridata component.
1014 * @param uri           The uri to be parsed
1015 * @param [out] format  A pointer that will be updated to point to an allocated
1016 *                      string holding the format component of the URI
1017 * @return NULL if an error occured, otherwise return a pointer to the uridata
1018 * component
1019 *
1020 * @note The format component is strdup'd by this function, so be sure to free
1021 * it when you are done with the split URI. Similarly, do not pass a pointer
1022 * to an allocated string into this function as the 'format' parameter, as
1023 * that memory will be leaked and replaced with the strdup'd format.
1024 */
1025DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
1026
1027/** Create an input trace from a URI
1028 *
1029 * @param uri A valid libtrace URI to be opened
1030 * @return An opaque pointer to a libtrace_t
1031 *
1032 * Some valid URI's are:
1033 *  - erf:/path/to/erf/file
1034 *  - erf:-  (stdin)
1035 *  - dag:/dev/dagcard                 
1036 *  - pcapint:pcapinterface                (eg: pcap:eth0)
1037 *  - pcap:/path/to/pcap/file
1038 *  - pcap:-
1039 *  - rt:hostname
1040 *  - rt:hostname:port
1041 *
1042 *  If an error occured when attempting to open the trace file, a
1043 *  trace is still returned so trace_is_err() should be called to find out
1044 *  if an error occured. The trace is created in the configuration state, you
1045 *  must call trace_start before attempting to read packets from the trace.
1046 */
1047DLLEXPORT libtrace_t *trace_create(const char *uri);
1048
1049/** Creates a "dummy" trace file that has only the format type set.
1050 *
1051 * @param uri A valid (but fake) URI indicating the format of the dummy trace that is to be created.
1052 * @return An opaque pointer to a (sparsely initialised) libtrace_t
1053 *
1054 * Only the format portion of the uri parameter matters - the 'file' being
1055 * opened does not have to exist.
1056 *
1057 * @note IMPORTANT: Do not attempt to call trace_read_packet or other such
1058 * functions with the dummy trace. Its intended purpose is to provide access
1059 * to the format functions where the original trace may no longer exist or is
1060 * not the correct format, e.g. reading ERF packets from an RT input.
1061 */
1062DLLEXPORT libtrace_t *trace_create_dead(const char *uri);
1063
1064/** Creates a trace output file from a URI.
1065 *
1066 * @param uri The uri string describing the output format and destination
1067 * @return An opaque pointer to a libtrace_output_t
1068 *
1069 * Valid URIs include:
1070 *  - erf:/path/to/erf/file
1071 *  - pcap:/path/to/pcap/file
1072 *
1073 *  If an error occured when attempting to open the output trace, a trace is
1074 *  still returned but trace_errno will be set. Use trace_is_err_out() and
1075 *  trace_perror_output() to get more information.
1076 */
1077DLLEXPORT libtrace_out_t *trace_create_output(const char *uri);
1078
1079/** Start an input trace
1080 * @param libtrace      The trace to start
1081 * @return 0 on success, -1 on failure
1082 *
1083 * This does the actual work of starting the input trace and applying
1084 * all the config options.  This may fail, returning -1. The libtrace error
1085 * handling functions can be used to get more information about what
1086 * specifically went wrong.
1087 */
1088DLLEXPORT int trace_start(libtrace_t *libtrace);
1089
1090/** Pauses an input trace
1091 * @param libtrace      The trace to pause
1092 * @return 0 on success, -1 on failure
1093 *
1094 * This stops an input trace that is in progress and returns you to the
1095 * configuration state.  Any packets that arrive on a live capture after
1096 * trace_pause() has been called will be discarded.  To resume the trace, call
1097 * trace_start().
1098 */
1099DLLEXPORT int trace_pause(libtrace_t *libtrace);
1100
1101/** Start an output trace
1102 * @param libtrace      The trace to start
1103 * @return 0 on success, -1 on failure
1104 *
1105 * This does the actual work with starting a trace capable of writing packets. 
1106 * This generally creates the output file.
1107 */
1108DLLEXPORT int trace_start_output(libtrace_out_t *libtrace);
1109
1110/** Valid configuration options for input traces */
1111typedef enum {
1112        /** Maximum number of bytes to be captured for any given packet */
1113        TRACE_OPTION_SNAPLEN,   
1114
1115        /** If enabled, places a live capture interface into promiscuous mode */
1116        TRACE_OPTION_PROMISC,   
1117
1118        /** Apply this filter to all packets read from this trace */
1119        TRACE_OPTION_FILTER,   
1120
1121        /** Defines the frequency of meta-data reporting, e.g. DUCK packets */
1122        TRACE_OPTION_META_FREQ,
1123
1124        /** If enabled, the libtrace event API will ignore time gaps between
1125         * packets when reading from a trace file */
1126        TRACE_OPTION_EVENT_REALTIME
1127} trace_option_t;
1128
1129/** Sets an input config option
1130 * @param libtrace      The trace object to apply the option to
1131 * @param option        The option to set
1132 * @param value         The value to set the option to
1133 * @return -1 if option configuration failed, 0 otherwise
1134 * This should be called after trace_create, and before trace_start
1135 */
1136DLLEXPORT int trace_config(libtrace_t *libtrace,
1137                trace_option_t option,
1138                void *value);
1139
1140/** Valid compression types
1141 * Note, this must be kept in sync with WANDIO_COMPRESS_* numbers in wandio.h
1142 */
1143typedef enum {
1144        TRACE_OPTION_COMPRESSTYPE_NONE = 0, /**< No compression */
1145        TRACE_OPTION_COMPRESSTYPE_ZLIB = 1, /**< GZip Compression */
1146        TRACE_OPTION_COMPRESSTYPE_BZ2  = 2, /**< BZip2 Compression */
1147        TRACE_OPTION_COMPRESSTYPE_LZO  = 3  /**< LZO Compression */
1148} trace_option_compresstype_t;
1149
1150/** Valid configuration options for output traces */
1151typedef enum {
1152        /** File flags to use when opening an output file, e.g. O_APPEND */
1153        TRACE_OPTION_OUTPUT_FILEFLAGS,
1154        /** Compression level: 0 = no compression, 1 = faster compression,
1155         * 9 = better compression */
1156        TRACE_OPTION_OUTPUT_COMPRESS,
1157        /** Compression type, see trace_option_compresstype_t */
1158        TRACE_OPTION_OUTPUT_COMPRESSTYPE
1159} trace_option_output_t;
1160
1161/** Sets an output config option
1162 *
1163 * @param libtrace      The output trace object to apply the option to
1164 * @param option        The option to set
1165 * @param value         The value to set the option to
1166 * @return -1 if option configuration failed, 0 otherwise
1167 * This should be called after trace_create_output, and before
1168 * trace_start_output
1169 */
1170DLLEXPORT int trace_config_output(libtrace_out_t *libtrace,
1171                trace_option_output_t option,
1172                void *value
1173                );
1174
1175/** Close an input trace, freeing up any resources it may have been using
1176 *
1177 * @param trace         The input trace to be destroyed
1178 *
1179 */
1180DLLEXPORT void trace_destroy(libtrace_t *trace);
1181
1182/** Close a dummy trace file, freeing up any resources it may have been using
1183 * @param trace         The dummy trace to be destroyed
1184 */
1185DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
1186
1187/** Close an output trace, freeing up any resources it may have been using
1188 * @param trace         The output trace to be destroyed
1189 */
1190DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
1191
1192/** Check (and clear) the current error state of an input trace
1193 * @param trace         The input trace to check the error state on
1194 * @return The current error status and message
1195 *
1196 * This reads and returns the current error state and sets the current error
1197 * to "no error".
1198 */
1199DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
1200
1201/** Indicate if there has been an error on an input trace
1202 * @param trace         The input trace to check the error state on
1203 * @return true if an error has occurred, false otherwise
1204 *
1205 * @note This does not clear the error status, and only returns true or false.
1206 */
1207DLLEXPORT bool trace_is_err(libtrace_t *trace);
1208
1209/** Outputs the error message for an input trace to stderr and clear the error
1210 * status.
1211 * @param trace         The input trace with the error to output
1212 * @param msg           The message to prepend to the error
1213 *
1214 * This function does clear the error status.
1215 */
1216DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...) PRINTF(2,3);
1217
1218/** Checks (and clears) the current error state for an output trace
1219 * @param trace         The output trace to check the error state on
1220 * @return The current error status and message
1221 *
1222 * This reads and returns the current error state and sets the current error
1223 * to "no error".
1224 */
1225DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
1226
1227/** Indicates if there is an error on an output trace
1228 * @param trace         The output trace to check the error state on
1229 * @return true if an error has occurred, false otherwise.
1230 *
1231 * This does not clear the error status, and only returns true or false.
1232 */
1233DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
1234
1235/** Outputs the error message for an output trace to stderr and clear the error
1236 * status.
1237 * @param trace         The output trace with the error to output
1238 * @param msg           The message to prepend to the error
1239 * This function does clear the error status.
1240 */
1241DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...)
1242        PRINTF(2,3);
1243
1244/** Returns the number of packets observed on an input trace.
1245 * Includes the number of packets counted as early as possible, before
1246 * filtering, and includes dropped packets.
1247 *
1248 * @param trace         The input trace to examine
1249 * @returns The number of packets seen at the capture point before filtering.
1250 *
1251 * If the number is not known, this function will return UINT64_MAX
1252 */
1253DLLEXPORT
1254uint64_t trace_get_received_packets(libtrace_t *trace);
1255
1256/** Returns the number of packets that were captured, but discarded for not
1257 * matching a provided filter.
1258 *
1259 * @param trace         The input trace to examine
1260 * @returns The number of packets that were successfully captured, but filtered
1261 *
1262 * If the number is not known, this function will return UINT64_MAX
1263 */
1264DLLEXPORT
1265uint64_t trace_get_filtered_packets(libtrace_t *trace);
1266
1267/** Returns the number of packets that have been dropped on an input trace due
1268 * to lack of buffer space on the capturing device.
1269 *
1270 * @param trace         The input trace to examine
1271 * @returns The number of packets captured, but dropped due to buffer overruns
1272 *
1273 * If the number is not known, this function will return UINT64_MAX
1274 */
1275DLLEXPORT
1276uint64_t trace_get_dropped_packets(libtrace_t *trace);
1277
1278/** Returns the number of packets that have been read from the input trace using
1279 * trace_read_packet().
1280 *
1281 * @param trace         The input trace to examine
1282 * @returns The number of packets that have been read by the libtrace user.
1283 *
1284 * If the number is not known, this function will return UINT64_MAX
1285 */
1286DLLEXPORT
1287uint64_t trace_get_accepted_packets(libtrace_t *trace);
1288
1289
1290/*@}*/
1291
1292/** @name Reading / Writing packets
1293 * These members deal with creating, reading and writing packets
1294 *
1295 * @{
1296 */
1297
1298/** Create a new packet object
1299 *
1300 * @return A pointer to an initialised libtrace_packet_t object
1301 */
1302DLLEXPORT libtrace_packet_t *trace_create_packet(void);
1303
1304/** Copy a packet object
1305 * @param packet        The source packet to copy
1306 * @return A new packet which has the same content as the source packet
1307 *
1308 * @note This always involves a copy, which can be slow.  Use of this
1309 * function should be avoided where possible.
1310 *
1311 * @par The reason you would want to use this function is that a zerocopied
1312 * packet from a device will be stored using memory owned by the device which
1313 * may be a limited resource. Copying the packet will ensure that the packet
1314 * is now stored in memory owned and managed by libtrace.
1315 */
1316DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
1317
1318/** Destroy a packet object
1319 * @param packet        The packet to be destroyed
1320 *
1321 */
1322DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
1323
1324
1325/** Read the next packet from an input trace
1326 *
1327 * @param trace         The libtrace opaque pointer for the input trace
1328 * @param packet        The packet opaque pointer
1329 * @return 0 on EOF, negative value on error, number of bytes read when successful.
1330 *
1331 * @note The number of bytes read is usually (but not always) the same as
1332 * trace_get_framing_length()+trace_get_capture_length() depending on the
1333 * trace format.
1334 *
1335 * @note The trace must have been started with trace_start before calling
1336 * this function
1337 *
1338 * @note When reading from a live capture, this function will block until a
1339 * packet is observed on the capture interface. The libtrace event API
1340 * (e.g. trace_event()) should be used if non-blocking operation is required.
1341 */
1342DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
1343
1344/** Event types
1345 * see \ref libtrace_eventobj_t and \ref trace_event
1346 */
1347typedef enum {
1348        TRACE_EVENT_IOWAIT,     /**< Wait on the given file descriptor */
1349        TRACE_EVENT_SLEEP,      /**< Sleep for the given amount of time */
1350        TRACE_EVENT_PACKET,     /**< Packet has been read from input trace */
1351        TRACE_EVENT_TERMINATE   /**< End of input trace */
1352} libtrace_event_t;
1353
1354/** Structure returned by libtrace_event explaining what the current event is */
1355typedef struct libtrace_eventobj_t {
1356        libtrace_event_t type; /**< Event type (iowait,sleep,packet) */
1357       
1358        /** If the event is IOWAIT, the file descriptor to wait on */
1359        int fd;               
1360        /** If the event is SLEEP, the amount of time to sleep for in seconds */
1361        double seconds;       
1362        /** If the event is PACKET, the value returned by trace_read_packet() */
1363        int size;
1364} libtrace_eventobj_t;
1365
1366/** Processes the next libtrace event from an input trace.
1367 * @param trace The libtrace opaque pointer for the input trace
1368 * @param packet The libtrace_packet opaque pointer to use for reading packets
1369 * @return A libtrace_event struct containing the event type and details of the event.
1370 *
1371 * Type can be:
1372 *  TRACE_EVENT_IOWAIT  Waiting on I/O on a file descriptor
1373 *  TRACE_EVENT_SLEEP   Wait a specified amount of time for the next event
1374 *  TRACE_EVENT_PACKET  Packet was read from the trace
1375 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
1376 */
1377DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
1378                libtrace_packet_t *packet);
1379
1380
1381/** Write one packet out to the output trace
1382 *
1383 * @param trace         The libtrace_out opaque pointer for the output trace
1384 * @param packet        The packet opaque pointer of the packet to be written
1385 * @return The number of bytes written out, if zero or negative then an error has occured.
1386 */
1387DLLEXPORT int trace_write_packet(libtrace_out_t *trace, libtrace_packet_t *packet);
1388
1389/** Gets the capture format for a given packet.
1390 * @param packet        The packet to get the capture format for.
1391 * @return The capture format of the packet
1392 *
1393 * @note Due to ability to convert packets between formats relatively easily
1394 * in Libtrace, the format of the packet right now may not be the format that
1395 * the packet was originally captured with.
1396 */
1397DLLEXPORT
1398enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1399
1400/** Construct a libtrace packet from a buffer containing the packet payload.
1401 * @param[in,out] packet        Libtrace Packet object to update with the new
1402 *                              data.
1403 * @param linktype              The linktype of the packet data.
1404 * @param[in] data              The packet data (including linklayer).
1405 * @param len                   Length of packet data provided in the buffer.
1406 *
1407 * @note The constructed packet will be in the PCAP format.
1408 *
1409 * @note To be useful, the provided buffer must start with the layer 2 header
1410 * (or a metadata header, if desired).
1411 */
1412DLLEXPORT
1413void trace_construct_packet(libtrace_packet_t *packet,
1414                libtrace_linktype_t linktype, const void *data, uint16_t len);
1415
1416/*@}*/
1417
1418/** @name Protocol decodes
1419 * These functions locate and return a pointer to various headers inside a
1420 * packet
1421 *
1422 * A packet is divided up into several "layers.":
1423 *
1424 * @li Framing header -- This is the header provided by the capture format
1425 * itself rather than anything that was sent over the network. This provides
1426 * basic details about the packet record including capture lengths, wire
1427 * lengths, timestamps, direction information and any other metadata that is
1428 * part of the capture format. 
1429 *
1430 * @li Metadata header (optional) -- A header containing metadata about a
1431 * packet that was captured, but the metadata was not transmitted over the
1432 * wire.  Some examples include RadioTap and Linux_sll headers.  This can be
1433 * retrieved by trace_get_packet_meta(), or skipped using
1434 * trace_get_payload_from_meta(). There may be multiple "metadata" headers on
1435 * a packet.
1436 *
1437 * @li Layer 2/Link layer/Datalink header -- This can be retrieved by
1438 * trace_get_layer2(), or skipped using trace_get_payload_from_layer2().
1439 *
1440 * @li Layer 3/IP/IPv6 -- This can be retrieved by trace_get_layer3().  As a
1441 * convenience trace_get_ip()/trace_get_ip6() can be used to find an IPv4/IPv6
1442 * header.
1443 *
1444 * @li Layer 5/transport -- These are protocols carried in IPv4/IPv6 frames.
1445 * These can be retrieved using trace_get_transport().
1446 *
1447 * @{
1448 */
1449
1450
1451/** Gets a pointer to the first byte of the packet as it was captured and
1452 * returns its corresponding linktype and capture length.
1453 *
1454 * Use this function instead of the deprecated trace_get_link().
1455 *
1456 * @param packet The packet to get the buffer from
1457 * @param[out] linktype The linktype of the returned pointer
1458 * @param[out] remaining The capture length (the number of captured bytes from
1459 * the returned pointer)
1460 * @return A pointer to the first byte of the packet
1461 */
1462DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet,
1463                libtrace_linktype_t *linktype, uint32_t *remaining);
1464
1465/** Get a pointer to the link layer for a given packet
1466 * @param packet        The packet to get the link layer for
1467 *
1468 * @return A pointer to the link layer, or NULL if there is no link layer
1469 *
1470 * @deprecated This function is deprecated: Use trace_get_packet_buffer() or
1471 * one of the trace_get_layer*() functions instead.
1472 * @note You should call trace_get_link_type to find out what type of link
1473 * layer has been returned to you.
1474 */
1475DLLEXPORT SIMPLE_FUNCTION DEPRECATED
1476void *trace_get_link(const libtrace_packet_t *packet);
1477
1478/** Get a pointer to the IPv4 header (if any) for a given packet
1479 * @param packet        The packet to get the IPv4 header for
1480 *
1481 * @return A pointer to the IPv4 header, or NULL if there is no IPv4 header
1482 *
1483 * If a partial IP header is present, i.e. the packet has been truncated before
1484 * the end of the IP header, this function will return NULL.
1485 *
1486 * You should consider using \ref trace_get_layer3 instead of this function.
1487 */
1488DLLEXPORT SIMPLE_FUNCTION
1489libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
1490
1491/** get a pointer to the IPv6 header (if any)
1492 * @param packet        The packet to get the IPv6 header for
1493 *
1494 * @return A pointer to the IPv6 header, or NULL if there is no IPv6 header
1495 *
1496 * If a partial IPv6 header is present, i.e. the packet has been truncated
1497 * before the end of the IP header, this function will return NULL.
1498 *
1499 * You should consider using \ref trace_get_layer3 instead of this function.
1500 */
1501DLLEXPORT SIMPLE_FUNCTION
1502libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet);
1503
1504/** Return a pointer to the first metadata header in a packet, if present.
1505 *
1506 * This function takes a pointer to a libtrace packet and if any metadata
1507 * headers exist, returns a pointer to the first one, along with its
1508 * corresponding linktype.
1509 *
1510 * If no metadata headers exist in the packet, NULL is returned.
1511 *
1512 * A metadata header is a header that was prepended by the capturing device,
1513 * such as a Linux SLL header, or a Radiotap wireless monitoring header.
1514 * Subsequent metadata headers may be accessed with the
1515 * trace_get_payload_from_meta(...) function.
1516 *
1517 * @param packet The libtrace packet
1518 * @param[out] linktype The linktype of the returned metadata header
1519 * @param[out] remaining The number of bytes captured after the returned
1520 * pointer.
1521 * @return A pointer to the first metadata header, or NULL if there are no
1522 * metadata headers present.
1523 *
1524 * remaining may be NULL, however linktype must be provided.
1525 */
1526DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet,
1527                libtrace_linktype_t *linktype,
1528                uint32_t *remaining);
1529
1530/** Returns the payload of a metadata header.
1531 *
1532 * This function takes a pointer to the start of a metadata header (either
1533 * obtained via trace_get_packet_meta(...) or by a previous call to
1534 * trace_get_payload_from_meta(...)) along with its corresponding linktype and
1535 * returns the payload, i.e. the next header. It will also update the linktype
1536 * parameter to indicate the type of payload.
1537 * 
1538 * If the linktype indicates that the header passed in is not a metadata
1539 * header, the function returns NULL to indicate this. The linktype remains
1540 * unchanged in this case.
1541 *
1542 * This function allows the user to iterate through metadata headers which are
1543 * sometimes present before the actual packet as it was received on the wire.
1544 * Examples of metadata headers include the Linux SLL header and the Radiotap
1545 * wireless monitoring header.
1546 *
1547 * If the metadata header passed into this function is truncated, this
1548 * function will return NULL, and remaining will be set to 0.
1549 *
1550 * If there are 0 bytes of payload following the provided metadata header, the
1551 * function will return a pointer to where the header would otherwise be and
1552 * remaining will be 0.
1553 *
1554 * Therefore, be sure to check the value of remaining after calling this
1555 * function!
1556 *
1557 * @param[in] meta A pointer to a metadata header
1558 * @param[in,out] linktype The linktype of meta (updated to indicate the
1559 * linktype of the returned header if applicable).
1560 * @param[in,out] remaining The number of bytes after the meta pointer.
1561 * @return A pointer to the payload of the metadata header. If meta is not a
1562 * pointer to a metadata header, NULL is returned and linktype remains
1563 * unchanged.
1564 *
1565 * All parameters are mandatory.
1566 */
1567DLLEXPORT void *trace_get_payload_from_meta(const void *meta,
1568                libtrace_linktype_t *linktype,
1569                uint32_t *remaining);
1570
1571
1572/** Get a pointer to the layer 2 header. Generally this is the first byte of the
1573 * packet as it was seen on the wire.
1574 *
1575 * This function takes a libtrace packet and skips over any metadata headers if
1576 * present (such as Linux SLL or Radiotap) and returns a pointer to the first
1577 * byte of the packet that was actually received by the network interface.
1578 *
1579 * @param packet The libtrace packet to find the layer 2 header for
1580 * @param[out] linktype The linktype of the returned layer 2 header
1581 * @param[out] remaining The number of bytes left in the packet after the
1582 * returned pointer.
1583 * @return A pointer to the first byte of the packet as it was seen on the
1584 * wire. If no layer 2 header is present, this function will return NULL.
1585 *
1586 * remaining may be NULL, otherwise it will be filled in by the function.
1587 */
1588DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet,
1589                libtrace_linktype_t *linktype,
1590                uint32_t *remaining);
1591
1592/** Gets a pointer to the next header following a layer 2 header
1593 *
1594 * @param l2                    The pointer to the current layer 2 header
1595 * @param linktype              The type of the layer 2 header
1596 * @param[out] ethertype        An optional output variable of the ethernet type of the new header
1597 * @param[in,out] remaining     Updated with the number of captured bytes
1598                                remaining
1599 *
1600 * @return A pointer to the header following the provided layer 2 header, or
1601 * NULL if no subsequent header is present.
1602 *
1603 * Remaining must point to the number of bytes captured from the layer 2 header
1604 * and beyond.  It will be decremented by the number of bytes skipped to find
1605 * the payload.
1606 *
1607 * If the layer 2 header is complete but there are zero bytes of payload after
1608 * the end of the header, a pointer to where the payload would be is returned
1609 * and remaining will be set to 0.  If the layer 2 header is incomplete
1610 * (truncated), then NULL is returned and remaining will be set to 0.
1611 * Therefore, it is very important to check the value of remaining after
1612 * calling this function.
1613 *
1614 */
1615DLLEXPORT void *trace_get_payload_from_layer2(void *l2,
1616                libtrace_linktype_t linktype,
1617                uint16_t *ethertype,
1618                uint32_t *remaining);
1619
1620
1621/** Get a pointer to the layer 3 (e.g. IP) header.
1622 * @param packet  The libtrace packet to find the layer 3 header for
1623 * @param[out] ethertype The ethertype of the layer 3 header
1624 * @param[out] remaining The amount of captured data remaining in the packet starting from the returned pointer, i.e. including the layer 3 header.
1625 *
1626 * @return A pointer to the layer 3 header. If no layer 3 header is present in
1627 * the packet, NULL is returned. If the layer 3 header is truncated, a valid
1628 * pointer will still be returned so be sure to check the value of remaining
1629 * before attempting to process the returned header.
1630 *
1631 * remaining may be NULL, otherwise it will be set to the number of captured
1632 * bytes after the pointer returned.
1633 */
1634DLLEXPORT
1635void *trace_get_layer3(const libtrace_packet_t *packet,
1636                uint16_t *ethertype, uint32_t *remaining);
1637
1638/** Gets a pointer to the transport layer header (if any)
1639 * @param packet   The libtrace packet to find the transport header for
1640 * @param[out] proto    The protocol present at the transport layer.
1641 * @param[out] remaining The amount of captured data remaining in the packet
1642 * starting from the returned pointer, i.e. including the transport header.
1643 *
1644 * @return A pointer to the transport layer header. If no transport header is
1645 * present in the packet, NULL is returned. If the transport header is
1646 * truncated, a valid pointer will still be returned so be sure to check the
1647 * value of remaining before attempting to process the returned header.
1648 *
1649 * remaining may be NULL, otherwise it will be set to the number of captured
1650 * bytes after the returned pointer.
1651 *
1652 * @note proto may be NULL if proto is unneeded.
1653 */
1654DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet,
1655                uint8_t *proto, uint32_t *remaining);
1656
1657/** Gets a pointer to the payload following an IPv4 header
1658 * @param ip            The IPv4 Header
1659 * @param[out] proto    The protocol of the header following the IPv4 header
1660 * @param[in,out] remaining Updated with the number of captured bytes remaining
1661 *
1662 * @return A pointer to the transport layer header, or NULL if no subsequent
1663 * header is present.
1664 *
1665 * When calling this function, remaining must contain the number of captured
1666 * bytes remaining in the packet starting from the IPv4 header (including the
1667 * IPv4 header itself).
1668 *
1669 * remaining will be decremented by the size of the IPv4 header (including any
1670 * options). If the IPv4 header is complete but there are zero bytes of payload
1671 * after the IPv4 header, a pointer to where the payload would be is returned
1672 * and remaining will be set to 0.  If the IPv4 header is incomplete, NULL will
1673 * be returned and remaining will be set to 0. Therefore, it is very important
1674 * to check the value of remaining after calling this function.
1675 *
1676 * proto may be NULL, in which case it won't be updated.
1677 *
1678 * @note This is similar to trace_get_transport_from_ip in libtrace2
1679 */
1680DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
1681                uint32_t *remaining);
1682
1683/** Gets a pointer to the payload following an IPv6 header
1684 * @param ipptr         The IPv6 Header
1685 * @param[out] proto    The protocol of the header following the IPv6 header
1686 * @param[in,out] remaining Updated with the number of captured bytes remaining
1687 *
1688 * @return A pointer to the transport layer header, or NULL if no subsequent
1689 * header is present.
1690 *
1691 * When calling this function, remaining must contain the number of captured
1692 * bytes remaining in the packet starting from the IPv6 header (including the
1693 * IPv6 header itself).
1694 *
1695 * remaining will be decremented by the size of the IPv6 header (including any
1696 * options). If the IPv6 header is complete but there are zero bytes of payload
1697 * after the IPv6 header, a pointer to where the payload would be is returned
1698 * and remaining will be set to 0.  If the IPv6 header is incomplete, NULL will
1699 * be returned and remaining will be set to 0. Therefore, it is very important
1700 * to check the value of remaining after calling this function.
1701 *
1702 * proto may be NULL, in which case it won't be updated.
1703 *
1704 */
1705DLLEXPORT void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr,
1706                uint8_t *proto, uint32_t *remaining);
1707
1708/** Gets a pointer to the payload following a link header
1709 * @param linkptr       A pointer to the link layer header
1710 * @param linktype      The linktype of the link header being examined
1711 * @param[out] type     An output variable for the ethernet type
1712 * @param[in,out] remaining Updated with the number of captured bytes remaining
1713 *
1714 * @return A pointer to the header following the link header, or NULL if no
1715 * subsequent header is present.
1716 *
1717 * When calling this function, remaining must contain the number of captured
1718 * bytes remaining in the packet starting from the link header (including the
1719 * link header itself). remaining will be updated to contain the number of
1720 * bytes remaining after the link header has been skipped.
1721 *
1722 * @deprecated This function is deprecated: you should be using
1723 * trace_get_payload_from_layer2() or trace_get_payload_from_meta() instead.
1724 *
1725 */
1726DLLEXPORT void *trace_get_payload_from_link(void *linkptr,
1727                libtrace_linktype_t linktype,
1728                uint16_t *type, uint32_t *remaining);
1729
1730/** Gets a pointer to the payload following an 802.1q (VLAN) header.
1731 * @param vlan      A pointer to the VLAN header
1732 * @param[out] type  The ethernet type, replaced with the VLAN ether type
1733 * @param[in,out] remaining Updated with the number of captured bytes remaining
1734 *
1735 * @return A pointer to the header beyond the VLAN header, if one is present.
1736 * Otherwise, returns NULL. 
1737 *
1738 * When calling this function, remaining must contain the number of captured
1739 * bytes remaining in the packet starting from the VLAN header (including the
1740 * VLAN header itself). remaining will be updated to contain the number of
1741 * bytes remaining after the VLAN header has been skipped.
1742 *
1743 * If the VLAN header is complete but there are zero bytes of payload after
1744 * the VLAN header, a pointer to where the payload would be is returned and
1745 * remaining will be set to 0.  If the VLAN header is incomplete, NULL will be
1746 * returned and remaining will be set to 0. Therefore, it is important to check
1747 * the value of remaining after calling this function.
1748 *
1749 * type will be set to the ethertype of the VLAN payload. This parameter is not
1750 * mandatory, but is highly recommended.
1751 *
1752 */
1753DLLEXPORT void *trace_get_payload_from_vlan(
1754                void *vlan, uint16_t *type, uint32_t *remaining);
1755
1756/** Gets a pointer to the payload following an MPLS header.
1757 * @param mpls      A pointer to the MPLS header
1758 * @param[out] type  The ethernet type, replaced by the ether type of the
1759 * returned header - 0x0000 if an Ethernet header is returned
1760 * @param[in,out] remaining Updated with the number of captured bytes remaining
1761 *
1762 * @return A pointer to the header beyond the MPLS label, if one is present.
1763 * Will return NULL if there is not enough bytes remaining to skip past the
1764 * MPLS label.
1765 *
1766 * When calling this function, remaining must contain the number of captured
1767 * bytes remaining in the packet starting from the MPLS header (including the
1768 * MPLS header itself). remaining will be updated to contain the number of
1769 * bytes remaining after the MPLS header has been skipped.
1770 *
1771 * If the MPLS header is complete but there are zero bytes of payload after
1772 * the MPLS header, a pointer to where the payload would be is returned and
1773 * remaining will be set to 0.  If the MPLS header is incomplete, NULL will be
1774 * returned and remaining will be set to 0. Therefore, it is important to check
1775 * the value of remaining after calling this function.
1776 *
1777 * type will be set to the ethertype of the MPLS payload. This parameter is
1778 * mandatory - it may not be NULL.
1779 *
1780 * @note This function will only remove one MPLS label at a time - the type
1781 * will be set to 0x8847 if there is another MPLS label following the one
1782 * skipped by this function.
1783 *
1784 */
1785DLLEXPORT void *trace_get_payload_from_mpls(
1786                void *mpls, uint16_t *type, uint32_t *remaining);
1787
1788/** Gets a pointer to the payload following a PPPoE header
1789 * @param pppoe      A pointer to the PPPoE header
1790 * @param[out] type  The ethernet type, replaced by the ether type of the
1791 * returned header - 0x0000 if an Ethernet header is returned
1792 * @param[in,out] remaining  Updated with the number of captured bytes remaining
1793 *
1794 * @return A pointer to the header beyond the PPPoE header. NOTE that this
1795 * function will also skip over the PPP header that will immediately follow
1796 * the PPPoE header. This function will return NULL if there are not enough
1797 * bytes remaining to skip past both the PPPoE and PPP headers.
1798 *
1799 * When calling this function, remaining must contain the number of captured
1800 * bytes remaining in the packet starting from the PPPoE header (including the
1801 * PPPoE header itself). remaining will be updated to contain the number of
1802 * bytes remaining after the PPPoE and PPP headers have been removed.
1803 *
1804 * If the PPPoE and PPP headers are complete but there are zero bytes of
1805 * payload after the PPP header, a pointer to where the payload would be is
1806 * returned and remaining will be set to 0.  If the PPPoE or PPP header is
1807 * incomplete, NULL will be returned and remaining will be set to 0. Therefore,
1808 * it is important to check the value of remaining after calling this function.
1809 *
1810 * type will be set to the ether type of the PPP payload. This parameter is
1811 * mandatory - it may not be NULL.
1812 *
1813 */
1814DLLEXPORT void *trace_get_payload_from_pppoe(
1815                void *pppoe, uint16_t *type, uint32_t *remaining);
1816
1817/** Gets a pointer to the payload following a TCP header
1818 * @param tcp           A pointer to the TCP header
1819 * @param[in,out] remaining Updated with the number of captured bytes remaining
1820 *
1821 * @return A pointer to the TCP payload, or NULL if the TCP header is truncated.
1822 *
1823 * When calling this function, remaining must contain the number of captured
1824 * bytes remaining in the packet starting from the TCP header (including the
1825 * TCP header itself). remaining will be updated to contain the number of
1826 * bytes remaining after the TCP header has been skipped.
1827 *
1828 * If the TCP header is complete but there are zero bytes of payload after
1829 * the TCP header, a pointer to where the payload would be is returned and
1830 * remaining will be set to 0.  If the TCP header is incomplete, NULL will be
1831 * returned and remaining will be set to 0. Therefore, it is important to check
1832 * the value of remaining after calling this function.
1833 *
1834 */
1835DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp,
1836                uint32_t *remaining);
1837
1838/** Gets a pointer to the payload following a UDP header
1839 * @param udp           A pointer to the UDP header
1840 * @param[in,out] remaining Updated with the number of captured bytes remaining
1841 *
1842 * @return A pointer to the UDP payload, or NULL if the UDP header is truncated.
1843 *
1844 * When calling this function, remaining must contain the number of captured
1845 * bytes remaining in the packet starting from the UDP header (including the
1846 * UDP header itself). remaining will be updated to contain the number of
1847 * bytes remaining after the UDP header has been skipped.
1848 *
1849 * If the UDP header is complete but there are zero bytes of payload after
1850 * the UDP header, a pointer to where the payload would be is returned and
1851 * remaining will be set to 0.  If the UDP header is incomplete, NULL will be
1852 * returned and remaining will be set to 0. Therefore, it is important to check
1853 * the value of remaining after calling this function.
1854 *
1855 */
1856DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
1857
1858/** Gets a pointer to the payload following a ICMP header
1859 * @param icmp           A pointer to the ICMP header
1860 * @param[in,out] remaining Updated with the number of captured bytes remaining
1861 *
1862 * @return A pointer to the ICMP payload, or NULL if the ICMP header is
1863 * truncated.
1864 *
1865 * When calling this function, remaining must contain the number of captured
1866 * bytes remaining in the packet starting from the ICMP header (including the
1867 * ICMP header itself). remaining will be updated to contain the number of
1868 * bytes remaining after the ICMP header has been skipped.
1869 *
1870 * If the ICMP header is complete but there are zero bytes of payload after
1871 * the ICMP header, a pointer to where the payload would be is returned and
1872 * remaining will be set to 0.  If the ICMP header is incomplete, NULL will be
1873 * returned and remaining will be set to 0. Therefore, it is important to check
1874 * the value of remaining after calling this function.
1875 *
1876 * @note In the case of some ICMP messages, the payload may be the IP header
1877 * from the packet that triggered the ICMP message.
1878 *
1879 */
1880DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp,
1881                uint32_t *remaining);
1882
1883/** Get a pointer to the TCP header (if present)
1884 * @param packet        The packet to get the TCP header from
1885 *
1886 * @return A pointer to the TCP header, or NULL if there is not a complete TCP
1887 * header present in the packet.
1888 *
1889 * This is a short-cut function enabling quick and easy access to the TCP
1890 * header if that is all you care about. However, we recommend the use of the
1891 * more generic trace_get_transport() function instead.
1892 *
1893 * @note Unlike trace_get_transport(), this function will return NULL if the
1894 * TCP header is incomplete or truncated.
1895 */
1896DLLEXPORT SIMPLE_FUNCTION
1897libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
1898
1899/** Get a pointer to the TCP header following an IPv4 header (if present)
1900 * @param ip            The IP header to find the subsequent TCP header for
1901 * @param[in,out] remaining Updated with the number of captured bytes remaining
1902 *
1903 * @return A pointer to the TCP header, or NULL if no TCP header is present in
1904 * the packet.
1905 *
1906 * When calling this function, remaining must contain the number of captured
1907 * bytes remaining in the packet starting from the IP header (including the
1908 * IP header itself). remaining will be updated to contain the number of
1909 * bytes remaining after the IP header has been skipped.
1910 *
1911 * If the IP header is complete but there are zero bytes of payload after
1912 * the IP header, a pointer to where the payload would be is returned and
1913 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1914 * returned and remaining will be set to 0. Therefore, it is important to check
1915 * the value of remaining after calling this function.
1916 *
1917 * @note This function is rather redundant now that the layer 3 header is
1918 * cached. There should be no performance advantage for the user to call this
1919 * function over just calling trace_get_transport().
1920 *
1921 * @note The last parameter has changed from libtrace2
1922 */
1923DLLEXPORT SIMPLE_FUNCTION
1924libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
1925
1926/** Get a pointer to the UDP header (if present)
1927 * @param packet        The packet to get the UDP header from
1928 *
1929 * @return A pointer to the UDP header, or NULL if there is not a complete UDP
1930 * header present in the packet.
1931 *
1932 * This is a short-cut function enabling quick and easy access to the UDP
1933 * header if that is all you care about. However, we recommend the use of the
1934 * more generic trace_get_transport() function instead.
1935 *
1936 * @note Unlike trace_get_transport(), this function will return NULL if the
1937 * UDP header is incomplete or truncated.
1938 */
1939DLLEXPORT SIMPLE_FUNCTION
1940libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
1941
1942/** Get a pointer to the UDP header following an IPv4 header (if present)
1943 * @param ip            The IP header to find the subsequent UDP header for
1944 * @param[in,out] remaining Updated with the number of captured bytes remaining
1945 *
1946 * @return A pointer to the UDP header, or NULL if no UDP header is present in
1947 * the packet.
1948 *
1949 * When calling this function, remaining must contain the number of captured
1950 * bytes remaining in the packet starting from the IP header (including the
1951 * IP header itself). remaining will be updated to contain the number of
1952 * bytes remaining after the IP header has been skipped.
1953 *
1954 * If the IP header is complete but there are zero bytes of payload after
1955 * the IP header, a pointer to where the payload would be is returned and
1956 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1957 * returned and remaining will be set to 0. Therefore, it is important to check
1958 * the value of remaining after calling this function.
1959 *
1960 * @note This function is rather redundant now that the layer 3 header is
1961 * cached. There should be no performance advantage for the user to call this
1962 * function over just calling trace_get_transport().
1963 *
1964 * @note The last parameter has changed from libtrace2
1965 */
1966DLLEXPORT SIMPLE_FUNCTION
1967libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1968
1969/** Get a pointer to the ICMP header (if present)
1970 * @param packet        The packet to get the ICMP header from
1971 *
1972 * @return A pointer to the ICMP header, or NULL if there is not a complete
1973 * ICMP header present in the packet.
1974 *
1975 * This is a short-cut function enabling quick and easy access to the ICMP
1976 * header if that is all you care about. However, we recommend the use of the
1977 * more generic trace_get_transport() function instead.
1978 *
1979 * @note Unlike trace_get_transport(), this function will return NULL if the
1980 * ICMP header is incomplete or truncated.
1981 */
1982DLLEXPORT SIMPLE_FUNCTION
1983libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
1984
1985/** Get a pointer to the ICMP header following an IPv4 header (if present)
1986 * @param ip            The IP header to find the subsequent ICMP header for
1987 * @param[in,out] remaining Updated with the number of captured bytes remaining
1988 *
1989 * @return A pointer to the ICMP header, or NULL if no UDP header is present in
1990 * the packet.
1991 *
1992 * When calling this function, remaining must contain the number of captured
1993 * bytes remaining in the packet starting from the IP header (including the
1994 * IP header itself). remaining will be updated to contain the number of
1995 * bytes remaining after the IP header has been skipped.
1996 *
1997 * If the IP header is complete but there are zero bytes of payload after
1998 * the IP header, a pointer to where the payload would be is returned and
1999 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
2000 * returned and remaining will be set to 0. Therefore, it is important to check
2001 * the value of remaining after calling this function.
2002 *
2003 * @note This function is rather redundant now that the layer 3 header is
2004 * cached. There should be no performance advantage for the user to call this
2005 * function over just calling trace_get_transport().
2006 *
2007 * @note The last parameter has changed from libtrace2
2008 */
2009DLLEXPORT SIMPLE_FUNCTION
2010libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
2011
2012/** Get a pointer to the OSPF header (if present)
2013 * @param packet        The packet to get the OSPF header from
2014 * @param[out] version  The OSPF version number
2015 * @param[out] remaining        Updated with the number of captured bytes remaining
2016 * @return A pointer to the start of the OSPF header or NULL if there is no
2017 * complete OSPF header present in the packet
2018 *
2019 * This is a short-cut function enabling quick and easy access to the OSPF
2020 * header. If you only care about the OSPF header, this function may be useful
2021 * but we otherwise recommend the use of the more generic trace_get_transport()
2022 * function instead.
2023 *
2024 * Upon return, 'version' is updated to contain the OSPF version number for the
2025 * packet so that the returned pointer may be cast to the correct type.
2026 * The version parameter MUST contain a valid pointer; it MUST NOT be NULL.
2027 *
2028 * 'remaining' is also set to contain the number of captured bytes remaining
2029 * starting from the pointer returned by this function.
2030 *
2031 * @note Unlike trace_get_transport(), this function will return NULL if the
2032 * OSPF header is incomplete or truncated.
2033 */
2034DLLEXPORT SIMPLE_FUNCTION
2035void *trace_get_ospf_header(libtrace_packet_t *packet, uint8_t *version,
2036                uint32_t *remaining);
2037
2038/** Get a pointer to the contents of the OSPF packet *after* the OSPF header
2039 * @param header        The OSPF header to get the OSPF contents from
2040 * @param[out] ospf_type        The OSPF packet type
2041 * @param[in, out] remaining    Updated with the number of captured bytes remaining
2042 * @return A pointer to the first byte after the OSPF header.
2043 *
2044 * This function returns a void pointer that can be cast appropriately
2045 * based on the ospf_type. For example, if the ospf_type is TRACE_OSPF_HELLO
2046 * then the return pointer should be cast as a libtrace_ospf_hello_v2_t
2047 * structure.
2048 *
2049 * If the OSPF header is truncated, then NULL will be returned. If the OSPF
2050 * contents are missing or truncated, the pointer to the start of the content
2051 * will still be returned so be careful to check the value of remaining.
2052 *
2053 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2054 * packet starting from the beginning of the OSPF header. It will be updated
2055 * to contain the number of bytes remaining from the start of the OSPF contents.
2056 *
2057 * @note This function only works for OSPF version 2 packets.
2058 * @note Use trace_get_first_ospf_lsa_v2_from_X() and trace_get_next_ospf_lsa_v2() to read the LSAs from Link State Update packets.
2059 * @note Use trace_get_first_ospf_lsa_v2_from_X() and trace_get_next_ospf_lsa_header_v2() to read the LSA headers from Link State Ack packets.
2060 *
2061 */
2062DLLEXPORT SIMPLE_FUNCTION
2063void *trace_get_ospf_contents_v2(libtrace_ospf_v2_t *header,
2064                uint8_t *ospf_type, uint32_t *remaining);
2065
2066/** Get a pointer to the start of the first LSA contained within an LS Update packet
2067 * @param ls_update     Pointer to the LS Update header
2068 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2069 * @return A pointer to the first LSA in the packet.
2070 *
2071 * This function simply skips past the LS Update header to provide a suitable
2072 * pointer to pass into trace_get_next_ospf_lsa_v2.
2073 *
2074 * If the OSPF packet is truncated, then NULL will be returned.
2075 *
2076 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2077 * packet starting from the beginning of the LS Update header. It will be
2078 * updated to contain the number of bytes remaining from the start of the
2079 * first LSA.
2080 *
2081 * @note This function only works for OSPF version 2 packets.
2082 * @note trace_get_next_ospf_lsa_v2() should be subequently used to process the LSAs
2083 */
2084DLLEXPORT SIMPLE_FUNCTION
2085unsigned char *trace_get_first_ospf_lsa_from_update_v2(
2086                libtrace_ospf_ls_update_t *ls_update,
2087                uint32_t *remaining);
2088
2089/** Get a pointer to the start of the first LSA contained within an Database Description packet
2090 * @param db_desc       Pointer to the Database Description header
2091 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2092 * @return A pointer to the first LSA in the packet.
2093 *
2094 * This function simply skips past the Database Description header to provide a
2095 * suitable pointer to pass into trace_get_next_ospf_lsa_header_v2.
2096 *
2097 * If the OSPF packet is truncated, then NULL will be returned.
2098 *
2099 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2100 * packet starting from the beginning of the Database Description header. It
2101 * will be updated to contain the number of bytes remaining from the start of
2102 * the first LSA.
2103 *
2104 * @note This function only works for OSPF version 2 packets.
2105 * @note trace_get_next_ospf_lsa_header_v2() should be subequently used to process the LSA headers
2106 */
2107DLLEXPORT SIMPLE_FUNCTION
2108unsigned char *trace_get_first_ospf_lsa_from_db_desc_v2(
2109                libtrace_ospf_db_desc_v2_t *db_desc,
2110                uint32_t *remaining);
2111
2112/** Get a pointer to the start of the first link contained within a Router LSA
2113 * @param lsa   Pointer to the Router LSA
2114 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2115 * @return A pointer to the first link in the packet.
2116 *
2117 * This function simply skips past the Router LSA header to provide a
2118 * suitable pointer to pass into trace_get_next_ospf_link_v2.
2119 *
2120 * If the OSPF packet is truncated, then NULL will be returned.
2121 *
2122 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2123 * packet starting from the beginning of the Router LSA (not including the LSA
2124 * header) header. It will be updated to contain the number of bytes remaining
2125 * from the start of the first Link.
2126 *
2127 * @note This function only works for OSPF version 2 packets.
2128 * @note trace_get_next_ospf_link_v2() should be subequently used to process
2129 * the links
2130 */
2131DLLEXPORT SIMPLE_FUNCTION
2132unsigned char *trace_get_first_ospf_link_from_router_lsa_v2(
2133                libtrace_ospf_router_lsa_v2_t *lsa,
2134                uint32_t *remaining);
2135
2136/** Parses an OSPF Router LSA Link and finds the next Link (if there is one)
2137 * @param[in,out] current       Pointer to the next Link (updated by this function)
2138 * @param[out] link             Set to point to the Link located at the original value of current
2139 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2140 * @param[out] link_len         Set to the size of the Link pointed to by 'link'
2141 * @return 0 if there are no more links after the current one, 1 otherwise.
2142 *
2143 * When called, 'current' MUST point to an OSPF Router LSA link. Ideally, this
2144 * would come from either a call to
2145 * trace_get_first_ospf_link_from_router_lsa_v2() or a previous call of this
2146 * function.
2147 *
2148 * 'link' will be set to the value of 'current', so that the caller may then
2149 * do any processing they wish on that particular link. 'current' is advanced
2150 * to point to the next link and 'link_len' is updated to report the size of
2151 * the original link.
2152 *
2153 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2154 * packet starting from the beginning of the Link pointed to by 'current'.
2155 * It will be updated to contain the number of bytes remaining from the start
2156 * of the next link.
2157 *
2158 * If this function returns 0 but 'link' is NOT NULL, that link is still valid
2159 * but there are no more links after this one.
2160 * If this function returns 0 AND link is NULL, the link is obviously not
2161 * suitable for processing.
2162 *
2163 * @note This function only works for OSPF version 2 packets.
2164 */
2165DLLEXPORT SIMPLE_FUNCTION
2166int trace_get_next_ospf_link_v2(unsigned char **current,
2167                libtrace_ospf_link_v2_t **link,
2168                uint32_t *remaining,
2169                uint32_t *link_len);
2170
2171/** Parses an OSPF LSA and finds the next LSA (if there is one)
2172 * @param[in,out] current       Pointer to the next LSA (updated by this function)
2173 * @param[out] lsa_hdr          Set to point to the header of the LSA located at the original value of current
2174 * @param[out] lsa_body         Set to point to the body of the LSA located at the original value of current
2175 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2176 * @param[out] lsa_type         Set to the type of the LSA located at the original value of current
2177 * @param[out] lsa_length       Set to the size of the LSA located at the original value of current
2178 * @return 1 if there are more LSAs after the current one, 0 if there are no more LSAs to come, and -1 if the current LSA is incomplete, truncated or invalid.
2179 *
2180 * When called, 'current' MUST point to an OSPF LSA. Ideally, this would come
2181 * from either a call to trace_get_first_ospf_lsa_from_update_v2() or a
2182 * previous call of this function.
2183 *
2184 * This function should only be used to access COMPLETE LSAs, i.e. LSAs that
2185 * have both a header and a body. In OSPFv2, only the LSAs contained within
2186 * LSA Update packets meet this requirement. trace_get_next_ospf_lsa_header_v2
2187 * should be used to read header-only LSAs, e.g. those present in LS Acks.
2188 *
2189 * 'lsa_hdr' will be set to the value of 'current', so that the caller may then
2190 * do any processing they wish on that particular LSA. 'lsa_body' will be set
2191 * to point to the first byte after the LSA header. 'current' is advanced
2192 * to point to the next LSA. 'lsa_length' is updated to contain the size of
2193 * the parsed LSA, while 'lsa_type' is set to indicate the LSA type.
2194 *
2195 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2196 * packet starting from the beginning of the LSA pointed to by 'current'.
2197 * It will be updated to contain the number of bytes remaining from the start
2198 * of the next LSA.
2199 *
2200 * If this function returns 0 but 'lsa_hdr' is NOT NULL, that LSA is still
2201 * valid but there are no more LSAs after this one.
2202 * If this function returns 0 AND 'lsa_hdr' is NULL, the LSA is obviously not
2203 * suitable for processing.
2204 *
2205 * It is also recommended to check the value of 'lsa_body' before
2206 * de-referencing it. 'lsa_body' will be set to NULL if there is only an LSA
2207 * header present.
2208 *
2209 * @note This function only works for OSPF version 2 packets.
2210 *
2211 */
2212DLLEXPORT SIMPLE_FUNCTION
2213int trace_get_next_ospf_lsa_v2(unsigned char **current,
2214                libtrace_ospf_lsa_v2_t **lsa_hdr,
2215                unsigned char **lsa_body,
2216                uint32_t *remaining,
2217                uint8_t *lsa_type,
2218                uint16_t *lsa_length);
2219
2220/** Parses an OSPF LSA header and finds the next LSA (if there is one)
2221 * @param[in,out] current       Pointer to the next LSA (updated by this function)
2222 * @param[out] lsa_hdr          Set to point to the header of the LSA located at the original value of current
2223 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2224 * @param[out] lsa_length       Set to the size of the LSA located at the original value of current
2225 * @return 1 if there are more LSAs after the current one, 0 if there are no more LSAs to come, and -1 if the current LSA is incomplete, truncated or invalid.
2226 *
2227 * When called, 'current' MUST point to an OSPF LSA. Ideally, this would come
2228 * from either a call to trace_get_first_ospf_lsa_from_db_desc_v2() or a
2229 * previous call of this function.
2230 *
2231 * This function should only be used to access LSA headers, i.e. LSAs that
2232 * have a header only. In OSPFv2, the LSAs contained within LSA Ack and
2233 * Database Description packets meet this requirement.
2234 * trace_get_next_ospf_lsa_v2 should be used to read full LSAs, e.g. those
2235 * present in LS Updates.
2236 *
2237 * 'lsa_hdr' will be set to the value of 'current', so that the caller may then
2238 * do any processing they wish on that particular LSA header. 'current' is
2239 * advanced to point to the next LSA header. 'lsa_length' is updated to contain
2240 * the size of the parsed LSA header.
2241 *
2242 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2243 * packet starting from the beginning of the LSA pointed to by 'current'.
2244 * It will be updated to contain the number of bytes remaining from the start
2245 * of the next LSA.
2246 *
2247 * If this function returns 0 but 'lsa_hdr' is NOT NULL, that LSA is still
2248 * valid but there are no more LSAs after this one.
2249 * If this function returns 0 AND 'lsa_hdr' is NULL, the LSA is obviously not
2250 * suitable for processing.
2251 *
2252 * @note This function only works for OSPF version 2 packets.
2253 *
2254 */
2255DLLEXPORT SIMPLE_FUNCTION
2256int trace_get_next_ospf_lsa_header_v2(unsigned char **current,
2257                libtrace_ospf_lsa_v2_t **lsa_hdr,
2258                uint32_t *remaining,
2259                uint8_t *lsa_type,
2260                uint16_t *lsa_length);
2261
2262/** Extracts the metric field from an AS External LSA packet
2263 *
2264 * @param as_lsa        The AS External LSA
2265 * @returns The value of the metric field
2266 *
2267 * The metric field in the AS External LSA packet is a 24-bit value, which
2268 * is difficult to extract correctly. To avoid byte-ordering issues, use this
2269 * function which will extract the correct value for you.
2270 */
2271DLLEXPORT SIMPLE_FUNCTION
2272uint32_t trace_get_ospf_metric_from_as_external_lsa_v2(
2273                libtrace_ospf_as_external_lsa_v2_t *as_lsa);
2274
2275/** Extracts the metric field from a Summary LSA packet
2276 *
2277 * @param sum_lsa       The Summary LSA
2278 * @returns The value of the metric field
2279 *
2280 * The metric field in the Summary LSA packet is a 24-bit value, which
2281 * is difficult to extract correctly. To avoid byte-ordering issues, use this
2282 * function which will extract the correct value for you.
2283 */
2284DLLEXPORT SIMPLE_FUNCTION
2285uint32_t trace_get_ospf_metric_from_summary_lsa_v2(
2286                libtrace_ospf_summary_lsa_v2_t *sum_lsa);
2287
2288
2289/** Gets the destination MAC address for a given packet
2290 * @param packet        The packet to extract the destination MAC address from
2291 *
2292 * @return A pointer to the destination MAC address field in the layer 2
2293 * header, (or NULL if there is no destination MAC address or layer 2 header
2294 * available)
2295 *
2296 * @note This is a zero-copy function, so the memory that the returned pointer
2297 * points to is part of the packet itself.
2298 */
2299DLLEXPORT SIMPLE_FUNCTION
2300uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
2301
2302/** Gets the source MAC address for a given packet
2303 * @param packet        The packet to extract the source MAC address from
2304 *
2305 * @return A pointer to the source MAC address field in the layer 2
2306 * header, (or NULL if there is no source MAC address or layer 2 header
2307 * available)
2308 *
2309 * @note This is a zero-copy function, so the memory that the returned pointer
2310 * points to is part of the packet itself.
2311 */
2312DLLEXPORT SIMPLE_FUNCTION
2313uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
2314
2315/** Get the source IP address for a given packet
2316 * @param packet        The packet to extract the source IP address from
2317 * @param addr          A pointer to a sockaddr structure to store the address
2318 *                      in. If NULL, static storage is used instead.
2319 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
2320 * platforms a sockaddr holding a MAC address. Returns NULL if no source IP
2321 * address was available.
2322 *
2323 * @note The best way to use this function is to pass in a pointer to the
2324 * struct sockaddr_storage for the addr parameter. This will avoid problems
2325 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
2326 * IPv4.
2327 */
2328DLLEXPORT SIMPLE_FUNCTION
2329struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
2330                struct sockaddr *addr);
2331
2332/** Get the destination IP address for a given packet
2333 * @param packet        The packet to extract the destination IP address from
2334 * @param addr          A pointer to a sockaddr structure to store the address
2335 *                      in. If NULL, static storage is used instead.
2336 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
2337 * platforms a sockaddr holding a MAC address. Returns NULL if no destination
2338 * IP address was available.
2339 *
2340 * @note The best way to use this function is to pass in a pointer to the
2341 * struct sockaddr_storage for the addr parameter. This will avoid problems
2342 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
2343 * IPv4.
2344 */
2345DLLEXPORT SIMPLE_FUNCTION
2346struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
2347                struct sockaddr *addr);
2348
2349
2350/** Parses an IP or TCP option
2351 * @param[in,out] ptr   The pointer to the current option
2352 * @param[in,out] len   The total length of all the remaining options
2353 * @param[out] type     The type of the option
2354 * @param[out] optlen   The length of the option
2355 * @param[out] data     The data of the option
2356 *
2357 * @return bool true if there is another option (and the fields are filled in)
2358 *               or false if this was the last option.
2359 *
2360 * This updates ptr to point to the next option after this one, and updates
2361 * len to be the number of bytes remaining in the options area.  Type is updated
2362 * to be the code of this option, and data points to the data of this option,
2363 * with optlen saying how many bytes there are.
2364 *
2365 * @note Beware of fragmented packets.
2366 */
2367DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
2368                        unsigned char *type,
2369                        unsigned char *optlen,
2370                        unsigned char **data);
2371
2372/*@}*/
2373
2374/** @name Time
2375 * These functions deal with the timestamp describing when a packet was
2376 * captured and can convert it into various formats
2377 * @{
2378 */
2379
2380/** Get the packet timestamp in the DAG time format
2381 * @param packet        The packet to extract the timestamp from
2382 *
2383 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
2384 * past 1970-01-01, the lower 32bits are partial seconds)
2385 */
2386DLLEXPORT SIMPLE_FUNCTION
2387uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
2388
2389/** Get the packet timestamp as a struct timeval
2390 * @param packet        The packet to extract the timestamp from
2391 *
2392 * @return The time that this packet was captured in a struct timeval
2393 */
2394DLLEXPORT SIMPLE_FUNCTION
2395struct timeval trace_get_timeval(const libtrace_packet_t *packet);
2396
2397/** Get the packet timestamp as a struct timespec
2398 * @param packet        The packet to extract the timestamp from
2399 *
2400 * @return The time that this packet was captured in a struct timespec
2401 */
2402DLLEXPORT SIMPLE_FUNCTION
2403struct timespec trace_get_timespec(const libtrace_packet_t *packet);
2404
2405/** Get the packet timestamp in floating point seconds
2406 * @param packet        The packet to extract the timestamp from
2407 *
2408 * @return time that this packet was seen in 64-bit floating point seconds from
2409 * the UNIX epoch (1970-01-01 00:00:00 UTC).
2410 */
2411DLLEXPORT SIMPLE_FUNCTION
2412double trace_get_seconds(const libtrace_packet_t *packet);
2413
2414/** Seek within an input trace to a time specified in floating point seconds
2415 * @param trace         The input trace to seek within
2416 * @param seconds       The time to seek to, in floating point seconds
2417 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
2418 * the error that occurred.
2419 *
2420 * This will make the next packet read to be the first packet to occur at or
2421 * after the specified time.  This must be called in the configuration state
2422 * (i.e. before trace_start() or after trace_pause()).
2423 *
2424 * The time format accepted by this function is 64-bit floating point seconds
2425 * since the UNIX epoch (1970-01-01 00:00:00 UTC), i.e. the same format as
2426 * trace_get_seconds().
2427 *
2428 * @note This function may be extremely slow.
2429 */
2430DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
2431
2432/** Seek within an input trace to a time specified as a timeval
2433 * @param trace         The input trace to seek within
2434 * @param tv            The time to seek to, as a timeval
2435 *
2436 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
2437 * the error that occurred.
2438 *
2439 * This will make the next packet read to be the first packet to occur at or
2440 * after the specified time.  This must be called in the configuration state
2441 * (i.e. before trace_start() or after trace_pause()).
2442 *
2443 * @note This function may be extremely slow.
2444 */
2445DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
2446
2447/** Seek within an input trace to a time specified as an ERF timestamp
2448 * @param trace         The input trace to seek within
2449 * @param ts            The time to seek to, as an ERF timestamp
2450 *
2451 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
2452 * the error that occurred.
2453 *
2454 * This will make the next packet read to be the first packet to occur at or
2455 * after the specified time.  This must be called in the configuration state
2456 * (i.e. before trace_start() or after trace_pause()).
2457 *
2458 * The time format accepted by this function is the ERF timestamp, which is a
2459 * 64-bit value where the upper 32 bits are seconds since the UNIX epoch and
2460 * the lower 32 bits are partial seconds.
2461 *
2462 * @note This function may be extremely slow.
2463 */
2464DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
2465
2466/*@}*/
2467
2468/** @name Sizes
2469 * This section deals with finding or setting the various different lengths
2470 * that a packet can have, e.g. capture lengths, wire lengths, etc.
2471 * @{
2472 */
2473/** Get the current size of the packet (in bytes), taking into account any
2474 * truncation or snapping that may have previously been performed.
2475 *
2476 * @param packet        The packet to determine the capture length for
2477 * @return The size of the packet read from the input trace, i.e. what is
2478 * actually available to libtrace at the moment.
2479 *
2480 * @note Most traces are header captures, so this value may not be the same
2481 * as the size of the packet when it was captured. Use trace_get_wire_length()
2482 * to get the original size of the packet.
2483 
2484 * @note This can (and often is) different for different packets in a trace!
2485 
2486 * @note This is sometimes called the "snaplen".
2487 *
2488 * @note The return size refers to the network-level payload of the packet and
2489 * does not include any capture framing headers. For example, an Ethernet
2490 * packet with an empty TCP packet will return sizeof(ethernet_header) +
2491 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
2492 * (pcap/erf/etc) header.
2493 */
2494DLLEXPORT SIMPLE_FUNCTION
2495size_t trace_get_capture_length(const libtrace_packet_t *packet);
2496
2497/** Get the size of the packet as it was originally seen on the wire (in bytes).
2498 * @param packet        The packet to determine the wire length for
2499 * @return The size of the packet as it was on the wire.
2500 *
2501 * @note This value may not be the same as the capture length, due to
2502 * truncation.
2503 *
2504 * @note trace_get_wire_length \em includes  the Frame Check Sequence. This is
2505 * different behaviour compared to most PCAP-based tools.
2506 *
2507 * @note The return size refers to the network-level payload of the packet and
2508 * does not include any capture framing headers. For example, an Ethernet
2509 * packet with an empty TCP packet will return sizeof(ethernet_header) +
2510 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
2511 * (pcap/erf/etc) header.
2512 */
2513DLLEXPORT SIMPLE_FUNCTION
2514size_t trace_get_wire_length(const libtrace_packet_t *packet);
2515
2516/** Get the length of the capture framing headers (in bytes).
2517 * @param packet        The packet to determine the framing length for
2518 * @return The size of the capture format header encapsulating the packet.
2519 *
2520 * @note This length corresponds to the difference between the amount of
2521 * memory required to store a captured packet and the capture length reported
2522 * by trace_capture_length()
2523 */
2524DLLEXPORT SIMPLE_FUNCTION
2525size_t trace_get_framing_length(const libtrace_packet_t *packet);
2526
2527/** Get the length of the original payload content of the packet (in bytes).
2528 * @param packet        The packet to determine the payload length for
2529 * @return The size of the packet payload (without headers). Returns 0 if
2530 * unable to calculate payload length correctly.
2531 *
2532 * This function reports the amount of data that followed the transport header
2533 * when the packet was originally captured, i.e. prior to any snapping. Best
2534 * described as the wire length minus the packet headers.
2535 *
2536 * Currently only supports some protocols and will return 0 if an unsupported
2537 * protocol header is encountered, or if one of the headers is truncated.
2538 *
2539 * @note Supports IPv4, IPv6, TCP, UDP and ICMP.
2540 */
2541DLLEXPORT SIMPLE_FUNCTION
2542size_t trace_get_payload_length(const libtrace_packet_t *packet);
2543
2544/** Truncate ("snap") the packet to the suggested length
2545 * @param packet        The packet to truncate
2546 * @param size          The new length of the packet (in bytes)
2547 *
2548 * @return The new capture length of the packet or the original capture
2549 * length of the packet if unchanged.
2550 *
2551 * This function will modify the capture length of the given packet. The wire
2552 * length will not be changed, so you can always determine what the original
2553 * packet size was, prior to the truncation.
2554 *
2555 * @note You can only use this function to decrease the capture length. Any
2556 * attempt to increase capture length will have no effect.
2557 */
2558DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
2559
2560/*@}*/
2561
2562
2563/** Gets the link layer type for a packet
2564 * @param packet        The packet to extract the link layer type for
2565 * @return A libtrace_linktype_t describing the link layer protocol being used
2566 * by this packet.
2567 */
2568DLLEXPORT SIMPLE_FUNCTION
2569libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
2570
2571/** Set the direction flag for a packet, if the capture format supports
2572 * direction tagging.
2573 *
2574 * @param packet        The packet to set the direction for
2575 * @param direction     The new direction
2576 * @returns -1 on error, or the direction that was set.
2577 *
2578 * @note Few capture formats actually support direction tagging. Most notably,
2579 * we cannot set the direction on PCAP packets.
2580 */
2581DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
2582
2583/** Get the direction flag for a packet, if it has one.
2584 * @param packet  The packet to get the direction for
2585 *
2586 * @return A value representing the direction flag, or -1 if this is not
2587 * supported by the capture format.
2588 *
2589 * The direction is defined as 0 for packets originating locally (ie, outbound)
2590 * and 1 for packets originating remotely (ie, inbound). Other values are
2591 * possible, which might be overloaded to mean special things for certain
2592 * traces, e.g. in the Waikato traces, 2 is used to represent an "Unknown"
2593 * direction.
2594 *
2595 * For DAG/ERF traces, the direction is extracted from the "Interface" bits in
2596 * the ERF header, which can range from 0 - 3.
2597 */
2598DLLEXPORT SIMPLE_FUNCTION
2599libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
2600
2601/** @name BPF
2602 * This section deals with using Berkley Packet Filters to filter input traces
2603 * @{
2604 */
2605/** Creates a BPF filter
2606 * @param filterstring The filter string describing the BPF filter to create
2607 * @return An opaque pointer to a libtrace_filter_t object
2608 *
2609 * @note The filter is not actually compiled at this point, so no correctness
2610 * tests are performed here. trace_create_filter() will always return ok, but
2611 * if the filter is poorly constructed an error will be generated when the
2612 * filter is actually used.
2613 */
2614DLLEXPORT SIMPLE_FUNCTION
2615libtrace_filter_t *trace_create_filter(const char *filterstring);
2616
2617/** Create a BPF filter based on pre-compiled byte-code.
2618 * @param bf_insns      A pointer to the start of the byte-code
2619 * @param bf_len        The number of BPF instructions 
2620 * @return              An opaque pointer to a libtrace_filter_t object
2621 * @note                The supplied byte-code is not checked for correctness.
2622 *                      Instead, incorrect byte-code will generate an error
2623 *                      once the filter is actually used.
2624 * @author              Scott Raynel
2625 */
2626DLLEXPORT libtrace_filter_t *
2627trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len);
2628
2629/** Apply a BPF filter to a packet
2630 * @param filter        The filter to be applied       
2631 * @param packet        The packet to be matched against the filter
2632 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
2633 *
2634 * @note Due to the way BPF filters are built, the filter is not actually
2635 * compiled until the first time trace_create_filter is called. If your filter
2636 * is incorrect, it will generate an error message and assert, exiting the
2637 * program. This behaviour may change to a more graceful handling of this error
2638 * in the future.
2639 */
2640DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
2641                const libtrace_packet_t *packet);
2642
2643/** Destroy a BPF filter
2644 * @param filter        The filter to be destroyed
2645 *
2646 * Deallocates all the resources associated with a BPF filter.
2647 */
2648DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
2649/*@}*/
2650
2651/** @name Portability
2652 * This section contains functions that deal with portability issues, e.g. byte
2653 * ordering.
2654 * @{
2655 */
2656
2657/** Converts an ethernet address to a printable string
2658 * @param addr  Ethernet address in network byte order
2659 * @param buf   Buffer to store the ascii representation, or NULL to indicate
2660 * that static storage should be used.
2661 * @return buf, or if buf is NULL then a statically allocated buffer.
2662 *
2663 * This function is similar to the GNU ether_ntoa_r function, with a few
2664 * minor differences.  If NULL is passed as buf, then the function will
2665 * use an internal static buffer. If NULL isn't passed then the function
2666 * will use that buffer instead.
2667 *
2668 * The address pointers returned by trace_get_source_mac() and
2669 * trace_get_destination_mac() can be passed directly into this function.
2670 *
2671 * @note The type of addr isn't struct ether_addr as it is with ether_ntoa_r,
2672 * however it is bit compatible so that a cast will work.
2673 */
2674DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
2675
2676/** Convert a string to an ethernet address
2677 * @param buf   A string containing an Ethernet address in hex format
2678 * delimited with :'s.
2679 * @param addr  Buffer to store the binary representation, or NULL to indicate
2680 * that static storage should be used.
2681 * @return addr, or if addr is NULL then a statically allocated buffer.
2682 *
2683 * This function is similar to the GNU ether_aton_r function, with a few
2684 * minor differences.  If NULL is passed as addr, then the function will
2685 * use an internal static buffer. If NULL isn't passed then the function will
2686 * use that buffer instead.
2687 *
2688 * The address returned by this function will be in network byte order.
2689 *
2690 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
2691 * however it is bit compatible so that a cast will work.
2692 */
2693DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
2694
2695/*@}*/
2696
2697/** @name Ports
2698 * This section contains functions for dealing with port numbers at the
2699 * transport layer.
2700 *
2701 * @{
2702 */
2703
2704/** An indication of which port is the "server" port for a given port pair */
2705typedef enum {
2706        USE_DEST,       /**< Destination port is the server port */
2707        USE_SOURCE      /**< Source port is the server port */
2708} serverport_t;
2709
2710/** Gets the source port for a given packet
2711 * @param packet        The packet to get the source port from
2712 * @return The source port in HOST byte order or 0 if no suitable port number
2713 * can be extracted from the packet.
2714 *
2715 * This function will return 0 if the transport protocol is known not to
2716 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2717 * present in the packet or the transport header has been truncated such that
2718 * the port fields are not readable.
2719 *
2720 * @note If the transport protocol is not known by libtrace, the first two
2721 * bytes of the transport header will be treated as the source port field.
2722 */
2723DLLEXPORT SIMPLE_FUNCTION
2724uint16_t trace_get_source_port(const libtrace_packet_t *packet);
2725
2726/** Gets the destination port for a given packet
2727 * @param packet        The packet to get the destination port from
2728 * @return The destination port in HOST byte order or 0 if no suitable port
2729 * number can be extracted from the packet.
2730 *
2731 * This function will return 0 if the transport protocol is known not to
2732 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2733 * present in the packet or the transport header has been truncated such that
2734 * the port fields are not readable.
2735 *
2736 * @note If the transport protocol is not known by libtrace, the third and
2737 * fourth bytes of the transport header will be treated as the destination
2738 * port field.
2739 *
2740 */
2741DLLEXPORT SIMPLE_FUNCTION
2742uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
2743
2744/** Hint at which of the two provided ports is the server port.
2745 *
2746 * @param protocol      The IP layer protocol, eg 6 (tcp), 17 (udp)
2747 * @param source        The source port from the packet
2748 * @param dest          The destination port from the packet
2749 *
2750 * @return one of USE_SOURCE or USE_DEST describing on which of the two ports
2751 * is most likely to be the server port.
2752 *
2753 * @note Ports must be provided in HOST byte order!
2754 *
2755 * This function is based almost entirely on heuristics and should not be
2756 * treated as a definitive means of identifying the server port. However, it
2757 * is deterministic, so it is very handy for identifying both halves of the
2758 * same flow.
2759 */
2760DLLEXPORT SIMPLE_FUNCTION
2761int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
2762
2763/*@}*/
2764
2765/** @name Wireless trace support
2766 * Functions to access wireless information from packets that have wireless
2767 * monitoring headers such as Radiotap or Prism.
2768 *
2769 * The trace_get_wireless_* functions provide an abstract interface for
2770 * retrieving information from wireless traces. They take a pointer to the
2771 * wireless monitoring header (usually found with trace_get_packet_meta()) and
2772 * the linktype of the header passed in.
2773 *
2774 * All of the trace_get_wireless_* functions return false if the requested
2775 * information was unavailable, or true if it was. The actual data is stored
2776 * in an output variable supplied by the caller. Values returned into the
2777 * output variable will always be returned in host byte order.
2778 * @{
2779 */
2780
2781
2782#ifndef ARPHRD_80211_RADIOTAP
2783/** libc doesn't define this yet, so we have to do so ourselves */
2784#define ARPHRD_80211_RADIOTAP 803
2785#endif
2786
2787/** Get the wireless Timer Synchronisation Function
2788 *
2789 * Gets the value of the timer synchronisation function for this frame, which
2790 * is a value in microseconds indicating the time that the first bit of the
2791 * MPDU was received by the MAC.
2792 *
2793 * @param linkptr The wireless meta header
2794 * @param linktype The linktype of the wireless meta header passed in
2795 * @param[out] tsft The value of the timer synchronisation function.
2796 * @return true if the field was available, false if not.
2797 */
2798DLLEXPORT bool trace_get_wireless_tsft(void *linkptr,
2799        libtrace_linktype_t linktype, uint64_t *tsft);
2800
2801/** Get the wireless data rate
2802 *
2803 * @param linkptr The wireless meta header
2804 * @param linktype The linktype of the wireless meta header passed in
2805 * @param[out] rate The data-rate of the frame in units of 500kbps
2806 * @return true if the field was available, false if not.
2807 */
2808DLLEXPORT bool trace_get_wireless_rate(void *linkptr,
2809        libtrace_linktype_t linktype, uint8_t *rate);
2810
2811/** Get the wireless channel frequency
2812 * @param linkptr The wireless meta header
2813 * @param linktype The linktype of the wireless meta header passed in
2814 * @param[out] freq The frequency in MHz of the channel the frame was
2815 * transmitted or received on.
2816 * @return true if the field was available, false if not.
2817 */
2818DLLEXPORT bool trace_get_wireless_freq(void *linkptr,
2819        libtrace_linktype_t linktype, uint16_t *freq);
2820
2821/** Get the wireless signal strength in dBm
2822 * @param linkptr The wireless meta header
2823 * @param linktype The linktype of the wireless meta header passed in
2824 * @param[out] strength The RF signal power at the antenna, in dB difference
2825 * from 1mW.
2826 * @return true if the field was available, false if not.
2827 */
2828DLLEXPORT bool trace_get_wireless_signal_strength_dbm(void *linkptr,
2829        libtrace_linktype_t linktype, int8_t *strength);
2830
2831/** Get the wireless noise strength in dBm
2832 * @param linkptr The wireless meta header
2833 * @param linktype The linktype of the wireless meta header passed in
2834 * @param[out] strength The RF noise power at the antenna, in dB difference
2835 * from 1mW.
2836 * @return true if the field was available, false if not.
2837 */
2838DLLEXPORT bool trace_get_wireless_noise_strength_dbm(void *linkptr,
2839        libtrace_linktype_t linktype, int8_t *strength);
2840
2841/** Get the wireless signal strength in dB
2842 * @param linkptr The wireless meta header
2843 * @param linktype The linktype of the wireless meta header passed in
2844 * @param[out] strength The RF signal power at the antenna, in dB difference
2845 * from a fixed reference.
2846 * @return true if the field was available, false if not.
2847 */
2848DLLEXPORT bool trace_get_wireless_signal_strength_db(void *linkptr,
2849        libtrace_linktype_t linktype, uint8_t *strength);
2850
2851/** Get the wireless noise strength in dB
2852 * @param linkptr The wireless meta header
2853 * @param linktype The linktype of the wireless meta header passed in
2854 * @param[out] strength The RF noise power at the antenna, in dB difference
2855 * from a fixed reference.
2856 * @return true if the field was available, false if not.
2857 */
2858DLLEXPORT bool trace_get_wireless_noise_strength_db(void *linkptr,
2859        libtrace_linktype_t linktype, uint8_t *strength);
2860
2861/** Get the wireless transmit attenuation
2862 * @param linkptr The wireless meta header
2863 * @param linktype The linktype of the wireless meta header passed in
2864 * @param[out] attenuation The transmit power as a unitless distance from
2865 * maximum power set at factory calibration. 0 indicates maximum transmission
2866 * power.
2867 * @return true if the field was available, false if not.
2868 */
2869DLLEXPORT bool trace_get_wireless_tx_attenuation(void *linkptr,
2870        libtrace_linktype_t linktype, uint16_t *attenuation);
2871
2872/** Get the wireless transmit attenuation in dB
2873 * @param linkptr The wireless meta header
2874 * @param linktype The linktype of the wireless meta header passed in
2875 * @param[out] attenuation The transmit power as dB difference from maximum
2876 * power set at factory calibration. 0 indicates maximum power.
2877 * @return true if the field was available, false if not.
2878 */
2879DLLEXPORT bool trace_get_wireless_tx_attenuation_db(void *linkptr,
2880        libtrace_linktype_t linktype, uint16_t *attenuation);
2881
2882/** Get the wireless transmit power in dBm
2883 * @param linkptr The wireless meta header
2884 * @param linktype The linktype of the wireless meta header passed in
2885 * @param[out] txpower The transmit power as dB from a 1mW reference. This is
2886 * the absolute power level measured at the antenna port. 
2887 * @return true if the field was available, false if not.
2888 */
2889DLLEXPORT bool trace_get_wireless_tx_power_dbm(void *linkptr,
2890                libtrace_linktype_t linktype, int8_t *txpower);
2891
2892/** Get the wireless antenna
2893 * @param linkptr The wireless meta header
2894 * @param linktype The linktype of the wireless meta header passed in
2895 * @param[out] antenna The antenna that was used to transmit or receive the
2896 * frame.
2897 * @return true if the field was available, false if not.
2898 */
2899DLLEXPORT bool trace_get_wireless_antenna(void *linkptr,
2900        libtrace_linktype_t linktype, uint8_t *antenna);
2901
2902/*@}*/
2903
2904#ifdef __cplusplus
2905} /* extern "C" */
2906#endif /* #ifdef __cplusplus */
2907#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.