In the final scene of the horror movie, just when you think the monster
[obnox/wireshark/wip.git] / wiretap / file.c
index 742cddcdce36c7057eeabd8f8bab89de44083f1d..c95eee5ceff5efcd70203dd0f2c9bd6bc8d6050a 100644 (file)
@@ -1,9 +1,9 @@
 /* file.c
  *
- * $Id: file.c,v 1.27 1999/10/31 17:46:06 gram Exp $
+ * $Id: file.c,v 1.56 2000/07/26 06:04:32 guy Exp $
  *
  * Wiretap Library
- * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
+ * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+
 #include <stdio.h>
 #include <fcntl.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <errno.h>
-#include "wtap.h"
-#include "file.h"
+
+#ifdef HAVE_IO_H
+#include <io.h>        /* open/close on win32 */
+#endif
+
+#include "wtap-int.h"
+#include "file_wrappers.h"
 #include "buffer.h"
 #include "lanalyzer.h"
 #include "ngsniffer.h"
@@ -43,6 +49,7 @@
 #include "netmon.h"
 #include "netxray.h"
 #include "toshiba.h"
+#include "i4btrace.h"
 
 /* The open_file_* routines should return:
  *
@@ -86,18 +93,27 @@ static int (*open_routines[])(wtap *, int *) = {
         */
        ascend_open,
        toshiba_open,
+       i4btrace_open,
 };
 
-int wtap_def_seek_read (FILE *fh, int seek_off, guint8 *pd, int len)
+int wtap_def_seek_read(wtap *wth, int seek_off,
+       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len)
 {
-       file_seek(fh, seek_off, SEEK_SET);
-       return file_read(pd, sizeof(guint8), len, fh);
+       file_seek(wth->random_fh, seek_off, SEEK_SET);
+
+       return file_read(pd, sizeof(guint8), len, wth->random_fh);
 }
 
 #define        N_FILE_TYPES    (sizeof open_routines / sizeof open_routines[0])
 
-/* Opens a file and prepares a wtap struct */
-wtap* wtap_open_offline(const char *filename, int *err)
+/* Opens a file and prepares a wtap struct.
+   If "do_random" is TRUE, it opens the file twice; the second open
+   allows the application to do random-access I/O without moving
+   the seek offset for sequential I/O, which is used by Ethereal
+   so that it can do sequential I/O to a capture file that's being
+   written to as new packets arrive independently of random I/O done
+   to display protocol trees for packets when they're selected. */
+wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random)
 {
        struct stat statb;
        wtap    *wth;
@@ -110,34 +126,55 @@ wtap* wtap_open_offline(const char *filename, int *err)
        }
 #ifndef WIN32
        if (! S_ISREG(statb.st_mode) && ! S_ISFIFO(statb.st_mode)) {
-               *err = WTAP_ERR_NOT_REGULAR_FILE;
+               if (S_ISDIR(statb.st_mode))
+                       *err = EISDIR;
+               else
+                       *err = WTAP_ERR_NOT_REGULAR_FILE;
                return NULL;
        }
 #endif
 
        errno = ENOMEM;
-       wth = (wtap*)malloc(sizeof(wtap));
+       wth = g_malloc(sizeof(wtap));
        if (wth == NULL) {
                *err = errno;
                return NULL;
        }
 
+/* Win32 needs the O_BINARY flag for open() */
+#ifndef O_BINARY
+#define O_BINARY       0
+#endif
+
        /* Open the file */
        errno = WTAP_ERR_CANT_OPEN;
