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"
28 /* Magic number in Aethra PC108 files. */
31 static const guchar aethra_magic[MAGIC_SIZE] = {
32 'V', '0', '2', '0', '8'
35 /* Aethra file header. */
37 guchar magic[MAGIC_SIZE];
39 guchar sw_vers[60]; /* software version string, not null-terminated */
41 guint8 start_sec; /* seconds of capture start time */
42 guint8 start_min; /* minutes of capture start time */
43 guint8 start_hour; /* hour of capture start time */
44 guint8 unknown3[5007];
45 guint8 start_year[2]; /* year of capture start date */
46 guint8 start_month[2]; /* month of capture start date */
48 guint8 start_day[2]; /* day of capture start date */
50 guchar com_info[16]; /* COM port and speed, null-padded(?) */
52 guchar xxx_vers[41]; /* unknown version string (longer, null-padded?) */
55 /* Aethra record header. Yes, the alignment is weird.
56 All multi-byte fields are little-endian. */
57 struct aethrarec_hdr {
58 guint8 rec_size[2]; /* record length, not counting the length itself */
59 guint8 rec_type; /* record type */
60 guint8 timestamp[4]; /* milliseconds since start of capture */
61 guint8 flags; /* low-order bit: 0 = N->U, 1 = U->N */
67 * As the indications from the device and signalling messages appear not
68 * to have the 8th bit set, and at least some B-channel records do, we
69 * assume, for now, that the 8th bit indicates bearer information.
71 * 0x9F is the record type seen for B31 channel records; that might be
72 * 0x80|31, so, for now, we assume that if the 8th bit is set, the B
73 * channel number is in the low 7 bits.
75 #define AETHRA_BEARER 0x80 /* bearer information */
77 #define AETHRA_DEVICE 0x00 /* indication from the monitoring device */
78 #define AETHRA_ISDN_LINK 0x01 /* information from the ISDN link */
81 * In AETHRA_DEVICE records, the flags field has what appears to
82 * be a record subtype.
84 #define AETHRA_DEVICE_STOP_MONITOR 0x00 /* Stop Monitor */
85 #define AETHRA_DEVICE_START_MONITOR 0x04 /* Start Monitor */
86 #define AETHRA_DEVICE_ACTIVATION 0x05 /* Activation */
87 #define AETHRA_DEVICE_START_CAPTURE 0x5F /* Start Capture */
90 * In AETHRA_ISDN_LINK and bearer channel records, the flags field has
91 * a direction flag and possibly some other bits.
93 * In AETHRA_ISDN_LINK records, at least some of the other bits are
96 * In bearer channel records, there are records with data and
97 * "Constant Value" records with a single byte. Data has a
98 * flags value of 0x14 ORed with the direction flag, and Constant Value
99 * records have a flags value of 0x16 ORed with the direction flag.
100 * There are also records of an unknown type with 0x02, probably
101 * ORed with the direction flag.
103 #define AETHRA_U_TO_N 0x01 /* set for TE->NT */
105 #define AETHRA_ISDN_LINK_SUBTYPE 0xFE
106 #define AETHRA_ISDN_LINK_LAPD 0x00 /* LAPD frame */
107 #define AETHRA_ISDN_LINK_SA_BITS 0x2E /* 2048K PRI Sa bits (G.704 section 2.3.2) */
108 #define AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED 0x30 /* All Alarms Cleared */
114 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
115 gint64 *data_offset);
116 static gboolean aethra_seek_read(wtap *wth, gint64 seek_off,
117 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
118 static gboolean aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
119 struct wtap_pkthdr *phdr, int *err, gchar **err_info);
121 wtap_open_return_val aethra_open(wtap *wth, int *err, gchar **err_info)
123 struct aethra_hdr hdr;
127 /* Read in the string that should be at the start of a "aethra" file */
128 if (!wtap_read_bytes(wth->fh, hdr.magic, sizeof hdr.magic, err,
130 if (*err != WTAP_ERR_SHORT_READ)
131 return WTAP_OPEN_ERROR;
132 return WTAP_OPEN_NOT_MINE;
135 if (memcmp(hdr.magic, aethra_magic, sizeof aethra_magic) != 0)
136 return WTAP_OPEN_NOT_MINE;
138 /* Read the rest of the header. */
139 if (!wtap_read_bytes(wth->fh, (char *)&hdr + sizeof hdr.magic,
140 sizeof hdr - sizeof hdr.magic, err, err_info))
141 return WTAP_OPEN_ERROR;
142 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_AETHRA;
143 aethra = (aethra_t *)g_malloc(sizeof(aethra_t));
144 wth->priv = (void *)aethra;
145 wth->subtype_read = aethra_read;
146 wth->subtype_seek_read = aethra_seek_read;
149 * Convert the time stamp to a "time_t".
151 tm.tm_year = pletoh16(&hdr.start_year) - 1900;
152 tm.tm_mon = pletoh16(&hdr.start_month) - 1;
153 tm.tm_mday = pletoh16(&hdr.start_day);
154 tm.tm_hour = hdr.start_hour;
155 tm.tm_min = hdr.start_min;
156 tm.tm_sec = hdr.start_sec;
158 aethra->start = mktime(&tm);
161 * We've only seen ISDN files, so, for now, we treat all
164 wth->file_encap = WTAP_ENCAP_ISDN;
165 wth->snapshot_length = 0; /* not available in header */
166 wth->file_tsprec = WTAP_TSPREC_MSEC;
167 return WTAP_OPEN_MINE;
171 static guint packet = 0;
174 /* Read the next packet */
175 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
178 struct aethrarec_hdr hdr;
181 * Keep reading until we see an AETHRA_ISDN_LINK with a subtype
182 * of AETHRA_ISDN_LINK_LAPD record or get an end-of-file.
185 *data_offset = file_tell(wth->fh);
187 /* Read record header. */
188 if (!aethra_read_rec_header(wth, wth->fh, &hdr, &wth->phdr, err, err_info))
192 * XXX - if this is big, we might waste memory by
193 * growing the buffer to handle it.
195 if (wth->phdr.caplen != 0) {
196 if (!wtap_read_packet_bytes(wth->fh, wth->frame_buffer,
197 wth->phdr.caplen, err, err_info))
198 return FALSE; /* Read error */
203 switch (hdr.rec_type) {
205 case AETHRA_ISDN_LINK:
207 fprintf(stderr, "Packet %u: type 0x%02x (AETHRA_ISDN_LINK)\n",
208 packet, hdr.rec_type);
210 switch (hdr.flags & AETHRA_ISDN_LINK_SUBTYPE) {
212 case AETHRA_ISDN_LINK_LAPD:
214 * The data is a LAPD frame.
217 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_LAPD)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
221 case AETHRA_ISDN_LINK_SA_BITS:
223 * These records have one data byte, which
224 * has the Sa bits in the lower 5 bits.
226 * XXX - what about stuff other than 2048K
230 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_SA_BITS)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
234 case AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED:
236 * No data, just an "all alarms cleared"
240 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
246 fprintf(stderr, " subtype 0x%02x, packet_size %u, direction 0x%02x\n",
247 hdr.flags & AETHRA_ISDN_LINK_SUBTYPE, wth->phdr.caplen, hdr.flags & AETHRA_U_TO_N);
255 fprintf(stderr, "Packet %u: type 0x%02x, packet_size %u, flags 0x%02x\n",
256 packet, hdr.rec_type, wth->phdr.caplen, hdr.flags);
267 aethra_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
268 Buffer *buf, int *err, gchar **err_info)
270 struct aethrarec_hdr hdr;
272 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
275 if (!aethra_read_rec_header(wth, wth->random_fh, &hdr, phdr, err,
278 *err = WTAP_ERR_SHORT_READ;
283 * Read the packet data.
285 if (!wtap_read_packet_bytes(wth->random_fh, buf, phdr->caplen, err, err_info))
286 return FALSE; /* failed */
292 aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
293 struct wtap_pkthdr *phdr, int *err, gchar **err_info)
295 aethra_t *aethra = (aethra_t *)wth->priv;
300 /* Read record header. */
301 if (!wtap_read_bytes_or_eof(fh, hdr, sizeof *hdr, err, err_info))
304 rec_size = pletoh16(hdr->rec_size);
305 if (rec_size < (sizeof *hdr - sizeof hdr->rec_size)) {
306 /* The record is shorter than a record header. */
307 *err = WTAP_ERR_BAD_FILE;
308 *err_info = g_strdup_printf("aethra: File has %u-byte record, less than minimum of %u",
310 (unsigned int)(sizeof *hdr - sizeof hdr->rec_size));
313 if (rec_size > WTAP_MAX_PACKET_SIZE) {
315 * Probably a corrupt capture file; return an error,
316 * so that our caller doesn't blow up trying to allocate
317 * space for an immensely-large packet.
319 *err = WTAP_ERR_BAD_FILE;
320 *err_info = g_strdup_printf("aethra: File has %u-byte packet, bigger than maximum of %u",
321 rec_size, WTAP_MAX_PACKET_SIZE);
325 packet_size = rec_size - (guint32)(sizeof *hdr - sizeof hdr->rec_size);
327 msecs = pletoh32(hdr->timestamp);
328 phdr->rec_type = REC_TYPE_PACKET;
329 phdr->presence_flags = WTAP_HAS_TS;
330 phdr->ts.secs = aethra->start + (msecs / 1000);
331 phdr->ts.nsecs = (msecs % 1000) * 1000000;
332 phdr->caplen = packet_size;
333 phdr->len = packet_size;
334 phdr->pseudo_header.isdn.uton = (hdr->flags & AETHRA_U_TO_N);
335 phdr->pseudo_header.isdn.channel = 0; /* XXX - D channel */
341 * Editor modelines - http://www.wireshark.org/tools/modelines.html
346 * indent-tabs-mode: t
349 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
350 * :indentSize=8:tabSize=8:noTabs=false: