source: lib/libtrace.h.in @ e3a639a

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

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

Conflicts:

lib/Makefile.am
lib/libtrace_int.h

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