source: lib/libtrace.h.in @ b53d019

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since b53d019 was b53d019, checked in by Shane Alcock <salcock@…>, 6 years ago

Tidy up some documentation in libtrace.h

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