source: lib/libtrace.h.in @ 2627439

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