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 (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 Bit 0(0 NT, 1 TE), Protocol in Bits 7:1, see enum
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, int seekback)
67 if(seekback) cur_off = file_tell(fh);
68 else cur_off=0; /* suppress uninitialized warning */
70 for(i=0; i<len; i++) {
73 return -2; /* EOF or error */
75 return -1; /* error !!, read into next frame */
77 /* we need to escape */
87 if (file_seek(fh, cur_off, SEEK_SET, &err) == -1)
88 return err<0?err:-err;
93 /* Magic text to check for eyesdn-ness of file */
94 static const unsigned char eyesdn_hdr_magic[] =
95 { 'E', 'y', 'e', 'S', 'D', 'N'};
96 #define EYESDN_HDR_MAGIC_SIZE (sizeof(eyesdn_hdr_magic) / sizeof(eyesdn_hdr_magic[0]))
98 /* Size of a record header */
99 #define EYESDN_HDR_LENGTH 12
102 * XXX - is this the biggest packet we can get?
104 #define EYESDN_MAX_PACKET_LEN 16384
106 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
107 gint64 *data_offset);
108 static gboolean eyesdn_seek_read(wtap *wth, gint64 seek_off,
109 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
110 int *err, gchar **err_info);
111 static gboolean parse_eyesdn_packet_data(FILE_T fh, int pkt_len, guint8* buf,
112 int *err, gchar **err_info);
113 static int parse_eyesdn_rec_hdr(wtap *wth, FILE_T fh,
114 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
116 /* Seeks to the beginning of the next packet, and returns the
117 byte offset. Returns -1 on failure, and sets "*err" to the error. */
118 static gint64 eyesdn_seek_next_packet(wtap *wth, int *err)
123 while ((byte = file_getc(wth->fh)) != EOF) {
125 cur_off = file_tell(wth->fh);
128 *err = file_error(wth->fh);
134 if (file_eof(wth->fh)) {
138 /* We (presumably) got an error (there's no equivalent to "ferror()"
139 in zlib, alas, so we don't have a wrapper to check for an error). */
140 *err = file_error(wth->fh);
145 int eyesdn_open(wtap *wth, int *err, gchar **err_info _U_)
148 char magic[EYESDN_HDR_MAGIC_SIZE];
150 /* Look for eyesdn header */
151 errno = WTAP_ERR_CANT_READ;
152 bytes_read = file_read(&magic, 1, sizeof magic, wth->fh);
153 if (bytes_read != sizeof magic) {
154 *err = file_error(wth->fh);
159 if (memcmp(magic, eyesdn_hdr_magic, EYESDN_HDR_MAGIC_SIZE) != 0)
162 wth->data_offset = 0;
163 wth->file_encap = WTAP_ENCAP_PER_PACKET;
164 wth->file_type = WTAP_FILE_EYESDN;
165 wth->snapshot_length = 0; /* not known */
166 wth->subtype_read = eyesdn_read;
167 wth->subtype_seek_read = eyesdn_seek_read;
168 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
173 /* Find the next packet and parse it; called from wtap_read(). */
174 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
181 /* Find the next packet */
182 offset = eyesdn_seek_next_packet(wth, err);
186 /* Parse the header */
187 pkt_len = parse_eyesdn_rec_hdr(wth, wth->fh, &wth->pseudo_header, err,
192 /* Make sure we have enough room for the packet */
193 buffer_assure_space(wth->frame_buffer, EYESDN_MAX_PACKET_LEN);
194 buf = buffer_start_ptr(wth->frame_buffer);
196 /* Read the packet data */
197 if (!parse_eyesdn_packet_data(wth->fh, pkt_len, buf, err, err_info))
200 wth->data_offset = offset;
201 *data_offset = offset;
205 /* Used to read packets in random-access fashion */
207 eyesdn_seek_read (wtap *wth, gint64 seek_off,
208 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
209 int *err, gchar **err_info)
213 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
216 pkt_len = parse_eyesdn_rec_hdr(NULL, wth->random_fh, pseudo_header,
219 if (pkt_len != len) {
221 *err = WTAP_ERR_BAD_RECORD;
222 *err_info = g_strdup_printf("eyesdn: requested length %d doesn't match length %d",
228 return parse_eyesdn_packet_data(wth->random_fh, pkt_len, pd, err,
232 /* Parses a packet record header. */
234 parse_eyesdn_rec_hdr(wtap *wth, FILE_T fh,
235 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
237 guint8 hdr[EYESDN_HDR_LENGTH];
241 guint8 channel, direction;
243 /* Our file pointer should be at the summary information header
244 * for a packet. Read in that header and extract the useful
247 if (esc_read(hdr, EYESDN_HDR_LENGTH, fh, 0) != EYESDN_HDR_LENGTH) {
248 *err = file_error(fh);
250 *err = WTAP_ERR_SHORT_READ;
254 /* extract information from header */
255 usecs = pntoh24(&hdr[0]);
261 secs = (secs << 8) | hdr[4];
262 secs = (secs << 8) | hdr[5];
263 secs = (secs << 8) | hdr[6];
264 secs = (secs << 8) | hdr[7];
268 pkt_len = pntohs(&hdr[10]);
270 switch(direction >> 1) {
272 case EYESDN_ENCAP_ISDN: /* ISDN */
273 pseudo_header->isdn.uton = direction & 1;
274 pseudo_header->isdn.channel = channel;
275 if(channel) { /* bearer channels */
277 wth->phdr.pkt_encap = WTAP_ENCAP_ISDN; /* recognises PPP */
278 pseudo_header->isdn.uton=!pseudo_header->isdn.uton; /* bug */
280 } else { /* D channel */
282 wth->phdr.pkt_encap = WTAP_ENCAP_ISDN;
286 case EYESDN_ENCAP_MSG: /* Layer 1 message */
288 wth->phdr.pkt_encap = WTAP_ENCAP_LAYER1_EVENT;
290 pseudo_header->l1event.uton = (direction & 1);
292 case EYESDN_ENCAP_LAPB: /* X.25 via LAPB */
294 wth->phdr.pkt_encap = WTAP_ENCAP_LAPB;
296 pseudo_header->x25.flags = (direction & 1) ? 0 : 0x80;
298 case EYESDN_ENCAP_ATM: { /* ATM cells */
300 unsigned char cell[CELL_LEN];
301 if(pkt_len != CELL_LEN) {
302 *err = WTAP_ERR_BAD_RECORD;
303 *err_info = g_strdup_printf("eyesdn: ATM cell has a length "
304 "!= 53 (%u)", pkt_len);
308 if (esc_read(cell, CELL_LEN, fh, 1) != CELL_LEN) {
309 *err = file_error(fh);
311 *err = WTAP_ERR_SHORT_READ;
316 wth->phdr.pkt_encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
318 pseudo_header->atm.flags=ATM_RAW_CELL;
319 pseudo_header->atm.aal=AAL_UNKNOWN;
320 pseudo_header->atm.type=TRAF_UMTS_FP;
321 pseudo_header->atm.subtype=TRAF_ST_UNKNOWN;
322 pseudo_header->atm.vpi=((cell[0]&0xf)<<4) + (cell[0]&0xf);
323 pseudo_header->atm.vci=((cell[0]&0xf)<<4) + cell[0]; /* from cell */
324 pseudo_header->atm.channel=direction & 1;
327 case EYESDN_ENCAP_MTP2: /* SS7 frames */
328 pseudo_header->mtp2.sent = direction & 1;
329 pseudo_header->mtp2.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
330 pseudo_header->mtp2.link_number = channel;
332 wth->phdr.pkt_encap = WTAP_ENCAP_MTP2;
335 case EYESDN_ENCAP_DPNSS: /* DPNSS */
336 pseudo_header->isdn.uton = direction & 1;
337 pseudo_header->isdn.channel = channel;
339 wth->phdr.pkt_encap = WTAP_ENCAP_DPNSS;
342 case EYESDN_ENCAP_DASS2: /* DASS2 frames */
343 pseudo_header->isdn.uton = direction & 1;
344 pseudo_header->isdn.channel = channel;
346 wth->phdr.pkt_encap = WTAP_ENCAP_DPNSS;
349 case EYESDN_ENCAP_BACNET: /* BACNET async over HDLC frames */
350 /* pseudo_header->isdn.uton = direction & 1; */
351 /* pseudo_header->isdn.channel = channel; */
353 wth->phdr.pkt_encap = WTAP_ENCAP_BACNET_MS_TP;
358 if(pkt_len > EYESDN_MAX_PACKET_LEN) {
359 *err = WTAP_ERR_BAD_RECORD;
360 *err_info = g_strdup_printf("eyesdn: File has %u-byte packet, bigger than maximum of %u",
361 pkt_len, EYESDN_MAX_PACKET_LEN);
366 wth->phdr.ts.secs = secs;
367 wth->phdr.ts.nsecs = usecs * 1000;
368 wth->phdr.caplen = pkt_len;
369 wth->phdr.len = pkt_len;
377 parse_eyesdn_packet_data(FILE_T fh, int pkt_len, guint8* buf, int *err,
382 errno = WTAP_ERR_CANT_READ;
383 bytes_read = esc_read(buf, pkt_len, fh, 0);
384 if (bytes_read != pkt_len) {
385 if (bytes_read == -2) {
386 *err = file_error(fh);
388 *err = WTAP_ERR_SHORT_READ;
389 } else if (bytes_read == -1) {
390 *err = WTAP_ERR_BAD_RECORD;
391 *err_info = g_strdup("eyesdn: No flag character seen in frame");
393 *err = WTAP_ERR_SHORT_READ;