source: lib/libtrace.h.in @ 10f924c

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

Fixed broken -fvisibility check in configure

  • Added a m4 script that does this properly rather than our hax gcc version check.
  • Tidied up CFLAGS in configure so we aren't adding the same sets of flags multiple times
  • Created a wandio_internal.h file for storing global variables that shouldn't be made public

Thanks to Alistair King, whose patch to try and make this work for
non-gcc systems brought my attention to just how broken this was :)

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