source: lib/libtrace.h.in @ 9b42f3e

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

Added m4 macros to check for gcc compiler attributes

Instead of relying on GNUC, we now individually check
whether each attribute is supported by the C compiler and set
preprocessor macros accordingly.

Removed similar attribute definitions from wandio.h, as these are
not used at all by libwandio.

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