1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | /** @file */ |
---|
31 | |
---|
32 | #ifndef LIBTRACE_INT_H |
---|
33 | #define LIBTRACE_INT_H |
---|
34 | |
---|
35 | #ifdef __cplusplus |
---|
36 | extern "C" { |
---|
37 | #endif |
---|
38 | |
---|
39 | #include "common.h" |
---|
40 | #include "config.h" |
---|
41 | #include "libtrace.h" |
---|
42 | |
---|
43 | #ifdef _MSC_VER |
---|
44 | #pragma warning(disable:4996) |
---|
45 | #endif |
---|
46 | |
---|
47 | #ifdef HAVE_INTTYPES_H |
---|
48 | # include <inttypes.h> |
---|
49 | #else |
---|
50 | # include "lt_inttypes.h" |
---|
51 | #endif |
---|
52 | |
---|
53 | #ifdef HAVE_STDDEF_H |
---|
54 | # include <stddef.h> |
---|
55 | #else |
---|
56 | #ifndef WIN32 |
---|
57 | # error "Can't find stddev.h -- do you define ptrdiff_t elsewhere?" |
---|
58 | #endif |
---|
59 | #endif |
---|
60 | |
---|
61 | |
---|
62 | #include "rt_protocol.h" |
---|
63 | |
---|
64 | #ifdef HAVE_PCAP_BPF_H |
---|
65 | # include <pcap-bpf.h> |
---|
66 | # define HAVE_BPF 1 |
---|
67 | #else |
---|
68 | # ifdef HAVE_NET_BPF_H |
---|
69 | # include <net/bpf.h> |
---|
70 | # define HAVE_BPF 1 |
---|
71 | # endif |
---|
72 | #endif |
---|
73 | |
---|
74 | #ifdef HAVE_PCAP_H |
---|
75 | # include <pcap.h> |
---|
76 | # ifdef HAVE_PCAP_INT_H |
---|
77 | # include <pcap-int.h> |
---|
78 | # endif |
---|
79 | #endif |
---|
80 | |
---|
81 | #ifdef HAVE_ZLIB_H |
---|
82 | # include <zlib.h> |
---|
83 | #endif |
---|
84 | |
---|
85 | #ifndef HAVE_STRNCASECMP |
---|
86 | # ifndef HAVE__STRNICMP |
---|
87 | int strncasecmp(const char *str1, const char *str2, size_t n); |
---|
88 | # else |
---|
89 | # define strncasecmp _strnicmp |
---|
90 | # endif |
---|
91 | #endif |
---|
92 | |
---|
93 | #ifndef HAVE_SNPRINTF |
---|
94 | # ifndef HAVE_SPRINTF_S |
---|
95 | int snprintf(char *str, size_t size, const char *format, ...); |
---|
96 | # else |
---|
97 | # define snprintf sprintf_s |
---|
98 | # endif |
---|
99 | #endif |
---|
100 | |
---|
101 | #include "wag.h" |
---|
102 | #include "daglegacy.h" |
---|
103 | |
---|
104 | #ifdef HAVE_DAG_API |
---|
105 | # include "dagnew.h" |
---|
106 | # include "dagapi.h" |
---|
107 | # ifndef DAG_VERSION_2_4 |
---|
108 | # include <daginf.h> |
---|
109 | # endif |
---|
110 | #else |
---|
111 | # include "dagformat.h" |
---|
112 | #endif |
---|
113 | |
---|
114 | #define RP_BUFSIZE 65536 |
---|
115 | |
---|
116 | struct libtrace_event_status_t { |
---|
117 | libtrace_packet_t *packet; |
---|
118 | int psize; |
---|
119 | double tdelta; |
---|
120 | double trace_last_ts; |
---|
121 | }; |
---|
122 | |
---|
123 | /** The information about traces that are open |
---|
124 | * @internal |
---|
125 | */ |
---|
126 | struct libtrace_t { |
---|
127 | struct libtrace_format_t *format; /**< format driver pointer */ |
---|
128 | void *format_data; /**<format data pointer */ |
---|
129 | bool started; /**< if this trace has started */ |
---|
130 | libtrace_err_t err; /**< error information */ |
---|
131 | struct libtrace_event_status_t event; /**< the next event */ |
---|
132 | char *uridata; /**< the uri of this trace */ |
---|
133 | struct libtrace_filter_t *filter; /**< used by libtrace if the module |
---|
134 | * doesn't support filters natively |
---|
135 | */ |
---|
136 | unsigned int snaplen; /**< used by libtrace if the module |
---|
137 | * doesn't support snapping natively |
---|
138 | */ |
---|
139 | }; |
---|
140 | |
---|
141 | /** Information about output traces |
---|
142 | * @internal |
---|
143 | */ |
---|
144 | struct libtrace_out_t { |
---|
145 | struct libtrace_format_t *format; /**< format driver */ |
---|
146 | void *format_data; /**< format data */ |
---|
147 | bool started; /**< trace started */ |
---|
148 | libtrace_err_t err; /**< Associated error */ |
---|
149 | char *uridata; /**< URI associated with this trace */ |
---|
150 | }; |
---|
151 | |
---|
152 | void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...); |
---|
153 | void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...); |
---|
154 | |
---|
155 | typedef struct libtrace_sll_header_t { |
---|
156 | uint16_t pkttype; /* packet type */ |
---|
157 | uint16_t hatype; /* link-layer address type */ |
---|
158 | uint16_t halen; /* link-layer address length */ |
---|
159 | char addr[8]; /* link-layer address */ |
---|
160 | uint16_t protocol; /* protocol */ |
---|
161 | } libtrace_sll_header_t; |
---|
162 | |
---|
163 | #define TRACE_SLL_HOST 0 |
---|
164 | #define TRACE_SLL_BROADCAST 1 |
---|
165 | #define TRACE_SLL_MULTICAST 2 |
---|
166 | #define TRACE_SLL_OTHERHOST 3 |
---|
167 | #define TRACE_SLL_OUTGOING 4 |
---|
168 | |
---|
169 | #ifndef PF_RULESET_NAME_SIZE |
---|
170 | #define PF_RULESET_NAME_SIZE 16 |
---|
171 | #endif |
---|
172 | |
---|
173 | #ifndef IFNAMSIZ |
---|
174 | #define IFNAMSIZ 16 |
---|
175 | #endif |
---|
176 | |
---|
177 | typedef struct libtrace_pflog_header_t { |
---|
178 | uint8_t length; |
---|
179 | sa_family_t af; |
---|
180 | uint8_t action; |
---|
181 | uint8_t reason; |
---|
182 | char ifname[IFNAMSIZ]; |
---|
183 | char ruleset[PF_RULESET_NAME_SIZE]; |
---|
184 | uint32_t rulenr; |
---|
185 | uint32_t subrulenr; |
---|
186 | uint8_t dir; |
---|
187 | uint8_t pad[3]; |
---|
188 | } libtrace_pflog_header_t; |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | /** Module definition structure */ |
---|
193 | /* all of these should return -1, or NULL on failure */ |
---|
194 | struct libtrace_format_t { |
---|
195 | /** the uri name of this module */ |
---|
196 | char *name; |
---|
197 | /** the version of this module */ |
---|
198 | char *version; |
---|
199 | /** the RT protocol type of this module */ |
---|
200 | enum base_format_t type; |
---|
201 | /** stuff that deals with input @{ */ |
---|
202 | /** initialise an trace (or NULL if input is not supported) */ |
---|
203 | int (*init_input)(libtrace_t *libtrace); |
---|
204 | /** configure an trace (or NULL if input is not supported) */ |
---|
205 | int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value); |
---|
206 | /** start/unpause an trace (or NULL if input not supported) */ |
---|
207 | int (*start_input)(libtrace_t *libtrace); |
---|
208 | /** pause an trace (or NULL if input not supported) */ |
---|
209 | int (*pause_input)(libtrace_t *libtrace); |
---|
210 | /** @} */ |
---|
211 | /** stuff that deals with output @{ */ |
---|
212 | /** initialise output traces (or NULL if output not supported) */ |
---|
213 | int (*init_output)(libtrace_out_t *libtrace); |
---|
214 | /** configure output traces (or NULL if output not supported) */ |
---|
215 | int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *); |
---|
216 | /** start output traces (or NULL if output not supported) |
---|
217 | * There is no pause for output traces, as packets are not arriving |
---|
218 | * asyncronously |
---|
219 | */ |
---|
220 | int (*start_output)(libtrace_out_t *libtrace); |
---|
221 | /** @} */ |
---|
222 | /** finish an input trace, cleanup (or NULL if input not supported) |
---|
223 | * if the trace is not paused, libtrace will pause the trace before |
---|
224 | * calling this function. |
---|
225 | */ |
---|
226 | int (*fin_input)(libtrace_t *libtrace); |
---|
227 | /** finish an output trace, cleanup (or NULL if output not supported) */ |
---|
228 | int (*fin_output)(libtrace_out_t *libtrace); |
---|
229 | /** read a packet from a trace into the provided packet structure |
---|
230 | * @returns -1 on error, or get_framing_length()+get_capture_length() \ |
---|
231 | * on success. |
---|
232 | * if this function is not supported, this field may be NULL. |
---|
233 | */ |
---|
234 | int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet); |
---|
235 | /** finalise a packet |
---|
236 | * cleanup any resources used by a packet that can't be reused for |
---|
237 | * the next packet. |
---|
238 | */ |
---|
239 | void (*fin_packet)(libtrace_packet_t *packet); |
---|
240 | /** write a packet to a trace from the provided packet |
---|
241 | * (or NULL if output not supported) |
---|
242 | */ |
---|
243 | int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet); |
---|
244 | /** return the libtrace link type for this packet |
---|
245 | * @return the libtrace link type, or -1 if this link type is unknown |
---|
246 | */ |
---|
247 | libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet); |
---|
248 | /** return the direction of this packet |
---|
249 | * @note This callback may be NULL if not supported. |
---|
250 | */ |
---|
251 | libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet); |
---|
252 | /** set the direction of this packet |
---|
253 | * @note This callback may be NULL if not supported. |
---|
254 | */ |
---|
255 | libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction); |
---|
256 | /** return the erf timestamp of the packet. |
---|
257 | * @return the 64bit erf timestamp |
---|
258 | * This field may be NULL in the structure, and libtrace will |
---|
259 | * synthesise the result from get_timeval or get_seconds if they |
---|
260 | * exist. AT least one of get_erf_timestamp, get_timeval or |
---|
261 | * get_seconds must be implemented. |
---|
262 | */ |
---|
263 | uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet); |
---|
264 | /** return the timeval of this packet. |
---|
265 | * @return the timeval |
---|
266 | * This field may be NULL in the structure, and libtrace will |
---|
267 | * synthesise the result from get_erf_timestamp or get_seconds if they |
---|
268 | * exist. AT least one of get_erf_timestamp, get_timeval or |
---|
269 | * get_seconds must be implemented. |
---|
270 | */ |
---|
271 | struct timeval (*get_timeval)(const libtrace_packet_t *packet); |
---|
272 | /** return the timestamp of this packet. |
---|
273 | * @return the floating point seconds since 1970-01-01 00:00:00 |
---|
274 | * This field may be NULL in the structure, and libtrace will |
---|
275 | * synthesise the result from get_timeval or get_erf_timestamp if they |
---|
276 | * exist. AT least one of get_erf_timestamp, get_timeval or |
---|
277 | * get_seconds must be implemented. |
---|
278 | */ |
---|
279 | double (*get_seconds)(const libtrace_packet_t *packet); |
---|
280 | /** move the pointer within the trace. |
---|
281 | * @return 0 on success, -1 on failure. |
---|
282 | * The next packet returned by read_packet be the first |
---|
283 | * packet in the trace to have a timestamp equal or greater than |
---|
284 | * timestamp. |
---|
285 | * @note this function may be NULL if the format does not support |
---|
286 | * this feature. If the format implements seek_timeval and/or |
---|
287 | * seek_seconds then libtrace will call those functions instead. |
---|
288 | */ |
---|
289 | int (*seek_erf)(libtrace_t *trace, uint64_t timestamp); |
---|
290 | /** move the pointer within the trace. |
---|
291 | * @return 0 on success, -1 on failure. |
---|
292 | * The next packet returned by read_packet be the first |
---|
293 | * packet in the trace to have a timestamp equal or greater than |
---|
294 | * timestamp. |
---|
295 | * @note this function may be NULL if the format does not support |
---|
296 | * this feature. If the format implements seek_erf and/or |
---|
297 | * seek_seconds then libtrace will call those functions instead. |
---|
298 | */ |
---|
299 | int (*seek_timeval)(libtrace_t *trace, struct timeval tv); |
---|
300 | /** move the pointer within the trace. |
---|
301 | * @return 0 on success, -1 on failure. |
---|
302 | * The next packet returned by read_packet be the first |
---|
303 | * packet in the trace to have a timestamp equal or greater than |
---|
304 | * tv. |
---|
305 | * @note this function may be NULL if the format does not support |
---|
306 | * this feature. If the format implements seek_erf and/or |
---|
307 | * seek_timeval then libtrace will call those functions instead. |
---|
308 | */ |
---|
309 | int (*seek_seconds)(libtrace_t *trace, double seconds); |
---|
310 | /** return the captured payload length |
---|
311 | * @return the amount of data captured in a trace. |
---|
312 | * This is the number of bytes actually in the trace. This does not |
---|
313 | * include the trace framing length. This is usually shorter or |
---|
314 | * equal to the wire length. |
---|
315 | */ |
---|
316 | int (*get_capture_length)(const libtrace_packet_t *packet); |
---|
317 | /** return the original length of the packet on the wire. |
---|
318 | * @return the length of the packet on the wire before truncation. |
---|
319 | * This is the number of bytes actually in the trace. This does not |
---|
320 | * include the trace framing length. This is usually shorter or |
---|
321 | * equal to the wire length. |
---|
322 | */ |
---|
323 | int (*get_wire_length)(const libtrace_packet_t *packet); |
---|
324 | /** return the length of the trace framing header |
---|
325 | * @return the length of the framing header |
---|
326 | * The framing header is the extra metadata a trace stores about |
---|
327 | * a packet. This does not include the wire or capture length |
---|
328 | * of the packet. Usually get_framing_length()+get_capture_length() |
---|
329 | * is the size returned by read_packet |
---|
330 | */ |
---|
331 | int (*get_framing_length)(const libtrace_packet_t *packet); |
---|
332 | /** truncate (snap) the packet |
---|
333 | * @returns the new size |
---|
334 | * @note This callback may be NULL if not supported. |
---|
335 | */ |
---|
336 | size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size); |
---|
337 | /** return the filedescriptor associated with this interface. |
---|
338 | * @note This callback may be NULL if not supported. |
---|
339 | * This function is only needed if you use trace_event_interface |
---|
340 | * as the pointer for trace_event |
---|
341 | */ |
---|
342 | int (*get_fd)(const libtrace_t *trace); |
---|
343 | /** return the next event from this source |
---|
344 | * @note may be NULL if not supported. |
---|
345 | */ |
---|
346 | struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet); |
---|
347 | /** return information about this trace format to standard out */ |
---|
348 | void (*help)(void); |
---|
349 | /** next pointer, should be NULL */ |
---|
350 | struct libtrace_format_t *next; |
---|
351 | }; |
---|
352 | |
---|
353 | extern struct libtrace_format_t *form; |
---|
354 | |
---|
355 | void register_format(struct libtrace_format_t *format); |
---|
356 | |
---|
357 | libtrace_linktype_t pcap_dlt_to_libtrace(libtrace_dlt_t dlt); |
---|
358 | libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type); |
---|
359 | libtrace_rt_types_t pcap_dlt_to_rt(libtrace_dlt_t dlt); |
---|
360 | libtrace_dlt_t rt_to_pcap_dlt(libtrace_rt_types_t rt_type); |
---|
361 | libtrace_linktype_t erf_type_to_libtrace(char erf); |
---|
362 | char libtrace_to_erf_type(libtrace_linktype_t linktype); |
---|
363 | libtrace_linktype_t arphrd_type_to_libtrace(unsigned int); |
---|
364 | unsigned int libtrace_to_arphrd_type(libtrace_linktype_t); |
---|
365 | |
---|
366 | void promote_packet(libtrace_packet_t *packet); |
---|
367 | bool demote_packet(libtrace_packet_t *packet); |
---|
368 | |
---|
369 | void *trace_get_payload_from_linux_sll(void *, uint16_t *, uint32_t *); |
---|
370 | void *trace_get_payload_from_pos(void *, uint16_t *, uint32_t *); |
---|
371 | void *trace_get_payload_from_atm(void *, uint8_t *, uint32_t *); |
---|
372 | |
---|
373 | uint64_t byteswap64(uint64_t num); |
---|
374 | uint32_t byteswap32(uint32_t num); |
---|
375 | uint16_t byteswap16(uint16_t num); |
---|
376 | |
---|
377 | /* Because some traces/protocols are defined as |
---|
378 | * being "big endian" or "little endian" we have |
---|
379 | * this series of macros. |
---|
380 | */ |
---|
381 | #if BYTE_ORDER == BIG_ENDIAN |
---|
382 | #define bswap_host_to_be64(num) ((uint64_t)(num)) |
---|
383 | #define bswap_host_to_le64(num) byteswap64(num) |
---|
384 | #define bswap_host_to_be32(num) ((uint32_t)(num)) |
---|
385 | #define bswap_host_to_le32(num) byteswap32(num) |
---|
386 | #define bswap_host_to_be16(num) ((uint16_t)(num)) |
---|
387 | #define bswap_host_to_le16(num) byteswap16(num) |
---|
388 | |
---|
389 | #define bswap_be_to_host64(num) ((uint64_t)(num)) |
---|
390 | #define bswap_le_to_host64(num) byteswap64(num) |
---|
391 | #define bswap_be_to_host32(num) ((uint32_t)(num)) |
---|
392 | #define bswap_le_to_host32(num) byteswap32(num) |
---|
393 | #define bswap_be_to_host16(num) ((uint16_t)(num)) |
---|
394 | #define bswap_le_to_host16(num) byteswap16(num) |
---|
395 | |
---|
396 | /* We use ntoh*() here, because the compiler may |
---|
397 | * attempt to optimise it |
---|
398 | */ |
---|
399 | #elif BYTE_ORDER == LITTLE_ENDIAN |
---|
400 | #define bswap_host_to_be64(num) (byteswap64(num)) |
---|
401 | #define bswap_host_to_le64(num) ((uint64_t)(num)) |
---|
402 | #define bswap_host_to_be32(num) (htonl(num)) |
---|
403 | #define bswap_host_to_le32(num) ((uint32_t)(num)) |
---|
404 | #define bswap_host_to_be16(num) (htons(num)) |
---|
405 | #define bswap_host_to_le16(num) ((uint16_t)(num)) |
---|
406 | |
---|
407 | #define bswap_be_to_host64(num) (byteswap64(num)) |
---|
408 | #define bswap_le_to_host64(num) ((uint64_t)(num)) |
---|
409 | #define bswap_be_to_host32(num) (ntohl(num)) |
---|
410 | #define bswap_le_to_host32(num) ((uint32_t)(num)) |
---|
411 | #define bswap_be_to_host16(num) (ntohs(num)) |
---|
412 | #define bswap_le_to_host16(num) ((uint16_t)(num)) |
---|
413 | |
---|
414 | #else |
---|
415 | #error "Unknown byte order" |
---|
416 | #endif |
---|
417 | |
---|
418 | #ifdef HAVE_BPF |
---|
419 | /* A type encapsulating a bpf filter |
---|
420 | * This type covers the compiled bpf filter, as well as the original filter |
---|
421 | * string |
---|
422 | * |
---|
423 | */ |
---|
424 | struct libtrace_filter_t { |
---|
425 | struct bpf_program filter; |
---|
426 | int flag; |
---|
427 | char * filterstring; |
---|
428 | }; |
---|
429 | #else |
---|
430 | struct libtrace_filter_t {}; |
---|
431 | #endif |
---|
432 | |
---|
433 | /** libtrace packet |
---|
434 | */ |
---|
435 | typedef struct libtrace_pcapfile_pkt_hdr_t { |
---|
436 | uint32_t ts_sec; |
---|
437 | uint32_t ts_usec; |
---|
438 | uint32_t caplen; |
---|
439 | uint32_t wirelen; |
---|
440 | } libtrace_pcapfile_pkt_hdr_t; |
---|
441 | |
---|
442 | void erf_constructor(void); |
---|
443 | void legacy_constructor(void); |
---|
444 | void linuxnative_constructor(void); |
---|
445 | void pcap_constructor(void); |
---|
446 | void pcapfile_constructor(void); |
---|
447 | void rt_constructor(void); |
---|
448 | void wag_constructor(void); |
---|
449 | void duck_constructor(void); |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | #ifdef __cplusplus |
---|
454 | } |
---|
455 | #endif |
---|
456 | |
---|
457 | #endif /* LIBTRACE_INT_H */ |
---|