source: lib/libtrace.h.in @ 29bbef0

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

My work from over summer, with a few things tidied up and updated to include recent commits/patches to bring this up to date. Still very much work in progress.

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