source: lib/libtrace.h.in @ be22b51

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since be22b51 was be22b51, checked in by Shane Alcock <salcock@…>, 14 years ago

Other libtrace programs don't like meta-data so added a new trace_type that would allow them to ignore it without having to do anything special

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