source: lib/libtrace.h @ e7bc155

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

Merge more of mattb's portability fixes.

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