From Harald Welte:
[obnox/wireshark/wip.git] / wiretap / nettl.c
index dad9c0212289aa72a661fce1bb3d8b137adb024e..735fe9edf7867f5c50cb318725d8e1714cb8d5b8 100644 (file)
 #define MAGIC_SIZE     12
 
 /* HP-UX 9.x */
-static guint8 nettl_magic_hpux9[MAGIC_SIZE] = {
+static const guint8 nettl_magic_hpux9[MAGIC_SIZE] = {
     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x00
 };
 /* HP-UX 10.x and 11.x */
-static guint8 nettl_magic_hpux10[MAGIC_SIZE] = {
+static const guint8 nettl_magic_hpux10[MAGIC_SIZE] = {
     0x54, 0x52, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80
 };
 
@@ -58,7 +58,7 @@ struct nettl_file_hdr {
     gchar      tz[20];
     gchar      host_name[9];
     gchar      os_vers[9];
-    guchar     os_v;
+    guint8     os_v;
     guint8     xxa[8];
     gchar      model[11];
     guint16    unknown;        /* just padding to 128 bytes? */
@@ -175,32 +175,38 @@ struct nettlrec_ns_ls_drv_eth_hdr {
 
 /* header is followed by data and once again the total length (2 bytes) ! */
 
+typedef struct {
+       gboolean is_hpux_11;
+} nettl_t;
+
 static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
                gint64 *data_offset);
 static gboolean nettl_seek_read(wtap *wth, gint64 seek_off,
-               union wtap_pseudo_header *pseudo_header, guchar *pd,
+               union wtap_pseudo_header *pseudo_header, guint8 *pd,
                int length, int *err, gchar **err_info);
 static int nettl_read_rec_header(wtap *wth, FILE_T fh,
                struct wtap_pkthdr *phdr, union wtap_pseudo_header *pseudo_header,
                int *err, gchar **err_info, gboolean *fddihack);
-static gboolean nettl_read_rec_data(FILE_T fh, guchar *pd, int length,
-               int *err, gboolean fddihack);
-static void nettl_close(wtap *wth);
+static gboolean nettl_read_rec_data(FILE_T fh, guint8 *pd, int length,
+               int *err, gchar **err_info, gboolean fddihack);
 static gboolean nettl_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
+    const union wtap_pseudo_header *pseudo_header, const guint8 *pd, int *err);
 
-int nettl_open(wtap *wth, int *err, gchar **err_info _U_)
+int nettl_open(wtap *wth, int *err, gchar **err_info)
 {
     struct nettl_file_hdr file_hdr;
     guint16 dummy[2];
     int subsys;
     int bytes_read;
+    nettl_t *nettl;
+
+    memset(&file_hdr, 0, sizeof(file_hdr));
 
     /* Read in the string that should be at the start of a HP file */
     errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(file_hdr.magic, 1, MAGIC_SIZE, wth->fh);
+    bytes_read = file_read(file_hdr.magic, MAGIC_SIZE, wth->fh);
     if (bytes_read != MAGIC_SIZE) {
-       *err = file_error(wth->fh);
+       *err = file_error(wth->fh, err_info);
        if (*err != 0)
            return -1;
        return 0;
@@ -212,10 +218,10 @@ int nettl_open(wtap *wth, int *err, gchar **err_info _U_)
     }
 
     /* Read the rest of the file header */
-    bytes_read = file_read(file_hdr.file_name, 1, FILE_HDR_SIZE - MAGIC_SIZE,
+    bytes_read = file_read(file_hdr.file_name, FILE_HDR_SIZE - MAGIC_SIZE,
                           wth->fh);
     if (bytes_read != FILE_HDR_SIZE - MAGIC_SIZE) {
-       *err = file_error(wth->fh);
+       *err = file_error(wth->fh, err_info);
        if (*err != 0)
            return -1;
        return 0;
@@ -223,26 +229,32 @@ int nettl_open(wtap *wth, int *err, gchar **err_info _U_)
 
     /* This is an nettl file */
     wth->file_type = WTAP_FILE_NETTL;
-    wth->capture.nettl = g_malloc(sizeof(nettl_t));
+    nettl = g_malloc(sizeof(nettl_t));
+    wth->priv = (void *)nettl;
     if (file_hdr.os_vers[2] == '1' && file_hdr.os_vers[3] == '1')
-       wth->capture.nettl->is_hpux_11 = TRUE;
+       nettl->is_hpux_11 = TRUE;
     else
-       wth->capture.nettl->is_hpux_11 = FALSE;
+       nettl->is_hpux_11 = FALSE;
     wth->subtype_read = nettl_read;
     wth->subtype_seek_read = nettl_seek_read;
-    wth->subtype_close = nettl_close;
     wth->snapshot_length = 0;  /* not available */
 
     /* read the first header to take a guess at the file encap */
-    bytes_read = file_read(dummy, 1, 4, wth->fh);
+    bytes_read = file_read(dummy, 4, wth->fh);
     if (bytes_read != 4) {
-        if (*err != 0)
+        if (*err != 0) {
+            wth->priv = NULL;
+            g_free(nettl);
             return -1;
+        }
         if (bytes_read != 0) {
             *err = WTAP_ERR_SHORT_READ;
-            g_free(wth->capture.nettl);
+            wth->priv = NULL;
+            g_free(nettl);
             return -1;
         }
+        wth->priv = NULL;
+        g_free(nettl);
         return 0;
     }
 
@@ -280,7 +292,7 @@ int nettl_open(wtap *wth, int *err, gchar **err_info _U_)
     }
 
     if (file_seek(wth->fh, FILE_HDR_SIZE, SEEK_SET, err) == -1) {
-        g_free(wth->capture.nettl);
+        g_free(nettl);
        return -1;
     }
     wth->data_offset = FILE_HDR_SIZE;
@@ -306,6 +318,17 @@ static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
     }
     wth->data_offset += ret;
 
+    if (wth->phdr.caplen > WTAP_MAX_PACKET_SIZE) {
+       /*
+        * Probably a corrupt capture file; don't blow up trying
+        * to allocate space for an immensely-large packet.
+        */
+       *err = WTAP_ERR_BAD_FILE;
+       *err_info = g_strdup_printf("nettl: File has %u-byte packet, bigger than maximum of %u",
+           wth->phdr.caplen, WTAP_MAX_PACKET_SIZE);
+       return FALSE;
+    }
+
     /*
      * If the per-file encapsulation isn't known, set it to this
      * packet's encapsulation.
@@ -326,7 +349,7 @@ static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
      */
     buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
     if (!nettl_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
-               wth->phdr.caplen, err, fddihack))
+               wth->phdr.caplen, err, err_info, fddihack))
        return FALSE;   /* Read error */
     wth->data_offset += wth->phdr.caplen;
     return TRUE;
