source: lib/format_pktmeta.c @ b27ed21

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

Add datatype enum to libtrace_meta_item_t structure, Convert interface speeds from network to host byte ordering

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