source: lib/libtrace.h.in @ b7a328d

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

Make sure all symbols are exported correctly

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