source: lib/libtrace.h.in @ 771ab22

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 771ab22 was 97d170d, checked in by Dan Collins <dan@…>, 7 years ago

Merge branch 'develop' of github.com:wanduow/libtrace into develop

Conflicts:

lib/format_dag25.c

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