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

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

tracedump now ignores packets that are not actual data packets, particularly RT meta-data packets.
Corrected the calculation of get_capture_length for RT meta-data packets.

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