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.
25 #include "file_wrappers.h"
26 #include <wsutil/buffer.h>
29 /* Magic number in Aethra PC108 files. */
32 static const guchar aethra_magic[MAGIC_SIZE] = {
33 'V', '0', '2', '0', '8'
36 /* Aethra file header. */
38 guchar magic[MAGIC_SIZE];
40 guchar sw_vers[60]; /* software version string, not null-terminated */
42 guint8 start_sec; /* seconds of capture start time */
43 guint8 start_min; /* minutes of capture start time */
44 guint8 start_hour; /* hour of capture start time */
45 guint8 unknown3[5007];
46 guint8 start_year[2]; /* year of capture start date */
47 guint8 start_month[2]; /* month of capture start date */
49 guint8 start_day[2]; /* day of capture start date */
51 guchar com_info[16]; /* COM port and speed, null-padded(?) */
53 guchar xxx_vers[41]; /* unknown version string (longer, null-padded?) */
56 /* Aethra record header. Yes, the alignment is weird.
57 All multi-byte fields are little-endian. */
58 struct aethrarec_hdr {
59 guint8 rec_size[2]; /* record length, not counting the length itself */
60 guint8 rec_type; /* record type */
61 guint8 timestamp[4]; /* milliseconds since start of capture */
62 guint8 flags; /* low-order bit: 0 = N->U, 1 = U->N */
68 * As the indications from the device and signalling messages appear not
69 * to have the 8th bit set, and at least some B-channel records do, we
70 * assume, for now, that the 8th bit indicates bearer information.
72 * 0x9F is the record type seen for B31 channel records; that might be
73 * 0x80|31, so, for now, we assume that if the 8th bit is set, the B
74 * channel number is in the low 7 bits.
76 #define AETHRA_BEARER 0x80 /* bearer information */
78 #define AETHRA_DEVICE 0x00 /* indication from the monitoring device */
79 #define AETHRA_ISDN_LINK 0x01 /* information from the ISDN link */
82 * In AETHRA_DEVICE records, the flags field has what appears to
83 * be a record subtype.
85 #define AETHRA_DEVICE_STOP_MONITOR 0x00 /* Stop Monitor */
86 #define AETHRA_DEVICE_START_MONITOR 0x04 /* Start Monitor */
87 #define AETHRA_DEVICE_ACTIVATION 0x05 /* Activation */
88 #define AETHRA_DEVICE_START_CAPTURE 0x5F /* Start Capture */
91 * In AETHRA_ISDN_LINK and bearer channel records, the flags field has
92 * a direction flag and possibly some other bits.
94 * In AETHRA_ISDN_LINK records, at least some of the other bits are
97 * In bearer channel records, there are records with data and
98 * "Constant Value" records with a single byte. Data has a
99 * flags value of 0x14 ORed with the direction flag, and Constant Value
100 * records have a flags value of 0x16 ORed with the direction flag.
101 * There are also records of an unknown type with 0x02, probably
102 * ORed with the direction flag.
104 #define AETHRA_U_TO_N 0x01 /* set for TE->NT */
106 #define AETHRA_ISDN_LINK_SUBTYPE 0xFE
107 #define AETHRA_ISDN_LINK_LAPD 0x00 /* LAPD frame */
108 #define AETHRA_ISDN_LINK_SA_BITS 0x2E /* 2048K PRI Sa bits (G.704 section 2.3.2) */
109 #define AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED 0x30 /* All Alarms Cleared */
115 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
116 gint64 *data_offset);
117 static gboolean aethra_seek_read(wtap *wth, gint64 seek_off,
118 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
119 static gboolean aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
120 struct wtap_pkthdr *phdr, int *err, gchar **err_info);
122 int aethra_open(wtap *wth, int *err, gchar **err_info)
124 struct aethra_hdr hdr;
128 /* Read in the string that should be at the start of a "aethra" file */
129 if (!wtap_read_bytes(wth->fh, hdr.magic, sizeof hdr.magic, err,
131 if (*err != WTAP_ERR_SHORT_READ)
136 if (memcmp(hdr.magic, aethra_magic, sizeof aethra_magic) != 0)
139 /* Read the rest of the header. */
140 if (!wtap_read_bytes(wth->fh, (char *)&hdr + sizeof hdr.magic,
141 sizeof hdr - sizeof hdr.magic, err, err_info))
143 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_AETHRA;
144 aethra = (aethra_t *)g_malloc(sizeof(aethra_t));
145 wth->priv = (void *)aethra;
146 wth->subtype_read = aethra_read;
147 wth->subtype_seek_read = aethra_seek_read;
150 * Convert the time stamp to a "time_t".
152 tm.tm_year = pletoh16(&hdr.start_year) - 1900;
153 tm.tm_mon = pletoh16(&hdr.start_month) - 1;
154 tm.tm_mday = pletoh16(&hdr.start_day);
155 tm.tm_hour = hdr.start_hour;
156 tm.tm_min = hdr.start_min;
157 tm.tm_sec = hdr.start_sec;
159 aethra->start = mktime(&tm);
162 * We've only seen ISDN files, so, for now, we treat all
165 wth->file_encap = WTAP_ENCAP_ISDN;
166 wth->snapshot_length = 0; /* not available in header */
167 wth->file_tsprec = WTAP_TSPREC_MSEC;
172 static guint packet = 0;
175 /* Read the next packet */
176 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
179 struct aethrarec_hdr hdr;
182 * Keep reading until we see an AETHRA_ISDN_LINK with a subtype
183 * of AETHRA_ISDN_LINK_LAPD record or get an end-of-file.
186 *data_offset = file_tell(wth->fh);
188 /* Read record header. */
189 if (!aethra_read_rec_header(wth, wth->fh, &hdr, &wth->phdr, err, err_info))
193 * XXX - if this is big, we might waste memory by
194 * growing the buffer to handle it.
196 if (wth->phdr.caplen != 0) {
197 if (!wtap_read_packet_bytes(wth->fh, wth->frame_buffer,
198 wth->phdr.caplen, err, err_info))
199 return FALSE; /* Read error */
204 switch (hdr.rec_type) {
206 case AETHRA_ISDN_LINK:
208 fprintf(stderr, "Packet %u: type 0x%02x (AETHRA_ISDN_LINK)\n",
209 packet, hdr.rec_type);
211 switch (hdr.flags & AETHRA_ISDN_LINK_SUBTYPE) {
213 case AETHRA_ISDN_LINK_LAPD:
215 * The data is a LAPD frame.
218 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_LAPD)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
222 case AETHRA_ISDN_LINK_SA_BITS:
224 * These records have one data byte, which
225 * has the Sa bits in the lower 5 bits.
227 * XXX - what about stuff other than 2048K
231 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_SA_BITS)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
235 case AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED:
237 * No data, just an "all alarms cleared"
241 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
247 fprintf(stderr, " subtype 0x%02x, packet_size %u, direction 0x%02x\n",
248 hdr.flags & AETHRA_ISDN_LINK_SUBTYPE, wth->phdr.caplen, hdr.flags & AETHRA_U_TO_N);
256 fprintf(stderr, "Packet %u: type 0x%02x, packet_size %u, flags 0x%02x\n",
257 packet, hdr.rec_type, wth->phdr.caplen, hdr.flags);
268 aethra_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
269 Buffer *buf, int *err, gchar **err_info)
271 struct aethrarec_hdr hdr;
273 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
276 if (!aethra_read_rec_header(wth, wth->random_fh, &hdr, phdr, err,
279 *err = WTAP_ERR_SHORT_READ;
284 * Read the packet data.
286 if (!wtap_read_packet_bytes(wth->random_fh, buf, phdr->caplen, err, err_info))
287 return FALSE; /* failed */
293 aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
294 struct wtap_pkthdr *phdr, int *err, gchar **err_info)
296 aethra_t *aethra = (aethra_t *)wth->priv;
301 /* Read record header. */
302 if (!wtap_read_bytes_or_eof(fh, hdr, sizeof *hdr, err, err_info))
305 rec_size = pletoh16(hdr->rec_size);
306 if (rec_size < (sizeof *hdr - sizeof hdr->rec_size)) {
307 /* The record is shorter than a record header. */
308 *err = WTAP_ERR_BAD_FILE;
309 *err_info = g_strdup_printf("aethra: File has %u-byte record, less than minimum of %u",
311 (unsigned int)(sizeof *hdr - sizeof hdr->rec_size));
315 packet_size = rec_size - (guint32)(sizeof *hdr - sizeof hdr->rec_size);
317 msecs = pletoh32(hdr->timestamp);
318 phdr->rec_type = REC_TYPE_PACKET;
319 phdr->presence_flags = WTAP_HAS_TS;
320 phdr->ts.secs = aethra->start + (msecs / 1000);
321 phdr->ts.nsecs = (msecs % 1000) * 1000000;
322 phdr->caplen = packet_size;
323 phdr->len = packet_size;
324 phdr->pseudo_header.isdn.uton = (hdr->flags & AETHRA_U_TO_N);
325 phdr->pseudo_header.isdn.channel = 0; /* XXX - D channel */