source: lib/libtrace.h.in @ 8753bb8

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