source: lib/libtrace.h.in @ a700713

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

Add documentation to new function

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