-       if (!(wth->fd = open(filename, O_RDONLY))) {
+       wth->fd = open(filename, O_RDONLY|O_BINARY);
+       if (wth->fd < 0) {
                *err = errno;
-               free(wth);
+               g_free(wth);
                return NULL;
        }
        if (!(wth->fh = filed_open(wth->fd, "rb"))) {
                *err = errno;
-               free(wth);
+               g_free(wth);
                return NULL;
        }
 
+       if (do_random) {
+               if (!(wth->random_fh = file_open(filename, "rb"))) {
+                       *err = errno;
+                       file_close(wth->fh);
+                       g_free(wth);
+                       return NULL;
+               }
+       } else
+               wth->random_fh = NULL;
+
        /* initialization */
        wth->file_encap = WTAP_ENCAP_UNKNOWN;
        wth->data_offset = 0;
+       wth->subtype_sequential_close = NULL;
+       wth->subtype_close = NULL;
 
        /* Try all file types */
        for (i = 0; i < N_FILE_TYPES; i++) {
@@ -145,11 +182,8 @@ wtap* wtap_open_offline(const char *filename, int *err)
 
                case -1:
                        /* I/O error - give up */
-                       /* XXX - why pass err to open_routines[i]() if err is
-                        * overwritten here? */
-                       *err = errno;
                        file_close(wth->fh);
-                       free(wth);
+                       g_free(wth);
                        return NULL;
 
                case 0:
@@ -163,8 +197,10 @@ wtap* wtap_open_offline(const char *filename, int *err)
        }
 
        /* Well, it's not one of the types of file we know about. */
+       if (wth->random_fh != NULL)
+               file_close(wth->random_fh);
        file_close(wth->fh);
-       free(wth);
+       g_free(wth);
        *err = WTAP_ERR_FILE_UNKNOWN_FORMAT;
        return NULL;
 
@@ -174,6 +210,155 @@ success:
        return wth;
 }
 
