3 * $Id: radcom.c,v 1.34 2002/03/05 05:58:40 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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.
31 #include "file_wrappers.h"
39 guint32 sec; /* seconds since midnight */
43 struct unaligned_frame_date {
47 char sec[4]; /* seconds since midnight */
51 static guint8 radcom_magic[8] = {
52 0x42, 0xD2, 0x00, 0x34, 0x12, 0x66, 0x22, 0x88
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 */
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,
74 static int radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr,
76 static int radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err);
78 int radcom_open(wtap *wth, int *err)
82 struct frame_date start_date;
86 char encap_magic[4] = {0x00, 0x42, 0x43, 0x09};
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);
99 if (memcmp(magic, radcom_magic, 8) != 0) {
103 if (file_seek(wth->fh, 0x8B, SEEK_SET) == -1) {
104 *err = file_error(wth->fh);
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);
116 wth->data_offset += 1;
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);
126 wth->data_offset += 1;
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);
138 wth->data_offset += sizeof(struct frame_date);
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 */
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;
155 if (file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR) == -1) {
156 *err = file_error(wth->fh);
159 wth->data_offset += sizeof(struct frame_date);
161 errno = WTAP_ERR_CANT_READ;
162 bytes_read = file_read(search_encap, 1, 4, wth->fh);
163 if (bytes_read != 4) {
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);
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) {
178 wth->data_offset += 4;
180 if (file_seek(wth->fh, 12, SEEK_CUR) == -1) {
181 *err = file_error(wth->fh);
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) {
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;
196 g_message("pcap: network type \"%.4s\" unknown", search_encap);
197 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
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)) {
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);
212 errno = WTAP_ERR_CANT_READ;
213 bytes_read = file_read(&next_date, 1, sizeof(struct frame_date),
215 if (bytes_read != sizeof(struct frame_date)) {
220 if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
221 if (file_seek(wth->fh, 294, SEEK_CUR) == -1) {
222 *err = file_error(wth->fh);
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);
231 wth->data_offset += 297;
237 *err = file_error(wth->fh);
243 /* Read the next packet */
244 static gboolean radcom_read(wtap *wth, int *err, long *data_offset)
247 struct radcomrec_hdr hdr;
254 /* Read record header. */
255 *data_offset = wth->data_offset;
256 ret = radcom_read_rec_header(wth->fh, &hdr, err);
258 /* Read error or EOF */
261 wth->data_offset += sizeof hdr;
262 length = pletohs(&hdr.length);
263 if (length == 0) return FALSE;
265 if (wth->file_encap == WTAP_ENCAP_LAPB)
266 length -= 2; /* FCS */
268 wth->phdr.len = length;
269 wth->phdr.caplen = length;
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;
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;
284 * Read the packet data.
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;
292 wth->phdr.pkt_encap = wth->file_encap;
294 if (wth->file_encap == WTAP_ENCAP_LAPB) {
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);
302 *err = WTAP_ERR_SHORT_READ;
305 wth->data_offset += sizeof fcs;
312 radcom_seek_read(wtap *wth, long seek_off,
313 union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err)
316 struct radcomrec_hdr hdr;
318 if (file_seek(wth->random_fh, seek_off, SEEK_SET) == -1) {
319 *err = file_error(wth->random_fh);
323 /* Read record header. */
324 ret = radcom_read_rec_header(wth->random_fh, &hdr, err);
326 /* Read error or EOF */
328 /* EOF means "short read" in random-access mode */
329 *err = WTAP_ERR_SHORT_READ;
334 pseudo_header->x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
337 * Read the packet data.
339 return radcom_read_rec_data(wth->random_fh, pd, length, err);
343 radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr, int *err)
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);
353 if (bytes_read != 0) {
354 *err = WTAP_ERR_SHORT_READ;
363 radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
367 errno = WTAP_ERR_CANT_READ;
368 bytes_read = file_read(pd, 1, length, fh);
370 if (bytes_read != length) {
371 *err = file_error(fh);
373 *err = WTAP_ERR_SHORT_READ;