Have the per-capture-file-type open routines "wtap_open_offline()" calls
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 19 Aug 1999 05:31:38 +0000 (05:31 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 19 Aug 1999 05:31:38 +0000 (05:31 +0000)
return 1 on success, -1 if they got an error, and 0 if the file isn't of
the type that file is checking for, and supply an error code if they
return -1; have "wtap_open_offline()" use that error code.  Also, have
the per-capture-file-type open routines treat errors accessing the file
as errors, and return -1, rather than just returning 0 so that we try
another file type.

Have the per-capture-file-type read routines "wtap_loop()" calls return
-1 and supply an error code on error (and not, as they did in some
cases, call "g_error()" and abort), and have "wtap_loop()", if the read
routine returned an error, return FALSE (and pass an error-code-pointer
argument onto the read routines, so they fill it in), and return TRUE on
success.

Add some new error codes for them to return.

Now that "wtap_loop()" can return a success/failure indication and an
error code, in "read_cap_file()" put up a message box if we get an error
reading the file, and return the error code.

Handle the additional errors we can get when opening a capture file.

If the attempt to open a capture file succeeds, but the attempt to read
it fails, don't treat that as a complete failure - we may have managed
to read some of the capture file, and we should display what we managed
to read.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@516 f5534014-38df-0310-8fa8-9805f1628bb7

21 files changed:
capture.c
ethereal.c
file.c
wiretap/file.c
wiretap/iptrace.c
wiretap/iptrace.h
wiretap/lanalyzer.c
wiretap/lanalyzer.h
wiretap/libpcap.c
wiretap/libpcap.h
wiretap/netmon.c
wiretap/netmon.h
wiretap/netxray.c
wiretap/netxray.h
wiretap/ngsniffer.c
wiretap/radcom.c
wiretap/radcom.h
wiretap/snoop.c
wiretap/snoop.h
wiretap/wtap.c
wiretap/wtap.h

index 803a720ba49c198d5302147f52b4993304ba5bc4..b077e6beda8a85b81a07104ee43d0be76788c64b 100644 (file)
--- a/capture.c
+++ b/capture.c
@@ -1,7 +1,7 @@
 /* capture.c
  * Routines for packet capture windows
  *
- * $Id: capture.c,v 1.55 1999/08/18 17:08:39 guy Exp $
+ * $Id: capture.c,v 1.56 1999/08/19 05:31:21 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -702,10 +702,9 @@ fail:
     if ((err = open_cap_file(cf.save_file, &cf)) == 0) {
       /* Set the read filter to NULL. */
       cf.rfcode = NULL;
-      if ((err = read_cap_file(&cf)) == 0) {
-        set_menu_sensitivity("/File/Save", TRUE);
-        set_menu_sensitivity("/File/Save As...", FALSE);
-      }
+      err = read_cap_file(&cf);
+      set_menu_sensitivity("/File/Save", TRUE);
+      set_menu_sensitivity("/File/Save As...", FALSE);
     }
   }
 }
index 82414c299122ec5f0ccc371e18f7bcfcd188f0aa..5b9d14347c2d735859ca22ff0ba3fc155403439d 100644 (file)
@@ -1,6 +1,6 @@
 /* ethereal.c
  *
- * $Id: ethereal.c,v 1.95 1999/08/18 04:17:27 guy Exp $
+ * $Id: ethereal.c,v 1.96 1999/08/19 05:31:19 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -485,27 +485,25 @@ file_open_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
   gtk_widget_hide(GTK_WIDGET (fs));
   gtk_widget_destroy(GTK_WIDGET (fs));
 
-  if ((err = read_cap_file(&cf)) == 0) {
-         /* The read succeeded.  Save the directory name; we can
-            write over cf_name. */
-         s = strrchr(cf_name, '/');
-         if (s && last_open_dir) {
-                 *s = '\0';
-                 if (strcmp(last_open_dir, cf_name) != 0) {
-                         g_free(last_open_dir);
-                         last_open_dir = g_strdup(cf_name);
-                 }
-         }
-         else if (s) { /* ! last_open_dir */
-                 *s = '\0';
+  err = read_cap_file(&cf);
+  /* Save the directory name; we can write over cf_name. */
+  s = strrchr(cf_name, '/');
+  if (s && last_open_dir) {
+         *s = '\0';
+         if (strcmp(last_open_dir, cf_name) != 0) {
+                 g_free(last_open_dir);
                  last_open_dir = g_strdup(cf_name);
          }
-         else {
-                 last_open_dir = NULL;
-         }
-         set_menu_sensitivity("/File/Save", FALSE);
-         set_menu_sensitivity("/File/Save As...", TRUE);
   }
+  else if (s) { /* ! last_open_dir */
+         *s = '\0';
+         last_open_dir = g_strdup(cf_name);
+  }
+  else {
+         last_open_dir = NULL;
+  }
+  set_menu_sensitivity("/File/Save", FALSE);
+  set_menu_sensitivity("/File/Save As...", TRUE);
   g_free(cf_name);
 }
 
@@ -567,8 +565,8 @@ file_save_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
        g_free(cf.save_file);
        cf.save_file = g_strdup(cf_name);
        cf.user_saved = 1;
