MSWSP: use GuidPropertySet_find_guid() in parse_CFullPropSpec()
[wireshark-wip.git] / wiretap / wtap.c
blob4b9a32677a406f22f8fa5e7e72b7f778bc538727
1 /* wtap.c
3 * $Id$
5 * Wiretap Library
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.
23 #include "config.h"
25 #include <string.h>
26 #include <errno.h>
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
36 #ifdef HAVE_LIBZ
37 #include <zlib.h>
38 #endif
40 #include "wtap-int.h"
42 #include "file_wrappers.h"
43 #include <wsutil/file_util.h>
44 #include "buffer.h"
47 * Return the size of the file, as reported by the OS.
48 * (gint64, in case that's 64 bits.)
50 gint64
51 wtap_file_size(wtap *wth, int *err)
53 ws_statb64 statb;
55 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
56 &statb, err) == -1)
57 return -1;
58 return statb.st_size;
62 * Do an fstat on the file.
64 int
65 wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
67 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
68 statb, err) == -1)
69 return -1;
70 return 0;
73 int
74 wtap_file_type_subtype(wtap *wth)
76 return wth->file_type_subtype;
79 gboolean
80 wtap_iscompressed(wtap *wth)
82 return file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh);
85 guint
86 wtap_snapshot_length(wtap *wth)
88 return wth->snapshot_length;
91 int
92 wtap_file_encap(wtap *wth)
94 return wth->file_encap;
97 int
98 wtap_file_tsprecision(wtap *wth)
100 return wth->tsprecision;
103 wtapng_section_t *
104 wtap_file_get_shb_info(wtap *wth)
106 wtapng_section_t *shb_hdr;
108 if(wth == NULL)
109 return NULL;
110 shb_hdr = g_new(wtapng_section_t,1);
111 shb_hdr->section_length = wth->shb_hdr.section_length;
112 /* options */
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. */
119 return shb_hdr;
122 void
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;
140 return idb_info;
143 /* Table of the encapsulation types we know about. */
144 struct encap_type_info {
145 const char *name;
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 */
160 { "SLIP", "slip" },
162 /* WTAP_ENCAP_PPP */
163 { "PPP", "ppp" },
165 /* WTAP_ENCAP_FDDI */
166 { "FDDI", "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 */
187 { "LAPB", "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 */
196 { "NULL", "null" },
198 /* WTAP_ENCAP_ASCEND */
199 { "Lucent/Ascend access equipment", "ascend" },
201 /* WTAP_ENCAP_ISDN */
202 { "ISDN", "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" },
225 /* WTAP_ENCAP_SLL */
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 */
259 { "SDLC", "sdlc" },
261 /* WTAP_ENCAP_TZSP */
262 { "Tazmen sniffer protocol", "tzsp" },
264 /* WTAP_ENCAP_ENC */
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 */
283 { "IrDA", "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" },
390 /* WTAP_ENCAP_K12 */
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" },
420 /* WTAP_ENCAP_BER */
421 { "ASN.1 Basic Encoding Rules", "ber" },
423 /* WTAP_ENCAP_JUNIPER_VP */
424 { "Juniper Voice PIC", "juniper-vp" },
426 /* WTAP_ENCAP_USB */
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 */
439 { "MPEG", "mpeg" },
441 /* WTAP_ENCAP_PPI */
442 { "Per-Packet Information header", "ppi" },
444 /* WTAP_ENCAP_ERF */
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" },
471 /* WTAP_ENCAP_LIN */
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" },
486 /* WTAP_ENCAP_I2C */
487 { "I2C", "i2c" },
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 */
544 { "LAPD", "lapd" },
546 /* WTAP_ENCAP_DVBCI */
547 { "DVB-CI (Common Interface)", "dvbci"},
549 /* WTAP_ENCAP_MUX27010 */
550 { "MUX27010", "mux27010"},
552 /* WTAP_ENCAP_MIME */
553 { "MIME", "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" },
588 /* WTAP_ENCAP_SDH */
589 { "SDH", "sdh" },
591 /* WTAP_ENCAP_DBUS */
592 { "D-Bus", "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 */
601 { "SCTP", "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" },
612 /* WTAP_ENCAP_RTAC_SERIAL */
613 { "RTAC serial-line", "rtac-serial" },
615 /* WTAP_ENCAP_BLUETOOTH_LE_LL */
616 { "Bluetooth Low Energy Link Layer", "bluetooth-le-ll" },
618 /* WTAP_ENCAP_WIRESHARK_UPPER_PDU */
619 { "Wireshark Upper PDU export", "wireshark-upper-pdu" },
622 WS_DLL_LOCAL
623 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
624 static GArray* encap_table_arr = NULL;
626 #define encap_table_entry(encap) \
627 g_array_index(encap_table_arr, struct encap_type_info, encap)
629 static void wtap_init_encap_types(void) {
631 if (encap_table_arr) return;
633 encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
635 g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
638 int wtap_get_num_encap_types(void) {
639 wtap_init_encap_types();
640 return wtap_num_encap_types;
644 int wtap_register_encap_type(const char* name, const char* short_name) {
645 struct encap_type_info e;
646 wtap_init_encap_types();
648 e.name = g_strdup(name);
649 e.short_name = g_strdup(short_name);
651 g_array_append_val(encap_table_arr,e);
653 return wtap_num_encap_types++;
657 /* Name that should be somewhat descriptive. */
658 const char *
659 wtap_encap_string(int encap)
661 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
662 return "Illegal";
663 else if (encap == WTAP_ENCAP_PER_PACKET)
664 return "Per packet";
665 else
666 return encap_table_entry(encap).name;
669 /* Name to use in, say, a command-line flag specifying the type. */
670 const char *
671 wtap_encap_short_string(int encap)
673 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
674 return "illegal";
675 else if (encap == WTAP_ENCAP_PER_PACKET)
676 return "per-packet";
677 else
678 return encap_table_entry(encap).short_name;
681 /* Translate a short name to a capture file type. */
683 wtap_short_string_to_encap(const char *short_name)
685 int encap;
687 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
688 if (encap_table_entry(encap).short_name != NULL &&
689 strcmp(short_name, encap_table_entry(encap).short_name) == 0)
690 return encap;
692 return -1; /* no such encapsulation type */
695 static const char *wtap_errlist[] = {
696 "The file isn't a plain file or pipe",
697 "The file is being opened for random access but is a pipe",
698 "The file isn't a capture file in a known format",
699 "File contains record data we don't support",
700 "That file format cannot be written to a pipe",
701 NULL,
702 "Files can't be saved in that format",
703 "Files from that network type can't be saved in that format",
704 "That file format doesn't support per-packet encapsulations",
705 NULL,
706 NULL,
707 "Less data was read than was expected",
708 "The file appears to be damaged or corrupt.",
709 "Less data was written than was requested",
710 "Uncompression error: data oddly truncated",
711 "Uncompression error: data would overflow buffer",
712 "Uncompression error: bad LZ77 offset",
713 "The standard input cannot be opened for random access",
714 "That file format doesn't support compression",
715 NULL,
716 NULL,
717 "Uncompression error",
718 "Internal error"
720 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
722 const char *
723 wtap_strerror(int err)
725 static char errbuf[128];
726 unsigned int wtap_errlist_index;
728 if (err < 0) {
729 wtap_errlist_index = -1 - err;
730 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
731 g_snprintf(errbuf, 128, "Error %d", err);
732 return errbuf;
734 if (wtap_errlist[wtap_errlist_index] == NULL)
735 return "Unknown reason";
736 return wtap_errlist[wtap_errlist_index];
737 } else
738 return g_strerror(err);
741 /* Close only the sequential side, freeing up memory it uses.
743 Note that we do *not* want to call the subtype's close function,
744 as it would free any per-subtype data, and that data may be
745 needed by the random-access side.
747 Instead, if the subtype has a "sequential close" function, we call it,
748 to free up stuff used only by the sequential side. */
749 void
750 wtap_sequential_close(wtap *wth)
752 if (wth->subtype_sequential_close != NULL)
753 (*wth->subtype_sequential_close)(wth);
755 if (wth->fh != NULL) {
756 file_close(wth->fh);
757 wth->fh = NULL;
760 if (wth->frame_buffer) {
761 buffer_free(wth->frame_buffer);
762 g_free(wth->frame_buffer);
763 wth->frame_buffer = NULL;
767 static void
768 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
770 g_free(data);
774 * Close the file descriptors for the sequential and random streams, but
775 * don't discard any information about those streams. Used on Windows if
776 * we need to rename a file that we have open or if we need to rename on
777 * top of a file we have open.
779 void
780 wtap_fdclose(wtap *wth)
782 if (wth->fh != NULL)
783 file_fdclose(wth->fh);
784 if (wth->random_fh != NULL)
785 file_fdclose(wth->random_fh);
788 void
789 wtap_close(wtap *wth)
791 gint i, j;
792 wtapng_if_descr_t *wtapng_if_descr;
793 wtapng_if_stats_t *if_stats;
795 wtap_sequential_close(wth);
797 if (wth->subtype_close != NULL)
798 (*wth->subtype_close)(wth);
800 if (wth->random_fh != NULL)
801 file_close(wth->random_fh);
803 if (wth->priv != NULL)
804 g_free(wth->priv);
806 if (wth->fast_seek != NULL) {
807 g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
808 g_ptr_array_free(wth->fast_seek, TRUE);
810 for(i = 0; i < (gint)wth->number_of_interfaces; i++) {
811 wtapng_if_descr = &g_array_index(wth->interface_data, wtapng_if_descr_t, i);
812 if(wtapng_if_descr->opt_comment != NULL){
813 g_free(wtapng_if_descr->opt_comment);
815 if(wtapng_if_descr->if_name != NULL){
816 g_free(wtapng_if_descr->if_name);
818 if(wtapng_if_descr->if_description != NULL){
819 g_free(wtapng_if_descr->if_description);
821 if(wtapng_if_descr->if_filter_str != NULL){
822 g_free(wtapng_if_descr->if_filter_str);
824 if(wtapng_if_descr->if_filter_bpf_bytes != NULL){
825 g_free(wtapng_if_descr->if_filter_bpf_bytes);
827 if(wtapng_if_descr->if_os != NULL){
828 g_free(wtapng_if_descr->if_os);
830 for(j = 0; j < (gint)wtapng_if_descr->num_stat_entries; j++) {
831 if_stats = &g_array_index(wtapng_if_descr->interface_statistics, wtapng_if_stats_t, j);
832 if(if_stats->opt_comment != NULL){
833 g_free(if_stats->opt_comment);
836 if(wtapng_if_descr->num_stat_entries != 0){
837 g_array_free(wtapng_if_descr->interface_statistics, TRUE);
840 if(wth->number_of_interfaces != 0){
841 g_array_free(wth->interface_data, TRUE);
843 g_free(wth);
846 void
847 wtap_cleareof(wtap *wth) {
848 /* Reset EOF */
849 file_clearerr(wth->fh);
852 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
853 if (wth)
854 wth->add_new_ipv4 = add_new_ipv4;
857 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
858 if (wth)
859 wth->add_new_ipv6 = add_new_ipv6;
862 gboolean
863 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
866 * Set the packet encapsulation to the file's encapsulation
867 * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
868 * right answer (and means that the read routine for this
869 * capture file type doesn't have to set it), and if it
870 * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
871 * anyway.
873 wth->phdr.pkt_encap = wth->file_encap;
875 if (!wth->subtype_read(wth, err, err_info, data_offset)) {
877 * If we didn't get an error indication, we read
878 * the last packet. See if there's any deferred
879 * error, as might, for example, occur if we're
880 * reading a compressed file, and we got an error
881 * reading compressed data from the file, but
882 * got enough compressed data to decompress the
883 * last packet of the file.
885 if (*err == 0)
886 *err = file_error(wth->fh, err_info);
887 return FALSE; /* failure */
891 * It makes no sense for the captured data length to be bigger
892 * than the actual data length.
894 if (wth->phdr.caplen > wth->phdr.len)
895 wth->phdr.caplen = wth->phdr.len;
898 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
899 * probably means the file has that encapsulation type
900 * but the read routine didn't set this packet's
901 * encapsulation type.
903 g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
905 return TRUE; /* success */
909 * Read packet data into a Buffer, growing the buffer as necessary.
911 * This returns an error on a short read, even if the short read hit
912 * the EOF immediately. (The assumption is that each packet has a
913 * header followed by raw packet data, and that we've already read the
914 * header, so if we get an EOF trying to read the packet data, the file
915 * has been cut short, even if the read didn't read any data at all.)
917 gboolean
918 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
919 gchar **err_info)
921 int bytes_read;
923 buffer_assure_space(buf, length);
924 errno = WTAP_ERR_CANT_READ;
925 bytes_read = file_read(buffer_start_ptr(buf), length, fh);
927 if (bytes_read < 0 || (guint)bytes_read != length) {
928 *err = file_error(fh, err_info);
929 if (*err == 0)
930 *err = WTAP_ERR_SHORT_READ;
931 return FALSE;
933 return TRUE;
937 * Return an approximation of the amount of data we've read sequentially
938 * from the file so far. (gint64, in case that's 64 bits.)
940 gint64
941 wtap_read_so_far(wtap *wth)
943 return file_tell_raw(wth->fh);
946 struct wtap_pkthdr *
947 wtap_phdr(wtap *wth)
949 return &wth->phdr;
952 guint8 *
953 wtap_buf_ptr(wtap *wth)
955 return buffer_start_ptr(wth->frame_buffer);
958 gboolean
959 wtap_seek_read(wtap *wth, gint64 seek_off,
960 struct wtap_pkthdr *phdr, Buffer *buf, int len,
961 int *err, gchar **err_info)
963 return wth->subtype_seek_read(wth, seek_off, phdr, buf, len,
964 err, err_info);