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