source: lib/libtrace.h.in @ 4bd8a5b

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

Cleanup a gazillion warnings

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