source: lib/libtrace.h @ 74c7660

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

renamed trace_truncate_packet to trace_set_capture_length

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