source: lib/libtrace.h.in @ 10a46bd

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

Squash warning

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