Change wtap_read() API so that the data offset is set via a pointer, and
[metze/wireshark/wip.git] / wiretap / radcom.c
1 /* radcom.c
2  *
3  * $Id: radcom.c,v 1.26 2000/09/07 05:34:18 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdlib.h>
28 #include <errno.h>
29 #include "wtap-int.h"
30 #include "file_wrappers.h"
31 #include "buffer.h"
32 #include "radcom.h"
33
34 struct frame_date {
35         guint16 year;
36         guint8  month;
37         guint8  day;
38         guint32 sec;            /* seconds since midnight */
39         guint32 usec;
40 };
41
42 struct unaligned_frame_date {
43         char    year[2];
44         char    month;
45         char    day;
46         char    sec[4];         /* seconds since midnight */
47         char    usec[4];
48 };
49
50 static guint8 radcom_magic[8] = {
51         0x42, 0xD2, 0x00, 0x34, 0x12, 0x66, 0x22, 0x88
52 };
53
54 /* RADCOM record header - followed by frame data (perhaps including FCS).
55    The first two bytes of "xxz" appear to equal "length", as do the
56    second two bytes; if a RADCOM box can be told not to save all of
57    the captured packet, might one or the other of those be the
58    captured length of the packet? */
59 struct radcomrec_hdr {
60         char    xxx[4];         /* unknown */
61         char    length[2];      /* packet length */
62         char    xxy[5];         /* unknown */
63         struct unaligned_frame_date date; /* date/time stamp of packet */
64         char    xxz[6];         /* unknown */
65         char    dce;            /* DCE/DTE flag (and other flags?) */
66         char    xxw[9];         /* unknown */
67 };
68
69 static gboolean radcom_read(wtap *wth, int *err, int *data_offset);
70 static int radcom_seek_read(wtap *wth, int seek_off,
71         union wtap_pseudo_header *pseudo_header, u_char *pd, int length);
72 static int radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr,
73         int *err);
74 static int radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err);
75
76 int radcom_open(wtap *wth, int *err)
77 {
78         int bytes_read;
79         char magic[8];
80         struct frame_date start_date;
81         guint32 sec;
82         struct tm tm;
83         char byte;
84         char encap_magic[7] = {0x54, 0x43, 0x50, 0x00, 0x42, 0x43, 0x09};
85         char search_encap[7];
86
87         /* Read in the string that should be at the start of a RADCOM file */
88         file_seek(wth->fh, 0, SEEK_SET);
89         errno = WTAP_ERR_CANT_READ;
90         bytes_read = file_read(magic, 1, 8, wth->fh);
91         if (bytes_read != 8) {
92                 *err = file_error(wth->fh);
93                 if (*err != 0)
94                         return -1;
95                 return 0;
96         }
97
98         if (memcmp(magic, radcom_magic, 8) != 0) {
99                 return 0;
100         }
101
102         file_seek(wth->fh, 0x8B, SEEK_SET);
103         wth->data_offset = 0x8B;
104         errno = WTAP_ERR_CANT_READ;
105         bytes_read = file_read(&byte, 1, 1, wth->fh);
106         if (bytes_read != 1) {
107                 *err = file_error(wth->fh);
108                 if (*err != 0)
109                         return -1;
110                 return 0;
111         }
112         wth->data_offset += 1;
113         while (byte) {
114                 errno = WTAP_ERR_CANT_READ;
115                 bytes_read = file_read(&byte, 1, 1, wth->fh);
116                 if (bytes_read != 1) {
117                         *err = file_error(wth->fh);
118                         if (*err != 0)
119                                 return -1;
120                         return 0;
121                 }
122                 wth->data_offset += 1;
123         }
124         file_seek(wth->fh, 1, SEEK_CUR);
125         wth->data_offset += 1;
126
127         /* Get capture start time */
128         errno = WTAP_ERR_CANT_READ;
129         bytes_read = file_read(&start_date, 1, sizeof(struct frame_date), wth->fh);
130         if (bytes_read != sizeof(struct frame_date)) {
131                 *err = file_error(wth->fh);
132                 if (*err != 0)
133                         return -1;
134                 return 0;
135         }
136         wth->data_offset += sizeof(struct frame_date);
137
138         /* This is a radcom file */
139         wth->file_type = WTAP_FILE_RADCOM;
140         wth->subtype_read = radcom_read;
141         wth->subtype_seek_read = radcom_seek_read;
142         wth->snapshot_length = 16384;   /* not available in header, only in frame */
143
144         tm.tm_year = pletohs(&start_date.year)-1900;
145         tm.tm_mon = start_date.month-1;
146         tm.tm_mday = start_date.day;
147         sec = pletohl(&start_date.sec);
148         tm.tm_hour = sec/3600;
149         tm.tm_min = (sec%3600)/60;
150         tm.tm_sec = sec%60;
151         tm.tm_isdst = -1;
152
153         file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR);
154         wth->data_offset += sizeof(struct frame_date);
155
156         errno = WTAP_ERR_CANT_READ;
157         bytes_read = file_read(search_encap, 1, 7, wth->fh);
158         if (bytes_read != 7) {
159                 goto read_error;
160         }
161         wth->data_offset += 7;
162         while (memcmp(encap_magic, search_encap, 7)) {
163                 file_seek(wth->fh, -6, SEEK_CUR);
164                 wth->data_offset -= 6;
165                 errno = WTAP_ERR_CANT_READ;
166                 bytes_read = file_read(search_encap, 1, 7, wth->fh);
167                 if (bytes_read != 7) {
168                         goto read_error;
169                 }
170                 wth->data_offset += 7;
171         }
172         file_seek(wth->fh, 12, SEEK_CUR);
173         wth->data_offset += 12;
174         errno = WTAP_ERR_CANT_READ;
175         bytes_read = file_read(search_encap, 1, 4, wth->fh);
176         if (bytes_read != 4) {
177                 goto read_error;
178         }
179         wth->data_offset += 4;
180         if (!memcmp(search_encap, "LAPB", 4))
181                 wth->file_encap = WTAP_ENCAP_LAPB;
182         else if (!memcmp(search_encap, "Ethe", 4))
183                 wth->file_encap = WTAP_ENCAP_ETHERNET;
184         else {
185                 g_message("pcap: network type \"%.4s\" unknown", search_encap);
186                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
187                 return -1;
188         }
189
190         /*bytes_read = file_read(&next_date, 1, sizeof(struct frame_date), wth->fh);
191         errno = WTAP_ERR_CANT_READ;
192         if (bytes_read != sizeof(struct frame_date)) {
193                 goto read_error;
194         }
195
196         while (memcmp(&start_date, &next_date, 4)) {
197                 file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR);
198                 errno = WTAP_ERR_CANT_READ;
199                 bytes_read = file_read(&next_date, 1, sizeof(struct frame_date),
200                                    wth->fh);
201                 if (bytes_read != sizeof(struct frame_date)) {
202                         goto read_error;
203                 }
204         }*/
205
206         if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
207                 file_seek(wth->fh, 294, SEEK_CUR);
208                 wth->data_offset += 294;
209         } else if (wth->file_encap == WTAP_ENCAP_LAPB) {
210                 file_seek(wth->fh, 297, SEEK_CUR);
211                 wth->data_offset += 297;
212         }
213
214         return 1;
215
216 read_error:
217         *err = file_error(wth->fh);
218         if (*err != 0)
219                 return -1;
220         return 0;
221 }
222
223 /* Read the next packet */
224 static gboolean radcom_read(wtap *wth, int *err, int *data_offset)
225 {
226         int     ret;
227         struct radcomrec_hdr hdr;
228         guint16 length;
229         guint32 sec;
230         int     bytes_read;
231         struct tm tm;
232         char    fcs[2];
233
234         /* Read record header. */
235         *data_offset = wth->data_offset;
236         ret = radcom_read_rec_header(wth->fh, &hdr, err);
237         if (ret <= 0) {
238                 /* Read error or EOF */
239                 return FALSE;
240         }
241         wth->data_offset += sizeof hdr;
242         length = pletohs(&hdr.length);
243         if (length == 0) return FALSE;
244
245         if (wth->file_encap == WTAP_ENCAP_LAPB)
246                 length -= 2; /* FCS */
247
248         wth->phdr.len = length;
249         wth->phdr.caplen = length;
250
251         tm.tm_year = pletohs(&hdr.date.year)-1900;
252         tm.tm_mon = hdr.date.month-1;
253         tm.tm_mday = hdr.date.day;
254         sec = pletohl(&hdr.date.sec);
255         tm.tm_hour = sec/3600;
256         tm.tm_min = (sec%3600)/60;
257         tm.tm_sec = sec%60;
258         tm.tm_isdst = -1;
259         wth->phdr.ts.tv_sec = mktime(&tm);
260         wth->phdr.ts.tv_usec = pletohl(&hdr.date.usec);
261         wth->pseudo_header.x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
262
263         /*
264          * Read the packet data.
265          */
266         buffer_assure_space(wth->frame_buffer, length);
267         if (radcom_read_rec_data(wth->fh,
268             buffer_start_ptr(wth->frame_buffer), length, err) < 0)
269                 return FALSE;   /* Read error */
270         wth->data_offset += length;
271
272         wth->phdr.pkt_encap = wth->file_encap;
273
274         if (wth->file_encap == WTAP_ENCAP_LAPB) {
275                 /* Read the FCS.
276                    XXX - should we put it in the pseudo-header? */
277                 errno = WTAP_ERR_CANT_READ;
278                 bytes_read = file_read(&fcs, 1, sizeof fcs, wth->fh);
279                 if (bytes_read != sizeof fcs) {
280                         *err = file_error(wth->fh);
281                         if (*err == 0)
282                                 *err = WTAP_ERR_SHORT_READ;
283                         return FALSE;
284                 }
285                 wth->data_offset += sizeof fcs;
286         }
287
288         return TRUE;
289 }
290
291 static int
292 radcom_seek_read(wtap *wth, int seek_off,
293     union wtap_pseudo_header *pseudo_header, u_char *pd, int length)
294 {
295         int     ret;
296         int     err;            /* XXX - return this */
297         struct radcomrec_hdr hdr;
298
299         file_seek(wth->random_fh, seek_off, SEEK_SET);
300
301         /* Read record header. */
302         ret = radcom_read_rec_header(wth->random_fh, &hdr, &err);
303         if (ret <= 0) {
304                 /* Read error or EOF */
305                 return ret;
306         }
307
308         pseudo_header->x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
309
310         /*
311          * Read the packet data.
312          */
313         return radcom_read_rec_data(wth->random_fh, pd, length, &err);
314 }
315
316 static int
317 radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr, int *err)
318 {
319         int     bytes_read;
320
321         errno = WTAP_ERR_CANT_READ;
322         bytes_read = file_read(hdr, 1, sizeof *hdr, fh);
323         if (bytes_read != sizeof *hdr) {
324                 *err = file_error(fh);
325                 if (*err != 0)
326                         return -1;
327                 if (bytes_read != 0) {
328                         *err = WTAP_ERR_SHORT_READ;
329                         return -1;
330                 }
331                 return 0;
332         }
333         return 1;
334 }
335
336 static int
337 radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
338 {
339         int     bytes_read;
340
341         errno = WTAP_ERR_CANT_READ;
342         bytes_read = file_read(pd, 1, length, fh);
343
344         if (bytes_read != length) {
345                 *err = file_error(fh);
346                 if (*err == 0)
347                         *err = WTAP_ERR_SHORT_READ;
348                 return -1;
349         }
350         return 0;
351 }