@@ -334,7 +357,7 @@ static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
 
 static gboolean
 nettl_seek_read(wtap *wth, gint64 seek_off,
-               union wtap_pseudo_header *pseudo_header, guchar *pd,
+               union wtap_pseudo_header *pseudo_header, guint8 *pd,
                int length, int *err, gchar **err_info)
 {
     int ret;
@@ -359,7 +382,8 @@ nettl_seek_read(wtap *wth, gint64 seek_off,
     /*
      * Read the packet data.
      */
-    return nettl_read_rec_data(wth->random_fh, pd, length, err, fddihack);
+    return nettl_read_rec_data(wth->random_fh, pd, length, err, err_info,
+        fddihack);
 }
 
 static int
@@ -367,6 +391,7 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
                union wtap_pseudo_header *pseudo_header, int *err,
                gchar **err_info, gboolean *fddihack)
 {
+    nettl_t *nettl = (nettl_t *)wth->priv;
     int bytes_read;
     struct nettlrec_hdr rec_hdr;
     guint16 hdr_len;
@@ -378,9 +403,9 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
     guint8 dummyc[16];
 
     errno = WTAP_ERR_CANT_READ;
-    bytes_read = file_read(&rec_hdr.hdr_len, 1, sizeof rec_hdr.hdr_len, fh);
+    bytes_read = file_read(&rec_hdr.hdr_len, sizeof rec_hdr.hdr_len, fh);
     if (bytes_read != sizeof rec_hdr.hdr_len) {
-       *err = file_error(fh);
+       *err = file_error(fh, err_info);
        if (*err != 0)
            return -1;
        if (bytes_read != 0) {
@@ -392,14 +417,14 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
     offset += 2;
     hdr_len = g_ntohs(rec_hdr.hdr_len);
     if (hdr_len < NETTL_REC_HDR_LEN) {
-       *err = WTAP_ERR_BAD_RECORD;
+       *err = WTAP_ERR_BAD_FILE;
        *err_info = g_strdup_printf("nettl: record header length %u too short",
            hdr_len);
        return -1;
     }
-    bytes_read = file_read(&rec_hdr.subsys, 1, NETTL_REC_HDR_LEN - 2, fh);
+    bytes_read = file_read(&rec_hdr.subsys, NETTL_REC_HDR_LEN - 2, fh);
     if (bytes_read != NETTL_REC_HDR_LEN - 2) {
-       *err = file_error(fh);
+       *err = file_error(fh, err_info);
        if (*err == 0)
            *err = WTAP_ERR_SHORT_READ;
        return -1;
@@ -411,11 +436,8 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
        return -1;
     offset += hdr_len;
 
-    if ( ( (pntohl(&rec_hdr.kind) & ~NETTL_HDR_SUBSYSTEM_BITS_MASK)
-         & (NETTL_HDR_PROCEDURE_TRACE |
-            NETTL_HDR_STATE_TRACE |
-            NETTL_HDR_ERROR_TRACE) ) != 0) {
-        /* not actually a packet trace record */
+    if ( (pntohl(&rec_hdr.kind) & NETTL_HDR_PDU_MASK) == 0 ) {
+        /* not actually a data packet (PDU) trace record */
         phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_IP;
         length = pntohl(&rec_hdr.length);
         caplen = pntohl(&rec_hdr.caplen);
@@ -434,8 +456,10 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
        case NETTL_SUBSYS_IGELAN :
        case NETTL_SUBSYS_IETHER :
        case NETTL_SUBSYS_IXGBE :
+       case NETTL_SUBSYS_HSSN :
        case NETTL_SUBSYS_IGSSN :
        case NETTL_SUBSYS_ICXGBE :
+       case NETTL_SUBSYS_IEXGBE :
        case NETTL_SUBSYS_HPPB_FDDI :
        case NETTL_SUBSYS_EISA_FDDI :
         case NETTL_SUBSYS_PCI_FDDI :
@@ -492,9 +516,9 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
                     padlen = 0;
                 } else {
                    /* outbound appears to have variable padding */
-                   bytes_read = file_read(dummyc, 1, 9, fh);
+                   bytes_read = file_read(dummyc, 9, fh);
                    if (bytes_read != 9) {
-                       *err = file_error(fh);
+                       *err = file_error(fh, err_info);
                        if (*err == 0)
                            *err = WTAP_ERR_SHORT_READ;
                        return -1;
@@ -545,9 +569,9 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
               we assumes everything is. We will crash and burn for anything else */
            /* for encapsulated 100baseT we do this */
            phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
-           bytes_read = file_read(&drv_eth_hdr, 1, NS_LS_DRV_ETH_HDR_LEN, fh);
+           bytes_read = file_read(&drv_eth_hdr, NS_LS_DRV_ETH_HDR_LEN, fh);
            if (bytes_read != NS_LS_DRV_ETH_HDR_LEN) {
-               *err = file_error(fh);
+               *err = file_error(fh, err_info);
                if (*err == 0)
                    *err = WTAP_ERR_SHORT_READ;
                return -1;
@@ -563,7 +587,7 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
             *
             * And what are the extra two bytes?
             */
-            if (wth->capture.nettl->is_hpux_11) {
+            if (nettl->is_hpux_11) {
                 if (file_seek(fh, 2, SEEK_CUR, err) == -1) return -1;
                offset += 2;
             }
@@ -600,8 +624,10 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
            break;
 
        default:
+            /* We're going to assume it's ethernet if we don't recognize the
+               subsystem -- We'll probably spew junks and core if it isn't... */
            wth->file_encap = WTAP_ENCAP_PER_PACKET;
-           phdr->pkt_encap = WTAP_ENCAP_NETTL_UNKNOWN;
+           phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
             length = pntohl(&rec_hdr.length);
             caplen = pntohl(&rec_hdr.caplen);
             padlen = 0;
@@ -609,14 +635,14 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
     }
 
     if (length < padlen) {
-       *err = WTAP_ERR_BAD_RECORD;
+       *err = WTAP_ERR_BAD_FILE;
        *err_info = g_strdup_printf("nettl: packet length %u in record header too short, less than %u",
            length, padlen);
        return -1;
     }
     phdr->len = length - padlen;
     if (caplen < padlen) {
-       *err = WTAP_ERR_BAD_RECORD;
+       *err = WTAP_ERR_BAD_FILE;
        *err_info = g_strdup_printf("nettl: captured length %u in record header too short, less than %u",
            caplen, padlen);
        return -1;
@@ -635,37 +661,51 @@ nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
 }
 
 static gboolean
-nettl_read_rec_data(FILE_T fh, guchar *pd, int length, int *err, gboolean fddihack)
+nettl_read_rec_data(FILE_T fh, guint8 *pd, int length, int *err,
+       gchar **err_info, gboolean fddihack)
 {
-    int bytes_read;
-    guchar *p=NULL;
+    int bytes_to_read, bytes_read;
     guint8 dummy[3];
 
-    if (fddihack == TRUE) {
-       /* read in FC, dest, src, DSAP and SSAP */
-       if (file_read(pd, 1, 15, fh) == 15) {
-          if (pd[13] == 0xAA) {
-             /* it's SNAP, have to eat 3 bytes??? */
-             if (file_read(dummy, 1, 3, fh) == 3) {
-                p=pd+15;
-                bytes_read = file_read(p, 1, length-18, fh);
-                bytes_read += 18;
-             } else {
-                bytes_read = -1;
-             }
-          } else {
-             /* not SNAP */
-             p=pd+15;
-             bytes_read = file_read(p, 1, length-15, fh);
-             bytes_read += 15;
-          }
-       } else
-          bytes_read = -1;
+    if (fddihack) {
+        /* read in FC, dest, src, DSAP and SSAP */
+        bytes_to_read = 15;
+        if (bytes_to_read > length)
+            bytes_to_read = length;
+        bytes_read = file_read(pd, bytes_to_read, fh);
+        if (bytes_read != bytes_to_read) {
+            if (*err == 0)
+                *err = WTAP_ERR_SHORT_READ;
+            return FALSE;
+        }
+        length -= bytes_read;
+        if (length == 0) {
+               /* There's nothing past the FC, dest, src, DSAP and SSAP */
+               return TRUE;
+        }
+        if (pd[13] == 0xAA) {
+            /* it's SNAP, have to eat 3 bytes??? */
+            bytes_to_read = 3;
+            if (bytes_to_read > length)
+                bytes_to_read = length;
+            bytes_read = file_read(dummy, bytes_to_read, fh);
+            if (bytes_read != bytes_to_read) {
+                if (*err == 0)
+                    *err = WTAP_ERR_SHORT_READ;
+                return FALSE;
+            }
+            length -= bytes_read;
+            if (length == 0) {
+                /* There's nothing past the FC, dest, src, DSAP, SSAP, and 3 bytes to eat */
+               return TRUE;
+           }
+        }
+        bytes_read = file_read(pd + 15, length, fh);
     } else
-       bytes_read = file_read(pd, 1, length, fh);
+        bytes_read = file_read(pd, length, fh);
 
     if (bytes_read != length) {
-       *err = file_error(fh);
+       *err = file_error(fh, err_info);
        if (*err == 0)
            *err = WTAP_ERR_SHORT_READ;
        return FALSE;
@@ -673,11 +713,6 @@ nettl_read_rec_data(FILE_T fh, guchar *pd, int length, int *err, gboolean fddiha
     return TRUE;
 }
 
-static void nettl_close(wtap *wth)
-{
-    g_free(wth->capture.nettl);
-}
-
 
 /* Returns 0 if we could write the specified encapsulation type,
    an error indication otherwise.  nettl files are WTAP_ENCAP_UNKNOWN
@@ -713,10 +748,9 @@ int nettl_dump_can_write_encap(int encap)
 
 /* Returns TRUE on success, FALSE on failure;
    sets "*err" to an error code on failure */
-gboolean nettl_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
+gboolean nettl_dump_open(wtap_dumper *wdh, int *err)
 {
        struct nettl_file_hdr file_hdr;
-       size_t nwritten;
 
        /* This is a nettl file */
        wdh->subtype_write = nettl_dump;
@@ -732,14 +766,8 @@ gboolean nettl_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
        file_hdr.os_v=0x55;
        g_strlcpy(file_hdr.model,"9000/800",11);
        file_hdr.unknown=g_htons(0x406);
-       nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
-       if (nwritten != sizeof(file_hdr)) {
-               if (nwritten == 0 && ferror(wdh->fh))
-                       *err = errno;
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
                return FALSE;
-       }
        wdh->bytes_dumped += sizeof(file_hdr);
 
        return TRUE;
@@ -750,10 +778,9 @@ gboolean nettl_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
 static gboolean nettl_dump(wtap_dumper *wdh,
        const struct wtap_pkthdr *phdr,
        const union wtap_pseudo_header *pseudo_header _U_,
-       const guchar *pd, int *err)
+       const guint8 *pd, int *err)
 {
        struct nettlrec_hdr rec_hdr;
-       size_t nwritten;
        guint8 dummyc[24];
 
        memset(&rec_hdr,0,sizeof(rec_hdr));
@@ -824,67 +851,37 @@ static gboolean nettl_dump(wtap_dumper *wdh,
                        return FALSE;
        }
 
-       nwritten = fwrite(&rec_hdr, 1, sizeof(rec_hdr), wdh->fh);
-       if (nwritten != sizeof(rec_hdr)) {
-               if (nwritten == 0 && ferror(wdh->fh))
-                       *err = errno;
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof(rec_hdr), err))
                return FALSE;
-       }
        wdh->bytes_dumped += sizeof(rec_hdr);
 
        /* Write out 4 extra bytes of unknown stuff for HP-UX11
         * header format.
         */
        memset(dummyc, 0, sizeof dummyc);
-       nwritten = fwrite(dummyc, 1, 4, wdh->fh);
-       if (nwritten != 4) {
-               if (nwritten == 0 && ferror(wdh->fh))
-                       *err = errno;
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, dummyc, 4, err))
                return FALSE;
-       }
        wdh->bytes_dumped += 4;
 
        if ((phdr->pkt_encap == WTAP_ENCAP_FDDI_BITSWAPPED) ||
            (phdr->pkt_encap == WTAP_ENCAP_NETTL_FDDI)) {
                /* add those weird 3 bytes of padding */
-               nwritten = fwrite(dummyc, 1, 3, wdh->fh);
-               if (nwritten != 3) {
-                       if (nwritten == 0 && ferror(wdh->fh))
-                               *err = errno;
-                       else
-                               *err = WTAP_ERR_SHORT_WRITE;
+               if (!wtap_dump_file_write(wdh, dummyc, 3, err))
                        return FALSE;
-               }
                wdh->bytes_dumped += 3;
        }
 /*
        } else if (phdr->pkt_encap == WTAP_ENCAP_NETTL_X25) {
-               nwritten = fwrite(dummyc, 1, 24, wdh->fh);
-               if (nwritten != 24) {
-                       if (nwritten == 0 && ferror(wdh->fh))
-                               *err = errno;
-                       else
-                               *err = WTAP_ERR_SHORT_WRITE;
+               if (!wtap_dump_file_write(wdh, dummyc, 24, err))
                        return FALSE;
-               }
                wdh->bytes_dumped += 24;
        }
 */
 
        /* write actual PDU data */
 
-       nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
-       if (nwritten != phdr->caplen) {
-               if (nwritten == 0 && ferror(wdh->fh))
-                       *err = errno;
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
                return FALSE;
-       }
         wdh->bytes_dumped += phdr->caplen;
 
        return TRUE;