source: lib/libtrace.h.in @ 64d4398

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

Fixed bugs when converting packets from Linux SLL (pcap) to ERF - thanks to Simon Wadsworth for reporting this problem

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