source: lib/libtrace.h.in @ 1935565

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 1935565 was d500159, checked in by Richard Sanger <rjs51@…>, 9 years ago

Added new format type ring:. Works just like the old int:. In fact much of the int: code is reused. However ring: uses PACKET_MMAP ring buffers to improve performance

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