source: lib/format_pktmeta.c @ e0f2dca

develop
Last change on this file since e0f2dca was e0f2dca, checked in by Jacob Van Walraven <jcv9@…>, 2 years ago

Fix posible memory leaks, Add missing TRACE_ERR_OUT_OF_MEMORY err code

  • Property mode set to 100644
File size: 23.8 KB
Line 
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 */
11static 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 */
30int 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
49libtrace_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 */
129libtrace_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 */
156char *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        } else {
171                memcpy(space, r->items[index].data, spacelen);
172        }
173        trace_destroy_meta(r);
174        return space;
175}
176
177/* Get the interface MAC address/s for a meta packet.
178 * Must be destroyed with trace_destroy_meta().
179 *
180 * @params libtrace_packet_t meta packet.
181 * @returns Pointer to libtrace_meta_t structure containing all found interface mac
182 * addresses or NULL.
183 */
184libtrace_meta_t *trace_get_interface_mac_meta(libtrace_packet_t *packet) {
185        if (trace_meta_check_input(packet, "trace_get_interface_mac()")<0) {
186                return NULL;
187        }
188
189        libtrace_meta_t *r = NULL;
190
191        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
192                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_MAC);
193        }
194        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
195                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_MAC);
196        }
197
198        return r;
199}
200/* Get the interface MAC address for a meta packet.
201 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
202 * specify the interface index.
203 *
204 * @params libtrace_packet_t meta packet to extract the MAC address from.
205 * @params A pointer to a character buffer to store the MAC address in.
206 * @params The size of the buffer passed in.
207 * @params The interface index within the meta packet.
208 * @returns Pointer to the character buffer containing the MAC address or NULL.
209 */
210char *trace_get_interface_mac(libtrace_packet_t *packet, char *space, int spacelen,
211        int index) {
212
213        libtrace_meta_t *r = trace_get_interface_mac_meta(packet);
214        if (r == NULL) { return NULL; }
215        if (index >= r->num) {
216                trace_destroy_meta(r);
217                return NULL;
218        }
219        if (r->items[index].len > spacelen) {
220                memcpy(space, r->items[index].data, spacelen);
221        } else {
222                memcpy(space, r->items[index].data, r->items[index].len);
223        }
224        trace_destroy_meta(r);
225        return space;
226}
227
228/* Get the interface speed/s from a meta packet.
229 * Must be destroyed with trace_destroy_meta().
230 *
231 * @params libtrace_packet_t packet.
232 * @returns Pointer to libtrace_meta_t structure containing all found interface
233 * speeds or NULL.
234 */
235libtrace_meta_t *trace_get_interface_speed_meta(libtrace_packet_t *packet) {
236        if (trace_meta_check_input(packet, "trace_get_interface_speed()")<0) {
237                return NULL;
238        }
239
240        libtrace_meta_t *r = NULL;
241
242        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
243                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_SPEED);
244        }
245        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
246                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_SPEED);
247        }
248
249        return r;
250}
251/* Get the interface speed 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 interface speed from.
256 * @params The interface index within the meta packet.
257 * @returns uint64_t interface speed or NULL.
258 */
259uint64_t trace_get_interface_speed(libtrace_packet_t *packet, int index) {
260        libtrace_meta_t *r = trace_get_interface_speed_meta(packet);
261        if (r == NULL) { return 0; }
262        /* If the index wanted does not exist return 0 */
263        if (index >= r->num) {
264                trace_destroy_meta(r);
265                return 0;
266        }
267        /* Need to check this more ERF reports this in network order */
268        uint64_t data = *(uint64_t *)r->items[index].data;
269        trace_destroy_meta(r);
270        return data;
271}
272
273/* Get the interface ipv4 address/s for a meta packet.
274 * Must be destroyed with trace_destroy_meta().
275 *
276 * @params libtrace_packet_t meta packet.
277 * @returns Pointer to libtrace_meta_t structure containing all found ipv4 addresses
278 * or NULL
279 */
280libtrace_meta_t *trace_get_interface_ipv4_meta(libtrace_packet_t *packet) {
281        if (trace_meta_check_input(packet, "trace_get_interface_ip4()")<0) {
282                return NULL;
283        }
284
285        libtrace_meta_t *r = NULL;
286
287        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
288                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
289        }
290        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
291                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
292        }
293
294        return r;
295}
296/* Get the interface ipv4 address for a meta packet.
297 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
298 * specify the interface index.
299 *
300 * @params libtrace_packet_t meta packet to extract the ipv4 address from.
301 * @params The interface index within the meta packet.
302 * @returns uint32_t ipv4 address or 0.
303 */
304uint32_t trace_get_interface_ipv4(libtrace_packet_t *packet, int index) {
305        libtrace_meta_t *r = trace_get_interface_ipv4_meta(packet);
306        if (r == NULL) { return 0; }
307        if (index >= r->num) {
308                trace_destroy_meta(r);
309                return 0;
310        }
311        uint32_t data = *(uint32_t *)r->items[index].data;
312        trace_destroy_meta(r);
313        return data;
314}
315/* Get the interface ipv4 address string for a meta packet.
316 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
317 * specify the interface index.
318 *
319 * @params libtrace_packet_t meta packet to extract the ipv4 address from.
320 * @params A pointer to a character buffer to store the ipv4 address string in.
321 * @params The size of the buffer passed in.
322 * @params The interface index within the meta packet.
323 * @returns Pointer to the character buffer containing the ipv4 address string or NULL.
324 */
325/* UNTESTED */
326char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, char *space, int spacelen,
327        int index) {
328
329        uint32_t addr = htonl(trace_get_interface_ipv4(packet, index));
330        if (addr == 0) { return NULL; }
331
332        char *addrstr = inet_ntoa(*(struct in_addr *)&addr);
333        memcpy(space, addrstr, spacelen);
334        return space;
335}
336
337/* Get the interface ipv6 address/s for a meta packet.
338 * Must be destroyed with trace_destroy_meta().
339 *
340 * @params libtrace_packet_t meta packet.
341 * @returns Pointer to libtrace_meta_t structure containing all found ipv6 addresses
342 * or NULL.
343 */
344libtrace_meta_t *trace_get_interface_ipv6_meta(libtrace_packet_t *packet) {
345        if (trace_meta_check_input(packet, "trace_get_interface_ip6()")<0) {
346                return NULL;
347        }
348
349        libtrace_meta_t *r = NULL;
350
351        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
352                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
353        }
354        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
355                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
356        }
357
358        return r;
359}
360/* Get the interface ipv6 address for a meta packet.
361 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
362 * specify the interface index.
363 *
364 * @params libtrace_packet_t meta packet to extract the ipv6 address from.
365 * @params A pointer to a character buffer to store the ipv6 address in.
366 * @params The size of the buffer passed in.
367 * @params The interface index within the meta packet.
368 * @returns Pointer to the buffer containing the ipv6 address or NULL.
369 */
370void *trace_get_interface_ipv6(libtrace_packet_t *packet, void *space, int spacelen,
371        int index) {
372
373        libtrace_meta_t *r = trace_get_interface_ipv6_meta(packet);
374        if (r == NULL) { return NULL; }
375        if (r->num <= index) {
376                trace_destroy_meta(r);
377                return NULL;
378        }
379        if (r->items[index].len > spacelen) {
380                memcpy(space, r->items[index].data, spacelen);
381        } else {
382                memcpy(space, r->items[index].data, r->items[index].len);
383        }
384        trace_destroy_meta(r);
385        return space;
386}
387/* Get the interface ipv6 address string for a meta packet.
388 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
389 * specify the interface index.
390 *
391 * @params libtrace_packet_t meta packet to extract the ipv6 address from.
392 * @params A pointer to a character buffer to store the ipv6 address in.
393 * @params The size of the buffer passed in.
394 * @params The interface index within the meta packet.
395 * @returns Pointer to the character buffer containing the ipv6 address string or NULL.
396 */
397/* UNTESTED */
398char *trace_get_interface_ipv6_string(libtrace_packet_t *packet, char *space, int spacelen,
399        int index) {
400
401        if (spacelen < INET6_ADDRSTRLEN) {
402                return NULL;
403        }
404
405        void *addr = calloc(1, 16);
406        void *r = trace_get_interface_ipv6(packet, addr, 16, index);
407
408        if (r == NULL) {
409                return NULL;
410        }
411
412        inet_ntop(AF_INET6, addr, space, INET6_ADDRSTRLEN);
413        free(addr);
414
415        return space;
416}
417
418
419/* Get the interface description/s for a meta packet.
420 * Must be destroyed with trace_destroy_meta().
421 *
422 * @params libtrace_packet_t meta packet.
423 * @returns Pointer to libtrace_meta_t structure containing all found interface
424 * descriptions or NULL.
425 */
426libtrace_meta_t *trace_get_interface_description_meta(libtrace_packet_t *packet) {
427        if (trace_meta_check_input(packet, "trace_get_interface_description()")<0) {
428                return NULL;
429        }
430
431        libtrace_meta_t *r = NULL;
432
433        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
434                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_DESCR);
435        }
436        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
437                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_DESCR);
438        }
439
440        return r;
441}
442/* Get the interface description for a meta packet.
443 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
444 * specify the interface index.
445 *
446 * @params libtrace_packet_t meta packet to extract the interface description from.
447 * @params A pointer to a character buffer to store the interface description in.
448 * @params The size of the buffer passed in.
449 * @params The interface index within the meta packet.
450 * @returns Pointer to the character buffer containing the interface description or NULL.
451 */
452char *trace_get_interface_description(libtrace_packet_t *packet, char *space, int spacelen,
453        int index) {
454
455        libtrace_meta_t *r = trace_get_interface_description_meta(packet);
456        if (r == NULL) { return NULL; }
457        if (r->num <= index) {
458                trace_destroy_meta(r);
459                return NULL;
460        }
461        if (r->items[index].len > spacelen) {
462                memcpy(space, r->items[index].data, spacelen);
463        } else {
464                memcpy(space, r->items[index].data, r->items[index].len);
465        }
466        trace_destroy_meta(r);
467        return space;
468}
469
470
471/* Get the host OS for a meta packet.
472 * Must be destroyed with trace_destroy_meta().
473 *
474 * @params libtrace_packet_t meta packet.
475 * @returns Pointer to libtrace_meta_t structure containing the host OS or NULL.
476 */
477libtrace_meta_t *trace_get_host_os_meta(libtrace_packet_t *packet) {
478        if (trace_meta_check_input(packet, "trace_get_host_os()")<0) {
479                return NULL;
480        }
481
482        libtrace_meta_t *r = NULL;
483
484        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
485                r = trace_get_meta_option(packet, ERF_PROV_SECTION_HOST, ERF_PROV_OS);
486        }
487        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
488                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_OS);
489        }
490
491        return r;
492}
493/* Get the host OS for a meta packet.
494 *
495 * @params libtrace_packet_t meta packet to extract the host OS from.
496 * @params A pointer to a character buffer to store the host OS in.
497 * @params The size of the buffer passed in.
498 * @returns Pointer to the character buffer containing the host OS or NULL.
499 */
500char *trace_get_host_os(libtrace_packet_t *packet, char *space, int spacelen) {
501        libtrace_meta_t *r = trace_get_host_os_meta(packet);
502        if (r == NULL) { return NULL; }
503        if (r->items[0].len > spacelen) {
504                memcpy(space, r->items[0].data, spacelen);
505        } else {
506                memcpy(space, r->items[0].data, r->items[0].len);
507        }
508        trace_destroy_meta(r);
509        return space;
510}
511
512/* Get the interface frame check sequence length for a meta packet.
513 * Must be destroyed with trace_destroy_meta().
514 *
515 * @params libtrace_packet_t meta packet.
516 * @returns Pointer to libtrace_meta_t structure containing all found frame check
517 * sequence lengths or NULL.
518 */
519libtrace_meta_t *trace_get_interface_fcslen_meta(libtrace_packet_t *packet) {
520        if (trace_meta_check_input(packet, "trace_get_interface_frame_check_sequence_length()")<0) {
521                return NULL;
522        }
523
524        libtrace_meta_t *r = NULL;
525
526        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
527                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_FCS_LEN);
528        }
529        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
530                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_FCSLEN);
531        }
532
533        return r;
534}
535/* Get the interface frame check sequence length for a meta packet.
536 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
537 * specify the interface index.
538 *
539 * @params libtrace_packet_t meta packet to extract the interface fcslen from.
540 * @params The interface index within the meta packet.
541 * @returns uint32_t frame check sequence length or 0.
542 */
543uint32_t trace_get_interface_fcslen(libtrace_packet_t *packet, int index) {
544        libtrace_meta_t *r = trace_get_interface_fcslen_meta(packet);
545        if (r == NULL) { return 0; }
546        if (r->num <= index) {
547                trace_destroy_meta(r);
548                return 0;
549        }
550        uint32_t data = *(uint32_t *)r->items[index].data;
551        trace_destroy_meta(r);
552        return data;
553}
554
555/* Get any interface comments for a meta packet
556 * Must be destroyed with trace_destroy_meta()
557 *
558 * @params libtrace_packet_t packet
559 * @returns Pointer to libtrace_meta_t structure or NULL
560 */
561libtrace_meta_t *trace_get_interface_comment_meta(libtrace_packet_t *packet) {
562        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
563                return NULL;
564        }
565
566        libtrace_meta_t *r = NULL;
567
568        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
569                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_COMMENT);
570        }
571        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
572                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_OPTION_COMMENT);
573        }
574
575        return r;
576}
577/* Get the interface comment for a meta packet.
578 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
579 * specify the interface ID.
580 *
581 * @params libtrace_packet_t meta packet to extract the interface comment from.
582 * @params A pointer to a character buffer to store the interface description in.
583 * @params The size of the buffer passed in.
584 * @params The interface number within the meta packet.
585 * @returns Pointer to the character buffer containing the hardware description or NULL.
586 */
587char *trace_get_interface_comment(libtrace_packet_t *packet, char *space, int spacelen,
588        int index) {
589
590        libtrace_meta_t *r = trace_get_interface_comment_meta(packet);
591        if (r == NULL) { return NULL; }
592        if (index > r->num) {
593                trace_destroy_meta(r);
594                return NULL;
595        }
596        if (r->items[index].len > spacelen) {
597                memcpy(space, r->items[index].data, spacelen);
598        } else {
599                memcpy(space, r->items[index].data, r->items[index].len);
600        }
601        trace_destroy_meta(r);
602        return space;
603}
604
605/* Get the capture application for a meta packet
606 * Must be destroyed with trace_destroy_meta()
607 *
608 * @params libtrace_packet_t packet
609 * @returns Pointer to libtrace_meta_t structure or NULL
610 */
611libtrace_meta_t *trace_get_capture_application_meta(libtrace_packet_t *packet) {
612        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
613                return NULL;
614        }
615
616        libtrace_meta_t *r = NULL;
617
618        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
619                r = trace_get_meta_option(packet, ERF_PROV_SECTION_CAPTURE, ERF_PROV_APP_NAME);
620        }
621        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
622                r = trace_get_meta_option(packet, PCAPNG_SECTION_TYPE, PCAPNG_META_SHB_USERAPPL);
623        }
624
625        return r;
626}
627/* Get the capture application for a meta packet.
628 *
629 * @params libtrace_packet_t meta packet to extract the application name from.
630 * @params A pointer to a character buffer to store the application name in.
631 * @params The size of the buffer passed in.
632 * @returns Pointer to the character buffer containing the application name or NULL.
633 */
634char *trace_get_capture_application(libtrace_packet_t *packet, char *space, int spacelen) {
635        libtrace_meta_t *r = trace_get_capture_application_meta(packet);
636        if (r == NULL) { return NULL; }
637        if (r->items[0].len > spacelen) {
638                memcpy(space, r->items[0].data, spacelen);
639        } else {
640                memcpy(space, r->items[0].data, r->items[0].len);
641        }
642        trace_destroy_meta(r);
643        return space;
644}
645
646/* Get a meta section option from a meta packet
647 * Must be destroyed with trace_destroy_meta()
648 *
649 * @params libtrace_packet_t packet
650 * @params Section code
651 * @params Option code
652 * @returns Pointer to libtrace_meta_t structure or NULL
653 */
654libtrace_meta_t *trace_get_section_option(libtrace_packet_t *packet, uint32_t section_code,
655        uint16_t option_code) {
656
657        if (trace_meta_check_input(packet, "trace_get_custom_meta()")<0) {
658                return NULL;
659        }
660
661        return trace_get_meta_option(packet, section_code, option_code);
662}
663
664/* Get a section from a meta packet
665 * Must be destroyed with trace_destroy_meta()
666 *
667 * @params libtrace_packet_t packet
668 * @params Section code
669 * @returns Pointer to libtrace_meta_t structure or NULL
670 */
671libtrace_meta_t *trace_get_section(libtrace_packet_t *packet, uint32_t section_code) {
672        if (trace_meta_check_input(packet, "trace_get_section()")<0) {
673                return NULL;
674        }
675
676        return packet->trace->format->get_meta_section(packet, section_code);
677}
678
679/* ERF specific function */
680/* Get the DAG card model from a meta packet.
681 *
682 * @params libtrace_packet_t meta packet to extract the DAG model from.
683 * @params A pointer to a character buffer to store the DAG model in.
684 * @params The size of the buffer passed in.
685 * @returns Pointer to the character buffer containing the DAG model or NULL.
686 */
687char *trace_get_erf_dag_card_model(libtrace_packet_t *packet, char *space, int spacelen) {
688        libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE,
689                ERF_PROV_MODEL);
690
691        if (r == NULL) { return NULL; }
692        if (r->items[0].len > spacelen) {
693                memcpy(space, r->items[0].data, spacelen);
694        } else {
695                memcpy(space, r->items[0].data, r->items[0].len);
696        }
697        trace_destroy_meta(r);
698        return space;
699}
700/* Get the host DAG software version for a meta packet.
701 *
702 * @params libtrace_packet_t meta packet to extract the hosts DAG verion from.
703 * @params A pointer to a character buffer to store the DAG version in.
704 * @params The size of the buffer passed in.
705 * @returns Pointer to the character buffer containing the DAG version or NULL.
706 */
707char *trace_get_erf_dag_version(libtrace_packet_t *packet, char *space, int spacelen) {
708        libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE,
709                ERF_PROV_DAG_VERSION);
710
711        if (r == NULL) { return NULL; }
712
713        if (r->items[0].len > spacelen) {
714                memcpy(space, r->items[0].data, spacelen);
715        } else {
716                memcpy(space, r->items[0].data, r->items[0].len);
717        }
718        trace_destroy_meta(r);
719        return space;
720}
721/* Get the firmware version for a DAG module from a meta packet.
722 *
723 * @params libtrace_packet_t meta packet to extract the FW version from.
724 * @params A pointer to a character buffer to store the FW version in.
725 * @params The size of the buffer passed in.
726 * @returns Pointer to the character buffer containing the FW version or NULL.
727 */
728char *trace_get_erf_dag_fw_version(libtrace_packet_t *packet, char *space, int spacelen) {
729        libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE,
730                ERF_PROV_FW_VERSION);
731
732        if (r == NULL) { return NULL; }
733
734        if (r->items[0].len > spacelen) {
735                memcpy(space, r->items[0].data, spacelen);
736        } else {
737                memcpy(space, r->items[0].data, r->items[0].len);
738        }
739        trace_destroy_meta(r);
740        return space;
741}
742
Note: See TracBrowser for help on using the repository browser.