source: lib/libtrace.h.in @ c04929c

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since c04929c was c04929c, checked in by Richard Sanger <rjs51@…>, 8 years ago

Added the new live trace format Intel DPDK - See the wiki for more documentation

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