Revert "Allow wtap_read() and wtap_seek_read() to return non-packet records."
[metze/wireshark/wip.git] / wiretap / eyesdn.c
1 /* eyesdn.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 "wtap-int.h"
23 #include "buffer.h"
24 #include "eyesdn.h"
25 #include "file_wrappers.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31
32 /* This module reads the output of the EyeSDN USB S0/E1 ISDN probes
33  * They store HDLC frames of D and B channels in a binary format
34  * The fileformat is
35  *
36  * 1-6 Byte: EyeSDN - Magic
37  * 7-n Byte: Frames
38  *
39  * Each Frame starts with the 0xff Flag byte
40  * - Bytes 0-2: timestamp (usec in network byte order)
41  * - Bytes 3-7: timestamp (40bits sec since 1970 in network byte order)
42  * - Byte 8: channel (0 for D channel, 1-30 for B1-B30)
43  * - Byte 9: Sender Bit 0(0 NT, 1 TE), Protocol in Bits 7:1, see enum
44  * - Byte 10-11: frame size in bytes
45  * - Byte 12-n: Frame Payload
46  *
47  * All multibyte values are represented in network byte order
48  * The frame is terminated with a flag character (0xff)
49  * bytes 0xff within a frame are escaped using the 0xfe escape character
50  * the byte following the escape character is decremented by two:
51  * so 0xfe 0xfd is actually a 0xff
52  * Characters that need to be escaped are 0xff and 0xfe
53  */
54
55
56 static int esc_read(guint8 *buf, int len, FILE_T fh)
57 {
58         int i;
59         int value;
60
61         for(i=0; i<len; i++) {
62                 value=file_getc(fh);
63                 if(value==-1)
64                         return -2; /* EOF or error */
65                 if(value==0xff)
66                         return -1; /* error !!, read into next frame */
67                 if(value==0xfe) {
68                         /* we need to escape */
69                         value=file_getc(fh);
70                         if(value==-1)
71                                 return -2;
72                         value+=2;
73                 }
74                 buf[i]=value;
75         }
76
77         return i;
78 }
79
80 /* Magic text to check for eyesdn-ness of file */
81 static const unsigned char eyesdn_hdr_magic[]  =
82 { 'E', 'y', 'e', 'S', 'D', 'N'};
83 #define EYESDN_HDR_MAGIC_SIZE  (sizeof(eyesdn_hdr_magic)  / sizeof(eyesdn_hdr_magic[0]))
84
85 /* Size of a record header */
86 #define EYESDN_HDR_LENGTH               12
87
88 /*
89  * XXX - is this the biggest packet we can get?
90  */
91 #define EYESDN_MAX_PACKET_LEN   16384
92
93 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
94         gint64 *data_offset);
95 static gboolean eyesdn_seek_read(wtap *wth, gint64 seek_off,
96         struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
97 static int read_eyesdn_rec(FILE_T fh, struct wtap_pkthdr *phdr, Buffer* buf,
98         int *err, gchar **err_info);
99
100 /* Seeks to the beginning of the next packet, and returns the
101    byte offset.  Returns -1 on failure, and sets "*err" to the error
102    and "*err_info" to null or an additional error string. */
103 static gint64 eyesdn_seek_next_packet(wtap *wth, int *err, gchar **err_info)
104 {
105         int byte;
106         gint64 cur_off;
107
108         while ((byte = file_getc(wth->fh)) != EOF) {
109                 if (byte == 0xff) {
110                         cur_off = file_tell(wth->fh);
111                         if (cur_off == -1) {
112                                 /* Error. */
113                                 *err = file_error(wth->fh, err_info);
114                                 return -1;
115                         }
116                         return cur_off;
117                 }
118         }
119         /* EOF or error. */
120         *err = file_error(wth->fh, err_info);
121         return -1;
122 }
123
124 int eyesdn_open(wtap *wth, int *err, gchar **err_info)
125 {
126         int     bytes_read;
127         char    magic[EYESDN_HDR_MAGIC_SIZE];
128
129         /* Look for eyesdn header */
130         errno = WTAP_ERR_CANT_READ;
131         bytes_read = file_read(&magic, sizeof magic, wth->fh);
132         if (bytes_read != sizeof magic) {
133                 *err = file_error(wth->fh, err_info);
134                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
135                         return -1;
136                 return 0;
137         }
138         if (memcmp(magic, eyesdn_hdr_magic, EYESDN_HDR_MAGIC_SIZE) != 0)
139                 return 0;
140
141         wth->file_encap = WTAP_ENCAP_PER_PACKET;
142         wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_EYESDN;
143         wth->snapshot_length = 0; /* not known */
144         wth->subtype_read = eyesdn_read;
145         wth->subtype_seek_read = eyesdn_seek_read;
146         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
147
148         return 1;
149 }
150
151 /* Find the next packet and parse it; called from wtap_read(). */
152 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
153     gint64 *data_offset)
154 {
155         gint64  offset;
156
157         /* Find the next record */
158         offset = eyesdn_seek_next_packet(wth, err, err_info);
159         if (offset < 1)
160                 return FALSE;
161         *data_offset = offset;
162
163         /* Parse the record */
164         return read_eyesdn_rec(wth->fh, &wth->phdr, wth->frame_buffer,
165             err, err_info);
166 }
167
168 /* Used to read packets in random-access fashion */
169 static gboolean
170 eyesdn_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
171         Buffer *buf, int *err, gchar **err_info)
172 {
173         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
174                 return FALSE;
175
176         return read_eyesdn_rec(wth->random_fh, phdr, buf, err, err_info);
177 }
178
179 /* Parses a record. */
180 static gboolean
181 read_eyesdn_rec(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err,
182     gchar **err_info)
183 {
184         union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
185         guint8          hdr[EYESDN_HDR_LENGTH];
186         time_t          secs;
187         int             usecs;
188         int             pkt_len;
189         guint8          channel, direction;
190         int             bytes_read;
191         guint8          *pd;
192
193         /* Our file pointer should be at the summary information header
194          * for a packet. Read in that header and extract the useful
195          * information.
196          */
197         if (esc_read(hdr, EYESDN_HDR_LENGTH, fh) != EYESDN_HDR_LENGTH) {
198                 *err = file_error(fh, err_info);
199                 if (*err == 0)
200                         *err = WTAP_ERR_SHORT_READ;
201                 return FALSE;
202         }
203
204         /* extract information from header */
205         usecs = pntoh24(&hdr[0]);
206 #ifdef TV64BITS
207         secs = hdr[3];
208 #else
209         secs = 0;
210 #endif
211         secs = (secs << 8) | hdr[4];
212         secs = (secs << 8) | hdr[5];
213         secs = (secs << 8) | hdr[6];
214         secs = (secs << 8) | hdr[7];
215
216         channel = hdr[8];
217         direction = hdr[9];
218         pkt_len = pntoh16(&hdr[10]);
219
220         switch(direction >> 1) {
221
222         default:
223         case EYESDN_ENCAP_ISDN: /* ISDN */
224                 pseudo_header->isdn.uton = direction & 1;
225                 pseudo_header->isdn.channel = channel;
226                 if(channel) { /* bearer channels */
227                         phdr->pkt_encap = WTAP_ENCAP_ISDN; /* recognises PPP */
228                         pseudo_header->isdn.uton=!pseudo_header->isdn.uton; /* bug */
229                 } else { /* D channel */
230                         phdr->pkt_encap = WTAP_ENCAP_ISDN;
231                 }
232                 break;
233
234         case EYESDN_ENCAP_MSG: /* Layer 1 message */
235                 phdr->pkt_encap = WTAP_ENCAP_LAYER1_EVENT;
236                 pseudo_header->l1event.uton = (direction & 1);
237                 break;
238
239         case EYESDN_ENCAP_LAPB: /* X.25 via LAPB */
240                 phdr->pkt_encap = WTAP_ENCAP_LAPB;
241                 pseudo_header->x25.flags = (direction & 1) ? 0 : 0x80;
242                 break;
243
244         case EYESDN_ENCAP_ATM: { /* ATM cells */
245 #define CELL_LEN 53
246                 unsigned char cell[CELL_LEN];
247                 gint64 cur_off;
248
249                 if(pkt_len != CELL_LEN) {
250                         *err = WTAP_ERR_BAD_FILE;
251                         *err_info = g_strdup_printf(
252                             "eyesdn: ATM cell has a length != 53 (%u)",
253                             pkt_len);
254                         return FALSE;
255                 }
256
257                 cur_off = file_tell(fh);
258                 if (esc_read(cell, CELL_LEN, fh) != CELL_LEN) {
259                         *err = file_error(fh, err_info);
260                         if (*err == 0)
261                                 *err = WTAP_ERR_SHORT_READ;
262                         return FALSE;
263                 }
264                 if (file_seek(fh, cur_off, SEEK_SET, err) == -1)
265                         return FALSE;
266                 phdr->pkt_encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
267                 pseudo_header->atm.flags=ATM_RAW_CELL;
268                 pseudo_header->atm.aal=AAL_UNKNOWN;
269                 pseudo_header->atm.type=TRAF_UMTS_FP;
270                 pseudo_header->atm.subtype=TRAF_ST_UNKNOWN;
271                 pseudo_header->atm.vpi=((cell[0]&0xf)<<4) + (cell[0]&0xf);
272                 pseudo_header->atm.vci=((cell[0]&0xf)<<4) + cell[0]; /* from cell */
273                 pseudo_header->atm.channel=direction & 1;
274                 }
275                 break;
276
277         case EYESDN_ENCAP_MTP2: /* SS7 frames */
278                 pseudo_header->mtp2.sent = direction & 1;
279                 pseudo_header->mtp2.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
280                 pseudo_header->mtp2.link_number = channel;
281                 phdr->pkt_encap = WTAP_ENCAP_MTP2_WITH_PHDR;
282                 break;
283
284         case EYESDN_ENCAP_DPNSS: /* DPNSS */
285                 pseudo_header->isdn.uton = direction & 1;
286                 pseudo_header->isdn.channel = channel;
287                 phdr->pkt_encap = WTAP_ENCAP_DPNSS;
288                 break;
289
290         case EYESDN_ENCAP_DASS2: /* DASS2 frames */
291                 pseudo_header->isdn.uton = direction & 1;
292                 pseudo_header->isdn.channel = channel;
293                 phdr->pkt_encap = WTAP_ENCAP_DPNSS;
294                 break;
295
296         case EYESDN_ENCAP_BACNET: /* BACNET async over HDLC frames */
297                 pseudo_header->isdn.uton = direction & 1;
298                 pseudo_header->isdn.channel = channel;
299                 phdr->pkt_encap = WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR;
300                 break;
301
302         case EYESDN_ENCAP_V5_EF: /* V5EF */
303                 pseudo_header->isdn.uton = direction & 1;
304                 pseudo_header->isdn.channel = channel;
305                 phdr->pkt_encap = WTAP_ENCAP_V5_EF;
306                 break;
307         }
308
309         if(pkt_len > EYESDN_MAX_PACKET_LEN) {
310                 *err = WTAP_ERR_BAD_FILE;
311                 *err_info = g_strdup_printf("eyesdn: File has %u-byte packet, bigger than maximum of %u",
312                     pkt_len, EYESDN_MAX_PACKET_LEN);
313                 return FALSE;
314         }
315
316         phdr->presence_flags = WTAP_HAS_TS;
317         phdr->ts.secs = secs;
318         phdr->ts.nsecs = usecs * 1000;
319         phdr->caplen = pkt_len;
320         phdr->len = pkt_len;
321
322         /* Make sure we have enough room for the packet */
323         buffer_assure_space(buf, EYESDN_MAX_PACKET_LEN);
324
325         errno = WTAP_ERR_CANT_READ;
326         pd = buffer_start_ptr(buf);
327         bytes_read = esc_read(pd, pkt_len, fh);
328         if (bytes_read != pkt_len) {
329                 if (bytes_read == -2) {
330                         *err = file_error(fh, err_info);
331                         if (*err == 0)
332                                 *err = WTAP_ERR_SHORT_READ;
333                 } else if (bytes_read == -1) {
334                         *err = WTAP_ERR_BAD_FILE;
335                         *err_info = g_strdup("eyesdn: No flag character seen in frame");
336                 } else
337                         *err = WTAP_ERR_SHORT_READ;
338                 return FALSE;
339         }
340         return TRUE;
341 }
342
343
344 static gboolean
345 esc_write(wtap_dumper *wdh, const guint8 *buf, int len, int *err)
346 {
347         int i;
348         guint8 byte;
349         static const guint8 esc = 0xfe;
350
351         for(i=0; i<len; i++) {
352                 byte=buf[i];
353                 if(byte == 0xff || byte == 0xfe) {
354                         /*
355                          * Escape the frame delimiter and escape byte.
356                          */
357                         if (!wtap_dump_file_write(wdh, &esc, sizeof esc, err))
358                                 return FALSE;
359                         byte-=2;
360                 }
361                 if (!wtap_dump_file_write(wdh, &byte, sizeof byte, err))
362                         return FALSE;
363         }
364         return TRUE;
365 }
366
367 static gboolean eyesdn_dump(wtap_dumper *wdh,
368                             const struct wtap_pkthdr *phdr,
369                             const guint8 *pd, int *err);
370
371 gboolean eyesdn_dump_open(wtap_dumper *wdh, int *err)
372 {
373         wdh->subtype_write=eyesdn_dump;
374         wdh->subtype_close=NULL;
375
376         if (!wtap_dump_file_write(wdh, eyesdn_hdr_magic,
377             EYESDN_HDR_MAGIC_SIZE, err))
378                 return FALSE;
379         wdh->bytes_dumped += EYESDN_HDR_MAGIC_SIZE;
380         *err=0;
381         return TRUE;
382 }
383
384 int eyesdn_dump_can_write_encap(int encap)
385 {
386         switch (encap) {
387         case WTAP_ENCAP_ISDN:
388         case WTAP_ENCAP_LAYER1_EVENT:
389         case WTAP_ENCAP_DPNSS:
390         case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
391         case WTAP_ENCAP_LAPB:
392         case WTAP_ENCAP_MTP2_WITH_PHDR:
393         case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
394         case WTAP_ENCAP_PER_PACKET:
395                 return 0;
396
397         default:
398                 return WTAP_ERR_UNSUPPORTED_ENCAP;
399         }
400 }
401
402 /* Write a record for a packet to a dump file.
403  *    Returns TRUE on success, FALSE on failure. */
404 static gboolean eyesdn_dump(wtap_dumper *wdh,
405                             const struct wtap_pkthdr *phdr,
406                             const guint8 *pd, int *err)
407 {
408         static const guint8 start_flag = 0xff;
409         const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
410         guint8 buf[EYESDN_HDR_LENGTH];
411         int usecs;
412         time_t secs;
413         int channel;
414         int origin;
415         int protocol;
416         int size;
417
418         /* Don't write out anything bigger than we can read.
419          * (The length field in packet headers is 16 bits, which
420          * imposes a hard limit.) */
421         if (phdr->caplen > 65535) {
422                 *err = WTAP_ERR_PACKET_TOO_LARGE;
423                 return FALSE;
424         }
425
426         usecs=phdr->ts.nsecs/1000;
427         secs=phdr->ts.secs;
428         size=phdr->caplen;
429         origin = pseudo_header->isdn.uton;
430         channel = pseudo_header->isdn.channel;
431
432         switch(phdr->pkt_encap) {
433
434         case WTAP_ENCAP_ISDN:
435                 protocol=EYESDN_ENCAP_ISDN; /* set depending on decoder format and mode */
436                 break;
437
438         case WTAP_ENCAP_LAYER1_EVENT:
439                 protocol=EYESDN_ENCAP_MSG;
440                 break;
441
442         case WTAP_ENCAP_DPNSS:
443                 protocol=EYESDN_ENCAP_DPNSS;
444                 break;
445
446 #if 0
447         case WTAP_ENCAP_DASS2:
448                 protocol=EYESDN_ENCAP_DASS2;
449                 break;
450 #endif
451
452         case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
453                 protocol=EYESDN_ENCAP_ATM;
454                 channel=0x80;
455                 break;
456
457         case WTAP_ENCAP_LAPB:
458                 protocol=EYESDN_ENCAP_LAPB;
459                 break;
460
461         case WTAP_ENCAP_MTP2_WITH_PHDR:
462                 protocol=EYESDN_ENCAP_MTP2;
463                 break;
464
465         case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
466                 protocol=EYESDN_ENCAP_BACNET;
467                 break;
468
469         case WTAP_ENCAP_V5_EF:
470                 protocol=EYESDN_ENCAP_V5_EF;
471                 break;
472
473         default:
474                 *err=WTAP_ERR_UNSUPPORTED_ENCAP;
475                 return FALSE;
476         }
477
478         phton24(&buf[0], usecs);
479
480         buf[3] = (guint8)0;
481         buf[4] = (guint8)(0xff & (secs >> 24));
482         buf[5] = (guint8)(0xff & (secs >> 16));
483         buf[6] = (guint8)(0xff & (secs >> 8));
484         buf[7] = (guint8)(0xff & (secs >> 0));
485
486         buf[8] = (guint8) channel;
487         buf[9] = (guint8) (origin?1:0) + (protocol << 1);
488         phtons(&buf[10], size);
489
490         /* start flag */
491         if (!wtap_dump_file_write(wdh, &start_flag, sizeof start_flag, err))
492                 return FALSE;
493         if (!esc_write(wdh, buf, 12, err))
494                 return FALSE;
495         if (!esc_write(wdh, pd, size, err))
496                 return FALSE;
497         return TRUE;
498 }