source: lib/libtrace.h @ 0d57541

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

Add preliminary seek support

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