Squelch a compiler warning.
[metze/wireshark/wip.git] / wiretap / capsa.c
1 /* capsa.c
2  *
3  * Wiretap Library
4  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "config.h"
22 #include <errno.h>
23 #include <string.h>
24 #include "wtap-int.h"
25 #include "file_wrappers.h"
26 #include "capsa.h"
27
28 /*
29  * A file begins with a header containing:
30  *
31  *   a 4-byte magic number, with 'c', 'p', 's', 'e';
32  *
33  *   either a 2-byte little-endian "format indicator" (version number?),
34  *   or a 1-byte major version number followed by a 1-byte minor version
35  *   number, or a 1-byte "format indicator" followed by something else
36  *   that's always been 0;
37  *
38  *   a 2-byte 0xe8 0x03 (1000 - a data rate?  megabits/second?)
39  *
40  *   4 bytes of 0x01 0x00 0x01 0x00;
41  *
42  *   either a 4-byte little-endian file size followed by 0x00 0x00 0x00 0x00
43  *   or an 8-byte little-endian file size;
44  *
45  *   a 4-byte little-endian packet count (in dns_error_of_udp, it exceeds?)
46  *
47  *   a 4-byte little-endian number?
48  *
49  *   hex 2c 01 c8 00 00 00 da 36 00 00 00 00 00 00;
50  *
51  *   the same 4-byte little-endian number as above (yes, misaligned);
52  *
53  *   0x01 or 0x03;
54  *
55  *   a bunch of 0s, up to an offset of 0x36d6;
56  *
57  *   more stuff.
58  *
59  * Following that is a sequence of { record offset block, up to 200 records }
60  * pairs.
61  *
62  * A record offset block has 1 byte with the value 0xfe, a sequence of
63  * up to 200 4-byte little-endian record offsets, and 4 or more bytes
64  * of unknown data, making the block 805 bytes long.
65  *
66  * The record offsets are offsets, from the beginning of the record offset
67  * block (i.e., from the 0xfe byte), of the records following the block.
68  */
69
70 /* Magic number in Capsa files. */
71 static const char capsa_magic[] = {
72         'c', 'p', 's', 'e'
73 };
74
75 /*
76  * Before each group of 200 or fewer records there's a block of frame
77  * offsets, giving the offsets, from the beginning of that block minus
78  * one(1), of the next N records.
79  */
80 #define N_RECORDS_PER_GROUP     200
81
82 /* Capsa (format indicator 1) record header. */
83 struct capsarec_hdr {
84         guint32 unknown1;       /* low-order 32 bits of a number? */
85         guint32 unknown2;       /* 0x00 0x00 0x00 0x00 */
86         guint32 timestamplo;    /* low-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
87         guint32 timestamphi;    /* high-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
88         guint16 rec_len;        /* length of record */
89         guint16 incl_len;       /* number of octets captured in file */
90         guint16 orig_len;       /* actual length of packet */
91         guint16 unknown5;       /* 0x00 0x00 */
92         guint8 count1;          /* count1*4 bytes after unknown8 */
93         guint8 count2;          /* count2*4 bytes after that */
94         guint16 unknown7;       /* 0x01 0x10 */
95         guint32 unknown8;       /* 0x00 0x00 0x00 0x00 or random numbers */
96 };
97
98 /* Packet Builder (format indicator 2) record header. */
99 struct pbrec_hdr {
100         guint16 rec_len;        /* length of record */
101         guint16 incl_len;       /* number of octets captured in file */
102         guint16 orig_len;       /* actual length of packet */
103         guint16 unknown1;
104         guint16 unknown2;
105         guint16 unknown3;
106         guint32 unknown4;
107         guint32 timestamplo;    /* low-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
108         guint32 timestamphi;    /* high-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
109         guint32 unknown5;
110         guint32 unknown6;
111 };
112
113 typedef struct {
114         guint16 format_indicator;
115         guint32 number_of_frames;
116         guint32 frame_count;
117         gint64 base_offset;
118         guint32 record_offsets[N_RECORDS_PER_GROUP];
119 } capsa_t;
120
121 static gboolean capsa_read(wtap *wth, int *err, gchar **err_info,
122     gint64 *data_offset);
123 static gboolean capsa_seek_read(wtap *wth, gint64 seek_off,
124     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
125 static int capsa_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
126     Buffer *buf, int *err, gchar **err_info);
127
128 wtap_open_return_val capsa_open(wtap *wth, int *err, gchar **err_info)
129 {
130         char magic[sizeof capsa_magic];
131         guint16 format_indicator;
132         int file_type_subtype;
133         guint32 number_of_frames;
134         capsa_t *capsa;
135
136         /* Read in the string that should be at the start of a Capsa file */
137         if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
138                 if (*err != WTAP_ERR_SHORT_READ)
139                         return WTAP_OPEN_ERROR;
140                 return WTAP_OPEN_NOT_MINE;
141         }
142
143         if (memcmp(magic, capsa_magic, sizeof capsa_magic) != 0) {
144                 return WTAP_OPEN_NOT_MINE;
145         }
146
147         /* Read the mysterious "format indicator" */
148         if (!wtap_read_bytes(wth->fh, &format_indicator, sizeof format_indicator,
149             err, err_info))
150                 return WTAP_OPEN_ERROR;
151         format_indicator = GUINT16_FROM_LE(format_indicator);
152
153         /*
154          * Make sure it's a format we support.
155          */
156         switch (format_indicator) {
157
158         case 1:         /* Capsa */
159                 file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COLASOFT_CAPSA;
160                 break;
161
162         case 2:         /* Packet Builder */
163                 file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COLASOFT_PACKET_BUILDER;
164                 break;
165
166         default:
167                 *err = WTAP_ERR_UNSUPPORTED;
168                 *err_info = g_strdup_printf("capsa: format indicator %u unsupported",
169                     format_indicator);
170                 return WTAP_OPEN_ERROR;
171         }
172
173         /*
174          * Link speed, in megabytes/second?
175          */
176         if (!file_skip(wth->fh, 2, err))
177                 return WTAP_OPEN_ERROR;
178
179         /*
180          * Flags of some sort?  Four 1-byte numbers, two of which are 1
181          * and two of which are zero?  Two 2-byte numbers or flag fields,
182          * both of which are 1?
183          */
184         if (!file_skip(wth->fh, 4, err))
185                 return WTAP_OPEN_ERROR;
186
187         /*
188          * File size, in bytes.
189          */
190         if (!file_skip(wth->fh, 4, err))
191                 return WTAP_OPEN_ERROR;
192
193         /*
194          * Zeroes?  Or upper 4 bytes of file size?
195          */
196         if (!file_skip(wth->fh, 4, err))
197                 return WTAP_OPEN_ERROR;
198
199         /*
200          * Count of packets.
201          */
202         if (!wtap_read_bytes(wth->fh, &number_of_frames, sizeof number_of_frames,
203             err, err_info))
204                 return WTAP_OPEN_ERROR;
205         number_of_frames = GUINT32_FROM_LE(number_of_frames);
206
207         /*
208          * Skip past what we think is file header.
209          */
210         if (!file_seek(wth->fh, 0x44ef, SEEK_SET, err))
211                 return WTAP_OPEN_ERROR;
212
213         wth->file_type_subtype = file_type_subtype;
214         capsa = (capsa_t *)g_malloc(sizeof(capsa_t));
215         capsa->format_indicator = format_indicator;
216         capsa->number_of_frames = number_of_frames;
217         capsa->frame_count = 0;
218         wth->priv = (void *)capsa;
219         wth->subtype_read = capsa_read;
220         wth->subtype_seek_read = capsa_seek_read;
221         /*
222          * XXX - we've never seen a Wi-Fi Capsa capture, so we don't
223          * yet know how to handle them.
224          */
225         wth->file_encap = WTAP_ENCAP_ETHERNET;
226         wth->snapshot_length = 0;       /* not available in header */
227         wth->file_tsprec = WTAP_TSPREC_USEC;
228         return WTAP_OPEN_MINE;
229 }
230
231 /* Read the next packet */
232 static gboolean capsa_read(wtap *wth, int *err, gchar **err_info,
233     gint64 *data_offset)
234 {
235         capsa_t *capsa = (capsa_t *)wth->priv;
236         guint32 frame_within_block;
237         int     padbytes;
238
239         if (capsa->frame_count == capsa->number_of_frames) {
240                 /*
241                  * No more frames left.  Return an EOF.
242                  */
243                 *err = 0;
244                 return FALSE;
245         }
246         frame_within_block = capsa->frame_count % N_RECORDS_PER_GROUP;
247         if (frame_within_block == 0) {
248                 /*
249                  * Here's a record offset block.
250                  * Get the offset of the block, and then skip the
251                  * first byte.
252                  */
253                 capsa->base_offset = file_tell(wth->fh);
254                 if (!file_skip(wth->fh, 1, err))
255                         return FALSE;
256
257                 /*
258                  * Now read the record offsets.
259                  */
260                 if (!wtap_read_bytes(wth->fh, &capsa->record_offsets,
261                     sizeof capsa->record_offsets, err, err_info))
262                         return FALSE;
263
264                 /*
265                  * And finish processing all 805 bytes by skipping
266                  * the last 4 bytes.
267                  */
268                 if (!file_skip(wth->fh, 4, err))
269                         return FALSE;
270         }
271
272         *data_offset = capsa->base_offset +
273             GUINT32_FROM_LE(capsa->record_offsets[frame_within_block]);
274         if (!file_seek(wth->fh, *data_offset, SEEK_SET, err))
275                 return FALSE;
276
277         padbytes = capsa_read_packet(wth, wth->fh, &wth->phdr,
278             wth->frame_buffer, err, err_info);
279         if (padbytes == -1)
280                 return FALSE;
281
282         /*
283          * Skip over the padding, if any.
284          */
285         if (padbytes != 0) {
286                 if (!file_skip(wth->fh, padbytes, err))
287                         return FALSE;
288         }
289
290         capsa->frame_count++;
291
292         return TRUE;
293 }
294
295 static gboolean
296 capsa_seek_read(wtap *wth, gint64 seek_off,
297     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
298 {
299         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
300                 return FALSE;
301
302         if (capsa_read_packet(wth, wth->random_fh, phdr, buf, err, err_info) == -1) {
303                 if (*err == 0)
304                         *err = WTAP_ERR_SHORT_READ;
305                 return FALSE;
306         }
307         return TRUE;
308 }
309
310 static int
311 capsa_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
312     Buffer *buf, int *err, gchar **err_info)
313 {
314         capsa_t *capsa = (capsa_t *)wth->priv;
315         struct capsarec_hdr capsarec_hdr;
316         struct pbrec_hdr pbrec_hdr;
317         guint32 rec_size;
318         guint32 packet_size;
319         guint32 orig_size;
320         guint32 header_size;
321         guint64 timestamp;
322
323         /* Read record header. */
324         switch (capsa->format_indicator) {
325
326         case 1:
327                 if (!wtap_read_bytes_or_eof(fh, &capsarec_hdr,
328                     sizeof capsarec_hdr, err, err_info))
329                         return -1;
330                 rec_size = GUINT16_FROM_LE(capsarec_hdr.rec_len);
331                 orig_size = GUINT16_FROM_LE(capsarec_hdr.orig_len);
332                 packet_size = GUINT16_FROM_LE(capsarec_hdr.incl_len);
333                 header_size = sizeof capsarec_hdr;
334                 timestamp = (((guint64)GUINT32_FROM_LE(capsarec_hdr.timestamphi))<<32) + GUINT32_FROM_LE(capsarec_hdr.timestamplo);
335
336                 /*
337                  * OK, the rest of this is variable-length.
338                  * We skip: (count1+count2)*4 bytes.
339                  * XXX - what is that?  Measured statistics?
340                  * Calculated statistics?
341                  */
342                 if (!file_skip(fh, (capsarec_hdr.count1 + capsarec_hdr.count2)*4,
343                     err))
344                         return -1;
345                 header_size += (capsarec_hdr.count1 + capsarec_hdr.count2)*4;
346                 break;
347
348         case 2:
349                 if (!wtap_read_bytes_or_eof(fh, &pbrec_hdr,
350                     sizeof pbrec_hdr, err, err_info))
351                         return -1;
352                 rec_size = GUINT16_FROM_LE(pbrec_hdr.rec_len);
353                 orig_size = GUINT16_FROM_LE(pbrec_hdr.orig_len);
354                 packet_size = GUINT16_FROM_LE(pbrec_hdr.incl_len);
355                 header_size = sizeof pbrec_hdr;
356                 timestamp = (((guint64)GUINT32_FROM_LE(pbrec_hdr.timestamphi))<<32) + GUINT32_FROM_LE(pbrec_hdr.timestamplo);
357                 /*
358                  * XXX - from the results of some conversions between
359                  * Capsa format and pcap by Colasoft Packet Builder,
360                  * I do not trust its conversion of time stamps (at
361                  * least one of Colasoft's sample files, when
362                  * converted to pcap format, has, as its time stamps,
363                  * time stamps on the day after the conversion was
364                  * done, which seems like more than just coincidence).
365                  */
366                 break;
367
368         default:
369                 g_assert_not_reached();
370                 *err = WTAP_ERR_INTERNAL;
371                 return -1;
372         }
373         if (orig_size > WTAP_MAX_PACKET_SIZE) {
374                 /*
375                  * Probably a corrupt capture file; don't blow up trying
376                  * to allocate space for an immensely-large packet.
377                  */
378                 *err = WTAP_ERR_BAD_FILE;
379                 *err_info = g_strdup_printf("capsa: File has %u-byte original length, bigger than maximum of %u",
380                     orig_size, WTAP_MAX_PACKET_SIZE);
381                 return -1;
382         }
383         if (packet_size > WTAP_MAX_PACKET_SIZE) {
384                 /*
385                  * Probably a corrupt capture file; don't blow up trying
386                  * to allocate space for an immensely-large packet.
387                  */
388                 *err = WTAP_ERR_BAD_FILE;
389                 *err_info = g_strdup_printf("capsa: File has %u-byte packet, bigger than maximum of %u",
390                     packet_size, WTAP_MAX_PACKET_SIZE);
391                 return -1;
392         }
393         if (header_size + packet_size > rec_size) {
394                 /*
395                  * Probably a corrupt capture file.
396                  */
397                 *err = WTAP_ERR_BAD_FILE;
398                 *err_info = g_strdup_printf("capsa: File has %u-byte packet with %u-byte record header, bigger than record size %u",
399                     packet_size, header_size, rec_size);
400                 return -1;
401         }
402
403         /*
404          * The "on the wire" record size always includes the CRC.
405          * If it's greater than the "captured" size by 4, then
406          * we subtract 4 from it, to reflect the way the "on the wire"
407          * record size works for other file formats.
408          */
409         if (orig_size == packet_size + 4)
410                 orig_size = packet_size;
411
412         /*
413          * We assume there's no FCS in this frame.
414          * XXX - is there ever one?
415          */
416         phdr->pseudo_header.eth.fcs_len = 0;
417
418         phdr->rec_type = REC_TYPE_PACKET;
419         phdr->caplen = packet_size;
420         phdr->len = orig_size;
421         phdr->presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_TS;
422         phdr->ts.secs = (time_t)(timestamp / 1000000);
423         phdr->ts.nsecs = ((int)(timestamp % 1000000))*1000;
424
425         /*
426          * Read the packet data.
427          */
428         if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
429                 return -1;      /* failed */
430
431         return rec_size - (header_size + packet_size);
432 }
433
434 /*
435  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
436  *
437  * Local variables:
438  * c-basic-offset: 8
439  * tab-width: 8
440  * indent-tabs-mode: t
441  * End:
442  *
443  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
444  * :indentSize=8:tabSize=8:noTabs=false:
445  */