source: lib/libtrace.h @ 23971d0

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

Portability fixes for windows

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