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