source: lib/libtrace.h.in @ 11041eb

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