source: lib/format_pktmeta.c @ d0f25d4

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

Add ERF and PCAPNG decoders to libpacketdump

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