epan/dissectors/packet-xml.c try to decrypt data, but the data doesn't look correct yet
[metze/wireshark/wip.git] / wiretap / snoop.c
index f971df547a7f2c1bf075e704c82fdcdb008c4e27..95aaa7d9f984d91da6d4e8fd067713420eaad7e1 100644 (file)
@@ -1,33 +1,16 @@
 /* snoop.c
- *
- * $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.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 #include <errno.h>
 #include <string.h>
 #include "wtap-int.h"
 #include "file_wrappers.h"
-#include "buffer.h"
 #include "atm.h"
 #include "snoop.h"
 /* See RFC 1761 for a description of the "snoop" file format. */
@@ -91,17 +74,16 @@ struct shomiti_trailer {
 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean snoop_seek_read(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
-    int *err, gchar **err_info);
+    wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
+static int snoop_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
+    Buffer *buf, int *err, gchar **err_info);
 static gboolean snoop_read_atm_pseudoheader(FILE_T fh,
-    union wtap_pseudo_header *pseudo_header, int *err);
+    union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
 static gboolean snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
     int *header_size);
-static gboolean snoop_read_rec_data(FILE_T fh, guchar *pd, int length,
-    int *err);
-static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
+static gboolean snoop_dump(wtap_dumper *wdh, const wtap_rec *rec,
+    const guint8 *pd, int *err, gchar **err_info);
 
 /*
  * See
@@ -111,6 +93,12 @@ static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
  * for the "dlpi.h" header file specified by The Open Group, which lists
  * the DL_ values for various protocols; Solaris 7 uses the same values.
  *
+ * See
+ *
+ *     http://www.iana.org/assignments/snoop-datalink-types/snoop-datalink-types.xml
+ *
+ * for the IETF list of snoop datalink types.
+ *
  * The page at
  *
  *     http://mrpink.lerc.nasa.gov/118x/support.html
@@ -139,19 +127,21 @@ static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
  *
  * Source to an "atmdump" package, which includes a modified version of
  * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
- * also includes "atmdump", which is a modified "tcpdump", is available
+ * also includes "atmdump", which is a modified "tcpdump", was available
  * at
  *
  *     ftp://ftp.cs.ndsu.nodak.edu/pub/freebsd/atm/atm-bpf.tgz
  *
- * and that code also indicates that DL_IPATM is used, and that an
- * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
- * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
- * and then the ATM PDU, and suggests that the direction flag is 0x80 for
- * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
- * "received" (presumably meaning DCE->DTE).  That code was used as the
- * basis for the SunATM support in current CVS versions of libpcap and
- * tcpdump, and it works.
+ * (the host name is no longer valid) and that code also indicated that
+ * DL_IPATM is used, and that an ATM packet handed up from the Sun driver
+ * for the Sun SBus ATM card on Solaris 2.5.1 has 1 byte of direction,
+ * 1 byte of VPI, 2 bytes of VCI, and then the ATM PDU, and suggests that
+ * the direction flag is 0x80 for "transmitted" (presumably meaning
+ * DTE->DCE) and presumably not 0x80 for "received" (presumably meaning
+ * DCE->DTE).  That code was used as the basis for the SunATM support in
+ * later versions of libpcap and tcpdump, and it worked at the time the
+ * development was done with the SunATM code on the system on which the
+ * development was done.
  *
  * In fact, the "direction" byte appears to have some other stuff, perhaps
  * a traffic type, in the lower 7 bits, with the 8th bit indicating the
@@ -165,20 +155,17 @@ static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
  * if it can't, this may be useful reference information for anybody doing
  * code to use DLPI to do raw packet captures on those network types.
  *
- * See
- *
  *     http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
  *
- * for information on Shomiti's mutant flavor of snoop.  For some unknown
- * unknown reason, they decided not to just Go With The DLPI Flow, and
- * instead used the types unspecified in RFC 1461 for their own nefarious
- * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
- * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
- * of them from the "Shomiti" versions of same.
+ * gives information on Shomiti's mutant flavor of snoop.  For some unknown
+ * reason, they decided not to just Go With The DLPI Flow, and instead used
+ * the types unspecified in RFC 1461 for their own nefarious purposes, such
+ * as distinguishing 10MB from 100MB from 1000MB Ethernet and distinguishing
+ * 4MB from 16MB Token Ring, and distinguishing both of them from the
+ * "Shomiti" versions of same.
  */
