3 * $Id: eyesdn.c,v 1.8 2004/04/02 07:40:37 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.
29 #include "file_wrappers.h"
36 /* This module reads the output of the EyeSDN USB S0/E1 ISDN probes
37 * They store HDLC frames of D and B channels in a binary format
40 * 1-6 Byte: EyeSDN - Magic
43 * Each Frame starts with the 0xff Flag byte
44 * - Bytes 0-2: timestamp (long usec in network byte order)
45 * - Bytes 3-7: timestamp (40bits sec since 1970 in network byte order)
46 * - Byte 8: channel (0 for D channel, 1-30 for B1-B30)
47 * - Byte 9: Sender (0 NT, 1 TE)
48 * - Byte 10-11: frame size in bytes
49 * - Byte 12-n: Frame Payload
51 * All multibyte values are represented in network byte order
52 * The frame is terminated with a flag character (0xff)
53 * bytes 0xff within a frame are escaped using the 0xfe escape character
54 * the byte following the escape character is decremented by two:
55 * so 0xfe 0xfd is actually a 0xff
56 * Characters that need to be escaped are 0xff and 0xfe
60 static int esc_read(guint8 *buf, int len, FILE_T fh)
65 for(i=0; i<len; i++) {
68 return -2; /* EOF or error */
70 return -1; /* error !!, read into next frame */
72 /* we need to escape */
83 /* Magic text to check for eyesdn-ness of file */
84 static const unsigned char eyesdn_hdr_magic[] =
85 { 'E', 'y', 'e', 'S', 'D', 'N'};
86 #define EYESDN_HDR_MAGIC_SIZE (sizeof(eyesdn_hdr_magic) / sizeof(eyesdn_hdr_magic[0]))
88 /* Size of a record header */
89 #define EYESDN_HDR_LENGTH 12
92 * XXX - is this the biggest packet we can get?
94 #define EYESDN_MAX_PACKET_LEN 16384
96 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
98 static gboolean eyesdn_seek_read(wtap *wth, long seek_off,
99 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
100 int *err, gchar **err_info);
101 static gboolean parse_eyesdn_packet_data(FILE_T fh, int pkt_len, guint8* buf,
102 int *err, gchar **err_info);
103 static int parse_eyesdn_rec_hdr(wtap *wth, FILE_T fh,
104 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
106 /* Seeks to the beginning of the next packet, and returns the
107 byte offset. Returns -1 on failure, and sets "*err" to the error. */
108 static long eyesdn_seek_next_packet(wtap *wth, int *err)
113 while ((byte = file_getc(wth->fh)) != EOF) {
115 cur_off = file_tell(wth->fh);
118 *err = file_error(wth->fh);
124 if (file_eof(wth->fh)) {
128 /* We (presumably) got an error (there's no equivalent to "ferror()"
129 in zlib, alas, so we don't have a wrapper to check for an error). */
130 *err = file_error(wth->fh);
135 int eyesdn_open(wtap *wth, int *err, gchar **err_info _U_)
138 char magic[EYESDN_HDR_MAGIC_SIZE];
140 /* Look for eyesdn header */
141 errno = WTAP_ERR_CANT_READ;
142 bytes_read = file_read(&magic, 1, sizeof magic, wth->fh);
143 if (bytes_read != sizeof magic) {
144 *err = file_error(wth->fh);
149 if (memcmp(magic, eyesdn_hdr_magic, EYESDN_HDR_MAGIC_SIZE) != 0)
152 wth->data_offset = 0;
153 wth->file_encap = WTAP_ENCAP_ISDN;
154 wth->file_type = WTAP_FILE_EYESDN;
155 wth->snapshot_length = 0; /* not known */
156 wth->subtype_read = eyesdn_read;
157 wth->subtype_seek_read = eyesdn_seek_read;
162 /* Find the next packet and parse it; called from wtap_loop() and wtap_read(). */
163 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
170 /* Find the next packet */
171 offset = eyesdn_seek_next_packet(wth, err);
175 /* Parse the header */
176 pkt_len = parse_eyesdn_rec_hdr(wth, wth->fh, &wth->pseudo_header, err,
181 /* Make sure we have enough room for the packet */
182 buffer_assure_space(wth->frame_buffer, EYESDN_MAX_PACKET_LEN);
183 buf = buffer_start_ptr(wth->frame_buffer);
185 /* Read the packet data */
186 if (!parse_eyesdn_packet_data(wth->fh, pkt_len, buf, err, err_info))
189 wth->data_offset = offset;
190 *data_offset = offset;
194 /* Used to read packets in random-access fashion */
196 eyesdn_seek_read (wtap *wth, long seek_off,
197 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
198 int *err, gchar **err_info)
202 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
205 pkt_len = parse_eyesdn_rec_hdr(NULL, wth->random_fh, pseudo_header,
208 if (pkt_len != len) {
210 *err = WTAP_ERR_BAD_RECORD;
211 *err_info = g_strdup_printf("eyesdn: requested length %d doesn't match length %d",
217 return parse_eyesdn_packet_data(wth->random_fh, pkt_len, pd, err,
221 /* Parses a packet record header. */
223 parse_eyesdn_rec_hdr(wtap *wth, FILE_T fh,
224 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
226 guint8 hdr[EYESDN_HDR_LENGTH];
227 unsigned long secs, usecs;
229 unsigned int channel, direction;
231 /* Our file pointer should be at the summary information header
232 * for a packet. Read in that header and extract the useful
235 if (esc_read(hdr, EYESDN_HDR_LENGTH, fh) != EYESDN_HDR_LENGTH) {
236 *err = file_error(fh);
238 *err = WTAP_ERR_SHORT_READ;
242 /* extract information from header */
243 usecs = ((unsigned long) hdr[0]);
244 usecs = (usecs << 8) | ((unsigned long) hdr[1]);
245 usecs = (usecs << 8) | ((unsigned long) hdr[2]);
247 secs = ((unsigned long) hdr[3]);
251 secs = (secs << 8) | ((unsigned long) hdr[4]);
252 secs = (secs << 8) | ((unsigned long) hdr[5]);
253 secs = (secs << 8) | ((unsigned long) hdr[6]);
254 secs = (secs << 8) | ((unsigned long) hdr[7]);
258 pkt_len = ((unsigned long) hdr[10]);
259 pkt_len = (pkt_len << 8) | ((unsigned long) hdr[11]);
263 *err = WTAP_ERR_BAD_RECORD;
264 *err_info = g_strdup_printf("eyesdn: bad channel number %u",
270 *err = WTAP_ERR_BAD_RECORD;
271 *err_info = g_strdup_printf("eyesdn: bad direction value %u",
275 if(pkt_len > EYESDN_MAX_PACKET_LEN) {
276 *err = WTAP_ERR_BAD_RECORD;
277 *err_info = g_strdup_printf("eyesdn: File has %u-byte packet, bigger than maximum of %u",
278 pkt_len, EYESDN_MAX_PACKET_LEN);
283 wth->phdr.ts.tv_sec = secs;
284 wth->phdr.ts.tv_usec = usecs;
285 wth->phdr.caplen = pkt_len;
286 wth->phdr.len = pkt_len;
288 pseudo_header->isdn.uton = direction;
289 pseudo_header->isdn.channel = channel;
296 parse_eyesdn_packet_data(FILE_T fh, int pkt_len, guint8* buf, int *err,
301 errno = WTAP_ERR_CANT_READ;
302 bytes_read = esc_read(buf, pkt_len, fh);
303 if (bytes_read != pkt_len) {
304 if (bytes_read == -2) {
305 *err = file_error(fh);
307 *err = WTAP_ERR_SHORT_READ;
308 } else if (bytes_read == -1) {
309 *err = WTAP_ERR_BAD_RECORD;
310 *err_info = g_strdup("eyesdn: No flag character seen in frame");
312 *err = WTAP_ERR_SHORT_READ;