source: lib/libtrace.h.in @ 362c5f2

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

Added some better error checking to the connection stage of format_rt
Fixed outdated documentation in libtrace.h
Removed warning about demote_packet in format_erf

  • Property mode set to 100644
File size: 44.7 KB
RevLine 
[a79ddbe]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 *
[e5f1431]27 * $Id: libtrace.h 773 2006-05-01 12:58:09Z perry $
[a79ddbe]28 *
29 */
30
[2137b49]31#ifndef LIBTRACE_H
32#define LIBTRACE_H
[27615e9]33
[2c060e3]34/** @file
35 *
36 * @brief Trace file processing library header
37 *
38 * @author Daniel Lawson
39 * @author Perry Lorier
40 *
[e5f1431]41 * @version $Id: libtrace.h 773 2006-05-01 12:58:09Z perry $
[2c060e3]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
[fba4ca0]48 * See the example/ directory in the source distribution for some simple examples
[2c060e3]49 * @par Linking
[90e4aa4]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
[d95447b]54 * Woody, at least, does not support large file offsets.
[2c060e3]55 *
56 */
57
[803ea87]58#include <sys/types.h>
[77285d9]59#include <sys/time.h>
60
[ce0d2cc]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;
[e2aebe7]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_BITFIELD32       uint64_t
[39e141f]85#else
[e2aebe7]86    #ifdef HAVE_GCCVISIBILITYPATCH
87        #define DLLEXPORT __attribute__ (visibility("default"))
88        #define DLLLOCAL __attribute__ (visibility("hidden"))
89    #else
90        #define DLLEXPORT
91        #define DLLLOCAL
92    #endif
93    /* GCC warns if the bitfield type is not "unsigned int", however windows
94     * generates incorrect code for this (see above), so we define these
95     * macros.  How Hidious.  So much for C's portability.
96     */
97    #define LT_BITFIELD8        unsigned int
98    #define LT_BITFIELD16       unsigned int
99    #define LT_BITFIELD32       unsigned int
100    #define LT_BITFIELD32       unsigned int
[ce0d2cc]101#endif
102
103#ifdef WIN32
104#   include <winsock2.h>
105#   include <ws2tcpip.h>
106    typedef short sa_family_t;
107    /* Make up for a lack of stdbool.h */
108#    define bool signed char
109#    define false 0
110#    define true 1
[39e141f]111#    if !defined(ssize_t)
112     /* XXX: Not 64-bit safe! */
113#    define ssize_t int
114#    endif   
[ce0d2cc]115#else
116#    include <netinet/in.h>
[3fcb8b4]117
118#ifndef __cplusplus
[ce0d2cc]119#    include <stdbool.h>
[3fcb8b4]120#endif
121
[77285d9]122#    include <sys/types.h>
123#    include <sys/socket.h>
[ce0d2cc]124#endif
125
[803ea87]126/** API version as 2 byte hex digits, eg 0xXXYYZZ */
[e5f1431]127#define LIBTRACE_API_VERSION \
128            ((@LIBTRACE_MAJOR@<<16)|(@LIBTRACE_MID@<<8)|(@LIBTRACE_MINOR@))
[803ea87]129
130#ifdef __cplusplus
131extern "C" {
132#endif
133
134/* Function does not depend on anything but its
135 * parameters, used to hint gcc's optimisations
136 */
137#if __GNUC__ >= 3
138#  define SIMPLE_FUNCTION __attribute__((pure))
[3b8a5ef]139#  define UNUSED __attribute__((unused))
[7068467]140#  define PACKED __attribute__((packed))
[23971d0]141#  define CONSTRUCTOR __attribute__((constructor))
[803ea87]142#else
143#  define SIMPLE_FUNCTION
[3b8a5ef]144#  define UNUSED
[77285d9]145#  define PACKED
[23971d0]146#  define CONSTRUCTOR
[803ea87]147#endif
148       
[5eae97a]149/** Opaque structure holding information about an output trace */
[5a67294]150typedef struct libtrace_out_t libtrace_out_t;
[5eae97a]151       
[2c060e3]152/** Opaque structure holding information about a trace */
[5a67294]153typedef struct libtrace_t libtrace_t;
[2c060e3]154       
[c5a22ec]155/** Opaque structure holding information about a bpf filter */
[5a67294]156typedef struct libtrace_filter_t libtrace_filter_t;
[2137b49]157
[83445f0]158/** If a packet has memory allocated
[2e223eca]159 * If the packet has allocated it's own memory it's buffer_control should
160 * be TRACE_CTRL_PACKET, when the packet is destroyed it's memory will be
161 * free()'d.  If it's doing zerocopy out of memory owned by something else
162 * it should be TRACE_CTRL_EXTERNAL.
163 * @note the letters p and e are magic numbers used to detect if the packet
[0a6638f]164 * wasn't created properly
165 */
[2e223eca]166typedef enum {
167        TRACE_CTRL_PACKET='p',
168        TRACE_CTRL_EXTERNAL='e'
169} buf_control_t;
[83445f0]170/** The size of a packet's buffer when managed by libtrace */
[8283f05]171#define LIBTRACE_PACKET_BUFSIZE 65536
[d3b2234]172
[83445f0]173/** The libtrace packet structure, applications shouldn't be
174 * meddling around in here
[d3b2234]175 */
[5a67294]176typedef struct libtrace_packet_t {
[0d768c8]177        struct libtrace_t *trace; /**< pointer to the trace */
178        void *header;           /**< pointer to the framing header */
179        void *payload;          /**< pointer to the link layer */
180        buf_control_t buf_control; /**< who owns the memory */
181        void *buffer;           /**< allocated buffer */
[d3b2234]182        size_t size;            /**< trace_get_framing_length()
183                                 * +trace_get_capture_length() */
[7ac9705]184        uint32_t type;          /**< rt protocol type for the packet */
[7068467]185} libtrace_packet_t;
[0ea3526]186
187/** libtrace error information */
188typedef struct trace_err_t{
189        int err_num;            /**< error code */
190        char problem[255];      /**< the format, uri etc that caused the error for reporting purposes */
191} libtrace_err_t;
[2137b49]192
[ffc8c8d]193/** Enumeration of error codes */
[880aa58]194enum {
[91db67b]195        /** No Error has occured.... yet. */
[880aa58]196        TRACE_ERR_NOERROR       = 0,
[91db67b]197        /** The URI passed to trace_create() is unsupported, or badly formed */
[880aa58]198        TRACE_ERR_BAD_FORMAT    = -1,
[91db67b]199        /** The trace failed to initialise */
200        TRACE_ERR_INIT_FAILED   = -2,
201        /** Unknown config option */
202        TRACE_ERR_UNKNOWN_OPTION= -3,
203        /** This output uri cannot write packets of this type */
204        TRACE_ERR_NO_CONVERSION = -4,
205        /** This packet is corrupt, or unusable for the action required */
206        TRACE_ERR_BAD_PACKET    = -5,
[83445f0]207        /** Option known, but unsupported by this format */
208        TRACE_ERR_OPTION_UNAVAIL= -6,
[91db67b]209        /** This feature is unsupported */
210        TRACE_ERR_UNSUPPORTED   = -7
[880aa58]211};
[ffc8c8d]212
[83445f0]213/** Enumeration of DLT types supported by libtrace */
[114b8d6]214typedef enum {
215        TRACE_DLT_NULL = 0,
216        TRACE_DLT_EN10MB = 1,
217        TRACE_DLT_ATM_RFC1483 = 11,
218        TRACE_DLT_IEEE802_11 = 105,
219        TRACE_DLT_LINUX_SLL = 113,
220        TRACE_DLT_PFLOG = 117
221} libtrace_dlt_t ;
222
[83445f0]223/** Enumeration of link layer types supported by libtrace */
[c37ab9f]224typedef enum {
[c5ebbaa]225    /* TRACE_TYPE_LEGACY = 0            Obsolete */
[c37ab9f]226       TRACE_TYPE_HDLC_POS = 1,
[c5ebbaa]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 */
[02b7297]237       TRACE_TYPE_80211_PRISM = 12,
[c5ebbaa]238       TRACE_TYPE_AAL5 = 13,
239       TRACE_TYPE_DUCK = 14             /**< Pseudo link layer for DUCK packets */
[c37ab9f]240     } libtrace_linktype_t;
241
242/** Trace directions
243 * Note that these are the directions used by convention, more directions
244 * are possible, not just these 3, and that they may not conform to this
245 * convention.
246 */
247typedef enum {
248        TRACE_DIR_OUTGOING = 0,         /**< Packets originating "outside" */
249        TRACE_DIR_INCOMING = 1,         /**< Packets originating "inside" */
250        TRACE_DIR_OTHER    = 2          /**< Packets with an unknown direction, or one that's unknown */
251} libtrace_direction_t;
[114b8d6]252
[91c9552]253/** @name Protocol structures
[7068467]254 * These convenience structures are here as they are portable ways of dealing
255 * with various protocols.
256 * @{
257 */
258
[ce0d2cc]259#ifdef WIN32
260#pragma pack(push)
261#pragma pack(1)
262#endif
263
[83445f0]264/** Generic IP header structure */
[ce0d2cc]265typedef struct libtrace_ip
[fba4ca0]266{
[568dc0e]267#if BYTE_ORDER == LITTLE_ENDIAN
[e2aebe7]268    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
269    LT_BITFIELD8 ip_v:4;                /**< Version */
[568dc0e]270#elif BYTE_ORDER == BIG_ENDIAN
[e2aebe7]271    LT_BITFIELD8 ip_v:4;                /**< Version */
272    LT_BITFIELD8 ip_hl:4;               /**< Header Length */
[568dc0e]273#else
274#   error "Adjust your <bits/endian.h> defines"
[2c060e3]275#endif
[83445f0]276    uint8_t  ip_tos;                    /**< Type of Service */
277    uint16_t ip_len;                    /**< Total Length */
278    int16_t  ip_id;                     /**< Identification */
[7068467]279#if BYTE_ORDER == LITTLE_ENDIAN
[e2aebe7]280    LT_BITFIELD16 ip_off:12;            /**< Fragment Offset */
281    LT_BITFIELD16 ip_mf:1;              /**< More Fragments Flag */
282    LT_BITFIELD16 ip_df:1;              /**< Dont Fragment Flag */
283    LT_BITFIELD16 ip_rf:1;              /**< Reserved Fragment Flag */
[7068467]284#elif BYTE_ORDER == BIG_ENDIAN
[e2aebe7]285    LT_BITFIELD16 ip_rf:1;              /**< Fragment Offset */
286    LT_BITFIELD16 ip_df:1;              /**< More Fragments Flag */
287    LT_BITFIELD16 ip_mf:1;              /**< Dont Fragment Flag */
288    LT_BITFIELD16 ip_off:12;            /**< Reserved Fragment Flag */
[7068467]289#else
290#   error "Adjust your <bits/endian.h> defines"
291#endif
[83445f0]292    uint8_t  ip_ttl;                    /**< Time to Live */
293    uint8_t  ip_p;                      /**< Protocol */
294    uint16_t ip_sum;                    /**< Checksum */
295    struct in_addr ip_src;              /**< Source Address */
296    struct in_addr ip_dst;              /**< Destination Address */
[ce0d2cc]297} PACKED libtrace_ip_t;
[4af54d1]298
[83445f0]299/** IPv6 header extension structure */
[ce0d2cc]300typedef struct libtrace_ip6_ext
[a842286]301{
302        uint8_t nxt;
303        uint8_t len;
[ce0d2cc]304} PACKED libtrace_ip6_ext_t;
[a842286]305
[83445f0]306/** Generic IPv6 header structure */
[e7bc155]307typedef struct libtrace_ip6
[a842286]308{
309    uint32_t flow;
310    uint16_t plen;                      /**< Payload length */
311    uint8_t nxt;                        /**< Next header */
[4af54d1]312    uint8_t hlim;                       /**< Hop limit */
313    struct in6_addr ip_src;             /**< source address */
314    struct in6_addr ip_dst;             /**< dest address */
[e7bc155]315} PACKED libtrace_ip6_t;
[2c060e3]316
[83445f0]317/** Generic TCP header structure */
[5a67294]318typedef struct libtrace_tcp
[2c060e3]319  {
[e7bc155]320    uint16_t source;            /**< Source Port */
321    uint16_t dest;              /**< Destination port */
322    uint32_t seq;               /**< Sequence number */
323    uint32_t ack_seq;           /**< Acknowledgement Number */
[568dc0e]324#  if BYTE_ORDER == LITTLE_ENDIAN
[e2aebe7]325    LT_BITFIELD8 res1:4;        /**< Reserved bits */
326    LT_BITFIELD8 doff:4;        /**< Data Offset */     
327    LT_BITFIELD8 fin:1;         /**< FIN */
328    LT_BITFIELD8 syn:1;         /**< SYN flag */
329    LT_BITFIELD8 rst:1;         /**< RST flag */
330    LT_BITFIELD8 psh:1;         /**< PuSH flag */
331    LT_BITFIELD8 ack:1;         /**< ACK flag */
332    LT_BITFIELD8 urg:1;         /**< URG flag */
333    LT_BITFIELD8 res2:2;        /**< Reserved */
[568dc0e]334#  elif BYTE_ORDER == BIG_ENDIAN
[e2aebe7]335    LT_BITFIELD8 doff:4;        /**< Data offset */
336    LT_BITFIELD8 res1:4;        /**< Reserved bits */
337    LT_BITFIELD8 res2:2;        /**< Reserved */
338    LT_BITFIELD8 urg:1;         /**< URG flag */
339    LT_BITFIELD8 ack:1;         /**< ACK flag */
340    LT_BITFIELD8 psh:1;         /**< PuSH flag */
341    LT_BITFIELD8 rst:1;         /**< RST flag */
342    LT_BITFIELD8 syn:1;         /**< SYN flag */
343    LT_BITFIELD8 fin:1;         /**< FIN flag */
[2c060e3]344#  else
345#   error "Adjust your <bits/endian.h> defines"
346#  endif
[e7bc155]347    uint16_t window;            /**< Window Size */
348    uint16_t check;             /**< Checksum */
349    uint16_t urg_ptr;           /**< Urgent Pointer */
350} PACKED libtrace_tcp_t;
[2c060e3]351
[83445f0]352/** Generic UDP header structure */
[5a67294]353typedef struct libtrace_udp {
[e7bc155]354  uint16_t      source;         /**< Source port */
355  uint16_t      dest;           /**< Destination port */
356  uint16_t      len;            /**< Length */
357  uint16_t      check;          /**< Checksum */
358} PACKED libtrace_udp_t;
[2c060e3]359
[83445f0]360/** Generic ICMP header structure */
[5a67294]361typedef struct libtrace_icmp
[2c060e3]362{
[83445f0]363  uint8_t type;         /**< Message Type */
364  uint8_t code;         /**< Type Sub-code */
365  uint16_t checksum;            /**< Checksum */
[2c060e3]366  union
367  {
368    struct
369    {
[e7bc155]370      uint16_t  id;
371      uint16_t  sequence;
[83445f0]372    } echo;                     /**< Echo Datagram */
373    uint32_t    gateway;        /**< Gateway Address */
[2c060e3]374    struct
375    {
[e7bc155]376      uint16_t  unused;
377      uint16_t  mtu;
[83445f0]378    } frag;                     /**< Path MTU Discovery */
379  } un;                         /**< Union for Payloads of Various ICMP Codes */
[e7bc155]380} PACKED libtrace_icmp_t;
[fba4ca0]381
[83445f0]382/** Generic LLC/SNAP header structure */
[3b8a5ef]383typedef struct libtrace_llcsnap
384{
[12778c2]385/* LLC */
386  uint8_t dsap;                 /**< Destination Service Access Point */
387  uint8_t ssap;                 /**< Source Service Access Point */
[e7bc155]388  uint8_t control;
[12778c2]389/* SNAP */
[e2aebe7]390  LT_BITFIELD32 oui:24;         /**< Organisationally Unique Identifier (scope)*/
[12778c2]391  uint16_t type;                /**< Protocol within OUI */
[e7bc155]392} PACKED libtrace_llcsnap_t;
[3b8a5ef]393
[fba4ca0]394/** 802.3 frame */
[3b8338b]395typedef struct libtrace_ether
[fba4ca0]396{
[83445f0]397  uint8_t ether_dhost[6];       /**< Destination Ether Addr */
398  uint8_t ether_shost[6];       /**< Source Ether Addr */
399  uint16_t ether_type;          /**< Packet Type ID Field (next-header) */
[e7bc155]400} PACKED libtrace_ether_t;
[2c060e3]401
[5bbe424]402/** 802.1Q frame */
[3b8338b]403typedef struct libtrace_8021q
[5bbe424]404{
[e2aebe7]405  LT_BITFIELD16 vlan_pri:3;      /**< VLAN User Priority */
406  LT_BITFIELD16 vlan_cfi:1;      /**< VLAN Format Indicator,
[2e223eca]407                                   * 0 for ethernet, 1 for token ring */
[e2aebe7]408  LT_BITFIELD16 vlan_id:12;      /**< VLAN Id */
[83445f0]409  uint16_t vlan_ether_type;      /**< VLAN Sub-packet Type ID Field
[2e223eca]410                                   * (next-header)*/
[e7bc155]411} PACKED libtrace_8021q_t;
[2c060e3]412
[f04e489]413/** ATM cell */
[5a67294]414typedef struct libtrace_atm_cell
[f04e489]415{
[e2aebe7]416  LT_BITFIELD32 gfc:4;          /**< Generic Flow Control */
417  LT_BITFIELD32 vpi:8;          /**< Virtual Path Identifier */
418  LT_BITFIELD32 vci:8;          /**< Virtual Channel Identifier */
419  LT_BITFIELD32 pt:3;           /**< Payload Type */
420  LT_BITFIELD32 clp:1;          /**< Cell Loss Priority */
421  LT_BITFIELD32 hec:8;          /**< Header Error Control */
[e7bc155]422} PACKED libtrace_atm_cell_t;
[f04e489]423
424/** POS header */
[5a67294]425typedef struct libtrace_pos
[f04e489]426{
[e7bc155]427 uint16_t header;
[83445f0]428 uint16_t ether_type;           /**< Ether Type */
[e7bc155]429} PACKED libtrace_pos_t;
[4af54d1]430
431/** 802.11 header */
432typedef struct libtrace_80211_t {
[e2aebe7]433        LT_BITFIELD32      protocol:2;
434        LT_BITFIELD32      type:2;
435        LT_BITFIELD32      subtype:4;
436        LT_BITFIELD32      to_ds:1;             /**< Packet to Distribution Service */
437        LT_BITFIELD32      from_ds:1;   /**< Packet from Distribution Service */
438        LT_BITFIELD32      more_frag:1; /**< Packet has more fragments */
439        LT_BITFIELD32      retry:1;             /**< Packet is a retry */
440        LT_BITFIELD32      power:1;
441        LT_BITFIELD32      more_data:1;
442        LT_BITFIELD32      wep:1;
443        LT_BITFIELD32      order:1;
[7fc01569]444        uint16_t     duration;
[4af54d1]445        uint8_t      mac1[6];
446        uint8_t      mac2[6];
447        uint8_t      mac3[6];
448        uint16_t     SeqCtl;
449        uint8_t      mac4[6];
[e7bc155]450} PACKED libtrace_80211_t;
451
452#ifdef WIN32
453#pragma pack(pop)
454#endif
[4af54d1]455
456
[7068467]457/*@}*/
[f04e489]458
[40ba6ad]459/** Prints help information for libtrace
460 *
461 * Function prints out some basic help information regarding libtrace,
462 * and then prints out the help() function registered with each input module
463 */
[39e141f]464DLLEXPORT void trace_help();
[2c060e3]465
[91c9552]466/** @name Trace management
467 * These members deal with creating, configuring, starting, pausing and
468 * cleaning up a trace object
[7068467]469 *@{
[7f32bb5]470 */
471
[2c060e3]472/** Create a trace file from a URI
473 *
[31709e8d]474 * @param uri containing a valid libtrace URI
[83445f0]475 * @return an opaque pointer to a libtrace_t
[2c060e3]476 *
477 * Valid URI's are:
478 *  - erf:/path/to/erf/file
479 *  - erf:-  (stdin)
[fba4ca0]480 *  - dag:/dev/dagcard                 
[fe43699]481 *  - pcapint:pcapinterface                (eg: pcap:eth0)
[2c060e3]482 *  - pcap:/path/to/pcap/file
483 *  - pcap:-
[83445f0]484 *  - rt:hostname
485 *  - rt:hostname:port
486 *  - rtclient:hostname (deprecated)
487 *  - rtclient:hostname:port (deprecated)
488 *  - wag:/dev/wagcard
489 *  - wtf:-
490 *  - wtf:/path/to/wtf/file
[2c060e3]491 *
[2e223eca]492 *  If an error occured when attempting to open the trace file, an error
493 *  trace is returned and trace_get_error should be called to find out
494 *  if an error occured, and what that error was.  The trace is created in the
495 *  configuration state, you must call trace_start to start the capture.
[2c060e3]496 */
[39e141f]497DLLEXPORT libtrace_t *trace_create(const char *uri);
[2c060e3]498
[084d95a]499/** Creates a "dummy" trace file that has only the format type set.
500 *
[83445f0]501 * @return an opaque pointer to a (sparsely initialised) libtrace_t
[084d95a]502 *
[2e223eca]503 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions
504 * with the dummy trace. Its intended purpose is to act as a packet->trace for
505 * libtrace_packet_t's that are not associated with a libtrace_t structure.
[084d95a]506 */
[39e141f]507DLLEXPORT libtrace_t *trace_create_dead(const char *uri);
[084d95a]508
[5eae97a]509/** Creates a trace output file from a URI.
510 *
[fba4ca0]511 * @param uri   the uri string describing the output format and destination
[83445f0]512 * @return an opaque pointer to a libtrace_output_t
[5eae97a]513 *
514 * Valid URI's are:
[83445f0]515 *  - erf:/path/to/erf/file
516 *  - pcap:/path/to/pcap/file
517 *  - wtf:/path/to/wtf/file
[5eae97a]518 *
[fba4ca0]519 *  If an error occured when attempting to open the output trace, NULL is returned
520 *  and trace_errno is set. Use trace_perror() to get more information
[5eae97a]521 */
[39e141f]522DLLEXPORT libtrace_out_t *trace_create_output(const char *uri);
[5eae97a]523
[7068467]524/** Start the capture
525 * @param libtrace      The trace to start
[83445f0]526 * @return 0 on success, -1 on failure
[f99a183]527 *
[7068467]528 * This does the actual work with starting the trace capture, and applying
529 * all the config options.  This may fail.
530 */
[39e141f]531DLLEXPORT int trace_start(libtrace_t *libtrace);
[7068467]532
[1fbd938]533/** Pause the capture
534 * @param libtrace      The trace to pause
[83445f0]535 * @return 0 on success, -1 on failure
[1fbd938]536 *
537 * This stops a capture in progress and returns you to the configuration
538 * state.  Any packets that arrive after trace_pause() has been called
539 * will be discarded.  To resume capture, call trace_start().
540 */
[39e141f]541DLLEXPORT int trace_pause(libtrace_t *libtrace);
[1fbd938]542
[7068467]543/** Start an output trace
544 * @param libtrace      The trace to start
[83445f0]545 * @return 0 on success, -1 on failure
[fba4ca0]546 *
[7068467]547 * This does the actual work with starting a trace for write.  This generally
548 * creates the file.
[f99a183]549 */
[39e141f]550DLLEXPORT int trace_start_output(libtrace_out_t *libtrace);
[7068467]551
552/** Valid trace capture options */
553typedef enum {
554        TRACE_OPTION_SNAPLEN, /**< Number of bytes captured */
555        TRACE_OPTION_PROMISC, /**< Capture packets to other hosts */
[cd7eec7]556        TRACE_OPTION_FILTER,  /**< Apply this filter to all packets recieved */
557        TRACE_META_FREQ       /**< Frequency of meta-data information, e.g. DUCK packets */
[7068467]558} trace_option_t;
559
560/** Sets an input config option
561 * @param libtrace      the trace object to apply the option to
562 * @param option        the option to set
563 * @param value         the value to set the option to
564 * @return -1 if option configuration failed, 0 otherwise
565 * This should be called after trace_create, and before trace_start
566 */
[39e141f]567DLLEXPORT int trace_config(libtrace_t *libtrace,
[7068467]568                trace_option_t option,
569                void *value);
570
571typedef enum {
572        TRACE_OPTION_OUTPUT_FILEFLAGS, /**< File flags to open the trace file
573                                        * with.  eg O_APPEND
574                                        */
575        TRACE_OPTION_OUTPUT_COMPRESS   /**< Compression level, eg 6. */
576} trace_option_output_t;
577
578/** Sets an output config option
579 *
580 * @param libtrace      the output trace object to apply the option to
581 * @param option        the option to set
582 * @param value         the value to set the option to
583 * @return -1 if option configuration failed, 0 otherwise
584 * This should be called after trace_create_output, and before
585 * trace_start_output
586 */
[39e141f]587DLLEXPORT int trace_config_output(libtrace_out_t *libtrace,
[7068467]588                trace_option_output_t option,
589                void *value
590                );
[91ebc50]591
[2c060e3]592/** Close a trace file, freeing up any resources it may have been using
593 *
594 */
[39e141f]595DLLEXPORT void trace_destroy(libtrace_t *trace);
[2c060e3]596
[31709e8d]597/** Close a trace file, freeing up any resources it may have been using
598 * @param trace         trace file to be destroyed
599 */
[39e141f]600DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
[084d95a]601
[5eae97a]602/** Close a trace output file, freeing up any resources it may have been using
[31709e8d]603 * @param trace         the output trace file to be destroyed
[5eae97a]604 */
[39e141f]605DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
[1fbd938]606
[0ea3526]607/** Check (and clear) the current error state of an input trace
608 * @param trace         the trace file to check the error state on
609 * @return Error report
610 * This reads and returns the current error state and sets the current error
611 * to "no error".
612 */
[39e141f]613DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
[0ea3526]614
[eeab9832]615/** Return if there is an error
616 * @param trace         the trace file to check the error state on
617 * This does not clear the error status, and only returns true or false.
618 */
[39e141f]619DLLEXPORT bool trace_is_err(libtrace_t *trace);
[eeab9832]620
621/** Output an error message to stderr and clear the error status.
622 * @param trace         the trace with the error to output
623 * @param msg           the message to prefix to the error
624 * This function does clear the error status.
625 */
[39e141f]626DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...);
[eeab9832]627
[0ea3526]628/** Check (and clear) the current error state of an output trace
629 * @param trace         the output trace file to check the error state on
630 * @return Error report
631 * This reads and returns the current error state and sets the current error
632 * to "no error".
633 */
[39e141f]634DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
[0ea3526]635
[eeab9832]636/** Return if there is an error
637 * @param trace         the trace file to check the error state on
638 * This does not clear the error status, and only returns true or false.
639 */
[39e141f]640DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
[eeab9832]641
642/** Output an error message to stderr and clear the error status.
643 * @param trace         the trace with the error to output
644 * @param msg           the message to prefix to the error
645 * This function does clear the error status.
646 */
[39e141f]647DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...);
[eeab9832]648
649
[7068467]650/*@}*/
[7f32bb5]651
652/** @name Reading/Writing packets
653 * These members deal with creating, reading and writing packets
[7068467]654 *
655 * @{
[7f32bb5]656 */
[5eae97a]657
[14d8a63]658/** Create a new packet object
659 *
660 * @return a pointer to an initialised libtrace_packet_t object
661 */
[39e141f]662DLLEXPORT libtrace_packet_t *trace_create_packet();
[14d8a63]663
[91c9552]664/** Copy a packet
665 * @param packet        the source packet to copy
666 * @return a new packet which has the same content as the source packet
667 * @note This always involves a copy, which can be slow.  Use of this
668 * function should be avoided where possible.
669 * @par The reason you would want to use this function is that a zerocopied
670 * packet from a device is using the devices memory which may be a limited
671 * resource.  Copying the packet will cause it to be copied into the systems
672 * memory.
673 */
[39e141f]674DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
[91c9552]675
[14d8a63]676/** Destroy a packet object
677 *
678 * sideeffect: sets packet to NULL
679 */
[e2aebe7]680DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet);
[14d8a63]681
682
[83445f0]683/** Read one packet from the trace
[2c060e3]684 *
[5eae97a]685 * @param trace         the libtrace opaque pointer
[2137b49]686 * @param packet        the packet opaque pointer
[9d7d9e9]687 * @return 0 on EOF, negative value on error, number of bytes read when
688 * successful.
[7068467]689 *
[9d7d9e9]690 * @note the number of bytes read is usually (but not always) the same as
691 * trace_get_framing_length()+trace_get_capture_length() depending on the
692 * trace format.
[7068467]693 * @note the trace must have been started with trace_start before calling
694 * this function
695 */
[39e141f]696DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
[7068467]697
698/** Event types
699 * see \ref libtrace_eventobj_t and \ref trace_event
700 */
701typedef enum {
702        TRACE_EVENT_IOWAIT,     /**< Need to block on fd */
703        TRACE_EVENT_SLEEP,      /**< Sleep for some time */
704        TRACE_EVENT_PACKET,     /**< packet has arrived */
705        TRACE_EVENT_TERMINATE   /**< End of trace */
706} libtrace_event_t;
707
[83445f0]708/** Structure returned by libtrace_event explaining what the current event is */
[7068467]709typedef struct libtrace_eventobj_t {
710        libtrace_event_t type; /**< event type (iowait,sleep,packet) */
711        int fd;                /**< if IOWAIT, the fd to sleep on */
[ed7c716]712        double seconds;        /**< if SLEEP, the amount of time to sleep for
713                                */
714        int size;              /**< if PACKET, the value returned from
715                                *  trace_read_packet
716                                */
[7068467]717} libtrace_eventobj_t;
718
[83445f0]719/** Processes the next libtrace event
[7068467]720 * @param trace the libtrace opaque pointer
721 * @param packet the libtrace_packet opaque pointer
722 * @return libtrace_event struct containing the type, and potential
723 *      fd or seconds to sleep on
[2c060e3]724 *
[7068467]725 * Type can be:
726 *  TRACE_EVENT_IOWAIT  Waiting on I/O on fd
727 *  TRACE_EVENT_SLEEP   Next event in seconds
728 *  TRACE_EVENT_PACKET  Packet arrived in buffer with size size
729 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
[2c060e3]730 */
[39e141f]731DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
[7068467]732                libtrace_packet_t *packet);
733
[2c060e3]734
[5eae97a]735/** Write one packet out to the output trace
736 *
737 * @param trace         the libtrace_out opaque pointer
738 * @param packet        the packet opaque pointer
[7068467]739 * @return the number of bytes written out, if zero or negative then an error has occured.
[5eae97a]740 */
[39e141f]741DLLEXPORT int trace_write_packet(libtrace_out_t *trace, const libtrace_packet_t *packet);
[7068467]742/*@}*/
[7f32bb5]743
[91c9552]744/** @name Protocol decodes
745 * These functions locate and return a pointer to various headers inside a
746 * packet
[7068467]747 * @{
[7f32bb5]748 */
[5eae97a]749
[2c060e3]750/** get a pointer to the link layer
[2137b49]751 * @param packet        the packet opaque pointer
[2c060e3]752 *
[7068467]753 * @return a pointer to the link layer, or NULL if there is no link layer
[2c060e3]754 *
[83445f0]755 * @note you should call trace_get_link_type to find out what type of link
756 * layer this is
[2c060e3]757 */
[39e141f]758DLLEXPORT SIMPLE_FUNCTION
[7068467]759void *trace_get_link(const libtrace_packet_t *packet);
[2c060e3]760
761/** get a pointer to the IP header (if any)
[2137b49]762 * @param packet        the packet opaque pointer
[2c060e3]763 *
[83445f0]764 * @return a pointer to the IP header, or NULL if there is no IP header
[2c060e3]765 */
[2ba89b7]766DLLEXPORT SIMPLE_FUNCTION
[9231fe5]767libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
[2c060e3]768
[9c87732]769/** Gets a pointer to the transport layer header (if any)
770 * @param packet        a pointer to a libtrace_packet structure
[a842286]771 * @param[out] proto    transport layer protocol
[9c87732]772 *
[7068467]773 * @return a pointer to the transport layer header, or NULL if there is no header
[a842286]774 *
775 * @note proto may be NULL if proto is unneeded.
[9c87732]776 */
[39e141f]777DLLEXPORT void *trace_get_transport(libtrace_packet_t *packet, uint8_t *proto,
[9231fe5]778                uint32_t *remaining);
[9c87732]779
[4af54d1]780/** Gets a pointer to the payload given a pointer to the IP header
[9c87732]781 * @param ip            The IP Header
[a842286]782 * @param[out] proto    An output variable of the IP protocol
[9231fe5]783 * @param[in,out] remaining Updated with the number of bytes remaining
[9c87732]784 *
[9231fe5]785 * @return a pointer to the transport layer header, or NULL if header isn't present.
786 *
787 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
788 * of bytes captured of the IP header and beyond.  It will be updated after this
789 * function to the number of bytes remaining after the IP header (and any IP options)
790 * have been removed.
[9c87732]791 *
[9231fe5]792 * proto may be NULL if not needed.
793 *
794 * @note This is similar to trace_get_transport_from_ip in libtrace2
[4af54d1]795 */
[39e141f]796DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
[9231fe5]797                uint32_t *remaining);
[4af54d1]798
[a700713]799
800/** Gets a pointer to the payload given a pointer to the IP header
801 * @param ip            The link pointer
802 * @param[out] type     An output variable of the ethernet type
803 * @param[in,out] remaining Updated with the number of bytes remaining
804 *
805 * @return a pointer to the transport layer header, or NULL if header isn't
806 * present.
807 *
808 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
809 * of bytes captured of the linklayer and beyond.  It will be updated after
810 * this function to the number of bytes remaining after the IP header (and any
811 * IP options) have been removed.
812 *
813 * type may be NULL if not needed.
814 */
[e2aebe7]815DLLEXPORT void *trace_get_payload_from_link(void *link,
816                libtrace_linktype_t linktype,
[c2cb712]817                uint16_t *type, uint32_t *remaining);
[e2aebe7]818
[4af54d1]819/** Gets a pointer to the payload given a pointer to a tcp header
820 * @param tcp           The tcp Header
[9231fe5]821 * @param[in,out] remaining Updated with the number of bytes remaining
[4af54d1]822 *
[9231fe5]823 * @return a pointer to the tcp payload, or NULL if the payload isn't present.
[4af54d1]824 *
[9231fe5]825 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
826 * of bytes captured of the TCP header and beyond.  It will be updated after this
827 * function to the number of bytes remaining after the TCP header (and any TCP options)
828 * have been removed.
829 *
830 * @note This is similar to trace_get_transport_from_ip in libtrace2
[4af54d1]831 */
[39e141f]832DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining);
[4af54d1]833
834/** Gets a pointer to the payload given a pointer to a udp header
835 * @param udp           The udp Header
[9231fe5]836 * @param[in,out] remaining Updated with the number of bytes remaining
[4af54d1]837 *
[9231fe5]838 * @return a pointer to the udp payload, or NULL if the payload 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 TCP header and beyond.  It will be updated after this
842 * function to the number of bytes remaining after the TCP header (and any TCP options)
843 * have been removed.
[4af54d1]844 *
[9231fe5]845 * @note This is similar trace_get_transport_from_ip in libtrace2
[4af54d1]846 */
[39e141f]847DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
[4af54d1]848
849/** Gets a pointer to the payload given a pointer to a icmp header
[9231fe5]850 * @param icmp          The icmp Header
851 * @param[in,out] remaining Updated with the number of bytes remaining
[4af54d1]852 *
[9231fe5]853 * @return a pointer to the icmp payload, or NULL if the payload isn't present.
854 *
855 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
856 * of bytes captured of the TCP header and beyond.  It will be updated after this
857 * function to the number of bytes remaining after the TCP header (and any TCP options)
858 * have been removed.
[4af54d1]859 *
[9231fe5]860 * @note This is similar to trace_get_payload_from_icmp in libtrace2
[9c87732]861 */
[83445f0]862DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *remaining);
[9c87732]863
[2c060e3]864/** get a pointer to the TCP header (if any)
[2137b49]865 * @param packet        the packet opaque pointer
[2c060e3]866 *
[7068467]867 * @return a pointer to the TCP header, or NULL if there is not a TCP packet
[2c060e3]868 */
[39e141f]869DLLEXPORT SIMPLE_FUNCTION
[9231fe5]870libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
[2c060e3]871
[68667ee]872/** get a pointer to the TCP header (if any) given a pointer to the IP header
873 * @param ip            The IP header
[9231fe5]874 * @param[in,out] remaining Updated with the number of bytes remaining
[68667ee]875 *
[7068467]876 * @return a pointer to the TCP header, or NULL if this is not a TCP packet
[68667ee]877 *
[9231fe5]878 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
879 * of bytes captured of the TCP header and beyond.  It will be updated after this
880 * function to the number of bytes remaining after the TCP header (and any TCP options)
881 * have been removed.
[68667ee]882 *
[9231fe5]883 * @note The last parameter has changed from libtrace2
[68667ee]884 */
[39e141f]885DLLEXPORT SIMPLE_FUNCTION
[9231fe5]886libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
[68667ee]887
[2c060e3]888/** get a pointer to the UDP header (if any)
[2137b49]889 * @param packet        the packet opaque pointer
[2c060e3]890 *
[7068467]891 * @return a pointer to the UDP header, or NULL if this is not a UDP packet
[2c060e3]892 */
[39e141f]893DLLEXPORT SIMPLE_FUNCTION
[4af54d1]894libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
[2c060e3]895
[68667ee]896/** get a pointer to the UDP header (if any) given a pointer to the IP header
897 * @param       ip      The IP header
[9231fe5]898 * @param[in,out] remaining Updated with the number of bytes remaining
[68667ee]899 *
[7068467]900 * @return a pointer to the UDP header, or NULL if this is not an UDP packet
[68667ee]901 *
[9231fe5]902 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
903 * of bytes captured of the TCP header and beyond.  It will be updated after this
904 * function to the number of bytes remaining after the TCP header (and any TCP options)
905 * have been removed.
906 *
907 * @note Beware the change from libtrace2 from skipped to remaining
[68667ee]908 */
[39e141f]909DLLEXPORT SIMPLE_FUNCTION
[9231fe5]910libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
[68667ee]911
[2c060e3]912/** get a pointer to the ICMP header (if any)
[2137b49]913 * @param packet        the packet opaque pointer
[2c060e3]914 *
[7068467]915 * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet
[2c060e3]916 */
[39e141f]917DLLEXPORT SIMPLE_FUNCTION
[9231fe5]918libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
[2c060e3]919
[68667ee]920/** get a pointer to the ICMP header (if any) given a pointer to the IP header
921 * @param ip            The IP header
[9231fe5]922 * @param[in,out] remaining Updated with the number of bytes remaining
[68667ee]923 *
[7068467]924 * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet
[68667ee]925 *
[9231fe5]926 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
927 * of bytes captured of the TCP header and beyond.  It will be updated after this
928 * function to the number of bytes remaining after the TCP header (and any TCP options)
929 * have been removed.
930 *
931 * @note Beware the change from libtrace2 from skipped to remaining
932 */
[39e141f]933DLLEXPORT SIMPLE_FUNCTION
[9231fe5]934libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
935
[01459e4]936/** Get the destination MAC address
[9231fe5]937 * @param packet        the packet opaque pointer
938 * @return a pointer to the destination mac, (or NULL if there is no
939 * destination MAC)
940 */
[39e141f]941DLLEXPORT SIMPLE_FUNCTION
[9231fe5]942uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
943
[01459e4]944/** Get the source MAC address
[9231fe5]945 * @param packet        the packet opaque pointer
946 * @return a pointer to the source mac, (or NULL if there is no source MAC)
[68667ee]947 */
[e093e5c]948DLLEXPORT SIMPLE_FUNCTION
[9231fe5]949uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
950
[01459e4]951/** Get the source IP address
[9231fe5]952 * @param packet        the packet opaque pointer
953 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
954 *                      static storage.
955 * @return NULL if there is no source address, or a sockaddr holding a v4 or v6 address
956 */
[39e141f]957DLLEXPORT SIMPLE_FUNCTION
[9231fe5]958struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
959                struct sockaddr *addr);
960
[01459e4]961/** Get the destination IP address
[9231fe5]962 * @param packet        the packet opaque pointer
963 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
964 *                      static storage.
[01459e4]965 * @return NULL if there is no destination address, or a sockaddr holding a v4 or v6 address
[9231fe5]966 */
[39e141f]967DLLEXPORT SIMPLE_FUNCTION
[9231fe5]968struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
969                struct sockaddr *addr);
970
[7068467]971/*@}*/
[68667ee]972
973/** parse an ip or tcp option
974 * @param[in,out] ptr   the pointer to the current option
975 * @param[in,out] len   the length of the remaining buffer
976 * @param[out] type     the type of the option
977 * @param[out] optlen   the length of the option
978 * @param[out] data     the data of the option
979 *
[7068467]980 * @return bool true if there is another option (and the fields are filled in)
[68667ee]981 *               or false if this was the last option.
982 *
983 * This updates ptr to point to the next option after this one, and updates
984 * len to be the number of bytes remaining in the options area.  Type is updated
985 * to be the code of this option, and data points to the data of this option,
986 * with optlen saying how many bytes there are.
987 *
988 * @note Beware of fragmented packets.
989 */
[39e141f]990DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
[68667ee]991                        unsigned char *type,
992                        unsigned char *optlen,
993                        unsigned char **data);
994
995
[7f32bb5]996/** @name Time
997 * These functions deal with time that a packet arrived and return it
998 * in various formats
[7068467]999 * @{
[7f32bb5]1000 */
[2c060e3]1001/** Get the current time in DAG time format
[2137b49]1002 * @param packet        the packet opaque pointer
1003 *
[7068467]1004 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
[2c060e3]1005 * past 1970-01-01, the lower 32bits are partial seconds)
1006 */
[39e141f]1007DLLEXPORT SIMPLE_FUNCTION
[7068467]1008uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
[2c060e3]1009
1010/** Get the current time in struct timeval
[2137b49]1011 * @param packet        the packet opaque pointer
1012 *
[7068467]1013 * @return time that this packet was seen in a struct timeval
[2c060e3]1014 */
[39e141f]1015DLLEXPORT SIMPLE_FUNCTION
[7068467]1016struct timeval trace_get_timeval(const libtrace_packet_t *packet);
[2c060e3]1017
1018/** Get the current time in floating point seconds
[2137b49]1019 * @param packet        the packet opaque pointer
1020 *
[7068467]1021 * @return time that this packet was seen in 64bit floating point seconds
[2c060e3]1022 */
[39e141f]1023DLLEXPORT SIMPLE_FUNCTION
[7068467]1024double trace_get_seconds(const libtrace_packet_t *packet);
[91c9552]1025
1026/** Seek within a trace
1027 * @param trace         trace to seek
1028 * @param seconds       time to seek to
1029 * @return 0 on success.
1030 * Make the next packet read to be the first packet to occur at or after the
1031 * time searched for.  This must be called in the configuration state (ie,
1032 * before trace_start() or after trace_pause().
1033 * @note This function may be extremely slow.
1034 */
[39e141f]1035DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
[91c9552]1036
1037/** Seek within a trace
1038 * @param trace         trace to seek
1039 * @param tv            time to seek to
1040 * @return 0 on success.
1041 * Make the next packet read to be the first packet to occur at or after the
1042 * time searched for.  This must be called in the configuration state (ie,
1043 * before trace_start() or after trace_pause().
1044 * @note This function may be extremely slow.
1045 */
[39e141f]1046DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
[91c9552]1047
[0d57541]1048/** Seek within a trace
1049 * @param trace         trace to seek
1050 * @param ts            erf timestamp
1051 * @return 0 on success.
1052 * Make the next packet read to be the first packet to occur at or after the
1053 * time searched for.  This must be called in the configuration state (ie,
1054 * before trace_start() or after trace_pause().
1055 * @note This function may be extremely slow.
1056 */
[39e141f]1057DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
[0d57541]1058
[7068467]1059/*@}*/
[2c060e3]1060
[7f32bb5]1061/** @name Sizes
1062 * This section deals with finding or setting the various different lengths
1063 * a packet can have
[7068467]1064 * @{
[7f32bb5]1065 */
[2c060e3]1066/** Get the size of the packet in the trace
[2137b49]1067 * @param packet        the packet opaque pointer
[7068467]1068 * @return the size of the packet in the trace
[2c060e3]1069 * @note Due to this being a header capture, or anonymisation, this may not
[52f8fc2]1070 * be the same size as the original packet.  See get_wire_length() for the
1071 * original size of the packet.
[2c060e3]1072 * @note This can (and often is) different for different packets in a trace!
[52f8fc2]1073 * @note This is sometimes called the "snaplen".
1074 * @note The return size refers to the network-level payload of the packet and
[83445f0]1075 * does not include any capture framing headers. For example, an Ethernet
1076 * packet with an empty TCP packet will return sizeof(ethernet_header) +
1077 * sizeof(ip_header) + sizeof(tcp_header).
[2c060e3]1078 */
[39e141f]1079DLLEXPORT SIMPLE_FUNCTION
[d8f02df]1080size_t trace_get_capture_length(const libtrace_packet_t *packet);
[2c060e3]1081
1082/** Get the size of the packet as it was seen on the wire.
[2137b49]1083 * @param packet        the packet opaque pointer
[7068467]1084 * @return the size of the packet as it was on the wire.
[2c060e3]1085 * @note Due to the trace being a header capture, or anonymisation this may
1086 * not be the same as the Capture Len.
[ed636fb4]1087 * @note trace_getwire_length \em{includes} FCS.
[2c060e3]1088 */
[39e141f]1089DLLEXPORT SIMPLE_FUNCTION
[d8f02df]1090size_t trace_get_wire_length(const libtrace_packet_t *packet);
[2c060e3]1091
[7c8eacf]1092/** Get the length of the capture framing headers.
1093 * @param packet        the packet opaque pointer
[1fbd938]1094 * @return the size of the packet as it was on the wire.
[7c8eacf]1095 * @note this length corresponds to the difference between the size of a
1096 * captured packet in memory, and the captured length of the packet
1097 */
[39e141f]1098DLLEXPORT SIMPLE_FUNCTION
[d8f02df]1099size_t trace_get_framing_length(const libtrace_packet_t *packet);
[7c8eacf]1100
[91c9552]1101/** Truncate ("snap") the packet at the suggested length
[7f32bb5]1102 * @param packet        the packet opaque pointer
1103 * @param size          the new length of the packet
[d3b2234]1104 * @return the new capture length of the packet, or the original capture
1105 * length of the packet if unchanged
[7f32bb5]1106 */
[39e141f]1107DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
[7f32bb5]1108
[7068467]1109/*@}*/
[7f32bb5]1110
[7c8eacf]1111
[2c060e3]1112/** Get the type of the link layer
[2137b49]1113 * @param packet        the packet opaque pointer
[7068467]1114 * @return libtrace_linktype_t
[2c060e3]1115 */
[39e141f]1116DLLEXPORT SIMPLE_FUNCTION
[eeab9832]1117libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
[2c060e3]1118
[80a2e99]1119/** Set the direction flag, if it has one
1120 * @param packet        the packet opaque pointer
[c37ab9f]1121 * @param direction     the new direction
1122 * @returns -1 on error, or the direction that was set.
[80a2e99]1123 */
[c37ab9f]1124DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction);
[80a2e99]1125
[14a9d05]1126/** Get the direction flag, if it has one
[2137b49]1127 * @param packet        the packet opaque pointer
[c37ab9f]1128 * @return a value containing the direction flag, or -1 if this is not supported
[9075d49]1129 * The direction is defined as 0 for packets originating locally (ie, outbound)
1130 * and 1 for packets originating remotely (ie, inbound).
1131 * Other values are possible, which might be overloaded to mean special things
1132 * for a special trace.
[14a9d05]1133 */
[39e141f]1134DLLEXPORT SIMPLE_FUNCTION
[c37ab9f]1135libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet);
[2c060e3]1136
[7f32bb5]1137/** @name BPF
1138 * This section deals with using Berkley Packet Filters
[7068467]1139 * @{
[7f32bb5]1140 */
[c681e86]1141/** setup a BPF filter
1142 * @param filterstring a char * containing the bpf filter string
[7068467]1143 * @return opaque pointer pointer to a libtrace_filter_t object
[fba4ca0]1144 * @note The filter is not actually compiled at this point, so no correctness
[362c5f2]1145 * tests are performed here. trace_create_filter will always return ok, but
[fba4ca0]1146 * if the filter is poorly constructed an error will be generated when the
1147 * filter is actually used
[c681e86]1148 */
[39e141f]1149DLLEXPORT SIMPLE_FUNCTION
[e2aebe7]1150libtrace_filter_t *trace_create_filter(const char *filterstring);
[c681e86]1151
1152/** apply a BPF filter
[2137b49]1153 * @param filter        the filter opaque pointer
1154 * @param packet        the packet opaque pointer
[e2aebe7]1155 * @return >0 if the filter matches, 0 if it doesn't, -1 on error.
[91c9552]1156 * @note Due to the way BPF filters are built, the filter is not actually
[e2aebe7]1157 * compiled until the first time trace_create_filter is called. If your filter
1158 * is incorrect, it will generate an error message and assert, exiting the
[91c9552]1159 * program. This behaviour may change to more graceful handling of this error
1160 * in the future.
[c681e86]1161 */
[e2aebe7]1162DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
[7068467]1163                const libtrace_packet_t *packet);
[747c501]1164
1165/** destory of BPF filter
1166 * @param filter        the filter opaque pointer
1167 * Deallocate all the resources associated with a BPF filter
1168 */
[e2aebe7]1169DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter);
[7068467]1170/*@}*/
[83e0a25]1171
[752709f]1172/** @name Portability
1173 * This section has functions that causes annoyances to portability for one
1174 * reason or another.
1175 * @{
1176 */
1177
1178/** Convert an ethernet address to a string
1179 * @param addr  Ethernet address in network byte order
1180 * @param buf   Buffer to store the ascii representation, or NULL
1181 * @return buf, or if buf is NULL then a statically allocated buffer.
1182 *
1183 * This function is similar to the GNU ether_ntoa_r function, with a few
1184 * minor differences.  if NULL is passed as buf, then the function will
1185 * use an internal static buffer, if NULL isn't passed then the function
1186 * will use that buffer instead.
1187 *
1188 * @note the type of addr isn't struct ether_addr as it is with ether_ntoa_r,
1189 * however it is bit compatible so that a cast will work.
1190 */
[39e141f]1191DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
[752709f]1192
1193/** Convert a string to an ethernet address
1194 * @param buf   Ethernet address in hex format delimited with :'s.
1195 * @param addr  buffer to store the binary representation, or NULL
1196 * @return addr, or if addr is NULL, then a statically allocated buffer.
1197 *
1198 * This function is similar to the GNU ether_aton_r function, with a few
1199 * minor differences.  if NULL is passed as addr, then the function will
1200 * use an internal static buffer, if NULL isn't passed then the function will
1201 * use that buffer instead.
1202 *
1203 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
1204 * however it is bit compatible so that a cast will work.
1205 */
[39e141f]1206DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
[752709f]1207
1208/*@}*/
1209
1210
[31709e8d]1211/** Which port is the server port */
1212typedef enum {
1213        USE_DEST,       /**< Destination port is the server port */
1214        USE_SOURCE      /**< Source port is the server port */
1215} serverport_t;
[83e0a25]1216
[58e9e02]1217/** Get the source port
1218 * @param packet        the packet to read from
[83445f0]1219 * @return a port in \em HOST byte order, or equivalent to ports for this
[58e9e02]1220 * protocol, or 0 if this protocol has no ports.
1221 */
[39e141f]1222DLLEXPORT SIMPLE_FUNCTION
[7068467]1223uint16_t trace_get_source_port(const libtrace_packet_t *packet);
[58e9e02]1224
1225/** Get the destination port
1226 * @param packet        the packet to read from
[7068467]1227 * @return a port in \em HOST byte order, or equivilent to ports for this
[58e9e02]1228 * protocol, or 0 if this protocol has no ports.
1229 */
[39e141f]1230DLLEXPORT SIMPLE_FUNCTION
[7068467]1231uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
[58e9e02]1232
[83e0a25]1233/** hint at the server port in specified protocol
1234 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
1235 * @param source        the source port from the packet
1236 * @param dest          the destination port from the packet
[7068467]1237 * @return one of USE_SOURCE or USE_DEST depending on which one you should use
[fba4ca0]1238 * @note ports must be in \em HOST byte order!
[83e0a25]1239 */
[39e141f]1240DLLEXPORT SIMPLE_FUNCTION
[83e0a25]1241int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
1242
[de8c420b]1243/** Takes a uri and splits it into a format and uridata component.
1244 * @param uri           the uri to be parsed
1245 * @param format        destination location for the format component of the uri
[7068467]1246 * @return 0 if an error occured, otherwise return the uridata component
[de8c420b]1247 */
[39e141f]1248DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
[3d4d52d]1249
[2e223eca]1250/** RT protocol base format identifiers
1251 * This is used to say what kind of packet is being sent over the rt protocol
1252 */
[6dbc47a]1253enum base_format_t {
1254        TRACE_FORMAT_ERF          =1,
1255        TRACE_FORMAT_PCAP         =2,
[dd2eaee]1256        TRACE_FORMAT_PCAPFILE     =3,
1257        TRACE_FORMAT_WAG          =4,
1258        TRACE_FORMAT_RT           =5,
1259        TRACE_FORMAT_LEGACY_ATM   =6,
1260        TRACE_FORMAT_LEGACY_POS   =7,
1261        TRACE_FORMAT_LEGACY_ETH   =8,
[cd7eec7]1262        TRACE_FORMAT_LINUX_NATIVE =9,
1263        TRACE_FORMAT_DUCK         =10
[6dbc47a]1264};
1265
[83445f0]1266/** Gets the format type for a given packet.
[6dbc47a]1267 * @param packet        the packet opaque pointer
1268 * @return the format of the packet
1269 */
[e093e5c]1270DLLEXPORT
[6dbc47a]1271enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1272
[c5ebbaa]1273/** Construct a packet from a buffer.
1274 * @param packet[in,out]        Libtrace Packet object to update with the new
1275 *                              data.
1276 * @param linktype              The linktype of the packet.
1277 * @param[in] data              The packet data (including linklayer)
1278 * @param len                   Length of packet data
1279 */
1280DLLEXPORT
1281void trace_construct_packet(libtrace_packet_t *packet,
1282                libtrace_linktype_t linktype, const void *data, uint16_t len);
1283
[83445f0]1284
[27615e9]1285#ifdef __cplusplus
[7068467]1286} /* extern "C" */
1287#endif /* #ifdef __cplusplus */
1288#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.