source: lib/libtrace.h.in @ 5478d3d

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 5478d3d was 5478d3d, checked in by Shane Alcock <salcock@…>, 6 years ago

Fix all outstanding warnings

Implemented trace_get_statistics for formats that were missing it, so
we no longer need to use the deprecated trace_get_dropped_packets anywhere
within libtrace.

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