source: lib/libtrace.h.in @ 74ecbc7

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 74ecbc7 was 74ecbc7, checked in by Shane Alcock <salcock@…>, 7 years ago

Add more user-friendly checks for output file compression options

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