Witness: working witness.cnf
[wireshark-wip.git] / wiretap / aethra.c
blob2abb01f1af6d0fa5fa7abee5209f41124141c424
1 /* aethra.c
3 * $Id$
5 * Wiretap Library
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
24 #include <errno.h>
25 #include <string.h>
26 #include "wtap-int.h"
27 #include "file_wrappers.h"
28 #include "buffer.h"
29 #include "aethra.h"
31 /* Magic number in Aethra PC108 files. */
32 #define MAGIC_SIZE 5
34 static const guchar aethra_magic[MAGIC_SIZE] = {
35 'V', '0', '2', '0', '8'
38 /* Aethra file header. */
39 struct aethra_hdr {
40 guchar magic[MAGIC_SIZE];
41 guint8 unknown1[39];
42 guchar sw_vers[60]; /* software version string, not null-terminated */
43 guint8 unknown2[118];
44 guint8 start_sec; /* seconds of capture start time */
45 guint8 start_min; /* minutes of capture start time */
46 guint8 start_hour; /* hour of capture start time */
47 guint8 unknown3[5007];
48 guint8 start_year[2]; /* year of capture start date */
49 guint8 start_month[2]; /* month of capture start date */
50 guint8 unknown4[2];
51 guint8 start_day[2]; /* day of capture start date */
52 guint8 unknown5[8];
53 guchar com_info[16]; /* COM port and speed, null-padded(?) */
54 guint8 unknown6[107];
55 guchar xxx_vers[41]; /* unknown version string (longer, null-padded?) */
58 /* Aethra record header. Yes, the alignment is weird.
59 All multi-byte fields are little-endian. */
60 struct aethrarec_hdr {
61 guint8 rec_size[2]; /* record length, not counting the length itself */
62 guint8 rec_type; /* record type */
63 guint8 timestamp[4]; /* milliseconds since start of capture */
64 guint8 flags; /* low-order bit: 0 = N->U, 1 = U->N */
68 * Record types.
70 * As the indications from the device and signalling messages appear not
71 * to have the 8th bit set, and at least some B-channel records do, we
72 * assume, for now, that the 8th bit indicates bearer information.
74 * 0x9F is the record type seen for B31 channel records; that might be
75 * 0x80|31, so, for now, we assume that if the 8th bit is set, the B
76 * channel number is in the low 7 bits.
78 #define AETHRA_BEARER 0x80 /* bearer information */
80 #define AETHRA_DEVICE 0x00 /* indication from the monitoring device */
81 #define AETHRA_ISDN_LINK 0x01 /* information from the ISDN link */
84 * In AETHRA_DEVICE records, the flags field has what appears to
85 * be a record subtype.
87 #define AETHRA_DEVICE_STOP_MONITOR 0x00 /* Stop Monitor */
88 #define AETHRA_DEVICE_START_MONITOR 0x04 /* Start Monitor */
89 #define AETHRA_DEVICE_ACTIVATION 0x05 /* Activation */
90 #define AETHRA_DEVICE_START_CAPTURE 0x5F /* Start Capture */
93 * In AETHRA_ISDN_LINK and bearer channel records, the flags field has
94 * a direction flag and possibly some other bits.
96 * In AETHRA_ISDN_LINK records, at least some of the other bits are
97 * a subtype.
99 * In bearer channel records, there are records with data and
100 * "Constant Value" records with a single byte. Data has a
101 * flags value of 0x14 ORed with the direction flag, and Constant Value
102 * records have a flags value of 0x16 ORed with the direction flag.
103 * There are also records of an unknown type with 0x02, probably
104 * ORed with the direction flag.
106 #define AETHRA_U_TO_N 0x01 /* set for TE->NT */
108 #define AETHRA_ISDN_LINK_SUBTYPE 0xFE
109 #define AETHRA_ISDN_LINK_LAPD 0x00 /* LAPD frame */
110 #define AETHRA_ISDN_LINK_SA_BITS 0x2E /* 2048K PRI Sa bits (G.704 section 2.3.2) */
111 #define AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED 0x30 /* All Alarms Cleared */
113 typedef struct {
114 time_t start;
115 } aethra_t;
117 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
118 gint64 *data_offset);
119 static gboolean aethra_seek_read(wtap *wth, gint64 seek_off,
120 struct wtap_pkthdr *phdr, Buffer *buf, int length,
121 int *err, gchar **err_info);
122 static gboolean aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
123 struct wtap_pkthdr *phdr, int *err, gchar **err_info);
125 int aethra_open(wtap *wth, int *err, gchar **err_info)
127 int bytes_read;
128 struct aethra_hdr hdr;
129 struct tm tm;
130 aethra_t *aethra;
132 /* Read in the string that should be at the start of a "aethra" file */
133 errno = WTAP_ERR_CANT_READ;
134 bytes_read = file_read(hdr.magic, sizeof hdr.magic, wth->fh);
135 if (bytes_read != sizeof hdr.magic) {
136 *err = file_error(wth->fh, err_info);
137 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
138 return -1;
139 return 0;
142 if (memcmp(hdr.magic, aethra_magic, sizeof aethra_magic) != 0)
143 return 0;
145 /* Read the rest of the header. */
146 errno = WTAP_ERR_CANT_READ;
147 bytes_read = file_read((char *)&hdr + sizeof hdr.magic,
148 sizeof hdr - sizeof hdr.magic, wth->fh);
149 if (bytes_read != sizeof hdr - sizeof hdr.magic) {
150 *err = file_error(wth->fh, err_info);
151 if (*err == 0)
152 *err = WTAP_ERR_SHORT_READ;
153 return -1;
155 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_AETHRA;
156 aethra = (aethra_t *)g_malloc(sizeof(aethra_t));
157 wth->priv = (void *)aethra;
158 wth->subtype_read = aethra_read;
159 wth->subtype_seek_read = aethra_seek_read;
162 * Convert the time stamp to a "time_t".
164 tm.tm_year = pletohs(&hdr.start_year) - 1900;
165 tm.tm_mon = pletohs(&hdr.start_month) - 1;
166 tm.tm_mday = pletohs(&hdr.start_day);
167 tm.tm_hour = hdr.start_hour;
168 tm.tm_min = hdr.start_min;
169 tm.tm_sec = hdr.start_sec;
170 tm.tm_isdst = -1;
171 aethra->start = mktime(&tm);
174 * We've only seen ISDN files, so, for now, we treat all
175 * files as ISDN.
177 wth->file_encap = WTAP_ENCAP_ISDN;
178 wth->snapshot_length = 0; /* not available in header */
179 wth->tsprecision = WTAP_FILE_TSPREC_MSEC;
180 return 1;
183 #if 0
184 static guint packet = 0;
185 #endif
187 /* Read the next packet */
188 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
189 gint64 *data_offset)
191 struct aethrarec_hdr hdr;
194 * Keep reading until we see an AETHRA_ISDN_LINK with a subtype
195 * of AETHRA_ISDN_LINK_LAPD record or get an end-of-file.
197 for (;;) {
198 *data_offset = file_tell(wth->fh);
200 /* Read record header. */
201 if (!aethra_read_rec_header(wth, wth->fh, &hdr, &wth->phdr, err, err_info))
202 return FALSE;
205 * XXX - if this is big, we might waste memory by
206 * growing the buffer to handle it.
208 if (wth->phdr.caplen != 0) {
209 if (!wtap_read_packet_bytes(wth->fh, wth->frame_buffer,
210 wth->phdr.caplen, err, err_info))
211 return FALSE; /* Read error */
213 #if 0
214 packet++;
215 #endif
216 switch (hdr.rec_type) {
218 case AETHRA_ISDN_LINK:
219 #if 0
220 fprintf(stderr, "Packet %u: type 0x%02x (AETHRA_ISDN_LINK)\n",
221 packet, hdr.rec_type);
222 #endif
223 switch (hdr.flags & AETHRA_ISDN_LINK_SUBTYPE) {
225 case AETHRA_ISDN_LINK_LAPD:
227 * The data is a LAPD frame.
229 #if 0
230 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_LAPD)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
231 #endif
232 goto found;
234 case AETHRA_ISDN_LINK_SA_BITS:
236 * These records have one data byte, which
237 * has the Sa bits in the lower 5 bits.
239 * XXX - what about stuff other than 2048K
240 * PRI lines?
242 #if 0
243 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_SA_BITS)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
244 #endif
245 break;
247 case AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED:
249 * No data, just an "all alarms cleared"
250 * indication.
252 #if 0
253 fprintf(stderr, " subtype 0x%02x (AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
254 #endif
255 break;
257 default:
258 #if 0
259 fprintf(stderr, " subtype 0x%02x, packet_size %u, direction 0x%02x\n",
260 hdr.flags & AETHRA_ISDN_LINK_SUBTYPE, wth->phdr.caplen, hdr.flags & AETHRA_U_TO_N);
261 #endif
262 break;
264 break;
266 default:
267 #if 0
268 fprintf(stderr, "Packet %u: type 0x%02x, packet_size %u, flags 0x%02x\n",
269 packet, hdr.rec_type, wth->phdr.caplen, hdr.flags);
270 #endif
271 break;
275 found:
276 return TRUE;
279 static gboolean
280 aethra_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
281 Buffer *buf, int length, int *err, gchar **err_info)
283 struct aethrarec_hdr hdr;
285 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
286 return FALSE;
288 if (!aethra_read_rec_header(wth, wth->random_fh, &hdr, phdr, err, err_info))
289 return FALSE;
292 * Read the packet data.
294 if (!wtap_read_packet_bytes(wth->random_fh, buf, length, err, err_info))
295 return FALSE; /* failed */
297 return TRUE;
300 static gboolean
301 aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
302 struct wtap_pkthdr *phdr, int *err, gchar **err_info)
304 aethra_t *aethra = (aethra_t *)wth->priv;
305 int bytes_read;
306 guint32 rec_size;
307 guint32 packet_size;
308 guint32 msecs;
310 /* Read record header. */
311 errno = WTAP_ERR_CANT_READ;
312 bytes_read = file_read(hdr, sizeof *hdr, fh);
313 if (bytes_read != sizeof *hdr) {
314 *err = file_error(fh, err_info);
315 if (*err == 0 && bytes_read != 0)
316 *err = WTAP_ERR_SHORT_READ;
317 return FALSE;
320 rec_size = pletohs(hdr->rec_size);
321 if (rec_size < (sizeof *hdr - sizeof hdr->rec_size)) {
322 /* The record is shorter than a record header. */
323 *err = WTAP_ERR_BAD_FILE;
324 *err_info = g_strdup_printf("aethra: File has %u-byte record, less than minimum of %u",
325 rec_size,
326 (unsigned int)(sizeof *hdr - sizeof hdr->rec_size));
327 return FALSE;
330 packet_size = rec_size - (guint32)(sizeof *hdr - sizeof hdr->rec_size);
332 msecs = pletohl(hdr->timestamp);
333 phdr->presence_flags = WTAP_HAS_TS;
334 phdr->ts.secs = aethra->start + (msecs / 1000);
335 phdr->ts.nsecs = (msecs % 1000) * 1000000;
336 phdr->caplen = packet_size;
337 phdr->len = packet_size;
338 phdr->pseudo_header.isdn.uton = (hdr->flags & AETHRA_U_TO_N);
339 phdr->pseudo_header.isdn.channel = 0; /* XXX - D channel */
341 return TRUE;