source: lib/libtrace.h.in @ 007fb43

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 007fb43 was 007fb43, checked in by Shane Alcock <salcock@…>, 13 years ago
  • Some systems, such as OpenBSD 3.5, don't have a stdint.h so don't try to include it if we don't have it
  • Property mode set to 100644
File size: 66.9 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_pps
582{
583 uint8_t addres;                /**< PPP Address (0xFF - All stations) */
584 uint8_t header;                /**< PPP Control (0x03 - Unnumbered info) */
585 uint16_t protocol;             /**< PPP Protocol (htons(0x0021) - IPv4 */
586} PACKED libtrace_ppp_t;
587
588/** 802.11 header */
589typedef struct libtrace_80211_t {
590#if BYTE_ORDER == LITTLE_ENDIAN
591        LT_BITFIELD32      protocol:2;
592        LT_BITFIELD32      type:2;
593        LT_BITFIELD32      subtype:4;
594#else
595        LT_BITFIELD32      subtype:4;
596        LT_BITFIELD32      type:2;
597        LT_BITFIELD32      protocol:2;
598#endif
599
600#if BYTE_ORDER == LITTLE_ENDIAN
601        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
602        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
603        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
604        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
605        LT_BITFIELD32      power:1;
606        LT_BITFIELD32      more_data:1;
607        LT_BITFIELD32      wep:1;
608        LT_BITFIELD32      order:1;
609#else
610        LT_BITFIELD32      order:1;
611        LT_BITFIELD32      wep:1;
612        LT_BITFIELD32      more_data:1;
613        LT_BITFIELD32      power:1;
614        LT_BITFIELD32      retry:1;     /**< Packet is a retry */
615        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
616        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
617        LT_BITFIELD32      to_ds:1;     /**< Packet to Distribution Service */
618#endif
619        uint16_t     duration;
620        uint8_t      mac1[6];
621        uint8_t      mac2[6];
622        uint8_t      mac3[6];
623        uint16_t     SeqCtl;
624        uint8_t      mac4[6];
625} PACKED libtrace_80211_t;
626
627/** The Radiotap header pre-amble
628 *
629 * All Radiotap headers start with this pre-amble, followed by the fields
630 * specified in the it_present bitmask. If bit 31 of it_present is set, then
631 * another bitmask follows.
632 * @note All of the radiotap data fields are in little-endian byte-order.
633 */
634typedef struct libtrace_radiotap_t {
635    uint8_t     it_version; /**< Radiotap version */
636    uint8_t     it_pad; /**< Padding for natural alignment */
637    uint16_t    it_len; /**< Length in bytes of the entire Radiotap header */
638    uint32_t    it_present; /**< Which Radiotap fields are present */
639} PACKED libtrace_radiotap_t;
640
641
642#ifdef WIN32
643#pragma pack(pop)
644#endif
645
646
647/*@}*/
648
649/** Prints help information for libtrace
650 *
651 * Function prints out some basic help information regarding libtrace,
652 * and then prints out the help() function registered with each input module
653 */
654DLLEXPORT void trace_help(void);
655
656/** @name Trace management
657 * These members deal with creating, configuring, starting, pausing and
658 * cleaning up a trace object
659 *@{
660 */
661
662/** Create a trace file from a URI
663 *
664 * @param uri containing a valid libtrace URI
665 * @return an opaque pointer to a libtrace_t
666 *
667 * Valid URI's are:
668 *  - erf:/path/to/erf/file
669 *  - erf:-  (stdin)
670 *  - dag:/dev/dagcard                 
671 *  - pcapint:pcapinterface                (eg: pcap:eth0)
672 *  - pcap:/path/to/pcap/file
673 *  - pcap:-
674 *  - rt:hostname
675 *  - rt:hostname:port
676 *  - rtclient:hostname (deprecated)
677 *  - rtclient:hostname:port (deprecated)
678 *
679 *  If an error occured when attempting to open the trace file, an error
680 *  trace is returned and trace_get_error should be called to find out
681 *  if an error occured, and what that error was.  The trace is created in the
682 *  configuration state, you must call trace_start to start the capture.
683 */
684DLLEXPORT libtrace_t *trace_create(const char *uri);
685
686/** Creates a "dummy" trace file that has only the format type set.
687 *
688 * @return an opaque pointer to a (sparsely initialised) libtrace_t
689 *
690 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions
691 * with the dummy trace. Its intended purpose is to act as a packet->trace for
692 * libtrace_packet_t's that are not associated with a libtrace_t structure.
693 */
694DLLEXPORT libtrace_t *trace_create_dead(const char *uri);
695
696/** Creates a trace output file from a URI.
697 *
698 * @param uri   the uri string describing the output format and destination
699 * @return an opaque pointer to a libtrace_output_t
700 *
701 * Valid URI's are:
702 *  - erf:/path/to/erf/file
703 *  - pcap:/path/to/pcap/file
704 *  - wtf:/path/to/wtf/file
705 *
706 *  If an error occured when attempting to open the output trace, NULL is returned
707 *  and trace_errno is set. Use trace_perror_output() to get more information
708 */
709DLLEXPORT libtrace_out_t *trace_create_output(const char *uri);
710
711/** Start the capture
712 * @param libtrace      The trace to start
713 * @return 0 on success, -1 on failure
714 *
715 * This does the actual work with starting the trace capture, and applying
716 * all the config options.  This may fail.
717 */
718DLLEXPORT int trace_start(libtrace_t *libtrace);
719
720/** Pause the capture
721 * @param libtrace      The trace to pause
722 * @return 0 on success, -1 on failure
723 *
724 * This stops a capture in progress and returns you to the configuration
725 * state.  Any packets that arrive after trace_pause() has been called
726 * will be discarded.  To resume capture, call trace_start().
727 */
728DLLEXPORT int trace_pause(libtrace_t *libtrace);
729
730/** Start an output trace
731 * @param libtrace      The trace to start
732 * @return 0 on success, -1 on failure
733 *
734 * This does the actual work with starting a trace for write.  This generally
735 * creates the file.
736 */
737DLLEXPORT int trace_start_output(libtrace_out_t *libtrace);
738
739/** Valid trace capture options */
740typedef enum {
741        TRACE_OPTION_SNAPLEN,   /**< Number of bytes captured */
742        TRACE_OPTION_PROMISC,   /**< Capture packets to other hosts */
743        TRACE_OPTION_FILTER,    /**< Apply this filter to all packets */
744        TRACE_OPTION_META_FREQ, /**< Frequency of meta-data information, e.g. DUCK packets */
745        /** trace_event ignores gaps between packets when reading traces off disk */
746        TRACE_OPTION_EVENT_REALTIME
747} trace_option_t;
748
749/** Sets an input config option
750 * @param libtrace      the trace object to apply the option to
751 * @param option        the option to set
752 * @param value         the value to set the option to
753 * @return -1 if option configuration failed, 0 otherwise
754 * This should be called after trace_create, and before trace_start
755 */
756DLLEXPORT int trace_config(libtrace_t *libtrace,
757                trace_option_t option,
758                void *value);
759
760typedef enum {
761        TRACE_OPTION_OUTPUT_FILEFLAGS, /**< File flags to open the trace file
762                                        * with.  eg O_APPEND
763                                        */
764        TRACE_OPTION_OUTPUT_COMPRESS   /**< Compression level, eg 6. */
765} trace_option_output_t;
766
767/** Sets an output config option
768 *
769 * @param libtrace      the output trace object to apply the option to
770 * @param option        the option to set
771 * @param value         the value to set the option to
772 * @return -1 if option configuration failed, 0 otherwise
773 * This should be called after trace_create_output, and before
774 * trace_start_output
775 */
776DLLEXPORT int trace_config_output(libtrace_out_t *libtrace,
777                trace_option_output_t option,
778                void *value
779                );
780
781/** Close a trace file, freeing up any resources it may have been using
782 *
783 */
784DLLEXPORT void trace_destroy(libtrace_t *trace);
785
786/** Close a trace file, freeing up any resources it may have been using
787 * @param trace         trace file to be destroyed
788 */
789DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
790
791/** Close a trace output file, freeing up any resources it may have been using
792 * @param trace         the output trace file to be destroyed
793 */
794DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
795
796/** Check (and clear) the current error state of an input trace
797 * @param trace         the trace file to check the error state on
798 * @return Error report
799 * This reads and returns the current error state and sets the current error
800 * to "no error".
801 */
802DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
803
804/** Return if there is an error
805 * @param trace         the trace file to check the error state on
806 * This does not clear the error status, and only returns true or false.
807 */
808DLLEXPORT bool trace_is_err(libtrace_t *trace);
809
810/** Output an error message to stderr and clear the error status.
811 * @param trace         the trace with the error to output
812 * @param msg           the message to prefix to the error
813 * This function does clear the error status.
814 */
815DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...) PRINTF(2,3);
816
817/** Check (and clear) the current error state of an output trace
818 * @param trace         the output trace file to check the error state on
819 * @return Error report
820 * This reads and returns the current error state and sets the current error
821 * to "no error".
822 */
823DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
824
825/** Return if there is an error
826 * @param trace         the trace file to check the error state on
827 * This does not clear the error status, and only returns true or false.
828 */
829DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
830
831/** Output an error message to stderr and clear the error status.
832 * @param trace         the trace with the error to output
833 * @param msg           the message to prefix to the error
834 * This function does clear the error status.
835 */
836DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...)
837        PRINTF(2,3);
838
839/** Return the number of captured packets
840 * Includes the number of packets counted as early as possible, before
841 * filtering, and includes dropped packets.
842 *
843 * @param trace         Trace to examine
844 * @returns number of packets seen at the capture point before filtering.
845 *
846 * If this is not known, this will return UINT64_MAX
847 */
848uint64_t trace_get_received_packets(libtrace_t *trace);
849
850/** Return the number of filtered packets
851 * Returns the number of packets that were captured, but discarded for not
852 * matching a trace filter.  This includes packets
853 *
854 * @param trace         Trace file to examine
855 * @returns the number of packets that were successfully captured, but filtered
856 *
857 * If this is not known, this will return UINT64_MAX
858 */
859uint64_t trace_get_filtered_packets(libtrace_t *trace);
860
861/** Return the number of packets that have been dropped for lack of packets
862 * @param trace         Trace file to examine
863 * @returns The number of packets captured, but dropped due to buffer overruns
864 */
865uint64_t trace_get_dropped_packets(libtrace_t *trace);
866
867/** Return the number of packets that have been returned to library user
868 * @param trace         Trace file to examine
869 * @returns The number of packets returned to the user of the library.
870 */
871uint64_t trace_get_accepted_packets(libtrace_t *trace);
872
873
874/*@}*/
875
876/** @name Reading/Writing packets
877 * These members deal with creating, reading and writing packets
878 *
879 * @{
880 */
881
882/** Create a new packet object
883 *
884 * @return a pointer to an initialised libtrace_packet_t object
885 */
886DLLEXPORT libtrace_packet_t *trace_create_packet(void);
887
888/** Copy a packet
889 * @param packet        the source packet to copy
890 * @return a new packet which has the same content as the source packet
891 * @note This always involves a copy, which can be slow.  Use of this
892 * function should be avoided where possible.
893 * @par The reason you would want to use this function is that a zerocopied
894 * packet from a device is using the devices memory which may be a limited
895 * resource.  Copying the packet will cause it to be copied into the systems
896 * memory.
897 */
898DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
899
900/** Destroy a packet object
901 *
902 * sideeffect: sets packet to NULL
903 */
904DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
905
906
907/** Read one packet from the trace
908 *
909 * @param trace         the libtrace opaque pointer
910 * @param packet        the packet opaque pointer
911 * @return 0 on EOF, negative value on error, number of bytes read when
912 * successful.
913 *
914 * @note the number of bytes read is usually (but not always) the same as
915 * trace_get_framing_length()+trace_get_capture_length() depending on the
916 * trace format.
917 * @note the trace must have been started with trace_start before calling
918 * this function
919 */
920DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
921
922/** Event types
923 * see \ref libtrace_eventobj_t and \ref trace_event
924 */
925typedef enum {
926        TRACE_EVENT_IOWAIT,     /**< Need to block on fd */
927        TRACE_EVENT_SLEEP,      /**< Sleep for some time */
928        TRACE_EVENT_PACKET,     /**< packet has arrived */
929        TRACE_EVENT_TERMINATE   /**< End of trace */
930} libtrace_event_t;
931
932/** Structure returned by libtrace_event explaining what the current event is */
933typedef struct libtrace_eventobj_t {
934        libtrace_event_t type; /**< event type (iowait,sleep,packet) */
935        int fd;                /**< if IOWAIT, the fd to sleep on */
936        double seconds;        /**< if SLEEP, the amount of time to sleep for
937                                */
938        int size;              /**< if PACKET, the value returned from
939                                *  trace_read_packet
940                                */
941} libtrace_eventobj_t;
942
943/** Processes the next libtrace event
944 * @param trace the libtrace opaque pointer
945 * @param packet the libtrace_packet opaque pointer
946 * @return libtrace_event struct containing the type, and potential
947 *      fd or seconds to sleep on
948 *
949 * Type can be:
950 *  TRACE_EVENT_IOWAIT  Waiting on I/O on fd
951 *  TRACE_EVENT_SLEEP   Next event in seconds
952 *  TRACE_EVENT_PACKET  Packet arrived in buffer with size size
953 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
954 */
955DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
956                libtrace_packet_t *packet);
957
958
959/** Write one packet out to the output trace
960 *
961 * @param trace         the libtrace_out opaque pointer
962 * @param packet        the packet opaque pointer
963 * @return the number of bytes written out, if zero or negative then an error has occured.
964 */
965DLLEXPORT int trace_write_packet(libtrace_out_t *trace, libtrace_packet_t *packet);
966/*@}*/
967
968/** @name Protocol decodes
969 * These functions locate and return a pointer to various headers inside a
970 * packet
971 * @{
972 */
973
974
975/** Gets a pointer to the first byte of the packet as it was captured and
976 * returns its corresponding linktype and capture length.
977 *
978 * Use this function instead of the deprecated trace_get_link().
979 *
980 * @param packet the packet pointer
981 * @param[out] linktype the linktype of the returned pointer
982 * @param[out] remaining the capture length (the number of captured bytes from
983 * the returned pointer)
984 * @return a pointer to the first byte of the packet
985 */
986DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet,
987                libtrace_linktype_t *linktype, uint32_t *remaining);
988
989/** get a pointer to the link layer
990 * @param packet        the packet opaque pointer
991 *
992 * @return a pointer to the link layer, or NULL if there is no link layer
993 *
994 * @deprecated This function is deprecated: Use trace_get_packet_buffer() or
995 * one of the trace_get_layer*() functions instead.
996 * @note you should call trace_get_link_type to find out what type of link
997 * layer this is
998 */
999DLLEXPORT SIMPLE_FUNCTION DEPRECATED
1000void *trace_get_link(const libtrace_packet_t *packet);
1001
1002/** get a pointer to the IPv4 header (if any)
1003 * @param packet        the packet opaque pointer
1004 *
1005 * @return a pointer to the IPv4 header, or NULL if there is no IPv4 header
1006 *
1007 * You should consider using \ref trace_get_layer3 instead of this function.
1008 */
1009DLLEXPORT SIMPLE_FUNCTION
1010libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
1011
1012/** get a pointer to the IPv6 header (if any)
1013 * @param packet        the packet opaque pointer
1014 *
1015 * @return a pointer to the IPv6 header, or NULL if there is no IPv6 header
1016 *
1017 * You should consider using \ref trace_get_layer3 instead of this function.
1018 */
1019DLLEXPORT SIMPLE_FUNCTION
1020libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet);
1021
1022/** Return a pointer to the first metadata header in a packet, if present.
1023 *
1024 * This function takes a pointer to a libtrace packet and if any metadata
1025 * headers exist, returns a pointer to the first one, along with its
1026 * corresponding linktype.
1027 *
1028 * If no metadata headers exist in the packet, NULL is returned.
1029 *
1030 * A metadata header is a header that was prepended by the capturing device,
1031 * such as a Linux SLL header, or a Radiotap wireless monitoring header.
1032 * Subsequent metadata headers may be accessed with the
1033 * trace_get_payload_from_meta(...) function.
1034 *
1035 * @param packet the libtrace packet
1036 * @param[out] linktype the linktype of the returned metadata header
1037 * @param[out] remaining the number of bytes captured after the returned
1038 * pointer.
1039 * @return a pointer to the first metadata header, or NULL if there are no
1040 * metadata headers present.
1041 *
1042 * remaining may be NULL, however linktype must be provided.
1043 */
1044DLLEXPORT void *trace_get_packet_meta(const libtrace_packet_t *packet,
1045                libtrace_linktype_t *linktype,
1046                uint32_t *remaining);
1047
1048/** Returns the payload of a metadata header.
1049 *
1050 * This function takes a pointer to the start of a metadata header (either
1051 * obtained via trace_get_packet_meta(...) or by a previous call to
1052 * trace_get_payload_from_meta(...)) along with its corresponding linktype and
1053 * returns the payload, i.e. the next header. It will also update the linktype
1054 * parameter to indicate the type of payload.
1055 * 
1056 * If the linktype indicates that the header passed in is not a metadata
1057 * header, the function returns NULL to indicate this. The linktype remains
1058 * unchanged in this case.
1059 *
1060 * This function allows the user to iterate through metadata headers which are
1061 * sometimes present before the actual packet as it was received on the wire.
1062 * Examples of metadata headers include the Linux SLL header and the Radiotap
1063 * wireless monitoring header.
1064 *
1065 * @param[in] meta a pointer to a header
1066 * @param[in,out] linktype the linktype of meta (updated to indicate the
1067 * linktype of the returned header if applicable).
1068 * @param[in,out] remaining the number of bytes after the meta pointer.
1069 * @return a pointer to the payload of the metadata header. If meta is not a
1070 * pointer to a metadata header, NULL is returned and linktype remains
1071 * unchanged.
1072 *
1073 * All parameters are mandatory. NULL will be returned if any are NULL.
1074 */
1075DLLEXPORT void *trace_get_payload_from_meta(const void *meta,
1076                libtrace_linktype_t *linktype,
1077                uint32_t *remaining);
1078
1079
1080/** Get a pointer to the layer 2 header. Generally this is the first byte of the
1081 * packet as it was seen on the wire.
1082 *
1083 * This function takes a libtrace packet and skips over any metadata headers if
1084 * present (such as Linux SLL or Radiotap) and returns a pointer to the first
1085 * byte of the packet that was actually received by the network interface.
1086 *
1087 * @param packet the libtrace packet
1088 * @param[out] linktype the linktype of the returned layer 2 header
1089 * @param[out] remaining the number of bytes left in the packet after the
1090 * returned pointer.
1091 * @return a pointer to the first byte of the packet as it was seen on the
1092 * wire.
1093 *
1094 * remaining may be NULL, otherwise it will be filled in by the function.
1095 */
1096DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet,
1097                libtrace_linktype_t *linktype,
1098                uint32_t *remaining);
1099
1100/** Gets a pointer to the next header given a pointer to a layer2 header
1101 *
1102 * @param l2                    The pointer to the current layer2 header
1103 * @param linktype              The type of the layer2 header
1104 * @param[out] ethertype        An optional output variable of the ethernet type
1105 * @param[in,out] remaining     Optionally updated with the length remaining
1106 *
1107 * @return a pointer to the transport layer header, or NULL if header isn't
1108 * present.
1109 *
1110 * type may be NULL if not needed.
1111 *
1112 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1113 * of bytes captured of the layer2 header and beyond.  It will be decremented
1114 * by the number of bytes skipped to find the payload.
1115 *
1116 */
1117DLLEXPORT void *trace_get_payload_from_layer2(void *l2,
1118                libtrace_linktype_t linktype,
1119                uint16_t *ethertype,
1120                uint32_t *remaining);
1121
1122
1123/** Get a pointer to the layer 3 header.
1124 * @param packet                The packet opaque pointer
1125 * @param[out] ethertype        The ethertype of the layer 3 header
1126 * @param[out] remaining        The amount of space available after this header
1127 *                              has been removed.
1128 *
1129 * @return a pointer to the layer 3 header.
1130 * remaining may be NULL, otherwise it will be set to the number of captured
1131 * bytes after the pointer returned.
1132 */
1133DLLEXPORT
1134void *trace_get_layer3(const libtrace_packet_t *packet,
1135                uint16_t *ethertype, uint32_t *remaining);
1136
1137/** Gets a pointer to the transport layer header (if any)
1138 * @param packet        a pointer to a libtrace_packet structure
1139 * @param[out] proto    transport layer protocol
1140 *
1141 * @return a pointer to the transport layer header, or NULL if there is no
1142 * header
1143 *
1144 * @note proto may be NULL if proto is unneeded.
1145 */
1146DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet,
1147                uint8_t *proto, uint32_t *remaining);
1148
1149/** Gets a pointer to the payload given a pointer to the IP header
1150 * @param ip            The IP Header
1151 * @param[out] proto    An output variable of the IP protocol
1152 * @param[in,out] remaining Updated with the number of bytes remaining
1153 *
1154 * @return a pointer to the transport layer header, or NULL if header isn't
1155 * present.
1156 *
1157 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1158 * of bytes captured of the IP header and beyond.  It will be decremented by
1159 * the length of the IPv4 header (including any options).
1160 *
1161 * proto may be NULL if not needed.
1162 *
1163 * @note This is similar to trace_get_transport_from_ip in libtrace2
1164 */
1165DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
1166                uint32_t *remaining);
1167
1168/** Gets a pointer to the payload given a pointer to the IPv6 header
1169 * @param ipptr         The IPv6 Header
1170 * @param[out] proto    An output variable of the protocol of the next header
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 the IPv6 header
1174 * isn't complete.
1175 *
1176 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1177 * of bytes captured of the IP header and beyond.  It will be decremented by
1178 * this function by the length of the IPV6 header.
1179 *
1180 * proto may be NULL if not needed.
1181 *
1182 */
1183DLLEXPORT void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr,
1184                uint8_t *prot, uint32_t *remaining);
1185
1186/** Gets a pointer to the payload given a pointer to the link header
1187 * @param ip            The link pointer
1188 * @param[out] type     An output variable of the ethernet type
1189 * @param[in,out] remaining Updated with the number of bytes remaining
1190 *
1191 * @return a pointer to the transport layer header, or NULL if header isn't
1192 * present.
1193 *
1194 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1195 * of bytes captured of the linklayer and beyond.  It will be updated after
1196 * this function to the number of bytes remaining after the IP header (and any
1197 * IP options) have been removed.
1198 *
1199 * type may be NULL if not needed.
1200 *
1201 */
1202DLLEXPORT void *trace_get_payload_from_link(void *linkptr,
1203                libtrace_linktype_t linktype,
1204                uint16_t *type, uint32_t *remaining);
1205
1206/** Skips over any 802.1q headers, if present.
1207 * @param ethernet      A pointer to the payload following an ethernet header
1208 * -usually the result of calling trace_get_payload_from_link
1209 * @param[in,out] type  The ethernet type, replaced with the vlan ether type
1210 * @param[in,out] remaining Updated with the number of bytes remaining
1211 *
1212 * @return a pointer to the header beyond the vlan header, if present.
1213 * Otherwise, returns the ethernet payload that was passed in
1214 *
1215 * Remaining may be NULL. If Remaining is not NULL it must point to the number
1216 * of bytes captured past (but not including) the link layer. This function
1217 * will decrement it by the length of the 802.1q headers if present.
1218 *
1219 * Type must point to the value of the ethernet type. Libtrace will assert
1220 * fail if type is NULL.
1221 *
1222 */
1223DLLEXPORT void *trace_get_vlan_payload_from_ethernet_payload(
1224                void *ethernet_payload, uint16_t *type, uint32_t *remaining);
1225
1226/** Gets a pointer to the payload given a pointer to a tcp header
1227 * @param tcp           The tcp Header
1228 * @param[in,out] remaining Updated with the number of bytes remaining
1229 *
1230 * @return a pointer to the tcp payload, or NULL if the payload isn't present.
1231 *
1232 * Remaining may be NULL.  If remaining is not NULL it must point to the number
1233 * of bytes captured of the TCP header and beyond.  It will be decremented by
1234 * this function by the length of the TCP header (including any options).
1235 *
1236 */
1237DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp,
1238                uint32_t *remaining);
1239
1240/** Gets a pointer to the payload given a pointer to a udp header
1241 * @param udp           The udp Header
1242 * @param[in,out] remaining Updated with the number of bytes remaining
1243 *
1244 * @return a pointer to the udp payload, or NULL if the payload isn't present.
1245 *
1246 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1247 * of bytes captured of the UDP header and beyond.  It will be decremented by
1248 * this function to the number of bytes remaining after the UDP header.
1249 *
1250 */
1251DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
1252
1253/** Gets a pointer to the payload given a pointer to a icmp header
1254 * @param icmp          The icmp Header
1255 * @param[in,out] remaining Updated with the number of bytes remaining
1256 *
1257 * @return a pointer to the icmp payload, or NULL if the payload isn't present.
1258 *
1259 * Remaining may be NULL.  If remaining is not NULL it must point to the number
1260 * of bytes captured of the ICMP header and beyond.  It will be decremented
1261 * by the number of bytes in the ICMP header.
1262 *
1263 */
1264DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp,
1265                uint32_t *remaining);
1266
1267/** get a pointer to the TCP header (if any)
1268 * @param packet        the packet opaque pointer
1269 *
1270 * @return a pointer to the TCP header, or NULL if there is not a TCP packet
1271 *
1272 * @note you should probably use trace_get_transport()
1273 */
1274DLLEXPORT SIMPLE_FUNCTION
1275libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
1276
1277/** get a pointer to the TCP header (if any) given a pointer to the IP header
1278 * @param ip            The IP header
1279 * @param[in,out] remaining Updated with the number of bytes remaining
1280 *
1281 * @return a pointer to the TCP header, or NULL if this is not a TCP packet
1282 *
1283 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1284 * of bytes captured of the TCP header and beyond.  It will be decremented by
1285 * the number of bytes in the TCP header (including any TCP options).
1286 *
1287 * @note The last parameter has changed from libtrace2
1288 */
1289DLLEXPORT SIMPLE_FUNCTION
1290libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
1291
1292/** get a pointer to the UDP header (if any)
1293 * @param packet        the packet opaque pointer
1294 *
1295 * @return a pointer to the UDP header, or NULL if this is not a UDP packet
1296 */
1297DLLEXPORT SIMPLE_FUNCTION
1298libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
1299
1300/** get a pointer to the UDP header (if any) given a pointer to the IP header
1301 * @param       ip      The IP header
1302 * @param[in,out] remaining Updated with the number of bytes remaining
1303 *
1304 * @return a pointer to the UDP header, or NULL if this is not an UDP packet
1305 *
1306 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1307 * of bytes captured of the UDP header and beyond.  This function will
1308 * decremented it by the length of the UDP header.
1309 *
1310 * @note Beware the change from libtrace2 from skipped to remaining
1311 */
1312DLLEXPORT SIMPLE_FUNCTION
1313libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1314
1315/** get a pointer to the ICMP header (if any)
1316 * @param packet        the packet opaque pointer
1317 *
1318 * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet
1319 */
1320DLLEXPORT SIMPLE_FUNCTION
1321libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
1322
1323/** get a pointer to the ICMP header (if any) given a pointer to the IP header
1324 * @param ip            The IP header
1325 * @param[in,out] remaining Updated with the number of bytes remaining
1326 *
1327 * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet
1328 *
1329 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
1330 * of bytes captured of the ICMP header and beyond.  It will be decremented by
1331 * the length of the ICMP head in bytes.
1332 *
1333 * @note Beware the change from libtrace2 from skipped to remaining
1334 */
1335DLLEXPORT SIMPLE_FUNCTION
1336libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
1337
1338/** Get the destination MAC address
1339 * @param packet        the packet opaque pointer
1340 * @return a pointer to the destination mac, (or NULL if there is no
1341 * destination MAC)
1342 */
1343DLLEXPORT SIMPLE_FUNCTION
1344uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
1345
1346/** Get the source MAC address
1347 * @param packet        the packet opaque pointer
1348 * @return a pointer to the source mac, (or NULL if there is no source MAC)
1349 */
1350DLLEXPORT SIMPLE_FUNCTION
1351uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
1352
1353/** Get the source IP address
1354 * @param packet        the packet opaque pointer
1355 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
1356 *                      static storage.
1357 * @return NULL if there is no source address, or a sockaddr holding a v4 or v6
1358 * address
1359 */
1360DLLEXPORT SIMPLE_FUNCTION
1361struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
1362                struct sockaddr *addr);
1363
1364/** Get the destination IP address
1365 * @param packet        the packet opaque pointer
1366 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
1367 *                      static storage.
1368 * @return NULL if there is no destination address, or a sockaddr holding a v4
1369 * or v6 address
1370 */
1371DLLEXPORT SIMPLE_FUNCTION
1372struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
1373                struct sockaddr *addr);
1374
1375/*@}*/
1376
1377/** parse an ip or tcp option
1378 * @param[in,out] ptr   the pointer to the current option
1379 * @param[in,out] len   the length of the remaining buffer
1380 * @param[out] type     the type of the option
1381 * @param[out] optlen   the length of the option
1382 * @param[out] data     the data of the option
1383 *
1384 * @return bool true if there is another option (and the fields are filled in)
1385 *               or false if this was the last option.
1386 *
1387 * This updates ptr to point to the next option after this one, and updates
1388 * len to be the number of bytes remaining in the options area.  Type is updated
1389 * to be the code of this option, and data points to the data of this option,
1390 * with optlen saying how many bytes there are.
1391 *
1392 * @note Beware of fragmented packets.
1393 */
1394DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
1395                        unsigned char *type,
1396                        unsigned char *optlen,
1397                        unsigned char **data);
1398
1399
1400/** @name Time
1401 * These functions deal with time that a packet arrived and return it
1402 * in various formats
1403 * @{
1404 */
1405/** Get the current time in DAG time format
1406 * @param packet        the packet opaque pointer
1407 *
1408 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
1409 * past 1970-01-01, the lower 32bits are partial seconds)
1410 */
1411DLLEXPORT SIMPLE_FUNCTION
1412uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
1413
1414/** Get the current time in struct timeval
1415 * @param packet        the packet opaque pointer
1416 *
1417 * @return time that this packet was seen in a struct timeval
1418 */
1419DLLEXPORT SIMPLE_FUNCTION
1420struct timeval trace_get_timeval(const libtrace_packet_t *packet);
1421
1422/** Get the current time in floating point seconds
1423 * @param packet        the packet opaque pointer
1424 *
1425 * @return time that this packet was seen in 64bit floating point seconds from
1426 * the unix epoch (1970-01-01 00:00:00 UTC).
1427 */
1428DLLEXPORT SIMPLE_FUNCTION
1429double trace_get_seconds(const libtrace_packet_t *packet);
1430
1431/** Seek within a trace
1432 * @param trace         trace to seek
1433 * @param seconds       time to seek to
1434 * @return 0 on success.
1435 * Make the next packet read to be the first packet to occur at or after the
1436 * time searched for.  This must be called in the configuration state (ie,
1437 * before trace_start() or after trace_pause().
1438 * @note This function may be extremely slow.
1439 */
1440DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
1441
1442/** Seek within a trace
1443 * @param trace         trace to seek
1444 * @param tv            time to seek to
1445 * @return 0 on success.
1446 * Make the next packet read to be the first packet to occur at or after the
1447 * time searched for.  This must be called in the configuration state (ie,
1448 * before trace_start() or after trace_pause().
1449 * @note This function may be extremely slow.
1450 */
1451DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
1452
1453/** Seek within a trace
1454 * @param trace         trace to seek
1455 * @param ts            erf timestamp
1456 * @return 0 on success.
1457 * Make the next packet read to be the first packet to occur at or after the
1458 * time searched for.  This must be called in the configuration state (ie,
1459 * before trace_start() or after trace_pause().
1460 * @note This function may be extremely slow.
1461 */
1462DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
1463
1464/*@}*/
1465
1466/** @name Sizes
1467 * This section deals with finding or setting the various different lengths
1468 * a packet can have
1469 * @{
1470 */
1471/** Get the size of the packet in the trace (in bytes)
1472 * @param packet        the packet opaque pointer
1473 * @return the size of the packet in the trace
1474 * @note Due to this being a header capture, or anonymisation, this may not
1475 * be the same size as the original packet.  See get_wire_length() for the
1476 * original size of the packet.
1477 * @note This can (and often is) different for different packets in a trace!
1478 * @note This is sometimes called the "snaplen".
1479 * @note The return size refers to the network-level payload of the packet and
1480 * does not include any capture framing headers. For example, an Ethernet
1481 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1482 * sizeof(ip_header) + sizeof(tcp_header), but not the capture file
1483 * (pcap/erf/etc) header.
1484 */
1485DLLEXPORT SIMPLE_FUNCTION
1486size_t trace_get_capture_length(const libtrace_packet_t *packet);
1487
1488/** Get the size of the packet as it was seen on the wire (in bytes).
1489 * @param packet        the packet opaque pointer
1490 * @return the size of the packet as it was on the wire.
1491 * @note Due to the trace being a header capture, or anonymisation this may
1492 * not be the same as the Capture Len.
1493 * @note trace_getwire_length \em{includes} FCS.
1494 * @note The return size refers to the network-level payload of the packet and
1495 * does not include any capture framing headers. For example, an Ethernet
1496 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1497 * sizeof(ip_header) + sizeof(tcp_header), but not the capture file
1498 * (pcap/erf/etc) header.
1499 */
1500DLLEXPORT SIMPLE_FUNCTION
1501size_t trace_get_wire_length(const libtrace_packet_t *packet);
1502
1503/** Get the length of the capture framing headers (in bytes).
1504 * @param packet        the packet opaque pointer
1505 * @return the size of the packet as it was on the wire.
1506 * @note this length corresponds to the difference between the size of a
1507 * captured packet in memory, and the captured length of the packet
1508 */
1509DLLEXPORT SIMPLE_FUNCTION
1510size_t trace_get_framing_length(const libtrace_packet_t *packet);
1511
1512/** Truncate ("snap") the packet at the suggested length
1513 * @param packet        the packet opaque pointer
1514 * @param size          the new length of the packet (in bytes)
1515 * @return the new capture length of the packet, or the original capture
1516 * length of the packet if unchanged
1517 */
1518DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
1519
1520/*@}*/
1521
1522
1523/** Get the type of the link layer
1524 * @param packet        the packet opaque pointer
1525 * @return libtrace_linktype_t
1526 */
1527DLLEXPORT SIMPLE_FUNCTION
1528libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
1529
1530/** Set the direction flag, if it has one
1531 * @param packet        the packet opaque pointer
1532 * @param direction     the new direction
1533 * @returns -1 on error, or the direction that was set.
1534 */
1535DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
1536
1537/** Get the direction flag, if it has one
1538 * @param packet        the packet opaque pointer
1539 * @return a value containing the direction flag, or -1 if this is not supported
1540 * The direction is defined as 0 for packets originating locally (ie, outbound)
1541 * and 1 for packets originating remotely (ie, inbound).
1542 * Other values are possible, which might be overloaded to mean special things
1543 * for a special trace.
1544 */
1545DLLEXPORT SIMPLE_FUNCTION
1546libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
1547
1548/** @name BPF
1549 * This section deals with using Berkley Packet Filters
1550 * @{
1551 */
1552/** setup a BPF filter
1553 * @param filterstring a char * containing the bpf filter string
1554 * @return opaque pointer pointer to a libtrace_filter_t object
1555 * @note The filter is not actually compiled at this point, so no correctness
1556 * tests are performed here. trace_create_filter will always return ok, but
1557 * if the filter is poorly constructed an error will be generated when the
1558 * filter is actually used
1559 */
1560DLLEXPORT SIMPLE_FUNCTION
1561libtrace_filter_t *trace_create_filter(const char *filterstring);
1562
1563/** Setup a BPF filter based on pre-compiled byte-code.
1564 * @param bf_insns      A pointer to the start of the byte-code
1565 * @param bf_len        The number of BPF instructions 
1566 * @returns             an opaque pointer to a libtrace_filter_t object
1567 * @note                The supplied byte-code is not checked for correctness.
1568 * @author              Scott Raynel
1569 */
1570DLLEXPORT libtrace_filter_t *
1571trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len);
1572
1573/** Apply a BPF filter to a packet
1574 * @param filter        the filter opaque pointer
1575 * @param packet        the packet opaque pointer
1576 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
1577 * @note Due to the way BPF filters are built, the filter is not actually
1578 * compiled until the first time trace_create_filter is called. If your filter
1579 * is incorrect, it will generate an error message and assert, exiting the
1580 * program. This behaviour may change to more graceful handling of this error
1581 * in the future.
1582 */
1583DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
1584                const libtrace_packet_t *packet);
1585
1586/** Destroy a BPF filter
1587 * @param filter        the filter opaque pointer
1588 * Deallocate all the resources associated with a BPF filter
1589 */
1590DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
1591/*@}*/
1592
1593/** @name Portability
1594 * This section has functions that causes annoyances to portability for one
1595 * reason or another.
1596 * @{
1597 */
1598
1599/** Convert an ethernet address to a string
1600 * @param addr  Ethernet address in network byte order
1601 * @param buf   Buffer to store the ascii representation, or NULL
1602 * @return buf, or if buf is NULL then a statically allocated buffer.
1603 *
1604 * This function is similar to the GNU ether_ntoa_r function, with a few
1605 * minor differences.  if NULL is passed as buf, then the function will
1606 * use an internal static buffer, if NULL isn't passed then the function
1607 * will use that buffer instead.
1608 *
1609 * @note the type of addr isn't struct ether_addr as it is with ether_ntoa_r,
1610 * however it is bit compatible so that a cast will work.
1611 */
1612DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
1613
1614/** Convert a string to an ethernet address
1615 * @param buf   Ethernet address in hex format delimited with :'s.
1616 * @param addr  buffer to store the binary representation, or NULL
1617 * @return addr, or if addr is NULL, then a statically allocated buffer.
1618 *
1619 * This function is similar to the GNU ether_aton_r function, with a few
1620 * minor differences.  if NULL is passed as addr, then the function will
1621 * use an internal static buffer, if NULL isn't passed then the function will
1622 * use that buffer instead.
1623 *
1624 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
1625 * however it is bit compatible so that a cast will work.
1626 */
1627DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
1628
1629/*@}*/
1630
1631
1632/** Which port is the server port */
1633typedef enum {
1634        USE_DEST,       /**< Destination port is the server port */
1635        USE_SOURCE      /**< Source port is the server port */
1636} serverport_t;
1637
1638/** Get the source port
1639 * @param packet        the packet to read from
1640 * @return a port in \em HOST byte order, or equivalent to ports for this
1641 * protocol, or 0 if this protocol has no ports.
1642 */
1643DLLEXPORT SIMPLE_FUNCTION
1644uint16_t trace_get_source_port(const libtrace_packet_t *packet);
1645
1646/** Get the destination port
1647 * @param packet        the packet to read from
1648 * @return a port in \em HOST byte order, or equivilent to ports for this
1649 * protocol, or 0 if this protocol has no ports.
1650 */
1651DLLEXPORT SIMPLE_FUNCTION
1652uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
1653
1654/** hint at the server port in specified protocol
1655 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
1656 * @param source        the source port from the packet
1657 * @param dest          the destination port from the packet
1658 * @return one of USE_SOURCE or USE_DEST depending on which one you should use
1659 * @note ports must be in \em HOST byte order!
1660 */
1661DLLEXPORT SIMPLE_FUNCTION
1662int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
1663
1664/** Takes a uri and splits it into a format and uridata component.
1665 * @param uri           the uri to be parsed
1666 * @param format        destination location for the format component of the uri
1667 * @return 0 if an error occured, otherwise return the uridata component
1668 */
1669DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
1670
1671/** Gets the format type for a given packet.
1672 * @param packet        the packet opaque pointer
1673 * @return the format of the packet
1674 */
1675DLLEXPORT
1676enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1677
1678/** Construct a packet from a buffer.
1679 * @param packet[in,out]        Libtrace Packet object to update with the new
1680 *                              data.
1681 * @param linktype              The linktype of the packet.
1682 * @param[in] data              The packet data (including linklayer)
1683 * @param len                   Length of packet data
1684 */
1685DLLEXPORT
1686void trace_construct_packet(libtrace_packet_t *packet,
1687                libtrace_linktype_t linktype, const void *data, uint16_t len);
1688
1689/*@}*/
1690
1691/** @name Wireless trace support
1692 * Functions to access wireless information from packets that have wireless
1693 * monitoring headers such as Radiotap or Prism.
1694 *
1695 * The trace_get_wireless_* functions provide an abstract interface for
1696 * retrieving information from wireless traces. They take a pointer to the
1697 * wireless monitoring header (usually found with trace_get_link(packet)) and
1698 * the linktype of the header passed in.
1699 *
1700 * All of the trace_get_wireless_* functions return false if the requested
1701 * information was unavailable, or true if it was. The actual data is stored
1702 * in an output variable supplied by the caller. Values returned into the
1703 * output variable will always be returned in host byte order.
1704 * @{
1705 */
1706
1707
1708#ifndef ARPHRD_80211_RADIOTAP
1709/* libc doesn't define this yet, but it seems to be what everyone is using
1710 */
1711#define ARPHRD_80211_RADIOTAP 803
1712#endif
1713
1714/** Get the wireless Timer Syncronisation Function
1715 *
1716 * Gets the value of the timer syncronisation function for this frame, which
1717 * is a value in microseconds indicating the time that the first bit of the
1718 * MPDU was received by the MAC.
1719 *
1720 * @param link the wireless header
1721 * @param linktype the linktype of the wireless header passed in
1722 * @param[out] tsft the value of the timer syncronisation function.
1723 * @return true if the field was available, false if not.
1724 */
1725DLLEXPORT bool trace_get_wireless_tsft(void *linkptr,
1726        libtrace_linktype_t linktype, uint64_t *tsft);
1727
1728/** Get the wireless rate
1729 * @param link the wireless header
1730 * @param linktype the linktype of the wireless header passed in
1731 * @param[out] rate the data-rate of the frame in units of 500kbps
1732 * @return true if the field was available, false if not.
1733 */
1734DLLEXPORT bool trace_get_wireless_rate(void *linkptr,
1735        libtrace_linktype_t linktype, uint8_t *rate);
1736
1737/** Get the wireless channel frequency
1738 * @param link the wireless header
1739 * @param linktype the linktype of the wireless header passed in
1740 * @param[out] freq the frequency in MHz of the channel the frame was transmitted
1741 * or received on.
1742 * @return true if the field was available, false if not.
1743 */
1744DLLEXPORT bool trace_get_wireless_freq(void *linkptr,
1745        libtrace_linktype_t linktype, uint16_t *freq);
1746
1747/** Get the wireless signal strength in dBm
1748 * @param link the wireless header
1749 * @param linktype the linktype of the wireless header passed in
1750 * @param[out] strength the RF signal power at the antenna, in dB difference
1751 * from 1mW.
1752 * @return true if the field was available, false if not.
1753 */
1754DLLEXPORT bool trace_get_wireless_signal_strength_dbm(void *linkptr,
1755        libtrace_linktype_t linktype, int8_t *strength);
1756
1757/** Get the wireless noise strength in dBm
1758 * @param link the wireless header
1759 * @param linktype the linktype of the wireless header passed in
1760 * @param[out] strength the RF noise power at the antenna, in dB difference
1761 * from 1mW.
1762 * @return true if the field was available, false if not.
1763 */
1764DLLEXPORT bool trace_get_wireless_noise_strength_dbm(void *linkptr,
1765        libtrace_linktype_t linktype, int8_t *strength);
1766
1767/** Get the wireless signal strength in dB
1768 * @param link the wireless header
1769 * @param linktype the linktype of the wireless header passed in
1770 * @param[out] strength the RF signal power at the antenna,in dB difference
1771 * from a fixed reference.
1772 * @return true if the field was available, false if not.
1773 */
1774DLLEXPORT bool trace_get_wireless_signal_strength_db(void *linkptr,
1775        libtrace_linktype_t linktype, uint8_t *strength);
1776
1777/** Get the wireless noise strength in dB
1778 * @param link the wireless header
1779 * @param linktype the linktype of the wireless header passed in
1780 * @param[out] strength the RF noise power at the antenna, in dB difference
1781 * from a fixed reference.
1782 * @return true if the field was available, false if not.
1783 */
1784DLLEXPORT bool trace_get_wireless_noise_strength_db(void *linkptr,
1785        libtrace_linktype_t linktype, uint8_t *strength);
1786
1787/** Get the wireless transmit attenuation
1788 * @param link the wireless header
1789 * @param linktype the linktype of the wireless header passed in
1790 * @param[out] attenuation the transmit power as a unitless distance from maximum
1791 * power set at factory calibration. 0 indicates maximum transmission power.
1792 * @return true if the field was available, false if not.
1793 */
1794DLLEXPORT bool trace_get_wireless_tx_attenuation(void *linkptr,
1795        libtrace_linktype_t linktype, uint16_t *attenuation);
1796
1797/** Get the wireless transmit attenuation in dB
1798 * @param link the wireless header
1799 * @param linktype the linktype of the wireless header passed in
1800 * @param[out] attenuation the transmit power as dB difference from maximum power
1801 * set at factory calibration. 0 indicates maximum power.
1802 * @return true if the field was available, false if not.
1803 */
1804DLLEXPORT bool trace_get_wireless_tx_attenuation_db(void *linkptr,
1805        libtrace_linktype_t linktype, uint16_t *attenuation);
1806
1807/** Get the wireless transmit power in dBm @param link the wireless header
1808 * @param linktype the linktype of the wireless header passed in
1809 * @param[out] txpower the transmit power as dB from a 1mW reference. This is the absolute power level measured at the antenna port. 
1810 * @return true if the field was available, false if not.
1811 */
1812DLLEXPORT bool trace_get_wireless_tx_power_dbm(void *linkptr, libtrace_linktype_t
1813                linktype, int8_t *txpower);
1814
1815/** Get the wireless antenna
1816 * @param link the wireless header
1817 * @param linktype the linktype of the wireless header passed in
1818 * @param[out] antenna which antenna was used to transmit or receive the frame.
1819 * @return true if the field was available, false if not.
1820 */
1821DLLEXPORT bool trace_get_wireless_antenna(void *linkptr,
1822        libtrace_linktype_t linktype, uint8_t *antenna);
1823
1824/*@}*/
1825
1826#ifdef __cplusplus
1827} /* extern "C" */
1828#endif /* #ifdef __cplusplus */
1829#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.