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