source: lib/libtrace.h.in @ 49f8ceb

develop
Last change on this file since 49f8ceb was 49f8ceb, checked in by Jacob Van Walraven <jcv9@…>, 22 months ago

Fix incorrect type id for pcapng_meta, fix compiler warnings

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