source: lib/libtrace.h.in @ 991ff43

cachetimestampsdevelopetsiliverc-4.0.4ringdecrementfixringperformance
Last change on this file since 991ff43 was 991ff43, checked in by Shane Alcock <salcock@…>, 3 years ago

Add back in some missing ETSILI constants.

These seem to have been lost when rebasing back onto the develop
branch.

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