source: lib/libtrace.h.in @ cab58c5

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

Move RT_* to TRACE_RT_* and put it in libtrace.h

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