source: lib/libtrace.h.in @ 6399a90

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

Remove wag.h dependences

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