source: lib/libtrace.h.in @ e1868fb

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

Add attribute((format(printf))) flags to functions that should have it.
Found a nasty bug in trace_get_server_port() where the body of an if() wasn't
correctly {}'d.

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