source: lib/libtrace.h @ f99a183

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

Minor fixes - removed init_output and added some doc to libtrace.h

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