-int snoop_open(wtap *wth, int *err, gchar **err_info)
+wtap_open_return_val snoop_open(wtap *wth, int *err, gchar **err_info)
 {
-       int bytes_read;
        char magic[sizeof snoop_magic];
        struct snoop_hdr hdr;
        struct snooprec_hdr rec_hdr;
@@ -211,6 +198,7 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
                WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Token Ring */
                WTAP_ENCAP_UNKNOWN,     /* "ISO 8802/3 and Ethernet" */
                WTAP_ENCAP_UNKNOWN,     /* 100BaseT (but that's just Ethernet) */
+               WTAP_ENCAP_IP_OVER_IB_SNOOP,    /* Infiniband */
        };
        #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
        #define SNOOP_PRIVATE_BIT 0x80000000
@@ -244,36 +232,27 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
                WTAP_ENCAP_UNKNOWN,     /* Other */
                WTAP_ENCAP_UNKNOWN,     /* Other */
                WTAP_ENCAP_UNKNOWN,     /* Other */
-               WTAP_ENCAP_IEEE_802_11_WITH_RADIO,
+               WTAP_ENCAP_IEEE_802_11_WITH_RADIO, /* IEEE 802.11 with Radio Header */
+               WTAP_ENCAP_ETHERNET,    /* 10 Gigabit Ethernet */
        };
        #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
        int file_encap;
+       gint64 saved_offset;
 
        /* Read in the string that should be at the start of a "snoop" file */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
-       if (bytes_read != sizeof magic) {
-               *err = file_error(wth->fh);
-               if (*err != 0)
-                       return -1;
-               return 0;
+       if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
-       wth->data_offset += sizeof magic;
 
        if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
-               return 0;
+               return WTAP_OPEN_NOT_MINE;
        }
 
        /* Read the rest of the header. */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
-       if (bytes_read != sizeof hdr) {
-               *err = file_error(wth->fh);
-               if (*err != 0)
-                       return -1;
-               return 0;
-       }
-       wth->data_offset += sizeof hdr;
+       if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
+               return WTAP_OPEN_ERROR;
 
        /*
         * Make sure it's a version we support.
@@ -292,7 +271,7 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
        default:
                *err = WTAP_ERR_UNSUPPORTED;
                *err_info = g_strdup_printf("snoop: version %u unsupported", hdr.version);
-               return -1;
+               return WTAP_OPEN_ERROR;
        }
 
        /*
@@ -326,18 +305,10 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
        is_shomiti = FALSE;
 
        /* Read first record header. */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&rec_hdr, 1, sizeof rec_hdr, wth->fh);
