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

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

Fix some BSD warnings/errors

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