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