Remove unnecessary file_seek per packet, speeding up loading of a
[obnox/wireshark/wip.git] / wiretap / toshiba.c
1 /* toshiba.c
2  *
3  * $Id: toshiba.c,v 1.3 1999/11/11 06:29:59 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
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.h"
27 #include "buffer.h"
28 #include "toshiba.h"
29 #include "file.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 /* This module reads the output of the 'snoop' command in the Toshiba
36  * TR-600 and TR-650 "Compact" ISDN Routers. You can telnet to the
37  * router and run 'snoop' on the different channels, and at different
38  * detail levels. Be sure to choose 'dump' to get the hex dump.
39  * The 'snoop' command has nothing to do with the Solaris 'snoop'
40  * command, except that they both capture packets.
41  */
42
43 /*
44    Example 'snoop' output data:
45   
46
47 Script started on Thu Sep  9 21:48:49 1999
48 \e]0;gram@nirvana:/tmp\a$ telnet 10.0.0.254\r
49 Trying 10.0.0.254...\r
50 Connected to 10.0.0.254.\r
51 Escape character is '^]'.\r
52 \r
53 \r
54 TR-600(tr600) System Console\r
55 \r
56 Login:admin\r
57 Password:*******\r
58 *--------------------------------------------------------*\r
59 |             T O S H I B A    T R - 6 0 0               |\r
60 |                 <  Compact Router >                    |\r
61 |                       V1.02.02                         |\r
62 |                                                        |\r
63 |  (C) Copyright TOSHIBA Corp. 1997 All rights reserved. |\r
64 *--------------------------------------------------------*\r
65 \r
66 tr600>snoop dump b1\r
67  Trace start?(on/off/dump/dtl)->dump\r
68  IP Address?->b1\r
69 B1 Port Filetering\r
70 Trace start(Dump Mode)...\r
71 \r
72 tr600>[No.1] 00:00:09.14 B1:1 Tx 207.193.26.136->151.164.1.8 DNS  SPORT=1028 LEN=38 CHKSUM=4FD4 ID=2390 Query RD QCNT=1 pow.zing.org?\r
73 OFFSET 0001-0203-0405-0607-0809-0A0B-0C0D-0E0F 0123456789ABCDEF LEN=67\r
74 0000 : FF03 003D C000 0008 2145 0000 3A12 6500 ...=....!E..:.e.\r
75 0010 : 003F 11E6 58CF C11A 8897 A401 0804 0400 .?..X...........\r
76 0020 : 3500 264F D409 5601 0000 0100 0000 0000 5.&O..V.........\r
77 0030 : 0003 706F 7704 7A69 6E67 036F 7267 0000 ..pow.zing.org..\r
78 0040 : 0100 01                                 ...\r
79 \r
80 [No.2] 00:00:09.25 B1:1 Rx 151.164.1.8->207.193.26.136 DNS  DPORT=1028 LEN=193 CHKSUM=3E06 ID=2390 Answer RD RA QCNT=1 pow.zing.org? ANCNT=1 pow.zing.org=206.57.36.90 TTL=2652\r
81 OFFSET 0001-0203-0405-0607-0809-0A0B-0C0D-0E0F 0123456789ABCDEF LEN=222\r
82 0000 : FF03 003D C000 0013 2145 0000 D590 9340 ...=....!E.....@\r
83 0010 : 00F7 116F 8E97 A401 08CF C11A 8800 3504 ...o..........5.\r
84 0020 : 0400 C13E 0609 5681 8000 0100 0100 0300 ...>..V.........\r
85 0030 : 0303 706F 7704 7A69 6E67 036F 7267 0000 ..pow.zing.org..\r
86 0040 : 0100 01C0 0C00 0100 0100 000A 5C00 04CE ............\...\r
87 0050 : 3924 5A04 5A49 4E47 036F 7267 0000 0200 9$Z.ZING.org....\r
88 0060 : 0100 016F 5B00 0D03 4841 4E03 5449 5703 ...o[...HAN.TIW.\r
89 0070 : 4E45 5400 C02E 0002 0001 0001 6F5B 0006 NET.........o[..\r
90 0080 : 034E 5331 C02E C02E 0002 0001 0001 6F5B .NS1..........o[\r
91 0090 : 001C 0854 414C 4945 5349 4E0D 434F 4E46 ...TALIESIN.CONF\r
92 00A0 : 4142 554C 4154 494F 4E03 434F 4D00 C042 ABULATION.COM..B\r
93 00B0 : 0001 0001 0001 51EC 0004 CE39 2406 C05B ......Q....9$..[\r
94 00C0 : 0001 0001 0001 6F5B 0004 CE39 245A C06D ......o[...9$Z.m\r
95 00D0 : 0001 0001 0001 4521 0004 187C 1F01      ......E!...|..\r
96 \r
97  */
98
99 /* Magic text to check for toshiba-ness of file */
100 static const char toshiba_hdr_magic[]  =
101 { 'T', ' ', 'O', ' ', 'S', ' ', 'H', ' ', 'I', ' ', 'B', ' ', 'A' };
102 #define TOSHIBA_HDR_MAGIC_SIZE  (sizeof toshiba_hdr_magic  / sizeof toshiba_hdr_magic[0])
103
104 /* Magic text for start of packet */
105 static const char toshiba_rec_magic[]  = { '[', 'N', 'o', '.' };
106 #define TOSHIBA_REC_MAGIC_SIZE  (sizeof toshiba_rec_magic  / sizeof toshiba_rec_magic[0])
107
108 static int toshiba_read(wtap *wth, int *err);
109 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf, int byte_offset);
110 static int parse_toshiba_hex_dump(FILE *fh, int pkt_len, guint8* buf, int *err);
111 static int parse_toshiba_rec_hdr(wtap *wth, FILE *fh, int *err);
112 static int parse_toshiba(FILE *fh, guint8 *pd, int len);
113
114 /* Seeks to the beginning of the next packet, and returns the
115    byte offset.  Returns -1 on failure. */
116 /* XXX - Handle I/O errors. */
117 static int toshiba_seek_next_packet(wtap *wth)
118 {
119   int byte;
120   int level = 0;
121
122   while ((byte = file_getc(wth->fh)) != EOF) {
123     if (byte == toshiba_rec_magic[level]) {
124       level++;
125       if (level >= TOSHIBA_REC_MAGIC_SIZE) {
126               /* note: we're leaving file pointer right after the magic characters */
127         return file_tell(wth->fh) + 1;
128       }
129     } else {
130       level = 0;
131     }
132   }
133   return -1;
134 }
135
136 #define TOSHIBA_HEADER_LINES_TO_CHECK   200
137 #define TOSHIBA_LINE_LENGTH             240
138
139 /* Look through the first part of a file to see if this is
140  * a Toshiba trace file.
141  *
142  * Returns TRUE if it is, FALSE if it isn't.
143  */
144 static gboolean toshiba_check_file_type(wtap *wth)
145 {
146         char    buf[TOSHIBA_LINE_LENGTH];
147         int     reclen, i, line, byte, level;
148
149         buf[TOSHIBA_LINE_LENGTH-1] = 0;
150
151         for (line = 0; line < TOSHIBA_HEADER_LINES_TO_CHECK; line++) {
152                 if (file_gets(buf, TOSHIBA_LINE_LENGTH, wth->fh) >= 0) {
153
154                         reclen = strlen(buf);
155                         if (reclen < TOSHIBA_HDR_MAGIC_SIZE) {
156                                 continue;
157                         }
158
159                         level = 0;
160                         for (i = 0; i < reclen; i++) {
161                                 byte = buf[i];
162                                 if (byte == toshiba_hdr_magic[level]) {
163                                         level++;
164                                         if (level >= TOSHIBA_HDR_MAGIC_SIZE) {
165                                                 return TRUE;
166                                         }
167                                 }
168                                 else {
169                                         level = 0;
170                                 }
171                         }
172                 }
173                 else {
174                         return FALSE;
175                 }
176         }
177         return FALSE;
178 }
179
180
181 /* XXX - return -1 on I/O error and actually do something with 'err'. */
182 int toshiba_open(wtap *wth, int *err)
183 {
184         /* Reset file position and look for Toshiba header */
185         file_seek(wth->fh, 0, SEEK_SET);
186  
187         if (!toshiba_check_file_type(wth)) {
188                 return 0;
189         }
190
191         wth->data_offset = 0;
192         wth->file_encap = WTAP_ENCAP_PER_PACKET;
193         wth->file_type = WTAP_FILE_TOSHIBA;
194         wth->snapshot_length = 16384; /* just guessing */
195         wth->subtype_read = toshiba_read;
196
197         return 1;
198 }
199
200 /* Find the next packet and parse it; called from wtap_loop(). */
201 static int toshiba_read(wtap *wth, int *err)
202 {
203         int     offset = 0;
204         guint8  *buf;
205         int     pkt_len;
206
207         /* Find the next packet */
208         offset = toshiba_seek_next_packet(wth);
209         if (offset < 1) {
210                 return 0;
211         }
212
213         /* Parse the header */
214         pkt_len = parse_toshiba_rec_hdr(wth, wth->fh, err);
215
216         /* Make sure we have enough room for the packet */
217         buffer_assure_space(wth->frame_buffer, wth->snapshot_length);
218         buf = buffer_start_ptr(wth->frame_buffer);
219
220         /* Convert the ASCII hex dump to binary data */
221         parse_toshiba_hex_dump(wth->fh, pkt_len, buf, err);
222
223         wth->data_offset = offset;
224         return offset;
225 }
226
227 /* Used to read packets in random-access fashion */
228 int
229 toshiba_seek_read (FILE *fh, int seek_off, guint8 *pd, int len)
230 {
231         file_seek(fh, seek_off - 1, SEEK_SET);
232         return parse_toshiba(fh, pd, len);
233 }
234
235 /* Used to read packets in random-access fashion */
236 static int
237 parse_toshiba(FILE *fh, guint8 *pd, int len)
238 {
239         int     pkt_len;
240         int     err;
241
242         pkt_len = parse_toshiba_rec_hdr(NULL, fh, &err);
243
244         if (pkt_len != len) {
245                 return -1;
246         }
247
248         parse_toshiba_hex_dump(fh, pkt_len, pd, &err);
249
250         return 0;
251 }
252
253 /* Parses a packet record header. */
254 static int
255 parse_toshiba_rec_hdr(wtap *wth, FILE *fh, int *err)
256 {
257         char    line[TOSHIBA_LINE_LENGTH];
258         int     num_items_scanned;
259         int     pkt_len, pktnum, hr, min, sec, csec;
260         char    channel[10], direction[10];
261
262         /* Our file pointer should be on the line containing the
263          * summary information for a packet. Read in that line and
264          * extract the useful information
265          */
266         if (file_gets(line, TOSHIBA_LINE_LENGTH, fh) < 0) {
267                 *err = file_error(fh);
268                 if (*err == 0) {
269                         *err = WTAP_ERR_SHORT_READ;
270                 }
271                 return -1;
272         }
273
274         /* Find text in line after "[No." */
275         num_items_scanned = sscanf(line, "%d] %d:%d:%d.%d %s %s",
276                         &pktnum, &hr, &min, &sec, &csec, channel, direction);
277
278         if (num_items_scanned != 7) {
279                 *err = WTAP_ERR_BAD_RECORD;
280                 return -1;
281         }
282
283         /* The next line contains mostly junk, but it does contain the
284          * packet length. */
285         if (file_gets(line, TOSHIBA_LINE_LENGTH, fh) < 0) {
286                 *err = file_error(fh);
287                 if (*err == 0) {
288                         *err = WTAP_ERR_SHORT_READ;
289                 }
290                 return -1;
291         }
292
293         num_items_scanned = sscanf(line+64, "LEN=%d", &pkt_len);
294         if (num_items_scanned != 1) {
295                 *err = WTAP_ERR_BAD_RECORD;
296                 return -1;
297         }
298
299         if (wth) {
300                 wth->phdr.ts.tv_sec = 0;
301                 wth->phdr.ts.tv_usec = 0;
302                 wth->phdr.caplen = pkt_len;
303                 wth->phdr.len = pkt_len;
304                 switch (channel[0]) {
305                         case 'B':
306                                 wth->phdr.pkt_encap = WTAP_ENCAP_PPP;
307                                 break;
308
309                         case 'D':
310                                 wth->phdr.pkt_encap = WTAP_ENCAP_LAPD;
311                                 wth->phdr.pseudo_header.lapd.from_network_to_user = 
312                                         (direction[0] == 'R' ? TRUE : FALSE );
313                                 break;
314                         
315                         default:
316                                 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
317                                 break;
318                 }
319         }
320         return pkt_len;
321 }
322
323 /* Converts ASCII hex dump to binary data */
324 static int
325 parse_toshiba_hex_dump(FILE *fh, int pkt_len, guint8* buf, int *err)
326 {
327         char    line[TOSHIBA_LINE_LENGTH];
328         int     i, hex_lines;
329         
330         /* Calculate the number of hex dump lines, each
331          * containing 16 bytes of data */
332         hex_lines = pkt_len / 16 + ((pkt_len % 16) ? 1 : 0);
333
334         for (i = 0; i < hex_lines; i++) {
335                 if (file_gets(line, TOSHIBA_LINE_LENGTH, fh) < 0) {
336                         *err = file_error(fh);
337                         if (*err == 0) {
338                                 *err = WTAP_ERR_SHORT_READ;
339                         }
340                         return -1;
341                 }
342                 if (!parse_single_hex_dump_line(line, buf, i * 16)) {
343                         *err = WTAP_ERR_BAD_RECORD;
344                         return -1;
345                 }
346         }
347         return 0;
348 }
349
350 /*
351           1         2         3         4
352 0123456789012345678901234567890123456789012345
353 0000 : FF03 003D C000 0008 2145 0000 3A12 6500 ...=....!E..:.e.\r
354 0010 : 003F 11E6 58CF C11A 8897 A401 0804 0400 .?..X...........\r
355 0020 : 0100 01                                 ...\r
356 */
357
358 #define START_POS       7
359 #define HEX_LENGTH      ((8 * 4) + 7) /* eight clumps of 4 bytes with 7 inner spaces */
360
361 /* Take a string representing one line from a hex dump and converts the
362  * text to binary data. We check the printed offset with the offset
363  * we are passed to validate the record. We place the bytes in the buffer
364  * at the specified offset.
365  *
366  * In the process, we're going to write all over the string.
367  *
368  * Returns TRUE if good hex dump, FALSE if bad.
369  */
370 static gboolean
371 parse_single_hex_dump_line(char* rec, guint8 *buf, int byte_offset) {
372
373         int             pos, i;
374         char            *s;
375         unsigned long   value;
376         guint16         word_value;
377
378         /* Get the byte_offset directly from the record */
379         rec[4] = '\0';
380         s = rec;
381         value = strtoul(s, NULL, 16);
382
383         if (value != byte_offset) {
384                 return FALSE;
385         }
386
387         /* Go through the substring representing the values and:
388          *      1. Replace any spaces with '0's
389          *      2. Place \0's every 5 bytes (to terminate the string)
390          *
391          * Then read the eight sets of hex bytes
392          */
393
394         for (pos = START_POS; pos < START_POS + HEX_LENGTH; pos++) {
395                 if (rec[pos] == ' ') {
396                         rec[pos] = '0';
397                 }
398         }
399
400         pos = START_POS;
401         for (i = 0; i < 8; i++) {
402                 rec[pos+4] = '\0';
403
404                 word_value = (guint16) strtoul(&rec[pos], NULL, 16);
405                 buf[byte_offset + i * 2 + 0] = (guint8) (word_value >> 8);
406                 buf[byte_offset + i * 2 + 1] = (guint8) (word_value & 0x00ff);
407                 pos += 5;
408         }
409
410         return TRUE;
411 }