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