source: lib/libtrace.h @ 4af54d1

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

Split protocol decode logic into a seperate file, cleaned up some of the
protocol decode logic. Started IPv6 support, added some
trace_get_payload_from_* helper functions

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