Have the Wiretap open, read, and seek-and-read routines return, in
[metze/wireshark/wip.git] / wiretap / vms.c
1 /* vms.c
2  *
3  * $Id: vms.c,v 1.21 2004/01/25 21:55:17 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
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 /* Notes:
24  *   TCPIPtrace TCP fragments don't have the header line.  So, we are never
25  *   to look for that line for the first line of a packet except the first
26  *   packet.  This allows us to read fragmented packets.  Define
27  *   TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE to expect the first line to be
28  *   at the start of every packet.
29  */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include "wtap-int.h"
34 #include "buffer.h"
35 #include "vms.h"
36 #include "file_wrappers.h"
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42
43 /* This module reads the output of the various VMS TCPIP trace utilities
44  * such as TCPIPTRACE, TCPTRACE and UCX$TRACE
45  *
46  * It was initially based on toshiba.c and refined with code from cosine.c
47
48 --------------------------------------------------------------------------------
49    Example TCPIPTRACE TCPTRACE output data:
50
51    TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
52
53    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 84 = ^x0054
54    IP Identifier  = ^x178F,  Flags (0=0,DF=0,MF=0),
55          Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
56    IP TTL = 64 = ^x40,  Protocol = 17 = ^x11,  Header Checksum = ^x4C71
57    IP Source Address      = 10.12.1.80
58    IP Destination Address = 10.12.1.50
59
60    UDP Source Port = 731,   UDP Destination Port = 111
61    UDP Header and Datagram Length = 64 = ^x0040,   Checksum = ^xB6C0
62
63    50010C0A   714C1140   00008F17   54000045    0000    E..T....@.Lq...P
64    27E54C3C | C0B64000   6F00DB02 | 32010C0A    0010    ...2...o.@..<L.'
65    02000000   A0860100   02000000   00000000    0020    ................
66    00000000   00000000   00000000   03000000    0030    ................
67    06000000   01000000   A5860100   00000000    0040    ................
68                                     00000000    0050    ....
69 --------------------------------------------------------------------------------
70
71    Example UCX$TRACE output data:
72
73     UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93 
74
75    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 583 = ^x0247 
76    IP Identifier  = ^x702E,  Flags (0=0,DF=0,MF=0),  
77          Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000 
78    IP TTL = 128 = ^x80,  Protocol = 17 = ^x11,  Header Checksum = ^x70EC 
79    IP Source Address      = 10.20.4.159 
80    IP Destination Address = 10.20.4.255 
81
82    UDP Source Port = 138,   UDP Destination Port = 138 
83    UDP Header and Datagram Length = 563 = ^x0233,   Checksum = ^xB913 
84
85    9F04140A   70EC1180   0000702E   47020045    0000    E..G.p.....p....
86    B1B80E11 | B9133302   8A008A00 | FF04140A    0010    .........3......
87    46484648   45200000   1D028A00   9F04140A    0020    ...........EHFHF
88    43414341   4341434D   454D4546   45454550    0030    PEEEFEMEMCACACAC
89
90 --------------------------------------------------------------------------------
91
92    Alternate UCX$TRACE type output data:
93
94    TCPIP INTERnet trace RCV packet seq # = 1 at 23-OCT-1998 15:19:33.29
95
96    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 217 = ^x00D9
97    IP Identifier  = ^x0065,  Flags (0=0,DF=0,MF=0),
98          Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
99    IP TTL = 32 = ^x20,  Protocol = 17 = ^x11,  Header Checksum = ^x8F6C
100    IP Source Address      = 16.20.168.93
101    IP Destination Address = 16.20.255.255
102
103    UDP Source Port = 138,   UDP Destination Port = 138
104    UDP Header and Datagram Length = 197 = ^x00C5,   Checksum = ^x0E77
105
106    5DA81410   8F6C1120   00000065   D9000045    0000    E...awe.....l....]
107             | 0E77C500   8A008A00 | FFFF1410    0010    ..........w.
108
109 --------------------------------------------------------------------------------
110
111 The only difference between the utilities is the Packet header line, primarily
112 the utility identifier and the packet sequence formats.
113
114 There appear to be 2 formats for packet seqencing
115
116 Format 1:
117
118  ... packet nn at DD-MMM-YYYY hh:mm:ss.ss
119
120 Format 2:
121
122  ... packet seq # = nn at DD-MMM-YYYY hh:mm:ss.ss
123
124 If there are other formats then code will have to be written in parse_vms_rec_hdr()
125 to handle them.
126
127 --------------------------------------------------------------------------------
128
129  */
130
131 /* Magic text to check for VMS-ness of file using possible utility names
132  *
133  */
134 #define VMS_HDR_MAGIC_STR1      "TCPIPtrace"
135 #define VMS_HDR_MAGIC_STR2      "TCPtrace"
136 #define VMS_HDR_MAGIC_STR3      "INTERnet trace"
137
138 /* Magic text for start of packet */
139 #define VMS_REC_MAGIC_STR1      VMS_HDR_MAGIC_STR1
140 #define VMS_REC_MAGIC_STR2      VMS_HDR_MAGIC_STR2
141 #define VMS_REC_MAGIC_STR3      VMS_HDR_MAGIC_STR3
142
143 #define VMS_HEADER_LINES_TO_CHECK    200
144 #define VMS_LINE_LENGTH              240
145
146 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
147     long *data_offset);
148 static gboolean vms_seek_read(wtap *wth, long seek_off,
149     union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
150     int *err, gchar **err_info);
151 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf,
152     long byte_offset, int in_off, int remaining_bytes);
153 static gboolean parse_vms_hex_dump(FILE_T fh, int pkt_len, guint8* buf,
154     int *err, gchar **err_info);
155 static int parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info);
156
157 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
158 /* Seeks to the beginning of the next packet, and returns the
159    byte offset.  Returns -1 on failure, and sets "*err" to the error. */
160 static long vms_seek_next_packet(wtap *wth, int *err)
161 {
162   long cur_off;
163   char buf[VMS_LINE_LENGTH];
164   
165   while (1) {
166     cur_off = file_tell(wth->fh);
167     if (cur_off == -1) {
168       /* Error */
169       *err = file_error(wth->fh);
170       hdr = NULL;
171       return -1;
172     }
173     if (file_gets(buf, sizeof(buf), wth->fh) != NULL) {
174       if (strstr(buf, VMS_REC_MAGIC_STR1) ||
175           strstr(buf, VMS_REC_MAGIC_STR2) ||
176           strstr(buf, VMS_REC_MAGIC_STR2)) {
177         strncpy(hdr, buf, VMS_LINE_LENGTH-1);
178         hdr[VMS_LINE_LENGTH-1] = '\0';
179         return cur_off;
180       }
181     } else {
182       if (file_eof(wth->fh)) {
183         /* We got an EOF. */
184         *err = 0;
185       } else {
186         /* We (presumably) got an error (there's no
187            equivalent to "ferror()" in zlib, alas,
188            so we don't have a wrapper to check for
189            an error). */
190         *err = file_error(wth->fh);
191       }
192       break;
193     }
194   }
195   hdr = NULL;
196   return -1;
197 }
198 #endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
199
200 /* Look through the first part of a file to see if this is
201  * a VMS trace file.
202  *
203  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
204  * if we get an I/O error, "*err" will be set to a non-zero value.
205  *
206  * Leaves file handle at begining of line that contains the VMS Magic
207  * identifier.
208  */
209 static gboolean vms_check_file_type(wtap *wth, int *err)
210 {
211   char  buf[VMS_LINE_LENGTH];
212   guint reclen, line;
213   long mpos;
214   
215   buf[VMS_LINE_LENGTH-1] = '\0';
216   
217   for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
218     mpos = file_tell(wth->fh);
219     if (mpos == -1) {
220       /* Error. */
221       *err = file_error(wth->fh);
222       return FALSE;
223     }
224     if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) != NULL) {
225       
226       reclen = strlen(buf);
227       if (reclen < strlen(VMS_HDR_MAGIC_STR1) ||
228           reclen < strlen(VMS_HDR_MAGIC_STR2) || 
229           reclen < strlen(VMS_HDR_MAGIC_STR3)) {
230         continue;
231       }
232       
233       if (strstr(buf, VMS_HDR_MAGIC_STR1) ||
234           strstr(buf, VMS_HDR_MAGIC_STR2) ||
235           strstr(buf, VMS_HDR_MAGIC_STR3)) {
236         /* Go back to the beginning of this line, so we will
237          * re-read it. */
238         if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
239           /* Error. */
240           return FALSE;
241         }
242         return TRUE;
243       }
244     } else {
245       /* EOF or error. */
246       if (file_eof(wth->fh))
247         *err = 0;
248       else
249         *err = file_error(wth->fh);
250       return FALSE;
251     }
252   }
253   *err = 0;
254   return FALSE;
255 }
256
257
258 int vms_open(wtap *wth, int *err, gchar **err_info _U_)
259 {
260     /* Look for VMS header */
261     if (!vms_check_file_type(wth, err)) {
262         if (*err == 0)
263             return 0;
264         else
265             return -1;
266     }
267
268     wth->data_offset = 0;
269     wth->file_encap = WTAP_ENCAP_RAW_IP;
270     wth->file_type = WTAP_FILE_VMS;
271     wth->snapshot_length = 0; /* not known */
272     wth->subtype_read = vms_read;
273     wth->subtype_seek_read = vms_seek_read;
274
275     return 1;
276 }
277
278 /* Find the next packet and parse it; called from wtap_loop(). */
279 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
280     long *data_offset)
281 {
282     long   offset = 0;
283     guint8    *buf;
284     int    pkt_len;
285
286     /* Find the next packet */
287 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
288     offset = vms_seek_next_packet(wth, err);
289 #else
290     offset = file_tell(wth->fh);
291 #endif
292     if (offset < 1)
293         return FALSE;
294
295     /* Parse the header */
296     pkt_len = parse_vms_rec_hdr(wth, wth->fh, err, err_info);
297     if (pkt_len == -1)
298         return FALSE;
299
300     /* Make sure we have enough room for the packet */
301     buffer_assure_space(wth->frame_buffer, pkt_len);
302     buf = buffer_start_ptr(wth->frame_buffer);
303
304     /* Convert the ASCII hex dump to binary data */
305     if (!parse_vms_hex_dump(wth->fh, pkt_len, buf, err, err_info))
306         return FALSE;
307
308     wth->data_offset = offset;
309     *data_offset = offset;
310     return TRUE;
311 }
312
313 /* Used to read packets in random-access fashion */
314 static gboolean
315 vms_seek_read (wtap *wth, long seek_off,
316     union wtap_pseudo_header *pseudo_header _U_,
317     guint8 *pd, int len, int *err, gchar **err_info)
318 {
319     int    pkt_len;
320
321     if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
322         return FALSE;
323
324     pkt_len = parse_vms_rec_hdr(NULL, wth->random_fh, err, err_info);
325
326     if (pkt_len != len) {
327         if (pkt_len != -1) {
328             *err = WTAP_ERR_BAD_RECORD;
329             *err_info = g_strdup_printf("vms: requested length %d doesn't match length %d",
330                 len, pkt_len);
331         }
332         return FALSE;
333     }
334
335     return parse_vms_hex_dump(wth->random_fh, pkt_len, pd, err, err_info);
336 }
337
338 /* isdumpline assumes that dump lines start with some non-alphanumerics
339  * followed by 4 hex numbers - each 8 digits long, each hex number followed
340  * by 3 spaces.
341  */
342 static int
343 isdumpline( gchar *line )
344 {
345     int i, j;
346
347     while (*line && !isalnum(*line))
348         line++;
349
350     for (j=0; j<4; j++) {
351         for (i=0; i<8; i++, line++)
352             if (! isxdigit(*line))
353                 return FALSE;
354
355         for (i=0; i<3; i++, line++)
356             if (*line != ' ')
357                 return FALSE;
358     }
359
360     return isspace(*line);
361 }
362
363 /* Parses a packet record header. */
364 static int
365 parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err, gchar **err_info)
366 {
367     char   line[VMS_LINE_LENGTH + 1];
368     int    num_items_scanned;
369     int    pkt_len = 0;
370     int    pktnum;
371     int    csec = 101;
372     struct tm time;
373     char mon[4] = {'J', 'A', 'N', 0};
374     gchar *p;
375     static gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
376
377     time.tm_year = 1970;
378     time.tm_hour = 1;
379     time.tm_min = 1;
380     time.tm_sec = 1;
381
382     /* Skip lines until one starts with a hex number */
383     do {
384         if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
385             *err = file_error(fh);
386             if ((*err == 0) && (csec != 101)) {
387                 *err = WTAP_ERR_SHORT_READ;
388             }
389             return -1;
390         }
391         line[VMS_LINE_LENGTH] = '\0';
392
393         if ((csec == 101) && (p = strstr(line, "packet "))
394             && (! strstr(line, "could not save "))) {
395             /* Find text in line starting with "packet ". */
396
397             /* First look for the Format 1 type sequencing */
398             num_items_scanned = sscanf(p,  
399                                        "packet %d at %d-%3s-%d %d:%d:%d.%d",
400                                        &pktnum, &time.tm_mday, mon,
401                                        &time.tm_year, &time.tm_hour,
402                                        &time.tm_min, &time.tm_sec, &csec);
403             /* Next look for the Format 2 type sequencing */
404             if (num_items_scanned != 8) {
405               num_items_scanned = sscanf(p,
406                                          "packet seq # = %d at %d-%3s-%d %d:%d:%d.%d",
407                                          &pktnum, &time.tm_mday, mon,
408                                          &time.tm_year, &time.tm_hour,
409                                          &time.tm_min, &time.tm_sec, &csec);
410             }
411             /* if unknown format then exit with error        */
412             /* We will need to add code to handle new format */
413             if (num_items_scanned != 8) {
414                 *err = WTAP_ERR_BAD_RECORD;
415                 *err_info = g_strdup_printf("vms: header line not valid");
416                 return -1;
417             }
418         }
419         if ( (! pkt_len) && (p = strstr(line, "Length"))) {
420             p += sizeof("Length ");
421             while (*p && ! isdigit(*p))
422                 p++;
423
424             if ( !*p ) {
425                 *err = WTAP_ERR_BAD_RECORD;
426                 *err_info = g_strdup_printf("vms: Length field not valid");
427                 return -1;
428             }
429
430             pkt_len = atoi(p);
431             break;
432         }
433     } while (! isdumpline(line));
434
435     if (wth) {
436         p = strstr(months, mon);
437         if (p)
438             time.tm_mon = (p - months) / 3;
439         time.tm_year -= 1900;
440
441         time.tm_isdst = -1;
442         wth->phdr.ts.tv_sec = mktime(&time);
443
444         wth->phdr.ts.tv_usec = csec * 10000;
445         wth->phdr.caplen = pkt_len;
446         wth->phdr.len = pkt_len;
447         wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
448     }
449
450     return pkt_len;
451 }
452
453 /* Converts ASCII hex dump to binary data */
454 static gboolean
455 parse_vms_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
456     gchar **err_info)
457 {
458     gchar line[VMS_LINE_LENGTH + 1];
459     int    i;
460     int    offset = 0;
461
462     for (i = 0; i < pkt_len; i += 16) {
463         if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
464             *err = file_error(fh);
465             if (*err == 0) {
466                 *err = WTAP_ERR_SHORT_READ;
467             }
468             return FALSE;
469         }
470         line[VMS_LINE_LENGTH] = '\0';
471         if (i == 0) {
472             while (! isdumpline(line)) { /* advance to start of hex data */
473                 if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
474                     *err = file_error(fh);
475                     if (*err == 0) {
476                         *err = WTAP_ERR_SHORT_READ;
477                     }
478                     return FALSE;
479                 }
480                 line[VMS_LINE_LENGTH] = '\0';
481             }
482             while (line[offset] && !isxdigit(line[offset]))
483                 offset++;
484         }
485         if (!parse_single_hex_dump_line(line, buf, i,
486                                         offset, pkt_len - i)) {
487             *err = WTAP_ERR_BAD_RECORD;
488             *err_info = g_strdup_printf("vms: hex dump not valid");
489             return FALSE;
490         }
491     }
492     /* Avoid TCPIPTRACE-W-BUFFERSFUL, TCPIPtrace could not save n packets.
493      * errors. */
494     file_gets(line, VMS_LINE_LENGTH, fh);
495     return TRUE;
496 }
497
498 /*
499           1         2         3         4
500 0123456789012345678901234567890123456789012345
501    50010C0A   A34C0640   00009017   2C000045    0000    E..,....@.L....P
502    00000000   14945E52   0A00DC02 | 32010C0A    0010    ...2....R^......
503        0000 | B4050402   00003496   00020260    0020    `....4........
504 */
505
506 #define START_POS    7
507 #define HEX_LENGTH    ((8 * 4) + 7) /* eight clumps of 4 bytes with 7 inner spaces */
508 /* Take a string representing one line from a hex dump and converts the
509  * text to binary data. We check the printed offset with the offset
510  * we are passed to validate the record. We place the bytes in the buffer
511  * at the specified offset.
512  *
513  * Returns TRUE if good hex dump, FALSE if bad.
514  */
515 static gboolean
516 parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
517                int in_off, int remaining) {
518
519     int        i;
520     char        *s;
521     int        value;
522     static int offsets[16] = {39,37,35,33,28,26,24,22,17,15,13,11,6,4,2,0};
523     char lbuf[3] = {0,0,0};
524
525
526     /* Get the byte_offset directly from the record */
527     s = rec;
528     value = strtoul(s + 45 + in_off, NULL, 16);
529
530     if (value != byte_offset) {
531         return FALSE;
532     }
533
534     if (remaining > 16)
535         remaining = 16;
536
537     /* Read the octets right to left, as that is how they are displayed
538      * in VMS.
539      */
540
541     for (i = 0; i < remaining; i++) {
542         lbuf[0] = rec[offsets[i] + in_off];
543         lbuf[1] = rec[offsets[i] + 1 + in_off];
544
545         buf[byte_offset + i] = (guint8) strtoul(lbuf, NULL, 16);
546     }
547
548     return TRUE;
549 }