1 | #include "libtrace_int.h" |
---|
2 | #include "libtrace.h" |
---|
3 | #include "format_erf.h" |
---|
4 | #include "format_pcapng.h" |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <string.h> |
---|
9 | #include <endian.h> |
---|
10 | |
---|
11 | /* Internal Meta functions */ |
---|
12 | static int trace_meta_check_input(libtrace_packet_t *packet, char *input_func) { |
---|
13 | if (packet == NULL) { |
---|
14 | fprintf(stderr, "NULL packet passed into %s\n", input_func); |
---|
15 | return -1; |
---|
16 | } |
---|
17 | if (packet->trace == NULL) { |
---|
18 | fprintf(stderr, "Packet contains NULL trace in %s\n", input_func); |
---|
19 | return -1; |
---|
20 | } |
---|
21 | return 1; |
---|
22 | } |
---|
23 | |
---|
24 | static libtrace_meta_t *trace_meta_set_datatype(libtrace_meta_t *r, meta_datatype_t dt) { |
---|
25 | if (r == NULL) { return NULL; } |
---|
26 | |
---|
27 | int i; |
---|
28 | for (i=0; i<r->num; i++) { |
---|
29 | r->items[i].datatype = dt; |
---|
30 | } |
---|
31 | return r; |
---|
32 | } |
---|
33 | |
---|
34 | /* API functions to retrieve interface related packet data */ |
---|
35 | |
---|
36 | /* Destroy libtrace_meta_t structure |
---|
37 | * |
---|
38 | * @params libtrace_meta_t structure |
---|
39 | * returns 1 on success, -1 on failure |
---|
40 | */ |
---|
41 | int trace_destroy_meta(libtrace_meta_t *result) { |
---|
42 | int i; |
---|
43 | if (!result) { return -1; } |
---|
44 | |
---|
45 | for (i=0;i<result->num;i++) { |
---|
46 | if(result->items[i].data) { |
---|
47 | free(result->items[i].data); |
---|
48 | } |
---|
49 | } |
---|
50 | if (result->items) { |
---|
51 | free(result->items); |
---|
52 | } |
---|
53 | if (result) { |
---|
54 | free(result); |
---|
55 | } |
---|
56 | |
---|
57 | return 1; |
---|
58 | } |
---|
59 | |
---|
60 | /* Get the interface name/s for a meta packet. |
---|
61 | * Must be destroyed with trace_destroy_meta(). |
---|
62 | * |
---|
63 | * @params libtrace_packet_t meta packet. |
---|
64 | * @returns Pointer to libtrace_meta_t structure containing all found interface names |
---|
65 | * or NULL. |
---|
66 | */ |
---|
67 | libtrace_meta_t *trace_get_interface_name_meta(libtrace_packet_t *packet) { |
---|
68 | if (trace_meta_check_input(packet, "trace_get_interface_name()")<0) { |
---|
69 | return NULL; |
---|
70 | } |
---|
71 | |
---|
72 | libtrace_meta_t *r = NULL; |
---|
73 | |
---|
74 | /* get the result */ |
---|
75 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
76 | r = packet->trace->format->get_meta_section_option(packet, |
---|
77 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_NAME); |
---|
78 | } |
---|
79 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
80 | r = packet->trace->format->get_meta_section_option(packet, |
---|
81 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_NAME); |
---|
82 | } |
---|
83 | |
---|
84 | return trace_meta_set_datatype(r, TRACE_META_STRING); |
---|
85 | } |
---|
86 | /* Get the interface name for a meta packet. |
---|
87 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
88 | * specify the interface index. |
---|
89 | * |
---|
90 | * @params libtrace_packet_t meta packet to extract the interface name from. |
---|
91 | * @params A pointer to a character buffer to store the interface name in. |
---|
92 | * @params The size of the buffer passed in. |
---|
93 | * @params The interface index within the meta packet. |
---|
94 | * @returns Pointer to the character buffer containing the interface name or NULL. |
---|
95 | */ |
---|
96 | char *trace_get_interface_name(libtrace_packet_t *packet, char *space, int spacelen, |
---|
97 | int index) { |
---|
98 | |
---|
99 | libtrace_meta_t *r = trace_get_interface_name_meta(packet); |
---|
100 | if (r == NULL) { return NULL; } |
---|
101 | /* If there is not a result for the index return */ |
---|
102 | if (r->num <= index) { return NULL; } |
---|
103 | /* Ensure the supplied memory allocation is enough, if not only fill |
---|
104 | * what we can */ |
---|
105 | if (spacelen > r->items[index].len) { |
---|
106 | memcpy(space, r->items[index].data, r->items[index].len); |
---|
107 | } else { |
---|
108 | memcpy(space, r->items[index].data, spacelen); |
---|
109 | } |
---|
110 | trace_destroy_meta(r); |
---|
111 | return space; |
---|
112 | } |
---|
113 | |
---|
114 | /* Get the interface MAC address/s for a meta packet. |
---|
115 | * Must be destroyed with trace_destroy_meta(). |
---|
116 | * |
---|
117 | * @params libtrace_packet_t meta packet. |
---|
118 | * @returns Pointer to libtrace_meta_t structure containing all found interface mac |
---|
119 | * addresses or NULL. |
---|
120 | */ |
---|
121 | libtrace_meta_t *trace_get_interface_mac_meta(libtrace_packet_t *packet) { |
---|
122 | if (trace_meta_check_input(packet, "trace_get_interface_mac()")<0) { |
---|
123 | return NULL; |
---|
124 | } |
---|
125 | |
---|
126 | libtrace_meta_t *r = NULL; |
---|
127 | |
---|
128 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
129 | r = packet->trace->format->get_meta_section_option(packet, |
---|
130 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_MAC); |
---|
131 | } |
---|
132 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
133 | r = packet->trace->format->get_meta_section_option(packet, |
---|
134 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_MAC); |
---|
135 | } |
---|
136 | |
---|
137 | return trace_meta_set_datatype(r, TRACE_META_STRING); |
---|
138 | } |
---|
139 | /* Get the interface MAC address for a meta packet. |
---|
140 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
141 | * specify the interface index. |
---|
142 | * |
---|
143 | * @params libtrace_packet_t meta packet to extract the MAC address from. |
---|
144 | * @params A pointer to a character buffer to store the MAC address in. |
---|
145 | * @params The size of the buffer passed in. |
---|
146 | * @params The interface index within the meta packet. |
---|
147 | * @returns Pointer to the character buffer containing the MAC address or NULL. |
---|
148 | */ |
---|
149 | char *trace_get_interface_mac(libtrace_packet_t *packet, char *space, int spacelen, |
---|
150 | int index) { |
---|
151 | |
---|
152 | libtrace_meta_t *r = trace_get_interface_mac_meta(packet); |
---|
153 | if (r == NULL) { return NULL; } |
---|
154 | if (index >= r->num) { return NULL; } |
---|
155 | if (r->items[index].len > spacelen) { |
---|
156 | memcpy(space, r->items[index].data, spacelen); |
---|
157 | } else { |
---|
158 | memcpy(space, r->items[index].data, r->items[index].len); |
---|
159 | } |
---|
160 | trace_destroy_meta(r); |
---|
161 | return space; |
---|
162 | } |
---|
163 | |
---|
164 | /* Get the interface speed/s from a meta packet. |
---|
165 | * Must be destroyed with trace_destroy_meta(). |
---|
166 | * |
---|
167 | * @params libtrace_packet_t packet. |
---|
168 | * @returns Pointer to libtrace_meta_t structure containing all found interface |
---|
169 | * speeds or NULL. |
---|
170 | */ |
---|
171 | libtrace_meta_t *trace_get_interface_speed_meta(libtrace_packet_t *packet) { |
---|
172 | if (trace_meta_check_input(packet, "trace_get_interface_speed()")<0) { |
---|
173 | return NULL; |
---|
174 | } |
---|
175 | |
---|
176 | libtrace_meta_t *r = NULL; |
---|
177 | |
---|
178 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
179 | r = packet->trace->format->get_meta_section_option(packet, |
---|
180 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_SPEED); |
---|
181 | } |
---|
182 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
183 | r = packet->trace->format->get_meta_section_option(packet, |
---|
184 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_SPEED); |
---|
185 | } |
---|
186 | |
---|
187 | /* Flip from network to host byte ordering */ |
---|
188 | int i; |
---|
189 | if (r != NULL) { |
---|
190 | for (i=0; i<r->num; i++) { |
---|
191 | uint64_t d = be64toh(*(uint64_t *)r->items[i].data); |
---|
192 | memcpy(r->items[i].data, &d, r->items[i].len); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | return trace_meta_set_datatype(r, TRACE_META_UINT64_T); |
---|
197 | } |
---|
198 | /* Get the interface speed for a meta packet. |
---|
199 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
200 | * specify the interface index. |
---|
201 | * |
---|
202 | * @params libtrace_packet_t meta packet to extract the interface speed from. |
---|
203 | * @params The interface index within the meta packet. |
---|
204 | * @returns uint64_t interface speed or NULL. |
---|
205 | */ |
---|
206 | uint64_t trace_get_interface_speed(libtrace_packet_t *packet, int index) { |
---|
207 | libtrace_meta_t *r = trace_get_interface_speed_meta(packet); |
---|
208 | if (r == NULL) { return 0; } |
---|
209 | /* If the index wanted does not exist return 0 */ |
---|
210 | if (index >= r->num) { return 0; } |
---|
211 | /* Need to check this more ERF reports this in network order */ |
---|
212 | uint64_t data = *(uint64_t *)r->items[index].data; |
---|
213 | trace_destroy_meta(r); |
---|
214 | return data; |
---|
215 | } |
---|
216 | |
---|
217 | /* Get the interface ipv4 address/s for a meta packet. |
---|
218 | * Must be destroyed with trace_destroy_meta(). |
---|
219 | * |
---|
220 | * @params libtrace_packet_t meta packet. |
---|
221 | * @returns Pointer to libtrace_meta_t structure containing all found ipv4 addresses |
---|
222 | * or NULL |
---|
223 | */ |
---|
224 | libtrace_meta_t *trace_get_interface_ipv4_meta(libtrace_packet_t *packet) { |
---|
225 | if (trace_meta_check_input(packet, "trace_get_interface_ip4()")<0) { |
---|
226 | return NULL; |
---|
227 | } |
---|
228 | |
---|
229 | libtrace_meta_t *r = NULL; |
---|
230 | |
---|
231 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
232 | r = packet->trace->format->get_meta_section_option(packet, |
---|
233 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4); |
---|
234 | } |
---|
235 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
236 | r = packet->trace->format->get_meta_section_option(packet, |
---|
237 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4); |
---|
238 | } |
---|
239 | |
---|
240 | /* Flip from network to host byte ordering */ |
---|
241 | if (r != NULL) { |
---|
242 | int i; |
---|
243 | for (i=0; i<r->num; i++) { |
---|
244 | uint32_t d = ntohl(*(uint32_t *)r->items[i].data); |
---|
245 | memcpy(r->items[i].data, &d, r->items[i].len); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | return trace_meta_set_datatype(r, TRACE_META_UINT32_T); |
---|
250 | } |
---|
251 | /* Get the interface ipv4 address for a meta packet. |
---|
252 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
253 | * specify the interface index. |
---|
254 | * |
---|
255 | * @params libtrace_packet_t meta packet to extract the ipv4 address from. |
---|
256 | * @params The interface index within the meta packet. |
---|
257 | * @returns uint32_t ipv4 address or 0. |
---|
258 | */ |
---|
259 | uint32_t trace_get_interface_ipv4(libtrace_packet_t *packet, int index) { |
---|
260 | libtrace_meta_t *r = trace_get_interface_ipv4_meta(packet); |
---|
261 | if (r == NULL) { return 0; } |
---|
262 | if (index >= r->num) { return 0; } |
---|
263 | uint32_t data = *(uint32_t *)r->items[index].data; |
---|
264 | trace_destroy_meta(r); |
---|
265 | return data; |
---|
266 | } |
---|
267 | /* Get the interface ipv4 address string for a meta packet. |
---|
268 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
269 | * specify the interface index. |
---|
270 | * |
---|
271 | * @params libtrace_packet_t meta packet to extract the ipv4 address from. |
---|
272 | * @params A pointer to a character buffer to store the ipv4 address string in. |
---|
273 | * @params The size of the buffer passed in. |
---|
274 | * @params The interface index within the meta packet. |
---|
275 | * @returns Pointer to the character buffer containing the ipv4 address string or NULL. |
---|
276 | */ |
---|
277 | /* UNTESTED */ |
---|
278 | char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, char *space, int spacelen, |
---|
279 | int index) { |
---|
280 | |
---|
281 | uint32_t addr = htonl(trace_get_interface_ipv4(packet, index)); |
---|
282 | if (addr == 0) { return NULL; } |
---|
283 | |
---|
284 | char *addrstr = inet_ntoa(*(struct in_addr *)&addr); |
---|
285 | memcpy(space, addrstr, spacelen); |
---|
286 | return space; |
---|
287 | } |
---|
288 | |
---|
289 | /* Get the interface ipv6 address/s for a meta packet. |
---|
290 | * Must be destroyed with trace_destroy_meta(). |
---|
291 | * |
---|
292 | * @params libtrace_packet_t meta packet. |
---|
293 | * @returns Pointer to libtrace_meta_t structure containing all found ipv6 addresses |
---|
294 | * or NULL. |
---|
295 | */ |
---|
296 | libtrace_meta_t *trace_get_interface_ipv6_meta(libtrace_packet_t *packet) { |
---|
297 | if (trace_meta_check_input(packet, "trace_get_interface_ip6()")<0) { |
---|
298 | return NULL; |
---|
299 | } |
---|
300 | |
---|
301 | libtrace_meta_t *r = NULL; |
---|
302 | |
---|
303 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
304 | r = packet->trace->format->get_meta_section_option(packet, |
---|
305 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4); |
---|
306 | } |
---|
307 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
308 | r = packet->trace->format->get_meta_section_option(packet, |
---|
309 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4); |
---|
310 | } |
---|
311 | |
---|
312 | return trace_meta_set_datatype(r, TRACE_META_UINT128_T); |
---|
313 | } |
---|
314 | /* Get the interface ipv6 address for a meta packet. |
---|
315 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
316 | * specify the interface index. |
---|
317 | * |
---|
318 | * @params libtrace_packet_t meta packet to extract the ipv6 address from. |
---|
319 | * @params A pointer to a character buffer to store the ipv6 address in. |
---|
320 | * @params The size of the buffer passed in. |
---|
321 | * @params The interface index within the meta packet. |
---|
322 | * @returns Pointer to the buffer containing the ipv6 address or NULL. |
---|
323 | */ |
---|
324 | void *trace_get_interface_ipv6(libtrace_packet_t *packet, void *space, int spacelen, |
---|
325 | int index) { |
---|
326 | |
---|
327 | libtrace_meta_t *r = trace_get_interface_ipv6_meta(packet); |
---|
328 | if (r == NULL) { return NULL; } |
---|
329 | if (r->num <= index) { return NULL; } |
---|
330 | if (r->items[index].len > spacelen) { |
---|
331 | memcpy(space, r->items[index].data, spacelen); |
---|
332 | } else { |
---|
333 | memcpy(space, r->items[index].data, r->items[index].len); |
---|
334 | } |
---|
335 | trace_destroy_meta(r); |
---|
336 | return space; |
---|
337 | } |
---|
338 | /* Get the interface ipv6 address string for a meta packet. |
---|
339 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
340 | * specify the interface index. |
---|
341 | * |
---|
342 | * @params libtrace_packet_t meta packet to extract the ipv6 address from. |
---|
343 | * @params A pointer to a character buffer to store the ipv6 address in. |
---|
344 | * @params The size of the buffer passed in. |
---|
345 | * @params The interface index within the meta packet. |
---|
346 | * @returns Pointer to the character buffer containing the ipv6 address string or NULL. |
---|
347 | */ |
---|
348 | /* UNTESTED */ |
---|
349 | char *trace_get_interface_ipv6_string(libtrace_packet_t *packet, char *space, int spacelen, |
---|
350 | int index) { |
---|
351 | |
---|
352 | if (spacelen < INET6_ADDRSTRLEN) { |
---|
353 | return NULL; |
---|
354 | } |
---|
355 | |
---|
356 | void *addr = calloc(1, 16); |
---|
357 | void *r = trace_get_interface_ipv6(packet, addr, 16, index); |
---|
358 | |
---|
359 | if (r == NULL) { |
---|
360 | return NULL; |
---|
361 | } |
---|
362 | |
---|
363 | inet_ntop(AF_INET6, addr, space, INET6_ADDRSTRLEN); |
---|
364 | free(addr); |
---|
365 | |
---|
366 | return space; |
---|
367 | } |
---|
368 | |
---|
369 | |
---|
370 | /* Get the interface description/s for a meta packet. |
---|
371 | * Must be destroyed with trace_destroy_meta(). |
---|
372 | * |
---|
373 | * @params libtrace_packet_t meta packet. |
---|
374 | * @returns Pointer to libtrace_meta_t structure containing all found interface |
---|
375 | * descriptions or NULL. |
---|
376 | */ |
---|
377 | libtrace_meta_t *trace_get_interface_description_meta(libtrace_packet_t *packet) { |
---|
378 | if (trace_meta_check_input(packet, "trace_get_interface_description()")<0) { |
---|
379 | return NULL; |
---|
380 | } |
---|
381 | |
---|
382 | libtrace_meta_t *r = NULL; |
---|
383 | |
---|
384 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
385 | r = packet->trace->format->get_meta_section_option(packet, |
---|
386 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_DESCR); |
---|
387 | } |
---|
388 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
389 | r = packet->trace->format->get_meta_section_option(packet, |
---|
390 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_DESCR); |
---|
391 | } |
---|
392 | |
---|
393 | return trace_meta_set_datatype(r, TRACE_META_STRING); |
---|
394 | } |
---|
395 | /* Get the interface description for a meta packet. |
---|
396 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
397 | * specify the interface index. |
---|
398 | * |
---|
399 | * @params libtrace_packet_t meta packet to extract the interface description from. |
---|
400 | * @params A pointer to a character buffer to store the interface description in. |
---|
401 | * @params The size of the buffer passed in. |
---|
402 | * @params The interface index within the meta packet. |
---|
403 | * @returns Pointer to the character buffer containing the interface description or NULL. |
---|
404 | */ |
---|
405 | char *trace_get_interface_description(libtrace_packet_t *packet, char *space, int spacelen, |
---|
406 | int index) { |
---|
407 | |
---|
408 | libtrace_meta_t *r = trace_get_interface_description_meta(packet); |
---|
409 | if (r == NULL) { return NULL; } |
---|
410 | if (r->num <= index) { return NULL; } |
---|
411 | if (r->items[index].len > spacelen) { |
---|
412 | memcpy(space, r->items[index].data, spacelen); |
---|
413 | } else { |
---|
414 | memcpy(space, r->items[index].data, r->items[index].len); |
---|
415 | } |
---|
416 | trace_destroy_meta(r); |
---|
417 | return space; |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | /* Get the host OS for a meta packet. |
---|
422 | * Must be destroyed with trace_destroy_meta(). |
---|
423 | * |
---|
424 | * @params libtrace_packet_t meta packet. |
---|
425 | * @returns Pointer to libtrace_meta_t structure containing the host OS or NULL. |
---|
426 | */ |
---|
427 | libtrace_meta_t *trace_get_host_os_meta(libtrace_packet_t *packet) { |
---|
428 | if (trace_meta_check_input(packet, "trace_get_host_os()")<0) { |
---|
429 | return NULL; |
---|
430 | } |
---|
431 | |
---|
432 | libtrace_meta_t *r = NULL; |
---|
433 | |
---|
434 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
435 | r = packet->trace->format->get_meta_section_option(packet, |
---|
436 | ERF_PROV_SECTION_HOST, ERF_PROV_OS); |
---|
437 | } |
---|
438 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
439 | r = packet->trace->format->get_meta_section_option(packet, |
---|
440 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_OS); |
---|
441 | } |
---|
442 | |
---|
443 | return trace_meta_set_datatype(r, TRACE_META_STRING); |
---|
444 | } |
---|
445 | /* Get the host OS for a meta packet. |
---|
446 | * |
---|
447 | * @params libtrace_packet_t meta packet to extract the host OS from. |
---|
448 | * @params A pointer to a character buffer to store the host OS in. |
---|
449 | * @params The size of the buffer passed in. |
---|
450 | * @returns Pointer to the character buffer containing the host OS or NULL. |
---|
451 | */ |
---|
452 | char *trace_get_host_os(libtrace_packet_t *packet, char *space, int spacelen) { |
---|
453 | libtrace_meta_t *r = trace_get_host_os_meta(packet); |
---|
454 | if (r == NULL) { return NULL; } |
---|
455 | if (r->items[0].len > spacelen) { |
---|
456 | memcpy(space, r->items[0].data, spacelen); |
---|
457 | } else { |
---|
458 | memcpy(space, r->items[0].data, r->items[0].len); |
---|
459 | } |
---|
460 | trace_destroy_meta(r); |
---|
461 | return space; |
---|
462 | } |
---|
463 | |
---|
464 | /* Get the interface frame check sequence length for a meta packet. |
---|
465 | * Must be destroyed with trace_destroy_meta(). |
---|
466 | * |
---|
467 | * @params libtrace_packet_t meta packet. |
---|
468 | * @returns Pointer to libtrace_meta_t structure containing all found frame check |
---|
469 | * sequence lengths or NULL. |
---|
470 | */ |
---|
471 | libtrace_meta_t *trace_get_interface_fcslen_meta(libtrace_packet_t *packet) { |
---|
472 | if (trace_meta_check_input(packet, "trace_get_interface_frame_check_sequence_length()")<0) { |
---|
473 | return NULL; |
---|
474 | } |
---|
475 | |
---|
476 | libtrace_meta_t *r = NULL; |
---|
477 | |
---|
478 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
479 | r = packet->trace->format->get_meta_section_option(packet, |
---|
480 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_FCS_LEN); |
---|
481 | } |
---|
482 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
483 | r = packet->trace->format->get_meta_section_option(packet, |
---|
484 | PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_FCSLEN); |
---|
485 | } |
---|
486 | |
---|
487 | /* Flip from network to host byte ordering */ |
---|
488 | if (r != NULL) { |
---|
489 | int i; |
---|
490 | for (i=0; i<r->num; i++) { |
---|
491 | uint32_t d = ntohl(*(uint32_t *)r->items[i].data); |
---|
492 | memcpy(r->items[i].data, &d, r->items[i].len); |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | return trace_meta_set_datatype(r, TRACE_META_UINT32_T); |
---|
497 | } |
---|
498 | /* Get the interface frame check sequence length for a meta packet. |
---|
499 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
500 | * specify the interface index. |
---|
501 | * |
---|
502 | * @params libtrace_packet_t meta packet to extract the interface fcslen from. |
---|
503 | * @params The interface index within the meta packet. |
---|
504 | * @returns uint32_t frame check sequence length or 0. |
---|
505 | */ |
---|
506 | uint32_t trace_get_interface_fcslen(libtrace_packet_t *packet, int index) { |
---|
507 | libtrace_meta_t *r = trace_get_interface_fcslen_meta(packet); |
---|
508 | if (r == NULL) { return 0; } |
---|
509 | if (r->num <= index) { return 0; } |
---|
510 | uint32_t data = *(uint32_t *)r->items[index].data; |
---|
511 | trace_destroy_meta(r); |
---|
512 | return data; |
---|
513 | } |
---|
514 | |
---|
515 | /* Get any interface comments for a meta packet |
---|
516 | * Must be destroyed with trace_destroy_meta() |
---|
517 | * |
---|
518 | * @params libtrace_packet_t packet |
---|
519 | * @returns Pointer to libtrace_meta_t structure or NULL |
---|
520 | */ |
---|
521 | libtrace_meta_t *trace_get_interface_comment_meta(libtrace_packet_t *packet) { |
---|
522 | if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) { |
---|
523 | return NULL; |
---|
524 | } |
---|
525 | |
---|
526 | libtrace_meta_t *r = NULL; |
---|
527 | |
---|
528 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
529 | r = packet->trace->format->get_meta_section_option(packet, |
---|
530 | ERF_PROV_SECTION_INTERFACE, ERF_PROV_COMMENT); |
---|
531 | } |
---|
532 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
533 | r = packet->trace->format->get_meta_section_option(packet, |
---|
534 | PCAPNG_INTERFACE_TYPE, PCAPNG_OPTION_COMMENT); |
---|
535 | } |
---|
536 | |
---|
537 | return trace_meta_set_datatype(r, TRACE_META_STRING); |
---|
538 | } |
---|
539 | /* Get the interface comment for a meta packet. |
---|
540 | * Note: ERF packets can contain multiple interfaces per meta packet. Use index to |
---|
541 | * specify the interface ID. |
---|
542 | * |
---|
543 | * @params libtrace_packet_t meta packet to extract the interface comment from. |
---|
544 | * @params A pointer to a character buffer to store the interface description in. |
---|
545 | * @params The size of the buffer passed in. |
---|
546 | * @params The interface number within the meta packet. |
---|
547 | * @returns Pointer to the character buffer containing the hardware description or NULL. |
---|
548 | */ |
---|
549 | char *trace_get_interface_comment(libtrace_packet_t *packet, char *space, int spacelen, |
---|
550 | int index) { |
---|
551 | |
---|
552 | libtrace_meta_t *r = trace_get_interface_comment_meta(packet); |
---|
553 | if (r == NULL) { return NULL; } |
---|
554 | if (index > r->num) { return NULL; } |
---|
555 | if (r->items[index].len > spacelen) { |
---|
556 | memcpy(space, r->items[index].data, spacelen); |
---|
557 | } else { |
---|
558 | memcpy(space, r->items[index].data, r->items[index].len); |
---|
559 | } |
---|
560 | trace_destroy_meta(r); |
---|
561 | return space; |
---|
562 | } |
---|
563 | |
---|
564 | /* Get the capture application for a meta packet |
---|
565 | * Must be destroyed with trace_destroy_meta() |
---|
566 | * |
---|
567 | * @params libtrace_packet_t packet |
---|
568 | * @returns Pointer to libtrace_meta_t structure or NULL |
---|
569 | */ |
---|
570 | libtrace_meta_t *trace_get_capture_application_meta(libtrace_packet_t *packet) { |
---|
571 | if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) { |
---|
572 | return NULL; |
---|
573 | } |
---|
574 | |
---|
575 | libtrace_meta_t *r = NULL; |
---|
576 | |
---|
577 | if (packet->trace->format->type == TRACE_FORMAT_ERF) { |
---|
578 | r = packet->trace->format->get_meta_section_option(packet, |
---|
579 | ERF_PROV_SECTION_CAPTURE, ERF_PROV_APP_NAME); |
---|
580 | } |
---|
581 | if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { |
---|
582 | r = packet->trace->format->get_meta_section_option(packet, |
---|
583 | PCAPNG_SECTION_TYPE, PCAPNG_META_SHB_USERAPPL); |
---|
584 | } |
---|
585 | |
---|
586 | return trace_meta_set_datatype(r, TRACE_META_STRING); |
---|
587 | } |
---|
588 | /* Get the capture application for a meta packet. |
---|
589 | * |
---|
590 | * @params libtrace_packet_t meta packet to extract the application name from. |
---|
591 | * @params A pointer to a character buffer to store the application name in. |
---|
592 | * @params The size of the buffer passed in. |
---|
593 | * @returns Pointer to the character buffer containing the application name or NULL. |
---|
594 | */ |
---|
595 | char *trace_get_capture_application(libtrace_packet_t *packet, char *space, int spacelen) { |
---|
596 | libtrace_meta_t *r = trace_get_capture_application_meta(packet); |
---|
597 | if (r == NULL) { return NULL; } |
---|
598 | if (r->items[0].len > spacelen) { |
---|
599 | memcpy(space, r->items[0].data, spacelen); |
---|
600 | } else { |
---|
601 | memcpy(space, r->items[0].data, r->items[0].len); |
---|
602 | } |
---|
603 | trace_destroy_meta(r); |
---|
604 | return space; |
---|
605 | } |
---|
606 | |
---|
607 | /* Get a meta section option from a meta packet |
---|
608 | * Must be destroyed with trace_destroy_meta() |
---|
609 | * |
---|
610 | * @params libtrace_packet_t packet |
---|
611 | * @params Section code |
---|
612 | * @params Option code |
---|
613 | * @returns Pointer to libtrace_meta_t structure or NULL |
---|
614 | */ |
---|
615 | libtrace_meta_t *trace_get_section_option(libtrace_packet_t *packet, uint32_t section_code, |
---|
616 | uint16_t option_code) { |
---|
617 | |
---|
618 | if (trace_meta_check_input(packet, "trace_get_custom_meta()")<0) { |
---|
619 | return NULL; |
---|
620 | } |
---|
621 | |
---|
622 | return packet->trace->format->get_meta_section_option(packet, |
---|
623 | section_code, option_code); |
---|
624 | } |
---|
625 | |
---|
626 | /* Get a section from a meta packet |
---|
627 | * Must be destroyed with trace_destroy_meta() |
---|
628 | * |
---|
629 | * @params libtrace_packet_t packet |
---|
630 | * @params Section code |
---|
631 | * @returns Pointer to libtrace_meta_t structure or NULL |
---|
632 | */ |
---|
633 | libtrace_meta_t *trace_get_section(libtrace_packet_t *packet, uint32_t section_code) { |
---|
634 | if (trace_meta_check_input(packet, "trace_get_section()")<0) { |
---|
635 | return NULL; |
---|
636 | } |
---|
637 | |
---|
638 | return packet->trace->format->get_meta_section(packet, section_code); |
---|
639 | } |
---|
640 | |
---|
641 | /* ERF specific function */ |
---|
642 | /* Get the DAG card model from a meta packet. |
---|
643 | * |
---|
644 | * @params libtrace_packet_t meta packet to extract the DAG model from. |
---|
645 | * @params A pointer to a character buffer to store the DAG model in. |
---|
646 | * @params The size of the buffer passed in. |
---|
647 | * @returns Pointer to the character buffer containing the DAG model or NULL. |
---|
648 | */ |
---|
649 | char *trace_get_erf_dag_card_model(libtrace_packet_t *packet, char *space, int spacelen) { |
---|
650 | libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE, |
---|
651 | ERF_PROV_MODEL); |
---|
652 | |
---|
653 | if (r == NULL) { return NULL; } |
---|
654 | if (r->items[0].len > spacelen) { |
---|
655 | memcpy(space, r->items[0].data, spacelen); |
---|
656 | } else { |
---|
657 | memcpy(space, r->items[0].data, r->items[0].len); |
---|
658 | } |
---|
659 | trace_destroy_meta(r); |
---|
660 | return space; |
---|
661 | } |
---|
662 | /* Get the host DAG software version for a meta packet. |
---|
663 | * |
---|
664 | * @params libtrace_packet_t meta packet to extract the hosts DAG verion from. |
---|
665 | * @params A pointer to a character buffer to store the DAG version in. |
---|
666 | * @params The size of the buffer passed in. |
---|
667 | * @returns Pointer to the character buffer containing the DAG version or NULL. |
---|
668 | */ |
---|
669 | char *trace_get_erf_dag_version(libtrace_packet_t *packet, char *space, int spacelen) { |
---|
670 | libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE, |
---|
671 | ERF_PROV_DAG_VERSION); |
---|
672 | |
---|
673 | if (r == NULL) { return NULL; } |
---|
674 | |
---|
675 | if (r->items[0].len > spacelen) { |
---|
676 | memcpy(space, r->items[0].data, spacelen); |
---|
677 | } else { |
---|
678 | memcpy(space, r->items[0].data, r->items[0].len); |
---|
679 | } |
---|
680 | trace_destroy_meta(r); |
---|
681 | return space; |
---|
682 | } |
---|
683 | /* Get the firmware version for a DAG module from a meta packet. |
---|
684 | * |
---|
685 | * @params libtrace_packet_t meta packet to extract the FW version from. |
---|
686 | * @params A pointer to a character buffer to store the FW version in. |
---|
687 | * @params The size of the buffer passed in. |
---|
688 | * @returns Pointer to the character buffer containing the FW version or NULL. |
---|
689 | */ |
---|
690 | char *trace_get_erf_dag_fw_version(libtrace_packet_t *packet, char *space, int spacelen) { |
---|
691 | libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE, |
---|
692 | ERF_PROV_FW_VERSION); |
---|
693 | |
---|
694 | if (r == NULL) { return NULL; } |
---|
695 | |
---|
696 | if (r->items[0].len > spacelen) { |
---|
697 | memcpy(space, r->items[0].data, spacelen); |
---|
698 | } else { |
---|
699 | memcpy(space, r->items[0].data, r->items[0].len); |
---|
700 | } |
---|
701 | trace_destroy_meta(r); |
---|
702 | return space; |
---|
703 | } |
---|
704 | |
---|