4 * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
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.
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.
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.
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.
32 #include "file_wrappers.h"
39 /* This module reads the output of the various VMS TCPIP trace utilities
40 * such as TCPIPTRACE, TCPTRACE and UCX$TRACE
42 * It was initially based on toshiba.c and refined with code from cosine.c
44 --------------------------------------------------------------------------------
45 Example TCPIPTRACE TCPTRACE output data:
47 TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
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
56 UDP Source Port = 731, UDP Destination Port = 111
57 UDP Header and Datagram Length = 64 = ^x0040, Checksum = ^xB6C0
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 ................
65 --------------------------------------------------------------------------------
67 Example UCX$TRACE output data:
69 UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93
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
78 UDP Source Port = 138, UDP Destination Port = 138
79 UDP Header and Datagram Length = 563 = ^x0233, Checksum = ^xB913
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
86 --------------------------------------------------------------------------------
88 Alternate UCX$TRACE type output data:
90 TCPIP INTERnet trace RCV packet seq # = 1 at 23-OCT-1998 15:19:33.29
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
99 UDP Source Port = 138, UDP Destination Port = 138
100 UDP Header and Datagram Length = 197 = ^x00C5, Checksum = ^x0E77
102 5DA81410 8F6C1120 00000065 D9000045 0000 E...awe.....l....]
103 | 0E77C500 8A008A00 | FFFF1410 0010 ..........w.
105 --------------------------------------------------------------------------------
107 The only difference between the utilities is the Packet header line, primarily
108 the utility identifier and the packet sequence formats.
110 There appear to be 2 formats for packet seqencing
114 ... packet nn at DD-MMM-YYYY hh:mm:ss.ss
118 ... packet seq # = nn at DD-MMM-YYYY hh:mm:ss.ss
120 If there are other formats then code will have to be written in parse_vms_packet()
123 --------------------------------------------------------------------------------
127 /* Magic text to check for VMS-ness of file using possible utility names
130 #define VMS_HDR_MAGIC_STR1 "TCPIPtrace"
131 #define VMS_HDR_MAGIC_STR2 "TCPtrace"
132 #define VMS_HDR_MAGIC_STR3 "INTERnet trace"
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
139 #define VMS_HEADER_LINES_TO_CHECK 200
140 #define VMS_LINE_LENGTH 240
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);
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)
158 char buf[VMS_LINE_LENGTH];
161 cur_off = file_tell(wth->fh);
164 *err = file_error(wth->fh, err_info);
167 if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
169 *err = file_error(wth->fh, err_info);
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);
181 #endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
183 /* Look through the first part of a file to see if this is
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.
190 * Leaves file handle at beginning of line that contains the VMS Magic
193 static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info)
195 char buf[VMS_LINE_LENGTH];
199 buf[VMS_LINE_LENGTH-1] = '\0';
201 for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
202 mpos = file_tell(wth->fh);
205 *err = file_error(wth->fh, err_info);
208 if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) == NULL) {
210 *err = file_error(wth->fh, err_info);
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)) {
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
226 if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
238 int vms_open(wtap *wth, int *err, gchar **err_info)
240 /* Look for VMS header */
241 if (!vms_check_file_type(wth, err, err_info)) {
242 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
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;
257 /* Find the next packet and parse it; called from wtap_read(). */
258 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
263 /* Find the next packet */
264 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
265 offset = vms_seek_next_packet(wth, err, err_info);
267 offset = file_tell(wth->fh);
270 *err = file_error(wth->fh, err_info);
273 *data_offset = offset;
275 /* Parse the packet */
276 return parse_vms_packet(wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
279 /* Used to read packets in random-access fashion */
281 vms_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
282 Buffer *buf, int *err, gchar **err_info)
284 if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
287 if (!parse_vms_packet(wth->random_fh, phdr, buf, err, err_info)) {
289 *err = WTAP_ERR_SHORT_READ;
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
300 isdumpline( gchar *line )
304 while (*line && !isalnum((guchar)*line))
307 for (j=0; j<4; j++) {
308 for (i=0; i<8; i++, line++)
309 if (! isxdigit((guchar)*line))
312 for (i=0; i<3; i++, line++)
317 return isspace((guchar)*line);
320 /* Parses a packet record. */
322 parse_vms_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
324 char line[VMS_LINE_LENGTH + 1];
325 int num_items_scanned;
330 char mon[4] = {'J', 'A', 'N', 0};
332 static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
344 /* Skip lines until one starts with a hex number */
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;
353 line[VMS_LINE_LENGTH] = '\0';
355 if ((csec == 101) && (p = strstr(line, "packet ")) != NULL
356 && (! strstr(line, "could not save "))) {
357 /* Find text in line starting with "packet ". */
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);
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");
381 if ( (! pkt_len) && (p = strstr(line, "Length"))) {
382 p += sizeof("Length ");
383 while (*p && ! isdigit((guchar)*p))
387 *err = WTAP_ERR_BAD_FILE;
388 *err_info = g_strdup_printf("vms: Length field not valid");
395 } while (! isdumpline(line));
397 p = strstr(months, mon);
399 tm.tm_mon = (int) (p - months) / 3;
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;
410 /* Make sure we have enough room for the packet */
411 buffer_assure_space(buf, pkt_len);
412 pd = buffer_start_ptr(buf);
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);
419 *err = WTAP_ERR_SHORT_READ;
423 line[VMS_LINE_LENGTH] = '\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);
429 *err = WTAP_ERR_SHORT_READ;
433 line[VMS_LINE_LENGTH] = '\0';
435 while (line[offset] && !isxdigit((guchar)line[offset]))
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");
445 /* Avoid TCPIPTRACE-W-BUFFERSFUL, TCPIPtrace could not save n packets.
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);
454 /* There is no next line, so there's no "TCPIPtrace could not
455 * save n packets" line; not an error. */
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........
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.
478 * Returns TRUE if good hex dump, FALSE if bad.
481 parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
482 int in_off, int remaining) {
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};
491 /* Get the byte_offset directly from the record */
493 value = (int)strtoul(s + 45 + in_off, NULL, 16); /* XXX - error check? */
495 if (value != byte_offset) {
502 /* Read the octets right to left, as that is how they are displayed
506 for (i = 0; i < remaining; i++) {
507 lbuf[0] = rec[offsets[i] + in_off];
508 lbuf[1] = rec[offsets[i] + 1 + in_off];
510 buf[byte_offset + i] = (guint8) strtoul(lbuf, NULL, 16);