source: lib/libtrace.h.in @ 5696294

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

Make some previously exported trace_get_wireless functions static, as they don't either aren't useful yet or aren't necessarily portable across drivers.

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