82657f2e2be1bec8dc0bac969e4d40272d458df8
[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 <wsutil/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 /* find the transaction type for the data bytes of the next packet
111     and the number of data bytes in that packet
112    the fd is moved such that it can be used in a subsequent call
113     to retrieve the data */
114 static gboolean
115 find_next_pkt_dat_type_len(FILE_T fh,
116         guint8 *dat_trans_type, /* transaction type used for the data bytes */
117         guint16 *dat_len,       /* the number of data bytes in the packet */
118         int *err, gchar **err_info)
119 {
120     guint8       block[2];
121     size_read_t  size_stat;
122
123     if (!dat_trans_type || !dat_len)
124         return FALSE;
125
126     RESET_STAT_VALS;
127
128     do {
129         if (!wtap_read_bytes_or_eof(fh, block, sizeof(block), err, err_info)) {
130             RESET_STAT_VALS;
131             return FALSE;
132         }
133
134         /* our strategy is to continue reading until we have a high and a
135            low size byte for the same direction, duplicates or spurious data
136            bytes are ignored */
137
138         switch (block[1]) {
139             case TRANS_READ_SIZE_LOW:
140                 if (*dat_trans_type != TRANS_CAM_HOST)
141                     RESET_STAT_VALS;
142                 *dat_trans_type = TRANS_CAM_HOST;
143                 *dat_len |= block[0];
144                 SIZE_ADD_LOW;
145                 break;
146             case TRANS_READ_SIZE_HIGH:
147                 if (*dat_trans_type != TRANS_CAM_HOST)
148                     RESET_STAT_VALS;
149                 *dat_trans_type = TRANS_CAM_HOST;
150                 *dat_len |= (block[0] << 8);
151                 SIZE_ADD_HIGH;
152                 break;
153             case TRANS_WRITE_SIZE_LOW:
154                 if (*dat_trans_type != TRANS_HOST_CAM)
155                     RESET_STAT_VALS;
156                 *dat_trans_type = TRANS_HOST_CAM;
157                 *dat_len |= block[0];
158                 SIZE_ADD_LOW;
159                 break;
160             case TRANS_WRITE_SIZE_HIGH:
161                 if (*dat_trans_type != TRANS_HOST_CAM)
162                     RESET_STAT_VALS;
163                 *dat_trans_type = TRANS_HOST_CAM;
164                 *dat_len |= (block[0] << 8);
165                 SIZE_ADD_HIGH;
166                 break;
167             default:
168                 break;
169         }
170     } while (size_stat != SIZE_HAVE_ALL);
171
172     return TRUE;
173 }
174
175
176 /* buffer allocated by the caller, must be long enough to hold
177    dat_len bytes, ... */
178 static gint
179 read_packet_data(FILE_T fh, guint8 dat_trans_type, guint8 *buf, guint16 dat_len,
180                  int *err, gchar **err_info)
181 {
182     guint8  *p;
183     guint8   block[2];
184     guint16  bytes_count = 0;
185
186     if (!buf)
187         return -1;
188
189     /* we're not checking for end-of-file here, we read as many bytes as
190        we can get (up to dat_len) and return those
191        end-of-file will be detected when we search for the next packet */
192
193     p = buf;
194     while (bytes_count < dat_len) {
195         if (!wtap_read_bytes_or_eof(fh, block, sizeof(block), err, err_info))
196             break;
197
198         if (block[1] == dat_trans_type) {
199             *p++ = block[0];
200             bytes_count++;
201         }
202         else if (IS_TRANS_SIZE(block[1])) {
203             /* go back before the size transaction block
204                the next packet should be able to pick up this block */
205             if (-1 == file_seek(fh, -(gint64)sizeof(block), SEEK_CUR, err))
206                 return -1;
207             break;
208         }
209     }
210
211     return bytes_count;
212 }
213
214
215 /* create a DVB-CI pseudo header
216    return its length or -1 for error */
217 static gint
218 create_pseudo_hdr(guint8 *buf, guint8 dat_trans_type, guint16 dat_len)
219 {
220     if (!buf)
221         return -1;
222
223     buf[0] = DVB_CI_PSEUDO_HDR_VER;
224
225     if (dat_trans_type==TRANS_CAM_HOST)
226         buf[1] = DVB_CI_PSEUDO_HDR_CAM_TO_HOST;
227     else if (dat_trans_type==TRANS_HOST_CAM)
228         buf[1] = DVB_CI_PSEUDO_HDR_HOST_TO_CAM;
229     else
230         return -1;
231
232     buf[2] = (dat_len>>8) & 0xFF;
233     buf[3] = dat_len & 0xFF;
234
235     return DVB_CI_PSEUDO_HDR_LEN;
236 }
237
238
239 static gboolean
240 camins_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
241     int *err, gchar **err_info)
242 {
243     guint8      dat_trans_type;
244     guint16     dat_len;
245     guint8     *p;
246     gint        offset, bytes_read;
247
248     if (!find_next_pkt_dat_type_len(fh, &dat_trans_type, &dat_len, err, err_info))
249         return FALSE;
250
251     ws_buffer_assure_space(buf, DVB_CI_PSEUDO_HDR_LEN+dat_len);
252     p = ws_buffer_start_ptr(buf);
253     /* NULL check for p is done in create_pseudo_hdr() */
254     offset = create_pseudo_hdr(p, dat_trans_type, dat_len);
255     if (offset<0) {
256         /* shouldn't happen, all invalid packets must be detected by
257            find_next_pkt_dat_type_len() */
258         *err = WTAP_ERR_INTERNAL;
259         return FALSE;
260     }
261
262     bytes_read = read_packet_data(fh, dat_trans_type,
263             &p[offset], dat_len, err, err_info);
264     /* 0<=bytes_read<=dat_len is very likely a corrupted packet
265        we let the dissector handle this */
266     if (bytes_read < 0)
267         return FALSE;
268     offset += bytes_read;
269
270     phdr->rec_type = REC_TYPE_PACKET;
271     phdr->pkt_encap = WTAP_ENCAP_DVBCI;
272     /* timestamps aren't supported for now */
273     phdr->caplen = offset;
274     phdr->len = offset;
275
276     return TRUE;
277 }
278
279
280 static gboolean
281 camins_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
282 {
283     *data_offset = file_tell(wth->fh);
284
285     return camins_read_packet(wth->fh, &wth->phdr, wth->frame_buffer, err,
286         err_info);
287 }
288
289
290 static gboolean
291 camins_seek_read(wtap *wth, gint64 seek_off,
292     struct wtap_pkthdr *pkthdr, Buffer *buf, int *err, gchar **err_info)
293 {
294     if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err))
295         return FALSE;
296
297     return camins_read_packet(wth->random_fh, pkthdr, buf, err, err_info);
298 }
299
300
301
302 int camins_open(wtap *wth, int *err, gchar **err_info _U_)
303 {
304     guint8  found_start_blocks = 0;
305     guint8  count = 0;
306     guint8  block[2];
307
308     /* all CAM Inspector files I've looked at have at least two blocks of
309        0x00 0xE1 within the first 20 bytes */
310     do {
311         if (!wtap_read_bytes(wth->fh, block, sizeof(block), err, err_info)) {
312             if (*err == WTAP_ERR_SHORT_READ)
313                 break;
314             return -1;
315         }
316
317         if (block[0]==0x00 && block[1] == 0xE1)
318             found_start_blocks++;
319
320         count++;
321     } while (count<20);
322
323     if (found_start_blocks < 2)
324         return 0;   /* no CAM Inspector file */
325
326     /* rewind the fh so we re-read from the beginning */
327     if (-1 == file_seek(wth->fh, 0, SEEK_SET, err))
328         return -1;
329
330    wth->file_encap = WTAP_ENCAP_DVBCI;
331    wth->snapshot_length = 0;
332    wth->file_tsprec = WTAP_TSPREC_MSEC;
333
334    wth->priv = NULL;
335
336    wth->subtype_read = camins_read;
337    wth->subtype_seek_read = camins_seek_read;
338    wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_CAMINS;
339
340    *err = 0;
341    return 1;
342 }
343
344
345 /*
346  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
347  *
348  * Local variables:
349  * c-basic-offset: 4
350  * tab-width: 8
351  * indent-tabs-mode: nil
352  * End:
353  *
354  * vi: set shiftwidth=4 tabstop=8 expandtab:
355  * :indentSize=4:tabSize=8:noTabs=true:
356  */