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.
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
44 #include "file_wrappers.h"
45 #include <wsutil/file_util.h>
49 * Return the size of the file, as reported by the OS.
50 * (gint64, in case that's 64 bits.)
53 wtap_file_size(wtap *wth, int *err)
57 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
64 * Do an fstat on the file.
67 wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
69 if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
76 wtap_file_type(wtap *wth)
78 return wth->file_type;
82 wtap_snapshot_length(wtap *wth)
84 return wth->snapshot_length;
88 wtap_file_encap(wtap *wth)
90 return wth->file_encap;
94 wtap_file_tsprecision(wtap *wth)
96 return wth->tsprecision;
99 /* Table of the encapsulation types we know about. */
100 struct encap_type_info {
102 const char *short_name;
105 static struct encap_type_info encap_table_base[] = {
106 /* WTAP_ENCAP_UNKNOWN */
107 { "Unknown", "unknown" },
109 /* WTAP_ENCAP_ETHERNET */
110 { "Ethernet", "ether" },
112 /* WTAP_ENCAP_TOKEN_RING */
113 { "Token Ring", "tr" },
115 /* WTAP_ENCAP_SLIP */
121 /* WTAP_ENCAP_FDDI */
124 /* WTAP_ENCAP_FDDI_BITSWAPPED */
125 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
127 /* WTAP_ENCAP_RAW_IP */
128 { "Raw IP", "rawip" },
130 /* WTAP_ENCAP_ARCNET */
131 { "ARCNET", "arcnet" },
133 /* WTAP_ENCAP_ARCNET_LINUX */
134 { "Linux ARCNET", "arcnet_linux" },
136 /* WTAP_ENCAP_ATM_RFC1483 */
137 { "RFC 1483 ATM", "atm-rfc1483" },
139 /* WTAP_ENCAP_LINUX_ATM_CLIP */
140 { "Linux ATM CLIP", "linux-atm-clip" },
142 /* WTAP_ENCAP_LAPB */
145 /* WTAP_ENCAP_ATM_PDUS */
146 { "ATM PDUs", "atm-pdus" },
148 /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
149 { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
151 /* WTAP_ENCAP_NULL */
154 /* WTAP_ENCAP_ASCEND */
155 { "Lucent/Ascend access equipment", "ascend" },
157 /* WTAP_ENCAP_ISDN */
160 /* WTAP_ENCAP_IP_OVER_FC */
161 { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
163 /* WTAP_ENCAP_PPP_WITH_PHDR */
164 { "PPP with Directional Info", "ppp-with-direction" },
166 /* WTAP_ENCAP_IEEE_802_11 */
167 { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
169 /* WTAP_ENCAP_PRISM_HEADER */
170 { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
172 /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
173 { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
175 /* WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP */
176 { "IEEE 802.11 plus radiotap WLAN header", "ieee-802-11-radiotap" },
178 /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
179 { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
182 { "Linux cooked-mode capture", "linux-sll" },
184 /* WTAP_ENCAP_FRELAY */
185 { "Frame Relay", "frelay" },
187 /* WTAP_ENCAP_FRELAY_WITH_PHDR */
188 { "Frame Relay with Directional Info", "frelay-with-direction" },
190 /* WTAP_ENCAP_CHDLC */
191 { "Cisco HDLC", "chdlc" },
193 /* WTAP_ENCAP_CISCO_IOS */
194 { "Cisco IOS internal", "ios" },
196 /* WTAP_ENCAP_LOCALTALK */
197 { "Localtalk", "ltalk" },
199 /* WTAP_ENCAP_OLD_PFLOG */
200 { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
202 /* WTAP_ENCAP_HHDLC */
203 { "HiPath HDLC", "hhdlc" },
205 /* WTAP_ENCAP_DOCSIS */
206 { "Data Over Cable Service Interface Specification", "docsis" },
208 /* WTAP_ENCAP_COSINE */
209 { "CoSine L2 debug log", "cosine" },
211 /* WTAP_ENCAP_WFLEET_HDLC */
212 { "Wellfleet HDLC", "whdlc" },
214 /* WTAP_ENCAP_SDLC */
217 /* WTAP_ENCAP_TZSP */
218 { "Tazmen sniffer protocol", "tzsp" },
221 { "OpenBSD enc(4) encapsulating interface", "enc" },
223 /* WTAP_ENCAP_PFLOG */
224 { "OpenBSD PF Firewall logs", "pflog" },
226 /* WTAP_ENCAP_CHDLC_WITH_PHDR */
227 { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
229 /* WTAP_ENCAP_BLUETOOTH_H4 */
230 { "Bluetooth H4", "bluetooth-h4" },
232 /* WTAP_ENCAP_MTP2 */
233 { "SS7 MTP2", "mtp2" },
235 /* WTAP_ENCAP_MTP3 */
236 { "SS7 MTP3", "mtp3" },
238 /* WTAP_ENCAP_IRDA */
241 /* WTAP_ENCAP_USER0 */
242 { "USER 0", "user0" },
244 /* WTAP_ENCAP_USER1 */
245 { "USER 1", "user1" },
247 /* WTAP_ENCAP_USER2 */
248 { "USER 2", "user2" },
250 /* WTAP_ENCAP_USER3 */
251 { "USER 3", "user3" },
253 /* WTAP_ENCAP_USER4 */
254 { "USER 4", "user4" },
256 /* WTAP_ENCAP_USER5 */
257 { "USER 5", "user5" },
259 /* WTAP_ENCAP_USER6 */
260 { "USER 6", "user6" },
262 /* WTAP_ENCAP_USER7 */
263 { "USER 7", "user7" },
265 /* WTAP_ENCAP_USER8 */
266 { "USER 8", "user8" },
268 /* WTAP_ENCAP_USER9 */
269 { "USER 9", "user9" },
271 /* WTAP_ENCAP_USER10 */
272 { "USER 10", "user10" },
274 /* WTAP_ENCAP_USER11 */
275 { "USER 11", "user11" },
277 /* WTAP_ENCAP_USER12 */
278 { "USER 12", "user12" },
280 /* WTAP_ENCAP_USER13 */
281 { "USER 13", "user13" },
283 /* WTAP_ENCAP_USER14 */
284 { "USER 14", "user14" },
286 /* WTAP_ENCAP_USER15 */
287 { "USER 15", "user15" },
289 /* WTAP_ENCAP_SYMANTEC */
290 { "Symantec Enterprise Firewall", "symantec" },
292 /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
293 { "Apple IP-over-IEEE 1394", "ap1394" },
295 /* WTAP_ENCAP_BACNET_MS_TP */
296 { "BACnet MS/TP", "bacnet-ms-tp" },
298 /* WTAP_ENCAP_NETTL_RAW_ICMP */
299 { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
301 /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
302 { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
304 /* WTAP_ENCAP_GPRS_LLC */
305 { "GPRS LLC", "gprs-llc" },
307 /* WTAP_ENCAP_JUNIPER_ATM1 */
308 { "Juniper ATM1", "juniper-atm1" },
310 /* WTAP_ENCAP_JUNIPER_ATM2 */
311 { "Juniper ATM2", "juniper-atm2" },
313 /* WTAP_ENCAP_REDBACK */
314 { "Redback SmartEdge", "redback" },
316 /* WTAP_ENCAP_NETTL_RAW_IP */
317 { "Raw IP with nettl headers", "rawip-nettl" },
319 /* WTAP_ENCAP_NETTL_ETHERNET */
320 { "Ethernet with nettl headers", "ether-nettl" },
322 /* WTAP_ENCAP_NETTL_TOKEN_RING */
323 { "Token Ring with nettl headers", "tr-nettl" },
325 /* WTAP_ENCAP_NETTL_FDDI */
326 { "FDDI with nettl headers", "fddi-nettl" },
328 /* WTAP_ENCAP_NETTL_UNKNOWN */
329 { "Unknown link-layer type with nettl headers", "unknown-nettl" },
331 /* WTAP_ENCAP_MTP2_WITH_PHDR */
332 { "MTP2 with pseudoheader", "mtp2-with-phdr" },
334 /* WTAP_ENCAP_JUNIPER_PPPOE */
335 { "Juniper PPPoE", "juniper-pppoe" },
337 /* WTAP_ENCAP_GCOM_TIE1 */
338 { "GCOM TIE1", "gcom-tie1" },
340 /* WTAP_ENCAP_GCOM_SERIAL */
341 { "GCOM Serial", "gcom-serial" },
343 /* WTAP_ENCAP_NETTL_X25 */
344 { "X25 with nettl headers", "x25-nettl" },
347 { "K12 protocol analyzer", "k12" },
349 /* WTAP_ENCAP_JUNIPER_MLPPP */
350 { "Juniper MLPPP", "juniper-mlppp" },
352 /* WTAP_ENCAP_JUNIPER_MLFR */
353 { "Juniper MLFR", "juniper-mlfr" },
355 /* WTAP_ENCAP_JUNIPER_ETHER */
356 { "Juniper Ethernet", "juniper-ether" },
358 /* WTAP_ENCAP_JUNIPER_PPP */
359 { "Juniper PPP", "juniper-ppp" },
361 /* WTAP_ENCAP_JUNIPER_FRELAY */
362 { "Juniper Frame-Relay", "juniper-frelay" },
364 /* WTAP_ENCAP_JUNIPER_CHDLC */
365 { "Juniper C-HDLC", "juniper-chdlc" },
367 /* WTAP_ENCAP_JUNIPER_GGSN */
368 { "Juniper GGSN", "juniper-ggsn" },
370 /* WTAP_ENCAP_LINUX_LAPD */
373 /* WTAP_ENCAP_CATAPULT_DCT2000 */
374 { "Catapult DCT2000", "dct2000" },
377 { "ASN.1 Basic Encoding Rules", "ber" },
379 /* WTAP_ENCAP_JUNIPER_VP */
380 { "Juniper Voice PIC", "juniper-vp" },
383 { "Raw USB packets", "usb" },
385 /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
386 { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
388 /* WTAP_ENCAP_NETTL_RAW_TELNET */
389 { "Raw telnet with nettl headers", "raw-telnet-nettl" },
391 /* WTAP_ENCAP_USB_LINUX */
392 { "USB packets with Linux header", "usb-linux" },
394 /* WTAP_ENCAP_MPEG */
398 { "Per-Packet Information header", "ppi" },
401 { "Endace Record File", "erf" },
403 /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
404 { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
406 /* WTAP_ENCAP_SITA */
407 { "SITA WAN packets", "sita-wan" },
409 /* WTAP_ENCAP_SCCP */
410 { "SS7 SCCP", "sccp" },
412 /* WTAP_ENCAP_BLUETOOTH_HCI */
413 { "Bluetooth without transport layer", "bluetooth-hci" },
415 /* WTAP_ENCAP_IPMB */
416 { "Intelligent Platform Management Bus", "ipmb" },
418 /* WTAP_ENCAP_IEEE802_15_4 */
419 { "IEEE 802.15.4 Wireless PAN", "wpan" },
421 /* WTAP_ENCAP_X2E_XORAYA */
422 { "X2E Xoraya", "x2e-xoraya" },
424 /* WTAP_ENCAP_FLEXRAY */
425 { "FlexRay", "flexray" },
428 { "Local Interconnect Network", "lin" },
430 /* WTAP_ENCAP_MOST */
431 { "Media Oriented Systems Transport", "most" },
433 /* WTAP_ENCAP_CAN20B */
434 { "Controller Area Network 2.0B", "can20b" },
436 /* WTAP_ENCAP_LAYER1_EVENT */
437 { "EyeSDN Layer 1 event", "layer1-event" },
439 /* WTAP_ENCAP_X2E_SERIAL */
440 { "X2E serial line capture", "x2e-serial" },
445 /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
446 { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
448 /* WTAP_ENCAP_TNEF */
449 { "Transport-Neutral Encapsulation Format", "tnef" },
451 /* WTAP_ENCAP_USB_LINUX_MMAPPED */
452 { "USB packets with Linux header and padding", "usb-linux-mmap" },
454 /* WTAP_ENCAP_GSM_UM */
455 { "GSM Um Interface", "gsm_um" },
457 /* WTAP_ENCAP_DPNSS */
458 { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
460 /* WTAP_ENCAP_PACKETLOGGER */
461 { "PacketLogger", "packetlogger" },
463 /* WTAP_ENCAP_NSTRACE_1_0 */
464 { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
466 /* WTAP_ENCAP_NSTRACE_2_0 */
467 { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
469 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
470 { "Fibre Channel FC-2", "fc2" },
472 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
473 { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
475 /* WTAP_ENCAP_JPEG_JFIF */
476 { "JPEG/JFIF", "jfif" },
478 /* WTAP_ENCAP_IPNET */
479 { "Solaris IPNET", "ipnet" },
481 /* WTAP_ENCAP_SOCKETCAN */
482 { "SocketCAN", "socketcan" },
484 /* WTAP_ENCAP_IEEE802_11_NETMON_RADIO */
485 { "IEEE 802.11 plus Network Monitor radio header", "ieee-802-11-netmon" },
487 /* WTAP_ENCAP_IEEE802_15_4_NOFCS */
488 { "IEEE 802.15.4 Wireless PAN with FCS not present", "wpan-nofcs" },
490 /* WTAP_ENCAP_RAW_IPFIX */
491 { "IPFIX", "ipfix" },
493 /* WTAP_ENCAP_RAW_IP4 */
494 { "Raw IPv4", "rawip4" },
496 /* WTAP_ENCAP_RAW_IP6 */
497 { "Raw IPv6", "rawip6" },
499 /* WTAP_ENCAP_LAPD */
500 { "Lapd header", "lapd" },
502 /* WTAP_ENCAP_DVBCI */
503 { "DVB-CI (Common Interface)", "dvbci"},
505 /* WTAP_ENCAP_MUX27010 */
506 { "MUX27010", "mux27010"}
509 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
510 static GArray* encap_table_arr = NULL;
511 static const struct encap_type_info* encap_table = NULL;
513 static void wtap_init_encap_types(void) {
515 if (encap_table_arr) return;
517 encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
519 g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
521 encap_table = (void*)encap_table_arr->data;
524 int wtap_get_num_encap_types(void) {
525 wtap_init_encap_types();
526 return wtap_num_encap_types;
530 int wtap_register_encap_type(char* name, char* short_name) {
531 struct encap_type_info e;
532 wtap_init_encap_types();
534 e.name = g_strdup(name);
535 e.short_name = g_strdup(short_name);
537 g_array_append_val(encap_table_arr,e);
539 encap_table = (void*)encap_table_arr->data;
541 return wtap_num_encap_types++;
545 /* Name that should be somewhat descriptive. */
547 *wtap_encap_string(int encap)
549 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
551 else if (encap == WTAP_ENCAP_PER_PACKET)
554 return encap_table[encap].name;
557 /* Name to use in, say, a command-line flag specifying the type. */
559 *wtap_encap_short_string(int encap)
561 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
563 else if (encap == WTAP_ENCAP_PER_PACKET)
566 return encap_table[encap].short_name;
569 /* Translate a short name to a capture file type. */
571 wtap_short_string_to_encap(const char *short_name)
575 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
576 if (encap_table[encap].short_name != NULL &&
577 strcmp(short_name, encap_table[encap].short_name) == 0)
580 return -1; /* no such encapsulation type */
583 static const char *wtap_errlist[] = {
584 "The file isn't a plain file or pipe",
585 "The file is being opened for random access but is a pipe",
586 "The file isn't a capture file in a known format",
587 "File contains record data we don't support",
588 "That file format cannot be written to a pipe",
590 "Files can't be saved in that format",
591 "Files from that network type can't be saved in that format",
592 "That file format doesn't support per-packet encapsulations",
595 "Less data was read than was expected",
596 "File contains a record that's not valid",
597 "Less data was written than was requested",
598 "Uncompression error: data oddly truncated",
599 "Uncompression error: data would overflow buffer",
600 "Uncompression error: bad LZ77 offset",
601 "The standard input cannot be opened for random access",
602 "That file format doesn't support compression",
604 "Uncompression error",
607 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
610 *wtap_strerror(int err)
612 static char errbuf[128];
613 unsigned int wtap_errlist_index;
616 wtap_errlist_index = -1 - err;
617 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
618 g_snprintf(errbuf, 128, "Error %d", err);
621 if (wtap_errlist[wtap_errlist_index] == NULL)
622 return "Unknown reason";
623 return wtap_errlist[wtap_errlist_index];
625 return strerror(err);
628 /* Close only the sequential side, freeing up memory it uses.
630 Note that we do *not* want to call the subtype's close function,
631 as it would free any per-subtype data, and that data may be
632 needed by the random-access side.
634 Instead, if the subtype has a "sequential close" function, we call it,
635 to free up stuff used only by the sequential side. */
637 wtap_sequential_close(wtap *wth)
639 if (wth->subtype_sequential_close != NULL)
640 (*wth->subtype_sequential_close)(wth);
642 if (wth->fh != NULL) {
647 if (wth->frame_buffer) {
648 buffer_free(wth->frame_buffer);
649 g_free(wth->frame_buffer);
650 wth->frame_buffer = NULL;
655 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
661 wtap_close(wtap *wth)
663 wtap_sequential_close(wth);
665 if (wth->subtype_close != NULL)
666 (*wth->subtype_close)(wth);
668 if (wth->random_fh != NULL)
669 file_close(wth->random_fh);
671 if (wth->priv != NULL)
674 if (wth->fast_seek != NULL) {
675 g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
676 g_ptr_array_free(wth->fast_seek, TRUE);
682 wtap_cleareof(wtap *wth _U_) {
684 if (file_eof(wth->fh))
685 file_clearerr(wth->fh);
688 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
690 wth->add_new_ipv4 = add_new_ipv4;
693 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
695 wth->add_new_ipv6 = add_new_ipv6;
699 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
702 * Set the packet encapsulation to the file's encapsulation
703 * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
704 * right answer (and means that the read routine for this
705 * capture file type doesn't have to set it), and if it
706 * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
709 wth->phdr.pkt_encap = wth->file_encap;
711 if (!wth->subtype_read(wth, err, err_info, data_offset)) {
713 * If we didn't get an error indication, we read
714 * the last packet. See if there's any deferred
715 * error, as might, for example, occur if we're
716 * reading a compressed file, and we got an error
717 * reading compressed data from the file, but
718 * got enough compressed data to decompress the
719 * last packet of the file.
722 *err = file_error(wth->fh, err_info);
723 return FALSE; /* failure */
727 * It makes no sense for the captured data length to be bigger
728 * than the actual data length.
730 if (wth->phdr.caplen > wth->phdr.len)
731 wth->phdr.caplen = wth->phdr.len;
734 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
735 * probably means the file has that encapsulation type
736 * but the read routine didn't set this packet's
737 * encapsulation type.
739 g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
741 return TRUE; /* success */
745 * Return an approximation of the amount of data we've read sequentially
746 * from the file so far. (gint64, in case that's 64 bits.)
749 wtap_read_so_far(wtap *wth)
751 return file_tell_raw(wth->fh);
760 union wtap_pseudo_header*
761 wtap_pseudoheader(wtap *wth)
763 return &wth->pseudo_header;
767 wtap_buf_ptr(wtap *wth)
769 return buffer_start_ptr(wth->frame_buffer);
773 wtap_seek_read(wtap *wth, gint64 seek_off,
774 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
775 int *err, gchar **err_info)
777 return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,