source: lib/libtrace.h @ 114b8d6

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

Remove reliance on pcap.h for DLT_'s (and while we're here, make them a pretty
enum, so we get warnings if we forget one somewhere)

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