source: lib/libtrace.h.in @ 16da8f3

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

Add ATM support

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