3 * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "file_wrappers.h"
24 #include <wsutil/buffer.h>
35 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
37 static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
38 Buffer *buf, int *err, gchar **err_info)
43 if (!wtap_read_bytes_or_eof(fh, &dh, DUMP_HDR_SIZE, err, err_info))
46 packet_size = GUINT16_FROM_LE(dh.len);
47 if (packet_size > WTAP_MAX_PACKET_SIZE) {
49 * Probably a corrupt capture file; don't blow up trying
50 * to allocate space for an immensely-large packet.
52 *err = WTAP_ERR_BAD_FILE;
53 *err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
54 packet_size, WTAP_MAX_PACKET_SIZE);
58 phdr->rec_type = REC_TYPE_PACKET;
59 phdr->presence_flags = WTAP_HAS_TS;
60 phdr->ts.secs = GUINT32_FROM_LE(dh.ts_sec);
61 phdr->ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
62 phdr->caplen = packet_size;
63 phdr->len = packet_size;
65 phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
67 return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
70 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
73 *data_offset = file_tell(wth->fh);
75 return hcidump_process_packet(wth->fh, &wth->phdr, wth->frame_buffer,
79 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
80 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
82 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
85 return hcidump_process_packet(wth->random_fh, phdr, buf, err, err_info);
88 wtap_open_return_val hcidump_open(wtap *wth, int *err, gchar **err_info)
93 if (!wtap_read_bytes(wth->fh, &dh, DUMP_HDR_SIZE, err, err_info)) {
94 if (*err != WTAP_ERR_SHORT_READ)
95 return WTAP_OPEN_ERROR;
96 return WTAP_OPEN_NOT_MINE;
99 if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
100 || GUINT16_FROM_LE(dh.len) < 1)
101 return WTAP_OPEN_NOT_MINE;
103 if (!wtap_read_bytes(wth->fh, &type, 1, err, err_info)) {
104 if (*err != WTAP_ERR_SHORT_READ)
105 return WTAP_OPEN_ERROR;
106 return WTAP_OPEN_NOT_MINE;
109 if (type < 1 || type > 4)
110 return WTAP_OPEN_NOT_MINE;
112 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
113 return WTAP_OPEN_ERROR;
115 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_HCIDUMP;
116 wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
117 wth->snapshot_length = 0;
119 wth->subtype_read = hcidump_read;
120 wth->subtype_seek_read = hcidump_seek_read;
121 wth->file_tsprec = WTAP_TSPREC_USEC;
123 return WTAP_OPEN_MINE;