-       if (bytes_read != sizeof rec_hdr) {
-               *err = file_error(wth->fh);
-               if (*err == 0 && bytes_read != 0)
-                       *err = WTAP_ERR_SHORT_READ;
-               if (*err != 0) {
-                       /*
-                        * A real-live error.
-                        */
-                       return -1;
-               }
+       saved_offset = file_tell(wth->fh);
+       if (!wtap_read_bytes_or_eof(wth->fh, &rec_hdr, sizeof rec_hdr, err, err_info)) {
+               if (*err != 0)
+                       return WTAP_OPEN_ERROR;
 
                /*
                 * The file ends after the record header, which means this
@@ -378,46 +349,46 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
        /*
         * Seek back to the beginning of the first record.
         */
-       if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1)
-               return -1;
+       if (file_seek(wth->fh, saved_offset, SEEK_SET, err) == -1)
+               return WTAP_OPEN_ERROR;
 
        hdr.network = g_ntohl(hdr.network);
        if (is_shomiti) {
                if (hdr.network >= NUM_SHOMITI_ENCAPS
                    || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
-                       *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+                       *err = WTAP_ERR_UNSUPPORTED;
                        *err_info = g_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
                            hdr.network);
-                       return -1;
+                       return WTAP_OPEN_ERROR;
                }
                file_encap = shomiti_encap[hdr.network];
 
                /* This is a Shomiti file */
-               wth->file_type = WTAP_FILE_SHOMITI;
+               wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SHOMITI;
        } else if (hdr.network & SNOOP_PRIVATE_BIT) {
                if ((hdr.network^SNOOP_PRIVATE_BIT) >= NUM_SNOOP_PRIVATE_ENCAPS
                    || snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT] == WTAP_ENCAP_UNKNOWN) {
-                       *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+                       *err = WTAP_ERR_UNSUPPORTED;
                        *err_info = g_strdup_printf("snoop: private network type %u unknown or unsupported",
                            hdr.network);
-                       return -1;
+                       return WTAP_OPEN_ERROR;
                }
                file_encap = snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT];
 
                /* This is a snoop file */
-               wth->file_type = WTAP_FILE_SNOOP;
+               wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
        } else {
                if (hdr.network >= NUM_SNOOP_ENCAPS
                    || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
-                       *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+                       *err = WTAP_ERR_UNSUPPORTED;
                        *err_info = g_strdup_printf("snoop: network type %u unknown or unsupported",
                            hdr.network);
-                       return -1;
+                       return WTAP_OPEN_ERROR;
                }
                file_encap = snoop_encap[hdr.network];
 
                /* This is a snoop file */
-               wth->file_type = WTAP_FILE_SNOOP;
+               wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
        }
 
        /*
@@ -429,8 +400,8 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
        wth->subtype_seek_read = snoop_seek_read;
        wth->file_encap = file_encap;
        wth->snapshot_length = 0;       /* not available in header */
