source: lib/libtrace.h @ 91c9552

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

Documentation fixes
Make bpf return valid errors
Add trace_copy_packet()

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