source: lib/format_pktmeta.c @ 254c926

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

Cleanup some duplicate code, Added datatype/option_name for libtrace_meta_t structure

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