source: lib/libtrace.h.in @ 17c5749

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

Merge remote-tracking branch 'upsteam/develop' into FixingDPDK

Conflicts:

README
lib/format_linux.c
test/Makefile

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