No need for WTAP_ERR_CANT_READ.
[metze/wireshark/wip.git] / wiretap / aethra.c
1 /* aethra.c
2  *
3  * Wiretap Library
4  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22 #include <errno.h>
23 #include <string.h>
24 #include "wtap-int.h"
25 #include "file_wrappers.h"
26 #include <wsutil/buffer.h>
27 #include "aethra.h"
28
29 /* Magic number in Aethra PC108 files. */
30 #define MAGIC_SIZE      5
31
32 static const guchar aethra_magic[MAGIC_SIZE] = {
33         'V', '0', '2', '0', '8'
34 };
35
36 /* Aethra file header. */
37 struct aethra_hdr {
38         guchar  magic[MAGIC_SIZE];
39         guint8  unknown1[39];
40         guchar  sw_vers[60];    /* software version string, not null-terminated */
41         guint8  unknown2[118];
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 */
48         guint8  unknown4[2];
49         guint8  start_day[2];   /* day of capture start date */
50         guint8  unknown5[8];
51         guchar  com_info[16];   /* COM port and speed, null-padded(?) */
52         guint8  unknown6[107];
53         guchar  xxx_vers[41];   /* unknown version string (longer, null-padded?) */
54 };
55
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 */
63 };
64
65 /*
66  * Record types.
67  *
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.
71  *
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.
75  */
76 #define AETHRA_BEARER           0x80    /* bearer information */
77
78 #define AETHRA_DEVICE           0x00    /* indication from the monitoring device */
79 #define AETHRA_ISDN_LINK        0x01    /* information from the ISDN link */
80
81 /*
82  * In AETHRA_DEVICE records, the flags field has what appears to
83  * be a record subtype.
84  */
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 */
89
90 /*
91  * In AETHRA_ISDN_LINK and bearer channel records, the flags field has
92  * a direction flag and possibly some other bits.
93  *
94  * In AETHRA_ISDN_LINK records, at least some of the other bits are
95  * a subtype.
96  *
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.
103  */
104 #define AETHRA_U_TO_N                           0x01    /* set for TE->NT */
105
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 */
110
111 typedef struct {
112         time_t  start;
113 } aethra_t;
114
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);
121
122 int aethra_open(wtap *wth, int *err, gchar **err_info)
123 {
124         struct aethra_hdr hdr;
125         struct tm tm;
126         aethra_t *aethra;
127
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,
130             err_info)) {
131                 if (*err != WTAP_ERR_SHORT_READ)
132                         return -1;
133                 return 0;
134         }
135
136         if (memcmp(hdr.magic, aethra_magic, sizeof aethra_magic) != 0)
137                 return 0;
138
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))
142                 return -1;
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;
148
149         /*
150          * Convert the time stamp to a "time_t".
151          */
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;
158         tm.tm_isdst = -1;
159         aethra->start = mktime(&tm);
160
161         /*
162          * We've only seen ISDN files, so, for now, we treat all
163          * files as ISDN.
164          */
165         wth->file_encap = WTAP_ENCAP_ISDN;
166         wth->snapshot_length = 0;       /* not available in header */
167         wth->file_tsprec = WTAP_TSPREC_MSEC;
168         return 1;
169 }
170
171 #if 0
172 static guint packet = 0;
173 #endif
174
175 /* Read the next packet */
176 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
177     gint64 *data_offset)
178 {
179         struct aethrarec_hdr hdr;
180
181         /*
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.
184          */
185         for (;;) {
186                 *data_offset = file_tell(wth->fh);
187
188                 /* Read record header. */
189                 if (!aethra_read_rec_header(wth, wth->fh, &hdr, &wth->phdr, err, err_info))
190                         return FALSE;
191
192                 /*
193                  * XXX - if this is big, we might waste memory by
194                  * growing the buffer to handle it.
195                  */
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 */
200                 }
201 #if 0
202 packet++;
203 #endif
204                 switch (hdr.rec_type) {
205
206                 case AETHRA_ISDN_LINK:
207 #if 0
208 fprintf(stderr, "Packet %u: type 0x%02x (AETHRA_ISDN_LINK)\n",
209 packet, hdr.rec_type);
210 #endif
211                         switch (hdr.flags & AETHRA_ISDN_LINK_SUBTYPE) {
212
213                         case AETHRA_ISDN_LINK_LAPD:
214                                 /*
215                                  * The data is a LAPD frame.
216                                  */
217 #if 0
218 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_LAPD)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
219 #endif
220                                 goto found;
221
222                         case AETHRA_ISDN_LINK_SA_BITS:
223                                 /*
224                                  * These records have one data byte, which
225                                  * has the Sa bits in the lower 5 bits.
226                                  *
227                                  * XXX - what about stuff other than 2048K
228                                  * PRI lines?
229                                  */
230 #if 0
231 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_SA_BITS)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
232 #endif
233                                 break;
234
235                         case AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED:
236                                 /*
237                                  * No data, just an "all alarms cleared"
238                                  * indication.
239                                  */
240 #if 0
241 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
242 #endif
243                                 break;
244
245                         default:
246 #if 0
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);
249 #endif
250                                 break;
251                         }
252                         break;
253
254                 default:
255 #if 0
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);
258 #endif
259                         break;
260                 }
261         }
262
263 found:
264         return TRUE;
265 }
266
267 static gboolean
268 aethra_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
269     Buffer *buf, int *err, gchar **err_info)
270 {
271         struct aethrarec_hdr hdr;
272
273         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
274                 return FALSE;
275
276         if (!aethra_read_rec_header(wth, wth->random_fh, &hdr, phdr, err,
277             err_info)) {
278                 if (*err == 0)
279                         *err = WTAP_ERR_SHORT_READ;
280                 return FALSE;
281         }
282
283         /*
284          * Read the packet data.
285          */
286         if (!wtap_read_packet_bytes(wth->random_fh, buf, phdr->caplen, err, err_info))
287                 return FALSE;   /* failed */
288
289         return TRUE;
290 }
291
292 static gboolean
293 aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
294     struct wtap_pkthdr *phdr, int *err, gchar **err_info)
295 {
296         aethra_t *aethra = (aethra_t *)wth->priv;
297         guint32 rec_size;
298         guint32 packet_size;
299         guint32 msecs;
300
301         /* Read record header. */
302         if (!wtap_read_bytes_or_eof(fh, hdr, sizeof *hdr, err, err_info))
303                 return FALSE;
304
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",
310                     rec_size,
311                     (unsigned int)(sizeof *hdr - sizeof hdr->rec_size));
312                 return FALSE;
313         }
314
315         packet_size = rec_size - (guint32)(sizeof *hdr - sizeof hdr->rec_size);
316
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 */
326
327         return TRUE;
328 }