source: lib/libtrace.h.in @ aa22b5b

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

If we have a complete header, but truncated at the end of it, we shouldn't return NULL from
get_payload_from_* functions so we can distinguish it from a truncated header.

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