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