source: lib/format_pktmeta.c @ ef5ba20

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

add abilty to get custom option from meta packets, add abilty to get entire section from meta packet, meta api now returns libtrace_meta_t structure

  • Property mode set to 100644
File size: 11.2 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/* Internal Meta functions */
12
13static int trace_meta_check_input(libtrace_packet_t *packet, char *input_func) {
14        if (packet == NULL) {
15                fprintf(stderr, "NULL packet passed into %s\n", input_func);
16                return -1;
17        }
18        if (packet->trace == NULL) {
19                fprintf(stderr, "Packet contains NULL trace in %s\n", input_func);
20                return -1;
21        }
22        return 1;
23}
24
25/* API functions to retrieve interface related packet data */
26
27/* Destroy libtrace_meta_t structure
28 *
29 * @params libtrace_meta_t structure
30 * returns 1 on success, -1 on failure
31 */
32int trace_destroy_meta(libtrace_meta_t *result) {
33        int i;
34        if (!result) { return -1; }
35
36        for (i=0;i<result->num;i++) {
37                if(result->items[i].data) {
38                        free(result->items[i].data);
39                }
40        }
41        if (result->items) {
42                free(result->items);
43        }
44        if (result) {
45                free(result);
46        }
47
48        return 1;
49}
50
51/* Get the interface name/s
52 * Must be destroyed with trace_destroy_meta()
53 *
54 * @params libtrace_packet_t meta packet
55 * @returns Pointer to libtrace_meta_t structure or NULL
56 */
57libtrace_meta_t *trace_get_interface_name(libtrace_packet_t *packet) {
58        if (trace_meta_check_input(packet, "trace_get_interface_name()")<0) {
59                return NULL;
60        }
61
62        /* get the result */
63        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
64                return packet->trace->format->get_meta_section_item(packet,
65                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_NAME);
66        }
67        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
68                return packet->trace->format->get_meta_section_item(packet,
69                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_NAME);
70        }
71
72        return NULL;
73}
74
75/* Get the interface MAC address/s
76 * Must be destroyed with trace_destroy_meta()
77 *
78 * @params libtrace_packet_t meta packet
79 * @returns Pointer to libtrace_meta_t structure or NULL
80 */
81libtrace_meta_t *trace_get_interface_mac(libtrace_packet_t *packet) {
82        if (trace_meta_check_input(packet, "trace_get_interface_mac()")<0) {
83                return NULL;
84        }
85
86        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
87                return packet->trace->format->get_meta_section_item(packet,
88                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_MAC);
89        }
90        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
91                return packet->trace->format->get_meta_section_item(packet,
92                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_MAC);
93        }
94
95        return NULL;
96}
97
98/* Get the interface speed/s from a meta packet
99 * Must be destroyed with trace_destroy_meta()
100 *
101 * @params libtrace_packet_t packet
102 * @returns Pointer to libtrace_meta_t structure or NULL
103 */
104libtrace_meta_t *trace_get_interface_speed(libtrace_packet_t *packet) {
105        if (trace_meta_check_input(packet, "trace_get_interface_speed()")<0) {
106                return NULL;
107        }
108
109        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
110                return packet->trace->format->get_meta_section_item(packet,
111                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_SPEED);
112        }
113        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
114                return packet->trace->format->get_meta_section_item(packet,
115                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_SPEED);
116        }
117
118        return NULL;
119}
120
121/* Get the interface ipv4 address/s
122 * Must be destroyed with trace_destroy_meta()
123 *
124 * @params libtrace_packet_t meta packet
125 * @returns Pointer to libtrace_meta_t structure or NULL
126 */
127libtrace_meta_t *trace_get_interface_ip4(libtrace_packet_t *packet) {
128        if (trace_meta_check_input(packet, "trace_get_interface_ip4()")<0) {
129                return NULL;
130        }
131
132        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
133                return packet->trace->format->get_meta_section_item(packet,
134                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
135        }
136        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
137                return packet->trace->format->get_meta_section_item(packet,
138                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
139        }
140
141        return NULL;
142}
143libtrace_meta_t *trace_get_interface_ipv4(libtrace_packet_t *packet) {
144        return trace_get_interface_ip4(packet);
145}
146
147/* Get the interface ipv6 address/s
148 * Must be destroyed with trace_destroy_meta()
149 *
150 * @params libtrace_packet_t meta packet
151 * @returns Pointer to libtrace_meta_t structure or NULL
152 */
153libtrace_meta_t *trace_get_interface_ip6(libtrace_packet_t *packet) {
154        if (trace_meta_check_input(packet, "trace_get_interface_ip6()")<0) {
155                return NULL;
156        }
157
158        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
159                return packet->trace->format->get_meta_section_item(packet,
160                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_IPV4);
161        }
162        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
163                return packet->trace->format->get_meta_section_item(packet,
164                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_IP4);
165        }
166
167        return NULL;
168}
169libtrace_meta_t *trace_get_interface_ipv6(libtrace_packet_t *packet) {
170        return trace_get_interface_ip6(packet);
171}
172
173/* Get the interface description/s
174 * Must be destroyed with trace_destroy_meta()
175 *
176 * @params libtrace_packet_t meta packet
177 * @returns Pointer to libtrace_meta_t structure or NULL
178 */
179libtrace_meta_t *trace_get_interface_description(libtrace_packet_t *packet) {
180        if (trace_meta_check_input(packet, "trace_get_interface_description()")<0) {
181                return NULL;
182        }
183
184        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
185                return packet->trace->format->get_meta_section_item(packet,
186                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_DESCR);
187        }
188        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
189                return packet->trace->format->get_meta_section_item(packet,
190                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_DESCR);
191        }
192
193        return NULL;
194}
195
196/* Get the interface number/s
197 * Must be destroyed with trace_destroy_meta()
198 *
199 * @params libtrace_packet_t meta packet
200 * @returns Pointer to libtrace_meta_t structure or NULL
201 */
202libtrace_meta_t *trace_get_interface_num(libtrace_packet_t *packet) {
203        if (trace_meta_check_input(packet, "trace_get_interface_num()")<0) {
204                return NULL;
205        }
206
207        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
208                return packet->trace->format->get_meta_section_item(packet,
209                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_IF_NUM);
210        }
211        /* Note: pcapng doesnt provide this */
212
213        return NULL;
214}
215
216/* Get the host OS
217 * Must be destroyed with trace_destroy_meta()
218 *
219 * @params libtrace_packet_t meta packet
220 * @returns Pointer to libtrace_meta_t structure or NULL
221 */
222libtrace_meta_t *trace_get_host_os(libtrace_packet_t *packet) {
223        if (trace_meta_check_input(packet, "trace_get_host_os()")<0) {
224                return NULL;
225        }
226
227        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
228                return packet->trace->format->get_meta_section_item(packet,
229                        ERF_PROV_SECTION_HOST, ERF_PROV_OS);
230        }
231        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
232                return packet->trace->format->get_meta_section_item(packet,
233                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_OS);
234        }
235
236        return NULL;
237}
238
239/* Get the frame check sequence length
240 * Must be destroyed with trace_destroy_meta()
241 *
242 * @params libtrace_packet_t meta packet
243 * @returns Pointer to libtrace_meta_t structure or NULL
244 */
245libtrace_meta_t *trace_get_interface_frame_check_sequence_length(libtrace_packet_t *packet) {
246        if (trace_meta_check_input(packet, "trace_get_interface_frame_check_sequence_length()")<0) {
247                return NULL;
248        }
249
250        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
251                return packet->trace->format->get_meta_section_item(packet,
252                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_FCS_LEN);
253        }
254        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
255                return packet->trace->format->get_meta_section_item(packet,
256                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_FCSLEN);
257        }
258
259        return NULL;
260}
261
262/* Get the hardware description
263 * Must be destroyed with trace_destroy_meta()
264 *
265 * @params libtrace_packet_t meta packet
266 * @returns Pointer to libtrace_meta_t structure or NULL
267 */
268libtrace_meta_t *trace_get_interface_hardware_description(libtrace_packet_t *packet) {
269        if (trace_meta_check_input(packet, "trace_get_interface_hardware_description()")<0) {
270                return NULL;
271        }
272
273        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
274                return packet->trace->format->get_meta_section_item(packet,
275                        PCAPNG_INTERFACE_TYPE, PCAPNG_META_IF_HARDWARE);
276        }
277        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
278                return packet->trace->format->get_meta_section_item(packet,
279                        ERF_PROV_SECTION_MODULE, ERF_PROV_MODEL);
280        }
281
282        return NULL;
283}
284
285/* Get any interface comments for a meta packet
286 * Must be destroyed with trace_destroy_meta()
287 *
288 * @params libtrace_packet_t packet
289 * @returns Pointer to libtrace_meta_t structure or NULL
290 */
291libtrace_meta_t *trace_get_interface_comment(libtrace_packet_t *packet) {
292        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
293                return NULL;
294        }
295
296        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
297                return packet->trace->format->get_meta_section_item(packet,
298                        ERF_PROV_SECTION_INTERFACE, ERF_PROV_COMMENT);
299        }
300        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
301                return packet->trace->format->get_meta_section_item(packet,
302                        PCAPNG_INTERFACE_TYPE, PCAPNG_OPTION_COMMENT);
303        }
304
305        return NULL;
306}
307
308/* Get the capture application for a meta packet
309 * Must be destroyed with trace_destroy_meta()
310 *
311 * @params libtrace_packet_t packet
312 * @returns Pointer to libtrace_meta_t structure or NULL
313 */
314libtrace_meta_t *trace_get_capture_application(libtrace_packet_t *packet) {
315        if (trace_meta_check_input(packet, "trace_get_interface_comment()")<0) {
316                return NULL;
317        }
318
319        if (packet->trace->format->type == TRACE_FORMAT_ERF) {
320                return packet->trace->format->get_meta_section_item(packet,
321                        ERF_PROV_SECTION_CAPTURE, ERF_PROV_APP_NAME);
322        }
323        if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) {
324                return packet->trace->format->get_meta_section_item(packet,
325                        PCAPNG_SECTION_TYPE, PCAPNG_META_SHB_USERAPPL);
326        }
327
328        return NULL;
329}
330
331/* Get meta section option from a meta packet
332 * Must be destroyed with trace_destroy_meta()
333 *
334 * @params libtrace_packet_t packet
335 * @params Section code
336 * @params Option code
337 * @returns Pointer to libtrace_meta_t structure or NULL
338 */
339libtrace_meta_t *trace_get_section_option(libtrace_packet_t *packet, uint32_t section_code,
340        uint16_t option_code) {
341
342        if (trace_meta_check_input(packet, "trace_get_custom_meta()")<0) {
343                return NULL;
344        }
345
346        return packet->trace->format->get_meta_section_item(packet,
347                section_code, option_code);
348}
349
350/* Get a section from a meta packet
351 * Must be destroyed with trace_destroy_meta()
352 *
353 * @params libtrace_packet_t packet
354 * @params Section code
355 * @returns Pointer to libtrace_meta_t structure or NULL
356 */
357libtrace_meta_t *trace_get_section(libtrace_packet_t *packet, uint32_t section_code) {
358        if (trace_meta_check_input(packet, "trace_get_section()")<0) {
359                return NULL;
360        }
361
362        return packet->trace->format->get_meta_section(packet, section_code);
363}
Note: See TracBrowser for help on using the repository browser.