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