source: lib/libtrace.h.in @ d439067

develop
Last change on this file since d439067 was d439067, checked in by Shane Alcock <salcock@…>, 23 months ago

Move packet cached fields into a distinct structure.

This will help tidy up the packet structure a little, as well as
simplify the cache clearing process (and maybe even speed it up
a little).

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