source: lib/libtrace.h.in @ 0fd4ad6

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

Libtrace now makes the DAG driver version number available
Configure didn't seem to deal very well with machines that didn't have DAG

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