From Harald Welte:
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef HAVE_LIBZ
39 #include <zlib.h>
40 #endif
41
42 #include "wtap-int.h"
43
44 #include "file_wrappers.h"
45 #include <wsutil/file_util.h>
46 #include "buffer.h"
47
48 /*
49  * Return the size of the file, as reported by the OS.
50  * (gint64, in case that's 64 bits.)
51  */
52 gint64
53 wtap_file_size(wtap *wth, int *err)
54 {
55         ws_statb64 statb;
56
57         if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
58             &statb, err) == -1)
59                 return -1;
60         return statb.st_size;
61 }
62
63 /*
64  * Do an fstat on the file.
65  */
66 int
67 wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
68 {
69         if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
70             statb, err) == -1)
71                 return -1;
72         return 0;
73 }
74
75 int
76 wtap_file_type(wtap *wth)
77 {
78         return wth->file_type;
79 }
80
81 guint
82 wtap_snapshot_length(wtap *wth)
83 {
84         return wth->snapshot_length;
85 }
86
87 int
88 wtap_file_encap(wtap *wth)
89 {
90         return wth->file_encap;
91 }
92
93 int
94 wtap_file_tsprecision(wtap *wth)
95 {
96         return wth->tsprecision;
97 }
98
99 /* Table of the encapsulation types we know about. */
100 struct encap_type_info {
101         const char *name;
102         const char *short_name;
103 };
104
105 static struct encap_type_info encap_table_base[] = {
106         /* WTAP_ENCAP_UNKNOWN */
107         { "Unknown", "unknown" },
108
109         /* WTAP_ENCAP_ETHERNET */
110         { "Ethernet", "ether" },
111
112         /* WTAP_ENCAP_TOKEN_RING */
113         { "Token Ring", "tr" },
114
115         /* WTAP_ENCAP_SLIP */
116         { "SLIP", "slip" },
117
118         /* WTAP_ENCAP_PPP */
119         { "PPP", "ppp" },
120
121         /* WTAP_ENCAP_FDDI */
122         { "FDDI", "fddi" },
123
124         /* WTAP_ENCAP_FDDI_BITSWAPPED */
125         { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
126
127         /* WTAP_ENCAP_RAW_IP */
128         { "Raw IP", "rawip" },
129
130         /* WTAP_ENCAP_ARCNET */
131         { "ARCNET", "arcnet" },
132
133         /* WTAP_ENCAP_ARCNET_LINUX */
134         { "Linux ARCNET", "arcnet_linux" },
135
136         /* WTAP_ENCAP_ATM_RFC1483 */
137         { "RFC 1483 ATM", "atm-rfc1483" },
138
139         /* WTAP_ENCAP_LINUX_ATM_CLIP */
140         { "Linux ATM CLIP", "linux-atm-clip" },
141
142         /* WTAP_ENCAP_LAPB */
143         { "LAPB", "lapb" },
144
145         /* WTAP_ENCAP_ATM_PDUS */
146         { "ATM PDUs", "atm-pdus" },
147
148         /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
149         { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
150
151         /* WTAP_ENCAP_NULL */
152         { "NULL", "null" },
153
154         /* WTAP_ENCAP_ASCEND */
155         { "Lucent/Ascend access equipment", "ascend" },
156
157         /* WTAP_ENCAP_ISDN */
158         { "ISDN", "isdn" },
159
160         /* WTAP_ENCAP_IP_OVER_FC */
161         { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
162
163         /* WTAP_ENCAP_PPP_WITH_PHDR */
164         { "PPP with Directional Info", "ppp-with-direction" },
165
166         /* WTAP_ENCAP_IEEE_802_11 */
167         { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
168
169         /* WTAP_ENCAP_PRISM_HEADER */
170         { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
171
172         /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
173         { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
174
175         /* WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP */
176         { "IEEE 802.11 plus radiotap WLAN header", "ieee-802-11-radiotap" },
177
178         /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
179         { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
180
181         /* WTAP_ENCAP_SLL */
182         { "Linux cooked-mode capture", "linux-sll" },
183
184         /* WTAP_ENCAP_FRELAY */
185         { "Frame Relay", "frelay" },
186
187         /* WTAP_ENCAP_FRELAY_WITH_PHDR */
188         { "Frame Relay with Directional Info", "frelay-with-direction" },
189
190         /* WTAP_ENCAP_CHDLC */
191         { "Cisco HDLC", "chdlc" },
192
193         /* WTAP_ENCAP_CISCO_IOS */
194         { "Cisco IOS internal", "ios" },
195
196         /* WTAP_ENCAP_LOCALTALK */
197         { "Localtalk", "ltalk" },
198
199         /* WTAP_ENCAP_OLD_PFLOG  */
200         { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
201
202         /* WTAP_ENCAP_HHDLC */
203         { "HiPath HDLC", "hhdlc" },
204
205         /* WTAP_ENCAP_DOCSIS */
206         { "Data Over Cable Service Interface Specification", "docsis" },
207
208         /* WTAP_ENCAP_COSINE */
209         { "CoSine L2 debug log", "cosine" },
210
211         /* WTAP_ENCAP_WFLEET_HDLC */
212         { "Wellfleet HDLC", "whdlc" },
213
214         /* WTAP_ENCAP_SDLC */
215         { "SDLC", "sdlc" },
216
217         /* WTAP_ENCAP_TZSP */
218         { "Tazmen sniffer protocol", "tzsp" },
219
220         /* WTAP_ENCAP_ENC */
221         { "OpenBSD enc(4) encapsulating interface", "enc" },
222
223         /* WTAP_ENCAP_PFLOG  */
224         { "OpenBSD PF Firewall logs", "pflog" },
225
226         /* WTAP_ENCAP_CHDLC_WITH_PHDR */
227         { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
228
229         /* WTAP_ENCAP_BLUETOOTH_H4 */
230         { "Bluetooth H4", "bluetooth-h4" },
231
232         /* WTAP_ENCAP_MTP2 */
233         { "SS7 MTP2", "mtp2" },
234
235         /* WTAP_ENCAP_MTP3 */
236         { "SS7 MTP3", "mtp3" },
237
238         /* WTAP_ENCAP_IRDA */
239         { "IrDA", "irda" },
240
241         /* WTAP_ENCAP_USER0 */
242         { "USER 0", "user0" },
243
244         /* WTAP_ENCAP_USER1 */
245         { "USER 1", "user1" },
246
247         /* WTAP_ENCAP_USER2 */
248         { "USER 2", "user2" },
249
250         /* WTAP_ENCAP_USER3 */
251         { "USER 3", "user3" },
252
253         /* WTAP_ENCAP_USER4 */
254         { "USER 4", "user4" },
255
256         /* WTAP_ENCAP_USER5 */
257         { "USER 5", "user5" },
258
259         /* WTAP_ENCAP_USER6 */
260         { "USER 6", "user6" },
261
262         /* WTAP_ENCAP_USER7 */
263         { "USER 7", "user7" },
264
265         /* WTAP_ENCAP_USER8 */
266         { "USER 8", "user8" },
267
268         /* WTAP_ENCAP_USER9 */
269         { "USER 9", "user9" },
270
271         /* WTAP_ENCAP_USER10 */
272         { "USER 10", "user10" },
273
274         /* WTAP_ENCAP_USER11 */
275         { "USER 11", "user11" },
276
277         /* WTAP_ENCAP_USER12 */
278         { "USER 12", "user12" },
279
280         /* WTAP_ENCAP_USER13 */
281         { "USER 13", "user13" },
282
283         /* WTAP_ENCAP_USER14 */
284         { "USER 14", "user14" },
285
286         /* WTAP_ENCAP_USER15 */
287         { "USER 15", "user15" },
288
289         /* WTAP_ENCAP_SYMANTEC */
290         { "Symantec Enterprise Firewall", "symantec" },
291
292         /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
293         { "Apple IP-over-IEEE 1394", "ap1394" },
294
295         /* WTAP_ENCAP_BACNET_MS_TP */
296         { "BACnet MS/TP", "bacnet-ms-tp" },
297
298         /* WTAP_ENCAP_NETTL_RAW_ICMP */
299         { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
300
301         /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
302         { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
303
304         /* WTAP_ENCAP_GPRS_LLC */
305         { "GPRS LLC", "gprs-llc" },
306
307         /* WTAP_ENCAP_JUNIPER_ATM1 */
308         { "Juniper ATM1", "juniper-atm1" },
309
310         /* WTAP_ENCAP_JUNIPER_ATM2 */
311         { "Juniper ATM2", "juniper-atm2" },
312
313         /* WTAP_ENCAP_REDBACK */
314         { "Redback SmartEdge", "redback" },
315
316         /* WTAP_ENCAP_NETTL_RAW_IP */
317         { "Raw IP with nettl headers", "rawip-nettl" },
318
319         /* WTAP_ENCAP_NETTL_ETHERNET */
320         { "Ethernet with nettl headers", "ether-nettl" },
321
322         /* WTAP_ENCAP_NETTL_TOKEN_RING */
323         { "Token Ring with nettl headers", "tr-nettl" },
324
325         /* WTAP_ENCAP_NETTL_FDDI */
326         { "FDDI with nettl headers", "fddi-nettl" },
327
328         /* WTAP_ENCAP_NETTL_UNKNOWN */
329         { "Unknown link-layer type with nettl headers", "unknown-nettl" },
330
331         /* WTAP_ENCAP_MTP2_WITH_PHDR */
332         { "MTP2 with pseudoheader", "mtp2-with-phdr" },
333
334         /* WTAP_ENCAP_JUNIPER_PPPOE */
335         { "Juniper PPPoE", "juniper-pppoe" },
336
337         /* WTAP_ENCAP_GCOM_TIE1 */
338         { "GCOM TIE1", "gcom-tie1" },
339
340         /* WTAP_ENCAP_GCOM_SERIAL */
341         { "GCOM Serial", "gcom-serial" },
342
343         /* WTAP_ENCAP_NETTL_X25 */
344         { "X25 with nettl headers", "x25-nettl" },
345
346         /* WTAP_ENCAP_K12 */
347         { "K12 protocol analyzer", "k12" },
348
349         /* WTAP_ENCAP_JUNIPER_MLPPP */
350         { "Juniper MLPPP", "juniper-mlppp" },
351
352         /* WTAP_ENCAP_JUNIPER_MLFR */
353         { "Juniper MLFR", "juniper-mlfr" },
354
355         /* WTAP_ENCAP_JUNIPER_ETHER */
356         { "Juniper Ethernet", "juniper-ether" },
357
358         /* WTAP_ENCAP_JUNIPER_PPP */
359         { "Juniper PPP", "juniper-ppp" },
360
361         /* WTAP_ENCAP_JUNIPER_FRELAY */
362         { "Juniper Frame-Relay", "juniper-frelay" },
363
364         /* WTAP_ENCAP_JUNIPER_CHDLC */
365         { "Juniper C-HDLC", "juniper-chdlc" },
366
367         /* WTAP_ENCAP_JUNIPER_GGSN */
368         { "Juniper GGSN", "juniper-ggsn" },
369
370         /* WTAP_ENCAP_LINUX_LAPD */
371         { "LAPD", "lapd" },
372
373         /* WTAP_ENCAP_CATAPULT_DCT2000 */
374         { "Catapult DCT2000", "dct2000" },
375
376         /* WTAP_ENCAP_BER */
377         { "ASN.1 Basic Encoding Rules", "ber" },
378
379         /* WTAP_ENCAP_JUNIPER_VP */
380         { "Juniper Voice PIC", "juniper-vp" },
381
382         /* WTAP_ENCAP_USB */
383         { "Raw USB packets", "usb" },
384
385         /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
386         { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
387
388         /* WTAP_ENCAP_NETTL_RAW_TELNET */
389         { "Raw telnet with nettl headers", "raw-telnet-nettl" },
390
391         /* WTAP_ENCAP_USB_LINUX */
392         { "USB packets with Linux header", "usb-linux" },
393
394         /* WTAP_ENCAP_MPEG */
395         { "MPEG", "mpeg" },
396
397         /* WTAP_ENCAP_PPI */
398         { "Per-Packet Information header", "ppi" },
399
400         /* WTAP_ENCAP_ERF */
401         { "Endace Record File", "erf" },
402
403         /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
404         { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
405
406         /* WTAP_ENCAP_SITA */
407         { "SITA WAN packets", "sita-wan" },
408
409         /* WTAP_ENCAP_SCCP */
410         { "SS7 SCCP", "sccp" },
411
412         /* WTAP_ENCAP_BLUETOOTH_HCI */
413         { "Bluetooth without transport layer", "bluetooth-hci" },
414
415         /* WTAP_ENCAP_IPMB */
416         { "Intelligent Platform Management Bus", "ipmb" },
417
418         /* WTAP_ENCAP_IEEE802_15_4 */
419         { "IEEE 802.15.4 Wireless PAN", "wpan" },
420
421         /* WTAP_ENCAP_X2E_XORAYA */
422         { "X2E Xoraya", "x2e-xoraya" },
423
424         /* WTAP_ENCAP_FLEXRAY */
425         { "FlexRay", "flexray" },
426
427         /* WTAP_ENCAP_LIN */
428         { "Local Interconnect Network", "lin" },
429
430         /* WTAP_ENCAP_MOST */
431         { "Media Oriented Systems Transport", "most" },
432
433         /* WTAP_ENCAP_CAN20B */
434         { "Controller Area Network 2.0B", "can20b" },
435
436         /* WTAP_ENCAP_LAYER1_EVENT */
437         { "EyeSDN Layer 1 event", "layer1-event" },
438
439         /* WTAP_ENCAP_X2E_SERIAL */
440         { "X2E serial line capture", "x2e-serial" },
441
442         /* WTAP_ENCAP_I2C */
443         { "I2C", "i2c" },
444
445         /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
446         { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
447
448         /* WTAP_ENCAP_TNEF */
449         { "Transport-Neutral Encapsulation Format", "tnef" },
450
451         /* WTAP_ENCAP_USB_LINUX_MMAPPED */
452         { "USB packets with Linux header and padding", "usb-linux-mmap" },
453
454         /* WTAP_ENCAP_GSM_UM */
455         { "GSM Um Interface", "gsm_um" },
456
457         /* WTAP_ENCAP_DPNSS */
458         { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
459
460         /* WTAP_ENCAP_PACKETLOGGER */
461         { "PacketLogger", "packetlogger" },
462
463         /* WTAP_ENCAP_NSTRACE_1_0 */
464         { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
465
466         /* WTAP_ENCAP_NSTRACE_2_0 */
467         { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
468
469         /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
470         { "Fibre Channel FC-2", "fc2" },
471
472         /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
473         { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
474
475         /* WTAP_ENCAP_JPEG_JFIF */
476         { "JPEG/JFIF", "jfif" },
477
478         /* WTAP_ENCAP_IPNET */
479         { "Solaris IPNET", "ipnet" },
480
481         /* WTAP_ENCAP_SOCKETCAN */
482         { "SocketCAN", "socketcan" },
483
484         /* WTAP_ENCAP_IEEE802_11_NETMON_RADIO */
485         { "IEEE 802.11 plus Network Monitor radio header", "ieee-802-11-netmon" },
486
487         /* WTAP_ENCAP_IEEE802_15_4_NOFCS */
488         { "IEEE 802.15.4 Wireless PAN with FCS not present", "wpan-nofcs" },
489
490         /* WTAP_ENCAP_RAW_IPFIX */
491         { "IPFIX", "ipfix" },
492
493         /* WTAP_ENCAP_RAW_IP4 */
494         { "Raw IPv4", "rawip4" },
495
496         /* WTAP_ENCAP_RAW_IP6 */
497         { "Raw IPv6", "rawip6" },
498
499         /* WTAP_ENCAP_LAPD */
500         { "Lapd header", "lapd" },
501
502         /* WTAP_ENCAP_DVBCI */
503         { "DVB-CI (Common Interface)", "dvbci"},
504
505         /* WTAP_ENCAP_MUX27010 */
506         { "MUX27010", "mux27010"},
507
508         /* WTAP_ENCAP_MIME */
509         { "MIME", "mime" },
510
511         /* WTAP_ENCAP_NETANALYZER */
512         { "netANALYZER", "netanalyzer" },
513
514         /* WTAP_ENCAP_NETANALYZER_TRANSPARENT */
515         { "netANALYZER-Transparent", "netanalyzer-transparent" },
516
517         /* WTAP_ENCAP_IP_OVER_IB */
518         { "IP over Infiniband", "ip-over-ib" },
519
520         /* WTAP_ENCAP_MPEG_2_TS */
521         { "ISO/IEC 13818-1 MPEG2-TS", "mp2ts" }
522 };
523
524 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
525 static GArray* encap_table_arr = NULL;
526 static const struct encap_type_info* encap_table = NULL;
527
528 static void wtap_init_encap_types(void) {
529
530         if (encap_table_arr) return;
531
532         encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
533
534         g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
535
536         encap_table = (void*)encap_table_arr->data;
537 }
538
539 int wtap_get_num_encap_types(void) {
540         wtap_init_encap_types();
541         return wtap_num_encap_types;
542 }
543
544
545 int wtap_register_encap_type(char* name, char* short_name) {
546         struct encap_type_info e;
547         wtap_init_encap_types();
548
549         e.name = g_strdup(name);
550         e.short_name = g_strdup(short_name);
551
552         g_array_append_val(encap_table_arr,e);
553
554         encap_table = (void*)encap_table_arr->data;
555
556         return wtap_num_encap_types++;
557 }
558
559
560 /* Name that should be somewhat descriptive. */
561 const char
562 *wtap_encap_string(int encap)
563 {
564         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
565                 return "Illegal";
566         else if (encap == WTAP_ENCAP_PER_PACKET)
567                 return "Per packet";
568         else
569                 return encap_table[encap].name;
570 }
571
572 /* Name to use in, say, a command-line flag specifying the type. */
573 const char
574 *wtap_encap_short_string(int encap)
575 {
576         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
577                 return "illegal";
578         else if (encap == WTAP_ENCAP_PER_PACKET)
579                 return "per-packet";
580         else
581                 return encap_table[encap].short_name;
582 }
583
584 /* Translate a short name to a capture file type. */
585 int
586 wtap_short_string_to_encap(const char *short_name)
587 {
588         int encap;
589
590         for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
591                 if (encap_table[encap].short_name != NULL &&
592                     strcmp(short_name, encap_table[encap].short_name) == 0)
593                         return encap;
594         }
595         return -1;      /* no such encapsulation type */
596 }
597
598 static const char *wtap_errlist[] = {
599         "The file isn't a plain file or pipe",
600         "The file is being opened for random access but is a pipe",
601         "The file isn't a capture file in a known format",
602         "File contains record data we don't support",
603         "That file format cannot be written to a pipe",
604         NULL,
605         "Files can't be saved in that format",
606         "Files from that network type can't be saved in that format",
607         "That file format doesn't support per-packet encapsulations",
608         NULL,
609         NULL,
610         "Less data was read than was expected",
611         "The file appears to be damaged or corrupt.",
612         "Less data was written than was requested",
613         "Uncompression error: data oddly truncated",
614         "Uncompression error: data would overflow buffer",
615         "Uncompression error: bad LZ77 offset",
616         "The standard input cannot be opened for random access",
617         "That file format doesn't support compression",
618         NULL,
619         "Uncompression error",
620         "Internal error"
621 };
622 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
623
624 const char
625 *wtap_strerror(int err)
626 {
627         static char errbuf[128];
628         unsigned int wtap_errlist_index;
629
630         if (err < 0) {
631                 wtap_errlist_index = -1 - err;
632                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
633                         g_snprintf(errbuf, 128, "Error %d", err);
634                         return errbuf;
635                 }
636                 if (wtap_errlist[wtap_errlist_index] == NULL)
637                         return "Unknown reason";
638                 return wtap_errlist[wtap_errlist_index];
639         } else
640                 return g_strerror(err);
641 }
642
643 /* Close only the sequential side, freeing up memory it uses.
644
645    Note that we do *not* want to call the subtype's close function,
646    as it would free any per-subtype data, and that data may be
647    needed by the random-access side.
648
649    Instead, if the subtype has a "sequential close" function, we call it,
650    to free up stuff used only by the sequential side. */
651 void
652 wtap_sequential_close(wtap *wth)
653 {
654         if (wth->subtype_sequential_close != NULL)
655                 (*wth->subtype_sequential_close)(wth);
656
657         if (wth->fh != NULL) {
658                 file_close(wth->fh);
659                 wth->fh = NULL;
660         }
661
662         if (wth->frame_buffer) {
663                 buffer_free(wth->frame_buffer);
664                 g_free(wth->frame_buffer);
665                 wth->frame_buffer = NULL;
666         }
667 }
668
669 static void
670 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
671 {
672         g_free(data);
673 }
674
675 void
676 wtap_close(wtap *wth)
677 {
678         wtap_sequential_close(wth);
679
680         if (wth->subtype_close != NULL)
681                 (*wth->subtype_close)(wth);
682
683         if (wth->random_fh != NULL)
684                 file_close(wth->random_fh);
685
686         if (wth->priv != NULL)
687                 g_free(wth->priv);
688
689         if (wth->fast_seek != NULL) {
690                 g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
691                 g_ptr_array_free(wth->fast_seek, TRUE);
692         }
693         g_free(wth);
694 }
695
696 void
697 wtap_cleareof(wtap *wth) {
698         /* Reset EOF */
699         file_clearerr(wth->fh);
700 }
701
702 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
703         if (wth)
704                 wth->add_new_ipv4 = add_new_ipv4;
705 }
706
707 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
708         if (wth)
709                 wth->add_new_ipv6 = add_new_ipv6;
710 }
711
712 gboolean
713 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
714 {
715         /*
716          * Set the packet encapsulation to the file's encapsulation
717          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
718          * right answer (and means that the read routine for this
719          * capture file type doesn't have to set it), and if it
720          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
721          * anyway.
722          */
723         wth->phdr.pkt_encap = wth->file_encap;
724
725         if (!wth->subtype_read(wth, err, err_info, data_offset)) {
726                 /*
727                  * If we didn't get an error indication, we read
728                  * the last packet.  See if there's any deferred
729                  * error, as might, for example, occur if we're
730                  * reading a compressed file, and we got an error
731                  * reading compressed data from the file, but
732                  * got enough compressed data to decompress the
733                  * last packet of the file.
734                  */
735                 if (*err == 0)
736                         *err = file_error(wth->fh, err_info);
737                 return FALSE;   /* failure */
738         }
739
740         /*
741          * It makes no sense for the captured data length to be bigger
742          * than the actual data length.
743          */
744         if (wth->phdr.caplen > wth->phdr.len)
745                 wth->phdr.caplen = wth->phdr.len;
746
747         /*
748          * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
749          * probably means the file has that encapsulation type
750          * but the read routine didn't set this packet's
751          * encapsulation type.
752          */
753         g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
754
755         return TRUE;    /* success */
756 }
757
758 /*
759  * Return an approximation of the amount of data we've read sequentially
760  * from the file so far.  (gint64, in case that's 64 bits.)
761  */
762 gint64
763 wtap_read_so_far(wtap *wth)
764 {
765         return file_tell_raw(wth->fh);
766 }
767
768 struct wtap_pkthdr*
769 wtap_phdr(wtap *wth)
770 {
771         return &wth->phdr;
772 }
773
774 union wtap_pseudo_header*
775 wtap_pseudoheader(wtap *wth)
776 {
777         return &wth->pseudo_header;
778 }
779
780 guint8*
781 wtap_buf_ptr(wtap *wth)
782 {
783         return buffer_start_ptr(wth->frame_buffer);
784 }
785
786 gboolean
787 wtap_seek_read(wtap *wth, gint64 seek_off,
788         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
789         int *err, gchar **err_info)
790 {
791         return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
792                 err, err_info);
793 }