6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
42 #include "file_wrappers.h"
43 #include <wsutil/file_util.h>
47 * Return the size of the file, as reported by the OS.
48 * (gint64, in case that's 64 bits.)
51 wtap_file_size(wtap *wth, int *err)
55 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
62 * Do an fstat on the file.
65 wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
67 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
74 wtap_file_type(wtap *wth)
76 return wth->file_type;
80 wtap_iscompressed(wtap *wth)
82 return file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh);
86 wtap_snapshot_length(wtap *wth)
88 return wth->snapshot_length;
92 wtap_file_encap(wtap *wth)
94 return wth->file_encap;
98 wtap_file_tsprecision(wtap *wth)
100 return wth->tsprecision;
104 wtap_file_get_shb_info(wtap *wth)
106 wtapng_section_t *shb_hdr;
110 shb_hdr = g_new(wtapng_section_t,1);
111 shb_hdr->section_length = wth->shb_hdr.section_length;
113 shb_hdr->opt_comment = wth->shb_hdr.opt_comment; /* NULL if not available */
114 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. */
115 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. */
116 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. */
123 wtap_write_shb_comment(wtap *wth, gchar *comment)
125 g_free(wth->shb_hdr.opt_comment);
126 wth->shb_hdr.opt_comment = comment;
130 wtapng_iface_descriptions_t *
131 wtap_file_get_idb_info(wtap *wth)
133 wtapng_iface_descriptions_t *idb_info;
135 idb_info = g_new(wtapng_iface_descriptions_t,1);
137 idb_info->number_of_interfaces = wth->number_of_interfaces;
138 idb_info->interface_data = wth->interface_data;
143 /* Table of the encapsulation types we know about. */
144 struct encap_type_info {
146 const char *short_name;
149 static struct encap_type_info encap_table_base[] = {
150 /* WTAP_ENCAP_UNKNOWN */
151 { "Unknown", "unknown" },
153 /* WTAP_ENCAP_ETHERNET */
154 { "Ethernet", "ether" },
156 /* WTAP_ENCAP_TOKEN_RING */
157 { "Token Ring", "tr" },
159 /* WTAP_ENCAP_SLIP */
165 /* WTAP_ENCAP_FDDI */
168 /* WTAP_ENCAP_FDDI_BITSWAPPED */
169 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
171 /* WTAP_ENCAP_RAW_IP */
172 { "Raw IP", "rawip" },
174 /* WTAP_ENCAP_ARCNET */
175 { "ARCNET", "arcnet" },
177 /* WTAP_ENCAP_ARCNET_LINUX */
178 { "Linux ARCNET", "arcnet_linux" },
180 /* WTAP_ENCAP_ATM_RFC1483 */
181 { "RFC 1483 ATM", "atm-rfc1483" },
183 /* WTAP_ENCAP_LINUX_ATM_CLIP */
184 { "Linux ATM CLIP", "linux-atm-clip" },
186 /* WTAP_ENCAP_LAPB */
189 /* WTAP_ENCAP_ATM_PDUS */
190 { "ATM PDUs", "atm-pdus" },
192 /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
193 { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
195 /* WTAP_ENCAP_NULL */
198 /* WTAP_ENCAP_ASCEND */
199 { "Lucent/Ascend access equipment", "ascend" },
201 /* WTAP_ENCAP_ISDN */
204 /* WTAP_ENCAP_IP_OVER_FC */
205 { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
207 /* WTAP_ENCAP_PPP_WITH_PHDR */
208 { "PPP with Directional Info", "ppp-with-direction" },
210 /* WTAP_ENCAP_IEEE_802_11 */
211 { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
213 /* WTAP_ENCAP_IEEE_802_11_PRISM */
214 { "IEEE 802.11 plus Prism II monitor mode radio header", "ieee-802-11-prism" },
216 /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
217 { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
219 /* WTAP_ENCAP_IEEE_802_11_RADIOTAP */
220 { "IEEE 802.11 plus radiotap radio header", "ieee-802-11-radiotap" },
222 /* WTAP_ENCAP_IEEE_802_11_AVS */
223 { "IEEE 802.11 plus AVS radio header", "ieee-802-11-avs" },
226 { "Linux cooked-mode capture", "linux-sll" },
228 /* WTAP_ENCAP_FRELAY */
229 { "Frame Relay", "frelay" },
231 /* WTAP_ENCAP_FRELAY_WITH_PHDR */
232 { "Frame Relay with Directional Info", "frelay-with-direction" },
234 /* WTAP_ENCAP_CHDLC */
235 { "Cisco HDLC", "chdlc" },
237 /* WTAP_ENCAP_CISCO_IOS */
238 { "Cisco IOS internal", "ios" },
240 /* WTAP_ENCAP_LOCALTALK */
241 { "Localtalk", "ltalk" },
243 /* WTAP_ENCAP_OLD_PFLOG */
244 { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
246 /* WTAP_ENCAP_HHDLC */
247 { "HiPath HDLC", "hhdlc" },
249 /* WTAP_ENCAP_DOCSIS */
250 { "Data Over Cable Service Interface Specification", "docsis" },
252 /* WTAP_ENCAP_COSINE */
253 { "CoSine L2 debug log", "cosine" },
255 /* WTAP_ENCAP_WFLEET_HDLC */
256 { "Wellfleet HDLC", "whdlc" },
258 /* WTAP_ENCAP_SDLC */
261 /* WTAP_ENCAP_TZSP */
262 { "Tazmen sniffer protocol", "tzsp" },
265 { "OpenBSD enc(4) encapsulating interface", "enc" },
267 /* WTAP_ENCAP_PFLOG */
268 { "OpenBSD PF Firewall logs", "pflog" },
270 /* WTAP_ENCAP_CHDLC_WITH_PHDR */
271 { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
273 /* WTAP_ENCAP_BLUETOOTH_H4 */
274 { "Bluetooth H4", "bluetooth-h4" },
276 /* WTAP_ENCAP_MTP2 */
277 { "SS7 MTP2", "mtp2" },
279 /* WTAP_ENCAP_MTP3 */
280 { "SS7 MTP3", "mtp3" },
282 /* WTAP_ENCAP_IRDA */
285 /* WTAP_ENCAP_USER0 */
286 { "USER 0", "user0" },
288 /* WTAP_ENCAP_USER1 */
289 { "USER 1", "user1" },
291 /* WTAP_ENCAP_USER2 */
292 { "USER 2", "user2" },
294 /* WTAP_ENCAP_USER3 */
295 { "USER 3", "user3" },
297 /* WTAP_ENCAP_USER4 */
298 { "USER 4", "user4" },
300 /* WTAP_ENCAP_USER5 */
301 { "USER 5", "user5" },
303 /* WTAP_ENCAP_USER6 */
304 { "USER 6", "user6" },
306 /* WTAP_ENCAP_USER7 */
307 { "USER 7", "user7" },
309 /* WTAP_ENCAP_USER8 */
310 { "USER 8", "user8" },
312 /* WTAP_ENCAP_USER9 */
313 { "USER 9", "user9" },
315 /* WTAP_ENCAP_USER10 */
316 { "USER 10", "user10" },
318 /* WTAP_ENCAP_USER11 */
319 { "USER 11", "user11" },
321 /* WTAP_ENCAP_USER12 */
322 { "USER 12", "user12" },
324 /* WTAP_ENCAP_USER13 */
325 { "USER 13", "user13" },
327 /* WTAP_ENCAP_USER14 */
328 { "USER 14", "user14" },
330 /* WTAP_ENCAP_USER15 */
331 { "USER 15", "user15" },
333 /* WTAP_ENCAP_SYMANTEC */
334 { "Symantec Enterprise Firewall", "symantec" },
336 /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
337 { "Apple IP-over-IEEE 1394", "ap1394" },
339 /* WTAP_ENCAP_BACNET_MS_TP */
340 { "BACnet MS/TP", "bacnet-ms-tp" },
342 /* WTAP_ENCAP_NETTL_RAW_ICMP */
343 { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
345 /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
346 { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
348 /* WTAP_ENCAP_GPRS_LLC */
349 { "GPRS LLC", "gprs-llc" },
351 /* WTAP_ENCAP_JUNIPER_ATM1 */
352 { "Juniper ATM1", "juniper-atm1" },
354 /* WTAP_ENCAP_JUNIPER_ATM2 */
355 { "Juniper ATM2", "juniper-atm2" },
357 /* WTAP_ENCAP_REDBACK */
358 { "Redback SmartEdge", "redback" },
360 /* WTAP_ENCAP_NETTL_RAW_IP */
361 { "Raw IP with nettl headers", "rawip-nettl" },
363 /* WTAP_ENCAP_NETTL_ETHERNET */
364 { "Ethernet with nettl headers", "ether-nettl" },
366 /* WTAP_ENCAP_NETTL_TOKEN_RING */
367 { "Token Ring with nettl headers", "tr-nettl" },
369 /* WTAP_ENCAP_NETTL_FDDI */
370 { "FDDI with nettl headers", "fddi-nettl" },
372 /* WTAP_ENCAP_NETTL_UNKNOWN */
373 { "Unknown link-layer type with nettl headers", "unknown-nettl" },
375 /* WTAP_ENCAP_MTP2_WITH_PHDR */
376 { "MTP2 with pseudoheader", "mtp2-with-phdr" },
378 /* WTAP_ENCAP_JUNIPER_PPPOE */
379 { "Juniper PPPoE", "juniper-pppoe" },
381 /* WTAP_ENCAP_GCOM_TIE1 */
382 { "GCOM TIE1", "gcom-tie1" },
384 /* WTAP_ENCAP_GCOM_SERIAL */
385 { "GCOM Serial", "gcom-serial" },
387 /* WTAP_ENCAP_NETTL_X25 */
388 { "X.25 with nettl headers", "x25-nettl" },
391 { "K12 protocol analyzer", "k12" },
393 /* WTAP_ENCAP_JUNIPER_MLPPP */
394 { "Juniper MLPPP", "juniper-mlppp" },
396 /* WTAP_ENCAP_JUNIPER_MLFR */
397 { "Juniper MLFR", "juniper-mlfr" },
399 /* WTAP_ENCAP_JUNIPER_ETHER */
400 { "Juniper Ethernet", "juniper-ether" },
402 /* WTAP_ENCAP_JUNIPER_PPP */
403 { "Juniper PPP", "juniper-ppp" },
405 /* WTAP_ENCAP_JUNIPER_FRELAY */
406 { "Juniper Frame-Relay", "juniper-frelay" },
408 /* WTAP_ENCAP_JUNIPER_CHDLC */
409 { "Juniper C-HDLC", "juniper-chdlc" },
411 /* WTAP_ENCAP_JUNIPER_GGSN */
412 { "Juniper GGSN", "juniper-ggsn" },
414 /* WTAP_ENCAP_LINUX_LAPD */
415 { "LAPD with Linux pseudo-header", "linux-lapd" },
417 /* WTAP_ENCAP_CATAPULT_DCT2000 */
418 { "Catapult DCT2000", "dct2000" },
421 { "ASN.1 Basic Encoding Rules", "ber" },
423 /* WTAP_ENCAP_JUNIPER_VP */
424 { "Juniper Voice PIC", "juniper-vp" },
427 { "Raw USB packets", "usb" },
429 /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
430 { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
432 /* WTAP_ENCAP_NETTL_RAW_TELNET */
433 { "Raw telnet with nettl headers", "raw-telnet-nettl" },
435 /* WTAP_ENCAP_USB_LINUX */
436 { "USB packets with Linux header", "usb-linux" },
438 /* WTAP_ENCAP_MPEG */
442 { "Per-Packet Information header", "ppi" },
445 { "Extensible Record Format", "erf" },
447 /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
448 { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
450 /* WTAP_ENCAP_SITA */
451 { "SITA WAN packets", "sita-wan" },
453 /* WTAP_ENCAP_SCCP */
454 { "SS7 SCCP", "sccp" },
456 /* WTAP_ENCAP_BLUETOOTH_HCI */
457 { "Bluetooth without transport layer", "bluetooth-hci" },
459 /* WTAP_ENCAP_IPMB */
460 { "Intelligent Platform Management Bus", "ipmb" },
462 /* WTAP_ENCAP_IEEE802_15_4 */
463 { "IEEE 802.15.4 Wireless PAN", "wpan" },
465 /* WTAP_ENCAP_X2E_XORAYA */
466 { "X2E Xoraya", "x2e-xoraya" },
468 /* WTAP_ENCAP_FLEXRAY */
469 { "FlexRay", "flexray" },
472 { "Local Interconnect Network", "lin" },
474 /* WTAP_ENCAP_MOST */
475 { "Media Oriented Systems Transport", "most" },
477 /* WTAP_ENCAP_CAN20B */
478 { "Controller Area Network 2.0B", "can20b" },
480 /* WTAP_ENCAP_LAYER1_EVENT */
481 { "EyeSDN Layer 1 event", "layer1-event" },
483 /* WTAP_ENCAP_X2E_SERIAL */
484 { "X2E serial line capture", "x2e-serial" },
489 /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
490 { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
492 /* WTAP_ENCAP_TNEF */
493 { "Transport-Neutral Encapsulation Format", "tnef" },
495 /* WTAP_ENCAP_USB_LINUX_MMAPPED */
496 { "USB packets with Linux header and padding", "usb-linux-mmap" },
498 /* WTAP_ENCAP_GSM_UM */
499 { "GSM Um Interface", "gsm_um" },
501 /* WTAP_ENCAP_DPNSS */
502 { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
504 /* WTAP_ENCAP_PACKETLOGGER */
505 { "PacketLogger", "packetlogger" },
507 /* WTAP_ENCAP_NSTRACE_1_0 */
508 { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
510 /* WTAP_ENCAP_NSTRACE_2_0 */
511 { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
513 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
514 { "Fibre Channel FC-2", "fc2" },
516 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
517 { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
519 /* WTAP_ENCAP_JPEG_JFIF */
520 { "JPEG/JFIF", "jfif" },
522 /* WTAP_ENCAP_IPNET */
523 { "Solaris IPNET", "ipnet" },
525 /* WTAP_ENCAP_SOCKETCAN */
526 { "SocketCAN", "socketcan" },
528 /* WTAP_ENCAP_IEEE_802_11_NETMON */
529 { "IEEE 802.11 plus Network Monitor radio header", "ieee-802-11-netmon" },
531 /* WTAP_ENCAP_IEEE802_15_4_NOFCS */
532 { "IEEE 802.15.4 Wireless PAN with FCS not present", "wpan-nofcs" },
534 /* WTAP_ENCAP_RAW_IPFIX */
535 { "IPFIX", "ipfix" },
537 /* WTAP_ENCAP_RAW_IP4 */
538 { "Raw IPv4", "rawip4" },
540 /* WTAP_ENCAP_RAW_IP6 */
541 { "Raw IPv6", "rawip6" },
543 /* WTAP_ENCAP_LAPD */
546 /* WTAP_ENCAP_DVBCI */
547 { "DVB-CI (Common Interface)", "dvbci"},
549 /* WTAP_ENCAP_MUX27010 */
550 { "MUX27010", "mux27010"},
552 /* WTAP_ENCAP_MIME */
555 /* WTAP_ENCAP_NETANALYZER */
556 { "netANALYZER", "netanalyzer" },
558 /* WTAP_ENCAP_NETANALYZER_TRANSPARENT */
559 { "netANALYZER-Transparent", "netanalyzer-transparent" },
561 /* WTAP_ENCAP_IP_OVER_IB */
562 { "IP over Infiniband", "ip-over-ib" },
564 /* WTAP_ENCAP_MPEG_2_TS */
565 { "ISO/IEC 13818-1 MPEG2-TS", "mp2ts" },
567 /* WTAP_ENCAP_PPP_ETHER */
568 { "PPP-over-Ethernet session", "pppoes" },
570 /* WTAP_ENCAP_NFC_LLCP */
571 { "NFC LLCP", "nfc-llcp" },
573 /* WTAP_ENCAP_NFLOG */
574 { "NFLOG", "nflog" },
576 /* WTAP_ENCAP_V5_EF */
577 { "V5 Envelope Function", "v5-ef" },
579 /* WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR */
580 { "BACnet MS/TP with Directional Info", "bacnet-ms-tp-with-direction" },
582 /* WTAP_ENCAP_IXVERIWAVE */
583 { "IxVeriWave header and stats block", "ixveriwave" },
585 /* WTAP_ENCAP_IEEE_802_11_AIROPEEK */
586 { "IEEE 802.11 plus AiroPeek radio header", "ieee-802-11-airopeek" },
591 /* WTAP_ENCAP_DBUS */
594 /* WTAP_ENCAP_AX25_KISS */
595 { "AX.25 with KISS header", "ax25-kiss" },
597 /* WTAP_ENCAP_AX25 */
598 { "Amateur Radio AX.25", "ax25" },
600 /* WTAP_ENCAP_SCTP */
603 /* WTAP_ENCAP_INFINIBAND */
604 { "InfiniBand", "infiniband" },
606 /* WTAP_ENCAP_JUNIPER_SVCS */
607 { "Juniper Services", "juniper-svcs" },
609 /* WTAP_ENCAP_USBPCAP */
610 { "USB packets with USBPcap header", "usb-usbpcap" },
614 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
615 static GArray* encap_table_arr = NULL;
616 static const struct encap_type_info* encap_table = NULL;
618 static void wtap_init_encap_types(void) {
620 if (encap_table_arr) return;
622 encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
624 g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
626 encap_table = (struct encap_type_info *)encap_table_arr->data;
629 int wtap_get_num_encap_types(void) {
630 wtap_init_encap_types();
631 return wtap_num_encap_types;
635 int wtap_register_encap_type(const char* name, const char* short_name) {
636 struct encap_type_info e;
637 wtap_init_encap_types();
639 e.name = g_strdup(name);
640 e.short_name = g_strdup(short_name);
642 g_array_append_val(encap_table_arr,e);
644 encap_table = (struct encap_type_info *)encap_table_arr->data;
646 return wtap_num_encap_types++;
650 /* Name that should be somewhat descriptive. */
652 wtap_encap_string(int encap)
654 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
656 else if (encap == WTAP_ENCAP_PER_PACKET)
659 return encap_table[encap].name;
662 /* Name to use in, say, a command-line flag specifying the type. */
664 wtap_encap_short_string(int encap)
666 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
668 else if (encap == WTAP_ENCAP_PER_PACKET)
671 return encap_table[encap].short_name;
674 /* Translate a short name to a capture file type. */
676 wtap_short_string_to_encap(const char *short_name)
680 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
681 if (encap_table[encap].short_name != NULL &&
682 strcmp(short_name, encap_table[encap].short_name) == 0)
685 return -1; /* no such encapsulation type */
688 static const char *wtap_errlist[] = {
689 "The file isn't a plain file or pipe",
690 "The file is being opened for random access but is a pipe",
691 "The file isn't a capture file in a known format",
692 "File contains record data we don't support",
693 "That file format cannot be written to a pipe",
695 "Files can't be saved in that format",
696 "Files from that network type can't be saved in that format",
697 "That file format doesn't support per-packet encapsulations",
700 "Less data was read than was expected",
701 "The file appears to be damaged or corrupt.",
702 "Less data was written than was requested",
703 "Uncompression error: data oddly truncated",
704 "Uncompression error: data would overflow buffer",
705 "Uncompression error: bad LZ77 offset",
706 "The standard input cannot be opened for random access",
707 "That file format doesn't support compression",
709 "Uncompression error",
712 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
715 wtap_strerror(int err)
717 static char errbuf[128];
718 unsigned int wtap_errlist_index;
721 wtap_errlist_index = -1 - err;
722 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
723 g_snprintf(errbuf, 128, "Error %d", err);
726 if (wtap_errlist[wtap_errlist_index] == NULL)
727 return "Unknown reason";
728 return wtap_errlist[wtap_errlist_index];
730 return g_strerror(err);
733 /* Close only the sequential side, freeing up memory it uses.
735 Note that we do *not* want to call the subtype's close function,
736 as it would free any per-subtype data, and that data may be
737 needed by the random-access side.
739 Instead, if the subtype has a "sequential close" function, we call it,
740 to free up stuff used only by the sequential side. */
742 wtap_sequential_close(wtap *wth)
744 if (wth->subtype_sequential_close != NULL)
745 (*wth->subtype_sequential_close)(wth);
747 if (wth->fh != NULL) {
752 if (wth->frame_buffer) {
753 buffer_free(wth->frame_buffer);
754 g_free(wth->frame_buffer);
755 wth->frame_buffer = NULL;
760 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
766 * Close the file descriptors for the sequential and random streams, but
767 * don't discard any information about those streams. Used on Windows if
768 * we need to rename a file that we have open or if we need to rename on
769 * top of a file we have open.
772 wtap_fdclose(wtap *wth)
775 file_fdclose(wth->fh);
776 if (wth->random_fh != NULL)
777 file_fdclose(wth->random_fh);
781 wtap_close(wtap *wth)
784 wtapng_if_descr_t *wtapng_if_descr;
785 wtapng_if_stats_t *if_stats;
787 wtap_sequential_close(wth);
789 if (wth->subtype_close != NULL)
790 (*wth->subtype_close)(wth);
792 if (wth->random_fh != NULL)
793 file_close(wth->random_fh);
795 if (wth->priv != NULL)
798 if (wth->fast_seek != NULL) {
799 g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
800 g_ptr_array_free(wth->fast_seek, TRUE);
802 for(i = 0; i < (gint)wth->number_of_interfaces; i++) {
803 wtapng_if_descr = &g_array_index(wth->interface_data, wtapng_if_descr_t, i);
804 if(wtapng_if_descr->opt_comment != NULL){
805 g_free(wtapng_if_descr->opt_comment);
807 if(wtapng_if_descr->if_name != NULL){
808 g_free(wtapng_if_descr->if_name);
810 if(wtapng_if_descr->if_description != NULL){
811 g_free(wtapng_if_descr->if_description);
813 if(wtapng_if_descr->if_filter_str != NULL){
814 g_free(wtapng_if_descr->if_filter_str);
816 if(wtapng_if_descr->if_filter_bpf_bytes != NULL){
817 g_free(wtapng_if_descr->if_filter_bpf_bytes);
819 if(wtapng_if_descr->if_os != NULL){
820 g_free(wtapng_if_descr->if_os);
822 for(j = 0; j < (gint)wtapng_if_descr->num_stat_entries; j++) {
823 if_stats = &g_array_index(wtapng_if_descr->interface_statistics, wtapng_if_stats_t, j);
824 if(if_stats->opt_comment != NULL){
825 g_free(if_stats->opt_comment);
828 if(wtapng_if_descr->num_stat_entries != 0){
829 g_array_free(wtapng_if_descr->interface_statistics, TRUE);
832 if(wth->number_of_interfaces != 0){
833 g_array_free(wth->interface_data, TRUE);
839 wtap_cleareof(wtap *wth) {
841 file_clearerr(wth->fh);
844 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
846 wth->add_new_ipv4 = add_new_ipv4;
849 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
851 wth->add_new_ipv6 = add_new_ipv6;
855 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
858 * Set the packet encapsulation to the file's encapsulation
859 * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
860 * right answer (and means that the read routine for this
861 * capture file type doesn't have to set it), and if it
862 * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
865 wth->phdr.pkt_encap = wth->file_encap;
867 if (!wth->subtype_read(wth, err, err_info, data_offset)) {
869 * If we didn't get an error indication, we read
870 * the last packet. See if there's any deferred
871 * error, as might, for example, occur if we're
872 * reading a compressed file, and we got an error
873 * reading compressed data from the file, but
874 * got enough compressed data to decompress the
875 * last packet of the file.
878 *err = file_error(wth->fh, err_info);
879 return FALSE; /* failure */
883 * It makes no sense for the captured data length to be bigger
884 * than the actual data length.
886 if (wth->phdr.caplen > wth->phdr.len)
887 wth->phdr.caplen = wth->phdr.len;
890 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
891 * probably means the file has that encapsulation type
892 * but the read routine didn't set this packet's
893 * encapsulation type.
895 g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
897 return TRUE; /* success */
901 * Return an approximation of the amount of data we've read sequentially
902 * from the file so far. (gint64, in case that's 64 bits.)
905 wtap_read_so_far(wtap *wth)
907 return file_tell_raw(wth->fh);
917 wtap_buf_ptr(wtap *wth)
919 return buffer_start_ptr(wth->frame_buffer);
923 wtap_seek_read(wtap *wth, gint64 seek_off,
924 struct wtap_pkthdr *phdr, guint8 *pd, int len,
925 int *err, gchar **err_info)
927 phdr->presence_flags = 0;
928 phdr->pkt_encap = wth->file_encap;
929 phdr->len = phdr->caplen = len;
931 if (!wth->subtype_seek_read(wth, seek_off, phdr, pd, len, err, err_info))
934 if (phdr->caplen > phdr->len)
935 phdr->caplen = phdr->len;
937 /* g_assert(phdr->pkt_encap != WTAP_ENCAP_PER_PACKET); */