source: lib/libtrace.h.in @ 8fbd1e6

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

Update documentation to explain the unix epoch for trace get seconds

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