source: lib/libtrace.h @ fe43699

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

help functions in erf, wag modules.

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