source: lib/libtrace.h.in @ 2725318

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

Cleanup some of the assertions

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