source: lib/libtrace.h.in @ 2f3a25e

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

Use DLT_RAW instead of DLT_NULL when writing out raw IP.
Also don't strip the 4 byte FCS off non-ethernet traces.

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