8c14305e4113a33a6f75d86979a4f6f173618717
[metze/wireshark/wip.git] / wiretap / vms.c
1 /* vms.c
2  *
3  * $Id: vms.c,v 1.4 2002/01/15 20:18:02 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 2001 by Marc Milgram <mmilgram@arrayinc.com>
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 "vms.h"
29 #include "file_wrappers.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 /* This module reads the output of the 'TCPIPTRACE' command in VMS
37  * It was initially based on toshiba.c.
38  */
39
40 /*
41    Example 'TCPIPTRACE' output data:
42    TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
43
44    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 84 = ^x0054
45    IP Identifier  = ^x178F,  Flags (0=0,DF=0,MF=0),
46          Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
47    IP TTL = 64 = ^x40,  Protocol = 17 = ^x11,  Header Checksum = ^x4C71
48    IP Source Address      = 10.12.1.80
49    IP Destination Address = 10.12.1.50
50
51    UDP Source Port = 731,   UDP Destination Port = 111
52    UDP Header and Datagram Length = 64 = ^x0040,   Checksum = ^xB6C0
53
54    50010C0A   714C1140   00008F17   54000045    0000    E..T....@.Lq...P
55    27E54C3C | C0B64000   6F00DB02 | 32010C0A    0010    ...2...o.@..<L.'
56    02000000   A0860100   02000000   00000000    0020    ................
57    00000000   00000000   00000000   03000000    0030    ................
58    06000000   01000000   A5860100   00000000    0040    ................
59                                     00000000    0050    ....
60
61 --------------------------------------------------------------------------------
62
63  */
64
65 /* Magic text to check for VMS-ness of file */
66 static const char vms_hdr_magic[]  =
67 { 'T', 'C', 'P', 'I', 'P', 't', 'r', 'a', 'c', 'e', ' '};
68 #define VMS_HDR_MAGIC_SIZE  (sizeof vms_hdr_magic  / sizeof vms_hdr_magic[0])
69
70 /* Magic text for start of packet */
71 #define vms_rec_magic vms_hdr_magic
72 #define VMS_REC_MAGIC_SIZE  (sizeof vms_rec_magic  / sizeof vms_rec_magic[0])
73
74 static gboolean vms_read(wtap *wth, int *err, long *data_offset);
75 static int vms_seek_read(wtap *wth, long seek_off,
76     union wtap_pseudo_header *pseudo_header, guint8 *pd, int len);
77 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset, int remaining_bytes);
78 static int parse_vms_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err);
79 static int parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err);
80
81
82 /* Seeks to the beginning of the next packet, and returns the
83    byte offset.  Returns -1 on failure. */
84 /* XXX - Handle I/O errors. */
85 static long vms_seek_next_packet(wtap *wth)
86 {
87   int byte;
88   unsigned int level = 0;
89
90   while ((byte = file_getc(wth->fh)) != EOF) {
91     if (byte == vms_rec_magic[level]) {
92       level++;
93       if (level >= VMS_REC_MAGIC_SIZE) {
94           /* note: we're leaving file pointer right after the magic characters */
95         return file_tell(wth->fh) + 1;
96       }
97     } else {
98       level = 0;
99     }
100   }
101   return -1;
102 }
103
104 #define VMS_HEADER_LINES_TO_CHECK    200
105 #define VMS_LINE_LENGTH        240
106
107 /* Look through the first part of a file to see if this is
108  * a VMS trace file.
109  *
110  * Returns TRUE if it is, FALSE if it isn't.
111  *
112  * Leaves file handle at begining of line that contains the VMS Magic
113  * identifier.
114  */
115 static gboolean vms_check_file_type(wtap *wth)
116 {
117     char    buf[VMS_LINE_LENGTH];
118     int    line, byte;
119     unsigned int reclen, i, level;
120     long mpos;
121    
122     buf[VMS_LINE_LENGTH-1] = 0;
123
124     for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
125         mpos = file_tell(wth->fh);
126         if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) != NULL) {
127
128             reclen = strlen(buf);
129             if (reclen < VMS_HDR_MAGIC_SIZE)
130                 continue;
131
132             level = 0;
133             for (i = 0; i < reclen; i++) {
134                 byte = buf[i];
135                 if (byte == vms_hdr_magic[level]) {
136                     level++;
137                     if (level >= VMS_HDR_MAGIC_SIZE) {
138                         file_seek(wth->fh, mpos, SEEK_SET);
139                         return TRUE;
140                     }
141                 }
142                 else
143                     level = 0;
144             }
145         }
146         else
147             return FALSE;
148     }
149     return FALSE;
150 }
151
152
153 /* XXX - return -1 on I/O error and actually do something with 'err'. */
154 int vms_open(wtap *wth, int *err)
155 {
156     /* Look for VMS header */
157     if (!vms_check_file_type(wth)) {
158         return 0;
159     }
160
161     wth->data_offset = 0;
162     wth->file_encap = WTAP_ENCAP_RAW_IP;
163     wth->file_type = WTAP_FILE_VMS;
164     wth->snapshot_length = 16384; /* just guessing */
165     wth->subtype_read = vms_read;
166     wth->subtype_seek_read = vms_seek_read;
167
168     return 1;
169 }
170
171 /* Find the next packet and parse it; called from wtap_loop(). */
172 static gboolean vms_read(wtap *wth, int *err, long *data_offset)
173 {
174     long   offset = 0;
175     guint8    *buf;
176     int    pkt_len;
177
178     /* Find the next packet */
179     offset = vms_seek_next_packet(wth);
180     if (offset < 1) {
181         *err = 0;    /* XXX - assume, for now, that it's an EOF */
182         return FALSE;
183     }
184
185     /* Parse the header */
186     pkt_len = parse_vms_rec_hdr(wth, wth->fh, err);
187
188     /* Make sure we have enough room for the packet */
189     buffer_assure_space(wth->frame_buffer, wth->snapshot_length);
190     buf = buffer_start_ptr(wth->frame_buffer);
191
192     /* Convert the ASCII hex dump to binary data */
193     parse_vms_hex_dump(wth->fh, pkt_len, buf, err);
194
195     wth->data_offset = offset;
196     *data_offset = offset;
197     return TRUE;
198 }
199
200 /* Used to read packets in random-access fashion */
201 static int
202 vms_seek_read (wtap *wth, long seek_off, union wtap_pseudo_header *pseudo_header,
203     guint8 *pd, int len)
204 {
205     int    pkt_len;
206     int    err;
207
208     file_seek(wth->random_fh, seek_off - 1, SEEK_SET);
209
210     pkt_len = parse_vms_rec_hdr(NULL, wth->random_fh, &err);
211
212     if (pkt_len != len) {
213         return -1;
214     }
215
216     parse_vms_hex_dump(wth->random_fh, pkt_len, pd, &err);
217
218     return 0;
219 }
220
221 /* isdumpline assumes that dump lines start with some spaces followed by a
222  * hex number.
223  */
224 static int
225 isdumpline( guchar *line )
226 {
227     int i = 0;
228
229     while (i<VMS_LINE_LENGTH && !isalnum(line[i]))
230         i++;
231
232     if (! isxdigit(line[i]))
233         return 0;
234
235     while (i<VMS_LINE_LENGTH && isxdigit(line[i]))
236         i++;
237
238     return isspace(line[i]);
239 }
240
241 /* Parses a packet record header. */
242 static int
243 parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err)
244 {
245     char    line[VMS_LINE_LENGTH];
246     int    num_items_scanned;
247     int    pkt_len, pktnum, csec;
248     struct tm time;
249     char mon[4];
250     guchar *p;
251     static guchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
252
253     pkt_len = 0;
254
255     /* Our file pointer should be on the first line containing the
256      * summary information for a packet. Read in that line and
257      * extract the useful information
258      */
259     if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
260         *err = file_error(fh);
261         if (*err == 0) {
262             *err = WTAP_ERR_SHORT_READ;
263         }
264         return -1;
265     }
266
267     p = strstr(line, "packet ");
268     if ( !p ) {
269         *err = WTAP_ERR_BAD_RECORD;
270         return 01;
271     }
272    
273     /* Find text in line starting with "packet ". */
274     num_items_scanned = sscanf(p,
275                    "packet %d at %d-%3s-%d %d:%d:%d.%d",
276                    &pktnum, &time.tm_mday, mon,
277                    &time.tm_year, &time.tm_hour, &time.tm_min,
278                    &time.tm_sec, &csec);
279
280     if (num_items_scanned != 8) {
281         *err = WTAP_ERR_BAD_RECORD;
282         return -1;
283     }
284
285     /* Skip lines until one starts with a hex number */
286     do {
287         if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
288             *err = file_error(fh);
289             if (*err == 0) {
290                 *err = WTAP_ERR_SHORT_READ;
291             }
292             return -1;
293         }
294         if ( (! pkt_len) && (p = strstr(line, "Length"))) {
295             p += sizeof("Length ");
296             while (*p && ! isdigit(*p))
297                 p++;
298
299             if ( !*p ) {
300                 *err = WTAP_ERR_BAD_RECORD;
301                 return -1;
302             }
303
304             pkt_len = atoi(p);
305             break;
306         }
307     } while (! isdumpline(line));
308
309     if (wth) {
310         p = strstr(months, mon);
311         if (p)
312             time.tm_mon = (p - months) / 3;
313         time.tm_year -= 1900;
314
315         wth->phdr.ts.tv_sec = mktime(&time);
316
317         wth->phdr.ts.tv_usec = csec * 10000;
318         wth->phdr.caplen = pkt_len;
319         wth->phdr.len = pkt_len;
320         wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
321     }
322
323     return pkt_len;
324 }
325
326 /* Converts ASCII hex dump to binary data */
327 static int
328 parse_vms_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err)
329 {
330     guchar line[VMS_LINE_LENGTH];
331     int    i, hex_lines;
332     int    offset = 0;
333
334     /* Calculate the number of hex dump lines, each
335      * containing 16 bytes of data */
336     hex_lines = pkt_len / 16 + ((pkt_len % 16) ? 1 : 0);
337
338     for (i = 0; i < hex_lines; i++) {
339         if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
340             *err = file_error(fh);
341             if (*err == 0) {
342                 *err = WTAP_ERR_SHORT_READ;
343             }
344             return -1;
345         }
346         if (i == 0) {
347             while (! isdumpline(line)) /* advance to start of hex data */
348                 if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
349                     *err = file_error(fh);
350                     if (*err == 0) {
351                         *err = WTAP_ERR_SHORT_READ;
352                     }
353                     return -1;
354                 }
355             while (line[offset] && !isxdigit(line[offset]))
356                 offset++;
357         }
358         if (!parse_single_hex_dump_line(line, buf, i * 16,
359                         offset)) {
360             *err = WTAP_ERR_BAD_RECORD;
361             return -1;
362         }
363     }
364     return 0;
365 }
366
367 /*
368           1         2         3         4
369 0123456789012345678901234567890123456789012345
370    50010C0A   A34C0640   00009017   2C000045    0000    E..,....@.L....P
371    00000000   14945E52   0A00DC02 | 32010C0A    0010    ...2....R^......
372        0000 | B4050402   00003496   00020260    0020    `....4........
373 */
374
375 #define START_POS    7
376 #define HEX_LENGTH    ((8 * 4) + 7) /* eight clumps of 4 bytes with 7 inner spaces */
377 /* Take a string representing one line from a hex dump and converts the
378  * text to binary data. We check the printed offset with the offset
379  * we are passed to validate the record. We place the bytes in the buffer
380  * at the specified offset.
381  *
382  * In the process, we're going to write all over the string.
383  *
384  * Returns TRUE if good hex dump, FALSE if bad.
385  */
386 static gboolean
387 parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
388                int in_off) {
389
390     int        i;
391     char        *s;
392     int        value;
393     static int offsets[16] = {39,37,35,33,28,26,24,22,17,15,13,11,6,4,2,0};
394     char lbuf[3] = {0,0,0};
395    
396
397     /* Get the byte_offset directly from the record */
398     s = rec;
399     value = strtoul(s + 45 + in_off, NULL, 16);
400    
401     if (value != byte_offset) {
402         return FALSE;
403     }
404
405     /* Read the octets right to left, as that is how they are displayed
406      * in VMS.
407      */
408
409     for (i = 0; i < 16; i++) {
410         lbuf[0] = rec[offsets[i] + in_off];
411         lbuf[1] = rec[offsets[i] + 1 + in_off];
412
413         buf[byte_offset + i] = (guint8) strtoul(lbuf, NULL, 16);
414     }
415
416     return TRUE;
417 }