Revert "Allow wtap_read() and wtap_seek_read() to return non-packet records."
[metze/wireshark/wip.git] / wiretap / camins.c
1 /* camins.c
2  *
3  * File format support for Rabbit Labs CAM Inspector files
4  * Copyright (c) 2013 by Martin Kaiser <martin@kaiser.cx>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25
26 /* CAM Inspector is a commercial log tool for DVB-CI
27    it stores recorded packets between a CI module and a DVB receiver,
28    using a proprietary file format
29
30    a CAM Inspector file consists of 16bit blocks
31    the first byte contains payload data,
32    the second byte contains a "transaction type"
33
34    we currently support the following transaction types
35
36    0x20 == data transfer from CI module to host
37    0x22 == host reads the lower byte of the size register
38    0x23 == host reads the higher byte of the size register
39    0x2A == host writes the lower byte of the size register
40    0x2B == host writes the higher byte of the size register
41    0x28 == data transfer from host to CI module
42
43    using these transaction types, we can identify and assemble data transfers
44    from the host to the CAM and vice versa
45
46    a host->module data transfer will use the following transactions
47       one 0x2A and one 0x2B transaction to write the 16bit size
48       <size> 0x28 transactions to transfer one byte at a time
49    this will be assembled into one packet
50
51    the module->host transfer is similar
52
53    error handling
54    when we run into an error while assembling a data transfer, the
55    primary goal is to recover so that we can handle the next transfer
56    correctly (all files I used for testing contained errors where
57    apparently the logging hardware missed some bytes)
58 */
59
60 #include "config.h"
61
62 #include <string.h>
63 #include <glib.h>
64 #include <wtap.h>
65 #include <wtap-int.h>
66 #include <file_wrappers.h>
67 #include <buffer.h>
68
69 #include "camins.h"
70
71
72 #define TRANS_CAM_HOST        0x20
73 #define TRANS_READ_SIZE_LOW   0x22
74 #define TRANS_READ_SIZE_HIGH  0x23
75 #define TRANS_HOST_CAM        0x28
76 #define TRANS_WRITE_SIZE_LOW  0x2A
77 #define TRANS_WRITE_SIZE_HIGH 0x2B
78
79 #define IS_TRANS_SIZE(x) \
80     ((x)==TRANS_WRITE_SIZE_LOW || (x)==TRANS_WRITE_SIZE_HIGH || \
81      (x)==TRANS_READ_SIZE_LOW || (x)==TRANS_READ_SIZE_HIGH)
82
83 typedef enum {
84     SIZE_HAVE_NONE,
85     SIZE_HAVE_LOW,
86     SIZE_HAVE_HIGH,
87     SIZE_HAVE_ALL
88 } size_read_t;
89
90 #define RESET_STAT_VALS \
91 { \
92     *dat_trans_type = 0x00; \
93     *dat_len = 0x00; \
94     size_stat = SIZE_HAVE_NONE; \
95 }
96
97 #define SIZE_ADD_LOW \
98 { size_stat = (size_stat==SIZE_HAVE_HIGH ? SIZE_HAVE_ALL : SIZE_HAVE_LOW); }
99
100 #define SIZE_ADD_HIGH \
101 { size_stat = (size_stat==SIZE_HAVE_LOW ? SIZE_HAVE_ALL : SIZE_HAVE_HIGH); }
102
103 /* PCAP DVB-CI pseudo-header, see http://www.kaiser.cx/pcap-dvbci.html */
104 #define DVB_CI_PSEUDO_HDR_VER 0
105 #define DVB_CI_PSEUDO_HDR_LEN 4
106 #define DVB_CI_PSEUDO_HDR_CAM_TO_HOST 0xFF
107 #define DVB_CI_PSEUDO_HDR_HOST_TO_CAM 0xFE
108
109
110 /* read a block of data from the camins file and handle the errors */
111 static gboolean
112 read_block(FILE_T fh, guint8 *buf, guint16 buf_len, int *err, gchar **err_info)
113 {
114     int bytes_read;
115
116     bytes_read = file_read((void *)buf, buf_len, fh);
117     if (bytes_read != buf_len) {
118         *err = file_error(fh, err_info);
119         /* bytes_read==0 is end of file */
120         if (bytes_read>0 && *err == 0) {
121             *err = WTAP_ERR_SHORT_READ;
122         }
123         return FALSE;
124     }
125
126     return TRUE;
127 }
128
129
130 /* find the transaction type for the data bytes of the next packet
131     and the number of data bytes in that packet
132    the fd is moved such that it can be used in a subsequent call
133     to retrieve the data */
134 static gboolean
135 find_next_pkt_dat_type_len(FILE_T fh,
136         guint8 *dat_trans_type, /* transaction type used for the data bytes */
137         guint16 *dat_len,       /* the number of data bytes in the packet */
138         int *err, gchar **err_info)
139 {
140     guint8       block[2];
141     size_read_t  size_stat;
142
143     if (!dat_trans_type || !dat_len)
144         return FALSE;
145
146     RESET_STAT_VALS;
147
148     do {
149         if (read_block(fh, block, sizeof(block), err, err_info) == FALSE) {
150             RESET_STAT_VALS;
151             return FALSE;
152         }
153
154         /* our strategy is to continue reading until we have a high and a
155            low size byte for the same direction, duplicates or spurious data
156            bytes are ignored */
157
158         switch (block[1]) {
159             case TRANS_READ_SIZE_LOW:
160                 if (*dat_trans_type != TRANS_CAM_HOST)
161                     RESET_STAT_VALS;
162                 *dat_trans_type = TRANS_CAM_HOST;
163                 *dat_len |= block[0];
164                 SIZE_ADD_LOW;
165                 break;
166             case TRANS_READ_SIZE_HIGH:
167                 if (*dat_trans_type != TRANS_CAM_HOST)
168                     RESET_STAT_VALS;
169                 *dat_trans_type = TRANS_CAM_HOST;
170                 *dat_len |= (block[0] << 8);
171                 SIZE_ADD_HIGH;
172                 break;
173             case TRANS_WRITE_SIZE_LOW:
174                 if (*dat_trans_type != TRANS_HOST_CAM)
175                     RESET_STAT_VALS;
176                 *dat_trans_type = TRANS_HOST_CAM;
177                 *dat_len |= block[0];
178                 SIZE_ADD_LOW;
179                 break;
180             case TRANS_WRITE_SIZE_HIGH:
181                 if (*dat_trans_type != TRANS_HOST_CAM)
182                     RESET_STAT_VALS;
183                 *dat_trans_type = TRANS_HOST_CAM;
184                 *dat_len |= (block[0] << 8);
185                 SIZE_ADD_HIGH;
186                 break;
187             default:
188                 break;
189         }
190     } while (size_stat != SIZE_HAVE_ALL);
191
192     return TRUE;
193 }
194
195
196 /* buffer allocated by the caller, must be long enough to hold
197    dat_len bytes, ... */
198 static gint
199 read_packet_data(FILE_T fh, guint8 dat_trans_type, guint8 *buf, guint16 dat_len,
200                  int *err, gchar **err_info)
201 {
202     guint8  *p;
203     guint8   block[2];
204     guint16  bytes_count = 0;
205
206     if (!buf)
207         return -1;
208
209     /* we're not checking for end-of-file here, we read as many bytes as
210        we can get (up to dat_len) and return those
211        end-of-file will be detected when we search for the next packet */
212
213     p = buf;
214     while (bytes_count < dat_len) {
215         if (read_block(fh, block, sizeof(block), err, err_info) == FALSE)
216             break;
217
218         if (block[1] == dat_trans_type) {
219             *p++ = block[0];
220             bytes_count++;
221         }
222         else if (IS_TRANS_SIZE(block[1])) {
223             /* go back before the size transaction block
224                the next packet should be able to pick up this block */
225             if (-1 == file_seek(fh, -(gint64)sizeof(block), SEEK_CUR, err))
226                 return -1;
227             break;
228         }
229     }
230
231     return bytes_count;
232 }
233
234
235 /* create a DVB-CI pseudo header
236    return its length or -1 for error */
237 static gint
238 create_pseudo_hdr(guint8 *buf, guint8 dat_trans_type, guint16 dat_len)
239 {
240     if (!buf)
241         return -1;
242
243     buf[0] = DVB_CI_PSEUDO_HDR_VER;
244
245     if (dat_trans_type==TRANS_CAM_HOST)
246         buf[1] = DVB_CI_PSEUDO_HDR_CAM_TO_HOST;
247     else if (dat_trans_type==TRANS_HOST_CAM)
248         buf[1] = DVB_CI_PSEUDO_HDR_HOST_TO_CAM;
249     else
250         return -1;
251
252     buf[2] = (dat_len>>8) & 0xFF;
253     buf[3] = dat_len & 0xFF;
254
255     return DVB_CI_PSEUDO_HDR_LEN;
256 }
257
258
259 static gboolean
260 camins_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
261     int *err, gchar **err_info)
262 {
263     guint8      dat_trans_type;
264     guint16     dat_len;
265     guint8     *p;
266     gint        offset, bytes_read;
267
268     if (!find_next_pkt_dat_type_len(fh, &dat_trans_type, &dat_len, err, err_info))
269         return FALSE;
270
271     buffer_assure_space(buf, DVB_CI_PSEUDO_HDR_LEN+dat_len);
272     p = buffer_start_ptr(buf);
273     /* NULL check for p is done in create_pseudo_hdr() */
274     offset = create_pseudo_hdr(p, dat_trans_type, dat_len);
275     if (offset<0) {
276         /* shouldn't happen, all invalid packets must be detected by
277            find_next_pkt_dat_type_len() */
278         *err = WTAP_ERR_INTERNAL;
279         return FALSE;
280     }
281
282     bytes_read = read_packet_data(fh, dat_trans_type,
283             &p[offset], dat_len, err, err_info);
284     /* 0<=bytes_read<=dat_len is very likely a corrupted packet
285        we let the dissector handle this */
286     if (bytes_read < 0)
287         return FALSE;
288     offset += bytes_read;
289
290     phdr->pkt_encap = WTAP_ENCAP_DVBCI;
291     /* timestamps aren't supported for now */
292     phdr->caplen = offset;
293     phdr->len = offset;
294
295     return TRUE;
296 }
297
298
299 static gboolean
300 camins_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
301 {
302     *data_offset = file_tell(wth->fh);
303
304     return camins_read_packet(wth->fh, &wth->phdr, wth->frame_buffer, err,
305         err_info);
306 }
307
308
309 static gboolean
310 camins_seek_read(wtap *wth, gint64 seek_off,
311     struct wtap_pkthdr *pkthdr, Buffer *buf, int *err, gchar **err_info)
312 {
313     if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err))
314         return FALSE;
315
316     return camins_read_packet(wth->random_fh, pkthdr, buf, err, err_info);
317 }
318
319
320
321 int camins_open(wtap *wth, int *err, gchar **err_info _U_)
322 {
323     guint8  found_start_blocks = 0;
324     guint8  count = 0;
325     guint8  block[2];
326     int     bytes_read;
327
328     /* all CAM Inspector files I've looked at have at least two blocks of
329        0x00 0xE1 within the first 20 bytes */
330     do {
331         bytes_read = file_read(block, sizeof(block), wth->fh);
332         if (bytes_read != sizeof(block))
333             break;
334
335         if (block[0]==0x00 && block[1] == 0xE1)
336             found_start_blocks++;
337
338         count++;
339     } while (count<20);
340
341     if (found_start_blocks < 2)
342         return 0;   /* no CAM Inspector file */
343
344     /* rewind the fh so we re-read from the beginning */
345     if (-1 == file_seek(wth->fh, 0, SEEK_SET, err))
346         return -1;
347
348    wth->file_encap = WTAP_ENCAP_DVBCI;
349    wth->snapshot_length = 0;
350    wth->tsprecision = WTAP_FILE_TSPREC_MSEC;
351
352    wth->priv = NULL;
353
354    wth->subtype_read = camins_read;
355    wth->subtype_seek_read = camins_seek_read;
356    wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_CAMINS;
357
358    *err = 0;
359    return 1;
360 }
361
362
363 /*
364  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
365  *
366  * Local variables:
367  * c-basic-offset: 4
368  * tab-width: 8
369  * indent-tabs-mode: nil
370  * End:
371  *
372  * vi: set shiftwidth=4 tabstop=8 expandtab:
373  * :indentSize=4:tabSize=8:noTabs=true:
374  */