From Tomasz Mon via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8503 :
[metze/wireshark/wip.git] / wiretap / vms.c
index 8173e576cf07a8c734044af4ac434c99be54640a..56ae0c378e664755ffa8d61294439387dec2042f 100644 (file)
@@ -17,7 +17,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 /* Notes:
@@ -27,9 +27,7 @@
  *   TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE to expect the first line to be
  *   at the start of every packet.
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 #include "wtap-int.h"
 #include "buffer.h"
 #include "vms.h"
 
    Example UCX$TRACE output data:
 
-    UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93 
+    UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93
 
-   IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 583 = ^x0247 
-   IP Identifier  = ^x702E,  Flags (0=0,DF=0,MF=0),  
-         Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000 
-   IP TTL = 128 = ^x80,  Protocol = 17 = ^x11,  Header Checksum = ^x70EC 
-   IP Source Address      = 10.20.4.159 
-   IP Destination Address = 10.20.4.255 
+   IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 583 = ^x0247
+   IP Identifier  = ^x702E,  Flags (0=0,DF=0,MF=0),
+         Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
+   IP TTL = 128 = ^x80,  Protocol = 17 = ^x11,  Header Checksum = ^x70EC
+   IP Source Address      = 10.20.4.159
+   IP Destination Address = 10.20.4.255
 
-   UDP Source Port = 138,   UDP Destination Port = 138 
-   UDP Header and Datagram Length = 563 = ^x0233,   Checksum = ^xB913 
+   UDP Source Port = 138,   UDP Destination Port = 138
+   UDP Header and Datagram Length = 563 = ^x0233,   Checksum = ^xB913
 
    9F04140A   70EC1180   0000702E   47020045    0000    E..G.p.....p....
    B1B80E11 | B9133302   8A008A00 | FF04140A    0010    .........3......
@@ -146,53 +144,44 @@ to handle them.
 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean vms_seek_read(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
+    struct wtap_pkthdr *phdr, guint8 *pd, int len,
     int *err, gchar **err_info);
 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf,
     long byte_offset, int in_off, int remaining_bytes);
 static gboolean parse_vms_hex_dump(FILE_T fh, int pkt_len, guint8* buf,
     int *err, gchar **err_info);
-static int parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info);
+static gboolean parse_vms_rec_hdr(FILE_T fh, struct wtap_pkthdr *phdr,
+    int *err, gchar **err_info);
 
 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
 /* Seeks to the beginning of the next packet, and returns the
-   byte offset.  Returns -1 on failure, and sets "*err" to the error. */
-static long vms_seek_next_packet(wtap *wth, int *err)
+   byte offset.  Returns -1 on failure, and sets "*err" to the error
+   and sets "*err_info" to null or an additional error string. */
+static long vms_seek_next_packet(wtap *wth, int *err, gchar **err_info)
 {
-  long cur_off;
-  char buf[VMS_LINE_LENGTH];
-  
-  while (1) {
-    cur_off = file_tell(wth->fh);
-    if (cur_off == -1) {
-      /* Error */
-      *err = file_error(wth->fh, err_info);
-      hdr = NULL;
-      return -1;
-    }
-    if (file_gets(buf, sizeof(buf), wth->fh) != NULL) {
-      if (strstr(buf, VMS_REC_MAGIC_STR1) ||
-         strstr(buf, VMS_REC_MAGIC_STR2) ||
-         strstr(buf, VMS_REC_MAGIC_STR2)) {
-                 g_strlcpy(hdr, buf,VMS_LINE_LENGTH);
-                 return cur_off;
-      }
-    } else {
-      if (file_eof(wth->fh)) {
-       /* 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, err_info);
-      }
-      break;
+    long cur_off;
+    char buf[VMS_LINE_LENGTH];
+
+    while (1) {
+        cur_off = file_tell(wth->fh);
+        if (cur_off == -1) {
+            /* Error */
+            *err = file_error(wth->fh, err_info);
+            return -1;
+        }
+        if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
+            /* EOF or error. */
+            *err = file_error(wth->fh, err_info);
+            break;
+        }
+        if (strstr(buf, VMS_REC_MAGIC_STR1) ||
+            strstr(buf, VMS_REC_MAGIC_STR2) ||
+            strstr(buf, VMS_REC_MAGIC_STR2)) {
+            g_strlcpy(hdr, buf,VMS_LINE_LENGTH);
+            return cur_off;
+        }
     }
-  }
-  hdr = NULL;
-  return -1;
+    return -1;
 }
 #endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
 
