kerberos dissect KERBEROS_AD_GSS_API_ETYPE_NEGOTIATION content
[metze/wireshark/wip.git] / wiretap / btsnoop.c
index b5427af934706ed79244959a3216d82f36905e5a..91a09d3b3a8465113cc52c900ffffa4ddc1276ad 100644 (file)
@@ -1,23 +1,9 @@
 /* btsnoop.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "config.h"
@@ -25,8 +11,6 @@
 #include <string.h>
 #include "wtap-int.h"
 #include "file_wrappers.h"
-#include "buffer.h"
-#include "atm.h"
 #include "btsnoop.h"
 
 /*
 
 /* Magic number in "btsnoop" files. */
 static const char btsnoop_magic[] = {
-       'b', 't', 's', 'n', 'o', 'o', 'p', '\0'
+    'b', 't', 's', 'n', 'o', 'o', 'p', '\0'
 };
 
 /* "btsnoop" file header (minus magic number). */
 struct btsnoop_hdr {
-       guint32 version;        /* version number (should be 1) */
-       guint32 datalink;       /* datalink type */
+    guint32     version;        /* version number (should be 1) */
+    guint32     datalink;       /* datalink type */
 };
 
 /* "btsnoop" record header. */
 struct btsnooprec_hdr {
-       guint32 orig_len;       /* actual length of packet */
-       guint32 incl_len;       /* number of octets captured in file */
-       guint32 flags;          /* packet flags */
-       guint32 cum_drops;      /* cumulative number of dropped packets */
-       gint64  ts_usec;        /* timestamp microseconds */
+    guint32     orig_len;       /* actual length of packet */
+    guint32     incl_len;       /* number of octets captured in file */
+    guint32     flags;          /* packet flags */
+    guint32     cum_drops;      /* cumulative number of dropped packets */
+    gint64      ts_usec;        /* timestamp microseconds */
 };
 
 /* H1 is unframed data with the packet type encoded in the flags field of capture header */
 /* It can be used for any datalink by placing logging above the datalink layer of HCI */
-#define KHciLoggerDatalinkTypeH1               1001
+#define KHciLoggerDatalinkTypeH1                1001
 /* H4 is the serial HCI with packet type encoded in the first byte of each packet */
-#define KHciLoggerDatalinkTypeH4               1002
+#define KHciLoggerDatalinkTypeH4                1002
 /* CSR's PPP derived bluecore serial protocol - in practice we log in H1 format after deframing */
-#define KHciLoggerDatalinkTypeBCSP             1003
+#define KHciLoggerDatalinkTypeBCSP              1003
 /* H5 is the official three wire serial protocol derived from BCSP*/
-#define KHciLoggerDatalinkTypeH5               1004
+#define KHciLoggerDatalinkTypeH5                1004
+/* Linux Monitor */
+#define KHciLoggerDatalinkLinuxMonitor   2001
+/* BlueZ 5 Simulator */
+#define KHciLoggerDatalinkBlueZ5Simulator       2002
 
-#define KHciLoggerHostToController             0
-#define KHciLoggerControllerToHost             0x00000001
-#define KHciLoggerACLDataFrame                 0
-#define KHciLoggerCommandOrEvent               0x00000002
+#define KHciLoggerHostToController              0
+#define KHciLoggerControllerToHost              0x00000001
+#define KHciLoggerACLDataFrame                  0
+#define KHciLoggerCommandOrEvent                0x00000002
 
 static const gint64 KUnixTimeBase = G_GINT64_CONSTANT(0x00dcddb30f2f8000); /* offset from symbian - unix time */
 
 static gboolean btsnoop_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, guint8 *pd, int length,
