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