source: lib/libtrace.h.in @ bf7e954

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

Split compression type out from fileflags

  • Property mode set to 100644
File size: 93.9 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 compression types
885 * Note, this must be kept in sync with WANDIO_COMPRESS_* numbers in wandio.h
886 */
887typedef enum {
888        TRACE_OPTION_COMPRESSTYPE_NONE = 0, /**< No compression */
889        TRACE_OPTION_COMPRESSTYPE_ZLIB = 1, /**< GZip Compression */
890        TRACE_OPTION_COMPRESSTYPE_BZ2  = 2, /**< BZip2 Compression */
891        TRACE_OPTION_COMPRESSTYPE_LZO  = 3, /**< LZO Compression */
892} trace_option_compresstype_t;
893
894/** Valid configuration options for output traces */
895typedef enum {
896        /** File flags to use when opening an output file, e.g. O_APPEND */
897        TRACE_OPTION_OUTPUT_FILEFLAGS,
898        /** Compression level: 0 = no compression, 1 = faster compression,
899         * 9 = better compression */
900        TRACE_OPTION_OUTPUT_COMPRESS,
901        /** Compression type, see trace_option_compresstype_t */
902        TRACE_OPTION_OUTPUT_COMPRESSTYPE,
903} trace_option_output_t;
904
905/** Sets an output config option
906 *
907 * @param libtrace      The output trace object to apply the option to
908 * @param option        The option to set
909 * @param value         The value to set the option to
910 * @return -1 if option configuration failed, 0 otherwise
911 * This should be called after trace_create_output, and before
912 * trace_start_output
913 */
914DLLEXPORT int trace_config_output(libtrace_out_t *libtrace,
915                trace_option_output_t option,
916                void *value
917                );
918
919/** Close an input trace, freeing up any resources it may have been using
920 *
921 * @param trace         The input trace to be destroyed
922 *
923 */
924DLLEXPORT void trace_destroy(libtrace_t *trace);
925
926/** Close a dummy trace file, freeing up any resources it may have been using
927 * @param trace         The dummy trace to be destroyed
928 */
929DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
930
931/** Close an output trace, freeing up any resources it may have been using
932 * @param trace         The output trace to be destroyed
933 */
934DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
935
936/** Check (and clear) the current error state of an input trace
937 * @param trace         The input trace to check the error state on
938 * @return The current error status and message
939 *
940 * This reads and returns the current error state and sets the current error
941 * to "no error".
942 */
943DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
944
945/** Indicate if there has been an error on an input trace
946 * @param trace         The input trace to check the error state on
947 * @return true if an error has occurred, false otherwise
948 *
949 * @note This does not clear the error status, and only returns true or false.
950 */
951DLLEXPORT bool trace_is_err(libtrace_t *trace);
952
953/** Outputs the error message for an input trace to stderr and clear the error
954 * status.
955 * @param trace         The input trace with the error to output
956 * @param msg           The message to prepend to the error
957 *
958 * This function does clear the error status.
959 */
960DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...) PRINTF(2,3);
961
962/** Checks (and clears) the current error state for an output trace
963 * @param trace         The output trace to check the error state on
964 * @return The current error status and message
965 *
966 * This reads and returns the current error state and sets the current error
967 * to "no error".
968 */
969DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
970
971/** Indicates if there is an error on an output trace
972 * @param trace         The output trace to check the error state on
973 * @return true if an error has occurred, false otherwise.
974 *
975 * This does not clear the error status, and only returns true or false.
976 */
977DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
978
979/** Outputs the error message for an output trace to stderr and clear the error
980 * status.
981 * @param trace         The output trace with the error to output
982 * @param msg           The message to prepend to the error
983 * This function does clear the error status.
984 */
985DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...)
986        PRINTF(2,3);
987
988/** Returns the number of packets observed on an input trace.
989 * Includes the number of packets counted as early as possible, before
990 * filtering, and includes dropped packets.
991 *
992 * @param trace         The input trace to examine
993 * @returns The number of packets seen at the capture point before filtering.
994 *
995 * If the number is not known, this function will return UINT64_MAX
996 */
997DLLEXPORT
998uint64_t trace_get_received_packets(libtrace_t *trace);
999
1000/** Returns the number of packets that were captured, but discarded for not
1001 * matching a provided filter.
1002 *
1003 * @param trace         The input trace to examine
1004 * @returns The number of packets that were successfully captured, but filtered
1005 *
1006 * If the number is not known, this function will return UINT64_MAX
1007 */
1008DLLEXPORT
1009uint64_t trace_get_filtered_packets(libtrace_t *trace);
1010
1011/** Returns the number of packets that have been dropped on an input trace due
1012 * to lack of buffer space on the capturing device.
1013 *
1014 * @param trace         The input trace to examine
1015 * @returns The number of packets captured, but dropped due to buffer overruns
1016 *
1017 * If the number is not known, this function will return UINT64_MAX
1018 */
1019DLLEXPORT
1020uint64_t trace_get_dropped_packets(libtrace_t *trace);
1021
1022/** Returns the number of packets that have been read from the input trace using
1023 * trace_read_packet().
1024 *
1025 * @param trace         The input trace to examine
1026 * @returns The number of packets that have been read by the libtrace user.
1027 *
1028 * If the number is not known, this function will return UINT64_MAX
1029 */
1030DLLEXPORT
1031uint64_t trace_get_accepted_packets(libtrace_t *trace);
1032
1033
1034/*@}*/
1035
1036/** @name Reading / Writing packets
1037 * These members deal with creating, reading and writing packets
1038 *
1039 * @{
1040 */
1041
1042/** Create a new packet object
1043 *
1044 * @return A pointer to an initialised libtrace_packet_t object
1045 */
1046DLLEXPORT libtrace_packet_t *trace_create_packet(void);
1047
1048/** Copy a packet object
1049 * @param packet        The source packet to copy
1050 * @return A new packet which has the same content as the source packet
1051 *
1052 * @note This always involves a copy, which can be slow.  Use of this
1053 * function should be avoided where possible.
1054 *
1055 * @par The reason you would want to use this function is that a zerocopied
1056 * packet from a device will be stored using memory owned by the device which
1057 * may be a limited resource. Copying the packet will ensure that the packet
1058 * is now stored in memory owned and managed by libtrace.
1059 */
1060DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
1061
1062/** Destroy a packet object
1063 * @param packet        The packet to be destroyed
1064 *
1065 */
1066DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
1067
1068
1069/** Read the next packet from an input trace
1070 *
1071 * @param trace         The libtrace opaque pointer for the input trace
1072 * @param packet        The packet opaque pointer
1073 * @return 0 on EOF, negative value on error, number of bytes read when successful.
1074 *
1075 * @note The number of bytes read is usually (but not always) the same as
1076 * trace_get_framing_length()+trace_get_capture_length() depending on the
1077 * trace format.
1078 *
1079 * @note The trace must have been started with trace_start before calling
1080 * this function
1081 *
1082 * @note When reading from a live capture, this function will block until a
1083 * packet is observed on the capture interface. The libtrace event API
1084 * (e.g. trace_event()) should be used if non-blocking operation is required.
1085 */
1086DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
1087
1088/** Event types
1089 * see \ref libtrace_eventobj_t and \ref trace_event
1090 */
1091typedef enum {
1092        TRACE_EVENT_IOWAIT,     /**< Wait on the given file descriptor */
1093        TRACE_EVENT_SLEEP,      /**< Sleep for the given amount of time */
1094        TRACE_EVENT_PACKET,     /**< Packet has been read from input trace */
1095        TRACE_EVENT_TERMINATE   /**< End of input trace */
1096} libtrace_event_t;
1097
1098/** Structure returned by libtrace_event explaining what the current event is */
1099typedef struct libtrace_eventobj_t {
1100        libtrace_event_t type; /**< Event type (iowait,sleep,packet) */
1101       
1102        /** If the event is IOWAIT, the file descriptor to wait on */
1103        int fd;               
1104        /** If the event is SLEEP, the amount of time to sleep for in seconds */
1105        double seconds;       
1106        /** If the event is PACKET, the value returned by trace_read_packet() */
1107        int size;
1108} libtrace_eventobj_t;
1109
1110/** Processes the next libtrace event from an input trace.
1111 * @param trace The libtrace opaque pointer for the input trace
1112 * @param packet The libtrace_packet opaque pointer to use for reading packets
1113 * @return A libtrace_event struct containing the event type and details of the event.
1114 *
1115 * Type can be:
1116 *  TRACE_EVENT_IOWAIT  Waiting on I/O on a file descriptor
1117 *  TRACE_EVENT_SLEEP   Wait a specified amount of time for the next event
1118 *  TRACE_EVENT_PACKET  Packet was read from the trace
1119 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
1120 */
1121DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
1122                libtrace_packet_t *packet);
1123
1124
1125/** Write one packet out to the output trace
1126 *
1127 * @param trace         The libtrace_out opaque pointer for the output trace
1128 * @param packet        The packet opaque pointer of the packet to be written
1129 * @return The number of bytes written out, if zero or negative then an error has occured.
1130 */
1131DLLEXPORT int trace_write_packet(libtrace_out_t *trace, libtrace_packet_t *packet);
1132
1133/** Gets the capture format for a given packet.
1134 * @param packet        The packet to get the capture format for.
1135 * @return The capture format of the packet
1136 *
1137 * @note Due to ability to convert packets between formats relatively easily
1138 * in Libtrace, the format of the packet right now may not be the format that
1139 * the packet was originally captured with.
1140 */
1141DLLEXPORT
1142enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1143
1144/** Construct a libtrace packet from a buffer containing the packet payload.
1145 * @param[in,out] packet        Libtrace Packet object to update with the new
1146 *                              data.
1147 * @param linktype              The linktype of the packet data.
1148 * @param[in] data              The packet data (including linklayer).
1149 * @param len                   Length of packet data provided in the buffer.
1150 *
1151 * @note The constructed packet will be in the PCAP format.
1152 *
1153 * @note To be useful, the provided buffer must start with the layer 2 header
1154 * (or a metadata header, if desired).
1155 */
1156DLLEXPORT
1157void trace_construct_packet(libtrace_packet_t *packet,
1158                libtrace_linktype_t linktype, const void *data, uint16_t len);
1159
1160/*@}*/
1161
1162/** @name Protocol decodes
1163 * These functions locate and return a pointer to various headers inside a
1164 * packet
1165 *
1166 * A packet is divided up into several "layers.":
1167 *
1168 * @li Framing header -- This is the header provided by the capture format
1169 * itself rather than anything that was sent over the network. This provides
1170 * basic details about the packet record including capture lengths, wire
1171 * lengths, timestamps, direction information and any other metadata that is
1172 * part of the capture format. 
1173 *
1174 * @li Metadata header (optional) -- A header containing metadata about a
1175 * packet that was captured, but the metadata was not transmitted over the
1176 * wire.  Some examples include RadioTap and Linux_sll headers.  This can be
1177 * retrieved by trace_get_packet_meta(), or skipped using
1178 * trace_get_payload_from_meta(). There may be multiple "metadata" headers on
1179 * a packet.
1180 *
1181 * @li Layer 2/Link layer/Datalink header -- This can be retrieved by
1182 * trace_get_layer2(), or skipped using trace_get_payload_from_layer2().
1183 *
1184 * @li Layer 3/IP/IPv6 -- This can be retrieved by trace_get_layer3().  As a
1185 * convenience trace_get_ip()/trace_get_ip6() can be used to find an IPv4/IPv6
1186 * header.
1187 *
1188 * @li Layer 5/transport -- These are protocols carried in IPv4/IPv6 frames.
1189 * These can be retrieved using trace_get_transport().
1190 *
1191 * @{
1192 */
1193
1194
1195/** Gets a pointer to the first byte of the packet as it was captured and
1196 * returns its corresponding linktype and capture length.
1197 *
1198 * Use this function instead of the deprecated trace_get_link().
1199 *
1200 * @param packet The packet to get the buffer from
1201 * @param[out] linktype The linktype of the returned pointer
1202 * @param[out] remaining The capture length (the number of captured bytes from
1203 * the returned pointer)
1204 * @return A pointer to the first byte of the packet
1205 */
1206DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet,
1207                libtrace_linktype_t *linktype, uint32_t *remaining);
1208
1209/** Get a pointer to the link layer for a given packet
1210 * @param packet        The packet to get the link layer for
1211 *
1212 * @return A pointer to the link layer, or NULL if there is no link layer
1213 *
1214 * @deprecated This function is deprecated: Use trace_get_packet_buffer() or
1215 * one of the trace_get_layer*() functions instead.
1216 * @note You should call trace_get_link_type to find out what type of link
1217 * layer has been returned to you.
1218 */
1219DLLEXPORT SIMPLE_FUNCTION DEPRECATED
1220void *trace_get_link(const libtrace_packet_t *packet);
1221
1222/** Get a pointer to the IPv4 header (if any) for a given packet
1223 * @param packet        The packet to get the IPv4 header for
1224 *
1225 * @return A pointer to the IPv4 header, or NULL if there is no IPv4 header
1226 *
1227 * If a partial IP header is present, i.e. the packet has been truncated before
1228 * the end of the IP header, this function will return NULL.
1229 *
1230 * You should consider using \ref trace_get_layer3 instead of this function.
1231 */
1232DLLEXPORT SIMPLE_FUNCTION
1233libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
1234
1235/** get a pointer to the IPv6 header (if any)
1236 * @param packet        The packet to get the IPv6 header for
1237 *
1238 * @return A pointer to the IPv6 header, or NULL if there is no IPv6 header
1239 *
1240 * If a partial IPv6 header is present, i.e. the packet has been truncated
1241 * before the end of the IP header, this function will return NULL.
1242 *
1243 * You should consider using \ref trace_get_layer3 instead of this function.
1244 */
1245DLLEXPORT SIMPLE_FUNCTION
1246libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet);
1247
1248/** Return a pointer to the first metadata header in a packet, if present.
1249 *
1250 * This function takes a pointer to a libtrace packet and if any metadata
1251 * headers exist, returns a pointer to the first one, along with its
1252 * corresponding linktype.
1253 *
1254 * If no metadata headers exist in the packet, NULL is returned.
1255 *
1256 * A metadata header is a header that was prepended by the capturing device,
1257 * such as a Linux SLL header, or a Radiotap wireless monitoring header.
1258 * Subsequent metadata headers may be accessed with the
1259 * trace_get_payload_from_meta(...) function.
1260 *
1261 * @param packet The libtrace packet
1262 * @param[out] linktype The linktype of the returned metadata header
1263 * @param[out] remaining The number of bytes captured after the returned
1264 * pointer.
1265 * @return A pointer to the first metadata header, or NULL if there are no
1266 * metadata headers present.
1267 *
1268 * remaining may be NULL, however linktype must be provided.
1269 */
1270DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet,
1271                libtrace_linktype_t *linktype,
1272                uint32_t *remaining);
1273
1274/** Returns the payload of a metadata header.
1275 *
1276 * This function takes a pointer to the start of a metadata header (either
1277 * obtained via trace_get_packet_meta(...) or by a previous call to
1278 * trace_get_payload_from_meta(...)) along with its corresponding linktype and
1279 * returns the payload, i.e. the next header. It will also update the linktype
1280 * parameter to indicate the type of payload.
1281 * 
1282 * If the linktype indicates that the header passed in is not a metadata
1283 * header, the function returns NULL to indicate this. The linktype remains
1284 * unchanged in this case.
1285 *
1286 * This function allows the user to iterate through metadata headers which are
1287 * sometimes present before the actual packet as it was received on the wire.
1288 * Examples of metadata headers include the Linux SLL header and the Radiotap
1289 * wireless monitoring header.
1290 *
1291 * If the metadata header passed into this function is truncated, this
1292 * function will return NULL, and remaining will be set to 0.
1293 *
1294 * If there are 0 bytes of payload following the provided metadata header, the
1295 * function will return a pointer to where the header would otherwise be and
1296 * remaining will be 0.
1297 *
1298 * Therefore, be sure to check the value of remaining after calling this
1299 * function!
1300 *
1301 * @param[in] meta A pointer to a metadata header
1302 * @param[in,out] linktype The linktype of meta (updated to indicate the
1303 * linktype of the returned header if applicable).
1304 * @param[in,out] remaining The number of bytes after the meta pointer.
1305 * @return A pointer to the payload of the metadata header. If meta is not a
1306 * pointer to a metadata header, NULL is returned and linktype remains
1307 * unchanged.
1308 *
1309 * All parameters are mandatory.
1310 */
1311DLLEXPORT void *trace_get_payload_from_meta(const void *meta,
1312                libtrace_linktype_t *linktype,
1313                uint32_t *remaining);
1314
1315
1316/** Get a pointer to the layer 2 header. Generally this is the first byte of the
1317 * packet as it was seen on the wire.
1318 *
1319 * This function takes a libtrace packet and skips over any metadata headers if
1320 * present (such as Linux SLL or Radiotap) and returns a pointer to the first
1321 * byte of the packet that was actually received by the network interface.
1322 *
1323 * @param packet The libtrace packet to find the layer 2 header for
1324 * @param[out] linktype The linktype of the returned layer 2 header
1325 * @param[out] remaining The number of bytes left in the packet after the
1326 * returned pointer.
1327 * @return A pointer to the first byte of the packet as it was seen on the
1328 * wire. If no layer 2 header is present, this function will return NULL.
1329 *
1330 * remaining may be NULL, otherwise it will be filled in by the function.
1331 */
1332DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet,
1333                libtrace_linktype_t *linktype,
1334                uint32_t *remaining);
1335
1336/** Gets a pointer to the next header following a layer 2 header
1337 *
1338 * @param l2                    The pointer to the current layer 2 header
1339 * @param linktype              The type of the layer 2 header
1340 * @param[out] ethertype        An optional output variable of the ethernet type of the new header
1341 * @param[in,out] remaining     Updated with the number of captured bytes
1342                                remaining
1343 *
1344 * @return A pointer to the header following the provided layer 2 header, or
1345 * NULL if no subsequent header is present.
1346 *
1347 * Remaining must point to the number of bytes captured from the layer 2 header
1348 * and beyond.  It will be decremented by the number of bytes skipped to find
1349 * the payload.
1350 *
1351 * If the layer 2 header is complete but there are zero bytes of payload after
1352 * the end of the header, a pointer to where the payload would be is returned
1353 * and remaining will be set to 0.  If the layer 2 header is incomplete
1354 * (truncated), then NULL is returned and remaining will be set to 0.
1355 * Therefore, it is very important to check the value of remaining after
1356 * calling this function.
1357 *
1358 */
1359DLLEXPORT void *trace_get_payload_from_layer2(void *l2,
1360                libtrace_linktype_t linktype,
1361                uint16_t *ethertype,
1362                uint32_t *remaining);
1363
1364
1365/** Get a pointer to the layer 3 (e.g. IP) header.
1366 * @param packet  The libtrace packet to find the layer 3 header for
1367 * @param[out] ethertype The ethertype of the layer 3 header
1368 * @param[out] remaining The amount of captured data remaining in the packet starting from the returned pointer, i.e. including the layer 3 header.
1369 *
1370 * @return A pointer to the layer 3 header. If no layer 3 header is present in
1371 * the packet, NULL is returned. If the layer 3 header is truncated, a valid
1372 * pointer will still be returned so be sure to check the value of remaining
1373 * before attempting to process the returned header.
1374 *
1375 * remaining may be NULL, otherwise it will be set to the number of captured
1376 * bytes after the pointer returned.
1377 */
1378DLLEXPORT
1379void *trace_get_layer3(const libtrace_packet_t *packet,
1380                uint16_t *ethertype, uint32_t *remaining);
1381
1382/** Gets a pointer to the transport layer header (if any)
1383 * @param packet   The libtrace packet to find the transport header for
1384 * @param[out] proto    The protocol present at the transport layer.
1385 * @param[out] remaining The amount of captured data remaining in the packet
1386 * starting from the returned pointer, i.e. including the transport header.
1387 *
1388 * @return A pointer to the transport layer header. If no transport header is
1389 * present in the packet, NULL is returned. If the transport header is
1390 * truncated, a valid pointer will still be returned so be sure to check the
1391 * value of remaining before attempting to process the returned header.
1392 *
1393 * remaining may be NULL, otherwise it will be set to the number of captured
1394 * bytes after the returned pointer.
1395 *
1396 * @note proto may be NULL if proto is unneeded.
1397 */
1398DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet,
1399                uint8_t *proto, uint32_t *remaining);
1400
1401/** Gets a pointer to the payload following an IPv4 header
1402 * @param ip            The IPv4 Header
1403 * @param[out] proto    The protocol of the header following the IPv4 header
1404 * @param[in,out] remaining Updated with the number of captured bytes remaining
1405 *
1406 * @return A pointer to the transport layer header, or NULL if no subsequent
1407 * header is present.
1408 *
1409 * When calling this function, remaining must contain the number of captured
1410 * bytes remaining in the packet starting from the IPv4 header (including the
1411 * IPv4 header itself).
1412 *
1413 * remaining will be decremented by the size of the IPv4 header (including any
1414 * options). If the IPv4 header is complete but there are zero bytes of payload
1415 * after the IPv4 header, a pointer to where the payload would be is returned
1416 * and remaining will be set to 0.  If the IPv4 header is incomplete, NULL will
1417 * be returned and remaining will be set to 0. Therefore, it is very important
1418 * to check the value of remaining after calling this function.
1419 *
1420 * proto may be NULL, in which case it won't be updated.
1421 *
1422 * @note This is similar to trace_get_transport_from_ip in libtrace2
1423 */
1424DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
1425                uint32_t *remaining);
1426
1427/** Gets a pointer to the payload following an IPv6 header
1428 * @param ipptr         The IPv6 Header
1429 * @param[out] proto    The protocol of the header following the IPv6 header
1430 * @param[in,out] remaining Updated with the number of captured bytes remaining
1431 *
1432 * @return A pointer to the transport layer header, or NULL if no subsequent
1433 * header is present.
1434 *
1435 * When calling this function, remaining must contain the number of captured
1436 * bytes remaining in the packet starting from the IPv6 header (including the
1437 * IPv6 header itself).
1438 *
1439 * remaining will be decremented by the size of the IPv6 header (including any
1440 * options). If the IPv6 header is complete but there are zero bytes of payload
1441 * after the IPv6 header, a pointer to where the payload would be is returned
1442 * and remaining will be set to 0.  If the IPv6 header is incomplete, NULL will
1443 * be returned and remaining will be set to 0. Therefore, it is very important
1444 * to check the value of remaining after calling this function.
1445 *
1446 * proto may be NULL, in which case it won't be updated.
1447 *
1448 */
1449DLLEXPORT void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr,
1450                uint8_t *proto, uint32_t *remaining);
1451
1452/** Gets a pointer to the payload following a link header
1453 * @param linkptr       A pointer to the link layer header
1454 * @param linktype      The linktype of the link header being examined
1455 * @param[out] type     An output variable for the ethernet type
1456 * @param[in,out] remaining Updated with the number of captured bytes remaining
1457 *
1458 * @return A pointer to the header following the link header, or NULL if no
1459 * subsequent header is present.
1460 *
1461 * When calling this function, remaining must contain the number of captured
1462 * bytes remaining in the packet starting from the link header (including the
1463 * link header itself). remaining will be updated to contain the number of
1464 * bytes remaining after the link header has been skipped.
1465 *
1466 * @deprecated This function is deprecated: you should be using
1467 * trace_get_payload_from_layer2() or trace_get_payload_from_meta() instead.
1468 *
1469 */
1470DLLEXPORT void *trace_get_payload_from_link(void *linkptr,
1471                libtrace_linktype_t linktype,
1472                uint16_t *type, uint32_t *remaining);
1473
1474/** Gets a pointer to the payload following an 802.1q (VLAN) header.
1475 * @param vlan      A pointer to the VLAN header
1476 * @param[out] type  The ethernet type, replaced with the VLAN ether type
1477 * @param[in,out] remaining Updated with the number of captured bytes remaining
1478 *
1479 * @return A pointer to the header beyond the VLAN header, if one is present.
1480 * Otherwise, returns NULL. 
1481 *
1482 * When calling this function, remaining must contain the number of captured
1483 * bytes remaining in the packet starting from the VLAN header (including the
1484 * VLAN header itself). remaining will be updated to contain the number of
1485 * bytes remaining after the VLAN header has been skipped.
1486 *
1487 * If the VLAN header is complete but there are zero bytes of payload after
1488 * the VLAN header, a pointer to where the payload would be is returned and
1489 * remaining will be set to 0.  If the VLAN header is incomplete, NULL will be
1490 * returned and remaining will be set to 0. Therefore, it is important to check
1491 * the value of remaining after calling this function.
1492 *
1493 * type will be set to the ethertype of the VLAN payload. This parameter is not
1494 * mandatory, but is highly recommended.
1495 *
1496 */
1497DLLEXPORT void *trace_get_payload_from_vlan(
1498                void *vlan, uint16_t *type, uint32_t *remaining);
1499
1500/** Gets a pointer to the payload following an MPLS header.
1501 * @param mpls      A pointer to the MPLS header
1502 * @param[out] type  The ethernet type, replaced by the ether type of the
1503 * returned header - 0x0000 if an Ethernet header is returned
1504 * @param[in,out] remaining Updated with the number of captured bytes remaining
1505 *
1506 * @return A pointer to the header beyond the MPLS label, if one is present.
1507 * Will return NULL if there is not enough bytes remaining to skip past the
1508 * MPLS label.
1509 *
1510 * When calling this function, remaining must contain the number of captured
1511 * bytes remaining in the packet starting from the MPLS header (including the
1512 * MPLS header itself). remaining will be updated to contain the number of
1513 * bytes remaining after the MPLS header has been skipped.
1514 *
1515 * If the MPLS header is complete but there are zero bytes of payload after
1516 * the MPLS header, a pointer to where the payload would be is returned and
1517 * remaining will be set to 0.  If the MPLS header is incomplete, NULL will be
1518 * returned and remaining will be set to 0. Therefore, it is important to check
1519 * the value of remaining after calling this function.
1520 *
1521 * type will be set to the ethertype of the MPLS payload. This parameter is
1522 * mandatory - it may not be NULL.
1523 *
1524 * @note This function will only remove one MPLS label at a time - the type
1525 * will be set to 0x8847 if there is another MPLS label following the one
1526 * skipped by this function.
1527 *
1528 */
1529DLLEXPORT void *trace_get_payload_from_mpls(
1530                void *mpls, uint16_t *type, uint32_t *remaining);
1531
1532/** Gets a pointer to the payload following a PPPoE header
1533 * @param pppoe      A pointer to the PPPoE header
1534 * @param[out] type  The ethernet type, replaced by the ether type of the
1535 * returned header - 0x0000 if an Ethernet header is returned
1536 * @param[in,out] remaining  Updated with the number of captured bytes remaining
1537 *
1538 * @return A pointer to the header beyond the PPPoE header. NOTE that this
1539 * function will also skip over the PPP header that will immediately follow
1540 * the PPPoE header. This function will return NULL if there are not enough
1541 * bytes remaining to skip past both the PPPoE and PPP headers.
1542 *
1543 * When calling this function, remaining must contain the number of captured
1544 * bytes remaining in the packet starting from the PPPoE header (including the
1545 * PPPoE header itself). remaining will be updated to contain the number of
1546 * bytes remaining after the PPPoE and PPP headers have been removed.
1547 *
1548 * If the PPPoE and PPP headers are complete but there are zero bytes of
1549 * payload after the PPP header, a pointer to where the payload would be is
1550 * returned and remaining will be set to 0.  If the PPPoE or PPP header is
1551 * incomplete, NULL will be returned and remaining will be set to 0. Therefore,
1552 * it is important to check the value of remaining after calling this function.
1553 *
1554 * type will be set to the ether type of the PPP payload. This parameter is
1555 * mandatory - it may not be NULL.
1556 *
1557 */
1558DLLEXPORT void *trace_get_payload_from_pppoe(
1559                void *pppoe, uint16_t *type, uint32_t *remaining);
1560
1561/** Gets a pointer to the payload following a TCP header
1562 * @param tcp           A pointer to the TCP header
1563 * @param[in,out] remaining Updated with the number of captured bytes remaining
1564 *
1565 * @return A pointer to the TCP payload, or NULL if the TCP header is truncated.
1566 *
1567 * When calling this function, remaining must contain the number of captured
1568 * bytes remaining in the packet starting from the TCP header (including the
1569 * TCP header itself). remaining will be updated to contain the number of
1570 * bytes remaining after the TCP header has been skipped.
1571 *
1572 * If the TCP header is complete but there are zero bytes of payload after
1573 * the TCP header, a pointer to where the payload would be is returned and
1574 * remaining will be set to 0.  If the TCP header is incomplete, NULL will be
1575 * returned and remaining will be set to 0. Therefore, it is important to check
1576 * the value of remaining after calling this function.
1577 *
1578 */
1579DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp,
1580                uint32_t *remaining);
1581
1582/** Gets a pointer to the payload following a UDP header
1583 * @param udp           A pointer to the UDP header
1584 * @param[in,out] remaining Updated with the number of captured bytes remaining
1585 *
1586 * @return A pointer to the UDP payload, or NULL if the UDP header is truncated.
1587 *
1588 * When calling this function, remaining must contain the number of captured
1589 * bytes remaining in the packet starting from the UDP header (including the
1590 * UDP header itself). remaining will be updated to contain the number of
1591 * bytes remaining after the UDP header has been skipped.
1592 *
1593 * If the UDP header is complete but there are zero bytes of payload after
1594 * the UDP header, a pointer to where the payload would be is returned and
1595 * remaining will be set to 0.  If the UDP header is incomplete, NULL will be
1596 * returned and remaining will be set to 0. Therefore, it is important to check
1597 * the value of remaining after calling this function.
1598 *
1599 */
1600DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
1601
1602/** Gets a pointer to the payload following a ICMP header
1603 * @param icmp           A pointer to the ICMP header
1604 * @param[in,out] remaining Updated with the number of captured bytes remaining
1605 *
1606 * @return A pointer to the ICMP payload, or NULL if the ICMP header is
1607 * truncated.
1608 *
1609 * When calling this function, remaining must contain the number of captured
1610 * bytes remaining in the packet starting from the ICMP header (including the
1611 * ICMP header itself). remaining will be updated to contain the number of
1612 * bytes remaining after the ICMP header has been skipped.
1613 *
1614 * If the ICMP header is complete but there are zero bytes of payload after
1615 * the ICMP header, a pointer to where the payload would be is returned and
1616 * remaining will be set to 0.  If the ICMP header is incomplete, NULL will be
1617 * returned and remaining will be set to 0. Therefore, it is important to check
1618 * the value of remaining after calling this function.
1619 *
1620 * @note In the case of some ICMP messages, the payload may be the IP header
1621 * from the packet that triggered the ICMP message.
1622 *
1623 */
1624DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp,
1625                uint32_t *remaining);
1626
1627/** Get a pointer to the TCP header (if present)
1628 * @param packet        The packet to get the TCP header from
1629 *
1630 * @return A pointer to the TCP header, or NULL if there is not a complete TCP
1631 * header present in the packet.
1632 *
1633 * This is a short-cut function enabling quick and easy access to the TCP
1634 * header if that is all you care about. However, we recommend the use of the
1635 * more generic trace_get_transport() function instead.
1636 *
1637 * @note Unlike trace_get_transport(), this function will return NULL if the
1638 * TCP header is incomplete or truncated.
1639 */
1640DLLEXPORT SIMPLE_FUNCTION
1641libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
1642
1643/** Get a pointer to the TCP header following an IPv4 header (if present)
1644 * @param ip            The IP header to find the subsequent TCP header for
1645 * @param[in,out] remaining Updated with the number of captured bytes remaining
1646 *
1647 * @return A pointer to the TCP header, or NULL if no TCP header is present in
1648 * the packet.
1649 *
1650 * When calling this function, remaining must contain the number of captured
1651 * bytes remaining in the packet starting from the IP header (including the
1652 * IP header itself). remaining will be updated to contain the number of
1653 * bytes remaining after the IP header has been skipped.
1654 *
1655 * If the IP header is complete but there are zero bytes of payload after
1656 * the IP header, a pointer to where the payload would be is returned and
1657 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1658 * returned and remaining will be set to 0. Therefore, it is important to check
1659 * the value of remaining after calling this function.
1660 *
1661 * @note This function is rather redundant now that the layer 3 header is
1662 * cached. There should be no performance advantage for the user to call this
1663 * function over just calling trace_get_transport().
1664 *
1665 * @note The last parameter has changed from libtrace2
1666 */
1667DLLEXPORT SIMPLE_FUNCTION
1668libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
1669
1670/** Get a pointer to the UDP header (if present)
1671 * @param packet        The packet to get the UDP header from
1672 *
1673 * @return A pointer to the UDP header, or NULL if there is not a complete UDP
1674 * header present in the packet.
1675 *
1676 * This is a short-cut function enabling quick and easy access to the UDP
1677 * header if that is all you care about. However, we recommend the use of the
1678 * more generic trace_get_transport() function instead.
1679 *
1680 * @note Unlike trace_get_transport(), this function will return NULL if the
1681 * UDP header is incomplete or truncated.
1682 */
1683DLLEXPORT SIMPLE_FUNCTION
1684libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
1685
1686/** Get a pointer to the UDP header following an IPv4 header (if present)
1687 * @param ip            The IP header to find the subsequent UDP header for
1688 * @param[in,out] remaining Updated with the number of captured bytes remaining
1689 *
1690 * @return A pointer to the UDP header, or NULL if no UDP header is present in
1691 * the packet.
1692 *
1693 * When calling this function, remaining must contain the number of captured
1694 * bytes remaining in the packet starting from the IP header (including the
1695 * IP header itself). remaining will be updated to contain the number of
1696 * bytes remaining after the IP header has been skipped.
1697 *
1698 * If the IP header is complete but there are zero bytes of payload after
1699 * the IP header, a pointer to where the payload would be is returned and
1700 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1701 * returned and remaining will be set to 0. Therefore, it is important to check
1702 * the value of remaining after calling this function.
1703 *
1704 * @note This function is rather redundant now that the layer 3 header is
1705 * cached. There should be no performance advantage for the user to call this
1706 * function over just calling trace_get_transport().
1707 *
1708 * @note The last parameter has changed from libtrace2
1709 */
1710DLLEXPORT SIMPLE_FUNCTION
1711libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1712
1713/** Get a pointer to the ICMP header (if present)
1714 * @param packet        The packet to get the ICMP header from
1715 *
1716 * @return A pointer to the ICMP header, or NULL if there is not a complete
1717 * ICMP header present in the packet.
1718 *
1719 * This is a short-cut function enabling quick and easy access to the ICMP
1720 * header if that is all you care about. However, we recommend the use of the
1721 * more generic trace_get_transport() function instead.
1722 *
1723 * @note Unlike trace_get_transport(), this function will return NULL if the
1724 * ICMP header is incomplete or truncated.
1725 */
1726DLLEXPORT SIMPLE_FUNCTION
1727libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
1728
1729/** Get a pointer to the ICMP header following an IPv4 header (if present)
1730 * @param ip            The IP header to find the subsequent ICMP header for
1731 * @param[in,out] remaining Updated with the number of captured bytes remaining
1732 *
1733 * @return A pointer to the ICMP header, or NULL if no UDP header is present in
1734 * the packet.
1735 *
1736 * When calling this function, remaining must contain the number of captured
1737 * bytes remaining in the packet starting from the IP header (including the
1738 * IP header itself). remaining will be updated to contain the number of
1739 * bytes remaining after the IP header has been skipped.
1740 *
1741 * If the IP header is complete but there are zero bytes of payload after
1742 * the IP header, a pointer to where the payload would be is returned and
1743 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
1744 * returned and remaining will be set to 0. Therefore, it is important to check
1745 * the value of remaining after calling this function.
1746 *
1747 * @note This function is rather redundant now that the layer 3 header is
1748 * cached. There should be no performance advantage for the user to call this
1749 * function over just calling trace_get_transport().
1750 *
1751 * @note The last parameter has changed from libtrace2
1752 */
1753DLLEXPORT SIMPLE_FUNCTION
1754libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1755
1756/** Gets the destination MAC address for a given packet
1757 * @param packet        The packet to extract the destination MAC address from
1758 *
1759 * @return A pointer to the destination MAC address field in the layer 2
1760 * header, (or NULL if there is no destination 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_destination_mac(libtrace_packet_t *packet);
1768
1769/** Gets the source MAC address for a given packet
1770 * @param packet        The packet to extract the source MAC address from
1771 *
1772 * @return A pointer to the source MAC address field in the layer 2
1773 * header, (or NULL if there is no source MAC address or layer 2 header
1774 * available)
1775 *
1776 * @note This is a zero-copy function, so the memory that the returned pointer
1777 * points to is part of the packet itself.
1778 */
1779DLLEXPORT SIMPLE_FUNCTION
1780uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
1781
1782/** Get the source IP address for a given packet
1783 * @param packet        The packet to extract the source IP address from
1784 * @param addr          A pointer to a sockaddr structure to store the address
1785 *                      in. If NULL, static storage is used instead.
1786 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
1787 * platforms a sockaddr holding a MAC address. Returns NULL if no source IP
1788 * address was available.
1789 *
1790 * @note The best way to use this function is to pass in a pointer to the
1791 * struct sockaddr_storage for the addr parameter. This will avoid problems
1792 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
1793 * IPv4.
1794 */
1795DLLEXPORT SIMPLE_FUNCTION
1796struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
1797                struct sockaddr *addr);
1798
1799/** Get the destination IP address for a given packet
1800 * @param packet        The packet to extract the destination IP address from
1801 * @param addr          A pointer to a sockaddr structure to store the address
1802 *                      in. If NULL, static storage is used instead.
1803 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
1804 * platforms a sockaddr holding a MAC address. Returns NULL if no destination
1805 * IP address was available.
1806 *
1807 * @note The best way to use this function is to pass in a pointer to the
1808 * struct sockaddr_storage for the addr parameter. This will avoid problems
1809 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
1810 * IPv4.
1811 */
1812DLLEXPORT SIMPLE_FUNCTION
1813struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
1814                struct sockaddr *addr);
1815
1816
1817/** Parses an IP or TCP option
1818 * @param[in,out] ptr   The pointer to the current option
1819 * @param[in,out] len   The total length of all the remaining options
1820 * @param[out] type     The type of the option
1821 * @param[out] optlen   The length of the option
1822 * @param[out] data     The data of the option
1823 *
1824 * @return bool true if there is another option (and the fields are filled in)
1825 *               or false if this was the last option.
1826 *
1827 * This updates ptr to point to the next option after this one, and updates
1828 * len to be the number of bytes remaining in the options area.  Type is updated
1829 * to be the code of this option, and data points to the data of this option,
1830 * with optlen saying how many bytes there are.
1831 *
1832 * @note Beware of fragmented packets.
1833 */
1834DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
1835                        unsigned char *type,
1836                        unsigned char *optlen,
1837                        unsigned char **data);
1838
1839/*@}*/
1840
1841/** @name Time
1842 * These functions deal with the timestamp describing when a packet was
1843 * captured and can convert it into various formats
1844 * @{
1845 */
1846
1847/** Get the packet timestamp in the DAG time format
1848 * @param packet        The packet to extract the timestamp from
1849 *
1850 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
1851 * past 1970-01-01, the lower 32bits are partial seconds)
1852 */
1853DLLEXPORT SIMPLE_FUNCTION
1854uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
1855
1856/** Get the packet timestamp as a struct timeval
1857 * @param packet        The packet to extract the timestamp from
1858 *
1859 * @return The time that this packet was captured in a struct timeval
1860 */
1861DLLEXPORT SIMPLE_FUNCTION
1862struct timeval trace_get_timeval(const libtrace_packet_t *packet);
1863
1864/** Get the packet timestamp as a struct timespec
1865 * @param packet        The packet to extract the timestamp from
1866 *
1867 * @return The time that this packet was captured in a struct timespec
1868 */
1869DLLEXPORT SIMPLE_FUNCTION
1870struct timespec trace_get_timespec(const libtrace_packet_t *packet);
1871
1872/** Get the packet timestamp in floating point seconds
1873 * @param packet        The packet to extract the timestamp from
1874 *
1875 * @return time that this packet was seen in 64-bit floating point seconds from
1876 * the UNIX epoch (1970-01-01 00:00:00 UTC).
1877 */
1878DLLEXPORT SIMPLE_FUNCTION
1879double trace_get_seconds(const libtrace_packet_t *packet);
1880
1881/** Seek within an input trace to a time specified in floating point seconds
1882 * @param trace         The input trace to seek within
1883 * @param seconds       The time to seek to, in floating point seconds
1884 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
1885 * the error that occurred.
1886 *
1887 * This will make the next packet read to be the first packet to occur at or
1888 * after the specified time.  This must be called in the configuration state
1889 * (i.e. before trace_start() or after trace_pause()).
1890 *
1891 * The time format accepted by this function is 64-bit floating point seconds
1892 * since the UNIX epoch (1970-01-01 00:00:00 UTC), i.e. the same format as
1893 * trace_get_seconds().
1894 *
1895 * @note This function may be extremely slow.
1896 */
1897DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
1898
1899/** Seek within an input trace to a time specified as a timeval
1900 * @param trace         The input trace to seek within
1901 * @param tv            The time to seek to, as a timeval
1902 *
1903 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
1904 * the error that occurred.
1905 *
1906 * This will make the next packet read to be the first packet to occur at or
1907 * after the specified time.  This must be called in the configuration state
1908 * (i.e. before trace_start() or after trace_pause()).
1909 *
1910 * @note This function may be extremely slow.
1911 */
1912DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
1913
1914/** Seek within an input trace to a time specified as an ERF timestamp
1915 * @param trace         The input trace to seek within
1916 * @param ts            The time to seek to, as an ERF timestamp
1917 *
1918 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
1919 * the error that occurred.
1920 *
1921 * This will make the next packet read to be the first packet to occur at or
1922 * after the specified time.  This must be called in the configuration state
1923 * (i.e. before trace_start() or after trace_pause()).
1924 *
1925 * The time format accepted by this function is the ERF timestamp, which is a
1926 * 64-bit value where the upper 32 bits are seconds since the UNIX epoch and
1927 * the lower 32 bits are partial seconds.
1928 *
1929 * @note This function may be extremely slow.
1930 */
1931DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
1932
1933/*@}*/
1934
1935/** @name Sizes
1936 * This section deals with finding or setting the various different lengths
1937 * that a packet can have, e.g. capture lengths, wire lengths, etc.
1938 * @{
1939 */
1940/** Get the current size of the packet (in bytes), taking into account any
1941 * truncation or snapping that may have previously been performed.
1942 *
1943 * @param packet        The packet to determine the capture length for
1944 * @return The size of the packet read from the input trace, i.e. what is
1945 * actually available to libtrace at the moment.
1946 *
1947 * @note Most traces are header captures, so this value may not be the same
1948 * as the size of the packet when it was captured. Use trace_get_wire_length()
1949 * to get the original size of the packet.
1950 
1951 * @note This can (and often is) different for different packets in a trace!
1952 
1953 * @note This is sometimes called the "snaplen".
1954 *
1955 * @note The return size refers to the network-level payload of the packet and
1956 * does not include any capture framing headers. For example, an Ethernet
1957 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1958 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
1959 * (pcap/erf/etc) header.
1960 */
1961DLLEXPORT SIMPLE_FUNCTION
1962size_t trace_get_capture_length(const libtrace_packet_t *packet);
1963
1964/** Get the size of the packet as it was originally seen on the wire (in bytes).
1965 * @param packet        The packet to determine the wire length for
1966 * @return The size of the packet as it was on the wire.
1967 *
1968 * @note This value may not be the same as the capture length, due to
1969 * truncation.
1970 *
1971 * @note trace_get_wire_length \em includes  the Frame Check Sequence. This is
1972 * different behaviour compared to most PCAP-based tools.
1973 *
1974 * @note The return size refers to the network-level payload of the packet and
1975 * does not include any capture framing headers. For example, an Ethernet
1976 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1977 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
1978 * (pcap/erf/etc) header.
1979 */
1980DLLEXPORT SIMPLE_FUNCTION
1981size_t trace_get_wire_length(const libtrace_packet_t *packet);
1982
1983/** Get the length of the capture framing headers (in bytes).
1984 * @param packet        The packet to determine the framing length for
1985 * @return The size of the capture format header encapsulating the packet.
1986 *
1987 * @note This length corresponds to the difference between the amount of
1988 * memory required to store a captured packet and the capture length reported
1989 * by trace_capture_length()
1990 */
1991DLLEXPORT SIMPLE_FUNCTION
1992size_t trace_get_framing_length(const libtrace_packet_t *packet);
1993
1994/** Truncate ("snap") the packet to the suggested length
1995 * @param packet        The packet to truncate
1996 * @param size          The new length of the packet (in bytes)
1997 *
1998 * @return The new capture length of the packet or the original capture
1999 * length of the packet if unchanged.
2000 *
2001 * This function will modify the capture length of the given packet. The wire
2002 * length will not be changed, so you can always determine what the original
2003 * packet size was, prior to the truncation.
2004 *
2005 * @note You can only use this function to decrease the capture length. Any
2006 * attempt to increase capture length will have no effect.
2007 */
2008DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
2009
2010/*@}*/
2011
2012
2013/** Gets the link layer type for a packet
2014 * @param packet        The packet to extract the link layer type for
2015 * @return A libtrace_linktype_t describing the link layer protocol being used
2016 * by this packet.
2017 */
2018DLLEXPORT SIMPLE_FUNCTION
2019libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
2020
2021/** Set the direction flag for a packet, if the capture format supports
2022 * direction tagging.
2023 *
2024 * @param packet        The packet to set the direction for
2025 * @param direction     The new direction
2026 * @returns -1 on error, or the direction that was set.
2027 *
2028 * @note Few capture formats actually support direction tagging. Most notably,
2029 * we cannot set the direction on PCAP packets.
2030 */
2031DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
2032
2033/** Get the direction flag for a packet, if it has one.
2034 * @param packet  The packet to get the direction for
2035 *
2036 * @return A value representing the direction flag, or -1 if this is not
2037 * supported by the capture format.
2038 *
2039 * The direction is defined as 0 for packets originating locally (ie, outbound)
2040 * and 1 for packets originating remotely (ie, inbound). Other values are
2041 * possible, which might be overloaded to mean special things for certain
2042 * traces, e.g. in the Waikato traces, 2 is used to represent an "Unknown"
2043 * direction.
2044 *
2045 * For DAG/ERF traces, the direction is extracted from the "Interface" bits in
2046 * the ERF header, which can range from 0 - 3.
2047 */
2048DLLEXPORT SIMPLE_FUNCTION
2049libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
2050
2051/** @name BPF
2052 * This section deals with using Berkley Packet Filters to filter input traces
2053 * @{
2054 */
2055/** Creates a BPF filter
2056 * @param filterstring The filter string describing the BPF filter to create
2057 * @return An opaque pointer to a libtrace_filter_t object
2058 *
2059 * @note The filter is not actually compiled at this point, so no correctness
2060 * tests are performed here. trace_create_filter() will always return ok, but
2061 * if the filter is poorly constructed an error will be generated when the
2062 * filter is actually used.
2063 */
2064DLLEXPORT SIMPLE_FUNCTION
2065libtrace_filter_t *trace_create_filter(const char *filterstring);
2066
2067/** Create a BPF filter based on pre-compiled byte-code.
2068 * @param bf_insns      A pointer to the start of the byte-code
2069 * @param bf_len        The number of BPF instructions 
2070 * @return              An opaque pointer to a libtrace_filter_t object
2071 * @note                The supplied byte-code is not checked for correctness.
2072 *                      Instead, incorrect byte-code will generate an error
2073 *                      once the filter is actually used.
2074 * @author              Scott Raynel
2075 */
2076DLLEXPORT libtrace_filter_t *
2077trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len);
2078
2079/** Apply a BPF filter to a packet
2080 * @param filter        The filter to be applied       
2081 * @param packet        The packet to be matched against the filter
2082 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
2083 *
2084 * @note Due to the way BPF filters are built, the filter is not actually
2085 * compiled until the first time trace_create_filter is called. If your filter
2086 * is incorrect, it will generate an error message and assert, exiting the
2087 * program. This behaviour may change to a more graceful handling of this error
2088 * in the future.
2089 */
2090DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
2091                const libtrace_packet_t *packet);
2092
2093/** Destroy a BPF filter
2094 * @param filter        The filter to be destroyed
2095 *
2096 * Deallocates all the resources associated with a BPF filter.
2097 */
2098DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
2099/*@}*/
2100
2101/** @name Portability
2102 * This section contains functions that deal with portability issues, e.g. byte
2103 * ordering.
2104 * @{
2105 */
2106
2107/** Converts an ethernet address to a printable string
2108 * @param addr  Ethernet address in network byte order
2109 * @param buf   Buffer to store the ascii representation, or NULL to indicate
2110 * that static storage should be used.
2111 * @return buf, or if buf is NULL then a statically allocated buffer.
2112 *
2113 * This function is similar to the GNU ether_ntoa_r function, with a few
2114 * minor differences.  If NULL is passed as buf, then the function will
2115 * use an internal static buffer. If NULL isn't passed then the function
2116 * will use that buffer instead.
2117 *
2118 * The address pointers returned by trace_get_source_mac() and
2119 * trace_get_destination_mac() can be passed directly into this function.
2120 *
2121 * @note The type of addr isn't struct ether_addr as it is with ether_ntoa_r,
2122 * however it is bit compatible so that a cast will work.
2123 */
2124DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
2125
2126/** Convert a string to an ethernet address
2127 * @param buf   A string containing an Ethernet address in hex format
2128 * delimited with :'s.
2129 * @param addr  Buffer to store the binary representation, or NULL to indicate
2130 * that static storage should be used.
2131 * @return addr, or if addr is NULL then a statically allocated buffer.
2132 *
2133 * This function is similar to the GNU ether_aton_r function, with a few
2134 * minor differences.  If NULL is passed as addr, then the function will
2135 * use an internal static buffer. If NULL isn't passed then the function will
2136 * use that buffer instead.
2137 *
2138 * The address returned by this function will be in network byte order.
2139 *
2140 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
2141 * however it is bit compatible so that a cast will work.
2142 */
2143DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
2144
2145/*@}*/
2146
2147/** @name Ports
2148 * This section contains functions for dealing with port numbers at the
2149 * transport layer.
2150 *
2151 * @{
2152 */
2153
2154/** An indication of which port is the "server" port for a given port pair */
2155typedef enum {
2156        USE_DEST,       /**< Destination port is the server port */
2157        USE_SOURCE      /**< Source port is the server port */
2158} serverport_t;
2159
2160/** Gets the source port for a given packet
2161 * @param packet        The packet to get the source port from
2162 * @return The source port in HOST byte order or 0 if no suitable port number
2163 * can be extracted from the packet.
2164 *
2165 * This function will return 0 if the transport protocol is known not to
2166 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2167 * present in the packet or the transport header has been truncated such that
2168 * the port fields are not readable.
2169 *
2170 * @note If the transport protocol is not known by libtrace, the first two
2171 * bytes of the transport header will be treated as the source port field.
2172 */
2173DLLEXPORT SIMPLE_FUNCTION
2174uint16_t trace_get_source_port(const libtrace_packet_t *packet);
2175
2176/** Gets the destination port for a given packet
2177 * @param packet        The packet to get the destination port from
2178 * @return The destination port in HOST byte order or 0 if no suitable port
2179 * number can be extracted from the packet.
2180 *
2181 * This function will return 0 if the transport protocol is known not to
2182 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2183 * present in the packet or the transport header has been truncated such that
2184 * the port fields are not readable.
2185 *
2186 * @note If the transport protocol is not known by libtrace, the third and
2187 * fourth bytes of the transport header will be treated as the destination
2188 * port field.
2189 *
2190 */
2191DLLEXPORT SIMPLE_FUNCTION
2192uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
2193
2194/** Hint at which of the two provided ports is the server port.
2195 *
2196 * @param protocol      The IP layer protocol, eg 6 (tcp), 17 (udp)
2197 * @param source        The source port from the packet
2198 * @param dest          The destination port from the packet
2199 *
2200 * @return one of USE_SOURCE or USE_DEST describing on which of the two ports
2201 * is most likely to be the server port.
2202 *
2203 * @note Ports must be provided in HOST byte order!
2204 *
2205 * This function is based almost entirely on heuristics and should not be
2206 * treated as a definitive means of identifying the server port. However, it
2207 * is deterministic, so it is very handy for identifying both halves of the
2208 * same flow.
2209 */
2210DLLEXPORT SIMPLE_FUNCTION
2211int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
2212
2213/*@}*/
2214
2215/** @name Wireless trace support
2216 * Functions to access wireless information from packets that have wireless
2217 * monitoring headers such as Radiotap or Prism.
2218 *
2219 * The trace_get_wireless_* functions provide an abstract interface for
2220 * retrieving information from wireless traces. They take a pointer to the
2221 * wireless monitoring header (usually found with trace_get_packet_meta()) and
2222 * the linktype of the header passed in.
2223 *
2224 * All of the trace_get_wireless_* functions return false if the requested
2225 * information was unavailable, or true if it was. The actual data is stored
2226 * in an output variable supplied by the caller. Values returned into the
2227 * output variable will always be returned in host byte order.
2228 * @{
2229 */
2230
2231
2232#ifndef ARPHRD_80211_RADIOTAP
2233/** libc doesn't define this yet, so we have to do so ourselves */
2234#define ARPHRD_80211_RADIOTAP 803
2235#endif
2236
2237/** Get the wireless Timer Synchronisation Function
2238 *
2239 * Gets the value of the timer synchronisation function for this frame, which
2240 * is a value in microseconds indicating the time that the first bit of the
2241 * MPDU was received by the MAC.
2242 *
2243 * @param linkptr The wireless meta header
2244 * @param linktype The linktype of the wireless meta header passed in
2245 * @param[out] tsft The value of the timer synchronisation function.
2246 * @return true if the field was available, false if not.
2247 */
2248DLLEXPORT bool trace_get_wireless_tsft(void *linkptr,
2249        libtrace_linktype_t linktype, uint64_t *tsft);
2250
2251/** Get the wireless data rate
2252 *
2253 * @param linkptr The wireless meta header
2254 * @param linktype The linktype of the wireless meta header passed in
2255 * @param[out] rate The data-rate of the frame in units of 500kbps
2256 * @return true if the field was available, false if not.
2257 */
2258DLLEXPORT bool trace_get_wireless_rate(void *linkptr,
2259        libtrace_linktype_t linktype, uint8_t *rate);
2260
2261/** Get the wireless channel frequency
2262 * @param linkptr The wireless meta header
2263 * @param linktype The linktype of the wireless meta header passed in
2264 * @param[out] freq The frequency in MHz of the channel the frame was
2265 * transmitted or received on.
2266 * @return true if the field was available, false if not.
2267 */
2268DLLEXPORT bool trace_get_wireless_freq(void *linkptr,
2269        libtrace_linktype_t linktype, uint16_t *freq);
2270
2271/** Get the wireless signal strength in dBm
2272 * @param linkptr The wireless meta header
2273 * @param linktype The linktype of the wireless meta header passed in
2274 * @param[out] strength The RF signal power at the antenna, in dB difference
2275 * from 1mW.
2276 * @return true if the field was available, false if not.
2277 */
2278DLLEXPORT bool trace_get_wireless_signal_strength_dbm(void *linkptr,
2279        libtrace_linktype_t linktype, int8_t *strength);
2280
2281/** Get the wireless noise strength in dBm
2282 * @param linkptr The wireless meta header
2283 * @param linktype The linktype of the wireless meta header passed in
2284 * @param[out] strength The RF noise power at the antenna, in dB difference
2285 * from 1mW.
2286 * @return true if the field was available, false if not.
2287 */
2288DLLEXPORT bool trace_get_wireless_noise_strength_dbm(void *linkptr,
2289        libtrace_linktype_t linktype, int8_t *strength);
2290
2291/** Get the wireless signal strength in dB
2292 * @param linkptr The wireless meta header
2293 * @param linktype The linktype of the wireless meta header passed in
2294 * @param[out] strength The RF signal power at the antenna, in dB difference
2295 * from a fixed reference.
2296 * @return true if the field was available, false if not.
2297 */
2298DLLEXPORT bool trace_get_wireless_signal_strength_db(void *linkptr,
2299        libtrace_linktype_t linktype, uint8_t *strength);
2300
2301/** Get the wireless noise strength in dB
2302 * @param linkptr The wireless meta header
2303 * @param linktype The linktype of the wireless meta header passed in
2304 * @param[out] strength The RF noise power at the antenna, in dB difference
2305 * from a fixed reference.
2306 * @return true if the field was available, false if not.
2307 */
2308DLLEXPORT bool trace_get_wireless_noise_strength_db(void *linkptr,
2309        libtrace_linktype_t linktype, uint8_t *strength);
2310
2311/** Get the wireless transmit attenuation
2312 * @param linkptr The wireless meta header
2313 * @param linktype The linktype of the wireless meta header passed in
2314 * @param[out] attenuation The transmit power as a unitless distance from
2315 * maximum power set at factory calibration. 0 indicates maximum transmission
2316 * power.
2317 * @return true if the field was available, false if not.
2318 */
2319DLLEXPORT bool trace_get_wireless_tx_attenuation(void *linkptr,
2320        libtrace_linktype_t linktype, uint16_t *attenuation);
2321
2322/** Get the wireless transmit attenuation in dB
2323 * @param linkptr The wireless meta header
2324 * @param linktype The linktype of the wireless meta header passed in
2325 * @param[out] attenuation The transmit power as dB difference from maximum
2326 * power set at factory calibration. 0 indicates maximum power.
2327 * @return true if the field was available, false if not.
2328 */
2329DLLEXPORT bool trace_get_wireless_tx_attenuation_db(void *linkptr,
2330        libtrace_linktype_t linktype, uint16_t *attenuation);
2331
2332/** Get the wireless transmit power in dBm
2333 * @param linkptr The wireless meta header
2334 * @param linktype The linktype of the wireless meta header passed in
2335 * @param[out] txpower The transmit power as dB from a 1mW reference. This is
2336 * the absolute power level measured at the antenna port. 
2337 * @return true if the field was available, false if not.
2338 */
2339DLLEXPORT bool trace_get_wireless_tx_power_dbm(void *linkptr,
2340                libtrace_linktype_t linktype, int8_t *txpower);
2341
2342/** Get the wireless antenna
2343 * @param linkptr The wireless meta header
2344 * @param linktype The linktype of the wireless meta header passed in
2345 * @param[out] antenna The antenna that was used to transmit or receive the
2346 * frame.
2347 * @return true if the field was available, false if not.
2348 */
2349DLLEXPORT bool trace_get_wireless_antenna(void *linkptr,
2350        libtrace_linktype_t linktype, uint8_t *antenna);
2351
2352/*@}*/
2353
2354#ifdef __cplusplus
2355} /* extern "C" */
2356#endif /* #ifdef __cplusplus */
2357#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.