source: lib/libtrace.h.in @ e90d5a8

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