4 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "file_wrappers.h"
30 /* This module reads the output of the EyeSDN USB S0/E1 ISDN probes
31 * They store HDLC frames of D and B channels in a binary format
34 * 1-6 Byte: EyeSDN - Magic
37 * Each Frame starts with the 0xff Flag byte
38 * - Bytes 0-2: timestamp (usec in network byte order)
39 * - Bytes 3-7: timestamp (40bits sec since 1970 in network byte order)
40 * - Byte 8: channel (0 for D channel, 1-30 for B1-B30)
41 * - Byte 9: Sender Bit 0(0 NT, 1 TE), Protocol in Bits 7:1, see enum
42 * - Byte 10-11: frame size in bytes
43 * - Byte 12-n: Frame Payload
45 * All multibyte values are represented in network byte order
46 * The frame is terminated with a flag character (0xff)
47 * bytes 0xff within a frame are escaped using the 0xfe escape character
48 * the byte following the escape character is decremented by two:
49 * so 0xfe 0xfd is actually a 0xff
50 * Characters that need to be escaped are 0xff and 0xfe
54 static gboolean esc_read(FILE_T fh, guint8 *buf, int len, int *err, gchar **err_info)
59 for(i=0; i<len; i++) {
63 *err=file_error(fh, err_info);
65 *err=WTAP_ERR_SHORT_READ;
69 /* error !!, read into next frame */
70 *err=WTAP_ERR_BAD_FILE;
71 *err_info=g_strdup("eyesdn: No flag character seen in frame");
75 /* we need to escape */
79 *err=file_error(fh, err_info);
81 *err=WTAP_ERR_SHORT_READ;
92 /* Magic text to check for eyesdn-ness of file */
93 static const unsigned char eyesdn_hdr_magic[] =
94 { 'E', 'y', 'e', 'S', 'D', 'N'};
95 #define EYESDN_HDR_MAGIC_SIZE sizeof(eyesdn_hdr_magic)
97 /* Size of a record header */
98 #define EYESDN_HDR_LENGTH 12
100 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
101 gint64 *data_offset);
102 static gboolean eyesdn_seek_read(wtap *wth, gint64 seek_off,
103 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
104 static int read_eyesdn_rec(FILE_T fh, struct wtap_pkthdr *phdr, Buffer* buf,
105 int *err, gchar **err_info);
107 /* Seeks to the beginning of the next packet, and returns the
108 byte offset. Returns -1 on failure, and sets "*err" to the error
109 and "*err_info" to null or an additional error string. */
110 static gint64 eyesdn_seek_next_packet(wtap *wth, int *err, gchar **err_info)
115 while ((byte = file_getc(wth->fh)) != EOF) {
117 cur_off = file_tell(wth->fh);
120 *err = file_error(wth->fh, err_info);
127 *err = file_error(wth->fh, err_info);
131 wtap_open_return_val eyesdn_open(wtap *wth, int *err, gchar **err_info)
133 char magic[EYESDN_HDR_MAGIC_SIZE];
135 /* Look for eyesdn header */
136 if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
137 if (*err != WTAP_ERR_SHORT_READ)
138 return WTAP_OPEN_ERROR;
139 return WTAP_OPEN_NOT_MINE;
141 if (memcmp(magic, eyesdn_hdr_magic, EYESDN_HDR_MAGIC_SIZE) != 0)
142 return WTAP_OPEN_NOT_MINE;
144 wth->file_encap = WTAP_ENCAP_PER_PACKET;
145 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_EYESDN;
146 wth->snapshot_length = 0; /* not known */
147 wth->subtype_read = eyesdn_read;
148 wth->subtype_seek_read = eyesdn_seek_read;
149 wth->file_tsprec = WTAP_TSPREC_USEC;
151 return WTAP_OPEN_MINE;
154 /* Find the next packet and parse it; called from wtap_read(). */
155 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
160 /* Find the next record */
161 offset = eyesdn_seek_next_packet(wth, err, err_info);
164 *data_offset = offset;
166 /* Parse the record */
167 return read_eyesdn_rec(wth->fh, &wth->phdr, wth->frame_buffer,
171 /* Used to read packets in random-access fashion */
173 eyesdn_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
174 Buffer *buf, int *err, gchar **err_info)
176 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
179 return read_eyesdn_rec(wth->random_fh, phdr, buf, err, err_info);
182 /* Parses a record. */
184 read_eyesdn_rec(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err,
187 union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
188 guint8 hdr[EYESDN_HDR_LENGTH];
192 guint8 channel, direction;
195 /* Our file pointer should be at the summary information header
196 * for a packet. Read in that header and extract the useful
199 if (!esc_read(fh, hdr, EYESDN_HDR_LENGTH, err, err_info))
202 /* extract information from header */
203 usecs = pntoh24(&hdr[0]);
209 secs = (secs << 8) | hdr[4];
210 secs = (secs << 8) | hdr[5];
211 secs = (secs << 8) | hdr[6];
212 secs = (secs << 8) | hdr[7];
216 pkt_len = pntoh16(&hdr[10]);
218 switch(direction >> 1) {
221 case EYESDN_ENCAP_ISDN: /* ISDN */
222 pseudo_header->isdn.uton = direction & 1;
223 pseudo_header->isdn.channel = channel;
224 if(channel) { /* bearer channels */
225 phdr->pkt_encap = WTAP_ENCAP_ISDN; /* recognises PPP */
226 pseudo_header->isdn.uton=!pseudo_header->isdn.uton; /* bug */
227 } else { /* D channel */
228 phdr->pkt_encap = WTAP_ENCAP_ISDN;
232 case EYESDN_ENCAP_MSG: /* Layer 1 message */
233 phdr->pkt_encap = WTAP_ENCAP_LAYER1_EVENT;
234 pseudo_header->l1event.uton = (direction & 1);
237 case EYESDN_ENCAP_LAPB: /* X.25 via LAPB */
238 phdr->pkt_encap = WTAP_ENCAP_LAPB;
239 pseudo_header->x25.flags = (direction & 1) ? 0 : 0x80;
242 case EYESDN_ENCAP_ATM: { /* ATM cells */
244 unsigned char cell[CELL_LEN];
247 if(pkt_len != CELL_LEN) {
248 *err = WTAP_ERR_BAD_FILE;
249 *err_info = g_strdup_printf(
250 "eyesdn: ATM cell has a length != 53 (%u)",
255 cur_off = file_tell(fh);
256 if (!esc_read(fh, cell, CELL_LEN, err, err_info))
258 if (file_seek(fh, cur_off, SEEK_SET, err) == -1)
260 phdr->pkt_encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
261 pseudo_header->atm.flags=ATM_RAW_CELL;
262 pseudo_header->atm.aal=AAL_UNKNOWN;
263 pseudo_header->atm.type=TRAF_UMTS_FP;
264 pseudo_header->atm.subtype=TRAF_ST_UNKNOWN;
265 pseudo_header->atm.vpi=((cell[0]&0xf)<<4) + (cell[0]&0xf);
266 pseudo_header->atm.vci=((cell[0]&0xf)<<4) + cell[0]; /* from cell */
267 pseudo_header->atm.channel=direction & 1;
271 case EYESDN_ENCAP_MTP2: /* SS7 frames */
272 pseudo_header->mtp2.sent = direction & 1;
273 pseudo_header->mtp2.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
274 pseudo_header->mtp2.link_number = channel;
275 phdr->pkt_encap = WTAP_ENCAP_MTP2_WITH_PHDR;
278 case EYESDN_ENCAP_DPNSS: /* DPNSS */
279 pseudo_header->isdn.uton = direction & 1;
280 pseudo_header->isdn.channel = channel;
281 phdr->pkt_encap = WTAP_ENCAP_DPNSS;
284 case EYESDN_ENCAP_DASS2: /* DASS2 frames */
285 pseudo_header->isdn.uton = direction & 1;
286 pseudo_header->isdn.channel = channel;
287 phdr->pkt_encap = WTAP_ENCAP_DPNSS;
290 case EYESDN_ENCAP_BACNET: /* BACNET async over HDLC frames */
291 pseudo_header->isdn.uton = direction & 1;
292 pseudo_header->isdn.channel = channel;
293 phdr->pkt_encap = WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR;
296 case EYESDN_ENCAP_V5_EF: /* V5EF */
297 pseudo_header->isdn.uton = direction & 1;
298 pseudo_header->isdn.channel = channel;
299 phdr->pkt_encap = WTAP_ENCAP_V5_EF;
303 if(pkt_len > WTAP_MAX_PACKET_SIZE) {
304 *err = WTAP_ERR_BAD_FILE;
305 *err_info = g_strdup_printf("eyesdn: File has %u-byte packet, bigger than maximum of %u",
306 pkt_len, WTAP_MAX_PACKET_SIZE);
310 phdr->rec_type = REC_TYPE_PACKET;
311 phdr->presence_flags = WTAP_HAS_TS;
312 phdr->ts.secs = secs;
313 phdr->ts.nsecs = usecs * 1000;
314 phdr->caplen = pkt_len;
317 /* Make sure we have enough room for the packet */
318 ws_buffer_assure_space(buf, pkt_len);
320 pd = ws_buffer_start_ptr(buf);
321 if (!esc_read(fh, pd, pkt_len, err, err_info))
328 esc_write(wtap_dumper *wdh, const guint8 *buf, int len, int *err)
332 static const guint8 esc = 0xfe;
334 for(i=0; i<len; i++) {
336 if(byte == 0xff || byte == 0xfe) {
338 * Escape the frame delimiter and escape byte.
340 if (!wtap_dump_file_write(wdh, &esc, sizeof esc, err))
344 if (!wtap_dump_file_write(wdh, &byte, sizeof byte, err))
350 static gboolean eyesdn_dump(wtap_dumper *wdh,
351 const struct wtap_pkthdr *phdr,
352 const guint8 *pd, int *err, gchar **err_info);
354 gboolean eyesdn_dump_open(wtap_dumper *wdh, int *err)
356 wdh->subtype_write=eyesdn_dump;
358 if (!wtap_dump_file_write(wdh, eyesdn_hdr_magic,
359 EYESDN_HDR_MAGIC_SIZE, err))
361 wdh->bytes_dumped += EYESDN_HDR_MAGIC_SIZE;
366 int eyesdn_dump_can_write_encap(int encap)
369 case WTAP_ENCAP_ISDN:
370 case WTAP_ENCAP_LAYER1_EVENT:
371 case WTAP_ENCAP_DPNSS:
372 case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
373 case WTAP_ENCAP_LAPB:
374 case WTAP_ENCAP_MTP2_WITH_PHDR:
375 case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
376 case WTAP_ENCAP_PER_PACKET:
380 return WTAP_ERR_UNWRITABLE_ENCAP;
384 /* Write a record for a packet to a dump file.
385 * Returns TRUE on success, FALSE on failure. */
386 static gboolean eyesdn_dump(wtap_dumper *wdh,
387 const struct wtap_pkthdr *phdr,
388 const guint8 *pd, int *err, gchar **err_info _U_)
390 static const guint8 start_flag = 0xff;
391 const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
392 guint8 buf[EYESDN_HDR_LENGTH];
400 /* We can only write packet records. */
401 if (phdr->rec_type != REC_TYPE_PACKET) {
402 *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
406 /* Don't write out anything bigger than we can read.
407 * (The length field in packet headers is 16 bits, which
408 * imposes a hard limit.) */
409 if (phdr->caplen > 65535) {
410 *err = WTAP_ERR_PACKET_TOO_LARGE;
414 usecs=phdr->ts.nsecs/1000;
417 origin = pseudo_header->isdn.uton;
418 channel = pseudo_header->isdn.channel;
420 switch(phdr->pkt_encap) {
422 case WTAP_ENCAP_ISDN:
423 protocol=EYESDN_ENCAP_ISDN; /* set depending on decoder format and mode */
426 case WTAP_ENCAP_LAYER1_EVENT:
427 protocol=EYESDN_ENCAP_MSG;
430 case WTAP_ENCAP_DPNSS:
431 protocol=EYESDN_ENCAP_DPNSS;
435 case WTAP_ENCAP_DASS2:
436 protocol=EYESDN_ENCAP_DASS2;
440 case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
441 protocol=EYESDN_ENCAP_ATM;
445 case WTAP_ENCAP_LAPB:
446 protocol=EYESDN_ENCAP_LAPB;
449 case WTAP_ENCAP_MTP2_WITH_PHDR:
450 protocol=EYESDN_ENCAP_MTP2;
453 case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
454 protocol=EYESDN_ENCAP_BACNET;
457 case WTAP_ENCAP_V5_EF:
458 protocol=EYESDN_ENCAP_V5_EF;
462 *err=WTAP_ERR_UNWRITABLE_ENCAP;
466 phton24(&buf[0], usecs);
469 buf[4] = (guint8)(0xff & (secs >> 24));
470 buf[5] = (guint8)(0xff & (secs >> 16));
471 buf[6] = (guint8)(0xff & (secs >> 8));
472 buf[7] = (guint8)(0xff & (secs >> 0));
474 buf[8] = (guint8) channel;
475 buf[9] = (guint8) (origin?1:0) + (protocol << 1);
476 phtons(&buf[10], size);
479 if (!wtap_dump_file_write(wdh, &start_flag, sizeof start_flag, err))
481 if (!esc_write(wdh, buf, 12, err))
483 if (!esc_write(wdh, pd, size, err))
489 * Editor modelines - http://www.wireshark.org/tools/modelines.html
494 * indent-tabs-mode: t
497 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
498 * :indentSize=8:tabSize=8:noTabs=false: