3 * $Id: radcom.c,v 1.20 2000/04/15 21:12:37 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);
72 int radcom_open(wtap *wth, int *err)
76 struct frame_date start_date;
80 char encap_magic[7] = {0x54, 0x43, 0x50, 0x00, 0x42, 0x43, 0x09};
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);
94 if (memcmp(magic, radcom_magic, 8) != 0) {
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);
108 wth->data_offset += 1;
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);
118 wth->data_offset += 1;
120 file_seek(wth->fh, 1, SEEK_CUR);
121 wth->data_offset += 1;
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);
132 wth->data_offset += sizeof(struct frame_date);
134 /* This is a radcom file */
135 wth->file_type = WTAP_FILE_RADCOM;
136 wth->subtype_read = radcom_read;
137 wth->snapshot_length = 16384; /* not available in header, only in frame */
139 tm.tm_year = pletohs(&start_date.year)-1900;
140 tm.tm_mon = start_date.month-1;
141 tm.tm_mday = start_date.day;
142 sec = pletohl(&start_date.sec);
143 tm.tm_hour = sec/3600;
144 tm.tm_min = (sec%3600)/60;
148 file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR);
149 wth->data_offset += sizeof(struct frame_date);
151 errno = WTAP_ERR_CANT_READ;
152 bytes_read = file_read(search_encap, 1, 7, wth->fh);
153 if (bytes_read != 7) {
156 wth->data_offset += 7;
157 while (memcmp(encap_magic, search_encap, 7)) {
158 file_seek(wth->fh, -6, SEEK_CUR);
159 wth->data_offset -= 6;
160 errno = WTAP_ERR_CANT_READ;
161 bytes_read = file_read(search_encap, 1, 7, wth->fh);
162 if (bytes_read != 7) {
165 wth->data_offset += 7;
167 file_seek(wth->fh, 12, SEEK_CUR);
168 wth->data_offset += 12;
169 errno = WTAP_ERR_CANT_READ;
170 bytes_read = file_read(search_encap, 1, 4, wth->fh);
171 if (bytes_read != 4) {
174 wth->data_offset += 4;
175 if (!memcmp(search_encap, "LAPB", 4))
176 wth->file_encap = WTAP_ENCAP_LAPB;
177 else if (!memcmp(search_encap, "Ethe", 4))
178 wth->file_encap = WTAP_ENCAP_ETHERNET;
180 g_message("pcap: network type \"%.4s\" unknown", search_encap);
181 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
185 /*bytes_read = file_read(&next_date, 1, sizeof(struct frame_date), wth->fh);
186 errno = WTAP_ERR_CANT_READ;
187 if (bytes_read != sizeof(struct frame_date)) {
191 while (memcmp(&start_date, &next_date, 4)) {
192 file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR);
193 errno = WTAP_ERR_CANT_READ;
194 bytes_read = file_read(&next_date, 1, sizeof(struct frame_date),
196 if (bytes_read != sizeof(struct frame_date)) {
201 if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
202 file_seek(wth->fh, 294, SEEK_CUR);
203 wth->data_offset += 294;
204 } else if (wth->file_encap == WTAP_ENCAP_LAPB) {
205 file_seek(wth->fh, 297, SEEK_CUR);
206 wth->data_offset += 297;
212 *err = file_error(wth->fh);
218 /* Read the next packet */
219 static int radcom_read(wtap *wth, int *err)
222 struct radcomrec_hdr hdr;
229 /* Read record header. */
230 errno = WTAP_ERR_CANT_READ;
231 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
232 if (bytes_read != sizeof hdr) {
233 *err = file_error(wth->fh);
236 if (bytes_read != 0) {
237 *err = WTAP_ERR_SHORT_READ;
242 wth->data_offset += sizeof hdr;
243 length = pletohs(&hdr.length);
244 if (length == 0) return 0;
246 if (wth->file_encap == WTAP_ENCAP_LAPB)
247 length -= 2; /* FCS */
249 wth->phdr.len = length;
250 wth->phdr.caplen = length;
252 tm.tm_year = pletohs(&hdr.date.year)-1900;
253 tm.tm_mon = hdr.date.month-1;
254 tm.tm_mday = hdr.date.day;
255 sec = pletohl(&hdr.date.sec);
256 tm.tm_hour = sec/3600;
257 tm.tm_min = (sec%3600)/60;
260 wth->phdr.ts.tv_sec = mktime(&tm);
261 wth->phdr.ts.tv_usec = pletohl(&hdr.date.usec);
262 wth->phdr.pseudo_header.x25.flags = (hdr.dce & 0x1) ? 0x00 : 0x80;
265 * Read the packet data.
267 buffer_assure_space(wth->frame_buffer, length);
268 data_offset = wth->data_offset;
269 errno = WTAP_ERR_CANT_READ;
270 bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
273 if (bytes_read != length) {
274 *err = file_error(wth->fh);
276 *err = WTAP_ERR_SHORT_READ;
279 wth->data_offset += length;
281 wth->phdr.pkt_encap = wth->file_encap;
283 if (wth->file_encap == WTAP_ENCAP_LAPB) {
285 XXX - should we put it in the pseudo-header? */
286 errno = WTAP_ERR_CANT_READ;
287 bytes_read = file_read(&fcs, 1, sizeof fcs, wth->fh);
288 if (bytes_read != sizeof fcs) {
289 *err = file_error(wth->fh);
291 *err = WTAP_ERR_SHORT_READ;
294 wth->data_offset += sizeof fcs;