source: lib/format_pktmeta.c @ 977e0db

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

Add meta api for erf and pcapng meta packets

  • Property mode set to 100644
File size: 5.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
11/* API functions to retrieve interface related packet data */
12
13
14
15/* Reads the interface name for a packet
16 *
17 * @params libtrace_packet_t packet
18 * @returns pointer to NULL terminated string containing the interface name
19 */
20
21char *trace_get_interface_name(libtrace_packet_t *packet) {
22        void *ptr = NULL;
23
24        /* find the result if we havnt already */
25        if (packet->meta.interface_name != NULL) {
26                return packet->meta.interface_name;
27        }
28
29        /* get the result */
30        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
31                ptr = packet->trace->format->get_meta_data(packet,
32                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_NAME);
33        }
34        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
35                ptr = packet->trace->format->get_meta_data(packet,
36                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_NAME);
37        }
38
39        /* If a result was found */
40        if (ptr != NULL) {
41                libtrace_meta_result_t *result = (libtrace_meta_result_t *)ptr;
42
43                /* store this result against the packet itself */
44                packet->meta.interface_name = malloc(ntohs(result->len)+1);
45                /* add NULL terminator to string */
46                packet->meta.interface_name[ntohs(result->len)] = '\0';
47                /* copy result over */
48                memcpy(packet->meta.interface_name, ptr+sizeof(libtrace_meta_result_t),
49                        ntohs(result->len));
50
51                return packet->meta.interface_name;
52        }
53
54        return NULL;
55}
56
57/* Gets the interface MAC address from a meta packet
58 *
59 * @params libtrace_packet_t packet
60 * @returns A pointer to a MAC address
61 */
62char *trace_get_interface_mac(libtrace_packet_t *packet) {
63        void *ptr = NULL;
64
65        if (packet->meta.interface_mac != NULL) {
66                return packet->meta.interface_mac;
67        }
68
69        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
70                ptr = packet->trace->format->get_meta_data(packet,
71                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_MAC);
72        }
73        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
74                ptr = packet->trace->format->get_meta_data(packet,
75                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_MAC);
76        }
77
78        if (ptr != NULL) {
79                /* allocate memory within the packet to store the result
80                 * exclude any padding that could be included */
81                packet->meta.interface_mac = malloc(6);
82                /* copy result over */
83                memcpy(packet->meta.interface_mac, ptr+sizeof(libtrace_meta_result_t), 6);
84
85                return packet->meta.interface_mac;
86        }
87
88        return NULL;
89}
90
91/* Gets the interface speed from a meta packet
92 *
93 * @params libtrace_packet_t packet
94 * @returns uint64_t containing the interface speed or 0 if not found.
95 */
96uint64_t trace_get_interface_speed(libtrace_packet_t *packet) {
97        /* Get the result */
98        void *ptr = packet->trace->format->get_meta_data(packet,
99                ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_SPEED);
100
101        /* If a result was found */
102        if (ptr != NULL) {
103                uint64_t *result = (uint64_t *)(ptr+sizeof(libtrace_meta_result_t));
104                packet->meta.interface_speed = *result;
105
106                return packet->meta.interface_speed;
107        }
108
109        return 0;
110}
111
112/* Gets the interface ipv4 address from a meta packet
113 *
114 * @params libtrace_packet_t packet
115 * @returns A pointer to the IP4 address field within the packet
116 */
117uint32_t trace_get_interface_ip4(libtrace_packet_t *packet) {
118        void *ptr = packet->trace->format->get_meta_data(packet,
119                ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
120
121        if (ptr != NULL) {
122                uint32_t *result = (uint32_t *)(ptr+sizeof(libtrace_meta_result_t));
123                packet->meta.interface_ipv4 = *result;
124
125                return packet->meta.interface_ipv4;
126        }
127
128        return 0;
129}
130uint32_t trace_get_interface_ipv4(libtrace_packet_t *packet) {
131        return trace_get_interface_ip4(packet);
132}
133
134char *trace_get_interface_ip6(libtrace_packet_t *packet UNUSED) {
135        return NULL;
136}
137char *trace_get_interface_ipv6(libtrace_packet_t *packet) {
138        return trace_get_interface_ip6(packet);
139}
140
141/* Gets the interface description for a packet
142 *
143 * @params libtrace_packet_t packet
144 * @returns A char* to a NULL terminated interface description
145 */
146char *trace_get_interface_description(libtrace_packet_t *packet) {
147        if (packet->meta.interface_description != NULL) {
148                return packet->meta.interface_description;
149        }
150
151        void *ptr = packet->trace->format->get_meta_data(packet,
152                ERF_PROV_SECTION_INTERFACE, ERF_PROV_DESCR);
153
154        if (ptr != NULL) {
155                libtrace_meta_result_t *result = (libtrace_meta_result_t *)ptr;
156                packet->meta.interface_description = malloc(ntohs(result->len)+1);
157                packet->meta.interface_description[ntohs(result->len)] = '\0';
158                memcpy(packet->meta.interface_description, ptr+sizeof(libtrace_meta_result_t),
159                        ntohs(result->len));
160
161                return packet->meta.interface_description;
162        }
163
164        return NULL;
165}
166
167libtrace_meta_result_t *trace_get_interface_num(libtrace_packet_t *packet) {
168        libtrace_meta_result_t *result = packet->trace->format->get_meta_data(packet,
169                ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_NUM);
170        return result;
171}
172
173libtrace_meta_result_t *trace_get_host_os(libtrace_packet_t *packet UNUSED) {
174        return NULL;
175}
176
177libtrace_meta_result_t *trace_get_tzone(libtrace_packet_t *packet UNUSED) {
178        return NULL;
179}
180
181libtrace_meta_result_t *trace_get_app_name(libtrace_packet_t *packet UNUSED) {
182        return NULL;
183}
Note: See TracBrowser for help on using the repository browser.