source: lib/libtrace.h.in @ 6df012d

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 6df012d was 6df012d, checked in by Scott Raynel <smr26@…>, 15 years ago

Fix get_payload_from_link for prism and radiotap.
Make sure to use bswap when looking at radiotap length.
Tidy up exported API, don't export get_payload_from_prism or _from_radiotap.

  • Property mode set to 100644
File size: 55.0 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 link header
855 * @param ip            The link pointer
856 * @param[out] type     An output variable of the ethernet type
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 header isn't
860 * present.
861 *
862 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
863 * of bytes captured of the linklayer and beyond.  It will be updated after
864 * this function to the number of bytes remaining after the IP header (and any
865 * IP options) have been removed.
866 *
867 * type may be NULL if not needed.
868 *
869 */
870DLLEXPORT void *trace_get_payload_from_link(void *link,
871                libtrace_linktype_t linktype,
872                uint16_t *type, uint32_t *remaining);
873
874/** Gets a pointer to the payload given a pointer to a tcp header
875 * @param tcp           The tcp Header
876 * @param[in,out] remaining Updated with the number of bytes remaining
877 *
878 * @return a pointer to the tcp payload, or NULL if the payload isn't present.
879 *
880 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
881 * of bytes captured of the TCP header and beyond.  It will be updated after this
882 * function to the number of bytes remaining after the TCP header (and any TCP options)
883 * have been removed.
884 *
885 * @note This is similar to trace_get_transport_from_ip in libtrace2
886 */
887DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining);
888
889/** Gets a pointer to the payload given a pointer to a udp header
890 * @param udp           The udp Header
891 * @param[in,out] remaining Updated with the number of bytes remaining
892 *
893 * @return a pointer to the udp payload, or NULL if the payload isn't present.
894 *
895 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
896 * of bytes captured of the TCP header and beyond.  It will be updated after this
897 * function to the number of bytes remaining after the TCP header (and any TCP options)
898 * have been removed.
899 *
900 * @note This is similar trace_get_transport_from_ip in libtrace2
901 */
902DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
903
904/** Gets a pointer to the payload given a pointer to a icmp header
905 * @param icmp          The icmp Header
906 * @param[in,out] remaining Updated with the number of bytes remaining
907 *
908 * @return a pointer to the icmp payload, or NULL if the payload isn't present.
909 *
910 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
911 * of bytes captured of the TCP header and beyond.  It will be updated after this
912 * function to the number of bytes remaining after the TCP header (and any TCP options)
913 * have been removed.
914 *
915 * @note This is similar to trace_get_payload_from_icmp in libtrace2
916 */
917DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *remaining);
918
919/** get a pointer to the TCP header (if any)
920 * @param packet        the packet opaque pointer
921 *
922 * @return a pointer to the TCP header, or NULL if there is not a TCP packet
923 */
924DLLEXPORT SIMPLE_FUNCTION
925libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
926
927/** get a pointer to the TCP header (if any) given a pointer to the IP header
928 * @param ip            The IP header
929 * @param[in,out] remaining Updated with the number of bytes remaining
930 *
931 * @return a pointer to the TCP header, or NULL if this is not a TCP packet
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 The last parameter has changed from libtrace2
939 */
940DLLEXPORT SIMPLE_FUNCTION
941libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
942
943/** get a pointer to the UDP header (if any)
944 * @param packet        the packet opaque pointer
945 *
946 * @return a pointer to the UDP header, or NULL if this is not a UDP packet
947 */
948DLLEXPORT SIMPLE_FUNCTION
949libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
950
951/** get a pointer to the UDP header (if any) given a pointer to the IP header
952 * @param       ip      The IP header
953 * @param[in,out] remaining Updated with the number of bytes remaining
954 *
955 * @return a pointer to the UDP header, or NULL if this is not an UDP packet
956 *
957 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
958 * of bytes captured of the TCP header and beyond.  It will be updated after this
959 * function to the number of bytes remaining after the TCP header (and any TCP options)
960 * have been removed.
961 *
962 * @note Beware the change from libtrace2 from skipped to remaining
963 */
964DLLEXPORT SIMPLE_FUNCTION
965libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
966
967/** get a pointer to the ICMP header (if any)
968 * @param packet        the packet opaque pointer
969 *
970 * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet
971 */
972DLLEXPORT SIMPLE_FUNCTION
973libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
974
975/** get a pointer to the ICMP header (if any) given a pointer to the IP header
976 * @param ip            The IP header
977 * @param[in,out] remaining Updated with the number of bytes remaining
978 *
979 * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet
980 *
981 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
982 * of bytes captured of the TCP header and beyond.  It will be updated after this
983 * function to the number of bytes remaining after the TCP header (and any TCP options)
984 * have been removed.
985 *
986 * @note Beware the change from libtrace2 from skipped to remaining
987 */
988DLLEXPORT SIMPLE_FUNCTION
989libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
990
991/** Get the destination MAC address
992 * @param packet        the packet opaque pointer
993 * @return a pointer to the destination mac, (or NULL if there is no
994 * destination MAC)
995 */
996DLLEXPORT SIMPLE_FUNCTION
997uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
998
999/** Get the source MAC address
1000 * @param packet        the packet opaque pointer
1001 * @return a pointer to the source mac, (or NULL if there is no source MAC)
1002 */
1003DLLEXPORT SIMPLE_FUNCTION
1004uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
1005
1006/** Get the source IP address
1007 * @param packet        the packet opaque pointer
1008 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
1009 *                      static storage.
1010 * @return NULL if there is no source address, or a sockaddr holding a v4 or v6 address
1011 */
1012DLLEXPORT SIMPLE_FUNCTION
1013struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
1014                struct sockaddr *addr);
1015
1016/** Get the destination IP address
1017 * @param packet        the packet opaque pointer
1018 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
1019 *                      static storage.
1020 * @return NULL if there is no destination address, or a sockaddr holding a v4 or v6 address
1021 */
1022DLLEXPORT SIMPLE_FUNCTION
1023struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
1024                struct sockaddr *addr);
1025
1026/*@}*/
1027
1028/** parse an ip or tcp option
1029 * @param[in,out] ptr   the pointer to the current option
1030 * @param[in,out] len   the length of the remaining buffer
1031 * @param[out] type     the type of the option
1032 * @param[out] optlen   the length of the option
1033 * @param[out] data     the data of the option
1034 *
1035 * @return bool true if there is another option (and the fields are filled in)
1036 *               or false if this was the last option.
1037 *
1038 * This updates ptr to point to the next option after this one, and updates
1039 * len to be the number of bytes remaining in the options area.  Type is updated
1040 * to be the code of this option, and data points to the data of this option,
1041 * with optlen saying how many bytes there are.
1042 *
1043 * @note Beware of fragmented packets.
1044 */
1045DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
1046                        unsigned char *type,
1047                        unsigned char *optlen,
1048                        unsigned char **data);
1049
1050
1051/** @name Time
1052 * These functions deal with time that a packet arrived and return it
1053 * in various formats
1054 * @{
1055 */
1056/** Get the current time in DAG time format
1057 * @param packet        the packet opaque pointer
1058 *
1059 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
1060 * past 1970-01-01, the lower 32bits are partial seconds)
1061 */
1062DLLEXPORT SIMPLE_FUNCTION
1063uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
1064
1065/** Get the current time in struct timeval
1066 * @param packet        the packet opaque pointer
1067 *
1068 * @return time that this packet was seen in a struct timeval
1069 */
1070DLLEXPORT SIMPLE_FUNCTION
1071struct timeval trace_get_timeval(const libtrace_packet_t *packet);
1072
1073/** Get the current time in floating point seconds
1074 * @param packet        the packet opaque pointer
1075 *
1076 * @return time that this packet was seen in 64bit floating point seconds
1077 */
1078DLLEXPORT SIMPLE_FUNCTION
1079double trace_get_seconds(const libtrace_packet_t *packet);
1080
1081/** Seek within a trace
1082 * @param trace         trace to seek
1083 * @param seconds       time to seek to
1084 * @return 0 on success.
1085 * Make the next packet read to be the first packet to occur at or after the
1086 * time searched for.  This must be called in the configuration state (ie,
1087 * before trace_start() or after trace_pause().
1088 * @note This function may be extremely slow.
1089 */
1090DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
1091
1092/** Seek within a trace
1093 * @param trace         trace to seek
1094 * @param tv            time to seek to
1095 * @return 0 on success.
1096 * Make the next packet read to be the first packet to occur at or after the
1097 * time searched for.  This must be called in the configuration state (ie,
1098 * before trace_start() or after trace_pause().
1099 * @note This function may be extremely slow.
1100 */
1101DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
1102
1103/** Seek within a trace
1104 * @param trace         trace to seek
1105 * @param ts            erf timestamp
1106 * @return 0 on success.
1107 * Make the next packet read to be the first packet to occur at or after the
1108 * time searched for.  This must be called in the configuration state (ie,
1109 * before trace_start() or after trace_pause().
1110 * @note This function may be extremely slow.
1111 */
1112DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
1113
1114/*@}*/
1115
1116/** @name Sizes
1117 * This section deals with finding or setting the various different lengths
1118 * a packet can have
1119 * @{
1120 */
1121/** Get the size of the packet in the trace
1122 * @param packet        the packet opaque pointer
1123 * @return the size of the packet in the trace
1124 * @note Due to this being a header capture, or anonymisation, this may not
1125 * be the same size as the original packet.  See get_wire_length() for the
1126 * original size of the packet.
1127 * @note This can (and often is) different for different packets in a trace!
1128 * @note This is sometimes called the "snaplen".
1129 * @note The return size refers to the network-level payload of the packet and
1130 * does not include any capture framing headers. For example, an Ethernet
1131 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1132 * sizeof(ip_header) + sizeof(tcp_header).
1133 */
1134DLLEXPORT SIMPLE_FUNCTION
1135size_t trace_get_capture_length(const libtrace_packet_t *packet);
1136
1137/** Get the size of the packet as it was seen on the wire.
1138 * @param packet        the packet opaque pointer
1139 * @return the size of the packet as it was on the wire.
1140 * @note Due to the trace being a header capture, or anonymisation this may
1141 * not be the same as the Capture Len.
1142 * @note trace_getwire_length \em{includes} FCS.
1143 */
1144DLLEXPORT SIMPLE_FUNCTION
1145size_t trace_get_wire_length(const libtrace_packet_t *packet);
1146
1147/** Get the length of the capture framing headers.
1148 * @param packet        the packet opaque pointer
1149 * @return the size of the packet as it was on the wire.
1150 * @note this length corresponds to the difference between the size of a
1151 * captured packet in memory, and the captured length of the packet
1152 */
1153DLLEXPORT SIMPLE_FUNCTION
1154size_t trace_get_framing_length(const libtrace_packet_t *packet);
1155
1156/** Truncate ("snap") the packet at the suggested length
1157 * @param packet        the packet opaque pointer
1158 * @param size          the new length of the packet
1159 * @return the new capture length of the packet, or the original capture
1160 * length of the packet if unchanged
1161 */
1162DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
1163
1164/*@}*/
1165
1166
1167/** Get the type of the link layer
1168 * @param packet        the packet opaque pointer
1169 * @return libtrace_linktype_t
1170 */
1171DLLEXPORT SIMPLE_FUNCTION
1172libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
1173
1174/** Set the direction flag, if it has one
1175 * @param packet        the packet opaque pointer
1176 * @param direction     the new direction
1177 * @returns -1 on error, or the direction that was set.
1178 */
1179DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
1180
1181/** Get the direction flag, if it has one
1182 * @param packet        the packet opaque pointer
1183 * @return a value containing the direction flag, or -1 if this is not supported
1184 * The direction is defined as 0 for packets originating locally (ie, outbound)
1185 * and 1 for packets originating remotely (ie, inbound).
1186 * Other values are possible, which might be overloaded to mean special things
1187 * for a special trace.
1188 */
1189DLLEXPORT SIMPLE_FUNCTION
1190libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
1191
1192/** @name BPF
1193 * This section deals with using Berkley Packet Filters
1194 * @{
1195 */
1196/** setup a BPF filter
1197 * @param filterstring a char * containing the bpf filter string
1198 * @return opaque pointer pointer to a libtrace_filter_t object
1199 * @note The filter is not actually compiled at this point, so no correctness
1200 * tests are performed here. trace_create_filter will always return ok, but
1201 * if the filter is poorly constructed an error will be generated when the
1202 * filter is actually used
1203 */
1204DLLEXPORT SIMPLE_FUNCTION
1205libtrace_filter_t *trace_create_filter(const char *filterstring);
1206
1207/** apply a BPF filter
1208 * @param filter        the filter opaque pointer
1209 * @param packet        the packet opaque pointer
1210 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
1211 * @note Due to the way BPF filters are built, the filter is not actually
1212 * compiled until the first time trace_create_filter is called. If your filter
1213 * is incorrect, it will generate an error message and assert, exiting the
1214 * program. This behaviour may change to more graceful handling of this error
1215 * in the future.
1216 */
1217DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
1218                const libtrace_packet_t *packet);
1219
1220/** destory of BPF filter
1221 * @param filter        the filter opaque pointer
1222 * Deallocate all the resources associated with a BPF filter
1223 */
1224DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
1225/*@}*/
1226
1227/** @name Portability
1228 * This section has functions that causes annoyances to portability for one
1229 * reason or another.
1230 * @{
1231 */
1232
1233/** Convert an ethernet address to a string
1234 * @param addr  Ethernet address in network byte order
1235 * @param buf   Buffer to store the ascii representation, or NULL
1236 * @return buf, or if buf is NULL then a statically allocated buffer.
1237 *
1238 * This function is similar to the GNU ether_ntoa_r function, with a few
1239 * minor differences.  if NULL is passed as buf, then the function will
1240 * use an internal static buffer, if NULL isn't passed then the function
1241 * will use that buffer instead.
1242 *
1243 * @note the type of addr isn't struct ether_addr as it is with ether_ntoa_r,
1244 * however it is bit compatible so that a cast will work.
1245 */
1246DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
1247
1248/** Convert a string to an ethernet address
1249 * @param buf   Ethernet address in hex format delimited with :'s.
1250 * @param addr  buffer to store the binary representation, or NULL
1251 * @return addr, or if addr is NULL, then a statically allocated buffer.
1252 *
1253 * This function is similar to the GNU ether_aton_r function, with a few
1254 * minor differences.  if NULL is passed as addr, then the function will
1255 * use an internal static buffer, if NULL isn't passed then the function will
1256 * use that buffer instead.
1257 *
1258 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
1259 * however it is bit compatible so that a cast will work.
1260 */
1261DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
1262
1263/*@}*/
1264
1265
1266/** Which port is the server port */
1267typedef enum {
1268        USE_DEST,       /**< Destination port is the server port */
1269        USE_SOURCE      /**< Source port is the server port */
1270} serverport_t;
1271
1272/** Get the source port
1273 * @param packet        the packet to read from
1274 * @return a port in \em HOST byte order, or equivalent to ports for this
1275 * protocol, or 0 if this protocol has no ports.
1276 */
1277DLLEXPORT SIMPLE_FUNCTION
1278uint16_t trace_get_source_port(const libtrace_packet_t *packet);
1279
1280/** Get the destination port
1281 * @param packet        the packet to read from
1282 * @return a port in \em HOST byte order, or equivilent to ports for this
1283 * protocol, or 0 if this protocol has no ports.
1284 */
1285DLLEXPORT SIMPLE_FUNCTION
1286uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
1287
1288/** hint at the server port in specified protocol
1289 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
1290 * @param source        the source port from the packet
1291 * @param dest          the destination port from the packet
1292 * @return one of USE_SOURCE or USE_DEST depending on which one you should use
1293 * @note ports must be in \em HOST byte order!
1294 */
1295DLLEXPORT SIMPLE_FUNCTION
1296int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
1297
1298/** Takes a uri and splits it into a format and uridata component.
1299 * @param uri           the uri to be parsed
1300 * @param format        destination location for the format component of the uri
1301 * @return 0 if an error occured, otherwise return the uridata component
1302 */
1303DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
1304
1305/** RT protocol base format identifiers
1306 * This is used to say what kind of packet is being sent over the rt protocol
1307 */
1308enum base_format_t {
1309        TRACE_FORMAT_ERF          =1,
1310        TRACE_FORMAT_PCAP         =2,
1311        TRACE_FORMAT_PCAPFILE     =3,
1312        TRACE_FORMAT_WAG          =4,
1313        TRACE_FORMAT_RT           =5,
1314        TRACE_FORMAT_LEGACY_ATM   =6,
1315        TRACE_FORMAT_LEGACY_POS   =7,
1316        TRACE_FORMAT_LEGACY_ETH   =8,
1317        TRACE_FORMAT_LINUX_NATIVE =9,
1318        TRACE_FORMAT_DUCK         =10
1319};
1320
1321/** Gets the format type for a given packet.
1322 * @param packet        the packet opaque pointer
1323 * @return the format of the packet
1324 */
1325DLLEXPORT
1326enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1327
1328/** Construct a packet from a buffer.
1329 * @param packet[in,out]        Libtrace Packet object to update with the new
1330 *                              data.
1331 * @param linktype              The linktype of the packet.
1332 * @param[in] data              The packet data (including linklayer)
1333 * @param len                   Length of packet data
1334 */
1335DLLEXPORT
1336void trace_construct_packet(libtrace_packet_t *packet,
1337                libtrace_linktype_t linktype, const void *data, uint16_t len);
1338
1339/*@}*/
1340
1341/** @name Wireless trace support
1342 * Functions to access wireless information from packets that have wireless
1343 * monitoring headers such as Radiotap or Prism.
1344 *
1345 * The trace_get_wireless_* functions provide an abstract interface for
1346 * retrieving information from wireless traces. They take a pointer to the
1347 * wireless monitoring header (usually found with trace_get_link(packet)) and
1348 * the linktype of the header passed in.
1349 *
1350 * All of the trace_get_wireless_* functions return false if the requested
1351 * information was unavailable, or true if it was. The actual data is stored
1352 * in an output variable supplied by the caller. Values returned into the
1353 * output variable will always be returned in host byte order.
1354 * @{
1355 */
1356
1357
1358#ifndef ARPHRD_80211_RADIOTAP
1359/* libc doesn't define this yet, but it seems to be what everyone is using
1360 */
1361#define ARPHRD_80211_RADIOTAP 803
1362#endif
1363
1364/** Get the wireless Timer Syncronisation Function
1365 *
1366 * Gets the value of the timer syncronisation function for this frame, which
1367 * is a value in microseconds indicating the time that the first bit of the
1368 * MPDU was received by the MAC.
1369 *
1370 * @param link the wireless header
1371 * @param linktype the linktype of the wireless header passed in
1372 * @param[out] tsft the value of the timer syncronisation function.
1373 * @return true if the field was available, false if not.
1374 */
1375DLLEXPORT bool trace_get_wireless_tsft(void *link,
1376        libtrace_linktype_t linktype, uint64_t *tsft);
1377
1378/** Get the wireless flags
1379 * @param link the wireless header
1380 * @param linktype the linktype of the wireless header passed in
1381 * @param[out] flags the wireless flags.
1382 * @return true if the field was available, false if not.
1383 */
1384DLLEXPORT bool trace_get_wireless_flags(void *link,
1385        libtrace_linktype_t linktype, uint8_t *flags);
1386
1387/** Get the wireless rate
1388 * @param link the wireless header
1389 * @param linktype the linktype of the wireless header passed in
1390 * @param[out] rate the data-rate of the frame in units of 500kbps
1391 * @return true if the field was available, false if not.
1392 */
1393DLLEXPORT bool trace_get_wireless_rate(void *link,
1394        libtrace_linktype_t linktype, uint8_t *rate);
1395
1396/** Get the wireless channel frequency
1397 * @param link the wireless header
1398 * @param linktype the linktype of the wireless header passed in
1399 * @param[out] freq the frequency in MHz of the channel the frame was transmitted
1400 * or received on.
1401 * @return true if the field was available, false if not.
1402 */
1403DLLEXPORT bool trace_get_wireless_freq(void *link,
1404        libtrace_linktype_t linktype, uint16_t *freq);
1405
1406/** Get the wireless channel flags
1407 * @param link the wireless header
1408 * @param linktype the linktype of the wireless header passed in
1409 * @param[out] flags the channel flags
1410 * @return true if the field was available, false if not.
1411 */
1412DLLEXPORT bool trace_get_wireless_channel_flags(void *link,
1413        libtrace_linktype_t linktype, uint16_t *flags);
1414
1415/** Get the wireless FHSS Hop Set
1416 * @param link the wireless header
1417 * @param linktype the linktype of the wireless header passed in
1418 * @param[out] hopset the fhss hop set
1419 * @return true if the field was available, false if not.
1420 */
1421DLLEXPORT bool trace_get_wireless_fhss_hopset(void *link,
1422        libtrace_linktype_t linktype, uint8_t *hopset);
1423       
1424/** Get the wireless FHSS Hop Pattern
1425 * @param link the wireless header
1426 * @param linktype the linktype of the wireless header passed in
1427 * @param[out] hoppattern the fhss hoppattern
1428 * @return true if the field was available, false if not.
1429 */
1430DLLEXPORT bool trace_get_wireless_fhss_hoppattern(void *link,
1431        libtrace_linktype_t linktype, uint8_t *hoppattern);
1432
1433/** Get the wireless signal strength in dBm
1434 * @param link the wireless header
1435 * @param linktype the linktype of the wireless header passed in
1436 * @param[out] strength the RF signal power at the antenna, in dB difference
1437 * from 1mW.
1438 * @return true if the field was available, false if not.
1439 */
1440DLLEXPORT bool trace_get_wireless_signal_strength_dbm(void *link,
1441        libtrace_linktype_t linktype, int8_t *strength);
1442
1443/** Get the wireless noise strength in dBm
1444 * @param link the wireless header
1445 * @param linktype the linktype of the wireless header passed in
1446 * @param[out] strength the RF noise power at the antenna, in dB difference
1447 * from 1mW.
1448 * @return true if the field was available, false if not.
1449 */
1450DLLEXPORT bool trace_get_wireless_noise_strength_dbm(void *link,
1451        libtrace_linktype_t linktype, int8_t *strength);
1452
1453/** Get the wireless signal strength in dB
1454 * @param link the wireless header
1455 * @param linktype the linktype of the wireless header passed in
1456 * @param[out] strength the RF signal power at the antenna,in dB difference
1457 * from a fixed reference.
1458 * @return true if the field was available, false if not.
1459 */
1460DLLEXPORT bool trace_get_wireless_signal_strength_db(void *link,
1461        libtrace_linktype_t linktype, uint8_t *strength);
1462
1463/** Get the wireless noise strength in dB
1464 * @param link the wireless header
1465 * @param linktype the linktype of the wireless header passed in
1466 * @param[out] strength the RF noise power at the antenna, in dB difference
1467 * from a fixed reference.
1468 * @return true if the field was available, false if not.
1469 */
1470DLLEXPORT bool trace_get_wireless_noise_strength_db(void *link,
1471        libtrace_linktype_t linktype, uint8_t *strength);
1472
1473/** Get the wireless Barker code lock quality
1474 * @param link the wireless header
1475 * @param linktype the linktype of the wireless header passed in
1476 * @param[out] quality the quality of the Barker Code lock.
1477 * @return true if the field was available, false if not.
1478 */
1479DLLEXPORT bool trace_get_wireless_lock_quality(void *link,
1480        libtrace_linktype_t linktype, uint16_t *quality);
1481
1482/** Get the wireless transmit attenuation
1483 * @param link the wireless header
1484 * @param linktype the linktype of the wireless header passed in
1485 * @param[out] attenuation the transmit power as a unitless distance from maximum
1486 * power set at factory calibration. 0 indicates maximum transmission power.
1487 * @return true if the field was available, false if not.
1488 */
1489DLLEXPORT bool trace_get_wireless_tx_attenuation(void *link,
1490        libtrace_linktype_t linktype, uint16_t *attenuation);
1491
1492/** Get the wireless transmit attenuation in dB
1493 * @param link the wireless header
1494 * @param linktype the linktype of the wireless header passed in
1495 * @param[out] attenuation the transmit power as dB difference from maximum power
1496 * set at factory calibration. 0 indicates maximum power.
1497 * @return true if the field was available, false if not.
1498 */
1499DLLEXPORT bool trace_get_wireless_tx_attenuation_db(void *link,
1500        libtrace_linktype_t linktype, uint16_t *attenuation);
1501
1502/** Get the wireless transmit power in dBm
1503 * @param link the wireless header
1504 * @param linktype the linktype of the wireless header passed in
1505 * @param[out] txpower the transmit power as dB from a 1mW reference. This is the
1506 * absolute power level measured at the antenna port.
1507 * @return true if the field was available, false if not.
1508 */
1509DLLEXPORT bool trace_get_wireless_tx_power_dbm(void *link,
1510        libtrace_linktype_t linktype, int8_t *txpower);
1511
1512/** Get the wireless antenna
1513 * @param link the wireless header
1514 * @param linktype the linktype of the wireless header passed in
1515 * @param[out] antenna which antenna was used to transmit or receive the frame.
1516 * @return true if the field was available, false if not.
1517 */
1518DLLEXPORT bool trace_get_wireless_antenna(void *link,
1519        libtrace_linktype_t linktype, uint8_t *antenna);
1520
1521/** Get the wireless Frame Check Sequence field
1522 * @param link the wireless header
1523 * @param linktype the linktype of the wireless header passed in
1524 * @param[out] fcs the Frame Check Sequence of the frame.
1525 * @return true if the field was available, false if not.
1526 */
1527DLLEXPORT bool trace_get_wireless_fcs(void *link,
1528        libtrace_linktype_t linktype, uint32_t *fcs);
1529
1530/*@}*/
1531
1532#ifdef __cplusplus
1533} /* extern "C" */
1534#endif /* #ifdef __cplusplus */
1535#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.