-    int *err, gchar **err_info);
-static gboolean btsnoop_read_record_header(wtap *wth, FILE_T fh,
-    struct wtap_pkthdr *phdr, int *err, gchar **err_info);
-static gboolean btsnoop_read_rec_data(FILE_T fh, guint8 *pd, int length,
-    int *err, gchar **err_info);
-
-int btsnoop_open(wtap *wth, int *err, gchar **err_info)
+    wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
+static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
+    wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
+
+wtap_open_return_val btsnoop_open(wtap *wth, int *err, gchar **err_info)
 {
-       int bytes_read;
-       char magic[sizeof btsnoop_magic];
-       struct btsnoop_hdr hdr;
-
-       int file_encap=WTAP_ENCAP_UNKNOWN;
-
-       /* Read in the string that should be at the start of a "btsnoop" file */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(magic, sizeof magic, wth->fh);
-       if (bytes_read != sizeof magic) {
-               *err = file_error(wth->fh, err_info);
-               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                       return -1;
-               return 0;
-       }
-
-       if (memcmp(magic, btsnoop_magic, sizeof btsnoop_magic) != 0) {
-               return 0;
-       }
-
-       /* Read the rest of the header. */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
-       if (bytes_read != sizeof hdr) {
-               *err = file_error(wth->fh, err_info);
-               if (*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
-               return -1;
-       }
-
-       /*
-        * Make sure it's a version we support.
-        */
-       hdr.version = g_ntohl(hdr.version);
-       if (hdr.version != 1) {
-               *err = WTAP_ERR_UNSUPPORTED;
-               *err_info = g_strdup_printf("btsnoop: version %u unsupported", hdr.version);
-               return -1;
-       }
-
-       hdr.datalink = g_ntohl(hdr.datalink);
-       switch (hdr.datalink) {
-       case KHciLoggerDatalinkTypeH1:
-               file_encap=WTAP_ENCAP_BLUETOOTH_HCI;
-               break;
-       case KHciLoggerDatalinkTypeBCSP:
-               *err = WTAP_ERR_UNSUPPORTED;
-               *err_info = g_strdup_printf("btsnoop: BCSP capture logs unsupported");
-               return -1;
-       case KHciLoggerDatalinkTypeH5:
-               *err = WTAP_ERR_UNSUPPORTED;
-               *err_info = g_strdup_printf("btsnoop: H5 capture logs unsupported");
-               return -1;
-       case KHciLoggerDatalinkTypeH4:
-               file_encap=WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
-               break;
-       default:
-               *err = WTAP_ERR_UNSUPPORTED;
-               *err_info = g_strdup_printf("btsnoop: datalink type %u unknown or unsupported", hdr.datalink);
-               return -1;
-       }
-
-       wth->subtype_read = btsnoop_read;
-       wth->subtype_seek_read = btsnoop_seek_read;
-       wth->file_encap = file_encap;
-       wth->snapshot_length = 0;       /* not available in header */
-       wth->tsprecision = WTAP_FILE_TSPREC_USEC;
-       wth->file_type = WTAP_FILE_BTSNOOP;
-       return 1;
+    char magic[sizeof btsnoop_magic];
+    struct btsnoop_hdr hdr;
+
+    int file_encap=WTAP_ENCAP_UNKNOWN;
+
+    /* Read in the string that should be at the start of a "btsnoop" file */
+    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;
+    }
+
+    if (memcmp(magic, btsnoop_magic, sizeof btsnoop_magic) != 0) {
+        return WTAP_OPEN_NOT_MINE;
+    }
+
+    /* Read the rest of the header. */
+    if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
+        return WTAP_OPEN_ERROR;
+
+    /*
+     * Make sure it's a version we support.
+     */
+    hdr.version = g_ntohl(hdr.version);
+    if (hdr.version != 1) {
+        *err = WTAP_ERR_UNSUPPORTED;
+        *err_info = g_strdup_printf("btsnoop: version %u unsupported", hdr.version);
+        return WTAP_OPEN_ERROR;
+    }
+
+    hdr.datalink = g_ntohl(hdr.datalink);
+    switch (hdr.datalink) {
+    case KHciLoggerDatalinkTypeH1:
+        file_encap=WTAP_ENCAP_BLUETOOTH_HCI;
+        break;
+    case KHciLoggerDatalinkTypeH4:
+        file_encap=WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
+        break;
+    case KHciLoggerDatalinkTypeBCSP:
+        *err = WTAP_ERR_UNSUPPORTED;
+        *err_info = g_strdup("btsnoop: BCSP capture logs unsupported");
+        return WTAP_OPEN_ERROR;
+    case KHciLoggerDatalinkTypeH5:
+        *err = WTAP_ERR_UNSUPPORTED;
+        *err_info = g_strdup("btsnoop: H5 capture logs unsupported");
+        return WTAP_OPEN_ERROR;
+    case KHciLoggerDatalinkLinuxMonitor:
+        file_encap=WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR;
+        break;
+    case KHciLoggerDatalinkBlueZ5Simulator:
+        *err = WTAP_ERR_UNSUPPORTED;
+        *err_info = g_strdup("btsnoop: BlueZ 5 Simulator capture logs unsupported");
+        return WTAP_OPEN_ERROR;
+    default:
+        *err = WTAP_ERR_UNSUPPORTED;
+        *err_info = g_strdup_printf("btsnoop: datalink type %u unknown or unsupported", hdr.datalink);
+        return WTAP_OPEN_ERROR;
+    }
+
+    wth->subtype_read = btsnoop_read;
+    wth->subtype_seek_read = btsnoop_seek_read;
+    wth->file_encap = file_encap;
+    wth->snapshot_length = 0;   /* not available in header */
+    wth->file_tsprec = WTAP_TSPREC_USEC;
+    wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_BTSNOOP;
+    return WTAP_OPEN_MINE;
 }
 
 static gboolean btsnoop_read(wtap *wth, int *err, gchar **err_info,
-    gint64 *data_offset)
+                             gint64 *data_offset)
 {
-       *data_offset = file_tell(wth->fh);
+    *data_offset = file_tell(wth->fh);
 
-       /* Read record header. */
-       if (!btsnoop_read_record_header(wth, wth->fh, &wth->phdr, err, err_info))
-               return FALSE;
+    return btsnoop_read_record(wth, wth->fh, &wth->rec, wth->rec_data,
+                               err, err_info);
+}
 
