source: lib/libtrace.h.in @ 226c08b

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 226c08b was 226c08b, checked in by Perry Lorier <perry@…>, 12 years ago

Create a new "NonData?" link type for things that aren't data at all

  • Property mode set to 100644
File size: 79.5 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *         
10 * All rights reserved.
11 *
12 * This code has been developed by the University of Waikato WAND
13 * research group. For further information please see http://www.wand.net.nz/
14 *
15 * libtrace is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * libtrace is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with libtrace; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 *
29 * $Id: libtrace.h 773 2006-05-01 12:58:09Z perry $
30 *
31 */
32
33#ifndef LIBTRACE_H
34#define LIBTRACE_H
35
36/** @file
37 *
38 * @brief Trace file processing library header
39 *
40 * @author Daniel Lawson
41 * @author Perry Lorier
42 *
43 * @version $Id: libtrace.h 773 2006-05-01 12:58:09Z perry $
44 *
45 * This library provides a per packet interface into a trace file, or a live
46 * captures.  It supports ERF, DAG cards, PCAP, Linux and BSD native sockets,
47 * legacy ERF formats etc.
48 *
49 * @par Usage
50 * See the example/ directory in the source distribution for some simple
51 * examples
52 *
53 * @par Linking
54 * To use this library you need to link against libtrace by passing -ltrace
55 * to your linker. You may also need to link against a version of libpcap
56 * and of zlib which are compiled for largefile support (if you wish to access
57 * traces larger than 2 GB). This is left as an exercise for the reader. Debian
58 * Woody, at least, does not support large file offsets.
59 *
60 */
61
62#include <sys/types.h>
63#ifndef WIN32
64#include <sys/time.h>
65#endif
66
67#ifdef _MSC_VER
68    /* define the following from MSVC's internal types */
69    typedef             __int8  int8_t;
70    typedef             __int16 int16_t;
71    typedef             __int32 int32_t;
72    typedef             __int64 int64_t;
73    typedef unsigned    __int8  uint8_t;
74    typedef unsigned    __int16 uint16_t;
75    typedef unsigned    __int32 uint32_t;
76    typedef unsigned    __int64 uint64_t;
77    #ifdef LT_BUILDING_DLL
78        #define DLLEXPORT __declspec(dllexport)
79    #else
80        #define DLLEXPORT __declspec(dllimport)
81    #endif
82    #define DLLLOCAL
83    /* Windows pads bitfields out to to the size of their parent type
84     * however gcc warns that this doesn't meet with the iso C specification
85     * so produces warnings for this behaviour.  sigh.
86     */
87    #define LT_BITFIELD8        uint8_t
88    #define LT_BITFIELD16       uint16_t
89    #define LT_BITFIELD32       uint32_t
90    #define LT_BITFIELD64       uint64_t
91#else
92    #ifdef HAVE_STDINT_H
93        #   include <stdint.h>
94    #endif
95    #if __GNUC__ >= 4
96        #ifdef LT_BUILDING_DLL
97                #define DLLEXPORT __attribute__ ((visibility("default")))
98                #define DLLLOCAL __attribute__ ((visibility("hidden")))
99        #else
100                #define DLLEXPORT
101                #define DLLLOCAL
102        #endif
103    #else
104        #define DLLEXPORT
105        #define DLLLOCAL
106    #endif
107    /* GCC warns if the bitfield type is not "unsigned int", however windows
108     * generates incorrect code for this (see above), so we define these
109     * macros.  How Hidious.  So much for C's portability.
110     */
111    #define LT_BITFIELD8        unsigned int
112    #define LT_BITFIELD16       unsigned int
113    #define LT_BITFIELD32       unsigned int
114    #define LT_BITFIELD64       unsigned int
115#endif
116
117#ifdef WIN32
118#   include <winsock2.h>
119#   include <ws2tcpip.h>
120    typedef short sa_family_t;
121    /* Make up for a lack of stdbool.h */
122#    define bool signed char
123#    define false 0
124#    define true 1
125#    if !defined(ssize_t)
126     /* XXX: Not 64-bit safe! */
127#    define ssize_t int
128#    endif   
129#else
130#    include <netinet/in.h>
131
132#ifndef __cplusplus
133#    include <stdbool.h>
134#endif
135
136#    include <sys/types.h>
137#    include <sys/socket.h>
138#endif
139
140/** API version as 2 byte hex digits, eg 0xXXYYZZ */
141#define LIBTRACE_API_VERSION \
142            ((@LIBTRACE_MAJOR@<<16)|(@LIBTRACE_MID@<<8)|(@LIBTRACE_MINOR@))
143
144/** Replaced with the current SVN revision number when 'make dist' is invoked
145 *  to create a distributable tarball */
146#define LIBTRACE_SVN_REVISION 0
147
148/** DAG driver version installed on the current system */
149#define DAG_DRIVER_V "@DAG_VERSION_NUM@"
150   
151#ifdef __cplusplus
152extern "C" {
153#endif
154
155/* Function does not depend on anything but its
156 * parameters, used to hint gcc's optimisations
157 */
158#if __GNUC__ >= 3
159#  define DEPRECATED __attribute__((deprecated))
160#  define SIMPLE_FUNCTION __attribute__((pure))
161#  define UNUSED __attribute__((unused))
162#  define PACKED __attribute__((packed))
163#  define PRINTF(formatpos,argpos) __attribute__((format(printf,formatpos,argpos)))
164#else
165#  define DEPRECATED
166#  define SIMPLE_FUNCTION
167#  define UNUSED
168#  define PACKED
169#  define PRINTF(formatpos,argpos)
170#endif
171       
172/** Opaque structure holding information about an output trace */
173typedef struct libtrace_out_t libtrace_out_t;
174       
175/** Opaque structure holding information about a trace */
176typedef struct libtrace_t libtrace_t;
177       
178/** Opaque structure holding information about a bpf filter */
179typedef struct libtrace_filter_t libtrace_filter_t;
180
181/** If the packet has allocated its own memory the buffer_control should be
182 * set to TRACE_CTRL_PACKET, so that the memory will be freed when the packet
183 * is destroyed. If the packet has been zerocopied out of memory owned by
184 * something else, e.g. a DAG card, it should be TRACE_CTRL_EXTERNAL.
185 *
186 * @note The letters p and e are magic numbers used to detect if the packet
187 * wasn't created properly.
188 */
189typedef enum {
190        TRACE_CTRL_PACKET='p',  /**< Buffer memory is owned by the packet */
191        TRACE_CTRL_EXTERNAL='e' /**< Buffer memory is owned by an external source */
192} buf_control_t;
193
194/** The size of a packet's buffer when managed by libtrace */
195#define LIBTRACE_PACKET_BUFSIZE 65536
196
197/** Libtrace error information */
198typedef struct trace_err_t{
199        int err_num;            /**< error code */
200        char problem[255];      /**< the format, uri etc that caused the error for reporting purposes */
201} libtrace_err_t;
202
203/** Enumeration of error codes */
204enum {
205        /** No Error has occured.... yet. */
206        TRACE_ERR_NOERROR       = 0,
207        /** The URI passed to trace_create() is unsupported, or badly formed */
208        TRACE_ERR_BAD_FORMAT    = -1,
209        /** The trace failed to initialise */
210        TRACE_ERR_INIT_FAILED   = -2,
211        /** Unknown config option */
212        TRACE_ERR_UNKNOWN_OPTION= -3,
213        /** This output uri cannot write packets of this type */
214        TRACE_ERR_NO_CONVERSION = -4,
215        /** This packet is corrupt, or unusable for the action required */
216        TRACE_ERR_BAD_PACKET    = -5,
217        /** Option known, but unsupported by this format */
218        TRACE_ERR_OPTION_UNAVAIL= -6,
219        /** This feature is unsupported */
220        TRACE_ERR_UNSUPPORTED   = -7,
221        /** Illegal use of the API */
222        TRACE_ERR_BAD_STATE     = -8
223};
224
225/** Enumeration of DLTs supported by libtrace
226 */
227typedef enum {
228        TRACE_DLT_NULL = 0,
229        TRACE_DLT_EN10MB = 1,
230        TRACE_DLT_PPP = 9,
231        TRACE_DLT_ATM_RFC1483 = 11,
232       
233        /* Sigh. This is handled in files with LINKTYPE's */
234#ifdef __OpenBSD__
235        TRACE_DLT_RAW = 14,     
236#else
237        TRACE_DLT_RAW = 12,
238#endif
239        TRACE_DLT_PPP_SERIAL = 50,
240        TRACE_DLT_LINKTYPE_RAW = 101,
241        TRACE_DLT_C_HDLC = 104,
242        TRACE_DLT_IEEE802_11 = 105,
243        TRACE_DLT_LINUX_SLL = 113,
244        TRACE_DLT_PFLOG = 117,
245        TRACE_DLT_IEEE802_11_RADIO = 127 /**< Radiotap */
246} libtrace_dlt_t ;
247
248/** Enumeration of link layer types supported by libtrace */
249typedef enum {
250    /* TRACE_TYPE_LEGACY = 0            Obsolete */
251       TRACE_TYPE_HDLC_POS = 1,         /**< HDLC over POS */
252       TRACE_TYPE_ETH = 2,              /**< 802.3 style Ethernet */
253       TRACE_TYPE_ATM = 3,              /**< ATM frame */
254       TRACE_TYPE_80211 = 4,            /**< 802.11 frames */
255       TRACE_TYPE_NONE = 5,             /**< Raw IP frames */
256       TRACE_TYPE_LINUX_SLL = 6,        /**< Linux "null" framing */
257       TRACE_TYPE_PFLOG = 7,            /**< FreeBSD's PFlog */
258    /* TRACE_TYPE_LEGACY_DEFAULT        Obsolete */
259       TRACE_TYPE_POS = 9,              /**< Packet-over-SONET */
260    /* TRACE_TYPE_LEGACY_ATM            Obsolete */
261    /* TRACE_TYPE_LEGACY_ETH            Obsolete */
262       TRACE_TYPE_80211_PRISM = 12,     /**< 802.11 Prism frames */
263       TRACE_TYPE_AAL5 = 13,            /**< ATM Adaptation Layer 5 frames */
264       TRACE_TYPE_DUCK = 14,         /**< Pseudo link layer for DUCK packets */
265       TRACE_TYPE_80211_RADIO = 15,  /**< Radiotap + 802.11 */
266       TRACE_TYPE_LLCSNAP = 16,      /**< Raw LLC/SNAP */
267       TRACE_TYPE_PPP = 17,          /**< PPP frames */
268       TRACE_TYPE_METADATA = 18,        /**< WDCAP-style meta-data */
269       TRACE_TYPE_NONDATA = 19          /**< Not a data packet */
270} libtrace_linktype_t;
271
272/** RT protocol base format identifiers.
273 * This is used to describe the capture format of the packet is being sent
274 * using the RT protocol.
275 */
276enum base_format_t {
277        TRACE_FORMAT_ERF          =1,   /**< ERF (DAG capture format) */
278        TRACE_FORMAT_PCAP         =2,   /**< Live PCAP capture */
279        TRACE_FORMAT_PCAPFILE     =3,   /**< PCAP trace file */
280        TRACE_FORMAT_WAG          =4,   /**< WAG live capture (Obsolete) */
281        TRACE_FORMAT_RT           =5,   /**< RT network protocol */
282        TRACE_FORMAT_LEGACY_ATM   =6,   /**< Legacy ERF for ATM capture */
283        TRACE_FORMAT_LEGACY_POS   =7,   /**< Legacy ERF for POS capture */
284        TRACE_FORMAT_LEGACY_ETH   =8,   /**< Legacy ERF for ETH capture */
285        TRACE_FORMAT_LINUX_NATIVE =9,   /**< Linux native interface capture */
286        TRACE_FORMAT_DUCK         =10,  /**< DAG Clock information */
287        TRACE_FORMAT_BPF          =11,  /**< BSD native interface capture */
288        TRACE_FORMAT_TSH          =12,  /**< TSH trace format */
289        TRACE_FORMAT_ATMHDR       =13,  /**< Legacy ATM header capture */
290        TRACE_FORMAT_LEGACY_NZIX  =14   /**< Legacy format used for NZIX traces */
291};
292
293/* RT protocol packet types */
294typedef enum {
295        TRACE_RT_HELLO          =1, /**< Connection accepted */
296        TRACE_RT_START          =2, /**< Request for data transmission to begin
297                                    */
298        TRACE_RT_ACK            =3, /**< Data acknowledgement */
299        TRACE_RT_STATUS         =4, /**< Fifo status packet */
300        TRACE_RT_DUCK           =5, /**< Dag duck info packet */
301        TRACE_RT_END_DATA       =6, /**< Server is exiting message */
302        TRACE_RT_CLOSE          =7, /**< Client is exiting message */
303        TRACE_RT_DENY_CONN      =8, /**< Connection has been denied */
304        TRACE_RT_PAUSE          =9, /**< Request server to suspend sending data
305                                     */
306        TRACE_RT_PAUSE_ACK      =10,/**< Server is paused message */
307        TRACE_RT_OPTION         =11,/**< Option request */
308        TRACE_RT_KEYCHANGE      =12,/**< Anonymisation key has changed */
309        TRACE_RT_DUCK_2_4       =13,/**< Dag 2.4 Duck */
310        TRACE_RT_DUCK_2_5       =14,/**< Dag 2.5 Duck */
311        TRACE_RT_LOSTCONN       =15,/**< Lost connection to server */
312        TRACE_RT_SERVERSTART    =16,/**< Reliable server has been restarted */
313        TRACE_RT_CLIENTDROP     =17,/**< Reliable client was lost */
314        TRACE_RT_METADATA       =18,/**< Packet contains server meta-data */
315
316        /** Not actually used - all DATA types begin from this value */
317        TRACE_RT_DATA_SIMPLE    = 1000,
318       
319        /** RT is encapsulating an ERF capture record */
320        TRACE_RT_DATA_ERF       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_ERF,
321        /** RT is encapsulating a WAG capture record */
322        TRACE_RT_DATA_WAG       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_WAG,
323        /** RT is encapsulating a Legacy ATM capture record */
324        TRACE_RT_DATA_LEGACY_ATM=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_ATM,
325        /** RT is encapsulating a Legacy POS capture record */
326        TRACE_RT_DATA_LEGACY_POS=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_POS,
327        /** RT is encapsulating a Legacy ETH capture record */
328        TRACE_RT_DATA_LEGACY_ETH=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_ETH,
329        /** RT is encapsulating a Linux native capture record */
330        TRACE_RT_DATA_LINUX_NATIVE=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LINUX_NATIVE,
331        /** RT is encapsulating a BSD native capture record */
332        TRACE_RT_DATA_BPF       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_BPF,
333        /** RT is encapsulating a TSH capture record */
334        TRACE_RT_DATA_TSH       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_TSH,
335        /** RT is encapsulating an ATM header capture record */
336        TRACE_RT_DATA_ATMHDR = TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_ATMHDR,
337        /** RT is encapsulating a Legacy NZIX capture record */
338        TRACE_RT_DATA_LEGACY_NZIX=TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_LEGACY_NZIX,
339
340        /** As PCAP does not store the linktype with the packet, we need to
341         * create a separate RT type for each supported DLT, starting from
342         * this value */
343        TRACE_RT_DATA_DLT               = 2000,
344        /** RT is encapsulating a PCAP capture record with a NULL linktype */
345        TRACE_RT_DLT_NULL               =TRACE_RT_DATA_DLT+TRACE_DLT_NULL,
346        /** RT is encapsulating a PCAP capture record with an Ethernet
347         * linktype */
348        TRACE_RT_DLT_EN10MB             =TRACE_RT_DATA_DLT+TRACE_DLT_EN10MB,
349        /** RT is encapsulating a PCAP capture record with an 802.11
350         * linktype */
351        TRACE_RT_DLT_IEEE802_11         =TRACE_RT_DATA_DLT+TRACE_DLT_IEEE802_11,
352        /** RT is encapsulating a PCAP capture record with a Linux SLL
353         * linktype */
354        TRACE_RT_DLT_LINUX_SLL          =TRACE_RT_DATA_DLT+TRACE_DLT_LINUX_SLL,
355        /** RT is encapsulating a PCAP capture record with a PFlog linktype */
356        TRACE_RT_DLT_PFLOG              =TRACE_RT_DATA_DLT+TRACE_DLT_PFLOG,
357        /** RT is encapsulating a PCAP capture record with an AAL5 linktype */
358        TRACE_RT_DLT_ATM_RFC1483        =TRACE_RT_DATA_DLT+TRACE_DLT_ATM_RFC1483,
359        /** Unused value marking the end of the valid range for PCAP RT
360         * encapsulation */
361        TRACE_RT_DATA_DLT_END           = 2999,
362        /** Unused value marking the end of the valid range for all RT packet
363         * types */
364        TRACE_RT_LAST                   = (2<<31)
365} libtrace_rt_types_t;
366
367/** IP Protocol values */
368typedef enum {
369        TRACE_IPPROTO_IP        = 0,    /**< IP pseudo protocol number */
370        TRACE_IPPROTO_ICMP      = 1,    /**< Internet Control Message protocol */
371        TRACE_IPPROTO_IGMP      = 2,    /**< Internet Group Management Protocol */
372        TRACE_IPPROTO_IPIP      = 4,    /**< IP encapsulated in IP */
373        TRACE_IPPROTO_TCP       = 6,    /**< Transmission Control Protocol */
374        TRACE_IPPROTO_UDP       = 17,   /**< User Datagram Protocol */
375        TRACE_IPPROTO_IPV6      = 41,   /**< IPv6 over IPv4 */
376        TRACE_IPPROTO_ROUTING   = 43,   /**< IPv6 Routing header */
377        TRACE_IPPROTO_FRAGMENT  = 44,   /**< IPv6 Fragmentation header */
378        TRACE_IPPROTO_RSVP      = 46,   /**< Resource Reservation Protocol */
379        TRACE_IPPROTO_GRE       = 47,   /**< General Routing Encapsulation */
380        TRACE_IPPROTO_ESP       = 50,   /**< Encapsulated Security Payload [RFC2406] */
381        TRACE_IPPROTO_AH        = 51,   /**< Authentication Header [RFC2402] */
382        TRACE_IPPROTO_ICMPV6    = 58,   /**< ICMPv6 */
383        TRACE_IPPROTO_NONE      = 59,   /**< IPv6 no next header */
384        TRACE_IPPROTO_DSTOPTS   = 60,   /**< IPv6 destination options */
385        TRACE_IPPROTO_PIM       = 103,  /**< Protocol Independant Multicast */
386        TRACE_IPPROTO_SCTP      = 132   /**< Stream Control Transmission Protocol */
387} libtrace_ipproto_t;
388
389/** Ethertypes supported by Libtrace */
390typedef enum {
391        /* Numbers <=1500 are of course, LLC/SNAP */
392        TRACE_ETHERTYPE_IP      = 0x0800,       /**< IPv4 */
393        TRACE_ETHERTYPE_ARP     = 0x0806,       /**< Address resolution protocol */
394        TRACE_ETHERTYPE_RARP    = 0x8035,       /**< Reverse ARP */
395        TRACE_ETHERTYPE_8021Q   = 0x8100,       /**< 802.1q VLAN Extended Header */
396        TRACE_ETHERTYPE_IPV6    = 0x86DD,       /**< IPv6 */
397        TRACE_ETHERTYPE_MPLS    = 0x8847,       /**< MPLS Unicast traffic */
398        TRACE_ETHERTYPE_MPLS_MC = 0x8848,       /**< MPLS Multicast traffic */
399        TRACE_ETHERTYPE_PPP_DISC = 0x8863,      /**< PPPoE Service Discovery */
400        TRACE_ETHERTYPE_PPP_SES = 0x8864        /**< PPPoE Session Messages */
401} libtrace_ethertype_t;
402
403/** The libtrace packet structure. Applications shouldn't be
404 * meddling around in here
405 */
406typedef struct libtrace_packet_t {
407        struct libtrace_t *trace;       /**< Pointer to the trace */
408        void *header;                   /**< Pointer to the framing header */
409        void *payload;                  /**< Pointer to the link layer */
410        void *buffer;                   /**< Allocated buffer */
411        libtrace_rt_types_t  type;      /**< RT protocol type for the packet */
412        buf_control_t buf_control;      /**< Describes memory ownership */
413        int capture_length;             /**< Cached capture length */
414        void *l3_header;                /**< Cached l3 header */
415        uint16_t l3_ethertype;          /**< Cached l3 ethertype */
416} libtrace_packet_t;
417
418
419/** Trace directions.
420 * Note that these are the directions used by convention. More directions
421 * are possible, not just these 3, and that they may not conform to this
422 * convention.
423 */
424typedef enum {
425        TRACE_DIR_OUTGOING = 0,         /**< Packets originating "inside" */
426        TRACE_DIR_INCOMING = 1,         /**< Packets originating "outside" */
427        TRACE_DIR_OTHER    = 2          /**< Packets with an unknown direction, or one that's unknown */
428} libtrace_direction_t;
429
430/** Enumeration of Radiotap fields */
431typedef enum {
432    TRACE_RADIOTAP_TSFT = 0, /**< Timer synchronisation function, in microseconds (uint64) */
433    TRACE_RADIOTAP_FLAGS = 1, /**< Wireless flags (uint8) */
434    TRACE_RADIOTAP_RATE = 2, /**< Bitrate in units of 500kbps (uint8) */
435    TRACE_RADIOTAP_CHANNEL = 3, /**< Frequency in MHz (uint16) and channel flags (uint16) */
436    TRACE_RADIOTAP_FHSS = 4, /**< FHSS hop set (uint8) and hopping pattern (uint8) */
437    TRACE_RADIOTAP_DBM_ANTSIGNAL = 5, /**< Signal power in dBm (int8) */
438    TRACE_RADIOTAP_DBM_ANTNOISE = 6, /**< Noise power in dBm (int8) */
439    TRACE_RADIOTAP_LOCK_QUALITY = 7, /**< Barker Code lock quality (uint16) */
440    TRACE_RADIOTAP_TX_ATTENUATION = 8, /**< TX attenuation as unitless distance from max power (uint16) */
441    TRACE_RADIOTAP_DB_TX_ATTENUATION = 9, /**< TX attenutation as dB from max power (uint16) */
442    TRACE_RADIOTAP_DBM_TX_POWER = 10, /**< TX Power in dBm (int8) */
443    TRACE_RADIOTAP_ANTENNA = 11, /**< Antenna frame was rx'd or tx'd on (uint8) */
444    TRACE_RADIOTAP_DB_ANTSIGNAL = 12, /**< Signal power in dB from a fixed reference (uint8) */
445    TRACE_RADIOTAP_DB_ANTNOISE = 13, /**< Noise power in dB from a fixed reference (uint8) */
446    TRACE_RADIOTAP_RX_FLAGS = 14, /** Properties of received frame (uint16) */
447    TRACE_RADIOTAP_TX_FLAGS = 15, /** Properties of transmitted frame (uint16) */
448    TRACE_RADIOTAP_RTS_RETRIES = 16, /** Number of rts retries frame used (uint8) */
449    TRACE_RADIOTAP_DATA_RETRIES = 17, /** Number of unicast retries a transmitted frame used (uint8) */
450    TRACE_RADIOTAP_EXT = 31
451} libtrace_radiotap_field_t;
452
453
454/** @name Protocol structures
455 * These convenience structures provide portable versions of the headers
456 * for a variety of protocols.
457 * @{
458 */
459
460#ifdef WIN32
461#pragma pack(push)
462#pragma pack(1)
463#endif
464
465/** Generic IP header structure */
466typedef struct libtrace_ip
467{
468#if BYTE_ORDER == LITTLE_ENDIAN
469    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
470    LT_BITFIELD8 ip_v:4;                /**< Version */
471#elif BYTE_ORDER == BIG_ENDIAN
472    LT_BITFIELD8 ip_v:4;                /**< Version */
473    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
474#else
475#   error "Adjust your <bits/endian.h> defines"
476#endif
477    uint8_t  ip_tos;                    /**< Type of Service */
478    uint16_t ip_len;                    /**< Total Length */
479    int16_t  ip_id;                     /**< Identification */
480    uint16_t ip_off;                    /**< IP Fragment offset (and flags) */
481    uint8_t  ip_ttl;                    /**< Time to Live */
482    uint8_t  ip_p;                      /**< Protocol */
483    uint16_t ip_sum;                    /**< Checksum */
484    struct in_addr ip_src;              /**< Source Address */
485    struct in_addr ip_dst;              /**< Destination Address */
486} PACKED libtrace_ip_t;
487
488/** IPv6 header extension structure */
489typedef struct libtrace_ip6_ext
490{
491        uint8_t nxt;    /**< Next header */
492        uint8_t len;    /**< Length of the current header */
493} PACKED libtrace_ip6_ext_t;
494
495/** Generic IPv6 header structure
496 *
497 * @note The flow label field also includes the Version and Traffic Class
498 * fields, because we haven't figured out a nice way to deal with fields
499 * crossing byte boundaries on both Big and Little Endian machines */
500typedef struct libtrace_ip6
501{
502    uint32_t flow;                      /**< Flow label */
503    uint16_t plen;                      /**< Payload length */
504    uint8_t nxt;                        /**< Next header */
505    uint8_t hlim;                       /**< Hop limit */
506    struct in6_addr ip_src;             /**< Source address */
507    struct in6_addr ip_dst;             /**< Destination address */
508} PACKED libtrace_ip6_t;
509
510/** Generic TCP header structure */
511typedef struct libtrace_tcp
512  {
513    uint16_t source;            /**< Source Port */
514    uint16_t dest;              /**< Destination port */
515    uint32_t seq;               /**< Sequence number */
516    uint32_t ack_seq;           /**< Acknowledgement Number */
517#  if BYTE_ORDER == LITTLE_ENDIAN
518    LT_BITFIELD8 res1:4;        /**< Reserved bits */
519    LT_BITFIELD8 doff:4;        /**< Data Offset */     
520    LT_BITFIELD8 fin:1;         /**< FIN */
521    LT_BITFIELD8 syn:1;         /**< SYN flag */
522    LT_BITFIELD8 rst:1;         /**< RST flag */
523    LT_BITFIELD8 psh:1;         /**< PuSH flag */
524    LT_BITFIELD8 ack:1;         /**< ACK flag */
525    LT_BITFIELD8 urg:1;         /**< URG flag */
526    LT_BITFIELD8 res2:2;        /**< Reserved */
527#  elif BYTE_ORDER == BIG_ENDIAN
528    LT_BITFIELD8 doff:4;        /**< Data offset */
529    LT_BITFIELD8 res1:4;        /**< Reserved bits */
530    LT_BITFIELD8 res2:2;        /**< Reserved */
531    LT_BITFIELD8 urg:1;         /**< URG flag */
532    LT_BITFIELD8 ack:1;         /**< ACK flag */
533    LT_BITFIELD8 psh:1;         /**< PuSH flag */
534    LT_BITFIELD8 rst:1;         /**< RST flag */
535    LT_BITFIELD8 syn:1;         /**< SYN flag */
536    LT_BITFIELD8 fin:1;         /**< FIN flag */
537#  else
538#   error "Adjust your <bits/endian.h> defines"
539#  endif
540    uint16_t window;            /**< Window Size */
541    uint16_t check;             /**< Checksum */
542    uint16_t urg_ptr;           /**< Urgent Pointer */
543} PACKED libtrace_tcp_t;
544
545/** Generic UDP header structure */
546typedef struct libtrace_udp {
547  uint16_t      source;         /**< Source port */
548  uint16_t      dest;           /**< Destination port */
549  uint16_t      len;            /**< Length */
550  uint16_t      check;          /**< Checksum */
551} PACKED libtrace_udp_t;
552
553/** Generic ICMP header structure */
554typedef struct libtrace_icmp
555{
556  uint8_t type;         /**< Message Type */
557  uint8_t code;         /**< Type Sub-code */
558  uint16_t checksum;            /**< Checksum */
559  union
560  {
561    struct
562    {
563      uint16_t  id;             /**< ID of the Echo request */
564      uint16_t  sequence;       /**< Sequence number of the Echo request */
565    } echo;                     /**< Echo Datagram */
566    uint32_t    gateway;        /**< Gateway Address */
567    struct
568    {
569      uint16_t  unused;         /**< Unused */
570      uint16_t  mtu;            /**< Next-hop MTU */
571    } frag;                     /**< Path MTU Discovery */
572  } un;                         /**< Union for Payloads of Various ICMP Codes */
573} PACKED libtrace_icmp_t;
574
575/** Generic LLC/SNAP header structure */
576typedef struct libtrace_llcsnap
577{
578/* LLC */
579  uint8_t dsap;                 /**< Destination Service Access Point */
580  uint8_t ssap;                 /**< Source Service Access Point */
581  uint8_t control;              /**< Control field */
582/* SNAP */
583  LT_BITFIELD32 oui:24;         /**< Organisationally Unique Identifier (scope)*/
584  uint16_t type;                /**< Protocol within OUI */
585} PACKED libtrace_llcsnap_t;
586
587/** 802.3 frame */
588typedef struct libtrace_ether
589{
590  uint8_t ether_dhost[6];       /**< Destination Ether Addr */
591  uint8_t ether_shost[6];       /**< Source Ether Addr */
592  uint16_t ether_type;          /**< Packet Type ID Field (next-header) */
593} PACKED libtrace_ether_t;
594
595/** 802.1Q frame */
596typedef struct libtrace_8021q
597{
598  LT_BITFIELD16 vlan_pri:3;      /**< VLAN User Priority */
599  LT_BITFIELD16 vlan_cfi:1;      /**< VLAN Format Indicator,
600                                   * 0 for ethernet, 1 for token ring */
601  LT_BITFIELD16 vlan_id:12;      /**< VLAN Id */
602  uint16_t vlan_ether_type;      /**< VLAN Sub-packet Type ID Field
603                                   * (next-header)*/
604} PACKED libtrace_8021q_t;
605
606/** ATM User Network Interface (UNI) Cell. */
607typedef struct libtrace_atm_cell
608{
609  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
610  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
611  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
612  LT_BITFIELD32 pt:3;           /**< Payload Type */
613  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
614  LT_BITFIELD32 hec:8;          /**< Header Error Control */
615} PACKED libtrace_atm_cell_t;
616
617/** ATM Network Node/Network Interface (NNI) Cell. */
618typedef struct libtrace_atm_nni_cell
619{
620  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
621  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
622  LT_BITFIELD32 pt:3;           /**< Payload Type */
623  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
624  LT_BITFIELD32 hec:8;          /**< Header Error Control */
625} PACKED libtrace_atm_nni_cell_t;
626
627/** Captured UNI cell.
628 *
629 * Endace don't capture the HEC, presumably to keep alignment.  This
630 * version of the \ref libtrace_atm_cell is used when dealing with DAG
631 * captures of uni cells.
632 *
633 */
634typedef struct libtrace_atm_capture_cell
635{
636  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
637  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
638  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
639  LT_BITFIELD32 pt:3;           /**< Payload Type */
640  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
641} PACKED libtrace_atm_capture_cell_t;
642
643/** Captured NNI cell.
644 *
645 * Endace don't capture the HEC, presumably to keep alignment.  This
646 * version of the \ref libtrace_atm_nni_cell is used when dealing with DAG
647 * captures of nni cells.
648 *
649 */
650typedef struct libtrace_atm_nni_capture_cell
651{
652  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
653  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
654  LT_BITFIELD32 pt:3;           /**< Payload Type */
655  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
656  LT_BITFIELD32 hec:8;          /**< Header Error Control */
657} PACKED libtrace_atm_nni_capture_cell_t;
658
659/** PPP header */
660typedef struct libtrace_ppp
661{
662 /* I can't figure out where the hell these two variables come from. They're
663  * definitely not in RFC 1661 which defines PPP. Probably some weird thing
664  * relating to the lack of distinction between PPP, HDLC and CHDLC */
665       
666/* uint8_t address; */          /**< PPP Address (0xFF - All stations) */
667/* uint8_t header;  */          /**< PPP Control (0x03 - Unnumbered info) */
668 uint16_t protocol;             /**< PPP Protocol (htons(0x0021) - IPv4 */
669} PACKED libtrace_ppp_t;
670
671/** PPPoE header */
672typedef struct libtrace_pppoe
673{
674 LT_BITFIELD8 version:4;        /**< Protocol version number */
675 LT_BITFIELD8 type:4;           /**< PPPoE Type */
676 uint8_t code;                  /**< PPPoE Code */
677 uint16_t session_id;           /**< Session Identifier */
678 uint16_t length;               /**< Total Length of the PPP packet */
679} PACKED libtrace_pppoe_t;
680
681/** 802.11 header */
682typedef struct libtrace_80211_t {
683#if BYTE_ORDER == LITTLE_ENDIAN
684        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
685        LT_BITFIELD32      type:2;      /**< Frame Type */
686        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
687#else
688        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
689        LT_BITFIELD32      type:2;      /**< Frame Type */
690        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
691#endif
692
693#if BYTE_ORDER == LITTLE_ENDIAN
694        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
695        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
696        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
697        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
698        LT_BITFIELD32      power:1;     /**< Power Management mode */
699        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
700        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
701        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
702#else
703        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
704        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
705        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
706        LT_BITFIELD32      power:1;     /**< Power Management mode */
707        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
708        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
709        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
710        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
711#endif
712        uint16_t     duration;  /**< Duration value for NAV calculation */
713        uint8_t      mac1[6];   /**< MAC Address 1 */
714        uint8_t      mac2[6];   /**< MAC Address 2 */
715        uint8_t      mac3[6];   /**< MAC Address 3 */
716        uint16_t     SeqCtl;    /**< Sequence Control */       
717        uint8_t      mac4[6];   /**< MAC Address 4 */
718} PACKED libtrace_80211_t;
719
720/** The Radiotap header pre-amble
721 *
722 * All Radiotap headers start with this pre-amble, followed by the fields
723 * specified in the it_present bitmask. If bit 31 of it_present is set, then
724 * another bitmask follows.
725 * @note All of the radiotap data fields are in little-endian byte-order.
726 */
727typedef struct libtrace_radiotap_t {
728    uint8_t     it_version; /**< Radiotap version */
729    uint8_t     it_pad; /**< Padding for natural alignment */
730    uint16_t    it_len; /**< Length in bytes of the entire Radiotap header */
731    uint32_t    it_present; /**< Which Radiotap fields are present */
732} PACKED libtrace_radiotap_t;
733
734
735#ifdef WIN32
736#pragma pack(pop)
737#endif
738
739
740/*@}*/
741
742/** Prints help information for libtrace
743 *
744 * Function prints out some basic help information regarding libtrace,
745 * and then prints out the help() function registered with each input module
746 */
747DLLEXPORT void trace_help(void);
748
749/** @name Trace management
750 * These members deal with creating, configuring, starting, pausing and
751 * cleaning up a trace object
752 *@{
753 */
754
755/** Create an input trace from a URI
756 *
757 * @param uri A valid libtrace URI to be opened
758 * @return An opaque pointer to a libtrace_t
759 *
760 * Some valid URI's are:
761 *  - erf:/path/to/erf/file
762 *  - erf:-  (stdin)
763 *  - dag:/dev/dagcard                 
764 *  - pcapint:pcapinterface                (eg: pcap:eth0)
765 *  - pcap:/path/to/pcap/file
766 *  - pcap:-
767 *  - rt:hostname
768 *  - rt:hostname:port
769 *
770 *  If an error occured when attempting to open the trace file, a
771 *  trace is still returned so trace_is_err() should be called to find out
772 *  if an error occured. The trace is created in the configuration state, you
773 *  must call trace_start before attempting to read packets from the trace.
774 */
775DLLEXPORT libtrace_t *trace_create(const char *uri);
776
777/** Creates a "dummy" trace file that has only the format type set.
778 *
779 * @param uri A valid (but fake) URI indicating the format of the dummy trace that is to be created.
780 * @return An opaque pointer to a (sparsely initialised) libtrace_t
781 *
782 * Only the format portion of the uri parameter matters - the 'file' being
783 * opened does not have to exist.
784 *
785 * @note IMPORTANT: Do not attempt to call trace_read_packet or other such
786 * functions with the dummy trace. Its intended purpose is to provide access
787 * to the format functions where the original trace may no longer exist or is
788 * not the correct format, e.g. reading ERF packets from an RT input.
789 */
790DLLEXPORT libtrace_t *trace_create_dead(const char *uri);
791
792/** Creates a trace output file from a URI.
793 *
794 * @param uri The uri string describing the output format and destination
795 * @return An opaque pointer to a libtrace_output_t
796 *
797 * Valid URIs include:
798 *  - erf:/path/to/erf/file
799 *  - pcap:/path/to/pcap/file
800 *
801 *  If an error occured when attempting to open the output trace, a trace is
802 *  still returned but trace_errno will be set. Use trace_is_err_out() and
803 *  trace_perror_output() to get more information.
804 */
805DLLEXPORT libtrace_out_t *trace_create_output(const char *uri);
806
807/** Start an input trace
808 * @param libtrace      The trace to start
809 * @return 0 on success, -1 on failure
810 *
811 * This does the actual work of starting the input trace and applying
812 * all the config options.  This may fail, returning -1. The libtrace error
813 * handling functions can be used to get more information about what
814 * specifically went wrong.
815 */
816DLLEXPORT int trace_start(libtrace_t *libtrace);
817
818/** Pauses an input trace
819 * @param libtrace      The trace to pause
820 * @return 0 on success, -1 on failure
821 *
822 * This stops an input trace that is in progress and returns you to the
823 * configuration state.  Any packets that arrive on a live capture after
824 * trace_pause() has been called will be discarded.  To resume the trace, call
825 * trace_start().
826 */
827DLLEXPORT int trace_pause(libtrace_t *libtrace);
828
829/** Start an output trace
830 * @param libtrace      The trace to start
831 * @return 0 on success, -1 on failure
832 *
833 * This does the actual work with starting a trace capable of writing packets. 
834 * This generally creates the output file.
835 */
836DLLEXPORT int trace_start_output(libtrace_out_t *libtrace);
837
838/** Valid configuration options for input traces */
839typedef enum {
840        /** Maximum number of bytes to be captured for any given packet */
841        TRACE_OPTION_SNAPLEN,   
842
843        /** If enabled, places a live capture interface into promiscuous mode */
844        TRACE_OPTION_PROMISC,   
845
846        /** Apply this filter to all packets read from this trace */
847        TRACE_OPTION_FILTER,   
848
849        /** Defines the frequency of meta-data reporting, e.g. DUCK packets */
850        TRACE_OPTION_META_FREQ,
851
852        /* If enabled, the libtrace event API will ignore time gaps between
853         * packets when reading from a trace file */
854        TRACE_OPTION_EVENT_REALTIME
855} trace_option_t;
856
857/** Sets an input config option
858 * @param libtrace      The trace object to apply the option to
859 * @param option        The option to set
860 * @param value         The value to set the option to
861 * @return -1 if option configuration failed, 0 otherwise
862 * This should be called after trace_create, and before trace_start
863 */
864DLLEXPORT int trace_config(libtrace_t *libtrace,
865                trace_option_t option,
866                void *value);
867
868/** Valid configuration options for output traces */
869typedef enum {
870        /** File flags to use when opening an output file, e.g. O_APPEND */
871        TRACE_OPTION_OUTPUT_FILEFLAGS,
872        /** Compression level: 0 = no compression, 1 = faster compression,
873         * 9 = better compression */
874        TRACE_OPTION_OUTPUT_COMPRESS   
875} trace_option_output_t;
876
877/** Sets an output config option
878 *
879 * @param libtrace      The output trace object to apply the option to
880 * @param option        The option to set
881 * @param value         The value to set the option to
882 * @return -1 if option configuration failed, 0 otherwise
883 * This should be called after trace_create_output, and before
884 * trace_start_output
885 */
886DLLEXPORT int trace_config_output(libtrace_out_t *libtrace,
887                trace_option_output_t option,
888                void *value
889                );
890
891/** Close an input trace, freeing up any resources it may have been using
892 *
893 * @param trace         The input trace to be destroyed
894 *
895 */
896DLLEXPORT void trace_destroy(libtrace_t *trace);
897
898/** Close a dummy trace file, freeing up any resources it may have been using
899 * @param trace         The dummy trace to be destroyed
900 */
901DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
902
903/** Close an output trace, freeing up any resources it may have been using
904 * @param trace         The output trace to be destroyed
905 */
906DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
907
908/** Check (and clear) the current error state of an input trace
909 * @param trace         The input trace to check the error state on
910 * @return The current error status and message
911 *
912 * This reads and returns the current error state and sets the current error
913 * to "no error".
914 */
915DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
916
917/** Indicate if there has been an error on an input trace
918 * @param trace         The input trace to check the error state on
919 * @return true if an error has occurred, false otherwise
920 *
921 * @note This does not clear the error status, and only returns true or false.
922 */
923DLLEXPORT bool trace_is_err(libtrace_t *trace);
924
925/** Outputs the error message for an input trace to stderr and clear the error
926 * status.
927 * @param trace         The input trace with the error to output
928 * @param msg           The message to prepend to the error
929 *
930 * This function does clear the error status.
931 */
932DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...) PRINTF(2,3);
933
934/** Checks (and clears) the current error state for an output trace
935 * @param trace         The output trace to check the error state on
936 * @return The current error status and message
937 *
938 * This reads and returns the current error state and sets the current error
939 * to "no error".
940 */
941DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
942
943/** Indicates if there is an error on an output trace
944 * @param trace         The output trace to check the error state on
945 * @return true if an error has occurred, false otherwise.
946 *
947 * This does not clear the error status, and only returns true or false.
948 */
949DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
950
951/** Outputs the error message for an output trace to stderr and clear the error
952 * status.
953 * @param trace         The output trace with the error to output
954 * @param msg           The message to prepend to the error
955 * This function does clear the error status.
956 */
957DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...)
958        PRINTF(2,3);
959
960/** Returns the number of packets observed on an input trace.
961 * Includes the number of packets counted as early as possible, before
962 * filtering, and includes dropped packets.
963 *
964 * @param trace         The input trace to examine
965 * @returns The number of packets seen at the capture point before filtering.
966 *
967 * If the number is not known, this function will return UINT64_MAX
968 */
969DLLEXPORT
970uint64_t trace_get_received_packets(libtrace_t *trace);
971
972/** Returns the number of packets that were captured, but discarded for not
973 * matching a provided filter.
974 *
975 * @param trace         The input trace to examine
976 * @returns The number of packets that were successfully captured, but filtered
977 *
978 * If the number is not known, this function will return UINT64_MAX
979 */
980DLLEXPORT
981uint64_t trace_get_filtered_packets(libtrace_t *trace);
982
983/** Returns the number of packets that have been dropped on an input trace due
984 * to lack of buffer space on the capturing device.
985 *
986 * @param trace         The input trace to examine
987 * @returns The number of packets captured, but dropped due to buffer overruns
988 *
989 * If the number is not known, this function will return UINT64_MAX
990 */
991DLLEXPORT
992uint64_t trace_get_dropped_packets(libtrace_t *trace);
993
994/** Returns the number of packets that have been read from the input trace using
995 * trace_read_packet().
996 *
997 * @param trace         The input trace to examine
998 * @returns The number of packets that have been read by the libtrace user.
999 *
1000 * If the number is not known, this function will return UINT64_MAX
1001 */
1002DLLEXPORT
1003uint64_t trace_get_accepted_packets(libtrace_t *trace);
1004
1005
1006/*@}*/
1007
1008/** @name Reading/Writing packets
1009 * These members deal with creating, reading and writing packets
1010 *
1011 * @{
1012 */
1013
1014/** Create a new packet object
1015 *
1016 * @return A pointer to an initialised libtrace_packet_t object
1017 */
1018DLLEXPORT libtrace_packet_t *trace_create_packet(void);
1019
1020/** Copy a packet object
1021 * @param packet        The source packet to copy
1022 * @return A new packet which has the same content as the source packet
1023 *
1024 * @note This always involves a copy, which can be slow.  Use of this
1025 * function should be avoided where possible.
1026 *
1027 * @par The reason you would want to use this function is that a zerocopied
1028 * packet from a device will be stored using memory owned by the device which
1029 * may be a limited resource. Copying the packet will ensure that the packet
1030 * is now stored in memory owned and managed by libtrace.
1031 */
1032DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
1033
1034/** Destroy a packet object
1035 * @param packet        The packet to be destroyed
1036 *
1037 */
1038DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
1039
1040
1041/** Read the next packet from an input trace
1042 *
1043 * @param trace         The libtrace opaque pointer for the input trace
1044 * @param packet        The packet opaque pointer
1045 * @return 0 on EOF, negative value on error, number of bytes read when successful.
1046 *
1047 * @note The number of bytes read is usually (but not always) the same as
1048 * trace_get_framing_length()+trace_get_capture_length() depending on the
1049 * trace format.
1050 *
1051 * @note The trace must have been started with trace_start before calling
1052 * this function
1053 *
1054 * @note When reading from a live capture, this function will block until a
1055 * packet is observed on the capture interface. The libtrace event API
1056 * (e.g. trace_event()) should be used if non-blocking operation is required.
1057 */
1058DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
1059
1060/** Event types
1061 * see \ref libtrace_eventobj_t and \ref trace_event
1062 */
1063typedef enum {
1064        TRACE_EVENT_IOWAIT,     /**< Wait on the given file descriptor */
1065        TRACE_EVENT_SLEEP,      /**< Sleep for the given amount of time */
1066        TRACE_EVENT_PACKET,     /**< Packet has been read from input trace */
1067        TRACE_EVENT_TERMINATE   /**< End of input trace */
1068} libtrace_event_t;
1069
1070/** Structure returned by libtrace_event explaining what the current event is */
1071typedef struct libtrace_eventobj_t {
1072        libtrace_event_t type; /**< Event type (iowait,sleep,packet) */
1073       
1074        /** If the event is IOWAIT, the file descriptor to wait on */
1075        int fd;               
1076        /** If the event is SLEEP, the amount of time to sleep for in seconds */
1077        double seconds;       
1078        /** If the event is PACKET, the value returned by trace_read_packet() */
1079        int size;
1080} libtrace_eventobj_t;
1081
1082/** Processes the next libtrace event from an input trace.
1083 * @param trace The libtrace opaque pointer for the input trace
1084 * @param packet The libtrace_packet opaque pointer to use for reading packets
1085 * @return A libtrace_event struct containing the event type and details of the event.
1086 *
1087 * Type can be:
1088 *  TRACE_EVENT_IOWAIT  Waiting on I/O on a file descriptor
1089 *  TRACE_EVENT_SLEEP   Wait a specified amount of time for the next event
1090 *  TRACE_EVENT_PACKET  Packet was read from the trace
1091 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
1092 */
1093DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
1094                libtrace_packet_t *packet);
1095
1096
1097/** Write one packet out to the output trace
1098 *
1099 * @param trace         The libtrace_out opaque pointer for the output trace
1100 * @param packet        The packet opaque pointer of the packet to be written
1101 * @return The number of bytes written out, if zero or negative then an error has occured.
1102 */
1103DLLEXPORT int trace_write_packet(libtrace_out_t *trace, libtrace_packet_t *packet);
1104/*@}*/
1105
1106/** @name Protocol decodes
1107 * These functions locate and return a pointer to various headers inside a
1108 * packet
1109 *
1110 * A packet is divided up into several "layers.":
1111 *
1112 * @li Framing header -- This is the header provided by the capture format
1113 * itself rather than anything that was sent over the network. This provides
1114 * basic details about the packet record including capture lengths, wire
1115 * lengths, timestamps, direction information and any other metadata that is
1116 * part of the capture format. 
1117 *
1118 * @li Metadata header (optional) -- A header containing metadata about a
1119 * packet that was captured, but the metadata was not transmitted over the
1120 * wire.  Some examples include RadioTap and Linux_sll headers.  This can be
1121 * retrieved by trace_get_packet_meta(), or skipped using
1122 * trace_get_payload_from_meta(). There may be multiple "metadata" headers on
1123 * a packet.
1124 *
1125 * @li Layer 2/Link layer/Datalink header -- This can be retrieved by
1126 * trace_get_layer2(), or skipped using trace_get_payload_from_layer2().
1127 *
1128 * @li Layer 3/IP/IPv6 -- This can be retrieved by trace_get_layer3().  As a
1129 * convenience trace_get_ip()/trace_get_ip6() can be used to find an IPv4/IPv6
1130 * header.
1131 *
1132 * @li Layer 5/transport -- These are protocols carried in IPv4/IPv6 frames.
1133 * These can be retrieved using trace_get_transport().
1134 *
1135 * @{
1136 */
1137
1138
1139/** Gets a pointer to the first byte of the packet as it was captured and
1140 * returns its corresponding linktype and capture length.
1141 *
1142 * Use this function instead of the deprecated trace_get_link().
1143 *
1144 * @param packet The packet to get the buffer from
1145 * @param[out] linktype The linktype of the returned pointer
1146 * @param[out] remaining The capture length (the number of captured bytes from
1147 * the returned pointer)
1148 * @return A pointer to the first byte of the packet
1149 */
1150DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet,
1151                libtrace_linktype_t *linktype, uint32_t *remaining);
1152
1153/** Get a pointer to the link layer for a given packet
1154 * @param packet        The packet to get the link layer for
1155 *
1156 * @return A pointer to the link layer, or NULL if there is no link layer
1157 *
1158 * @deprecated This function is deprecated: Use trace_get_packet_buffer() or
1159 * one of the trace_get_layer*() functions instead.
1160 * @note You should call trace_get_link_type to find out what type of link
1161 * layer has been returned to you.
1162 */
1163DLLEXPORT SIMPLE_FUNCTION DEPRECATED
1164void *trace_get_link(const libtrace_packet_t *packet);
1165
1166/** Get a pointer to the IPv4 header (if any) for a given packet
1167 * @param packet        The packet to get the IPv4 header for
1168 *
1169 * @return A pointer to the IPv4 header, or NULL if there is no IPv4 header
1170 *
1171 * If a partial IP header is present, i.e. the packet has been truncated before
1172 * the end of the IP header, this function will return NULL.
1173 *
1174 * You should consider using \ref trace_get_layer3 instead of this function.
1175 */
1176DLLEXPORT SIMPLE_FUNCTION
1177libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
1178
1179/** get a pointer to the IPv6 header (if any)
1180 * @param packet        the packet opaque pointer
1181 *
1182 * @return a pointer to the IPv6 header, or NULL if there is no IPv6 header
1183 *
1184 * You should consider using \ref trace_get_layer3 instead of this function.
1185 */
1186DLLEXPORT SIMPLE_FUNCTION
1187libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet);
1188
1189/** Return a pointer to the first metadata header in a packet, if present.
1190 *
1191 * This function takes a pointer to a libtrace packet and if any metadata
1192 * headers exist, returns a pointer to the first one, along with its
1193 * corresponding linktype.
1194 *
1195 * If no metadata headers exist in the packet, NULL is returned.
1196 *
1197 * A metadata header is a header that was prepended by the capturing device,
1198 * such as a Linux SLL header, or a Radiotap wireless monitoring header.
1199 * Subsequent metadata headers may be accessed with the
1200 * trace_get_payload_from_meta(...) function.
1201 *
1202 * @param packet the libtrace packet
1203 * @param[out] linktype the linktype of the returned metadata header
1204 * @param[out] remaining the number of bytes captured after the returned
1205 * pointer.
1206 * @return a pointer to the first metadata header, or NULL if there are no
1207 * metadata headers present.
1208 *
1209 * remaining may be NULL, however linktype must be provided.
1210 */
1211DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet,
1212                libtrace_linktype_t *linktype,
1213                uint32_t *remaining);
1214
1215/** Returns the payload of a metadata header.
1216 *
1217 * This function takes a pointer to the start of a metadata header (either
1218 * obtained via trace_get_packet_meta(...) or by a previous call to
1219 * trace_get_payload_from_meta(...)) along with its corresponding linktype and
1220 * returns the payload, i.e. the next header. It will also update the linktype
1221 * parameter to indicate the type of payload.
1222 * 
1223 * If the linktype indicates that the header passed in is not a metadata
1224 * header, the function returns NULL to indicate this. The linktype remains
1225 * unchanged in this case.
1226 *
1227 * This function allows the user to iterate through metadata headers which are
1228 * sometimes present before the actual packet as it was received on the wire.
1229 * Examples of metadata headers include the Linux SLL header and the Radiotap
1230 * wireless monitoring header.
1231 *
1232 * If the header is truncated, this function will return NULL, and remaining will be 0.
1233 * If there are 0 bytes of payload, the function will return a pointer, and remaining will be 0.
1234 *
1235 * @param[in] meta a pointer to a header
1236 * @param[in,out] linktype the linktype of meta (updated to indicate the
1237 * linktype of the returned header if applicable).
1238 * @param[in,out] remaining the number of bytes after the meta pointer.
1239 * @return a pointer to the payload of the metadata header. If meta is not a
1240 * pointer to a metadata header, NULL is returned and linktype remains
1241 * unchanged.
1242 *
1243 * All parameters are mandatory.
1244 */
1245DLLEXPORT void *trace_get_payload_from_meta(const void *meta,
1246                libtrace_linktype_t *linktype,
1247                uint32_t *remaining);
1248
1249
1250/** Get a pointer to the layer 2 header. Generally this is the first byte of the
1251 * packet as it was seen on the wire.
1252 *
1253 * This function takes a libtrace packet and skips over any metadata headers if
1254 * present (such as Linux SLL or Radiotap) and returns a pointer to the first
1255 * byte of the packet that was actually received by the network interface.
1256 *
1257 * @param packet the libtrace packet
1258 * @param[out] linktype the linktype of the returned layer 2 header
1259 * @param[out] remaining the number of bytes left in the packet after the
1260 * returned pointer.
1261 * @return a pointer to the first byte of the packet as it was seen on the
1262 * wire.
1263 *
1264 * remaining may be NULL, otherwise it will be filled in by the function.
1265 */
1266DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet,
1267                libtrace_linktype_t *linktype,
1268                uint32_t *remaining);
1269
1270/** Gets a pointer to the next header given a pointer to a layer2 header
1271 *
1272 * @param l2                    The pointer to the current layer2 header
1273 * @param linktype              The type of the layer2 header
1274 * @param[out] ethertype        An optional output variable of the ethernet type of the new header
1275 * @param[in,out] remaining     Updated with the length remaining
1276 *
1277 * @return a pointer to the transport layer header, or NULL if header isn't
1278 * present.
1279 *
1280 * Remaining must point to the number of bytes captured of the layer2 header
1281 * and beyond.  It will be decremented by the number of bytes skipped to find
1282 * the payload.
1283 *
1284 * If the layer2 header is complete but there are zero bytes of payload after the end of the header,
1285 * a pointer to where the payload would be is returned and remaining will be set to 0.  If the
1286 * layer2 header is incomplete (truncated), then NULL is returned and remaining will be set to 0.
1287 *
1288 */
1289DLLEXPORT void *trace_get_payload_from_layer2(void *l2,
1290                libtrace_linktype_t linktype,
1291                uint16_t *ethertype,
1292                uint32_t *remaining);
1293
1294
1295/** Get a pointer to the layer 3 header.
1296 * @param packet                The packet opaque pointer
1297 * @param[out] ethertype        The ethertype of the layer 3 header
1298 * @param[out] remaining        The amount of space available after this header
1299 *                              has been removed.
1300 *
1301 * @return a pointer to the layer 3 header.
1302 * remaining may be NULL, otherwise it will be set to the number of captured
1303 * bytes after the pointer returned.
1304 */
1305DLLEXPORT
1306void *trace_get_layer3(const libtrace_packet_t *packet,
1307                uint16_t *ethertype, uint32_t *remaining);
1308
1309/** Gets a pointer to the transport layer header (if any)
1310 * @param packet        a pointer to a libtrace_packet structure
1311 * @param[out] proto    transport layer protocol
1312 *
1313 * @return a pointer to the transport layer header, or NULL if there is no
1314 * header
1315 *
1316 * @note proto may be NULL if proto is unneeded.
1317 */
1318DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet,
1319                uint8_t *proto, uint32_t *remaining);
1320
1321/** Gets a pointer to the payload given a pointer to the IP header
1322 * @param ip            The IP Header
1323 * @param[out] proto    An output variable of the IP protocol
1324 * @param[in,out] remaining Updated with the number of bytes remaining
1325 *
1326 * @return a pointer to the transport layer header, or NULL if header isn't
1327 * present.
1328 *
1329 * Remaining will be decremented by the size of the IPv4 header (including any options).
1330 * If the IPv4 header is complete but there are zero bytes of payload after the IPv4 header, a pointer
1331 * to where the payload would be is returned and remaining will be set to 0.  If the IPv4 header is
1332 * incomplete, NULL will be returned and remaining will be set to 0.
1333 *
1334 * proto may be NULL, in which case it won't be updated
1335 *
1336 * @note This is similar to trace_get_transport_from_ip in libtrace2
1337 */
1338DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
1339                uint32_t *remaining);
1340
1341/** Gets a pointer to the payload given a pointer to the IPv6 header
1342 * @param ipptr         The IPv6 Header
1343 * @param[out] proto    An output variable of the protocol of the next header
1344 * @param[in,out] remaining Updated with the number of bytes remaining
1345 *
1346 * @return a pointer to the transport layer header, or NULL if the IPv6 header
1347 * isn't complete.
1348 *
1349 * Remaining will be decremented by the size of the IPv6 header (including any options).
1350 * If the IPv6 header is complete but there are zero bytes of payload after the IPv6 header, a pointer
1351 * to where the payload would be is returned and remaining will be set to 0.  If the IPv6 header is
1352 * incomplete, NULL will be returned and remaining will be set to 0.
1353 *
1354 * proto may be NULL, in which case it won't be updated.
1355 *
1356 */
1357DLLEXPORT void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr,
1358                uint8_t *proto, uint32_t *remaining);
1359
1360/** Gets a pointer to the payload given a pointer to the link header
1361 * @param ip            The link pointer
1362 * @param[out] type     An output variable of the ethernet type
1363 * @param[in,out] remaining Updated with the number of bytes remaining
1364 *
1365 * @return a pointer to the transport layer header, or NULL if header isn't
1366 * present.
1367 *
1368 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1369 * of bytes captured of the linklayer and beyond.  It will be updated after
1370 * this function to the number of bytes remaining after the IP header (and any
1371 * IP options) have been removed.
1372 *
1373 * type may be NULL if not needed.
1374 *
1375 */
1376DLLEXPORT void *trace_get_payload_from_link(void *linkptr,
1377                libtrace_linktype_t linktype,
1378                uint16_t *type, uint32_t *remaining);
1379
1380/** Skips over any 802.1q header, if present.
1381 * @param vlan      A pointer to the vlan header
1382 * @param[out] type  The ethernet type, replaced with the vlan ether type
1383 * @param[in,out] remaining Updated with the number of bytes remaining
1384 *
1385 * @return a pointer to the header beyond the vlan header, if present.
1386 * Otherwise, returns NULL. 
1387 *
1388 * Remaining will be decremented by the size of the vlan header.  If the vlan
1389 * header is complete but there are zero bytes of payload after the vlan
1390 * header, a pointer to where the payload would be is returned and remaining
1391 * will be set to 0.  If the vlan header is incomplete, NULL will be returned
1392 * and remaining will be set to 0.
1393 *
1394 * type will be set to the ethertype contained within the vlan payload.
1395 *
1396 */
1397DLLEXPORT void *trace_get_payload_from_vlan(
1398                void *vlan, uint16_t *type, uint32_t *remaining);
1399
1400/** Skips over a MPLS header
1401 * @param mpls      A pointer to the mpls header
1402 * @param[out] type  The ethernet type, replaced by the ether type of the
1403 * following header - 0x0000 if an Ethernet header is deemed to be next
1404 * @param[in,out] remaining     Updated with the number of bytes remaining
1405 *
1406 * @return a pointer to the header beyond the MPLS label, if present. Will
1407 * return NULL if there is not enough bytes remaining to skip past the MPLS
1408 * label.
1409 *
1410 * Remaining will be decremented by the size of the MPLS header.  If the MPLS
1411 * header is complete but there are zero bytes of payload after the MPLS
1412 * header, a pointer to where the payload would be is returned and remaining
1413 * will be set to 0.  If the MPLS header is incomplete, NULL will be returned
1414 * and remaining will be set to 0.
1415 *
1416 * Type must point to the value of the ethernet type. Libtrace will assert
1417 * fail if type is NULL.
1418 *
1419 * NOTE that this function will only remove one MPLS label at a time - the type
1420 * will be set to 0x8847 if there is another MPLS label after the one
1421 * removed by this function.
1422 *
1423 */
1424DLLEXPORT void *trace_get_payload_from_mpls(
1425                void *mpls, uint16_t *type, uint32_t *remaining);
1426
1427/** Skips over a PPPoE header and the following PPP header
1428 * @param pppoe      A pointer to the PPPoE header
1429 * @param[out] type  The ethernet type, replaced by the ether type of the
1430 * next header - 0x0000 if an Ethernet header is deemed to be next
1431 * @param[in,out] remaining     Updated with the number of bytes remaining
1432 *
1433 * @return a pointer to the header beyond the PPPoE header. NOTE that this
1434 * function will also skip over the PPP header that will immediately follow
1435 * the PPPoE header. This function will return NULL if there are not enough
1436 * bytes remaining to skip past both the PPPoE and PPP headers.
1437 *
1438 * Remaining will be decremented by the size of the PPPoE and PPP headers.  If
1439 * the headers are complete but there are zero bytes of payload after the PPP
1440 * header, a pointer to where the payload would be is returned and remaining
1441 * will be set to 0.  If the PPP or PPPoE header is incomplete, NULL will be
1442 * returned and remaining will be set to 0.
1443 *
1444 * Type must point to the value of the ethernet type. Libtrace will assert
1445 * fail if type is NULL.
1446 *
1447 */
1448DLLEXPORT void *trace_get_payload_from_pppoe(
1449                void *pppoe, uint16_t *type, uint32_t *remaining);
1450
1451/** Gets a pointer to the payload given a pointer to a tcp header
1452 * @param tcp           The tcp Header
1453 * @param[in,out] remaining Updated with the number of bytes remaining
1454 *
1455 * @return a pointer to the tcp payload, or NULL if the tcp header is truncated.
1456 *
1457 * Remaining will be decremented by the size of the TCP header.  If the TCP
1458 * header is complete but there are zero bytes of payload after the TCP
1459 * header, a pointer to where the payload would be is returned and remaining
1460 * will be set to 0.  If the TCP header is incomplete, NULL will be returned
1461 * and remaining will be set to 0.
1462 *
1463 */
1464DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp,
1465                uint32_t *remaining);
1466
1467/** Gets a pointer to the payload given a pointer to a udp header
1468 * @param udp           The udp Header
1469 * @param[in,out] remaining Updated with the number of bytes remaining
1470 *
1471 * @return a pointer to the udp payload, or NULL if the udp header is truncated.
1472 *
1473 * Remaining will be decremented by the size of the TCP header.  If the TCP
1474 * header is complete but there are zero bytes of payload after the TCP
1475 * header, a pointer to where the payload would be is returned and remaining
1476 * will be set to 0.  If the TCP header is incomplete, NULL will be returned
1477 * and remaining will be set to 0.
1478 *
1479 */
1480DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
1481
1482/** Gets a pointer to the payload given a pointer to a icmp header
1483 * @param icmp          The icmp Header
1484 * @param[in,out] remaining Updated with the number of bytes remaining
1485 *
1486 * @return a pointer to the icmp payload, or NULL if the icmp header is truncated.
1487 *
1488 * Remaining will be decremented by the size of the TCP header.  If the TCP
1489 * header is complete but there are zero bytes of payload after the TCP
1490 * header, a pointer to where the payload would be is returned and remaining
1491 * will be set to 0.  If the TCP header is incomplete, NULL will be returned
1492 * and remaining will be set to 0.
1493 *
1494 */
1495DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp,
1496                uint32_t *remaining);
1497
1498/** get a pointer to the TCP header (if any)
1499 * @param packet        the packet opaque pointer
1500 *
1501 * @return a pointer to the TCP header, or NULL if there is not a TCP packet
1502 *
1503 * @note you should probably use trace_get_transport()
1504 */
1505DLLEXPORT SIMPLE_FUNCTION
1506libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
1507
1508/** get a pointer to the TCP header (if any) given a pointer to the IP header
1509 * @param ip            The IP header
1510 * @param[in,out] remaining Updated with the number of bytes remaining
1511 *
1512 * @return a pointer to the TCP header, or NULL if this is not a TCP packet
1513 *
1514 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1515 * of bytes captured of the TCP header and beyond.  It will be decremented by
1516 * the number of bytes in the TCP header (including any TCP options).
1517 *
1518 * @note The last parameter has changed from libtrace2
1519 */
1520DLLEXPORT SIMPLE_FUNCTION
1521libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
1522
1523/** get a pointer to the UDP header (if any)
1524 * @param packet        the packet opaque pointer
1525 *
1526 * @return a pointer to the UDP header, or NULL if this is not a UDP packet
1527 */
1528DLLEXPORT SIMPLE_FUNCTION
1529libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
1530
1531/** get a pointer to the UDP header (if any) given a pointer to the IP header
1532 * @param       ip      The IP header
1533 * @param[in,out] remaining Updated with the number of bytes remaining
1534 *
1535 * @return a pointer to the UDP header, or NULL if this is not an UDP packet
1536 *
1537 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1538 * of bytes captured of the UDP header and beyond.  This function will
1539 * decremented it by the length of the UDP header.
1540 *
1541 * @note Beware the change from libtrace2 from skipped to remaining
1542 */
1543DLLEXPORT SIMPLE_FUNCTION
1544libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1545
1546/** get a pointer to the ICMP header (if any)
1547 * @param packet        the packet opaque pointer
1548 *
1549 * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet
1550 */
1551DLLEXPORT SIMPLE_FUNCTION
1552libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
1553
1554/** get a pointer to the ICMP header (if any) given a pointer to the IP header
1555 * @param ip            The IP header
1556 * @param[in,out] remaining Updated with the number of bytes remaining
1557 *
1558 * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet
1559 *
1560 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1561 * of bytes captured of the ICMP header and beyond.  It will be decremented by
1562 * the length of the ICMP head in bytes.
1563 *
1564 * @note Beware the change from libtrace2 from skipped to remaining
1565 */
1566DLLEXPORT SIMPLE_FUNCTION
1567libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1568
1569/** Get the destination MAC address
1570 * @param packet        the packet opaque pointer
1571 * @return a pointer to the destination mac, (or NULL if there is no
1572 * destination MAC)
1573 */
1574DLLEXPORT SIMPLE_FUNCTION
1575uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
1576
1577/** Get the source MAC address
1578 * @param packet        the packet opaque pointer
1579 * @return a pointer to the source mac, (or NULL if there is no source MAC)
1580 */
1581DLLEXPORT SIMPLE_FUNCTION
1582uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
1583
1584/** Get the source IP address
1585 * @param packet        the packet opaque pointer
1586 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
1587 *                      static storage.
1588 * @return NULL if there is no source address, or a sockaddr holding a v4 or v6
1589 * address, or on some platforms a sockaddr holding a MAC address.
1590 */
1591DLLEXPORT SIMPLE_FUNCTION
1592struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
1593                struct sockaddr *addr);
1594
1595/** Get the destination IP address
1596 * @param packet        the packet opaque pointer
1597 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
1598 *                      static storage.
1599 * @return NULL if there is no destination address, or a sockaddr holding a v4
1600 * or v6 address, or on some platforms a sockaddr holding a MAC address.
1601 */
1602DLLEXPORT SIMPLE_FUNCTION
1603struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
1604                struct sockaddr *addr);
1605
1606/*@}*/
1607
1608/** parse an ip or tcp option
1609 * @param[in,out] ptr   the pointer to the current option
1610 * @param[in,out] len   the length of the remaining buffer
1611 * @param[out] type     the type of the option
1612 * @param[out] optlen   the length of the option
1613 * @param[out] data     the data of the option
1614 *
1615 * @return bool true if there is another option (and the fields are filled in)
1616 *               or false if this was the last option.
1617 *
1618 * This updates ptr to point to the next option after this one, and updates
1619 * len to be the number of bytes remaining in the options area.  Type is updated
1620 * to be the code of this option, and data points to the data of this option,
1621 * with optlen saying how many bytes there are.
1622 *
1623 * @note Beware of fragmented packets.
1624 */
1625DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
1626                        unsigned char *type,
1627                        unsigned char *optlen,
1628                        unsigned char **data);
1629
1630
1631/** @name Time
1632 * These functions deal with time that a packet arrived and return it
1633 * in various formats
1634 * @{
1635 */
1636/** Get the current time in DAG time format
1637 * @param packet        the packet opaque pointer
1638 *
1639 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
1640 * past 1970-01-01, the lower 32bits are partial seconds)
1641 */
1642DLLEXPORT SIMPLE_FUNCTION
1643uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
1644
1645/** Get the current time in struct timeval
1646 * @param packet        the packet opaque pointer
1647 *
1648 * @return time that this packet was seen in a struct timeval
1649 */
1650DLLEXPORT SIMPLE_FUNCTION
1651struct timeval trace_get_timeval(const libtrace_packet_t *packet);
1652
1653/** Get the current time in struct timespec
1654 * @param packet        the packet opaque pointer
1655 *
1656 * @return time that this packet was seen in a struct timespec
1657 */
1658DLLEXPORT SIMPLE_FUNCTION
1659struct timespec trace_get_timespec(const libtrace_packet_t *packet);
1660
1661/** Get the current time in floating point seconds
1662 * @param packet        the packet opaque pointer
1663 *
1664 * @return time that this packet was seen in 64bit floating point seconds from
1665 * the unix epoch (1970-01-01 00:00:00 UTC).
1666 */
1667DLLEXPORT SIMPLE_FUNCTION
1668double trace_get_seconds(const libtrace_packet_t *packet);
1669
1670/** Seek within a trace
1671 * @param trace         trace to seek
1672 * @param seconds       time to seek to
1673 * @return 0 on success.
1674 * Make the next packet read to be the first packet to occur at or after the
1675 * time searched for.  This must be called in the configuration state (ie,
1676 * before trace_start() or after trace_pause().
1677 * @note This function may be extremely slow.
1678 */
1679DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
1680
1681/** Seek within a trace
1682 * @param trace         trace to seek
1683 * @param tv            time to seek to
1684 * @return 0 on success.
1685 * Make the next packet read to be the first packet to occur at or after the
1686 * time searched for.  This must be called in the configuration state (ie,
1687 * before trace_start() or after trace_pause().
1688 * @note This function may be extremely slow.
1689 */
1690DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
1691
1692/** Seek within a trace
1693 * @param trace         trace to seek
1694 * @param ts            erf timestamp
1695 * @return 0 on success.
1696 * Make the next packet read to be the first packet to occur at or after the
1697 * time searched for.  This must be called in the configuration state (ie,
1698 * before trace_start() or after trace_pause().
1699 * @note This function may be extremely slow.
1700 */
1701DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
1702
1703/*@}*/
1704
1705/** @name Sizes
1706 * This section deals with finding or setting the various different lengths
1707 * a packet can have
1708 * @{
1709 */
1710/** Get the size of the packet in the trace (in bytes)
1711 * @param packet        the packet opaque pointer
1712 * @return the size of the packet in the trace
1713 * @note Due to this being a header capture, or anonymisation, this may not
1714 * be the same size as the original packet.  See get_wire_length() for the
1715 * original size of the packet.
1716 * @note This can (and often is) different for different packets in a trace!
1717 * @note This is sometimes called the "snaplen".
1718 * @note The return size refers to the network-level payload of the packet and
1719 * does not include any capture framing headers. For example, an Ethernet
1720 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1721 * sizeof(ip_header) + sizeof(tcp_header), but not the capture file
1722 * (pcap/erf/etc) header.
1723 */
1724DLLEXPORT SIMPLE_FUNCTION
1725size_t trace_get_capture_length(const libtrace_packet_t *packet);
1726
1727/** Get the size of the packet as it was seen on the wire (in bytes).
1728 * @param packet        the packet opaque pointer
1729 * @return the size of the packet as it was on the wire.
1730 * @note Due to the trace being a header capture, or anonymisation this may
1731 * not be the same as the Capture Len.
1732 * @note trace_get_wire_length \em{includes} FCS.  This is different to pcap based tools.
1733 * @note The return size refers to the network-level payload of the packet and
1734 * does not include any capture framing headers. For example, an Ethernet
1735 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1736 * sizeof(ip_header) + sizeof(tcp_header), but not the capture file
1737 * (pcap/erf/etc) header.
1738 */
1739DLLEXPORT SIMPLE_FUNCTION
1740size_t trace_get_wire_length(const libtrace_packet_t *packet);
1741
1742/** Get the length of the capture framing headers (in bytes).
1743 * @param packet        the packet opaque pointer
1744 * @return the size of the packet as it was on the wire.
1745 * @note this length corresponds to the difference between the size of a
1746 * captured packet in memory, and the captured length of the packet
1747 */
1748DLLEXPORT SIMPLE_FUNCTION
1749size_t trace_get_framing_length(const libtrace_packet_t *packet);
1750
1751/** Truncate ("snap") the packet at the suggested length
1752 * @param packet        the packet opaque pointer
1753 * @param size          the new length of the packet (in bytes)
1754 * @return the new capture length of the packet, or the original capture
1755 * length of the packet if unchanged
1756 */
1757DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
1758
1759/*@}*/
1760
1761
1762/** Get the type of the link layer
1763 * @param packet        the packet opaque pointer
1764 * @return libtrace_linktype_t
1765 */
1766DLLEXPORT SIMPLE_FUNCTION
1767libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
1768
1769/** Set the direction flag, if it has one
1770 * @param packet        the packet opaque pointer
1771 * @param direction     the new direction
1772 * @returns -1 on error, or the direction that was set.
1773 */
1774DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
1775
1776/** Get the direction flag, if it has one
1777 * @param packet        the packet opaque pointer
1778 * @return a value containing the direction flag, or -1 if this is not supported
1779 * The direction is defined as 0 for packets originating locally (ie, outbound)
1780 * and 1 for packets originating remotely (ie, inbound).
1781 * Other values are possible, which might be overloaded to mean special things
1782 * for a special trace.
1783 */
1784DLLEXPORT SIMPLE_FUNCTION
1785libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
1786
1787/** @name BPF
1788 * This section deals with using Berkley Packet Filters
1789 * @{
1790 */
1791/** setup a BPF filter
1792 * @param filterstring a char * containing the bpf filter string
1793 * @return opaque pointer pointer to a libtrace_filter_t object
1794 * @note The filter is not actually compiled at this point, so no correctness
1795 * tests are performed here. trace_create_filter will always return ok, but
1796 * if the filter is poorly constructed an error will be generated when the
1797 * filter is actually used
1798 */
1799DLLEXPORT SIMPLE_FUNCTION
1800libtrace_filter_t *trace_create_filter(const char *filterstring);
1801
1802/** Setup a BPF filter based on pre-compiled byte-code.
1803 * @param bf_insns      A pointer to the start of the byte-code
1804 * @param bf_len        The number of BPF instructions 
1805 * @returns             an opaque pointer to a libtrace_filter_t object
1806 * @note                The supplied byte-code is not checked for correctness.
1807 * @author              Scott Raynel
1808 */
1809DLLEXPORT libtrace_filter_t *
1810trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len);
1811
1812/** Apply a BPF filter to a packet
1813 * @param filter        the filter opaque pointer
1814 * @param packet        the packet opaque pointer
1815 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
1816 * @note Due to the way BPF filters are built, the filter is not actually
1817 * compiled until the first time trace_create_filter is called. If your filter
1818 * is incorrect, it will generate an error message and assert, exiting the
1819 * program. This behaviour may change to more graceful handling of this error
1820 * in the future.
1821 */
1822DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
1823                const libtrace_packet_t *packet);
1824
1825/** Destroy a BPF filter
1826 * @param filter        the filter opaque pointer
1827 * Deallocate all the resources associated with a BPF filter
1828 */
1829DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
1830/*@}*/
1831
1832/** @name Portability
1833 * This section has functions that causes annoyances to portability for one
1834 * reason or another.
1835 * @{
1836 */
1837
1838/** Convert an ethernet address to a string
1839 * @param addr  Ethernet address in network byte order
1840 * @param buf   Buffer to store the ascii representation, or NULL
1841 * @return buf, or if buf is NULL then a statically allocated buffer.
1842 *
1843 * This function is similar to the GNU ether_ntoa_r function, with a few
1844 * minor differences.  if NULL is passed as buf, then the function will
1845 * use an internal static buffer, if NULL isn't passed then the function
1846 * will use that buffer instead.
1847 *
1848 * @note the type of addr isn't struct ether_addr as it is with ether_ntoa_r,
1849 * however it is bit compatible so that a cast will work.
1850 */
1851DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
1852
1853/** Convert a string to an ethernet address
1854 * @param buf   Ethernet address in hex format delimited with :'s.
1855 * @param addr  buffer to store the binary representation, or NULL
1856 * @return addr, or if addr is NULL, then a statically allocated buffer.
1857 *
1858 * This function is similar to the GNU ether_aton_r function, with a few
1859 * minor differences.  if NULL is passed as addr, then the function will
1860 * use an internal static buffer, if NULL isn't passed then the function will
1861 * use that buffer instead.
1862 *
1863 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
1864 * however it is bit compatible so that a cast will work.
1865 */
1866DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
1867
1868/*@}*/
1869
1870
1871/** Which port is the server port */
1872typedef enum {
1873        USE_DEST,       /**< Destination port is the server port */
1874        USE_SOURCE      /**< Source port is the server port */
1875} serverport_t;
1876
1877/** Get the source port
1878 * @param packet        the packet to read from
1879 * @return a port in \em HOST byte order, or equivalent to ports for this
1880 * protocol, or 0 if this protocol has no ports.
1881 */
1882DLLEXPORT SIMPLE_FUNCTION
1883uint16_t trace_get_source_port(const libtrace_packet_t *packet);
1884
1885/** Get the destination port
1886 * @param packet        the packet to read from
1887 * @return a port in \em HOST byte order, or equivilent to ports for this
1888 * protocol, or 0 if this protocol has no ports.
1889 */
1890DLLEXPORT SIMPLE_FUNCTION
1891uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
1892
1893/** hint at the server port in specified protocol
1894 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
1895 * @param source        the source port from the packet
1896 * @param dest          the destination port from the packet
1897 * @return one of USE_SOURCE or USE_DEST depending on which one you should use
1898 * @note ports must be in \em HOST byte order!
1899 */
1900DLLEXPORT SIMPLE_FUNCTION
1901int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
1902
1903/** Takes a uri and splits it into a format and uridata component.
1904 * @param uri           the uri to be parsed
1905 * @param format        destination location for the format component of the uri
1906 * @return 0 if an error occured, otherwise return the uridata component
1907 */
1908DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
1909
1910/** Gets the format type for a given packet.
1911 * @param packet        the packet opaque pointer
1912 * @return the format of the packet
1913 */
1914DLLEXPORT
1915enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1916
1917/** Construct a packet from a buffer.
1918 * @param packet[in,out]        Libtrace Packet object to update with the new
1919 *                              data.
1920 * @param linktype              The linktype of the packet.
1921 * @param[in] data              The packet data (including linklayer)
1922 * @param len                   Length of packet data
1923 */
1924DLLEXPORT
1925void trace_construct_packet(libtrace_packet_t *packet,
1926                libtrace_linktype_t linktype, const void *data, uint16_t len);
1927
1928/*@}*/
1929
1930/** @name Wireless trace support
1931 * Functions to access wireless information from packets that have wireless
1932 * monitoring headers such as Radiotap or Prism.
1933 *
1934 * The trace_get_wireless_* functions provide an abstract interface for
1935 * retrieving information from wireless traces. They take a pointer to the
1936 * wireless monitoring header (usually found with trace_get_link(packet)) and
1937 * the linktype of the header passed in.
1938 *
1939 * All of the trace_get_wireless_* functions return false if the requested
1940 * information was unavailable, or true if it was. The actual data is stored
1941 * in an output variable supplied by the caller. Values returned into the
1942 * output variable will always be returned in host byte order.
1943 * @{
1944 */
1945
1946
1947#ifndef ARPHRD_80211_RADIOTAP
1948/* libc doesn't define this yet, but it seems to be what everyone is using
1949 */
1950#define ARPHRD_80211_RADIOTAP 803
1951#endif
1952
1953/** Get the wireless Timer Syncronisation Function
1954 *
1955 * Gets the value of the timer syncronisation function for this frame, which
1956 * is a value in microseconds indicating the time that the first bit of the
1957 * MPDU was received by the MAC.
1958 *
1959 * @param link the wireless header
1960 * @param linktype the linktype of the wireless header passed in
1961 * @param[out] tsft the value of the timer syncronisation function.
1962 * @return true if the field was available, false if not.
1963 */
1964DLLEXPORT bool trace_get_wireless_tsft(void *linkptr,
1965        libtrace_linktype_t linktype, uint64_t *tsft);
1966
1967/** Get the wireless rate
1968 * @param link the wireless header
1969 * @param linktype the linktype of the wireless header passed in
1970 * @param[out] rate the data-rate of the frame in units of 500kbps
1971 * @return true if the field was available, false if not.
1972 */
1973DLLEXPORT bool trace_get_wireless_rate(void *linkptr,
1974        libtrace_linktype_t linktype, uint8_t *rate);
1975
1976/** Get the wireless channel frequency
1977 * @param link the wireless header
1978 * @param linktype the linktype of the wireless header passed in
1979 * @param[out] freq the frequency in MHz of the channel the frame was transmitted
1980 * or received on.
1981 * @return true if the field was available, false if not.
1982 */
1983DLLEXPORT bool trace_get_wireless_freq(void *linkptr,
1984        libtrace_linktype_t linktype, uint16_t *freq);
1985
1986/** Get the wireless signal strength in dBm
1987 * @param link the wireless header
1988 * @param linktype the linktype of the wireless header passed in
1989 * @param[out] strength the RF signal power at the antenna, in dB difference
1990 * from 1mW.
1991 * @return true if the field was available, false if not.
1992 */
1993DLLEXPORT bool trace_get_wireless_signal_strength_dbm(void *linkptr,
1994        libtrace_linktype_t linktype, int8_t *strength);
1995
1996/** Get the wireless noise strength in dBm
1997 * @param link the wireless header
1998 * @param linktype the linktype of the wireless header passed in
1999 * @param[out] strength the RF noise power at the antenna, in dB difference
2000 * from 1mW.
2001 * @return true if the field was available, false if not.
2002 */
2003DLLEXPORT bool trace_get_wireless_noise_strength_dbm(void *linkptr,
2004        libtrace_linktype_t linktype, int8_t *strength);
2005
2006/** Get the wireless signal strength in dB
2007 * @param link the wireless header
2008 * @param linktype the linktype of the wireless header passed in
2009 * @param[out] strength the RF signal power at the antenna,in dB difference
2010 * from a fixed reference.
2011 * @return true if the field was available, false if not.
2012 */
2013DLLEXPORT bool trace_get_wireless_signal_strength_db(void *linkptr,
2014        libtrace_linktype_t linktype, uint8_t *strength);
2015
2016/** Get the wireless noise strength in dB
2017 * @param link the wireless header
2018 * @param linktype the linktype of the wireless header passed in
2019 * @param[out] strength the RF noise power at the antenna, in dB difference
2020 * from a fixed reference.
2021 * @return true if the field was available, false if not.
2022 */
2023DLLEXPORT bool trace_get_wireless_noise_strength_db(void *linkptr,
2024        libtrace_linktype_t linktype, uint8_t *strength);
2025
2026/** Get the wireless transmit attenuation
2027 * @param link the wireless header
2028 * @param linktype the linktype of the wireless header passed in
2029 * @param[out] attenuation the transmit power as a unitless distance from maximum
2030 * power set at factory calibration. 0 indicates maximum transmission power.
2031 * @return true if the field was available, false if not.
2032 */
2033DLLEXPORT bool trace_get_wireless_tx_attenuation(void *linkptr,
2034        libtrace_linktype_t linktype, uint16_t *attenuation);
2035
2036/** Get the wireless transmit attenuation in dB
2037 * @param link the wireless header
2038 * @param linktype the linktype of the wireless header passed in
2039 * @param[out] attenuation the transmit power as dB difference from maximum power
2040 * set at factory calibration. 0 indicates maximum power.
2041 * @return true if the field was available, false if not.
2042 */
2043DLLEXPORT bool trace_get_wireless_tx_attenuation_db(void *linkptr,
2044        libtrace_linktype_t linktype, uint16_t *attenuation);
2045
2046/** Get the wireless transmit power in dBm @param link the wireless header
2047 * @param linktype the linktype of the wireless header passed in
2048 * @param[out] txpower the transmit power as dB from a 1mW reference. This is the absolute power level measured at the antenna port. 
2049 * @return true if the field was available, false if not.
2050 */
2051DLLEXPORT bool trace_get_wireless_tx_power_dbm(void *linkptr, libtrace_linktype_t
2052                linktype, int8_t *txpower);
2053
2054/** Get the wireless antenna
2055 * @param link the wireless header
2056 * @param linktype the linktype of the wireless header passed in
2057 * @param[out] antenna which antenna was used to transmit or receive the frame.
2058 * @return true if the field was available, false if not.
2059 */
2060DLLEXPORT bool trace_get_wireless_antenna(void *linkptr,
2061        libtrace_linktype_t linktype, uint8_t *antenna);
2062
2063/*@}*/
2064
2065#ifdef __cplusplus
2066} /* extern "C" */
2067#endif /* #ifdef __cplusplus */
2068#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.