+/* Table of the file types we know about. */
+const static struct file_type_info {
+       const char *name;
+       const char *short_name;
+       int     (*can_write_encap)(int, int);
+       int     (*dump_open)(wtap_dumper *, int *);
+} dump_open_table[WTAP_NUM_FILE_TYPES] = {
+       /* WTAP_FILE_UNKNOWN */
+       { NULL, NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_WTAP */
+       { "Wiretap (Ethereal)", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_PCAP */
+       { "libpcap (tcpdump, Ethereal, etc.)", "libpcap",
+         libpcap_dump_can_write_encap, libpcap_dump_open },
+
+       /* WTAP_FILE_PCAP_SS990417 */
+       { "Red Hat Linux 6.1 libpcap (tcpdump)", "rh6_1libpcap",
+         libpcap_dump_can_write_encap, libpcap_dump_open },
+
+       /* WTAP_FILE_PCAP_SS990915 */
+       { "SuSE Linux 6.3 libpcap (tcpdump)", "suse6_3libpcap",
+         libpcap_dump_can_write_encap, libpcap_dump_open },
+
+       /* WTAP_FILE_PCAP_SS991029 */
+       { "modified libpcap (tcpdump)", "modlibpcap",
+         libpcap_dump_can_write_encap, libpcap_dump_open },
+
+       /* WTAP_FILE_LANALYZER */
+       { "Novell LANalyzer", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_NGSNIFFER_UNCOMPRESSED */
+       { "Network Associates Sniffer (DOS-based)", "ngsniffer",
+         ngsniffer_dump_can_write_encap, ngsniffer_dump_open },
+
+       /* WTAP_FILE_NGSNIFFER_COMPRESSED */
+       { "Network Associates Sniffer (DOS-based), compressed", "ngsniffer_comp",
+         NULL, NULL },
+
+       /* WTAP_FILE_SNOOP */
+       { "Sun snoop", "snoop",
+         snoop_dump_can_write_encap, snoop_dump_open },
+
+       /* WTAP_FILE_IPTRACE_1_0 */
+       { "AIX iptrace 1.0", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_IPTRACE_2_0 */
+       { "AIX iptrace 2.0", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_NETMON_1_x */
+       { "Microsoft Network Monitor 1.x", "netmon1",
+         netmon_dump_can_write_encap, netmon_dump_open },
+
+       /* WTAP_FILE_NETMON_2_x */
+       { "Microsoft Network Monitor 2.x", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_NETXRAY_1_0 */
+       { "Cinco Networks NetXRay", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_NETXRAY_1_1 */
+       { "Network Associates Sniffer (Windows-based) 1.1", "ngwsniffer_1_1",
+         netxray_dump_can_write_encap, netxray_dump_open_1_1 },
+
+       /* WTAP_FILE_NETXRAY_2_00x */
+       { "Network Associates Sniffer (Windows-based) 2.00x", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_RADCOM */
+       { "RADCOM WAN/LAN analyzer", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_ASCEND */
+       { "Lucent/Ascend access server trace", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_NETTL */
+       { "HP-UX nettl trace", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_TOSHIBA */
+       { "Toshiba Compact ISDN Router snoop trace", NULL,
+         NULL, NULL },
+
+       /* WTAP_FILE_I4BTRACE */
+       { "I4B ISDN trace", NULL,
+         NULL, NULL },
+
+};
+
+/* Name that should be somewhat descriptive. */
+const char *wtap_file_type_string(int filetype)
+{
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES) {
+               g_error("Unknown capture file type %d", filetype);
+               return NULL;
+       } else
+               return dump_open_table[filetype].name;
+}
+
+/* Name to use in, say, a command-line flag specifying the type. */
+const char *wtap_file_type_short_string(int filetype)
+{
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES)
+               return NULL;
+       else
+               return dump_open_table[filetype].short_name;
+}
+
+/* Translate a short name to a capture file type. */
+int wtap_short_string_to_file_type(const char *short_name)
+{
+       int filetype;
+
+       for (filetype = 0; filetype < WTAP_NUM_FILE_TYPES; filetype++) {
+               if (dump_open_table[filetype].short_name != NULL &&
+                   strcmp(short_name, dump_open_table[filetype].short_name) == 0)
+                       return filetype;
+       }
+       return -1;      /* no such file type, or we can't write it */
+}
+
+gboolean wtap_dump_can_open(int filetype)
+{
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
+           || dump_open_table[filetype].dump_open == NULL)
+               return FALSE;
+
+       return TRUE;
+}
+
+gboolean wtap_dump_can_write_encap(int filetype, int encap)
+{
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
+           || dump_open_table[filetype].can_write_encap == NULL)
+               return FALSE;
+
+       if ((*dump_open_table[filetype].can_write_encap)(filetype, encap) != 0)
+               return FALSE;
+
+       return TRUE;
+}
 
 static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype,
     int encap, int snaplen, int *err);
@@ -186,7 +371,7 @@ wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
        /* In case "fopen()" fails but doesn't set "errno", set "errno"
           to a generic "the open failed" error. */
        errno = WTAP_ERR_CANT_OPEN;
-       fh = fopen(filename, "w");
+       fh = fopen(filename, "wb");
        if (fh == NULL) {
                *err = errno;
                return NULL;    /* can't create file */
@@ -202,7 +387,7 @@ wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
        /* In case "fopen()" fails but doesn't set "errno", set "errno"
           to a generic "the open failed" error. */
        errno = WTAP_ERR_CANT_OPEN;
-       fh = fdopen(fd, "w");
+       fh = fdopen(fd, "wb");
        if (fh == NULL) {
                *err = errno;
                return NULL;    /* can't create standard I/O stream */
@@ -215,7 +400,29 @@ static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype, int encap,
 {
        wtap_dumper *wdh;
 
-       wdh = malloc(sizeof (wtap_dumper));
+       if (filetype < 0 || filetype >= WTAP_NUM_FILE_TYPES
+           || dump_open_table[filetype].dump_open == NULL) {
+               /* Invalid type, or type we don't know how to write. */
+               *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
+               /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
+                  will be closed if we can't write that file type. */
+               fclose(fh);
+               return NULL;
+       }
+
+       /* OK, we know how to write that type; can we write the specified
+          encapsulation type? */
+       *err = (*dump_open_table[filetype].can_write_encap)(filetype, encap);
+       if (*err != 0) {
+               /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
+                  will be closed if we can't write that encapsulation type. */
+               fclose(fh);
+               return NULL;
+       }
+
+       /* OK, we can write the specified encapsulation type.  Allocate
+          a data structure for the output stream. */
+       wdh = g_malloc(sizeof (wtap_dumper));
        if (wdh == NULL) {
                *err = errno;
                /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
@@ -227,25 +434,21 @@ static wtap_dumper* wtap_dump_open_common(FILE *fh, int filetype, int encap,
        wdh->file_type = filetype;
        wdh->snaplen = snaplen;
        wdh->encap = encap;
-
-       switch (filetype) {
-
-       case WTAP_FILE_PCAP:
-               if (!libpcap_dump_open(wdh, err))
-                       goto fail;
-               break;
-
-       default:
-               /* We currently only support dumping "libpcap" files */
-               *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
-               goto fail;
+       wdh->dump.opaque = NULL;
+       wdh->subtype_write = NULL;
+       wdh->subtype_close = NULL;
+
+       /* Now try to open the file for writing. */
+       if (!(*dump_open_table[filetype].dump_open)(wdh, err)) {
+               /* The attempt failed. */
+               g_free(wdh);
+               /* NOTE: this means the FD handed to "wtap_dump_fdopen()"
+                  will be closed if the open fails. */
+               fclose(fh);
+               return NULL;
        }
-       return wdh;
 
-fail:
-       free(wdh);
-       fclose(fh);
-       return NULL;    /* XXX - provide a reason why we failed */
+       return wdh;     /* success! */
 }
 
 FILE* wtap_dump_file(wtap_dumper *wdh)
@@ -253,18 +456,21 @@ FILE* wtap_dump_file(wtap_dumper *wdh)
        return wdh->fh;
 }
 
-int wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const u_char *pd, int *err)
+gboolean wtap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
+    const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err)
 {
-       return (wdh->subtype_write)(wdh, phdr, pd, err);
+       return (wdh->subtype_write)(wdh, phdr, pseudo_header, pd, err);
 }
 
-int wtap_dump_close(wtap_dumper *wdh, int *err)
+gboolean wtap_dump_close(wtap_dumper *wdh, int *err)
 {
-       int ret = 1;
+       gboolean ret = TRUE;
 
-       if (!(wdh->subtype_close)(wdh, err))
-               ret = 0;
+       if (wdh->subtype_close != NULL) {
+               /* There's a close routine for this dump stream. */
+               if (!(wdh->subtype_close)(wdh, err))
+                       ret = FALSE;
+       }
        errno = WTAP_ERR_CANT_CLOSE;
        if (fclose(wdh->fh) == EOF) {
                if (ret) {
@@ -274,46 +480,10 @@ int wtap_dump_close(wtap_dumper *wdh, int *err)
                        if (err != NULL)
                                *err = errno;
                }
-               ret = 0;
+               ret = FALSE;
        }
-       free(wdh);
+       if (wdh->dump.opaque != NULL)
+               g_free(wdh->dump.opaque);
+       g_free(wdh);
        return ret;
 }
-
-/*
- * Routine to return a Wiretap error code (0 for no error, an errno
- * for a file error, or a WTAP_ERR_ code for other errors) for an
- * I/O stream.
- */
-#ifdef HAVE_LIBZ
-int
-file_error(void *fh)
-{
-       int errnum;
-
-       gzerror(fh, &errnum);
-       switch (errnum) {
-
-       case Z_OK:              /* no error */
-               return 0;
-
-       case Z_STREAM_END:      /* EOF - not an error */
-               return 0;
-
-       case Z_ERRNO:           /* file I/O error */
-               return errno;
-
-       default:
-               return WTAP_ERR_ZLIB + errnum;
-       }
-}
-#else /* HAVE_LIBZ */
-int
-file_error(FILE *fh)
-{
-       if (ferror(fh))
-               return errno;
-       else
-               return 0;
-}
-#endif /* HAVE_LIBZ */