Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / wiretap / ascend.c
index 28b69da107e18efd526fc57df9776b342763ebc7..73c2202b7492fab9c81a537614bd6064b2392b6e 100644 (file)
@@ -1,25 +1,25 @@
 /* ascend.c
  *
- * $Id: ascend.c,v 1.26 2001/11/13 23:55:43 gram Exp $
+ * $Id$
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
- *
  */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -29,6 +29,8 @@
 #include "ascend-int.h"
 #include "file_wrappers.h"
 
+#include <errno.h>
+
 #ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
    around quite a bit, otherwise I'd put a more specific URL here.
 
    Example 'wandsess' output data:
-   
+
 RECV-iguana:241:(task: B02614C0, time: 1975432.85) 49 octets @ 8003BD94
-  [0000]: FF 03 00 3D C0 06 CA 22 2F 45 00 00 28 6A 3B 40 
-  [0010]: 00 3F 03 D7 37 CE 41 62 12 CF 00 FB 08 20 27 00 
-  [0020]: 50 E4 08 DD D7 7C 4C 71 92 50 10 7D 78 67 C8 00 
-  [0030]: 00 
+  [0000]: FF 03 00 3D C0 06 CA 22 2F 45 00 00 28 6A 3B 40
+  [0010]: 00 3F 03 D7 37 CE 41 62 12 CF 00 FB 08 20 27 00
+  [0020]: 50 E4 08 DD D7 7C 4C 71 92 50 10 7D 78 67 C8 00
+  [0030]: 00
 XMIT-iguana:241:(task: B04E12C0, time: 1975432.85) 53 octets @ 8009EB16
-  [0000]: FF 03 00 3D C0 09 1E 31 21 45 00 00 2C 2D BD 40 
-  [0010]: 00 7A 06 D8 B1 CF 00 FB 08 CE 41 62 12 00 50 20 
-  [0020]: 29 7C 4C 71 9C 9A 6A 93 A4 60 12 22 38 3F 10 00 
-  [0030]: 00 02 04 05 B4 
+  [0000]: FF 03 00 3D C0 09 1E 31 21 45 00 00 2C 2D BD 40
+  [0010]: 00 7A 06 D8 B1 CF 00 FB 08 CE 41 62 12 00 50 20
+  [0020]: 29 7C 4C 71 9C 9A 6A 93 A4 60 12 22 38 3F 10 00
+  [0030]: 00 02 04 05 B4
 
     Example 'wdd' output data:
 
@@ -78,8 +80,8 @@ WD_DIALOUT_DISP: chunk 2515EE type IP.
 
   Note that a maximum of eight rows will be displayed (for a maximum of
   128 bytes), no matter what the octet count is.
-  
-  When reading a packet, the module prepends an ascend_pkt_hdr to the 
+
+  When reading a packet, the module prepends an ascend_pkt_hdr to the
   data.
 
  */
