In Tethereal:
[obnox/wireshark/wip.git] / wiretap / file.c
1 /* file.c
2  *
3  * $Id: file.c,v 1.55 2000/07/20 09:39:43 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
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 <fcntl.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33
34 #ifdef HAVE_IO_H
35 #include <io.h> /* open/close on win32 */
36 #endif
37
38 #include "wtap-int.h"
39 #include "file_wrappers.h"
40 #include "buffer.h"
41 #include "lanalyzer.h"
42 #include "ngsniffer.h"
43 #include "radcom.h"
44 #include "ascend.h"
45 #include "nettl.h"
46 #include "libpcap.h"
47 #include "snoop.h"
48 #include "iptrace.h"
49 #include "netmon.h"
50 #include "netxray.h"
51 #include "toshiba.h"
52 #include "i4btrace.h"
53
54 /* The open_file_* routines should return:
55  *
56  *      -1 on an I/O error;
57  *
58  *      1 if the file they're reading is one of the types it handles;
59  *
60  *      0 if the file they're reading isn't the type they're checking for.
61  *
62  * If the routine handles this type of file, it should set the "file_type"
63  * field in the "struct wtap" to the type of the file.
64  *
65  * XXX - I need to drag my damn ANSI C spec in to figure out how to
66  * declare a "const" array of pointers to functions; putting "const"
67  * right after "static" isn't the right answer, at least according
68  * to GCC, which whines if I do that.
69  *
70  * Put the trace files that are merely saved telnet-sessions last, since it's
71  * possible that you could have captured someone a router telnet-session
72  * using another tool. So, a libpcap trace of an toshiba "snoop" session
73  * should be discovered as a libpcap file, not a toshiba file.
74  */
75
76 static int (*open_routines[])(wtap *, int *) = {
77         /* Files that have magic bytes in fixed locations. These
78          * are easy to identify.
79          */
80         libpcap_open,
81         lanalyzer_open,
82         ngsniffer_open,
83         snoop_open,
84         iptrace_open,
85         netmon_open,
86         netxray_open,
87         radcom_open,
88         nettl_open,
89
90         /* Files whose magic headers are in text *somewhere* in the
91          * file (usually because the trace is just a saved copy of
92          * the telnet session). 
93          */
94         ascend_open,
95         toshiba_open,
96         i4btrace_open,
97 };
98
99 int wtap_def_seek_read(wtap *wth, int seek_off,
100         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len)
101 {
102         file_seek(wth->random_fh, seek_off, SEEK_SET);
103
104         return file_read(pd, sizeof(guint8), len, wth->random_fh);
105 }
106
107 #define N_FILE_TYPES    (sizeof open_routines / sizeof open_routines[0])
108
109 /* Opens a file and prepares a wtap struct.
110    If "do_random" is TRUE, it opens the file twice; the second open
111    allows the application to do random-access I/O without moving
112    the seek offset for sequential I/O, which is used by Ethereal
113    so that it can do sequential I/O to a capture file that's being
114    written to as new packets arrive independently of random I/O done
115    to display protocol trees for packets when they're selected. */
116 wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
117 {
118         struct stat statb;
119         wtap    *wth;
120         int     i;
121
122         /* First, make sure the file is valid */
123         if (stat(filename, &statb) < 0) {
124                 *err = errno;
125                 return NULL;
126         }
127 #ifndef WIN32
128         if (! S_ISREG(statb.st_mode) && ! S_ISFIFO(statb.st_mode)) {
129                 if (S_ISDIR(statb.st_mode))
130                         *err = EISDIR;
131                 else
132                         *err = WTAP_ERR_NOT_REGULAR_FILE;
133                 return NULL;
134         }
135 #endif
136
137         errno = ENOMEM;
138         wth = g_malloc(sizeof(wtap));
139         if (wth == NULL) {
140                 *err = errno;
141                 return NULL;
142         }
143
144 /* Win32 needs the O_BINARY flag for open() */
145 #ifndef O_BINARY
146 #define O_BINARY        0
147 #endif
148
149         /* Open the file */
150         errno = WTAP_ERR_CANT_OPEN;
151         wth->fd = open(filename, O_RDONLY|O_BINARY);
152         if (wth->fd < 0) {
153                 *err = errno;
154                 g_free(wth);
155                 return NULL;
156         }
157         if (!(wth->fh = filed_open(wth->fd, "rb"))) {
158                 *err = errno;
159                 g_free(wth);
160                 return NULL;
161         }
162
163         if (do_random) {
164                 if (!(wth->random_fh = file_open(filename, "rb"))) {
165                         *err = errno;
166                         file_close(wth->fh);
167                         g_free(wth);
168                         return NULL;
169                 }
170         } else
171                 wth->random_fh = NULL;
172
173         /* initialization */
174         wth->file_encap = WTAP_ENCAP_UNKNOWN;
175         wth->data_offset = 0;
176         wth->subtype_sequential_close = NULL;
177         wth->subtype_close = NULL;
178
179         /* Try all file types */
180         for (i = 0; i < N_FILE_TYPES; i++) {
181                 switch ((*open_routines[i])(wth, err)) {
182
183                 case -1:
184                         /* I/O error - give up */
185                         file_close(wth->fh);
186                         g_free(wth);
187                         return NULL;
188
189                 case 0:
190                         /* No I/O error, but not that type of file */
191                         break;
192
193                 case 1:
194                         /* We found the file type */
195                         goto success;
196                 }
197         }
198
199         /* Well, it's not one of the types of file we know about. */
200         if (wth->random_fh != NULL)
201                 file_close(wth->random_fh);
202         file_close(wth->fh);
203         g_free(wth);
204         *err = WTAP_ERR_FILE_UNKNOWN_FORMAT;
205         return NULL;
206
207 success:
208         wth->frame_buffer = g_malloc(sizeof(struct Buffer));
209         buffer_init(wth->frame_buffer, 1500);
210         return wth;
211 }
212
213 /* Table of the file types we know about. */
214 const static struct file_type_info {
215         const char *name;
216         const char *short_name;
217         int     (*can_write_encap)(int, int);
218         int     (*dump_open)(wtap_dumper *, int *);
219 } dump_open_table[WTAP_NUM_FILE_TYPES] = {
220         /* WTAP_FILE_UNKNOWN */
221         { NULL, NULL,
222           NULL, NULL },
223
224         /* WTAP_FILE_WTAP */
225         { "Wiretap (Ethereal)", NULL,
226           NULL, NULL },
227
228         /* WTAP_FILE_PCAP */
229         { "libpcap (tcpdump, Ethereal, etc.)", "libpcap",
230           libpcap_dump_can_write_encap, libpcap_dump_open },
231
232         /* WTAP_FILE_PCAP_MODIFIED */
233         { "modified libpcap (tcpdump)", "modlibpcap",
234           libpcap_dump_can_write_encap, libpcap_dump_open },
235
236         /* WTAP_FILE_PCAP_RH_6_1 */
237         { "Red Hat Linux 6.1 libpcap (tcpdump)", "rh6_1libpcap",
238           libpcap_dump_can_write_encap, libpcap_dump_open },
239
240         /* WTAP_FILE_LANALYZER */
241         { "Novell LANalyzer", NULL,
242           NULL, NULL },
243
244         /* WTAP_FILE_NGSNIFFER_UNCOMPRESSED */
245         { "Network Associates Sniffer (DOS-based)", "ngsniffer",
246           ngsniffer_dump_can_write_encap, ngsniffer_dump_open },
247
248         /* WTAP_FILE_NGSNIFFER_COMPRESSED */
249         { "Network Associates Sniffer (DOS-based), compressed", "ngsniffer_comp",
250           NULL, NULL },
251
252         /* WTAP_FILE_SNOOP */
253         { "Sun snoop", "snoop",
254           snoop_dump_can_write_encap, snoop_dump_open },
255
256         /* WTAP_FILE_IPTRACE_1_0 */
257         { "AIX iptrace 1.0", NULL,
258           NULL, NULL },
259
260         /* WTAP_FILE_IPTRACE_2_0 */
261         { "AIX iptrace 2.0", NULL,
262           NULL, NULL },
263
264         /* WTAP_FILE_NETMON_1_x */
265         { "Microsoft Network Monitor 1.x", "netmon1",
266           netmon_dump_can_write_encap, netmon_dump_open },
267
268         /* WTAP_FILE_NETMON_2_x */
269         { "Microsoft Network Monitor 2.x", NULL,
270           NULL, NULL },
271
272         /* WTAP_FILE_NETXRAY_1_0 */
273         { "Cinco Networks NetXRay", NULL,
274           NULL, NULL },
275
276         /* WTAP_FILE_NETXRAY_1_1 */
277         { "Network Associates Sniffer (Windows-based) 1.1", "ngwsniffer_1_1",
278           netxray_dump_can_write_encap, netxray_dump_open_1_1 },
279
280         /* WTAP_FILE_NETXRAY_2_00x */
281         { "Network Associates Sniffer (Windows-based) 2.00x", NULL,
282           NULL, NULL },
283
284         /* WTAP_FILE_RADCOM */
285         { "RADCOM WAN/LAN analyzer", NULL,
286           NULL, NULL },
287
288         /* WTAP_FILE_ASCEND */
289         { "Lucent/Ascend access server trace", NULL,
290           NULL, NULL },
291
292         /* WTAP_FILE_NETTL */
293         { "HP-UX nettl trace", NULL,
294           NULL, NULL },
295
296         /* WTAP_FILE_TOSHIBA */
297         { "Toshiba Compact ISDN Router snoop trace", NULL,
298           NULL, NULL },
299
300         /* WTAP_FILE_I4BTRACE */
301         { "I4B ISDN trace", NULL,
302           NULL, NULL },
303
304 };
305
306 /* Name that should be somewhat descriptive. */
307 const char *wtap_file_type_string(int filetype)
308 {
309         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES) {
310                 g_error("Unknown capture file type %d", filetype);
311                 return NULL;
312         } else
313                 return dump_open_table[filetype].name;
314 }
315
316 /* Name to use in, say, a command-line flag specifying the type. */
317 const char *wtap_file_type_short_string(int filetype)
318 {
319         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES)
320                 return NULL;
321         else
322                 return dump_open_table[filetype].short_name;
323 }
324
325 /* Translate a short name to a capture file type. */
326 int wtap_short_string_to_file_type(const char *short_name)
327 {
328         int filetype;
329
330         for (filetype = 0; filetype < WTAP_NUM_FILE_TYPES; filetype++) {
331                 if (dump_open_table[filetype].short_name != NULL &&
332                     strcmp(short_name, dump_open_table[filetype].short_name) == 0)
333                         return filetype;
334         }
335         return -1;      /* no such file type, or we can't write it */
336 }
337
338 gboolean wtap_dump_can_open(int filetype)
339 {
340         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
341             || dump_open_table[filetype].dump_open == NULL)
342                 return FALSE;
343
344         return TRUE;
345 }
346
347 gboolean wtap_dump_can_write_encap(int filetype, int encap)
348 {
349         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
350             || dump_open_table[filetype].can_write_encap == NULL)
351                 return FALSE;
352
353         if ((*dump_open_table[filetype].can_write_encap)(filetype, encap) != 0)
354                 return FALSE;
355
356         return TRUE;
357 }
358
359 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype,
360     int encap, int snaplen, int *err);
361
362 wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
363                                 int snaplen, int *err)
364 {
365         FILE *fh;
366
367         /* In case "fopen()" fails but doesn't set "errno", set "errno"
368            to a generic "the open failed" error. */
369         errno = WTAP_ERR_CANT_OPEN;
370         fh = fopen(filename, "wb");
371         if (fh == NULL) {
372                 *err = errno;
373                 return NULL;    /* can't create file */
374         }
375         return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
376 }
377
378 wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
379                                 int *err)
380 {
381         FILE *fh;
382
383         /* In case "fopen()" fails but doesn't set "errno", set "errno"
384            to a generic "the open failed" error. */
385         errno = WTAP_ERR_CANT_OPEN;
386         fh = fdopen(fd, "wb");
387         if (fh == NULL) {
388                 *err = errno;
389                 return NULL;    /* can't create standard I/O stream */
390         }
391         return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
392 }
393
394 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype, int encap,
395                                         int snaplen, int *err)
396 {
397         wtap_dumper *wdh;
398
399         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
400             || dump_open_table[filetype].dump_open == NULL) {
401                 /* Invalid type, or type we don't know how to write. */
402                 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
403                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
404                    will be closed if we can't write that file type. */
405                 fclose(fh);
406                 return NULL;
407         }
408
409         /* OK, we know how to write that type; can we write the specified
410            encapsulation type? */
411         *err = (*dump_open_table[filetype].can_write_encap)(filetype, encap);
412         if (*err != 0) {
413                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
414                    will be closed if we can't write that encapsulation type. */
415                 fclose(fh);
416                 return NULL;
417         }
418
419         /* OK, we can write the specified encapsulation type.  Allocate
420            a data structure for the output stream. */
421         wdh = g_malloc(sizeof (wtap_dumper));
422         if (wdh == NULL) {
423                 *err = errno;
424                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
425                    will be closed if the malloc fails. */
426                 fclose(fh);
427                 return NULL;
428         }
429         wdh->fh = fh;
430         wdh->file_type = filetype;
431         wdh->snaplen = snaplen;
432         wdh->encap = encap;
433         wdh->dump.opaque = NULL;
434         wdh->subtype_write = NULL;
435         wdh->subtype_close = NULL;
436
437         /* Now try to open the file for writing. */
438         if (!(*dump_open_table[filetype].dump_open)(wdh, err)) {
439                 /* The attempt failed. */
440                 g_free(wdh);
441                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
442                    will be closed if the open fails. */
443                 fclose(fh);
444                 return NULL;
445         }
446
447         return wdh;     /* success! */
448 }
449
450 FILE* wtap_dump_file(wtap_dumper *wdh)
451 {
452         return wdh->fh;
453 }
454
455 gboolean wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
456     const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err)
457 {
458         return (wdh->subtype_write)(wdh, phdr, pseudo_header, pd, err);
459 }
460
461 gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
462 {
463         gboolean ret = TRUE;
464
465         if (wdh->subtype_close != NULL) {
466                 /* There's a close routine for this dump stream. */
467                 if (!(wdh->subtype_close)(wdh, err))
468                         ret = FALSE;
469         }
470         errno = WTAP_ERR_CANT_CLOSE;
471         if (fclose(wdh->fh) == EOF) {
472                 if (ret) {
473                         /* The per-format close function succeeded,
474                            but the fclose didn't.  Save the reason
475                            why, if our caller asked for it. */
476                         if (err != NULL)
477                                 *err = errno;
478                 }
479                 ret = FALSE;
480         }
481         if (wdh->dump.opaque != NULL)
482                 g_free(wdh->dump.opaque);
483         g_free(wdh);
484         return ret;
485 }