source: lib/libtrace.h @ d3b2234

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

depacket->sizeification

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