source: lib/libtrace.h.in @ 11c0f44

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