3 * $Id: radcom.c,v 1.24 2000/07/26 00:20:07 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
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 int radcom_read(wtap *wth, int *err);
71 static int radcom_seek_read(wtap *wth, int 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,
75 static int radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err);
77 int radcom_open(wtap *wth, int *err)
81 struct frame_date start_date;
85 char encap_magic[7] = {0x54, 0x43, 0x50, 0x00, 0x42, 0x43, 0x09};
88 /* Read in the string that should be at the start of a RADCOM file */
89 file_seek(wth->fh, 0, SEEK_SET);
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 file_seek(wth->fh, 0x8B, SEEK_SET);
104 wth->data_offset = 0x8B;
105 errno = WTAP_ERR_CANT_READ;
106 bytes_read = file_read(&byte, 1, 1, wth->fh);
107 if (bytes_read != 1) {
108 *err = file_error(wth->fh);
113 wth->data_offset += 1;
115 errno = WTAP_ERR_CANT_READ;
116 bytes_read = file_read(&byte, 1, 1, wth->fh);
117 if (bytes_read != 1) {
118 *err = file_error(wth->fh);
123 wth->data_offset += 1;
125 file_seek(wth->fh, 1, SEEK_CUR);
126 wth->data_offset += 1;
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);
137 wth->data_offset += sizeof(struct frame_date);
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 = 16384; /* not available in header, only in frame */
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;
154 file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR);
155 wth->data_offset += sizeof(struct frame_date);
157 errno = WTAP_ERR_CANT_READ;
158 bytes_read = file_read(search_encap, 1, 7, wth->fh);
159 if (bytes_read != 7) {
162 wth->data_offset += 7;
163 while (memcmp(encap_magic, search_encap, 7)) {
164 file_seek(wth->fh, -6, SEEK_CUR);
165 wth->data_offset -= 6;
166 errno = WTAP_ERR_CANT_READ;
167 bytes_read = file_read(search_encap, 1, 7, wth->fh);
168 if (bytes_read != 7) {
171 wth->data_offset += 7;
173 file_seek(wth->fh, 12, SEEK_CUR);
174 wth->data_offset += 12;
175 errno = WTAP_ERR_CANT_READ;
176 bytes_read = file_read(search_encap, 1, 4, wth->fh);
177 if (bytes_read != 4) {
180 wth->data_offset += 4;
181 if (!memcmp(search_encap, "LAPB", 4))
182 wth->file_encap = WTAP_ENCAP_LAPB;
183 else if (!memcmp(search_encap, "Ethe", 4))
184 wth->file_encap = WTAP_ENCAP_ETHERNET;
186 g_message("pcap: network type \"%.4s\" unknown", search_encap);
187 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
191 /*bytes_read = file_read(&next_date, 1, sizeof(struct frame_date), wth->fh);
192 errno = WTAP_ERR_CANT_READ;
193 if (bytes_read != sizeof(struct frame_date)) {
197 while (memcmp(&start_date, &next_date, 4)) {
198 file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR);
199 errno = WTAP_ERR_CANT_READ;
200 bytes_read = file_read(&next_date, 1, sizeof(struct frame_date),
202 if (bytes_read != sizeof(struct frame_date)) {
207 if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
208 file_seek(wth->fh, 294, SEEK_CUR);
209 wth->data_offset += 294;
210 } else if (wth->file_encap == WTAP_ENCAP_LAPB) {
211 file_seek(wth->fh, 297, SEEK_CUR);
212 wth->data_offset += 297;
218 *err = file_error(wth->fh);
224 /* Read the next packet */
225 static int radcom_read(wtap *wth, int *err)
229 struct radcomrec_hdr hdr;
236 /* Read record header. */
237 record_offset = wth->data_offset;
238 ret = radcom_read_rec_header(wth->fh, &hdr, err);
240 /* Read error or EOF */
243 wth->data_offset += sizeof hdr;
244 length = pletohs(&hdr.length);
245 if (length == 0) return 0;
247 if (wth->file_encap == WTAP_ENCAP_LAPB)
248 length -= 2; /* FCS */
250 wth->phdr.len = length;
251 wth->phdr.caplen = length;
253 tm.tm_year = pletohs(&hdr.date.year)-1900;
254 tm.tm_mon = hdr.date.month-1;
255 tm.tm_mday = hdr.date.day;
256 sec = pletohl(&hdr.date.sec);
257 tm.tm_hour = sec/3600;
258 tm.tm_min = (sec%3600)/60;
261 wth->phdr.ts.tv_sec = mktime(&tm);
262 wth->phdr.ts.tv_usec = pletohl(&hdr.date.usec);
263 wth->pseudo_header.x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
266 * Read the packet data.
268 buffer_assure_space(wth->frame_buffer, length);
269 if (radcom_read_rec_data(wth->fh,
270 buffer_start_ptr(wth->frame_buffer), length, err) < 0)
271 return -1; /* Read error */
272 wth->data_offset += length;
274 wth->phdr.pkt_encap = wth->file_encap;
276 if (wth->file_encap == WTAP_ENCAP_LAPB) {
278 XXX - should we put it in the pseudo-header? */
279 errno = WTAP_ERR_CANT_READ;
280 bytes_read = file_read(&fcs, 1, sizeof fcs, wth->fh);
281 if (bytes_read != sizeof fcs) {
282 *err = file_error(wth->fh);
284 *err = WTAP_ERR_SHORT_READ;
287 wth->data_offset += sizeof fcs;
290 return record_offset;
294 radcom_seek_read(wtap *wth, int seek_off,
295 union wtap_pseudo_header *pseudo_header, u_char *pd, int length)
298 int err; /* XXX - return this */
299 struct radcomrec_hdr hdr;
301 file_seek(wth->random_fh, seek_off, SEEK_SET);
303 /* Read record header. */
304 ret = radcom_read_rec_header(wth->random_fh, &hdr, &err);
306 /* Read error or EOF */
310 pseudo_header->x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
313 * Read the packet data.
315 return radcom_read_rec_data(wth->random_fh, pd, length, &err);
319 radcom_read_rec_header(FILE_T fh, struct radcomrec_hdr *hdr, int *err)
323 errno = WTAP_ERR_CANT_READ;
324 bytes_read = file_read(hdr, 1, sizeof *hdr, fh);
325 if (bytes_read != sizeof *hdr) {
326 *err = file_error(fh);
329 if (bytes_read != 0) {
330 *err = WTAP_ERR_SHORT_READ;
339 radcom_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
343 errno = WTAP_ERR_CANT_READ;
344 bytes_read = file_read(pd, 1, length, fh);
346 if (bytes_read != length) {
347 *err = file_error(fh);
349 *err = WTAP_ERR_SHORT_READ;