-       /* Read packet data. */
-       buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
-       if (!btsnoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
-           wth->phdr.caplen, err, err_info)) {
-               return FALSE;   /* Read error */
-       }
+static gboolean btsnoop_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;
 
-       return TRUE;
+    return btsnoop_read_record(wth, wth->random_fh, rec, buf, err, err_info);
 }
 
-static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, guint8 *pd, int length,
-    int *err, gchar **err_info)
+static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
+                                    wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
 {
-       if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
-               return FALSE;
+    struct btsnooprec_hdr hdr;
+    guint32 packet_size;
+    guint32 flags;
+    guint32 orig_size;
+    gint64 ts;
 
-       /* Read record header. */
-       if (!btsnoop_read_record_header(wth, wth->random_fh, phdr, err, err_info))
-               return FALSE;
+    /* Read record header. */
 
-       /* Read packet data. */
-       if (!btsnoop_read_rec_data(wth->random_fh, pd, length, err, err_info))
-               return FALSE;   /* failed */
+    if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
+        return FALSE;
 
-       return TRUE;
-}
+    packet_size = g_ntohl(hdr.incl_len);
+    orig_size = g_ntohl(hdr.orig_len);
+    flags = g_ntohl(hdr.flags);
+    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_FILE;
+        *err_info = g_strdup_printf("btsnoop: File has %u-byte packet, bigger than maximum of %u",
+                                    packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
+        return FALSE;
+    }
 
