Have "wtap_seek_read()" return 0 on success and -1 on failure, and take
[obnox/wireshark/wip.git] / wiretap / radcom.c
1 /* radcom.c
2  *
3  * $Id: radcom.c,v 1.34 2002/03/05 05:58:40 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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 <string.h>
30 #include "wtap-int.h"
31 #include "file_wrappers.h"
32 #include "buffer.h"
33 #include "radcom.h"
34
35 struct frame_date {
36         guint16 year;
37         guint8  month;
38         guint8  day;
39         guint32 sec;            /* seconds since midnight */
40         guint32 usec;
41 };
42
43 struct unaligned_frame_date {
44         char    year[2];
45         char    month;
46         char    day;
47         char    sec[4];         /* seconds since midnight */
48         char    usec[4];
49 };
50
51 static guint8 radcom_magic[8] = {
52         0x42, 0xD2, 0x00, 0x34, 0x12, 0x66, 0x22, 0x88
53 };
54
55 /* RADCOM record header - followed by frame data (perhaps including FCS).
56    The first two bytes of "xxz" appear to equal "length", as do the
57    second two bytes; if a RADCOM box can be told not to save all of
58    the captured packet, might one or the other of those be the
59    captured length of the packet? */
60 struct radcomrec_hdr {
61         char    xxx[4];         /* unknown */
62         char    length[2];      /* packet length */
63         char    xxy[5];         /* unknown */
64         struct unaligned_frame_date date; /* date/time stamp of packet */
65         char    xxz[6];         /* unknown */
66         char    dce;            /* DCE/DTE flag (and other flags?) */
67         char    xxw[9];         /* unknown */
68 };
69
70 static gboolean radcom_read(wtap *wth, int *err, long *data_offset);
71 static int radcom_seek_read(wtap *wth, long seek_off,
72         union wtap_pseudo_header *pseudo_header, u_char *pd, int length,
73         int *err);
74 static int radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr,
75         int *err);
76 static int radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err);
77
78 int radcom_open(wtap *wth, int *err)
79 {
80         int bytes_read;
81         char magic[8];
82         struct frame_date start_date;
83         guint32 sec;
84         struct tm tm;
85         char byte;
86         char encap_magic[4] = {0x00, 0x42, 0x43, 0x09};
87         char search_encap[7];
88
89         /* Read in the string that should be at the start of a RADCOM file */
90         errno = WTAP_ERR_CANT_READ;
91         bytes_read = file_read(magic, 1, 8, wth->fh);
92         if (bytes_read != 8) {
93                 *err = file_error(wth->fh);
94                 if (*err != 0)
95                         return -1;
96                 return 0;
97         }
98
99         if (memcmp(magic, radcom_magic, 8) != 0) {
100                 return 0;
101         }
102
103         if (file_seek(wth->fh, 0x8B, SEEK_SET) == -1) {
104                 *err = file_error(wth->fh);
105                 return -1;
106         }
107         wth->data_offset = 0x8B;
108         errno = WTAP_ERR_CANT_READ;
109         bytes_read = file_read(&byte, 1, 1, wth->fh);
110         if (bytes_read != 1) {
111                 *err = file_error(wth->fh);
112                 if (*err != 0)
113                         return -1;
114                 return 0;
115         }
116         wth->data_offset += 1;
117         while (byte) {
118                 errno = WTAP_ERR_CANT_READ;
119                 bytes_read = file_read(&byte, 1, 1, wth->fh);
120                 if (bytes_read != 1) {
121                         *err = file_error(wth->fh);
122                         if (*err != 0)
123                                 return -1;
124                         return 0;
125                 }
126                 wth->data_offset += 1;
127         }
128
129         /* Get capture start time */
130         errno = WTAP_ERR_CANT_READ;
131         bytes_read = file_read(&start_date, 1, sizeof(struct frame_date), wth->fh);
132         if (bytes_read != sizeof(struct frame_date)) {
133                 *err = file_error(wth->fh);
134                 if (*err != 0)
135                         return -1;
136                 return 0;
137         }
138         wth->data_offset += sizeof(struct frame_date);
139
140         /* This is a radcom file */
141         wth->file_type = WTAP_FILE_RADCOM;
142         wth->subtype_read = radcom_read;
143         wth->subtype_seek_read = radcom_seek_read;
144         wth->snapshot_length = 0;       /* not available in header, only in frame */
145
146         tm.tm_year = pletohs(&start_date.year)-1900;
147         tm.tm_mon = start_date.month-1;
148         tm.tm_mday = start_date.day;
149         sec = pletohl(&start_date.sec);
150         tm.tm_hour = sec/3600;
151         tm.tm_min = (sec%3600)/60;
152         tm.tm_sec = sec%60;
153         tm.tm_isdst = -1;
154
155         if (file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR) == -1) {
156                 *err = file_error(wth->fh);
157                 return -1;
158         }
159         wth->data_offset += sizeof(struct frame_date);
160
161         errno = WTAP_ERR_CANT_READ;
162         bytes_read = file_read(search_encap, 1, 4, wth->fh);
163         if (bytes_read != 4) {
164                 goto read_error;
165         }
166         wth->data_offset += 4;
167         while (memcmp(encap_magic, search_encap, 4)) {
168                 if (file_seek(wth->fh, -3, SEEK_CUR) == -1) {
169                         *err = file_error(wth->fh);
170                         return -1;
171                 }
172                 wth->data_offset -= 3;
173                 errno = WTAP_ERR_CANT_READ;
174                 bytes_read = file_read(search_encap, 1, 4, wth->fh);
175                 if (bytes_read != 4) {
176                         goto read_error;
177                 }
178                 wth->data_offset += 4;
179         }
180         if (file_seek(wth->fh, 12, SEEK_CUR) == -1) {
181                 *err = file_error(wth->fh);
182                 return -1;
183         }
184         wth->data_offset += 12;
185         errno = WTAP_ERR_CANT_READ;
186         bytes_read = file_read(search_encap, 1, 4, wth->fh);
187         if (bytes_read != 4) {
188                 goto read_error;
189         }
190         wth->data_offset += 4;
191         if (!memcmp(search_encap, "LAPB", 4))
192                 wth->file_encap = WTAP_ENCAP_LAPB;
193         else if (!memcmp(search_encap, "Ethe", 4))
194                 wth->file_encap = WTAP_ENCAP_ETHERNET;
195         else {
196                 g_message("pcap: network type \"%.4s\" unknown", search_encap);
197                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
198                 return -1;
199         }
200
201         /*bytes_read = file_read(&next_date, 1, sizeof(struct frame_date), wth->fh);
202         errno = WTAP_ERR_CANT_READ;
203         if (bytes_read != sizeof(struct frame_date)) {
204                 goto read_error;
205         }
206
207         while (memcmp(&start_date, &next_date, 4)) {
208                 if (file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR) == -1) {
209                         *err = file_error(wth->fh);
210                         return -1;
211                 }
212                 errno = WTAP_ERR_CANT_READ;
213                 bytes_read = file_read(&next_date, 1, sizeof(struct frame_date),
214                                    wth->fh);
215                 if (bytes_read != sizeof(struct frame_date)) {
216                         goto read_error;
217                 }
218         }*/
219
220         if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
221                 if (file_seek(wth->fh, 294, SEEK_CUR) == -1) {
222                         *err = file_error(wth->fh);
223                         return -1;
224                 }
225                 wth->data_offset += 294;
226         } else if (wth->file_encap == WTAP_ENCAP_LAPB) {
227                 if (file_seek(wth->fh, 297, SEEK_CUR) == -1) {
228                         *err = file_error(wth->fh);
229                         return -1;
230                 }
231                 wth->data_offset += 297;
232         }
233
234         return 1;
235
236 read_error:
237         *err = file_error(wth->fh);
238         if (*err != 0)
239                 return -1;
240         return 0;
241 }
242
243 /* Read the next packet */
244 static gboolean radcom_read(wtap *wth, int *err, long *data_offset)
245 {
246         int     ret;
247         struct radcomrec_hdr hdr;
248         guint16 length;
249         guint32 sec;
250         int     bytes_read;
251         struct tm tm;
252         char    fcs[2];
253
254         /* Read record header. */
255         *data_offset = wth->data_offset;
256         ret = radcom_read_rec_header(wth->fh, &hdr, err);
257         if (ret <= 0) {
258                 /* Read error or EOF */
259                 return FALSE;
260         }
261         wth->data_offset += sizeof hdr;
262         length = pletohs(&hdr.length);
263         if (length == 0) return FALSE;
264
265         if (wth->file_encap == WTAP_ENCAP_LAPB)
266                 length -= 2; /* FCS */
267
268         wth->phdr.len = length;
269         wth->phdr.caplen = length;
270
271         tm.tm_year = pletohs(&hdr.date.year)-1900;
272         tm.tm_mon = hdr.date.month-1;
273         tm.tm_mday = hdr.date.day;
274         sec = pletohl(&hdr.date.sec);
275         tm.tm_hour = sec/3600;
276         tm.tm_min = (sec%3600)/60;
277         tm.tm_sec = sec%60;
278         tm.tm_isdst = -1;
279         wth->phdr.ts.tv_sec = mktime(&tm);
280         wth->phdr.ts.tv_usec = pletohl(&hdr.date.usec);
281         wth->pseudo_header.x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
282
283         /*
284          * Read the packet data.
285          */
286         buffer_assure_space(wth->frame_buffer, length);
287         if (radcom_read_rec_data(wth->fh,
288             buffer_start_ptr(wth->frame_buffer), length, err) < 0)
289                 return FALSE;   /* Read error */
290         wth->data_offset += length;
291
292         wth->phdr.pkt_encap = wth->file_encap;
293
294         if (wth->file_encap == WTAP_ENCAP_LAPB) {
295                 /* Read the FCS.
296                    XXX - should we put it in the pseudo-header? */
297                 errno = WTAP_ERR_CANT_READ;
298                 bytes_read = file_read(&fcs, 1, sizeof fcs, wth->fh);
299                 if (bytes_read != sizeof fcs) {
300                         *err = file_error(wth->fh);
301                         if (*err == 0)
302                                 *err = WTAP_ERR_SHORT_READ;
303                         return FALSE;
304                 }
305                 wth->data_offset += sizeof fcs;
306         }
307
308         return TRUE;
309 }
310
311 static int
312 radcom_seek_read(wtap *wth, long seek_off,
313     union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err)
314 {
315         int     ret;
316         struct radcomrec_hdr hdr;
317
318         if (file_seek(wth->random_fh, seek_off, SEEK_SET) == -1) {
319                 *err = file_error(wth->random_fh);
320                 return -1;
321         }
322
323         /* Read record header. */
324         ret = radcom_read_rec_header(wth->random_fh, &hdr, err);
325         if (ret <= 0) {
326                 /* Read error or EOF */
327                 if (ret == 0) {
328                         /* EOF means "short read" in random-access mode */
329                         *err = WTAP_ERR_SHORT_READ;
330                 }
331                 return -1;
332         }
333
334         pseudo_header->x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
335
336         /*
337          * Read the packet data.
338          */
339         return radcom_read_rec_data(wth->random_fh, pd, length, err);
340 }
341
342 static int
343 radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr, int *err)
344 {
345         int     bytes_read;
346
347         errno = WTAP_ERR_CANT_READ;
348         bytes_read = file_read(hdr, 1, sizeof *hdr, fh);
349         if (bytes_read != sizeof *hdr) {
350                 *err = file_error(fh);
351                 if (*err != 0)
352                         return -1;
353                 if (bytes_read != 0) {
354                         *err = WTAP_ERR_SHORT_READ;
355                         return -1;
356                 }
357                 return 0;
358         }
359         return 1;
360 }
361
362 static int
363 radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
364 {
365         int     bytes_read;
366
367         errno = WTAP_ERR_CANT_READ;
368         bytes_read = file_read(pd, 1, length, fh);
369
370         if (bytes_read != length) {
371                 *err = file_error(fh);
372                 if (*err == 0)
373                         *err = WTAP_ERR_SHORT_READ;
374                 return -1;
375         }
376         return 0;
377 }