source: lib/libtrace.h @ 1f3696b

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

Slight changes to code, basically #includes and #defines, and it should build cleanly on freebsd now.

  • Property mode set to 100644
File size: 11.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#ifdef __cplusplus
38extern "C" { 
39#endif
40/** @file
41 *
42 * @brief Trace file processing library header
43 *
44 * @author Daniel Lawson
45 * @author Perry Lorier
46 *
47 * @version $Id$
48 *
49 * This library provides a per packet interface into a trace file, or a live
50 * captures.  It supports ERF, DAG cards, WAG cards, WAG's event format,
51 * pcap etc.
52 *
53 * @par Usage
54 * <ol>
55 * <li> include "libtrace.h"
56 * <li> call create_trace with the uri of the trace you're interested in.<br>
57 * This is usually passed in as argv[1] to your program.
58 * <li> call libtrace_read_packet(), passing in the libtrace_t returned from
59 * create trace and a buffer (and the buffer length)
60 * <li> call getIP() on the buffer, and do whatever you need
61 * <li> loop back to step 3, until libtrace_read_packet() returns -1
62 * </ol>
63 * @par Linking
64 * To use this library you need to link against libtrace by passing -ltrace
65 * to your linker. You may also need to link against a version of libpcap
66 * and of zlib which are compiled for largefile support (if you wish to access
67 * traces larger than 2 GB). This is left as an exercise for the reader. Debian
68 * Woody, at least, does not support large files offsets.
69 *
70 */
71
72#define COLLECTOR_PORT 3435
73
74/** Opaque structure holding information about a trace */
75struct libtrace_t;
76       
77/** Opaque structure holding information about a bpf filter */
78struct libtrace_filter_t;
79
80/** Opaque structure holding information about a packet */
81#define LIBTRACE_PACKET_BUFSIZE 65536
82struct libtrace_packet_t {
83        struct libtrace_t *trace;
84        char buffer[LIBTRACE_PACKET_BUFSIZE];
85        size_t size;
86        uint8_t status;
87};
88
89/** Structure for dealing with IP packets */
90struct libtrace_ip
91  {
92#if BYTE_ORDER == __LITTLE_ENDIAN
93    unsigned int ip_hl:4;               /**< header length */
94    unsigned int ip_v:4;                /**< version */
95#endif
96#if BYTE_ORDER == __BIG_ENDIAN
97    unsigned int ip_v:4;                /**< version */
98    unsigned int ip_hl:4;               /**< header length */
99#endif
100    u_int8_t ip_tos;                    /**< type of service */
101    u_short ip_len;                     /**< total length */
102    u_short ip_id;                      /**< identification */
103    u_short ip_off;                     /**< fragment offset field */
104#define IP_RF 0x8000                    /**< reserved fragment flag */
105#define IP_DF 0x4000                    /**< dont fragment flag */
106#define IP_MF 0x2000                    /**< more fragments flag */
107#define IP_OFFMASK 0x1fff               /**< mask for fragmenting bits */
108    u_int8_t ip_ttl;                    /**< time to live */
109    u_int8_t ip_p;                      /**< protocol */
110    u_short ip_sum;                     /**< checksum */
111    struct in_addr ip_src, ip_dst;      /**< source and dest address */
112  };
113
114/** Structure for dealing with TCP packets */
115struct libtrace_tcp
116  {
117    u_int16_t source;           /**< Source Port */
118    u_int16_t dest;             /**< Destination port */
119    u_int32_t seq;              /**< Sequence number */
120    u_int32_t ack_seq;          /**< Acknowledgement Number */
121#  if BYTE_ORDER == __LITTLE_ENDIAN
122    u_int16_t res1:4;           /**< Reserved bits */
123    u_int16_t doff:4;           
124    u_int16_t fin:1;            /**< FIN */
125    u_int16_t syn:1;            /**< SYN flag */
126    u_int16_t rst:1;            /**< RST flag */
127    u_int16_t psh:1;            /**< PuSH flag */
128    u_int16_t ack:1;            /**< ACK flag */
129    u_int16_t urg:1;            /**< URG flag */
130    u_int16_t res2:2;           /**< Reserved */
131#  elif BYTE_ORDER == __BIG_ENDIAN
132    u_int16_t doff:4;           
133    u_int16_t res1:4;
134    u_int16_t res2:2;
135    u_int16_t urg:1;
136    u_int16_t ack:1;
137    u_int16_t psh:1;
138    u_int16_t rst:1;
139    u_int16_t syn:1;
140    u_int16_t fin:1;
141#  else
142#   error "Adjust your <bits/endian.h> defines"
143#  endif
144    u_int16_t window;           /**< Window Size */
145    u_int16_t check;            /**< Checksum */
146    u_int16_t urg_ptr;          /**< Urgent Pointer */
147};
148
149/** UDP Header for dealing with UDP packets */
150struct libtrace_udp {
151  u_int16_t     source;         /**< Source port */
152  u_int16_t     dest;           /**< Destination port */
153  u_int16_t     len;            /**< Length */
154  u_int16_t     check;          /**< Checksum */
155};
156
157/** ICMP Header for dealing with icmp packets */
158struct libtrace_icmp
159{
160  u_int8_t type;                /* message type */
161  u_int8_t code;                /* type sub-code */
162  u_int16_t checksum;
163  union
164  {
165    struct
166    {
167      u_int16_t id;
168      u_int16_t sequence;
169    } echo;                     /* echo datagram */
170    u_int32_t   gateway;        /* gateway address */
171    struct
172    {
173      u_int16_t __unused;
174      u_int16_t mtu;
175    } frag;                     /* path mtu discovery */
176  } un;
177};
178
179
180
181
182/** Create a trace file from a URI
183 *
184 * @returns opaque pointer to a libtrace_t
185 *
186 * Valid URI's are:
187 *  - erf:/path/to/erf/file
188 *  - erf:/path/to/erf/file.gz
189 *  - erf:/path/to/rtclient/socket
190 *  - erf:-  (stdin)
191 *  - dag:/dev/dagcard                  (implementd?)
192 *  - pcap:pcapinterface                (eg: pcap:eth0)
193 *  - pcap:/path/to/pcap/file
194 *  - pcap:/path/to/pcap/file.gz
195 *  - pcap:/path/to/pcap/socket         (implemented?)
196 *  - pcap:-
197 *  - rtclient:hostname
198 *  - rtclient:hostname:port
199 *  - wag:/path/to/wag/file
200 *  - wag:/path/to/wag/file.gz
201 *  - wag:/path/to/wag/socket
202 *  - wag:/dev/device
203 *
204 *  If an error occured why attempting to open the trace file, NULL is returned
205 *  and an error is output to stdout.
206 */
207struct libtrace_t *trace_create(char *uri);
208
209/** Close a trace file, freeing up any resources it may have been using
210 *
211 */
212void trace_destroy(struct libtrace_t *trace);
213
214/** Read one packet from the trace into buffer
215 *
216 * @param libtrace      the libtrace opaque pointer
217 * @param packet        the packet opaque pointer
218 * @returns false if it failed to read a packet
219 *
220 */
221int trace_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet);
222
223/** get a pointer to the link layer
224 * @param packet        the packet opaque pointer
225 *
226 * @returns a pointer to the link layer, or NULL if there is no link layer
227 *
228 * @note you should call getLinkType() to find out what type of link layer
229 * this is
230 */
231void *trace_get_link(struct libtrace_packet_t *packet);
232
233/** get a pointer to the IP header (if any)
234 * @param packet        the packet opaque pointer
235 *
236 * @returns a pointer to the IP header, or NULL if there is not an IP packet
237 */
238struct libtrace_ip *trace_get_ip(struct libtrace_packet_t *packet);
239
240/** get a pointer to the TCP header (if any)
241 * @param packet        the packet opaque pointer
242 *
243 * @returns a pointer to the TCP header, or NULL if there is not a TCP packet
244 */
245struct libtrace_tcp *trace_get_tcp(struct libtrace_packet_t *packet);
246
247/** get a pointer to the UDP header (if any)
248 * @param packet        the packet opaque pointer
249 *
250 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet
251 */
252struct libtrace_udp *trace_get_udp(struct libtrace_packet_t *packet);
253
254/** get a pointer to the ICMP header (if any)
255 * @param packet        the packet opaque pointer
256 *
257 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet
258 */
259struct libtrace_icmp *trace_get_icmp(struct libtrace_packet_t *packet);
260
261/** Get the current time in DAG time format
262 * @param packet        the packet opaque pointer
263 *
264 * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
265 * past 1970-01-01, the lower 32bits are partial seconds)
266 * @author Daniel Lawson
267 */
268uint64_t trace_get_erf_timestamp(struct libtrace_packet_t *packet);
269
270/** Get the current time in struct timeval
271 * @param packet        the packet opaque pointer
272 *
273 * @returns time that this packet was seen in a struct timeval
274 * @author Daniel Lawson
275 * @author Perry Lorier
276 */ 
277struct timeval trace_get_timeval(struct libtrace_packet_t *packet);
278
279/** Get the current time in floating point seconds
280 * @param packet        the packet opaque pointer
281 *
282 * @returns time that this packet was seen in 64bit floating point seconds
283 * @author Perry Lorier
284 */
285double trace_get_seconds(struct libtrace_packet_t *packet);
286
287/** Get the size of the packet in the trace
288 * @param packet        the packet opaque pointer
289 * @returns the size of the packet in the trace
290 * @author Perry Lorier
291 * @note Due to this being a header capture, or anonymisation, this may not
292 * be the same size as the original packet.  See get_wire_length() for the original
293 * size of the packet.
294 * @note This can (and often is) different for different packets in a trace!
295 * @par
296 *  This is sometimes called the "snaplen".
297 */
298
299int trace_get_capture_length(struct libtrace_packet_t *packet);
300
301/** Get the size of the packet as it was seen on the wire.
302 * @param packet        the packet opaque pointer
303 * @returns the size of the packet as it was on the wire.
304 * @author Perry Lorier
305 * @author Daniel Lawson
306 * @note Due to the trace being a header capture, or anonymisation this may
307 * not be the same as the Capture Len.
308 */ 
309
310int trace_get_wire_length(struct libtrace_packet_t *packet);
311
312/** Link layer types
313 */
314typedef enum { 
315       TRACE_TYPE_LEGACY, 
316       TRACE_TYPE_HDLC_POS, 
317       TRACE_TYPE_ETH,
318       TRACE_TYPE_ATM,
319       TRACE_TYPE_80211,
320     } libtrace_linktype_t;
321
322/** Get the type of the link layer
323 * @param packet        the packet opaque pointer
324 * @returns libtrace_linktype_t
325 * @author Perry Lorier
326 * @author Daniel Lawson
327 */
328
329inline libtrace_linktype_t trace_get_link_type(struct libtrace_packet_t *packet);
330
331/** Get the destination MAC addres
332 * @param packet        the packet opaque pointer
333 * @returns a pointer to the destination mac, (or NULL if there is no
334 * destination MAC)
335 * @author Perry Lorier
336 */
337uint8_t *trace_get_destination_mac(struct libtrace_packet_t *packet);
338
339/** Get the source MAC addres
340 * @param packet        the packet opaque pointer
341 * @returns a pointer to the source mac, (or NULL if there is no source MAC)
342 * @author Perry Lorier
343 */
344uint8_t *trace_get_source_mac(struct libtrace_packet_t *packet);
345
346/** Get the direction flag, if it has one
347 * @param packet        the packet opaque pointer
348 * @returns a signed value containing the direction flag, or -1 if this is not supported
349 * @author Daniel Lawson
350 */
351int8_t trace_get_direction(struct libtrace_packet_t *packet);
352
353/** Event types */
354typedef enum {
355        TRACE_EVENT_IOWAIT,
356        TRACE_EVENT_SLEEP,
357        TRACE_EVENT_PACKET
358} libtrace_event_t;
359
360/** process a libtrace event
361 * @returns
362 *  TRACE_EVENT_IOWAIT  Waiting on I/O on <fd>
363 *  TRACE_EVENT_SLEEP   Next event in <seconds>
364 *  TRACE_EVENT_PACKET  Packet arrived in <buffer> with size <size>
365 */
366libtrace_event_t trace_event(struct libtrace_packet_t *packet,
367                int *fd,double *seconds);
368
369/** setup a BPF filter
370 * @param filterstring a char * containing the bpf filter string
371 * @returns opaque pointer pointer to a libtrace_filter_t object
372 * @author Daniel Lawson
373 */
374struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring);
375
376/** apply a BPF filter
377 * @param filter        the filter opaque pointer
378 * @param packet        the packet opaque pointer
379 * @returns the return value from bpf_filter
380 * @author Daniel Lawson
381 */
382int trace_bpf_filter(struct libtrace_filter_t *filter,
383                struct libtrace_packet_t *packet);
384
385#ifdef __cplusplus
386} // extern "C"
387#endif // #ifdef __cplusplus
388#endif // LIBTRACE_H_
Note: See TracBrowser for help on using the repository browser.