5f7634abb2561517ecd1411970a8b45e8d9f52b6
[metze/wireshark/wip.git] / wiretap / hcidump.c
1 /* hcidump.c
2  *
3  * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "config.h"
21
22 #include "wtap-int.h"
23 #include "file_wrappers.h"
24 #include <wsutil/buffer.h>
25 #include "hcidump.h"
26
27 struct dump_hdr {
28         guint16 len;
29         guint8  in;
30         guint8  pad;
31         guint32 ts_sec;
32         guint32 ts_usec;
33 };
34
35 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
36
37 static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
38     Buffer *buf, int *err, gchar **err_info)
39 {
40         struct dump_hdr dh;
41         int packet_size;
42
43         if (!wtap_read_bytes_or_eof(fh, &dh, DUMP_HDR_SIZE, err, err_info))
44                 return FALSE;
45
46         packet_size = GUINT16_FROM_LE(dh.len);
47         if (packet_size > WTAP_MAX_PACKET_SIZE) {
48                 /*
49                  * Probably a corrupt capture file; don't blow up trying
50                  * to allocate space for an immensely-large packet.
51                  */
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);
55                 return FALSE;
56         }
57
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;
64
65         phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
66
67         return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
68 }
69
70 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
71     gint64 *data_offset)
72 {
73         *data_offset = file_tell(wth->fh);
74
75         return hcidump_process_packet(wth->fh, &wth->phdr, wth->frame_buffer,
76             err, err_info);
77 }
78
79 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
80     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
81 {
82         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
83                 return FALSE;
84
85         return hcidump_process_packet(wth->random_fh, phdr, buf, err, err_info);
86 }
87
88 wtap_open_return_val hcidump_open(wtap *wth, int *err, gchar **err_info)
89 {
90         struct dump_hdr dh;
91         guint8 type;
92
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;
97         }
98
99         if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
100             || GUINT16_FROM_LE(dh.len) < 1)
101                 return WTAP_OPEN_NOT_MINE;
102
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;
107         }
108
109         if (type < 1 || type > 4)
110                 return WTAP_OPEN_NOT_MINE;
111
112         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
113                 return WTAP_OPEN_ERROR;
114
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;
118
119         wth->subtype_read = hcidump_read;
120         wth->subtype_seek_read = hcidump_seek_read;
121         wth->file_tsprec = WTAP_TSPREC_USEC;
122
123         return WTAP_OPEN_MINE;
124 }