* Don't declare a used arg as unused.
[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 int
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
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;
512
513 static void wtap_init_encap_types(void) {
514
515         if (encap_table_arr) return;
516
517         encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
518
519         g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
520
521         encap_table = (void*)encap_table_arr->data;
522 }
523
524 int wtap_get_num_encap_types(void) {
525         wtap_init_encap_types();
526         return wtap_num_encap_types;
527 }
528
529
530 int wtap_register_encap_type(char* name, char* short_name) {
531         struct encap_type_info e;
532         wtap_init_encap_types();
533
534         e.name = g_strdup(name);
535         e.short_name = g_strdup(short_name);
536
537         g_array_append_val(encap_table_arr,e);
538
539         encap_table = (void*)encap_table_arr->data;
540
541         return wtap_num_encap_types++;
542 }
543
544
545 /* Name that should be somewhat descriptive. */
546 const char
547 *wtap_encap_string(int encap)
548 {
549         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
550                 return "Illegal";
551         else if (encap == WTAP_ENCAP_PER_PACKET)
552                 return "Per packet";
553         else
554                 return encap_table[encap].name;
555 }
556
557 /* Name to use in, say, a command-line flag specifying the type. */
558 const char
559 *wtap_encap_short_string(int encap)
560 {
561         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
562                 return "illegal";
563         else if (encap == WTAP_ENCAP_PER_PACKET)
564                 return "per-packet";
565         else
566                 return encap_table[encap].short_name;
567 }
568
569 /* Translate a short name to a capture file type. */
570 int
571 wtap_short_string_to_encap(const char *short_name)
572 {
573         int encap;
574
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)
578                         return encap;
579         }
580         return -1;      /* no such encapsulation type */
581 }
582
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",
589         NULL,
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",
593         NULL,
594         NULL,
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",
603         NULL,
604         "Uncompression error",
605         "Internal error"
606 };
607 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
608
609 const char
610 *wtap_strerror(int err)
611 {
612         static char errbuf[128];
613         unsigned int wtap_errlist_index;
614
615         if (err < 0) {
616                 wtap_errlist_index = -1 - err;
617                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
618                         g_snprintf(errbuf, 128, "Error %d", err);
619                         return errbuf;
620                 }
621                 if (wtap_errlist[wtap_errlist_index] == NULL)
622                         return "Unknown reason";
623                 return wtap_errlist[wtap_errlist_index];
624         } else
625                 return strerror(err);
626 }
627
628 /* Close only the sequential side, freeing up memory it uses.
629
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.
633
634    Instead, if the subtype has a "sequential close" function, we call it,
635    to free up stuff used only by the sequential side. */
636 void
637 wtap_sequential_close(wtap *wth)
638 {
639         if (wth->subtype_sequential_close != NULL)
640                 (*wth->subtype_sequential_close)(wth);
641
642         if (wth->fh != NULL) {
643                 file_close(wth->fh);
644                 wth->fh = NULL;
645         }
646
647         if (wth->frame_buffer) {
648                 buffer_free(wth->frame_buffer);
649                 g_free(wth->frame_buffer);
650                 wth->frame_buffer = NULL;
651         }
652 }
653
654 static void
655 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
656 {
657         g_free(data);
658 }
659
660 void
661 wtap_close(wtap *wth)
662 {
663         wtap_sequential_close(wth);
664
665         if (wth->subtype_close != NULL)
666                 (*wth->subtype_close)(wth);
667
668         if (wth->random_fh != NULL)
669                 file_close(wth->random_fh);
670
671         if (wth->priv != NULL)
672                 g_free(wth->priv);
673
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);
677         }
678         g_free(wth);
679 }
680
681 void
682 wtap_cleareof(wtap *wth) {
683         /* Reset EOF */
684         file_clearerr(wth->fh);
685 }
686
687 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
688         if (wth)
689                 wth->add_new_ipv4 = add_new_ipv4;
690 }
691
692 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
693         if (wth)
694                 wth->add_new_ipv6 = add_new_ipv6;
695 }
696
697 gboolean
698 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
699 {
700         /*
701          * Set the packet encapsulation to the file's encapsulation
702          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
703          * right answer (and means that the read routine for this
704          * capture file type doesn't have to set it), and if it
705          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
706          * anyway.
707          */
708         wth->phdr.pkt_encap = wth->file_encap;
709
710         if (!wth->subtype_read(wth, err, err_info, data_offset)) {
711                 /*
712                  * If we didn't get an error indication, we read
713                  * the last packet.  See if there's any deferred
714                  * error, as might, for example, occur if we're
715                  * reading a compressed file, and we got an error
716                  * reading compressed data from the file, but
717                  * got enough compressed data to decompress the
718                  * last packet of the file.
719                  */
720                 if (*err == 0)
721                         *err = file_error(wth->fh, err_info);
722                 return FALSE;   /* failure */
723         }
724
725         /*
726          * It makes no sense for the captured data length to be bigger
727          * than the actual data length.
728          */
729         if (wth->phdr.caplen > wth->phdr.len)
730                 wth->phdr.caplen = wth->phdr.len;
731
732         /*
733          * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
734          * probably means the file has that encapsulation type
735          * but the read routine didn't set this packet's
736          * encapsulation type.
737          */
738         g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
739
740         return TRUE;    /* success */
741 }
742
743 /*
744  * Return an approximation of the amount of data we've read sequentially
745  * from the file so far.  (gint64, in case that's 64 bits.)
746  */
747 gint64
748 wtap_read_so_far(wtap *wth)
749 {
750         return file_tell_raw(wth->fh);
751 }
752
753 struct wtap_pkthdr*
754 wtap_phdr(wtap *wth)
755 {
756         return &wth->phdr;
757 }
758
759 union wtap_pseudo_header*
760 wtap_pseudoheader(wtap *wth)
761 {
762         return &wth->pseudo_header;
763 }
764
765 guint8*
766 wtap_buf_ptr(wtap *wth)
767 {
768         return buffer_start_ptr(wth->frame_buffer);
769 }
770
771 gboolean
772 wtap_seek_read(wtap *wth, gint64 seek_off,
773         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
774         int *err, gchar **err_info)
775 {
776         return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
777                 err, err_info);
778 }