3bf9adf2f4e688e18565bc6573fd8f721998ddf9
[obnox/wireshark/wip.git] / wiretap / radcom.c
1 /* radcom.c
2  *
3  * $Id: radcom.c,v 1.33 2002/03/04 00:25:35 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 static int radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr,
74         int *err);
75 static int radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err);
76
77 int radcom_open(wtap *wth, int *err)
78 {
79         int bytes_read;
80         char magic[8];
81         struct frame_date start_date;
82         guint32 sec;
83         struct tm tm;
84         char byte;
85         char encap_magic[4] = {0x00, 0x42, 0x43, 0x09};
86         char search_encap[7];
87
88         /* Read in the string that should be at the start of a RADCOM file */
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         if (file_seek(wth->fh, 0x8B, SEEK_SET) == -1) {
103                 *err = file_error(wth->fh);
104                 return -1;
105         }
106         wth->data_offset = 0x8B;
107         errno = WTAP_ERR_CANT_READ;
108         bytes_read = file_read(&byte, 1, 1, wth->fh);
109         if (bytes_read != 1) {
110                 *err = file_error(wth->fh);
111                 if (*err != 0)
112                         return -1;
113                 return 0;
114         }
115         wth->data_offset += 1;
116         while (byte) {
117                 errno = WTAP_ERR_CANT_READ;
118                 bytes_read = file_read(&byte, 1, 1, wth->fh);
119                 if (bytes_read != 1) {
120                         *err = file_error(wth->fh);
121                         if (*err != 0)
122                                 return -1;
123                         return 0;
124                 }
125                 wth->data_offset += 1;
126         }
127
128         /* Get capture start time */
129         errno = WTAP_ERR_CANT_READ;
130         bytes_read = file_read(&start_date, 1, sizeof(struct frame_date), wth->fh);
131         if (bytes_read != sizeof(struct frame_date)) {
132                 *err = file_error(wth->fh);
133                 if (*err != 0)
134                         return -1;
135                 return 0;
136         }
137         wth->data_offset += sizeof(struct frame_date);
138
139         /* This is a radcom file */
140         wth->file_type = WTAP_FILE_RADCOM;
141         wth->subtype_read = radcom_read;
142         wth->subtype_seek_read = radcom_seek_read;
143         wth->snapshot_length = 0;       /* not available in header, only in frame */
144
145         tm.tm_year = pletohs(&start_date.year)-1900;
146         tm.tm_mon = start_date.month-1;
147         tm.tm_mday = start_date.day;
148         sec = pletohl(&start_date.sec);
149         tm.tm_hour = sec/3600;
150         tm.tm_min = (sec%3600)/60;
151         tm.tm_sec = sec%60;
152         tm.tm_isdst = -1;
153
154         if (file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR) == -1) {
155                 *err = file_error(wth->fh);
156                 return -1;
157         }
158         wth->data_offset += sizeof(struct frame_date);
159
160         errno = WTAP_ERR_CANT_READ;
161         bytes_read = file_read(search_encap, 1, 4, wth->fh);
162         if (bytes_read != 4) {
163                 goto read_error;
164         }
165         wth->data_offset += 4;
166         while (memcmp(encap_magic, search_encap, 4)) {
167                 if (file_seek(wth->fh, -3, SEEK_CUR) == -1) {
168                         *err = file_error(wth->fh);
169                         return -1;
170                 }
171                 wth->data_offset -= 3;
172                 errno = WTAP_ERR_CANT_READ;
173                 bytes_read = file_read(search_encap, 1, 4, wth->fh);
174                 if (bytes_read != 4) {
175                         goto read_error;
176                 }
177                 wth->data_offset += 4;
178         }
179         if (file_seek(wth->fh, 12, SEEK_CUR) == -1) {
180                 *err = file_error(wth->fh);
181                 return -1;
182         }
183         wth->data_offset += 12;
184         errno = WTAP_ERR_CANT_READ;
185         bytes_read = file_read(search_encap, 1, 4, wth->fh);
186         if (bytes_read != 4) {
187                 goto read_error;
188         }
189         wth->data_offset += 4;
190         if (!memcmp(search_encap, "LAPB", 4))
191                 wth->file_encap = WTAP_ENCAP_LAPB;
192         else if (!memcmp(search_encap, "Ethe", 4))
193                 wth->file_encap = WTAP_ENCAP_ETHERNET;
194         else {
195                 g_message("pcap: network type \"%.4s\" unknown", search_encap);
196                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
197                 return -1;
198         }
199
200         /*bytes_read = file_read(&next_date, 1, sizeof(struct frame_date), wth->fh);
201         errno = WTAP_ERR_CANT_READ;
202         if (bytes_read != sizeof(struct frame_date)) {
203                 goto read_error;
204         }
205
206         while (memcmp(&start_date, &next_date, 4)) {
207                 if (file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR) == -1) {
208                         *err = file_error(wth->fh);
209                         return -1;
210                 }
211                 errno = WTAP_ERR_CANT_READ;
212                 bytes_read = file_read(&next_date, 1, sizeof(struct frame_date),
213                                    wth->fh);
214                 if (bytes_read != sizeof(struct frame_date)) {
215                         goto read_error;
216                 }
217         }*/
218
219         if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
220                 if (file_seek(wth->fh, 294, SEEK_CUR) == -1) {
221                         *err = file_error(wth->fh);
222                         return -1;
223                 }
224                 wth->data_offset += 294;
225         } else if (wth->file_encap == WTAP_ENCAP_LAPB) {
226                 if (file_seek(wth->fh, 297, SEEK_CUR) == -1) {
227                         *err = file_error(wth->fh);
228                         return -1;
229                 }
230                 wth->data_offset += 297;
231         }
232
233         return 1;
234
235 read_error:
236         *err = file_error(wth->fh);
237         if (*err != 0)
238                 return -1;
239         return 0;
240 }
241
242 /* Read the next packet */
243 static gboolean radcom_read(wtap *wth, int *err, long *data_offset)
244 {
245         int     ret;
246         struct radcomrec_hdr hdr;
247         guint16 length;
248         guint32 sec;
249         int     bytes_read;
250         struct tm tm;
251         char    fcs[2];
252
253         /* Read record header. */
254         *data_offset = wth->data_offset;
255         ret = radcom_read_rec_header(wth->fh, &hdr, err);
256         if (ret <= 0) {
257                 /* Read error or EOF */
258                 return FALSE;
259         }
260         wth->data_offset += sizeof hdr;
261         length = pletohs(&hdr.length);
262         if (length == 0) return FALSE;
263
264         if (wth->file_encap == WTAP_ENCAP_LAPB)
265                 length -= 2; /* FCS */
266
267         wth->phdr.len = length;
268         wth->phdr.caplen = length;
269
270         tm.tm_year = pletohs(&hdr.date.year)-1900;
271         tm.tm_mon = hdr.date.month-1;
272         tm.tm_mday = hdr.date.day;
273         sec = pletohl(&hdr.date.sec);
274         tm.tm_hour = sec/3600;
275         tm.tm_min = (sec%3600)/60;
276         tm.tm_sec = sec%60;
277         tm.tm_isdst = -1;
278         wth->phdr.ts.tv_sec = mktime(&tm);
279         wth->phdr.ts.tv_usec = pletohl(&hdr.date.usec);
280         wth->pseudo_header.x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
281
282         /*
283          * Read the packet data.
284          */
285         buffer_assure_space(wth->frame_buffer, length);
286         if (radcom_read_rec_data(wth->fh,
287             buffer_start_ptr(wth->frame_buffer), length, err) < 0)
288                 return FALSE;   /* Read error */
289         wth->data_offset += length;
290
291         wth->phdr.pkt_encap = wth->file_encap;
292
293         if (wth->file_encap == WTAP_ENCAP_LAPB) {
294                 /* Read the FCS.
295                    XXX - should we put it in the pseudo-header? */
296                 errno = WTAP_ERR_CANT_READ;
297                 bytes_read = file_read(&fcs, 1, sizeof fcs, wth->fh);
298                 if (bytes_read != sizeof fcs) {
299                         *err = file_error(wth->fh);
300                         if (*err == 0)
301                                 *err = WTAP_ERR_SHORT_READ;
302                         return FALSE;
303                 }
304                 wth->data_offset += sizeof fcs;
305         }
306
307         return TRUE;
308 }
309
310 static int
311 radcom_seek_read(wtap *wth, long seek_off,
312     union wtap_pseudo_header *pseudo_header, u_char *pd, int length)
313 {
314         int     ret;
315         int     err;            /* XXX - return this */
316         struct radcomrec_hdr hdr;
317
318         file_seek(wth->random_fh, seek_off, SEEK_SET);
319
320         /* Read record header. */
321         ret = radcom_read_rec_header(wth->random_fh, &hdr, &err);
322         if (ret <= 0) {
323                 /* Read error or EOF */
324                 return ret;
325         }
326
327         pseudo_header->x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
328
329         /*
330          * Read the packet data.
331          */
332         return radcom_read_rec_data(wth->random_fh, pd, length, &err);
333 }
334
335 static int
336 radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr, int *err)
337 {
338         int     bytes_read;
339
340         errno = WTAP_ERR_CANT_READ;
341         bytes_read = file_read(hdr, 1, sizeof *hdr, fh);
342         if (bytes_read != sizeof *hdr) {
343                 *err = file_error(fh);
344                 if (*err != 0)
345                         return -1;
346                 if (bytes_read != 0) {
347                         *err = WTAP_ERR_SHORT_READ;
348                         return -1;
349                 }
350                 return 0;
351         }
352         return 1;
353 }
354
355 static int
356 radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
357 {
358         int     bytes_read;
359
360         errno = WTAP_ERR_CANT_READ;
361         bytes_read = file_read(pd, 1, length, fh);
362
363         if (bytes_read != length) {
364                 *err = file_error(fh);
365                 if (*err == 0)
366                         *err = WTAP_ERR_SHORT_READ;
367                 return -1;
368         }
369         return 0;
370 }