source: lib/libtrace.h.in @ 835a1ec

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 835a1ec was 835a1ec, checked in by Perry Lorier <perry@…>, 15 years ago

Fix broken endianness fixes

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