source: lib/libtrace.h.in @ a1b899f

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

Libtrace now exports the svn revision number of the distribution
Added two new RT_TYPES that signify server restarts and client connection failures.
Updated the INSTALL documentation.
Added more option combinations to synopt_report.c
The examples directory is no longer treated as EXTRA_DIST in automake

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