3 * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
5 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "file_wrappers.h"
14 static int hcidump_file_type_subtype
= -1;
16 void register_hcidump(void);
26 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
28 static bool hcidump_read_packet(FILE_T fh
, wtap_rec
*rec
,
29 int *err
, char **err_info
)
34 if (!wtap_read_bytes_or_eof(fh
, &dh
, DUMP_HDR_SIZE
, err
, err_info
))
37 packet_size
= GUINT16_FROM_LE(dh
.len
);
38 if (packet_size
> WTAP_MAX_PACKET_SIZE_STANDARD
) {
40 * Probably a corrupt capture file; don't blow up trying
41 * to allocate space for an immensely-large packet.
43 *err
= WTAP_ERR_BAD_FILE
;
44 *err_info
= ws_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
45 packet_size
, WTAP_MAX_PACKET_SIZE_STANDARD
);
49 rec
->rec_type
= REC_TYPE_PACKET
;
50 rec
->block
= wtap_block_create(WTAP_BLOCK_PACKET
);
51 rec
->presence_flags
= WTAP_HAS_TS
;
52 rec
->ts
.secs
= GUINT32_FROM_LE(dh
.ts_sec
);
53 rec
->ts
.nsecs
= GUINT32_FROM_LE(dh
.ts_usec
) * 1000;
54 rec
->rec_header
.packet_header
.caplen
= packet_size
;
55 rec
->rec_header
.packet_header
.len
= packet_size
;
57 rec
->rec_header
.packet_header
.pseudo_header
.p2p
.sent
= (dh
.in
? false : true);
59 return wtap_read_bytes_buffer(fh
, &rec
->data
, packet_size
, err
, err_info
);
62 static bool hcidump_read(wtap
*wth
, wtap_rec
*rec
,
63 int *err
, char **err_info
, int64_t *data_offset
)
65 *data_offset
= file_tell(wth
->fh
);
67 return hcidump_read_packet(wth
->fh
, rec
, err
, err_info
);
70 static bool hcidump_seek_read(wtap
*wth
, int64_t seek_off
,
71 wtap_rec
*rec
, int *err
, char **err_info
)
73 if (file_seek(wth
->random_fh
, seek_off
, SEEK_SET
, err
) == -1)
76 return hcidump_read_packet(wth
->random_fh
, rec
, err
, err_info
);
79 wtap_open_return_val
hcidump_open(wtap
*wth
, int *err
, char **err_info
)
84 if (!wtap_read_bytes(wth
->fh
, &dh
, DUMP_HDR_SIZE
, err
, err_info
)) {
85 if (*err
!= WTAP_ERR_SHORT_READ
)
86 return WTAP_OPEN_ERROR
;
87 return WTAP_OPEN_NOT_MINE
;
90 if ((dh
.in
!= 0 && dh
.in
!= 1) || dh
.pad
!= 0
91 || GUINT16_FROM_LE(dh
.len
) < 1)
92 return WTAP_OPEN_NOT_MINE
;
94 if (!wtap_read_bytes(wth
->fh
, &type
, 1, err
, err_info
)) {
95 if (*err
!= WTAP_ERR_SHORT_READ
)
96 return WTAP_OPEN_ERROR
;
97 return WTAP_OPEN_NOT_MINE
;
100 if (type
< 1 || type
> 4)
101 return WTAP_OPEN_NOT_MINE
;
103 if (file_seek(wth
->fh
, 0, SEEK_SET
, err
) == -1)
104 return WTAP_OPEN_ERROR
;
106 wth
->file_type_subtype
= hcidump_file_type_subtype
;
107 wth
->file_encap
= WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR
;
108 wth
->snapshot_length
= 0;
110 wth
->subtype_read
= hcidump_read
;
111 wth
->subtype_seek_read
= hcidump_seek_read
;
112 wth
->file_tsprec
= WTAP_TSPREC_USEC
;
115 * Add an IDB; we don't know how many interfaces were
116 * involved, so we just say one interface, about which
117 * we only know the link-layer type, snapshot length,
118 * and time stamp resolution.
120 wtap_add_generated_idb(wth
);
122 return WTAP_OPEN_MINE
;
125 static const struct supported_block_type hcidummp_blocks_supported
[] = {
127 * We support packet blocks, with no comments or other options.
129 { WTAP_BLOCK_PACKET
, MULTIPLE_BLOCKS_SUPPORTED
, NO_OPTIONS_SUPPORTED
}
132 static const struct file_type_subtype_info hcidump_info
= {
133 "Bluetooth HCI dump", "hcidump", NULL
, NULL
,
134 false, BLOCKS_SUPPORTED(hcidummp_blocks_supported
),
138 void register_hcidump(void)
140 hcidump_file_type_subtype
= wtap_register_file_type_subtype(&hcidump_info
);
143 * Register name for backwards compatibility with the
144 * wtap_filetypes table in Lua.
146 wtap_register_backwards_compatibility_lua_name("HCIDUMP",
147 hcidump_file_type_subtype
);
151 * Editor modelines - https://www.wireshark.org/tools/modelines.html
156 * indent-tabs-mode: t
159 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
160 * :indentSize=8:tabSize=8:noTabs=false: