Rename WTAP_ERR_REC_TYPE_UNSUPPORTED to WTAP_ERR_UNWRITABLE_REC_TYPE.
[metze/wireshark/wip.git] / wiretap / visual.c
index 8c70495f9c99f966c575075cc4f1adbf071ea31c..3bb8043e868c29519d090f675d2a9bb4b11e5a7a 100644 (file)
@@ -2,8 +2,6 @@
  * File read and write routines for Visual Networks cap files.
  * Copyright (c) 2001, Tom Nisbet  tnisbet@visualnetworks.com
  *
- * $Id$
- *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
@@ -27,7 +25,7 @@
 #include <string.h>
 #include "wtap-int.h"
 #include "file_wrappers.h"
-#include "buffer.h"
+#include <wsutil/buffer.h>
 #include "visual.h"
 
 /*
@@ -163,49 +161,39 @@ struct visual_write_info
 static gboolean visual_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, Buffer *buf, int packet_size,
-    int *err, gchar **err_info);
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
 static gboolean visual_read_packet(wtap *wth, FILE_T fh,
     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
 static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const guint8 *pd, int *err);
+    const guint8 *pd, int *err, gchar **err_info);
 static gboolean visual_dump_close(wtap_dumper *wdh, int *err);
 static void visual_dump_free(wtap_dumper *wdh);
 
 
 /* Open a file for reading */
-int visual_open(wtap *wth, int *err, gchar **err_info)
+wtap_open_return_val visual_open(wtap *wth, int *err, gchar **err_info)
 {
-    int bytes_read;
     char magic[sizeof visual_magic];
     struct visual_file_hdr vfile_hdr;
     struct visual_read_info * visual;
     int encap;
 
     /* Check the magic string at the start of the file */
-    errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(magic, sizeof magic, wth->fh);
-    if (bytes_read != sizeof magic)
+    if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info))
     {
-        *err = file_error(wth->fh, err_info);
-        if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-            return -1;
-        return 0;
+        if (*err != WTAP_ERR_SHORT_READ)
+            return WTAP_OPEN_ERROR;
+        return WTAP_OPEN_NOT_MINE;
     }
     if (memcmp(magic, visual_magic, sizeof visual_magic) != 0)
     {
-        return 0;
+        return WTAP_OPEN_NOT_MINE;
     }
 
     /* Read the rest of the file header. */
-    errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(&vfile_hdr, sizeof vfile_hdr, wth->fh);
-    if (bytes_read != sizeof vfile_hdr)
+    if (!wtap_read_bytes(wth->fh, &vfile_hdr, sizeof vfile_hdr, err, err_info))
     {
-        *err = file_error(wth->fh, err_info);
-        if (*err == 0)
-            *err = WTAP_ERR_SHORT_READ;
-        return -1;
+        return WTAP_OPEN_ERROR;
     }
 
     /* Verify the file version is known */
@@ -214,7 +202,7 @@ int visual_open(wtap *wth, int *err, gchar **err_info)
     {
         *err = WTAP_ERR_UNSUPPORTED;
         *err_info = g_strdup_printf("visual: file version %u unsupported", vfile_hdr.file_version);
-        return -1;
+        return WTAP_OPEN_ERROR;
     }
 
     /* Translate the encapsulation type; these values are SNMP ifType
@@ -253,10 +241,10 @@ int visual_open(wtap *wth, int *err, gchar **err_info)
        break;
 
     default:
-        *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+        *err = WTAP_ERR_UNSUPPORTED;
         *err_info = g_strdup_printf("visual: network type %u unknown or unsupported",
                                      vfile_hdr.media_type);
-        return -1;
+        return WTAP_OPEN_ERROR;
     }
 
     /* Fill in the wiretap struct with data from the file header */
@@ -267,7 +255,7 @@ int visual_open(wtap *wth, int *err, gchar **err_info)
     /* Set up the pointers to the handlers for this file type */
     wth->subtype_read = visual_read;
     wth->subtype_seek_read = visual_seek_read;
-    wth->tsprecision = WTAP_FILE_TSPREC_USEC;
+    wth->file_tsprec = WTAP_TSPREC_USEC;
 
     /* Add Visual-specific information to the wiretap struct for later use. */
     visual = (struct visual_read_info *)g_malloc(sizeof(struct visual_read_info));
