source: lib/libtrace.h @ 12778c2

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

More commenting

  • Property mode set to 100644
File size: 41.7 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id$
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/* LLC */
325  uint8_t dsap;                 /**< Destination Service Access Point */
326  uint8_t ssap;                 /**< Source Service Access Point */
327  uint8_t control;
328/* SNAP */
329  uint32_t oui:24;              /**< Organisationally Unique Identifier (scope)*/
330  uint16_t type;                /**< Protocol within OUI */
331} PACKED libtrace_llcsnap_t;
332
333/** 802.3 frame */
334typedef struct libtrace_ether
335{
336  uint8_t ether_dhost[6];       /**< destination ether addr */
337  uint8_t ether_shost[6];       /**< source ether addr */
338  uint16_t ether_type;          /**< packet type ID field (next-header) */
339} PACKED libtrace_ether_t;
340
341/** 802.1Q frame */
342typedef struct libtrace_8021q
343{
344  uint8_t  ether_dhost[6];      /**< destination eth addr */
345  uint8_t  ether_shost[6];      /**< source ether addr    */
346  uint16_t ether_type;          /**< packet type ID field , 0x8100 for VLAN */
347  unsigned int vlan_pri:3;       /**< vlan user priority */
348  unsigned int vlan_cfi:1;       /**< vlan format indicator,
349                                   * 0 for ethernet, 1 for token ring */
350  unsigned int vlan_id:12;       /**< vlan id */
351  uint16_t vlan_ether_type;      /**< vlan sub-packet type ID field
352                                   * (next-header)*/
353} PACKED libtrace_8021q_t;
354
355/** ATM cell */
356typedef struct libtrace_atm_cell
357{
358  unsigned int gfc:4;           /**< Generic Flow Control */
359  uint8_t vpi;                  /**< Virtual Path Identifier */
360  uint16_t vci;                 /**< Virtual Channel Identifier */
361  unsigned int pt:3;            /**< Payload Type */
362  unsigned int clp:1;           /**< Cell Loss Priority */
363  uint8_t hec;                  /**< Header Error Control */
364} PACKED libtrace_atm_cell_t;
365
366/** POS header */
367typedef struct libtrace_pos
368{
369 uint16_t header;
370 uint16_t ether_type;           /**< ether type */
371} PACKED libtrace_pos_t;
372
373/** 802.11 header */
374typedef struct libtrace_80211_t {
375        unsigned int      protocol:2;
376        unsigned int      type:2;
377        unsigned int      subtype:4;
378        unsigned int      to_ds:1;              /**< Packet to Distribution Service */
379        unsigned int      from_ds:1;            /**< Packet from Distribution Service */
380        unsigned int      more_frag:1;          /**< Packet has more fragments */
381        unsigned int      retry:1;              /**< Packet is a retry */
382        unsigned int      power:1;
383        unsigned int      more_data:1;
384        unsigned int      wep:1;
385        unsigned int      order:1;
386        unsigned int     duration;
387        uint8_t      mac1[6];
388        uint8_t      mac2[6];
389        uint8_t      mac3[6];
390        uint16_t     SeqCtl;
391        uint8_t      mac4[6];
392} PACKED libtrace_80211_t;
393
394#ifdef WIN32
395#pragma pack(pop)
396#endif
397
398
399/*@}*/
400
401/** Prints help information for libtrace
402 *
403 * Function prints out some basic help information regarding libtrace,
404 * and then prints out the help() function registered with each input module
405 */
406DLLEXPORT void trace_help();
407
408/** Gets the output format for a given output trace
409 *
410 * @param libtrace      the output trace to get the name of the format fo
411 * @return callee-owned null-terminated char* containing the output format
412 *
413 */
414DLLEXPORT SIMPLE_FUNCTION
415char *trace_get_output_format(const libtrace_out_t *libtrace);
416
417/** @name Trace management
418 * These members deal with creating, configuring, starting, pausing and
419 * cleaning up a trace object
420 *@{
421 */
422
423/** Create a trace file from a URI
424 *
425 * @param uri containing a valid libtrace URI
426 * @return opaque pointer to a libtrace_t
427 *
428 * Valid URI's are:
429 *  - erf:/path/to/erf/file
430 *  - erf:/path/to/erf/file.gz
431 *  - erf:/path/to/rtclient/socket
432 *  - erf:-  (stdin)
433 *  - dag:/dev/dagcard                 
434 *  - pcapint:pcapinterface                (eg: pcap:eth0)
435 *  - pcap:/path/to/pcap/file
436 *  - pcap:-
437 *  - rtclient:hostname
438 *  - rtclient:hostname:port
439 *  - wag:-
440 *  - wag:/path/to/wag/file
441 *  - wag:/path/to/wag/file.gz
442 *  - wag:/path/to/wag/socket
443 *
444 *  If an error occured when attempting to open the trace file, an error
445 *  trace is returned and trace_get_error should be called to find out
446 *  if an error occured, and what that error was.  The trace is created in the
447 *  configuration state, you must call trace_start to start the capture.
448 */
449DLLEXPORT libtrace_t *trace_create(const char *uri);
450
451/** Creates a "dummy" trace file that has only the format type set.
452 *
453 * @return opaque pointer to a (sparsely initialised) libtrace_t
454 *
455 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions
456 * with the dummy trace. Its intended purpose is to act as a packet->trace for
457 * libtrace_packet_t's that are not associated with a libtrace_t structure.
458 */
459DLLEXPORT libtrace_t *trace_create_dead(const char *uri);
460
461/** Creates a trace output file from a URI.
462 *
463 * @param uri   the uri string describing the output format and destination
464 * @return opaque pointer to a libtrace_output_t
465 * @author Shane Alcock
466 *
467 * Valid URI's are:
468 *  - gzerf:/path/to/erf/file.gz
469 *  - gzerf:/path/to/erf/file
470 *  - rtserver:hostname
471 *  - rtserver:hostname:port
472 *
473 *  If an error occured when attempting to open the output trace, NULL is returned
474 *  and trace_errno is set. Use trace_perror() to get more information
475 */
476DLLEXPORT libtrace_out_t *trace_create_output(const char *uri);
477
478/** Start the capture
479 * @param libtrace      The trace to start
480 * @return 0 on success
481 *
482 * This does the actual work with starting the trace capture, and applying
483 * all the config options.  This may fail.
484 */
485DLLEXPORT int trace_start(libtrace_t *libtrace);
486
487/** Pause the capture
488 * @param libtrace      The trace to pause
489 * @return 0 on success
490 *
491 * This stops a capture in progress and returns you to the configuration
492 * state.  Any packets that arrive after trace_pause() has been called
493 * will be discarded.  To resume capture, call trace_start().
494 */
495DLLEXPORT int trace_pause(libtrace_t *libtrace);
496
497/** Start an output trace
498 * @param libtrace      The trace to start
499 * @return 0 on success
500 *
501 * This does the actual work with starting a trace for write.  This generally
502 * creates the file.
503 */
504DLLEXPORT int trace_start_output(libtrace_out_t *libtrace);
505
506/** Valid trace capture options */
507typedef enum {
508        TRACE_OPTION_SNAPLEN, /**< Number of bytes captured */
509        TRACE_OPTION_PROMISC, /**< Capture packets to other hosts */
510        TRACE_OPTION_FILTER   /**< Apply this filter to all packets recieved */
511} trace_option_t;
512
513/** Sets an input config option
514 * @param libtrace      the trace object to apply the option to
515 * @param option        the option to set
516 * @param value         the value to set the option to
517 * @return -1 if option configuration failed, 0 otherwise
518 * This should be called after trace_create, and before trace_start
519 */
520DLLEXPORT int trace_config(libtrace_t *libtrace,
521                trace_option_t option,
522                void *value);
523
524typedef enum {
525        TRACE_OPTION_OUTPUT_FILEFLAGS, /**< File flags to open the trace file
526                                        * with.  eg O_APPEND
527                                        */
528        TRACE_OPTION_OUTPUT_COMPRESS   /**< Compression level, eg 6. */
529} trace_option_output_t;
530
531/** Sets an output config option
532 *
533 * @param libtrace      the output trace object to apply the option to
534 * @param option        the option to set
535 * @param value         the value to set the option to
536 * @return -1 if option configuration failed, 0 otherwise
537 * This should be called after trace_create_output, and before
538 * trace_start_output
539 */
540DLLEXPORT int trace_config_output(libtrace_out_t *libtrace, 
541                trace_option_output_t option,
542                void *value
543                );
544
545/** Close a trace file, freeing up any resources it may have been using
546 *
547 */
548DLLEXPORT void trace_destroy(libtrace_t *trace);
549
550/** Close a trace file, freeing up any resources it may have been using
551 * @param trace         trace file to be destroyed
552 */
553DLLEXPORT void trace_destroy_dead(libtrace_t *trace);
554
555/** Close a trace output file, freeing up any resources it may have been using
556 * @param trace         the output trace file to be destroyed
557 *
558 * @author Shane Alcock
559 */
560DLLEXPORT void trace_destroy_output(libtrace_out_t *trace);
561
562/** Check (and clear) the current error state of an input trace
563 * @param trace         the trace file to check the error state on
564 * @return Error report
565 * This reads and returns the current error state and sets the current error
566 * to "no error".
567 */
568DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace);
569
570/** Return if there is an error
571 * @param trace         the trace file to check the error state on
572 * This does not clear the error status, and only returns true or false.
573 */
574DLLEXPORT bool trace_is_err(libtrace_t *trace);
575
576/** Output an error message to stderr and clear the error status.
577 * @param trace         the trace with the error to output
578 * @param msg           the message to prefix to the error
579 * This function does clear the error status.
580 */
581DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...);
582
583/** Check (and clear) the current error state of an output trace
584 * @param trace         the output trace file to check the error state on
585 * @return Error report
586 * This reads and returns the current error state and sets the current error
587 * to "no error".
588 */
589DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace);
590
591/** Return if there is an error
592 * @param trace         the trace file to check the error state on
593 * This does not clear the error status, and only returns true or false.
594 */
595DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace);
596
597/** Output an error message to stderr and clear the error status.
598 * @param trace         the trace with the error to output
599 * @param msg           the message to prefix to the error
600 * This function does clear the error status.
601 */
602DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...);
603
604
605/*@}*/
606
607/** @name Reading/Writing packets
608 * These members deal with creating, reading and writing packets
609 *
610 * @{
611 */
612
613/** Create a new packet object
614 *
615 * @return a pointer to an initialised libtrace_packet_t object
616 */
617DLLEXPORT libtrace_packet_t *trace_create_packet();
618
619/** Copy a packet
620 * @param packet        the source packet to copy
621 * @return a new packet which has the same content as the source packet
622 * @note This always involves a copy, which can be slow.  Use of this
623 * function should be avoided where possible.
624 * @par The reason you would want to use this function is that a zerocopied
625 * packet from a device is using the devices memory which may be a limited
626 * resource.  Copying the packet will cause it to be copied into the systems
627 * memory.
628 */
629DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet);
630
631/** Destroy a packet object
632 *
633 * sideeffect: sets packet to NULL
634 */
635DLLEXPORT void trace_destroy_packet(libtrace_packet_t **packet);
636
637
638/** Read one packet from the trace into buffer
639 *
640 * @param trace         the libtrace opaque pointer
641 * @param packet        the packet opaque pointer
642 * @return 0 on EOF, negative value on error
643 *
644 * @note the trace must have been started with trace_start before calling
645 * this function
646 */
647DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet);
648
649/** Event types
650 * see \ref libtrace_eventobj_t and \ref trace_event
651 */
652typedef enum {
653        TRACE_EVENT_IOWAIT,     /**< Need to block on fd */
654        TRACE_EVENT_SLEEP,      /**< Sleep for some time */
655        TRACE_EVENT_PACKET,     /**< packet has arrived */
656        TRACE_EVENT_TERMINATE   /**< End of trace */
657} libtrace_event_t;
658
659/** structure returned by libtrace_event explaining what the current event is */
660typedef struct libtrace_eventobj_t {
661        libtrace_event_t type; /**< event type (iowait,sleep,packet) */
662        int fd;                /**< if IOWAIT, the fd to sleep on */
663        double seconds;        /**< if SLEEP, the amount of time to sleep for
664                                */
665        int size;              /**< if PACKET, the value returned from
666                                *  trace_read_packet
667                                */
668} libtrace_eventobj_t;
669
670/** process a libtrace event
671 * @param trace the libtrace opaque pointer
672 * @param packet the libtrace_packet opaque pointer
673 * @return libtrace_event struct containing the type, and potential
674 *      fd or seconds to sleep on
675 *
676 * Type can be:
677 *  TRACE_EVENT_IOWAIT  Waiting on I/O on fd
678 *  TRACE_EVENT_SLEEP   Next event in seconds
679 *  TRACE_EVENT_PACKET  Packet arrived in buffer with size size
680 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
681 */
682DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
683                libtrace_packet_t *packet);
684
685
686/** Write one packet out to the output trace
687 *
688 * @param trace         the libtrace_out opaque pointer
689 * @param packet        the packet opaque pointer
690 * @return the number of bytes written out, if zero or negative then an error has occured.
691 */
692DLLEXPORT int trace_write_packet(libtrace_out_t *trace, const libtrace_packet_t *packet);
693/*@}*/
694
695/** @name Protocol decodes
696 * These functions locate and return a pointer to various headers inside a
697 * packet
698 * @{
699 */
700
701/** get a pointer to the link layer
702 * @param packet        the packet opaque pointer
703 *
704 * @return a pointer to the link layer, or NULL if there is no link layer
705 *
706 * @note you should call getLinkType() to find out what type of link layer
707 * this is
708 */
709DLLEXPORT SIMPLE_FUNCTION
710void *trace_get_link(const libtrace_packet_t *packet);
711
712/** get a pointer to the IP header (if any)
713 * @param packet        the packet opaque pointer
714 *
715 * @return a pointer to the IP header, or NULL if there is not an IP packet
716 */
717SIMPLE_FUNCTION
718libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet);
719
720/** Gets a pointer to the transport layer header (if any)
721 * @param packet        a pointer to a libtrace_packet structure
722 * @param[out] proto    transport layer protocol
723 *
724 * @return a pointer to the transport layer header, or NULL if there is no header
725 *
726 * @note proto may be NULL if proto is unneeded.
727 */
728DLLEXPORT void *trace_get_transport(libtrace_packet_t *packet, uint8_t *proto, 
729                uint32_t *remaining);
730
731/** Gets a pointer to the payload given a pointer to the IP header
732 * @param ip            The IP Header
733 * @param[out] proto    An output variable of the IP protocol
734 * @param[in,out] remaining Updated with the number of bytes remaining
735 *
736 * @return a pointer to the transport layer header, or NULL if header isn't present.
737 *
738 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
739 * of bytes captured of the IP header and beyond.  It will be updated after this
740 * function to the number of bytes remaining after the IP header (and any IP options)
741 * have been removed.
742 *
743 * proto may be NULL if not needed.
744 *
745 * @note This is similar to trace_get_transport_from_ip in libtrace2
746 */
747DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto,
748                uint32_t *remaining);
749
750/** Gets a pointer to the payload given a pointer to a tcp header
751 * @param tcp           The tcp Header
752 * @param[in,out] remaining Updated with the number of bytes remaining
753 *
754 * @return a pointer to the tcp payload, or NULL if the payload isn't present.
755 *
756 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
757 * of bytes captured of the TCP header and beyond.  It will be updated after this
758 * function to the number of bytes remaining after the TCP header (and any TCP options)
759 * have been removed.
760 *
761 * @note This is similar to trace_get_transport_from_ip in libtrace2
762 */
763DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining);
764
765/** Gets a pointer to the payload given a pointer to a udp header
766 * @param udp           The udp Header
767 * @param[in,out] remaining Updated with the number of bytes remaining
768 *
769 * @return a pointer to the udp payload, or NULL if the payload isn't present.
770 *
771 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
772 * of bytes captured of the TCP header and beyond.  It will be updated after this
773 * function to the number of bytes remaining after the TCP header (and any TCP options)
774 * have been removed.
775 *
776 * @note This is similar trace_get_transport_from_ip in libtrace2
777 */
778DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining);
779
780/** Gets a pointer to the payload given a pointer to a icmp header
781 * @param icmp          The icmp Header
782 * @param[in,out] remaining Updated with the number of bytes remaining
783 *
784 * @return a pointer to the icmp payload, or NULL if the payload isn't present.
785 *
786 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
787 * of bytes captured of the TCP header and beyond.  It will be updated after this
788 * function to the number of bytes remaining after the TCP header (and any TCP options)
789 * have been removed.
790 *
791 * @note This is similar to trace_get_payload_from_icmp in libtrace2
792 */
793DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *skipped);
794
795/** get a pointer to the TCP header (if any)
796 * @param packet        the packet opaque pointer
797 *
798 * @return a pointer to the TCP header, or NULL if there is not a TCP packet
799 */
800DLLEXPORT SIMPLE_FUNCTION
801libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet);
802
803/** get a pointer to the TCP header (if any) given a pointer to the IP header
804 * @param ip            The IP header
805 * @param[in,out] remaining Updated with the number of bytes remaining
806 *
807 * @return a pointer to the TCP header, or NULL if this is not a TCP packet
808 *
809 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
810 * of bytes captured of the TCP header and beyond.  It will be updated after this
811 * function to the number of bytes remaining after the TCP header (and any TCP options)
812 * have been removed.
813 *
814 * @note The last parameter has changed from libtrace2
815 */
816DLLEXPORT SIMPLE_FUNCTION
817libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining);
818
819/** get a pointer to the UDP header (if any)
820 * @param packet        the packet opaque pointer
821 *
822 * @return a pointer to the UDP header, or NULL if this is not a UDP packet
823 */
824DLLEXPORT SIMPLE_FUNCTION
825libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet);
826
827/** get a pointer to the UDP header (if any) given a pointer to the IP header
828 * @param       ip      The IP header
829 * @param[in,out] remaining Updated with the number of bytes remaining
830 *
831 * @return a pointer to the UDP header, or NULL if this is not an UDP packet
832 *
833 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
834 * of bytes captured of the TCP header and beyond.  It will be updated after this
835 * function to the number of bytes remaining after the TCP header (and any TCP options)
836 * have been removed.
837 *
838 * @note Beware the change from libtrace2 from skipped to remaining
839 */
840DLLEXPORT SIMPLE_FUNCTION
841libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
842
843/** get a pointer to the ICMP header (if any)
844 * @param packet        the packet opaque pointer
845 *
846 * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet
847 */
848DLLEXPORT SIMPLE_FUNCTION
849libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet);
850
851/** get a pointer to the ICMP header (if any) given a pointer to the IP header
852 * @param ip            The IP header
853 * @param[in,out] remaining Updated with the number of bytes remaining
854 *
855 * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet
856 *
857 * Remaining may be NULL.  If Remaining is not NULL it must point to the number
858 * of bytes captured of the TCP header and beyond.  It will be updated after this
859 * function to the number of bytes remaining after the TCP header (and any TCP options)
860 * have been removed.
861 *
862 * @note Beware the change from libtrace2 from skipped to remaining
863 */
864DLLEXPORT SIMPLE_FUNCTION
865libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining);
866
867/** Get the destination MAC addres
868 * @param packet        the packet opaque pointer
869 * @return a pointer to the destination mac, (or NULL if there is no
870 * destination MAC)
871 */
872DLLEXPORT SIMPLE_FUNCTION
873uint8_t *trace_get_destination_mac(libtrace_packet_t *packet);
874
875/** Get the source MAC addres
876 * @param packet        the packet opaque pointer
877 * @return a pointer to the source mac, (or NULL if there is no source MAC)
878 */
879SIMPLE_FUNCTION
880uint8_t *trace_get_source_mac(libtrace_packet_t *packet);
881
882/** Get the source addres
883 * @param packet        the packet opaque pointer
884 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
885 *                      static storage.
886 * @return NULL if there is no source address, or a sockaddr holding a v4 or v6 address
887 */
888DLLEXPORT SIMPLE_FUNCTION
889struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet,
890                struct sockaddr *addr);
891
892/** Get the source addres
893 * @param packet        the packet opaque pointer
894 * @param addr          a pointer to a sockaddr to store the address in, or NULL to use
895 *                      static storage.
896 * @return NULL if there is no source address, or a sockaddr holding a v4 or v6 address
897 */
898DLLEXPORT SIMPLE_FUNCTION
899struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet,
900                struct sockaddr *addr);
901
902/*@}*/
903
904/** parse an ip or tcp option
905 * @param[in,out] ptr   the pointer to the current option
906 * @param[in,out] len   the length of the remaining buffer
907 * @param[out] type     the type of the option
908 * @param[out] optlen   the length of the option
909 * @param[out] data     the data of the option
910 *
911 * @return bool true if there is another option (and the fields are filled in)
912 *               or false if this was the last option.
913 *
914 * This updates ptr to point to the next option after this one, and updates
915 * len to be the number of bytes remaining in the options area.  Type is updated
916 * to be the code of this option, and data points to the data of this option,
917 * with optlen saying how many bytes there are.
918 *
919 * @note Beware of fragmented packets.
920 */
921DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
922                        unsigned char *type,
923                        unsigned char *optlen,
924                        unsigned char **data);
925
926
927/** @name Time
928 * These functions deal with time that a packet arrived and return it
929 * in various formats
930 * @{
931 */
932/** Get the current time in DAG time format
933 * @param packet        the packet opaque pointer
934 *
935 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
936 * past 1970-01-01, the lower 32bits are partial seconds)
937 * @author Daniel Lawson
938 */
939DLLEXPORT SIMPLE_FUNCTION
940uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
941
942/** Get the current time in struct timeval
943 * @param packet        the packet opaque pointer
944 *
945 * @return time that this packet was seen in a struct timeval
946 * @author Daniel Lawson
947 * @author Perry Lorier
948 */ 
949DLLEXPORT SIMPLE_FUNCTION
950struct timeval trace_get_timeval(const libtrace_packet_t *packet);
951
952/** Get the current time in floating point seconds
953 * @param packet        the packet opaque pointer
954 *
955 * @return time that this packet was seen in 64bit floating point seconds
956 * @author Daniel Lawson
957 * @author Perry Lorier
958 */
959DLLEXPORT SIMPLE_FUNCTION
960double trace_get_seconds(const libtrace_packet_t *packet);
961
962/** Seek within a trace
963 * @param trace         trace to seek
964 * @param seconds       time to seek to
965 * @return 0 on success.
966 * Make the next packet read to be the first packet to occur at or after the
967 * time searched for.  This must be called in the configuration state (ie,
968 * before trace_start() or after trace_pause().
969 * @note This function may be extremely slow.
970 */
971DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds);
972
973/** Seek within a trace
974 * @param trace         trace to seek
975 * @param tv            time to seek to
976 * @return 0 on success.
977 * Make the next packet read to be the first packet to occur at or after the
978 * time searched for.  This must be called in the configuration state (ie,
979 * before trace_start() or after trace_pause().
980 * @note This function may be extremely slow.
981 */
982DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
983
984/** Seek within a trace
985 * @param trace         trace to seek
986 * @param ts            erf timestamp
987 * @return 0 on success.
988 * Make the next packet read to be the first packet to occur at or after the
989 * time searched for.  This must be called in the configuration state (ie,
990 * before trace_start() or after trace_pause().
991 * @note This function may be extremely slow.
992 */
993DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts);
994
995/*@}*/
996
997/** @name Sizes
998 * This section deals with finding or setting the various different lengths
999 * a packet can have
1000 * @{
1001 */
1002/** Get the size of the packet in the trace
1003 * @param packet        the packet opaque pointer
1004 * @return the size of the packet in the trace
1005 * @author Perry Lorier
1006 * @note Due to this being a header capture, or anonymisation, this may not
1007 * be the same size as the original packet.  See get_wire_length() for the
1008 * original size of the packet.
1009 * @note This can (and often is) different for different packets in a trace!
1010 * @note This is sometimes called the "snaplen".
1011 * @note The return size refers to the network-level payload of the packet and
1012 * does not include any capture headers. For example, an Ethernet packet with
1013 * an empty TCP packet will return sizeof(ethernet_header) + sizeof(ip_header)
1014 * + sizeof(tcp_header).
1015 */
1016DLLEXPORT SIMPLE_FUNCTION
1017size_t trace_get_capture_length(const libtrace_packet_t *packet);
1018
1019/** Get the size of the packet as it was seen on the wire.
1020 * @param packet        the packet opaque pointer
1021 * @return the size of the packet as it was on the wire.
1022 * @note Due to the trace being a header capture, or anonymisation this may
1023 * not be the same as the Capture Len.
1024 * @note trace_getwire_length \em{includes} FCS.
1025 */ 
1026DLLEXPORT SIMPLE_FUNCTION
1027size_t trace_get_wire_length(const libtrace_packet_t *packet);
1028
1029/** Get the length of the capture framing headers.
1030 * @param packet        the packet opaque pointer
1031 * @return the size of the packet as it was on the wire.
1032 * @author Perry Lorier
1033 * @author Daniel Lawson
1034 * @note this length corresponds to the difference between the size of a
1035 * captured packet in memory, and the captured length of the packet
1036 */ 
1037DLLEXPORT SIMPLE_FUNCTION
1038size_t trace_get_framing_length(const libtrace_packet_t *packet);
1039
1040/** Truncate ("snap") the packet at the suggested length
1041 * @param packet        the packet opaque pointer
1042 * @param size          the new length of the packet
1043 * @return the new capture length of the packet, or the original capture
1044 * length of the packet if unchanged
1045 */
1046DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
1047
1048/*@}*/
1049
1050
1051/** Link layer types
1052 * This enumates the various different link types that libtrace understands
1053 */
1054typedef enum { 
1055       TRACE_TYPE_HDLC_POS = 1, 
1056       TRACE_TYPE_ETH,                  /**< 802.3 style Ethernet */
1057       TRACE_TYPE_ATM,
1058       TRACE_TYPE_AAL5,
1059       TRACE_TYPE_80211,                /**< 802.11 frames */
1060       TRACE_TYPE_NONE,
1061       TRACE_TYPE_LINUX_SLL,            /**< Linux "null" framing */
1062       TRACE_TYPE_PFLOG,                /**< FreeBSD's PFlug */
1063       TRACE_TYPE_POS,
1064       TRACE_TYPE_80211_PRISM = 12
1065     } libtrace_linktype_t;
1066
1067/** Get the type of the link layer
1068 * @param packet        the packet opaque pointer
1069 * @return libtrace_linktype_t
1070 * @author Perry Lorier
1071 * @author Daniel Lawson
1072 */
1073DLLEXPORT SIMPLE_FUNCTION
1074libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
1075
1076/** Set the direction flag, if it has one
1077 * @param packet        the packet opaque pointer
1078 * @param direction     the new direction (0,1,2,3)
1079 * @return a signed value containing the direction flag, or -1 if this is not supported
1080 * @author Daniel Lawson
1081 */
1082DLLEXPORT int8_t trace_set_direction(libtrace_packet_t *packet, int8_t direction);
1083
1084/** Get the direction flag, if it has one
1085 * @param packet        the packet opaque pointer
1086 * @return a signed value containing the direction flag, or -1 if this is not supported
1087 * The direction is defined as 0 for packets originating locally (ie, outbound)
1088 * and 1 for packets originating remotely (ie, inbound).
1089 * Other values are possible, which might be overloaded to mean special things
1090 * for a special trace.
1091 * @author Daniel Lawson
1092 */
1093DLLEXPORT SIMPLE_FUNCTION
1094int8_t trace_get_direction(const libtrace_packet_t *packet);
1095
1096/** @name BPF
1097 * This section deals with using Berkley Packet Filters
1098 * @{
1099 */
1100/** setup a BPF filter
1101 * @param filterstring a char * containing the bpf filter string
1102 * @return opaque pointer pointer to a libtrace_filter_t object
1103 * @author Daniel Lawson
1104 * @note The filter is not actually compiled at this point, so no correctness
1105 * tests are performed here. trace_bpf_setfilter will always return ok, but
1106 * if the filter is poorly constructed an error will be generated when the
1107 * filter is actually used
1108 */
1109DLLEXPORT SIMPLE_FUNCTION
1110libtrace_filter_t *trace_bpf_setfilter(const char *filterstring);
1111
1112/** apply a BPF filter
1113 * @param filter        the filter opaque pointer
1114 * @param packet        the packet opaque pointer
1115 * @return 1 if the filter matches, 0 if it doesn't.
1116 * @note Due to the way BPF filters are built, the filter is not actually
1117 * compiled until the first time trace_bpf_filter is called. If your filter is
1118 * incorrect, it will generate an error message and assert, exiting the
1119 * program. This behaviour may change to more graceful handling of this error
1120 * in the future.
1121 */
1122DLLEXPORT int trace_bpf_filter(libtrace_filter_t *filter,
1123                const libtrace_packet_t *packet);
1124/*@}*/
1125
1126/** @name Portability
1127 * This section has functions that causes annoyances to portability for one
1128 * reason or another.
1129 * @{
1130 */
1131
1132/** Convert an ethernet address to a string
1133 * @param addr  Ethernet address in network byte order
1134 * @param buf   Buffer to store the ascii representation, or NULL
1135 * @return buf, or if buf is NULL then a statically allocated buffer.
1136 *
1137 * This function is similar to the GNU ether_ntoa_r function, with a few
1138 * minor differences.  if NULL is passed as buf, then the function will
1139 * use an internal static buffer, if NULL isn't passed then the function
1140 * will use that buffer instead.
1141 *
1142 * @note the type of addr isn't struct ether_addr as it is with ether_ntoa_r,
1143 * however it is bit compatible so that a cast will work.
1144 */ 
1145DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf);
1146
1147/** Convert a string to an ethernet address
1148 * @param buf   Ethernet address in hex format delimited with :'s.
1149 * @param addr  buffer to store the binary representation, or NULL
1150 * @return addr, or if addr is NULL, then a statically allocated buffer.
1151 *
1152 * This function is similar to the GNU ether_aton_r function, with a few
1153 * minor differences.  if NULL is passed as addr, then the function will
1154 * use an internal static buffer, if NULL isn't passed then the function will
1155 * use that buffer instead.
1156 *
1157 * @note the type of addr isn't struct ether_addr as it is with ether_aton_r,
1158 * however it is bit compatible so that a cast will work.
1159 */
1160DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr);
1161
1162/*@}*/
1163
1164
1165/** Which port is the server port */
1166typedef enum {
1167        USE_DEST,       /**< Destination port is the server port */
1168        USE_SOURCE      /**< Source port is the server port */
1169} serverport_t;
1170
1171/** Get the source port
1172 * @param packet        the packet to read from
1173 * @return a port in \em HOST byte order, or equivilent to ports for this
1174 * protocol, or 0 if this protocol has no ports.
1175 * @author Perry Lorier
1176 */
1177DLLEXPORT SIMPLE_FUNCTION
1178uint16_t trace_get_source_port(const libtrace_packet_t *packet);
1179
1180/** Get the destination port
1181 * @param packet        the packet to read from
1182 * @return a port in \em HOST byte order, or equivilent to ports for this
1183 * protocol, or 0 if this protocol has no ports.
1184 * @author Perry Lorier
1185 */
1186DLLEXPORT SIMPLE_FUNCTION
1187uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
1188
1189/** hint at the server port in specified protocol
1190 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
1191 * @param source        the source port from the packet
1192 * @param dest          the destination port from the packet
1193 * @return one of USE_SOURCE or USE_DEST depending on which one you should use
1194 * @note ports must be in \em HOST byte order!
1195 * @author Daniel Lawson
1196 */
1197DLLEXPORT SIMPLE_FUNCTION
1198int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
1199
1200/** Takes a uri and splits it into a format and uridata component.
1201 * Primarily for internal use but made available for external use.
1202 * @param uri           the uri to be parsed
1203 * @param format        destination location for the format component of the uri
1204 * @return 0 if an error occured, otherwise return the uridata component
1205 * @author Shane Alcock
1206 */
1207DLLEXPORT const char *trace_parse_uri(const char *uri, char **format);
1208
1209/** RT protocol base format identifiers
1210 * This is used to say what kind of packet is being sent over the rt protocol
1211 */ 
1212enum base_format_t {
1213        TRACE_FORMAT_ERF          =1,
1214        TRACE_FORMAT_PCAP         =2,
1215        TRACE_FORMAT_PCAPFILE     =3,
1216        TRACE_FORMAT_WAG          =4,
1217        TRACE_FORMAT_RT           =5,
1218        TRACE_FORMAT_LEGACY_ATM   =6,
1219        TRACE_FORMAT_LEGACY_POS   =7,
1220        TRACE_FORMAT_LEGACY_ETH   =8,
1221        TRACE_FORMAT_LINUX_NATIVE =9
1222};
1223
1224/** Gets the framing header type for a given packet.
1225 * @param packet        the packet opaque pointer
1226 * @return the format of the packet
1227 */
1228enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
1229
1230#ifdef __cplusplus
1231} /* extern "C" */
1232#endif /* #ifdef __cplusplus */
1233#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.