source: lib/libtrace.h @ a114d8b5

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

trace_perror created

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