source: lib/libtrace.h.in @ dfda78f

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

Fix annoying typo.

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