Some cleanup of the RADIUS AVP output (Tunnel Tagging Tag), and a bit of
[obnox/wireshark/wip.git] / wiretap / file.c
1 /* file.c
2  *
3  * $Id: file.c,v 1.48 2000/02/03 06:29:07 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.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 (FILE *fh, int seek_off, guint8 *pd, int len)
100 {
101         file_seek(fh, seek_off, SEEK_SET);
102         return file_read(pd, sizeof(guint8), len, fh);
103 }
104
105 #define N_FILE_TYPES    (sizeof open_routines / sizeof open_routines[0])
106
107 /* Opens a file and prepares a wtap struct */
108 wtap* wtap_open_offline(const char *filename, int *err)
109 {
110         struct stat statb;
111         wtap    *wth;
112         int     i;
113
114         /* First, make sure the file is valid */
115         if (stat(filename, &statb) < 0) {
116                 *err = errno;
117                 return NULL;
118         }
119 #ifndef WIN32
120         if (! S_ISREG(statb.st_mode) && ! S_ISFIFO(statb.st_mode)) {
121                 *err = WTAP_ERR_NOT_REGULAR_FILE;
122                 return NULL;
123         }
124 #endif
125
126         errno = ENOMEM;
127         wth = g_malloc(sizeof(wtap));
128         if (wth == NULL) {
129                 *err = errno;
130                 return NULL;
131         }
132
133 /* Win32 needs the O_BINARY flag for open() */
134 #ifndef O_BINARY
135 #define O_BINARY        0
136 #endif
137
138         /* Open the file */
139         errno = WTAP_ERR_CANT_OPEN;
140         if (!(wth->fd = open(filename, O_RDONLY|O_BINARY))) {
141                 *err = errno;
142                 g_free(wth);
143                 return NULL;
144         }
145         if (!(wth->fh = filed_open(wth->fd, "rb"))) {
146                 *err = errno;
147                 g_free(wth);
148                 return NULL;
149         }
150
151         /* initialization */
152         wth->file_encap = WTAP_ENCAP_UNKNOWN;
153         wth->data_offset = 0;
154
155         /* Try all file types */
156         for (i = 0; i < N_FILE_TYPES; i++) {
157                 switch ((*open_routines[i])(wth, err)) {
158
159                 case -1:
160                         /* I/O error - give up */
161                         file_close(wth->fh);
162                         g_free(wth);
163                         return NULL;
164
165                 case 0:
166                         /* No I/O error, but not that type of file */
167                         break;
168
169                 case 1:
170                         /* We found the file type */
171                         goto success;
172                 }
173         }
174
175         /* Well, it's not one of the types of file we know about. */
176         file_close(wth->fh);
177         g_free(wth);
178         *err = WTAP_ERR_FILE_UNKNOWN_FORMAT;
179         return NULL;
180
181 success:
182         wth->frame_buffer = g_malloc(sizeof(struct Buffer));
183         buffer_init(wth->frame_buffer, 1500);
184         return wth;
185 }
186
187 /* Table of the file types we know about. */
188 const static struct file_type_info {
189         const char *name;
190         const char *short_name;
191         int     (*can_write_encap)(int, int);
192         int     (*dump_open)(wtap_dumper *, int *);
193 } dump_open_table[WTAP_NUM_FILE_TYPES] = {
194         /* WTAP_FILE_UNKNOWN */
195         { NULL, NULL,
196           NULL, NULL },
197
198         /* WTAP_FILE_WTAP */
199         { "Wiretap (Ethereal)", NULL,
200           NULL, NULL },
201
202         /* WTAP_FILE_PCAP */
203         { "libpcap (tcpdump, Ethereal, etc.)", "libpcap",
204           libpcap_dump_can_write_encap, libpcap_dump_open },
205
206         /* WTAP_FILE_PCAP_MODIFIED */
207         { "modified libpcap (tcpdump)", "modlibpcap",
208           libpcap_dump_can_write_encap, libpcap_dump_open },
209
210         /* WTAP_FILE_PCAP_RH_6_1 */
211         { "Red Hat Linux 6.1 libpcap (tcpdump)", "rh6_1libpcap",
212           libpcap_dump_can_write_encap, libpcap_dump_open },
213
214         /* WTAP_FILE_LANALYZER */
215         { "Novell LANalyzer", NULL,
216           NULL, NULL },
217
218         /* WTAP_FILE_NGSNIFFER */
219         { "Network Associates Sniffer (DOS-based)", "ngsniffer",
220           ngsniffer_dump_can_write_encap, ngsniffer_dump_open },
221
222         /* WTAP_FILE_SNOOP */
223         { "Sun snoop", "snoop",
224           snoop_dump_can_write_encap, snoop_dump_open },
225
226         /* WTAP_FILE_IPTRACE_1_0 */
227         { "AIX iptrace 1.0", NULL,
228           NULL, NULL },
229
230         /* WTAP_FILE_IPTRACE_2_0 */
231         { "AIX iptrace 2.0", NULL,
232           NULL, NULL },
233
234         /* WTAP_FILE_NETMON_1_x */
235         { "Microsoft Network Monitor 1.x", "netmon1",
236           netmon_dump_can_write_encap, netmon_dump_open },
237
238         /* WTAP_FILE_NETMON_2_x */
239         { "Microsoft Network Monitor 2.x", NULL,
240           NULL, NULL },
241
242         /* WTAP_FILE_NETXRAY_1_0 */
243         { "Cinco Networks NetXRay", NULL,
244           NULL, NULL },
245
246         /* WTAP_FILE_NETXRAY_1_1 */
247         { "Network Associates Sniffer (Windows-based) 1.1", "ngwsniffer_1_1",
248           netxray_dump_can_write_encap, netxray_dump_open_1_1 },
249
250         /* WTAP_FILE_NETXRAY_2_001 */
251         { "Network Associates Sniffer (Windows-based) 2.001", NULL,
252           NULL, NULL },
253
254         /* WTAP_FILE_RADCOM */
255         { "RADCOM WAN/LAN analyzer", NULL,
256           NULL, NULL },
257
258         /* WTAP_FILE_ASCEND */
259         { "Lucent/Ascend access server trace", NULL,
260           NULL, NULL },
261
262         /* WTAP_FILE_NETTL */
263         { "HP-UX nettl trace", NULL,
264           NULL, NULL },
265
266         /* WTAP_FILE_TOSHIBA */
267         { "Toshiba Compact ISDN Router snoop trace", NULL,
268           NULL, NULL },
269
270         /* WTAP_FILE_I4BTRACE */
271         { "I4B ISDN trace", NULL,
272           NULL, NULL },
273
274 };
275
276 /* Name that should be somewhat descriptive. */
277 const char *wtap_file_type_string(int filetype)
278 {
279         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES) {
280                 g_error("Unknown capture file type %d", filetype);
281                 return NULL;
282         } else
283                 return dump_open_table[filetype].name;
284 }
285
286 /* Name to use in, say, a command-line flag specifying the type. */
287 const char *wtap_file_type_short_string(int filetype)
288 {
289         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES)
290                 return NULL;
291         else
292                 return dump_open_table[filetype].short_name;
293 }
294
295 /* Translate a short name to a capture file type. */
296 int wtap_short_string_to_file_type(const char *short_name)
297 {
298         int filetype;
299
300         for (filetype = 0; filetype < WTAP_NUM_FILE_TYPES; filetype++) {
301                 if (dump_open_table[filetype].short_name != NULL &&
302                     strcmp(short_name, dump_open_table[filetype].short_name) == 0)
303                         return filetype;
304         }
305         return -1;      /* no such file type, or we can't write it */
306 }
307
308 gboolean wtap_dump_can_open(int filetype)
309 {
310         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
311             || dump_open_table[filetype].dump_open == NULL)
312                 return FALSE;
313
314         return TRUE;
315 }
316
317 gboolean wtap_dump_can_write_encap(int filetype, int encap)
318 {
319         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
320             || dump_open_table[filetype].can_write_encap == NULL)
321                 return FALSE;
322
323         if ((*dump_open_table[filetype].can_write_encap)(filetype, encap) != 0)
324                 return FALSE;
325
326         return TRUE;
327 }
328
329 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype,
330     int encap, int snaplen, int *err);
331
332 wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
333                                 int snaplen, int *err)
334 {
335         FILE *fh;
336
337         /* In case "fopen()" fails but doesn't set "errno", set "errno"
338            to a generic "the open failed" error. */
339         errno = WTAP_ERR_CANT_OPEN;
340         fh = fopen(filename, "wb");
341         if (fh == NULL) {
342                 *err = errno;
343                 return NULL;    /* can't create file */
344         }
345         return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
346 }
347
348 wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
349                                 int *err)
350 {
351         FILE *fh;
352
353         /* In case "fopen()" fails but doesn't set "errno", set "errno"
354            to a generic "the open failed" error. */
355         errno = WTAP_ERR_CANT_OPEN;
356         fh = fdopen(fd, "wb");
357         if (fh == NULL) {
358                 *err = errno;
359                 return NULL;    /* can't create standard I/O stream */
360         }
361         return wtap_dump_open_common(fh, filetype, encap, snaplen, err);
362 }
363
364 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype, int encap,
365                                         int snaplen, int *err)
366 {
367         wtap_dumper *wdh;
368
369         if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
370             || dump_open_table[filetype].dump_open == NULL) {
371                 /* Invalid type, or type we don't know how to write. */
372                 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
373                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
374                    will be closed if we can't write that file type. */
375                 fclose(fh);
376                 return NULL;
377         }
378
379         /* OK, we know how to write that type; can we write the specified
380            encapsulation type? */
381         *err = (*dump_open_table[filetype].can_write_encap)(filetype, encap);
382         if (*err != 0) {
383                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
384                    will be closed if we can't write that encapsulation type. */
385                 fclose(fh);
386                 return NULL;
387         }
388
389         /* OK, we can write the specified encapsulation type.  Allocate
390            a data structure for the output stream. */
391         wdh = g_malloc(sizeof (wtap_dumper));
392         if (wdh == NULL) {
393                 *err = errno;
394                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
395                    will be closed if the malloc fails. */
396                 fclose(fh);
397                 return NULL;
398         }
399         wdh->fh = fh;
400         wdh->file_type = filetype;
401         wdh->snaplen = snaplen;
402         wdh->encap = encap;
403         wdh->private.opaque = NULL;
404         wdh->subtype_write = NULL;
405         wdh->subtype_close = NULL;
406
407         /* Now try to open the file for writing. */
408         if (!(*dump_open_table[filetype].dump_open)(wdh, err)) {
409                 /* The attempt failed. */
410                 g_free(wdh);
411                 /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
412                    will be closed if the open fails. */
413                 fclose(fh);
414                 return NULL;
415         }
416
417         return wdh;     /* success! */
418 }
419
420 FILE* wtap_dump_file(wtap_dumper *wdh)
421 {
422         return wdh->fh;
423 }
424
425 gboolean wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
426     const u_char *pd, int *err)
427 {
428         return (wdh->subtype_write)(wdh, phdr, pd, err);
429 }
430
431 gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
432 {
433         gboolean ret = TRUE;
434
435         if (wdh->subtype_close != NULL) {
436                 /* There's a close routine for this dump stream. */
437                 if (!(wdh->subtype_close)(wdh, err))
438                         ret = FALSE;
439         }
440         errno = WTAP_ERR_CANT_CLOSE;
441         if (fclose(wdh->fh) == EOF) {
442                 if (ret) {
443                         /* The per-format close function succeeded,
444                            but the fclose didn't.  Save the reason
445                            why, if our caller asked for it. */
446                         if (err != NULL)
447                                 *err = errno;
448                 }
449                 ret = FALSE;
450         }
451         if (wdh->private.opaque != NULL)
452                 g_free(wdh->private.opaque);
453         g_free(wdh);
454         return ret;
455 }