source: lib/libtrace.h.in @ b27ed21

develop
Last change on this file since b27ed21 was b27ed21, checked in by Jacob Van Walraven <jcv9@…>, 2 years ago

Add datatype enum to libtrace_meta_item_t structure, Convert interface speeds from network to host byte ordering

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