source: lib/libtrace.h.in @ 25a3255

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

More assertion cleanup

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