source: lib/libtrace.h.in @ d57ae6f

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

Added a new format: rawerf. This is a special format that will force ERF files to be opened and read without going through the compression autodetection. It will only work for uncompressed ERF files.

This should be a handy option for people who are working with lots of uncompressed ERF files -- running the compression autodetection against uncompressed ERF traces can very occasionally produce a false positive, i.e. decide the file is compressed. By skipping the autodetection and forcing the traces to be read as uncompressed ERF, the threat of this bug will go away.

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