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