source: lib/libtrace.h.in @ d994324

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since d994324 was d994324, checked in by Richard Sanger <rsangerarj@…>, 7 years ago

Remove anything to do with the combiner from set configuration and removes any options/storage related to these such as the next expected packet.

Instead this is done using trace_set_combiner now, and the for the built-in combiners.h header. This is a lot more flexible and allows the users to specify there own combiner, and any number of options for it.

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