source: lib/libtrace.h.in @ 317e903

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformance
Last change on this file since 317e903 was 317e903, checked in by Shane Alcock <salcock@…>, 4 years ago

Add support for PPTP GRE

Credit to EaseTheWorld? for producing the original patch for this.

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