@@ -203,55 +192,51 @@ static long vms_seek_next_packet(wtap *wth, int *err)
  * if we get an I/O error, "*err" will be set to a non-zero value and
  * "*err_info will be set to null or an additional error string.
  *
- * Leaves file handle at begining of line that contains the VMS Magic
+ * Leaves file handle at beginning of line that contains the VMS Magic
  * identifier.
  */
 static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info)
 {
-  char buf[VMS_LINE_LENGTH];
-  guint        reclen, line;
-  gint64 mpos;
-  
-  buf[VMS_LINE_LENGTH-1] = '\0';
-  
-  for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
-    mpos = file_tell(wth->fh);
-    if (mpos == -1) {
-      /* Error. */
-      *err = file_error(wth->fh, err_info);
-      return FALSE;
-    }
-    if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) != NULL) {
-      
-      reclen = (guint) strlen(buf);
-      if (reclen < strlen(VMS_HDR_MAGIC_STR1) ||
-         reclen < strlen(VMS_HDR_MAGIC_STR2) || 
-         reclen < strlen(VMS_HDR_MAGIC_STR3)) {
-       continue;
-      }
-      
-      if (strstr(buf, VMS_HDR_MAGIC_STR1) ||
-         strstr(buf, VMS_HDR_MAGIC_STR2) ||
-         strstr(buf, VMS_HDR_MAGIC_STR3)) {
-       /* Go back to the beginning of this line, so we will
-        * re-read it. */
-       if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
-         /* Error. */
-         return FALSE;
-       }
-       return TRUE;
-      }
-    } else {
-      /* EOF or error. */
-      if (file_eof(wth->fh))
-       *err = 0;
-      else
-       *err = file_error(wth->fh, err_info);
-      return FALSE;
+    char buf[VMS_LINE_LENGTH];
+    guint reclen, line;
+    gint64 mpos;
+
+    buf[VMS_LINE_LENGTH-1] = '\0';
+
+    for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
+        mpos = file_tell(wth->fh);
+        if (mpos == -1) {
+            /* Error. */
+            *err = file_error(wth->fh, err_info);
+            return FALSE;
+        }
+        if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) == NULL) {
+            /* EOF or error. */
+            *err = file_error(wth->fh, err_info);
+            return FALSE;
+        }
+
+        reclen = (guint) strlen(buf);
+        if (reclen < strlen(VMS_HDR_MAGIC_STR1) ||
+            reclen < strlen(VMS_HDR_MAGIC_STR2) ||
+            reclen < strlen(VMS_HDR_MAGIC_STR3)) {
+            continue;
+        }
+
+        if (strstr(buf, VMS_HDR_MAGIC_STR1) ||
+            strstr(buf, VMS_HDR_MAGIC_STR2) ||
+            strstr(buf, VMS_HDR_MAGIC_STR3)) {
+            /* Go back to the beginning of this line, so we will
+             * re-read it. */
+            if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
+                /* Error. */
+                return FALSE;
+            }
+            return TRUE;
+        }
     }
-  }
-  *err = 0;
-  return FALSE;
+    *err = 0;
+    return FALSE;
 }
 
 
@@ -259,10 +244,9 @@ int vms_open(wtap *wth, int *err, gchar **err_info)
 {
     /* Look for VMS header */
     if (!vms_check_file_type(wth, err, err_info)) {
-        if (*err == 0)
-            return 0;
-        else
+        if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
             return -1;
+        return 0;
     }
 
     wth->file_encap = WTAP_ENCAP_RAW_IP;
@@ -270,7 +254,7 @@ int vms_open(wtap *wth, int *err, gchar **err_info)
     wth->snapshot_length = 0; /* not known */
     wth->subtype_read = vms_read;
     wth->subtype_seek_read = vms_seek_read;
-       wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
+    wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
 
     return 1;
 }
@@ -281,11 +265,10 @@ static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
 {
     gint64   offset = 0;
     guint8    *buf;
-    int    pkt_len;
 
     /* Find the next packet */
 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
-    offset = vms_seek_next_packet(wth, err);
+    offset = vms_seek_next_packet(wth, err, err_info);
 #else
     offset = file_tell(wth->fh);
 #endif
@@ -295,16 +278,15 @@ static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
     }
 
     /* Parse the header */
-    pkt_len = parse_vms_rec_hdr(wth, wth->fh, err, err_info);
-    if (pkt_len == -1)
+    if (!parse_vms_rec_hdr(wth->fh, &wth->phdr, err, err_info))
        return FALSE;
 
     /* Make sure we have enough room for the packet */
