source: lib/libtrace.h.in @ 341c38f

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

Merge remote-tracking branch 'upstream/develop' into develop

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