source: lib/libtrace.h @ ffc8c8d

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

better return codes in format_*.c, trace_perror stuff

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