source: lib/libtrace.h.in @ 44028d4

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