source: lib/libtrace.h @ ed7c716

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

formatting

  • Property mode set to 100644
File size: 30.2 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id$
28 *
29 */
30
31#ifndef LIBTRACE_H
32#define LIBTRACE_H
33
34/** @file
35 *
36 * @brief Trace file processing library header
37 *
38 * @author Daniel Lawson
39 * @author Perry Lorier
40 *
41 * @version $Id$
42 *
43 * This library provides a per packet interface into a trace file, or a live
44 * captures.  It supports ERF, DAG cards, WAG cards, WAG's event format,
45 * pcap etc.
46 *
47 * @par Usage
48 * See the example/ directory in the source distribution for some simple examples
49 * @par Linking
50 * To use this library you need to link against libtrace by passing -ltrace
51 * to your linker. You may also need to link against a version of libpcap
52 * and of zlib which are compiled for largefile support (if you wish to access
53 * traces larger than 2 GB). This is left as an exercise for the reader. Debian
54 * Woody, at least, does not support large file offsets.
55 *
56 */
57
58#include <sys/types.h>
59#include <netinet/in.h>
60/** 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                                */
498        int size;              /**< if PACKET, the value returned from
499                                *  trace_read_packet
500                                */
501} libtrace_eventobj_t;
502
503/** process a libtrace event
504 * @param trace the libtrace opaque pointer
505 * @param packet the libtrace_packet opaque pointer
506 * @return libtrace_event struct containing the type, and potential
507 *      fd or seconds to sleep on
508 *
509 * Type can be:
510 *  TRACE_EVENT_IOWAIT  Waiting on I/O on fd
511 *  TRACE_EVENT_SLEEP   Next event in seconds
512 *  TRACE_EVENT_PACKET  Packet arrived in buffer with size size
513 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
514 */
515libtrace_eventobj_t trace_event(libtrace_t *trace,
516                libtrace_packet_t *packet);
517
518
519/** Write one packet out to the output trace
520 *
521 * @param trace         the libtrace_out opaque pointer
522 * @param packet        the packet opaque pointer
523 * @return the number of bytes written out, if zero or negative then an error has occured.
524 */
525int trace_write_packet(libtrace_out_t *trace, const libtrace_packet_t *packet);
526/*@}*/
527
528/** @name Protocol decodes
529 * These functions locate and return a pointer to various headers inside a
530 * packet
531 * @{
532 */
533
534/** get a pointer to the link layer
535 * @param packet        the packet opaque pointer
536 *
537 * @return a pointer to the link layer, or NULL if there is no link layer
538 *
539 * @note you should call getLinkType() to find out what type of link layer
540 * this is
541 */
542SIMPLE_FUNCTION
543void *trace_get_link(const libtrace_packet_t *packet);
544
545/** get a pointer to the IP header (if any)
546 * @param packet        the packet opaque pointer
547 *
548 * @return a pointer to the IP header, or NULL if there is not an IP packet
549 */
550SIMPLE_FUNCTION
551libtrace_ip_t *trace_get_ip(const libtrace_packet_t *packet);
552
553/** Gets a pointer to the transport layer header (if any)
554 * @param packet        a pointer to a libtrace_packet structure
555 *
556 * @return a pointer to the transport layer header, or NULL if there is no header
557 */
558void *trace_get_transport(const libtrace_packet_t *packet);
559
560/** Gets a pointer to the transport layer header (if any) given a pointer to the
561 * IP header
562 * @param ip            The IP Header
563 * @param[out] skipped  An output variable of the number of bytes skipped
564 *
565 * @return a pointer to the transport layer header, or NULL if there is no header
566 *
567 * Skipped can be NULL, in which case it will be ignored
568 */
569void *trace_get_transport_from_ip(const libtrace_ip_t *ip, int *skipped);
570
571/** get a pointer to the TCP header (if any)
572 * @param packet        the packet opaque pointer
573 *
574 * @return a pointer to the TCP header, or NULL if there is not a TCP packet
575 */
576SIMPLE_FUNCTION
577libtrace_tcp_t *trace_get_tcp(const libtrace_packet_t *packet);
578
579/** get a pointer to the TCP header (if any) given a pointer to the IP header
580 * @param ip            The IP header
581 * @param[out] skipped  An output variable of the number of bytes skipped
582 *
583 * @return a pointer to the TCP header, or NULL if this is not a TCP packet
584 *
585 * Skipped can be NULL, in which case it will be ignored by the program.
586 *
587 * @author Perry Lorier
588 */
589SIMPLE_FUNCTION
590libtrace_tcp_t *trace_get_tcp_from_ip(const libtrace_ip_t *ip,int *skipped);
591
592/** get a pointer to the UDP header (if any)
593 * @param packet        the packet opaque pointer
594 *
595 * @return a pointer to the UDP header, or NULL if this is not a UDP packet
596 */
597SIMPLE_FUNCTION
598libtrace_udp_t *trace_get_udp(const libtrace_packet_t *packet);
599
600/** get a pointer to the UDP header (if any) given a pointer to the IP header
601 * @param       ip      The IP header
602 * @param[out]  skipped An output variable of the number of bytes skipped
603 *
604 * @return a pointer to the UDP header, or NULL if this is not an UDP packet
605 *
606 * Skipped may be NULL, in which case it will be ignored by this function.
607 */
608SIMPLE_FUNCTION
609libtrace_udp_t *trace_get_udp_from_ip(const libtrace_ip_t *ip,int *skipped);
610
611/** get a pointer to the ICMP header (if any)
612 * @param packet        the packet opaque pointer
613 *
614 * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet
615 */
616SIMPLE_FUNCTION
617libtrace_icmp_t *trace_get_icmp(const libtrace_packet_t *packet);
618
619/** get a pointer to the ICMP header (if any) given a pointer to the IP header
620 * @param ip            The IP header
621 * @param[out] skipped  An output variable of the number of bytes skipped
622 *
623 * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet
624 *
625 * Skipped may be NULL, in which case it will be ignored by this function
626 */
627SIMPLE_FUNCTION
628libtrace_icmp_t *trace_get_icmp_from_ip(const libtrace_ip_t *ip,int *skipped);
629/*@}*/
630
631/** parse an ip or tcp option
632 * @param[in,out] ptr   the pointer to the current option
633 * @param[in,out] len   the length of the remaining buffer
634 * @param[out] type     the type of the option
635 * @param[out] optlen   the length of the option
636 * @param[out] data     the data of the option
637 *
638 * @return bool true if there is another option (and the fields are filled in)
639 *               or false if this was the last option.
640 *
641 * This updates ptr to point to the next option after this one, and updates
642 * len to be the number of bytes remaining in the options area.  Type is updated
643 * to be the code of this option, and data points to the data of this option,
644 * with optlen saying how many bytes there are.
645 *
646 * @note Beware of fragmented packets.
647 */
648int trace_get_next_option(unsigned char **ptr,int *len,
649                        unsigned char *type,
650                        unsigned char *optlen,
651                        unsigned char **data);
652
653
654/** @name Time
655 * These functions deal with time that a packet arrived and return it
656 * in various formats
657 * @{
658 */
659/** Get the current time in DAG time format
660 * @param packet        the packet opaque pointer
661 *
662 * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
663 * past 1970-01-01, the lower 32bits are partial seconds)
664 * @author Daniel Lawson
665 */
666SIMPLE_FUNCTION
667uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet);
668
669/** Get the current time in struct timeval
670 * @param packet        the packet opaque pointer
671 *
672 * @return time that this packet was seen in a struct timeval
673 * @author Daniel Lawson
674 * @author Perry Lorier
675 */ 
676SIMPLE_FUNCTION
677struct timeval trace_get_timeval(const libtrace_packet_t *packet);
678
679/** Get the current time in floating point seconds
680 * @param packet        the packet opaque pointer
681 *
682 * @return time that this packet was seen in 64bit floating point seconds
683 * @author Daniel Lawson
684 * @author Perry Lorier
685 */
686SIMPLE_FUNCTION
687double trace_get_seconds(const libtrace_packet_t *packet);
688
689/** Seek within a trace
690 * @param trace         trace to seek
691 * @param seconds       time to seek to
692 * @return 0 on success.
693 * Make the next packet read to be the first packet to occur at or after the
694 * time searched for.  This must be called in the configuration state (ie,
695 * before trace_start() or after trace_pause().
696 * @note This function may be extremely slow.
697 */
698int trace_seek_seconds(libtrace_t *trace, double seconds);
699
700/** Seek within a trace
701 * @param trace         trace to seek
702 * @param tv            time to seek to
703 * @return 0 on success.
704 * Make the next packet read to be the first packet to occur at or after the
705 * time searched for.  This must be called in the configuration state (ie,
706 * before trace_start() or after trace_pause().
707 * @note This function may be extremely slow.
708 */
709int trace_seek_timeval(libtrace_t *trace, struct timeval tv);
710
711/*@}*/
712
713/** @name Sizes
714 * This section deals with finding or setting the various different lengths
715 * a packet can have
716 * @{
717 */
718/** Get the size of the packet in the trace
719 * @param packet        the packet opaque pointer
720 * @return the size of the packet in the trace
721 * @author Perry Lorier
722 * @note Due to this being a header capture, or anonymisation, this may not
723 * be the same size as the original packet.  See get_wire_length() for the
724 * original size of the packet.
725 * @note This can (and often is) different for different packets in a trace!
726 * @note This is sometimes called the "snaplen".
727 * @note The return size refers to the network-level payload of the packet and
728 * does not include any capture headers. For example, an Ethernet packet with
729 * an empty TCP packet will return sizeof(ethernet_header) + sizeof(ip_header)
730 * + sizeof(tcp_header).
731 */
732SIMPLE_FUNCTION
733int trace_get_capture_length(const libtrace_packet_t *packet);
734
735/** Get the size of the packet as it was seen on the wire.
736 * @param packet        the packet opaque pointer
737 * @return the size of the packet as it was on the wire.
738 * @note Due to the trace being a header capture, or anonymisation this may
739 * not be the same as the Capture Len.
740 */ 
741SIMPLE_FUNCTION
742int trace_get_wire_length(const libtrace_packet_t *packet);
743
744/** Get the length of the capture framing headers.
745 * @param packet        the packet opaque pointer
746 * @return the size of the packet as it was on the wire.
747 * @author Perry Lorier
748 * @author Daniel Lawson
749 * @note this length corresponds to the difference between the size of a
750 * captured packet in memory, and the captured length of the packet
751 */ 
752SIMPLE_FUNCTION
753int trace_get_framing_length(const libtrace_packet_t *packet);
754
755/** Truncate ("snap") the packet at the suggested length
756 * @param packet        the packet opaque pointer
757 * @param size          the new length of the packet
758 * @return the new length of the packet, or the original length of the
759 * packet if unchanged
760 * @author Daniel Lawson
761 */
762size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size);
763
764/*@}*/
765
766
767/** Link layer types
768 * This enumates the various different link types that libtrace understands
769 */
770typedef enum { 
771       TRACE_TYPE_LEGACY,       
772       TRACE_TYPE_HDLC_POS, 
773       TRACE_TYPE_ETH,                  /**< 802.3 style Ethernet */
774       TRACE_TYPE_ATM,
775       TRACE_TYPE_80211,                /**< 802.11 frames */
776       TRACE_TYPE_NONE,
777       TRACE_TYPE_LINUX_SLL,            /**< Linux "null" framing */
778       TRACE_TYPE_PFLOG,                /**< FreeBSD's PFlug */
779       TRACE_TYPE_LEGACY_DEFAULT,
780       TRACE_TYPE_LEGACY_POS,
781       TRACE_TYPE_LEGACY_ATM,
782       TRACE_TYPE_LEGACY_ETH,
783       TRACE_TYPE_80211_PRISM
784     } libtrace_linktype_t;
785
786/** Get the type of the link layer
787 * @param packet        the packet opaque pointer
788 * @return libtrace_linktype_t
789 * @author Perry Lorier
790 * @author Daniel Lawson
791 */
792SIMPLE_FUNCTION
793inline libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet);
794
795/** Get the destination MAC addres
796 * @param packet        the packet opaque pointer
797 * @return a pointer to the destination mac, (or NULL if there is no
798 * destination MAC)
799 * @author Perry Lorier
800 */
801SIMPLE_FUNCTION
802uint8_t *trace_get_destination_mac(const libtrace_packet_t *packet);
803
804/** Get the source MAC addres
805 * @param packet        the packet opaque pointer
806 * @return a pointer to the source mac, (or NULL if there is no source MAC)
807 * @author Perry Lorier
808 */
809SIMPLE_FUNCTION
810uint8_t *trace_get_source_mac(const libtrace_packet_t *packet);
811
812/** Set the direction flag, if it has one
813 * @param packet        the packet opaque pointer
814 * @param direction     the new direction (0,1,2,3)
815 * @return a signed value containing the direction flag, or -1 if this is not supported
816 * @author Daniel Lawson
817 */
818int8_t trace_set_direction(libtrace_packet_t *packet, int8_t direction);
819
820/** Get the direction flag, if it has one
821 * @param packet        the packet opaque pointer
822 * @return a signed value containing the direction flag, or -1 if this is not supported
823 * The direction is defined as 0 for packets originating locally (ie, outbound)
824 * and 1 for packets originating remotely (ie, inbound).
825 * Other values are possible, which might be overloaded to mean special things
826 * for a special trace.
827 * @author Daniel Lawson
828 */
829SIMPLE_FUNCTION
830int8_t trace_get_direction(const libtrace_packet_t *packet);
831
832/** @name BPF
833 * This section deals with using Berkley Packet Filters
834 * @{
835 */
836/** setup a BPF filter
837 * @param filterstring a char * containing the bpf filter string
838 * @return opaque pointer pointer to a libtrace_filter_t object
839 * @author Daniel Lawson
840 * @note The filter is not actually compiled at this point, so no correctness
841 * tests are performed here. trace_bpf_setfilter will always return ok, but
842 * if the filter is poorly constructed an error will be generated when the
843 * filter is actually used
844 */
845SIMPLE_FUNCTION
846libtrace_filter_t *trace_bpf_setfilter(const char *filterstring);
847
848/** apply a BPF filter
849 * @param filter        the filter opaque pointer
850 * @param packet        the packet opaque pointer
851 * @return 1 if the filter matches, 0 if it doesn't.
852 * @note Due to the way BPF filters are built, the filter is not actually
853 * compiled until the first time trace_bpf_filter is called. If your filter is
854 * incorrect, it will generate an error message and assert, exiting the
855 * program. This behaviour may change to more graceful handling of this error
856 * in the future.
857 */
858int trace_bpf_filter(libtrace_filter_t *filter,
859                const libtrace_packet_t *packet);
860/*@}*/
861
862/** Which port is the server port */
863typedef enum {
864        USE_DEST,       /**< Destination port is the server port */
865        USE_SOURCE      /**< Source port is the server port */
866} serverport_t;
867
868/** Get the source port
869 * @param packet        the packet to read from
870 * @return a port in \em HOST byte order, or equivilent to ports for this
871 * protocol, or 0 if this protocol has no ports.
872 * @author Perry Lorier
873 */
874SIMPLE_FUNCTION
875uint16_t trace_get_source_port(const libtrace_packet_t *packet);
876
877/** Get the destination port
878 * @param packet        the packet to read from
879 * @return a port in \em HOST byte order, or equivilent to ports for this
880 * protocol, or 0 if this protocol has no ports.
881 * @author Perry Lorier
882 */
883SIMPLE_FUNCTION
884uint16_t trace_get_destination_port(const libtrace_packet_t *packet);
885
886/** hint at the server port in specified protocol
887 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
888 * @param source        the source port from the packet
889 * @param dest          the destination port from the packet
890 * @return one of USE_SOURCE or USE_DEST depending on which one you should use
891 * @note ports must be in \em HOST byte order!
892 * @author Daniel Lawson
893 */
894SIMPLE_FUNCTION
895int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
896
897/** Takes a uri and splits it into a format and uridata component.
898 * Primarily for internal use but made available for external use.
899 * @param uri           the uri to be parsed
900 * @param format        destination location for the format component of the uri
901 * @return 0 if an error occured, otherwise return the uridata component
902 * @author Shane Alcock
903 */
904const char *trace_parse_uri(const char *uri, char **format);
905
906/* Base format type definitions */
907enum base_format_t {
908        TRACE_FORMAT_ERF          =1,
909        TRACE_FORMAT_PCAP         =2,
910        TRACE_FORMAT_WAG          =3,
911        TRACE_FORMAT_RT           =4,
912        TRACE_FORMAT_LEGACY_ATM   =5,
913        TRACE_FORMAT_LEGACY_POS   =6,
914        TRACE_FORMAT_LEGACY_ETH   =7
915};
916
917/** Gets the framing header type for a given packet.
918 * @param packet        the packet opaque pointer
919 * @return the format of the packet
920 */
921enum base_format_t trace_get_format(struct libtrace_packet_t *packet);
922
923
924/** libtrace error information */
925extern struct trace_err_t{
926        int err_num;            /**< error code */
927        char problem[255];      /**< the format, uri etc that caused the error for reporting purposes */
928} trace_err;
929#ifdef __cplusplus
930} /* extern "C" */
931#endif /* #ifdef __cplusplus */
932#endif /* LIBTRACE_H_ */
Note: See TracBrowser for help on using the repository browser.