-       wth->tsprecision = WTAP_FILE_TSPREC_USEC;
-       return 1;
+       wth->file_tsprec = WTAP_TSPREC_USEC;
+       return WTAP_OPEN_MINE;
 }
 
 typedef struct {
@@ -449,74 +420,110 @@ typedef struct {
 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset)
 {
+       int     padbytes;
+
+       *data_offset = file_tell(wth->fh);
+
+       padbytes = snoop_read_packet(wth, wth->fh, &wth->rec,
+           wth->rec_data, err, err_info);
+       if (padbytes == -1)
+               return FALSE;
+
+       /*
+        * Skip over the padding, if any.
+        */
+       if (padbytes != 0) {
+               if (!wtap_read_bytes(wth->fh, NULL, padbytes, err, err_info))
+                       return FALSE;
+       }
+
+       return TRUE;
+}
+
+static gboolean
+snoop_seek_read(wtap *wth, gint64 seek_off,
+    wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
+{
+       if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
+               return FALSE;
+
+       if (snoop_read_packet(wth, wth->random_fh, rec, buf, err, err_info) == -1) {
+               if (*err == 0)
+                       *err = WTAP_ERR_SHORT_READ;
+               return FALSE;
+       }
+       return TRUE;
+}
+
+static int
+snoop_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
+    Buffer *buf, int *err, gchar **err_info)
+{
+       struct snooprec_hdr hdr;
        guint32 rec_size;
        guint32 packet_size;
        guint32 orig_size;
-       int     bytes_read;
-       struct snooprec_hdr hdr;
-       char    padbuf[4];
-       guint   padbytes;
-       int     bytes_to_read;
        int header_size;
 
        /* Read record header. */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
-       if (bytes_read != sizeof hdr) {
-               *err = file_error(wth->fh);
-               if (*err == 0 && bytes_read != 0)
-                       *err = WTAP_ERR_SHORT_READ;
-               return FALSE;
-       }
-       wth->data_offset += sizeof hdr;
+       if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
+               return -1;
 
        rec_size = g_ntohl(hdr.rec_len);
        orig_size = g_ntohl(hdr.orig_len);
        packet_size = g_ntohl(hdr.incl_len);
-       if (packet_size > WTAP_MAX_PACKET_SIZE) {
+       if (orig_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
+               /*
+                * 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("snoop: File has %u-byte original length, bigger than maximum of %u",
+                   orig_size, WTAP_MAX_PACKET_SIZE_STANDARD);
+               return -1;
+       }
+       if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
                /*
                 * Probably a corrupt capture file; don't blow up trying
                 * to allocate space for an immensely-large packet.
                 */
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
-                   packet_size, WTAP_MAX_PACKET_SIZE);
-               return FALSE;
+                   packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
+               return -1;
        }
        if (packet_size > rec_size) {
                /*
                 * Probably a corrupt capture file.
                 */
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
                    packet_size, rec_size);
-               return FALSE;
+               return -1;
        }
 
-       *data_offset = wth->data_offset;
-
-       /*
-        * If this is an ATM packet, the first four bytes are the
-        * direction of the packet (transmit/receive), the VPI, and
-        * the VCI; read them and generate the pseudo-header from
-        * them.
-        */
        switch (wth->file_encap) {
 
        case WTAP_ENCAP_ATM_PDUS:
+               /*
+                * This is an ATM packet, so the first four bytes are
+                * the direction of the packet (transmit/receive), the
+                * VPI, and the VCI; read them and generate the
+                * pseudo-header from them.
+                */
                if (packet_size < sizeof (struct snoop_atm_hdr)) {
                        /*
                         * Uh-oh, the packet isn't big enough to even
                         * have a pseudo-header.
                         */
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header",
                            packet_size);
-                       return FALSE;
+                       return -1;
                }
-               if (!snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
-                   err))
-                       return FALSE;   /* Read error */
+               if (!snoop_read_atm_pseudoheader(fh, &rec->rec_header.packet_header.pseudo_header,
+                   err, err_info))
+                       return -1;      /* Read error */
 
                /*
                 * Don't count the pseudo-header as part of the packet.
@@ -524,7 +531,6 @@ static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
                rec_size -= (guint32)sizeof (struct snoop_atm_hdr);
                orig_size -= (guint32)sizeof (struct snoop_atm_hdr);
                packet_size -= (guint32)sizeof (struct snoop_atm_hdr);
-               wth->data_offset += sizeof (struct snoop_atm_hdr);
                break;
 
        case WTAP_ENCAP_ETHERNET:
@@ -533,10 +539,10 @@ static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
                 * this frame; if this is a Shomit file, we assume there
                 * is.  (XXX - or should we treat it a "maybe"?)
                 */
-               if (wth->file_type == WTAP_FILE_SHOMITI)
-                       wth->pseudo_header.eth.fcs_len = 4;
+               if (wth->file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_SHOMITI)
+                       rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 4;
                else
-                       wth->pseudo_header.eth.fcs_len = 0;
+                       rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 0;
                break;
 
        case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
@@ -545,14 +551,14 @@ static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
                         * Uh-oh, the packet isn't big enough to even
                         * have a pseudo-header.
                         */
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup_printf("snoop: Shomiti wireless file has a %u-byte packet, too small to have even a wireless pseudo-header",
                            packet_size);
-                       return FALSE;
+                       return -1;
                }
