source: lib/libtrace.h.in @ 3ec29a9

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 3ec29a9 was 3ec29a9, checked in by Scott Raynel <smr26@…>, 15 years ago

Decode new Radiotap fields

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