source: lib/libtrace.h @ dd22d84

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

config option support added + zlib compression level

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