source: lib/libtrace.h.in @ a49a9eb

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

Add an object cache with thread local caches
All packets used by a trace are put through this.
Adds bulk read/write operations to the ringbuffer (used by the object cache)
Replace semaphores with condition variables to support these bulk operations.
Internally use bulk read operations from a single threaded formats to reduce lock overhead.
Replaces the asserts around pthread_* functions with a version that will still run the command if NDEBUG

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