-static gboolean btsnoop_read_record_header(wtap *wth, FILE_T fh,
-    struct wtap_pkthdr *phdr, int *err, gchar **err_info)
-{
-       int     bytes_read;
-       struct btsnooprec_hdr hdr;
-       guint32 packet_size;
-       guint32 flags;
-       guint32 orig_size;
-       gint64 ts;
-
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&hdr, sizeof hdr, fh);
-       if (bytes_read != sizeof hdr) {
-               *err = file_error(fh, err_info);
-               if (*err == 0 && bytes_read != 0)
-                       *err = WTAP_ERR_SHORT_READ;
-               return FALSE;
-       }
-
-       packet_size = g_ntohl(hdr.incl_len);
-       orig_size = g_ntohl(hdr.orig_len);
-       flags = g_ntohl(hdr.flags);
-       if (packet_size > 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("btsnoop: File has %u-byte packet, bigger than maximum of %u",
-                   packet_size, WTAP_MAX_PACKET_SIZE);
-               return FALSE;
-       }
-
-       ts = GINT64_FROM_BE(hdr.ts_usec);
-       ts -= KUnixTimeBase;
-
-       phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
-       phdr->ts.secs = (guint)(ts / 1000000);
-       phdr->ts.nsecs = (guint)((ts % 1000000) * 1000);
-       phdr->caplen = packet_size;
-       phdr->len = orig_size;
-       if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR)
-       {
-               phdr->pseudo_header.p2p.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
-       }
-       else if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_HCI)
-       {
-               phdr->pseudo_header.bthci.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
-               if(flags & KHciLoggerCommandOrEvent)
-               {
-                       if(phdr->pseudo_header.bthci.sent)
-                       {
-                               phdr->pseudo_header.bthci.channel = BTHCI_CHANNEL_COMMAND;
-                       }
-                       else
-                       {
-                               phdr->pseudo_header.bthci.channel = BTHCI_CHANNEL_EVENT;
-                       }
-               }
-               else
-               {
-                       phdr->pseudo_header.bthci.channel = BTHCI_CHANNEL_ACL;
-               }
-       }
-       return TRUE;
-}
+    ts = GINT64_FROM_BE(hdr.ts_usec);
+    ts -= KUnixTimeBase;
 