-        if ((err = open_cap_file(cf_name, &cf)) == 0 &&
-           (err = read_cap_file(&cf)) == 0) {
+        if ((err = open_cap_file(cf_name, &cf)) == 0) {
+               err = read_cap_file(&cf);
                set_menu_sensitivity("/File/Save", FALSE);
                set_menu_sensitivity("/File/Save As...", TRUE);
        }
@@ -587,8 +585,8 @@ file_save_as_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
        g_free(cf.filename);
        cf.filename = g_strdup(cf_name);
        cf.user_saved = 1;
-        if ((err = open_cap_file(cf.filename, &cf)) == 0 &&
-           (err = read_cap_file(&cf)) == 0) {
+        if ((err = open_cap_file(cf.filename, &cf)) == 0) {
+               err = read_cap_file(&cf);
                set_menu_sensitivity("/File/Save", FALSE);
                set_menu_sensitivity("/File/Save As...", TRUE);
        }
@@ -606,7 +604,7 @@ file_reload_cmd_cb(GtkWidget *w, gpointer data) {
   cf.dfilter = g_strdup(gtk_entry_get_text(GTK_ENTRY(filter_te)));
   if (open_cap_file(cf.filename, &cf) == 0)
     read_cap_file(&cf);
-  /* XXX - change the menu if it fails? */
+  /* XXX - change the menu if the open fails? */
 }
 
 /* Run the current display filter on the current packet set, and
@@ -1475,9 +1473,8 @@ main(int argc, char *argv[])
     }
     if (!rfilter_parse_failed) {
       cf.rfcode = rfcode;
-      if ((err = open_cap_file(cf_name, &cf)) == 0)
+      if ((err = open_cap_file(cf_name, &cf)) == 0) {
         err = read_cap_file(&cf);
-      if (err == 0) {
         s = strrchr(cf_name, '/');
         if (s) {
           last_open_dir = cf_name;
diff --git a/file.c b/file.c
index 9967a5832c0dce1fdf0c05e8b9dd205d6d21748f..c31e8ba5dbc16c8813d1e40d2d32e5557d9b934e 100644 (file)
--- a/file.c
+++ b/file.c
@@ -1,7 +1,7 @@
 /* file.c
  * File I/O routines
  *
- * $Id: file.c,v 1.74 1999/08/15 23:40:33 gram Exp $
+ * $Id: file.c,v 1.75 1999/08/19 05:31:22 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -192,11 +192,13 @@ int
 read_cap_file(capture_file *cf) {
   gchar  *name_ptr, *load_msg, *load_fmt = " Loading: %s...";
   gchar  *done_fmt = " File: %s  Drops: %d";
-#if 0
-  gchar  *err_fmt  = " Error: Could not load '%s'";
-#endif
+  int     success;
+  int     err;
   gint    timeout;
   size_t  msg_len;
+  char   *errmsg;
+  char    errmsg_errno[1024+1];
+  gchar   err_str[PCAP_ERRBUF_SIZE];
 
   if ((name_ptr = (gchar *) strrchr(cf->filename, '/')) == NULL)
     name_ptr = cf->filename;
@@ -210,7 +212,7 @@ read_cap_file(capture_file *cf) {
   timeout = gtk_timeout_add(250, file_progress_cb, (gpointer) cf);
 
   freeze_clist(cf);
-  wtap_loop(cf->wth, 0, wtap_dispatch_cb, (u_char *) cf);
+  success = wtap_loop(cf->wth, 0, wtap_dispatch_cb, (u_char *) cf, &err);
   wtap_close(cf->wth);
   cf->wth = NULL;
   cf->fh = fopen(cf->filename, "r");
@@ -238,19 +240,39 @@ read_cap_file(capture_file *cf) {
   set_menu_sensitivity("/File/Print...", TRUE);
   set_menu_sensitivity("/Display/Options...", TRUE);
   set_menu_sensitivity("/Tools/Summary", TRUE);
-  return 0;
 
-#if 0
-  /* XXX - need to check whether the read succeeded. */
-fail:
-  msg_len = strlen(name_ptr) + strlen(err_fmt) + 2;
-  load_msg = g_malloc(msg_len);
-  snprintf(load_msg, msg_len, err_fmt, name_ptr);
-  gtk_statusbar_push(GTK_STATUSBAR(info_bar), file_ctx, load_msg);
-  g_free(load_msg);
-  close_cap_file(cf);  /* Close the capture file. */
-  return -1;
-#endif
+  if (!success) {
+    /* Put up a message box noting that the read failed somewhere along
+       the line.  Don't throw out the stuff we managed to read, though,
+       if any. */
+    switch (err) {
+
+    case WTAP_ERR_CANT_READ:
+      errmsg = "An attempt to read from the file failed for"
+               " some unknown reason.";
+      break;
+
+    case WTAP_ERR_SHORT_READ:
+      errmsg = "The capture file appears to have been cut short"
+               " in the middle of a packet.";
+      break;
+
+    default:
+      if (err < 0) {
+        sprintf(errmsg_errno, "An error occurred while reading the"
+                              " capture file: Error %d.", err);
+      } else {
+        sprintf(errmsg_errno, "An error occurred while reading the"
+                              " capture file: %s.", strerror(err));
+      }
+      errmsg = errmsg_errno;
+      break;
+    }
+    snprintf(err_str, PCAP_ERRBUF_SIZE, errmsg);
+    simple_dialog(ESD_TYPE_WARN, NULL, err_str);
+    return (err);
+  } else
+    return (0);
 }
 
 #ifdef HAVE_LIBPCAP
@@ -262,6 +284,7 @@ cap_file_input_cb (gpointer data, gint source, GdkInputCondition condition) {
   int  nread;
   int  to_read = 0;
   gboolean exit_loop = FALSE;
+  int  err;
 
   /* avoid reentrancy problems and stack overflow */
   gtk_input_remove(cap_input_id);
@@ -273,7 +296,8 @@ cap_file_input_cb (gpointer data, gint source, GdkInputCondition condition) {
        and stop capture (restore menu items) */
     gtk_clist_freeze(GTK_CLIST(packet_list));
 
-    wtap_loop(cf->wth, 0, wtap_dispatch_cb, (u_char *) cf);      
+    /* XXX - do something if this fails? */
+    wtap_loop(cf->wth, 0, wtap_dispatch_cb, (u_char *) cf, &err);
 
     thaw_clist(cf);
 
@@ -313,7 +337,8 @@ cap_file_input_cb (gpointer data, gint source, GdkInputCondition condition) {
   }
 
   gtk_clist_freeze(GTK_CLIST(packet_list));
-  wtap_loop(cf->wth, to_read, wtap_dispatch_cb, (u_char *) cf);      
+  /* XXX - do something if this fails? */
+  wtap_loop(cf->wth, to_read, wtap_dispatch_cb, (u_char *) cf, &err);
   gtk_clist_thaw(GTK_CLIST(packet_list));
 
   /* restore pipe handler */
@@ -993,9 +1018,23 @@ file_open_error_message(int err, int for_writing)
     break;
 
   case WTAP_ERR_FILE_UNKNOWN_FORMAT:
+  case WTAP_ERR_UNSUPPORTED:
+  case WTAP_ERR_BAD_RECORD:
     errmsg = "The file \"%s\" is not a capture file in a format Ethereal understands.";
     break;
 
+  case WTAP_ERR_CANT_OPEN:
+    if (for_writing)
+      errmsg = "The file \"%s\" could not be created for some unknown reason.";
+    else
+      errmsg = "The file \"%s\" could not be opened for some unknown reason.";
+    break;
+
+  case WTAP_ERR_SHORT_READ:
+    errmsg = "The file \"%s\" appears to have been cut short"
+             " in the middle of a packet.";
+    break;
+
   case ENOENT:
     if (for_writing)
       errmsg = "The path to the file \"%s\" does not exist.";
index cc53ea37ff804d0f3f85964ac69c9626bf9a60ac..f120b4ccf4c05757b8d399e8ccc3724fed777c79 100644 (file)
@@ -1,6 +1,6 @@
 /* file.c
  *
- * $Id: file.c,v 1.16 1999/08/18 17:49:34 guy Exp $
+ * $Id: file.c,v 1.17 1999/08/19 05:31:33 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
 #include "netmon.h"
 #include "netxray.h"
 
-/* The open_file_* routines should return the WTAP_FILE_* type
- * that they are checking for if the file is successfully recognized
- * as such. If the file is not of that type, the routine should return
- * WTAP_FILE_UNKNOWN */
+/* The open_file_* routines should return:
+ *
+ *     -1 on an I/O error;
+ *
+ *     1 if the file they're reading is one of the types it handles;
+ *
+ *     0 if the file they're reading isn't the type they're checking for.
+ *
+ * If the routine handles this type of file, it should set the "file_type"
+ * field in the "struct wtap" to the type of the file.
+ *
+ * XXX - I need to drag my damn ANSI C spec in to figure out how to
+ * declare a "const" array of pointers to functions; putting "const"
+ * right after "static" isn't the right answer, at least according
+ * to GCC, which whines if I do that.
+ */
+
+static int (*open_routines[])(wtap *, int *) = {
+       libpcap_open,
+       lanalyzer_open,
+       ngsniffer_open,
+       snoop_open,
+       iptrace_open,
+       netmon_open,
+       netxray_open,
+       radcom_open
+};
+
+#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)
 {
        struct stat statb;
        wtap    *wth;
+       int     i;
 
        /* First, make sure the file is valid */
        if (stat(filename, &statb) < 0) {
@@ -81,41 +107,27 @@ wtap* wtap_open_offline(const char *filename, int *err)
        wth->file_encap = WTAP_ENCAP_NONE;
 
        /* Try all file types */
-
-       /* WTAP_FILE_PCAP */
-       if ((wth->file_type = libpcap_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
-       }
-       /* WTAP_FILE_NGSNIFFER */
-       if ((wth->file_type = ngsniffer_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
-       }
-       /* WTAP_FILE_RADCOM */
-       if ((wth->file_type = radcom_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
-       }
-       /* WTAP_FILE_LANALYZER */
-       if ((wth->file_type = lanalyzer_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
-       }
-       /* WTAP_FILE_SNOOP */
-       if ((wth->file_type = snoop_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
-       }
-       /* WTAP_FILE_IPTRACE */
-       if ((wth->file_type = iptrace_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
-       }
-       /* WTAP_FILE_NETMON_xxx */
-       if ((wth->file_type = netmon_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
-       }
-       /* WTAP_FILE_NETXRAY_xxx */
-       if ((wth->file_type = netxray_open(wth)) != WTAP_FILE_UNKNOWN) {
-               goto success;
+       for (i = 0; i < N_FILE_TYPES; i++) {
+               switch ((*open_routines[i])(wth, err)) {
+
+               case -1:
+                       /* I/O error - give up */
+                       *err = errno;
+                       fclose(wth->fh);
+                       free(wth);
+                       return NULL;
+
+               case 0:
+                       /* No I/O error, but not that type of file */
+                       break;
+
+               case 1:
+                       /* We found the file type */
+                       goto success;
+               }
        }
 
-/* failure: */
+       /* Well, it's not one of the types of file we know about. */
        fclose(wth->fh);
        free(wth);
        *err = WTAP_ERR_FILE_UNKNOWN_FORMAT;
index 87fc5501b732cfa8e1233dd667c5c6410826cf5c..5c35a06fc505489a5ba4d5074f4e209c2cfb6ff3 100644 (file)
@@ -1,6 +1,6 @@
 /* iptrace.c
  *
- * $Id: iptrace.c,v 1.5 1999/07/28 01:35:34 gerald Exp $
+ * $Id: iptrace.c,v 1.6 1999/08/19 05:31:37 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
 #include "config.h"
 #endif
 #include <stdlib.h>
+#include <errno.h>
 #include <time.h>
 #include <string.h>
 #include "wtap.h"
 #include "buffer.h"
 #include "iptrace.h"
 
-int iptrace_open(wtap *wth)
+static int iptrace_read(wtap *wth, int *err);
+
+int iptrace_open(wtap *wth, int *err)
 {
        int bytes_read;
        char name[12];
 
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(name, 1, 11, wth->fh);
-
        if (bytes_read != 11) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
        name[11] = 0;
        if (strcmp(name, "iptrace 2.0") != 0) {
-               return WTAP_FILE_UNKNOWN;
+               return 0;
        }
-       wth->subtype_read = iptrace_read;
 
-       return WTAP_FILE_IPTRACE;
+       wth->file_type = WTAP_FILE_IPTRACE;
+       wth->subtype_read = iptrace_read;
+       return 1;
 }
 
 /* Read the next packet */
-int iptrace_read(wtap *wth)
+static int iptrace_read(wtap *wth, int *err)
 {
        int bytes_read;
        int data_offset;
@@ -60,8 +68,13 @@ int iptrace_read(wtap *wth)
        char if_name1, if_name2;
 
        /* Read the descriptor data */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(header, 1, 40, wth->fh);
        if (bytes_read != 40) {
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
                /* because of the way we have to kill the iptrace command,
                 * the existence of a partial header or packet is probable,
                 * and we should not complain about it. Simply return
@@ -76,6 +89,7 @@ int iptrace_read(wtap *wth)
        /* Read the packet data */
        buffer_assure_space(wth->frame_buffer, packet_size);
        data_offset = ftell(wth->fh);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                packet_size, wth->fh);
 
@@ -84,10 +98,15 @@ int iptrace_read(wtap *wth)
                 * pretend that we reached the end of the file
                 * normally. If, however, there was a read error
                 * because of some other reason, complain
+                *
+                * XXX - this was returning -1 even on a partial
+                * packet; should we return 0 on a partial packet,
+                * so that "wtap_loop()" quits and reports success?
                 */
-               if (ferror(wth->fh)) {
-                       g_print("iptrace_read: fread for data: read error\n");
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index 2c883dc44b026355e0f33423ec61b2b8f62e9cb3..e3d5dc73744d19513d924497adaf73fa180c1f4a 100644 (file)
@@ -1,6 +1,6 @@
 /* iptrace.h
  *
- * $Id: iptrace.h,v 1.1 1999/01/03 04:30:13 gram Exp $
+ * $Id: iptrace.h,v 1.2 1999/08/19 05:31:38 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -36,5 +36,4 @@ struct iptrace_pkt_hdr {
 #endif
 
 
-int iptrace_open(wtap *wth);
-int iptrace_read(wtap *wth);
+int iptrace_open(wtap *wth, int *err);
index 21b5b0b32732082e432459554661728242dfff0a..898d89e1eda76dac41f1ea5df49891ea736b3d78 100644 (file)
@@ -1,6 +1,6 @@
 /* lanalyzer.c
  *
- * $Id: lanalyzer.c,v 1.10 1999/07/13 02:53:24 gram Exp $
+ * $Id: lanalyzer.c,v 1.11 1999/08/19 05:31:34 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
 #include "config.h"
 #endif
 #include <stdlib.h>
+#include <errno.h>
 #include <time.h>
 #include "wtap.h"
 #include "buffer.h"
 #include "lanalyzer.h"
 
-int lanalyzer_open(wtap *wth)
+/* The LANalyzer format is documented (at least in part) in Novell document
+   TID022037, which can be found at, among other places:
+
+       http://www.hackzone.ru/nsp/info/nw/lan/trace.txt
+ */
+
+/* Record types. */
+#define REC_TRACE_HEADER       0x1001
+#define REC_CYCLIC_TRACE_HEADER        0x1007
+#define REC_TRACE_SUMMARY      0x1002
+#define REC_TRACE_PACKET_DATA  0x1005
+
+/* LANalyzer board types (which indicate the type of network on which
+   the capture was done). */
+#define BOARD_325              226     /* LANalyzer 325 (Ethernet) */
+#define BOARD_325TR            227     /* LANalyzer 325TR (Token-ring) */
+
+static int lanalyzer_read(wtap *wth, int *err);
+
+int lanalyzer_open(wtap *wth, int *err)
 {
        int bytes_read;
        char record_type[2];
@@ -41,21 +61,26 @@ int lanalyzer_open(wtap *wth)
        struct tm tm;
 
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(record_type, 1, 2, wth->fh);
        bytes_read += fread(record_length, 1, 2, wth->fh);
-       type = pletohs(record_type);
-       length = pletohs(record_length); /* make sure to do this for while() loop */
-
        if (bytes_read != 4) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
+       type = pletohs(record_type);
+       length = pletohs(record_length); /* make sure to do this for while() loop */
 
-       if (type != 0x1001 && type != 0x1007) {
-               return WTAP_FILE_UNKNOWN;
+       if (type != REC_TRACE_HEADER && type != REC_CYCLIC_TRACE_HEADER) {  
+               return 0;
        }
 
        /* If we made it this far, then the file is a LANAlyzer file.
         * Let's get some info from it */
+       wth->file_type = WTAP_FILE_LANALYZER;
        wth->capture.lanalyzer = g_malloc(sizeof(lanalyzer_t));
        wth->subtype_read = lanalyzer_read;
 /*     wth->snapshot_length = 16384; */ /* available in header as 'mxslc' */
@@ -64,11 +89,17 @@ int lanalyzer_open(wtap *wth)
 
        while (1) {
                fseek(wth->fh, length, SEEK_CUR);
+               errno = WTAP_ERR_CANT_READ;
                bytes_read = fread(record_type, 1, 2, wth->fh);
                bytes_read += fread(record_length, 1, 2, wth->fh);
                if (bytes_read != 4) {
+                       if (ferror(wth->fh)) {
+                               *err = errno;
+                               free(wth->capture.lanalyzer);
+                               return -1;
+                       }
                        free(wth->capture.lanalyzer);
-                       return WTAP_FILE_UNKNOWN;
+                       return 0;
                }
 
                type = pletohs(record_type);
@@ -77,8 +108,19 @@ int lanalyzer_open(wtap *wth)
 /*             g_message("Record 0x%04X Length %d", type, length);*/
                switch (type) {
                        /* Trace Summary Record */
-                       case 0x1002:
-                               fread(summary, 1, 210, wth->fh);
+                       case REC_TRACE_SUMMARY:
+                               errno = WTAP_ERR_CANT_READ;
+                               bytes_read = fread(summary, 1, sizeof summary,
+                                   wth->fh);
+                               if (bytes_read != sizeof summary) {
+                                       if (ferror(wth->fh)) {
+                                               *err = errno;
+                                               free(wth->capture.lanalyzer);
+                                               return -1;
+                                       }
+                                       free(wth->capture.lanalyzer);
+                                       return 0;
+                               }
 
                                /* Assume that the date of the creation of the trace file
                                 * is the same date of the trace. Lanalyzer doesn't
@@ -114,10 +156,10 @@ int lanalyzer_open(wtap *wth)
                                length = 0; /* to fake the next iteration of while() */
                                board_type = pletohs(&summary[188]);
                                switch (board_type) {
-                                       case 226:
+                                       case BOARD_325:
                                                wth->file_encap = WTAP_ENCAP_ETHERNET;
                                                break;
-                                       case 227:
+                                       case BOARD_325TR:
                                                wth->file_encap = WTAP_ENCAP_TR;
                                                break;
                                        default:
@@ -126,9 +168,9 @@ int lanalyzer_open(wtap *wth)
                                break;
 
                        /* Trace Packet Data Record */
-                       case 0x1005:
+                       case REC_TRACE_PACKET_DATA:
                                wth->capture.lanalyzer->pkt_len = length - 32;
-                               return WTAP_FILE_LANALYZER;
+                               return 1;
 
                /*      default: no default action */
                /*              printf("Record 0x%04X Length %d\n", type, length);*/
@@ -136,42 +178,63 @@ int lanalyzer_open(wtap *wth)
        } 
 
        /* never gets here */
-       return WTAP_FILE_LANALYZER;
+       return 0;
 }
 
+#define DESCRIPTOR_LEN 32
+
 /* Read the next packet */
-int lanalyzer_read(wtap *wth)
+static int lanalyzer_read(wtap *wth, int *err)
 {
        int packet_size = wth->capture.lanalyzer->pkt_len; /* slice, really */
        int bytes_read;
        char record_type[2];
        char record_length[2];
        guint16 type, length;
-       gchar descriptor[32];
+       gchar descriptor[DESCRIPTOR_LEN];
        int     data_offset;
        guint16 time_low, time_med, time_high, true_size;
        double t;
 
        /* If this is the very first packet, then the fh cursor will already
-        * be at the start of the packet data instead of at the start of the Trace
-        * Packet Data Record. Check for this */
+        * be at the start of the packet data instead of at the start of the
+        * Trace Packet Data Record. Check for this */
        if (!packet_size) {
-
-               /* Increment fh cursor to next record */
+               /* This isn't the first packet (the record type and length
+                * of which we've already read in the loop in the open
+                * routine); read the record type and length. */
+               errno = WTAP_ERR_CANT_READ;
                bytes_read = fread(record_type, 1, 2, wth->fh);
-               bytes_read += fread(record_length, 1, 2, wth->fh);
-               if (bytes_read != 4) {
+               if (bytes_read != 2) {
+                       if (ferror(wth->fh)) {
+                               *err = errno;
+                               return -1;
+                       }
+                       if (bytes_read != 0) {
+                               *err = WTAP_ERR_SHORT_READ;
+                               return -1;
+                       }
                        return 0;
                }
+               bytes_read = fread(record_length, 1, 2, wth->fh);
+               if (bytes_read != 2) {
+                       if (ferror(wth->fh))
+                               *err = errno;
+                       else
+                               *err = WTAP_ERR_SHORT_READ;
+                       return -1;
+               }
 
                type = pletohs(record_type);
                length = pletohs(record_length);
 
-               if (type != 0x1005) {
+               if (type != REC_TRACE_PACKET_DATA) {
+                       /* XXX - return -1 and set "*err" to
+                        * WTAP_ERR_BAD_RECORD? */
                        return 0;
                }
                else {
-                       packet_size = length - 32;
+                       packet_size = length - DESCRIPTOR_LEN;
                }
        }
        else {
@@ -179,26 +242,28 @@ int lanalyzer_read(wtap *wth)
        }       
 
        /* Read the descriptor data */
-       bytes_read = fread(descriptor, 1, 32, wth->fh);
-       if (bytes_read != 32) {
-               g_error("lanalyzer_read: not enough descriptor data (%d bytes)",
-                               bytes_read);
-               return 0;
+       errno = WTAP_ERR_CANT_READ;
+       bytes_read = fread(descriptor, 1, DESCRIPTOR_LEN, wth->fh);
+       if (bytes_read != DESCRIPTOR_LEN) {
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
+               return -1;
        }
 
        /* Read the packet data */
        buffer_assure_space(wth->frame_buffer, packet_size);
        data_offset = ftell(wth->fh);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                packet_size, wth->fh);
 
        if (bytes_read != packet_size) {
-               if (ferror(wth->fh)) {
-                       g_error("lanalyzer_read: fread for data: read error\n");
-               } else {
-                       g_error("lanalyzer_read: fread for data: %d bytes out of %d read",
-                               bytes_read, packet_size);
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index 3efd992ba164b6e1daceaf634e6996d6dead6564..336051bd1f01b494f7552001e106d5ccf52e18d1 100644 (file)
@@ -1,6 +1,6 @@
 /* lanalyzer.h
  *
- * $Id: lanalyzer.h,v 1.1 1998/11/12 06:01:23 gram Exp $
+ * $Id: lanalyzer.h,v 1.2 1999/08/19 05:31:34 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -21,7 +21,4 @@
  *
  */
 
-
-
-int lanalyzer_open(wtap *wth);
-int lanalyzer_read(wtap *wth);
+int lanalyzer_open(wtap *wth, int *err);
index 9dffe50627b42ccce477de8b51988c6a91bd1a6b..d2445db79e7e9aa9a3871151073a027ea326c7cf 100644 (file)
@@ -1,6 +1,6 @@
 /* libpcap.c
  *
- * $Id: libpcap.c,v 1.9 1999/08/18 17:08:47 guy Exp $
+ * $Id: libpcap.c,v 1.10 1999/08/19 05:31:37 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -70,7 +70,7 @@ struct pcaprec_hdr {
        guint32 orig_len;       /* actual length of packet */
 };
 
-static int libpcap_read(wtap *wth);
+static int libpcap_read(wtap *wth, int *err);
 static int libpcap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
     const u_char *pd, int *err);
 static int libpcap_dump_close(wtap_dumper *wdh, int *err);
@@ -99,8 +99,7 @@ static const int pcap_encap[] = {
 };
 #define NUM_PCAP_ENCAPS (sizeof pcap_encap / sizeof pcap_encap[0])
 
-/* Returns WTAP_FILE_PCAP on success, WTAP_FILE_UNKNOWN on failure */
-int libpcap_open(wtap *wth)
+int libpcap_open(wtap *wth, int *err)
 {
        int bytes_read;
        guint32 magic;
@@ -109,10 +108,14 @@ int libpcap_open(wtap *wth)
 
        /* Read in the number that should be at the start of a "libpcap" file */
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&magic, 1, sizeof magic, wth->fh);
-
        if (bytes_read != sizeof magic) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        if (magic == PCAP_SWAPPED_MAGIC) {
@@ -121,13 +124,18 @@ int libpcap_open(wtap *wth)
                byte_swapped = 1;
        }
        if (magic != PCAP_MAGIC) {
-               return WTAP_FILE_UNKNOWN;
+               return 0;
        }
 
        /* Read the rest of the header. */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh);
        if (bytes_read != sizeof hdr) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        if (byte_swapped) {
@@ -139,14 +147,19 @@ int libpcap_open(wtap *wth)
        }
        if (hdr.version_major < 2) {
                /* We only support version 2.0 and later. */
-               return WTAP_FILE_UNKNOWN;
+               g_message("pcap: major version %d unsupported",
+                   hdr.version_major);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
        if (hdr.network >= NUM_PCAP_ENCAPS) {
-               g_error("pcap: network type %d unknown", hdr.network);
-               return WTAP_FILE_UNKNOWN;
+               g_message("pcap: network type %d unknown", hdr.network);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
        /* This is a libpcap file */
+       wth->file_type = WTAP_FILE_PCAP;
        wth->capture.pcap = g_malloc(sizeof(libpcap_t));
        wth->capture.pcap->byte_swapped = byte_swapped;
        wth->capture.pcap->version_major = hdr.version_major;
@@ -154,12 +167,11 @@ int libpcap_open(wtap *wth)
        wth->subtype_read = libpcap_read;
        wth->file_encap = pcap_encap[hdr.network];
        wth->snapshot_length = hdr.snaplen;
-
-       return WTAP_FILE_PCAP;
+       return 1;
 }
 
 /* Read the next packet */
-static int libpcap_read(wtap *wth)
+static int libpcap_read(wtap *wth, int *err)
 {
        int     packet_size;
        int     bytes_read;
@@ -167,11 +179,15 @@ static int libpcap_read(wtap *wth)
        int     data_offset;
 
        /* Read record header. */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh);
        if (bytes_read != sizeof hdr) {
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
                if (bytes_read != 0) {
-                       g_error("pcap_read: not enough packet header data (%d bytes)",
-                                       bytes_read);
+                       *err = WTAP_ERR_SHORT_READ;
                        return -1;
                }
                return 0;
@@ -207,16 +223,15 @@ static int libpcap_read(wtap *wth)
        packet_size = hdr.incl_len;
        buffer_assure_space(wth->frame_buffer, packet_size);
        data_offset = ftell(wth->fh);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                        packet_size, wth->fh);
 
        if (bytes_read != packet_size) {
-               if (ferror(wth->fh)) {
-                       g_error("pcap_read: fread for data: read error\n");
-               } else {
-                       g_error("pcap_read: fread for data: %d bytes out of %d",
-                               bytes_read, packet_size);
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index 2bd1449996ef5aea4daad158e7f09d223c563cb1..740f6a7a05b7ae0d41556e91163e050e1c7c51a2 100644 (file)
@@ -1,6 +1,6 @@
 /* libpcap.h
  *
- * $Id: libpcap.h,v 1.2 1999/08/18 04:17:36 guy Exp $
+ * $Id: libpcap.h,v 1.3 1999/08/19 05:31:37 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -21,5 +21,5 @@
  *
  */
 
-int libpcap_open(wtap *wth);
+int libpcap_open(wtap *wth, int *err);
 int libpcap_dump_open(wtap_dumper *wdh, int *err);
index 32ce63e1b10fe36929588a1d38ff07a3a378ab3d..77afdaeaf2a70f7136096d779af7e77971c69e14 100644 (file)
@@ -1,6 +1,6 @@
 /* netmon.c
  *
- * $Id: netmon.c,v 1.9 1999/08/18 04:17:38 guy Exp $
+ * $Id: netmon.c,v 1.10 1999/08/19 05:31:36 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -23,6 +23,7 @@
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+#include <errno.h>
 #include <time.h>
 #include "wtap.h"
 #include "buffer.h"
@@ -89,8 +90,9 @@ struct netmonrec_2_x_hdr {
        guint32 incl_len;       /* number of octets captured in file */
 };
 
-/* Returns WTAP_FILE_NETMON on success, WTAP_FILE_UNKNOWN on failure */
-int netmon_open(wtap *wth)
+static int netmon_read(wtap *wth, int *err);
+
+int netmon_open(wtap *wth, int *err)
 {
        int bytes_read;
        char magic[sizeof netmon_1_x_magic];
@@ -116,21 +118,30 @@ int netmon_open(wtap *wth)
        /* Read in the string that should be at the start of a Network
         * Monitor file */
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(magic, 1, sizeof magic, wth->fh);
-
        if (bytes_read != sizeof magic) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        if (memcmp(magic, netmon_1_x_magic, sizeof netmon_1_x_magic) != 0
         && memcmp(magic, netmon_2_x_magic, sizeof netmon_1_x_magic) != 0) {
-               return WTAP_FILE_UNKNOWN;
+               return 0;
        }
 
        /* Read the rest of the header. */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh);
        if (bytes_read != sizeof hdr) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        switch (hdr.ver_major) {
@@ -144,16 +155,20 @@ int netmon_open(wtap *wth)
                break;
 
        default:
-               return WTAP_FILE_UNKNOWN;
+               g_message("netmon: major version %d unsupported", hdr.ver_major);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
        hdr.network = pletohs(&hdr.network);
        if (hdr.network >= NUM_NETMON_ENCAPS) {
-               g_error("netmon: network type %d unknown", hdr.network);
-               return WTAP_FILE_UNKNOWN;
+               g_message("netmon: network type %d unknown", hdr.network);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
        /* This is a netmon file */
+       wth->file_type = file_type;
        wth->capture.netmon = g_malloc(sizeof(netmon_t));
        wth->subtype_read = netmon_read;
        wth->file_encap = netmon_encap[hdr.network];
@@ -198,11 +213,11 @@ int netmon_open(wtap *wth)
        /* Seek to the beginning of the data records. */
        fseek(wth->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET);
 
-       return file_type;
+       return 1;
 }
 
 /* Read the next packet */
-int netmon_read(wtap *wth)
+static int netmon_read(wtap *wth, int *err)
 {
        int     packet_size = 0;
        int     bytes_read;
@@ -234,11 +249,15 @@ int netmon_read(wtap *wth)
                hdr_size = sizeof (struct netmonrec_2_x_hdr);
                break;
        }
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, hdr_size, wth->fh);
        if (bytes_read != hdr_size) {
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
                if (bytes_read != 0) {
-                       g_error("netmon_read: not enough packet header data (%d bytes)",
-                                       bytes_read);
+                       *err = WTAP_ERR_SHORT_READ;
                        return -1;
                }
                return 0;
@@ -256,16 +275,15 @@ int netmon_read(wtap *wth)
                break;
        }
        buffer_assure_space(wth->frame_buffer, packet_size);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                        packet_size, wth->fh);
 
        if (bytes_read != packet_size) {
-               if (ferror(wth->fh)) {
-                       g_error("netmon_read: fread for data: read error\n");
-               } else {
-                       g_error("netmon_read: fread for data: %d bytes out of %d",
-                               bytes_read, packet_size);
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index 6fef1cf3506b2c08ce9915a7f2cde84e350803ca..a18626179342f3f5d9a7fb5869b429ef9e4c88a5 100644 (file)
@@ -1,6 +1,6 @@
 /* netmon.h
  *
- * $Id: netmon.h,v 1.1 1999/01/18 21:34:54 gram Exp $
+ * $Id: netmon.h,v 1.2 1999/08/19 05:31:36 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -21,5 +21,4 @@
  *
  */
 
-int netmon_open(wtap *wth);
-int netmon_read(wtap *wth);
+int netmon_open(wtap *wth, int *err);
index 17ce354b43115765b690507ccdb80ab45fd85a42..1f8391d41f4caba651b16e205b86382af5efe3a9 100644 (file)
@@ -1,6 +1,6 @@
 /* netxray.c
  *
- * $Id: netxray.c,v 1.9 1999/08/18 04:17:37 guy Exp $
+ * $Id: netxray.c,v 1.10 1999/08/19 05:31:35 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -25,6 +25,7 @@
 #endif
 
 #include <stdlib.h>
+#include <errno.h>
 #include <time.h>
 #include "wtap.h"
 #include "netxray.h"
@@ -86,8 +87,9 @@ struct netxrayrec_2_x_hdr {
        guint32 xxx[7];         /* unknown */
 };
 
-/* Returns WTAP_FILE_NETXRAY on success, WTAP_FILE_UNKNOWN on failure */
-int netxray_open(wtap *wth)
+static int netxray_read(wtap *wth, int *err);
+
+int netxray_open(wtap *wth, int *err)
 {
        int bytes_read;
        char magic[sizeof netxray_magic];
@@ -114,20 +116,29 @@ int netxray_open(wtap *wth)
        /* Read in the string that should be at the start of a NetXRay
         * file */
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(magic, 1, sizeof magic, wth->fh);
-
        if (bytes_read != sizeof magic) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        if (memcmp(magic, netxray_magic, sizeof netxray_magic) != 0) {
-               return WTAP_FILE_UNKNOWN;
+               return 0;
        }
 
        /* Read the rest of the header. */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh);
        if (bytes_read != sizeof hdr) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        /* It appears that version 1.1 files (as produced by Windows
@@ -150,16 +161,20 @@ int netxray_open(wtap *wth)
                version_major = 2;
                file_type = WTAP_FILE_NETXRAY_2_001;
        } else {
-               return WTAP_FILE_UNKNOWN;
+               g_message("netxray: version \"%.8s\" unsupported", hdr.version);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
        hdr.network = pletohs(&hdr.network);
        if (hdr.network >= NUM_NETXRAY_ENCAPS) {
-               g_error("netxray: network type %d unknown", hdr.network);
-               return WTAP_FILE_UNKNOWN;
+               g_message("netxray: network type %d unknown", hdr.network);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
        /* This is a netxray file */
+       wth->file_type = file_type;
        wth->capture.netxray = g_malloc(sizeof(netxray_t));
        wth->subtype_read = netxray_read;
        wth->file_encap = netxray_encap[hdr.network];
@@ -183,11 +198,11 @@ int netxray_open(wtap *wth)
        /* Seek to the beginning of the data records. */
        fseek(wth->fh, pletohl(&hdr.start_offset), SEEK_SET);
 
-       return file_type;
+       return 1;
 }
 
 /* Read the next packet */
-int netxray_read(wtap *wth)
+static int netxray_read(wtap *wth, int *err)
 {
        int     packet_size;
        int     bytes_read;
@@ -217,11 +232,15 @@ reread:
                hdr_size = sizeof (struct netxrayrec_2_x_hdr);
                break;
        }
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, hdr_size, wth->fh);
        if (bytes_read != hdr_size) {
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
                if (bytes_read != 0) {
-                       g_error("netxray_read: not enough packet header data (%d bytes)",
-                                       bytes_read);
+                       *err = WTAP_ERR_SHORT_READ;
                        return -1;
                }
 
@@ -240,16 +259,15 @@ reread:
 
        packet_size = pletohs(&hdr.hdr_1_x.incl_len);
        buffer_assure_space(wth->frame_buffer, packet_size);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                        packet_size, wth->fh);
 
        if (bytes_read != packet_size) {
-               if (ferror(wth->fh)) {
-                       g_error("netxray_read: fread for data: read error\n");
-               } else {
-                       g_error("netxray_read: fread for data: %d bytes out of %d",
-                               bytes_read, packet_size);
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index e66e324b9473312aed16940960c30473166344ec..a21744b2ffcdcda00b398e0a4f1543a23a8f279c 100644 (file)
@@ -1,6 +1,6 @@
 /* netxray.h
  *
- * $Id: netxray.h,v 1.1 1999/02/20 06:49:27 guy Exp $
+ * $Id: netxray.h,v 1.2 1999/08/19 05:31:36 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -21,5 +21,4 @@
  *
  */
 
-int netxray_open(wtap *wth);
-int netxray_read(wtap *wth);
+int netxray_open(wtap *wth, int *err);
index 03b429a709d4a642c9d3f9686c9908a210db3837..bd7bc0fe3776137f2ee3b37e4f814c20b35f6805 100644 (file)
@@ -1,6 +1,6 @@
 /* ngsniffer.c
  *
- * $Id: ngsniffer.c,v 1.14 1999/08/02 02:04:37 guy Exp $
+ * $Id: ngsniffer.c,v 1.15 1999/08/19 05:31:33 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -60,6 +60,7 @@
 #endif
 
 #include <stdlib.h>
+#include <errno.h>
 #include <time.h>
 #include "wtap.h"
 #include "buffer.h"
@@ -259,11 +260,12 @@ static int sniffer_encap[] = {
                WTAP_ENCAP_NONE         /* ATM */
 };
 
-static int get_atm_linktype(wtap *wth);
+static int ngsniffer_read(wtap *wth, int *err);
+
+static int get_atm_linktype(wtap *wth, int *err);
 static int linktype_for_packet(u_int app_traf_type, u_int app_hl_type);
 
-/* Returns WTAP_FILE_NGSNIFFER on success, WTAP_FILE_UNKNOWN on failure */
-int ngsniffer_open(wtap *wth)
+int ngsniffer_open(wtap *wth, int *err)
 {
        int bytes_read;
        char magic[18];
@@ -278,32 +280,35 @@ int ngsniffer_open(wtap *wth)
 
        /* Read in the string that should be at the start of a Sniffer file */
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(magic, 1, 17, wth->fh);
-
        if (bytes_read != 17) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        magic[17] = 0;
 
        if (strcmp(magic, "TRSNIFF data    \x1a")) {
-               return WTAP_FILE_UNKNOWN;
+               return 0;
        }
 
-       /* This is a ngsniffer file */
-       wth->capture.ngsniffer = g_malloc(sizeof(ngsniffer_t));
-       wth->subtype_read = ngsniffer_read;
-       wth->snapshot_length = 16384;   /* not available in header, only in frame */
-
        /*
         * Read the first record, which the manual says is a version
         * record.
         */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(record_type, 1, 2, wth->fh);
        bytes_read += fread(record_length, 1, 4, wth->fh);
        if (bytes_read != 6) {
-               free(wth->capture.ngsniffer);
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        type = pletohs(record_type);
@@ -311,39 +316,49 @@ int ngsniffer_open(wtap *wth)
 
        if (type != REC_VERS) {
                g_message("ngsniffer: Sniffer file doesn't start with a version record");
-               free(wth->capture.ngsniffer);
-               return WTAP_FILE_UNKNOWN;
+               *err = WTAP_ERR_BAD_RECORD;
+               return -1;
        }
 
-       fread(&version, 1, sizeof version, wth->fh);
+       errno = WTAP_ERR_CANT_READ;
+       bytes_read = fread(&version, 1, sizeof version, wth->fh);
+       if (bytes_read != sizeof version) {
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
+       }
 
        /* Make sure this is an uncompressed Sniffer file */
        if (version.format != 1) {
                g_message("ngsniffer: Compressed Sniffer files are not supported");
-               free(wth->capture.ngsniffer);
-               return WTAP_FILE_UNKNOWN;
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
-       /* Get data link type */
+       /* Check the data link type */
        if (version.network >= NUM_NGSNIFF_ENCAPS) {
                g_message("ngsniffer: network type %d unknown", version.network);
-               free(wth->capture.ngsniffer);
-               return WTAP_FILE_UNKNOWN;
-       }
-       else {
-               wth->file_encap = sniffer_encap[version.network];
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
-       /* Get time unit */
+       /* Check the time unit */
        if (version.timeunit >= NUM_NGSNIFF_TIMEUNITS) {
                g_message("ngsniffer: Unknown timeunit %d", version.timeunit);
-               free(wth->capture.ngsniffer);
-               return WTAP_FILE_UNKNOWN;
-       }
-       else {
-               wth->capture.ngsniffer->timeunit = Usec[version.timeunit];
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
+       /* This is a ngsniffer file */
+       wth->file_type = WTAP_FILE_NGSNIFFER;
+       wth->capture.ngsniffer = g_malloc(sizeof(ngsniffer_t));
+       wth->subtype_read = ngsniffer_read;
+       wth->snapshot_length = 16384;   /* not available in header, only in frame */
+       wth->capture.ngsniffer->timeunit = Usec[version.timeunit];
+       wth->file_encap = sniffer_encap[version.network];
+
        /* Get capture start time */
        start_time = pletohs(&version.time);
        start_date = pletohs(&version.date);
@@ -405,23 +420,28 @@ int ngsniffer_open(wtap *wth)
                 * to cope with raw cells or other types of frames
                 * anyway; the only place you lose is with FORE
                 * SPANS.
+                *
+                * XXX - eventually add a "Sniffer ATM encapsulation"
+                * that returns the full data, including the ATM
+                * pseudo-header.
                 */
-               wth->file_encap = get_atm_linktype(wth);
+               *err = WTAP_ERR_UNSUPPORTED;
+               wth->file_encap = get_atm_linktype(wth, err);
                if (wth->file_encap == -1) {
                        /*
                         * Oops, we couldn't find a link type we can
                         * handle.
                         */
-                       g_message("ngsniffer: no LANE or RFC 1483 LLC-multiplexed frames found");
                        free(wth->capture.ngsniffer);
-                       return WTAP_FILE_UNKNOWN;
+                       g_message("ngsniffer: no LANE or RFC 1483 LLC-multiplexed frames found");
+                       return -1;
                }
                wth->capture.ngsniffer->is_atm = 1;
        }
-       return WTAP_FILE_NGSNIFFER;
+       return 1;
 }
 
-static int get_atm_linktype(wtap *wth)
+static int get_atm_linktype(wtap *wth, int *err)
 {
        int     bytes_read;
        char record_type[2];
@@ -435,15 +455,40 @@ static int get_atm_linktype(wtap *wth)
                /*
                 * Read the record header.
                 */
+               errno = WTAP_ERR_CANT_READ;
                bytes_read = fread(record_type, 1, 2, wth->fh);
-               bytes_read += fread(record_length, 1, 4, wth->fh);
-               if (bytes_read != 6) {
+               if (bytes_read != 2) {
                        /*
                         * End of file or error.  Probably means there *are*
                         * no LANE or RFC 1483 LLC-multiplexed frames,
                         * which means we can't handle this in
                         * "wiretap".  Return an error.
                         */
+                       if (ferror(wth->fh))
+                               *err = errno;
+                       else {
+                               /*
+                                * If we read no bytes at all, treat
+                                * that as an EOF, not a short read;
+                                * "*err" got set initially to
+                                * WTAP_ERR_UNSUPPORTED, which is the
+                                * most appropriate error if we just
+                                * ran out of packets without seeing
+                                * any LANE or RFC 1483 LLC-multiplexed
+                                * frames.
+                                */
+                               if (bytes_read != 0)
+                                       *err = WTAP_ERR_SHORT_READ;
+                       }
+                       return -1;
+               }
+               errno = WTAP_ERR_CANT_READ;
+               bytes_read = fread(record_length, 1, 4, wth->fh);
+               if (bytes_read != 4) {
+                       if (ferror(wth->fh))
+                               *err = errno;
+                       else
+                               *err = WTAP_ERR_SHORT_READ;
                        return -1;
                }
 
@@ -455,7 +500,11 @@ static int get_atm_linktype(wtap *wth)
                         * End of file.  Probably means there *are*
                         * no LANE or RFC 1483 LLC-multiplexed frames,
                         * which means we can't handle this in
-                        * "wiretap".  Return an error.
+                        * "wiretap".  "*err" got set initially to
+                        * WTAP_ERR_UNSUPPORTED, which is the most
+                        * appropriate error if we just ran out of
+                        * packets without seeing any LANE or RFC
+                        * 1483 LLC-multiplexed frames.
                         */
                        return -1;
                }
@@ -467,16 +516,19 @@ static int get_atm_linktype(wtap *wth)
                         * Umm, we're not supposed to get these in an
                         * ATM Sniffer file; return an error.
                         */
+                       g_message("ngsniffer: REC_FRAME2 record in an ATM Sniffer file");
+                       *err = WTAP_ERR_BAD_RECORD;
                        return -1;
 
                case REC_FRAME4:
                        /* Read the f_frame4_struct */
+                       errno = WTAP_ERR_CANT_READ;
                        bytes_read = fread(&frame4, 1, sizeof frame4, wth->fh);
                        if (bytes_read != sizeof frame4) {
-                               /*
-                                * Read error or short record.  Return
-                                * an error.
-                                */
+                               if (ferror(wth->fh))
+                                       *err = errno;
+                               else
+                                       *err = WTAP_ERR_SHORT_READ;
                                return -1;
                        }
                        time_low = pletohs(&frame4.time_low);
@@ -572,9 +624,8 @@ static int linktype_for_packet(u_int app_traf_type, u_int app_hl_type)
 }
 
 /* Read the next packet */
-int ngsniffer_read(wtap *wth)
+static int ngsniffer_read(wtap *wth, int *err)
 {
-       int     packet_size = wth->capture.ngsniffer->pkt_len;
        int     bytes_read;
        char record_type[2];
        char record_length[4]; /* only 1st 2 bytes are length */
@@ -604,14 +655,28 @@ int ngsniffer_read(wtap *wth)
                /*
                 * Read the record header.
                 */
+               errno = WTAP_ERR_CANT_READ;
                bytes_read = fread(record_type, 1, 2, wth->fh);
-               bytes_read += fread(record_length, 1, 4, wth->fh);
-               if (bytes_read != 6) {
-                       /*
-                        * End of file or error.
-                        */
+               if (bytes_read != 2) {
+                       if (ferror(wth->fh)) {
+                               *err = errno;
+                               return -1;
+                       }
+                       if (bytes_read != 0) {
+                               *err = WTAP_ERR_SHORT_READ;
+                               return -1;
+                       }
                        return 0;
                }
+               errno = WTAP_ERR_CANT_READ;
+               bytes_read = fread(record_length, 1, 4, wth->fh);
+               if (bytes_read != 4) {
+                       if (ferror(wth->fh))
+                               *err = errno;
+                       else
+                               *err = WTAP_ERR_SHORT_READ;
+                       return -1;
+               }
 
                type = pletohs(record_type);
                length = pletohs(record_length);
@@ -620,15 +685,14 @@ int ngsniffer_read(wtap *wth)
 
                case REC_FRAME2:
                        /* Read the f_frame2_struct */
+                       errno = WTAP_ERR_CANT_READ;
                        bytes_read = fread(&frame2, 1, sizeof frame2, wth->fh);
                        if (bytes_read != sizeof frame2) {
-                               /*
-                                * Read error or short record.  Return
-                                * an error.
-                                */
-                               g_message("ngsniffer_read: not enough frame2 data (%d bytes)",
-                                       bytes_read);
-                               return 0;
+                               if (ferror(wth->fh))
+                                       *err = errno;
+                               else
+                                       *err = WTAP_ERR_SHORT_READ;
+                               return -1;
                        }
                        time_low = pletohs(&frame2.time_low);
                        time_med = pletohs(&frame2.time_med);
@@ -647,15 +711,14 @@ int ngsniffer_read(wtap *wth)
 
                case REC_FRAME4:
                        /* Read the f_frame4_struct */
+                       errno = WTAP_ERR_CANT_READ;
                        bytes_read = fread(&frame4, 1, sizeof frame4, wth->fh);
                        if (bytes_read != sizeof frame4) {
-                               /*
-                                * Read error or short record.  Return
-                                * an error.
-                                */
-                               g_message("ngsniffer_read: not enough frame4 data (%d bytes)",
-                                       bytes_read);
-                               return 0;
+                               if (ferror(wth->fh))
+                                       *err = errno;
+                               else
+                                       *err = WTAP_ERR_SHORT_READ;
+                               return -1;
                        }
                        time_low = pletohs(&frame4.time_low);
                        time_med = pletohs(&frame4.time_med);
@@ -732,16 +795,15 @@ found:
         */
        buffer_assure_space(wth->frame_buffer, length);
        data_offset = ftell(wth->fh);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                        length, wth->fh);
 
        if (bytes_read != length) {
-               if (ferror(wth->fh)) {
-                       g_message("ngsniffer_read: fread for data: read error\n");
-               } else {
-                       g_message("ngsniffer_read: fread for data: %d bytes out of %d",
-                               bytes_read, packet_size);
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index 4ec27bf4a866ce17a7051985e5eeb2a1509f82bb..09d47efb711edf749ad6d94a7c872383edfde02b 100644 (file)
 #endif
 
 #include <stdlib.h>
+#include <errno.h>
 #include <time.h>
 #include "wtap.h"
 #include "buffer.h"
 #include "radcom.h"
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
 
 struct frame_date {
        guint16 year;
@@ -43,8 +41,9 @@ static char radcom_magic[8] = {
        0x42, 0xD2, 0x00, 0x34, 0x12, 0x66, 0x22, 0x88
 };
 
-/* Returns WTAP_FILE_RADCOM on success, WTAP_FILE_UNKNOWN on failure */
-int radcom_open(wtap *wth)
+static int radcom_read(wtap *wth, int *err);
+
+int radcom_open(wtap *wth, int *err)
 {
        int bytes_read;
        char magic[8];
@@ -56,40 +55,60 @@ int radcom_open(wtap *wth)
 
        /* Read in the string that should be at the start of a Sniffer file */
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(magic, 1, 8, wth->fh);
-
        if (bytes_read != 8) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        if (memcmp(magic, radcom_magic, 8)) {
-               return WTAP_FILE_UNKNOWN;
+               return 0;
        }
 
-       /* This is a radcom file */
-       wth->capture.radcom = g_malloc(sizeof(radcom_t));
-       wth->subtype_read = radcom_read;
-       wth->snapshot_length = 16384;   /* not available in header, only in frame */
        fseek(wth->fh, 0x8B, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&byte, 1, 1, wth->fh);
        if (bytes_read != 1) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
        while (byte) {
+               errno = WTAP_ERR_CANT_READ;
                bytes_read = fread(&byte, 1, 1, wth->fh);
                if (bytes_read != 1) {
-                       return WTAP_FILE_UNKNOWN;
+                       if (ferror(wth->fh)) {
+                               *err = errno;
+                               return -1;
+                       }
+                       return 0;
                }
        }
        fseek(wth->fh, 1, SEEK_CUR);
 
        /* Get capture start time */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&start_date, 1, sizeof(struct frame_date), wth->fh);
-
        if (bytes_read != sizeof(struct frame_date)) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
+       /* This is a radcom file */
+       wth->file_type = WTAP_FILE_RADCOM;
+       wth->capture.radcom = g_malloc(sizeof(radcom_t));
+       wth->subtype_read = radcom_read;
+       wth->snapshot_length = 16384;   /* not available in header, only in frame */
+
        tm.tm_year = start_date.year-1900;
        tm.tm_mon = start_date.month-1;
        tm.tm_mday = start_date.day;
@@ -101,40 +120,48 @@ int radcom_open(wtap *wth)
 
        fseek(wth->fh, sizeof(struct frame_date), SEEK_CUR);
 
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(search_encap, 1, 7, wth->fh);
        if (bytes_read != 7) {
-               return WTAP_FILE_UNKNOWN;
+               goto read_error;
        }
        while (memcmp(encap_magic, search_encap, 7)) {
                fseek(wth->fh, -6, SEEK_CUR);
+               errno = WTAP_ERR_CANT_READ;
                bytes_read = fread(search_encap, 1, 7, wth->fh);
                if (bytes_read != 7) {
-                       return WTAP_FILE_UNKNOWN;
+                       goto read_error;
                }
        }
        fseek(wth->fh, 12, SEEK_CUR);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(search_encap, 1, 4, wth->fh);
        if (bytes_read != 4) {
-               return WTAP_FILE_UNKNOWN;
+               goto read_error;
        }
        if (!memcmp(search_encap, "LAPB", 4))
                wth->file_encap = WTAP_ENCAP_LAPB;
        else if (!memcmp(search_encap, "Ethe", 4))
                wth->file_encap = WTAP_ENCAP_ETHERNET;
-       else
-               return WTAP_FILE_UNKNOWN;
+       else {
+               g_message("pcap: network type \"%.4s\" unknown", search_encap);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
+       }
 
        /*bytes_read = fread(&next_date, 1, sizeof(struct frame_date), wth->fh);
+       errno = WTAP_ERR_CANT_READ;
        if (bytes_read != sizeof(struct frame_date)) {
-               return WTAP_FILE_UNKNOWN;
+               goto read_error;
        }
 
        while (memcmp(&start_date, &next_date, 4)) {
                fseek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR);
+               errno = WTAP_ERR_CANT_READ;
                bytes_read = fread(&next_date, 1, sizeof(struct frame_date),
                                   wth->fh);
                if (bytes_read != sizeof(struct frame_date)) {
-                       return WTAP_FILE_UNKNOWN;
+                       goto read_error;
                }
        }*/
 
@@ -143,11 +170,20 @@ int radcom_open(wtap *wth)
        else if (wth->file_encap == WTAP_ENCAP_LAPB)
                fseek(wth->fh, 297, SEEK_CUR);
 
-       return WTAP_FILE_RADCOM;
+       return 1;
+
+read_error:
+       if (ferror(wth->fh)) {
+               *err = errno;
+               free(wth->capture.radcom);
+               return -1;
+       }
+       free(wth->capture.radcom);
+       return 0;
 }
 
 /* Read the next packet */
-int radcom_read(wtap *wth)
+static int radcom_read(wtap *wth, int *err)
 {
        int     bytes_read;
        guint16 length;
@@ -161,30 +197,35 @@ int radcom_read(wtap *wth)
        /*
         * Read the frame size
         */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&length, 1, 2, wth->fh);
        if (bytes_read != 2) {
-               /*
-                * End of file or error.
-                */
-               g_message("radcom_read: not enough frame data (%d bytes)",
-                       bytes_read);
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               if (bytes_read != 0) {
+                       *err = WTAP_ERR_SHORT_READ;
+                       return -1;
+               }
                return 0;
        }
 
-       if (wth->file_encap == WTAP_ENCAP_LAPB) length -= 2; /* FCS */
+       if (wth->file_encap == WTAP_ENCAP_LAPB)
+               length -= 2; /* FCS */
 
        wth->phdr.len = length;
        wth->phdr.caplen = length;
 
        fseek(wth->fh, 5, SEEK_CUR);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&date, 1, sizeof(struct frame_date), wth->fh);
        if (bytes_read != sizeof(struct frame_date)) {
-               /*
-                * End of file or error.
-                */
-               g_message("radcom_read: not enough frame data (%d bytes)",
-                       bytes_read);
-               return 0;
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
+               return -1;
        }
 
        tm.tm_year = date.year-1900;
@@ -198,14 +239,14 @@ int radcom_read(wtap *wth)
        wth->phdr.ts.tv_usec = date.usec;
 
        fseek(wth->fh, 6, SEEK_CUR);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&dce, 1, 1, wth->fh);
        if (bytes_read != 1) {
-               /*
-                * End of file or error.
-                */
-               g_message("radcom_read: not enough frame data (%d bytes)",
-                       bytes_read);
-               return 0;
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
+               return -1;
        }
        wth->phdr.flags = (dce & 0x1) ? 0x00 : 0x80;
 
@@ -216,16 +257,15 @@ int radcom_read(wtap *wth)
         */
        buffer_assure_space(wth->frame_buffer, length);
        data_offset = ftell(wth->fh);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                        length, wth->fh);
 
        if (bytes_read != length) {
-               if (ferror(wth->fh)) {
-                       g_message("radcom_read: fread for data: read error\n");
-               } else {
-                       g_message("radcom_read: fread for data: %d bytes out of %d",
-                               bytes_read, length);
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index 044ad6831a787e68a2c552feb1703f32c2e55563..d4492316c859b9b361e6446dea85f6075a4424bb 100644 (file)
@@ -19,5 +19,4 @@
  *
  */
 
-int radcom_open(wtap *wth);
-int radcom_read(wtap *wth);
+int radcom_open(wtap *wth, int *err);
index ceb6aeac7e6408001b8be8c07197829436452fbc..c5661049ba7ad2cfef567ace657a15964c8ebea7 100644 (file)
@@ -1,6 +1,6 @@
 /* snoop.c
  *
- * $Id: snoop.c,v 1.5 1999/07/13 02:53:26 gram Exp $
+ * $Id: snoop.c,v 1.6 1999/08/19 05:31:35 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -23,6 +23,7 @@
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+#include <errno.h>
 #include "wtap.h"
 #include "buffer.h"
 #include "snoop.h"
@@ -53,8 +54,9 @@ struct snooprec_hdr {
        guint32 ts_usec;        /* timestamp microseconds */
 };
 
-/* Returns WTAP_FILE_SNOOP on success, WTAP_FILE_UNKNOWN on failure */
-int snoop_open(wtap *wth)
+static int snoop_read(wtap *wth, int *err);
+
+int snoop_open(wtap *wth, int *err)
 {
        int bytes_read;
        char magic[sizeof snoop_magic];
@@ -75,43 +77,55 @@ int snoop_open(wtap *wth)
 
        /* Read in the string that should be at the start of a "snoop" file */
        fseek(wth->fh, 0, SEEK_SET);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(magic, 1, sizeof magic, wth->fh);
-
        if (bytes_read != sizeof magic) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
-               return WTAP_FILE_UNKNOWN;
+               return 0;
        }
 
        /* Read the rest of the header. */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh);
        if (bytes_read != sizeof hdr) {
-               return WTAP_FILE_UNKNOWN;
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
+               return 0;
        }
 
        hdr.version = ntohl(hdr.version);
        if (hdr.version != 2) {
                /* We only support version 2. */
-               return WTAP_FILE_UNKNOWN;
+               g_message("snoop: version %d unsupported", hdr.version);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
        hdr.network = ntohl(hdr.network);
        if (hdr.network >= NUM_SNOOP_ENCAPS) {
-               g_error("snoop: network type %d unknown", hdr.network);
-               return WTAP_FILE_UNKNOWN;
+               g_message("snoop: network type %d unknown", hdr.network);
+               *err = WTAP_ERR_UNSUPPORTED;
+               return -1;
        }
 
        /* This is a snoop file */
+       wth->file_type = WTAP_FILE_SNOOP;
        wth->subtype_read = snoop_read;
        wth->file_encap = snoop_encap[hdr.network];
        wth->snapshot_length = 16384;   /* XXX - not available in header */
-
-       return WTAP_FILE_SNOOP;
+       return 1;
 }
 
 /* Read the next packet */
-int snoop_read(wtap *wth)
+static int snoop_read(wtap *wth, int *err)
 {
        int     packet_size;
        int     bytes_read;
@@ -119,11 +133,15 @@ int snoop_read(wtap *wth)
        int     data_offset;
 
        /* Read record header. */
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh);
        if (bytes_read != sizeof hdr) {
+               if (ferror(wth->fh)) {
+                       *err = errno;
+                       return -1;
+               }
                if (bytes_read != 0) {
-                       g_error("snoop_read: not enough packet header data (%d bytes)",
-                                       bytes_read);
+                       *err = WTAP_ERR_SHORT_READ;
                        return -1;
                }
                return 0;
@@ -132,16 +150,15 @@ int snoop_read(wtap *wth)
        packet_size = ntohl(hdr.incl_len);
        buffer_assure_space(wth->frame_buffer, packet_size);
        data_offset = ftell(wth->fh);
+       errno = WTAP_ERR_CANT_READ;
        bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
                        packet_size, wth->fh);
 
        if (bytes_read != packet_size) {
-               if (ferror(wth->fh)) {
-                       g_error("snoop_read: fread for data: read error\n");
-               } else {
-                       g_error("snoop_read: fread for data: %d bytes out of %d",
-                               bytes_read, packet_size);
-               }
+               if (ferror(wth->fh))
+                       *err = errno;
+               else
+                       *err = WTAP_ERR_SHORT_READ;
                return -1;
        }
 
index d1d5461de208250ec03560b76686fc8366a5f9d2..20e559b09c4402022cc21827c77d86dc4a1fe04f 100644 (file)
@@ -1,6 +1,6 @@
 /* snoop.h
  *
- * $Id: snoop.h,v 1.1 1998/11/15 05:29:15 guy Exp $
+ * $Id: snoop.h,v 1.2 1999/08/19 05:31:35 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -21,5 +21,4 @@
  *
  */
 
-int snoop_open(wtap *wth);
-int snoop_read(wtap *wth);
+int snoop_open(wtap *wth, int *err);
index 49f9312f42a91686779355011e0c51d644a448ea..52e96dc3185a5a7da9600d683f2daf2b4547aff6 100644 (file)
@@ -1,6 +1,6 @@
 /* wtap.c
  *
- * $Id: wtap.c,v 1.14 1999/08/18 04:17:37 guy Exp $
+ * $Id: wtap.c,v 1.15 1999/08/19 05:31:33 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -127,13 +127,19 @@ void wtap_close(wtap *wth)
        fclose(wth->fh);
 }
 
-void wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user)
+int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
+       int *err)
 {
        int data_offset, loop = 0;
 
-       while ((data_offset = wth->subtype_read(wth)) > 0) {
+       while ((data_offset = wth->subtype_read(wth, err)) > 0) {
                callback(user, &wth->phdr, data_offset,
                    buffer_start_ptr(wth->frame_buffer));
-               if (count > 0 && ++loop >= count) break;
+               if (count > 0 && ++loop >= count)
+                       break;
        }
+       if (data_offset < 0)
+               return FALSE;   /* failure */
+       else
+               return TRUE;    /* success */
 }
index ff9c689074c3fd2d7450ff84b15bbca0a639a1b9..3f9bcc3c69eda472b1dbd0205976fdced0974b8c 100644 (file)
@@ -1,6 +1,6 @@
 /* wtap.h
  *
- * $Id: wtap.h,v 1.26 1999/08/18 17:08:47 guy Exp $
+ * $Id: wtap.h,v 1.27 1999/08/19 05:31:38 guy Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@@ -153,7 +153,7 @@ struct wtap;
 struct bpf_instruction;
 struct Buffer;
 
-typedef int (*subtype_func)(struct wtap*);
+typedef int (*subtype_read_func)(struct wtap*, int*);
 typedef struct wtap {
        FILE*                   fh;
        int                     file_type;
@@ -170,7 +170,7 @@ typedef struct wtap {
                netxray_t               *netxray;
        } capture;
 
-       subtype_func            subtype_read;
+       subtype_read_func       subtype_read;
        int                     file_encap;     /* per-file, for those
                                                   file formats that have
                                                   per-file encapsulation
@@ -201,7 +201,7 @@ typedef struct wtap_dumper {
  * a negative number, indicating the type of error, on other failures.
  */
 wtap* wtap_open_offline(const char *filename, int *err);
-void wtap_loop(wtap *wth, int, wtap_handler, u_char*);
+int wtap_loop(wtap *wth, int, wtap_handler, u_char*, int*);
 
 FILE* wtap_file(wtap *wth);
 int wtap_snapshot_length(wtap *wth); /* per file */
@@ -228,15 +228,24 @@ int wtap_pcap_encap_to_wtap_encap(int encap);
        /* The file being opened for reading isn't a plain file */
 #define        WTAP_ERR_FILE_UNKNOWN_FORMAT            -2
        /* The file being opened is not a capture file in a known format */
-#define        WTAP_ERR_CANT_OPEN                      -3
+#define        WTAP_ERR_UNSUPPORTED                    -3
+       /* Supported file type, but there's something in the file we
+          can't support */
+#define        WTAP_ERR_CANT_OPEN                      -4
        /* The file couldn't be opened, reason unknown */
-#define        WTAP_ERR_UNSUPPORTED_FILE_TYPE          -4
+#define        WTAP_ERR_UNSUPPORTED_FILE_TYPE          -5
        /* Wiretap can't save files in the specified format */
-#define        WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED   -5
+#define        WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED   -6
        /* The specified format doesn't support per-packet encapsulations */
-#define        WTAP_ERR_CANT_CLOSE                     -6
+#define        WTAP_ERR_CANT_CLOSE                     -7
        /* The file couldn't be closed, reason unknown */
-#define        WTAP_ERR_SHORT_WRITE                    -7
+#define        WTAP_ERR_CANT_READ                      -8
+       /* An attempt to read failed, reason unknown */
+#define        WTAP_ERR_SHORT_READ                     -9
+       /* An attempt to read read less data than it should have */
+#define        WTAP_ERR_BAD_RECORD                     -10
+       /* We read an invalid record */
+#define        WTAP_ERR_SHORT_WRITE                    -11
        /* An attempt to write wrote less data than it should have */
 
 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a