source: lib/libtrace.h.in @ ea75ec2

cachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformance
Last change on this file since ea75ec2 was ea75ec2, checked in by Shane Alcock <salcock@…>, 3 years ago

Add new format for receiving and parsing nDAG records

Also added new statistic: missing_records -- this tracks records
that have gone missing between the original capture device and
the current processing host (i.e. lost on the network between the
two). Useful for nDAG as there is a distinction between packets
dropped by the DAG and packets that were not received by the nDAG
client.

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