source: lib/libtrace.h @ ca87bcd

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since ca87bcd was ca87bcd, checked in by Daniel Lawson <dlawson@…>, 15 years ago

fixed API_VERSION define
added missing timestamp format conversions

  • Property mode set to 100644
File size: 19.1 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 trace output file from a URI.
239 *
240 * @returns opaque pointer to a libtrace_output_t
241 *
242 * Valid URI's are:
243 *  - gzerf:/path/to/erf/file.gz
244 *  - gzerf:/path/to/erf/file
245 *  - rtserver:hostname
246 *  - rtserver:hostname:port
247 *
248 *  If an error occured when attempting to open the output trace, NULL is returned and
249 *  an error is output to stdout.
250 */
251struct libtrace_out_t *trace_output_create(char *uri);
252
253/** Configures a trace output file as specified by an option string in getopt() format
254 *
255 * @param libtrace      the output trace file to be configured
256 * @param options       the string containing the configuration options
257 * @returns -1 if configuration fails, 0 if successful
258 */
259int trace_output_config(struct libtrace_out_t *libtrace, char *options);
260
261/** Close a trace file, freeing up any resources it may have been using
262 *
263 */
264void trace_destroy(struct libtrace_t *trace);
265
266/** Close a trace output file, freeing up any resources it may have been using
267 *
268 */
269void trace_output_destroy(struct libtrace_out_t *trace);
270
271/** Read one packet from the trace into buffer
272 *
273 * @param trace         the libtrace opaque pointer
274 * @param packet        the packet opaque pointer
275 * @returns false if it failed to read a packet
276 *
277 */
278int trace_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet);
279
280/** Write one packet out to the output trace
281 *
282 * @param trace         the libtrace_out opaque pointer
283 * @param packet        the packet opaque pointer
284 * @returns the number of bytes written out, if zero or negative then an error has occured.
285 */
286int trace_write_packet(struct libtrace_out_t *trace, struct libtrace_packet_t *packet);
287
288/** get a pointer to the link layer
289 * @param packet        the packet opaque pointer
290 *
291 * @returns a pointer to the link layer, or NULL if there is no link layer
292 *
293 * @note you should call getLinkType() to find out what type of link layer
294 * this is
295 */
296void *trace_get_link(const struct libtrace_packet_t *packet);
297
298/** get a pointer to the IP header (if any)
299 * @param packet        the packet opaque pointer
300 *
301 * @returns a pointer to the IP header, or NULL if there is not an IP packet
302 */
303struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet);
304
305/** get a pointer to the TCP header (if any)
306 * @param packet        the packet opaque pointer
307 *
308 * @returns a pointer to the TCP header, or NULL if there is not a TCP packet
309 */
310struct libtrace_tcp *trace_get_tcp(const struct libtrace_packet_t *packet);
311
312/** get a pointer to the TCP header (if any) given a pointer to the IP header
313 * @param ip            The IP header
314 * @param[out] skipped  An output variable of the number of bytes skipped
315 *
316 * @returns a pointer to the TCP header, or NULL if this is not a TCP packet
317 *
318 * Skipped can be NULL, in which case it will be ignored by the program.
319 *
320 * @author Perry Lorier
321 */
322struct libtrace_tcp *trace_get_tcp_from_ip(struct libtrace_ip *ip,int *skipped);
323
324/** get a pointer to the UDP header (if any)
325 * @param packet        the packet opaque pointer
326 *
327 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet
328 */
329struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet);
330
331/** get a pointer to the UDP header (if any) given a pointer to the IP header
332 * @param       ip      The IP header
333 * @param[out]  skipped An output variable of the number of bytes skipped
334 *
335 * @returns a pointer to the UDP header, or NULL if this is not an UDP packet
336 *
337 * Skipped may be NULL, in which case it will be ignored by this function.
338 */
339struct libtrace_udp *trace_get_udp_from_ip(struct libtrace_ip *ip,int *skipped);
340
341/** get a pointer to the ICMP header (if any)
342 * @param packet        the packet opaque pointer
343 *
344 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet
345 */
346struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet);
347
348/** get a pointer to the ICMP header (if any) given a pointer to the IP header
349 * @param ip            The IP header
350 * @param[out] skipped  An output variable of the number of bytes skipped
351 *
352 * @returns a pointer to the ICMP header, or NULL if this is not an ICMP packet
353 *
354 * Skipped may be NULL, in which case it will be ignored by this function
355 */
356struct libtrace_icmp *trace_get_icmp_from_ip(struct libtrace_ip *ip,int *skipped);
357
358/** parse an ip or tcp option
359 * @param[in,out] ptr   the pointer to the current option
360 * @param[in,out] len   the length of the remaining buffer
361 * @param[out] type     the type of the option
362 * @param[out] optlen   the length of the option
363 * @param[out] data     the data of the option
364 *
365 * @returns bool true if there is another option (and the fields are filled in)
366 *               or false if this was the last option.
367 *
368 * This updates ptr to point to the next option after this one, and updates
369 * len to be the number of bytes remaining in the options area.  Type is updated
370 * to be the code of this option, and data points to the data of this option,
371 * with optlen saying how many bytes there are.
372 *
373 * @note Beware of fragmented packets.
374 */
375int trace_get_next_option(unsigned char **ptr,int *len,
376                        unsigned char *type,
377                        unsigned char *optlen,
378                        unsigned char **data);
379
380
381/** Get the current time in DAG time format
382 * @param packet        the packet opaque pointer
383 *
384 * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
385 * past 1970-01-01, the lower 32bits are partial seconds)
386 * @author Daniel Lawson
387 */
388uint64_t trace_get_erf_timestamp(const struct libtrace_packet_t *packet);
389
390/** Get the current time in struct timeval
391 * @param packet        the packet opaque pointer
392 *
393 * @returns time that this packet was seen in a struct timeval
394 * @author Daniel Lawson
395 * @author Perry Lorier
396 */ 
397struct timeval trace_get_timeval(const struct libtrace_packet_t *packet);
398
399/** Get the current time in floating point seconds
400 * @param packet        the packet opaque pointer
401 *
402 * @returns time that this packet was seen in 64bit floating point seconds
403 * @author Perry Lorier
404 */
405double trace_get_seconds(const struct libtrace_packet_t *packet);
406
407/** Get the size of the packet in the trace
408 * @param packet        the packet opaque pointer
409 * @returns the size of the packet in the trace
410 * @author Perry Lorier
411 * @note Due to this being a header capture, or anonymisation, this may not
412 * be the same size as the original packet.  See get_wire_length() for the original
413 * size of the packet.
414 * @note This can (and often is) different for different packets in a trace!
415 * @par
416 *  This is sometimes called the "snaplen".
417 */
418
419int trace_get_capture_length(const struct libtrace_packet_t *packet);
420
421/** Get the size of the packet as it was seen on the wire.
422 * @param packet        the packet opaque pointer
423 * @returns the size of the packet as it was on the wire.
424 * @author Perry Lorier
425 * @author Daniel Lawson
426 * @note Due to the trace being a header capture, or anonymisation this may
427 * not be the same as the Capture Len.
428 */ 
429
430int trace_get_wire_length(const struct libtrace_packet_t *packet);
431
432/** Link layer types
433 */
434typedef enum { 
435       TRACE_TYPE_LEGACY, 
436       TRACE_TYPE_HDLC_POS, 
437       TRACE_TYPE_ETH,
438       TRACE_TYPE_ATM,
439       TRACE_TYPE_80211,
440       TRACE_TYPE_NONE,
441       TRACE_TYPE_LINUX_SLL,
442       TRACE_TYPE_PFLOG
443     } libtrace_linktype_t;
444
445/** Get the type of the link layer
446 * @param packet        the packet opaque pointer
447 * @returns libtrace_linktype_t
448 * @author Perry Lorier
449 * @author Daniel Lawson
450 */
451
452inline libtrace_linktype_t trace_get_link_type(const struct libtrace_packet_t *packet);
453
454/** Get the destination MAC addres
455 * @param packet        the packet opaque pointer
456 * @returns a pointer to the destination mac, (or NULL if there is no
457 * destination MAC)
458 * @author Perry Lorier
459 */
460uint8_t *trace_get_destination_mac(const struct libtrace_packet_t *packet);
461
462/** Get the source MAC addres
463 * @param packet        the packet opaque pointer
464 * @returns a pointer to the source mac, (or NULL if there is no source MAC)
465 * @author Perry Lorier
466 */
467uint8_t *trace_get_source_mac(const struct libtrace_packet_t *packet);
468
469/** Truncate the packet at the suggested length
470 * @param packet        the packet opaque pointer
471 * @param len           the new length of the packet
472 * @returns the new length of the packet, or the original length of the
473 * packet if unchanged
474 * @author Daniel Lawson
475 */
476size_t trace_set_capture_length(struct libtrace_packet_t *packet, size_t size);
477
478/** Set the direction flag, if it has one
479 * @param packet        the packet opaque pointer
480 * @param direction     the new direction (0,1,2,3)
481 * @returns a signed value containing the direction flag, or -1 if this is not supported
482 * @author Daniel Lawson
483 */
484int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction);
485
486/** Get the direction flag, if it has one
487 * @param packet        the packet opaque pointer
488 * @returns a signed value containing the direction flag, or -1 if this is not supported
489 * The direction is defined as 0 for packets originating locally (ie, outbound)
490 * and 1 for packets originating remotely (ie, inbound).
491 * Other values are possible, which might be overloaded to mean special things
492 * for a special trace.
493 * @author Daniel Lawson
494 */
495int8_t trace_get_direction(const struct libtrace_packet_t *packet);
496
497/** Event types */
498typedef enum {
499        TRACE_EVENT_IOWAIT,
500        TRACE_EVENT_SLEEP,
501        TRACE_EVENT_PACKET,
502        TRACE_EVENT_TERMINATE
503} libtrace_event_t;
504
505/** structure returned by libtrace_event explaining what the current event is */
506struct libtrace_eventobj_t {
507        libtrace_event_t type; /**< event type (iowait,sleep,packet */
508        int fd;                /**< if IOWAIT, the fd to sleep on */
509        double seconds;        /**< if SLEEP, the amount of time to sleep for */
510        int size;              /**< if PACKET, the value returned from trace_read_packet */
511};
512
513/** process a libtrace event
514 * @param trace the libtrace opaque pointer
515 * @param packet the libtrace_packet opaque pointer
516 * @param fd a pointer to a file descriptor to listen on
517 * @param seconds a pointer the time in seconds since to the next event
518 * @returns libtrace_event struct containing the type, and potential
519 *      fd or seconds to sleep on
520 *
521 * Type can be:
522 *  TRACE_EVENT_IOWAIT  Waiting on I/O on <fd>
523 *  TRACE_EVENT_SLEEP   Next event in <seconds>
524 *  TRACE_EVENT_PACKET  Packet arrived in <buffer> with size <size>
525 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
526 */
527struct libtrace_eventobj_t trace_event(struct libtrace_t *trace,
528                struct libtrace_packet_t *packet);
529
530/** setup a BPF filter
531 * @param filterstring a char * containing the bpf filter string
532 * @returns opaque pointer pointer to a libtrace_filter_t object
533 * @author Daniel Lawson
534 */
535struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring);
536
537/** apply a BPF filter
538 * @param filter        the filter opaque pointer
539 * @param packet        the packet opaque pointer
540 * @returns 0 if the filter fails, 1 if it succeeds
541 * @author Daniel Lawson
542 */
543int trace_bpf_filter(struct libtrace_filter_t *filter,
544                const struct libtrace_packet_t *packet);
545
546
547typedef enum {USE_DEST, USE_SOURCE} serverport_t;
548
549/** Get the source port
550 * @param packet        the packet to read from
551 * @returns a port in \em HOST byte order, or equivilent to ports for this
552 * protocol, or 0 if this protocol has no ports.
553 * @author Perry Lorier
554 */
555uint16_t trace_get_source_port(const struct libtrace_packet_t *packet);
556
557/** Get the destination port
558 * @param packet        the packet to read from
559 * @returns a port in \em HOST byte order, or equivilent to ports for this
560 * protocol, or 0 if this protocol has no ports.
561 * @author Perry Lorier
562 */
563uint16_t trace_get_destination_port(const struct libtrace_packet_t *packet);
564
565/** hint at the server port in specified protocol
566 * @param protocol      the IP layer protocol, eg 6 (tcp), 17 (udp)
567 * @param source        the source port from the packet
568 * @param dest          the destination port from the packet
569 * @returns one of USE_SOURCE or USE_DEST depending on which one you should use
570 * @note ports must be in \em host byte order!
571 * @author Daniel Lawson
572 */
573int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest);
574
575#ifdef __cplusplus
576} // extern "C"
577#endif // #ifdef __cplusplus
578#endif // LIBTRACE_H_
Note: See TracBrowser for help on using the repository browser.