3e876aa7aa3733ba56f65387bfaacaa953a31f06
[obnox/wireshark/wip.git] / wiretap / file_access.c
1 /* file_access.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #include <errno.h>
40
41 #include <wsutil/file_util.h>
42
43 #include "wtap-int.h"
44 #include "file_wrappers.h"
45 #include "buffer.h"
46 #include "lanalyzer.h"
47 #include "airopeek9.h"
48 #include "ngsniffer.h"
49 #include "radcom.h"
50 #include "ascendtext.h"
51 #include "nettl.h"
52 #include "libpcap.h"
53 #include "snoop.h"
54 #include "iptrace.h"
55 #include "iseries.h"
56 #include "netmon.h"
57 #include "netxray.h"
58 #include "toshiba.h"
59 #include "eyesdn.h"
60 #include "i4btrace.h"
61 #include "csids.h"
62 #include "pppdump.h"
63 #include "etherpeek.h"
64 #include "vms.h"
65 #include "dbs-etherwatch.h"
66 #include "visual.h"
67 #include "cosine.h"
68 #include "5views.h"
69 #include "erf.h"
70 #include "hcidump.h"
71 #include "network_instruments.h"
72 #include "k12.h"
73 #include "ber.h"
74 #include "catapult_dct2000.h"
75 #include "mpeg.h"
76 #include "netscreen.h"
77 #include "commview.h"
78 #include "pcapng.h"
79 #include "aethra.h"
80 #include "btsnoop.h"
81 #include "tnef.h"
82 #include "dct3trace.h"
83 #include "packetlogger.h"
84 #include "daintree-sna.h"
85 #include "netscaler.h"
86 #include "mime_file.h"
87 #include "ipfix.h"
88
89
90 /* The open_file_* routines should return:
91  *
92  *      -1 on an I/O error;
93  *
94  *      1 if the file they're reading is one of the types it handles;
95  *
96  *      0 if the file they're reading isn't the type they're checking for.
97  *
98  * If the routine handles this type of file, it should set the "file_type"
99  * field in the "struct wtap" to the type of the file.
100  *
101  * Put the trace files that are merely saved telnet-sessions last, since it's
102  * possible that you could have captured someone a router telnet-session
103  * using another tool. So, a libpcap trace of an toshiba "snoop" session
104  * should be discovered as a libpcap file, not a toshiba file.
105  */
106
107
108 static wtap_open_routine_t open_routines_base[] = {
109         /* Files that have magic bytes in fixed locations. These
110          * are easy to identify.
111          */
112         libpcap_open,
113         lanalyzer_open,
114         ngsniffer_open,
115         snoop_open,
116         iptrace_open,
117         netmon_open,
118         netxray_open,
119         radcom_open,
120         nettl_open,
121         visual_open,
122         _5views_open,
123         network_instruments_open,
124         airopeek9_open,
125         dbs_etherwatch_open,
126         k12_open,
127         catapult_dct2000_open,
128         ber_open,
129         pcapng_open,
130         aethra_open,
131         btsnoop_open,
132         packetlogger_open, /* This type does not have a magic number, but its
133                             * files are sometimes grabbed by mpeg_open. */
134         mpeg_open,
135         tnef_open,
136         dct3trace_open,
137         daintree_sna_open,
138         mime_file_open,
139         /* Files that don't have magic bytes at a fixed location,
140          * but that instead require a heuristic of some sort to
141          * identify them.  This includes the ASCII trace files that
142          * would be, for example, saved copies of a Telnet session
143          * to some box.
144          */
145
146         /* I put NetScreen *before* erf, because there were some
147          * false positives with my test-files (Sake Blok, July 2007)
148          */
149         netscreen_open,
150         erf_open,
151         ipfix_open,
152         k12text_open,
153         etherpeek_open,
154         pppdump_open,
155         iseries_open,
156         ascend_open,
157         eyesdn_open,
158         toshiba_open,
159         i4btrace_open,
160         csids_open,
161         vms_open,
162         cosine_open,
163         hcidump_open,
164         commview_open,
165         nstrace_open
166 };
167
168 #define N_FILE_TYPES    (sizeof open_routines_base / sizeof open_routines_base[0])
169
170 static wtap_open_routine_t* open_routines = NULL;
171
172 static GArray* open_routines_arr = NULL;
173
174
175 /* initialize the open routines array if it has not been initialized yet */
176 static void init_open_routines(void) {
177
178         if (open_routines_arr) return;
179
180         open_routines_arr = g_array_new(FALSE,TRUE,sizeof(wtap_open_routine_t));
181
182         g_array_append_vals(open_routines_arr,open_routines_base,N_FILE_TYPES);
183
184         open_routines = (wtap_open_routine_t*)(void *)open_routines_arr->data;
185 }
186
187 void wtap_register_open_routine(wtap_open_routine_t open_routine, gboolean has_magic) {
188         init_open_routines();
189
190         if (has_magic)
191                 g_array_prepend_val(open_routines_arr,open_routine);
192         else
193                 g_array_append_val(open_routines_arr,open_routine);
194
195         open_routines = (wtap_open_routine_t*)(void *)open_routines_arr->data;
196 }
197
198 /*
199  * Visual C++ on Win32 systems doesn't define these.  (Old UNIX systems don't
200  * define them either.)
201  *
202  * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
203  */
204 #ifndef S_ISREG
205 #define S_ISREG(mode)   (((mode) & S_IFMT) == S_IFREG)
206 #endif
207 #ifndef S_IFIFO
208 #define S_IFIFO _S_IFIFO
209 #endif
210 #ifndef S_ISFIFO
211 #define S_ISFIFO(mode)  (((mode) & S_IFMT) == S_IFIFO)
212 #endif
213 #ifndef S_ISDIR
214 #define S_ISDIR(mode)   (((mode) & S_IFMT) == S_IFDIR)
215 #endif
216
217 /* Opens a file and prepares a wtap struct.
218    If "do_random" is TRUE, it opens the file twice; the second open
219    allows the application to do random-access I/O without moving
220    the seek offset for sequential I/O, which is used by Wireshark
221    so that it can do sequential I/O to a capture file that's being
222    written to as new packets arrive independently of random I/O done
223    to display protocol trees for packets when they're selected. */
224 wtap* wtap_open_offline(const char *filename, int *err, char **err_info,
225                         gboolean do_random)
226 {
227         int     fd;
228         ws_statb64 statb;
229         wtap    *wth;
230         unsigned int    i;
231         gboolean use_stdin = FALSE;
232
233         /* open standard input if filename is '-' */
234         if (strcmp(filename, "-") == 0)
235                 use_stdin = TRUE;
236
237         /* First, make sure the file is valid */
238         if (use_stdin) {
239                 if (ws_fstat64(0, &statb) < 0) {
240                         *err = errno;
241                         return NULL;
242                 }
243         } else {
244                 if (ws_stat64(filename, &statb) < 0) {
245                         *err = errno;
246                         return NULL;
247                 }
248         }
249         if (S_ISFIFO(statb.st_mode)) {
250                 /*
251                  * Opens of FIFOs are allowed only when not opening
252                  * for random access.
253                  *
254                  * XXX - currently, we do seeking when trying to find
255                  * out the file type, so we don't actually support
256                  * opening FIFOs.  However, we may eventually
257                  * do buffering that allows us to do at least some
258                  * file type determination even on pipes, so we
259                  * allow FIFO opens and let things fail later when
260                  * we try to seek.
261                  */
262                 if (do_random) {
263                         *err = WTAP_ERR_RANDOM_OPEN_PIPE;
264                         return NULL;
265                 }
266         } else if (S_ISDIR(statb.st_mode)) {
267                 /*
268                  * Return different errors for "this is a directory"
269                  * and "this is some random special file type", so
270                  * the user can get a potentially more helpful error.
271                  */
272                 *err = EISDIR;
273                 return NULL;
274         } else if (! S_ISREG(statb.st_mode)) {
275                 *err = WTAP_ERR_NOT_REGULAR_FILE;
276                 return NULL;
277         }
278
279         /*
280          * We need two independent descriptors for random access, so
281          * they have different file positions.  If we're opening the
282          * standard input, we can only dup it to get additional
283          * descriptors, so we can't have two independent descriptors,
284          * and thus can't do random access.
285          */
286         if (use_stdin && do_random) {
287                 *err = WTAP_ERR_RANDOM_OPEN_STDIN;
288                 return NULL;
289         }
290
291         errno = ENOMEM;
292         wth = (wtap *)g_malloc0(sizeof(wtap));
293
294         /* Open the file */
295         errno = WTAP_ERR_CANT_OPEN;
296         if (use_stdin) {
297                 /*
298                  * We dup FD 0, so that we don't have to worry about
299                  * a file_close of wth->fh closing the standard
300                  * input of the process.
301                  */
302                 fd = ws_dup(0);
303                 if (fd < 0) {
304                         *err = errno;
305                         g_free(wth);
306                         return NULL;
307                 }
308 #ifdef _WIN32
309                 if (_setmode(fd, O_BINARY) == -1) {
310                         /* "Shouldn't happen" */
311                         *err = errno;
312                         g_free(wth);
313                         return NULL;
314                 }
315 #endif
316                 if (!(wth->fh = filed_open(fd))) {
317                         *err = errno;
318                         ws_close(fd);
319                         g_free(wth);
320                         return NULL;
321                 }
322         } else {
323                 if (!(wth->fh = file_open(filename))) {
324                         *err = errno;
325                         g_free(wth);
326                         return NULL;
327                 }
328         }
329
330         if (do_random) {
331                 if (!(wth->random_fh = file_open(filename))) {
332                         *err = errno;
333                         file_close(wth->fh);
334                         g_free(wth);
335                         return NULL;
336                 }
337         } else
338                 wth->random_fh = NULL;
339
340         /* initialization */
341         wth->file_encap = WTAP_ENCAP_UNKNOWN;
342         wth->data_offset = 0;
343         wth->subtype_sequential_close = NULL;
344         wth->subtype_close = NULL;
345         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
346         wth->priv = NULL;
347
348         init_open_routines();
349         if (wth->random_fh) {
350                 wth->fast_seek = g_ptr_array_new();
351
352                 file_set_random_access(wth->fh, FALSE, wth->fast_seek);
353                 file_set_random_access(wth->random_fh, TRUE, wth->fast_seek);
354         }
355
356         /* Try all file types */
357         for (i = 0; i < open_routines_arr->len; i++) {
358                 /* Seek back to the beginning of the file; the open routine
359                    for the previous file type may have left the file
360                    position somewhere other than the beginning, and the
361                    open routine for this file type will probably want
362                    to start reading at the beginning.
363
364                    Initialize the data offset while we're at it. */
365                 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
366                         /* I/O error - give up */
367                         if (wth->random_fh != NULL)
368                                 file_close(wth->random_fh);
369                         file_close(wth->fh);
370                         g_free(wth);
371                         return NULL;
372                 }
373                 wth->data_offset = 0;
374
375                 switch ((*open_routines[i])(wth, err, err_info)) {
376
377                 case -1:
378                         /* I/O error - give up */
379                         if (wth->random_fh != NULL)
380                                 file_close(wth->random_fh);
381                         file_close(wth->fh);
382                         g_free(wth);
383                         return NULL;
384
385                 case 0:
386                         /* No I/O error, but not that type of file */
387                         break;
388
389                 case 1:
390                         /* We found the file type */
391                         goto success;
392                 }
393         }
394
395         /* Well, it's not one of the types of file we know about. */
396         wtap_close(wth);
397         *err = WTAP_ERR_FILE_UNKNOWN_FORMAT;
398         return NULL;
399
400 success:
401         wth->frame_buffer = (struct Buffer *)g_malloc(sizeof(struct Buffer));
402         buffer_init(wth->frame_buffer, 1500);
403         return wth;
404 }
405
406 /* Table of the file types we know about. */
407 static const struct file_type_info dump_open_table_base[] = {
408         /* WTAP_FILE_UNKNOWN (only used internally for initialization) */
409         { NULL, NULL, NULL, NULL, FALSE, FALSE,
410           NULL, NULL },
411
412         /* WTAP_FILE_PCAP */
413         /* Gianluca Varenni suggests that we add "deprecated" to the description. */
414         { "Wireshark/tcpdump/... - libpcap", "libpcap", "*.pcap;*.cap", ".pcap", FALSE, FALSE,
415           libpcap_dump_can_write_encap, libpcap_dump_open },
416
417         /* WTAP_FILE_PCAPNG */
418         { "Wireshark - pcapng", "pcapng", "*.pcapng", ".pcapng", FALSE, TRUE,
419           pcapng_dump_can_write_encap, pcapng_dump_open },
420
421         /* WTAP_FILE_PCAP_NSEC */
422         { "Wireshark - nanosecond libpcap", "nseclibpcap", "*.pcap;*.cap", ".pcap", FALSE, FALSE,
423           libpcap_dump_can_write_encap, libpcap_dump_open },
424
425         /* WTAP_FILE_PCAP_AIX */
426         { "AIX tcpdump - libpcap", "aixlibpcap", "*.pcap;*.cap", ".pcap", FALSE, FALSE,
427           NULL, NULL },
428
429         /* WTAP_FILE_PCAP_SS991029 */
430         { "Modified tcpdump - libpcap", "modlibpcap", "*.pcap;*.cap", ".pcap", FALSE, FALSE,
431           libpcap_dump_can_write_encap, libpcap_dump_open },
432
433         /* WTAP_FILE_PCAP_NOKIA */
434         { "Nokia tcpdump - libpcap ", "nokialibpcap", "*.pcap;*.cap", ".pcap", FALSE, FALSE,
435           libpcap_dump_can_write_encap, libpcap_dump_open },
436
437         /* WTAP_FILE_PCAP_SS990417 */
438         { "RedHat 6.1 tcpdump - libpcap", "rh6_1libpcap", "*.pcap;*.cap", ".pcap", FALSE, FALSE,
439           libpcap_dump_can_write_encap, libpcap_dump_open },
440
441         /* WTAP_FILE_PCAP_SS990915 */
442         { "SuSE 6.3 tcpdump - libpcap", "suse6_3libpcap", "*.pcap;*.cap", ".pcap", FALSE, FALSE,
443           libpcap_dump_can_write_encap, libpcap_dump_open },
444
445         /* WTAP_FILE_5VIEWS */
446         { "Accellent 5Views capture", "5views", "*.5vw", ".5vw", TRUE, FALSE,
447           _5views_dump_can_write_encap, _5views_dump_open },
448
449         /* WTAP_FILE_IPTRACE_1_0 */
450         { "AIX iptrace 1.0", "iptrace_1", NULL, NULL, FALSE, FALSE,
451           NULL, NULL },
452
453         /* WTAP_FILE_IPTRACE_2_0 */
454         { "AIX iptrace 2.0", "iptrace_2", NULL, NULL, FALSE, FALSE,
455           NULL, NULL },
456
457         /* WTAP_FILE_BER */
458         { "ASN.1 Basic Encoding Rules", "ber", NULL, NULL, FALSE, FALSE,
459                 NULL, NULL },
460
461         /* WTAP_FILE_HCIDUMP */
462         { "Bluetooth HCI dump", "hcidump", NULL, NULL, FALSE, FALSE,
463           NULL, NULL },
464
465         /* WTAP_FILE_CATAPULT_DCT2000 */
466         { "Catapult DCT2000 trace (.out format)", "dct2000", "*.out", ".out", FALSE, FALSE,
467           catapult_dct2000_dump_can_write_encap, catapult_dct2000_dump_open },
468
469         /* WTAP_FILE_NETXRAY_OLD */
470         { "Cinco Networks NetXRay 1.x", "netxray1", "*.cap", ".cap", TRUE, FALSE,
471           NULL, NULL },
472
473         /* WTAP_FILE_NETXRAY_1_0 */
474         { "Cinco Networks NetXRay 2.0 or later", "netxray2", "*.cap", ".cap", TRUE, FALSE,
475           NULL, NULL },
476
477         /* WTAP_FILE_COSINE */
478         { "CoSine IPSX L2 capture", "cosine", "*.txt", ".txt", FALSE, FALSE,
479           NULL, NULL },
480
481         /* WTAP_FILE_CSIDS */
482         { "CSIDS IPLog", "csids", NULL, NULL, FALSE, FALSE,
483           NULL, NULL },
484
485         /* WTAP_FILE_DBS_ETHERWATCH */
486         { "DBS Etherwatch (VMS)", "etherwatch", "*.txt", ".txt", FALSE, FALSE,
487           NULL, NULL},
488
489         /* WTAP_FILE_ERF */
490         { "Endace ERF capture", "erf", "*.erf", ".erf", FALSE, FALSE,
491           erf_dump_can_write_encap, erf_dump_open },
492
493         /* WTAP_FILE_EYESDN */
494         { "EyeSDN USB S0/E1 ISDN trace format", "eyesdn", "*.trc", ".trc", FALSE, FALSE,
495            eyesdn_dump_can_write_encap, eyesdn_dump_open },
496
497         /* WTAP_FILE_NETTL */
498         { "HP-UX nettl trace", "nettl", "*.trc0;*.trc1", ".trc0", FALSE, FALSE,
499           nettl_dump_can_write_encap, nettl_dump_open },
500
501         /* WTAP_FILE_ISERIES */
502         { "IBM iSeries comm. trace (ASCII)", "iseries_ascii", "*.txt", ".txt", FALSE, FALSE,
503           NULL, NULL },
504
505         /* WTAP_FILE_ISERIES_UNICODE */
506         { "IBM iSeries comm. trace (UNICODE)", "iseries_unicode", "*.txt", ".txt", FALSE, FALSE,
507           NULL, NULL },
508
509         /* WTAP_FILE_I4BTRACE */
510         { "I4B ISDN trace", "i4btrace", NULL, NULL, FALSE, FALSE,
511           NULL, NULL },
512
513         /* WTAP_FILE_ASCEND */
514         { "Lucent/Ascend access server trace", "ascend", "*.txt", ".txt", FALSE, FALSE,
515           NULL, NULL },
516
517         /* WTAP_FILE_NETMON_1_x */
518         { "Microsoft NetMon 1.x", "netmon1", "*.cap", ".cap", TRUE, FALSE,
519           netmon_dump_can_write_encap_1_x, netmon_dump_open },
520
521         /* WTAP_FILE_NETMON_2_x */
522         { "Microsoft NetMon 2.x", "netmon2", "*.cap", ".cap", TRUE, FALSE,
523           netmon_dump_can_write_encap_2_x, netmon_dump_open },
524
525         /* WTAP_FILE_NGSNIFFER_UNCOMPRESSED */
526         { "NA Sniffer (DOS)", "ngsniffer", "*.cap;*.enc;*.trc;*.fdc;*.syc", ".cap", FALSE, FALSE,
527           ngsniffer_dump_can_write_encap, ngsniffer_dump_open },
528
529         /* WTAP_FILE_NGSNIFFER_COMPRESSED */
530         { "NA Sniffer (DOS), compressed", "ngsniffer_comp", "*.caz", ".caz", FALSE, FALSE,
531           NULL, NULL },
532
533         /* WTAP_FILE_NETXRAY_1_1 */
534         { "NA Sniffer (Windows) 1.1", "ngwsniffer_1_1", "*.cap", ".cap", TRUE, FALSE,
535           netxray_dump_can_write_encap_1_1, netxray_dump_open_1_1 },
536
537         /* WTAP_FILE_NETXRAY_2_00x */
538         { "NA Sniffer (Windows) 2.00x", "ngwsniffer_2_0", "*.cap", ".cap", TRUE, FALSE,
539           netxray_dump_can_write_encap_2_0, netxray_dump_open_2_0 },
540
541         /* WTAP_FILE_NETWORK_INSTRUMENTS */
542         { "Network Instruments Observer", "niobserver", "*.bfr", ".bfr", FALSE, FALSE,
543           network_instruments_dump_can_write_encap, network_instruments_dump_open },
544
545         /* WTAP_FILE_LANALYZER */
546         { "Novell LANalyzer","lanalyzer", "*.tr1", ".tr1", TRUE, FALSE,
547           lanalyzer_dump_can_write_encap, lanalyzer_dump_open },
548
549         /* WTAP_FILE_PPPDUMP */
550         { "pppd log (pppdump format)", "pppd", NULL, NULL, FALSE, FALSE,
551           NULL, NULL },
552
553         /* WTAP_FILE_RADCOM */
554         { "RADCOM WAN/LAN analyzer", "radcom", NULL, NULL, FALSE, FALSE,
555           NULL, NULL },
556
557         /* WTAP_FILE_SNOOP */
558         { "Sun snoop", "snoop", "*.snoop;*.cap", ".snoop", FALSE, FALSE,
559           snoop_dump_can_write_encap, snoop_dump_open },
560
561         /* WTAP_FILE_SHOMITI */
562         { "Shomiti/Finisar Surveyor", "shomiti", "*.cap", ".cap", FALSE, FALSE,
563           NULL, NULL },
564
565         /* WTAP_FILE_VMS */
566         { "TCPIPtrace (VMS)", "tcpiptrace", "*.txt", ".txt", FALSE, FALSE,
567           NULL, NULL},
568
569         /* WTAP_FILE_K12 */
570         { "Tektronix K12xx 32-bit .rf5 format", "rf5", "*.rf5", ".rf5", TRUE, FALSE,
571                 k12_dump_can_write_encap, k12_dump_open },
572
573         /* WTAP_FILE_TOSHIBA */
574         { "Toshiba Compact ISDN Router snoop", "toshiba", "*.txt", ".txt", FALSE, FALSE,
575           NULL, NULL },
576
577         /* WTAP_FILE_VISUAL_NETWORKS */
578         { "Visual Networks traffic capture", "visual", NULL, NULL, TRUE, FALSE,
579           visual_dump_can_write_encap, visual_dump_open },
580
581         /* WTAP_FILE_ETHERPEEK_V56 */
582         { "WildPackets Ether/TokenPeek (V5 & V6)", "peek56", "*.tpc;*.apc;*.pkt;*.wpz", ".pkt", FALSE, FALSE,
583           NULL, NULL },
584
585         /* WTAP_FILE_ETHERPEEK_V7 */
586         { "WildPackets Ether/Token/AiroPeek (V7)", "peek7", "*.tpc;*.apc;*.pkt;*.wpz", ".pkt", FALSE, FALSE,
587           NULL, NULL },
588
589         /* WTAP_FILE_ETHERPEEK_V9 */
590         { "WildPackets Ether/AiroPeek (V9)", "peek9", "*.tpc;*.apc;*.pkt;*.wpz", ".pkt", FALSE, FALSE,
591           NULL, NULL },
592
593         /* WTAP_FILE_MPEG */
594         { "MPEG", "mpeg", "*.mpeg;*.mpg;*.mp3", ".mpeg", FALSE, FALSE,
595           NULL, NULL },
596
597         /* WTAP_FILE_K12TEXT  */
598         { "K12 text file", "k12text", "*.txt", ".txt", FALSE, FALSE,
599           k12text_dump_can_write_encap, k12text_dump_open },
600
601         /* WTAP_FILE_NETSCREEN */
602         { "NetScreen snoop text file", "netscreen", "*.txt", ".txt", FALSE, FALSE,
603           NULL, NULL },
604
605         /* WTAP_FILE_COMMVIEW */
606         { "TamoSoft CommView", "commview", "*.ncf", ".ncf", FALSE, FALSE,
607           commview_dump_can_write_encap, commview_dump_open },
608
609         /* WTAP_FILE_BTSNOOP */
610         { "Symbian OS btsnoop", "btsnoop", "*.log", ".log", FALSE, FALSE,
611           btsnoop_dump_can_write_encap, btsnoop_dump_open_h4 },
612
613         /* WTAP_FILE_TNEF */
614         { "Transport-Neutral Encapsulation Format", "tnef", NULL, NULL, FALSE, FALSE,
615           NULL, NULL },
616
617         /* WTAP_FILE_DCT3TRACE */
618         { "Gammu DCT3 trace", "dct3trace", "*.xml", NULL, FALSE, FALSE,
619           NULL, NULL },
620
621         /* WTAP_FILE_PACKETLOGGER */
622         { "PacketLogger", "pklg", "*.pklg", ".pklg", FALSE, FALSE,
623           NULL, NULL },
624
625         /* WTAP_FILE_DAINTREE_SNA */
626         { "Daintree SNA", "dsna", "*.dcf", ".dcf", FALSE, FALSE,
627           NULL, NULL },
628
629         /* WTAP_FILE_NETSCALER_1_0 */
630         { "NetScaler Trace (Version 1.0)", "nstrace10", NULL, NULL, TRUE, FALSE,
631           nstrace_10_dump_can_write_encap, nstrace_dump_open },
632
633         /* WTAP_FILE_NETSCALER_2_0 */
634         { "NetScaler Trace (Version 2.0)", "nstrace20", "*.cap", ".cap", TRUE, FALSE,
635           nstrace_20_dump_can_write_encap, nstrace_dump_open },
636
637         /* WTAP_FILE_JPEG_JFIF */
638         { "JPEG/JFIF", "jpeg", "*.jpg;*.jpeg;*.jfif", ".jpg", FALSE, FALSE,
639           NULL, NULL },
640
641         /* WTAP_FILE_IPFIX */
642         { "IPFIX File Format", "ipfix", "*.pfx;*.ipfix", ".pfx", FALSE, FALSE,
643           NULL, NULL },
644
645         /* WTAP_ENCAP_MIME */
646         { "MIME File Format", "mime", NULL, NULL, FALSE, FALSE,
647            NULL, NULL },
648
649         /* WTAP_FILE_AETHRA */
650         { "Aethra .aps file", "aethra", "*.aps", ".aps", FALSE, FALSE,
651           NULL, NULL },
652 };
653
654 gint wtap_num_file_types = sizeof(dump_open_table_base) / sizeof(struct file_type_info);
655
656 static GArray*  dump_open_table_arr = NULL;
657 static const struct file_type_info* dump_open_table = dump_open_table_base;
658
659 /* initialize the open routines array if it has not being initialized yet */
660 static void init_file_types(void) {
661
662         if (dump_open_table_arr) return;
663
664         dump_open_table_arr = g_array_new(FALSE,TRUE,sizeof(struct file_type_info));
665
666         g_array_append_vals(dump_open_table_arr,dump_open_table_base,wtap_num_file_types);
667
668         dump_open_table = (const struct file_type_info*)(void *)dump_open_table_arr->data;
669 }
670
671 int wtap_register_file_type(const struct file_type_info* fi) {
672         init_file_types();
673
674         g_array_append_val(dump_open_table_arr,*fi);
675
676         dump_open_table = (const struct file_type_info*)(void *)dump_open_table_arr->data;
677
678         return wtap_num_file_types++;
679 }
680
681 int wtap_get_num_file_types(void)
682 {
683         return wtap_num_file_types;
684 }
685
686 /* Name that should be somewhat descriptive. */
687 const char *wtap_file_type_string(int filetype)
688 {
689         if (filetype < 0 || filetype >= wtap_num_file_types) {
690                 g_error("Unknown capture file type %d", filetype);
691                 /** g_error() does an abort() and thus never returns **/
692                 return "";
693         } else
694                 return dump_open_table[filetype].name;
695 }
696
697 /* Name to use in, say, a command-line flag specifying the type. */
698 const char *wtap_file_type_short_string(int filetype)
699 {
700         if (filetype < 0 || filetype >= wtap_num_file_types)
701                 return NULL;
702         else
703                 return dump_open_table[filetype].short_name;
704 }
705
706 /* Translate a short name to a capture file type. */
707 int wtap_short_string_to_file_type(const char *short_name)
708 {
709         int filetype;
710
711         for (filetype = 0; filetype < wtap_num_file_types; filetype++) {
712                 if (dump_open_table[filetype].short_name != NULL &&
713                     strcmp(short_name, dump_open_table[filetype].short_name) == 0)
714                         return filetype;
715         }
716         return -1;      /* no such file type, or we can't write it */
717 }
718
719 /* Return a list of file extensions that are used by the specified file type.
720    This includes compressed extensions, e.g. not just "pcap" but also
721    "pcap.gz" if we can read gzipped files.
722
723    All strings in the list are allocated with g_malloc() and must be freed
724    with g_free(). */
725 GSList *wtap_get_file_extensions_list(int filetype)
726 {
727         gchar **extensions_set, **extensionp;
728         gchar *extension;
729         GSList *compressed_file_extensions, *compressed_file_extension;
730         GSList *extensions;
731
732         if (filetype < 0 || filetype >= wtap_num_file_types)
733                 return NULL;    /* not a valid file type */
734
735         if (dump_open_table[filetype].file_extensions == NULL)
736                 return NULL;    /* valid, but no extensions list */
737
738         /*
739          * Split the extension-list string into a set of extensions.
740          */
741         extensions_set = g_strsplit(dump_open_table[filetype].file_extensions,
742             ";", 0);
743
744         /*
745          * Get the list of compressed-file extensions.
746          */
747         compressed_file_extensions = wtap_get_compressed_file_extensions();
748
749         /*
750          * Add each of those extensions to the list.
751          */
752         extensions = NULL;      /* empty list, to start with */
753         for (extensionp = extensions_set; *extensionp != NULL; extensionp++) {
754                 extension = *extensionp;
755
756                 /*
757                  * XXX - skip past the "*.".
758                  */
759                 extension += 2;
760
761                 /*
762                  * Now add the extension.
763                  */
764                 extensions = g_slist_append(extensions, g_strdup(extension));
765
766                 /*
767                  * Now add the extensions for compressed-file versions of
768                  * that extension.
769                  */
770                 for (compressed_file_extension = compressed_file_extensions;
771                     compressed_file_extension != NULL;
772                     compressed_file_extension = g_slist_next(compressed_file_extension)) {
773                         extensions = g_slist_append(extensions,
774                             g_strdup_printf("%s.%s", extension,
775                               (gchar *)compressed_file_extension->data));
776                 }
777         }
778         g_strfreev(extensions_set);
779         g_slist_free(compressed_file_extensions);
780         return extensions;
781 }
782
783 /*
784  * Free a list returned by wtap_file_extensions_list().
785  */
786 void wtap_free_file_extensions_list(GSList *extensions)
787 {
788         GSList *extension;
789
790         for (extension = extensions; extension != NULL;
791             extension = g_slist_next(extension)) {
792                 g_free(extension->data);
793         }
794         g_slist_free(extensions);
795 }
796
797 /* Return the default file extension to use with the specified file type. */
798 const char *wtap_file_extension_default_string(int filetype)
799 {
800         if (filetype < 0 || filetype >= wtap_num_file_types)
801                 return NULL;
802         else
803                 return dump_open_table[filetype].file_extension_default;
804 }
805
806 gboolean wtap_dump_can_open(int filetype)
807 {
808         if (filetype < 0 || filetype >= wtap_num_file_types
809             || dump_open_table[filetype].dump_open == NULL)
810                 return FALSE;
811
812         return TRUE;
813 }
814
815 gboolean wtap_dump_can_write_encap(int filetype, int encap)
816 {
817         if (filetype < 0 || filetype >= wtap_num_file_types
818             || dump_open_table[filetype].can_write_encap == NULL)
819                 return FALSE;
820
821         if ((*dump_open_table[filetype].can_write_encap)(encap) != 0)
822                 return FALSE;
823
824         return TRUE;
825 }
826
827 #ifdef HAVE_LIBZ
828 gboolean wtap_dump_can_compress(int filetype)
829 {
830         /*
831          * If this is an unknown file type, or if we have to
832          * seek when writing out a file with this file type,
833          * return FALSE.
834          */
835         if (filetype < 0 || filetype >= wtap_num_file_types
836             || dump_open_table[filetype].writing_must_seek)
837                 return FALSE;
838
839         return TRUE;
840 }
841 #else
842 gboolean wtap_dump_can_compress(int filetype _U_)
843 {
844         return FALSE;
845 }
846 #endif
847
848 gboolean wtap_dump_has_name_resolution(int filetype)
849 {
850         if (filetype < 0 || filetype >= wtap_num_file_types
851             || dump_open_table[filetype].has_name_resolution == FALSE)
852                 return FALSE;
853
854         return TRUE;
855 }
856
857 static gboolean wtap_dump_open_check(int filetype, int encap, gboolean comressed, int *err);
858 static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen,
859                                         gboolean compressed, int *err);
860 static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, gboolean compressed, int *err);
861
862 static WFILE_T wtap_dump_file_open(wtap_dumper *wdh, const char *filename);
863 static WFILE_T wtap_dump_file_fdopen(wtap_dumper *wdh, int fd);
864 static int wtap_dump_file_close(wtap_dumper *wdh);
865
866 wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
867                                 int snaplen, gboolean compressed, int *err)
868 {
869         wtap_dumper *wdh;
870         WFILE_T fh;
871
872         /* Check whether we can open a capture file with that file type
873            and that encapsulation. */
874         if (!wtap_dump_open_check(filetype, encap, compressed, err))
875                 return NULL;
876
877         /* Allocate a data structure for the output stream. */
878         wdh = wtap_dump_alloc_wdh(filetype, encap, snaplen, compressed, err);
879         if (wdh == NULL)
880                 return NULL;    /* couldn't allocate it */
881
882         /* "-" means stdout */
883         if (strcmp(filename, "-") == 0) {
884                 if (compressed) {
885                         *err = EINVAL;  /* XXX - return a Wiretap error code for this */
886                         g_free(wdh);
887                         return NULL;    /* compress won't work on stdout */
888                 }
889 #ifdef _WIN32
890                 if (_setmode(fileno(stdout), O_BINARY) == -1) {
891                         /* "Should not happen" */
892                         *err = errno;
893                         g_free(wdh);
894                         return NULL;    /* couldn't put standard output in binary mode */
895                 }
896 #endif
897                 wdh->fh = stdout;
898         } else {
899                 /* In case "fopen()" fails but doesn't set "errno", set "errno"
900                    to a generic "the open failed" error. */
901                 errno = WTAP_ERR_CANT_OPEN;
902                 fh = wtap_dump_file_open(wdh, filename);
903                 if (fh == NULL) {
904                         *err = errno;
905                         g_free(wdh);
906                         return NULL;    /* can't create file */
907                 }
908                 wdh->fh = fh;
909         }
910
911         if (!wtap_dump_open_finish(wdh, filetype, compressed, err)) {
912                 /* Get rid of the file we created; we couldn't finish
913                    opening it. */
914                 if (wdh->fh != stdout) {
915                         wtap_dump_file_close(wdh);
916                         ws_unlink(filename);
917                 }
918                 g_free(wdh);
919                 return NULL;
920         }
921         return wdh;
922 }
923
924 wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
925                                 gboolean compressed, int *err)
926 {
927         wtap_dumper *wdh;
928         WFILE_T fh;
929
930         /* Check whether we can open a capture file with that file type
931            and that encapsulation. */
932         if (!wtap_dump_open_check(filetype, encap, compressed, err))
933                 return NULL;
934
935         /* Allocate a data structure for the output stream. */
936         wdh = wtap_dump_alloc_wdh(filetype, encap, snaplen, compressed, err);
937         if (wdh == NULL)
938                 return NULL;    /* couldn't allocate it */
939
940 #ifdef _WIN32
941         if (fd == 1) {
942                 if (_setmode(fileno(stdout), O_BINARY) == -1) {
943                         /* "Should not happen" */
944                         *err = errno;
945                         g_free(wdh);
946                         return NULL;    /* couldn't put standard output in binary mode */
947                 }
948         }
949 #endif
950
951         /* In case "fopen()" fails but doesn't set "errno", set "errno"
952            to a generic "the open failed" error. */
953         errno = WTAP_ERR_CANT_OPEN;
954         fh = wtap_dump_file_fdopen(wdh, fd);
955         if (fh == NULL) {
956                 *err = errno;
957                 g_free(wdh);
958                 return NULL;    /* can't create standard I/O stream */
959         }
960         wdh->fh = fh;
961
962         if (!wtap_dump_open_finish(wdh, filetype, compressed, err)) {
963                 wtap_dump_file_close(wdh);
964                 g_free(wdh);
965                 return NULL;
966         }
967         return wdh;
968 }
969
970 static gboolean wtap_dump_open_check(int filetype, int encap, gboolean compressed, int *err)
971 {
972         if (!wtap_dump_can_open(filetype)) {
973                 /* Invalid type, or type we don't know how to write. */
974                 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
975                 return FALSE;
976         }
977
978         /* OK, we know how to write that type; can we write the specified
979            encapsulation type? */
980         *err = (*dump_open_table[filetype].can_write_encap)(encap);
981         if (*err != 0)
982                 return FALSE;
983
984         /* if compression is wanted, do we support this for this filetype? */
985         if(compressed && !wtap_dump_can_compress(filetype)) {
986                 *err = WTAP_ERR_COMPRESSION_NOT_SUPPORTED;
987                 return FALSE;
988         }
989
990         *err = (*dump_open_table[filetype].can_write_encap)(encap);
991         if (*err != 0)
992                 return FALSE;
993
994         /* All systems go! */
995         return TRUE;
996 }
997
998 static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen,
999                                         gboolean compressed, int *err)
1000 {
1001         wtap_dumper *wdh;
1002
1003         wdh = (wtap_dumper *)g_malloc0(sizeof (wtap_dumper));
1004         if (wdh == NULL) {
1005                 *err = errno;
1006                 return NULL;
1007         }
1008
1009         wdh->file_type = filetype;
1010         wdh->snaplen = snaplen;
1011         wdh->encap = encap;
1012         wdh->compressed = compressed;
1013         return wdh;
1014 }
1015
1016 static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, gboolean compressed, int *err)
1017 {
1018         int fd;
1019         gboolean cant_seek;
1020
1021         /* Can we do a seek on the file descriptor?
1022            If not, note that fact. */
1023         if(compressed) {
1024                 cant_seek = TRUE;
1025         } else {
1026                 fd = fileno((FILE *)wdh->fh);
1027                 if (lseek(fd, 1, SEEK_CUR) == -1)
1028                         cant_seek = TRUE;
1029                 else {
1030                         /* Undo the seek. */
1031                         lseek(fd, 0, SEEK_SET);
1032                         cant_seek = FALSE;
1033                 }
1034         }
1035
1036         /* If this file type requires seeking, and we can't seek, fail. */
1037         if (dump_open_table[filetype].writing_must_seek && cant_seek) {
1038                 *err = WTAP_ERR_CANT_WRITE_TO_PIPE;
1039                 return FALSE;
1040         }
1041
1042         /* Now try to open the file for writing. */
1043         if (!(*dump_open_table[filetype].dump_open)(wdh, err)) {
1044                 return FALSE;
1045         }
1046
1047         return TRUE;    /* success! */
1048 }
1049
1050 gboolean wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
1051                    const union wtap_pseudo_header *pseudo_header, const guint8 *pd, int *err)
1052 {
1053         return (wdh->subtype_write)(wdh, phdr, pseudo_header, pd, err);
1054 }
1055
1056 void wtap_dump_flush(wtap_dumper *wdh)
1057 {
1058 #ifdef HAVE_LIBZ
1059         if(wdh->compressed) {
1060                 gzwfile_flush((GZWFILE_T)wdh->fh);
1061         } else
1062 #endif
1063         {
1064                 fflush((FILE *)wdh->fh);
1065         }
1066 }
1067
1068 gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
1069 {
1070         gboolean ret = TRUE;
1071
1072         if (wdh->subtype_close != NULL) {
1073                 /* There's a close routine for this dump stream. */
1074                 if (!(wdh->subtype_close)(wdh, err))
1075                         ret = FALSE;
1076         }
1077         errno = WTAP_ERR_CANT_CLOSE;
1078         /* Don't close stdout */
1079         if (wdh->fh != stdout) {
1080                 if (wtap_dump_file_close(wdh) == EOF) {
1081                         if (ret) {
1082                                 /* The per-format close function succeeded,
1083                                    but the fclose didn't.  Save the reason
1084                                    why, if our caller asked for it. */
1085                                 if (err != NULL)
1086                                         *err = errno;
1087                         }
1088                         ret = FALSE;
1089                 }
1090         } else {
1091                 /* as we don't close stdout, at least try to flush it */
1092                 wtap_dump_flush(wdh);
1093         }
1094         if (wdh->priv != NULL)
1095                 g_free(wdh->priv);
1096         g_free(wdh);
1097         return ret;
1098 }
1099
1100 gint64 wtap_get_bytes_dumped(wtap_dumper *wdh)
1101 {
1102         return wdh->bytes_dumped;
1103 }
1104
1105 void wtap_set_bytes_dumped(wtap_dumper *wdh, gint64 bytes_dumped)
1106 {
1107         wdh->bytes_dumped = bytes_dumped;
1108 }
1109
1110 gboolean wtap_dump_set_addrinfo_list(wtap_dumper *wdh, struct addrinfo *addrinfo_list)
1111 {
1112         if (!wdh || wdh->file_type < 0 || wdh->file_type >= wtap_num_file_types
1113                 || dump_open_table[wdh->file_type].has_name_resolution == FALSE)
1114                         return FALSE;
1115         wdh->addrinfo_list = addrinfo_list;
1116         return TRUE;
1117 }
1118
1119 /* internally open a file for writing (compressed or not) */
1120 #ifdef HAVE_LIBZ
1121 static WFILE_T wtap_dump_file_open(wtap_dumper *wdh, const char *filename)
1122 {
1123         if(wdh->compressed) {
1124                 return gzwfile_open(filename);
1125         } else {
1126                 return ws_fopen(filename, "wb");
1127         }
1128 }
1129 #else
1130 static WFILE_T wtap_dump_file_open(wtap_dumper *wdh _U_, const char *filename)
1131 {
1132         return ws_fopen(filename, "wb");
1133 }
1134 #endif
1135
1136 /* internally open a file for writing (compressed or not) */
1137 #ifdef HAVE_LIBZ
1138 static WFILE_T wtap_dump_file_fdopen(wtap_dumper *wdh, int fd)
1139 {
1140         if(wdh->compressed) {
1141                 return gzwfile_fdopen(fd);
1142         } else {
1143                 return fdopen(fd, "wb");
1144         }
1145 }
1146 #else
1147 static WFILE_T wtap_dump_file_fdopen(wtap_dumper *wdh _U_, int fd)
1148 {
1149         return fdopen(fd, "wb");
1150 }
1151 #endif
1152
1153 /* internally writing raw bytes (compressed or not) */
1154 gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf, size_t bufsize,
1155                      int *err)
1156 {
1157         size_t nwritten;
1158
1159 #ifdef HAVE_LIBZ
1160         if (wdh->compressed) {
1161                 nwritten = gzwfile_write((GZWFILE_T)wdh->fh, buf, (unsigned) bufsize);
1162                 /*
1163                  * gzwfile_write() returns 0 on error.
1164                  */
1165                 if (nwritten == 0) {
1166                         *err = gzwfile_geterr((GZWFILE_T)wdh->fh);
1167                         return FALSE;
1168                 }
1169         } else
1170 #endif
1171         {
1172                 nwritten = fwrite(buf, 1, bufsize, (FILE *)wdh->fh);
1173                 /*
1174                  * At least according to the Mac OS X man page,
1175                  * this can return a short count on an error.
1176                  */
1177                 if (nwritten != bufsize) {
1178                         if (ferror((FILE *)wdh->fh))
1179                                 *err = errno;
1180                         else
1181                                 *err = WTAP_ERR_SHORT_WRITE;
1182                         return FALSE;
1183                 }
1184         }
1185         return TRUE;
1186 }
1187
1188 /* internally close a file for writing (compressed or not) */
1189 static int wtap_dump_file_close(wtap_dumper *wdh)
1190 {
1191 #ifdef HAVE_LIBZ
1192         if(wdh->compressed) {
1193                 return gzwfile_close((GZWFILE_T)wdh->fh);
1194         } else
1195 #endif
1196         {
1197                 return fclose((FILE *)wdh->fh);
1198         }
1199 }