4 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
6 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include <sys/types.h>
17 #include "wtap_opttypes.h"
20 #include "file_wrappers.h"
21 #include <wsutil/file_util.h>
22 #include <wsutil/buffer.h>
27 static plugins_t *libwiretap_plugins = NULL;
28 static GSList *wtap_plugins = NULL;
31 wtap_register_plugin(const wtap_plugin *plug)
33 wtap_plugins = g_slist_prepend(wtap_plugins, (wtap_plugin *)plug);
37 call_plugin_register_wtap_module(gpointer data, gpointer user_data _U_)
39 wtap_plugin *plug = (wtap_plugin *)data;
41 if (plug->register_wtap_module) {
42 plug->register_wtap_module();
45 #endif /* HAVE_PLUGINS */
48 * Return the size of the file, as reported by the OS.
49 * (gint64, in case that's 64 bits.)
52 wtap_file_size(wtap *wth, int *err)
56 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
63 * Do an fstat on the file.
66 wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
68 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
75 wtap_file_type_subtype(wtap *wth)
77 return wth->file_type_subtype;
81 wtap_iscompressed(wtap *wth)
83 return file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh);
87 wtap_snapshot_length(wtap *wth)
89 return wth->snapshot_length;
93 wtap_file_encap(wtap *wth)
95 return wth->file_encap;
99 wtap_file_tsprec(wtap *wth)
101 return wth->file_tsprec;
105 wtap_file_get_shb(wtap *wth)
107 if ((wth == NULL) || (wth->shb_hdrs == NULL) || (wth->shb_hdrs->len == 0))
110 return g_array_index(wth->shb_hdrs, wtap_block_t, 0);
114 wtap_file_get_shb_for_new_file(wtap *wth)
117 wtap_block_t shb_hdr_src, shb_hdr_dest;
120 if ((wth == NULL) || (wth->shb_hdrs == NULL) || (wth->shb_hdrs->len == 0))
123 shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
125 for (shb_count = 0; shb_count < wth->shb_hdrs->len; shb_count++) {
126 shb_hdr_src = g_array_index(wth->shb_hdrs, wtap_block_t, shb_count);
127 shb_hdr_dest = wtap_block_create(WTAP_BLOCK_NG_SECTION);
128 wtap_block_copy(shb_hdr_dest, shb_hdr_src);
129 g_array_append_val(shb_hdrs, shb_hdr_dest);
136 * XXX - replace with APIs that let us handle multiple comments.
139 wtap_write_shb_comment(wtap *wth, gchar *comment)
141 if ((wth != NULL) && (wth->shb_hdrs != NULL) && (wth->shb_hdrs->len > 0)) {
142 wtap_block_set_nth_string_option_value(g_array_index(wth->shb_hdrs, wtap_block_t, 0), OPT_COMMENT, 0, comment, (gsize)(comment ? strlen(comment) : 0));
146 wtapng_iface_descriptions_t *
147 wtap_file_get_idb_info(wtap *wth)
149 wtapng_iface_descriptions_t *idb_info;
151 idb_info = g_new(wtapng_iface_descriptions_t,1);
153 idb_info->interface_data = wth->interface_data;
160 wtap_free_idb_info(wtapng_iface_descriptions_t *idb_info)
162 if (idb_info == NULL)
165 wtap_block_array_free(idb_info->interface_data);
170 wtap_get_debug_if_descr(const wtap_block_t if_descr,
172 const char* line_end)
175 wtapng_if_descr_mandatory_t* if_descr_mand;
176 GString *info = g_string_new("");
180 wtapng_if_descr_filter_t* if_filter;
184 if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(if_descr);
185 if (wtap_block_get_string_option_value(if_descr, OPT_IDB_NAME, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
186 g_string_printf(info,
187 "%*cName = %s%s", indent, ' ',
188 tmp_content ? tmp_content : "UNKNOWN",
192 if (wtap_block_get_string_option_value(if_descr, OPT_IDB_DESCR, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
193 g_string_append_printf(info,
194 "%*cDescription = %s%s", indent, ' ',
195 tmp_content ? tmp_content : "NONE",
199 g_string_append_printf(info,
200 "%*cEncapsulation = %s (%d - %s)%s", indent, ' ',
201 wtap_encap_string(if_descr_mand->wtap_encap),
202 if_descr_mand->wtap_encap,
203 wtap_encap_short_string(if_descr_mand->wtap_encap),
206 if (wtap_block_get_string_option_value(if_descr, OPT_IDB_HARDWARE, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
207 g_string_append_printf(info,
208 "%*cHardware = %s%s", indent, ' ',
209 tmp_content ? tmp_content : "NONE",
213 if (wtap_block_get_uint64_option_value(if_descr, OPT_IDB_SPEED, &tmp64) == WTAP_OPTTYPE_SUCCESS) {
214 g_string_append_printf(info,
215 "%*cSpeed = %" G_GINT64_MODIFIER "u%s", indent, ' ',
220 g_string_append_printf(info,
221 "%*cCapture length = %u%s", indent, ' ',
222 if_descr_mand->snap_len,
225 if (wtap_block_get_uint8_option_value(if_descr, OPT_IDB_FCSLEN, &itmp8) == WTAP_OPTTYPE_SUCCESS) {
226 g_string_append_printf(info,
227 "%*cFCS length = %d%s", indent, ' ',
232 g_string_append_printf(info,
233 "%*cTime precision = %s (%d)%s", indent, ' ',
234 wtap_tsprec_string(if_descr_mand->tsprecision),
235 if_descr_mand->tsprecision,
238 g_string_append_printf(info,
239 "%*cTime ticks per second = %" G_GINT64_MODIFIER "u%s", indent, ' ',
240 if_descr_mand->time_units_per_second,
243 if (wtap_block_get_uint8_option_value(if_descr, OPT_IDB_TSRESOL, &tmp8) == WTAP_OPTTYPE_SUCCESS) {
244 g_string_append_printf(info,
245 "%*cTime resolution = 0x%.2x%s", indent, ' ',
250 if (wtap_block_get_custom_option_value(if_descr, OPT_IDB_FILTER, (void**)&if_filter) == WTAP_OPTTYPE_SUCCESS) {
251 g_string_append_printf(info,
252 "%*cFilter string = %s%s", indent, ' ',
253 if_filter->if_filter_str ? if_filter->if_filter_str : "NONE",
256 g_string_append_printf(info,
257 "%*cBPF filter length = %u%s", indent, ' ',
258 if_filter->bpf_filter_len,
262 if (wtap_block_get_string_option_value(if_descr, OPT_IDB_OS, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
263 g_string_append_printf(info,
264 "%*cOperating system = %s%s", indent, ' ',
265 tmp_content ? tmp_content : "UNKNOWN",
270 * XXX - support multiple comments.
272 if (wtap_block_get_nth_string_option_value(if_descr, OPT_COMMENT, 0, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
273 g_string_append_printf(info,
274 "%*cComment = %s%s", indent, ' ',
275 tmp_content ? tmp_content : "NONE",
279 g_string_append_printf(info,
280 "%*cNumber of stat entries = %u%s", indent, ' ',
281 if_descr_mand->num_stat_entries,
284 return g_string_free(info, FALSE);
288 wtap_file_get_nrb(wtap *wth)
290 if ((wth == NULL) || (wth->nrb_hdrs == NULL) || (wth->nrb_hdrs->len == 0))
293 return g_array_index(wth->nrb_hdrs, wtap_block_t, 0);
297 wtap_file_get_nrb_for_new_file(wtap *wth)
300 wtap_block_t nrb_hdr_src, nrb_hdr_dest;
303 if ((wth == NULL || wth->nrb_hdrs == NULL) || (wth->nrb_hdrs->len == 0))
306 nrb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
308 for (nrb_count = 0; nrb_count < wth->nrb_hdrs->len; nrb_count++) {
309 nrb_hdr_src = g_array_index(wth->nrb_hdrs, wtap_block_t, nrb_count);
310 nrb_hdr_dest = wtap_block_create(WTAP_BLOCK_NG_NRB);
311 wtap_block_copy(nrb_hdr_dest, nrb_hdr_src);
312 g_array_append_val(nrb_hdrs, nrb_hdr_dest);
318 /* Table of the encapsulation types we know about. */
319 struct encap_type_info {
321 const char *short_name;
324 static struct encap_type_info encap_table_base[] = {
325 /* WTAP_ENCAP_UNKNOWN */
326 { "Unknown", "unknown" },
328 /* WTAP_ENCAP_ETHERNET */
329 { "Ethernet", "ether" },
331 /* WTAP_ENCAP_TOKEN_RING */
332 { "Token Ring", "tr" },
334 /* WTAP_ENCAP_SLIP */
340 /* WTAP_ENCAP_FDDI */
343 /* WTAP_ENCAP_FDDI_BITSWAPPED */
344 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
346 /* WTAP_ENCAP_RAW_IP */
347 { "Raw IP", "rawip" },
349 /* WTAP_ENCAP_ARCNET */
350 { "ARCNET", "arcnet" },
352 /* WTAP_ENCAP_ARCNET_LINUX */
353 { "Linux ARCNET", "arcnet_linux" },
355 /* WTAP_ENCAP_ATM_RFC1483 */
356 { "RFC 1483 ATM", "atm-rfc1483" },
358 /* WTAP_ENCAP_LINUX_ATM_CLIP */
359 { "Linux ATM CLIP", "linux-atm-clip" },
361 /* WTAP_ENCAP_LAPB */
364 /* WTAP_ENCAP_ATM_PDUS */
365 { "ATM PDUs", "atm-pdus" },
367 /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
368 { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
370 /* WTAP_ENCAP_NULL */
371 { "NULL/Loopback", "null" },
373 /* WTAP_ENCAP_ASCEND */
374 { "Lucent/Ascend access equipment", "ascend" },
376 /* WTAP_ENCAP_ISDN */
379 /* WTAP_ENCAP_IP_OVER_FC */
380 { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
382 /* WTAP_ENCAP_PPP_WITH_PHDR */
383 { "PPP with Directional Info", "ppp-with-direction" },
385 /* WTAP_ENCAP_IEEE_802_11 */
386 { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
388 /* WTAP_ENCAP_IEEE_802_11_PRISM */
389 { "IEEE 802.11 plus Prism II monitor mode radio header", "ieee-802-11-prism" },
391 /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
392 { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
394 /* WTAP_ENCAP_IEEE_802_11_RADIOTAP */
395 { "IEEE 802.11 plus radiotap radio header", "ieee-802-11-radiotap" },
397 /* WTAP_ENCAP_IEEE_802_11_AVS */
398 { "IEEE 802.11 plus AVS radio header", "ieee-802-11-avs" },
401 { "Linux cooked-mode capture", "linux-sll" },
403 /* WTAP_ENCAP_FRELAY */
404 { "Frame Relay", "frelay" },
406 /* WTAP_ENCAP_FRELAY_WITH_PHDR */
407 { "Frame Relay with Directional Info", "frelay-with-direction" },
409 /* WTAP_ENCAP_CHDLC */
410 { "Cisco HDLC", "chdlc" },
412 /* WTAP_ENCAP_CISCO_IOS */
413 { "Cisco IOS internal", "ios" },
415 /* WTAP_ENCAP_LOCALTALK */
416 { "Localtalk", "ltalk" },
418 /* WTAP_ENCAP_OLD_PFLOG */
419 { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
421 /* WTAP_ENCAP_HHDLC */
422 { "HiPath HDLC", "hhdlc" },
424 /* WTAP_ENCAP_DOCSIS */
425 { "Data Over Cable Service Interface Specification", "docsis" },
427 /* WTAP_ENCAP_COSINE */
428 { "CoSine L2 debug log", "cosine" },
430 /* WTAP_ENCAP_WFLEET_HDLC */
431 { "Wellfleet HDLC", "whdlc" },
433 /* WTAP_ENCAP_SDLC */
436 /* WTAP_ENCAP_TZSP */
437 { "Tazmen sniffer protocol", "tzsp" },
440 { "OpenBSD enc(4) encapsulating interface", "enc" },
442 /* WTAP_ENCAP_PFLOG */
443 { "OpenBSD PF Firewall logs", "pflog" },
445 /* WTAP_ENCAP_CHDLC_WITH_PHDR */
446 { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
448 /* WTAP_ENCAP_BLUETOOTH_H4 */
449 { "Bluetooth H4", "bluetooth-h4" },
451 /* WTAP_ENCAP_MTP2 */
452 { "SS7 MTP2", "mtp2" },
454 /* WTAP_ENCAP_MTP3 */
455 { "SS7 MTP3", "mtp3" },
457 /* WTAP_ENCAP_IRDA */
460 /* WTAP_ENCAP_USER0 */
461 { "USER 0", "user0" },
463 /* WTAP_ENCAP_USER1 */
464 { "USER 1", "user1" },
466 /* WTAP_ENCAP_USER2 */
467 { "USER 2", "user2" },
469 /* WTAP_ENCAP_USER3 */
470 { "USER 3", "user3" },
472 /* WTAP_ENCAP_USER4 */
473 { "USER 4", "user4" },
475 /* WTAP_ENCAP_USER5 */
476 { "USER 5", "user5" },
478 /* WTAP_ENCAP_USER6 */
479 { "USER 6", "user6" },
481 /* WTAP_ENCAP_USER7 */
482 { "USER 7", "user7" },
484 /* WTAP_ENCAP_USER8 */
485 { "USER 8", "user8" },
487 /* WTAP_ENCAP_USER9 */
488 { "USER 9", "user9" },
490 /* WTAP_ENCAP_USER10 */
491 { "USER 10", "user10" },
493 /* WTAP_ENCAP_USER11 */
494 { "USER 11", "user11" },
496 /* WTAP_ENCAP_USER12 */
497 { "USER 12", "user12" },
499 /* WTAP_ENCAP_USER13 */
500 { "USER 13", "user13" },
502 /* WTAP_ENCAP_USER14 */
503 { "USER 14", "user14" },
505 /* WTAP_ENCAP_USER15 */
506 { "USER 15", "user15" },
508 /* WTAP_ENCAP_SYMANTEC */
509 { "Symantec Enterprise Firewall", "symantec" },
511 /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
512 { "Apple IP-over-IEEE 1394", "ap1394" },
514 /* WTAP_ENCAP_BACNET_MS_TP */
515 { "BACnet MS/TP", "bacnet-ms-tp" },
517 /* WTAP_ENCAP_NETTL_RAW_ICMP */
518 { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
520 /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
521 { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
523 /* WTAP_ENCAP_GPRS_LLC */
524 { "GPRS LLC", "gprs-llc" },
526 /* WTAP_ENCAP_JUNIPER_ATM1 */
527 { "Juniper ATM1", "juniper-atm1" },
529 /* WTAP_ENCAP_JUNIPER_ATM2 */
530 { "Juniper ATM2", "juniper-atm2" },
532 /* WTAP_ENCAP_REDBACK */
533 { "Redback SmartEdge", "redback" },
535 /* WTAP_ENCAP_NETTL_RAW_IP */
536 { "Raw IP with nettl headers", "rawip-nettl" },
538 /* WTAP_ENCAP_NETTL_ETHERNET */
539 { "Ethernet with nettl headers", "ether-nettl" },
541 /* WTAP_ENCAP_NETTL_TOKEN_RING */
542 { "Token Ring with nettl headers", "tr-nettl" },
544 /* WTAP_ENCAP_NETTL_FDDI */
545 { "FDDI with nettl headers", "fddi-nettl" },
547 /* WTAP_ENCAP_NETTL_UNKNOWN */
548 { "Unknown link-layer type with nettl headers", "unknown-nettl" },
550 /* WTAP_ENCAP_MTP2_WITH_PHDR */
551 { "MTP2 with pseudoheader", "mtp2-with-phdr" },
553 /* WTAP_ENCAP_JUNIPER_PPPOE */
554 { "Juniper PPPoE", "juniper-pppoe" },
556 /* WTAP_ENCAP_GCOM_TIE1 */
557 { "GCOM TIE1", "gcom-tie1" },
559 /* WTAP_ENCAP_GCOM_SERIAL */
560 { "GCOM Serial", "gcom-serial" },
562 /* WTAP_ENCAP_NETTL_X25 */
563 { "X.25 with nettl headers", "x25-nettl" },
566 { "K12 protocol analyzer", "k12" },
568 /* WTAP_ENCAP_JUNIPER_MLPPP */
569 { "Juniper MLPPP", "juniper-mlppp" },
571 /* WTAP_ENCAP_JUNIPER_MLFR */
572 { "Juniper MLFR", "juniper-mlfr" },
574 /* WTAP_ENCAP_JUNIPER_ETHER */
575 { "Juniper Ethernet", "juniper-ether" },
577 /* WTAP_ENCAP_JUNIPER_PPP */
578 { "Juniper PPP", "juniper-ppp" },
580 /* WTAP_ENCAP_JUNIPER_FRELAY */
581 { "Juniper Frame-Relay", "juniper-frelay" },
583 /* WTAP_ENCAP_JUNIPER_CHDLC */
584 { "Juniper C-HDLC", "juniper-chdlc" },
586 /* WTAP_ENCAP_JUNIPER_GGSN */
587 { "Juniper GGSN", "juniper-ggsn" },
589 /* WTAP_ENCAP_LINUX_LAPD */
590 { "LAPD with Linux pseudo-header", "linux-lapd" },
592 /* WTAP_ENCAP_CATAPULT_DCT2000 */
593 { "Catapult DCT2000", "dct2000" },
596 { "ASN.1 Basic Encoding Rules", "ber" },
598 /* WTAP_ENCAP_JUNIPER_VP */
599 { "Juniper Voice PIC", "juniper-vp" },
601 /* WTAP_ENCAP_USB_FREEBSD */
602 { "USB packets with FreeBSD header", "usb-freebsd" },
604 /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
605 { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
607 /* WTAP_ENCAP_NETTL_RAW_TELNET */
608 { "Raw telnet with nettl headers", "raw-telnet-nettl" },
610 /* WTAP_ENCAP_USB_LINUX */
611 { "USB packets with Linux header", "usb-linux" },
613 /* WTAP_ENCAP_MPEG */
617 { "Per-Packet Information header", "ppi" },
620 { "Extensible Record Format", "erf" },
622 /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
623 { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
625 /* WTAP_ENCAP_SITA */
626 { "SITA WAN packets", "sita-wan" },
628 /* WTAP_ENCAP_SCCP */
629 { "SS7 SCCP", "sccp" },
631 /* WTAP_ENCAP_BLUETOOTH_HCI */
632 { "Bluetooth without transport layer", "bluetooth-hci" },
634 /* WTAP_ENCAP_IPMB */
635 { "Intelligent Platform Management Bus", "ipmb" },
637 /* WTAP_ENCAP_IEEE802_15_4 */
638 { "IEEE 802.15.4 Wireless PAN", "wpan" },
640 /* WTAP_ENCAP_X2E_XORAYA */
641 { "X2E Xoraya", "x2e-xoraya" },
643 /* WTAP_ENCAP_FLEXRAY */
644 { "FlexRay", "flexray" },
647 { "Local Interconnect Network", "lin" },
649 /* WTAP_ENCAP_MOST */
650 { "Media Oriented Systems Transport", "most" },
652 /* WTAP_ENCAP_CAN20B */
653 { "Controller Area Network 2.0B", "can20b" },
655 /* WTAP_ENCAP_LAYER1_EVENT */
656 { "EyeSDN Layer 1 event", "layer1-event" },
658 /* WTAP_ENCAP_X2E_SERIAL */
659 { "X2E serial line capture", "x2e-serial" },
664 /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
665 { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
667 /* WTAP_ENCAP_TNEF */
668 { "Transport-Neutral Encapsulation Format", "tnef" },
670 /* WTAP_ENCAP_USB_LINUX_MMAPPED */
671 { "USB packets with Linux header and padding", "usb-linux-mmap" },
673 /* WTAP_ENCAP_GSM_UM */
674 { "GSM Um Interface", "gsm_um" },
676 /* WTAP_ENCAP_DPNSS */
677 { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
679 /* WTAP_ENCAP_PACKETLOGGER */
680 { "PacketLogger", "packetlogger" },
682 /* WTAP_ENCAP_NSTRACE_1_0 */
683 { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
685 /* WTAP_ENCAP_NSTRACE_2_0 */
686 { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
688 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
689 { "Fibre Channel FC-2", "fc2" },
691 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
692 { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
694 /* WTAP_ENCAP_JPEG_JFIF */
695 { "JPEG/JFIF", "jfif" },
697 /* WTAP_ENCAP_IPNET */
698 { "Solaris IPNET", "ipnet" },
700 /* WTAP_ENCAP_SOCKETCAN */
701 { "SocketCAN", "socketcan" },
703 /* WTAP_ENCAP_IEEE_802_11_NETMON */
704 { "IEEE 802.11 plus Network Monitor radio header", "ieee-802-11-netmon" },
706 /* WTAP_ENCAP_IEEE802_15_4_NOFCS */
707 { "IEEE 802.15.4 Wireless PAN with FCS not present", "wpan-nofcs" },
709 /* WTAP_ENCAP_RAW_IPFIX */
710 { "IPFIX", "ipfix" },
712 /* WTAP_ENCAP_RAW_IP4 */
713 { "Raw IPv4", "rawip4" },
715 /* WTAP_ENCAP_RAW_IP6 */
716 { "Raw IPv6", "rawip6" },
718 /* WTAP_ENCAP_LAPD */
721 /* WTAP_ENCAP_DVBCI */
722 { "DVB-CI (Common Interface)", "dvbci"},
724 /* WTAP_ENCAP_MUX27010 */
725 { "MUX27010", "mux27010"},
727 /* WTAP_ENCAP_MIME */
730 /* WTAP_ENCAP_NETANALYZER */
731 { "netANALYZER", "netanalyzer" },
733 /* WTAP_ENCAP_NETANALYZER_TRANSPARENT */
734 { "netANALYZER-Transparent", "netanalyzer-transparent" },
736 /* WTAP_ENCAP_IP_OVER_IB */
737 { "IP over Infiniband", "ip-over-ib" },
739 /* WTAP_ENCAP_MPEG_2_TS */
740 { "ISO/IEC 13818-1 MPEG2-TS", "mp2ts" },
742 /* WTAP_ENCAP_PPP_ETHER */
743 { "PPP-over-Ethernet session", "pppoes" },
745 /* WTAP_ENCAP_NFC_LLCP */
746 { "NFC LLCP", "nfc-llcp" },
748 /* WTAP_ENCAP_NFLOG */
749 { "NFLOG", "nflog" },
751 /* WTAP_ENCAP_V5_EF */
752 { "V5 Envelope Function", "v5-ef" },
754 /* WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR */
755 { "BACnet MS/TP with Directional Info", "bacnet-ms-tp-with-direction" },
757 /* WTAP_ENCAP_IXVERIWAVE */
758 { "IxVeriWave header and stats block", "ixveriwave" },
763 /* WTAP_ENCAP_DBUS */
766 /* WTAP_ENCAP_AX25_KISS */
767 { "AX.25 with KISS header", "ax25-kiss" },
769 /* WTAP_ENCAP_AX25 */
770 { "Amateur Radio AX.25", "ax25" },
772 /* WTAP_ENCAP_SCTP */
775 /* WTAP_ENCAP_INFINIBAND */
776 { "InfiniBand", "infiniband" },
778 /* WTAP_ENCAP_JUNIPER_SVCS */
779 { "Juniper Services", "juniper-svcs" },
781 /* WTAP_ENCAP_USBPCAP */
782 { "USB packets with USBPcap header", "usb-usbpcap" },
784 /* WTAP_ENCAP_RTAC_SERIAL */
785 { "RTAC serial-line", "rtac-serial" },
787 /* WTAP_ENCAP_BLUETOOTH_LE_LL */
788 { "Bluetooth Low Energy Link Layer", "bluetooth-le-ll" },
790 /* WTAP_ENCAP_WIRESHARK_UPPER_PDU */
791 { "Wireshark Upper PDU export", "wireshark-upper-pdu" },
793 /* WTAP_ENCAP_STANAG_4607 */
794 { "STANAG 4607", "s4607" },
796 /* WTAP_ENCAP_STANAG_5066_D_PDU */
797 { "STANAG 5066 Data Transfer Sublayer PDUs(D_PDU)", "s5066-dpdu"},
799 /* WTAP_ENCAP_NETLINK */
800 { "Linux Netlink", "netlink" },
802 /* WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
803 { "Bluetooth Linux Monitor", "bluetooth-linux-monitor" },
805 /* WTAP_ENCAP_BLUETOOTH_BREDR_BB */
806 { "Bluetooth BR/EDR Baseband RF", "bluetooth-bredr-bb-rf" },
808 /* WTAP_ENCAP_BLUETOOTH_LE_LL_WITH_PHDR */
809 { "Bluetooth Low Energy Link Layer RF", "bluetooth-le-ll-rf" },
811 /* WTAP_ENCAP_NSTRACE_3_0 */
812 { "NetScaler Encapsulation 3.0 of Ethernet", "nstrace30" },
814 /* WTAP_ENCAP_LOGCAT */
815 { "Android Logcat Binary format", "logcat" },
817 /* WTAP_ENCAP_LOGCAT_BRIEF */
818 { "Android Logcat Brief text format", "logcat_brief" },
820 /* WTAP_ENCAP_LOGCAT_PROCESS */
821 { "Android Logcat Process text format", "logcat_process" },
823 /* WTAP_ENCAP_LOGCAT_TAG */
824 { "Android Logcat Tag text format", "logcat_tag" },
826 /* WTAP_ENCAP_LOGCAT_THREAD */
827 { "Android Logcat Thread text format", "logcat_thread" },
829 /* WTAP_ENCAP_LOGCAT_TIME */
830 { "Android Logcat Time text format", "logcat_time" },
832 /* WTAP_ENCAP_LOGCAT_THREADTIME */
833 { "Android Logcat Threadtime text format", "logcat_threadtime" },
835 /* WTAP_ENCAP_LOGCAT_LONG */
836 { "Android Logcat Long text format", "logcat_long" },
838 /* WTAP_ENCAP_PKTAP */
839 { "Apple PKTAP", "pktap" },
841 /* WTAP_ENCAP_EPON */
842 { "Ethernet Passive Optical Network", "epon" },
844 /* WTAP_ENCAP_IPMI_TRACE */
845 { "IPMI Trace Data Collection", "ipmi-trace" },
847 /* WTAP_ENCAP_LOOP */
848 { "OpenBSD loopback", "loop" },
850 /* WTAP_ENCAP_JSON */
851 { "JavaScript Object Notation", "json" },
853 /* WTAP_ENCAP_NSTRACE_3_5 */
854 { "NetScaler Encapsulation 3.5 of Ethernet", "nstrace35" },
856 /* WTAP_ENCAP_ISO14443 */
857 { "ISO 14443 contactless smartcard standards", "iso14443" },
859 /* WTAP_ENCAP_GFP_T */
860 { "ITU-T G.7041/Y.1303 Generic Framing Procedure Transparent mode", "gfp-t" },
862 /* WTAP_ENCAP_GFP_F */
863 { "ITU-T G.7041/Y.1303 Generic Framing Procedure Frame-mapped mode", "gfp-f" },
865 /* WTAP_ENCAP_IP_OVER_IB_PCAP */
866 { "IP over IB", "ip-ib" },
868 /* WTAP_ENCAP_JUNIPER_VN */
869 { "Juniper VN", "juniper-vn" },
871 /* WTAP_ENCAP_USB_DARWIN */
872 { "USB packets with Darwin (macOS, etc.) headers", "usb-darwin" },
874 /* WTAP_ENCAP_LORATAP */
875 { "LoRaTap", "loratap"},
877 /* WTAP_ENCAP_3MB_ETHERNET */
878 { "Xerox 3MB Ethernet", "xeth"},
880 /* WTAP_ENCAP_VSOCK */
881 { "Linux vsock", "vsock" },
883 /* WTAP_ENCAP_NORDIC_BLE */
884 { "Nordic BLE Sniffer", "nordic_ble" },
886 /* WTAP_ENCAP_NETMON_NET_NETEVENT */
887 { "Network Monitor Network Event", "netmon_event" },
889 /* WTAP_ENCAP_NETMON_HEADER */
890 { "Network Monitor Header", "netmon_header" },
892 /* WTAP_ENCAP_NETMON_NET_FILTER */
893 { "Network Monitor Filter", "netmon_filter" },
895 /* WTAP_ENCAP_NETMON_NETWORK_INFO_EX */
896 { "Network Monitor Network Info", "netmon_network_info" },
898 /* WTAP_ENCAP_MA_WFP_CAPTURE_V4 */
899 { "Message Analyzer WFP Capture v4", "message_analyzer_wfp_capture_v4" },
901 /* WTAP_ENCAP_MA_WFP_CAPTURE_V6 */
902 { "Message Analyzer WFP Capture v6", "message_analyzer_wfp_capture_v6" },
904 /* WTAP_ENCAP_MA_WFP_CAPTURE_2V4 */
905 { "Message Analyzer WFP Capture2 v4", "message_analyzer_wfp_capture2_v4" },
907 /* WTAP_ENCAP_MA_WFP_CAPTURE_2V6 */
908 { "Message Analyzer WFP Capture2 v6", "message_analyzer_wfp_capture2_v6" },
910 /* WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V4 */
911 { "Message Analyzer WFP Capture Auth v4", "message_analyzer_wfp_capture_auth_v4" },
913 /* WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V6 */
914 { "Message Analyzer WFP Capture Auth v6", "message_analyzer_wfp_capture_auth_v6" },
916 /* WTAP_ENCAP_JUNIPER_ST */
917 { "Juniper Secure Tunnel Information", "juniper-st" },
919 /* WTAP_ENCAP_ETHERNET_MPACKET */
920 { "IEEE 802.3br mPackets", "ether-mpacket" },
922 /* WTAP_ENCAP_DOCSIS31_XRA31 */
923 { "DOCSIS with Excentis XRA pseudo-header", "docsis31_xra31" },
925 /* WTAP_ENCAP_DPAUXMON */
926 { "DisplayPort AUX channel with Unigraf pseudo-header", "dpauxmon" },
928 /* WTAP_ENCAP_RUBY_MARSHAL */
929 { "Ruby marshal object", "ruby_marshal" },
931 /* WTAP_ENCAP_RFC7468 */
932 { "RFC 7468 file", "rfc7468" },
934 /* WTAP_ENCAP_SYSTEMD_JOURNAL */
935 { "systemd journal", "sdjournal" }
939 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
940 static GArray* encap_table_arr = NULL;
942 #define encap_table_entry(encap) \
943 g_array_index(encap_table_arr, struct encap_type_info, encap)
945 static void wtap_init_encap_types(void) {
947 if (encap_table_arr) return;
949 encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
951 g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
954 static void wtap_cleanup_encap_types(void) {
955 if (encap_table_arr) {
956 g_array_free(encap_table_arr, TRUE);
957 encap_table_arr = NULL;
961 int wtap_get_num_encap_types(void) {
962 return wtap_num_encap_types;
966 int wtap_register_encap_type(const char* name, const char* short_name) {
967 struct encap_type_info e;
969 e.name = g_strdup(name);
970 e.short_name = g_strdup(short_name);
972 g_array_append_val(encap_table_arr,e);
974 return wtap_num_encap_types++;
978 /* Name that should be somewhat descriptive. */
980 wtap_encap_string(int encap)
982 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
984 else if (encap == WTAP_ENCAP_PER_PACKET)
987 return encap_table_entry(encap).name;
990 /* Name to use in, say, a command-line flag specifying the type. */
992 wtap_encap_short_string(int encap)
994 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
996 else if (encap == WTAP_ENCAP_PER_PACKET)
999 return encap_table_entry(encap).short_name;
1002 /* Translate a short name to a capture file type. */
1004 wtap_short_string_to_encap(const char *short_name)
1008 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
1009 if (encap_table_entry(encap).short_name != NULL &&
1010 strcmp(short_name, encap_table_entry(encap).short_name) == 0)
1013 return -1; /* no such encapsulation type */
1017 wtap_tsprec_string(int tsprec)
1021 case WTAP_TSPREC_PER_PACKET:
1024 case WTAP_TSPREC_SEC:
1027 case WTAP_TSPREC_DSEC:
1030 case WTAP_TSPREC_CSEC:
1033 case WTAP_TSPREC_MSEC:
1036 case WTAP_TSPREC_USEC:
1039 case WTAP_TSPREC_NSEC:
1042 case WTAP_TSPREC_UNKNOWN:
1050 static const char *wtap_errlist[] = {
1051 /* WTAP_ERR_NOT_REGULAR_FILE */
1052 "The file isn't a plain file or pipe",
1054 /* WTAP_ERR_RANDOM_OPEN_PIPE */
1055 "The file is being opened for random access but is a pipe",
1057 /* WTAP_ERR_FILE_UNKNOWN_FORMAT */
1058 "The file isn't a capture file in a known format",
1060 /* WTAP_ERR_UNSUPPORTED */
1061 "File contains record data we don't support",
1063 /* WTAP_ERR_CANT_WRITE_TO_PIPE */
1064 "That file format cannot be written to a pipe",
1066 /* WTAP_ERR_CANT_OPEN */
1069 /* WTAP_ERR_UNWRITABLE_FILE_TYPE */
1070 "Files can't be saved in that format",
1072 /* WTAP_ERR_UNWRITABLE_ENCAP */
1073 "Packets with that network type can't be saved in that format",
1075 /* WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED */
1076 "That file format doesn't support per-packet encapsulations",
1078 /* WTAP_ERR_CANT_WRITE */
1079 "A write failed for some unknown reason",
1081 /* WTAP_ERR_CANT_CLOSE */
1084 /* WTAP_ERR_SHORT_READ */
1085 "Less data was read than was expected",
1087 /* WTAP_ERR_BAD_FILE */
1088 "The file appears to be damaged or corrupt",
1090 /* WTAP_ERR_SHORT_WRITE */
1091 "Less data was written than was requested",
1093 /* WTAP_ERR_UNC_OVERFLOW */
1094 "Uncompression error: data would overflow buffer",
1096 /* WTAP_ERR_RANDOM_OPEN_STDIN */
1097 "The standard input cannot be opened for random access",
1099 /* WTAP_ERR_COMPRESSION_NOT_SUPPORTED */
1100 "That file format doesn't support compression",
1102 /* WTAP_ERR_CANT_SEEK */
1105 /* WTAP_ERR_CANT_SEEK_COMPRESSED */
1108 /* WTAP_ERR_DECOMPRESS */
1109 "Uncompression error",
1111 /* WTAP_ERR_INTERNAL */
1114 /* WTAP_ERR_PACKET_TOO_LARGE */
1115 "The packet being written is too large for that format",
1117 /* WTAP_ERR_CHECK_WSLUA */
1120 /* WTAP_ERR_UNWRITABLE_REC_TYPE */
1121 "That record type cannot be written in that format",
1123 /* WTAP_ERR_UNWRITABLE_REC_DATA */
1124 "That record can't be written in that format",
1126 /* WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED */
1127 "We don't support decompressing that type of compressed file",
1129 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
1132 wtap_strerror(int err)
1134 static char errbuf[128];
1135 unsigned int wtap_errlist_index;
1138 wtap_errlist_index = -1 - err;
1139 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
1140 g_snprintf(errbuf, 128, "Error %d", err);
1143 if (wtap_errlist[wtap_errlist_index] == NULL)
1144 return "Unknown reason";
1145 return wtap_errlist[wtap_errlist_index];
1147 return g_strerror(err);
1150 /* Close only the sequential side, freeing up memory it uses.
1152 Note that we do *not* want to call the subtype's close function,
1153 as it would free any per-subtype data, and that data may be
1154 needed by the random-access side.
1156 Instead, if the subtype has a "sequential close" function, we call it,
1157 to free up stuff used only by the sequential side. */
1159 wtap_sequential_close(wtap *wth)
1161 if (wth->subtype_sequential_close != NULL)
1162 (*wth->subtype_sequential_close)(wth);
1164 if (wth->fh != NULL) {
1165 file_close(wth->fh);
1169 if (wth->rec_data) {
1170 ws_buffer_free(wth->rec_data);
1171 g_free(wth->rec_data);
1172 wth->rec_data = NULL;
1177 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
1183 * Close the file descriptors for the sequential and random streams, but
1184 * don't discard any information about those streams. Used on Windows if
1185 * we need to rename a file that we have open or if we need to rename on
1186 * top of a file we have open.
1189 wtap_fdclose(wtap *wth)
1191 if (wth->fh != NULL)
1192 file_fdclose(wth->fh);
1193 if (wth->random_fh != NULL)
1194 file_fdclose(wth->random_fh);
1198 wtap_close(wtap *wth)
1200 wtap_sequential_close(wth);
1202 if (wth->subtype_close != NULL)
1203 (*wth->subtype_close)(wth);
1205 if (wth->random_fh != NULL)
1206 file_close(wth->random_fh);
1210 if (wth->fast_seek != NULL) {
1211 g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
1212 g_ptr_array_free(wth->fast_seek, TRUE);
1215 wtap_block_array_free(wth->shb_hdrs);
1216 wtap_block_array_free(wth->nrb_hdrs);
1217 wtap_block_array_free(wth->interface_data);
1223 wtap_cleareof(wtap *wth) {
1225 file_clearerr(wth->fh);
1228 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
1230 wth->add_new_ipv4 = add_new_ipv4;
1233 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
1235 wth->add_new_ipv6 = add_new_ipv6;
1239 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
1242 * Set the packet encapsulation to the file's encapsulation
1243 * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
1244 * right answer (and means that the read routine for this
1245 * capture file type doesn't have to set it), and if it
1246 * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
1249 * Do the same for the packet time stamp resolution.
1251 wth->rec.rec_header.packet_header.pkt_encap = wth->file_encap;
1252 wth->rec.tsprec = wth->file_tsprec;
1256 if (!wth->subtype_read(wth, err, err_info, data_offset)) {
1258 * If we didn't get an error indication, we read
1259 * the last packet. See if there's any deferred
1260 * error, as might, for example, occur if we're
1261 * reading a compressed file, and we got an error
1262 * reading compressed data from the file, but
1263 * got enough compressed data to decompress the
1264 * last packet of the file.
1267 *err = file_error(wth->fh, err_info);
1268 return FALSE; /* failure */
1272 * Is this a packet record?
1274 if (wth->rec.rec_type == REC_TYPE_PACKET) {
1276 * It makes no sense for the captured data length
1277 * to be bigger than the actual data length.
1279 if (wth->rec.rec_header.packet_header.caplen > wth->rec.rec_header.packet_header.len)
1280 wth->rec.rec_header.packet_header.caplen = wth->rec.rec_header.packet_header.len;
1283 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
1284 * probably means the file has that encapsulation type
1285 * but the read routine didn't set this packet's
1286 * encapsulation type.
1288 g_assert(wth->rec.rec_header.packet_header.pkt_encap != WTAP_ENCAP_PER_PACKET);
1291 return TRUE; /* success */
1295 * Read a given number of bytes from a file into a buffer or, if
1296 * buf is NULL, just discard them.
1298 * If we succeed, return TRUE.
1300 * If we get an EOF, return FALSE with *err set to 0, reporting this
1303 * If we get fewer bytes than the specified number, return FALSE with
1304 * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
1307 * If we get a read error, return FALSE with *err and *err_info set
1311 wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
1316 bytes_read = file_read(buf, count, fh);
1317 if (bytes_read < 0 || (guint)bytes_read != count) {
1318 *err = file_error(fh, err_info);
1319 if (*err == 0 && bytes_read > 0)
1320 *err = WTAP_ERR_SHORT_READ;
1327 * Read a given number of bytes from a file into a buffer or, if
1328 * buf is NULL, just discard them.
1330 * If we succeed, return TRUE.
1332 * If we get fewer bytes than the specified number, including getting
1333 * an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
1334 * this as a short read error.
1336 * If we get a read error, return FALSE with *err and *err_info set
1340 wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
1345 bytes_read = file_read(buf, count, fh);
1346 if (bytes_read < 0 || (guint)bytes_read != count) {
1347 *err = file_error(fh, err_info);
1349 *err = WTAP_ERR_SHORT_READ;
1356 * Read packet data into a Buffer, growing the buffer as necessary.
1358 * This returns an error on a short read, even if the short read hit
1359 * the EOF immediately. (The assumption is that each packet has a
1360 * header followed by raw packet data, and that we've already read the
1361 * header, so if we get an EOF trying to read the packet data, the file
1362 * has been cut short, even if the read didn't read any data at all.)
1365 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
1368 ws_buffer_assure_space(buf, length);
1369 return wtap_read_bytes(fh, ws_buffer_start_ptr(buf), length, err,
1374 * Return an approximation of the amount of data we've read sequentially
1375 * from the file so far. (gint64, in case that's 64 bits.)
1378 wtap_read_so_far(wtap *wth)
1380 return file_tell_raw(wth->fh);
1384 wtap_get_rec(wtap *wth)
1390 wtap_get_buf_ptr(wtap *wth)
1392 return ws_buffer_start_ptr(wth->rec_data);
1396 wtap_rec_init(wtap_rec *rec)
1398 memset(rec, 0, sizeof *rec);
1399 ws_buffer_init(&rec->options_buf, 0);
1403 wtap_rec_cleanup(wtap_rec *rec)
1405 ws_buffer_free(&rec->options_buf);
1409 wtap_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, Buffer *buf,
1410 int *err, gchar **err_info)
1413 * Set the packet encapsulation to the file's encapsulation
1414 * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
1415 * right answer (and means that the read routine for this
1416 * capture file type doesn't have to set it), and if it
1417 * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
1420 * Do the same for the packet time stamp resolution.
1422 rec->rec_header.packet_header.pkt_encap = wth->file_encap;
1423 rec->tsprec = wth->file_tsprec;
1427 if (!wth->subtype_seek_read(wth, seek_off, rec, buf, err, err_info))
1431 * Is this a packet record?
1433 if (rec->rec_type == REC_TYPE_PACKET) {
1435 * It makes no sense for the captured data length
1436 * to be bigger than the actual data length.
1438 if (rec->rec_header.packet_header.caplen > rec->rec_header.packet_header.len)
1439 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len;
1442 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
1443 * probably means the file has that encapsulation type
1444 * but the read routine didn't set this packet's
1445 * encapsulation type.
1447 g_assert(rec->rec_header.packet_header.pkt_encap != WTAP_ENCAP_PER_PACKET);
1454 * Initialize the library.
1457 wtap_init(gboolean load_wiretap_plugins)
1459 init_open_routines();
1460 wtap_opttypes_initialize();
1461 wtap_init_encap_types();
1462 if (load_wiretap_plugins) {
1464 libwiretap_plugins = plugins_init(WS_PLUGIN_WIRETAP);
1465 g_slist_foreach(wtap_plugins, call_plugin_register_wtap_module, NULL);
1471 * Cleanup the library
1476 wtap_cleanup_encap_types();
1477 wtap_opttypes_cleanup();
1478 ws_buffer_cleanup();
1479 cleanup_open_routines();
1481 g_slist_free(wtap_plugins);
1482 wtap_plugins = NULL;
1483 plugins_cleanup(libwiretap_plugins);
1484 libwiretap_plugins = NULL;
1489 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1494 * indent-tabs-mode: t
1497 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1498 * :indentSize=8:tabSize=8:noTabs=false: