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