207d29afecda6a038fe8104b8ce8108a006a9d52
[obnox/wireshark/wip.git] / wiretap / radcom.c
1 /* radcom.c
2  *
3  * $Id: radcom.c,v 1.14 1999/11/10 19:47:57 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.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 <time.h>
30 #include "wtap.h"
31 #include "file.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 char 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 int radcom_read(wtap *wth, int *err);
71
72 int radcom_open(wtap *wth, int *err)
73 {
74         int bytes_read;
75         char magic[8];
76         struct frame_date start_date;
77         guint32 sec;
78         struct tm tm;
79         char byte;
80         char encap_magic[7] = {0x54, 0x43, 0x50, 0x00, 0x42, 0x43, 0x09};
81         char search_encap[7];
82
83         /* Read in the string that should be at the start of a RADCOM file */
84         file_seek(wth->fh, 0, SEEK_SET);
85         errno = WTAP_ERR_CANT_READ;
86         bytes_read = file_read(magic, 1, 8, wth->fh);
87         if (bytes_read != 8) {
88                 *err = file_error(wth->fh);
89                 if (*err != 0)
90                         return -1;
91                 return 0;
92         }
93
94         if (memcmp(magic, radcom_magic, 8)) {
95                 return 0;
96         }
97
98         file_seek(wth->fh, 0x8B, SEEK_SET);
99         wth->data_offset = 0x8B;
100         errno = WTAP_ERR_CANT_READ;
101         bytes_read = file_read(&byte, 1, 1, wth->fh);
102         if (bytes_read != 1) {
103                 *err = file_error(wth->fh);
104                 if (*err != 0)
105                         return -1;
106                 return 0;
107         }
108         wth->data_offset += 1;
109         while (byte) {
110                 errno = WTAP_ERR_CANT_READ;
111                 bytes_read = file_read(&byte, 1, 1, wth->fh);
112                 if (bytes_read != 1) {
113                         *err = file_error(wth->fh);
114                         if (*err != 0)
115                                 return -1;
116                         return 0;
117                 }
118                 wth->data_offset += 1;
119         }
120         file_seek(wth->fh, 1, SEEK_CUR);
121         wth->data_offset += 1;
122
123         /* Get capture start time */
124         errno = WTAP_ERR_CANT_READ;
125         bytes_read = file_read(&start_date, 1, sizeof(struct frame_date), wth->fh);
126         if (bytes_read != sizeof(struct frame_date)) {
127                 *err = file_error(wth->fh);
128                 if (*err != 0)
129                         return -1;
130                 return 0;
131         }
132         wth->data_offset += sizeof(struct frame_date);
133
134         /* This is a radcom file */
135         wth->file_type = WTAP_FILE_RADCOM;
136         wth->capture.radcom = g_malloc(sizeof(radcom_t));
137         wth->subtype_read = radcom_read;
138         wth->snapshot_length = 16384;   /* not available in header, only in frame */
139
140         tm.tm_year = pletohs(&start_date.year)-1900;
141         tm.tm_mon = start_date.month-1;
142         tm.tm_mday = start_date.day;
143         sec = pletohl(&start_date.sec);
144         tm.tm_hour = sec/3600;
145         tm.tm_min = (sec%3600)/60;
146         tm.tm_sec = sec%60;
147         tm.tm_isdst = -1;
148         wth->capture.radcom->start = mktime(&tm);
149
150         file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR);
151         wth->data_offset += sizeof(struct frame_date);
152
153         errno = WTAP_ERR_CANT_READ;
154         bytes_read = file_read(search_encap, 1, 7, wth->fh);
155         if (bytes_read != 7) {
156                 goto read_error;
157         }
158         wth->data_offset += 7;
159         while (memcmp(encap_magic, search_encap, 7)) {
160                 file_seek(wth->fh, -6, SEEK_CUR);
161                 wth->data_offset -= 6;
162                 errno = WTAP_ERR_CANT_READ;
163                 bytes_read = file_read(search_encap, 1, 7, wth->fh);
164                 if (bytes_read != 7) {
165                         goto read_error;
166                 }
167                 wth->data_offset += 7;
168         }
169         file_seek(wth->fh, 12, SEEK_CUR);
170         wth->data_offset += 12;
171         errno = WTAP_ERR_CANT_READ;
172         bytes_read = file_read(search_encap, 1, 4, wth->fh);
173         if (bytes_read != 4) {
174                 goto read_error;
175         }
176         wth->data_offset += 4;
177         if (!memcmp(search_encap, "LAPB", 4))
178                 wth->file_encap = WTAP_ENCAP_LAPB;
179         else if (!memcmp(search_encap, "Ethe", 4))
180                 wth->file_encap = WTAP_ENCAP_ETHERNET;
181         else {
182                 g_message("pcap: network type \"%.4s\" unknown", search_encap);
183                 *err = WTAP_ERR_UNSUPPORTED;
184                 return -1;
185         }
186
187         /*bytes_read = file_read(&next_date, 1, sizeof(struct frame_date), wth->fh);
188         errno = WTAP_ERR_CANT_READ;
189         if (bytes_read != sizeof(struct frame_date)) {
190                 goto read_error;
191         }
192
193         while (memcmp(&start_date, &next_date, 4)) {
194                 file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR);
195                 errno = WTAP_ERR_CANT_READ;
196                 bytes_read = file_read(&next_date, 1, sizeof(struct frame_date),
197                                    wth->fh);
198                 if (bytes_read != sizeof(struct frame_date)) {
199                         goto read_error;
200                 }
201         }*/
202
203         if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
204                 file_seek(wth->fh, 294, SEEK_CUR);
205                 wth->data_offset += 294;
206         } else if (wth->file_encap == WTAP_ENCAP_LAPB) {
207                 file_seek(wth->fh, 297, SEEK_CUR);
208                 wth->data_offset += 297;
209         }
210
211         return 1;
212
213 read_error:
214         *err = file_error(wth->fh);
215         if (*err != 0) {
216                 g_free(wth->capture.radcom);
217                 return -1;
218         }
219         g_free(wth->capture.radcom);
220         return 0;
221 }
222
223 /* Read the next packet */
224 static int radcom_read(wtap *wth, int *err)
225 {
226         int     bytes_read;
227         struct radcomrec_hdr hdr;
228         guint16 length;
229         guint32 sec;
230         struct tm tm;
231         int     data_offset;
232         char    fcs[2];
233
234         /* Read record header. */
235         errno = WTAP_ERR_CANT_READ;
236         bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
237         if (bytes_read != sizeof hdr) {
238                 *err = file_error(wth->fh);
239                 if (*err != 0)
240                         return -1;
241                 if (bytes_read != 0) {
242                         *err = WTAP_ERR_SHORT_READ;
243                         return -1;
244                 }
245                 return 0;
246         }
247         wth->data_offset += sizeof hdr;
248         length = pletohs(&hdr.length);
249         if (length == 0) return 0;
250
251         if (wth->file_encap == WTAP_ENCAP_LAPB)
252                 length -= 2; /* FCS */
253
254         wth->phdr.len = length;
255         wth->phdr.caplen = length;
256
257         tm.tm_year = pletohs(&hdr.date.year)-1900;
258         tm.tm_mon = hdr.date.month-1;
259         tm.tm_mday = hdr.date.day;
260         sec = pletohl(&hdr.date.sec);
261         tm.tm_hour = sec/3600;
262         tm.tm_min = (sec%3600)/60;
263         tm.tm_sec = sec%60;
264         tm.tm_isdst = -1;
265         wth->phdr.ts.tv_sec = mktime(&tm);
266         wth->phdr.ts.tv_usec = pletohl(&hdr.date.usec);
267         wth->phdr.pseudo_header.x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
268
269         /*
270          * Read the packet data.
271          */
272         buffer_assure_space(wth->frame_buffer, length);
273         data_offset = wth->data_offset;
274         errno = WTAP_ERR_CANT_READ;
275         bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
276                         length, wth->fh);
277
278         if (bytes_read != length) {
279                 *err = file_error(wth->fh);
280                 if (*err == 0)
281                         *err = WTAP_ERR_SHORT_READ;
282                 return -1;
283         }
284         wth->data_offset += length;
285
286         wth->phdr.pkt_encap = wth->file_encap;
287
288         if (wth->file_encap == WTAP_ENCAP_LAPB) {
289                 /* Read the FCS.
290                    XXX - should we put it in the pseudo-header? */
291                 errno = WTAP_ERR_CANT_READ;
292                 bytes_read = file_read(&fcs, 1, sizeof fcs, wth->fh);
293                 if (bytes_read != sizeof fcs) {
294                         *err = file_error(wth->fh);
295                         if (*err == 0)
296                                 *err = WTAP_ERR_SHORT_READ;
297                         return -1;
298                 }
299                 wth->data_offset += sizeof fcs;
300         }
301
302         return data_offset;
303 }