source: lib/libtrace.h.in @ bcfe4ea

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

Adds per thread storage to for the format to use against libtrace_threads.
Passes threads as arguments to reads to save overhead of looking these up.
Various changes to the DPDK system including registering a thread to allow our threads to be start with different DPDK thread numbers for thread local memory caches.

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