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 "fifo.h" |
---|
63 | #include "rt_protocol.h" |
---|
64 | |
---|
65 | #if HAVE_PCAP_BPF_H |
---|
66 | # include <pcap-bpf.h> |
---|
67 | #else |
---|
68 | # ifdef HAVE_NET_BPF_H |
---|
69 | # include <net/bpf.h> |
---|
70 | # endif |
---|
71 | #endif |
---|
72 | |
---|
73 | #if HAVE_PCAP_H |
---|
74 | # include <pcap.h> |
---|
75 | # ifdef HAVE_PCAP_INT_H |
---|
76 | # include <pcap-int.h> |
---|
77 | # endif |
---|
78 | #endif |
---|
79 | |
---|
80 | #ifdef HAVE_ZLIB_H |
---|
81 | # include <zlib.h> |
---|
82 | #endif |
---|
83 | |
---|
84 | #ifndef HAVE_STRNCASECMP |
---|
85 | # ifndef HAVE__STRNICMP |
---|
86 | int strncasecmp(const char *str1, const char *str2, size_t n); |
---|
87 | # else |
---|
88 | # define strncasecmp _strnicmp |
---|
89 | # endif |
---|
90 | #endif |
---|
91 | |
---|
92 | #ifndef HAVE_SNPRINTF |
---|
93 | # ifndef HAVE_SPRINTF_S |
---|
94 | int snprintf(char *str, size_t size, const char *format, ...); |
---|
95 | # else |
---|
96 | # define snprintf sprintf_s |
---|
97 | # endif |
---|
98 | #endif |
---|
99 | |
---|
100 | #include "wag.h" |
---|
101 | #include "daglegacy.h" |
---|
102 | |
---|
103 | #ifdef HAVE_DAG_API |
---|
104 | # include "dagnew.h" |
---|
105 | # include "dagapi.h" |
---|
106 | # ifndef DAG_VERSION_2_4 |
---|
107 | # include <daginf.h> |
---|
108 | # endif |
---|
109 | #else |
---|
110 | # include "dagformat.h" |
---|
111 | #endif |
---|
112 | |
---|
113 | #define RP_BUFSIZE 65536 |
---|
114 | |
---|
115 | struct libtrace_event_status_t { |
---|
116 | libtrace_packet_t *packet; |
---|
117 | int psize; |
---|
118 | double tdelta; |
---|
119 | double trace_last_ts; |
---|
120 | }; |
---|
121 | |
---|
122 | /** The information about traces that are open |
---|
123 | * @internal |
---|
124 | */ |
---|
125 | struct libtrace_t { |
---|
126 | struct libtrace_format_t *format; /**< format driver pointer */ |
---|
127 | void *format_data; /**<format data pointer */ |
---|
128 | bool started; /**< if this trace has started */ |
---|
129 | libtrace_err_t err; /**< error information */ |
---|
130 | struct libtrace_event_status_t event; /**< the next event */ |
---|
131 | char *uridata; /**< the uri of this trace */ |
---|
132 | struct tracefifo_t *fifo; /**< fifo used in this trace */ |
---|
133 | struct libtrace_filter_t *filter; /**< used by libtrace if the module |
---|
134 | * doesn't support filters natively |
---|
135 | */ |
---|
136 | 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 | #ifndef PF_RULESET_NAME_SIZE |
---|
164 | #define PF_RULESET_NAME_SIZE 16 |
---|
165 | #endif |
---|
166 | |
---|
167 | #ifndef IFNAMSIZ |
---|
168 | #define IFNAMSIZ 16 |
---|
169 | #endif |
---|
170 | |
---|
171 | typedef struct libtrace_pflog_header_t { |
---|
172 | uint8_t length; |
---|
173 | sa_family_t af; |
---|
174 | uint8_t action; |
---|
175 | uint8_t reason; |
---|
176 | char ifname[IFNAMSIZ]; |
---|
177 | char ruleset[PF_RULESET_NAME_SIZE]; |
---|
178 | uint32_t rulenr; |
---|
179 | uint32_t subrulenr; |
---|
180 | uint8_t dir; |
---|
181 | uint8_t pad[3]; |
---|
182 | } libtrace_pflog_header_t; |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | /** Module definition structure */ |
---|
187 | /* all of these should return -1, or NULL on failure */ |
---|
188 | struct libtrace_format_t { |
---|
189 | /** the uri name of this module */ |
---|
190 | char *name; |
---|
191 | /** the version of this module */ |
---|
192 | char *version; |
---|
193 | /** the RT protocol type of this module */ |
---|
194 | enum base_format_t type; |
---|
195 | /** stuff that deals with input @{ */ |
---|
196 | /** initialise an trace (or NULL if input is not supported) */ |
---|
197 | int (*init_input)(libtrace_t *libtrace); |
---|
198 | /** configure an trace (or NULL if input is not supported) */ |
---|
199 | int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value); |
---|
200 | /** start/unpause an trace (or NULL if input not supported) */ |
---|
201 | int (*start_input)(libtrace_t *libtrace); |
---|
202 | /** pause an trace (or NULL if input not supported) */ |
---|
203 | int (*pause_input)(libtrace_t *libtrace); |
---|
204 | /** @} */ |
---|
205 | /** stuff that deals with output @{ */ |
---|
206 | /** initialise output traces (or NULL if output not supported) */ |
---|
207 | int (*init_output)(libtrace_out_t *libtrace); |
---|
208 | /** configure output traces (or NULL if output not supported) */ |
---|
209 | int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *); |
---|
210 | /** start output traces (or NULL if output not supported) |
---|
211 | * There is no pause for output traces, as packets are not arriving |
---|
212 | * asyncronously |
---|
213 | */ |
---|
214 | int (*start_output)(libtrace_out_t *libtrace); |
---|
215 | /** @} */ |
---|
216 | /** finish an input trace, cleanup (or NULL if input not supported) |
---|
217 | * if the trace is not paused, libtrace will pause the trace before |
---|
218 | * calling this function. |
---|
219 | */ |
---|
220 | int (*fin_input)(libtrace_t *libtrace); |
---|
221 | /** finish an output trace, cleanup (or NULL if output not supported) */ |
---|
222 | int (*fin_output)(libtrace_out_t *libtrace); |
---|
223 | /** read a packet from a trace into the provided packet structure |
---|
224 | * @returns -1 on error, or get_framing_length()+get_capture_length() \ |
---|
225 | * on success. |
---|
226 | * if this function is not supported, this field may be NULL. |
---|
227 | */ |
---|
228 | int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet); |
---|
229 | /** finalise a packet |
---|
230 | * cleanup any resources used by a packet that can't be reused for |
---|
231 | * the next packet. |
---|
232 | */ |
---|
233 | void (*fin_packet)(libtrace_packet_t *packet); |
---|
234 | /** write a packet to a trace from the provided packet |
---|
235 | * (or NULL if output not supported) |
---|
236 | */ |
---|
237 | int (*write_packet)(libtrace_out_t *libtrace, const libtrace_packet_t *packet); |
---|
238 | /** return the libtrace link type for this packet |
---|
239 | * @return the libtrace link type, or -1 if this link type is unknown |
---|
240 | */ |
---|
241 | libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet); |
---|
242 | /** return the direction of this packet |
---|
243 | * @note This callback may be NULL if not supported. |
---|
244 | */ |
---|
245 | libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet); |
---|
246 | /** set the direction of this packet |
---|
247 | * @note This callback may be NULL if not supported. |
---|
248 | */ |
---|
249 | libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction); |
---|
250 | /** return the erf timestamp of the packet. |
---|
251 | * @return the 64bit erf timestamp |
---|
252 | * This field may be NULL in the structure, and libtrace will |
---|
253 | * synthesise the result from get_timeval or get_seconds if they |
---|
254 | * exist. AT least one of get_erf_timestamp, get_timeval or |
---|
255 | * get_seconds must be implemented. |
---|
256 | */ |
---|
257 | uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet); |
---|
258 | /** return the timeval of this packet. |
---|
259 | * @return the timeval |
---|
260 | * This field may be NULL in the structure, and libtrace will |
---|
261 | * synthesise the result from get_erf_timestamp or get_seconds if they |
---|
262 | * exist. AT least one of get_erf_timestamp, get_timeval or |
---|
263 | * get_seconds must be implemented. |
---|
264 | */ |
---|
265 | struct timeval (*get_timeval)(const libtrace_packet_t *packet); |
---|
266 | /** return the timestamp of this packet. |
---|
267 | * @return the floating point seconds since 1970-01-01 00:00:00 |
---|
268 | * This field may be NULL in the structure, and libtrace will |
---|
269 | * synthesise the result from get_timeval or get_erf_timestamp if they |
---|
270 | * exist. AT least one of get_erf_timestamp, get_timeval or |
---|
271 | * get_seconds must be implemented. |
---|
272 | */ |
---|
273 | double (*get_seconds)(const libtrace_packet_t *packet); |
---|
274 | /** move the pointer within the trace. |
---|
275 | * @return 0 on success, -1 on failure. |
---|
276 | * The next packet returned by read_packet be the first |
---|
277 | * packet in the trace to have a timestamp equal or greater than |
---|
278 | * timestamp. |
---|
279 | * @note this function may be NULL if the format does not support |
---|
280 | * this feature. If the format implements seek_timeval and/or |
---|
281 | * seek_seconds then libtrace will call those functions instead. |
---|
282 | */ |
---|
283 | int (*seek_erf)(libtrace_t *trace, uint64_t timestamp); |
---|
284 | /** move the pointer within the trace. |
---|
285 | * @return 0 on success, -1 on failure. |
---|
286 | * The next packet returned by read_packet be the first |
---|
287 | * packet in the trace to have a timestamp equal or greater than |
---|
288 | * timestamp. |
---|
289 | * @note this function may be NULL if the format does not support |
---|
290 | * this feature. If the format implements seek_erf and/or |
---|
291 | * seek_seconds then libtrace will call those functions instead. |
---|
292 | */ |
---|
293 | int (*seek_timeval)(libtrace_t *trace, struct timeval tv); |
---|
294 | /** move the pointer within the trace. |
---|
295 | * @return 0 on success, -1 on failure. |
---|
296 | * The next packet returned by read_packet be the first |
---|
297 | * packet in the trace to have a timestamp equal or greater than |
---|
298 | * tv. |
---|
299 | * @note this function may be NULL if the format does not support |
---|
300 | * this feature. If the format implements seek_erf and/or |
---|
301 | * seek_timeval then libtrace will call those functions instead. |
---|
302 | */ |
---|
303 | int (*seek_seconds)(libtrace_t *trace, double seconds); |
---|
304 | /** return the captured payload length |
---|
305 | * @return the amount of data captured in a trace. |
---|
306 | * This is the number of bytes actually in the trace. This does not |
---|
307 | * include the trace framing length. This is usually shorter or |
---|
308 | * equal to the wire length. |
---|
309 | */ |
---|
310 | int (*get_capture_length)(const libtrace_packet_t *packet); |
---|
311 | /** return the original length of the packet on the wire. |
---|
312 | * @return the length of the packet on the wire before truncation. |
---|
313 | * This is the number of bytes actually in the trace. This does not |
---|
314 | * include the trace framing length. This is usually shorter or |
---|
315 | * equal to the wire length. |
---|
316 | */ |
---|
317 | int (*get_wire_length)(const libtrace_packet_t *packet); |
---|
318 | /** return the length of the trace framing header |
---|
319 | * @return the length of the framing header |
---|
320 | * The framing header is the extra metadata a trace stores about |
---|
321 | * a packet. This does not include the wire or capture length |
---|
322 | * of the packet. Usually get_framing_length()+get_capture_length() |
---|
323 | * is the size returned by read_packet |
---|
324 | */ |
---|
325 | int (*get_framing_length)(const libtrace_packet_t *packet); |
---|
326 | /** truncate (snap) the packet |
---|
327 | * @returns the new size |
---|
328 | * @note This callback may be NULL if not supported. |
---|
329 | */ |
---|
330 | size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size); |
---|
331 | /** return the filedescriptor associated with this interface. |
---|
332 | * @note This callback may be NULL if not supported. |
---|
333 | * This function is only needed if you use trace_event_interface |
---|
334 | * as the pointer for trace_event |
---|
335 | */ |
---|
336 | int (*get_fd)(const libtrace_t *trace); |
---|
337 | /** return the next event from this source |
---|
338 | * @note may be NULL if not supported. |
---|
339 | */ |
---|
340 | struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet); |
---|
341 | /** return information about this trace format to standard out */ |
---|
342 | void (*help)(); |
---|
343 | /** next pointer, should be NULL */ |
---|
344 | struct libtrace_format_t *next; |
---|
345 | }; |
---|
346 | |
---|
347 | extern struct libtrace_format_t *form; |
---|
348 | |
---|
349 | void register_format(struct libtrace_format_t *format); |
---|
350 | |
---|
351 | libtrace_linktype_t pcap_dlt_to_libtrace(libtrace_dlt_t dlt); |
---|
352 | libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type); |
---|
353 | enum rt_field_t pcap_dlt_to_rt(libtrace_dlt_t dlt); |
---|
354 | libtrace_dlt_t rt_to_pcap_dlt(enum rt_field_t rt_type); |
---|
355 | libtrace_linktype_t erf_type_to_libtrace(char erf); |
---|
356 | char libtrace_to_erf_type(libtrace_linktype_t linktype); |
---|
357 | |
---|
358 | void promote_packet(libtrace_packet_t *packet); |
---|
359 | bool demote_packet(libtrace_packet_t *packet); |
---|
360 | |
---|
361 | #if HAVE_BPF |
---|
362 | /* A type encapsulating a bpf filter |
---|
363 | * This type covers the compiled bpf filter, as well as the original filter |
---|
364 | * string |
---|
365 | * |
---|
366 | */ |
---|
367 | struct libtrace_filter_t { |
---|
368 | struct bpf_program filter; |
---|
369 | int flag; |
---|
370 | char * filterstring; |
---|
371 | }; |
---|
372 | #endif |
---|
373 | |
---|
374 | #ifdef __cplusplus |
---|
375 | } |
---|
376 | #endif |
---|
377 | |
---|
378 | #endif /* LIBTRACE_INT_H */ |
---|