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"
33 /* Magic number in Aethra PC108 files. */
36 static const guchar aethra_magic[MAGIC_SIZE] = {
37 'V', '0', '2', '0', '8'
40 /* Aethra file header. */
42 guchar magic[MAGIC_SIZE];
44 guchar sw_vers[60]; /* software version string, not null-terminated */
46 guint8 start_sec; /* seconds of capture start time */
47 guint8 start_min; /* minutes of capture start time */
48 guint8 start_hour; /* hour of capture start time */
49 guint8 unknown3[5007];
50 guint8 start_year[2]; /* year of capture start date */
51 guint8 start_month[2]; /* month of capture start date */
53 guint8 start_day[2]; /* day of capture start date */
55 guchar com_info[16]; /* COM port and speed, null-padded(?) */
57 guchar xxx_vers[41]; /* unknown version string (longer, null-padded?) */
60 /* Aethra record header. Yes, the alignment is weird.
61 All multi-byte fields are little-endian. */
62 struct aethrarec_hdr {
63 guint8 rec_size[2]; /* record length, not counting the length itself */
64 guint8 rec_type; /* record type */
65 guint8 timestamp[4]; /* milliseconds since start of capture */
66 guint8 flags; /* low-order bit: 0 = N->U, 1 = U->N */
72 * As the indications from the device and signalling messages appear not
73 * to have the 8th bit set, and at least some B-channel records do, we
74 * assume, for now, that the 8th bit indicates bearer information.
76 * 0x9F is the record type seen for B31 channel records; that might be
77 * 0x80|31, so, for now, we assume that if the 8th bit is set, the B
78 * channel number is in the low 7 bits.
80 #define AETHRA_BEARER 0x80 /* bearer information */
82 #define AETHRA_DEVICE 0x00 /* indication from the monitoring device */
83 #define AETHRA_ISDN_LINK 0x01 /* information from the ISDN link */
86 * In AETHRA_DEVICE records, the flags field has what appears to
87 * be a record subtype.
89 #define AETHRA_DEVICE_STOP_MONITOR 0x00 /* Stop Monitor */
90 #define AETHRA_DEVICE_START_MONITOR 0x04 /* Start Monitor */
91 #define AETHRA_DEVICE_ACTIVATION 0x05 /* Activation */
92 #define AETHRA_DEVICE_START_CAPTURE 0x5F /* Start Capture */
95 * In AETHRA_ISDN_LINK and bearer channel records, the flags field has
96 * a direction flag and possibly some other bits.
98 * In AETHRA_ISDN_LINK records, at least some of the other bits are
101 * In bearer channel records, there are records with data and
102 * "Constant Value" records with a single byte. Data has a
103 * flags value of 0x14 ORed with the direction flag, and Constant Value
104 * records have a flags value of 0x16 ORed with the direction flag.
105 * There are also records of an unknown type with 0x02, probably
106 * ORed with the direction flag.
108 #define AETHRA_N_TO_U 0x01 /* set for NT->TE */
110 #define AETHRA_ISDN_LINK_SUBTYPE 0xFE
111 #define AETHRA_ISDN_LINK_LAPD 0x00 /* LAPD frame */
112 #define AETHRA_ISDN_LINK_SA_BITS 0x2E /* 2048K PRI Sa bits (G.704 section 2.3.2) */
113 #define AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED 0x30 /* All Alarms Cleared */
119 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
120 gint64 *data_offset);
121 static gboolean aethra_seek_read(wtap *wth, gint64 seek_off,
122 union wtap_pseudo_header *pseudo_header, guint8 *pd, int length,
123 int *err, gchar **err_info);
124 static gboolean aethra_read_rec_header(FILE_T fh, struct aethrarec_hdr *hdr,
125 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
126 static gboolean aethra_read_rec_data(FILE_T fh, guint8 *pd, int length,
127 int *err, gchar **err_info);
129 int aethra_open(wtap *wth, int *err, gchar **err_info)
132 struct aethra_hdr hdr;
136 /* Read in the string that should be at the start of a "aethra" file */
137 errno = WTAP_ERR_CANT_READ;
138 bytes_read = file_read(hdr.magic, sizeof hdr.magic, wth->fh);
139 if (bytes_read != sizeof hdr.magic) {
140 *err = file_error(wth->fh, err_info);
145 wth->data_offset += sizeof hdr.magic;
147 if (memcmp(hdr.magic, aethra_magic, sizeof aethra_magic) != 0)
150 /* Read the rest of the header. */
151 errno = WTAP_ERR_CANT_READ;
152 bytes_read = file_read((char *)&hdr + sizeof hdr.magic,
153 sizeof hdr - sizeof hdr.magic, wth->fh);
154 if (bytes_read != sizeof hdr - sizeof hdr.magic) {
155 *err = file_error(wth->fh, err_info);
160 wth->data_offset += sizeof hdr - sizeof hdr.magic;
161 wth->file_type = WTAP_FILE_AETHRA;
162 aethra = (aethra_t *)g_malloc(sizeof(aethra_t));
163 wth->priv = (void *)aethra;
164 wth->subtype_read = aethra_read;
165 wth->subtype_seek_read = aethra_seek_read;
168 * Convert the time stamp to a "time_t".
170 tm.tm_year = pletohs(&hdr.start_year) - 1900;
171 tm.tm_mon = pletohs(&hdr.start_month) - 1;
172 tm.tm_mday = pletohs(&hdr.start_day);
173 tm.tm_hour = hdr.start_hour;
174 tm.tm_min = hdr.start_min;
175 tm.tm_sec = hdr.start_sec;
177 aethra->start = mktime(&tm);
180 * We've only seen ISDN files, so, for now, we treat all
183 wth->file_encap = WTAP_ENCAP_ISDN;
184 wth->snapshot_length = 0; /* not available in header */
185 wth->tsprecision = WTAP_FILE_TSPREC_MSEC;
190 static guint packet = 0;
193 /* Read the next packet */
194 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
197 aethra_t *aethra = (aethra_t *)wth->priv;
198 struct aethrarec_hdr hdr;
204 * Keep reading until we see an AETHRA_ISDN_LINK with a subtype
205 * of AETHRA_ISDN_LINK_LAPD record or get an end-of-file.
208 *data_offset = wth->data_offset;
210 /* Read record header. */
211 if (!aethra_read_rec_header(wth->fh, &hdr, &wth->pseudo_header,
215 rec_size = pletohs(hdr.rec_size);
216 if (rec_size < (sizeof hdr - sizeof hdr.rec_size)) {
217 /* The record is shorter than a record header. */
218 *err = WTAP_ERR_BAD_FILE;
219 *err_info = g_strdup_printf("aethra: File has %u-byte record, less than minimum of %u",
220 rec_size, (unsigned int)(sizeof hdr - sizeof hdr.rec_size));
223 wth->data_offset += sizeof hdr;
226 * XXX - if this is big, we might waste memory by
227 * growing the buffer to handle it.
229 packet_size = rec_size - (sizeof hdr - sizeof hdr.rec_size);
230 if (packet_size != 0) {
231 buffer_assure_space(wth->frame_buffer, packet_size);
232 if (!aethra_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
233 packet_size, err, err_info))
234 return FALSE; /* Read error */
235 wth->data_offset += packet_size;
240 switch (hdr.rec_type) {
242 case AETHRA_ISDN_LINK:
244 fprintf(stderr, "Packet %u: type 0x%02x (AETHRA_ISDN_LINK)\n",
245 packet, hdr.rec_type);
247 switch (hdr.flags & AETHRA_ISDN_LINK_SUBTYPE) {
249 case AETHRA_ISDN_LINK_LAPD:
251 * The data is a LAPD frame.
254 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_LAPD)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
258 case AETHRA_ISDN_LINK_SA_BITS:
260 * These records have one data byte, which
261 * has the Sa bits in the lower 5 bits.
263 * XXX - what about stuff other than 2048K
267 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_SA_BITS)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
271 case AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED:
273 * No data, just an "all alarms cleared"
277 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
283 fprintf(stderr, " subtype 0x%02x, packet_size %u, direction 0x%02x\n",
284 hdr.flags & AETHRA_ISDN_LINK_SUBTYPE, packet_size, hdr.flags & AETHRA_N_TO_U);
292 fprintf(stderr, "Packet %u: type 0x%02x, packet_size %u, flags 0x%02x\n",
293 packet, hdr.rec_type, packet_size, hdr.flags);
300 msecs = pletohl(hdr.timestamp);
301 wth->phdr.presence_flags = WTAP_HAS_TS;
302 wth->phdr.ts.secs = aethra->start + (msecs / 1000);
303 wth->phdr.ts.nsecs = (msecs % 1000) * 1000000;
304 wth->phdr.caplen = packet_size;
305 wth->phdr.len = packet_size;
311 aethra_seek_read(wtap *wth, gint64 seek_off,
312 union wtap_pseudo_header *pseudo_header, guint8 *pd, int length,
313 int *err, gchar **err_info)
315 struct aethrarec_hdr hdr;
317 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
320 if (!aethra_read_rec_header(wth->random_fh, &hdr, pseudo_header, err, err_info))
324 * Read the packet data.
326 if (!aethra_read_rec_data(wth->random_fh, pd, length, err, err_info))
327 return FALSE; /* failed */
333 aethra_read_rec_header(FILE_T fh, struct aethrarec_hdr *hdr,
334 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
338 /* Read record header. */
339 errno = WTAP_ERR_CANT_READ;
340 bytes_read = file_read(hdr, sizeof *hdr, fh);
341 if (bytes_read != sizeof *hdr) {
342 *err = file_error(fh, err_info);
343 if (*err == 0 && bytes_read != 0)
344 *err = WTAP_ERR_SHORT_READ;
348 pseudo_header->isdn.uton = !(hdr->flags & AETHRA_N_TO_U);
349 pseudo_header->isdn.channel = 0; /* XXX - D channel */
355 aethra_read_rec_data(FILE_T fh, guint8 *pd, int length, int *err,
360 errno = WTAP_ERR_CANT_READ;
361 bytes_read = file_read(pd, length, fh);
363 if (bytes_read != length) {
364 *err = file_error(fh, err_info);
366 *err = WTAP_ERR_SHORT_READ;