-    buffer_assure_space(wth->frame_buffer, pkt_len);
+    buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
     buf = buffer_start_ptr(wth->frame_buffer);
 
     /* Convert the ASCII hex dump to binary data */
-    if (!parse_vms_hex_dump(wth->fh, pkt_len, buf, err, err_info))
+    if (!parse_vms_hex_dump(wth->fh, wth->phdr.caplen, buf, err, err_info))
         return FALSE;
 
     *data_offset = offset;
@@ -313,27 +295,23 @@ static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
 
 /* Used to read packets in random-access fashion */
 static gboolean
-vms_seek_read (wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header _U_,
+vms_seek_read (wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
     guint8 *pd, int len, int *err, gchar **err_info)
 {
-    int    pkt_len;
-
     if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
         return FALSE;
 
-    pkt_len = parse_vms_rec_hdr(NULL, wth->random_fh, err, err_info);
+    if (!parse_vms_rec_hdr(wth->random_fh, phdr, err, err_info))
+        return FALSE;
 
-    if (pkt_len != len) {
-        if (pkt_len != -1) {
-            *err = WTAP_ERR_BAD_FILE;
-            *err_info = g_strdup_printf("vms: requested length %d doesn't match length %d",
-                len, pkt_len);
-        }
+    if (phdr->caplen != (guint32)len) {
+        *err = WTAP_ERR_BAD_FILE;
+        *err_info = g_strdup_printf("vms: requested length %d doesn't match length %d",
+            len, phdr->caplen);
         return FALSE;
     }
 
-    return parse_vms_hex_dump(wth->random_fh, pkt_len, pd, err, err_info);
+    return parse_vms_hex_dump(wth->random_fh, phdr->caplen, pd, err, err_info);
 }
 
 /* isdumpline assumes that dump lines start with some non-alphanumerics
@@ -362,8 +340,8 @@ isdumpline( gchar *line )
 }
 
 /* Parses a packet record header. */
-static int
-parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info)
+static gboolean
+parse_vms_rec_hdr(FILE_T fh, struct wtap_pkthdr *phdr, int *err, gchar **err_info)
 {
     char   line[VMS_LINE_LENGTH + 1];
     int    num_items_scanned;
@@ -389,7 +367,7 @@ parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info)
            if ((*err == 0) && (csec != 101)) {
                *err = WTAP_ERR_SHORT_READ;
             }
-            return -1;
+            return FALSE;
         }
        line[VMS_LINE_LENGTH] = '\0';
 
@@ -398,7 +376,7 @@ parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info)
            /* Find text in line starting with "packet ". */
 
            /* First look for the Format 1 type sequencing */
-           num_items_scanned = sscanf(p,  
+           num_items_scanned = sscanf(p,
                                       "packet %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
                                       &pktnum, &tm.tm_mday, mon,
                                       &tm.tm_year, &tm.tm_hour,
@@ -416,7 +394,7 @@ parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info)
            if (num_items_scanned != 8) {
                *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup_printf("vms: header line not valid");
-               return -1;
+               return FALSE;
            }
        }
         if ( (! pkt_len) && (p = strstr(line, "Length"))) {
@@ -427,7 +405,7 @@ parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info)
             if ( !*p ) {
                 *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup_printf("vms: Length field not valid");
-                return -1;
+                return FALSE;
             }
 
             pkt_len = atoi(p);
@@ -435,21 +413,19 @@ parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info)
         }
     } while (! isdumpline(line));
 
-    if (wth) {
-        p = strstr(months, mon);
-        if (p)
-            tm.tm_mon = (int) (p - months) / 3;
-        tm.tm_year -= 1900;
-
-       tm.tm_isdst = -1;
-        wth->phdr.presence_flags = WTAP_HAS_TS;
-        wth->phdr.ts.secs = mktime(&tm);
-        wth->phdr.ts.nsecs = csec * 10000000;
-        wth->phdr.caplen = pkt_len;
-        wth->phdr.len = pkt_len;
-    }
+    p = strstr(months, mon);
+    if (p)
+        tm.tm_mon = (int) (p - months) / 3;
+    tm.tm_year -= 1900;
+    tm.tm_isdst = -1;
 
-    return pkt_len;
+    phdr->presence_flags = WTAP_HAS_TS;
+    phdr->ts.secs = mktime(&tm);
+    phdr->ts.nsecs = csec * 10000000;
+    phdr->caplen = pkt_len;
+    phdr->len = pkt_len;
+
+    return TRUE;
 }
 
 /* Converts ASCII hex dump to binary data */