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