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>
45 #include "file_wrappers.h"
46 #include <wsutil/file_util.h>
50 * Return the size of the file, as reported by the OS.
51 * (gint64, in case that's 64 bits.)
54 wtap_file_size(wtap *wth, int *err)
58 if (fstat(wth->fd, &statb) == -1) {
67 wtap_file_type(wtap *wth)
69 return wth->file_type;
73 wtap_snapshot_length(wtap *wth)
75 return wth->snapshot_length;
79 wtap_file_encap(wtap *wth)
81 return wth->file_encap;
85 wtap_file_tsprecision(wtap *wth)
87 return wth->tsprecision;
90 /* Table of the encapsulation types we know about. */
91 struct encap_type_info {
93 const char *short_name;
96 static struct encap_type_info encap_table_base[] = {
97 /* WTAP_ENCAP_UNKNOWN */
98 { "Unknown", "unknown" },
100 /* WTAP_ENCAP_ETHERNET */
101 { "Ethernet", "ether" },
103 /* WTAP_ENCAP_TOKEN_RING */
104 { "Token Ring", "tr" },
106 /* WTAP_ENCAP_SLIP */
112 /* WTAP_ENCAP_FDDI */
115 /* WTAP_ENCAP_FDDI_BITSWAPPED */
116 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
118 /* WTAP_ENCAP_RAW_IP */
119 { "Raw IP", "rawip" },
121 /* WTAP_ENCAP_ARCNET */
122 { "ARCNET", "arcnet" },
124 /* WTAP_ENCAP_ARCNET_LINUX */
125 { "Linux ARCNET", "arcnet_linux" },
127 /* WTAP_ENCAP_ATM_RFC1483 */
128 { "RFC 1483 ATM", "atm-rfc1483" },
130 /* WTAP_ENCAP_LINUX_ATM_CLIP */
131 { "Linux ATM CLIP", "linux-atm-clip" },
133 /* WTAP_ENCAP_LAPB */
136 /* WTAP_ENCAP_ATM_PDUS */
137 { "ATM PDUs", "atm-pdus" },
139 /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
140 { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
142 /* WTAP_ENCAP_NULL */
145 /* WTAP_ENCAP_ASCEND */
146 { "Lucent/Ascend access equipment", "ascend" },
148 /* WTAP_ENCAP_ISDN */
151 /* WTAP_ENCAP_IP_OVER_FC */
152 { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
154 /* WTAP_ENCAP_PPP_WITH_PHDR */
155 { "PPP with Directional Info", "ppp-with-direction" },
157 /* WTAP_ENCAP_IEEE_802_11 */
158 { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
160 /* WTAP_ENCAP_PRISM_HEADER */
161 { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
163 /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
164 { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
166 /* WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP */
167 { "IEEE 802.11 plus radiotap WLAN header", "ieee-802-11-radiotap" },
169 /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
170 { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
173 { "Linux cooked-mode capture", "linux-sll" },
175 /* WTAP_ENCAP_FRELAY */
176 { "Frame Relay", "frelay" },
178 /* WTAP_ENCAP_FRELAY_WITH_PHDR */
179 { "Frame Relay with Directional Info", "frelay-with-direction" },
181 /* WTAP_ENCAP_CHDLC */
182 { "Cisco HDLC", "chdlc" },
184 /* WTAP_ENCAP_CISCO_IOS */
185 { "Cisco IOS internal", "ios" },
187 /* WTAP_ENCAP_LOCALTALK */
188 { "Localtalk", "ltalk" },
190 /* WTAP_ENCAP_OLD_PFLOG */
191 { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
193 /* WTAP_ENCAP_HHDLC */
194 { "HiPath HDLC", "hhdlc" },
196 /* WTAP_ENCAP_DOCSIS */
197 { "Data Over Cable Service Interface Specification", "docsis" },
199 /* WTAP_ENCAP_COSINE */
200 { "CoSine L2 debug log", "cosine" },
202 /* WTAP_ENCAP_WFLEET_HDLC */
203 { "Wellfleet HDLC", "whdlc" },
205 /* WTAP_ENCAP_SDLC */
208 /* WTAP_ENCAP_TZSP */
209 { "Tazmen sniffer protocol", "tzsp" },
212 { "OpenBSD enc(4) encapsulating interface", "enc" },
214 /* WTAP_ENCAP_PFLOG */
215 { "OpenBSD PF Firewall logs", "pflog" },
217 /* WTAP_ENCAP_CHDLC_WITH_PHDR */
218 { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
220 /* WTAP_ENCAP_BLUETOOTH_H4 */
221 { "Bluetooth H4", "bluetooth-h4" },
223 /* WTAP_ENCAP_MTP2 */
224 { "SS7 MTP2", "mtp2" },
226 /* WTAP_ENCAP_MTP3 */
227 { "SS7 MTP3", "mtp3" },
229 /* WTAP_ENCAP_IRDA */
232 /* WTAP_ENCAP_USER0 */
233 { "USER 0", "user0" },
235 /* WTAP_ENCAP_USER1 */
236 { "USER 1", "user1" },
238 /* WTAP_ENCAP_USER2 */
239 { "USER 2", "user2" },
241 /* WTAP_ENCAP_USER3 */
242 { "USER 3", "user3" },
244 /* WTAP_ENCAP_USER4 */
245 { "USER 4", "user4" },
247 /* WTAP_ENCAP_USER5 */
248 { "USER 5", "user5" },
250 /* WTAP_ENCAP_USER6 */
251 { "USER 6", "user6" },
253 /* WTAP_ENCAP_USER7 */
254 { "USER 7", "user7" },
256 /* WTAP_ENCAP_USER8 */
257 { "USER 8", "user8" },
259 /* WTAP_ENCAP_USER9 */
260 { "USER 9", "user9" },
262 /* WTAP_ENCAP_USER10 */
263 { "USER 10", "user10" },
265 /* WTAP_ENCAP_USER11 */
266 { "USER 11", "user11" },
268 /* WTAP_ENCAP_USER12 */
269 { "USER 12", "user12" },
271 /* WTAP_ENCAP_USER13 */
272 { "USER 13", "user13" },
274 /* WTAP_ENCAP_USER14 */
275 { "USER 14", "user14" },
277 /* WTAP_ENCAP_USER15 */
278 { "USER 15", "user15" },
280 /* WTAP_ENCAP_SYMANTEC */
281 { "Symantec Enterprise Firewall", "symantec" },
283 /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
284 { "Apple IP-over-IEEE 1394", "ap1394" },
286 /* WTAP_ENCAP_BACNET_MS_TP */
287 { "BACnet MS/TP", "bacnet-ms-tp" },
289 /* WTAP_ENCAP_NETTL_RAW_ICMP */
290 { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
292 /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
293 { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
295 /* WTAP_ENCAP_GPRS_LLC */
296 { "GPRS LLC", "gprs-llc" },
298 /* WTAP_ENCAP_JUNIPER_ATM1 */
299 { "Juniper ATM1", "juniper-atm1" },
301 /* WTAP_ENCAP_JUNIPER_ATM2 */
302 { "Juniper ATM2", "juniper-atm2" },
304 /* WTAP_ENCAP_REDBACK */
305 { "Redback SmartEdge", "redback" },
307 /* WTAP_ENCAP_NETTL_RAW_IP */
308 { "Raw IP with nettl headers", "rawip-nettl" },
310 /* WTAP_ENCAP_NETTL_ETHERNET */
311 { "Ethernet with nettl headers", "ether-nettl" },
313 /* WTAP_ENCAP_NETTL_TOKEN_RING */
314 { "Token Ring with nettl headers", "tr-nettl" },
316 /* WTAP_ENCAP_NETTL_FDDI */
317 { "FDDI with nettl headers", "fddi-nettl" },
319 /* WTAP_ENCAP_NETTL_UNKNOWN */
320 { "Unknown link-layer type with nettl headers", "unknown-nettl" },
322 /* WTAP_ENCAP_MTP2_WITH_PHDR */
323 { "MTP2 with pseudoheader", "mtp2-with-phdr" },
325 /* WTAP_ENCAP_JUNIPER_PPPOE */
326 { "Juniper PPPoE", "juniper-pppoe" },
328 /* WTAP_ENCAP_GCOM_TIE1 */
329 { "GCOM TIE1", "gcom-tie1" },
331 /* WTAP_ENCAP_GCOM_SERIAL */
332 { "GCOM Serial", "gcom-serial" },
334 /* WTAP_ENCAP_NETTL_X25 */
335 { "X25 with nettl headers", "x25-nettl" },
338 { "K12 protocol analyzer", "k12" },
340 /* WTAP_ENCAP_JUNIPER_MLPPP */
341 { "Juniper MLPPP", "juniper-mlppp" },
343 /* WTAP_ENCAP_JUNIPER_MLFR */
344 { "Juniper MLFR", "juniper-mlfr" },
346 /* WTAP_ENCAP_JUNIPER_ETHER */
347 { "Juniper Ethernet", "juniper-ether" },
349 /* WTAP_ENCAP_JUNIPER_PPP */
350 { "Juniper PPP", "juniper-ppp" },
352 /* WTAP_ENCAP_JUNIPER_FRELAY */
353 { "Juniper Frame-Relay", "juniper-frelay" },
355 /* WTAP_ENCAP_JUNIPER_CHDLC */
356 { "Juniper C-HDLC", "juniper-chdlc" },
358 /* WTAP_ENCAP_JUNIPER_GGSN */
359 { "Juniper GGSN", "juniper-ggsn" },
361 /* WTAP_ENCAP_LINUX_LAPD */
364 /* WTAP_ENCAP_CATAPULT_DCT2000 */
365 { "Catapult DCT2000", "dct2000" },
368 { "ASN.1 Basic Encoding Rules", "ber" },
370 /* WTAP_ENCAP_JUNIPER_VP */
371 { "Juniper Voice PIC", "juniper-vp" },
374 { "Raw USB packets", "usb" },
376 /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
377 { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
379 /* WTAP_ENCAP_NETTL_RAW_TELNET */
380 { "Raw telnet with nettl headers", "raw-telnet-nettl" },
382 /* WTAP_ENCAP_USB_LINUX */
383 { "USB packets with Linux header", "usb-linux" },
385 /* WTAP_ENCAP_MPEG */
389 { "Per-Packet Information header", "ppi" },
392 { "Endace Record File", "erf" },
394 /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
395 { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
397 /* WTAP_ENCAP_SITA */
398 { "SITA WAN packets", "sita-wan" },
400 /* WTAP_ENCAP_SCCP */
401 { "SS7 SCCP", "sccp" },
403 /* WTAP_ENCAP_BLUETOOTH_HCI */
404 { "Bluetooth without transport layer", "bluetooth-hci" },
406 /* WTAP_ENCAP_IPMB */
407 { "Intelligent Platform Management Bus", "ipmb" },
409 /* WTAP_ENCAP_IEEE802_15_4 */
410 { "IEEE 802.15.4 Wireless PAN", "wpan" },
412 /* WTAP_ENCAP_X2E_XORAYA */
413 { "X2E Xoraya", "x2e-xoraya" },
415 /* WTAP_ENCAP_FLEXRAY */
416 { "FlexRay", "flexray" },
419 { "Local Interconnect Network", "lin" },
421 /* WTAP_ENCAP_MOST */
422 { "Media Oriented Systems Transport", "most" },
424 /* WTAP_ENCAP_CAN20B */
425 { "Controller Area Network 2.0B", "can20b" },
427 /* WTAP_ENCAP_LAYER1_EVENT */
428 { "EyeSDN Layer 1 event", "layer1-event" },
430 /* WTAP_ENCAP_X2E_SERIAL */
431 { "X2E serial line capture", "x2e-serial" },
436 /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
437 { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
439 /* WTAP_ENCAP_TNEF */
440 { "Transport-Neutral Encapsulation Format", "tnef" },
442 /* WTAP_ENCAP_USB_LINUX_MMAPPED */
443 { "USB packets with Linux header and padding", "usb-linux-mmap" },
445 /* WTAP_ENCAP_GSM_UM */
446 { "GSM Um Interface", "gsm_um" },
448 /* WTAP_ENCAP_DPNSS */
449 { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
451 /* WTAP_ENCAP_PACKETLOGGER */
452 { "PacketLogger", "packetlogger" },
454 /* WTAP_ENCAP_NSTRACE_1_0 */
455 { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
457 /* WTAP_ENCAP_NSTRACE_2_0 */
458 { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
460 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
461 { "Fibre Channel FC-2", "fc2" },
463 /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
464 { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
466 /* WTAP_ENCAP_JPEG_JFIF */
467 { "JPEG/JFIF", "jfif" },
469 /* WTAP_ENCAP_IPNET */
470 { "Solaris IPNET", "ipnet" }
474 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
475 static GArray* encap_table_arr = NULL;
476 static const struct encap_type_info* encap_table = NULL;
478 static void wtap_init_encap_types(void) {
480 if (encap_table_arr) return;
482 encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
484 g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
486 encap_table = (void*)encap_table_arr->data;
489 int wtap_get_num_encap_types(void) {
490 wtap_init_encap_types();
491 return wtap_num_encap_types;
495 int wtap_register_encap_type(char* name, char* short_name) {
496 struct encap_type_info e;
497 wtap_init_encap_types();
499 e.name = g_strdup(name);
500 e.short_name = g_strdup(short_name);
502 g_array_append_val(encap_table_arr,e);
504 encap_table = (void*)encap_table_arr->data;
506 return wtap_num_encap_types++;
510 /* Name that should be somewhat descriptive. */
512 *wtap_encap_string(int encap)
514 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
516 else if (encap == WTAP_ENCAP_PER_PACKET)
519 return encap_table[encap].name;
522 /* Name to use in, say, a command-line flag specifying the type. */
524 *wtap_encap_short_string(int encap)
526 if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
528 else if (encap == WTAP_ENCAP_PER_PACKET)
531 return encap_table[encap].short_name;
534 /* Translate a short name to a capture file type. */
536 wtap_short_string_to_encap(const char *short_name)
540 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
541 if (encap_table[encap].short_name != NULL &&
542 strcmp(short_name, encap_table[encap].short_name) == 0)
545 return -1; /* no such encapsulation type */
548 static const char *wtap_errlist[] = {
549 "The file isn't a plain file or pipe",
550 "The file is being opened for random access but is a pipe",
551 "The file isn't a capture file in a known format",
552 "File contains record data we don't support",
553 "That file format cannot be written to a pipe",
555 "Files can't be saved in that format",
556 "Files from that network type can't be saved in that format",
557 "That file format doesn't support per-packet encapsulations",
560 "Less data was read than was expected",
561 "File contains a record that's not valid",
562 "Less data was written than was requested",
563 "Uncompression error: data oddly truncated",
564 "Uncompression error: data would overflow buffer",
565 "Uncompression error: bad LZ77 offset",
566 "The standard input cannot be opened for random access"
568 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
571 *wtap_strerror(int err)
573 static char errbuf[128];
574 unsigned int wtap_errlist_index;
578 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
579 /* Assume it's a zlib error. */
580 g_snprintf(errbuf, 128, "Uncompression error: %s",
581 zError(err - WTAP_ERR_ZLIB));
585 wtap_errlist_index = -1 - err;
586 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
587 g_snprintf(errbuf, 128, "Error %d", err);
590 if (wtap_errlist[wtap_errlist_index] == NULL)
591 return "Unknown reason";
592 return wtap_errlist[wtap_errlist_index];
594 return strerror(err);
597 /* Close only the sequential side, freeing up memory it uses.
599 Note that we do *not* want to call the subtype's close function,
600 as it would free any per-subtype data, and that data may be
601 needed by the random-access side.
603 Instead, if the subtype has a "sequential close" function, we call it,
604 to free up stuff used only by the sequential side. */
606 wtap_sequential_close(wtap *wth)
608 if (wth->subtype_sequential_close != NULL)
609 (*wth->subtype_sequential_close)(wth);
611 if (wth->fh != NULL) {
616 if (wth->frame_buffer) {
617 buffer_free(wth->frame_buffer);
618 g_free(wth->frame_buffer);
619 wth->frame_buffer = NULL;
624 wtap_close(wtap *wth)
626 wtap_sequential_close(wth);
628 if (wth->subtype_close != NULL)
629 (*wth->subtype_close)(wth);
631 if (wth->random_fh != NULL)
632 file_close(wth->random_fh);
634 if (wth->priv != NULL)
641 wtap_cleareof(wtap *wth _U_) {
650 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
653 * Set the packet encapsulation to the file's encapsulation
654 * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
655 * right answer (and means that the read routine for this
656 * capture file type doesn't have to set it), and if it
657 * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
660 wth->phdr.pkt_encap = wth->file_encap;
662 if (!wth->subtype_read(wth, err, err_info, data_offset))
663 return FALSE; /* failure */
666 * It makes no sense for the captured data length to be bigger
667 * than the actual data length.
669 if (wth->phdr.caplen > wth->phdr.len)
670 wth->phdr.caplen = wth->phdr.len;
673 * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
674 * probably means the file has that encapsulation type
675 * but the read routine didn't set this packet's
676 * encapsulation type.
678 g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
680 return TRUE; /* success */
684 * Return an approximation of the amount of data we've read sequentially
685 * from the file so far. (gint64, in case that's 64 bits.)
688 wtap_read_so_far(wtap *wth, int *err)
692 file_pos = ws_lseek(wth->fd, 0, SEEK_CUR);
693 if (file_pos == -1) {
707 union wtap_pseudo_header*
708 wtap_pseudoheader(wtap *wth)
710 return &wth->pseudo_header;
714 wtap_buf_ptr(wtap *wth)
716 return buffer_start_ptr(wth->frame_buffer);
720 wtap_seek_read(wtap *wth, gint64 seek_off,
721 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
722 int *err, gchar **err_info)
724 return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,