4 * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
6 * SPDX-License-Identifier: GPL-2.0-or-later
10 * TCPIPtrace TCP fragments don't have the header line. So, we are never
11 * to look for that line for the first line of a packet except the first
12 * packet. This allows us to read fragmented packets. Define
13 * TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE to expect the first line to be
14 * at the start of every packet.
19 #include "file_wrappers.h"
21 #include <wsutil/strtoi.h>
26 /* This module reads the output of the various VMS TCPIP trace utilities
27 * such as TCPIPTRACE, TCPTRACE and UCX$TRACE
29 * It was initially based on toshiba.c and refined with code from cosine.c
31 --------------------------------------------------------------------------------
32 Example TCPIPTRACE TCPTRACE output data:
34 TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
36 IP Version = 4, IHL = 5, TOS = 00, Total Length = 84 = ^x0054
37 IP Identifier = ^x178F, Flags (0=0,DF=0,MF=0),
38 Fragment Offset = 0 = ^x0000, Calculated Offset = 0 = ^x0000
39 IP TTL = 64 = ^x40, Protocol = 17 = ^x11, Header Checksum = ^x4C71
40 IP Source Address = 10.12.1.80
41 IP Destination Address = 10.12.1.50
43 UDP Source Port = 731, UDP Destination Port = 111
44 UDP Header and Datagram Length = 64 = ^x0040, Checksum = ^xB6C0
46 50010C0A 714C1140 00008F17 54000045 0000 E..T....@.Lq...P
47 27E54C3C | C0B64000 6F00DB02 | 32010C0A 0010 ...2...o.@..<L.'
48 02000000 A0860100 02000000 00000000 0020 ................
49 00000000 00000000 00000000 03000000 0030 ................
50 06000000 01000000 A5860100 00000000 0040 ................
52 --------------------------------------------------------------------------------
54 Example UCX$TRACE output data:
56 UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93
58 IP Version = 4, IHL = 5, TOS = 00, Total Length = 583 = ^x0247
59 IP Identifier = ^x702E, Flags (0=0,DF=0,MF=0),
60 Fragment Offset = 0 = ^x0000, Calculated Offset = 0 = ^x0000
61 IP TTL = 128 = ^x80, Protocol = 17 = ^x11, Header Checksum = ^x70EC
62 IP Source Address = 10.20.4.159
63 IP Destination Address = 10.20.4.255
65 UDP Source Port = 138, UDP Destination Port = 138
66 UDP Header and Datagram Length = 563 = ^x0233, Checksum = ^xB913
68 9F04140A 70EC1180 0000702E 47020045 0000 E..G.p.....p....
69 B1B80E11 | B9133302 8A008A00 | FF04140A 0010 .........3......
70 46484648 45200000 1D028A00 9F04140A 0020 ...........EHFHF
71 43414341 4341434D 454D4546 45454550 0030 PEEEFEMEMCACACAC
73 --------------------------------------------------------------------------------
75 Alternate UCX$TRACE type output data:
77 TCPIP INTERnet trace RCV packet seq # = 1 at 23-OCT-1998 15:19:33.29
79 IP Version = 4, IHL = 5, TOS = 00, Total Length = 217 = ^x00D9
80 IP Identifier = ^x0065, Flags (0=0,DF=0,MF=0),
81 Fragment Offset = 0 = ^x0000, Calculated Offset = 0 = ^x0000
82 IP TTL = 32 = ^x20, Protocol = 17 = ^x11, Header Checksum = ^x8F6C
83 IP Source Address = 16.20.168.93
84 IP Destination Address = 16.20.255.255
86 UDP Source Port = 138, UDP Destination Port = 138
87 UDP Header and Datagram Length = 197 = ^x00C5, Checksum = ^x0E77
89 5DA81410 8F6C1120 00000065 D9000045 0000 E...awe.....l....]
90 | 0E77C500 8A008A00 | FFFF1410 0010 ..........w.
92 --------------------------------------------------------------------------------
94 The only difference between the utilities is the Packet header line, primarily
95 the utility identifier and the packet sequence formats.
97 There appear to be 2 formats for packet seqencing
101 ... packet nn at DD-MMM-YYYY hh:mm:ss.ss
105 ... packet seq # = nn at DD-MMM-YYYY hh:mm:ss.ss
107 If there are other formats then code will have to be written in parse_vms_packet()
110 --------------------------------------------------------------------------------
114 /* Magic text to check for VMS-ness of file using possible utility names
117 #define VMS_HDR_MAGIC_STR1 "TCPIPtrace"
118 #define VMS_HDR_MAGIC_STR2 "TCPtrace"
119 #define VMS_HDR_MAGIC_STR3 "INTERnet trace"
121 /* Magic text for start of packet */
122 #define VMS_REC_MAGIC_STR1 VMS_HDR_MAGIC_STR1
123 #define VMS_REC_MAGIC_STR2 VMS_HDR_MAGIC_STR2
124 #define VMS_REC_MAGIC_STR3 VMS_HDR_MAGIC_STR3
126 #define VMS_HEADER_LINES_TO_CHECK 200
127 #define VMS_LINE_LENGTH 240
129 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
130 gint64 *data_offset);
131 static gboolean vms_seek_read(wtap *wth, gint64 seek_off,
132 wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
133 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf,
134 long byte_offset, int in_off, int remaining_bytes);
135 static gboolean parse_vms_packet(FILE_T fh, wtap_rec *rec,
136 Buffer *buf, int *err, gchar **err_info);
138 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
139 /* Seeks to the beginning of the next packet, and returns the
140 byte offset. Returns -1 on failure, and sets "*err" to the error
141 and sets "*err_info" to null or an additional error string. */
142 static long vms_seek_next_packet(wtap *wth, int *err, gchar **err_info)
145 char buf[VMS_LINE_LENGTH];
148 cur_off = file_tell(wth->fh);
151 *err = file_error(wth->fh, err_info);
154 if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
156 *err = file_error(wth->fh, err_info);
159 if (strstr(buf, VMS_REC_MAGIC_STR1) ||
160 strstr(buf, VMS_REC_MAGIC_STR2) ||
161 strstr(buf, VMS_REC_MAGIC_STR2)) {
162 g_strlcpy(hdr, buf,VMS_LINE_LENGTH);
168 #endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
170 /* Look through the first part of a file to see if this is
173 * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
174 * if we get an I/O error, "*err" will be set to a non-zero value and
175 * "*err_info will be set to null or an additional error string.
177 * Leaves file handle at beginning of line that contains the VMS Magic
180 static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info)
182 char buf[VMS_LINE_LENGTH];
186 buf[VMS_LINE_LENGTH-1] = '\0';
188 for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
189 mpos = file_tell(wth->fh);
192 *err = file_error(wth->fh, err_info);
195 if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) == NULL) {
197 *err = file_error(wth->fh, err_info);
201 reclen = (guint) strlen(buf);
202 if (reclen < strlen(VMS_HDR_MAGIC_STR1) ||
203 reclen < strlen(VMS_HDR_MAGIC_STR2) ||
204 reclen < strlen(VMS_HDR_MAGIC_STR3)) {
208 if (strstr(buf, VMS_HDR_MAGIC_STR1) ||
209 strstr(buf, VMS_HDR_MAGIC_STR2) ||
210 strstr(buf, VMS_HDR_MAGIC_STR3)) {
211 /* Go back to the beginning of this line, so we will
213 if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
225 wtap_open_return_val vms_open(wtap *wth, int *err, gchar **err_info)
227 /* Look for VMS header */
228 if (!vms_check_file_type(wth, err, err_info)) {
229 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
230 return WTAP_OPEN_ERROR;
231 return WTAP_OPEN_NOT_MINE;
234 wth->file_encap = WTAP_ENCAP_RAW_IP;
235 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_VMS;
236 wth->snapshot_length = 0; /* not known */
237 wth->subtype_read = vms_read;
238 wth->subtype_seek_read = vms_seek_read;
239 wth->file_tsprec = WTAP_TSPREC_CSEC;
241 return WTAP_OPEN_MINE;
244 /* Find the next packet and parse it; called from wtap_read(). */
245 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
250 /* Find the next packet */
251 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
252 offset = vms_seek_next_packet(wth, err, err_info);
254 offset = file_tell(wth->fh);
257 *err = file_error(wth->fh, err_info);
260 *data_offset = offset;
262 /* Parse the packet */
263 return parse_vms_packet(wth->fh, &wth->rec, wth->rec_data, err, err_info);
266 /* Used to read packets in random-access fashion */
268 vms_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
269 Buffer *buf, int *err, gchar **err_info)
271 if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
274 if (!parse_vms_packet(wth->random_fh, rec, buf, err, err_info)) {
276 *err = WTAP_ERR_SHORT_READ;
282 /* isdumpline assumes that dump lines start with some non-alphanumerics
283 * followed by 4 hex numbers - each 8 digits long, each hex number followed
287 isdumpline( gchar *line )
291 while (*line && !g_ascii_isalnum(*line))
294 for (j=0; j<4; j++) {
295 for (i=0; i<8; i++, line++)
296 if (! g_ascii_isxdigit(*line))
299 for (i=0; i<3; i++, line++)
304 return g_ascii_isspace(*line);
307 /* Parses a packet record. */
309 parse_vms_packet(FILE_T fh, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
311 char line[VMS_LINE_LENGTH + 1];
312 int num_items_scanned;
317 char mon[4] = {'J', 'A', 'N', 0};
320 static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
332 /* Skip lines until one starts with a hex number */
334 if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
335 *err = file_error(fh, err_info);
336 if ((*err == 0) && (csec != 101)) {
337 *err = WTAP_ERR_SHORT_READ;
341 line[VMS_LINE_LENGTH] = '\0';
343 if ((csec == 101) && (p = strstr(line, "packet ")) != NULL
344 && (! strstr(line, "could not save "))) {
345 /* Find text in line starting with "packet ". */
347 /* First look for the Format 1 type sequencing */
348 num_items_scanned = sscanf(p,
349 "packet %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
350 &pktnum, &tm.tm_mday, mon,
351 &tm.tm_year, &tm.tm_hour,
352 &tm.tm_min, &tm.tm_sec, &csec);
353 /* Next look for the Format 2 type sequencing */
354 if (num_items_scanned != 8) {
355 num_items_scanned = sscanf(p,
356 "packet seq # = %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
357 &pktnum, &tm.tm_mday, mon,
358 &tm.tm_year, &tm.tm_hour,
359 &tm.tm_min, &tm.tm_sec, &csec);
361 /* if unknown format then exit with error */
362 /* We will need to add code to handle new format */
363 if (num_items_scanned != 8) {
364 *err = WTAP_ERR_BAD_FILE;
365 *err_info = g_strdup("vms: header line not valid");
369 if ( (! pkt_len) && (p = strstr(line, "Length"))) {
370 p += sizeof("Length ");
371 while (*p && ! g_ascii_isdigit(*p))
375 *err = WTAP_ERR_BAD_FILE;
376 *err_info = g_strdup("vms: Length field not valid");
380 if (!ws_strtou32(p, &endp, &pkt_len) || (*endp != '\0' && !g_ascii_isspace(*endp))) {
381 *err = WTAP_ERR_BAD_FILE;
382 *err_info = g_strdup_printf("vms: Length field '%s' not valid", p);
387 } while (! isdumpline(line));
388 if (pkt_len > WTAP_MAX_PACKET_SIZE_STANDARD) {
390 * Probably a corrupt capture file; return an error,
391 * so that our caller doesn't blow up trying to allocate
392 * space for an immensely-large packet.
394 *err = WTAP_ERR_BAD_FILE;
395 *err_info = g_strdup_printf("vms: File has %u-byte packet, bigger than maximum of %u",
396 pkt_len, WTAP_MAX_PACKET_SIZE_STANDARD);
400 p = strstr(months, mon);
402 tm.tm_mon = (int) (p - months) / 3;
406 rec->rec_type = REC_TYPE_PACKET;
407 rec->presence_flags = WTAP_HAS_TS;
408 rec->ts.secs = mktime(&tm);
409 rec->ts.nsecs = csec * 10000000;
410 rec->rec_header.packet_header.caplen = pkt_len;
411 rec->rec_header.packet_header.len = pkt_len;
413 /* Make sure we have enough room for the packet */
414 ws_buffer_assure_space(buf, pkt_len);
415 pd = ws_buffer_start_ptr(buf);
417 /* Convert the ASCII hex dump to binary data */
418 for (i = 0; i < pkt_len; i += 16) {
419 if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
420 *err = file_error(fh, err_info);
422 *err = WTAP_ERR_SHORT_READ;
426 line[VMS_LINE_LENGTH] = '\0';
428 while (! isdumpline(line)) { /* advance to start of hex data */
429 if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
430 *err = file_error(fh, err_info);
432 *err = WTAP_ERR_SHORT_READ;
436 line[VMS_LINE_LENGTH] = '\0';
438 while (line[offset] && !g_ascii_isxdigit(line[offset]))
441 if (!parse_single_hex_dump_line(line, pd, i,
442 offset, pkt_len - i)) {
443 *err = WTAP_ERR_BAD_FILE;
444 *err_info = g_strdup("vms: hex dump not valid");
448 /* Avoid TCPIPTRACE-W-BUFFERSFUL, TCPIPtrace could not save n packets.
451 * XXX - when we support packet drop report information in the
452 * Wiretap API, we should parse those lines and return "n" as
453 * a packet drop count. */
454 if (!file_gets(line, VMS_LINE_LENGTH, fh)) {
455 *err = file_error(fh, err_info);
457 /* There is no next line, so there's no "TCPIPtrace could not
458 * save n packets" line; not an error. */
468 0123456789012345678901234567890123456789012345
469 50010C0A A34C0640 00009017 2C000045 0000 E..,....@.L....P
470 00000000 14945E52 0A00DC02 | 32010C0A 0010 ...2....R^......
471 0000 | B4050402 00003496 00020260 0020 `....4........
475 #define HEX_LENGTH ((8 * 4) + 7) /* eight clumps of 4 bytes with 7 inner spaces */
476 /* Take a string representing one line from a hex dump and converts the
477 * text to binary data. We check the printed offset with the offset
478 * we are passed to validate the record. We place the bytes in the buffer
479 * at the specified offset.
481 * Returns TRUE if good hex dump, FALSE if bad.
484 parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
485 int in_off, int remaining_bytes) {
490 static const int offsets[16] = {39,37,35,33,28,26,24,22,17,15,13,11,6,4,2,0};
491 char lbuf[3] = {0,0,0};
494 /* Get the byte_offset directly from the record */
496 value = (int)strtoul(s + 45 + in_off, NULL, 16); /* XXX - error check? */
498 if (value != byte_offset) {
502 if (remaining_bytes > 16)
503 remaining_bytes = 16;
505 /* Read the octets right to left, as that is how they are displayed
509 for (i = 0; i < remaining_bytes; i++) {
510 lbuf[0] = rec[offsets[i] + in_off];
511 lbuf[1] = rec[offsets[i] + 1 + in_off];
513 buf[byte_offset + i] = (guint8) strtoul(lbuf, NULL, 16);
520 * Editor modelines - http://www.wireshark.org/tools/modelines.html
525 * indent-tabs-mode: nil
528 * vi: set shiftwidth=4 tabstop=8 expandtab:
529 * :indentSize=4:tabSize=8:noTabs=true: