source: lib/libtrace.h.in @ 94dbe30

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 94dbe30 was 94dbe30, checked in by Shane Alcock <salcock@…>, 8 years ago
  • OpenBSD is a real nightmare with its handling of DLTs - dealing with traces captured on an OpenBSD loopback interface is still going to be disastrous but hopefully we can at least get the live stuff right
  • Property mode set to 100644
File size: 125.1 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34#ifndef LIBTRACE_H
35#define LIBTRACE_H
36
37/** @file
38 *
39 * @brief Trace file processing library header
40 *
41 * @author Daniel Lawson
42 * @author Perry Lorier
43 * @author Shane Alcock
44 *
45 * @version $Id$
46 *
47 * This library provides a per packet interface into a trace file, or a live
48 * captures.  It supports ERF, DAG cards, PCAP, Linux and BSD native sockets,
49 * legacy ERF formats etc.
50 *
51 * @par Usage
52 * See the example/ directory in the source distribution for some simple
53 * examples
54 *
55 * @par Linking
56 * To use this library you need to link against libtrace by passing -ltrace
57 * to your linker. You may also need to link against a version of libpcap
58 * and of zlib which are compiled for largefile support (if you wish to access
59 * traces larger than 2 GB). This is left as an exercise for the reader. Debian
60 * Woody, at least, does not support large file offsets.
61 *
62 */
63
64#include <sys/types.h>
65#ifndef WIN32
66#include <sys/time.h>
67#endif
68
69#ifdef _MSC_VER
70    /* define the following from MSVC's internal types */
71    typedef             __int8  int8_t;
72    typedef             __int16 int16_t;
73    typedef             __int32 int32_t;
74    typedef             __int64 int64_t;
75    typedef unsigned    __int8  uint8_t;
76    typedef unsigned    __int16 uint16_t;
77    typedef unsigned    __int32 uint32_t;
78    typedef unsigned    __int64 uint64_t;
79    #ifdef LT_BUILDING_DLL
80        #define DLLEXPORT __declspec(dllexport)
81    #else
82        #define DLLEXPORT __declspec(dllimport)
83    #endif
84    #define DLLLOCAL
85    /* Windows pads bitfields out to to the size of their parent type
86     * however gcc warns that this doesn't meet with the iso C specification
87     * so produces warnings for this behaviour.  sigh.
88     */
89    #define LT_BITFIELD8        uint8_t
90    #define LT_BITFIELD16       uint16_t
91    #define LT_BITFIELD32       uint32_t
92    #define LT_BITFIELD64       uint64_t
93#else
94    #ifdef HAVE_STDINT_H
95        #   include <stdint.h>
96    #endif
97    #if __GNUC__ >= 4
98        #ifdef LT_BUILDING_DLL
99                #define DLLEXPORT __attribute__ ((visibility("default")))
100                #define DLLLOCAL __attribute__ ((visibility("hidden")))
101        #else
102                #define DLLEXPORT
103                #define DLLLOCAL
104        #endif
105    #else
106        #define DLLEXPORT
107        #define DLLLOCAL
108    #endif
109    /* GCC warns if the bitfield type is not "unsigned int", however windows
110     * generates incorrect code for this (see above), so we define these
111     * macros.  How Hideous.  So much for C's portability.
112     */
113    #define LT_BITFIELD8        unsigned int
114    #define LT_BITFIELD16       unsigned int
115    #define LT_BITFIELD32       unsigned int
116    #define LT_BITFIELD64       unsigned int
117#endif
118
119#ifdef WIN32
120#   include <winsock2.h>
121#   include <ws2tcpip.h>
122    typedef short sa_family_t;
123    /* Make up for a lack of stdbool.h */
124#    define bool signed char
125#    define false 0
126#    define true 1
127#    if !defined(ssize_t)
128     /* XXX: Not 64-bit safe! */
129#    define ssize_t int
130#    endif   
131#else
132#    include <netinet/in.h>
133
134#ifndef __cplusplus
135#    include <stdbool.h>
136#endif
137
138#    include <sys/types.h>
139#    include <sys/socket.h>
140#endif
141
142/** API version as 2 byte hex digits, eg 0xXXYYZZ */
143#define LIBTRACE_API_VERSION \
144            ((@LIBTRACE_MAJOR@<<16)|(@LIBTRACE_MID@<<8)|(@LIBTRACE_MINOR@))
145
146/** Replaced with the current SVN revision number when 'make dist' is invoked
147 *  to create a distributable tarball */
148#define LIBTRACE_SVN_REVISION 0
149
150/** DAG driver version installed on the current system */
151#define DAG_DRIVER_V "@DAG_VERSION_NUM@"
152   
153#ifdef __cplusplus
154extern "C" {
155#endif
156
157/* Function does not depend on anything but its
158 * parameters, used to hint gcc's optimisations
159 */
160#if __GNUC__ >= 3
161#  define DEPRECATED __attribute__((deprecated))
162#  define SIMPLE_FUNCTION __attribute__((pure))
163#  define UNUSED __attribute__((unused))
164#  define PACKED __attribute__((packed))
165#  define PRINTF(formatpos,argpos) __attribute__((format(printf,formatpos,argpos)))
166#else
167#  define DEPRECATED
168#  define SIMPLE_FUNCTION
169#  define UNUSED
170#  define PACKED
171#  define PRINTF(formatpos,argpos)
172#endif
173       
174/** Opaque structure holding information about an output trace */
175typedef struct libtrace_out_t libtrace_out_t;
176       
177/** Opaque structure holding information about a trace */
178typedef struct libtrace_t libtrace_t;
179       
180/** Opaque structure holding information about a bpf filter */
181typedef struct libtrace_filter_t libtrace_filter_t;
182
183/** If the packet has allocated its own memory the buffer_control should be
184 * set to TRACE_CTRL_PACKET, so that the memory will be freed when the packet
185 * is destroyed. If the packet has been zerocopied out of memory owned by
186 * something else, e.g. a DAG card, it should be TRACE_CTRL_EXTERNAL.
187 *
188 * @note The letters p and e are magic numbers used to detect if the packet
189 * wasn't created properly.
190 */
191typedef enum {
192        TRACE_CTRL_PACKET='p',  /**< Buffer memory is owned by the packet */
193        TRACE_CTRL_EXTERNAL='e' /**< Buffer memory is owned by an external source */
194} buf_control_t;
195
196/** The size of a packet's buffer when managed by libtrace */
197#define LIBTRACE_PACKET_BUFSIZE 65536
198
199/** Libtrace error information */
200typedef struct trace_err_t{
201        int err_num;            /**< error code */
202        char problem[255];      /**< the format, uri etc that caused the error for reporting purposes */
203} libtrace_err_t;
204
205/** Enumeration of error codes */
206enum {
207        /** No Error has occured.... yet. */
208        TRACE_ERR_NOERROR       = 0,
209        /** The URI passed to trace_create() is unsupported, or badly formed */
210        TRACE_ERR_BAD_FORMAT    = -1,
211        /** The trace failed to initialise */
212        TRACE_ERR_INIT_FAILED   = -2,
213        /** Unknown config option */
214        TRACE_ERR_UNKNOWN_OPTION= -3,
215        /** This output uri cannot write packets of this type */
216        TRACE_ERR_NO_CONVERSION = -4,
217        /** This packet is corrupt, or unusable for the action required */
218        TRACE_ERR_BAD_PACKET    = -5,
219        /** Option known, but unsupported by this format */
220        TRACE_ERR_OPTION_UNAVAIL= -6,
221        /** This feature is unsupported */
222        TRACE_ERR_UNSUPPORTED   = -7,
223        /** Illegal use of the API */
224        TRACE_ERR_BAD_STATE     = -8,
225        /** Failed to compile a BPF filter */
226        TRACE_ERR_BAD_FILTER    = -9,
227        /** RT communication breakdown */
228        TRACE_ERR_RT_FAILURE    = -10,
229        /** Compression format unsupported */
230        TRACE_ERR_UNSUPPORTED_COMPRESS  = -11
231};
232
233/** Enumeration of DLTs supported by libtrace
234 */
235typedef enum {
236        /** pcap documents this as having the Address Family value in host byte order as the
237          * framing.  Ugly? Yes.
238          */
239        TRACE_DLT_NULL = 0,
240        TRACE_DLT_EN10MB = 1,
241        TRACE_DLT_PPP = 9,
242        TRACE_DLT_ATM_RFC1483 = 11,
243       
244        /** Ok, so OpenBSD has a different value for DLT_RAW as the rest of the planet, so detect
245          * this.  When reading to/from files we should be using TRACE_DLT_LINKTYPE_RAW instead.
246          * When talking about DLT's inside libtrace tho, we should be using /these/ DLT's.
247          */
248#ifdef __OpenBSD__
249        TRACE_DLT_OPENBSD_LOOP=12,
250        TRACE_DLT_RAW = 14,     
251#else
252        TRACE_DLT_RAW = 12,
253        TRACE_DLT_OPENBSD_LOOP = 108,
254#endif
255        TRACE_DLT_PPP_SERIAL = 50,
256        TRACE_DLT_LINKTYPE_RAW = 101, /**< See TRACE_DLT_RAW for explainations of pain. */
257        TRACE_DLT_C_HDLC = 104,
258        TRACE_DLT_IEEE802_11 = 105,
259        TRACE_DLT_LINUX_SLL = 113,
260        TRACE_DLT_PFLOG = 117,
261        TRACE_DLT_IEEE802_11_RADIO = 127 /**< Radiotap */
262} libtrace_dlt_t ;
263
264/** Enumeration of link layer types supported by libtrace */
265typedef enum {
266    /* TRACE_TYPE_LEGACY = 0            Obsolete */
267       TRACE_TYPE_HDLC_POS = 1,         /**< HDLC over POS */
268       TRACE_TYPE_ETH = 2,              /**< 802.3 style Ethernet */
269       TRACE_TYPE_ATM = 3,              /**< ATM frame */
270       TRACE_TYPE_80211 = 4,            /**< 802.11 frames */
271       TRACE_TYPE_NONE = 5,             /**< Raw IP frames */
272       TRACE_TYPE_LINUX_SLL = 6,        /**< Linux "null" framing */
273       TRACE_TYPE_PFLOG = 7,            /**< FreeBSD's PFlog */
274    /* TRACE_TYPE_LEGACY_DEFAULT        Obsolete */
275       TRACE_TYPE_POS = 9,              /**< Packet-over-SONET */
276    /* TRACE_TYPE_LEGACY_ATM            Obsolete */
277    /* TRACE_TYPE_LEGACY_ETH            Obsolete */
278       TRACE_TYPE_80211_PRISM = 12,     /**< 802.11 Prism frames */
279       TRACE_TYPE_AAL5 = 13,            /**< ATM Adaptation Layer 5 frames */
280       TRACE_TYPE_DUCK = 14,         /**< Pseudo link layer for DUCK packets */
281       TRACE_TYPE_80211_RADIO = 15,  /**< Radiotap + 802.11 */
282       TRACE_TYPE_LLCSNAP = 16,      /**< Raw LLC/SNAP */
283       TRACE_TYPE_PPP = 17,          /**< PPP frames */
284       TRACE_TYPE_METADATA = 18,        /**< WDCAP-style meta-data */
285       TRACE_TYPE_NONDATA = 19,         /**< Not a data packet */
286       TRACE_TYPE_OPENBSD_LOOP = 20     /**< OpenBSD loopback */
287} libtrace_linktype_t;
288
289/** RT protocol base format identifiers.
290 * This is used to describe the capture format of the packet is being sent
291 * using the RT protocol.
292 */
293enum base_format_t {
294        TRACE_FORMAT_ERF          =1,   /**< ERF (DAG capture format) */
295        TRACE_FORMAT_PCAP         =2,   /**< Live PCAP capture */
296        TRACE_FORMAT_PCAPFILE     =3,   /**< PCAP trace file */
297        TRACE_FORMAT_WAG          =4,   /**< WAG live capture (Obsolete) */
298        TRACE_FORMAT_RT           =5,   /**< RT network protocol */
299        TRACE_FORMAT_LEGACY_ATM   =6,   /**< Legacy ERF for ATM capture */
300        TRACE_FORMAT_LEGACY_POS   =7,   /**< Legacy ERF for POS capture */
301        TRACE_FORMAT_LEGACY_ETH   =8,   /**< Legacy ERF for ETH capture */
302        TRACE_FORMAT_LINUX_NATIVE =9,   /**< Linux native interface capture */
303        TRACE_FORMAT_DUCK         =10,  /**< DAG Clock information */
304        TRACE_FORMAT_BPF          =11,  /**< BSD native interface capture */
305        TRACE_FORMAT_TSH          =12,  /**< TSH trace format */
306        TRACE_FORMAT_ATMHDR       =13,  /**< Legacy ATM header capture */
307        TRACE_FORMAT_LEGACY_NZIX  =14,  /**< Legacy format used for NZIX traces */
308        TRACE_FORMAT_LINUX_RING   =15   /**< Linux native interface capture PACKET_MMAP */
309};
310
311/** RT protocol packet types */
312typedef enum {
313        TRACE_RT_HELLO          =1, /**< Connection accepted */
314        TRACE_RT_START          =2, /**< Request for data transmission to begin
315                                    */
316        TRACE_RT_ACK            =3, /**< Data acknowledgement */
317        TRACE_RT_STATUS         =4, /**< Fifo status packet */
318        TRACE_RT_DUCK           =5, /**< Dag duck info packet */
319        TRACE_RT_END_DATA       =6, /**< Server is exiting message */
320        TRACE_RT_CLOSE          =7, /**< Client is exiting message */
321        TRACE_RT_DENY_CONN      =8, /**< Connection has been denied */
322        TRACE_RT_PAUSE          =9, /**< Request server to suspend sending data
323                                     */
324        TRACE_RT_PAUSE_ACK      =10,/**< Server is paused message */
325        TRACE_RT_OPTION         =11,/**< Option request */
326        TRACE_RT_KEYCHANGE      =12,/**< Anonymisation key has changed */
327        TRACE_RT_DUCK_2_4       =13,/**< Dag 2.4 Duck */
328        TRACE_RT_DUCK_2_5       =14,/**< Dag 2.5 Duck */
329        TRACE_RT_LOSTCONN       =15,/**< Lost connection to server */
330        TRACE_RT_SERVERSTART    =16,/**< Reliable server has been restarted */
331        TRACE_RT_CLIENTDROP     =17,/**< Reliable client was lost */
332        TRACE_RT_METADATA       =18,/**< Packet contains server meta-data */
333
334        /** Not actually used - all DATA types begin from this value */
335        TRACE_RT_DATA_SIMPLE    = 1000,
336       
337        /** RT is encapsulating an ERF capture record */
338        TRACE_RT_DATA_ERF       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_ERF,
339        /** RT is encapsulating a WAG capture record */
340        TRACE_RT_DATA_WAG       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_WAG,
341        /** RT is encapsulating a Legacy ATM capture record */
342        TRACE_RT_DATA_LEGACY_ATM=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_ATM,
343        /** RT is encapsulating a Legacy POS capture record */
344        TRACE_RT_DATA_LEGACY_POS=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_POS,
345        /** RT is encapsulating a Legacy ETH capture record */
346        TRACE_RT_DATA_LEGACY_ETH=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LEGACY_ETH,
347        /** RT is encapsulating a Linux native capture record */
348        TRACE_RT_DATA_LINUX_NATIVE=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LINUX_NATIVE,
349        /** RT is encapsulating a BSD native capture record */
350        //TRACE_RT_DATA_BPF       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_BPF,
351        /** RT is encapsulating a TSH capture record */
352        TRACE_RT_DATA_TSH       =TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_TSH,
353        /** RT is encapsulating an ATM header capture record */
354        TRACE_RT_DATA_ATMHDR = TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_ATMHDR,
355        /** RT is encapsulating a Legacy NZIX capture record */
356        TRACE_RT_DATA_LEGACY_NZIX=TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_LEGACY_NZIX,
357        /** RT is encapsulating a Linux native PACKET_MMAP capture record */
358        TRACE_RT_DATA_LINUX_RING=TRACE_RT_DATA_SIMPLE+TRACE_FORMAT_LINUX_RING,
359
360        /** As PCAP does not store the linktype with the packet, we need to
361         * create a separate RT type for each supported DLT, starting from
362         * this value */
363        TRACE_RT_DATA_DLT               = 2000,
364        /** RT is encapsulating a PCAP capture record with a NULL linktype */
365        TRACE_RT_DLT_NULL               =TRACE_RT_DATA_DLT+TRACE_DLT_NULL,
366        /** RT is encapsulating a PCAP capture record with an Ethernet
367         * linktype */
368        TRACE_RT_DLT_EN10MB             =TRACE_RT_DATA_DLT+TRACE_DLT_EN10MB,
369        /** RT is encapsulating a PCAP capture record with an 802.11
370         * linktype */
371        TRACE_RT_DLT_IEEE802_11         =TRACE_RT_DATA_DLT+TRACE_DLT_IEEE802_11,
372        /** RT is encapsulating a PCAP capture record with a Linux SLL
373         * linktype */
374        TRACE_RT_DLT_LINUX_SLL          =TRACE_RT_DATA_DLT+TRACE_DLT_LINUX_SLL,
375        /** RT is encapsulating a PCAP capture record with a PFlog linktype */
376        TRACE_RT_DLT_PFLOG              =TRACE_RT_DATA_DLT+TRACE_DLT_PFLOG,
377        /** RT is encapsulating a PCAP capture record with an AAL5 linktype */
378        TRACE_RT_DLT_ATM_RFC1483        =TRACE_RT_DATA_DLT+TRACE_DLT_ATM_RFC1483,
379        /** Unused value marking the end of the valid range for PCAP RT
380         * encapsulation */
381        TRACE_RT_DATA_DLT_END           = 2999,
382        /** BPF does not store the linktype with the packet, so we need a
383         * separate RT type for each supported DLT. This value represents the
384         * starting point */
385        TRACE_RT_DATA_BPF               = 3000,
386
387        TRACE_RT_BPF_NULL               = TRACE_RT_DATA_BPF+TRACE_DLT_NULL,
388        TRACE_RT_BPF_EN10MB             = TRACE_RT_DATA_BPF+TRACE_DLT_EN10MB,
389        TRACE_RT_BPF_IEEE802_11         = TRACE_RT_DATA_BPF+TRACE_DLT_IEEE802_11,
390        TRACE_RT_BPF_PFLOG              =TRACE_RT_DATA_BPF+TRACE_DLT_PFLOG,
391        TRACE_RT_BPF_ATM_RFC1483        =TRACE_RT_DATA_BPF+TRACE_DLT_ATM_RFC1483,
392
393        TRACE_RT_DATA_BPF_END           = 3999,
394        /** Unused value marking the end of the valid range for all RT packet
395         * types */
396        TRACE_RT_LAST                   = 4000
397} libtrace_rt_types_t;
398
399/** IP Protocol values */
400typedef enum {
401        TRACE_IPPROTO_IP        = 0,    /**< IP pseudo protocol number */
402        TRACE_IPPROTO_ICMP      = 1,    /**< Internet Control Message protocol */
403        TRACE_IPPROTO_IGMP      = 2,    /**< Internet Group Management Protocol */
404        TRACE_IPPROTO_IPIP      = 4,    /**< IP encapsulated in IP */
405        TRACE_IPPROTO_TCP       = 6,    /**< Transmission Control Protocol */
406        TRACE_IPPROTO_UDP       = 17,   /**< User Datagram Protocol */
407        TRACE_IPPROTO_IPV6      = 41,   /**< IPv6 over IPv4 */
408        TRACE_IPPROTO_ROUTING   = 43,   /**< IPv6 Routing header */
409        TRACE_IPPROTO_FRAGMENT  = 44,   /**< IPv6 Fragmentation header */
410        TRACE_IPPROTO_RSVP      = 46,   /**< Resource Reservation Protocol */
411        TRACE_IPPROTO_GRE       = 47,   /**< General Routing Encapsulation */
412        TRACE_IPPROTO_ESP       = 50,   /**< Encapsulated Security Payload [RFC2406] */
413        TRACE_IPPROTO_AH        = 51,   /**< Authentication Header [RFC2402] */
414        TRACE_IPPROTO_ICMPV6    = 58,   /**< ICMPv6 */
415        TRACE_IPPROTO_NONE      = 59,   /**< IPv6 no next header */
416        TRACE_IPPROTO_DSTOPTS   = 60,   /**< IPv6 destination options */
417        TRACE_IPPROTO_OSPF      = 89,   /**< Open Shortest Path First routing protocol */
418        TRACE_IPPROTO_PIM       = 103,  /**< Protocol Independant Multicast */
419        TRACE_IPPROTO_SCTP      = 132   /**< Stream Control Transmission Protocol */
420} libtrace_ipproto_t;
421
422/** Ethertypes supported by Libtrace */
423typedef enum {
424        /* Numbers <=1500 are of course, LLC/SNAP */
425        TRACE_ETHERTYPE_LOOPBACK= 0x0060,       /**< Ethernet Loopback */
426        TRACE_ETHERTYPE_IP      = 0x0800,       /**< IPv4 */
427        TRACE_ETHERTYPE_ARP     = 0x0806,       /**< Address resolution protocol */
428        TRACE_ETHERTYPE_RARP    = 0x8035,       /**< Reverse ARP */
429        TRACE_ETHERTYPE_8021Q   = 0x8100,       /**< 802.1q VLAN Extended Header */
430        TRACE_ETHERTYPE_IPV6    = 0x86DD,       /**< IPv6 */
431        TRACE_ETHERTYPE_MPLS    = 0x8847,       /**< MPLS Unicast traffic */
432        TRACE_ETHERTYPE_MPLS_MC = 0x8848,       /**< MPLS Multicast traffic */
433        TRACE_ETHERTYPE_PPP_DISC= 0x8863,       /**< PPPoE Service Discovery */
434        TRACE_ETHERTYPE_PPP_SES = 0x8864        /**< PPPoE Session Messages */
435} libtrace_ethertype_t;
436
437/** The libtrace packet structure. Applications shouldn't be
438 * meddling around in here
439 */
440typedef struct libtrace_packet_t {
441        struct libtrace_t *trace;       /**< Pointer to the trace */
442        void *header;                   /**< Pointer to the framing header */
443        void *payload;                  /**< Pointer to the link layer */
444        void *buffer;                   /**< Allocated buffer */
445        libtrace_rt_types_t  type;      /**< RT protocol type for the packet */
446        buf_control_t buf_control;      /**< Describes memory ownership */
447        int capture_length;             /**< Cached capture length */
448        int wire_length;                /**< Cached wire length */
449        int payload_length;             /**< Cached payload length */
450        void *l2_header;                /**< Cached link header */
451        libtrace_linktype_t link_type;  /**< Cached link type */
452        uint32_t l2_remaining;          /**< Cached link remaining */
453        void *l3_header;                /**< Cached l3 header */
454        uint16_t l3_ethertype;          /**< Cached l3 ethertype */
455        uint32_t l3_remaining;          /**< Cached l3 remaining */
456        void *l4_header;                /**< Cached transport header */
457        uint8_t transport_proto;        /**< Cached transport protocol */
458        uint32_t l4_remaining;          /**< Cached transport remaining */
459} libtrace_packet_t;
460
461
462/** Trace directions.
463 * Note that these are the directions used by convention. More directions
464 * are possible, not just these 3, and that they may not conform to this
465 * convention.
466 */
467typedef enum {
468        TRACE_DIR_OUTGOING = 0,         /**< Packets originating "inside" */
469        TRACE_DIR_INCOMING = 1,         /**< Packets originating "outside" */
470        TRACE_DIR_OTHER    = 2          /**< Packets with an unknown direction, or one that's unknown */
471} libtrace_direction_t;
472
473/** Enumeration of Radiotap fields */
474typedef enum {
475    TRACE_RADIOTAP_TSFT = 0, /**< Timer synchronisation function, in microseconds (uint64) */
476    TRACE_RADIOTAP_FLAGS = 1, /**< Wireless flags (uint8) */
477    TRACE_RADIOTAP_RATE = 2, /**< Bitrate in units of 500kbps (uint8) */
478    TRACE_RADIOTAP_CHANNEL = 3, /**< Frequency in MHz (uint16) and channel flags (uint16) */
479    TRACE_RADIOTAP_FHSS = 4, /**< FHSS hop set (uint8) and hopping pattern (uint8) */
480    TRACE_RADIOTAP_DBM_ANTSIGNAL = 5, /**< Signal power in dBm (int8) */
481    TRACE_RADIOTAP_DBM_ANTNOISE = 6, /**< Noise power in dBm (int8) */
482    TRACE_RADIOTAP_LOCK_QUALITY = 7, /**< Barker Code lock quality (uint16) */
483    TRACE_RADIOTAP_TX_ATTENUATION = 8, /**< TX attenuation as unitless distance from max power (uint16) */
484    TRACE_RADIOTAP_DB_TX_ATTENUATION = 9, /**< TX attenutation as dB from max power (uint16) */
485    TRACE_RADIOTAP_DBM_TX_POWER = 10, /**< TX Power in dBm (int8) */
486    TRACE_RADIOTAP_ANTENNA = 11, /**< Antenna frame was rx'd or tx'd on (uint8) */
487    TRACE_RADIOTAP_DB_ANTSIGNAL = 12, /**< Signal power in dB from a fixed reference (uint8) */
488    TRACE_RADIOTAP_DB_ANTNOISE = 13, /**< Noise power in dB from a fixed reference (uint8) */
489    TRACE_RADIOTAP_RX_FLAGS = 14, /** Properties of received frame (uint16) */
490    TRACE_RADIOTAP_TX_FLAGS = 15, /** Properties of transmitted frame (uint16) */
491    TRACE_RADIOTAP_RTS_RETRIES = 16, /** Number of rts retries frame used (uint8) */
492    TRACE_RADIOTAP_DATA_RETRIES = 17, /** Number of unicast retries a transmitted frame used (uint8) */
493    TRACE_RADIOTAP_EXT = 31
494} libtrace_radiotap_field_t;
495
496
497/** @name Protocol structures
498 * These convenience structures provide portable versions of the headers
499 * for a variety of protocols.
500 * @{
501 */
502
503#ifdef WIN32
504#pragma pack(push)
505#pragma pack(1)
506#endif
507
508/** Generic IP header structure */
509typedef struct libtrace_ip
510{
511#if BYTE_ORDER == LITTLE_ENDIAN
512    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
513    LT_BITFIELD8 ip_v:4;                /**< Version */
514#elif BYTE_ORDER == BIG_ENDIAN
515    LT_BITFIELD8 ip_v:4;                /**< Version */
516    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
517#else
518#   error "Adjust your <bits/endian.h> defines"
519#endif
520    uint8_t  ip_tos;                    /**< Type of Service */
521    uint16_t ip_len;                    /**< Total Length */
522    int16_t  ip_id;                     /**< Identification */
523    uint16_t ip_off;                    /**< IP Fragment offset (and flags) */
524    uint8_t  ip_ttl;                    /**< Time to Live */
525    uint8_t  ip_p;                      /**< Protocol */
526    uint16_t ip_sum;                    /**< Checksum */
527    struct in_addr ip_src;              /**< Source Address */
528    struct in_addr ip_dst;              /**< Destination Address */
529} PACKED libtrace_ip_t;
530
531/** IPv6 header extension structure */
532typedef struct libtrace_ip6_ext
533{
534        uint8_t nxt;    /**< Next header */
535        uint8_t len;    /**< Length of the current header */
536} PACKED libtrace_ip6_ext_t;
537
538typedef struct libtrace_ip6_frag
539{
540        uint8_t nxt;    /**< Next header */
541        uint8_t res;    /**< Reserved */
542        uint16_t frag_off;      /**< Fragment Offset (includes M flag) */
543        uint32_t ident; /** Fragment identification */
544} PACKED libtrace_ip6_frag_t;
545
546/** Generic IPv6 header structure
547 *
548 * @note The flow label field also includes the Version and Traffic Class
549 * fields, because we haven't figured out a nice way to deal with fields
550 * crossing byte boundaries on both Big and Little Endian machines */
551typedef struct libtrace_ip6
552{
553    uint32_t flow;                      /**< Flow label */
554    uint16_t plen;                      /**< Payload length */
555    uint8_t nxt;                        /**< Next header */
556    uint8_t hlim;                       /**< Hop limit */
557    struct in6_addr ip_src;             /**< Source address */
558    struct in6_addr ip_dst;             /**< Destination address */
559} PACKED libtrace_ip6_t;
560
561/** Generic TCP header structure */
562typedef struct libtrace_tcp
563  {
564    uint16_t source;            /**< Source Port */
565    uint16_t dest;              /**< Destination port */
566    uint32_t seq;               /**< Sequence number */
567    uint32_t ack_seq;           /**< Acknowledgement Number */
568#  if BYTE_ORDER == LITTLE_ENDIAN
569    LT_BITFIELD8 ecn_ns:1;      /**< ECN Nonce Sum */
570    LT_BITFIELD8 res1:3;        /**< Reserved bits */
571    LT_BITFIELD8 doff:4;        /**< Data Offset */
572    LT_BITFIELD8 fin:1;         /**< FIN */
573    LT_BITFIELD8 syn:1;         /**< SYN flag */
574    LT_BITFIELD8 rst:1;         /**< RST flag */
575    LT_BITFIELD8 psh:1;         /**< PuSH flag */
576    LT_BITFIELD8 ack:1;         /**< ACK flag */
577    LT_BITFIELD8 urg:1;         /**< URG flag */
578    LT_BITFIELD8 ece:1;         /**< ECN Echo */
579    LT_BITFIELD8 cwr:1;         /**< ECN CWR */
580#  elif BYTE_ORDER == BIG_ENDIAN
581    LT_BITFIELD8 doff:4;        /**< Data offset */
582    LT_BITFIELD8 res1:3;        /**< Reserved bits */
583    LT_BITFIELD8 ecn_ns:1;      /**< ECN Nonce Sum */
584    LT_BITFIELD8 cwr:1;         /**< ECN CWR */
585    LT_BITFIELD8 ece:1;         /**< ECN Echo */
586    LT_BITFIELD8 urg:1;         /**< URG flag */
587    LT_BITFIELD8 ack:1;         /**< ACK flag */
588    LT_BITFIELD8 psh:1;         /**< PuSH flag */
589    LT_BITFIELD8 rst:1;         /**< RST flag */
590    LT_BITFIELD8 syn:1;         /**< SYN flag */
591    LT_BITFIELD8 fin:1;         /**< FIN flag */
592#  else
593#   error "Adjust your <bits/endian.h> defines"
594#  endif
595    uint16_t window;            /**< Window Size */
596    uint16_t check;             /**< Checksum */
597    uint16_t urg_ptr;           /**< Urgent Pointer */
598} PACKED libtrace_tcp_t;
599
600/** Generic UDP header structure */
601typedef struct libtrace_udp {
602  uint16_t      source;         /**< Source port */
603  uint16_t      dest;           /**< Destination port */
604  uint16_t      len;            /**< Length */
605  uint16_t      check;          /**< Checksum */
606} PACKED libtrace_udp_t;
607
608/** Generic ICMP header structure */
609typedef struct libtrace_icmp
610{
611  uint8_t type;         /**< Message Type */
612  uint8_t code;         /**< Type Sub-code */
613  uint16_t checksum;            /**< Checksum */
614  union
615  {
616    struct
617    {
618      uint16_t  id;             /**< ID of the Echo request */
619      uint16_t  sequence;       /**< Sequence number of the Echo request */
620    } echo;                     /**< Echo Datagram */
621    uint32_t    gateway;        /**< Gateway Address */
622    struct
623    {
624      uint16_t  unused;         /**< Unused */
625      uint16_t  mtu;            /**< Next-hop MTU */
626    } frag;                     /**< Path MTU Discovery */
627  } un;                         /**< Union for Payloads of Various ICMP Codes */
628} PACKED libtrace_icmp_t;
629
630typedef struct libtrace_icmp6 {
631  uint8_t type;         /**< Message Type */
632  uint8_t code;         /**< Type Sub-code */
633  uint16_t checksum;    /**< Checksum */
634
635  union {
636    struct {
637        uint8_t length;   /**< Length of original datagram content in 64 bit words */
638        uint8_t unused;   /**< Unused */       
639        uint8_t unused1;  /**< Unused */
640    } extend;   /**< Extensions added in RFC 4884 for Time Exceeded and Destination Unreachable Messages */
641
642    uint32_t mtu;       /**< MTU from Packet Too Big Message */
643    uint32_t pointer;   /**< Pointer from Parameter Problem Message */
644    struct {
645        uint16_t id;    /**< Echo Identifier */
646        uint16_t sequence; /**< Echo Sequence Number */
647    } echo; /**< Data required for Echo Request and Reply messages */
648  } un;
649} PACKED libtrace_icmp6_t;
650
651/** Generic LLC/SNAP header structure */
652typedef struct libtrace_llcsnap
653{
654/* LLC */
655  uint8_t dsap;                 /**< Destination Service Access Point */
656  uint8_t ssap;                 /**< Source Service Access Point */
657  uint8_t control;              /**< Control field */
658/* SNAP */
659  LT_BITFIELD32 oui:24;         /**< Organisationally Unique Identifier (scope)*/
660  uint16_t type;                /**< Protocol within OUI */
661} PACKED libtrace_llcsnap_t;
662
663/** 802.3 frame */
664typedef struct libtrace_ether
665{
666  uint8_t ether_dhost[6];       /**< Destination Ether Addr */
667  uint8_t ether_shost[6];       /**< Source Ether Addr */
668  uint16_t ether_type;          /**< Packet Type ID Field (next-header) */
669} PACKED libtrace_ether_t;
670
671/** 802.1Q frame */
672typedef struct libtrace_8021q
673{
674  LT_BITFIELD16 vlan_pri:3;      /**< VLAN User Priority */
675  LT_BITFIELD16 vlan_cfi:1;      /**< VLAN Format Indicator,
676                                   * 0 for ethernet, 1 for token ring */
677  LT_BITFIELD16 vlan_id:12;      /**< VLAN Id */
678  uint16_t vlan_ether_type;      /**< VLAN Sub-packet Type ID Field
679                                   * (next-header)*/
680} PACKED libtrace_8021q_t;
681
682/** ATM User Network Interface (UNI) Cell. */
683typedef struct libtrace_atm_cell
684{
685  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
686  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
687  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
688  LT_BITFIELD32 pt:3;           /**< Payload Type */
689  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
690  LT_BITFIELD32 hec:8;          /**< Header Error Control */
691} PACKED libtrace_atm_cell_t;
692
693/** ATM Network Node/Network Interface (NNI) Cell. */
694typedef struct libtrace_atm_nni_cell
695{
696  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
697  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
698  LT_BITFIELD32 pt:3;           /**< Payload Type */
699  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
700  LT_BITFIELD32 hec:8;          /**< Header Error Control */
701} PACKED libtrace_atm_nni_cell_t;
702
703/** Captured UNI cell.
704 *
705 * Endace don't capture the HEC, presumably to keep alignment.  This
706 * version of the \ref libtrace_atm_cell is used when dealing with DAG
707 * captures of uni cells.
708 *
709 */
710typedef struct libtrace_atm_capture_cell
711{
712  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
713  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
714  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
715  LT_BITFIELD32 pt:3;           /**< Payload Type */
716  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
717} PACKED libtrace_atm_capture_cell_t;
718
719/** Captured NNI cell.
720 *
721 * Endace don't capture the HEC, presumably to keep alignment.  This
722 * version of the \ref libtrace_atm_nni_cell is used when dealing with DAG
723 * captures of nni cells.
724 *
725 */
726typedef struct libtrace_atm_nni_capture_cell
727{
728  LT_BITFIELD32 vpi:12;         /**< Virtual Path Identifier */
729  LT_BITFIELD32 vci:16;         /**< Virtual Channel Identifier */
730  LT_BITFIELD32 pt:3;           /**< Payload Type */
731  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
732  LT_BITFIELD32 hec:8;          /**< Header Error Control */
733} PACKED libtrace_atm_nni_capture_cell_t;
734
735/** PPP header */
736typedef struct libtrace_ppp
737{
738 /* I can't figure out where the hell these two variables come from. They're
739  * definitely not in RFC 1661 which defines PPP. Probably some weird thing
740  * relating to the lack of distinction between PPP, HDLC and CHDLC */
741       
742/* uint8_t address; */          /**< PPP Address (0xFF - All stations) */
743/* uint8_t header;  */          /**< PPP Control (0x03 - Unnumbered info) */
744 uint16_t protocol;             /**< PPP Protocol (htons(0x0021) - IPv4 */
745} PACKED libtrace_ppp_t;
746
747/** PPPoE header */
748typedef struct libtrace_pppoe
749{
750 LT_BITFIELD8 version:4;        /**< Protocol version number */
751 LT_BITFIELD8 type:4;           /**< PPPoE Type */
752 uint8_t code;                  /**< PPPoE Code */
753 uint16_t session_id;           /**< Session Identifier */
754 uint16_t length;               /**< Total Length of the PPP packet */
755} PACKED libtrace_pppoe_t;
756
757/** 802.11 header */
758typedef struct libtrace_80211_t {
759#if BYTE_ORDER == LITTLE_ENDIAN
760        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
761        LT_BITFIELD32      type:2;      /**< Frame Type */
762        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
763#else
764        LT_BITFIELD32      subtype:4;   /**< Frame Subtype */
765        LT_BITFIELD32      type:2;      /**< Frame Type */
766        LT_BITFIELD32      protocol:2;  /**< Protocol Version */
767#endif
768
769#if BYTE_ORDER == LITTLE_ENDIAN
770        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
771        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
772        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
773        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
774        LT_BITFIELD32      power:1;     /**< Power Management mode */
775        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
776        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
777        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
778#else
779        LT_BITFIELD32      order:1;     /**< Strictly-Ordered class indicator */
780        LT_BITFIELD32      wep:1;       /**< WEP encryption indicator */
781        LT_BITFIELD32      more_data:1; /**< More data is buffered at station */
782        LT_BITFIELD32      power:1;     /**< Power Management mode */
783        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
784        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
785        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
786        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
787#endif
788       
789        uint16_t     duration;  /**< Duration value for NAV calculation */
790        uint8_t      mac1[6];   /**< MAC Address 1 */
791        uint8_t      mac2[6];   /**< MAC Address 2 */
792        uint8_t      mac3[6];   /**< MAC Address 3 */
793        uint16_t     SeqCtl;    /**< Sequence Control */       
794        uint8_t      mac4[6];   /**< MAC Address 4 */
795} PACKED libtrace_80211_t;
796
797/** The Radiotap header pre-amble
798 *
799 * All Radiotap headers start with this pre-amble, followed by the fields
800 * specified in the it_present bitmask. If bit 31 of it_present is set, then
801 * another bitmask follows.
802 * @note All of the radiotap data fields are in little-endian byte-order.
803 */
804typedef struct libtrace_radiotap_t {
805    uint8_t     it_version; /**< Radiotap version */
806    uint8_t     it_pad; /**< Padding for natural alignment */
807    uint16_t    it_len; /**< Length in bytes of the entire Radiotap header */
808    uint32_t    it_present; /**< Which Radiotap fields are present */
809} PACKED libtrace_radiotap_t;
810
811/** OSPF header */
812typedef struct libtrace_ospf_v2_t
813{
814        uint8_t ospf_v;         /**< OSPF Version, should be 2 */
815        uint8_t type;           /**< OSPF Packet Type */
816        uint16_t len;           /**< Packet length, including OSPF header */
817        struct in_addr router;  /**< Router ID of the packet source */
818        struct in_addr area;    /**< Area the packet belongs to */
819        uint16_t sum;           /**< Checksum */
820        uint16_t au_type;       /**< Authentication procedure */
821        uint16_t zero;          /**< Always zero */
822        uint8_t au_key_id;      /**< Authentication Key ID */
823        uint8_t au_data_len;    /**< Authentication Data Length */
824        uint32_t au_seq_num;    /**< Cryptographic Sequence Number */
825} PACKED libtrace_ospf_v2_t;
826
827/** Options Field present in some OSPFv2 packets */
828typedef struct libtrace_ospf_options_t {
829#if BYTE_ORDER == LITTLE_ENDIAN
830        LT_BITFIELD8 unused1:1;
831        LT_BITFIELD8 e_bit:1;
832        LT_BITFIELD8 mc_bit:1;
833        LT_BITFIELD8 np_bit:1;
834        LT_BITFIELD8 ea_bit:1;
835        LT_BITFIELD8 dc_bit:1;
836        LT_BITFIELD8 unused2:2;
837#elif BYTE_ORDER == BIG_ENDIAN
838        LT_BITFIELD8 unused2:2;
839        LT_BITFIELD8 dc_bit:1;
840        LT_BITFIELD8 ea_bit:1;
841        LT_BITFIELD8 np_bit:1;
842        LT_BITFIELD8 mc_bit:1;
843        LT_BITFIELD8 e_bit:1;
844        LT_BITFIELD8 unused1:1;
845#endif
846} PACKED libtrace_ospf_options_t;
847
848/** LSA Header for OSPFv2 */
849typedef struct libtrace_ospf_lsa_v2_t
850{
851        uint16_t age;           /**< Time in seconds since LSA originated */
852        libtrace_ospf_options_t lsa_options;    /**< Options */
853        uint8_t lsa_type;       /**< LSA type */
854        struct in_addr ls_id;   /**< Link State ID */
855        struct in_addr adv_router; /**< Router that originated this LSA */
856        uint32_t seq;           /**< LS sequence number */
857        uint16_t checksum;      /**< Checksum */
858        uint16_t length;        /**< Length of the LSA including LSA header */
859} PACKED libtrace_ospf_lsa_v2_t;
860
861/** OSPFv2 Hello Packet */
862typedef struct libtrace_ospf_hello_v2_t
863{
864        struct in_addr mask;    /**< Network mask for this interface */
865        uint16_t interval;      /**< Interval between Hello packets (secs) */
866        libtrace_ospf_options_t hello_options;  /**< Options */
867        uint8_t priority;       /**< Router Priority */
868        uint32_t deadint;       /**< Interval before declaring a router down */
869        struct in_addr designated;      /**< Designated router for the network */
870        struct in_addr backup;  /**< Backup designated router */
871
872        /** Neighbors follow from here, but there can be anywhere from 1 to N
873         * neighbors so I can't include that here */
874} PACKED libtrace_ospf_hello_v2_t;
875
876/** OSPFv2 Database Description packet */
877typedef struct libtrace_ospf_db_desc_v2_t
878{
879        uint16_t mtu;           /**< Interface MTU */
880        libtrace_ospf_options_t db_desc_options;        /**< Options */
881#if BYTE_ORDER == LITTLE_ENDIAN
882        LT_BITFIELD8 db_desc_ms:1;      /**< If set, this router is the master */
883        LT_BITFIELD8 db_desc_m:1;       /**< If set, more packets to follow */
884        LT_BITFIELD8 db_desc_i:1;       /**< If set, this is the first packet in sequence */
885        LT_BITFIELD8 zero:5;
886#elif BYTE_ORDER == BIG_ENDIAN
887        LT_BITFIELD8 zero:5;
888        LT_BITFIELD8 db_desc_i:1;       /**< If set, this is the first packet in sequence */
889        LT_BITFIELD8 db_desc_m:1;       /**< If set, more packets to follow */
890        LT_BITFIELD8 db_desc_ms:1;      /**< If set, this router is the master */
891#endif
892        uint32_t seq;           /**< Sequence number for DD packets */
893} PACKED libtrace_ospf_db_desc_v2_t;
894
895/** OSPF Link State Request Packet */
896typedef struct libtrace_ospf_ls_req_t
897{
898        uint32_t ls_type;       /**< Link State Type */
899        uint32_t ls_id;         /**< Link State Id */
900        uint32_t advertising_router;    /**< Advertising Router */
901} PACKED libtrace_ospf_ls_req_t;
902
903/** OSPF Link State Update Packet */
904typedef struct libtrace_ospf_ls_update_t
905{
906        uint32_t ls_num_adv;    /**< Number of LSAs in this packet */
907
908        /* Followed by LSAs, use API functions to access these */
909} PACKED libtrace_ospf_ls_update_t;
910
911/** OSPFv2 AS External LSA Body */
912typedef struct libtrace_ospf_as_external_lsa_t
913{
914        struct in_addr netmask; /**< Netmask for the destination */
915#if BYTE_ORDER == LITTLE_ENDIAN
916        LT_BITFIELD8 tos:7;
917        LT_BITFIELD8 e:1;       /**< If set, metric is Type 2. Else, Type 1 */
918#elif BYTE_ORDER == BIG_ENDIAN
919        LT_BITFIELD8 e:1;       /**< If set, metric is Type 2. Else, Type 1 */
920        LT_BITFIELD8 tos:7;
921#endif
922        uint8_t metric_a;       /**< Byte 1 of the Metric field */
923        uint8_t metric_b;       /**< Byte 2 of the Metric field */
924        uint8_t metric_c;       /**< Byte 3 of the Metric field */
925        struct in_addr forwarding;      /**< Forwarding address */
926        uint32_t external_tag;          /**< External Route Tag */
927} PACKED libtrace_ospf_as_external_lsa_v2_t;
928
929/** OSPFv2 Summary LSA Body */
930typedef struct libtrace_ospf_summary_lsa
931{
932        struct in_addr netmask; /**< Netmask for the destination */
933        uint8_t zero;           /**< Always zero */
934        uint8_t metric_a;       /**< Byte 1 of the Metric field */
935        uint8_t metric_b;       /**< Byte 2 of the Metric field */
936        uint8_t metric_c;       /**< Byte 3 of the Metric field */
937       
938} PACKED libtrace_ospf_summary_lsa_v2_t;
939
940/** OSPFv2 Network LSA Body */
941typedef struct libtrace_ospf_network_lsa_t
942{
943        struct in_addr netmask; /**< Netmask for the network */
944        /* Followed by IDs of attached routers */
945} PACKED libtrace_ospf_network_lsa_v2_t;
946
947/** OSPFv2 Router Link structure */
948typedef struct libtrace_ospf_link_t
949{
950        struct in_addr link_id;         /**< Object that link connects to */
951        struct in_addr link_data;       /**< Link Data field */
952        uint8_t type;                   /**< Link Type */
953        uint8_t num_tos;                /**< Number of TOS metrics */
954        uint16_t tos_metric;            /**< Cost of router link */
955} PACKED libtrace_ospf_link_v2_t;
956
957/** OSPFv2 Router LSA */
958typedef struct libtrace_ospf_router_lsa_t
959{
960#if BYTE_ORDER == LITTLE_ENDIAN
961        LT_BITFIELD8 b:1;       /**< Area Border Router Flag */
962        LT_BITFIELD8 e:1;       /**< External Router Flag */
963        LT_BITFIELD8 v:1;       /**< Virtual Endpoint Flag */
964        LT_BITFIELD8 zero:5;
965#elif BYTE_ORDER == BIG_ENDIAN
966        LT_BITFIELD8 zero:5;
967        LT_BITFIELD8 v:1;       /**< Virtual Endpoint Flag */
968        LT_BITFIELD8 e:1;       /**< External Router Flag */
969        LT_BITFIELD8 b:1;       /**< Area Border Router Flag */
970#endif
971        uint8_t zero2;          /**< Always zero */
972        uint16_t num_links;     /**< Number of links in LSA */
973} PACKED libtrace_ospf_router_lsa_v2_t;
974
975typedef enum {
976        TRACE_OSPF_HELLO = 1,           /**< OSPF Hello */
977        TRACE_OSPF_DATADESC = 2,        /**< OSPF Database Description */
978        TRACE_OSPF_LSREQ = 3,           /**< OSPF Link State Request */
979        TRACE_OSPF_LSUPDATE = 4,        /**< OSPF Link State Update */
980        TRACE_OSPF_LSACK = 5            /**< OSPF Link State Acknowledgement */
981} libtrace_ospf_types_t;
982
983typedef enum {
984        TRACE_OSPF_LS_ROUTER = 1,       /**< OSPF Router LSA */
985        TRACE_OSPF_LS_NETWORK = 2,      /**< OSPF Network LSA */
986        TRACE_OSPF_LS_SUMMARY = 3,      /**< OSPF Summary LSA */
987        TRACE_OSPF_LS_ASBR_SUMMARY = 4, /**< OSPF Summary LSA (ASBR) */
988        TRACE_OSPF_LS_EXTERNAL = 5      /**< OSPF AS External LSA */
989} libtrace_ospf_ls_types_t;
990
991/** A local definition of an SLL header */
992typedef struct libtrace_sll_header_t {
993        uint16_t pkttype;               /**< Packet type */
994        uint16_t hatype;                /**< Link-layer address type */
995        uint16_t halen;                 /**< Link-layer address length */
996        unsigned char addr[8];          /**< Link-layer address */
997        uint16_t protocol;              /**< Protocol */
998} libtrace_sll_header_t;
999
1000
1001/* SLL packet types */
1002
1003/** Packet was addressed for the local host */
1004#define TRACE_SLL_HOST          0
1005/** Packet was addressed for a broadcast address */
1006#define TRACE_SLL_BROADCAST     1
1007/** Packet was addressed for a multicast address */
1008#define TRACE_SLL_MULTICAST     2
1009/** Packet was addressed for another host but was captured by a promiscuous
1010 * device */
1011#define TRACE_SLL_OTHERHOST     3
1012/** Packet originated from the local host */
1013#define TRACE_SLL_OUTGOING      4
1014
1015
1016#ifdef WIN32
1017#pragma pack(pop)
1018#endif
1019
1020
1021/*@}*/
1022
1023/** Prints help information for libtrace
1024 *
1025 * Function prints out some basic help information regarding libtrace,
1026 * and then prints out the help() function registered with each input module
1027 */
1028DLLEXPORT void trace_help(void);
1029
1030/** Causes a libtrace reader to stop blocking whilst waiting on new packets
1031 * and immediately return EOF.
1032 *
1033 * This function is useful if you are handling signals within your libtrace
1034 * program. If a live source is not receiving any packets (or they are being
1035 * filtered), a call to trace_read_packet will result in an infinite loop as
1036 * it will block until a packet is received. Normally, a SIGINT would cause the
1037 * program to end and thus break the loop, but if you are handling the signal
1038 * yourself then that signal will never reach libtrace.
1039 *
1040 * Instead this function sets a global variable within libtrace that will
1041 * cause a blocking live capture to break on the next internal timeout,
1042 * allowing control to be returned to the user and their own signal handling
1043 * to kick in.
1044 */
1045DLLEXPORT void trace_interrupt(void);
1046
1047/** @name Trace management
1048 * These members deal with creating, configuring, starting, pausing and
1049 * cleaning up a trace object
1050 *@{
1051 */
1052
1053/** Takes a uri and splits it into a format and uridata component.
1054 * @param uri           The uri to be parsed
1055 * @param [out] format  A pointer that will be updated to point to an allocated
1056 *                      string holding the format component of the URI
1057 * @return NULL if an error occured, otherwise return a pointer to the uridata
1058 * component
1059 *
1060 * @note The format component is strdup'd by this function, so be sure to free
1061 * it when you are done with the split URI. Similarly, do not pass a pointer
1062 * to an allocated string into this function as the 'format' parameter, as
1063 * that memory will be leaked and replaced with the strdup'd format.
1064 */
1065DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
1066
1067/** Create an input trace from a URI
1068 *
1069 * @param uri A valid libtrace URI to be opened
1070 * @return An opaque pointer to a libtrace_t
1071 *
1072 * Some valid URI's are:
1073 *  - erf:/path/to/erf/file
1074 *  - erf:-  (stdin)
1075 *  - dag:/dev/dagcard                 
1076 *  - pcapint:pcapinterface                (eg: pcap:eth0)
1077 *  - pcap:/path/to/pcap/file
1078 *  - pcap:-
1079 *  - rt:hostname
1080 *  - rt:hostname:port
1081 *
1082 *  If an error occured when attempting to open the trace file, a
1083 *  trace is still returned so trace_is_err() should be called to find out
1084 *  if an error occured. The trace is created in the configuration state, you
1085 *  must call trace_start before attempting to read packets from the trace.
1086 */
1087DLLEXPORT libtrace_t *trace_create(const char *uri);
1088
1089/** Creates a "dummy" trace file that has only the format type set.
1090 *
1091 * @param uri A valid (but fake) URI indicating the format of the dummy trace that is to be created.
1092 * @return An opaque pointer to a (sparsely initialised) libtrace_t
1093 *
1094 * Only the format portion of the uri parameter matters - the 'file' being
1095 * opened does not have to exist.
1096 *
1097 * @note IMPORTANT: Do not attempt to call trace_read_packet or other such
1098 * functions with the dummy trace. Its intended purpose is to provide access
1099 * to the format functions where the original trace may no longer exist or is
1100 * not the correct format, e.g. reading ERF packets from an RT input.
1101 */
1102DLLEXPORT libtrace_t *trace_create_dead(const char *uri);
1103
1104/** Creates a trace output file from a URI.
1105 *
1106 * @param uri The uri string describing the output format and destination
1107 * @return An opaque pointer to a libtrace_output_t
1108 *
1109 * Valid URIs include:
1110 *  - erf:/path/to/erf/file
1111 *  - pcap:/path/to/pcap/file
1112 *
1113 *  If an error occured when attempting to open the output trace, a trace is
1114 *  still returned but trace_errno will be set. Use trace_is_err_out() and
1115 *  trace_perror_output() to get more information.
1116 */
1117DLLEXPORT libtrace_out_t *trace_create_output(const char *uri);
1118
1119/** Start an input trace
1120 * @param libtrace      The trace to start
1121 * @return 0 on success, -1 on failure
1122 *
1123 * This does the actual work of starting the input trace and applying
1124 * all the config options.  This may fail, returning -1. The libtrace error
1125 * handling functions can be used to get more information about what
1126 * specifically went wrong.
1127 */
1128DLLEXPORT int trace_start(libtrace_t *libtrace);
1129
1130/** Pauses an input trace
1131 * @param libtrace      The trace to pause
1132 * @return 0 on success, -1 on failure
1133 *
1134 * This stops an input trace that is in progress and returns you to the
1135 * configuration state.  Any packets that arrive on a live capture after
1136 * trace_pause() has been called will be discarded.  To resume the trace, call
1137 * trace_start().
1138 */
1139DLLEXPORT int trace_pause(libtrace_t *libtrace);
1140
1141/** Start an output trace
1142 * @param libtrace      The trace to start
1143 * @return 0 on success, -1 on failure
1144 *
1145 * This does the actual work with starting a trace capable of writing packets. 
1146 * This generally creates the output file.
1147 */
1148DLLEXPORT int trace_start_output(libtrace_out_t *libtrace);
1149
1150/** Valid configuration options for input traces */
1151typedef enum {
1152        /** Maximum number of bytes to be captured for any given packet */
1153        TRACE_OPTION_SNAPLEN,   
1154
1155        /** If enabled, places a live capture interface into promiscuous mode */
1156        TRACE_OPTION_PROMISC,   
1157
1158        /** Apply this filter to all packets read from this trace */
1159        TRACE_OPTION_FILTER,   
1160
1161        /** Defines the frequency of meta-data reporting, e.g. DUCK packets */
1162        TRACE_OPTION_META_FREQ,
1163
1164        /** If enabled, the libtrace event API will ignore time gaps between
1165         * packets when reading from a trace file */
1166        TRACE_OPTION_EVENT_REALTIME
1167} trace_option_t;
1168
1169/** Sets an input config option
1170 * @param libtrace      The trace object to apply the option to
1171 * @param option        The option to set
1172 * @param value         The value to set the option to
1173 * @return -1 if option configuration failed, 0 otherwise
1174 * This should be called after trace_create, and before trace_start
1175 */
1176DLLEXPORT int trace_config(libtrace_t *libtrace,
1177                trace_option_t option,
1178                void *value);
1179
1180/** Valid compression types
1181 * Note, this must be kept in sync with WANDIO_COMPRESS_* numbers in wandio.h
1182 */
1183typedef enum {
1184        TRACE_OPTION_COMPRESSTYPE_NONE = 0, /**< No compression */
1185        TRACE_OPTION_COMPRESSTYPE_ZLIB = 1, /**< GZip Compression */
1186        TRACE_OPTION_COMPRESSTYPE_BZ2  = 2, /**< BZip2 Compression */
1187        TRACE_OPTION_COMPRESSTYPE_LZO  = 3  /**< LZO Compression */
1188} trace_option_compresstype_t;
1189
1190/** Valid configuration options for output traces */
1191typedef enum {
1192        /** File flags to use when opening an output file, e.g. O_APPEND */
1193        TRACE_OPTION_OUTPUT_FILEFLAGS,
1194        /** Compression level: 0 = no compression, 1 = faster compression,
1195         * 9 = better compression */
1196        TRACE_OPTION_OUTPUT_COMPRESS,
1197        /** Compression type, see trace_option_compresstype_t */
1198        TRACE_OPTION_OUTPUT_COMPRESSTYPE
1199} trace_option_output_t;
1200
1201/** Sets an output config option
1202 *
1203 * @param libtrace      The output trace object to apply the option to
1204 * @param option        The option to set
1205 * @param value         The value to set the option to
1206 * @return -1 if option configuration failed, 0 otherwise
1207 * This should be called after trace_create_output, and before
1208 * trace_start_output
1209 */
1210DLLEXPORT int trace_config_output(libtrace_out_t *libtrace,
1211                trace_option_output_t option,
1212                void *value
1213                );
1214
1215/** Close an input trace, freeing up any resources it may have been using
1216 *
1217 * @param trace         The input trace to be destroyed
1218 *
1219 */
1220DLLEXPORT void trace_destroy(libtrace_t *trace);
1221
1222/** Close a dummy trace file, freeing up any resources it may have been using
1223 * @param trace         The dummy trace to be destroyed
1224 */
1225DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
1226
1227/** Close an output trace, freeing up any resources it may have been using
1228 * @param trace         The output trace to be destroyed
1229 */
1230DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
1231
1232/** Check (and clear) the current error state of an input trace
1233 * @param trace         The input trace to check the error state on
1234 * @return The current error status and message
1235 *
1236 * This reads and returns the current error state and sets the current error
1237 * to "no error".
1238 */
1239DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
1240
1241/** Indicate if there has been an error on an input trace
1242 * @param trace         The input trace to check the error state on
1243 * @return true if an error has occurred, false otherwise
1244 *
1245 * @note This does not clear the error status, and only returns true or false.
1246 */
1247DLLEXPORT bool trace_is_err(libtrace_t *trace);
1248
1249/** Outputs the error message for an input trace to stderr and clear the error
1250 * status.
1251 * @param trace         The input trace with the error to output
1252 * @param msg           The message to prepend to the error
1253 *
1254 * This function does clear the error status.
1255 */
1256DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...) PRINTF(2,3);
1257
1258/** Checks (and clears) the current error state for an output trace
1259 * @param trace         The output trace to check the error state on
1260 * @return The current error status and message
1261 *
1262 * This reads and returns the current error state and sets the current error
1263 * to "no error".
1264 */
1265DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
1266
1267/** Indicates if there is an error on an output trace
1268 * @param trace         The output trace to check the error state on
1269 * @return true if an error has occurred, false otherwise.
1270 *
1271 * This does not clear the error status, and only returns true or false.
1272 */
1273DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
1274
1275/** Outputs the error message for an output trace to stderr and clear the error
1276 * status.
1277 * @param trace         The output trace with the error to output
1278 * @param msg           The message to prepend to the error
1279 * This function does clear the error status.
1280 */
1281DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...)
1282        PRINTF(2,3);
1283
1284/** Returns the number of packets observed on an input trace.
1285 * Includes the number of packets counted as early as possible, before
1286 * filtering, and includes dropped packets.
1287 *
1288 * @param trace         The input trace to examine
1289 * @returns The number of packets seen at the capture point before filtering.
1290 *
1291 * If the number is not known, this function will return UINT64_MAX
1292 */
1293DLLEXPORT
1294uint64_t trace_get_received_packets(libtrace_t *trace);
1295
1296/** Returns the number of packets that were captured, but discarded for not
1297 * matching a provided filter.
1298 *
1299 * @param trace         The input trace to examine
1300 * @returns The number of packets that were successfully captured, but filtered
1301 *
1302 * If the number is not known, this function will return UINT64_MAX
1303 */
1304DLLEXPORT
1305uint64_t trace_get_filtered_packets(libtrace_t *trace);
1306
1307/** Returns the number of packets that have been dropped on an input trace due
1308 * to lack of buffer space on the capturing device.
1309 *
1310 * @param trace         The input trace to examine
1311 * @returns The number of packets captured, but dropped due to buffer overruns
1312 *
1313 * If the number is not known, this function will return UINT64_MAX
1314 */
1315DLLEXPORT
1316uint64_t trace_get_dropped_packets(libtrace_t *trace);
1317
1318/** Returns the number of packets that have been read from the input trace using
1319 * trace_read_packet().
1320 *
1321 * @param trace         The input trace to examine
1322 * @returns The number of packets that have been read by the libtrace user.
1323 *
1324 * If the number is not known, this function will return UINT64_MAX
1325 */
1326DLLEXPORT
1327uint64_t trace_get_accepted_packets(libtrace_t *trace);
1328
1329
1330/*@}*/
1331
1332/** @name Reading / Writing packets
1333 * These members deal with creating, reading and writing packets
1334 *
1335 * @{
1336 */
1337
1338/** Create a new packet object
1339 *
1340 * @return A pointer to an initialised libtrace_packet_t object
1341 */
1342DLLEXPORT libtrace_packet_t *trace_create_packet(void);
1343
1344/** Copy a packet object
1345 * @param packet        The source packet to copy
1346 * @return A new packet which has the same content as the source packet
1347 *
1348 * @note This always involves a copy, which can be slow.  Use of this
1349 * function should be avoided where possible.
1350 *
1351 * @par The reason you would want to use this function is that a zerocopied
1352 * packet from a device will be stored using memory owned by the device which
1353 * may be a limited resource. Copying the packet will ensure that the packet
1354 * is now stored in memory owned and managed by libtrace.
1355 */
1356DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
1357
1358/** Destroy a packet object
1359 * @param packet        The packet to be destroyed
1360 *
1361 */
1362DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
1363
1364
1365/** Read the next packet from an input trace
1366 *
1367 * @param trace         The libtrace opaque pointer for the input trace
1368 * @param packet        The packet opaque pointer
1369 * @return 0 on EOF, negative value on error, number of bytes read when successful.
1370 *
1371 * @note The number of bytes read is usually (but not always) the same as
1372 * trace_get_framing_length()+trace_get_capture_length() depending on the
1373 * trace format.
1374 *
1375 * @note The trace must have been started with trace_start before calling
1376 * this function
1377 *
1378 * @note When reading from a live capture, this function will block until a
1379 * packet is observed on the capture interface. The libtrace event API
1380 * (e.g. trace_event()) should be used if non-blocking operation is required.
1381 */
1382DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
1383
1384/** Event types
1385 * see \ref libtrace_eventobj_t and \ref trace_event
1386 */
1387typedef enum {
1388        TRACE_EVENT_IOWAIT,     /**< Wait on the given file descriptor */
1389        TRACE_EVENT_SLEEP,      /**< Sleep for the given amount of time */
1390        TRACE_EVENT_PACKET,     /**< Packet has been read from input trace */
1391        TRACE_EVENT_TERMINATE   /**< End of input trace */
1392} libtrace_event_t;
1393
1394/** Structure returned by libtrace_event explaining what the current event is */
1395typedef struct libtrace_eventobj_t {
1396        libtrace_event_t type; /**< Event type (iowait,sleep,packet) */
1397       
1398        /** If the event is IOWAIT, the file descriptor to wait on */
1399        int fd;               
1400        /** If the event is SLEEP, the amount of time to sleep for in seconds */
1401        double seconds;       
1402        /** If the event is PACKET, the value returned by trace_read_packet() */
1403        int size;
1404} libtrace_eventobj_t;
1405
1406/** Processes the next libtrace event from an input trace.
1407 * @param trace The libtrace opaque pointer for the input trace
1408 * @param packet The libtrace_packet opaque pointer to use for reading packets
1409 * @return A libtrace_event struct containing the event type and details of the event.
1410 *
1411 * Type can be:
1412 *  TRACE_EVENT_IOWAIT  Waiting on I/O on a file descriptor
1413 *  TRACE_EVENT_SLEEP   Wait a specified amount of time for the next event
1414 *  TRACE_EVENT_PACKET  Packet was read from the trace
1415 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
1416 */
1417DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
1418                libtrace_packet_t *packet);
1419
1420
1421/** Write one packet out to the output trace
1422 *
1423 * @param trace         The libtrace_out opaque pointer for the output trace
1424 * @param packet        The packet opaque pointer of the packet to be written
1425 * @return The number of bytes written out, if zero or negative then an error has occured.
1426 */
1427DLLEXPORT int trace_write_packet(libtrace_out_t *trace, libtrace_packet_t *packet);
1428
1429/** Gets the capture format for a given packet.
1430 * @param packet        The packet to get the capture format for.
1431 * @return The capture format of the packet
1432 *
1433 * @note Due to ability to convert packets between formats relatively easily
1434 * in Libtrace, the format of the packet right now may not be the format that
1435 * the packet was originally captured with.
1436 */
1437DLLEXPORT
1438enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1439
1440/** Construct a libtrace packet from a buffer containing the packet payload.
1441 * @param[in,out] packet        Libtrace Packet object to update with the new
1442 *                              data.
1443 * @param linktype              The linktype of the packet data.
1444 * @param[in] data              The packet data (including linklayer).
1445 * @param len                   Length of packet data provided in the buffer.
1446 *
1447 * @note The constructed packet will be in the PCAP format.
1448 *
1449 * @note To be useful, the provided buffer must start with the layer 2 header
1450 * (or a metadata header, if desired).
1451 */
1452DLLEXPORT
1453void trace_construct_packet(libtrace_packet_t *packet,
1454                libtrace_linktype_t linktype, const void *data, uint16_t len);
1455
1456/*@}*/
1457
1458/** @name Protocol decodes
1459 * These functions locate and return a pointer to various headers inside a
1460 * packet
1461 *
1462 * A packet is divided up into several "layers.":
1463 *
1464 * @li Framing header -- This is the header provided by the capture format
1465 * itself rather than anything that was sent over the network. This provides
1466 * basic details about the packet record including capture lengths, wire
1467 * lengths, timestamps, direction information and any other metadata that is
1468 * part of the capture format. 
1469 *
1470 * @li Metadata header (optional) -- A header containing metadata about a
1471 * packet that was captured, but the metadata was not transmitted over the
1472 * wire.  Some examples include RadioTap and Linux_sll headers.  This can be
1473 * retrieved by trace_get_packet_meta(), or skipped using
1474 * trace_get_payload_from_meta(). There may be multiple "metadata" headers on
1475 * a packet.
1476 *
1477 * @li Layer 2/Link layer/Datalink header -- This can be retrieved by
1478 * trace_get_layer2(), or skipped using trace_get_payload_from_layer2().
1479 *
1480 * @li Layer 3/IP/IPv6 -- This can be retrieved by trace_get_layer3().  As a
1481 * convenience trace_get_ip()/trace_get_ip6() can be used to find an IPv4/IPv6
1482 * header.
1483 *
1484 * @li Layer 5/transport -- These are protocols carried in IPv4/IPv6 frames.
1485 * These can be retrieved using trace_get_transport().
1486 *
1487 * @{
1488 */
1489
1490
1491/** Gets a pointer to the first byte of the packet as it was captured and
1492 * returns its corresponding linktype and capture length.
1493 *
1494 * Use this function instead of the deprecated trace_get_link().
1495 *
1496 * @param packet The packet to get the buffer from
1497 * @param[out] linktype The linktype of the returned pointer
1498 * @param[out] remaining The capture length (the number of captured bytes from
1499 * the returned pointer)
1500 * @return A pointer to the first byte of the packet
1501 */
1502DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet,
1503                libtrace_linktype_t *linktype, uint32_t *remaining);
1504
1505/** Get a pointer to the link layer for a given packet
1506 * @param packet        The packet to get the link layer for
1507 *
1508 * @return A pointer to the link layer, or NULL if there is no link layer
1509 *
1510 * @deprecated This function is deprecated: Use trace_get_packet_buffer() or
1511 * one of the trace_get_layer*() functions instead.
1512 * @note You should call trace_get_link_type to find out what type of link
1513 * layer has been returned to you.
1514 */
1515DLLEXPORT SIMPLE_FUNCTION DEPRECATED
1516void *trace_get_link(const libtrace_packet_t *packet);
1517
1518/** Get a pointer to the IPv4 header (if any) for a given packet
1519 * @param packet        The packet to get the IPv4 header for
1520 *
1521 * @return A pointer to the IPv4 header, or NULL if there is no IPv4 header
1522 *
1523 * If a partial IP header is present, i.e. the packet has been truncated before
1524 * the end of the IP header, this function will return NULL.
1525 *
1526 * You should consider using \ref trace_get_layer3 instead of this function.
1527 */
1528DLLEXPORT SIMPLE_FUNCTION
1529libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
1530
1531/** get a pointer to the IPv6 header (if any)
1532 * @param packet        The packet to get the IPv6 header for
1533 *
1534 * @return A pointer to the IPv6 header, or NULL if there is no IPv6 header
1535 *
1536 * If a partial IPv6 header is present, i.e. the packet has been truncated
1537 * before the end of the IP header, this function will return NULL.
1538 *
1539 * You should consider using \ref trace_get_layer3 instead of this function.
1540 */
1541DLLEXPORT SIMPLE_FUNCTION
1542libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet);
1543
1544/** Return a pointer to the first metadata header in a packet, if present.
1545 *
1546 * This function takes a pointer to a libtrace packet and if any metadata
1547 * headers exist, returns a pointer to the first one, along with its
1548 * corresponding linktype.
1549 *
1550 * If no metadata headers exist in the packet, NULL is returned.
1551 *
1552 * A metadata header is a header that was prepended by the capturing device,
1553 * such as a Linux SLL header, or a Radiotap wireless monitoring header.
1554 * Subsequent metadata headers may be accessed with the
1555 * trace_get_payload_from_meta(...) function.
1556 *
1557 * @param packet The libtrace packet
1558 * @param[out] linktype The linktype of the returned metadata header
1559 * @param[out] remaining The number of bytes captured after the returned
1560 * pointer.
1561 * @return A pointer to the first metadata header, or NULL if there are no
1562 * metadata headers present.
1563 *
1564 * remaining may be NULL, however linktype must be provided.
1565 */
1566DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet,
1567                libtrace_linktype_t *linktype,
1568                uint32_t *remaining);
1569
1570/** Returns the payload of a metadata header.
1571 *
1572 * This function takes a pointer to the start of a metadata header (either
1573 * obtained via trace_get_packet_meta(...) or by a previous call to
1574 * trace_get_payload_from_meta(...)) along with its corresponding linktype and
1575 * returns the payload, i.e. the next header. It will also update the linktype
1576 * parameter to indicate the type of payload.
1577 * 
1578 * If the linktype indicates that the header passed in is not a metadata
1579 * header, the function returns NULL to indicate this. The linktype remains
1580 * unchanged in this case.
1581 *
1582 * This function allows the user to iterate through metadata headers which are
1583 * sometimes present before the actual packet as it was received on the wire.
1584 * Examples of metadata headers include the Linux SLL header and the Radiotap
1585 * wireless monitoring header.
1586 *
1587 * If the metadata header passed into this function is truncated, this
1588 * function will return NULL, and remaining will be set to 0.
1589 *
1590 * If there are 0 bytes of payload following the provided metadata header, the
1591 * function will return a pointer to where the header would otherwise be and
1592 * remaining will be 0.
1593 *
1594 * Therefore, be sure to check the value of remaining after calling this
1595 * function!
1596 *
1597 * @param[in] meta A pointer to a metadata header
1598 * @param[in,out] linktype The linktype of meta (updated to indicate the
1599 * linktype of the returned header if applicable).
1600 * @param[in,out] remaining The number of bytes after the meta pointer.
1601 * @return A pointer to the payload of the metadata header. If meta is not a
1602 * pointer to a metadata header, NULL is returned and linktype remains
1603 * unchanged.
1604 *
1605 * All parameters are mandatory.
1606 */
1607DLLEXPORT void *trace_get_payload_from_meta(const void *meta,
1608                libtrace_linktype_t *linktype,
1609                uint32_t *remaining);
1610
1611
1612/** Get a pointer to the layer 2 header. Generally this is the first byte of the
1613 * packet as it was seen on the wire.
1614 *
1615 * This function takes a libtrace packet and skips over any metadata headers if
1616 * present (such as Linux SLL or Radiotap) and returns a pointer to the first
1617 * byte of the packet that was actually received by the network interface.
1618 *
1619 * @param packet The libtrace packet to find the layer 2 header for
1620 * @param[out] linktype The linktype of the returned layer 2 header
1621 * @param[out] remaining The number of bytes left in the packet after the
1622 * returned pointer.
1623 * @return A pointer to the first byte of the packet as it was seen on the
1624 * wire. If no layer 2 header is present, this function will return NULL.
1625 *
1626 * remaining may be NULL, otherwise it will be filled in by the function.
1627 */
1628DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet,
1629                libtrace_linktype_t *linktype,
1630                uint32_t *remaining);
1631
1632/** Gets a pointer to the next header following a layer 2 header
1633 *
1634 * @param l2                    The pointer to the current layer 2 header
1635 * @param linktype              The type of the layer 2 header
1636 * @param[out] ethertype        An optional output variable of the ethernet type of the new header
1637 * @param[in,out] remaining     Updated with the number of captured bytes
1638                                remaining
1639 *
1640 * @return A pointer to the header following the provided layer 2 header, or
1641 * NULL if no subsequent header is present.
1642 *
1643 * Remaining must point to the number of bytes captured from the layer 2 header
1644 * and beyond.  It will be decremented by the number of bytes skipped to find
1645 * the payload.
1646 *
1647 * If the layer 2 header is complete but there are zero bytes of payload after
1648 * the end of the header, a pointer to where the payload would be is returned
1649 * and remaining will be set to 0.  If the layer 2 header is incomplete
1650 * (truncated), then NULL is returned and remaining will be set to 0.
1651 * Therefore, it is very important to check the value of remaining after
1652 * calling this function.
1653 *
1654 */
1655DLLEXPORT void *trace_get_payload_from_layer2(void *l2,
1656                libtrace_linktype_t linktype,
1657                uint16_t *ethertype,
1658                uint32_t *remaining);
1659
1660
1661/** Get a pointer to the layer 3 (e.g. IP) header.
1662 * @param packet  The libtrace packet to find the layer 3 header for
1663 * @param[out] ethertype The ethertype of the layer 3 header
1664 * @param[out] remaining The amount of captured data remaining in the packet starting from the returned pointer, i.e. including the layer 3 header.
1665 *
1666 * @return A pointer to the layer 3 header. If no layer 3 header is present in
1667 * the packet, NULL is returned. If the layer 3 header is truncated, a valid
1668 * pointer will still be returned so be sure to check the value of remaining
1669 * before attempting to process the returned header.
1670 *
1671 * remaining may be NULL, otherwise it will be set to the number of captured
1672 * bytes after the pointer returned.
1673 */
1674DLLEXPORT
1675void *trace_get_layer3(const libtrace_packet_t *packet,
1676                uint16_t *ethertype, uint32_t *remaining);
1677
1678/** Calculates the expected IP checksum for a packet.
1679 * @param packet        The libtrace packet to calculate the checksum for
1680 * @param[out] csum     The checksum that is calculated by this function. This
1681 *                      may not be NULL.
1682 *
1683 * @return A pointer to the original checksum field within the IP
1684 * header. If the checksum field is not present in the packet, NULL is returned.
1685 *
1686 * @note The return value points to the checksum that exists within the current
1687 * packet. The value in csum is the value that the checksum should be, given
1688 * the current packet contents. 
1689 *
1690 * @note This function involves the use of a memcpy, so be careful about
1691 * calling it excessively if performance is a concern for you.
1692 *
1693 * New in libtrace 3.0.17
1694 */
1695DLLEXPORT uint16_t *trace_checksum_layer3(libtrace_packet_t *packet,
1696                uint16_t *csum);
1697
1698/** Calculates the expected checksum for the transport header in a packet.
1699 * @param packet        The libtrace packet to calculate the checksum for
1700 * @param[out] csum     The checksum that is calculated by this function. This
1701 *                      may not be NULL.
1702 *
1703 * @return A pointer to the original checksum field within the transport
1704 * header. If the checksum field is not present in the packet, NULL is returned.
1705 *
1706 * @note The return value points to the checksum that exists within the current
1707 * packet. The value in csum is the value that the checksum should be, given
1708 * the current packet contents. 
1709 *
1710 * @note This function involves the use of a memcpy, so be careful about
1711 * calling it excessively if performance is a concern for you.
1712 *
1713 * @note Because transport checksums are calculated across the entire payload,
1714 * truncated packets will result in NULL being returned.
1715 *
1716 * This function will determine the appropriate checksum for whatever transport
1717 * layer header is present in the provided packet. At this stage, this only
1718 * currently works for TCP, UDP and ICMP packets.
1719 *
1720 * Be wary of TCP checksum offloading if you are examining the checksum of
1721 * packets captured on the same host that generated them!
1722 *
1723 * New in libtrace 3.0.17
1724 */
1725DLLEXPORT uint16_t *trace_checksum_transport(libtrace_packet_t *packet,
1726                uint16_t *csum);
1727
1728/** Gets a pointer to the transport layer header (if any)
1729 * @param packet   The libtrace packet to find the transport header for
1730 * @param[out] proto    The protocol present at the transport layer.
1731 * @param[out] remaining The amount of captured data remaining in the packet
1732 * starting from the returned pointer, i.e. including the transport header.
1733 *
1734 * @return A pointer to the transport layer header. If no transport header is
1735 * present in the packet, NULL is returned. If the transport header is
1736 * truncated, a valid pointer will still be returned so be sure to check the
1737 * value of remaining before attempting to process the returned header.
1738 *
1739 * remaining may be NULL, otherwise it will be set to the number of captured
1740 * bytes after the returned pointer.
1741 *
1742 * @note proto may be NULL if proto is unneeded.
1743 */
1744DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet,
1745                uint8_t *proto, uint32_t *remaining);
1746
1747/** Gets a pointer to the payload following an IPv4 header
1748 * @param ip            The IPv4 Header
1749 * @param[out] proto    The protocol of the header following the IPv4 header
1750 * @param[in,out] remaining Updated with the number of captured bytes remaining
1751 *
1752 * @return A pointer to the transport layer header, or NULL if no subsequent
1753 * header is present.
1754 *
1755 * When calling this function, remaining must contain the number of captured
1756 * bytes remaining in the packet starting from the IPv4 header (including the
1757 * IPv4 header itself).
1758 *
1759 * remaining will be decremented by the size of the IPv4 header (including any
1760 * options). If the IPv4 header is complete but there are zero bytes of payload
1761 * after the IPv4 header, a pointer to where the payload would be is returned
1762 * and remaining will be set to 0.  If the IPv4 header is incomplete, NULL will
1763 * be returned and remaining will be set to 0. Therefore, it is very important
1764 * to check the value of remaining after calling this function.
1765 *
1766 * proto may be NULL, in which case it won't be updated.
1767 *
1768 * @note This is similar to trace_get_transport_from_ip in libtrace2
1769 */
1770DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
1771                uint32_t *remaining);
1772
1773/** Gets a pointer to the payload following an IPv6 header
1774 * @param ipptr         The IPv6 Header
1775 * @param[out] proto    The protocol of the header following the IPv6 header
1776 * @param[in,out] remaining Updated with the number of captured bytes remaining
1777 *
1778 * @return A pointer to the transport layer header, or NULL if no subsequent
1779 * header is present.
1780 *
1781 * When calling this function, remaining must contain the number of captured
1782 * bytes remaining in the packet starting from the IPv6 header (including the
1783 * IPv6 header itself).
1784 *
1785 * remaining will be decremented by the size of the IPv6 header (including any
1786 * options). If the IPv6 header is complete but there are zero bytes of payload
1787 * after the IPv6 header, a pointer to where the payload would be is returned
1788 * and remaining will be set to 0.  If the IPv6 header is incomplete, NULL will
1789 * be returned and remaining will be set to 0. Therefore, it is very important
1790 * to check the value of remaining after calling this function.
1791 *
1792 * proto may be NULL, in which case it won't be updated.
1793 *
1794 */
1795DLLEXPORT void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr,
1796                uint8_t *proto, uint32_t *remaining);
1797
1798/** Gets a pointer to the payload following a link header
1799 * @param linkptr       A pointer to the link layer header
1800 * @param linktype      The linktype of the link header being examined
1801 * @param[out] type     An output variable for the ethernet type
1802 * @param[in,out] remaining Updated with the number of captured bytes remaining
1803 *
1804 * @return A pointer to the header following the link header, or NULL if no
1805 * subsequent header is present.
1806 *
1807 * When calling this function, remaining must contain the number of captured
1808 * bytes remaining in the packet starting from the link header (including the
1809 * link header itself). remaining will be updated to contain the number of
1810 * bytes remaining after the link header has been skipped.
1811 *
1812 * @deprecated This function is deprecated: you should be using
1813 * trace_get_payload_from_layer2() or trace_get_payload_from_meta() instead.
1814 *
1815 */
1816DLLEXPORT void *trace_get_payload_from_link(void *linkptr,
1817                libtrace_linktype_t linktype,
1818                uint16_t *type, uint32_t *remaining);
1819
1820/** Gets a pointer to the payload following an 802.1q (VLAN) header.
1821 * @param vlan      A pointer to the VLAN header
1822 * @param[out] type  The ethernet type, replaced with the VLAN ether type
1823 * @param[in,out] remaining Updated with the number of captured bytes remaining
1824 *
1825 * @return A pointer to the header beyond the VLAN header, if one is present.
1826 * Otherwise, returns NULL. 
1827 *
1828 * When calling this function, remaining must contain the number of captured
1829 * bytes remaining in the packet starting from the VLAN header (including the
1830 * VLAN header itself). remaining will be updated to contain the number of
1831 * bytes remaining after the VLAN header has been skipped.
1832 *
1833 * If the VLAN header is complete but there are zero bytes of payload after
1834 * the VLAN header, a pointer to where the payload would be is returned and
1835 * remaining will be set to 0.  If the VLAN header is incomplete, NULL will be
1836 * returned and remaining will be set to 0. Therefore, it is important to check
1837 * the value of remaining after calling this function.
1838 *
1839 * type will be set to the ethertype of the VLAN payload. This parameter is not
1840 * mandatory, but is highly recommended.
1841 *
1842 */
1843DLLEXPORT void *trace_get_payload_from_vlan(
1844                void *vlan, uint16_t *type, uint32_t *remaining);
1845
1846/** Gets a pointer to the payload following an MPLS header.
1847 * @param mpls      A pointer to the MPLS header
1848 * @param[out] type  The ethernet type, replaced by the ether type of the
1849 * returned header - 0x0000 if an Ethernet header is returned
1850 * @param[in,out] remaining Updated with the number of captured bytes remaining
1851 *
1852 * @return A pointer to the header beyond the MPLS label, if one is present.
1853 * Will return NULL if there is not enough bytes remaining to skip past the
1854 * MPLS label.
1855 *
1856 * When calling this function, remaining must contain the number of captured
1857 * bytes remaining in the packet starting from the MPLS header (including the
1858 * MPLS header itself). remaining will be updated to contain the number of
1859 * bytes remaining after the MPLS header has been skipped.
1860 *
1861 * If the MPLS header is complete but there are zero bytes of payload after
1862 * the MPLS header, a pointer to where the payload would be is returned and
1863 * remaining will be set to 0.  If the MPLS header is incomplete, NULL will be
1864 * returned and remaining will be set to 0. Therefore, it is important to check
1865 * the value of remaining after calling this function.
1866 *
1867 * type will be set to the ethertype of the MPLS payload. This parameter is
1868 * mandatory - it may not be NULL.
1869 *
1870 * @note This function will only remove one MPLS label at a time - the type
1871 * will be set to 0x8847 if there is another MPLS label following the one
1872 * skipped by this function.
1873 *
1874 */
1875DLLEXPORT void *trace_get_payload_from_mpls(
1876                void *mpls, uint16_t *type, uint32_t *remaining);
1877
1878/** Gets a pointer to the payload following a PPPoE header
1879 * @param pppoe      A pointer to the PPPoE header
1880 * @param[out] type  The ethernet type, replaced by the ether type of the
1881 * returned header - 0x0000 if an Ethernet header is returned
1882 * @param[in,out] remaining  Updated with the number of captured bytes remaining
1883 *
1884 * @return A pointer to the header beyond the PPPoE header. NOTE that this
1885 * function will also skip over the PPP header that will immediately follow
1886 * the PPPoE header. This function will return NULL if there are not enough
1887 * bytes remaining to skip past both the PPPoE and PPP headers.
1888 *
1889 * When calling this function, remaining must contain the number of captured
1890 * bytes remaining in the packet starting from the PPPoE header (including the
1891 * PPPoE header itself). remaining will be updated to contain the number of
1892 * bytes remaining after the PPPoE and PPP headers have been removed.
1893 *
1894 * If the PPPoE and PPP headers are complete but there are zero bytes of
1895 * payload after the PPP header, a pointer to where the payload would be is
1896 * returned and remaining will be set to 0.  If the PPPoE or PPP header is
1897 * incomplete, NULL will be returned and remaining will be set to 0. Therefore,
1898 * it is important to check the value of remaining after calling this function.
1899 *
1900 * type will be set to the ether type of the PPP payload. This parameter is
1901 * mandatory - it may not be NULL.
1902 *
1903 */
1904DLLEXPORT void *trace_get_payload_from_pppoe(
1905                void *pppoe, uint16_t *type, uint32_t *remaining);
1906
1907/** Gets a pointer to the payload following a TCP header
1908 * @param tcp           A pointer to the TCP header
1909 * @param[in,out] remaining Updated with the number of captured bytes remaining
1910 *
1911 * @return A pointer to the TCP payload, or NULL if the TCP header is truncated.
1912 *
1913 * When calling this function, remaining must contain the number of captured
1914 * bytes remaining in the packet starting from the TCP header (including the
1915 * TCP header itself). remaining will be updated to contain the number of
1916 * bytes remaining after the TCP header has been skipped.
1917 *
1918 * If the TCP header is complete but there are zero bytes of payload after
1919 * the TCP header, a pointer to where the payload would be is returned and
1920 * remaining will be set to 0.  If the TCP header is incomplete, NULL will be
1921 * returned and remaining will be set to 0. Therefore, it is important to check
1922 * the value of remaining after calling this function.
1923 *
1924 */
1925DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp,
1926                uint32_t *remaining);
1927
1928/** Gets a pointer to the payload following a UDP header
1929 * @param udp           A pointer to the UDP header
1930 * @param[in,out] remaining Updated with the number of captured bytes remaining
1931 *
1932 * @return A pointer to the UDP payload, or NULL if the UDP header is truncated.
1933 *
1934 * When calling this function, remaining must contain the number of captured
1935 * bytes remaining in the packet starting from the UDP header (including the
1936 * UDP header itself). remaining will be updated to contain the number of
1937 * bytes remaining after the UDP header has been skipped.
1938 *
1939 * If the UDP header is complete but there are zero bytes of payload after
1940 * the UDP header, a pointer to where the payload would be is returned and
1941 * remaining will be set to 0.  If the UDP header is incomplete, NULL will be
1942 * returned and remaining will be set to 0. Therefore, it is important to check
1943 * the value of remaining after calling this function.
1944 *
1945 */
1946DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
1947
1948/** Gets a pointer to the payload following a ICMP header
1949 * @param icmp           A pointer to the ICMP header
1950 * @param[in,out] remaining Updated with the number of captured bytes remaining
1951 *
1952 * @return A pointer to the ICMP payload, or NULL if the ICMP header is
1953 * truncated.
1954 *
1955 * When calling this function, remaining must contain the number of captured
1956 * bytes remaining in the packet starting from the ICMP header (including the
1957 * ICMP header itself). remaining will be updated to contain the number of
1958 * bytes remaining after the ICMP header has been skipped.
1959 *
1960 * If the ICMP header is complete but there are zero bytes of payload after
1961 * the ICMP header, a pointer to where the payload would be is returned and
1962 * remaining will be set to 0.  If the ICMP header is incomplete, NULL will be
1963 * returned and remaining will be set to 0. Therefore, it is important to check
1964 * the value of remaining after calling this function.
1965 *
1966 * @note In the case of some ICMP messages, the payload may be the IP header
1967 * from the packet that triggered the ICMP message.
1968 *
1969 */
1970DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp,
1971                uint32_t *remaining);
1972
1973/** Gets a pointer to the payload following a ICMPv6 header
1974 * @param icmp           A pointer to the ICMPv6 header
1975 * @param[in,out] remaining Updated with the number of captured bytes remaining
1976 *
1977 * @return A pointer to the ICMPv6 payload, or NULL if the ICMPv6 header is
1978 * truncated.
1979 *
1980 * When calling this function, remaining must contain the number of captured
1981 * bytes remaining in the packet starting from the ICMPv6 header (including the
1982 * ICMP header itself). remaining will be updated to contain the number of
1983 * bytes remaining after the ICMPv6 header has been skipped.
1984 *
1985 * If the ICMPv6 header is complete but there are zero bytes of payload after
1986 * the header, a pointer to where the payload would be is returned and
1987 * remaining will be set to 0.  If the ICMPv6 header is incomplete, NULL will be
1988 * returned and remaining will be set to 0. Therefore, it is important to check
1989 * the value of remaining after calling this function.
1990 *
1991 * @note In the case of some ICMPv6 messages, the payload may be the IP header
1992 * from the packet that triggered the ICMP message.
1993 *
1994 */
1995DLLEXPORT void *trace_get_payload_from_icmp6(libtrace_icmp6_t *icmp,
1996                uint32_t *remaining);
1997
1998/** Get a pointer to the TCP header (if present)
1999 * @param packet        The packet to get the TCP header from
2000 *
2001 * @return A pointer to the TCP header, or NULL if there is not a complete TCP
2002 * header present in the packet.
2003 *
2004 * This is a short-cut function enabling quick and easy access to the TCP
2005 * header if that is all you care about. However, we recommend the use of the
2006 * more generic trace_get_transport() function instead.
2007 *
2008 * @note Unlike trace_get_transport(), this function will return NULL if the
2009 * TCP header is incomplete or truncated.
2010 */
2011DLLEXPORT SIMPLE_FUNCTION
2012libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
2013
2014/** Get a pointer to the TCP header following an IPv4 header (if present)
2015 * @param ip            The IP header to find the subsequent TCP header for
2016 * @param[in,out] remaining Updated with the number of captured bytes remaining
2017 *
2018 * @return A pointer to the TCP header, or NULL if no TCP header is present in
2019 * the packet.
2020 *
2021 * When calling this function, remaining must contain the number of captured
2022 * bytes remaining in the packet starting from the IP header (including the
2023 * IP header itself). remaining will be updated to contain the number of
2024 * bytes remaining after the IP header has been skipped.
2025 *
2026 * If the IP header is complete but there are zero bytes of payload after
2027 * the IP header, a pointer to where the payload would be is returned and
2028 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
2029 * returned and remaining will be set to 0. Therefore, it is important to check
2030 * the value of remaining after calling this function.
2031 *
2032 * @note This function is rather redundant now that the layer 3 header is
2033 * cached. There should be no performance advantage for the user to call this
2034 * function over just calling trace_get_transport().
2035 *
2036 * @note The last parameter has changed from libtrace2
2037 */
2038DLLEXPORT SIMPLE_FUNCTION
2039libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
2040
2041/** Get a pointer to the UDP header (if present)
2042 * @param packet        The packet to get the UDP header from
2043 *
2044 * @return A pointer to the UDP header, or NULL if there is not a complete UDP
2045 * header present in the packet.
2046 *
2047 * This is a short-cut function enabling quick and easy access to the UDP
2048 * header if that is all you care about. However, we recommend the use of the
2049 * more generic trace_get_transport() function instead.
2050 *
2051 * @note Unlike trace_get_transport(), this function will return NULL if the
2052 * UDP header is incomplete or truncated.
2053 */
2054DLLEXPORT SIMPLE_FUNCTION
2055libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
2056
2057/** Get a pointer to the UDP header following an IPv4 header (if present)
2058 * @param ip            The IP header to find the subsequent UDP header for
2059 * @param[in,out] remaining Updated with the number of captured bytes remaining
2060 *
2061 * @return A pointer to the UDP header, or NULL if no UDP header is present in
2062 * the packet.
2063 *
2064 * When calling this function, remaining must contain the number of captured
2065 * bytes remaining in the packet starting from the IP header (including the
2066 * IP header itself). remaining will be updated to contain the number of
2067 * bytes remaining after the IP header has been skipped.
2068 *
2069 * If the IP header is complete but there are zero bytes of payload after
2070 * the IP header, a pointer to where the payload would be is returned and
2071 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
2072 * returned and remaining will be set to 0. Therefore, it is important to check
2073 * the value of remaining after calling this function.
2074 *
2075 * @note This function is rather redundant now that the layer 3 header is
2076 * cached. There should be no performance advantage for the user to call this
2077 * function over just calling trace_get_transport().
2078 *
2079 * @note The last parameter has changed from libtrace2
2080 */
2081DLLEXPORT SIMPLE_FUNCTION
2082libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
2083
2084/** Get a pointer to the ICMP header (if present)
2085 * @param packet        The packet to get the ICMP header from
2086 *
2087 * @return A pointer to the ICMP header, or NULL if there is not a complete
2088 * ICMP header present in the packet.
2089 *
2090 * This is a short-cut function enabling quick and easy access to the ICMP
2091 * header if that is all you care about. However, we recommend the use of the
2092 * more generic trace_get_transport() function instead.
2093 *
2094 * @note Unlike trace_get_transport(), this function will return NULL if the
2095 * ICMP header is incomplete or truncated.
2096 */
2097DLLEXPORT SIMPLE_FUNCTION
2098libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
2099
2100/** Get a pointer to the ICMPv6 header (if present)
2101 * @param packet        The packet to get the ICMPv6 header from
2102 *
2103 * @return A pointer to the ICMPv6 header, or NULL if there is not a complete
2104 * ICMP header present in the packet.
2105 *
2106 * This is a short-cut function enabling quick and easy access to the ICMPv6
2107 * header if that is all you care about. However, we recommend the use of the
2108 * more generic trace_get_transport() function instead.
2109 *
2110 * @note Unlike trace_get_transport(), this function will return NULL if the
2111 * ICMPv6 header is incomplete or truncated.
2112 */
2113DLLEXPORT SIMPLE_FUNCTION
2114libtrace_icmp6_t *trace_get_icmp6(libtrace_packet_t *packet);
2115
2116/** Get a pointer to the ICMP header following an IPv4 header (if present)
2117 * @param ip            The IP header to find the subsequent ICMP header for
2118 * @param[in,out] remaining Updated with the number of captured bytes remaining
2119 *
2120 * @return A pointer to the ICMP header, or NULL if no UDP header is present in
2121 * the packet.
2122 *
2123 * When calling this function, remaining must contain the number of captured
2124 * bytes remaining in the packet starting from the IP header (including the
2125 * IP header itself). remaining will be updated to contain the number of
2126 * bytes remaining after the IP header has been skipped.
2127 *
2128 * If the IP header is complete but there are zero bytes of payload after
2129 * the IP header, a pointer to where the payload would be is returned and
2130 * remaining will be set to 0.  If the IP header is incomplete, NULL will be
2131 * returned and remaining will be set to 0. Therefore, it is important to check
2132 * the value of remaining after calling this function.
2133 *
2134 * @note This function is rather redundant now that the layer 3 header is
2135 * cached. There should be no performance advantage for the user to call this
2136 * function over just calling trace_get_transport().
2137 *
2138 * @note The last parameter has changed from libtrace2
2139 */
2140DLLEXPORT SIMPLE_FUNCTION
2141libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
2142
2143/** Get a pointer to the OSPF header (if present)
2144 * @param packet        The packet to get the OSPF header from
2145 * @param[out] version  The OSPF version number
2146 * @param[out] remaining        Updated with the number of captured bytes remaining
2147 * @return A pointer to the start of the OSPF header or NULL if there is no
2148 * complete OSPF header present in the packet
2149 *
2150 * This is a short-cut function enabling quick and easy access to the OSPF
2151 * header. If you only care about the OSPF header, this function may be useful
2152 * but we otherwise recommend the use of the more generic trace_get_transport()
2153 * function instead.
2154 *
2155 * Upon return, 'version' is updated to contain the OSPF version number for the
2156 * packet so that the returned pointer may be cast to the correct type.
2157 * The version parameter MUST contain a valid pointer; it MUST NOT be NULL.
2158 *
2159 * 'remaining' is also set to contain the number of captured bytes remaining
2160 * starting from the pointer returned by this function.
2161 *
2162 * @note Unlike trace_get_transport(), this function will return NULL if the
2163 * OSPF header is incomplete or truncated.
2164 */
2165DLLEXPORT SIMPLE_FUNCTION
2166void *trace_get_ospf_header(libtrace_packet_t *packet, uint8_t *version,
2167                uint32_t *remaining);
2168
2169/** Get a pointer to the contents of the OSPF packet *after* the OSPF header
2170 * @param header        The OSPF header to get the OSPF contents from
2171 * @param[out] ospf_type        The OSPF packet type
2172 * @param[in, out] remaining    Updated with the number of captured bytes remaining
2173 * @return A pointer to the first byte after the OSPF header.
2174 *
2175 * This function returns a void pointer that can be cast appropriately
2176 * based on the ospf_type. For example, if the ospf_type is TRACE_OSPF_HELLO
2177 * then the return pointer should be cast as a libtrace_ospf_hello_v2_t
2178 * structure.
2179 *
2180 * If the OSPF header is truncated, then NULL will be returned. If the OSPF
2181 * contents are missing or truncated, the pointer to the start of the content
2182 * will still be returned so be careful to check the value of remaining.
2183 *
2184 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2185 * packet starting from the beginning of the OSPF header. It will be updated
2186 * to contain the number of bytes remaining from the start of the OSPF contents.
2187 *
2188 * @note This function only works for OSPF version 2 packets.
2189 * @note Use trace_get_first_ospf_lsa_v2_from_X() and trace_get_next_ospf_lsa_v2() to read the LSAs from Link State Update packets.
2190 * @note Use trace_get_first_ospf_lsa_v2_from_X() and trace_get_next_ospf_lsa_header_v2() to read the LSA headers from Link State Ack packets.
2191 *
2192 */
2193DLLEXPORT SIMPLE_FUNCTION
2194void *trace_get_ospf_contents_v2(libtrace_ospf_v2_t *header,
2195                uint8_t *ospf_type, uint32_t *remaining);
2196
2197/** Get a pointer to the start of the first LSA contained within an LS Update packet
2198 * @param ls_update     Pointer to the LS Update header
2199 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2200 * @return A pointer to the first LSA in the packet.
2201 *
2202 * This function simply skips past the LS Update header to provide a suitable
2203 * pointer to pass into trace_get_next_ospf_lsa_v2.
2204 *
2205 * If the OSPF packet is truncated, then NULL will be returned.
2206 *
2207 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2208 * packet starting from the beginning of the LS Update header. It will be
2209 * updated to contain the number of bytes remaining from the start of the
2210 * first LSA.
2211 *
2212 * @note This function only works for OSPF version 2 packets.
2213 * @note trace_get_next_ospf_lsa_v2() should be subequently used to process the LSAs
2214 */
2215DLLEXPORT SIMPLE_FUNCTION
2216unsigned char *trace_get_first_ospf_lsa_from_update_v2(
2217                libtrace_ospf_ls_update_t *ls_update,
2218                uint32_t *remaining);
2219
2220/** Get a pointer to the start of the first LSA contained within an Database Description packet
2221 * @param db_desc       Pointer to the Database Description header
2222 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2223 * @return A pointer to the first LSA in the packet.
2224 *
2225 * This function simply skips past the Database Description header to provide a
2226 * suitable pointer to pass into trace_get_next_ospf_lsa_header_v2.
2227 *
2228 * If the OSPF packet is truncated, then NULL will be returned.
2229 *
2230 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2231 * packet starting from the beginning of the Database Description header. It
2232 * will be updated to contain the number of bytes remaining from the start of
2233 * the first LSA.
2234 *
2235 * @note This function only works for OSPF version 2 packets.
2236 * @note trace_get_next_ospf_lsa_header_v2() should be subequently used to process the LSA headers
2237 */
2238DLLEXPORT SIMPLE_FUNCTION
2239unsigned char *trace_get_first_ospf_lsa_from_db_desc_v2(
2240                libtrace_ospf_db_desc_v2_t *db_desc,
2241                uint32_t *remaining);
2242
2243/** Get a pointer to the start of the first link contained within a Router LSA
2244 * @param lsa   Pointer to the Router LSA
2245 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2246 * @return A pointer to the first link in the packet.
2247 *
2248 * This function simply skips past the Router LSA header to provide a
2249 * suitable pointer to pass into trace_get_next_ospf_link_v2.
2250 *
2251 * If the OSPF packet is truncated, then NULL will be returned.
2252 *
2253 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2254 * packet starting from the beginning of the Router LSA (not including the LSA
2255 * header) header. It will be updated to contain the number of bytes remaining
2256 * from the start of the first Link.
2257 *
2258 * @note This function only works for OSPF version 2 packets.
2259 * @note trace_get_next_ospf_link_v2() should be subequently used to process
2260 * the links
2261 */
2262DLLEXPORT SIMPLE_FUNCTION
2263unsigned char *trace_get_first_ospf_link_from_router_lsa_v2(
2264                libtrace_ospf_router_lsa_v2_t *lsa,
2265                uint32_t *remaining);
2266
2267/** Parses an OSPF Router LSA Link and finds the next Link (if there is one)
2268 * @param[in,out] current       Pointer to the next Link (updated by this function)
2269 * @param[out] link             Set to point to the Link located at the original value of current
2270 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2271 * @param[out] link_len         Set to the size of the Link pointed to by 'link'
2272 * @return 0 if there are no more links after the current one, 1 otherwise.
2273 *
2274 * When called, 'current' MUST point to an OSPF Router LSA link. Ideally, this
2275 * would come from either a call to
2276 * trace_get_first_ospf_link_from_router_lsa_v2() or a previous call of this
2277 * function.
2278 *
2279 * 'link' will be set to the value of 'current', so that the caller may then
2280 * do any processing they wish on that particular link. 'current' is advanced
2281 * to point to the next link and 'link_len' is updated to report the size of
2282 * the original link.
2283 *
2284 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2285 * packet starting from the beginning of the Link pointed to by 'current'.
2286 * It will be updated to contain the number of bytes remaining from the start
2287 * of the next link.
2288 *
2289 * If this function returns 0 but 'link' is NOT NULL, that link is still valid
2290 * but there are no more links after this one.
2291 * If this function returns 0 AND link is NULL, the link is obviously not
2292 * suitable for processing.
2293 *
2294 * @note This function only works for OSPF version 2 packets.
2295 */
2296DLLEXPORT SIMPLE_FUNCTION
2297int trace_get_next_ospf_link_v2(unsigned char **current,
2298                libtrace_ospf_link_v2_t **link,
2299                uint32_t *remaining,
2300                uint32_t *link_len);
2301
2302/** Parses an OSPF LSA and finds the next LSA (if there is one)
2303 * @param[in,out] current       Pointer to the next LSA (updated by this function)
2304 * @param[out] lsa_hdr          Set to point to the header of the LSA located at the original value of current
2305 * @param[out] lsa_body         Set to point to the body of the LSA located at the original value of current
2306 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2307 * @param[out] lsa_type         Set to the type of the LSA located at the original value of current
2308 * @param[out] lsa_length       Set to the size of the LSA located at the original value of current
2309 * @return 1 if there are more LSAs after the current one, 0 if there are no more LSAs to come, and -1 if the current LSA is incomplete, truncated or invalid.
2310 *
2311 * When called, 'current' MUST point to an OSPF LSA. Ideally, this would come
2312 * from either a call to trace_get_first_ospf_lsa_from_update_v2() or a
2313 * previous call of this function.
2314 *
2315 * This function should only be used to access COMPLETE LSAs, i.e. LSAs that
2316 * have both a header and a body. In OSPFv2, only the LSAs contained within
2317 * LSA Update packets meet this requirement. trace_get_next_ospf_lsa_header_v2
2318 * should be used to read header-only LSAs, e.g. those present in LS Acks.
2319 *
2320 * 'lsa_hdr' will be set to the value of 'current', so that the caller may then
2321 * do any processing they wish on that particular LSA. 'lsa_body' will be set
2322 * to point to the first byte after the LSA header. 'current' is advanced
2323 * to point to the next LSA. 'lsa_length' is updated to contain the size of
2324 * the parsed LSA, while 'lsa_type' is set to indicate the LSA type.
2325 *
2326 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2327 * packet starting from the beginning of the LSA pointed to by 'current'.
2328 * It will be updated to contain the number of bytes remaining from the start
2329 * of the next LSA.
2330 *
2331 * If this function returns 0 but 'lsa_hdr' is NOT NULL, that LSA is still
2332 * valid but there are no more LSAs after this one.
2333 * If this function returns 0 AND 'lsa_hdr' is NULL, the LSA is obviously not
2334 * suitable for processing.
2335 *
2336 * It is also recommended to check the value of 'lsa_body' before
2337 * de-referencing it. 'lsa_body' will be set to NULL if there is only an LSA
2338 * header present.
2339 *
2340 * @note This function only works for OSPF version 2 packets.
2341 *
2342 */
2343DLLEXPORT SIMPLE_FUNCTION
2344int trace_get_next_ospf_lsa_v2(unsigned char **current,
2345                libtrace_ospf_lsa_v2_t **lsa_hdr,
2346                unsigned char **lsa_body,
2347                uint32_t *remaining,
2348                uint8_t *lsa_type,
2349                uint16_t *lsa_length);
2350
2351/** Parses an OSPF LSA header and finds the next LSA (if there is one)
2352 * @param[in,out] current       Pointer to the next LSA (updated by this function)
2353 * @param[out] lsa_hdr          Set to point to the header of the LSA located at the original value of current
2354 * @param[in,out] remaining     Updated with the number of captured bytes remaining
2355 * @param[out] lsa_length       Set to the size of the LSA located at the original value of current
2356 * @return 1 if there are more LSAs after the current one, 0 if there are no more LSAs to come, and -1 if the current LSA is incomplete, truncated or invalid.
2357 *
2358 * When called, 'current' MUST point to an OSPF LSA. Ideally, this would come
2359 * from either a call to trace_get_first_ospf_lsa_from_db_desc_v2() or a
2360 * previous call of this function.
2361 *
2362 * This function should only be used to access LSA headers, i.e. LSAs that
2363 * have a header only. In OSPFv2, the LSAs contained within LSA Ack and
2364 * Database Description packets meet this requirement.
2365 * trace_get_next_ospf_lsa_v2 should be used to read full LSAs, e.g. those
2366 * present in LS Updates.
2367 *
2368 * 'lsa_hdr' will be set to the value of 'current', so that the caller may then
2369 * do any processing they wish on that particular LSA header. 'current' is
2370 * advanced to point to the next LSA header. 'lsa_length' is updated to contain
2371 * the size of the parsed LSA header.
2372 *
2373 * 'remaining' MUST be set to the amount of bytes remaining in the captured
2374 * packet starting from the beginning of the LSA pointed to by 'current'.
2375 * It will be updated to contain the number of bytes remaining from the start
2376 * of the next LSA.
2377 *
2378 * If this function returns 0 but 'lsa_hdr' is NOT NULL, that LSA is still
2379 * valid but there are no more LSAs after this one.
2380 * If this function returns 0 AND 'lsa_hdr' is NULL, the LSA is obviously not
2381 * suitable for processing.
2382 *
2383 * @note This function only works for OSPF version 2 packets.
2384 *
2385 */
2386DLLEXPORT SIMPLE_FUNCTION
2387int trace_get_next_ospf_lsa_header_v2(unsigned char **current,
2388                libtrace_ospf_lsa_v2_t **lsa_hdr,
2389                uint32_t *remaining,
2390                uint8_t *lsa_type,
2391                uint16_t *lsa_length);
2392
2393/** Extracts the metric field from an AS External LSA packet
2394 *
2395 * @param as_lsa        The AS External LSA
2396 * @returns The value of the metric field
2397 *
2398 * The metric field in the AS External LSA packet is a 24-bit value, which
2399 * is difficult to extract correctly. To avoid byte-ordering issues, use this
2400 * function which will extract the correct value for you.
2401 */
2402DLLEXPORT SIMPLE_FUNCTION
2403uint32_t trace_get_ospf_metric_from_as_external_lsa_v2(
2404                libtrace_ospf_as_external_lsa_v2_t *as_lsa);
2405
2406/** Extracts the metric field from a Summary LSA packet
2407 *
2408 * @param sum_lsa       The Summary LSA
2409 * @returns The value of the metric field
2410 *
2411 * The metric field in the Summary LSA packet is a 24-bit value, which
2412 * is difficult to extract correctly. To avoid byte-ordering issues, use this
2413 * function which will extract the correct value for you.
2414 */
2415DLLEXPORT SIMPLE_FUNCTION
2416uint32_t trace_get_ospf_metric_from_summary_lsa_v2(
2417                libtrace_ospf_summary_lsa_v2_t *sum_lsa);
2418
2419
2420/** Gets the destination MAC address for a given packet
2421 * @param packet        The packet to extract the destination MAC address from
2422 *
2423 * @return A pointer to the destination MAC address field in the layer 2
2424 * header, (or NULL if there is no destination MAC address or layer 2 header
2425 * available)
2426 *
2427 * @note This is a zero-copy function, so the memory that the returned pointer
2428 * points to is part of the packet itself.
2429 */
2430DLLEXPORT SIMPLE_FUNCTION
2431uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
2432
2433/** Gets the source MAC address for a given packet
2434 * @param packet        The packet to extract the source MAC address from
2435 *
2436 * @return A pointer to the source MAC address field in the layer 2
2437 * header, (or NULL if there is no source MAC address or layer 2 header
2438 * available)
2439 *
2440 * @note This is a zero-copy function, so the memory that the returned pointer
2441 * points to is part of the packet itself.
2442 */
2443DLLEXPORT SIMPLE_FUNCTION
2444uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
2445
2446/** Get the source IP address for a given packet
2447 * @param packet        The packet to extract the source IP address from
2448 * @param addr          A pointer to a sockaddr structure to store the address
2449 *                      in. If NULL, static storage is used instead.
2450 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
2451 * platforms a sockaddr holding a MAC address. Returns NULL if no source IP
2452 * address was available.
2453 *
2454 * @note The best way to use this function is to pass in a pointer to the
2455 * struct sockaddr_storage for the addr parameter. This will avoid problems
2456 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
2457 * IPv4.
2458 */
2459DLLEXPORT SIMPLE_FUNCTION
2460struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
2461                struct sockaddr *addr);
2462
2463/** Get the source IP address for a packet and convert it into a string
2464 * @param packet        The packet to extract the source IP address from
2465 * @param space         A pointer to a character buffer to store the address
2466 *                      in. If NULL, static storage is used instead.
2467 * @param spacelen      The size of the buffer passed in via 'space'. Set this
2468 *                      to zero if you are going to pass in a NULL buffer.
2469 * @return A pointer to a character buffer containing the string representation
2470 * of the source IP address. For packets where there is no suitable IP address,
2471 * the source MAC will be returned instead. Returns NULL if no valid address
2472 * is available.
2473 *
2474 * @note Be wary of the possibility of the address being an IPv6 address - make
2475 * sure your buffer is large enough!
2476 *
2477 * New in libtrace 3.0.17.
2478 */
2479DLLEXPORT SIMPLE_FUNCTION
2480char *trace_get_source_address_string(const libtrace_packet_t *packet,
2481                char *space, int spacelen);
2482
2483/** Get the destination IP address for a given packet
2484 * @param packet        The packet to extract the destination IP address from
2485 * @param addr          A pointer to a sockaddr structure to store the address
2486 *                      in. If NULL, static storage is used instead.
2487 * @return A pointer to a sockaddr holding a v4 or v6 IP address or on some
2488 * platforms a sockaddr holding a MAC address. Returns NULL if no destination
2489 * IP address was available.
2490 *
2491 * @note The best way to use this function is to pass in a pointer to the
2492 * struct sockaddr_storage for the addr parameter. This will avoid problems
2493 * with trying to shoe-horn an IPv6 address into a sockaddr that only supports
2494 * IPv4.
2495 */
2496DLLEXPORT SIMPLE_FUNCTION
2497struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
2498                struct sockaddr *addr);
2499
2500/** Get the destination IP address for a packet and convert it into a string
2501 * @param packet        The packet to extract the destination IP address from
2502 * @param space         A pointer to a character buffer to store the address
2503 *                      in. If NULL, static storage is used instead.
2504 * @param spacelen      The size of the buffer passed in via 'space'. Set this
2505 *                      to zero if you are going to pass in a NULL buffer.
2506 * @return A pointer to a character buffer containing the string representation
2507 * of the destination IP address. For packets where there is no suitable IP
2508 * address, the destination MAC will be returned instead. Returns NULL if no
2509 * valid address is available.
2510 *
2511 * @note Be wary of the possibility of the address being an IPv6 address - make
2512 * sure your buffer is large enough!
2513 *
2514 * New in libtrace 3.0.17.
2515 */
2516DLLEXPORT SIMPLE_FUNCTION
2517char *trace_get_destination_address_string(const libtrace_packet_t *packet,
2518                char *space, int spacelen);
2519
2520/** Parses an IP or TCP option
2521 * @param[in,out] ptr   The pointer to the current option
2522 * @param[in,out] len   The total length of all the remaining options
2523 * @param[out] type     The type of the option
2524 * @param[out] optlen   The length of the option
2525 * @param[out] data     The data of the option
2526 *
2527 * @return bool true if there is another option (and the fields are filled in)
2528 *               or false if this was the last option.
2529 *
2530 * This updates ptr to point to the next option after this one, and updates
2531 * len to be the number of bytes remaining in the options area.  Type is updated
2532 * to be the code of this option, and data points to the data of this option,
2533 * with optlen saying how many bytes there are.
2534 *
2535 * @note Beware of fragmented packets.
2536 */
2537DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
2538                        unsigned char *type,
2539                        unsigned char *optlen,
2540                        unsigned char **data);
2541
2542/*@}*/
2543
2544/** @name Time
2545 * These functions deal with the timestamp describing when a packet was
2546 * captured and can convert it into various formats
2547 * @{
2548 */
2549
2550/** Get the packet timestamp in the DAG time format
2551 * @param packet        The packet to extract the timestamp from
2552 *
2553 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
2554 * past 1970-01-01, the lower 32bits are partial seconds)
2555 */
2556DLLEXPORT SIMPLE_FUNCTION
2557uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
2558
2559/** Get the packet timestamp as a struct timeval
2560 * @param packet        The packet to extract the timestamp from
2561 *
2562 * @return The time that this packet was captured in a struct timeval
2563 */
2564DLLEXPORT SIMPLE_FUNCTION
2565struct timeval trace_get_timeval(const libtrace_packet_t *packet);
2566
2567/** Get the packet timestamp as a struct timespec
2568 * @param packet        The packet to extract the timestamp from
2569 *
2570 * @return The time that this packet was captured in a struct timespec
2571 */
2572DLLEXPORT SIMPLE_FUNCTION
2573struct timespec trace_get_timespec(const libtrace_packet_t *packet);
2574
2575/** Get the packet timestamp in floating point seconds
2576 * @param packet        The packet to extract the timestamp from
2577 *
2578 * @return time that this packet was seen in 64-bit floating point seconds from
2579 * the UNIX epoch (1970-01-01 00:00:00 UTC).
2580 */
2581DLLEXPORT SIMPLE_FUNCTION
2582double trace_get_seconds(const libtrace_packet_t *packet);
2583
2584/** Seek within an input trace to a time specified in floating point seconds
2585 * @param trace         The input trace to seek within
2586 * @param seconds       The time to seek to, in floating point seconds
2587 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
2588 * the error that occurred.
2589 *
2590 * This will make the next packet read to be the first packet to occur at or
2591 * after the specified time.  This must be called in the configuration state
2592 * (i.e. before trace_start() or after trace_pause()).
2593 *
2594 * The time format accepted by this function is 64-bit floating point seconds
2595 * since the UNIX epoch (1970-01-01 00:00:00 UTC), i.e. the same format as
2596 * trace_get_seconds().
2597 *
2598 * @note This function may be extremely slow.
2599 */
2600DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
2601
2602/** Seek within an input trace to a time specified as a timeval
2603 * @param trace         The input trace to seek within
2604 * @param tv            The time to seek to, as a timeval
2605 *
2606 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
2607 * the error that occurred.
2608 *
2609 * This will make the next packet read to be the first packet to occur at or
2610 * after the specified time.  This must be called in the configuration state
2611 * (i.e. before trace_start() or after trace_pause()).
2612 *
2613 * @note This function may be extremely slow.
2614 */
2615DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
2616
2617/** Seek within an input trace to a time specified as an ERF timestamp
2618 * @param trace         The input trace to seek within
2619 * @param ts            The time to seek to, as an ERF timestamp
2620 *
2621 * @return 0 on success, -1 if the seek fails. Use trace_perror() to determine
2622 * the error that occurred.
2623 *
2624 * This will make the next packet read to be the first packet to occur at or
2625 * after the specified time.  This must be called in the configuration state
2626 * (i.e. before trace_start() or after trace_pause()).
2627 *
2628 * The time format accepted by this function is the ERF timestamp, which is a
2629 * 64-bit value where the upper 32 bits are seconds since the UNIX epoch and
2630 * the lower 32 bits are partial seconds.
2631 *
2632 * @note This function may be extremely slow.
2633 */
2634DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
2635
2636/*@}*/
2637
2638/** @name Sizes
2639 * This section deals with finding or setting the various different lengths
2640 * that a packet can have, e.g. capture lengths, wire lengths, etc.
2641 * @{
2642 */
2643/** Get the current size of the packet (in bytes), taking into account any
2644 * truncation or snapping that may have previously been performed.
2645 *
2646 * @param packet        The packet to determine the capture length for
2647 * @return The size of the packet read from the input trace, i.e. what is
2648 * actually available to libtrace at the moment.
2649 *
2650 * @note Most traces are header captures, so this value may not be the same
2651 * as the size of the packet when it was captured. Use trace_get_wire_length()
2652 * to get the original size of the packet.
2653 
2654 * @note This can (and often is) different for different packets in a trace!
2655 
2656 * @note This is sometimes called the "snaplen".
2657 *
2658 * @note The return size refers to the network-level payload of the packet and
2659 * does not include any capture framing headers. For example, an Ethernet
2660 * packet with an empty TCP packet will return sizeof(ethernet_header) +
2661 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
2662 * (pcap/erf/etc) header.
2663 */
2664DLLEXPORT SIMPLE_FUNCTION
2665size_t trace_get_capture_length(const libtrace_packet_t *packet);
2666
2667/** Get the size of the packet as it was originally seen on the wire (in bytes).
2668 * @param packet        The packet to determine the wire length for
2669 * @return The size of the packet as it was on the wire.
2670 *
2671 * @note This value may not be the same as the capture length, due to
2672 * truncation.
2673 *
2674 * @note trace_get_wire_length \em includes  the Frame Check Sequence. This is
2675 * different behaviour compared to most PCAP-based tools.
2676 *
2677 * @note The return size refers to the network-level payload of the packet and
2678 * does not include any capture framing headers. For example, an Ethernet
2679 * packet with an empty TCP packet will return sizeof(ethernet_header) +
2680 * sizeof(ip_header) + sizeof(tcp_header), but not the capture format
2681 * (pcap/erf/etc) header.
2682 */
2683DLLEXPORT SIMPLE_FUNCTION
2684size_t trace_get_wire_length(const libtrace_packet_t *packet);
2685
2686/** Get the length of the capture framing headers (in bytes).
2687 * @param packet        The packet to determine the framing length for
2688 * @return The size of the capture format header encapsulating the packet.
2689 *
2690 * @note This length corresponds to the difference between the amount of
2691 * memory required to store a captured packet and the capture length reported
2692 * by trace_capture_length()
2693 */
2694DLLEXPORT SIMPLE_FUNCTION
2695size_t trace_get_framing_length(const libtrace_packet_t *packet);
2696
2697/** Get the length of the original payload content of the packet (in bytes).
2698 * @param packet        The packet to determine the payload length for
2699 * @return The size of the packet payload (without headers). Returns 0 if
2700 * unable to calculate payload length correctly.
2701 *
2702 * This function reports the amount of data that followed the transport header
2703 * when the packet was originally captured, i.e. prior to any snapping. Best
2704 * described as the wire length minus the packet headers.
2705 *
2706 * Currently only supports some protocols and will return 0 if an unsupported
2707 * protocol header is encountered, or if one of the headers is truncated.
2708 *
2709 * @note Supports IPv4, IPv6, TCP, UDP and ICMP.
2710 */
2711DLLEXPORT SIMPLE_FUNCTION
2712size_t trace_get_payload_length(const libtrace_packet_t *packet);
2713
2714/** Truncate ("snap") the packet to the suggested length
2715 * @param packet        The packet to truncate
2716 * @param size          The new length of the packet (in bytes)
2717 *
2718 * @return The new capture length of the packet or the original capture
2719 * length of the packet if unchanged.
2720 *
2721 * This function will modify the capture length of the given packet. The wire
2722 * length will not be changed, so you can always determine what the original
2723 * packet size was, prior to the truncation.
2724 *
2725 * @note You can only use this function to decrease the capture length. Any
2726 * attempt to increase capture length will have no effect.
2727 */
2728DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
2729
2730/*@}*/
2731
2732
2733/** Gets the link layer type for a packet
2734 * @param packet        The packet to extract the link layer type for
2735 * @return A libtrace_linktype_t describing the link layer protocol being used
2736 * by this packet.
2737 */
2738DLLEXPORT SIMPLE_FUNCTION
2739libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
2740
2741/** Set the direction flag for a packet, if the capture format supports
2742 * direction tagging.
2743 *
2744 * @param packet        The packet to set the direction for
2745 * @param direction     The new direction
2746 * @returns -1 on error, or the direction that was set.
2747 *
2748 * @note Few capture formats actually support direction tagging. Most notably,
2749 * we cannot set the direction on PCAP packets.
2750 */
2751DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
2752
2753/** Get the direction flag for a packet, if it has one.
2754 * @param packet  The packet to get the direction for
2755 *
2756 * @return A value representing the direction flag, or -1 if this is not
2757 * supported by the capture format.
2758 *
2759 * The direction is defined as 0 for packets originating locally (ie, outbound)
2760 * and 1 for packets originating remotely (ie, inbound). Other values are
2761 * possible, which might be overloaded to mean special things for certain
2762 * traces, e.g. in the Waikato traces, 2 is used to represent an "Unknown"
2763 * direction.
2764 *
2765 * For DAG/ERF traces, the direction is extracted from the "Interface" bits in
2766 * the ERF header, which can range from 0 - 3.
2767 */
2768DLLEXPORT SIMPLE_FUNCTION
2769libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
2770
2771/** @name BPF
2772 * This section deals with using Berkley Packet Filters to filter input traces
2773 * @{
2774 */
2775/** Creates a BPF filter
2776 * @param filterstring The filter string describing the BPF filter to create
2777 * @return An opaque pointer to a libtrace_filter_t object
2778 *
2779 * @note The filter is not actually compiled at this point, so no correctness
2780 * tests are performed here. trace_create_filter() will always return ok, but
2781 * if the filter is poorly constructed an error will be generated when the
2782 * filter is actually used.
2783 */
2784DLLEXPORT SIMPLE_FUNCTION
2785libtrace_filter_t *trace_create_filter(const char *filterstring);
2786
2787/** Create a BPF filter based on pre-compiled byte-code.
2788 * @param bf_insns      A pointer to the start of the byte-code
2789 * @param bf_len        The number of BPF instructions 
2790 * @return              An opaque pointer to a libtrace_filter_t object
2791 * @note                The supplied byte-code is not checked for correctness.
2792 *                      Instead, incorrect byte-code will generate an error
2793 *                      once the filter is actually used.
2794 * @author              Scott Raynel
2795 */
2796DLLEXPORT libtrace_filter_t *
2797trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len);
2798
2799/** Apply a BPF filter to a packet
2800 * @param filter        The filter to be applied       
2801 * @param packet        The packet to be matched against the filter
2802 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
2803 *
2804 * @note Due to the way BPF filters are built, the filter is not actually
2805 * compiled until the first time trace_create_filter is called. If your filter
2806 * is incorrect, it will generate an error message and assert, exiting the
2807 * program. This behaviour may change to a more graceful handling of this error
2808 * in the future.
2809 */
2810DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
2811                const libtrace_packet_t *packet);
2812
2813/** Destroy a BPF filter
2814 * @param filter        The filter to be destroyed
2815 *
2816 * Deallocates all the resources associated with a BPF filter.
2817 */
2818DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
2819/*@}*/
2820
2821/** @name Portability
2822 * This section contains functions that deal with portability issues, e.g. byte
2823 * ordering.
2824 * @{
2825 */
2826
2827/** Converts an ethernet address to a printable string
2828 * @param addr  Ethernet address in network byte order
2829 * @param buf   Buffer to store the ascii representation, or NULL to indicate
2830 * that static storage should be used.
2831 * @return buf, or if buf is NULL then a statically allocated buffer.
2832 *
2833 * This function is similar to the GNU ether_ntoa_r function, with a few
2834 * minor differences.  If NULL is passed as buf, then the function will
2835 * use an internal static buffer. If NULL isn't passed then the function
2836 * will use that buffer instead.
2837 *
2838 * The address pointers returned by trace_get_source_mac() and
2839 * trace_get_destination_mac() can be passed directly into this function.
2840 *
2841 * @note The type of addr isn't struct ether_addr as it is with ether_ntoa_r,
2842 * however it is bit compatible so that a cast will work.
2843 */
2844DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
2845
2846/** Convert a string to an ethernet address
2847 * @param buf   A string containing an Ethernet address in hex format
2848 * delimited with :'s.
2849 * @param addr  Buffer to store the binary representation, or NULL to indicate
2850 * that static storage should be used.
2851 * @return addr, or if addr is NULL then a statically allocated buffer.
2852 *
2853 * This function is similar to the GNU ether_aton_r function, with a few
2854 * minor differences.  If NULL is passed as addr, then the function will
2855 * use an internal static buffer. If NULL isn't passed then the function will
2856 * use that buffer instead.
2857 *
2858 * The address returned by this function will be in network byte order.
2859 *
2860 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
2861 * however it is bit compatible so that a cast will work.
2862 */
2863DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
2864
2865/*@}*/
2866
2867/** @name Ports
2868 * This section contains functions for dealing with port numbers at the
2869 * transport layer.
2870 *
2871 * @{
2872 */
2873
2874/** An indication of which port is the "server" port for a given port pair */
2875typedef enum {
2876        USE_DEST,       /**< Destination port is the server port */
2877        USE_SOURCE      /**< Source port is the server port */
2878} serverport_t;
2879
2880/** Gets the source port for a given packet
2881 * @param packet        The packet to get the source port from
2882 * @return The source port in HOST byte order or 0 if no suitable port number
2883 * can be extracted from the packet.
2884 *
2885 * This function will return 0 if the transport protocol is known not to
2886 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2887 * present in the packet or the transport header has been truncated such that
2888 * the port fields are not readable.
2889 *
2890 * @note If the transport protocol is not known by libtrace, the first two
2891 * bytes of the transport header will be treated as the source port field.
2892 */
2893DLLEXPORT SIMPLE_FUNCTION
2894uint16_t trace_get_source_port(const libtrace_packet_t *packet);
2895
2896/** Gets the destination port for a given packet
2897 * @param packet        The packet to get the destination port from
2898 * @return The destination port in HOST byte order or 0 if no suitable port
2899 * number can be extracted from the packet.
2900 *
2901 * This function will return 0 if the transport protocol is known not to
2902 * use port numbers, e.g. ICMP. 0 is also returned if no transport header is
2903 * present in the packet or the transport header has been truncated such that
2904 * the port fields are not readable.
2905 *
2906 * @note If the transport protocol is not known by libtrace, the third and
2907 * fourth bytes of the transport header will be treated as the destination
2908 * port field.
2909 *
2910 */
2911DLLEXPORT SIMPLE_FUNCTION
2912uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
2913
2914/** Hint at which of the two provided ports is the server port.
2915 *
2916 * @param protocol      The IP layer protocol, eg 6 (tcp), 17 (udp)
2917 * @param source        The source port from the packet
2918 * @param dest          The destination port from the packet
2919 *
2920 * @return one of USE_SOURCE or USE_DEST describing on which of the two ports
2921 * is most likely to be the server port.
2922 *
2923 * @note Ports must be provided in HOST byte order!
2924 *
2925 * This function is based almost entirely on heuristics and should not be
2926 * treated as a definitive means of identifying the server port. However, it
2927 * is deterministic, so it is very handy for identifying both halves of the
2928 * same flow.
2929 */
2930DLLEXPORT SIMPLE_FUNCTION
2931int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
2932
2933/*@}*/
2934
2935/** @name Wireless trace support
2936 * Functions to access wireless information from packets that have wireless
2937 * monitoring headers such as Radiotap or Prism.
2938 *
2939 * The trace_get_wireless_* functions provide an abstract interface for
2940 * retrieving information from wireless traces. They take a pointer to the
2941 * wireless monitoring header (usually found with trace_get_packet_meta()) and
2942 * the linktype of the header passed in.
2943 *
2944 * All of the trace_get_wireless_* functions return false if the requested
2945 * information was unavailable, or true if it was. The actual data is stored
2946 * in an output variable supplied by the caller. Values returned into the
2947 * output variable will always be returned in host byte order.
2948 * @{
2949 */
2950
2951
2952#ifndef ARPHRD_80211_RADIOTAP
2953/** libc doesn't define this yet, so we have to do so ourselves */
2954#define ARPHRD_80211_RADIOTAP 803
2955#endif
2956
2957/** Get the wireless Timer Synchronisation Function
2958 *
2959 * Gets the value of the timer synchronisation function for this frame, which
2960 * is a value in microseconds indicating the time that the first bit of the
2961 * MPDU was received by the MAC.
2962 *
2963 * @param linkptr The wireless meta header
2964 * @param linktype The linktype of the wireless meta header passed in
2965 * @param[out] tsft The value of the timer synchronisation function.
2966 * @return true if the field was available, false if not.
2967 */
2968DLLEXPORT bool trace_get_wireless_tsft(void *linkptr,
2969        libtrace_linktype_t linktype, uint64_t *tsft);
2970
2971/** Get the wireless data rate
2972 *
2973 * @param linkptr The wireless meta header
2974 * @param linktype The linktype of the wireless meta header passed in
2975 * @param[out] rate The data-rate of the frame in units of 500kbps
2976 * @return true if the field was available, false if not.
2977 */
2978DLLEXPORT bool trace_get_wireless_rate(void *linkptr,
2979        libtrace_linktype_t linktype, uint8_t *rate);
2980
2981/** Get the wireless channel frequency
2982 * @param linkptr The wireless meta header
2983 * @param linktype The linktype of the wireless meta header passed in
2984 * @param[out] freq The frequency in MHz of the channel the frame was
2985 * transmitted or received on.
2986 * @return true if the field was available, false if not.
2987 */
2988DLLEXPORT bool trace_get_wireless_freq(void *linkptr,
2989        libtrace_linktype_t linktype, uint16_t *freq);
2990
2991/** Get the wireless signal strength in dBm
2992 * @param linkptr The wireless meta header
2993 * @param linktype The linktype of the wireless meta header passed in
2994 * @param[out] strength The RF signal power at the antenna, in dB difference
2995 * from 1mW.
2996 * @return true if the field was available, false if not.
2997 */
2998DLLEXPORT bool trace_get_wireless_signal_strength_dbm(void *linkptr,
2999        libtrace_linktype_t linktype, int8_t *strength);
3000
3001/** Get the wireless noise strength in dBm
3002 * @param linkptr The wireless meta header
3003 * @param linktype The linktype of the wireless meta header passed in
3004 * @param[out] strength The RF noise power at the antenna, in dB difference
3005 * from 1mW.
3006 * @return true if the field was available, false if not.
3007 */
3008DLLEXPORT bool trace_get_wireless_noise_strength_dbm(void *linkptr,
3009        libtrace_linktype_t linktype, int8_t *strength);
3010
3011/** Get the wireless signal strength in dB
3012 * @param linkptr The wireless meta header
3013 * @param linktype The linktype of the wireless meta header passed in
3014 * @param[out] strength The RF signal power at the antenna, in dB difference
3015 * from a fixed reference.
3016 * @return true if the field was available, false if not.
3017 */
3018DLLEXPORT bool trace_get_wireless_signal_strength_db(void *linkptr,
3019        libtrace_linktype_t linktype, uint8_t *strength);
3020
3021/** Get the wireless noise strength in dB
3022 * @param linkptr The wireless meta header
3023 * @param linktype The linktype of the wireless meta header passed in
3024 * @param[out] strength The RF noise power at the antenna, in dB difference
3025 * from a fixed reference.
3026 * @return true if the field was available, false if not.
3027 */
3028DLLEXPORT bool trace_get_wireless_noise_strength_db(void *linkptr,
3029        libtrace_linktype_t linktype, uint8_t *strength);
3030
3031/** Get the wireless transmit attenuation
3032 * @param linkptr The wireless meta header
3033 * @param linktype The linktype of the wireless meta header passed in
3034 * @param[out] attenuation The transmit power as a unitless distance from
3035 * maximum power set at factory calibration. 0 indicates maximum transmission
3036 * power.
3037 * @return true if the field was available, false if not.
3038 */
3039DLLEXPORT bool trace_get_wireless_tx_attenuation(void *linkptr,
3040        libtrace_linktype_t linktype, uint16_t *attenuation);
3041
3042/** Get the wireless transmit attenuation in dB
3043 * @param linkptr The wireless meta header
3044 * @param linktype The linktype of the wireless meta header passed in
3045 * @param[out] attenuation The transmit power as dB difference from maximum
3046 * power set at factory calibration. 0 indicates maximum power.
3047 * @return true if the field was available, false if not.
3048 */
3049DLLEXPORT bool trace_get_wireless_tx_attenuation_db(void *linkptr,
3050        libtrace_linktype_t linktype, uint16_t *attenuation);
3051
3052/** Get the wireless transmit power in dBm
3053 * @param linkptr The wireless meta header
3054 * @param linktype The linktype of the wireless meta header passed in
3055 * @param[out] txpower The transmit power as dB from a 1mW reference. This is
3056 * the absolute power level measured at the antenna port. 
3057 * @return true if the field was available, false if not.
3058 */
3059DLLEXPORT bool trace_get_wireless_tx_power_dbm(void *linkptr,
3060                libtrace_linktype_t linktype, int8_t *txpower);
3061
3062/** Get the wireless antenna
3063 * @param linkptr The wireless meta header
3064 * @param linktype The linktype of the wireless meta header passed in
3065 * @param[out] antenna The antenna that was used to transmit or receive the
3066 * frame.
3067 * @return true if the field was available, false if not.
3068 */
3069DLLEXPORT bool trace_get_wireless_antenna(void *linkptr,
3070        libtrace_linktype_t linktype, uint8_t *antenna);
3071
3072/*@}*/
3073
3074#ifdef __cplusplus
3075} /* extern "C" */
3076#endif /* #ifdef __cplusplus */
3077#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.