source: lib/libtrace.h.in @ edb844c

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

Add the POS tracetype

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