source: lib/libtrace.h.in @ c7e547e

cachetimestampsdevelopdpdk-ndagetsiliverc-4.0.3rc-4.0.4ringdecrementfixringperformance
Last change on this file since c7e547e was c7e547e, checked in by Shane Alcock <salcock@…>, 3 years ago

Added a dpdkndag format for faster ndag reading

Instead of joining a multicast group and receiving nDAG packets
via the networking stack, this new format uses DPDK to sniff
the multicast direct from the wire. This should save some effort
shuffling the packets back through the kernel's networking stack.

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