source: lib/libtrace.h.in @ bdca481

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

Include stdint.h
Bump to beta3

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