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