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