source: lib/libtrace.h @ 084d95a

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

Fixed another reference to sizeof(dag_record_t) and added support for dummy traces

trace_create_dead and trace_destroy_dead

  • Property mode set to 100644
File size: 19.5 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#include <sys/types.h>
35#include <netinet/in.h>
36
37/** API version as 3 byte hex digits, eg 0xXXYYZZ */
38#define LIBTRACE_API_VERSION 0x100014
39
40#ifdef __cplusplus
41extern "C" { 
42#endif
43/** @file
44 *
45 * @brief Trace file processing library header
46 *
47 * @author Daniel Lawson
48 * @author Perry Lorier
49 *
50 * @version $Id$
51 *
52 * This library provides a per packet interface into a trace file, or a live
53 * captures.  It supports ERF, DAG cards, WAG cards, WAG's event format,
54 * pcap etc.
55 *
56 * @par Usage
57 * <ol>
58 * <li> include "libtrace.h"
59 * <li> call create_trace with the uri of the trace you're interested in.<br>
60 * This is usually passed in as argv[1] to your program.
61 * <li> call libtrace_read_packet(), passing in the libtrace_t returned from
62 * create trace and a buffer (and the buffer length)
63 * <li> call getIP() on the buffer, and do whatever you need
64 * <li> loop back to step 3, until libtrace_read_packet() returns -1
65 * </ol>
66 * @par Linking
67 * To use this library you need to link against libtrace by passing -ltrace
68 * to your linker. You may also need to link against a version of libpcap
69 * and of zlib which are compiled for largefile support (if you wish to access
70 * traces larger than 2 GB). This is left as an exercise for the reader. Debian
71 * Woody, at least, does not support large file offsets.
72 *
73 */
74
75#define COLLECTOR_PORT 3435
76
77       
78/** Opaque structure holding information about an output trace */
79struct libtrace_out_t;
80       
81/** Opaque structure holding information about a trace */
82struct libtrace_t;
83       
84/** Opaque structure holding information about a bpf filter */
85struct libtrace_filter_t;
86
87/** Opaque structure holding information about a packet */
88#define LIBTRACE_PACKET_BUFSIZE 65536
89struct libtrace_packet_t {
90        struct libtrace_t *trace;
91        //void *buffer;
92        char buffer[LIBTRACE_PACKET_BUFSIZE];
93        size_t size;
94        uint8_t status;
95};
96
97/** Enumeration of error codes */
98enum {E_NOERROR, E_BAD_FORMAT, E_NO_INIT, E_NO_INIT_OUT, E_URI_LONG, E_URI_NOCOLON, E_INIT_FAILED };
99
100/** Structure for dealing with IP packets */
101struct libtrace_ip
102  {
103#if BYTE_ORDER == LITTLE_ENDIAN
104    unsigned int ip_hl:4;               /**< header length */
105    unsigned int ip_v:4;                /**< version */
106#elif BYTE_ORDER == BIG_ENDIAN
107    unsigned int ip_v:4;                /**< version */
108    unsigned int ip_hl:4;               /**< header length */
109#else
110#   error "Adjust your <bits/endian.h> defines"
111#endif
112    u_int8_t ip_tos;                    /**< type of service */
113    u_short ip_len;                     /**< total length */
114    u_short ip_id;                      /**< identification */
115    u_short ip_off;                     /**< fragment offset field */
116#define IP_RF 0x8000                    /**< reserved fragment flag */
117#define IP_DF 0x4000                    /**< dont fragment flag */
118#define IP_MF 0x2000                    /**< more fragments flag */
119#define IP_OFFMASK 0x1fff               /**< mask for fragmenting bits */
120    u_int8_t ip_ttl;                    /**< time to live */
121    u_int8_t ip_p;                      /**< protocol */
122    u_short ip_sum;                     /**< checksum */
123    struct in_addr ip_src;              /**< source address */
124    struct in_addr ip_dst;              /**< dest address */
125  };
126
127/** Structure for dealing with TCP packets */
128struct libtrace_tcp
129  {
130    u_int16_t source;           /**< Source Port */
131    u_int16_t dest;             /**< Destination port */
132    u_int32_t seq;              /**< Sequence number */
133    u_int32_t ack_seq;          /**< Acknowledgement Number */
134#  if BYTE_ORDER == LITTLE_ENDIAN
135    u_int16_t res1:4;           /**< Reserved bits */
136    u_int16_t doff:4;           
137    u_int16_t fin:1;            /**< FIN */
138    u_int16_t syn:1;            /**< SYN flag */
139    u_int16_t rst:1;            /**< RST flag */
140    u_int16_t psh:1;            /**< PuSH flag */
141    u_int16_t ack:1;            /**< ACK flag */
142    u_int16_t urg:1;            /**< URG flag */
143    u_int16_t res2:2;           /**< Reserved */
144#  elif BYTE_ORDER == BIG_ENDIAN
145    u_int16_t doff:4;           
146    u_int16_t res1:4;           /**< Reserved bits */
147    u_int16_t res2:2;           /**< Reserved */
148    u_int16_t urg:1;            /**< URG flag */
149    u_int16_t ack:1;            /**< ACK flag */
150    u_int16_t psh:1;            /**< PuSH flag */
151    u_int16_t rst:1;            /**< RST flag */
152    u_int16_t syn:1;            /**< SYN flag */
153    u_int16_t fin:1;            /**< FIN flag */
154#  else
155#   error "Adjust your <bits/endian.h> defines"
156#  endif
157    u_int16_t window;           /**< Window Size */
158    u_int16_t check;            /**< Checksum */
159    u_int16_t urg_ptr;          /**< Urgent Pointer */
160};
161
162/** UDP Header for dealing with UDP packets */
163struct libtrace_udp {
164  u_int16_t     source;         /**< Source port */
165  u_int16_t     dest;           /**< Destination port */
166  u_int16_t     len;            /**< Length */
167  u_int16_t     check;          /**< Checksum */
168};
169
170/** ICMP Header for dealing with icmp packets */
171struct libtrace_icmp
172{
173  u_int8_t type;                /**< message type */
174  u_int8_t code;                /**< type sub-code */
175  u_int16_t checksum;           /**< checksum */
176  union
177  {
178    struct
179    {
180      u_int16_t id;
181      u_int16_t sequence;
182    } echo;                     /**< echo datagram */
183    u_int32_t   gateway;        /**< gateway address */
184    struct
185    {
186      u_int16_t unused;
187      u_int16_t mtu;
188    } frag;                     /**< path mtu discovery */
189  } un;
190};
191
192
193/** Prints help information for libtrace
194 *
195 * Function prints out some basic help information regarding libtrace,
196 * and then prints out the help() function registered with each input module
197 */
198void trace_help();
199
200/** Gets the output format for a given output trace
201 *
202 */
203char *trace_get_output_format(struct libtrace_out_t *libtrace);
204
205/** Prints error information
206 *
207 * Prints out a descriptive error message for the currently set trace_err value
208 */
209void trace_perror(char *caller);
210
211/** Create a trace file from a URI
212 *
213 * @returns opaque pointer to a libtrace_t
214 *
215 * Valid URI's are:
216 *  - erf:/path/to/erf/file
217 *  - erf:/path/to/erf/file.gz
218 *  - erf:/path/to/rtclient/socket
219 *  - erf:-  (stdin)
220 *  - dag:/dev/dagcard                  (implementd?)
221 *  - pcapint:pcapinterface                (eg: pcap:eth0)
222 *  - pcap:/path/to/pcap/file
223 *  - pcap:/path/to/pcap/file.gz
224 *  - pcap:/path/to/pcap/socket         (implemented?)
225 *  - pcap:-
226 *  - rtclient:hostname
227 *  - rtclient:hostname:port
228 *  - wag:/path/to/wag/file
229 *  - wag:/path/to/wag/file.gz
230 *  - wag:/path/to/wag/socket
231 *  - wag:/dev/device
232 *
233 *  If an error occured when attempting to open the trace file, NULL is returned
234 *  and an error is output to stdout.
235 */
236struct libtrace_t *trace_create(char *uri);
237
238/** Creates a "dummy" trace file that has only the format type set.
239 *
240 * @returns opaque pointer to a (sparsely initialised) libtrace_t
241 *
242 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions with
243 * the dummy trace. Its intended purpose is to act as a packet->trace for libtrace_packet_t's
244 * that are not associated with a libtrace_t structure.
245 */
246struct libtrace_t *trace_create_dead(char *uri);
247
248/** Creates a trace output file from a URI.
249 *
250 * @returns opaque pointer to a libtrace_output_t
251 *
252 * Valid URI's are:
253 *  - gzerf:/path/to/erf/file.gz
254 *  - gzerf:/path/to/erf/file
255 *  - rtserver:hostname
256 *  - rtserver:hostname:port
257 *
258 *  If an error occured when attempting to open the output trace, NULL is returned and
259 *  an error is output to stdout.
260 */
261struct libtrace_out_t *trace_output_create(char *uri);
262
263/** Configures a trace output file as specified by an option string in getopt() format
264 *
265 * @param libtrace      the output trace file to be configured
266 * @param options       the string containing the configuration options
267 * @returns -1 if configuration fails, 0 if successful
268 */
269int trace_output_config(struct libtrace_out_t *libtrace, char *options);
270
271/** Close a trace file, freeing up any resources it may have been using
272 *
273 */
274void trace_destroy(struct libtrace_t *trace);
275
276void trace_destroy_dead(struct libtrace_t *trace);
277
278/** Close a trace output file, freeing up any resources it may have been using
279 *
280 */
281void trace_output_destroy(struct libtrace_out_t *trace);
282
283/** Read one packet from the trace into buffer
284 *
285 * @param trace         the libtrace opaque pointer
286 * @param packet        the packet opaque pointer
287 * @returns false if it failed to read a packet
288 *
289 */
290int trace_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet);
291
292/** Write one packet out to the output trace
293 *
294 * @param trace         the libtrace_out opaque pointer
295 * @param packet        the packet opaque pointer
296 * @returns the number of bytes written out, if zero or negative then an error has occured.
297 */
298int trace_write_packet(struct libtrace_out_t *trace, struct libtrace_packet_t *packet);
299
300/** get a pointer to the link layer
301 * @param packet        the packet opaque pointer
302 *
303 * @returns a pointer to the link layer, or NULL if there is no link layer
304 *
305 * @note you should call getLinkType() to find out what type of link layer
306 * this is
307 */
308void *trace_get_link(const struct libtrace_packet_t *packet);
309
310/** get a pointer to the IP header (if any)
311 * @param packet        the packet opaque pointer
312 *
313 * @returns a pointer to the IP header, or NULL if there is not an IP packet
314 */
315struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet);
316
317/** get a pointer to the TCP header (if any)
318 * @param packet        the packet opaque pointer
319 *
320 * @returns a pointer to the TCP header, or NULL if there is not a TCP packet
321 */
322struct libtrace_tcp *trace_get_tcp(const struct libtrace_packet_t *packet);
323
324/** get a pointer to the TCP header (if any) given a pointer to the IP header
325 * @param ip            The IP header
326 * @param[out] skipped  An output variable of the number of bytes skipped
327 *
328 * @returns a pointer to the TCP header, or NULL if this is not a TCP packet
329 *
330 * Skipped can be NULL, in which case it will be ignored by the program.
331 *
332 * @author Perry Lorier
333 */
334struct libtrace_tcp *trace_get_tcp_from_ip(struct libtrace_ip *ip,int *skipped);
335
336/** get a pointer to the UDP header (if any)
337 * @param packet        the packet opaque pointer
338 *
339 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet
340 */
341struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet);
342
343/** get a pointer to the UDP header (if any) given a pointer to the IP header
344 * @param       ip      The IP header
345 * @param[out]  skipped An output variable of the number of bytes skipped
346 *
347 * @returns a pointer to the UDP header, or NULL if this is not an UDP packet
348 *
349 * Skipped may be NULL, in which case it will be ignored by this function.
350 */
351struct libtrace_udp *trace_get_udp_from_ip(struct libtrace_ip *ip,int *skipped);
352
353/** get a pointer to the ICMP header (if any)
354 * @param packet        the packet opaque pointer
355 *
356 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet
357 */
358struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet);
359
360/** get a pointer to the ICMP header (if any) given a pointer to the IP header
361 * @param ip            The IP header
362 * @param[out] skipped  An output variable of the number of bytes skipped
363 *
364 * @returns a pointer to the ICMP header, or NULL if this is not an ICMP packet
365 *
366 * Skipped may be NULL, in which case it will be ignored by this function
367 */
368struct libtrace_icmp *trace_get_icmp_from_ip(struct libtrace_ip *ip,int *skipped);
369
370/** parse an ip or tcp option
371 * @param[in,out] ptr   the pointer to the current option
372 * @param[in,out] len   the length of the remaining buffer
373 * @param[out] type     the type of the option
374 * @param[out] optlen   the length of the option
375 * @param[out] data     the data of the option
376 *
377 * @returns bool true if there is another option (and the fields are filled in)
378 *               or false if this was the last option.
379 *
380 * This updates ptr to point to the next option after this one, and updates
381 * len to be the number of bytes remaining in the options area.  Type is updated
382 * to be the code of this option, and data points to the data of this option,
383 * with optlen saying how many bytes there are.
384 *
385 * @note Beware of fragmented packets.
386 */
387int trace_get_next_option(unsigned char **ptr,int *len,
388                        unsigned char *type,
389                        unsigned char *optlen,
390                        unsigned char **data);
391
392
393/** Get the current time in DAG time format
394 * @param packet        the packet opaque pointer
395 *
396 * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
397 * past 1970-01-01, the lower 32bits are partial seconds)
398 * @author Daniel Lawson
399 */
400uint64_t trace_get_erf_timestamp(const struct libtrace_packet_t *packet);
401
402/** Get the current time in struct timeval
403 * @param packet        the packet opaque pointer
404 *
405 * @returns time that this packet was seen in a struct timeval
406 * @author Daniel Lawson
407 * @author Perry Lorier
408 */ 
409struct timeval trace_get_timeval(const struct libtrace_packet_t *packet);
410
411/** Get the current time in floating point seconds
412 * @param packet        the packet opaque pointer
413 *
414 * @returns time that this packet was seen in 64bit floating point seconds
415 * @author Perry Lorier
416 */
417double trace_get_seconds(const struct libtrace_packet_t *packet);
418
419/** Get the size of the packet in the trace
420 * @param packet        the packet opaque pointer
421 * @returns the size of the packet in the trace
422 * @author Perry Lorier
423 * @note Due to this being a header capture, or anonymisation, this may not
424 * be the same size as the original packet.  See get_wire_length() for the original
425 * size of the packet.
426 * @note This can (and often is) different for different packets in a trace!
427 * @par
428 *  This is sometimes called the "snaplen".
429 */
430
431int trace_get_capture_length(const struct libtrace_packet_t *packet);
432
433/** Get the size of the packet as it was seen on the wire.
434 * @param packet        the packet opaque pointer
435 * @returns the size of the packet as it was on the wire.
436 * @author Perry Lorier
437 * @author Daniel Lawson
438 * @note Due to the trace being a header capture, or anonymisation this may
439 * not be the same as the Capture Len.
440 */ 
441
442int trace_get_wire_length(const struct libtrace_packet_t *packet);
443
444/** Link layer types
445 */
446typedef enum { 
447       TRACE_TYPE_LEGACY, 
448       TRACE_TYPE_HDLC_POS, 
449       TRACE_TYPE_ETH,
450       TRACE_TYPE_ATM,
451       TRACE_TYPE_80211,
452       TRACE_TYPE_NONE,
453       TRACE_TYPE_LINUX_SLL,
454       TRACE_TYPE_PFLOG
455     } libtrace_linktype_t;
456
457/** Get the type of the link layer
458 * @param packet        the packet opaque pointer
459 * @returns libtrace_linktype_t
460 * @author Perry Lorier
461 * @author Daniel Lawson
462 */
463
464inline libtrace_linktype_t trace_get_link_type(const struct libtrace_packet_t *packet);
465
466/** Get the destination MAC addres
467 * @param packet        the packet opaque pointer
468 * @returns a pointer to the destination mac, (or NULL if there is no
469 * destination MAC)
470 * @author Perry Lorier
471 */
472uint8_t *trace_get_destination_mac(const struct libtrace_packet_t *packet);
473
474/** Get the source MAC addres
475 * @param packet        the packet opaque pointer
476 * @returns a pointer to the source mac, (or NULL if there is no source MAC)
477 * @author Perry Lorier
478 */
479uint8_t *trace_get_source_mac(const struct libtrace_packet_t *packet);
480
481/** Truncate the packet at the suggested length
482 * @param packet        the packet opaque pointer
483 * @param len           the new length of the packet
484 * @returns the new length of the packet, or the original length of the
485 * packet if unchanged
486 * @author Daniel Lawson
487 */
488size_t trace_set_capture_length(struct libtrace_packet_t *packet, size_t size);
489
490/** Set the direction flag, if it has one
491 * @param packet        the packet opaque pointer
492 * @param direction     the new direction (0,1,2,3)
493 * @returns a signed value containing the direction flag, or -1 if this is not supported
494 * @author Daniel Lawson
495 */
496int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction);
497
498/** Get the direction flag, if it has one
499 * @param packet        the packet opaque pointer
500 * @returns a signed value containing the direction flag, or -1 if this is not supported
501 * The direction is defined as 0 for packets originating locally (ie, outbound)
502 * and 1 for packets originating remotely (ie, inbound).
503 * Other values are possible, which might be overloaded to mean special things
504 * for a special trace.
505 * @author Daniel Lawson
506 */
507int8_t trace_get_direction(const struct libtrace_packet_t *packet);
508
509/** Event types */
510typedef enum {
511        TRACE_EVENT_IOWAIT,
512        TRACE_EVENT_SLEEP,
513        TRACE_EVENT_PACKET,
514        TRACE_EVENT_TERMINATE
515} libtrace_event_t;
516
517/** structure returned by libtrace_event explaining what the current event is */
518struct libtrace_eventobj_t {
519        libtrace_event_t type; /**< event type (iowait,sleep,packet */
520        int fd;                /**< if IOWAIT, the fd to sleep on */
521        double seconds;        /**< if SLEEP, the amount of time to sleep for */
522        int size;              /**< if PACKET, the value returned from trace_read_packet */
523};
524
525/** process a libtrace event
526 * @param trace the libtrace opaque pointer
527 * @param packet the libtrace_packet opaque pointer
528 * @param fd a pointer to a file descriptor to listen on
529 * @param seconds a pointer the time in seconds since to the next event
530 * @returns libtrace_event struct containing the type, and potential
531 *      fd or seconds to sleep on
532 *
533 * Type can be:
534 *  TRACE_EVENT_IOWAIT  Waiting on I/O on <fd>
535 *  TRACE_EVENT_SLEEP   Next event in <seconds>
536 *  TRACE_EVENT_PACKET  Packet arrived in <buffer> with size <size>
537 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
538 */
539struct libtrace_eventobj_t trace_event(struct libtrace_t *trace,
540                struct libtrace_packet_t *packet);
541
542/** setup a BPF filter
543 * @param filterstring a char * containing the bpf filter string
544 * @returns opaque pointer pointer to a libtrace_filter_t object
545 * @author Daniel Lawson
546 */
547struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring);
548
549/** apply a BPF filter
550 * @param filter        the filter opaque pointer
551 * @param packet        the packet opaque pointer
552 * @returns 0 if the filter fails, 1 if it succeeds
553 * @author Daniel Lawson
554 */
555int trace_bpf_filter(struct libtrace_filter_t *filter,
556                const struct libtrace_packet_t *packet);
557
558
559typedef enum {USE_DEST, USE_SOURCE} serverport_t;
560
561/** Get the source port
562 * @param packet        the packet to read from
563 * @returns a port in \em HOST byte order, or equivilent to ports for this
564 * protocol, or 0 if this protocol has no ports.
565 * @author Perry Lorier
566 */
567uint16_t trace_get_source_port(const struct libtrace_packet_t *packet);
568
569/** Get the destination port
570 * @param packet        the packet to read from
571 * @returns a port in \em HOST byte order, or equivilent to ports for this
572 * protocol, or 0 if this protocol has no ports.
573 * @author Perry Lorier
574 */
575uint16_t trace_get_destination_port(const struct libtrace_packet_t *packet);
576
577/** hint at the server port in specified protocol
578 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
579 * @param source        the source port from the packet
580 * @param dest          the destination port from the packet
581 * @returns one of USE_SOURCE or USE_DEST depending on which one you should use
582 * @note ports must be in \em host byte order!
583 * @author Daniel Lawson
584 */
585int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
586
587#ifdef __cplusplus
588} // extern "C"
589#endif // #ifdef __cplusplus
590#endif // LIBTRACE_H_
Note: See TracBrowser for help on using the repository browser.