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