source: lib/libtrace.h.in @ f7bcbfb

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

Fixed warnings that cropped up when building with clang

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