Add a presence flag field to the packet information structure filled in
[metze/wireshark/wip.git] / wiretap / aethra.c
1 /* aethra.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <errno.h>
27 #include <string.h>
28 #include "wtap-int.h"
29 #include "file_wrappers.h"
30 #include "buffer.h"
31 #include "aethra.h"
32
33 /* Magic number in Aethra PC108 files. */
34 #define MAGIC_SIZE      5
35
36 static const guchar aethra_magic[MAGIC_SIZE] = {
37         'V', '0', '2', '0', '8'
38 };
39
40 /* Aethra file header. */
41 struct aethra_hdr {
42         guchar  magic[MAGIC_SIZE];
43         guint8  unknown1[39];
44         guchar  sw_vers[60];    /* software version string, not null-terminated */
45         guint8  unknown2[118];
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 */
52         guint8  unknown4[2];
53         guint8  start_day[2];   /* day of capture start date */
54         guint8  unknown5[8];
55         guchar  com_info[16];   /* COM port and speed, null-padded(?) */
56         guint8  unknown6[107];
57         guchar  xxx_vers[41];   /* unknown version string (longer, null-padded?) */
58 };
59
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 */
67 };
68
69 /*
70  * Record types.
71  *
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.
75  *
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.
79  */
80 #define AETHRA_BEARER           0x80    /* bearer information */
81
82 #define AETHRA_DEVICE           0x00    /* indication from the monitoring device */
83 #define AETHRA_ISDN_LINK        0x01    /* information from the ISDN link */
84
85 /*
86  * In AETHRA_DEVICE records, the flags field has what appears to
87  * be a record subtype.
88  */
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 */
93
94 /*
95  * In AETHRA_ISDN_LINK and bearer channel records, the flags field has
96  * a direction flag and possibly some other bits.
97  *
98  * In AETHRA_ISDN_LINK records, at least some of the other bits are
99  * a subtype.
100  *
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.
107  */
108 #define AETHRA_N_TO_U                           0x01    /* set for NT->TE */
109
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 */
114
115 typedef struct {
116         time_t  start;
117 } aethra_t;
118
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);
128
129 int aethra_open(wtap *wth, int *err, gchar **err_info)
130 {
131         int bytes_read;
132         struct aethra_hdr hdr;
133         struct tm tm;
134         aethra_t *aethra;
135
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);
141                 if (*err != 0)
142                         return -1;
143                 return 0;
144         }
145         wth->data_offset += sizeof hdr.magic;
146
147         if (memcmp(hdr.magic, aethra_magic, sizeof aethra_magic) != 0)
148                 return 0;
149
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);
156                 if (*err != 0)
157                         return -1;
158                 return 0;
159         }
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;
166
167         /*
168          * Convert the time stamp to a "time_t".
169          */
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;
176         tm.tm_isdst = -1;
177         aethra->start = mktime(&tm);
178
179         /*
180          * We've only seen ISDN files, so, for now, we treat all
181          * files as ISDN.
182          */
183         wth->file_encap = WTAP_ENCAP_ISDN;
184         wth->snapshot_length = 0;       /* not available in header */
185         wth->tsprecision = WTAP_FILE_TSPREC_MSEC;
186         return 1;
187 }
188
189 #if 0
190 static guint packet = 0;
191 #endif
192
193 /* Read the next packet */
194 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
195     gint64 *data_offset)
196 {
197         aethra_t *aethra = (aethra_t *)wth->priv;
198         struct aethrarec_hdr hdr;
199         guint32 rec_size;
200         guint32 packet_size;
201         guint32 msecs;
202
203         /*
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.
206          */
207         for (;;) {
208                 *data_offset = wth->data_offset;
209
210                 /* Read record header. */
211                 if (!aethra_read_rec_header(wth->fh, &hdr, &wth->pseudo_header,
212                     err, err_info))
213                         return FALSE;
214
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));
221                         return FALSE;
222                 }
223                 wth->data_offset += sizeof hdr;
224
225                 /*
226                  * XXX - if this is big, we might waste memory by
227                  * growing the buffer to handle it.
228                  */
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;
236                 }
237 #if 0
238 packet++;
239 #endif
240                 switch (hdr.rec_type) {
241
242                 case AETHRA_ISDN_LINK:
243 #if 0
244 fprintf(stderr, "Packet %u: type 0x%02x (AETHRA_ISDN_LINK)\n",
245 packet, hdr.rec_type);
246 #endif
247                         switch (hdr.flags & AETHRA_ISDN_LINK_SUBTYPE) {
248
249                         case AETHRA_ISDN_LINK_LAPD:
250                                 /*
251                                  * The data is a LAPD frame.
252                                  */
253 #if 0
254 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_LAPD)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
255 #endif
256                                 goto found;
257
258                         case AETHRA_ISDN_LINK_SA_BITS:
259                                 /*
260                                  * These records have one data byte, which
261                                  * has the Sa bits in the lower 5 bits.
262                                  *
263                                  * XXX - what about stuff other than 2048K
264                                  * PRI lines?
265                                  */
266 #if 0
267 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_SA_BITS)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
268 #endif
269                                 break;
270
271                         case AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED:
272                                 /*
273                                  * No data, just an "all alarms cleared"
274                                  * indication.
275                                  */
276 #if 0
277 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
278 #endif
279                                 break;
280
281                         default:
282 #if 0
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);
285 #endif
286                                 break;
287                         }
288                         break;
289
290                 default:
291 #if 0
292 fprintf(stderr, "Packet %u: type 0x%02x, packet_size %u, flags 0x%02x\n",
293 packet, hdr.rec_type, packet_size, hdr.flags);
294 #endif
295                         break;
296                 }
297         }
298
299 found:
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;
306
307         return TRUE;
308 }
309
310 static gboolean
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)
314 {
315         struct aethrarec_hdr hdr;
316
317         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
318                 return FALSE;
319
320         if (!aethra_read_rec_header(wth->random_fh, &hdr, pseudo_header, err, err_info))
321                 return FALSE;
322
323         /*
324          * Read the packet data.
325          */
326         if (!aethra_read_rec_data(wth->random_fh, pd, length, err, err_info))
327                 return FALSE;   /* failed */
328
329         return TRUE;
330 }
331
332 static gboolean
333 aethra_read_rec_header(FILE_T fh, struct aethrarec_hdr *hdr,
334     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
335 {
336         int     bytes_read;
337
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;
345                 return FALSE;
346         }
347
348         pseudo_header->isdn.uton = !(hdr->flags & AETHRA_N_TO_U);
349         pseudo_header->isdn.channel = 0;        /* XXX - D channel */
350
351         return TRUE;
352 }
353
354 static gboolean
355 aethra_read_rec_data(FILE_T fh, guint8 *pd, int length, int *err,
356     gchar **err_info)
357 {
358         int     bytes_read;
359
360         errno = WTAP_ERR_CANT_READ;
361         bytes_read = file_read(pd, length, fh);
362
363         if (bytes_read != length) {
364                 *err = file_error(fh, err_info);
365                 if (*err == 0)
366                         *err = WTAP_ERR_SHORT_READ;
367                 return FALSE;
368         }
369         return TRUE;
370 }