source: lib/libtrace.h.in @ 6b98325

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 6b98325 was 6b98325, checked in by Richard Sanger <rsangerarj@…>, 6 years ago

Removes pconfig_input() and replaces trace_config() with trace_set_<option>

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