source: lib/libtrace.h @ 95459ed5

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

Fix unconditional include

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