source: lib/libtrace.h.in @ 979a84f4

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 979a84f4 was 979a84f4, checked in by Shane Alcock <salcock@…>, 10 years ago
  • We now cache the wire length as well
  • If we're going to cache the link type, we should probably use it whenever we call trace_get_link_type()!
  • Property mode set to 100644
File size: 96.9 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_PIM       = 103,  /**< Protocol Independant Multicast */
394        TRACE_IPPROTO_SCTP      = 132   /**< Stream Control Transmission Protocol */
395} libtrace_ipproto_t;
396
397/** Ethertypes supported by Libtrace */
398typedef enum {
399        /* Numbers <=1500 are of course, LLC/SNAP */
400        TRACE_ETHERTYPE_IP      = 0x0800,       /**< IPv4 */
401        TRACE_ETHERTYPE_ARP     = 0x0806,       /**< Address resolution protocol */
402        TRACE_ETHERTYPE_RARP    = 0x8035,       /**< Reverse ARP */
403        TRACE_ETHERTYPE_8021Q   = 0x8100,       /**< 802.1q VLAN Extended Header */
404        TRACE_ETHERTYPE_IPV6    = 0x86DD,       /**< IPv6 */
405        TRACE_ETHERTYPE_MPLS    = 0x8847,       /**< MPLS Unicast traffic */
406        TRACE_ETHERTYPE_MPLS_MC = 0x8848,       /**< MPLS Multicast traffic */
407        TRACE_ETHERTYPE_PPP_DISC= 0x8863,       /**< PPPoE Service Discovery */
408        TRACE_ETHERTYPE_PPP_SES = 0x8864        /**< PPPoE Session Messages */
409} libtrace_ethertype_t;
410
411/** The libtrace packet structure. Applications shouldn't be
412 * meddling around in here
413 */
414typedef struct libtrace_packet_t {
415        struct libtrace_t *trace;       /**< Pointer to the trace */
416        void *header;                   /**< Pointer to the framing header */
417        void *payload;                  /**< Pointer to the link layer */
418        void *buffer;                   /**< Allocated buffer */
419        libtrace_rt_types_t  type;      /**< RT protocol type for the packet */
420        buf_control_t buf_control;      /**< Describes memory ownership */
421        int capture_length;             /**< Cached capture length */
422        int wire_length;                /**< Cached wire length */
423        int payload_length;             /**< Cached payload length */
424        void *l2_header;                /**< Cached link header */
425        libtrace_linktype_t link_type;  /**< Cached link type */
426        uint32_t l2_remaining;          /**< Cached link remaining */
427        void *l3_header;                /**< Cached l3 header */
428        uint16_t l3_ethertype;          /**< Cached l3 ethertype */
429        uint32_t l3_remaining;          /**< Cached l3 remaining */
430        void *l4_header;                /**< Cached transport header */
431        uint8_t transport_proto;        /**< Cached transport protocol */
432        uint32_t l4_remaining;          /**< Cached transport remaining */
433} libtrace_packet_t;
434
435
436/** Trace directions.
437 * Note that these are the directions used by convention. More directions
438 * are possible, not just these 3, and that they may not conform to this
439 * convention.
440 */
441typedef enum {
442        TRACE_DIR_OUTGOING = 0,         /**< Packets originating "inside" */
443        TRACE_DIR_INCOMING = 1,         /**< Packets originating "outside" */
444        TRACE_DIR_OTHER    = 2          /**< Packets with an unknown direction, or one that's unknown */
445} libtrace_direction_t;
446
447/** Enumeration of Radiotap fields */
448typedef enum {
449    TRACE_RADIOTAP_TSFT = 0, /**< Timer synchronisation function, in microseconds (uint64) */
450    TRACE_RADIOTAP_FLAGS = 1, /**< Wireless flags (uint8) */
451    TRACE_RADIOTAP_RATE = 2, /**< Bitrate in units of 500kbps (uint8) */
452    TRACE_RADIOTAP_CHANNEL = 3, /**< Frequency in MHz (uint16) and channel flags (uint16) */
453    TRACE_RADIOTAP_FHSS = 4, /**< FHSS hop set (uint8) and hopping pattern (uint8) */
454    TRACE_RADIOTAP_DBM_ANTSIGNAL = 5, /**< Signal power in dBm (int8) */
455    TRACE_RADIOTAP_DBM_ANTNOISE = 6, /**< Noise power in dBm (int8) */
456    TRACE_RADIOTAP_LOCK_QUALITY = 7, /**< Barker Code lock quality (uint16) */
457    TRACE_RADIOTAP_TX_ATTENUATION = 8, /**< TX attenuation as unitless distance from max power (uint16) */
458    TRACE_RADIOTAP_DB_TX_ATTENUATION = 9, /**< TX attenutation as dB from max power (uint16) */
459    TRACE_RADIOTAP_DBM_TX_POWER = 10, /**< TX Power in dBm (int8) */
460    TRACE_RADIOTAP_ANTENNA = 11, /**< Antenna frame was rx'd or tx'd on (uint8) */
461    TRACE_RADIOTAP_DB_ANTSIGNAL = 12, /**< Signal power in dB from a fixed reference (uint8) */
462    TRACE_RADIOTAP_DB_ANTNOISE = 13, /**< Noise power in dB from a fixed reference (uint8) */
463    TRACE_RADIOTAP_RX_FLAGS = 14, /** Properties of received frame (uint16) */
464    TRACE_RADIOTAP_TX_FLAGS = 15, /** Properties of transmitted frame (uint16) */
465    TRACE_RADIOTAP_RTS_RETRIES = 16, /** Number of rts retries frame used (uint8) */
466    TRACE_RADIOTAP_DATA_RETRIES = 17, /** Number of unicast retries a transmitted frame used (uint8) */
467    TRACE_RADIOTAP_EXT = 31
468} libtrace_radiotap_field_t;
469
470
471/** @name Protocol structures
472 * These convenience structures provide portable versions of the headers
473 * for a variety of protocols.
474 * @{
475 */
476
477#ifdef WIN32
478#pragma pack(push)
479#pragma pack(1)
480#endif
481
482/** Generic IP header structure */
483typedef struct libtrace_ip
484{
485#if BYTE_ORDER == LITTLE_ENDIAN
486    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
487    LT_BITFIELD8 ip_v:4;                /**< Version */
488#elif BYTE_ORDER == BIG_ENDIAN
489    LT_BITFIELD8 ip_v:4;                /**< Version */
490    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
491#else
492#   error "Adjust your <bits/endian.h> defines"
493#endif
494    uint8_t  ip_tos;                    /**< Type of Service */
495    uint16_t ip_len;                    /**< Total Length */
496    int16_t  ip_id;                     /**< Identification */
497    uint16_t ip_off;                    /**< IP Fragment offset (and flags) */
498    uint8_t  ip_ttl;                    /**< Time to Live */
499    uint8_t  ip_p;                      /**< Protocol */
500    uint16_t ip_sum;                    /**< Checksum */
501    struct in_addr ip_src;              /**< Source Address */
502    struct in_addr ip_dst;              /**< Destination Address */
503} PACKED libtrace_ip_t;
504
505/** IPv6 header extension structure */
506typedef struct libtrace_ip6_ext
507{
508        uint8_t nxt;    /**< Next header */
509        uint8_t len;    /**< Length of the current header */
510} PACKED libtrace_ip6_ext_t;
511
512typedef struct libtrace_ip6_frag
513{
514        uint8_t nxt;    /**< Next header */
515        uint8_t res;    /**< Reserved */
516        uint16_t frag_off;      /**< Fragment Offset (includes M flag) */
517        uint32_t ident; /** Fragment identification */
518} PACKED libtrace_ip6_frag_t;
519
520/** Generic IPv6 header structure
521 *
522 * @note The flow label field also includes the Version and Traffic Class
523 * fields, because we haven't figured out a nice way to deal with fields
524 * crossing byte boundaries on both Big and Little Endian machines */
525typedef struct libtrace_ip6
526{
527    uint32_t flow;                      /**< Flow label */
528    uint16_t plen;                      /**< Payload length */
529    uint8_t nxt;                        /**< Next header */
530    uint8_t hlim;                       /**< Hop limit */
531    struct in6_addr ip_src;             /**< Source address */
532    struct in6_addr ip_dst;             /**< Destination address */
533} PACKED libtrace_ip6_t;
534
535/** Generic TCP header structure */
536typedef struct libtrace_tcp
537  {
538    uint16_t source;            /**< Source Port */
539    uint16_t dest;              /**< Destination port */
540    uint32_t seq;               /**< Sequence number */
541    uint32_t ack_seq;           /**< Acknowledgement Number */
542#  if BYTE_ORDER == LITTLE_ENDIAN
543    LT_BITFIELD8 ecn_ns:1;      /**< ECN Nonce Sum */
544    LT_BITFIELD8 res1:3;        /**< Reserved bits */
545    LT_BITFIELD8 doff:4;        /**< Data Offset */
546    LT_BITFIELD8 fin:1;         /**< FIN */
547    LT_BITFIELD8 syn:1;         /**< SYN flag */
548    LT_BITFIELD8 rst:1;         /**< RST flag */
549    LT_BITFIELD8 psh:1;         /**< PuSH flag */
550    LT_BITFIELD8 ack:1;         /**< ACK flag */
551    LT_BITFIELD8 urg:1;         /**< URG flag */
552    LT_BITFIELD8 ece:1;         /**< ECN Echo */
553    LT_BITFIELD8 cwr:1;         /**< ECN CWR */
554#  elif BYTE_ORDER == BIG_ENDIAN
555    LT_BITFIELD8 doff:4;        /**< Data offset */
556    LT_BITFIELD8 res1:3;        /**< Reserved bits */
557    LT_BITFIELD8 ecn_ns:1;      /**< ECN Nonce Sum */
558    LT_BITFIELD8 cwr:1;         /**< ECN CWR */
559    LT_BITFIELD8 ece:1;         /**< ECN Echo */
560    LT_BITFIELD8 urg:1;         /**< URG flag */
561    LT_BITFIELD8 ack:1;         /**< ACK flag */
562    LT_BITFIELD8 psh:1;         /**< PuSH flag */
563    LT_BITFIELD8 rst:1;         /**< RST flag */
564    LT_BITFIELD8 syn:1;         /**< SYN flag */
565    LT_BITFIELD8 fin:1;         /**< FIN flag */
566#  else
567#   error "Adjust your <bits/endian.h> defines"
568#  endif
569    uint16_t window;            /**< Window Size */
570    uint16_t check;             /**< Checksum */
571    uint16_t urg_ptr;           /**< Urgent Pointer */
572} PACKED libtrace_tcp_t;
573
574/** Generic UDP header structure */
575typedef struct libtrace_udp {
576  uint16_t      source;         /**< Source port */
577  uint16_t      dest;           /**< Destination port */
578  uint16_t      len;            /**< Length */
579  uint16_t      check;          /**< Checksum */
580} PACKED libtrace_udp_t;
581
582/** Generic ICMP header structure */
583typedef struct libtrace_icmp
584{
585  uint8_t type;         /**< Message Type */
586  uint8_t code;         /**< Type Sub-code */
587  uint16_t checksum;            /**< Checksum */
588  union
589  {
590    struct
591    {
592      uint16_t  id;             /**< ID of the Echo request */
593      uint16_t  sequence;       /**< Sequence number of the Echo request */
594    } echo;                     /**< Echo Datagram */
595    uint32_t    gateway;        /**< Gateway Address */
596    struct
597    {
598      uint16_t  unused;         /**< Unused */
599      uint16_t  mtu;            /**< Next-hop MTU */
600    } frag;                     /**< Path MTU Discovery */
601  } un;                         /**< Union for Payloads of Various ICMP Codes */
602} PACKED libtrace_icmp_t;
603
604/** Generic LLC/SNAP header structure */
605typedef struct libtrace_llcsnap
606{
607/* LLC */
608  uint8_t dsap;                 /**< Destination Service Access Point */
609  uint8_t ssap;                 /**< Source Service Access Point */
610  uint8_t control;              /**< Control field */
611/* SNAP */
612  LT_BITFIELD32 oui:24;         /**< Organisationally Unique Identifier (scope)*/
613  uint16_t type;                /**< Protocol within OUI */
614} PACKED libtrace_llcsnap_t;
615
616/** 802.3 frame */
617typedef struct libtrace_ether
618{
619  uint8_t ether_dhost[6];       /**< Destination Ether Addr */
620  uint8_t ether_shost[6];       /**< Source Ether Addr */
621  uint16_t ether_type;          /**< Packet Type ID Field (next-header) */
622} PACKED libtrace_ether_t;
623
624/** 802.1Q frame */
625typedef struct libtrace_8021q
626{
627  LT_BITFIELD16 vlan_pri:3;      /**< VLAN User Priority */
628  LT_BITFIELD16 vlan_cfi:1;      /**< VLAN Format Indicator,
629                                   * 0 for ethernet, 1 for token ring */
630  LT_BITFIELD16 vlan_id:12;      /**< VLAN Id */
631  uint16_t vlan_ether_type;      /**< VLAN Sub-packet Type ID Field
632                                   * (next-header)*/
633} PACKED libtrace_8021q_t;
634
635/** ATM User Network Interface (UNI) Cell. */
636typedef struct libtrace_atm_cell
637{
638  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
639  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
640  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
641  LT_BITFIELD32 pt:3;           /**< Payload Type */
642  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
643  LT_BITFIELD32 hec:8;          /**< Header Error Control */
644} PACKED libtrace_atm_cell_t;
645
646/** ATM Network Node/Network Interface (NNI) Cell. */
647typedef struct libtrace_atm_nni_cell
648{
649  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
650  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
651  LT_BITFIELD32 pt:3;           /**< Payload Type */
652  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
653  LT_BITFIELD32 hec:8;          /**< Header Error Control */
654} PACKED libtrace_atm_nni_cell_t;
655
656/** Captured UNI cell.
657 *
658 * Endace don't capture the HEC, presumably to keep alignment.  This
659 * version of the \ref libtrace_atm_cell is used when dealing with DAG
660 * captures of uni cells.
661 *
662 */
663typedef struct libtrace_atm_capture_cell
664{
665  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
666  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
667  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
668  LT_BITFIELD32 pt:3;           /**< Payload Type */
669  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
670} PACKED libtrace_atm_capture_cell_t;
671
672/** Captured NNI cell.
673 *
674 * Endace don't capture the HEC, presumably to keep alignment.  This
675 * version of the \ref libtrace_atm_nni_cell is used when dealing with DAG
676 * captures of nni cells.
677 *
678 */
679typedef struct libtrace_atm_nni_capture_cell
680{
681  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
682  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
683  LT_BITFIELD32 pt:3;           /**< Payload Type */
684  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
685  LT_BITFIELD32 hec:8;          /**< Header Error Control */
686} PACKED libtrace_atm_nni_capture_cell_t;
687
688/** PPP header */
689typedef struct libtrace_ppp
690{
691 /* I can't figure out where the hell these two variables come from. They're
692  * definitely not in RFC 1661 which defines PPP. Probably some weird thing
693  * relating to the lack of distinction between PPP, HDLC and CHDLC */
694       
695/* uint8_t address; */          /**< PPP Address (0xFF - All stations) */
696/* uint8_t header;  */          /**< PPP Control (0x03 - Unnumbered info) */
697 uint16_t protocol;             /**< PPP Protocol (htons(0x0021) - IPv4 */
698} PACKED libtrace_ppp_t;
699
700/** PPPoE header */
701typedef struct libtrace_pppoe
702{
703 LT_BITFIELD8 version:4;        /**< Protocol version number */
704 LT_BITFIELD8 type:4;           /**< PPPoE Type */
705 uint8_t code;                  /**< PPPoE Code */
706 uint16_t session_id;           /**< Session Identifier */
707 uint16_t length;               /**< Total Length of the PPP packet */
708} PACKED libtrace_pppoe_t;
709
710/** 802.11 header */
711typedef struct libtrace_80211_t {
712#if BYTE_ORDER == LITTLE_ENDIAN
713        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
714        LT_BITFIELD32      type:2;      /**< Frame Type */
715        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
716#else
717        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
718        LT_BITFIELD32      type:2;      /**< Frame Type */
719        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
720#endif
721
722#if BYTE_ORDER == LITTLE_ENDIAN
723        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
724        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
725        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
726        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
727        LT_BITFIELD32      power:1;     /**< Power Management mode */
728        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
729        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
730        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
731#else
732        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
733        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
734        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
735        LT_BITFIELD32      power:1;     /**< Power Management mode */
736        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
737        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
738        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
739        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
740#endif
741        uint16_t     duration;  /**< Duration value for NAV calculation */
742        uint8_t      mac1[6];   /**< MAC Address 1 */
743        uint8_t      mac2[6];   /**< MAC Address 2 */
744        uint8_t      mac3[6];   /**< MAC Address 3 */
745        uint16_t     SeqCtl;    /**< Sequence Control */       
746        uint8_t      mac4[6];   /**< MAC Address 4 */
747} PACKED libtrace_80211_t;
748
749/** The Radiotap header pre-amble
750 *
751 * All Radiotap headers start with this pre-amble, followed by the fields
752 * specified in the it_present bitmask. If bit 31 of it_present is set, then
753 * another bitmask follows.
754 * @note All of the radiotap data fields are in little-endian byte-order.
755 */
756typedef struct libtrace_radiotap_t {
757    uint8_t     it_version; /**< Radiotap version */
758    uint8_t     it_pad; /**< Padding for natural alignment */
759    uint16_t    it_len; /**< Length in bytes of the entire Radiotap header */
760    uint32_t    it_present; /**< Which Radiotap fields are present */
761} PACKED libtrace_radiotap_t;
762
763
764#ifdef WIN32
765#pragma pack(pop)
766#endif
767
768
769/*@}*/
770
771/** Prints help information for libtrace
772 *
773 * Function prints out some basic help information regarding libtrace,
774 * and then prints out the help() function registered with each input module
775 */
776DLLEXPORT void trace_help(void);
777
778/** Causes a libtrace reader to stop blocking whilst waiting on new packets
779 * and immediately return EOF.
780 *
781 * This function is useful if you are handling signals within your libtrace
782 * program. If a live source is not receiving any packets (or they are being
783 * filtered), a call to trace_read_packet will result in an infinite loop as
784 * it will block until a packet is received. Normally, a SIGINT would cause the
785 * program to end and thus break the loop, but if you are handling the signal
786 * yourself then that signal will never reach libtrace.
787 *
788 * Instead this function sets a global variable within libtrace that will
789 * cause a blocking live capture to break on the next internal timeout,
790 * allowing control to be returned to the user and their own signal handling
791 * to kick in.
792 */
793DLLEXPORT void trace_interrupt(void);
794
795/** @name Trace management
796 * These members deal with creating, configuring, starting, pausing and
797 * cleaning up a trace object
798 *@{
799 */
800
801/** Takes a uri and splits it into a format and uridata component.
802 * @param uri           The uri to be parsed
803 * @param [out] format  A pointer that will be updated to point to an allocated
804 *                      string holding the format component of the URI
805 * @return NULL if an error occured, otherwise return a pointer to the uridata
806 * component
807 *
808 * @note The format component is strdup'd by this function, so be sure to free
809 * it when you are done with the split URI. Similarly, do not pass a pointer
810 * to an allocated string into this function as the 'format' parameter, as
811 * that memory will be leaked and replaced with the strdup'd format.
812 */
813DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
814
815/** Create an input trace from a URI
816 *
817 * @param uri A valid libtrace URI to be opened
818 * @return An opaque pointer to a libtrace_t
819 *
820 * Some valid URI's are:
821 *  - erf:/path/to/erf/file
822 *  - erf:-  (stdin)
823 *  - dag:/dev/dagcard                 
824 *  - pcapint:pcapinterface                (eg: pcap:eth0)
825 *  - pcap:/path/to/pcap/file
826 *  - pcap:-
827 *  - rt:hostname
828 *  - rt:hostname:port
829 *
830 *  If an error occured when attempting to open the trace file, a
831 *  trace is still returned so trace_is_err() should be called to find out
832 *  if an error occured. The trace is created in the configuration state, you
833 *  must call trace_start before attempting to read packets from the trace.
834 */
835DLLEXPORT libtrace_t *trace_create(const char *uri);
836
837/** Creates a "dummy" trace file that has only the format type set.
838 *
839 * @param uri A valid (but fake) URI indicating the format of the dummy trace that is to be created.
840 * @return An opaque pointer to a (sparsely initialised) libtrace_t
841 *
842 * Only the format portion of the uri parameter matters - the 'file' being
843 * opened does not have to exist.
844 *
845 * @note IMPORTANT: Do not attempt to call trace_read_packet or other such
846 * functions with the dummy trace. Its intended purpose is to provide access
847 * to the format functions where the original trace may no longer exist or is
848 * not the correct format, e.g. reading ERF packets from an RT input.
849 */
850DLLEXPORT libtrace_t *trace_create_dead(const char *uri);
851
852/** Creates a trace output file from a URI.
853 *
854 * @param uri The uri string describing the output format and destination
855 * @return An opaque pointer to a libtrace_output_t
856 *
857 * Valid URIs include:
858 *  - erf:/path/to/erf/file
859 *  - pcap:/path/to/pcap/file
860 *
861 *  If an error occured when attempting to open the output trace, a trace is
862 *  still returned but trace_errno will be set. Use trace_is_err_out() and
863 *  trace_perror_output() to get more information.
864 */
865DLLEXPORT libtrace_out_t *trace_create_output(const char *uri);
866
867/** Start an input trace
868 * @param libtrace      The trace to start
869 * @return 0 on success, -1 on failure
870 *
871 * This does the actual work of starting the input trace and applying
872 * all the config options.  This may fail, returning -1. The libtrace error
873 * handling functions can be used to get more information about what
874 * specifically went wrong.
875 */
876DLLEXPORT int trace_start(libtrace_t *libtrace);
877
878/** Pauses an input trace
879 * @param libtrace      The trace to pause
880 * @return 0 on success, -1 on failure
881 *
882 * This stops an input trace that is in progress and returns you to the
883 * configuration state.  Any packets that arrive on a live capture after
884 * trace_pause() has been called will be discarded.  To resume the trace, call
885 * trace_start().
886 */
887DLLEXPORT int trace_pause(libtrace_t *libtrace);
888
889/** Start an output trace
890 * @param libtrace      The trace to start
891 * @return 0 on success, -1 on failure
892 *
893 * This does the actual work with starting a trace capable of writing packets. 
894 * This generally creates the output file.
895 */
896DLLEXPORT int trace_start_output(libtrace_out_t *libtrace);
897
898/** Valid configuration options for input traces */
899typedef enum {
900        /** Maximum number of bytes to be captured for any given packet */
901        TRACE_OPTION_SNAPLEN,   
902
903        /** If enabled, places a live capture interface into promiscuous mode */
904        TRACE_OPTION_PROMISC,   
905
906        /** Apply this filter to all packets read from this trace */
907        TRACE_OPTION_FILTER,   
908
909        /** Defines the frequency of meta-data reporting, e.g. DUCK packets */
910        TRACE_OPTION_META_FREQ,
911
912        /** If enabled, the libtrace event API will ignore time gaps between
913         * packets when reading from a trace file */
914        TRACE_OPTION_EVENT_REALTIME
915} trace_option_t;
916
917/** Sets an input config option
918 * @param libtrace      The trace object to apply the option to
919 * @param option        The option to set
920 * @param value         The value to set the option to
921 * @return -1 if option configuration failed, 0 otherwise
922 * This should be called after trace_create, and before trace_start
923 */
924DLLEXPORT int trace_config(libtrace_t *libtrace,
925                trace_option_t option,
926                void *value);
927
928/** Valid compression types
929 * Note, this must be kept in sync with WANDIO_COMPRESS_* numbers in wandio.h
930 */
931typedef enum {
932        TRACE_OPTION_COMPRESSTYPE_NONE = 0, /**< No compression */
933        TRACE_OPTION_COMPRESSTYPE_ZLIB = 1, /**< GZip Compression */
934        TRACE_OPTION_COMPRESSTYPE_BZ2  = 2, /**< BZip2 Compression */
935        TRACE_OPTION_COMPRESSTYPE_LZO  = 3  /**< LZO Compression */
936} trace_option_compresstype_t;
937
938/** Valid configuration options for output traces */
939typedef enum {
940        /** File flags to use when opening an output file, e.g. O_APPEND */
941        TRACE_OPTION_OUTPUT_FILEFLAGS,
942        /** Compression level: 0 = no compression, 1 = faster compression,
943         * 9 = better compression */
944        TRACE_OPTION_OUTPUT_COMPRESS,
945        /** Compression type, see trace_option_compresstype_t */
946        TRACE_OPTION_OUTPUT_COMPRESSTYPE
947} trace_option_output_t;
948
949/** Sets an output config option
950 *
951 * @param libtrace      The output trace object to apply the option to
952 * @param option        The option to set
953 * @param value         The value to set the option to
954 * @return -1 if option configuration failed, 0 otherwise
955 * This should be called after trace_create_output, and before
956 * trace_start_output
957 */
958DLLEXPORT int trace_config_output(libtrace_out_t *libtrace,
959                trace_option_output_t option,
960                void *value
961                );
962
963/** Close an input trace, freeing up any resources it may have been using
964 *
965 * @param trace         The input trace to be destroyed
966 *
967 */
968DLLEXPORT void trace_destroy(libtrace_t *trace);
969
970/** Close a dummy trace file, freeing up any resources it may have been using
971 * @param trace         The dummy trace to be destroyed
972 */
973DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
974
975/** Close an output trace, freeing up any resources it may have been using
976 * @param trace         The output trace to be destroyed
977 */
978DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
979
980/** Check (and clear) the current error state of an input trace
981 * @param trace         The input trace to check the error state on
982 * @return The current error status and message
983 *
984 * This reads and returns the current error state and sets the current error
985 * to "no error".
986 */
987DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
988
989/** Indicate if there has been an error on an input trace
990 * @param trace         The input trace to check the error state on
991 * @return true if an error has occurred, false otherwise
992 *
993 * @note This does not clear the error status, and only returns true or false.
994 */
995DLLEXPORT bool trace_is_err(libtrace_t *trace);
996
997/** Outputs the error message for an input trace to stderr and clear the error
998 * status.
999 * @param trace         The input trace with the error to output
1000 * @param msg           The message to prepend to the error
1001 *
1002 * This function does clear the error status.
1003 */
1004DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...) PRINTF(2,3);
1005
1006/** Checks (and clears) the current error state for an output trace
1007 * @param trace         The output trace to check the error state on
1008 * @return The current error status and message
1009 *
1010 * This reads and returns the current error state and sets the current error
1011 * to "no error".
1012 */
1013DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
1014
1015/** Indicates if there is an error on an output trace
1016 * @param trace         The output trace to check the error state on
1017 * @return true if an error has occurred, false otherwise.
1018 *
1019 * This does not clear the error status, and only returns true or false.
1020 */
1021DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
1022
1023/** Outputs the error message for an output trace to stderr and clear the error
1024 * status.
1025 * @param trace         The output trace with the error to output
1026 * @param msg           The message to prepend to the error
1027 * This function does clear the error status.
1028 */
1029DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...)
1030        PRINTF(2,3);
1031
1032/** Returns the number of packets observed on an input trace.
1033 * Includes the number of packets counted as early as possible, before
1034 * filtering, and includes dropped packets.
1035 *
1036 * @param trace         The input trace to examine
1037 * @returns The number of packets seen at the capture point before filtering.
1038 *
1039 * If the number is not known, this function will return UINT64_MAX
1040 */
1041DLLEXPORT
1042uint64_t trace_get_received_packets(libtrace_t *trace);
1043
1044/** Returns the number of packets that were captured, but discarded for not
1045 * matching a provided filter.
1046 *
1047 * @param trace         The input trace to examine
1048 * @returns The number of packets that were successfully captured, but filtered
1049 *
1050 * If the number is not known, this function will return UINT64_MAX
1051 */
1052DLLEXPORT
1053uint64_t trace_get_filtered_packets(libtrace_t *trace);
1054
1055/** Returns the number of packets that have been dropped on an input trace due
1056 * to lack of buffer space on the capturing device.
1057 *
1058 * @param trace         The input trace to examine
1059 * @returns The number of packets captured, but dropped due to buffer overruns
1060 *
1061 * If the number is not known, this function will return UINT64_MAX
1062 */
1063DLLEXPORT
1064uint64_t trace_get_dropped_packets(libtrace_t *trace);
1065
1066/** Returns the number of packets that have been read from the input trace using
1067 * trace_read_packet().
1068 *
1069 * @param trace         The input trace to examine
1070 * @returns The number of packets that have been read by the libtrace user.
1071 *
1072 * If the number is not known, this function will return UINT64_MAX
1073 */
1074DLLEXPORT
1075uint64_t trace_get_accepted_packets(libtrace_t *trace);
1076
1077
1078/*@}*/
1079
1080/** @name Reading / Writing packets
1081 * These members deal with creating, reading and writing packets
1082 *
1083 * @{
1084 */
1085
1086/** Create a new packet object
1087 *
1088 * @return A pointer to an initialised libtrace_packet_t object
1089 */
1090DLLEXPORT libtrace_packet_t *trace_create_packet(void);
1091
1092/** Copy a packet object
1093 * @param packet        The source packet to copy
1094 * @return A new packet which has the same content as the source packet
1095 *
1096 * @note This always involves a copy, which can be slow.  Use of this
1097 * function should be avoided where possible.
1098 *
1099 * @par The reason you would want to use this function is that a zerocopied
1100 * packet from a device will be stored using memory owned by the device which
1101 * may be a limited resource. Copying the packet will ensure that the packet
1102 * is now stored in memory owned and managed by libtrace.
1103 */
1104DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
1105
1106/** Destroy a packet object
1107 * @param packet        The packet to be destroyed
1108 *
1109 */
1110DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
1111
1112
1113/** Read the next packet from an input trace
1114 *
1115 * @param trace         The libtrace opaque pointer for the input trace
1116 * @param packet        The packet opaque pointer
1117 * @return 0 on EOF, negative value on error, number of bytes read when successful.
1118 *
1119 * @note The number of bytes read is usually (but not always) the same as
1120 * trace_get_framing_length()+trace_get_capture_length() depending on the
1121 * trace format.
1122 *
1123 * @note The trace must have been started with trace_start before calling
1124 * this function
1125 *
1126 * @note When reading from a live capture, this function will block until a
1127 * packet is observed on the capture interface. The libtrace event API
1128 * (e.g. trace_event()) should be used if non-blocking operation is required.
1129 */
1130DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
1131
1132/** Event types
1133 * see \ref libtrace_eventobj_t and \ref trace_event
1134 */
1135typedef enum {
1136        TRACE_EVENT_IOWAIT,     /**< Wait on the given file descriptor */
1137        TRACE_EVENT_SLEEP,      /**< Sleep for the given amount of time */
1138        TRACE_EVENT_PACKET,     /**< Packet has been read from input trace */
1139        TRACE_EVENT_TERMINATE   /**< End of input trace */
1140} libtrace_event_t;
1141
1142/** Structure returned by libtrace_event explaining what the current event is */
1143typedef struct libtrace_eventobj_t {
1144        libtrace_event_t type; /**< Event type (iowait,sleep,packet) */
1145       
1146        /** If the event is IOWAIT, the file descriptor to wait on */
1147        int fd;               
1148        /** If the event is SLEEP, the amount of time to sleep for in seconds */
1149        double seconds;       
1150        /** If the event is PACKET, the value returned by trace_read_packet() */
1151        int size;
1152} libtrace_eventobj_t;
1153
1154/** Processes the next libtrace event from an input trace.
1155 * @param trace The libtrace opaque pointer for the input trace
1156 * @param packet The libtrace_packet opaque pointer to use for reading packets
1157 * @return A libtrace_event struct containing the event type and details of the event.
1158 *
1159 * Type can be:
1160 *  TRACE_EVENT_IOWAIT  Waiting on I/O on a file descriptor
1161 *  TRACE_EVENT_SLEEP   Wait a specified amount of time for the next event
1162 *  TRACE_EVENT_PACKET  Packet was read from the trace
1163 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
1164 */
1165DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
1166                libtrace_packet_t *packet);
1167
1168
1169/** Write one packet out to the output trace
1170 *
1171 * @param trace         The libtrace_out opaque pointer for the output trace
1172 * @param packet        The packet opaque pointer of the packet to be written
1173 * @return The number of bytes written out, if zero or negative then an error has occured.
1174 */
1175DLLEXPORT int trace_write_packet(libtrace_out_t *trace, libtrace_packet_t *packet);
1176
1177/** Gets the capture format for a given packet.
1178 * @param packet        The packet to get the capture format for.
1179 * @return The capture format of the packet
1180 *
1181 * @note Due to ability to convert packets between formats relatively easily
1182 * in Libtrace, the format of the packet right now may not be the format that
1183 * the packet was originally captured with.
1184 */
1185DLLEXPORT
1186enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1187
1188/** Construct a libtrace packet from a buffer containing the packet payload.
1189 * @param[in,out] packet        Libtrace Packet object to update with the new
1190 *                              data.
1191 * @param linktype              The linktype of the packet data.
1192 * @param[in] data              The packet data (including linklayer).
1193 * @param len                   Length of packet data provided in the buffer.
1194 *
1195 * @note The constructed packet will be in the PCAP format.
1196 *
1197 * @note To be useful, the provided buffer must start with the layer 2 header
1198 * (or a metadata header, if desired).
1199 */
1200DLLEXPORT
1201void trace_construct_packet(libtrace_packet_t *packet,
1202                libtrace_linktype_t linktype, const void *data, uint16_t len);
1203
1204/*@}*/
1205
1206/** @name Protocol decodes
1207 * These functions locate and return a pointer to various headers inside a
1208 * packet
1209 *
1210 * A packet is divided up into several "layers.":
1211 *
1212 * @li Framing header -- This is the header provided by the capture format
1213 * itself rather than anything that was sent over the network. This provides
1214 * basic details about the packet record including capture lengths, wire
1215 * lengths, timestamps, direction information and any other metadata that is
1216 * part of the capture format. 
1217 *
1218 * @li Metadata header (optional) -- A header containing metadata about a
1219 * packet that was captured, but the metadata was not transmitted over the
1220 * wire.  Some examples include RadioTap and Linux_sll headers.  This can be
1221 * retrieved by trace_get_packet_meta(), or skipped using
1222 * trace_get_payload_from_meta(). There may be multiple "metadata" headers on
1223 * a packet.
1224 *
1225 * @li Layer 2/Link layer/Datalink header -- This can be retrieved by
1226 * trace_get_layer2(), or skipped using trace_get_payload_from_layer2().
1227 *
1228 * @li Layer 3/IP/IPv6 -- This can be retrieved by trace_get_layer3().  As a
1229 * convenience trace_get_ip()/trace_get_ip6() can be used to find an IPv4/IPv6
1230 * header.
1231 *
1232 * @li Layer 5/transport -- These are protocols carried in IPv4/IPv6 frames.
1233 * These can be retrieved using trace_get_transport().
1234 *
1235 * @{
1236 */
1237
1238
1239/** Gets a pointer to the first byte of the packet as it was captured and
1240 * returns its corresponding linktype and capture length.
1241 *
1242 * Use this function instead of the deprecated trace_get_link().
1243 *
1244 * @param packet The packet to get the buffer from
1245 * @param[out] linktype The linktype of the returned pointer
1246 * @param[out] remaining The capture length (the number of captured bytes from
1247 * the returned pointer)
1248 * @return A pointer to the first byte of the packet
1249 */
1250DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet,
1251                libtrace_linktype_t *linktype, uint32_t *remaining);
1252
1253/** Get a pointer to the link layer for a given packet
1254 * @param packet        The packet to get the link layer for
1255 *
1256 * @return A pointer to the link layer, or NULL if there is no link layer
1257 *
1258 * @deprecated This function is deprecated: Use trace_get_packet_buffer() or
1259 * one of the trace_get_layer*() functions instead.
1260 * @note You should call trace_get_link_type to find out what type of link
1261 * layer has been returned to you.
1262 */
1263DLLEXPORT SIMPLE_FUNCTION DEPRECATED
1264void *trace_get_link(const libtrace_packet_t *packet);
1265
1266/** Get a pointer to the IPv4 header (if any) for a given packet
1267 * @param packet        The packet to get the IPv4 header for
1268 *
1269 * @return A pointer to the IPv4 header, or NULL if there is no IPv4 header
1270 *
1271 * If a partial IP header is present, i.e. the packet has been truncated before
1272 * the end of the IP header, this function will return NULL.
1273 *
1274 * You should consider using \ref trace_get_layer3 instead of this function.
1275 */
1276DLLEXPORT SIMPLE_FUNCTION
1277libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
1278
1279/** get a pointer to the IPv6 header (if any)
1280 * @param packet        The packet to get the IPv6 header for
1281 *
1282 * @return A pointer to the IPv6 header, or NULL if there is no IPv6 header
1283 *
1284 * If a partial IPv6 header is present, i.e. the packet has been truncated
1285 * before the end of the IP header, this function will return NULL.
1286 *
1287 * You should consider using \ref trace_get_layer3 instead of this function.
1288 */
1289DLLEXPORT SIMPLE_FUNCTION
1290libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet);
1291
1292/** Return a pointer to the first metadata header in a packet, if present.
1293 *
1294 * This function takes a pointer to a libtrace packet and if any metadata
1295 * headers exist, returns a pointer to the first one, along with its
1296 * corresponding linktype.
1297 *
1298 * If no metadata headers exist in the packet, NULL is returned.
1299 *
1300 * A metadata header is a header that was prepended by the capturing device,
1301 * such as a Linux SLL header, or a Radiotap wireless monitoring header.
1302 * Subsequent metadata headers may be accessed with the
1303 * trace_get_payload_from_meta(...) function.
1304 *
1305 * @param packet The libtrace packet
1306 * @param[out] linktype The linktype of the returned metadata header
1307 * @param[out] remaining The number of bytes captured after the returned
1308 * pointer.
1309 * @return A pointer to the first metadata header, or NULL if there are no
1310 * metadata headers present.
1311 *
1312 * remaining may be NULL, however linktype must be provided.
1313 */
1314DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet,
1315                libtrace_linktype_t *linktype,
1316                uint32_t *remaining);
1317
1318/** Returns the payload of a metadata header.
1319 *
1320 * This function takes a pointer to the start of a metadata header (either
1321 * obtained via trace_get_packet_meta(...) or by a previous call to
1322 * trace_get_payload_from_meta(...)) along with its corresponding linktype and
1323 * returns the payload, i.e. the next header. It will also update the linktype
1324 * parameter to indicate the type of payload.
1325 * 
1326 * If the linktype indicates that the header passed in is not a metadata
1327 * header, the function returns NULL to indicate this. The linktype remains
1328 * unchanged in this case.
1329 *
1330 * This function allows the user to iterate through metadata headers which are
1331 * sometimes present before the actual packet as it was received on the wire.
1332 * Examples of metadata headers include the Linux SLL header and the Radiotap
1333 * wireless monitoring header.
1334 *
1335 * If the metadata header passed into this function is truncated, this
1336 * function will return NULL, and remaining will be set to 0.
1337 *
1338 * If there are 0 bytes of payload following the provided metadata header, the
1339 * function will return a pointer to where the header would otherwise be and
1340 * remaining will be 0.
1341 *
1342 * Therefore, be sure to check the value of remaining after calling this
1343 * function!
1344 *
1345 * @param[in] meta A pointer to a metadata header
1346 * @param[in,out] linktype The linktype of meta (updated to indicate the
1347 * linktype of the returned header if applicable).
1348 * @param[in,out] remaining The number of bytes after the meta pointer.
1349 * @return A pointer to the payload of the metadata header. If meta is not a
1350 * pointer to a metadata header, NULL is returned and linktype remains
1351 * unchanged.
1352 *
1353 * All parameters are mandatory.
1354 */
1355DLLEXPORT void *trace_get_payload_from_meta(const void *meta,
1356                libtrace_linktype_t *linktype,
1357                uint32_t *remaining);
1358
1359
1360/** Get a pointer to the layer 2 header. Generally this is the first byte of the
1361 * packet as it was seen on the wire.
1362 *
1363 * This function takes a libtrace packet and skips over any metadata headers if
1364 * present (such as Linux SLL or Radiotap) and returns a pointer to the first
1365 * byte of the packet that was actually received by the network interface.
1366 *
1367 * @param packet The libtrace packet to find the layer 2 header for
1368 * @param[out] linktype The linktype of the returned layer 2 header
1369 * @param[out] remaining The number of bytes left in the packet after the
1370 * returned pointer.
1371 * @return A pointer to the first byte of the packet as it was seen on the
1372 * wire. If no layer 2 header is present, this function will return NULL.
1373 *
1374 * remaining may be NULL, otherwise it will be filled in by the function.
1375 */
1376DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet,
1377                libtrace_linktype_t *linktype,
1378                uint32_t *remaining);
1379
1380/** Gets a pointer to the next header following a layer 2 header
1381 *
1382 * @param l2                    The pointer to the current layer 2 header
1383 * @param linktype              The type of the layer 2 header
1384 * @param[out] ethertype        An optional output variable of the ethernet type of the new header
1385 * @param[in,out] remaining     Updated with the number of captured bytes
1386                                remaining
1387 *
1388 * @return A pointer to the header following the provided layer 2 header, or
1389 * NULL if no subsequent header is present.
1390 *
1391 * Remaining must point to the number of bytes captured from the layer 2 header
1392 * and beyond.  It will be decremented by the number of bytes skipped to find
1393 * the payload.
1394 *
1395 * If the layer 2 header is complete but there are zero bytes of payload after
1396 * the end of the header, a pointer to where the payload would be is returned
1397 * and remaining will be set to 0.  If the layer 2 header is incomplete
1398 * (truncated), then NULL is returned and remaining will be set to 0.
1399 * Therefore, it is very important to check the value of remaining after
1400 * calling this function.
1401 *
1402 */
1403DLLEXPORT void *trace_get_payload_from_layer2(void *l2,
1404                libtrace_linktype_t linktype,
1405                uint16_t *ethertype,
1406                uint32_t *remaining);
1407
1408
1409/** Get a pointer to the layer 3 (e.g. IP) header.
1410 * @param packet  The libtrace packet to find the layer 3 header for
1411 * @param[out] ethertype The ethertype of the layer 3 header
1412 * @param[out] remaining The amount of captured data remaining in the packet starting from the returned pointer, i.e. including the layer 3 header.
1413 *
1414 * @return A pointer to the layer 3 header. If no layer 3 header is present in
1415 * the packet, NULL is returned. If the layer 3 header is truncated, a valid
1416 * pointer will still be returned so be sure to check the value of remaining
1417 * before attempting to process the returned header.
1418 *
1419 * remaining may be NULL, otherwise it will be set to the number of captured
1420 * bytes after the pointer returned.
1421 */
1422DLLEXPORT
1423void *trace_get_layer3(const libtrace_packet_t *packet,
1424                uint16_t *ethertype, uint32_t *remaining);
1425
1426/** Gets a pointer to the transport layer header (if any)
1427 * @param packet   The libtrace packet to find the transport header for
1428 * @param[out] proto    The protocol present at the transport layer.
1429 * @param[out] remaining The amount of captured data remaining in the packet
1430 * starting from the returned pointer, i.e. including the transport header.
1431 *
1432 * @return A pointer to the transport layer header. If no transport header is
1433 * present in the packet, NULL is returned. If the transport header is
1434 * truncated, a valid pointer will still be returned so be sure to check the
1435 * value of remaining before attempting to process the returned header.
1436 *
1437 * remaining may be NULL, otherwise it will be set to the number of captured
1438 * bytes after the returned pointer.
1439 *
1440 * @note proto may be NULL if proto is unneeded.
1441 */
1442DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet,
1443                uint8_t *proto, uint32_t *remaining);
1444
1445/** Gets a pointer to the payload following an IPv4 header
1446 * @param ip            The IPv4 Header
1447 * @param[out] proto    The protocol of the header following the IPv4 header
1448 * @param[in,out] remaining Updated with the number of captured bytes remaining
1449 *
1450 * @return A pointer to the transport layer header, or NULL if no subsequent
1451 * header is present.
1452 *
1453 * When calling this function, remaining must contain the number of captured
1454 * bytes remaining in the packet starting from the IPv4 header (including the
1455 * IPv4 header itself).
1456 *
1457 * remaining will be decremented by the size of the IPv4 header (including any
1458 * options). If the IPv4 header is complete but there are zero bytes of payload
1459 * after the IPv4 header, a pointer to where the payload would be is returned
1460 * and remaining will be set to 0.  If the IPv4 header is incomplete, NULL will
1461 * be returned and remaining will be set to 0. Therefore, it is very important
1462 * to check the value of remaining after calling this function.
1463 *
1464 * proto may be NULL, in which case it won't be updated.
1465 *
1466 * @note This is similar to trace_get_transport_from_ip in libtrace2
1467 */
1468DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
1469                uint32_t *remaining);
1470
1471/** Gets a pointer to the payload following an IPv6 header
1472 * @param ipptr         The IPv6 Header
1473 * @param[out] proto    The protocol of the header following the IPv6 header
1474 * @param[in,out] remaining Updated with the number of captured bytes remaining
1475 *
1476 * @return A pointer to the transport layer header, or NULL if no subsequent
1477 * header is present.
1478 *
1479 * When calling this function, remaining must contain the number of captured
1480 * bytes remaining in the packet starting from the IPv6 header (including the
1481 * IPv6 header itself).
1482 *
1483 * remaining will be decremented by the size of the IPv6 header (including any
1484 * options). If the IPv6 header is complete but there are zero bytes of payload
1485 * after the IPv6 header, a pointer to where the payload would be is returned
1486 * and remaining will be set to 0.  If the IPv6 header is incomplete, NULL will
1487 * be returned and remaining will be set to 0. Therefore, it is very important
1488 * to check the value of remaining after calling this function.
1489 *
1490 * proto may be NULL, in which case it won't be updated.
1491 *
1492 */
1493DLLEXPORT void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr,
1494                uint8_t *proto, uint32_t *remaining);
1495
1496/** Gets a pointer to the payload following a link header
1497 * @param linkptr       A pointer to the link layer header
1498 * @param linktype      The linktype of the link header being examined
1499 * @param[out] type     An output variable for the ethernet type
1500 * @param[in,out] remaining Updated with the number of captured bytes remaining
1501 *
1502 * @return A pointer to the header following the link header, or NULL if no
1503 * subsequent header is present.
1504 *
1505 * When calling this function, remaining must contain the number of captured
1506 * bytes remaining in the packet starting from the link header (including the
1507 * link header itself). remaining will be updated to contain the number of
1508 * bytes remaining after the link header has been skipped.
1509 *
1510 * @deprecated This function is deprecated: you should be using
1511 * trace_get_payload_from_layer2() or trace_get_payload_from_meta() instead.
1512 *
1513 */
1514DLLEXPORT void *trace_get_payload_from_link(void *linkptr,
1515                libtrace_linktype_t linktype,
1516                uint16_t *type, uint32_t *remaining);
1517
1518/** Gets a pointer to the payload following an 802.1q (VLAN) header.
1519 * @param vlan      A pointer to the VLAN header
1520 * @param[out] type  The ethernet type, replaced with the VLAN ether type
1521 * @param[in,out] remaining Updated with the number of captured bytes remaining
1522 *
1523 * @return A pointer to the header beyond the VLAN header, if one is present.
1524 * Otherwise, returns NULL. 
1525 *
1526 * When calling this function, remaining must contain the number of captured
1527 * bytes remaining in the packet starting from the VLAN header (including the
1528 * VLAN header itself). remaining will be updated to contain the number of
1529 * bytes remaining after the VLAN header has been skipped.
1530 *
1531 * If the VLAN header is complete but there are zero bytes of payload after
1532 * the VLAN header, a pointer to where the payload would be is returned and
1533 * remaining will be set to 0.  If the VLAN header is incomplete, NULL will be
1534 * returned and remaining will be set to 0. Therefore, it is important to check
1535 * the value of remaining after calling this function.
1536 *
1537 * type will be set to the ethertype of the VLAN payload. This parameter is not
1538 * mandatory, but is highly recommended.
1539 *
1540 */
1541DLLEXPORT void *trace_get_payload_from_vlan(
1542                void *vlan, uint16_t *type, uint32_t *remaining);
1543
1544/** Gets a pointer to the payload following an MPLS header.
1545 * @param mpls      A pointer to the MPLS header
1546 * @param[out] type  The ethernet type, replaced by the ether type of the
1547 * returned header - 0x0000 if an Ethernet header is returned
1548 * @param[in,out] remaining Updated with the number of captured bytes remaining
1549 *
1550 * @return A pointer to the header beyond the MPLS label, if one is present.
1551 * Will return NULL if there is not enough bytes remaining to skip past the
1552 * MPLS label.
1553 *
1554 * When calling this function, remaining must contain the number of captured
1555 * bytes remaining in the packet starting from the MPLS header (including the
1556 * MPLS header itself). remaining will be updated to contain the number of
1557 * bytes remaining after the MPLS header has been skipped.
1558 *
1559 * If the MPLS header is complete but there are zero bytes of payload after
1560 * the MPLS header, a pointer to where the payload would be is returned and
1561 * remaining will be set to 0.  If the MPLS header is incomplete, NULL will be
1562 * returned and remaining will be set to 0. Therefore, it is important to check
1563 * the value of remaining after calling this function.
1564 *
1565 * type will be set to the ethertype of the MPLS payload. This parameter is
1566 * mandatory - it may not be NULL.
1567 *
1568 * @note This function will only remove one MPLS label at a time - the type
1569 * will be set to 0x8847 if there is another MPLS label following the one
1570 * skipped by this function.
1571 *
1572 */
1573DLLEXPORT void *trace_get_payload_from_mpls(
1574                void *mpls, uint16_t *type, uint32_t *remaining);
1575
1576/** Gets a pointer to the payload following a PPPoE header
1577 * @param pppoe      A pointer to the PPPoE header
1578 * @param[out] type  The ethernet type, replaced by the ether type of the
1579 * returned header - 0x0000 if an Ethernet header is returned
1580 * @param[in,out] remaining  Updated with the number of captured bytes remaining
1581 *
1582 * @return A pointer to the header beyond the PPPoE header. NOTE that this
1583 * function will also skip over the PPP header that will immediately follow
1584 * the PPPoE header. This function will return NULL if there are not enough
1585 * bytes remaining to skip past both the PPPoE and PPP headers.
1586 *
1587 * When calling this function, remaining must contain the number of captured
1588 * bytes remaining in the packet starting from the PPPoE header (including the
1589 * PPPoE header itself). remaining will be updated to contain the number of
1590 * bytes remaining after the PPPoE and PPP headers have been removed.
1591 *
1592 * If the PPPoE and PPP headers are complete but there are zero bytes of
1593 * payload after the PPP header, a pointer to where the payload would be is
1594 * returned and remaining will be set to 0.  If the PPPoE or PPP header is
1595 * incomplete, NULL will be returned and remaining will be set to 0. Therefore,
1596 * it is important to check the value of remaining after calling this function.
1597 *
1598 * type will be set to the ether type of the PPP payload. This parameter is
1599 * mandatory - it may not be NULL.
1600 *
1601 */
1602DLLEXPORT void *trace_get_payload_from_pppoe(
1603                void *pppoe, uint16_t *type, uint32_t *remaining);
1604
1605/** Gets a pointer to the payload following a TCP header
1606 * @param tcp           A pointer to the TCP header
1607 * @param[in,out] remaining Updated with the number of captured bytes remaining
1608 *
1609 * @return A pointer to the TCP payload, or NULL if the TCP header is truncated.
1610 *
1611 * When calling this function, remaining must contain the number of captured
1612 * bytes remaining in the packet starting from the TCP header (including the
1613 * TCP header itself). remaining will be updated to contain the number of
1614 * bytes remaining after the TCP header has been skipped.
1615 *
1616 * If the TCP header is complete but there are zero bytes of payload after
1617 * the TCP header, a pointer to where the payload would be is returned and
1618 * remaining will be set to 0.  If the TCP header is incomplete, NULL will be
1619 * returned and remaining will be set to 0. Therefore, it is important to check
1620 * the value of remaining after calling this function.
1621 *
1622 */
1623DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp,
1624                uint32_t *remaining);
1625
1626/** Gets a pointer to the payload following a UDP header
1627 * @param udp           A pointer to the UDP header
1628 * @param[in,out] remaining Updated with the number of captured bytes remaining
1629 *
1630 * @return A pointer to the UDP payload, or NULL if the UDP header is truncated.
1631 *
1632 * When calling this function, remaining must contain the number of captured
1633 * bytes remaining in the packet starting from the UDP header (including the
1634 * UDP header itself). remaining will be updated to contain the number of
1635 * bytes remaining after the UDP header has been skipped.
1636 *
1637 * If the UDP header is complete but there are zero bytes of payload after
1638 * the UDP header, a pointer to where the payload would be is returned and
1639 * remaining will be set to 0.  If the UDP header is incomplete, NULL will be
1640 * returned and remaining will be set to 0. Therefore, it is important to check
1641 * the value of remaining after calling this function.
1642 *
1643 */
1644DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
1645
1646/** Gets a pointer to the payload following a ICMP header
1647 * @param icmp           A pointer to the ICMP header
1648 * @param[in,out] remaining Updated with the number of captured bytes remaining
1649 *
1650 * @return A pointer to the ICMP payload, or NULL if the ICMP header is
1651 * truncated.
1652 *
1653 * When calling this function, remaining must contain the number of captured
1654 * bytes remaining in the packet starting from the ICMP header (including the
1655 * ICMP header itself). remaining will be updated to contain the number of
1656 * bytes remaining after the ICMP header has been skipped.
1657 *
1658 * If the ICMP header is complete but there are zero bytes of payload after
1659 * the ICMP header, a pointer to where the payload would be is returned and
1660 * remaining will be set to 0.  If the ICMP header is incomplete, NULL will be
1661 * returned and remaining will be set to 0. Therefore, it is important to check
1662 * the value of remaining after calling this function.
1663 *
1664 * @note In the case of some ICMP messages, the payload may be the IP header
1665 * from the packet that triggered the ICMP message.
1666 *
1667 */
1668DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp,
1669                uint32_t *remaining);
1670
1671/** Get a pointer to the TCP header (if present)
1672 * @param packet        The packet to get the TCP header from
1673 *
1674 * @return A pointer to the TCP header, or NULL if there is not a complete TCP
1675 * header present in the packet.
1676 *
1677 * This is a short-cut function enabling quick and easy access to the TCP
1678 * header if that is all you care about. However, we recommend the use of the
1679 * more generic trace_get_transport() function instead.
1680 *
1681 * @note Unlike trace_get_transport(), this function will return NULL if the
1682 * TCP header is incomplete or truncated.
1683 */
1684DLLEXPORT SIMPLE_FUNCTION
1685libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
1686
1687/** Get a pointer to the TCP header following an IPv4 header (if present)
1688 * @param ip            The IP header to find the subsequent TCP header for
1689 * @param[in,out] remaining Updated with the number of captured bytes remaining
1690 *
1691 * @return A pointer to the TCP header, or NULL if no TCP header is present in
1692 * the packet.
1693 *
1694 * When calling this function, remaining must contain the number of captured
1695 * bytes remaining in the packet starting from the IP header (including the
1696 * IP header itself). remaining will be updated to contain the number of
1697 * bytes remaining after the IP header has been skipped.
1698 *
1699 * If the IP header is complete but there are zero bytes of payload after
1700 * the IP header, a pointer to where the payload would be is returned and
1701 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1702 * returned and remaining will be set to 0. Therefore, it is important to check
1703 * the value of remaining after calling this function.
1704 *
1705 * @note This function is rather redundant now that the layer 3 header is
1706 * cached. There should be no performance advantage for the user to call this
1707 * function over just calling trace_get_transport().
1708 *
1709 * @note The last parameter has changed from libtrace2
1710 */
1711DLLEXPORT SIMPLE_FUNCTION
1712libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
1713
1714/** Get a pointer to the UDP header (if present)
1715 * @param packet        The packet to get the UDP header from
1716 *
1717 * @return A pointer to the UDP header, or NULL if there is not a complete UDP
1718 * header present in the packet.
1719 *
1720 * This is a short-cut function enabling quick and easy access to the UDP
1721 * header if that is all you care about. However, we recommend the use of the
1722 * more generic trace_get_transport() function instead.
1723 *
1724 * @note Unlike trace_get_transport(), this function will return NULL if the
1725 * UDP header is incomplete or truncated.
1726 */
1727DLLEXPORT SIMPLE_FUNCTION
1728libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
1729
1730/** Get a pointer to the UDP header following an IPv4 header (if present)
1731 * @param ip            The IP header to find the subsequent UDP header for
1732 * @param[in,out] remaining Updated with the number of captured bytes remaining
1733 *
1734 * @return A pointer to the UDP header, or NULL if no UDP header is present in
1735 * the packet.
1736 *
1737 * When calling this function, remaining must contain the number of captured
1738 * bytes remaining in the packet starting from the IP header (including the
1739 * IP header itself). remaining will be updated to contain the number of
1740 * bytes remaining after the IP header has been skipped.
1741 *
1742 * If the IP header is complete but there are zero bytes of payload after
1743 * the IP header, a pointer to where the payload would be is returned and
1744 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1745 * returned and remaining will be set to 0. Therefore, it is important to check
1746 * the value of remaining after calling this function.
1747 *
1748 * @note This function is rather redundant now that the layer 3 header is
1749 * cached. There should be no performance advantage for the user to call this
1750 * function over just calling trace_get_transport().
1751 *
1752 * @note The last parameter has changed from libtrace2
1753 */
1754DLLEXPORT SIMPLE_FUNCTION
1755libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1756
1757/** Get a pointer to the ICMP header (if present)
1758 * @param packet        The packet to get the ICMP header from
1759 *
1760 * @return A pointer to the ICMP header, or NULL if there is not a complete
1761 * ICMP header present in the packet.
1762 *
1763 * This is a short-cut function enabling quick and easy access to the ICMP
1764 * header if that is all you care about. However, we recommend the use of the
1765 * more generic trace_get_transport() function instead.
1766 *
1767 * @note Unlike trace_get_transport(), this function will return NULL if the
1768 * ICMP header is incomplete or truncated.
1769 */
1770DLLEXPORT SIMPLE_FUNCTION
1771libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
1772
1773/** Get a pointer to the ICMP header following an IPv4 header (if present)
1774 * @param ip            The IP header to find the subsequent ICMP header for
1775 * @param[in,out] remaining Updated with the number of captured bytes remaining
1776 *
1777 * @return A pointer to the ICMP header, or NULL if no UDP header is present in
1778 * the packet.
1779 *
1780 * When calling this function, remaining must contain the number of captured
1781 * bytes remaining in the packet starting from the IP header (including the
1782 * IP header itself). remaining will be updated to contain the number of
1783 * bytes remaining after the IP header has been skipped.
1784 *
1785 * If the IP header is complete but there are zero bytes of payload after
1786 * the IP header, a pointer to where the payload would be is returned and
1787 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1788 * returned and remaining will be set to 0. Therefore, it is important to check
1789 * the value of remaining after calling this function.
1790 *
1791 * @note This function is rather redundant now that the layer 3 header is
1792 * cached. There should be no performance advantage for the user to call this
1793 * function over just calling trace_get_transport().
1794 *
1795 * @note The last parameter has changed from libtrace2
1796 */
1797DLLEXPORT SIMPLE_FUNCTION
1798libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1799
1800/** Gets the destination MAC address for a given packet
1801 * @param packet        The packet to extract the destination MAC address from
1802 *
1803 * @return A pointer to the destination MAC address field in the layer 2
1804 * header, (or NULL if there is no destination MAC address or layer 2 header
1805 * available)
1806 *
1807 * @note This is a zero-copy function, so the memory that the returned pointer
1808 * points to is part of the packet itself.
1809 */
1810DLLEXPORT SIMPLE_FUNCTION
1811uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
1812
1813/** Gets the source MAC address for a given packet
1814 * @param packet        The packet to extract the source MAC address from
1815 *
1816 * @return A pointer to the source MAC address field in the layer 2
1817 * header, (or NULL if there is no source MAC address or layer 2 header
1818 * available)
1819 *
1820 * @note This is a zero-copy function, so the memory that the returned pointer
1821 * points to is part of the packet itself.
1822 */
1823DLLEXPORT SIMPLE_FUNCTION
1824uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
1825
1826/** Get the source IP address for a given packet
1827 * @param packet        The packet to extract the source IP address from
1828 * @param addr          A pointer to a sockaddr structure to store the address
1829 *                      in. If NULL, static storage is used instead.
1830 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
1831 * platforms a sockaddr holding a MAC address. Returns NULL if no source IP
1832 * address was available.
1833 *
1834 * @note The best way to use this function is to pass in a pointer to the
1835 * struct sockaddr_storage for the addr parameter. This will avoid problems
1836 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
1837 * IPv4.
1838 */
1839DLLEXPORT SIMPLE_FUNCTION
1840struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
1841                struct sockaddr *addr);
1842
1843/** Get the destination IP address for a given packet
1844 * @param packet        The packet to extract the destination IP address from
1845 * @param addr          A pointer to a sockaddr structure to store the address
1846 *                      in. If NULL, static storage is used instead.
1847 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
1848 * platforms a sockaddr holding a MAC address. Returns NULL if no destination
1849 * IP address was available.
1850 *
1851 * @note The best way to use this function is to pass in a pointer to the
1852 * struct sockaddr_storage for the addr parameter. This will avoid problems
1853 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
1854 * IPv4.
1855 */
1856DLLEXPORT SIMPLE_FUNCTION
1857struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
1858                struct sockaddr *addr);
1859
1860
1861/** Parses an IP or TCP option
1862 * @param[in,out] ptr   The pointer to the current option
1863 * @param[in,out] len   The total length of all the remaining options
1864 * @param[out] type     The type of the option
1865 * @param[out] optlen   The length of the option
1866 * @param[out] data     The data of the option
1867 *
1868 * @return bool true if there is another option (and the fields are filled in)
1869 *               or false if this was the last option.
1870 *
1871 * This updates ptr to point to the next option after this one, and updates
1872 * len to be the number of bytes remaining in the options area.  Type is updated
1873 * to be the code of this option, and data points to the data of this option,
1874 * with optlen saying how many bytes there are.
1875 *
1876 * @note Beware of fragmented packets.
1877 */
1878DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
1879                        unsigned char *type,
1880                        unsigned char *optlen,
1881                        unsigned char **data);
1882
1883/*@}*/
1884
1885/** @name Time
1886 * These functions deal with the timestamp describing when a packet was
1887 * captured and can convert it into various formats
1888 * @{
1889 */
1890
1891/** Get the packet timestamp in the DAG time format
1892 * @param packet        The packet to extract the timestamp from
1893 *
1894 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
1895 * past 1970-01-01, the lower 32bits are partial seconds)
1896 */
1897DLLEXPORT SIMPLE_FUNCTION
1898uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
1899
1900/** Get the packet timestamp as a struct timeval
1901 * @param packet        The packet to extract the timestamp from
1902 *
1903 * @return The time that this packet was captured in a struct timeval
1904 */
1905DLLEXPORT SIMPLE_FUNCTION
1906struct timeval trace_get_timeval(const libtrace_packet_t *packet);
1907
1908/** Get the packet timestamp as a struct timespec
1909 * @param packet        The packet to extract the timestamp from
1910 *
1911 * @return The time that this packet was captured in a struct timespec
1912 */
1913DLLEXPORT SIMPLE_FUNCTION
1914struct timespec trace_get_timespec(const libtrace_packet_t *packet);
1915
1916/** Get the packet timestamp in floating point seconds
1917 * @param packet        The packet to extract the timestamp from
1918 *
1919 * @return time that this packet was seen in 64-bit floating point seconds from
1920 * the UNIX epoch (1970-01-01 00:00:00 UTC).
1921 */
1922DLLEXPORT SIMPLE_FUNCTION
1923double trace_get_seconds(const libtrace_packet_t *packet);
1924
1925/** Seek within an input trace to a time specified in floating point seconds
1926 * @param trace         The input trace to seek within
1927 * @param seconds       The time to seek to, in floating point seconds
1928 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
1929 * the error that occurred.
1930 *
1931 * This will make the next packet read to be the first packet to occur at or
1932 * after the specified time.  This must be called in the configuration state
1933 * (i.e. before trace_start() or after trace_pause()).
1934 *
1935 * The time format accepted by this function is 64-bit floating point seconds
1936 * since the UNIX epoch (1970-01-01 00:00:00 UTC), i.e. the same format as
1937 * trace_get_seconds().
1938 *
1939 * @note This function may be extremely slow.
1940 */
1941DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
1942
1943/** Seek within an input trace to a time specified as a timeval
1944 * @param trace         The input trace to seek within
1945 * @param tv            The time to seek to, as a timeval
1946 *
1947 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
1948 * the error that occurred.
1949 *
1950 * This will make the next packet read to be the first packet to occur at or
1951 * after the specified time.  This must be called in the configuration state
1952 * (i.e. before trace_start() or after trace_pause()).
1953 *
1954 * @note This function may be extremely slow.
1955 */
1956DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
1957
1958/** Seek within an input trace to a time specified as an ERF timestamp
1959 * @param trace         The input trace to seek within
1960 * @param ts            The time to seek to, as an ERF timestamp
1961 *
1962 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
1963 * the error that occurred.
1964 *
1965 * This will make the next packet read to be the first packet to occur at or
1966 * after the specified time.  This must be called in the configuration state
1967 * (i.e. before trace_start() or after trace_pause()).
1968 *
1969 * The time format accepted by this function is the ERF timestamp, which is a
1970 * 64-bit value where the upper 32 bits are seconds since the UNIX epoch and
1971 * the lower 32 bits are partial seconds.
1972 *
1973 * @note This function may be extremely slow.
1974 */
1975DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
1976
1977/*@}*/
1978
1979/** @name Sizes
1980 * This section deals with finding or setting the various different lengths
1981 * that a packet can have, e.g. capture lengths, wire lengths, etc.
1982 * @{
1983 */
1984/** Get the current size of the packet (in bytes), taking into account any
1985 * truncation or snapping that may have previously been performed.
1986 *
1987 * @param packet        The packet to determine the capture length for
1988 * @return The size of the packet read from the input trace, i.e. what is
1989 * actually available to libtrace at the moment.
1990 *
1991 * @note Most traces are header captures, so this value may not be the same
1992 * as the size of the packet when it was captured. Use trace_get_wire_length()
1993 * to get the original size of the packet.
1994 
1995 * @note This can (and often is) different for different packets in a trace!
1996 
1997 * @note This is sometimes called the "snaplen".
1998 *
1999 * @note The return size refers to the network-level payload of the packet and
2000 * does not include any capture framing headers. For example, an Ethernet
2001 * packet with an empty TCP packet will return sizeof(ethernet_header) +
2002 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
2003 * (pcap/erf/etc) header.
2004 */
2005DLLEXPORT SIMPLE_FUNCTION
2006size_t trace_get_capture_length(const libtrace_packet_t *packet);
2007
2008/** Get the size of the packet as it was originally seen on the wire (in bytes).
2009 * @param packet        The packet to determine the wire length for
2010 * @return The size of the packet as it was on the wire.
2011 *
2012 * @note This value may not be the same as the capture length, due to
2013 * truncation.
2014 *
2015 * @note trace_get_wire_length \em includes  the Frame Check Sequence. This is
2016 * different behaviour compared to most PCAP-based tools.
2017 *
2018 * @note The return size refers to the network-level payload of the packet and
2019 * does not include any capture framing headers. For example, an Ethernet
2020 * packet with an empty TCP packet will return sizeof(ethernet_header) +
2021 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
2022 * (pcap/erf/etc) header.
2023 */
2024DLLEXPORT SIMPLE_FUNCTION
2025size_t trace_get_wire_length(const libtrace_packet_t *packet);
2026
2027/** Get the length of the capture framing headers (in bytes).
2028 * @param packet        The packet to determine the framing length for
2029 * @return The size of the capture format header encapsulating the packet.
2030 *
2031 * @note This length corresponds to the difference between the amount of
2032 * memory required to store a captured packet and the capture length reported
2033 * by trace_capture_length()
2034 */
2035DLLEXPORT SIMPLE_FUNCTION
2036size_t trace_get_framing_length(const libtrace_packet_t *packet);
2037
2038/** Get the length of the original payload content of the packet (in bytes).
2039 * @param packet        The packet to determine the payload length for
2040 * @return The size of the packet payload (without headers). Returns 0 if
2041 * unable to calculate payload length correctly.
2042 *
2043 * This function reports the amount of data that followed the transport header
2044 * when the packet was originally captured, i.e. prior to any snapping. Best
2045 * described as the wire length minus the packet headers.
2046 *
2047 * Currently only supports some protocols and will return 0 if an unsupported
2048 * protocol header is encountered, or if one of the headers is truncated.
2049 *
2050 * @note Supports IPv4, IPv6, TCP, UDP and ICMP.
2051 */
2052DLLEXPORT SIMPLE_FUNCTION
2053size_t trace_get_payload_length(const libtrace_packet_t *packet);
2054
2055/** Truncate ("snap") the packet to the suggested length
2056 * @param packet        The packet to truncate
2057 * @param size          The new length of the packet (in bytes)
2058 *
2059 * @return The new capture length of the packet or the original capture
2060 * length of the packet if unchanged.
2061 *
2062 * This function will modify the capture length of the given packet. The wire
2063 * length will not be changed, so you can always determine what the original
2064 * packet size was, prior to the truncation.
2065 *
2066 * @note You can only use this function to decrease the capture length. Any
2067 * attempt to increase capture length will have no effect.
2068 */
2069DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
2070
2071/*@}*/
2072
2073
2074/** Gets the link layer type for a packet
2075 * @param packet        The packet to extract the link layer type for
2076 * @return A libtrace_linktype_t describing the link layer protocol being used
2077 * by this packet.
2078 */
2079DLLEXPORT SIMPLE_FUNCTION
2080libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
2081
2082/** Set the direction flag for a packet, if the capture format supports
2083 * direction tagging.
2084 *
2085 * @param packet        The packet to set the direction for
2086 * @param direction     The new direction
2087 * @returns -1 on error, or the direction that was set.
2088 *
2089 * @note Few capture formats actually support direction tagging. Most notably,
2090 * we cannot set the direction on PCAP packets.
2091 */
2092DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
2093
2094/** Get the direction flag for a packet, if it has one.
2095 * @param packet  The packet to get the direction for
2096 *
2097 * @return A value representing the direction flag, or -1 if this is not
2098 * supported by the capture format.
2099 *
2100 * The direction is defined as 0 for packets originating locally (ie, outbound)
2101 * and 1 for packets originating remotely (ie, inbound). Other values are
2102 * possible, which might be overloaded to mean special things for certain
2103 * traces, e.g. in the Waikato traces, 2 is used to represent an "Unknown"
2104 * direction.
2105 *
2106 * For DAG/ERF traces, the direction is extracted from the "Interface" bits in
2107 * the ERF header, which can range from 0 - 3.
2108 */
2109DLLEXPORT SIMPLE_FUNCTION
2110libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
2111
2112/** @name BPF
2113 * This section deals with using Berkley Packet Filters to filter input traces
2114 * @{
2115 */
2116/** Creates a BPF filter
2117 * @param filterstring The filter string describing the BPF filter to create
2118 * @return An opaque pointer to a libtrace_filter_t object
2119 *
2120 * @note The filter is not actually compiled at this point, so no correctness
2121 * tests are performed here. trace_create_filter() will always return ok, but
2122 * if the filter is poorly constructed an error will be generated when the
2123 * filter is actually used.
2124 */
2125DLLEXPORT SIMPLE_FUNCTION
2126libtrace_filter_t *trace_create_filter(const char *filterstring);
2127
2128/** Create a BPF filter based on pre-compiled byte-code.
2129 * @param bf_insns      A pointer to the start of the byte-code
2130 * @param bf_len        The number of BPF instructions 
2131 * @return              An opaque pointer to a libtrace_filter_t object
2132 * @note                The supplied byte-code is not checked for correctness.
2133 *                      Instead, incorrect byte-code will generate an error
2134 *                      once the filter is actually used.
2135 * @author              Scott Raynel
2136 */
2137DLLEXPORT libtrace_filter_t *
2138trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len);
2139
2140/** Apply a BPF filter to a packet
2141 * @param filter        The filter to be applied       
2142 * @param packet        The packet to be matched against the filter
2143 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
2144 *
2145 * @note Due to the way BPF filters are built, the filter is not actually
2146 * compiled until the first time trace_create_filter is called. If your filter
2147 * is incorrect, it will generate an error message and assert, exiting the
2148 * program. This behaviour may change to a more graceful handling of this error
2149 * in the future.
2150 */
2151DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
2152                const libtrace_packet_t *packet);
2153
2154/** Destroy a BPF filter
2155 * @param filter        The filter to be destroyed
2156 *
2157 * Deallocates all the resources associated with a BPF filter.
2158 */
2159DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
2160/*@}*/
2161
2162/** @name Portability
2163 * This section contains functions that deal with portability issues, e.g. byte
2164 * ordering.
2165 * @{
2166 */
2167
2168/** Converts an ethernet address to a printable string
2169 * @param addr  Ethernet address in network byte order
2170 * @param buf   Buffer to store the ascii representation, or NULL to indicate
2171 * that static storage should be used.
2172 * @return buf, or if buf is NULL then a statically allocated buffer.
2173 *
2174 * This function is similar to the GNU ether_ntoa_r function, with a few
2175 * minor differences.  If NULL is passed as buf, then the function will
2176 * use an internal static buffer. If NULL isn't passed then the function
2177 * will use that buffer instead.
2178 *
2179 * The address pointers returned by trace_get_source_mac() and
2180 * trace_get_destination_mac() can be passed directly into this function.
2181 *
2182 * @note The type of addr isn't struct ether_addr as it is with ether_ntoa_r,
2183 * however it is bit compatible so that a cast will work.
2184 */
2185DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
2186
2187/** Convert a string to an ethernet address
2188 * @param buf   A string containing an Ethernet address in hex format
2189 * delimited with :'s.
2190 * @param addr  Buffer to store the binary representation, or NULL to indicate
2191 * that static storage should be used.
2192 * @return addr, or if addr is NULL then a statically allocated buffer.
2193 *
2194 * This function is similar to the GNU ether_aton_r function, with a few
2195 * minor differences.  If NULL is passed as addr, then the function will
2196 * use an internal static buffer. If NULL isn't passed then the function will
2197 * use that buffer instead.
2198 *
2199 * The address returned by this function will be in network byte order.
2200 *
2201 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
2202 * however it is bit compatible so that a cast will work.
2203 */
2204DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
2205
2206/*@}*/
2207
2208/** @name Ports
2209 * This section contains functions for dealing with port numbers at the
2210 * transport layer.
2211 *
2212 * @{
2213 */
2214
2215/** An indication of which port is the "server" port for a given port pair */
2216typedef enum {
2217        USE_DEST,       /**< Destination port is the server port */
2218        USE_SOURCE      /**< Source port is the server port */
2219} serverport_t;
2220
2221/** Gets the source port for a given packet
2222 * @param packet        The packet to get the source port from
2223 * @return The source port in HOST byte order or 0 if no suitable port number
2224 * can be extracted from the packet.
2225 *
2226 * This function will return 0 if the transport protocol is known not to
2227 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2228 * present in the packet or the transport header has been truncated such that
2229 * the port fields are not readable.
2230 *
2231 * @note If the transport protocol is not known by libtrace, the first two
2232 * bytes of the transport header will be treated as the source port field.
2233 */
2234DLLEXPORT SIMPLE_FUNCTION
2235uint16_t trace_get_source_port(const libtrace_packet_t *packet);
2236
2237/** Gets the destination port for a given packet
2238 * @param packet        The packet to get the destination port from
2239 * @return The destination port in HOST byte order or 0 if no suitable port
2240 * number can be extracted from the packet.
2241 *
2242 * This function will return 0 if the transport protocol is known not to
2243 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2244 * present in the packet or the transport header has been truncated such that
2245 * the port fields are not readable.
2246 *
2247 * @note If the transport protocol is not known by libtrace, the third and
2248 * fourth bytes of the transport header will be treated as the destination
2249 * port field.
2250 *
2251 */
2252DLLEXPORT SIMPLE_FUNCTION
2253uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
2254
2255/** Hint at which of the two provided ports is the server port.
2256 *
2257 * @param protocol      The IP layer protocol, eg 6 (tcp), 17 (udp)
2258 * @param source        The source port from the packet
2259 * @param dest          The destination port from the packet
2260 *
2261 * @return one of USE_SOURCE or USE_DEST describing on which of the two ports
2262 * is most likely to be the server port.
2263 *
2264 * @note Ports must be provided in HOST byte order!
2265 *
2266 * This function is based almost entirely on heuristics and should not be
2267 * treated as a definitive means of identifying the server port. However, it
2268 * is deterministic, so it is very handy for identifying both halves of the
2269 * same flow.
2270 */
2271DLLEXPORT SIMPLE_FUNCTION
2272int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
2273
2274/*@}*/
2275
2276/** @name Wireless trace support
2277 * Functions to access wireless information from packets that have wireless
2278 * monitoring headers such as Radiotap or Prism.
2279 *
2280 * The trace_get_wireless_* functions provide an abstract interface for
2281 * retrieving information from wireless traces. They take a pointer to the
2282 * wireless monitoring header (usually found with trace_get_packet_meta()) and
2283 * the linktype of the header passed in.
2284 *
2285 * All of the trace_get_wireless_* functions return false if the requested
2286 * information was unavailable, or true if it was. The actual data is stored
2287 * in an output variable supplied by the caller. Values returned into the
2288 * output variable will always be returned in host byte order.
2289 * @{
2290 */
2291
2292
2293#ifndef ARPHRD_80211_RADIOTAP
2294/** libc doesn't define this yet, so we have to do so ourselves */
2295#define ARPHRD_80211_RADIOTAP 803
2296#endif
2297
2298/** Get the wireless Timer Synchronisation Function
2299 *
2300 * Gets the value of the timer synchronisation function for this frame, which
2301 * is a value in microseconds indicating the time that the first bit of the
2302 * MPDU was received by the MAC.
2303 *
2304 * @param linkptr The wireless meta header
2305 * @param linktype The linktype of the wireless meta header passed in
2306 * @param[out] tsft The value of the timer synchronisation function.
2307 * @return true if the field was available, false if not.
2308 */
2309DLLEXPORT bool trace_get_wireless_tsft(void *linkptr,
2310        libtrace_linktype_t linktype, uint64_t *tsft);
2311
2312/** Get the wireless data rate
2313 *
2314 * @param linkptr The wireless meta header
2315 * @param linktype The linktype of the wireless meta header passed in
2316 * @param[out] rate The data-rate of the frame in units of 500kbps
2317 * @return true if the field was available, false if not.
2318 */
2319DLLEXPORT bool trace_get_wireless_rate(void *linkptr,
2320        libtrace_linktype_t linktype, uint8_t *rate);
2321
2322/** Get the wireless channel frequency
2323 * @param linkptr The wireless meta header
2324 * @param linktype The linktype of the wireless meta header passed in
2325 * @param[out] freq The frequency in MHz of the channel the frame was
2326 * transmitted or received on.
2327 * @return true if the field was available, false if not.
2328 */
2329DLLEXPORT bool trace_get_wireless_freq(void *linkptr,
2330        libtrace_linktype_t linktype, uint16_t *freq);
2331
2332/** Get the wireless signal strength in dBm
2333 * @param linkptr The wireless meta header
2334 * @param linktype The linktype of the wireless meta header passed in
2335 * @param[out] strength The RF signal power at the antenna, in dB difference
2336 * from 1mW.
2337 * @return true if the field was available, false if not.
2338 */
2339DLLEXPORT bool trace_get_wireless_signal_strength_dbm(void *linkptr,
2340        libtrace_linktype_t linktype, int8_t *strength);
2341
2342/** Get the wireless noise strength in dBm
2343 * @param linkptr The wireless meta header
2344 * @param linktype The linktype of the wireless meta header passed in
2345 * @param[out] strength The RF noise power at the antenna, in dB difference
2346 * from 1mW.
2347 * @return true if the field was available, false if not.
2348 */
2349DLLEXPORT bool trace_get_wireless_noise_strength_dbm(void *linkptr,
2350        libtrace_linktype_t linktype, int8_t *strength);
2351
2352/** Get the wireless signal strength in dB
2353 * @param linkptr The wireless meta header
2354 * @param linktype The linktype of the wireless meta header passed in
2355 * @param[out] strength The RF signal power at the antenna, in dB difference
2356 * from a fixed reference.
2357 * @return true if the field was available, false if not.
2358 */
2359DLLEXPORT bool trace_get_wireless_signal_strength_db(void *linkptr,
2360        libtrace_linktype_t linktype, uint8_t *strength);
2361
2362/** Get the wireless noise strength in dB
2363 * @param linkptr The wireless meta header
2364 * @param linktype The linktype of the wireless meta header passed in
2365 * @param[out] strength The RF noise power at the antenna, in dB difference
2366 * from a fixed reference.
2367 * @return true if the field was available, false if not.
2368 */
2369DLLEXPORT bool trace_get_wireless_noise_strength_db(void *linkptr,
2370        libtrace_linktype_t linktype, uint8_t *strength);
2371
2372/** Get the wireless transmit attenuation
2373 * @param linkptr The wireless meta header
2374 * @param linktype The linktype of the wireless meta header passed in
2375 * @param[out] attenuation The transmit power as a unitless distance from
2376 * maximum power set at factory calibration. 0 indicates maximum transmission
2377 * power.
2378 * @return true if the field was available, false if not.
2379 */
2380DLLEXPORT bool trace_get_wireless_tx_attenuation(void *linkptr,
2381        libtrace_linktype_t linktype, uint16_t *attenuation);
2382
2383/** Get the wireless transmit attenuation in dB
2384 * @param linkptr The wireless meta header
2385 * @param linktype The linktype of the wireless meta header passed in
2386 * @param[out] attenuation The transmit power as dB difference from maximum
2387 * power set at factory calibration. 0 indicates maximum power.
2388 * @return true if the field was available, false if not.
2389 */
2390DLLEXPORT bool trace_get_wireless_tx_attenuation_db(void *linkptr,
2391        libtrace_linktype_t linktype, uint16_t *attenuation);
2392
2393/** Get the wireless transmit power in dBm
2394 * @param linkptr The wireless meta header
2395 * @param linktype The linktype of the wireless meta header passed in
2396 * @param[out] txpower The transmit power as dB from a 1mW reference. This is
2397 * the absolute power level measured at the antenna port. 
2398 * @return true if the field was available, false if not.
2399 */
2400DLLEXPORT bool trace_get_wireless_tx_power_dbm(void *linkptr,
2401                libtrace_linktype_t linktype, int8_t *txpower);
2402
2403/** Get the wireless antenna
2404 * @param linkptr The wireless meta header
2405 * @param linktype The linktype of the wireless meta header passed in
2406 * @param[out] antenna The antenna that was used to transmit or receive the
2407 * frame.
2408 * @return true if the field was available, false if not.
2409 */
2410DLLEXPORT bool trace_get_wireless_antenna(void *linkptr,
2411        libtrace_linktype_t linktype, uint8_t *antenna);
2412
2413/*@}*/
2414
2415#ifdef __cplusplus
2416} /* extern "C" */
2417#endif /* #ifdef __cplusplus */
2418#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.