source: lib/format_pktmeta.c @ 4d253ae

develop
Last change on this file since 4d253ae was 4d253ae, checked in by Jacob Van Walraven <jcv9@…>, 21 months ago

Ensure trace_get_meta_option() returns all occurances of the requested option not just the first one found, Add checks to ensure memory is allocated

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