source: lib/libtrace.h.in @ 15749d5

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

Added a trace_get_payload_length() function which returns the original payload length for a packet - got sick of re-implementing this function everytime I wanted to know the size of the application payload.

This function currently works for TCP, UDP and ICMP packets. All other protocols
will return 0.

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