source: lib/libtrace.h.in @ 322c516

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 322c516 was f625817, checked in by Shane Alcock <salcock@…>, 6 years ago

Reworked callback API and removed old per_msg and reporter functions

Updated tracertstats to use the new callback API.

Extended the callback approach to the reporter thread as well as the per
packet threads.

Added libtrace_callback_set_t structure, which is used to register the
user callback functions.

Added callback functionality for MESSAGE_RESULT (needed now that reporter
threads also do callbacks) and MESSAGE_USER (for user-defined messages). The
MESSAGE_USER callback is essentially the same as the old per_msg function
style.

Updated combiners to use send_message to pass results to the reporter thread.
send_message itself is now no longer static, so that combiners can use it.

Disabled building of tracestats_parallel as it was using the older version
of the callback API. Will update in a future commit.

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