Set the packet encapsulation value to the file encapsulation.
[obnox/wireshark/wip.git] / wiretap / eyesdn.c
1 /* eyesdn.c
2  *
3  * $Id: eyesdn.c,v 1.4 2004/03/03 01:49:16 guy Exp $
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 (0d for D channel, 0e for e channel, b1-ce for B1-B30)
47  * - Byte 9: Sender (0 NT, 1 TE)
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     return i;
81 }
82
83 /* Magic text to check for eyesdn-ness of file */
84 static const unsigned char eyesdn_hdr_magic[]  =
85 { 'E', 'y', 'e', 'S', 'D', 'N'};
86 #define EYESDN_HDR_MAGIC_SIZE  (sizeof(eyesdn_hdr_magic)  / sizeof(eyesdn_hdr_magic[0]))
87
88 /* Size of a record header */
89 #define EYESDN_HDR_LENGTH               12
90
91 /*
92  * XXX - is this the biggest packet we can get?
93  */
94 #define EYESDN_MAX_PACKET_LEN   16384
95
96 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
97         long *data_offset);
98 static gboolean eyesdn_seek_read(wtap *wth, long seek_off,
99         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
100         int *err, gchar **err_info);
101 static gboolean parse_eyesdn_packet_data(FILE_T fh, int pkt_len, guint8* buf,
102         int *err, gchar **err_info);
103 static int parse_eyesdn_rec_hdr(wtap *wth, FILE_T fh,
104         union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
105
106 /* Seeks to the beginning of the next packet, and returns the
107    byte offset.  Returns -1 on failure, and sets "*err" to the error. */
108 static long eyesdn_seek_next_packet(wtap *wth, int *err)
109 {
110   int byte;
111   long cur_off;
112
113   while ((byte = file_getc(wth->fh)) != EOF) {
114     if (byte == 0xff) {
115         cur_off = file_tell(wth->fh);
116         if (cur_off == -1) {
117           /* Error. */
118           *err = file_error(wth->fh);
119           return -1;
120         }
121         return cur_off;
122       }
123   }
124   if (file_eof(wth->fh)) {
125     /* We got an EOF. */
126     *err = 0;
127   } else {
128     /* We (presumably) got an error (there's no equivalent to "ferror()"
129        in zlib, alas, so we don't have a wrapper to check for an error). */
130     *err = file_error(wth->fh);
131   }
132   return -1;
133 }
134
135 /* Look through the first part of a file to see if this is
136  * a eyesdn trace file.
137  *
138  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
139  * if we get an I/O error, "*err" will be set to a non-zero value.
140  */
141 static gboolean eyesdn_check_file_type(wtap *wth, int *err)
142 {
143         char    buf[EYESDN_HDR_MAGIC_SIZE];
144         int     i, reclen;
145         guint   level;
146         char    byte;
147
148         reclen=EYESDN_HDR_MAGIC_SIZE;
149         if (file_read(buf, 1, reclen, wth->fh) == reclen) {
150             level = 0;
151             for (i = 0; i < reclen; i++) {
152                 byte = buf[i];
153                 if (byte == eyesdn_hdr_magic[level]) {
154                     level++;
155                     if (level >= EYESDN_HDR_MAGIC_SIZE) {
156                         return TRUE;
157                     }
158                 } else {
159                     level = 0;
160                 }
161             }
162         } else {/* EOF or error. */
163             if (file_eof(wth->fh))
164                 *err = 0;
165             else
166                 *err = file_error(wth->fh);
167             return FALSE;
168         }
169         *err = 0;
170         return FALSE;
171 }
172
173
174 int eyesdn_open(wtap *wth, int *err, gchar **err_info _U_)
175 {
176         /* Look for eyesdn header */
177         if (!eyesdn_check_file_type(wth, err)) {
178                 if (*err == 0)
179                         return 0;
180                 else
181                         return -1;
182         }
183
184         wth->data_offset = 0;
185         wth->file_encap = WTAP_ENCAP_ISDN;
186         wth->file_type = WTAP_FILE_EYESDN;
187         wth->snapshot_length = 0; /* not known */
188         wth->subtype_read = eyesdn_read;
189         wth->subtype_seek_read = eyesdn_seek_read;
190
191         return 1;
192 }
193
194 /* Find the next packet and parse it; called from wtap_loop() and wtap_read(). */
195 static gboolean eyesdn_read(wtap *wth, int *err, gchar **err_info,
196     long *data_offset)
197 {
198         long    offset;
199         guint8  *buf;
200         int     pkt_len;
201
202         /* Find the next packet */
203         offset = eyesdn_seek_next_packet(wth, err);
204         if (offset < 1)
205                 return FALSE;
206
207         /* Parse the header */
208         pkt_len = parse_eyesdn_rec_hdr(wth, wth->fh, &wth->pseudo_header, err,
209             err_info);
210         if (pkt_len == -1)
211                 return FALSE;
212
213         /* Make sure we have enough room for the packet */
214         buffer_assure_space(wth->frame_buffer, EYESDN_MAX_PACKET_LEN);
215         buf = buffer_start_ptr(wth->frame_buffer);
216
217         /* Read the packet data */
218         if (!parse_eyesdn_packet_data(wth->fh, pkt_len, buf, err, err_info))
219                 return FALSE;
220
221         wth->data_offset = offset;
222         *data_offset = offset;
223         wth->phdr.pkt_encap = wth->file_encap;
224         return TRUE;
225 }
226
227 /* Used to read packets in random-access fashion */
228 static gboolean
229 eyesdn_seek_read (wtap *wth, long seek_off,
230         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
231         int *err, gchar **err_info)
232 {
233         int     pkt_len;
234
235         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
236                 return FALSE;
237
238         pkt_len = parse_eyesdn_rec_hdr(NULL, wth->random_fh, pseudo_header,
239             err, err_info);
240
241         if (pkt_len != len) {
242                 if (pkt_len != -1) {
243                         *err = WTAP_ERR_BAD_RECORD;
244                         *err_info = g_strdup_printf("eyesdn: requested length %d doesn't match length %d",
245                             len, pkt_len);
246                 }
247                 return FALSE;
248         }
249
250         return parse_eyesdn_packet_data(wth->random_fh, pkt_len, pd, err,
251             err_info);
252 }
253
254 /* Parses a packet record header. */
255 static int
256 parse_eyesdn_rec_hdr(wtap *wth, FILE_T fh,
257     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
258 {
259         guint8          hdr[EYESDN_HDR_LENGTH];
260         unsigned long   secs, usecs;
261         int             pkt_len;
262         unsigned int    channel, direction;
263
264         /* Our file pointer should be at the summary information header
265          * for a packet. Read in that header and extract the useful
266          * information.
267          */
268         if (esc_read(hdr, EYESDN_HDR_LENGTH, fh) != EYESDN_HDR_LENGTH) {
269                 *err = file_error(fh);
270                 if (*err == 0)
271                         *err = WTAP_ERR_SHORT_READ;
272                 return -1;
273         }
274     
275         /* extract information from header */
276         usecs = ((unsigned long) hdr[0]);
277         usecs = (usecs << 8) | ((unsigned long) hdr[1]);
278         usecs = (usecs << 8) | ((unsigned long) hdr[2]);
279 #ifdef TV64BITS    
280         secs = ((unsigned long) hdr[3]);
281 #else    
282         secs = 0;
283 #endif    
284         secs = (secs << 8) | ((unsigned long) hdr[4]);
285         secs = (secs << 8) | ((unsigned long) hdr[5]);
286         secs = (secs << 8) | ((unsigned long) hdr[6]);
287         secs = (secs << 8) | ((unsigned long) hdr[7]);
288
289         channel = hdr[8];
290         direction = hdr[9];
291         pkt_len = ((unsigned long) hdr[10]) << 8;
292         pkt_len = (pkt_len << 8) | ((unsigned long) hdr[11]);
293
294         /* sanity checks */
295         if(channel>30) {
296             *err = WTAP_ERR_BAD_RECORD;
297             *err_info = g_strdup_printf("eyesdn: bad channel number %u",
298                 channel);
299             return -1;
300         }
301
302         if(direction>1) {
303             *err = WTAP_ERR_BAD_RECORD;
304             *err_info = g_strdup_printf("eyesdn: bad direction value %u",
305                 direction);
306             return -1;
307         }
308         if(pkt_len > EYESDN_MAX_PACKET_LEN) {
309             *err = WTAP_ERR_BAD_RECORD;
310             *err_info = g_strdup_printf("eyesdn: File has %u-byte packet, bigger than maximum of %u",
311                 pkt_len, EYESDN_MAX_PACKET_LEN);
312             return -1;
313         }
314
315         if (wth) {
316                 wth->phdr.ts.tv_sec = secs;
317                 wth->phdr.ts.tv_usec = usecs;
318                 wth->phdr.caplen = pkt_len;
319                 wth->phdr.len = pkt_len;
320         }
321         pseudo_header->isdn.uton = direction;
322         pseudo_header->isdn.channel = channel;
323
324         return pkt_len;
325 }
326
327 /* read a packet */
328 static gboolean
329 parse_eyesdn_packet_data(FILE_T fh, int pkt_len, guint8* buf, int *err,
330     gchar **err_info)
331 {
332         int bytes_read;
333
334         errno = WTAP_ERR_CANT_READ;
335         bytes_read = esc_read(buf, pkt_len, fh);
336         if (bytes_read != pkt_len) {
337             if (bytes_read == -2) {
338                 *err = file_error(fh);
339                 if (*err == 0)
340                     *err = WTAP_ERR_SHORT_READ;
341             }  else if (bytes_read == -1) {
342                 *err = WTAP_ERR_BAD_RECORD;
343                 *err_info = g_strdup("eyesdn: No flag character seen in frame");
344             } else
345                 *err = WTAP_ERR_SHORT_READ;
346             return FALSE;
347         }
348         return TRUE;
349 }