Remove write capabilities from wtap_optionblocks.
[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 "aethra.h"
27
28 /* Magic number in Aethra PC108 files. */
29 #define MAGIC_SIZE      5
30
31 static const guchar aethra_magic[MAGIC_SIZE] = {
32         'V', '0', '2', '0', '8'
33 };
34
35 /* Aethra file header. */
36 struct aethra_hdr {
37         guchar  magic[MAGIC_SIZE];
38         guint8  unknown1[39];
39         guchar  sw_vers[60];    /* software version string, not null-terminated */
40         guint8  unknown2[118];
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 */
47         guint8  unknown4[2];
48         guint8  start_day[2];   /* day of capture start date */
49         guint8  unknown5[8];
50         guchar  com_info[16];   /* COM port and speed, null-padded(?) */
51         guint8  unknown6[107];
52         guchar  xxx_vers[41];   /* unknown version string (longer, null-padded?) */
53 };
54
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 */
62 };
63
64 /*
65  * Record types.
66  *
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.
70  *
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.
74  */
75 #define AETHRA_BEARER           0x80    /* bearer information */
76
77 #define AETHRA_DEVICE           0x00    /* indication from the monitoring device */
78 #define AETHRA_ISDN_LINK        0x01    /* information from the ISDN link */
79
80 /*
81  * In AETHRA_DEVICE records, the flags field has what appears to
82  * be a record subtype.
83  */
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 */
88
89 /*
90  * In AETHRA_ISDN_LINK and bearer channel records, the flags field has
91  * a direction flag and possibly some other bits.
92  *
93  * In AETHRA_ISDN_LINK records, at least some of the other bits are
94  * a subtype.
95  *
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.
102  */
103 #define AETHRA_U_TO_N                           0x01    /* set for TE->NT */
104
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 */
109
110 typedef struct {
111         time_t  start;
112 } aethra_t;
113
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);
120
121 wtap_open_return_val aethra_open(wtap *wth, int *err, gchar **err_info)
122 {
123         struct aethra_hdr hdr;
124         struct tm tm;
125         aethra_t *aethra;
126
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,
129             err_info)) {
130                 if (*err != WTAP_ERR_SHORT_READ)
131                         return WTAP_OPEN_ERROR;
132                 return WTAP_OPEN_NOT_MINE;
133         }
134
135         if (memcmp(hdr.magic, aethra_magic, sizeof aethra_magic) != 0)
136                 return WTAP_OPEN_NOT_MINE;
137
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;
147
148         /*
149          * Convert the time stamp to a "time_t".
150          */
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;
157         tm.tm_isdst = -1;
158         aethra->start = mktime(&tm);
159
160         /*
161          * We've only seen ISDN files, so, for now, we treat all
162          * files as ISDN.
163          */
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;
168 }
169
170 #if 0
171 static guint packet = 0;
172 #endif
173
174 /* Read the next packet */
175 static gboolean aethra_read(wtap *wth, int *err, gchar **err_info,
176     gint64 *data_offset)
177 {
178         struct aethrarec_hdr hdr;
179
180         /*
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.
183          */
184         for (;;) {
185                 *data_offset = file_tell(wth->fh);
186
187                 /* Read record header. */
188                 if (!aethra_read_rec_header(wth, wth->fh, &hdr, &wth->phdr, err, err_info))
189                         return FALSE;
190
191                 /*
192                  * XXX - if this is big, we might waste memory by
193                  * growing the buffer to handle it.
194                  */
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 */
199                 }
200 #if 0
201 packet++;
202 #endif
203                 switch (hdr.rec_type) {
204
205                 case AETHRA_ISDN_LINK:
206 #if 0
207 fprintf(stderr, "Packet %u: type 0x%02x (AETHRA_ISDN_LINK)\n",
208 packet, hdr.rec_type);
209 #endif
210                         switch (hdr.flags & AETHRA_ISDN_LINK_SUBTYPE) {
211
212                         case AETHRA_ISDN_LINK_LAPD:
213                                 /*
214                                  * The data is a LAPD frame.
215                                  */
216 #if 0
217 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_LAPD)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
218 #endif
219                                 goto found;
220
221                         case AETHRA_ISDN_LINK_SA_BITS:
222                                 /*
223                                  * These records have one data byte, which
224                                  * has the Sa bits in the lower 5 bits.
225                                  *
226                                  * XXX - what about stuff other than 2048K
227                                  * PRI lines?
228                                  */
229 #if 0
230 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_SA_BITS)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
231 #endif
232                                 break;
233
234                         case AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED:
235                                 /*
236                                  * No data, just an "all alarms cleared"
237                                  * indication.
238                                  */
239 #if 0
240 fprintf(stderr, "    subtype 0x%02x (AETHRA_ISDN_LINK_ALL_ALARMS_CLEARED)\n", hdr.flags & AETHRA_ISDN_LINK_SUBTYPE);
241 #endif
242                                 break;
243
244                         default:
245 #if 0
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);
248 #endif
249                                 break;
250                         }
251                         break;
252
253                 default:
254 #if 0
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);
257 #endif
258                         break;
259                 }
260         }
261
262 found:
263         return TRUE;
264 }
265
266 static gboolean
267 aethra_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
268     Buffer *buf, int *err, gchar **err_info)
269 {
270         struct aethrarec_hdr hdr;
271
272         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
273                 return FALSE;
274
275         if (!aethra_read_rec_header(wth, wth->random_fh, &hdr, phdr, err,
276             err_info)) {
277                 if (*err == 0)
278                         *err = WTAP_ERR_SHORT_READ;
279                 return FALSE;
280         }
281
282         /*
283          * Read the packet data.
284          */
285         if (!wtap_read_packet_bytes(wth->random_fh, buf, phdr->caplen, err, err_info))
286                 return FALSE;   /* failed */
287
288         return TRUE;
289 }
290
291 static gboolean
292 aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
293     struct wtap_pkthdr *phdr, int *err, gchar **err_info)
294 {
295         aethra_t *aethra = (aethra_t *)wth->priv;
296         guint32 rec_size;
297         guint32 packet_size;
298         guint32 msecs;
299
300         /* Read record header. */
301         if (!wtap_read_bytes_or_eof(fh, hdr, sizeof *hdr, err, err_info))
302                 return FALSE;
303
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",
309                     rec_size,
310                     (unsigned int)(sizeof *hdr - sizeof hdr->rec_size));
311                 return FALSE;
312         }
313         if (rec_size > WTAP_MAX_PACKET_SIZE) {
314                 /*
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.
318                  */
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);
322                 return FALSE;
323         }
324
325         packet_size = rec_size - (guint32)(sizeof *hdr - sizeof hdr->rec_size);
326
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 */
336
337         return TRUE;
338 }
339
340 /*
341  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
342  *
343  * Local variables:
344  * c-basic-offset: 8
345  * tab-width: 8
346  * indent-tabs-mode: t
347  * End:
348  *
349  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
350  * :indentSize=8:tabSize=8:noTabs=false:
351  */