-               if (!snoop_read_shomiti_wireless_pseudoheader(wth->fh,
-                   &wth->pseudo_header, err, err_info, &header_size))
-                       return FALSE;   /* Read error */
+               if (!snoop_read_shomiti_wireless_pseudoheader(fh,
+                   &rec->rec_header.packet_header.pseudo_header, err, err_info, &header_size))
+                       return -1;      /* Read error */
 
                /*
                 * Don't count the pseudo-header as part of the packet.
@@ -560,142 +566,57 @@ static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
                rec_size -= header_size;
                orig_size -= header_size;
                packet_size -= header_size;
-               wth->data_offset += header_size;
                break;
        }
 
-       buffer_assure_space(wth->frame_buffer, packet_size);
-       if (!snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
-           packet_size, err))
-               return FALSE;   /* Read error */
-       wth->data_offset += packet_size;
-
-       wth->phdr.ts.secs = g_ntohl(hdr.ts_sec);
-       wth->phdr.ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
-       wth->phdr.caplen = packet_size;
-       wth->phdr.len = orig_size;
-
-       /*
-        * If this is ATM LANE traffic, try to guess what type of LANE
-        * traffic it is based on the packet contents.
-        */
-       if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
-           wth->pseudo_header.atm.type == TRAF_LANE) {
-               atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
-                   wth->phdr.caplen, &wth->pseudo_header);
-       }
+       rec->rec_type = REC_TYPE_PACKET;
+       rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
+       rec->ts.secs = g_ntohl(hdr.ts_sec);
+       rec->ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
+       rec->rec_header.packet_header.caplen = packet_size;
+       rec->rec_header.packet_header.len = orig_size;
 
-       /*
-        * Skip over the padding (don't "fseek()", as the standard
-        * I/O library on some platforms discards buffered data if
-        * you do that, which means it does a lot more reads).
-        * There's probably not much padding (it's probably padded only
-        * to a 4-byte boundary), so we probably need only do one read.
-        */
        if (rec_size < (sizeof hdr + packet_size)) {
                /*
                 * What, *negative* padding?  Bogus.
                 */
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
                    rec_size, packet_size);
-               return FALSE;
-       }
-       padbytes = rec_size - ((guint)sizeof hdr + packet_size);
-       while (padbytes != 0) {
-               bytes_to_read = padbytes;
-               if ((unsigned)bytes_to_read > sizeof padbuf)
-                       bytes_to_read = sizeof padbuf;
-               errno = WTAP_ERR_CANT_READ;
-               bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
-               if (bytes_read != bytes_to_read) {
-                       *err = file_error(wth->fh);
-                       if (*err == 0)
-                               *err = WTAP_ERR_SHORT_READ;
-                       return FALSE;
-               }
-               wth->data_offset += bytes_read;
-               padbytes -= bytes_read;
-       }
-
-       return TRUE;
-}
-
-static gboolean
-snoop_seek_read(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
-    int *err, gchar **err_info)
-{
-       if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
-               return FALSE;
-
-       switch (wth->file_encap) {
-
-       case WTAP_ENCAP_ATM_PDUS:
-               if (!snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
-                   err)) {
-                       /* Read error */
-                       return FALSE;
-               }
-               break;
-
-       case WTAP_ENCAP_ETHERNET:
-               /*
-                * If this is a snoop file, we assume there's no FCS in
-                * this frame; if this is a Shomit file, we assume there
-                * is.  (XXX - or should we treat it a "maybe"?)
-                */
-               if (wth->file_type == WTAP_FILE_SHOMITI)
-                       pseudo_header->eth.fcs_len = 4;
-               else
-                       pseudo_header->eth.fcs_len = 0;
-               break;
-
-       case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
-               if (!snoop_read_shomiti_wireless_pseudoheader(wth->random_fh,
-                   pseudo_header, err, err_info, NULL)) {
-                       /* Read error */
-                       return FALSE;
-               }
-               break;
+               return -1;
        }
 
        /*
         * Read the packet data.
         */
-       if (!snoop_read_rec_data(wth->random_fh, pd, length, err))
-               return FALSE;   /* failed */
+       if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
+               return -1;      /* failed */
 
        /*
         * If this is ATM LANE traffic, try to guess what type of LANE
         * traffic it is based on the packet contents.
         */
        if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
-           pseudo_header->atm.type == TRAF_LANE)
-               atm_guess_lane_type(pd, length, pseudo_header);
-       return TRUE;
+           rec->rec_header.packet_header.pseudo_header.atm.type == TRAF_LANE) {
+               atm_guess_lane_type(rec, ws_buffer_start_ptr(buf));
+       }
+
+       return rec_size - ((guint)sizeof hdr + packet_size);
 }
 
 static gboolean
 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
