source: lib/libtrace.h.in @ 7e758fc

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 7e758fc was 7e758fc, checked in by Shane Alcock <salcock@…>, 15 years ago

Exported trace_get_payload_from_ip6 as wdcap now uses it

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