source: lib/libtrace.h.in @ 8af0d01

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

Remove sliding window option, this was very complicated to ensure thread safety. For now its best left out.

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