4 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
40 #include "file_wrappers.h"
41 #include <wsutil/file_util.h>
46 #include <wsutil/plugins.h>
49 * List of wiretap plugins.
52 void (*register_wtap_module)(void); /* routine to call to register a wiretap module */
55 static GSList *wtap_plugins = NULL;
58 * Callback for each plugin found.
61 check_for_wtap_plugin(GModule *handle)
64 void (*register_wtap_module)(void);
68 * Do we have a register_wtap_module routine?
70 if (!g_module_symbol(handle, "register_wtap_module", &gp)) {
71 /* No, so this isn't a wiretap module plugin. */
76 * Yes - this plugin includes one or more wiretap modules.
78 register_wtap_module = (void (*)(void))gp;
81 * Add this one to the list of wiretap module plugins.
83 plugin = (wtap_plugin *)g_malloc(sizeof (wtap_plugin));
84 plugin->register_wtap_module = register_wtap_module;
85 wtap_plugins = g_slist_append(wtap_plugins, plugin);
90 wtap_register_plugin_types(void)
92 add_plugin_type("file format", check_for_wtap_plugin);
96 register_wtap_module_plugin(gpointer data, gpointer user_data _U_)
98 wtap_plugin *plugin = (wtap_plugin *)data;
100 (plugin->register_wtap_module)();
104 * For all wiretap module plugins, call their register routines.
107 register_all_wiretap_modules(void)
109 g_slist_foreach(wtap_plugins, register_wtap_module_plugin, NULL);
111 #endif /* HAVE_PLUGINS */
114 * Return the size of the file, as reported by the OS.
115 * (gint64, in case that's 64 bits.)
118 wtap_file_size(wtap *wth, int *err)
122 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
125 return statb.st_size;
129 * Do an fstat on the file.
132 wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
134 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
141 wtap_file_type_subtype(wtap *wth)
143 return wth->file_type_subtype;
147 wtap_iscompressed(wtap *wth)
149 return file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh);
153 wtap_snapshot_length(wtap *wth)
155 return wth->snapshot_length;
159 wtap_file_encap(wtap *wth)
161 return wth->file_encap;
165 wtap_file_tsprecision(wtap *wth)
167 return wth->tsprecision;
171 wtap_file_get_shb_info(wtap *wth)
173 wtapng_section_t *shb_hdr;
177 shb_hdr = g_new(wtapng_section_t,1);
178 shb_hdr->section_length = wth->shb_hdr.section_length;
180 shb_hdr->opt_comment = wth->shb_hdr.opt_comment; /* NULL if not available */
181 shb_hdr->shb_hardware = wth->shb_hdr.shb_hardware; /* NULL if not available, UTF-8 string containing the description of the hardware used to create this section. */
182 shb_hdr->shb_os = wth->shb_hdr.shb_os; /* NULL if not available, UTF-8 string containing the name of the operating system used to create this section. */
183 shb_hdr->shb_user_appl = wth->shb_hdr.shb_user_appl; /* NULL if not available, UTF-8 string containing the name of the application used to create this section. */
190 wtap_write_shb_comment(wtap *wth, gchar *comment)
192 g_free(wth->shb_hdr.opt_comment);
193 wth->shb_hdr.opt_comment = comment;
197 wtapng_iface_descriptions_t *
198 wtap_file_get_idb_info(wtap *wth)
200 wtapng_iface_descriptions_t *idb_info;
202 idb_info = g_new(wtapng_iface_descriptions_t,1);
204 idb_info->interface_data = wth->interface_data;
209 /* Table of the encapsulation types we know about. */
210 struct encap_type_info {
212 const char *short_name;
215 static struct encap_type_info encap_table_base[] = {
216 /* WTAP_ENCAP_UNKNOWN */
217 { "Unknown", "unknown" },
219 /* WTAP_ENCAP_ETHERNET */
220 { "Ethernet", "ether" },
222 /* WTAP_ENCAP_TOKEN_RING */
223 { "Token Ring", "tr" },
225 /* WTAP_ENCAP_SLIP */
231 /* WTAP_ENCAP_FDDI */
234 /* WTAP_ENCAP_FDDI_BITSWAPPED */
235 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
237 /* WTAP_ENCAP_RAW_IP */
238 { "Raw IP", "rawip" },
240 /* WTAP_ENCAP_ARCNET */
241 { "ARCNET", "arcnet" },
243 /* WTAP_ENCAP_ARCNET_LINUX */
244 { "Linux ARCNET", "arcnet_linux" },
246 /* WTAP_ENCAP_ATM_RFC1483 */
247 { "RFC 1483 ATM", "atm-rfc1483" },
249 /* WTAP_ENCAP_LINUX_ATM_CLIP */
250 { "Linux ATM CLIP", "linux-atm-clip" },
252 /* WTAP_ENCAP_LAPB */
255 /* WTAP_ENCAP_ATM_PDUS */
256 { "ATM PDUs", "atm-pdus" },
258 /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
259 { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
261 /* WTAP_ENCAP_NULL */
264 /* WTAP_ENCAP_ASCEND */
265 { "Lucent/Ascend access equipment", "ascend" },
267 /* WTAP_ENCAP_ISDN */
270 /* WTAP_ENCAP_IP_OVER_FC */
271 { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
273 /* WTAP_ENCAP_PPP_WITH_PHDR */
274 { "PPP with Directional Info", "ppp-with-direction" },
276 /* WTAP_ENCAP_IEEE_802_11 */
277 { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
279 /* WTAP_ENCAP_IEEE_802_11_PRISM */
280 { "IEEE 802.11 plus Prism II monitor mode radio header", "ieee-802-11-prism" },
282 /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
283 { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
285 /* WTAP_ENCAP_IEEE_802_11_RADIOTAP */
286 { "IEEE 802.11 plus radiotap radio header", "ieee-802-11-radiotap" },
288 /* WTAP_ENCAP_IEEE_802_11_AVS */
289 { "IEEE 802.11 plus AVS radio header", "ieee-802-11-avs" },
292 { "Linux cooked-mode capture", "linux-sll" },
294 /* WTAP_ENCAP_FRELAY */
295 { "Frame Relay", "frelay" },
297 /* WTAP_ENCAP_FRELAY_WITH_PHDR */
298 { "Frame Relay with Directional Info", "frelay-with-direction" },
300 /* WTAP_ENCAP_CHDLC */
301 { "Cisco HDLC", "chdlc" },
303 /* WTAP_ENCAP_CISCO_IOS */
304 { "Cisco IOS internal", "ios" },
306 /* WTAP_ENCAP_LOCALTALK */
307 { "Localtalk", "ltalk" },
309 /* WTAP_ENCAP_OLD_PFLOG */
310 { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
312 /* WTAP_ENCAP_HHDLC */
313 { "HiPath HDLC", "hhdlc" },
315 /* WTAP_ENCAP_DOCSIS */
316 { "Data Over Cable Service Interface Specification", "docsis" },
318 /* WTAP_ENCAP_COSINE */
319 { "CoSine L2 debug log", "cosine" },
321 /* WTAP_ENCAP_WFLEET_HDLC */
322 { "Wellfleet HDLC", "whdlc" },
324 /* WTAP_ENCAP_SDLC */
327 /* WTAP_ENCAP_TZSP */
328 { "Tazmen sniffer protocol", "tzsp" },
331 { "OpenBSD enc(4) encapsulating interface", "enc" },
333 /* WTAP_ENCAP_PFLOG */
334 { "OpenBSD PF Firewall logs", "pflog" },
336 /* WTAP_ENCAP_CHDLC_WITH_PHDR */
337 { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
339 /* WTAP_ENCAP_BLUETOOTH_H4 */
340 { "Bluetooth H4", "bluetooth-h4" },
342 /* WTAP_ENCAP_MTP2 */
343 { "SS7 MTP2", "mtp2" },
345 /* WTAP_ENCAP_MTP3 */
346 { "SS7 MTP3", "mtp3" },
348 /* WTAP_ENCAP_IRDA */
351 /* WTAP_ENCAP_USER0 */
352 { "USER 0", "user0" },
354 /* WTAP_ENCAP_USER1 */
355 { "USER 1", "user1" },
357 /* WTAP_ENCAP_USER2 */
358 { "USER 2", "user2" },
360 /* WTAP_ENCAP_USER3 */
361 { "USER 3", "user3" },
363 /* WTAP_ENCAP_USER4 */
364 { "USER 4", "user4" },
366 /* WTAP_ENCAP_USER5 */
367 { "USER 5", "user5" },
369 /* WTAP_ENCAP_USER6 */
370 { "USER 6", "user6" },
372 /* WTAP_ENCAP_USER7 */
373 { "USER 7", "user7" },
375 /* WTAP_ENCAP_USER8 */
376 { "USER 8", "user8" },
378 /* WTAP_ENCAP_USER9 */
379 { "USER 9", "user9" },
381 /* WTAP_ENCAP_USER10 */
382 { "USER 10", "user10" },
384 /* WTAP_ENCAP_USER11 */
385 { "USER 11", "user11" },
387 /* WTAP_ENCAP_USER12 */
388 { "USER 12", "user12" },
390 /* WTAP_ENCAP_USER13 */
391 { "USER 13", "user13" },
393 /* WTAP_ENCAP_USER14 */
394 { "USER 14", "user14" },
396 /* WTAP_ENCAP_USER15 */
397 { "USER 15", "user15" },
399 /* WTAP_ENCAP_SYMANTEC */
400 { "Symantec Enterprise Firewall", "symantec" },
402 /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
403 { "Apple IP-over-IEEE 1394", "ap1394" },
405 /* WTAP_ENCAP_BACNET_MS_TP */
406 { "BACnet MS/TP", "bacnet-ms-tp" },
408 /* WTAP_ENCAP_NETTL_RAW_ICMP */
409 { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
411 /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
412 { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
414 /* WTAP_ENCAP_GPRS_LLC */
415 { "GPRS LLC", "gprs-llc" },
417 /* WTAP_ENCAP_JUNIPER_ATM1 */
418 { "Juniper ATM1", "juniper-atm1" },
420 /* WTAP_ENCAP_JUNIPER_ATM2 */
421 { "Juniper ATM2", "juniper-atm2" },
423 /* WTAP_ENCAP_REDBACK */
424 { "Redback SmartEdge", "redback" },
426 /* WTAP_ENCAP_NETTL_RAW_IP */
427 { "Raw IP with nettl headers", "rawip-nettl" },
429 /* WTAP_ENCAP_NETTL_ETHERNET */
430 { "Ethernet with nettl headers", "ether-nettl" },
432 /* WTAP_ENCAP_NETTL_TOKEN_RING */
433 { "Token Ring with nettl headers", "tr-nettl" },
435 /* WTAP_ENCAP_NETTL_FDDI */
436 { "FDDI with nettl headers", "fddi-nettl" },
438 /* WTAP_ENCAP_NETTL_UNKNOWN */
439 { "Unknown link-layer type with nettl headers", "unknown-nettl" },
441 /* WTAP_ENCAP_MTP2_WITH_PHDR */
442 { "MTP2 with pseudoheader", "mtp2-with-phdr" },
444 /* WTAP_ENCAP_JUNIPER_PPPOE */
445 { "Juniper PPPoE", "juniper-pppoe" },
447 /* WTAP_ENCAP_GCOM_TIE1 */
448 { "GCOM TIE1", "gcom-tie1" },
450 /* WTAP_ENCAP_GCOM_SERIAL */
451 { "GCOM Serial", "gcom-serial" },
453 /* WTAP_ENCAP_NETTL_X25 */
454 { "X.25 with nettl headers", "x25-nettl" },
457 { "K12 protocol analyzer", "k12" },
459 /* WTAP_ENCAP_JUNIPER_MLPPP */
460 { "Juniper MLPPP", "juniper-mlppp" },
462 /* WTAP_ENCAP_JUNIPER_MLFR */
463 { "Juniper MLFR", "juniper-mlfr" },
465 /* WTAP_ENCAP_JUNIPER_ETHER */
466 { "Juniper Ethernet", "juniper-ether" },
468 /* WTAP_ENCAP_JUNIPER_PPP */
469 { "Juniper PPP", "juniper-ppp" },
471 /* WTAP_ENCAP_JUNIPER_FRELAY */
472 { "Juniper Frame-Relay", "juniper-frelay" },
474 /* WTAP_ENCAP_JUNIPER_CHDLC */
475 { "Juniper C-HDLC", "juniper-chdlc" },
477 /* WTAP_ENCAP_JUNIPER_GGSN */
478 { "Juniper GGSN", "juniper-ggsn" },
480 /* WTAP_ENCAP_LINUX_LAPD */
481 { "LAPD with Linux pseudo-header", "linux-lapd" },
483 /* WTAP_ENCAP_CATAPULT_DCT2000 */
484 { "Catapult DCT2000", "dct2000" },
487 { "ASN.1 Basic Encoding Rules", "ber" },
489 /* WTAP_ENCAP_JUNIPER_VP */
490 { "Juniper Voice PIC", "juniper-vp" },
493 { "Raw USB packets", "usb" },
495 /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
496 { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
498 /* WTAP_ENCAP_NETTL_RAW_TELNET */
499 { "Raw telnet with nettl headers", "raw-telnet-nettl" },
501 /* WTAP_ENCAP_USB_LINUX */
502 { "USB packets with Linux header", "usb-linux" },
504 /* WTAP_ENCAP_MPEG */
508 { "Per-Packet Information header", "ppi" },
511 { "Extensible Record Format", "erf" },
513 /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
514 { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
516 /* WTAP_ENCAP_SITA */
517 { "SITA WAN packets", "sita-wan" },
519 /* WTAP_ENCAP_SCCP */
520 { "SS7 SCCP", "sccp" },
522 /* WTAP_ENCAP_BLUETOOTH_HCI */
523 { "Bluetooth without transport layer", "bluetooth-hci" },
525 /* WTAP_ENCAP_IPMB */
526 { "Intelligent Platform Management Bus", "ipmb" },
528 /* WTAP_ENCAP_IEEE802_15_4 */
529 { "IEEE 802.15.4 Wireless PAN", "wpan" },
531 /* WTAP_ENCAP_X2E_XORAYA */
532 { "X2E Xoraya", "x2e-xoraya" },
534 /* WTAP_ENCAP_FLEXRAY */
535 { "FlexRay", "flexray" },
538 { "Local Interconnect Network", "lin" },
540 /* WTAP_ENCAP_MOST */
541 { "Media Oriented Systems Transport", "most" },
543 /* WTAP_ENCAP_CAN20B */
544 { "Controller Area Network 2.0B", "can20b" },
546 /* WTAP_ENCAP_LAYER1_EVENT */
547 { "EyeSDN Layer 1 event", "layer1-event" },
549 /* WTAP_ENCAP_X2E_SERIAL */
550 { "X2E serial line capture", "x2e-serial" },
555 /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
556 { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
558 /* WTAP_ENCAP_TNEF */
559 { "Transport-Neutral Encapsulation Format", "tnef" },
561 /* WTAP_ENCAP_USB_LINUX_MMAPPED */
562 { "USB packets with Linux header and padding", "usb-linux-mmap" },
564 /* WTAP_ENCAP_GSM_UM */
565 { "GSM Um Interface", "gsm_um" },
567 /* WTAP_ENCAP_DPNSS */
568 { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
570 /* WTAP_ENCAP_PACKETLOGGER */
571 { "PacketLogger", "packetlogger" },
573 /* WTAP_ENCAP_NSTRACE_1_0 */
574 { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
576 /* WTAP_ENCAP_NSTRACE_2_0 */
577 { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
579 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
580 { "Fibre Channel FC-2", "fc2" },
582 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
583 { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
585 /* WTAP_ENCAP_JPEG_JFIF */
586 { "JPEG/JFIF", "jfif" },
588 /* WTAP_ENCAP_IPNET */
589 { "Solaris IPNET", "ipnet" },
591 /* WTAP_ENCAP_SOCKETCAN */
592 { "SocketCAN", "socketcan" },
594 /* WTAP_ENCAP_IEEE_802_11_NETMON */
595 { "IEEE 802.11 plus Network Monitor radio header", "ieee-802-11-netmon" },
597 /* WTAP_ENCAP_IEEE802_15_4_NOFCS */
598 { "IEEE 802.15.4 Wireless PAN with FCS not present", "wpan-nofcs" },
600 /* WTAP_ENCAP_RAW_IPFIX */
601 { "IPFIX", "ipfix" },
603 /* WTAP_ENCAP_RAW_IP4 */
604 { "Raw IPv4", "rawip4" },
606 /* WTAP_ENCAP_RAW_IP6 */
607 { "Raw IPv6", "rawip6" },
609 /* WTAP_ENCAP_LAPD */
612 /* WTAP_ENCAP_DVBCI */
613 { "DVB-CI (Common Interface)", "dvbci"},
615 /* WTAP_ENCAP_MUX27010 */
616 { "MUX27010", "mux27010"},
618 /* WTAP_ENCAP_MIME */
621 /* WTAP_ENCAP_NETANALYZER */
622 { "netANALYZER", "netanalyzer" },
624 /* WTAP_ENCAP_NETANALYZER_TRANSPARENT */
625 { "netANALYZER-Transparent", "netanalyzer-transparent" },
627 /* WTAP_ENCAP_IP_OVER_IB */
628 { "IP over Infiniband", "ip-over-ib" },
630 /* WTAP_ENCAP_MPEG_2_TS */
631 { "ISO/IEC 13818-1 MPEG2-TS", "mp2ts" },
633 /* WTAP_ENCAP_PPP_ETHER */
634 { "PPP-over-Ethernet session", "pppoes" },
636 /* WTAP_ENCAP_NFC_LLCP */
637 { "NFC LLCP", "nfc-llcp" },
639 /* WTAP_ENCAP_NFLOG */
640 { "NFLOG", "nflog" },
642 /* WTAP_ENCAP_V5_EF */
643 { "V5 Envelope Function", "v5-ef" },
645 /* WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR */
646 { "BACnet MS/TP with Directional Info", "bacnet-ms-tp-with-direction" },
648 /* WTAP_ENCAP_IXVERIWAVE */
649 { "IxVeriWave header and stats block", "ixveriwave" },
651 /* WTAP_ENCAP_IEEE_802_11_AIROPEEK */
652 { "IEEE 802.11 plus AiroPeek radio header", "ieee-802-11-airopeek" },
657 /* WTAP_ENCAP_DBUS */
660 /* WTAP_ENCAP_AX25_KISS */
661 { "AX.25 with KISS header", "ax25-kiss" },
663 /* WTAP_ENCAP_AX25 */
664 { "Amateur Radio AX.25", "ax25" },
666 /* WTAP_ENCAP_SCTP */
669 /* WTAP_ENCAP_INFINIBAND */
670 { "InfiniBand", "infiniband" },
672 /* WTAP_ENCAP_JUNIPER_SVCS */
673 { "Juniper Services", "juniper-svcs" },
675 /* WTAP_ENCAP_USBPCAP */
676 { "USB packets with USBPcap header", "usb-usbpcap" },
678 /* WTAP_ENCAP_RTAC_SERIAL */
679 { "RTAC serial-line", "rtac-serial" },
681 /* WTAP_ENCAP_BLUETOOTH_LE_LL */
682 { "Bluetooth Low Energy Link Layer", "bluetooth-le-ll" },
684 /* WTAP_ENCAP_WIRESHARK_UPPER_PDU */
685 { "Wireshark Upper PDU export", "wireshark-upper-pdu" },
687 /* WTAP_ENCAP_STANAG_4607 */
688 { "STANAG 4607", "s4607" },
690 /* WTAP_ENCAP_STANAG_5066_D_PDU */
691 { "STANAG 5066 Data Transfer Sublayer PDUs(D_PDU)", "s5066-dpdu"},
693 /* WTAP_ENCAP_NETLINK */
694 { "Netlink", "Linux netlink" },
696 /* WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
697 { "Bluetooth Linux Monitor", "bluetooth-linux-monitor" },
699 /* WTAP_ENCAP_BLUETOOTH_BREDR_BB */
700 { "Bluetooth BR/EDR Baseband RF", "bluetooth-bredr-bb-rf" },
702 /* WTAP_ENCAP_BLUETOOTH_LE_LL_WITH_PHDR */
703 { "Bluetooth Low Energy Link Layer RF", "bluetooth-le-ll-rf" },
705 /* WTAP_ENCAP_NSTRACE_3_0 */
706 { "NetScaler Encapsulation 3.0 of Ethernet", "nstrace30" },
708 /* WTAP_ENCAP_LOGCAT */
709 { "Android Logcat Binary format", "logcat" },
711 /* WTAP_ENCAP_LOGCAT_BRIEF */
712 { "Android Logcat Brief text format", "logcat_brief" },
714 /* WTAP_ENCAP_LOGCAT_PROCESS */
715 { "Android Logcat Process text format", "logcat_process" },
717 /* WTAP_ENCAP_LOGCAT_TAG */
718 { "Android Logcat Tag text format", "logcat_tag" },
720 /* WTAP_ENCAP_LOGCAT_THREAD */
721 { "Android Logcat Thread text format", "logcat_thread" },
723 /* WTAP_ENCAP_LOGCAT_TIME */
724 { "Android Logcat Time text format", "logcat_time" },
726 /* WTAP_ENCAP_LOGCAT_THREADTIME */
727 { "Android Logcat Threadtime text format", "logcat_threadtime" },
729 /* WTAP_ENCAP_LOGCAT_LONG */
730 { "Android Logcat Long text format", "logcat_long" },
732 /* WTAP_ENCAP_PKTAP */
733 { "Apple PKTAP", "pktap" },
735 /* WTAP_ENCAP_EPON */
736 { "Ethernet Passive Optical Network", "epon" },
740 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
741 static GArray* encap_table_arr = NULL;
743 #define encap_table_entry(encap) \
744 g_array_index(encap_table_arr, struct encap_type_info, encap)
746 static void wtap_init_encap_types(void) {
748 if (encap_table_arr) return;
750 encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
752 g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
755 int wtap_get_num_encap_types(void) {
756 wtap_init_encap_types();
757 return wtap_num_encap_types;
761 int wtap_register_encap_type(const char* name, const char* short_name) {
762 struct encap_type_info e;
763 wtap_init_encap_types();
765 e.name = g_strdup(name);
766 e.short_name = g_strdup(short_name);
768 g_array_append_val(encap_table_arr,e);
770 return wtap_num_encap_types++;
774 /* Name that should be somewhat descriptive. */
776 wtap_encap_string(int encap)
778 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
780 else if (encap == WTAP_ENCAP_PER_PACKET)
783 return encap_table_entry(encap).name;
786 /* Name to use in, say, a command-line flag specifying the type. */
788 wtap_encap_short_string(int encap)
790 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
792 else if (encap == WTAP_ENCAP_PER_PACKET)
795 return encap_table_entry(encap).short_name;
798 /* Translate a short name to a capture file type. */
800 wtap_short_string_to_encap(const char *short_name)
804 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
805 if (encap_table_entry(encap).short_name != NULL &&
806 strcmp(short_name, encap_table_entry(encap).short_name) == 0)
809 return -1; /* no such encapsulation type */
812 static const char *wtap_errlist[] = {
813 "The file isn't a plain file or pipe",
814 "The file is being opened for random access but is a pipe",
815 "The file isn't a capture file in a known format",
816 "File contains record data we don't support",
817 "That file format cannot be written to a pipe",
819 "Files can't be saved in that format",
820 "Files from that network type can't be saved in that format",
821 "That file format doesn't support per-packet encapsulations",
824 "Less data was read than was expected",
825 "The file appears to be damaged or corrupt",
826 "Less data was written than was requested",
827 "Uncompression error: data oddly truncated",
828 "Uncompression error: data would overflow buffer",
829 "Uncompression error: bad LZ77 offset",
830 "The standard input cannot be opened for random access",
831 "That file format doesn't support compression",
834 "Uncompression error",
836 "The packet being written is too large for that format"
838 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
841 wtap_strerror(int err)
843 static char errbuf[128];
844 unsigned int wtap_errlist_index;
847 wtap_errlist_index = -1 - err;
848 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
849 g_snprintf(errbuf, 128, "Error %d", err);
852 if (wtap_errlist[wtap_errlist_index] == NULL)
853 return "Unknown reason";
854 return wtap_errlist[wtap_errlist_index];
856 return g_strerror(err);
859 /* Close only the sequential side, freeing up memory it uses.
861 Note that we do *not* want to call the subtype's close function,
862 as it would free any per-subtype data, and that data may be
863 needed by the random-access side.
865 Instead, if the subtype has a "sequential close" function, we call it,
866 to free up stuff used only by the sequential side. */
868 wtap_sequential_close(wtap *wth)
870 if (wth->subtype_sequential_close != NULL)
871 (*wth->subtype_sequential_close)(wth);
873 if (wth->fh != NULL) {
878 if (wth->frame_buffer) {
879 buffer_free(wth->frame_buffer);
880 g_free(wth->frame_buffer);
881 wth->frame_buffer = NULL;
886 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
892 * Close the file descriptors for the sequential and random streams, but
893 * don't discard any information about those streams. Used on Windows if
894 * we need to rename a file that we have open or if we need to rename on
895 * top of a file we have open.
898 wtap_fdclose(wtap *wth)
901 file_fdclose(wth->fh);
902 if (wth->random_fh != NULL)
903 file_fdclose(wth->random_fh);
907 wtap_close(wtap *wth)
910 wtapng_if_descr_t *wtapng_if_descr;
911 wtapng_if_stats_t *if_stats;
913 wtap_sequential_close(wth);
915 if (wth->subtype_close != NULL)
916 (*wth->subtype_close)(wth);
918 if (wth->random_fh != NULL)
919 file_close(wth->random_fh);
921 if (wth->priv != NULL)
924 if (wth->fast_seek != NULL) {
925 g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
926 g_ptr_array_free(wth->fast_seek, TRUE);
928 for(i = 0; i < wth->interface_data->len; i++) {
929 wtapng_if_descr = &g_array_index(wth->interface_data, wtapng_if_descr_t, i);
930 if(wtapng_if_descr->opt_comment != NULL){
931 g_free(wtapng_if_descr->opt_comment);
933 if(wtapng_if_descr->if_name != NULL){
934 g_free(wtapng_if_descr->if_name);
936 if(wtapng_if_descr->if_description != NULL){
937 g_free(wtapng_if_descr->if_description);
939 if(wtapng_if_descr->if_filter_str != NULL){
940 g_free(wtapng_if_descr->if_filter_str);
942 if(wtapng_if_descr->if_filter_bpf_bytes != NULL){
943 g_free(wtapng_if_descr->if_filter_bpf_bytes);
945 if(wtapng_if_descr->if_os != NULL){
946 g_free(wtapng_if_descr->if_os);
948 for(j = 0; j < wtapng_if_descr->num_stat_entries; j++) {
949 if_stats = &g_array_index(wtapng_if_descr->interface_statistics, wtapng_if_stats_t, j);
950 if(if_stats->opt_comment != NULL){
951 g_free(if_stats->opt_comment);
954 if(wtapng_if_descr->num_stat_entries != 0){
955 g_array_free(wtapng_if_descr->interface_statistics, TRUE);
958 g_array_free(wth->interface_data, TRUE);
963 wtap_cleareof(wtap *wth) {
965 file_clearerr(wth->fh);
968 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
970 wth->add_new_ipv4 = add_new_ipv4;
973 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
975 wth->add_new_ipv6 = add_new_ipv6;
979 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
984 * Set the packet encapsulation to the file's encapsulation
985 * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
986 * right answer (and means that the read routine for this
987 * capture file type doesn't have to set it), and if it
988 * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
991 wth->phdr.pkt_encap = wth->file_encap;
993 rectype = wth->subtype_read(wth, err, err_info, data_offset);
996 * If we didn't get an error indication, we read
997 * the last packet. See if there's any deferred
998 * error, as might, for example, occur if we're
999 * reading a compressed file, and we got an error
1000 * reading compressed data from the file, but
1001 * got enough compressed data to decompress the
1002 * last packet of the file.
1005 *err = file_error(wth->fh, err_info);
1006 return rectype; /* failure */
1010 * It makes no sense for the captured data length to be bigger
1011 * than the actual data length.
1013 if (wth->phdr.caplen > wth->phdr.len)
1014 wth->phdr.caplen = wth->phdr.len;
1017 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
1018 * probably means the file has that encapsulation type
1019 * but the read routine didn't set this packet's
1020 * encapsulation type.
1022 g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
1028 * Read packet data into a Buffer, growing the buffer as necessary.
1030 * This returns an error on a short read, even if the short read hit
1031 * the EOF immediately. (The assumption is that each packet has a
1032 * header followed by raw packet data, and that we've already read the
1033 * header, so if we get an EOF trying to read the packet data, the file
1034 * has been cut short, even if the read didn't read any data at all.)
1037 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
1042 buffer_assure_space(buf, length);
1043 errno = WTAP_ERR_CANT_READ;
1044 bytes_read = file_read(buffer_start_ptr(buf), length, fh);
1046 if (bytes_read < 0 || (guint)bytes_read != length) {
1047 *err = file_error(fh, err_info);
1049 *err = WTAP_ERR_SHORT_READ;
1056 * Return an approximation of the amount of data we've read sequentially
1057 * from the file so far. (gint64, in case that's 64 bits.)
1060 wtap_read_so_far(wtap *wth)
1062 return file_tell_raw(wth->fh);
1065 struct wtap_pkthdr *
1066 wtap_phdr(wtap *wth)
1072 wtap_buf_ptr(wtap *wth)
1074 return buffer_start_ptr(wth->frame_buffer);
1078 wtap_seek_read(wtap *wth, gint64 seek_off,
1079 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
1083 rectype = wth->subtype_seek_read(wth, seek_off, phdr, buf, err, err_info);
1088 * It makes no sense for the captured data length to be bigger
1089 * than the actual data length.
1091 if (wth->phdr.caplen > wth->phdr.len)
1092 wth->phdr.caplen = wth->phdr.len;
1095 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
1096 * probably means the file has that encapsulation type
1097 * but the read routine didn't set this packet's
1098 * encapsulation type.
1100 g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);