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