Move the definitions of LANalyzer records to lanalyzer.c.
[obnox/wireshark/wip.git] / wiretap / hcidump.c
1 /* hcidump.c
2  *
3  * $Id$
4  *
5  * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "wtap-int.h"
27 #include "file_wrappers.h"
28 #include "buffer.h"
29 #include "hcidump.h"
30
31 struct dump_hdr {
32         guint16 len;
33         guint8  in;
34         guint8  pad;
35         guint32 ts_sec;
36         guint32 ts_usec;
37 };
38
39 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
40
41 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
42     gint64 *data_offset)
43 {
44         struct dump_hdr dh;
45         guint8 *buf;
46         int bytes_read, packet_size;
47
48         *data_offset = wth->data_offset;
49
50         bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
51         if (bytes_read != DUMP_HDR_SIZE) {
52                 *err = file_error(wth->fh, err_info);
53                 if (*err == 0 && bytes_read != 0)
54                         *err = WTAP_ERR_SHORT_READ;
55                 return FALSE;
56         }
57         wth->data_offset += DUMP_HDR_SIZE;
58
59         packet_size = GUINT16_FROM_LE(dh.len);
60         if (packet_size > WTAP_MAX_PACKET_SIZE) {
61                 /*
62                  * Probably a corrupt capture file; don't blow up trying
63                  * to allocate space for an immensely-large packet.
64                  */
65                 *err = WTAP_ERR_BAD_RECORD;
66                 *err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
67                         packet_size, WTAP_MAX_PACKET_SIZE);
68                 return FALSE;
69         }
70
71         buffer_assure_space(wth->frame_buffer, packet_size);
72         buf = buffer_start_ptr(wth->frame_buffer);
73
74         bytes_read = file_read(buf, packet_size, wth->fh);
75         if (bytes_read != packet_size) {
76                 *err = file_error(wth->fh, err_info);
77                 if (*err == 0)
78                         *err = WTAP_ERR_SHORT_READ;
79                 return FALSE;
80         }
81         wth->data_offset += packet_size;
82
83         wth->phdr.ts.secs = GUINT32_FROM_LE(dh.ts_sec);
84         wth->phdr.ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
85         wth->phdr.caplen = packet_size;
86         wth->phdr.len = packet_size;
87
88         wth->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
89
90         return TRUE;
91 }
92
93 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
94     union wtap_pseudo_header *pseudo_header, guint8 *pd, int length,
95     int *err, gchar **err_info)
96 {
97         struct dump_hdr dh;
98         int bytes_read;
99
100         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
101                 return FALSE;
102
103         bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->random_fh);
104         if (bytes_read != DUMP_HDR_SIZE) {
105                 *err = file_error(wth->random_fh, err_info);
106                 if (*err == 0 && bytes_read != 0)
107                         *err = WTAP_ERR_SHORT_READ;
108                 return FALSE;
109         }
110
111         bytes_read = file_read(pd, length, wth->random_fh);
112         if (bytes_read != length) {
113                 *err = file_error(wth->random_fh, err_info);
114                 if (*err == 0)
115                         *err = WTAP_ERR_SHORT_READ;
116                 return FALSE;
117         }
118
119         pseudo_header->p2p.sent = (dh.in ? FALSE : TRUE);
120
121         return TRUE;
122 }
123
124 int hcidump_open(wtap *wth, int *err, gchar **err_info)
125 {
126         struct dump_hdr dh;
127         guint8 type;
128         int bytes_read;
129
130         bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
131         if (bytes_read != DUMP_HDR_SIZE) {
132                 *err = file_error(wth->fh, err_info);
133                 return (*err != 0) ? -1 : 0;
134         }
135
136         if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
137             || GUINT16_FROM_LE(dh.len) < 1)
138                 return 0;
139
140         bytes_read = file_read(&type, 1, wth->fh);
141         if (bytes_read != 1) {
142                 *err = file_error(wth->fh, err_info);
143                 return (*err != 0) ? -1 : 0;
144         }
145
146         if (type < 1 || type > 4)
147                 return 0;
148
149         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
150                 return -1;
151
152         wth->file_type = WTAP_FILE_HCIDUMP;
153         wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
154         wth->snapshot_length = 0;
155
156         wth->subtype_read = hcidump_read;
157         wth->subtype_seek_read = hcidump_seek_read;
158         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
159
160         return 1;
161 }