source: lib/libtrace.h.in @ c259635

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