-static gboolean btsnoop_read_rec_data(FILE_T fh, guint8 *pd, int length,
-    int *err, gchar **err_info)
-{
-       int     bytes_read;
-
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(pd, length, fh);
-
-       if (bytes_read != length) {
-               *err = file_error(fh, err_info);
-               if (*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
-               return FALSE;
-       }
-       return TRUE;
+    rec->rec_type = REC_TYPE_PACKET;
+    rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
+    rec->ts.secs = (guint)(ts / 1000000);
+    rec->ts.nsecs = (guint)((ts % 1000000) * 1000);
+    rec->rec_header.packet_header.caplen = packet_size;
+    rec->rec_header.packet_header.len = orig_size;
+    if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR)
+    {
+        rec->rec_header.packet_header.pseudo_header.p2p.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
+    } else if(wth->file_encap == WTAP_ENCAP_BLUETOOTH_HCI) {
+        rec->rec_header.packet_header.pseudo_header.bthci.sent = (flags & KHciLoggerControllerToHost) ? FALSE : TRUE;
+        if(flags & KHciLoggerCommandOrEvent)
+        {
+            if(rec->rec_header.packet_header.pseudo_header.bthci.sent)
+            {
+                rec->rec_header.packet_header.pseudo_header.bthci.channel = BTHCI_CHANNEL_COMMAND;
+            }
+            else
+            {
+                rec->rec_header.packet_header.pseudo_header.bthci.channel = BTHCI_CHANNEL_EVENT;
+            }
+        }
+        else
+        {
+            rec->rec_header.packet_header.pseudo_header.bthci.channel = BTHCI_CHANNEL_ACL;
+        }
+    } else  if (wth->file_encap == WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR) {
+        rec->rec_header.packet_header.pseudo_header.btmon.opcode = flags & 0xFFFF;
+        rec->rec_header.packet_header.pseudo_header.btmon.adapter_id = flags >> 16;
+    }
+
+
+    /* Read packet data. */
+    return wtap_read_packet_bytes(fh, buf, rec->rec_header.packet_header.caplen, err, err_info);
 }
 
 /* Returns 0 if we could write the specified encapsulation type,
@@ -283,9 +229,9 @@ int btsnoop_dump_can_write_encap(int encap)
     if (encap == WTAP_ENCAP_PER_PACKET)
         return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
 
-    /* XXX - for now we only support WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
-    if (encap != WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR)
-        return WTAP_ERR_UNSUPPORTED_ENCAP;
+    /* XXX - for now we only support WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR and WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
+    if (encap != WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR && encap != WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR)
+        return WTAP_ERR_UNWRITABLE_ENCAP;
 
     return 0;
 }
@@ -321,10 +267,10 @@ static guint8 btsnoop_lookup_flags(guint8 hci_type, gboolean sent, guint8 *flags
     return FALSE;
 }
 
-static gboolean btsnoop_dump_partial_rec_hdr(wtap_dumper *wdh _U_,
-    const struct wtap_pkthdr *phdr,
+static gboolean btsnoop_format_partial_rec_hdr(
+    const wtap_rec *rec,
     const union wtap_pseudo_header *pseudo_header,
-    const guint8 *pd, int *err,
+    const guint8 *pd, int *err, gchar **err_info,
     struct btsnooprec_hdr *rec_hdr)
 {
     gint64 ts_usec;
@@ -332,12 +278,15 @@ static gboolean btsnoop_dump_partial_rec_hdr(wtap_dumper *wdh _U_,
     guint8 flags = 0;
 
     if (!btsnoop_lookup_flags(*pd, pseudo_header->p2p.sent, &flags)) {
-        *err = WTAP_ERR_UNSUPPORTED;
+        *err = WTAP_ERR_UNWRITABLE_REC_DATA;
+        *err_info = g_strdup_printf("btsnoop: hci_type 0x%02x for %s data isn't supported",
+                                    *pd,
+                                    pseudo_header->p2p.sent ? "sent" : "received");
         return FALSE;
     }
 
-    nsecs = phdr->ts.nsecs;
-    ts_usec  = ((gint64) phdr->ts.secs * 1000000) + (nsecs / 1000);
+    nsecs = rec->ts.nsecs;
+    ts_usec  = ((gint64) rec->ts.secs * 1000000) + (nsecs / 1000);
     ts_usec += KUnixTimeBase;
 
     rec_hdr->flags = GUINT32_TO_BE(flags);
@@ -349,17 +298,33 @@ static gboolean btsnoop_dump_partial_rec_hdr(wtap_dumper *wdh _U_,
 
 /* FIXME: How do we support multiple backends?*/
 static gboolean btsnoop_dump_h1(wtap_dumper *wdh,
-    const struct wtap_pkthdr *phdr,
-    const guint8 *pd, int *err)
+    const wtap_rec *rec,
+    const guint8 *pd, int *err, gchar **err_info)
 {
-    const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
+    const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
     struct btsnooprec_hdr rec_hdr;
 
-    if (!btsnoop_dump_partial_rec_hdr(wdh, phdr, pseudo_header, pd, err, &rec_hdr))
+    /* We can only write packet records. */
+    if (rec->rec_type != REC_TYPE_PACKET) {
+        *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
         return FALSE;
+    }
 
-    rec_hdr.incl_len = GUINT32_TO_BE(phdr->caplen-1);
-    rec_hdr.orig_len = GUINT32_TO_BE(phdr->len-1);
+    /*
+     * Don't write out anything bigger than we can read.
+     * (This will also fail on a caplen of 0, as it should.)
+     */
+    if (rec->rec_header.packet_header.caplen-1 > WTAP_MAX_PACKET_SIZE_STANDARD) {
+        *err = WTAP_ERR_PACKET_TOO_LARGE;
+        return FALSE;
+    }
+
+    if (!btsnoop_format_partial_rec_hdr(rec, pseudo_header, pd, err, err_info,
+                                        &rec_hdr))
+        return FALSE;
+
+    rec_hdr.incl_len = GUINT32_TO_BE(rec->rec_header.packet_header.caplen-1);
+    rec_hdr.orig_len = GUINT32_TO_BE(rec->rec_header.packet_header.len-1);
 
     if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
         return FALSE;
@@ -369,36 +334,49 @@ static gboolean btsnoop_dump_h1(wtap_dumper *wdh,
     /* Skip HCI packet type */
     ++pd;
 
-    if (!wtap_dump_file_write(wdh, pd, phdr->caplen-1, err))
+    if (!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen-1, err))
         return FALSE;
 
-    wdh->bytes_dumped += phdr->caplen-1;
+    wdh->bytes_dumped += rec->rec_header.packet_header.caplen-1;
 
     return TRUE;
 }
 
 static gboolean btsnoop_dump_h4(wtap_dumper *wdh,
-    const struct wtap_pkthdr *phdr,
-    const guint8 *pd, int *err)
+    const wtap_rec *rec,
+    const guint8 *pd, int *err, gchar **err_info)
 {
-    const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
+    const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
     struct btsnooprec_hdr rec_hdr;
 
-    if (!btsnoop_dump_partial_rec_hdr(wdh, phdr, pseudo_header, pd, err, &rec_hdr))
+    /* We can only write packet records. */
+    if (rec->rec_type != REC_TYPE_PACKET) {
+        *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
+        return FALSE;
+    }
+
+    /* Don't write out anything bigger than we can read. */
+    if (rec->rec_header.packet_header.caplen > WTAP_MAX_PACKET_SIZE_STANDARD) {
+        *err = WTAP_ERR_PACKET_TOO_LARGE;
+        return FALSE;
+    }
+
+    if (!btsnoop_format_partial_rec_hdr(rec, pseudo_header, pd, err, err_info,
+                                        &rec_hdr))
         return FALSE;
 
-    rec_hdr.incl_len = GUINT32_TO_BE(phdr->caplen);
-    rec_hdr.orig_len = GUINT32_TO_BE(phdr->len);
+    rec_hdr.incl_len = GUINT32_TO_BE(rec->rec_header.packet_header.caplen);
+    rec_hdr.orig_len = GUINT32_TO_BE(rec->rec_header.packet_header.len);
 
     if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
         return FALSE;
 
     wdh->bytes_dumped += sizeof rec_hdr;
 
-    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;
 
-    wdh->bytes_dumped += phdr->caplen;
+    wdh->bytes_dumped += rec->rec_header.packet_header.caplen;
 
     return TRUE;
 }
