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