source: lib/libtrace.h.in @ 19b7d05

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