source: lib/libtrace.h.in @ fe4940e

developringperformance
Last change on this file since fe4940e was fe4940e, checked in by Shane Alcock <salcock@…>, 2 years ago

Cache packet framing length to avoid multiple calculations

Also when stripping vlan etc. headers, don't try to memmove if zero
bytes are going to be moved.

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