source: lib/libtrace.h.in @ a9d0e40

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

trace_prepare_packet is now an external API function

Previously it was for internal use only, but I now have a use
case where it makes sense to have access to it externally
(specifically: "casting" DAG buffer pointers to libtrace packets
so I can dynamically snap them without having the overhead of
the whole of libtrace).

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