@@ -99,9 +101,11 @@ static const char ascend_w2magic[] = { 'W', 'D', '_', 'D', 'I', 'A', 'L', 'O', '
 #define ASCEND_W1_SIZE (sizeof ascend_w1magic / sizeof ascend_w1magic[0])
 #define ASCEND_W2_SIZE (sizeof ascend_w2magic / sizeof ascend_w2magic[0])
 
-static gboolean ascend_read(wtap *wth, int *err, long *data_offset);
-static int ascend_seek_read (wtap *wth, long seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len);
+static gboolean ascend_read(wtap *wth, int *err, gchar **err_info,
+       long *data_offset);
+static gboolean ascend_seek_read(wtap *wth, long seek_off,
+       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
+       int *err, gchar **err_info);
 static void ascend_close(wtap *wth);
 
 /* Seeks to the beginning of the next packet, and returns the
@@ -112,8 +116,7 @@ static void ascend_close(wtap *wth);
    it sets "wth->capture.ascend->next_packet_seek_start" to the point
    at which the seek pointer should be set before this routine is called
    to find the packet *after* the packet it finds. */
-/* XXX - Handle I/O errors. */
-static long ascend_seek(wtap *wth, int max_seek)
+static long ascend_seek(wtap *wth, int max_seek, int *err)
 {
   int byte, bytes_read = 0;
   long date_off = -1, cur_off, packet_off;
@@ -125,6 +128,11 @@ static long ascend_seek(wtap *wth, int max_seek)
       if (x_level >= ASCEND_X_SIZE) {
         /* At what offset are we now? */
         cur_off = file_tell(wth->fh);
+        if (cur_off == -1) {
+          /* Error. */
+          *err = file_error(wth->fh);
+          return -1;
+        }
 
         /* Back up over the header we just read; that's where a read
            of this packet should start. */
@@ -139,6 +147,11 @@ static long ascend_seek(wtap *wth, int max_seek)
       if (r_level >= ASCEND_R_SIZE) {
         /* At what offset are we now? */
         cur_off = file_tell(wth->fh);
+        if (cur_off == -1) {
+          /* Error. */
+          *err = file_error(wth->fh);
+          return -1;
+        }
 
         /* Back up over the header we just read; that's where a read
            of this packet should start. */
@@ -152,7 +165,14 @@ static long ascend_seek(wtap *wth, int max_seek)
       w1_level++;
       if (w1_level >= ASCEND_W1_SIZE) {
         /* Get the offset at which the "Date:" header started. */
-        date_off = file_tell(wth->fh) - ASCEND_W1_SIZE;
+        cur_off = file_tell(wth->fh);
+        if (cur_off == -1) {
+          /* Error. */
+          *err = file_error(wth->fh);
+          return -1;
+        }
+
+        date_off = cur_off - ASCEND_W1_SIZE;
       }
     } else {
       w1_level = 0;
@@ -162,6 +182,12 @@ static long ascend_seek(wtap *wth, int max_seek)
       if (w2_level >= ASCEND_W2_SIZE) {
         /* At what offset are we now? */
         cur_off = file_tell(wth->fh);
+        if (cur_off == -1) {
+          /* Error. */
+          *err = file_error(wth->fh);
+          return -1;
+        }
+
         if (date_off != -1) {
           /* This packet has a date/time header; a read of it should
              start at the beginning of *that* header. */
@@ -179,6 +205,14 @@ static long ascend_seek(wtap *wth, int max_seek)
     }
     bytes_read++;
   }
+  if (byte != EOF || file_eof(wth->fh)) {
+    /* Either we didn't find the offset, or we got an EOF. */
+    *err = 0;
+  } else {
+    /* We (presumably) got an error (there's no equivalent to "ferror()"
+       in zlib, alas, so we don't have a wrapper to check for an error). */
+    *err = file_error(wth->fh);
+  }
   return -1;
 
 found:
@@ -195,12 +229,12 @@ found:
    * Move to where the read for this packet should start, and return
    * that seek offset.
    */
-  file_seek(wth->fh, packet_off, SEEK_SET);
+  if (file_seek(wth->fh, packet_off, SEEK_SET, err) == -1)
+    return -1;
   return packet_off;
 }
 
-/* XXX - return -1 on I/O error and actually do something with 'err'. */
-int ascend_open(wtap *wth, int *err)
+int ascend_open(wtap *wth, int *err, gchar **err_info _U_)
 {
   long offset;
   struct stat statbuf;
@@ -210,9 +244,12 @@ int ascend_open(wtap *wth, int *err)
      fill it in. */
   wth->capture.ascend = NULL;
 
-  offset = ascend_seek(wth, ASCEND_MAX_SEEK);
+  offset = ascend_seek(wth, ASCEND_MAX_SEEK, err);
   if (offset == -1) {
-    return 0;
+    if (*err == 0)
+      return 0;
+    else
+      return -1;
   }
 
   wth->data_offset = offset;
@@ -229,12 +266,16 @@ int ascend_open(wtap *wth, int *err)
      found it. */
   wth->capture.ascend->next_packet_seek_start = offset;
 
-  /* MAXen and Pipelines report the time since reboot.  In order to keep 
+  /* MAXen and Pipelines report the time since reboot.  In order to keep
      from reporting packet times near the epoch, we subtract the first
      packet's timestamp from the capture file's ctime, which gives us an
      offset that we can apply to each packet.
    */
-  fstat(wtap_fd(wth), &statbuf);
+  if (fstat(wtap_fd(wth), &statbuf) == -1) {
+    *err = errno;
+    g_free(wth->capture.ascend);
+    return -1;
+  }
   wth->capture.ascend->inittime = statbuf.st_ctime;
   wth->capture.ascend->adjusted = 0;
 
@@ -244,24 +285,26 @@ int ascend_open(wtap *wth, int *err)
 }
 
 /* Read the next packet; called from wtap_loop(). */
-static gboolean ascend_read(wtap *wth, int *err, long *data_offset)
+static gboolean ascend_read(wtap *wth, int *err, gchar **err_info,
+       long *data_offset)
 {
   long offset;
   guint8 *buf = buffer_start_ptr(wth->frame_buffer);
   ascend_pkthdr header;
 
   /* (f)lex reads large chunks of the file into memory, so file_tell() doesn't
-     give us the correct location of the packet.  Instead, we seek to the 
+     give us the correct location of the packet.  Instead, we seek to the
      offset after the header of the previous packet and try to find the next
      packet.  */
-  file_seek(wth->fh, wth->capture.ascend->next_packet_seek_start, SEEK_SET);
-  offset = ascend_seek(wth, ASCEND_MAX_SEEK);
-  if (offset == -1) {
-    *err = 0;          /* XXX - assume, for now, that it's an EOF */
+  if (file_seek(wth->fh, wth->capture.ascend->next_packet_seek_start,
+                SEEK_SET, err) == -1)
+    return FALSE;
+  offset = ascend_seek(wth, ASCEND_MAX_SEEK, err);
+  if (offset == -1)
     return FALSE;
-  }
   if (! parse_ascend(wth->fh, buf, &wth->pseudo_header.ascend, &header, 0)) {
     *err = WTAP_ERR_BAD_RECORD;
+    *err_info = g_strdup((ascend_parse_error != NULL) ? ascend_parse_error : "parse error");
     return FALSE;
   }
 
@@ -290,18 +333,24 @@ static gboolean ascend_read(wtap *wth, int *err, long *data_offset)
   wth->phdr.ts.tv_usec = header.usecs;
   wth->phdr.caplen = header.caplen;
   wth->phdr.len = header.len;
-  wth->phdr.pkt_encap = wth->file_encap;
   wth->data_offset = offset;
 
   *data_offset = offset;
   return TRUE;
 }
 
-static int ascend_seek_read (wtap *wth, long seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len)
+static gboolean ascend_seek_read(wtap *wth, long seek_off,
+       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
+       int *err, gchar **err_info)
 {
-  file_seek(wth->random_fh, seek_off, SEEK_SET);
-  return parse_ascend(wth->random_fh, pd, &pseudo_header->ascend, NULL, len);
+  if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
+    return FALSE;
+  if (! parse_ascend(wth->random_fh, pd, &pseudo_header->ascend, NULL, len)) {
+    *err = WTAP_ERR_BAD_RECORD;
+    *err_info = g_strdup((ascend_parse_error != NULL) ? ascend_parse_error : "parse error");
+    return FALSE;
+  }
+  return TRUE;
 }
 
 static void ascend_close(wtap *wth)