source: lib/libtrace.h.in @ 526d9d0

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

Move the accepted packet count to dispatching packets for threads.
Accounting for the doubled count when using a single threaded format.

Rework statistics logic slightly to remove duplicated code.

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