@@ -276,7 +264,7 @@ int visual_open(wtap *wth, int *err, gchar **err_info)
     visual->start_time = ((double) pletoh32(&vfile_hdr.start_time)) * 1000000;
     visual->current_pkt = 1;
 
-    return 1;
+    return WTAP_OPEN_MINE;
 }
 
 
@@ -307,8 +295,7 @@ static gboolean visual_read(wtap *wth, int *err, gchar **err_info,
 
 /* Read packet header and data for random access. */
 static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, Buffer *buf, int len _U_,
-    int *err, gchar **err_info)
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
 {
     /* Seek to the packet header */
     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
@@ -329,7 +316,6 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
 {
     struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
     struct visual_pkt_hdr vpkt_hdr;
-    int bytes_read;
     guint32 packet_size;
     struct visual_atm_hdr vatm_hdr;
     double  t;
@@ -339,21 +325,15 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
     guint8 *pd;
 
     /* Read the packet header. */
-    errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(&vpkt_hdr, (unsigned int)sizeof vpkt_hdr, fh);
-    if (bytes_read < 0 || (size_t)bytes_read != sizeof vpkt_hdr)
+    if (!wtap_read_bytes_or_eof(fh, &vpkt_hdr, (unsigned int)sizeof vpkt_hdr, err, err_info))
     {
-        *err = file_error(fh, err_info);
-        if (*err == 0 && bytes_read != 0)
-        {
-            *err = WTAP_ERR_SHORT_READ;
-        }
         return FALSE;
     }
 
     /* Get the included length of data. This includes extra headers + payload */
     packet_size = pletoh16(&vpkt_hdr.incl_len);
 
+    phdr->rec_type = REC_TYPE_PACKET;
     phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
 
     /* Set the packet time and length. */
@@ -462,15 +442,8 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
 
            ATM packets have an additional packet header; read and
            process it. */
-        errno = WTAP_ERR_CANT_READ;
-        bytes_read = file_read(&vatm_hdr, (unsigned int)sizeof vatm_hdr, fh);
-        if (bytes_read < 0 || (size_t)bytes_read != sizeof vatm_hdr)
+        if (!wtap_read_bytes(fh, &vatm_hdr, (unsigned int)sizeof vatm_hdr, err, err_info))
         {
-            *err = file_error(fh, err_info);
-            if (*err == 0)
-            {
-                *err = WTAP_ERR_SHORT_READ;
-            }
             return FALSE;
         }
 
@@ -554,12 +527,6 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
         return FALSE;
     }
 
-    /* Sanity check */
-    if (phdr->len < phdr->caplen)
-    {
-        phdr->len = phdr->caplen;
-    }
-
     /* Read the packet data */
     if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
         return FALSE;
@@ -582,7 +549,7 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
            be configured for auto-detect, in which case the encapsulation
            hint is 13, and the encapsulation must be guessed from the
            packet contents.  Auto-detect is the default. */
-        pd = buffer_start_ptr(buf);
+        pd = ws_buffer_start_ptr(buf);
 
         /* If PPP is specified in the encap hint, then use that */
         if (vpkt_hdr.encap_hint == 14)
@@ -638,7 +605,7 @@ int visual_dump_can_write_encap(int encap)
         return 0;
     }
 
-    return WTAP_ERR_UNSUPPORTED_ENCAP;
+    return WTAP_ERR_UNWRITABLE_ENCAP;
 }
 
 
@@ -675,7 +642,7 @@ gboolean visual_dump_open(wtap_dumper *wdh, int *err)
 /* Write a packet to a Visual dump file.
    Returns TRUE on success, FALSE on failure. */
 static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const guint8 *pd, int *err)
+    const guint8 *pd, int *err, gchar **err_info _U_)
 {
     const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
     struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
@@ -684,6 +651,18 @@ static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
     guint delta_msec;
     guint32 packet_status;
 
+    /* We can only write packet records. */
+    if (phdr->rec_type != REC_TYPE_PACKET) {
+        *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
+        return FALSE;
+    }
+
+    /* Don't write anything we're not willing to read. */
+    if (phdr->caplen > WTAP_MAX_PACKET_SIZE) {
+        *err = WTAP_ERR_PACKET_TOO_LARGE;
+        return FALSE;
+    }
+
     /* If the visual structure was never allocated then nothing useful
        can be done. */
     if (visual == 0)