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