source: lib/libtrace.h @ 9231fe5

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

Minor cleanups in format_rt
Shuffled protocols.c around for better vlan support
Cleaned up IPv6 support to be more generic (less duplicated code)
added trace_get_{source,destination}_address

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