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