source: lib/libtrace.h.in @ 4423dc7

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

First pass implementation of loss recording framework.

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