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