source: lib/libtrace.h @ ce0d2cc

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

More windows fixes merged

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