source: lib/libtrace.h.in @ 4631115

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

Merge branch 'master' into develop.

Update to include fixes/features etc from the 3.0.22 release.

Conflicts:

README
lib/format_dag25.c
lib/format_dpdk.c
lib/format_linux.c
lib/trace.c
test/Makefile

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