source: lib/libtrace.h @ 7ac9705

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 7ac9705 was 7ac9705, checked in by Shane Alcock <salcock@…>, 16 years ago

Changed rt types so that every pcap dlt has a matching rt type
Added functions for conversion between rt type and pcap dlt
format_rt now uses trace_set_err appropriately if rt_connect fails
format_pcap will check if a trace is dead before trying to look at

format data

Dead traces now have their format_data set to NULL
packet->type is now 32 bits, not 8

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