source: lib/format_pktmeta.c @ a0e5b65

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

NULL terminate all strings returned from the meta API

  • Property mode set to 100644
File size: 24.4 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                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 */
186libtrace_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 */
212char *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 */
239libtrace_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 */
263uint64_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 */
284libtrace_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 */
308uint32_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 A pointer to a character buffer to store the ipv4 address string in.
325 * @params The size of the buffer passed in.
326 * @params The interface index within the meta packet.
327 * @returns Pointer to the character buffer containing the ipv4 address string or NULL.
328 */
329/* UNTESTED */
330char *trace_get_interface_ipv4_string(libtrace_packet_t *packet, char *space, int spacelen,
331        int index) {
332
333        uint32_t addr = htonl(trace_get_interface_ipv4(packet, index));
334        if (addr == 0) { return NULL; }
335
336        char *addrstr = inet_ntoa(*(struct in_addr *)&addr);
337        memcpy(space, addrstr, spacelen);
338        space[spacelen] = '\0';
339        return space;
340}
341
342/* Get the interface ipv6 address/s for a meta packet.
343 * Must be destroyed with trace_destroy_meta().
344 *
345 * @params libtrace_packet_t meta packet.
346 * @returns Pointer to libtrace_meta_t structure containing all found ipv6 addresses
347 * or NULL.
348 */
349libtrace_meta_t *trace_get_interface_ipv6_meta(libtrace_packet_t *packet) {
350        if (trace_meta_check_input(packet, "trace_get_interface_ip6()")<0) {
351                return NULL;
352        }
353
354        libtrace_meta_t *r = NULL;
355
356        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
357                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
358        }
359        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
360                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
361        }
362
363        return r;
364}
365/* Get the interface ipv6 address for a meta packet.
366 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
367 * specify the interface index.
368 *
369 * @params libtrace_packet_t meta packet to extract the ipv6 address from.
370 * @params A pointer to a character buffer to store the ipv6 address in.
371 * @params The size of the buffer passed in.
372 * @params The interface index within the meta packet.
373 * @returns Pointer to the buffer containing the ipv6 address or NULL.
374 */
375void *trace_get_interface_ipv6(libtrace_packet_t *packet, void *space, int spacelen,
376        int index) {
377
378        libtrace_meta_t *r = trace_get_interface_ipv6_meta(packet);
379        if (r == NULL) { return NULL; }
380        if (r->num <= index) {
381                trace_destroy_meta(r);
382                return NULL;
383        }
384        if (r->items[index].len > spacelen) {
385                memcpy(space, r->items[index].data, spacelen);
386        } else {
387                memcpy(space, r->items[index].data, r->items[index].len);
388        }
389        trace_destroy_meta(r);
390        return space;
391}
392/* Get the interface ipv6 address string for a meta packet.
393 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
394 * specify the interface index.
395 *
396 * @params libtrace_packet_t meta packet to extract the ipv6 address from.
397 * @params A pointer to a character buffer to store the ipv6 address in.
398 * @params The size of the buffer passed in.
399 * @params The interface index within the meta packet.
400 * @returns Pointer to the character buffer containing the ipv6 address string or NULL.
401 */
402/* UNTESTED */
403char *trace_get_interface_ipv6_string(libtrace_packet_t *packet, char *space, int spacelen,
404        int index) {
405
406        if (spacelen < INET6_ADDRSTRLEN) {
407                return NULL;
408        }
409
410        void *addr = calloc(1, 16);
411        void *r = trace_get_interface_ipv6(packet, addr, 16, index);
412
413        if (r == NULL) {
414                return NULL;
415        }
416
417        inet_ntop(AF_INET6, addr, space, INET6_ADDRSTRLEN);
418        free(addr);
419
420        return space;
421}
422
423
424/* Get the interface description/s for a meta packet.
425 * Must be destroyed with trace_destroy_meta().
426 *
427 * @params libtrace_packet_t meta packet.
428 * @returns Pointer to libtrace_meta_t structure containing all found interface
429 * descriptions or NULL.
430 */
431libtrace_meta_t *trace_get_interface_description_meta(libtrace_packet_t *packet) {
432        if (trace_meta_check_input(packet, "trace_get_interface_description()")<0) {
433                return NULL;
434        }
435
436        libtrace_meta_t *r = NULL;
437
438        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
439                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_DESCR);
440        }
441        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
442                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_DESCR);
443        }
444
445        return r;
446}
447/* Get the interface description for a meta packet.
448 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
449 * specify the interface index.
450 *
451 * @params libtrace_packet_t meta packet to extract the interface description from.
452 * @params A pointer to a character buffer to store the interface description in.
453 * @params The size of the buffer passed in.
454 * @params The interface index within the meta packet.
455 * @returns Pointer to the character buffer containing the interface description or NULL.
456 */
457char *trace_get_interface_description(libtrace_packet_t *packet, char *space, int spacelen,
458        int index) {
459
460        libtrace_meta_t *r = trace_get_interface_description_meta(packet);
461        if (r == NULL) { return NULL; }
462        if (r->num <= index) {
463                trace_destroy_meta(r);
464                return NULL;
465        }
466        if (r->items[index].len > spacelen) {
467                memcpy(space, r->items[index].data, spacelen);
468                space[spacelen] = '\0';
469        } else {
470                memcpy(space, r->items[index].data, r->items[index].len);
471                space[r->items[index].len] = '\0';
472        }
473        trace_destroy_meta(r);
474        return space;
475}
476
477
478/* Get the host OS for a meta packet.
479 * Must be destroyed with trace_destroy_meta().
480 *
481 * @params libtrace_packet_t meta packet.
482 * @returns Pointer to libtrace_meta_t structure containing the host OS or NULL.
483 */
484libtrace_meta_t *trace_get_host_os_meta(libtrace_packet_t *packet) {
485        if (trace_meta_check_input(packet, "trace_get_host_os()")<0) {
486                return NULL;
487        }
488
489        libtrace_meta_t *r = NULL;
490
491        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
492                r = trace_get_meta_option(packet, ERF_PROV_SECTION_HOST, ERF_PROV_OS);
493        }
494        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
495                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_OS);
496        }
497
498        return r;
499}
500/* Get the host OS for a meta packet.
501 *
502 * @params libtrace_packet_t meta packet to extract the host OS from.
503 * @params A pointer to a character buffer to store the host OS in.
504 * @params The size of the buffer passed in.
505 * @returns Pointer to the character buffer containing the host OS or NULL.
506 */
507char *trace_get_host_os(libtrace_packet_t *packet, char *space, int spacelen) {
508        libtrace_meta_t *r = trace_get_host_os_meta(packet);
509        if (r == NULL) { return NULL; }
510        if (r->items[0].len > spacelen) {
511                memcpy(space, r->items[0].data, spacelen);
512                space[spacelen] = '\0';
513        } else {
514                memcpy(space, r->items[0].data, r->items[0].len);
515                space[r->items[0].len] = '\0';
516        }
517        trace_destroy_meta(r);
518        return space;
519}
520
521/* Get the interface frame check sequence length for a meta packet.
522 * Must be destroyed with trace_destroy_meta().
523 *
524 * @params libtrace_packet_t meta packet.
525 * @returns Pointer to libtrace_meta_t structure containing all found frame check
526 * sequence lengths or NULL.
527 */
528libtrace_meta_t *trace_get_interface_fcslen_meta(libtrace_packet_t *packet) {
529        if (trace_meta_check_input(packet, "trace_get_interface_frame_check_sequence_length()")<0) {
530                return NULL;
531        }
532
533        libtrace_meta_t *r = NULL;
534
535        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
536                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_FCS_LEN);
537        }
538        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
539                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_FCSLEN);
540        }
541
542        return r;
543}
544/* Get the interface frame check sequence length for a meta packet.
545 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
546 * specify the interface index.
547 *
548 * @params libtrace_packet_t meta packet to extract the interface fcslen from.
549 * @params The interface index within the meta packet.
550 * @returns uint32_t frame check sequence length or 0.
551 */
552uint32_t trace_get_interface_fcslen(libtrace_packet_t *packet, int index) {
553        libtrace_meta_t *r = trace_get_interface_fcslen_meta(packet);
554        if (r == NULL) { return 0; }
555        if (r->num <= index) {
556                trace_destroy_meta(r);
557                return 0;
558        }
559        uint32_t data = *(uint32_t *)r->items[index].data;
560        trace_destroy_meta(r);
561        return data;
562}
563
564/* Get any interface comments for a meta packet
565 * Must be destroyed with trace_destroy_meta()
566 *
567 * @params libtrace_packet_t packet
568 * @returns Pointer to libtrace_meta_t structure or NULL
569 */
570libtrace_meta_t *trace_get_interface_comment_meta(libtrace_packet_t *packet) {
571        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
572                return NULL;
573        }
574
575        libtrace_meta_t *r = NULL;
576
577        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
578                r = trace_get_meta_option(packet, ERF_PROV_SECTION_INTERFACE, ERF_PROV_COMMENT);
579        }
580        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
581                r = trace_get_meta_option(packet, PCAPNG_INTERFACE_TYPE, PCAPNG_OPTION_COMMENT);
582        }
583
584        return r;
585}
586/* Get the interface comment for a meta packet.
587 * Note: ERF packets can contain multiple interfaces per meta packet. Use index to
588 * specify the interface ID.
589 *
590 * @params libtrace_packet_t meta packet to extract the interface comment from.
591 * @params A pointer to a character buffer to store the interface description in.
592 * @params The size of the buffer passed in.
593 * @params The interface number within the meta packet.
594 * @returns Pointer to the character buffer containing the hardware description or NULL.
595 */
596char *trace_get_interface_comment(libtrace_packet_t *packet, char *space, int spacelen,
597        int index) {
598
599        libtrace_meta_t *r = trace_get_interface_comment_meta(packet);
600        if (r == NULL) { return NULL; }
601        if (index > r->num) {
602                trace_destroy_meta(r);
603                return NULL;
604        }
605        if (r->items[index].len > spacelen) {
606                memcpy(space, r->items[index].data, spacelen);
607                space[spacelen] = '\0';
608        } else {
609                memcpy(space, r->items[index].data, r->items[index].len);
610                space[r->items[index].len] = '\0';
611        }
612        trace_destroy_meta(r);
613        return space;
614}
615
616/* Get the capture application for a meta packet
617 * Must be destroyed with trace_destroy_meta()
618 *
619 * @params libtrace_packet_t packet
620 * @returns Pointer to libtrace_meta_t structure or NULL
621 */
622libtrace_meta_t *trace_get_capture_application_meta(libtrace_packet_t *packet) {
623        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
624                return NULL;
625        }
626
627        libtrace_meta_t *r = NULL;
628
629        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
630                r = trace_get_meta_option(packet, ERF_PROV_SECTION_CAPTURE, ERF_PROV_APP_NAME);
631        }
632        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
633                r = trace_get_meta_option(packet, PCAPNG_SECTION_TYPE, PCAPNG_META_SHB_USERAPPL);
634        }
635
636        return r;
637}
638/* Get the capture application for a meta packet.
639 *
640 * @params libtrace_packet_t meta packet to extract the application name from.
641 * @params A pointer to a character buffer to store the application name in.
642 * @params The size of the buffer passed in.
643 * @returns Pointer to the character buffer containing the application name or NULL.
644 */
645char *trace_get_capture_application(libtrace_packet_t *packet, char *space, int spacelen) {
646        libtrace_meta_t *r = trace_get_capture_application_meta(packet);
647        if (r == NULL) { return NULL; }
648        if (r->items[0].len > spacelen) {
649                memcpy(space, r->items[0].data, spacelen);
650                space[spacelen] = '\0';
651        } else {
652                memcpy(space, r->items[0].data, r->items[0].len);
653                space[r->items[0].len] = '\0';
654        }
655        trace_destroy_meta(r);
656        return space;
657}
658
659/* Get a meta section option from a meta packet
660 * Must be destroyed with trace_destroy_meta()
661 *
662 * @params libtrace_packet_t packet
663 * @params Section code
664 * @params Option code
665 * @returns Pointer to libtrace_meta_t structure or NULL
666 */
667libtrace_meta_t *trace_get_section_option(libtrace_packet_t *packet, uint32_t section_code,
668        uint16_t option_code) {
669
670        if (trace_meta_check_input(packet, "trace_get_custom_meta()")<0) {
671                return NULL;
672        }
673
674        return trace_get_meta_option(packet, section_code, option_code);
675}
676
677/* Get a section from a meta packet
678 * Must be destroyed with trace_destroy_meta()
679 *
680 * @params libtrace_packet_t packet
681 * @params Section code
682 * @returns Pointer to libtrace_meta_t structure or NULL
683 */
684libtrace_meta_t *trace_get_section(libtrace_packet_t *packet, uint32_t section_code) {
685        if (trace_meta_check_input(packet, "trace_get_section()")<0) {
686                return NULL;
687        }
688
689        return packet->trace->format->get_meta_section(packet, section_code);
690}
691
692/* ERF specific function */
693/* Get the DAG card model from a meta packet.
694 *
695 * @params libtrace_packet_t meta packet to extract the DAG model from.
696 * @params A pointer to a character buffer to store the DAG model in.
697 * @params The size of the buffer passed in.
698 * @returns Pointer to the character buffer containing the DAG model or NULL.
699 */
700char *trace_get_erf_dag_card_model(libtrace_packet_t *packet, char *space, int spacelen) {
701        libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE,
702                ERF_PROV_MODEL);
703
704        if (r == NULL) { return NULL; }
705        if (r->items[0].len > spacelen) {
706                memcpy(space, r->items[0].data, spacelen);
707                space[spacelen] = '\0';
708        } else {
709                memcpy(space, r->items[0].data, r->items[0].len);
710                space[r->items[0].len] = '\0';
711        }
712        trace_destroy_meta(r);
713        return space;
714}
715/* Get the host DAG software version for a meta packet.
716 *
717 * @params libtrace_packet_t meta packet to extract the hosts DAG verion from.
718 * @params A pointer to a character buffer to store the DAG version in.
719 * @params The size of the buffer passed in.
720 * @returns Pointer to the character buffer containing the DAG version or NULL.
721 */
722char *trace_get_erf_dag_version(libtrace_packet_t *packet, char *space, int spacelen) {
723        libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE,
724                ERF_PROV_DAG_VERSION);
725
726        if (r == NULL) { return NULL; }
727
728        if (r->items[0].len > spacelen) {
729                memcpy(space, r->items[0].data, spacelen);
730                space[spacelen] = '\0';
731        } else {
732                memcpy(space, r->items[0].data, r->items[0].len);
733                space[r->items[0].len] = '\0';
734        }
735        trace_destroy_meta(r);
736        return space;
737}
738/* Get the firmware version for a DAG module from a meta packet.
739 *
740 * @params libtrace_packet_t meta packet to extract the FW version from.
741 * @params A pointer to a character buffer to store the FW version in.
742 * @params The size of the buffer passed in.
743 * @returns Pointer to the character buffer containing the FW version or NULL.
744 */
745char *trace_get_erf_dag_fw_version(libtrace_packet_t *packet, char *space, int spacelen) {
746        libtrace_meta_t *r = trace_get_section_option(packet, ERF_PROV_SECTION_MODULE,
747                ERF_PROV_FW_VERSION);
748
749        if (r == NULL) { return NULL; }
750
751        if (r->items[0].len > spacelen) {
752                memcpy(space, r->items[0].data, spacelen);
753                space[spacelen] = '\0';
754        } else {
755                memcpy(space, r->items[0].data, r->items[0].len);
756                space[r->items[0].len] = '\0';
757        }
758        trace_destroy_meta(r);
759        return space;
760}
761
Note: See TracBrowser for help on using the repository browser.