kerberos dissect KERBEROS_AD_GSS_API_ETYPE_NEGOTIATION content
[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  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include <config.h>
10
11 #include <string.h>
12 #include <errno.h>
13
14 #include <sys/types.h>
15
16 #include "wtap-int.h"
17 #include "wtap_opttypes.h"
18 #include "pcapng.h"
19
20 #include "file_wrappers.h"
21 #include <wsutil/file_util.h>
22 #include <wsutil/buffer.h>
23
24 #ifdef HAVE_PLUGINS
25
26
27 static plugins_t *libwiretap_plugins = NULL;
28 static GSList *wtap_plugins = NULL;
29
30 void
31 wtap_register_plugin(const wtap_plugin *plug)
32 {
33         wtap_plugins = g_slist_prepend(wtap_plugins, (wtap_plugin *)plug);
34 }
35
36 static void
37 call_plugin_register_wtap_module(gpointer data, gpointer user_data _U_)
38 {
39         wtap_plugin *plug = (wtap_plugin *)data;
40
41         if (plug->register_wtap_module) {
42                 plug->register_wtap_module();
43         }
44 }
45 #endif /* HAVE_PLUGINS */
46
47 /*
48  * Return the size of the file, as reported by the OS.
49  * (gint64, in case that's 64 bits.)
50  */
51 gint64
52 wtap_file_size(wtap *wth, int *err)
53 {
54         ws_statb64 statb;
55
56         if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
57             &statb, err) == -1)
58                 return -1;
59         return statb.st_size;
60 }
61
62 /*
63  * Do an fstat on the file.
64  */
65 int
66 wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
67 {
68         if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
69             statb, err) == -1)
70                 return -1;
71         return 0;
72 }
73
74 int
75 wtap_file_type_subtype(wtap *wth)
76 {
77         return wth->file_type_subtype;
78 }
79
80 gboolean
81 wtap_iscompressed(wtap *wth)
82 {
83         return file_iscompressed((wth->fh == NULL) ? wth->random_fh : wth->fh);
84 }
85
86 guint
87 wtap_snapshot_length(wtap *wth)
88 {
89         return wth->snapshot_length;
90 }
91
92 int
93 wtap_file_encap(wtap *wth)
94 {
95         return wth->file_encap;
96 }
97
98 int
99 wtap_file_tsprec(wtap *wth)
100 {
101         return wth->file_tsprec;
102 }
103
104 wtap_block_t
105 wtap_file_get_shb(wtap *wth)
106 {
107         if ((wth == NULL) || (wth->shb_hdrs == NULL) || (wth->shb_hdrs->len == 0))
108                 return NULL;
109
110         return g_array_index(wth->shb_hdrs, wtap_block_t, 0);
111 }
112
113 GArray*
114 wtap_file_get_shb_for_new_file(wtap *wth)
115 {
116         guint shb_count;
117         wtap_block_t shb_hdr_src, shb_hdr_dest;
118         GArray* shb_hdrs;
119
120         if ((wth == NULL) || (wth->shb_hdrs == NULL) || (wth->shb_hdrs->len == 0))
121                 return NULL;
122
123         shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
124
125         for (shb_count = 0; shb_count < wth->shb_hdrs->len; shb_count++) {
126                 shb_hdr_src = g_array_index(wth->shb_hdrs, wtap_block_t, shb_count);
127                 shb_hdr_dest = wtap_block_create(WTAP_BLOCK_NG_SECTION);
128                 wtap_block_copy(shb_hdr_dest, shb_hdr_src);
129                 g_array_append_val(shb_hdrs, shb_hdr_dest);
130         }
131
132         return shb_hdrs;
133 }
134
135 /*
136  * XXX - replace with APIs that let us handle multiple comments.
137  */
138 void
139 wtap_write_shb_comment(wtap *wth, gchar *comment)
140 {
141         if ((wth != NULL) && (wth->shb_hdrs != NULL) && (wth->shb_hdrs->len > 0)) {
142                 wtap_block_set_nth_string_option_value(g_array_index(wth->shb_hdrs, wtap_block_t, 0), OPT_COMMENT, 0, comment, (gsize)(comment ? strlen(comment) : 0));
143         }
144 }
145
146 wtapng_iface_descriptions_t *
147 wtap_file_get_idb_info(wtap *wth)
148 {
149         wtapng_iface_descriptions_t *idb_info;
150
151         idb_info = g_new(wtapng_iface_descriptions_t,1);
152
153         idb_info->interface_data        = wth->interface_data;
154
155         return idb_info;
156 }
157
158
159 void
160 wtap_free_idb_info(wtapng_iface_descriptions_t *idb_info)
161 {
162         if (idb_info == NULL)
163                 return;
164
165         wtap_block_array_free(idb_info->interface_data);
166         g_free(idb_info);
167 }
168
169 gchar *
170 wtap_get_debug_if_descr(const wtap_block_t if_descr,
171                         const int indent,
172                         const char* line_end)
173 {
174         char* tmp_content;
175         wtapng_if_descr_mandatory_t* if_descr_mand;
176         GString *info = g_string_new("");
177         guint64 tmp64;
178         gint8 itmp8;
179         guint8 tmp8;
180         wtapng_if_descr_filter_t* if_filter;
181
182         g_assert(if_descr);
183
184         if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(if_descr);
185         if (wtap_block_get_string_option_value(if_descr, OPT_IDB_NAME, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
186                 g_string_printf(info,
187                                 "%*cName = %s%s", indent, ' ',
188                                 tmp_content ? tmp_content : "UNKNOWN",
189                                 line_end);
190         }
191
192         if (wtap_block_get_string_option_value(if_descr, OPT_IDB_DESCR, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
193                 g_string_append_printf(info,
194                                 "%*cDescription = %s%s", indent, ' ',
195                                 tmp_content ? tmp_content : "NONE",
196                                 line_end);
197         }
198
199         g_string_append_printf(info,
200                         "%*cEncapsulation = %s (%d - %s)%s", indent, ' ',
201                         wtap_encap_string(if_descr_mand->wtap_encap),
202                         if_descr_mand->wtap_encap,
203                         wtap_encap_short_string(if_descr_mand->wtap_encap),
204                         line_end);
205
206         if (wtap_block_get_string_option_value(if_descr, OPT_IDB_HARDWARE, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
207                 g_string_append_printf(info,
208                                 "%*cHardware = %s%s", indent, ' ',
209                                 tmp_content ? tmp_content : "NONE",
210                                 line_end);
211         }
212
213         if (wtap_block_get_uint64_option_value(if_descr, OPT_IDB_SPEED, &tmp64) == WTAP_OPTTYPE_SUCCESS) {
214                 g_string_append_printf(info,
215                                 "%*cSpeed = %" G_GINT64_MODIFIER "u%s", indent, ' ',
216                                 tmp64,
217                                 line_end);
218         }
219
220         g_string_append_printf(info,
221                         "%*cCapture length = %u%s", indent, ' ',
222                         if_descr_mand->snap_len,
223                         line_end);
224
225         if (wtap_block_get_uint8_option_value(if_descr, OPT_IDB_FCSLEN, &itmp8) == WTAP_OPTTYPE_SUCCESS) {
226                 g_string_append_printf(info,
227                                 "%*cFCS length = %d%s", indent, ' ',
228                                 itmp8,
229                                 line_end);
230         }
231
232         g_string_append_printf(info,
233                         "%*cTime precision = %s (%d)%s", indent, ' ',
234                         wtap_tsprec_string(if_descr_mand->tsprecision),
235                         if_descr_mand->tsprecision,
236                         line_end);
237
238         g_string_append_printf(info,
239                         "%*cTime ticks per second = %" G_GINT64_MODIFIER "u%s", indent, ' ',
240                         if_descr_mand->time_units_per_second,
241                         line_end);
242
243         if (wtap_block_get_uint8_option_value(if_descr, OPT_IDB_TSRESOL, &tmp8) == WTAP_OPTTYPE_SUCCESS) {
244                 g_string_append_printf(info,
245                                 "%*cTime resolution = 0x%.2x%s", indent, ' ',
246                                 tmp8,
247                                 line_end);
248         }
249
250         if (wtap_block_get_custom_option_value(if_descr, OPT_IDB_FILTER, (void**)&if_filter) == WTAP_OPTTYPE_SUCCESS) {
251                 g_string_append_printf(info,
252                                 "%*cFilter string = %s%s", indent, ' ',
253                                 if_filter->if_filter_str ? if_filter->if_filter_str : "NONE",
254                                 line_end);
255
256                 g_string_append_printf(info,
257                                 "%*cBPF filter length = %u%s", indent, ' ',
258                                 if_filter->bpf_filter_len,
259                                 line_end);
260         }
261
262         if (wtap_block_get_string_option_value(if_descr, OPT_IDB_OS, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
263                 g_string_append_printf(info,
264                                 "%*cOperating system = %s%s", indent, ' ',
265                                 tmp_content ? tmp_content : "UNKNOWN",
266                                 line_end);
267         }
268
269         /*
270          * XXX - support multiple comments.
271          */
272         if (wtap_block_get_nth_string_option_value(if_descr, OPT_COMMENT, 0, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
273                 g_string_append_printf(info,
274                                 "%*cComment = %s%s", indent, ' ',
275                                 tmp_content ? tmp_content : "NONE",
276                                 line_end);
277         }
278
279         g_string_append_printf(info,
280                         "%*cNumber of stat entries = %u%s", indent, ' ',
281                         if_descr_mand->num_stat_entries,
282                         line_end);
283
284         return g_string_free(info, FALSE);
285 }
286
287 wtap_block_t
288 wtap_file_get_nrb(wtap *wth)
289 {
290         if ((wth == NULL) || (wth->nrb_hdrs == NULL) || (wth->nrb_hdrs->len == 0))
291                 return NULL;
292
293         return g_array_index(wth->nrb_hdrs, wtap_block_t, 0);
294 }
295
296 GArray*
297 wtap_file_get_nrb_for_new_file(wtap *wth)
298 {
299         guint nrb_count;
300         wtap_block_t nrb_hdr_src, nrb_hdr_dest;
301         GArray* nrb_hdrs;
302
303         if ((wth == NULL || wth->nrb_hdrs == NULL) || (wth->nrb_hdrs->len == 0))
304                 return NULL;
305
306         nrb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
307
308         for (nrb_count = 0; nrb_count < wth->nrb_hdrs->len; nrb_count++) {
309                 nrb_hdr_src = g_array_index(wth->nrb_hdrs, wtap_block_t, nrb_count);
310                 nrb_hdr_dest = wtap_block_create(WTAP_BLOCK_NG_NRB);
311                 wtap_block_copy(nrb_hdr_dest, nrb_hdr_src);
312                 g_array_append_val(nrb_hdrs, nrb_hdr_dest);
313         }
314
315         return nrb_hdrs;
316 }
317
318 /* Table of the encapsulation types we know about. */
319 struct encap_type_info {
320         const char *name;
321         const char *short_name;
322 };
323
324 static struct encap_type_info encap_table_base[] = {
325         /* WTAP_ENCAP_UNKNOWN */
326         { "Unknown", "unknown" },
327
328         /* WTAP_ENCAP_ETHERNET */
329         { "Ethernet", "ether" },
330
331         /* WTAP_ENCAP_TOKEN_RING */
332         { "Token Ring", "tr" },
333
334         /* WTAP_ENCAP_SLIP */
335         { "SLIP", "slip" },
336
337         /* WTAP_ENCAP_PPP */
338         { "PPP", "ppp" },
339
340         /* WTAP_ENCAP_FDDI */
341         { "FDDI", "fddi" },
342
343         /* WTAP_ENCAP_FDDI_BITSWAPPED */
344         { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
345
346         /* WTAP_ENCAP_RAW_IP */
347         { "Raw IP", "rawip" },
348
349         /* WTAP_ENCAP_ARCNET */
350         { "ARCNET", "arcnet" },
351
352         /* WTAP_ENCAP_ARCNET_LINUX */
353         { "Linux ARCNET", "arcnet_linux" },
354
355         /* WTAP_ENCAP_ATM_RFC1483 */
356         { "RFC 1483 ATM", "atm-rfc1483" },
357
358         /* WTAP_ENCAP_LINUX_ATM_CLIP */
359         { "Linux ATM CLIP", "linux-atm-clip" },
360
361         /* WTAP_ENCAP_LAPB */
362         { "LAPB", "lapb" },
363
364         /* WTAP_ENCAP_ATM_PDUS */
365         { "ATM PDUs", "atm-pdus" },
366
367         /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
368         { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
369
370         /* WTAP_ENCAP_NULL */
371         { "NULL/Loopback", "null" },
372
373         /* WTAP_ENCAP_ASCEND */
374         { "Lucent/Ascend access equipment", "ascend" },
375
376         /* WTAP_ENCAP_ISDN */
377         { "ISDN", "isdn" },
378
379         /* WTAP_ENCAP_IP_OVER_FC */
380         { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
381
382         /* WTAP_ENCAP_PPP_WITH_PHDR */
383         { "PPP with Directional Info", "ppp-with-direction" },
384
385         /* WTAP_ENCAP_IEEE_802_11 */
386         { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
387
388         /* WTAP_ENCAP_IEEE_802_11_PRISM */
389         { "IEEE 802.11 plus Prism II monitor mode radio header", "ieee-802-11-prism" },
390
391         /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
392         { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
393
394         /* WTAP_ENCAP_IEEE_802_11_RADIOTAP */
395         { "IEEE 802.11 plus radiotap radio header", "ieee-802-11-radiotap" },
396
397         /* WTAP_ENCAP_IEEE_802_11_AVS */
398         { "IEEE 802.11 plus AVS radio header", "ieee-802-11-avs" },
399
400         /* WTAP_ENCAP_SLL */
401         { "Linux cooked-mode capture", "linux-sll" },
402
403         /* WTAP_ENCAP_FRELAY */
404         { "Frame Relay", "frelay" },
405
406         /* WTAP_ENCAP_FRELAY_WITH_PHDR */
407         { "Frame Relay with Directional Info", "frelay-with-direction" },
408
409         /* WTAP_ENCAP_CHDLC */
410         { "Cisco HDLC", "chdlc" },
411
412         /* WTAP_ENCAP_CISCO_IOS */
413         { "Cisco IOS internal", "ios" },
414
415         /* WTAP_ENCAP_LOCALTALK */
416         { "Localtalk", "ltalk" },
417
418         /* WTAP_ENCAP_OLD_PFLOG  */
419         { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
420
421         /* WTAP_ENCAP_HHDLC */
422         { "HiPath HDLC", "hhdlc" },
423
424         /* WTAP_ENCAP_DOCSIS */
425         { "Data Over Cable Service Interface Specification", "docsis" },
426
427         /* WTAP_ENCAP_COSINE */
428         { "CoSine L2 debug log", "cosine" },
429
430         /* WTAP_ENCAP_WFLEET_HDLC */
431         { "Wellfleet HDLC", "whdlc" },
432
433         /* WTAP_ENCAP_SDLC */
434         { "SDLC", "sdlc" },
435
436         /* WTAP_ENCAP_TZSP */
437         { "Tazmen sniffer protocol", "tzsp" },
438
439         /* WTAP_ENCAP_ENC */
440         { "OpenBSD enc(4) encapsulating interface", "enc" },
441
442         /* WTAP_ENCAP_PFLOG  */
443         { "OpenBSD PF Firewall logs", "pflog" },
444
445         /* WTAP_ENCAP_CHDLC_WITH_PHDR */
446         { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
447
448         /* WTAP_ENCAP_BLUETOOTH_H4 */
449         { "Bluetooth H4", "bluetooth-h4" },
450
451         /* WTAP_ENCAP_MTP2 */
452         { "SS7 MTP2", "mtp2" },
453
454         /* WTAP_ENCAP_MTP3 */
455         { "SS7 MTP3", "mtp3" },
456
457         /* WTAP_ENCAP_IRDA */
458         { "IrDA", "irda" },
459
460         /* WTAP_ENCAP_USER0 */
461         { "USER 0", "user0" },
462
463         /* WTAP_ENCAP_USER1 */
464         { "USER 1", "user1" },
465
466         /* WTAP_ENCAP_USER2 */
467         { "USER 2", "user2" },
468
469         /* WTAP_ENCAP_USER3 */
470         { "USER 3", "user3" },
471
472         /* WTAP_ENCAP_USER4 */
473         { "USER 4", "user4" },
474
475         /* WTAP_ENCAP_USER5 */
476         { "USER 5", "user5" },
477
478         /* WTAP_ENCAP_USER6 */
479         { "USER 6", "user6" },
480
481         /* WTAP_ENCAP_USER7 */
482         { "USER 7", "user7" },
483
484         /* WTAP_ENCAP_USER8 */
485         { "USER 8", "user8" },
486
487         /* WTAP_ENCAP_USER9 */
488         { "USER 9", "user9" },
489
490         /* WTAP_ENCAP_USER10 */
491         { "USER 10", "user10" },
492
493         /* WTAP_ENCAP_USER11 */
494         { "USER 11", "user11" },
495
496         /* WTAP_ENCAP_USER12 */
497         { "USER 12", "user12" },
498
499         /* WTAP_ENCAP_USER13 */
500         { "USER 13", "user13" },
501
502         /* WTAP_ENCAP_USER14 */
503         { "USER 14", "user14" },
504
505         /* WTAP_ENCAP_USER15 */
506         { "USER 15", "user15" },
507
508         /* WTAP_ENCAP_SYMANTEC */
509         { "Symantec Enterprise Firewall", "symantec" },
510
511         /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
512         { "Apple IP-over-IEEE 1394", "ap1394" },
513
514         /* WTAP_ENCAP_BACNET_MS_TP */
515         { "BACnet MS/TP", "bacnet-ms-tp" },
516
517         /* WTAP_ENCAP_NETTL_RAW_ICMP */
518         { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
519
520         /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
521         { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
522
523         /* WTAP_ENCAP_GPRS_LLC */
524         { "GPRS LLC", "gprs-llc" },
525
526         /* WTAP_ENCAP_JUNIPER_ATM1 */
527         { "Juniper ATM1", "juniper-atm1" },
528
529         /* WTAP_ENCAP_JUNIPER_ATM2 */
530         { "Juniper ATM2", "juniper-atm2" },
531
532         /* WTAP_ENCAP_REDBACK */
533         { "Redback SmartEdge", "redback" },
534
535         /* WTAP_ENCAP_NETTL_RAW_IP */
536         { "Raw IP with nettl headers", "rawip-nettl" },
537
538         /* WTAP_ENCAP_NETTL_ETHERNET */
539         { "Ethernet with nettl headers", "ether-nettl" },
540
541         /* WTAP_ENCAP_NETTL_TOKEN_RING */
542         { "Token Ring with nettl headers", "tr-nettl" },
543
544         /* WTAP_ENCAP_NETTL_FDDI */
545         { "FDDI with nettl headers", "fddi-nettl" },
546
547         /* WTAP_ENCAP_NETTL_UNKNOWN */
548         { "Unknown link-layer type with nettl headers", "unknown-nettl" },
549
550         /* WTAP_ENCAP_MTP2_WITH_PHDR */
551         { "MTP2 with pseudoheader", "mtp2-with-phdr" },
552
553         /* WTAP_ENCAP_JUNIPER_PPPOE */
554         { "Juniper PPPoE", "juniper-pppoe" },
555
556         /* WTAP_ENCAP_GCOM_TIE1 */
557         { "GCOM TIE1", "gcom-tie1" },
558
559         /* WTAP_ENCAP_GCOM_SERIAL */
560         { "GCOM Serial", "gcom-serial" },
561
562         /* WTAP_ENCAP_NETTL_X25 */
563         { "X.25 with nettl headers", "x25-nettl" },
564
565         /* WTAP_ENCAP_K12 */
566         { "K12 protocol analyzer", "k12" },
567
568         /* WTAP_ENCAP_JUNIPER_MLPPP */
569         { "Juniper MLPPP", "juniper-mlppp" },
570
571         /* WTAP_ENCAP_JUNIPER_MLFR */
572         { "Juniper MLFR", "juniper-mlfr" },
573
574         /* WTAP_ENCAP_JUNIPER_ETHER */
575         { "Juniper Ethernet", "juniper-ether" },
576
577         /* WTAP_ENCAP_JUNIPER_PPP */
578         { "Juniper PPP", "juniper-ppp" },
579
580         /* WTAP_ENCAP_JUNIPER_FRELAY */
581         { "Juniper Frame-Relay", "juniper-frelay" },
582
583         /* WTAP_ENCAP_JUNIPER_CHDLC */
584         { "Juniper C-HDLC", "juniper-chdlc" },
585
586         /* WTAP_ENCAP_JUNIPER_GGSN */
587         { "Juniper GGSN", "juniper-ggsn" },
588
589         /* WTAP_ENCAP_LINUX_LAPD */
590         { "LAPD with Linux pseudo-header", "linux-lapd" },
591
592         /* WTAP_ENCAP_CATAPULT_DCT2000 */
593         { "Catapult DCT2000", "dct2000" },
594
595         /* WTAP_ENCAP_BER */
596         { "ASN.1 Basic Encoding Rules", "ber" },
597
598         /* WTAP_ENCAP_JUNIPER_VP */
599         { "Juniper Voice PIC", "juniper-vp" },
600
601         /* WTAP_ENCAP_USB_FREEBSD */
602         { "USB packets with FreeBSD header", "usb-freebsd" },
603
604         /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
605         { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
606
607         /* WTAP_ENCAP_NETTL_RAW_TELNET */
608         { "Raw telnet with nettl headers", "raw-telnet-nettl" },
609
610         /* WTAP_ENCAP_USB_LINUX */
611         { "USB packets with Linux header", "usb-linux" },
612
613         /* WTAP_ENCAP_MPEG */
614         { "MPEG", "mpeg" },
615
616         /* WTAP_ENCAP_PPI */
617         { "Per-Packet Information header", "ppi" },
618
619         /* WTAP_ENCAP_ERF */
620         { "Extensible Record Format", "erf" },
621
622         /* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
623         { "Bluetooth H4 with linux header", "bluetooth-h4-linux" },
624
625         /* WTAP_ENCAP_SITA */
626         { "SITA WAN packets", "sita-wan" },
627
628         /* WTAP_ENCAP_SCCP */
629         { "SS7 SCCP", "sccp" },
630
631         /* WTAP_ENCAP_BLUETOOTH_HCI */
632         { "Bluetooth without transport layer", "bluetooth-hci" },
633
634         /* WTAP_ENCAP_IPMB */
635         { "Intelligent Platform Management Bus", "ipmb" },
636
637         /* WTAP_ENCAP_IEEE802_15_4 */
638         { "IEEE 802.15.4 Wireless PAN", "wpan" },
639
640         /* WTAP_ENCAP_X2E_XORAYA */
641         { "X2E Xoraya", "x2e-xoraya" },
642
643         /* WTAP_ENCAP_FLEXRAY */
644         { "FlexRay", "flexray" },
645
646         /* WTAP_ENCAP_LIN */
647         { "Local Interconnect Network", "lin" },
648
649         /* WTAP_ENCAP_MOST */
650         { "Media Oriented Systems Transport", "most" },
651
652         /* WTAP_ENCAP_CAN20B */
653         { "Controller Area Network 2.0B", "can20b" },
654
655         /* WTAP_ENCAP_LAYER1_EVENT */
656         { "EyeSDN Layer 1 event", "layer1-event" },
657
658         /* WTAP_ENCAP_X2E_SERIAL */
659         { "X2E serial line capture", "x2e-serial" },
660
661         /* WTAP_ENCAP_I2C */
662         { "I2C", "i2c" },
663
664         /* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
665         { "IEEE 802.15.4 Wireless PAN non-ASK PHY", "wpan-nonask-phy" },
666
667         /* WTAP_ENCAP_TNEF */
668         { "Transport-Neutral Encapsulation Format", "tnef" },
669
670         /* WTAP_ENCAP_USB_LINUX_MMAPPED */
671         { "USB packets with Linux header and padding", "usb-linux-mmap" },
672
673         /* WTAP_ENCAP_GSM_UM */
674         { "GSM Um Interface", "gsm_um" },
675
676         /* WTAP_ENCAP_DPNSS */
677         { "Digital Private Signalling System No 1 Link Layer", "dpnss_link" },
678
679         /* WTAP_ENCAP_PACKETLOGGER */
680         { "PacketLogger", "packetlogger" },
681
682         /* WTAP_ENCAP_NSTRACE_1_0 */
683         { "NetScaler Encapsulation 1.0 of Ethernet", "nstrace10" },
684
685         /* WTAP_ENCAP_NSTRACE_2_0 */
686         { "NetScaler Encapsulation 2.0 of Ethernet", "nstrace20" },
687
688         /* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
689         { "Fibre Channel FC-2", "fc2" },
690
691         /* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
692         { "Fibre Channel FC-2 With Frame Delimiter", "fc2sof"},
693
694         /* WTAP_ENCAP_JPEG_JFIF */
695         { "JPEG/JFIF", "jfif" },
696
697         /* WTAP_ENCAP_IPNET */
698         { "Solaris IPNET", "ipnet" },
699
700         /* WTAP_ENCAP_SOCKETCAN */
701         { "SocketCAN", "socketcan" },
702
703         /* WTAP_ENCAP_IEEE_802_11_NETMON */
704         { "IEEE 802.11 plus Network Monitor radio header", "ieee-802-11-netmon" },
705
706         /* WTAP_ENCAP_IEEE802_15_4_NOFCS */
707         { "IEEE 802.15.4 Wireless PAN with FCS not present", "wpan-nofcs" },
708
709         /* WTAP_ENCAP_RAW_IPFIX */
710         { "IPFIX", "ipfix" },
711
712         /* WTAP_ENCAP_RAW_IP4 */
713         { "Raw IPv4", "rawip4" },
714
715         /* WTAP_ENCAP_RAW_IP6 */
716         { "Raw IPv6", "rawip6" },
717
718         /* WTAP_ENCAP_LAPD */
719         { "LAPD", "lapd" },
720
721         /* WTAP_ENCAP_DVBCI */
722         { "DVB-CI (Common Interface)", "dvbci"},
723
724         /* WTAP_ENCAP_MUX27010 */
725         { "MUX27010", "mux27010"},
726
727         /* WTAP_ENCAP_MIME */
728         { "MIME", "mime" },
729
730         /* WTAP_ENCAP_NETANALYZER */
731         { "netANALYZER", "netanalyzer" },
732
733         /* WTAP_ENCAP_NETANALYZER_TRANSPARENT */
734         { "netANALYZER-Transparent", "netanalyzer-transparent" },
735
736         /* WTAP_ENCAP_IP_OVER_IB */
737         { "IP over Infiniband", "ip-over-ib" },
738
739         /* WTAP_ENCAP_MPEG_2_TS */
740         { "ISO/IEC 13818-1 MPEG2-TS", "mp2ts" },
741
742         /* WTAP_ENCAP_PPP_ETHER */
743         { "PPP-over-Ethernet session", "pppoes" },
744
745         /* WTAP_ENCAP_NFC_LLCP */
746         { "NFC LLCP", "nfc-llcp" },
747
748         /* WTAP_ENCAP_NFLOG */
749         { "NFLOG", "nflog" },
750
751         /* WTAP_ENCAP_V5_EF */
752         { "V5 Envelope Function", "v5-ef" },
753
754         /* WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR */
755         { "BACnet MS/TP with Directional Info", "bacnet-ms-tp-with-direction" },
756
757         /* WTAP_ENCAP_IXVERIWAVE */
758         { "IxVeriWave header and stats block", "ixveriwave" },
759
760         /* WTAP_ENCAP_SDH */
761         { "SDH", "sdh" },
762
763         /* WTAP_ENCAP_DBUS */
764         { "D-Bus", "dbus" },
765
766         /* WTAP_ENCAP_AX25_KISS */
767         { "AX.25 with KISS header", "ax25-kiss" },
768
769         /* WTAP_ENCAP_AX25 */
770         { "Amateur Radio AX.25", "ax25" },
771
772         /* WTAP_ENCAP_SCTP */
773         { "SCTP", "sctp" },
774
775         /* WTAP_ENCAP_INFINIBAND */
776         { "InfiniBand", "infiniband" },
777
778         /* WTAP_ENCAP_JUNIPER_SVCS */
779         { "Juniper Services", "juniper-svcs" },
780
781         /* WTAP_ENCAP_USBPCAP */
782         { "USB packets with USBPcap header", "usb-usbpcap" },
783
784         /* WTAP_ENCAP_RTAC_SERIAL */
785         { "RTAC serial-line", "rtac-serial" },
786
787         /* WTAP_ENCAP_BLUETOOTH_LE_LL */
788         { "Bluetooth Low Energy Link Layer", "bluetooth-le-ll" },
789
790         /* WTAP_ENCAP_WIRESHARK_UPPER_PDU */
791         { "Wireshark Upper PDU export", "wireshark-upper-pdu" },
792
793         /* WTAP_ENCAP_STANAG_4607 */
794         { "STANAG 4607", "s4607" },
795
796         /* WTAP_ENCAP_STANAG_5066_D_PDU */
797         { "STANAG 5066 Data Transfer Sublayer PDUs(D_PDU)", "s5066-dpdu"},
798
799         /* WTAP_ENCAP_NETLINK */
800         { "Linux Netlink", "netlink" },
801
802         /* WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
803         { "Bluetooth Linux Monitor", "bluetooth-linux-monitor" },
804
805         /* WTAP_ENCAP_BLUETOOTH_BREDR_BB */
806         { "Bluetooth BR/EDR Baseband RF", "bluetooth-bredr-bb-rf" },
807
808         /* WTAP_ENCAP_BLUETOOTH_LE_LL_WITH_PHDR */
809         { "Bluetooth Low Energy Link Layer RF", "bluetooth-le-ll-rf" },
810
811         /* WTAP_ENCAP_NSTRACE_3_0 */
812         { "NetScaler Encapsulation 3.0 of Ethernet", "nstrace30" },
813
814         /* WTAP_ENCAP_LOGCAT */
815         { "Android Logcat Binary format", "logcat" },
816
817         /* WTAP_ENCAP_LOGCAT_BRIEF */
818         { "Android Logcat Brief text format", "logcat_brief" },
819
820         /* WTAP_ENCAP_LOGCAT_PROCESS */
821         { "Android Logcat Process text format", "logcat_process" },
822
823         /* WTAP_ENCAP_LOGCAT_TAG */
824         { "Android Logcat Tag text format", "logcat_tag" },
825
826         /* WTAP_ENCAP_LOGCAT_THREAD */
827         { "Android Logcat Thread text format", "logcat_thread" },
828
829         /* WTAP_ENCAP_LOGCAT_TIME */
830         { "Android Logcat Time text format", "logcat_time" },
831
832         /* WTAP_ENCAP_LOGCAT_THREADTIME */
833         { "Android Logcat Threadtime text format", "logcat_threadtime" },
834
835         /* WTAP_ENCAP_LOGCAT_LONG */
836         { "Android Logcat Long text format", "logcat_long" },
837
838         /* WTAP_ENCAP_PKTAP */
839         { "Apple PKTAP", "pktap" },
840
841         /* WTAP_ENCAP_EPON */
842         { "Ethernet Passive Optical Network", "epon" },
843
844         /* WTAP_ENCAP_IPMI_TRACE */
845         { "IPMI Trace Data Collection", "ipmi-trace" },
846
847         /* WTAP_ENCAP_LOOP */
848         { "OpenBSD loopback", "loop" },
849
850         /* WTAP_ENCAP_JSON */
851         { "JavaScript Object Notation", "json" },
852
853         /* WTAP_ENCAP_NSTRACE_3_5 */
854         { "NetScaler Encapsulation 3.5 of Ethernet", "nstrace35" },
855
856         /* WTAP_ENCAP_ISO14443 */
857         { "ISO 14443 contactless smartcard standards", "iso14443" },
858
859         /* WTAP_ENCAP_GFP_T */
860         { "ITU-T G.7041/Y.1303 Generic Framing Procedure Transparent mode", "gfp-t" },
861
862         /* WTAP_ENCAP_GFP_F */
863         { "ITU-T G.7041/Y.1303 Generic Framing Procedure Frame-mapped mode", "gfp-f" },
864
865         /* WTAP_ENCAP_IP_OVER_IB_PCAP */
866         { "IP over IB", "ip-ib" },
867
868         /* WTAP_ENCAP_JUNIPER_VN */
869         { "Juniper VN", "juniper-vn" },
870
871         /* WTAP_ENCAP_USB_DARWIN */
872         { "USB packets with Darwin (macOS, etc.) headers", "usb-darwin" },
873
874         /* WTAP_ENCAP_LORATAP */
875         { "LoRaTap", "loratap"},
876
877         /* WTAP_ENCAP_3MB_ETHERNET */
878         { "Xerox 3MB Ethernet", "xeth"},
879
880         /* WTAP_ENCAP_VSOCK */
881         { "Linux vsock", "vsock" },
882
883         /* WTAP_ENCAP_NORDIC_BLE */
884         { "Nordic BLE Sniffer", "nordic_ble" },
885
886         /* WTAP_ENCAP_NETMON_NET_NETEVENT */
887         { "Network Monitor Network Event", "netmon_event" },
888
889         /* WTAP_ENCAP_NETMON_HEADER */
890         { "Network Monitor Header", "netmon_header" },
891
892         /* WTAP_ENCAP_NETMON_NET_FILTER */
893         { "Network Monitor Filter", "netmon_filter" },
894
895         /* WTAP_ENCAP_NETMON_NETWORK_INFO_EX */
896         { "Network Monitor Network Info", "netmon_network_info" },
897
898         /* WTAP_ENCAP_MA_WFP_CAPTURE_V4 */
899         { "Message Analyzer WFP Capture v4", "message_analyzer_wfp_capture_v4" },
900
901         /* WTAP_ENCAP_MA_WFP_CAPTURE_V6 */
902         { "Message Analyzer WFP Capture v6", "message_analyzer_wfp_capture_v6" },
903
904         /* WTAP_ENCAP_MA_WFP_CAPTURE_2V4 */
905         { "Message Analyzer WFP Capture2 v4", "message_analyzer_wfp_capture2_v4" },
906
907         /* WTAP_ENCAP_MA_WFP_CAPTURE_2V6 */
908         { "Message Analyzer WFP Capture2 v6", "message_analyzer_wfp_capture2_v6" },
909
910         /* WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V4 */
911         { "Message Analyzer WFP Capture Auth v4", "message_analyzer_wfp_capture_auth_v4" },
912
913         /* WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V6 */
914         { "Message Analyzer WFP Capture Auth v6", "message_analyzer_wfp_capture_auth_v6" },
915
916         /* WTAP_ENCAP_JUNIPER_ST */
917         { "Juniper Secure Tunnel Information", "juniper-st" },
918
919         /* WTAP_ENCAP_ETHERNET_MPACKET */
920         { "IEEE 802.3br mPackets", "ether-mpacket" },
921
922         /* WTAP_ENCAP_DOCSIS31_XRA31 */
923         { "DOCSIS with Excentis XRA pseudo-header", "docsis31_xra31" },
924
925         /* WTAP_ENCAP_DPAUXMON */
926         { "DisplayPort AUX channel with Unigraf pseudo-header", "dpauxmon" },
927
928         /* WTAP_ENCAP_RUBY_MARSHAL */
929         { "Ruby marshal object", "ruby_marshal" },
930
931         /* WTAP_ENCAP_RFC7468 */
932         { "RFC 7468 file", "rfc7468" },
933
934         /* WTAP_ENCAP_SYSTEMD_JOURNAL */
935         { "systemd journal", "sdjournal" }
936 };
937
938 WS_DLL_LOCAL
939 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
940 static GArray* encap_table_arr = NULL;
941
942 #define encap_table_entry(encap)        \
943         g_array_index(encap_table_arr, struct encap_type_info, encap)
944
945 static void wtap_init_encap_types(void) {
946
947         if (encap_table_arr) return;
948
949         encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
950
951         g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
952 }
953
954 static void wtap_cleanup_encap_types(void) {
955         if (encap_table_arr) {
956                 g_array_free(encap_table_arr, TRUE);
957                 encap_table_arr = NULL;
958         }
959 }
960
961 int wtap_get_num_encap_types(void) {
962         return wtap_num_encap_types;
963 }
964
965
966 int wtap_register_encap_type(const char* name, const char* short_name) {
967         struct encap_type_info e;
968
969         e.name = g_strdup(name);
970         e.short_name = g_strdup(short_name);
971
972         g_array_append_val(encap_table_arr,e);
973
974         return wtap_num_encap_types++;
975 }
976
977
978 /* Name that should be somewhat descriptive. */
979 const char *
980 wtap_encap_string(int encap)
981 {
982         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
983                 return "Illegal";
984         else if (encap == WTAP_ENCAP_PER_PACKET)
985                 return "Per packet";
986         else
987                 return encap_table_entry(encap).name;
988 }
989
990 /* Name to use in, say, a command-line flag specifying the type. */
991 const char *
992 wtap_encap_short_string(int encap)
993 {
994         if (encap < WTAP_ENCAP_PER_PACKET || encap >= WTAP_NUM_ENCAP_TYPES)
995                 return "illegal";
996         else if (encap == WTAP_ENCAP_PER_PACKET)
997                 return "per-packet";
998         else
999                 return encap_table_entry(encap).short_name;
1000 }
1001
1002 /* Translate a short name to a capture file type. */
1003 int
1004 wtap_short_string_to_encap(const char *short_name)
1005 {
1006         int encap;
1007
1008         for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
1009                 if (encap_table_entry(encap).short_name != NULL &&
1010                     strcmp(short_name, encap_table_entry(encap).short_name) == 0)
1011                         return encap;
1012         }
1013         return -1;      /* no such encapsulation type */
1014 }
1015
1016 const char*
1017 wtap_tsprec_string(int tsprec)
1018 {
1019         const char* s;
1020         switch (tsprec) {
1021                 case WTAP_TSPREC_PER_PACKET:
1022                         s = "per-packet";
1023                         break;
1024                 case WTAP_TSPREC_SEC:
1025                         s = "seconds";
1026                         break;
1027                 case WTAP_TSPREC_DSEC:
1028                         s = "deciseconds";
1029                         break;
1030                 case WTAP_TSPREC_CSEC:
1031                         s = "centiseconds";
1032                         break;
1033                 case WTAP_TSPREC_MSEC:
1034                         s = "milliseconds";
1035                         break;
1036                 case WTAP_TSPREC_USEC:
1037                         s = "microseconds";
1038                         break;
1039                 case WTAP_TSPREC_NSEC:
1040                         s = "nanoseconds";
1041                         break;
1042                 case WTAP_TSPREC_UNKNOWN:
1043                 default:
1044                         s = "UNKNOWN";
1045                         break;
1046         }
1047         return s;
1048 }
1049
1050 static const char *wtap_errlist[] = {
1051         /* WTAP_ERR_NOT_REGULAR_FILE */
1052         "The file isn't a plain file or pipe",
1053
1054         /* WTAP_ERR_RANDOM_OPEN_PIPE */
1055         "The file is being opened for random access but is a pipe",
1056
1057         /* WTAP_ERR_FILE_UNKNOWN_FORMAT */
1058         "The file isn't a capture file in a known format",
1059
1060         /* WTAP_ERR_UNSUPPORTED */
1061         "File contains record data we don't support",
1062
1063         /* WTAP_ERR_CANT_WRITE_TO_PIPE */
1064         "That file format cannot be written to a pipe",
1065
1066         /* WTAP_ERR_CANT_OPEN */
1067         NULL,
1068
1069         /* WTAP_ERR_UNWRITABLE_FILE_TYPE */
1070         "Files can't be saved in that format",
1071
1072         /* WTAP_ERR_UNWRITABLE_ENCAP */
1073         "Packets with that network type can't be saved in that format",
1074
1075         /* WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED */
1076         "That file format doesn't support per-packet encapsulations",
1077
1078         /* WTAP_ERR_CANT_WRITE */
1079         "A write failed for some unknown reason",
1080
1081         /* WTAP_ERR_CANT_CLOSE */
1082         NULL,
1083
1084         /* WTAP_ERR_SHORT_READ */
1085         "Less data was read than was expected",
1086
1087         /* WTAP_ERR_BAD_FILE */
1088         "The file appears to be damaged or corrupt",
1089
1090         /* WTAP_ERR_SHORT_WRITE */
1091         "Less data was written than was requested",
1092
1093         /* WTAP_ERR_UNC_OVERFLOW */
1094         "Uncompression error: data would overflow buffer",
1095
1096         /* WTAP_ERR_RANDOM_OPEN_STDIN */
1097         "The standard input cannot be opened for random access",
1098
1099         /* WTAP_ERR_COMPRESSION_NOT_SUPPORTED */
1100         "That file format doesn't support compression",
1101
1102         /* WTAP_ERR_CANT_SEEK */
1103         NULL,
1104
1105         /* WTAP_ERR_CANT_SEEK_COMPRESSED */
1106         NULL,
1107
1108         /* WTAP_ERR_DECOMPRESS */
1109         "Uncompression error",
1110
1111         /* WTAP_ERR_INTERNAL */
1112         "Internal error",
1113
1114         /* WTAP_ERR_PACKET_TOO_LARGE */
1115         "The packet being written is too large for that format",
1116
1117         /* WTAP_ERR_CHECK_WSLUA */
1118         NULL,
1119
1120         /* WTAP_ERR_UNWRITABLE_REC_TYPE */
1121         "That record type cannot be written in that format",
1122
1123         /* WTAP_ERR_UNWRITABLE_REC_DATA */
1124         "That record can't be written in that format",
1125
1126         /* WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED */
1127         "We don't support decompressing that type of compressed file",
1128 };
1129 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
1130
1131 const char *
1132 wtap_strerror(int err)
1133 {
1134         static char errbuf[128];
1135         unsigned int wtap_errlist_index;
1136
1137         if (err < 0) {
1138                 wtap_errlist_index = -1 - err;
1139                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
1140                         g_snprintf(errbuf, 128, "Error %d", err);
1141                         return errbuf;
1142                 }
1143                 if (wtap_errlist[wtap_errlist_index] == NULL)
1144                         return "Unknown reason";
1145                 return wtap_errlist[wtap_errlist_index];
1146         } else
1147                 return g_strerror(err);
1148 }
1149
1150 /* Close only the sequential side, freeing up memory it uses.
1151
1152    Note that we do *not* want to call the subtype's close function,
1153    as it would free any per-subtype data, and that data may be
1154    needed by the random-access side.
1155
1156    Instead, if the subtype has a "sequential close" function, we call it,
1157    to free up stuff used only by the sequential side. */
1158 void
1159 wtap_sequential_close(wtap *wth)
1160 {
1161         if (wth->subtype_sequential_close != NULL)
1162                 (*wth->subtype_sequential_close)(wth);
1163
1164         if (wth->fh != NULL) {
1165                 file_close(wth->fh);
1166                 wth->fh = NULL;
1167         }
1168
1169         if (wth->rec_data) {
1170                 ws_buffer_free(wth->rec_data);
1171                 g_free(wth->rec_data);
1172                 wth->rec_data = NULL;
1173         }
1174 }
1175
1176 static void
1177 g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
1178 {
1179         g_free(data);
1180 }
1181
1182 /*
1183  * Close the file descriptors for the sequential and random streams, but
1184  * don't discard any information about those streams.  Used on Windows if
1185  * we need to rename a file that we have open or if we need to rename on
1186  * top of a file we have open.
1187  */
1188 void
1189 wtap_fdclose(wtap *wth)
1190 {
1191         if (wth->fh != NULL)
1192                 file_fdclose(wth->fh);
1193         if (wth->random_fh != NULL)
1194                 file_fdclose(wth->random_fh);
1195 }
1196
1197 void
1198 wtap_close(wtap *wth)
1199 {
1200         wtap_sequential_close(wth);
1201
1202         if (wth->subtype_close != NULL)
1203                 (*wth->subtype_close)(wth);
1204
1205         if (wth->random_fh != NULL)
1206                 file_close(wth->random_fh);
1207
1208         g_free(wth->priv);
1209
1210         if (wth->fast_seek != NULL) {
1211                 g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
1212                 g_ptr_array_free(wth->fast_seek, TRUE);
1213         }
1214
1215         wtap_block_array_free(wth->shb_hdrs);
1216         wtap_block_array_free(wth->nrb_hdrs);
1217         wtap_block_array_free(wth->interface_data);
1218
1219         g_free(wth);
1220 }
1221
1222 void
1223 wtap_cleareof(wtap *wth) {
1224         /* Reset EOF */
1225         file_clearerr(wth->fh);
1226 }
1227
1228 void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
1229         if (wth)
1230                 wth->add_new_ipv4 = add_new_ipv4;
1231 }
1232
1233 void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
1234         if (wth)
1235                 wth->add_new_ipv6 = add_new_ipv6;
1236 }
1237
1238 gboolean
1239 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
1240 {
1241         /*
1242          * Set the packet encapsulation to the file's encapsulation
1243          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
1244          * right answer (and means that the read routine for this
1245          * capture file type doesn't have to set it), and if it
1246          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
1247          * anyway.
1248          *
1249          * Do the same for the packet time stamp resolution.
1250          */
1251         wth->rec.rec_header.packet_header.pkt_encap = wth->file_encap;
1252         wth->rec.tsprec = wth->file_tsprec;
1253
1254         *err = 0;
1255         *err_info = NULL;
1256         if (!wth->subtype_read(wth, err, err_info, data_offset)) {
1257                 /*
1258                  * If we didn't get an error indication, we read
1259                  * the last packet.  See if there's any deferred
1260                  * error, as might, for example, occur if we're
1261                  * reading a compressed file, and we got an error
1262                  * reading compressed data from the file, but
1263                  * got enough compressed data to decompress the
1264                  * last packet of the file.
1265                  */
1266                 if (*err == 0)
1267                         *err = file_error(wth->fh, err_info);
1268                 return FALSE;   /* failure */
1269         }
1270
1271         /*
1272          * Is this a packet record?
1273          */
1274         if (wth->rec.rec_type == REC_TYPE_PACKET) {
1275                 /*
1276                  * It makes no sense for the captured data length
1277                  * to be bigger than the actual data length.
1278                  */
1279                 if (wth->rec.rec_header.packet_header.caplen > wth->rec.rec_header.packet_header.len)
1280                         wth->rec.rec_header.packet_header.caplen = wth->rec.rec_header.packet_header.len;
1281
1282                 /*
1283                  * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
1284                  * probably means the file has that encapsulation type
1285                  * but the read routine didn't set this packet's
1286                  * encapsulation type.
1287                  */
1288                 g_assert(wth->rec.rec_header.packet_header.pkt_encap != WTAP_ENCAP_PER_PACKET);
1289         }
1290
1291         return TRUE;    /* success */
1292 }
1293
1294 /*
1295  * Read a given number of bytes from a file into a buffer or, if
1296  * buf is NULL, just discard them.
1297  *
1298  * If we succeed, return TRUE.
1299  *
1300  * If we get an EOF, return FALSE with *err set to 0, reporting this
1301  * as an EOF.
1302  *
1303  * If we get fewer bytes than the specified number, return FALSE with
1304  * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
1305  * error.
1306  *
1307  * If we get a read error, return FALSE with *err and *err_info set
1308  * appropriately.
1309  */
1310 gboolean
1311 wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
1312     gchar **err_info)
1313 {
1314         int     bytes_read;
1315
1316         bytes_read = file_read(buf, count, fh);
1317         if (bytes_read < 0 || (guint)bytes_read != count) {
1318                 *err = file_error(fh, err_info);
1319                 if (*err == 0 && bytes_read > 0)
1320                         *err = WTAP_ERR_SHORT_READ;
1321                 return FALSE;
1322         }
1323         return TRUE;
1324 }
1325
1326 /*
1327  * Read a given number of bytes from a file into a buffer or, if
1328  * buf is NULL, just discard them.
1329  *
1330  * If we succeed, return TRUE.
1331  *
1332  * If we get fewer bytes than the specified number, including getting
1333  * an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
1334  * this as a short read error.
1335  *
1336  * If we get a read error, return FALSE with *err and *err_info set
1337  * appropriately.
1338  */
1339 gboolean
1340 wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
1341     gchar **err_info)
1342 {
1343         int     bytes_read;
1344
1345         bytes_read = file_read(buf, count, fh);
1346         if (bytes_read < 0 || (guint)bytes_read != count) {
1347                 *err = file_error(fh, err_info);
1348                 if (*err == 0)
1349                         *err = WTAP_ERR_SHORT_READ;
1350                 return FALSE;
1351         }
1352         return TRUE;
1353 }
1354
1355 /*
1356  * Read packet data into a Buffer, growing the buffer as necessary.
1357  *
1358  * This returns an error on a short read, even if the short read hit
1359  * the EOF immediately.  (The assumption is that each packet has a
1360  * header followed by raw packet data, and that we've already read the
1361  * header, so if we get an EOF trying to read the packet data, the file
1362  * has been cut short, even if the read didn't read any data at all.)
1363  */
1364 gboolean
1365 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
1366     gchar **err_info)
1367 {
1368         ws_buffer_assure_space(buf, length);
1369         return wtap_read_bytes(fh, ws_buffer_start_ptr(buf), length, err,
1370             err_info);
1371 }
1372
1373 /*
1374  * Return an approximation of the amount of data we've read sequentially
1375  * from the file so far.  (gint64, in case that's 64 bits.)
1376  */
1377 gint64
1378 wtap_read_so_far(wtap *wth)
1379 {
1380         return file_tell_raw(wth->fh);
1381 }
1382
1383 wtap_rec *
1384 wtap_get_rec(wtap *wth)
1385 {
1386         return &wth->rec;
1387 }
1388
1389 guint8 *
1390 wtap_get_buf_ptr(wtap *wth)
1391 {
1392         return ws_buffer_start_ptr(wth->rec_data);
1393 }
1394
1395 void
1396 wtap_rec_init(wtap_rec *rec)
1397 {
1398         memset(rec, 0, sizeof *rec);
1399         ws_buffer_init(&rec->options_buf, 0);
1400 }
1401
1402 void
1403 wtap_rec_cleanup(wtap_rec *rec)
1404 {
1405         ws_buffer_free(&rec->options_buf);
1406 }
1407
1408 gboolean
1409 wtap_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, Buffer *buf,
1410     int *err, gchar **err_info)
1411 {
1412         /*
1413          * Set the packet encapsulation to the file's encapsulation
1414          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
1415          * right answer (and means that the read routine for this
1416          * capture file type doesn't have to set it), and if it
1417          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
1418          * anyway.
1419          *
1420          * Do the same for the packet time stamp resolution.
1421          */
1422         rec->rec_header.packet_header.pkt_encap = wth->file_encap;
1423         rec->tsprec = wth->file_tsprec;
1424
1425         *err = 0;
1426         *err_info = NULL;
1427         if (!wth->subtype_seek_read(wth, seek_off, rec, buf, err, err_info))
1428                 return FALSE;
1429
1430         /*
1431          * Is this a packet record?
1432          */
1433         if (rec->rec_type == REC_TYPE_PACKET) {
1434                 /*
1435                  * It makes no sense for the captured data length
1436                  * to be bigger than the actual data length.
1437                  */
1438                 if (rec->rec_header.packet_header.caplen > rec->rec_header.packet_header.len)
1439                         rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len;
1440
1441                 /*
1442                  * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
1443                  * probably means the file has that encapsulation type
1444                  * but the read routine didn't set this packet's
1445                  * encapsulation type.
1446                  */
1447                 g_assert(rec->rec_header.packet_header.pkt_encap != WTAP_ENCAP_PER_PACKET);
1448         }
1449
1450         return TRUE;
1451 }
1452
1453 /*
1454  * Initialize the library.
1455  */
1456 void
1457 wtap_init(gboolean load_wiretap_plugins)
1458 {
1459         init_open_routines();
1460         wtap_opttypes_initialize();
1461         wtap_init_encap_types();
1462         if (load_wiretap_plugins) {
1463 #ifdef HAVE_PLUGINS
1464                 libwiretap_plugins = plugins_init(WS_PLUGIN_WIRETAP);
1465                 g_slist_foreach(wtap_plugins, call_plugin_register_wtap_module, NULL);
1466 #endif
1467         }
1468 }
1469
1470 /*
1471  * Cleanup the library
1472  */
1473 void
1474 wtap_cleanup(void)
1475 {
1476         wtap_cleanup_encap_types();
1477         wtap_opttypes_cleanup();
1478         ws_buffer_cleanup();
1479         cleanup_open_routines();
1480 #ifdef HAVE_PLUGINS
1481         g_slist_free(wtap_plugins);
1482         wtap_plugins = NULL;
1483         plugins_cleanup(libwiretap_plugins);
1484         libwiretap_plugins = NULL;
1485 #endif
1486 }
1487
1488 /*
1489  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1490  *
1491  * Local variables:
1492  * c-basic-offset: 8
1493  * tab-width: 8
1494  * indent-tabs-mode: t
1495  * End:
1496  *
1497  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1498  * :indentSize=8:tabSize=8:noTabs=false:
1499  */