source: lib/libtrace.h.in @ 756b8f9

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 756b8f9 was a984307, checked in by Shane Alcock <salcock@…>, 5 years ago

Merge remote-tracking branch 'origin/develop' into libtrace4

Conflicts:

INSTALL
README
lib/format_dpdk.c
lib/trace.c
tools/tracesplit/tracesplit.c

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