source: lib/libtrace.h.in @ c876f29

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

Resolve merge conflict attempt 2

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