source: lib/libtrace.h.in @ 89cc0cc

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

More assertion cleanups

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