change all file offsets from long to gint64 so we can - theoretically - handle 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 (long 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  *     128 ATM cells, 129 ATM layer indications)
48  * - Byte 9: Sender (0 NT, 1 TE)
49  * - Byte 10-11: frame size in bytes
50  * - Byte 12-n: Frame Payload
51  * 
52  * All multibyte values are represented in network byte order
53  * The frame is terminated with a flag character (0xff)
54  * bytes 0xff within a frame are escaped using the 0xfe escape character
55  * the byte following the escape character is decremented by two:
56  * so 0xfe 0xfd is actually a 0xff
57  * Characters that need to be escaped are 0xff and 0xfe
58  */
59
60
61 static int esc_read(guint8 *buf, int len, FILE_T fh)
62 {
63     int i;
64     int value;
65     
66     for(i=0; i<len; i++) {
67         value=file_getc(fh);
68         if(value==-1)
69             return -2; /* EOF or error */
70         if(value==0xff)
71             return -1; /* error !!, read into next frame */
72         if(value==0xfe) {
73             /* we need to escape */
74             value=file_getc(fh);
75             if(value==-1)
76                 return -2;
77             value+=2;
78         }
79         buf[i]=value;
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 static gint64 eyesdn_seek_next_packet(wtap *wth, int *err)
110 {
111   int byte;
112   gint64 cur_off;
113
114   while ((byte = file_getc(wth->fh)) != EOF) {
115     if (byte == 0xff) {
116         cur_off = file_tell(wth->fh);
117         if (cur_off == -1) {
118           /* Error. */
119           *err = file_error(wth->fh);
120           return -1;
121         }
122         return cur_off;
123       }
124   }
125   if (file_eof(wth->fh)) {
126     /* We got an EOF. */
127     *err = 0;
128   } else {
129     /* We (presumably) got an error (there's no equivalent to "ferror()"
130        in zlib, alas, so we don't have a wrapper to check for an error). */
131     *err = file_error(wth->fh);
132   }
133   return -1;
134 }
135
136 int eyesdn_open(wtap *wth, int *err, gchar **err_info _U_)
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, 1, sizeof magic, wth->fh);
144         if (bytes_read != sizeof magic) {
145                 *err = file_error(wth->fh);
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_ISDN;
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);
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         unsigned long   secs, usecs;
230         int             pkt_len;
231         guint8          channel, direction;
232
233         /* Our file pointer should be at the summary information header
234          * for a packet. Read in that header and extract the useful
235          * information.
236          */
237         if (esc_read(hdr, EYESDN_HDR_LENGTH, fh) != EYESDN_HDR_LENGTH) {
238                 *err = file_error(fh);
239                 if (*err == 0)
240                         *err = WTAP_ERR_SHORT_READ;
241                 return -1;
242         }
243     
244         /* extract information from header */
245         usecs = ((unsigned long) hdr[0]);
246         usecs = (usecs << 8) | ((unsigned long) hdr[1]);
247         usecs = (usecs << 8) | ((unsigned long) hdr[2]);
248 #ifdef TV64BITS    
249         secs = ((unsigned long) hdr[3]);
250 #else    
251         secs = 0;
252 #endif    
253         secs = (secs << 8) | ((unsigned long) hdr[4]);
254         secs = (secs << 8) | ((unsigned long) hdr[5]);
255         secs = (secs << 8) | ((unsigned long) hdr[6]);
256         secs = (secs << 8) | ((unsigned long) hdr[7]);
257
258         channel = hdr[8];
259         direction = hdr[9];
260         pkt_len = ((unsigned long) hdr[10]);
261         pkt_len = (pkt_len << 8) | ((unsigned long) hdr[11]);
262
263         /* sanity checks */
264         if((channel>30)&&(channel<128)) {
265             *err = WTAP_ERR_BAD_RECORD;
266             *err_info = g_strdup_printf("eyesdn: bad channel number %u",
267                 channel);
268             return -1;
269         }
270
271         if(direction>1) {
272             *err = WTAP_ERR_BAD_RECORD;
273             *err_info = g_strdup_printf("eyesdn: bad direction value %u",
274                 direction);
275             return -1;
276         }
277         if(pkt_len > EYESDN_MAX_PACKET_LEN) {
278             *err = WTAP_ERR_BAD_RECORD;
279             *err_info = g_strdup_printf("eyesdn: File has %u-byte packet, bigger than maximum of %u",
280                 pkt_len, EYESDN_MAX_PACKET_LEN);
281             return -1;
282         }
283
284         if (wth) {
285                 wth->phdr.ts.secs = secs;
286                 wth->phdr.ts.nsecs = usecs * 1000;
287                 wth->phdr.caplen = pkt_len;
288                 wth->phdr.len = pkt_len;
289         }
290         pseudo_header->isdn.uton = direction;
291         pseudo_header->isdn.channel = channel;
292
293         return pkt_len;
294 }
295
296 /* read a packet */
297 static gboolean
298 parse_eyesdn_packet_data(FILE_T fh, int pkt_len, guint8* buf, int *err,
299     gchar **err_info)
300 {
301         int bytes_read;
302
303         errno = WTAP_ERR_CANT_READ;
304         bytes_read = esc_read(buf, pkt_len, fh);
305         if (bytes_read != pkt_len) {
306             if (bytes_read == -2) {
307                 *err = file_error(fh);
308                 if (*err == 0)
309                     *err = WTAP_ERR_SHORT_READ;
310             }  else if (bytes_read == -1) {
311                 *err = WTAP_ERR_BAD_RECORD;
312                 *err_info = g_strdup("eyesdn: No flag character seen in frame");
313             } else
314                 *err = WTAP_ERR_SHORT_READ;
315             return FALSE;
316         }
317         return TRUE;
318 }