-    int *err)
+    int *err, gchar **err_info)
 {
        struct snoop_atm_hdr atm_phdr;
-       int     bytes_read;
        guint8  vpi;
        guint16 vci;
 
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&atm_phdr, 1, sizeof (struct snoop_atm_hdr), fh);
-       if (bytes_read != sizeof (struct snoop_atm_hdr)) {
-               *err = file_error(fh);
-               if (*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
+       if (!wtap_read_bytes(fh, &atm_phdr, sizeof atm_phdr, err, err_info))
                return FALSE;
-       }
 
        vpi = atm_phdr.vpi;
-       vci = pntohs(&atm_phdr.vci);
+       vci = pntoh16(&atm_phdr.vci);
 
        /*
         * The lower 4 bits of the first byte of the header indicate
@@ -775,17 +696,10 @@ snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
     int *header_size)
 {
        shomiti_wireless_header whdr;
-       int     bytes_read;
        int     rsize;
 
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&whdr, 1, sizeof (shomiti_wireless_header), fh);
-       if (bytes_read != sizeof (shomiti_wireless_header)) {
-               *err = file_error(fh);
-               if (*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
+       if (!wtap_read_bytes(fh, &whdr, sizeof whdr, err, err_info))
                return FALSE;
-       }
 
        /* the 4th byte of the pad is actually a header length,
         * we've already read 8 bytes of it, and it must never
@@ -802,42 +716,31 @@ snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
         * 4 bytes of length plus 8 bytes of information?
         */
        if (whdr.pad[3] < 8) {
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup_printf("snoop: Header length in Surveyor record is %u, less than minimum of 8",
                    whdr.pad[3]);
                return FALSE;
        }
        /* Skip the header. */
        rsize = ((int) whdr.pad[3]) - 8;
-       if (file_seek(fh, rsize, SEEK_CUR, err) == -1)
+       if (!wtap_read_bytes(fh, NULL, rsize, err, err_info))
                return FALSE;
 
+       memset(&pseudo_header->ieee_802_11, 0, sizeof(pseudo_header->ieee_802_11));
        pseudo_header->ieee_802_11.fcs_len = 4;
+       pseudo_header->ieee_802_11.decrypted = FALSE;
+       pseudo_header->ieee_802_11.datapad = FALSE;
+       pseudo_header->ieee_802_11.phy = PHDR_802_11_PHY_UNKNOWN;
+       pseudo_header->ieee_802_11.has_channel = TRUE;
        pseudo_header->ieee_802_11.channel = whdr.channel;
+       pseudo_header->ieee_802_11.has_data_rate = TRUE;
        pseudo_header->ieee_802_11.data_rate = whdr.rate;
-       pseudo_header->ieee_802_11.signal_level = whdr.signal;
+       pseudo_header->ieee_802_11.has_signal_percent = TRUE;
+       pseudo_header->ieee_802_11.signal_percent = whdr.signal;
 
        /* add back the header and don't forget the pad as well */
-       if(header_size != NULL)
-           *header_size = rsize + 8 + 4;
-
-    return TRUE;
-}
+       *header_size = rsize + 8 + 4;
 
-static gboolean
-snoop_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
-{
-       int     bytes_read;
-
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(pd, 1, length, fh);
-
-       if (bytes_read != length) {
-               *err = file_error(fh);
-               if (*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
-               return FALSE;
-       }
        return TRUE;
 }
 
@@ -851,11 +754,11 @@ static const int wtap_encap[] = {
        0x08,           /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
        -1,             /* WTAP_ENCAP_RAW_IP -> unsupported */
        -1,             /* WTAP_ENCAP_ARCNET -> unsupported */
+       -1,             /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
        -1,             /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
        -1,             /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
        -1,             /* WTAP_ENCAP_LAPB -> unsupported*/
        0x12,           /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
-       -1              /* WTAP_ENCAP_NULL -> unsupported */
 };
 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
 
@@ -868,20 +771,19 @@ int snoop_dump_can_write_encap(int encap)
                return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
 
        if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
-               return WTAP_ERR_UNSUPPORTED_ENCAP;
+               return WTAP_ERR_UNWRITABLE_ENCAP;
 
        return 0;
 }
 
 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
    failure */
