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 | /** @file |
---|
35 | * |
---|
36 | * @brief Trace file processing library header |
---|
37 | * |
---|
38 | * @author Daniel Lawson |
---|
39 | * @author Perry Lorier |
---|
40 | * |
---|
41 | * @version $Id$ |
---|
42 | * |
---|
43 | * This library provides a per packet interface into a trace file, or a live |
---|
44 | * captures. It supports ERF, DAG cards, WAG cards, WAG's event format, |
---|
45 | * pcap etc. |
---|
46 | * |
---|
47 | * @par Usage |
---|
48 | * See the example/ directory in the source distribution for some simple examples |
---|
49 | * @par Linking |
---|
50 | * To use this library you need to link against libtrace by passing -ltrace |
---|
51 | * to your linker. You may also need to link against a version of libpcap |
---|
52 | * and of zlib which are compiled for largefile support (if you wish to access |
---|
53 | * traces larger than 2 GB). This is left as an exercise for the reader. Debian |
---|
54 | * Woody, at least, does not support large file offsets. |
---|
55 | * |
---|
56 | */ |
---|
57 | |
---|
58 | #include <sys/types.h> |
---|
59 | #include <netinet/in.h> |
---|
60 | /** API version as 2 byte hex digits, eg 0xXXYYZZ */ |
---|
61 | #define LIBTRACE_API_VERSION 0x030000 /* 3.0.00 */ |
---|
62 | |
---|
63 | #ifdef __cplusplus |
---|
64 | extern "C" { |
---|
65 | #endif |
---|
66 | |
---|
67 | /* Function does not depend on anything but its |
---|
68 | * parameters, used to hint gcc's optimisations |
---|
69 | */ |
---|
70 | #if __GNUC__ >= 3 |
---|
71 | # define SIMPLE_FUNCTION __attribute__((pure)) |
---|
72 | # define UNUSED __attribute__((unused)) |
---|
73 | # define PACKED __attribute__((packed)) |
---|
74 | #else |
---|
75 | # define SIMPLE_FUNCTION |
---|
76 | # define UNUSED |
---|
77 | #endif |
---|
78 | |
---|
79 | /** Opaque structure holding information about an output trace */ |
---|
80 | typedef struct libtrace_out_t libtrace_out_t; |
---|
81 | |
---|
82 | /** Opaque structure holding information about a trace */ |
---|
83 | typedef struct libtrace_t libtrace_t; |
---|
84 | |
---|
85 | /** Opaque structure holding information about a bpf filter */ |
---|
86 | typedef struct libtrace_filter_t libtrace_filter_t; |
---|
87 | |
---|
88 | /** if a packet has memory allocated |
---|
89 | * If the packet has allocated it's own memory it's buffer_control should |
---|
90 | * be TRACE_CTRL_PACKET, when the packet is destroyed it's memory will be |
---|
91 | * free()'d. If it's doing zerocopy out of memory owned by something else |
---|
92 | * it should be TRACE_CTRL_EXTERNAL. |
---|
93 | * @note the letters p and e are magic numbers used to detect if the packet |
---|
94 | * wasn't created properly |
---|
95 | */ |
---|
96 | typedef enum { |
---|
97 | TRACE_CTRL_PACKET='p', |
---|
98 | TRACE_CTRL_EXTERNAL='e' |
---|
99 | } buf_control_t; |
---|
100 | /** Structure holding information about a packet */ |
---|
101 | #define LIBTRACE_PACKET_BUFSIZE 65536 |
---|
102 | |
---|
103 | /** The libtrace structure, applications shouldn't be meddling around in here |
---|
104 | */ |
---|
105 | typedef struct libtrace_packet_t { |
---|
106 | struct libtrace_t *trace; /**< pointer to the trace */ |
---|
107 | void *header; /**< pointer to the framing header */ |
---|
108 | void *payload; /**< pointer to the link layer */ |
---|
109 | buf_control_t buf_control; /**< who owns the memory */ |
---|
110 | void *buffer; /**< allocated buffer */ |
---|
111 | size_t size; /**< trace_get_framing_length() |
---|
112 | * +trace_get_capture_length() */ |
---|
113 | uint8_t type; /**< rt protocol type for the packet */ |
---|
114 | } libtrace_packet_t; |
---|
115 | |
---|
116 | /** libtrace error information */ |
---|
117 | typedef struct trace_err_t{ |
---|
118 | int err_num; /**< error code */ |
---|
119 | char problem[255]; /**< the format, uri etc that caused the error for reporting purposes */ |
---|
120 | } libtrace_err_t; |
---|
121 | |
---|
122 | /** Enumeration of error codes */ |
---|
123 | enum { |
---|
124 | TRACE_ERR_NOERROR = 0, |
---|
125 | TRACE_ERR_BAD_FORMAT = -1, |
---|
126 | TRACE_ERR_NO_INIT = -2, |
---|
127 | TRACE_ERR_NO_INIT_OUT = -3, |
---|
128 | TRACE_ERR_URI_LONG = -4, |
---|
129 | TRACE_ERR_URI_NOCOLON = -5, |
---|
130 | TRACE_ERR_INIT_FAILED = -6, |
---|
131 | TRACE_ERR_UNKNOWN_OPTION= -7, |
---|
132 | TRACE_ERR_NO_CONVERSION = -8, |
---|
133 | TRACE_ERR_BAD_PACKET = -9, |
---|
134 | TRACE_ERR_OPTION_UNAVAIL= -10 |
---|
135 | }; |
---|
136 | |
---|
137 | /** @name Protocol structures |
---|
138 | * These convenience structures are here as they are portable ways of dealing |
---|
139 | * with various protocols. |
---|
140 | * @{ |
---|
141 | */ |
---|
142 | |
---|
143 | /** Structure for dealing with IP packets */ |
---|
144 | typedef PACKED struct libtrace_ip |
---|
145 | { |
---|
146 | #if BYTE_ORDER == LITTLE_ENDIAN |
---|
147 | unsigned int ip_hl:4; /**< header length */ |
---|
148 | unsigned int ip_v:4; /**< version */ |
---|
149 | #elif BYTE_ORDER == BIG_ENDIAN |
---|
150 | unsigned int ip_v:4; /**< version */ |
---|
151 | unsigned int ip_hl:4; /**< header length */ |
---|
152 | #else |
---|
153 | # error "Adjust your <bits/endian.h> defines" |
---|
154 | #endif |
---|
155 | u_int8_t ip_tos; /**< type of service */ |
---|
156 | u_short ip_len; /**< total length */ |
---|
157 | u_short ip_id; /**< identification */ |
---|
158 | #if BYTE_ORDER == LITTLE_ENDIAN |
---|
159 | unsigned int ip_off:12; /**< fragment offset */ |
---|
160 | unsigned int ip_mf:1; /**< more fragments flag */ |
---|
161 | unsigned int ip_df:1; /**< dont fragment flag */ |
---|
162 | unsigned int ip_rf:1; /**< reserved fragment flag */ |
---|
163 | #elif BYTE_ORDER == BIG_ENDIAN |
---|
164 | unsigned int ip_rf:1; |
---|
165 | unsigned int ip_df:1; |
---|
166 | unsigned int ip_mf:1; |
---|
167 | unsigned int ip_off:12; |
---|
168 | #else |
---|
169 | # error "Adjust your <bits/endian.h> defines" |
---|
170 | #endif |
---|
171 | u_int8_t ip_ttl; /**< time to live */ |
---|
172 | u_int8_t ip_p; /**< protocol */ |
---|
173 | u_short ip_sum; /**< checksum */ |
---|
174 | struct in_addr ip_src; /**< source address */ |
---|
175 | struct in_addr ip_dst; /**< dest address */ |
---|
176 | } libtrace_ip_t |
---|
177 | ; |
---|
178 | |
---|
179 | /** Structure for dealing with TCP packets */ |
---|
180 | typedef struct libtrace_tcp |
---|
181 | { |
---|
182 | u_int16_t source; /**< Source Port */ |
---|
183 | u_int16_t dest; /**< Destination port */ |
---|
184 | u_int32_t seq; /**< Sequence number */ |
---|
185 | u_int32_t ack_seq; /**< Acknowledgement Number */ |
---|
186 | # if BYTE_ORDER == LITTLE_ENDIAN |
---|
187 | unsigned int res1:4; /**< Reserved bits */ |
---|
188 | unsigned int doff:4; /**< data offset */ |
---|
189 | unsigned int fin:1; /**< FIN */ |
---|
190 | unsigned int syn:1; /**< SYN flag */ |
---|
191 | unsigned int rst:1; /**< RST flag */ |
---|
192 | unsigned int psh:1; /**< PuSH flag */ |
---|
193 | unsigned int ack:1; /**< ACK flag */ |
---|
194 | unsigned int urg:1; /**< URG flag */ |
---|
195 | unsigned int res2:2; /**< Reserved */ |
---|
196 | # elif BYTE_ORDER == BIG_ENDIAN |
---|
197 | unsigned int doff:4; /**< Data offset */ |
---|
198 | unsigned int res1:4; /**< Reserved bits */ |
---|
199 | unsigned int res2:2; /**< Reserved */ |
---|
200 | unsigned int urg:1; /**< URG flag */ |
---|
201 | unsigned int ack:1; /**< ACK flag */ |
---|
202 | unsigned int psh:1; /**< PuSH flag */ |
---|
203 | unsigned int rst:1; /**< RST flag */ |
---|
204 | unsigned int syn:1; /**< SYN flag */ |
---|
205 | unsigned int fin:1; /**< FIN flag */ |
---|
206 | # else |
---|
207 | # error "Adjust your <bits/endian.h> defines" |
---|
208 | # endif |
---|
209 | u_int16_t window; /**< Window Size */ |
---|
210 | u_int16_t check; /**< Checksum */ |
---|
211 | u_int16_t urg_ptr; /**< Urgent Pointer */ |
---|
212 | } __attribute__ ((packed)) libtrace_tcp_t; |
---|
213 | |
---|
214 | /** UDP Header for dealing with UDP packets */ |
---|
215 | typedef struct libtrace_udp { |
---|
216 | u_int16_t source; /**< Source port */ |
---|
217 | u_int16_t dest; /**< Destination port */ |
---|
218 | u_int16_t len; /**< Length */ |
---|
219 | u_int16_t check; /**< Checksum */ |
---|
220 | } __attribute__ ((packed)) libtrace_udp_t; |
---|
221 | |
---|
222 | /** ICMP Header for dealing with icmp packets */ |
---|
223 | typedef struct libtrace_icmp |
---|
224 | { |
---|
225 | u_int8_t type; /**< message type */ |
---|
226 | u_int8_t code; /**< type sub-code */ |
---|
227 | u_int16_t checksum; /**< checksum */ |
---|
228 | union |
---|
229 | { |
---|
230 | struct |
---|
231 | { |
---|
232 | u_int16_t id; |
---|
233 | u_int16_t sequence; |
---|
234 | } echo; /**< echo datagram */ |
---|
235 | u_int32_t gateway; /**< gateway address */ |
---|
236 | struct |
---|
237 | { |
---|
238 | u_int16_t unused; |
---|
239 | u_int16_t mtu; |
---|
240 | } frag; /**< path mtu discovery */ |
---|
241 | } un; /**< Union for payloads of various icmp codes */ |
---|
242 | } __attribute__ ((packed)) libtrace_icmp_t; |
---|
243 | |
---|
244 | /** LLC/SNAP header */ |
---|
245 | typedef struct libtrace_llcsnap |
---|
246 | { |
---|
247 | u_int8_t dsap; |
---|
248 | u_int8_t ssap; |
---|
249 | u_int8_t control; |
---|
250 | u_int32_t oui:24; |
---|
251 | u_int16_t type; |
---|
252 | } __attribute__ ((packed)) libtrace_llcsnap_t; |
---|
253 | |
---|
254 | /** 802.3 frame */ |
---|
255 | typedef struct libtrace_ether |
---|
256 | { |
---|
257 | u_int8_t ether_dhost[6]; /**< destination ether addr */ |
---|
258 | u_int8_t ether_shost[6]; /**< source ether addr */ |
---|
259 | u_int16_t ether_type; /**< packet type ID field (next-header) */ |
---|
260 | } __attribute__ ((packed)) libtrace_ether_t; |
---|
261 | |
---|
262 | /** 802.1Q frame */ |
---|
263 | typedef struct libtrace_8021q |
---|
264 | { |
---|
265 | u_int8_t ether_dhost[6]; /**< destination eth addr */ |
---|
266 | u_int8_t ether_shost[6]; /**< source ether addr */ |
---|
267 | u_int16_t ether_type; /**< packet type ID field , 0x8100 for VLAN */ |
---|
268 | unsigned int vlan_pri:3; /**< vlan user priority */ |
---|
269 | unsigned int vlan_cfi:1; /**< vlan format indicator, |
---|
270 | * 0 for ethernet, 1 for token ring */ |
---|
271 | unsigned int vlan_id:12; /**< vlan id */ |
---|
272 | u_int16_t vlan_ether_type; /**< vlan sub-packet type ID field |
---|
273 | * (next-header)*/ |
---|
274 | } __attribute__ ((packed)) libtrace_8021q_t; |
---|
275 | |
---|
276 | /** ATM cell */ |
---|
277 | typedef struct libtrace_atm_cell |
---|
278 | { |
---|
279 | unsigned int gfc:4; |
---|
280 | u_int8_t vpi; |
---|
281 | u_int16_t vci; |
---|
282 | unsigned int pt:3; |
---|
283 | unsigned int clp:1; |
---|
284 | unsigned int hec; |
---|
285 | } __attribute__ ((packed)) libtrace_atm_cell; |
---|
286 | |
---|
287 | /** POS header */ |
---|
288 | typedef struct libtrace_pos |
---|
289 | { |
---|
290 | u_int16_t header; |
---|
291 | u_int16_t ether_type; /**< ether type */ |
---|
292 | } __attribute__ ((packed)) libtrace_pos; |
---|
293 | /*@}*/ |
---|
294 | |
---|
295 | /** Prints help information for libtrace |
---|
296 | * |
---|
297 | * Function prints out some basic help information regarding libtrace, |
---|
298 | * and then prints out the help() function registered with each input module |
---|
299 | */ |
---|
300 | void trace_help(); |
---|
301 | |
---|
302 | /** Gets the output format for a given output trace |
---|
303 | * |
---|
304 | * @param libtrace the output trace to get the name of the format fo |
---|
305 | * @return callee-owned null-terminated char* containing the output format |
---|
306 | * |
---|
307 | */ |
---|
308 | SIMPLE_FUNCTION |
---|
309 | char *trace_get_output_format(const libtrace_out_t *libtrace); |
---|
310 | |
---|
311 | /** @name Trace management |
---|
312 | * These members deal with creating, configuring, starting, pausing and |
---|
313 | * cleaning up a trace object |
---|
314 | *@{ |
---|
315 | */ |
---|
316 | |
---|
317 | /** Create a trace file from a URI |
---|
318 | * |
---|
319 | * @param uri containing a valid libtrace URI |
---|
320 | * @return opaque pointer to a libtrace_t |
---|
321 | * |
---|
322 | * Valid URI's are: |
---|
323 | * - erf:/path/to/erf/file |
---|
324 | * - erf:/path/to/erf/file.gz |
---|
325 | * - erf:/path/to/rtclient/socket |
---|
326 | * - erf:- (stdin) |
---|
327 | * - dag:/dev/dagcard |
---|
328 | * - pcapint:pcapinterface (eg: pcap:eth0) |
---|
329 | * - pcap:/path/to/pcap/file |
---|
330 | * - pcap:- |
---|
331 | * - rtclient:hostname |
---|
332 | * - rtclient:hostname:port |
---|
333 | * - wag:- |
---|
334 | * - wag:/path/to/wag/file |
---|
335 | * - wag:/path/to/wag/file.gz |
---|
336 | * - wag:/path/to/wag/socket |
---|
337 | * |
---|
338 | * If an error occured when attempting to open the trace file, an error |
---|
339 | * trace is returned and trace_get_error should be called to find out |
---|
340 | * if an error occured, and what that error was. The trace is created in the |
---|
341 | * configuration state, you must call trace_start to start the capture. |
---|
342 | */ |
---|
343 | libtrace_t *trace_create(const char *uri); |
---|
344 | |
---|
345 | /** Creates a "dummy" trace file that has only the format type set. |
---|
346 | * |
---|
347 | * @return opaque pointer to a (sparsely initialised) libtrace_t |
---|
348 | * |
---|
349 | * IMPORTANT: Do not attempt to call trace_read_packet or other such functions |
---|
350 | * with the dummy trace. Its intended purpose is to act as a packet->trace for |
---|
351 | * libtrace_packet_t's that are not associated with a libtrace_t structure. |
---|
352 | */ |
---|
353 | libtrace_t *trace_create_dead(const char *uri); |
---|
354 | |
---|
355 | /** Creates a trace output file from a URI. |
---|
356 | * |
---|
357 | * @param uri the uri string describing the output format and destination |
---|
358 | * @return opaque pointer to a libtrace_output_t |
---|
359 | * @author Shane Alcock |
---|
360 | * |
---|
361 | * Valid URI's are: |
---|
362 | * - gzerf:/path/to/erf/file.gz |
---|
363 | * - gzerf:/path/to/erf/file |
---|
364 | * - rtserver:hostname |
---|
365 | * - rtserver:hostname:port |
---|
366 | * |
---|
367 | * If an error occured when attempting to open the output trace, NULL is returned |
---|
368 | * and trace_errno is set. Use trace_perror() to get more information |
---|
369 | */ |
---|
370 | libtrace_out_t *trace_create_output(const char *uri); |
---|
371 | |
---|
372 | /** Start the capture |
---|
373 | * @param libtrace The trace to start |
---|
374 | * @return 0 on success |
---|
375 | * |
---|
376 | * This does the actual work with starting the trace capture, and applying |
---|
377 | * all the config options. This may fail. |
---|
378 | */ |
---|
379 | int trace_start(libtrace_t *libtrace); |
---|
380 | |
---|
381 | /** Pause the capture |
---|
382 | * @param libtrace The trace to pause |
---|
383 | * @return 0 on success |
---|
384 | * |
---|
385 | * This stops a capture in progress and returns you to the configuration |
---|
386 | * state. Any packets that arrive after trace_pause() has been called |
---|
387 | * will be discarded. To resume capture, call trace_start(). |
---|
388 | */ |
---|
389 | int trace_pause(libtrace_t *libtrace); |
---|
390 | |
---|
391 | /** Start an output trace |
---|
392 | * @param libtrace The trace to start |
---|
393 | * @return 0 on success |
---|
394 | * |
---|
395 | * This does the actual work with starting a trace for write. This generally |
---|
396 | * creates the file. |
---|
397 | */ |
---|
398 | int trace_start_output(libtrace_out_t *libtrace); |
---|
399 | |
---|
400 | /** Valid trace capture options */ |
---|
401 | typedef enum { |
---|
402 | TRACE_OPTION_SNAPLEN, /**< Number of bytes captured */ |
---|
403 | TRACE_OPTION_PROMISC, /**< Capture packets to other hosts */ |
---|
404 | TRACE_OPTION_FILTER /**< Apply this filter to all packets recieved */ |
---|
405 | } trace_option_t; |
---|
406 | |
---|
407 | /** Sets an input config option |
---|
408 | * @param libtrace the trace object to apply the option to |
---|
409 | * @param option the option to set |
---|
410 | * @param value the value to set the option to |
---|
411 | * @return -1 if option configuration failed, 0 otherwise |
---|
412 | * This should be called after trace_create, and before trace_start |
---|
413 | */ |
---|
414 | int trace_config(libtrace_t *libtrace, |
---|
415 | trace_option_t option, |
---|
416 | void *value); |
---|
417 | |
---|
418 | typedef enum { |
---|
419 | TRACE_OPTION_OUTPUT_FILEFLAGS, /**< File flags to open the trace file |
---|
420 | * with. eg O_APPEND |
---|
421 | */ |
---|
422 | TRACE_OPTION_OUTPUT_COMPRESS /**< Compression level, eg 6. */ |
---|
423 | } trace_option_output_t; |
---|
424 | |
---|
425 | /** Sets an output config option |
---|
426 | * |
---|
427 | * @param libtrace the output trace object to apply the option to |
---|
428 | * @param option the option to set |
---|
429 | * @param value the value to set the option to |
---|
430 | * @return -1 if option configuration failed, 0 otherwise |
---|
431 | * This should be called after trace_create_output, and before |
---|
432 | * trace_start_output |
---|
433 | */ |
---|
434 | int trace_config_output(libtrace_out_t *libtrace, |
---|
435 | trace_option_output_t option, |
---|
436 | void *value |
---|
437 | ); |
---|
438 | |
---|
439 | /** Close a trace file, freeing up any resources it may have been using |
---|
440 | * |
---|
441 | */ |
---|
442 | void trace_destroy(libtrace_t *trace); |
---|
443 | |
---|
444 | /** Close a trace file, freeing up any resources it may have been using |
---|
445 | * @param trace trace file to be destroyed |
---|
446 | */ |
---|
447 | void trace_destroy_dead(libtrace_t *trace); |
---|
448 | |
---|
449 | /** Close a trace output file, freeing up any resources it may have been using |
---|
450 | * @param trace the output trace file to be destroyed |
---|
451 | * |
---|
452 | * @author Shane Alcock |
---|
453 | */ |
---|
454 | void trace_destroy_output(libtrace_out_t *trace); |
---|
455 | |
---|
456 | /** Check (and clear) the current error state of an input trace |
---|
457 | * @param trace the trace file to check the error state on |
---|
458 | * @return Error report |
---|
459 | * This reads and returns the current error state and sets the current error |
---|
460 | * to "no error". |
---|
461 | */ |
---|
462 | libtrace_err_t trace_get_err(libtrace_t *trace); |
---|
463 | |
---|
464 | /** Check (and clear) the current error state of an output trace |
---|
465 | * @param trace the output trace file to check the error state on |
---|
466 | * @return Error report |
---|
467 | * This reads and returns the current error state and sets the current error |
---|
468 | * to "no error". |
---|
469 | */ |
---|
470 | libtrace_err_t trace_get_err_output(libtrace_out_t *trace); |
---|
471 | |
---|
472 | /*@}*/ |
---|
473 | |
---|
474 | /** @name Reading/Writing packets |
---|
475 | * These members deal with creating, reading and writing packets |
---|
476 | * |
---|
477 | * @{ |
---|
478 | */ |
---|
479 | |
---|
480 | /** Create a new packet object |
---|
481 | * |
---|
482 | * @return a pointer to an initialised libtrace_packet_t object |
---|
483 | */ |
---|
484 | libtrace_packet_t *trace_create_packet(); |
---|
485 | |
---|
486 | /** Copy a packet |
---|
487 | * @param packet the source packet to copy |
---|
488 | * @return a new packet which has the same content as the source packet |
---|
489 | * @note This always involves a copy, which can be slow. Use of this |
---|
490 | * function should be avoided where possible. |
---|
491 | * @par The reason you would want to use this function is that a zerocopied |
---|
492 | * packet from a device is using the devices memory which may be a limited |
---|
493 | * resource. Copying the packet will cause it to be copied into the systems |
---|
494 | * memory. |
---|
495 | */ |
---|
496 | libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet); |
---|
497 | |
---|
498 | /** Destroy a packet object |
---|
499 | * |
---|
500 | * sideeffect: sets packet to NULL |
---|
501 | */ |
---|
502 | void trace_destroy_packet(libtrace_packet_t **packet); |
---|
503 | |
---|
504 | |
---|
505 | /** Read one packet from the trace into buffer |
---|
506 | * |
---|
507 | * @param trace the libtrace opaque pointer |
---|
508 | * @param packet the packet opaque pointer |
---|
509 | * @return 0 on EOF, negative value on error |
---|
510 | * |
---|
511 | * @note the trace must have been started with trace_start before calling |
---|
512 | * this function |
---|
513 | */ |
---|
514 | int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet); |
---|
515 | |
---|
516 | /** Event types |
---|
517 | * see \ref libtrace_eventobj_t and \ref trace_event |
---|
518 | */ |
---|
519 | typedef enum { |
---|
520 | TRACE_EVENT_IOWAIT, /**< Need to block on fd */ |
---|
521 | TRACE_EVENT_SLEEP, /**< Sleep for some time */ |
---|
522 | TRACE_EVENT_PACKET, /**< packet has arrived */ |
---|
523 | TRACE_EVENT_TERMINATE /**< End of trace */ |
---|
524 | } libtrace_event_t; |
---|
525 | |
---|
526 | /** structure returned by libtrace_event explaining what the current event is */ |
---|
527 | typedef struct libtrace_eventobj_t { |
---|
528 | libtrace_event_t type; /**< event type (iowait,sleep,packet) */ |
---|
529 | int fd; /**< if IOWAIT, the fd to sleep on */ |
---|
530 | double seconds; /**< if SLEEP, the amount of time to sleep for |
---|
531 | */ |
---|
532 | int size; /**< if PACKET, the value returned from |
---|
533 | * trace_read_packet |
---|
534 | */ |
---|
535 | } libtrace_eventobj_t; |
---|
536 | |
---|
537 | /** process a libtrace event |
---|
538 | * @param trace the libtrace opaque pointer |
---|
539 | * @param packet the libtrace_packet opaque pointer |
---|
540 | * @return libtrace_event struct containing the type, and potential |
---|
541 | * fd or seconds to sleep on |
---|
542 | * |
---|
543 | * Type can be: |
---|
544 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
545 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
546 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
547 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
548 | */ |
---|
549 | libtrace_eventobj_t trace_event(libtrace_t *trace, |
---|
550 | libtrace_packet_t *packet); |
---|
551 | |
---|
552 | |
---|
553 | /** Write one packet out to the output trace |
---|
554 | * |
---|
555 | * @param trace the libtrace_out opaque pointer |
---|
556 | * @param packet the packet opaque pointer |
---|
557 | * @return the number of bytes written out, if zero or negative then an error has occured. |
---|
558 | */ |
---|
559 | int trace_write_packet(libtrace_out_t *trace, const libtrace_packet_t *packet); |
---|
560 | /*@}*/ |
---|
561 | |
---|
562 | /** @name Protocol decodes |
---|
563 | * These functions locate and return a pointer to various headers inside a |
---|
564 | * packet |
---|
565 | * @{ |
---|
566 | */ |
---|
567 | |
---|
568 | /** get a pointer to the link layer |
---|
569 | * @param packet the packet opaque pointer |
---|
570 | * |
---|
571 | * @return a pointer to the link layer, or NULL if there is no link layer |
---|
572 | * |
---|
573 | * @note you should call getLinkType() to find out what type of link layer |
---|
574 | * this is |
---|
575 | */ |
---|
576 | SIMPLE_FUNCTION |
---|
577 | void *trace_get_link(const libtrace_packet_t *packet); |
---|
578 | |
---|
579 | /** get a pointer to the IP header (if any) |
---|
580 | * @param packet the packet opaque pointer |
---|
581 | * |
---|
582 | * @return a pointer to the IP header, or NULL if there is not an IP packet |
---|
583 | */ |
---|
584 | SIMPLE_FUNCTION |
---|
585 | libtrace_ip_t *trace_get_ip(const libtrace_packet_t *packet); |
---|
586 | |
---|
587 | /** Gets a pointer to the transport layer header (if any) |
---|
588 | * @param packet a pointer to a libtrace_packet structure |
---|
589 | * |
---|
590 | * @return a pointer to the transport layer header, or NULL if there is no header |
---|
591 | */ |
---|
592 | void *trace_get_transport(const libtrace_packet_t *packet); |
---|
593 | |
---|
594 | /** Gets a pointer to the transport layer header (if any) given a pointer to the |
---|
595 | * IP header |
---|
596 | * @param ip The IP Header |
---|
597 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
598 | * |
---|
599 | * @return a pointer to the transport layer header, or NULL if there is no header |
---|
600 | * |
---|
601 | * Skipped can be NULL, in which case it will be ignored |
---|
602 | */ |
---|
603 | void *trace_get_transport_from_ip(const libtrace_ip_t *ip, int *skipped); |
---|
604 | |
---|
605 | /** get a pointer to the TCP header (if any) |
---|
606 | * @param packet the packet opaque pointer |
---|
607 | * |
---|
608 | * @return a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
609 | */ |
---|
610 | SIMPLE_FUNCTION |
---|
611 | libtrace_tcp_t *trace_get_tcp(const libtrace_packet_t *packet); |
---|
612 | |
---|
613 | /** get a pointer to the TCP header (if any) given a pointer to the IP header |
---|
614 | * @param ip The IP header |
---|
615 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
616 | * |
---|
617 | * @return a pointer to the TCP header, or NULL if this is not a TCP packet |
---|
618 | * |
---|
619 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
620 | * |
---|
621 | * @author Perry Lorier |
---|
622 | */ |
---|
623 | SIMPLE_FUNCTION |
---|
624 | libtrace_tcp_t *trace_get_tcp_from_ip(const libtrace_ip_t *ip,int *skipped); |
---|
625 | |
---|
626 | /** get a pointer to the UDP header (if any) |
---|
627 | * @param packet the packet opaque pointer |
---|
628 | * |
---|
629 | * @return a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
630 | */ |
---|
631 | SIMPLE_FUNCTION |
---|
632 | libtrace_udp_t *trace_get_udp(const libtrace_packet_t *packet); |
---|
633 | |
---|
634 | /** get a pointer to the UDP header (if any) given a pointer to the IP header |
---|
635 | * @param ip The IP header |
---|
636 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
637 | * |
---|
638 | * @return a pointer to the UDP header, or NULL if this is not an UDP packet |
---|
639 | * |
---|
640 | * Skipped may be NULL, in which case it will be ignored by this function. |
---|
641 | */ |
---|
642 | SIMPLE_FUNCTION |
---|
643 | libtrace_udp_t *trace_get_udp_from_ip(const libtrace_ip_t *ip,int *skipped); |
---|
644 | |
---|
645 | /** get a pointer to the ICMP header (if any) |
---|
646 | * @param packet the packet opaque pointer |
---|
647 | * |
---|
648 | * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
649 | */ |
---|
650 | SIMPLE_FUNCTION |
---|
651 | libtrace_icmp_t *trace_get_icmp(const libtrace_packet_t *packet); |
---|
652 | |
---|
653 | /** get a pointer to the ICMP header (if any) given a pointer to the IP header |
---|
654 | * @param ip The IP header |
---|
655 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
656 | * |
---|
657 | * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet |
---|
658 | * |
---|
659 | * Skipped may be NULL, in which case it will be ignored by this function |
---|
660 | */ |
---|
661 | SIMPLE_FUNCTION |
---|
662 | libtrace_icmp_t *trace_get_icmp_from_ip(const libtrace_ip_t *ip,int *skipped); |
---|
663 | /*@}*/ |
---|
664 | |
---|
665 | /** parse an ip or tcp option |
---|
666 | * @param[in,out] ptr the pointer to the current option |
---|
667 | * @param[in,out] len the length of the remaining buffer |
---|
668 | * @param[out] type the type of the option |
---|
669 | * @param[out] optlen the length of the option |
---|
670 | * @param[out] data the data of the option |
---|
671 | * |
---|
672 | * @return bool true if there is another option (and the fields are filled in) |
---|
673 | * or false if this was the last option. |
---|
674 | * |
---|
675 | * This updates ptr to point to the next option after this one, and updates |
---|
676 | * len to be the number of bytes remaining in the options area. Type is updated |
---|
677 | * to be the code of this option, and data points to the data of this option, |
---|
678 | * with optlen saying how many bytes there are. |
---|
679 | * |
---|
680 | * @note Beware of fragmented packets. |
---|
681 | */ |
---|
682 | int trace_get_next_option(unsigned char **ptr,int *len, |
---|
683 | unsigned char *type, |
---|
684 | unsigned char *optlen, |
---|
685 | unsigned char **data); |
---|
686 | |
---|
687 | |
---|
688 | /** @name Time |
---|
689 | * These functions deal with time that a packet arrived and return it |
---|
690 | * in various formats |
---|
691 | * @{ |
---|
692 | */ |
---|
693 | /** Get the current time in DAG time format |
---|
694 | * @param packet the packet opaque pointer |
---|
695 | * |
---|
696 | * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
697 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
698 | * @author Daniel Lawson |
---|
699 | */ |
---|
700 | SIMPLE_FUNCTION |
---|
701 | uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet); |
---|
702 | |
---|
703 | /** Get the current time in struct timeval |
---|
704 | * @param packet the packet opaque pointer |
---|
705 | * |
---|
706 | * @return time that this packet was seen in a struct timeval |
---|
707 | * @author Daniel Lawson |
---|
708 | * @author Perry Lorier |
---|
709 | */ |
---|
710 | SIMPLE_FUNCTION |
---|
711 | struct timeval trace_get_timeval(const libtrace_packet_t *packet); |
---|
712 | |
---|
713 | /** Get the current time in floating point seconds |
---|
714 | * @param packet the packet opaque pointer |
---|
715 | * |
---|
716 | * @return time that this packet was seen in 64bit floating point seconds |
---|
717 | * @author Daniel Lawson |
---|
718 | * @author Perry Lorier |
---|
719 | */ |
---|
720 | SIMPLE_FUNCTION |
---|
721 | double trace_get_seconds(const libtrace_packet_t *packet); |
---|
722 | |
---|
723 | /** Seek within a trace |
---|
724 | * @param trace trace to seek |
---|
725 | * @param seconds time to seek to |
---|
726 | * @return 0 on success. |
---|
727 | * Make the next packet read to be the first packet to occur at or after the |
---|
728 | * time searched for. This must be called in the configuration state (ie, |
---|
729 | * before trace_start() or after trace_pause(). |
---|
730 | * @note This function may be extremely slow. |
---|
731 | */ |
---|
732 | int trace_seek_seconds(libtrace_t *trace, double seconds); |
---|
733 | |
---|
734 | /** Seek within a trace |
---|
735 | * @param trace trace to seek |
---|
736 | * @param tv time to seek to |
---|
737 | * @return 0 on success. |
---|
738 | * Make the next packet read to be the first packet to occur at or after the |
---|
739 | * time searched for. This must be called in the configuration state (ie, |
---|
740 | * before trace_start() or after trace_pause(). |
---|
741 | * @note This function may be extremely slow. |
---|
742 | */ |
---|
743 | int trace_seek_timeval(libtrace_t *trace, struct timeval tv); |
---|
744 | |
---|
745 | /*@}*/ |
---|
746 | |
---|
747 | /** @name Sizes |
---|
748 | * This section deals with finding or setting the various different lengths |
---|
749 | * a packet can have |
---|
750 | * @{ |
---|
751 | */ |
---|
752 | /** Get the size of the packet in the trace |
---|
753 | * @param packet the packet opaque pointer |
---|
754 | * @return the size of the packet in the trace |
---|
755 | * @author Perry Lorier |
---|
756 | * @note Due to this being a header capture, or anonymisation, this may not |
---|
757 | * be the same size as the original packet. See get_wire_length() for the |
---|
758 | * original size of the packet. |
---|
759 | * @note This can (and often is) different for different packets in a trace! |
---|
760 | * @note This is sometimes called the "snaplen". |
---|
761 | * @note The return size refers to the network-level payload of the packet and |
---|
762 | * does not include any capture headers. For example, an Ethernet packet with |
---|
763 | * an empty TCP packet will return sizeof(ethernet_header) + sizeof(ip_header) |
---|
764 | * + sizeof(tcp_header). |
---|
765 | */ |
---|
766 | SIMPLE_FUNCTION |
---|
767 | size_t trace_get_capture_length(const libtrace_packet_t *packet); |
---|
768 | |
---|
769 | /** Get the size of the packet as it was seen on the wire. |
---|
770 | * @param packet the packet opaque pointer |
---|
771 | * @return the size of the packet as it was on the wire. |
---|
772 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
773 | * not be the same as the Capture Len. |
---|
774 | */ |
---|
775 | SIMPLE_FUNCTION |
---|
776 | size_t trace_get_wire_length(const libtrace_packet_t *packet); |
---|
777 | |
---|
778 | /** Get the length of the capture framing headers. |
---|
779 | * @param packet the packet opaque pointer |
---|
780 | * @return the size of the packet as it was on the wire. |
---|
781 | * @author Perry Lorier |
---|
782 | * @author Daniel Lawson |
---|
783 | * @note this length corresponds to the difference between the size of a |
---|
784 | * captured packet in memory, and the captured length of the packet |
---|
785 | */ |
---|
786 | SIMPLE_FUNCTION |
---|
787 | size_t trace_get_framing_length(const libtrace_packet_t *packet); |
---|
788 | |
---|
789 | /** Truncate ("snap") the packet at the suggested length |
---|
790 | * @param packet the packet opaque pointer |
---|
791 | * @param size the new length of the packet |
---|
792 | * @return the new capture length of the packet, or the original capture |
---|
793 | * length of the packet if unchanged |
---|
794 | */ |
---|
795 | size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size); |
---|
796 | |
---|
797 | /*@}*/ |
---|
798 | |
---|
799 | |
---|
800 | /** Link layer types |
---|
801 | * This enumates the various different link types that libtrace understands |
---|
802 | */ |
---|
803 | typedef enum { |
---|
804 | TRACE_TYPE_LEGACY, |
---|
805 | TRACE_TYPE_HDLC_POS, |
---|
806 | TRACE_TYPE_ETH, /**< 802.3 style Ethernet */ |
---|
807 | TRACE_TYPE_ATM, |
---|
808 | TRACE_TYPE_80211, /**< 802.11 frames */ |
---|
809 | TRACE_TYPE_NONE, |
---|
810 | TRACE_TYPE_LINUX_SLL, /**< Linux "null" framing */ |
---|
811 | TRACE_TYPE_PFLOG, /**< FreeBSD's PFlug */ |
---|
812 | TRACE_TYPE_LEGACY_DEFAULT, |
---|
813 | TRACE_TYPE_LEGACY_POS, |
---|
814 | TRACE_TYPE_LEGACY_ATM, |
---|
815 | TRACE_TYPE_LEGACY_ETH, |
---|
816 | TRACE_TYPE_80211_PRISM |
---|
817 | } libtrace_linktype_t; |
---|
818 | |
---|
819 | /** Get the type of the link layer |
---|
820 | * @param packet the packet opaque pointer |
---|
821 | * @return libtrace_linktype_t |
---|
822 | * @author Perry Lorier |
---|
823 | * @author Daniel Lawson |
---|
824 | */ |
---|
825 | SIMPLE_FUNCTION |
---|
826 | inline libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet); |
---|
827 | |
---|
828 | /** Get the destination MAC addres |
---|
829 | * @param packet the packet opaque pointer |
---|
830 | * @return a pointer to the destination mac, (or NULL if there is no |
---|
831 | * destination MAC) |
---|
832 | * @author Perry Lorier |
---|
833 | */ |
---|
834 | SIMPLE_FUNCTION |
---|
835 | uint8_t *trace_get_destination_mac(const libtrace_packet_t *packet); |
---|
836 | |
---|
837 | /** Get the source MAC addres |
---|
838 | * @param packet the packet opaque pointer |
---|
839 | * @return a pointer to the source mac, (or NULL if there is no source MAC) |
---|
840 | * @author Perry Lorier |
---|
841 | */ |
---|
842 | SIMPLE_FUNCTION |
---|
843 | uint8_t *trace_get_source_mac(const libtrace_packet_t *packet); |
---|
844 | |
---|
845 | /** Set the direction flag, if it has one |
---|
846 | * @param packet the packet opaque pointer |
---|
847 | * @param direction the new direction (0,1,2,3) |
---|
848 | * @return a signed value containing the direction flag, or -1 if this is not supported |
---|
849 | * @author Daniel Lawson |
---|
850 | */ |
---|
851 | int8_t trace_set_direction(libtrace_packet_t *packet, int8_t direction); |
---|
852 | |
---|
853 | /** Get the direction flag, if it has one |
---|
854 | * @param packet the packet opaque pointer |
---|
855 | * @return a signed value containing the direction flag, or -1 if this is not supported |
---|
856 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
857 | * and 1 for packets originating remotely (ie, inbound). |
---|
858 | * Other values are possible, which might be overloaded to mean special things |
---|
859 | * for a special trace. |
---|
860 | * @author Daniel Lawson |
---|
861 | */ |
---|
862 | SIMPLE_FUNCTION |
---|
863 | int8_t trace_get_direction(const libtrace_packet_t *packet); |
---|
864 | |
---|
865 | /** @name BPF |
---|
866 | * This section deals with using Berkley Packet Filters |
---|
867 | * @{ |
---|
868 | */ |
---|
869 | /** setup a BPF filter |
---|
870 | * @param filterstring a char * containing the bpf filter string |
---|
871 | * @return opaque pointer pointer to a libtrace_filter_t object |
---|
872 | * @author Daniel Lawson |
---|
873 | * @note The filter is not actually compiled at this point, so no correctness |
---|
874 | * tests are performed here. trace_bpf_setfilter will always return ok, but |
---|
875 | * if the filter is poorly constructed an error will be generated when the |
---|
876 | * filter is actually used |
---|
877 | */ |
---|
878 | SIMPLE_FUNCTION |
---|
879 | libtrace_filter_t *trace_bpf_setfilter(const char *filterstring); |
---|
880 | |
---|
881 | /** apply a BPF filter |
---|
882 | * @param filter the filter opaque pointer |
---|
883 | * @param packet the packet opaque pointer |
---|
884 | * @return 1 if the filter matches, 0 if it doesn't. |
---|
885 | * @note Due to the way BPF filters are built, the filter is not actually |
---|
886 | * compiled until the first time trace_bpf_filter is called. If your filter is |
---|
887 | * incorrect, it will generate an error message and assert, exiting the |
---|
888 | * program. This behaviour may change to more graceful handling of this error |
---|
889 | * in the future. |
---|
890 | */ |
---|
891 | int trace_bpf_filter(libtrace_filter_t *filter, |
---|
892 | const libtrace_packet_t *packet); |
---|
893 | /*@}*/ |
---|
894 | |
---|
895 | /** Which port is the server port */ |
---|
896 | typedef enum { |
---|
897 | USE_DEST, /**< Destination port is the server port */ |
---|
898 | USE_SOURCE /**< Source port is the server port */ |
---|
899 | } serverport_t; |
---|
900 | |
---|
901 | /** Get the source port |
---|
902 | * @param packet the packet to read from |
---|
903 | * @return a port in \em HOST byte order, or equivilent to ports for this |
---|
904 | * protocol, or 0 if this protocol has no ports. |
---|
905 | * @author Perry Lorier |
---|
906 | */ |
---|
907 | SIMPLE_FUNCTION |
---|
908 | uint16_t trace_get_source_port(const libtrace_packet_t *packet); |
---|
909 | |
---|
910 | /** Get the destination port |
---|
911 | * @param packet the packet to read from |
---|
912 | * @return a port in \em HOST byte order, or equivilent to ports for this |
---|
913 | * protocol, or 0 if this protocol has no ports. |
---|
914 | * @author Perry Lorier |
---|
915 | */ |
---|
916 | SIMPLE_FUNCTION |
---|
917 | uint16_t trace_get_destination_port(const libtrace_packet_t *packet); |
---|
918 | |
---|
919 | /** hint at the server port in specified protocol |
---|
920 | * @param protocol the IP layer protocol, eg 6 (tcp), 17 (udp) |
---|
921 | * @param source the source port from the packet |
---|
922 | * @param dest the destination port from the packet |
---|
923 | * @return one of USE_SOURCE or USE_DEST depending on which one you should use |
---|
924 | * @note ports must be in \em HOST byte order! |
---|
925 | * @author Daniel Lawson |
---|
926 | */ |
---|
927 | SIMPLE_FUNCTION |
---|
928 | int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest); |
---|
929 | |
---|
930 | /** Takes a uri and splits it into a format and uridata component. |
---|
931 | * Primarily for internal use but made available for external use. |
---|
932 | * @param uri the uri to be parsed |
---|
933 | * @param format destination location for the format component of the uri |
---|
934 | * @return 0 if an error occured, otherwise return the uridata component |
---|
935 | * @author Shane Alcock |
---|
936 | */ |
---|
937 | const char *trace_parse_uri(const char *uri, char **format); |
---|
938 | |
---|
939 | /** RT protocol base format identifiers |
---|
940 | * This is used to say what kind of packet is being sent over the rt protocol |
---|
941 | */ |
---|
942 | enum base_format_t { |
---|
943 | TRACE_FORMAT_ERF =1, |
---|
944 | TRACE_FORMAT_PCAP =2, |
---|
945 | TRACE_FORMAT_WAG =3, |
---|
946 | TRACE_FORMAT_RT =4, |
---|
947 | TRACE_FORMAT_LEGACY_ATM =5, |
---|
948 | TRACE_FORMAT_LEGACY_POS =6, |
---|
949 | TRACE_FORMAT_LEGACY_ETH =7 |
---|
950 | }; |
---|
951 | |
---|
952 | /** Gets the framing header type for a given packet. |
---|
953 | * @param packet the packet opaque pointer |
---|
954 | * @return the format of the packet |
---|
955 | */ |
---|
956 | enum base_format_t trace_get_format(struct libtrace_packet_t *packet); |
---|
957 | |
---|
958 | |
---|
959 | #ifdef __cplusplus |
---|
960 | } /* extern "C" */ |
---|
961 | #endif /* #ifdef __cplusplus */ |
---|
962 | #endif /* LIBTRACE_H_ */ |
---|