@@ -408,24 +386,10 @@ gboolean btsnoop_dump_open_h1(wtap_dumper *wdh, int *err)
 {
     struct btsnoop_hdr file_hdr;
 
-    /* This is a libpcap file */
+    /* This is a btsnoop file */
     wdh->subtype_write = btsnoop_dump_h1;
-    wdh->subtype_close = NULL;
 
     /* Write the file header. */
-    switch (wdh->file_type) {
-
-    case WTAP_FILE_BTSNOOP:
-        wdh->tsprecision = WTAP_FILE_TSPREC_USEC;
-        break;
-
-    default:
-        /* We should never get here - our open routine
-           should only get called for the types above. */
-        *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
-        return FALSE;
-    }
-
     if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
         return FALSE;
 
@@ -450,24 +414,10 @@ gboolean btsnoop_dump_open_h4(wtap_dumper *wdh, int *err)
 {
     struct btsnoop_hdr file_hdr;
 
-    /* This is a libpcap file */
+    /* This is a btsnoop file */
     wdh->subtype_write = btsnoop_dump_h4;
-    wdh->subtype_close = NULL;
 
     /* Write the file header. */
-    switch (wdh->file_type) {
-
-    case WTAP_FILE_BTSNOOP:
-        wdh->tsprecision = WTAP_FILE_TSPREC_USEC;
-        break;
-
-    default:
-        /* We should never get here - our open routine
-           should only get called for the types above. */
-        *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
-        return FALSE;
-    }
-
     if (!wtap_dump_file_write(wdh, btsnoop_magic, sizeof btsnoop_magic, err))
         return FALSE;
 
@@ -485,3 +435,16 @@ gboolean btsnoop_dump_open_h4(wtap_dumper *wdh, int *err)
 
     return TRUE;
 }
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
+ */