-gboolean snoop_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
+gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
 {
        struct snoop_hdr file_hdr;
 
        /* This is a snoop file */
        wdh->subtype_write = snoop_dump;
-       wdh->subtype_close = NULL;
 
        /* Write the file header. */
        if (!wtap_dump_file_write(wdh, &snoop_magic, sizeof snoop_magic, err))
@@ -899,35 +801,57 @@ gboolean snoop_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
 /* Write a record for a packet to a dump file.
    Returns TRUE on success, FALSE on failure. */
 static gboolean snoop_dump(wtap_dumper *wdh,
-       const struct wtap_pkthdr *phdr,
-       const union wtap_pseudo_header *pseudo_header _U_,
-       const guchar *pd, int *err)
+       const wtap_rec *rec,
+       const guint8 *pd, int *err, gchar **err_info _U_)
 {
+       const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
        struct snooprec_hdr rec_hdr;
        int reclen;
        guint padlen;
-       static char zeroes[4];
+       static const char zeroes[4] = {0};
        struct snoop_atm_hdr atm_hdr;
        int atm_hdrsize;
 
+       /* We can only write packet records. */
+       if (rec->rec_type != REC_TYPE_PACKET) {
+               *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
+               return FALSE;
+       }
+
+       /*
+        * Make sure this packet doesn't have a link-layer type that
+        * differs from the one for the file.
+        */
+       if (wdh->encap != rec->rec_header.packet_header.pkt_encap) {
+               *err = WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
+               return FALSE;
+       }
+
        if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
                atm_hdrsize = sizeof (struct snoop_atm_hdr);
        else
                atm_hdrsize = 0;
 
        /* Record length = header length plus data length... */
-       reclen = (int)sizeof rec_hdr + phdr->caplen + atm_hdrsize;
+       reclen = (int)sizeof rec_hdr + rec->rec_header.packet_header.caplen + atm_hdrsize;
+
 
        /* ... plus enough bytes to pad it to a 4-byte boundary. */
        padlen = ((reclen + 3) & ~3) - reclen;
        reclen += padlen;
 
-       rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
-       rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
+       /* Don't write anything we're not willing to read. */
+       if (rec->rec_header.packet_header.caplen + atm_hdrsize > WTAP_MAX_PACKET_SIZE_STANDARD) {
+               *err = WTAP_ERR_PACKET_TOO_LARGE;
+               return FALSE;
+       }
+
+       rec_hdr.orig_len = g_htonl(rec->rec_header.packet_header.len + atm_hdrsize);
+       rec_hdr.incl_len = g_htonl(rec->rec_header.packet_header.caplen + atm_hdrsize);
        rec_hdr.rec_len = g_htonl(reclen);
        rec_hdr.cum_drops = 0;
-       rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
-       rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
+       rec_hdr.ts_sec = g_htonl(rec->ts.secs);
+       rec_hdr.ts_usec = g_htonl(rec->ts.nsecs / 1000);
        if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
                return FALSE;
 
@@ -970,7 +894,7 @@ static gboolean snoop_dump(wtap_dumper *wdh,
                        return FALSE;
        }
 
-       if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
+       if (!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen, err))
                return FALSE;
 
        /* Now write the padding. */
@@ -978,3 +902,16 @@ static gboolean snoop_dump(wtap_dumper *wdh,
                return FALSE;
        return TRUE;
 }
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vi: set shiftwidth=8 tabstop=8 noexpandtab:
+ * :indentSize=8:tabSize=8:noTabs=false:
+ */