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.
31 #include "file_wrappers.h"
36 /* This module reads the output of the various VMS TCPIP trace utilities
37 * such as TCPIPTRACE, TCPTRACE and UCX$TRACE
39 * It was initially based on toshiba.c and refined with code from cosine.c
41 --------------------------------------------------------------------------------
42 Example TCPIPTRACE TCPTRACE output data:
44 TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
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
53 UDP Source Port = 731, UDP Destination Port = 111
54 UDP Header and Datagram Length = 64 = ^x0040, Checksum = ^xB6C0
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 ................
62 --------------------------------------------------------------------------------
64 Example UCX$TRACE output data:
66 UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93
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
75 UDP Source Port = 138, UDP Destination Port = 138
76 UDP Header and Datagram Length = 563 = ^x0233, Checksum = ^xB913
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
83 --------------------------------------------------------------------------------
85 Alternate UCX$TRACE type output data:
87 TCPIP INTERnet trace RCV packet seq # = 1 at 23-OCT-1998 15:19:33.29
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
96 UDP Source Port = 138, UDP Destination Port = 138
97 UDP Header and Datagram Length = 197 = ^x00C5, Checksum = ^x0E77
99 5DA81410 8F6C1120 00000065 D9000045 0000 E...awe.....l....]
100 | 0E77C500 8A008A00 | FFFF1410 0010 ..........w.
102 --------------------------------------------------------------------------------
104 The only difference between the utilities is the Packet header line, primarily
105 the utility identifier and the packet sequence formats.
107 There appear to be 2 formats for packet seqencing
111 ... packet nn at DD-MMM-YYYY hh:mm:ss.ss
115 ... packet seq # = nn at DD-MMM-YYYY hh:mm:ss.ss
117 If there are other formats then code will have to be written in parse_vms_packet()
120 --------------------------------------------------------------------------------
124 /* Magic text to check for VMS-ness of file using possible utility names
127 #define VMS_HDR_MAGIC_STR1 "TCPIPtrace"
128 #define VMS_HDR_MAGIC_STR2 "TCPtrace"
129 #define VMS_HDR_MAGIC_STR3 "INTERnet trace"
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
136 #define VMS_HEADER_LINES_TO_CHECK 200
137 #define VMS_LINE_LENGTH 240
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);
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)
155 char buf[VMS_LINE_LENGTH];
158 cur_off = file_tell(wth->fh);
161 *err = file_error(wth->fh, err_info);
164 if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
166 *err = file_error(wth->fh, err_info);
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);
178 #endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
180 /* Look through the first part of a file to see if this is
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.
187 * Leaves file handle at beginning of line that contains the VMS Magic
190 static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info)
192 char buf[VMS_LINE_LENGTH];
196 buf[VMS_LINE_LENGTH-1] = '\0';
198 for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
199 mpos = file_tell(wth->fh);
202 *err = file_error(wth->fh, err_info);
205 if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) == NULL) {
207 *err = file_error(wth->fh, err_info);
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)) {
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
223 if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
235 wtap_open_return_val vms_open(wtap *wth, int *err, gchar **err_info)
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;
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;
251 return WTAP_OPEN_MINE;
254 /* Find the next packet and parse it; called from wtap_read(). */
255 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
260 /* Find the next packet */
261 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
262 offset = vms_seek_next_packet(wth, err, err_info);
264 offset = file_tell(wth->fh);
267 *err = file_error(wth->fh, err_info);
270 *data_offset = offset;
272 /* Parse the packet */
273 return parse_vms_packet(wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
276 /* Used to read packets in random-access fashion */
278 vms_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
279 Buffer *buf, int *err, gchar **err_info)
281 if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
284 if (!parse_vms_packet(wth->random_fh, phdr, buf, err, err_info)) {
286 *err = WTAP_ERR_SHORT_READ;
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
297 isdumpline( gchar *line )
301 while (*line && !g_ascii_isalnum(*line))
304 for (j=0; j<4; j++) {
305 for (i=0; i<8; i++, line++)
306 if (! g_ascii_isxdigit(*line))
309 for (i=0; i<3; i++, line++)
314 return g_ascii_isspace(*line);
317 /* Parses a packet record. */
319 parse_vms_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
321 char line[VMS_LINE_LENGTH + 1];
322 int num_items_scanned;
327 char mon[4] = {'J', 'A', 'N', 0};
329 static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
341 /* Skip lines until one starts with a hex number */
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;
350 line[VMS_LINE_LENGTH] = '\0';
352 if ((csec == 101) && (p = strstr(line, "packet ")) != NULL
353 && (! strstr(line, "could not save "))) {
354 /* Find text in line starting with "packet ". */
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);
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");
378 if ( (! pkt_len) && (p = strstr(line, "Length"))) {
379 p += sizeof("Length ");
380 while (*p && ! g_ascii_isdigit(*p))
384 *err = WTAP_ERR_BAD_FILE;
385 *err_info = g_strdup("vms: Length field not valid");
392 } while (! isdumpline(line));
393 if (pkt_len > WTAP_MAX_PACKET_SIZE) {
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.
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);
405 p = strstr(months, mon);
407 tm.tm_mon = (int) (p - months) / 3;
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;
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);
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);
427 *err = WTAP_ERR_SHORT_READ;
431 line[VMS_LINE_LENGTH] = '\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);
437 *err = WTAP_ERR_SHORT_READ;
441 line[VMS_LINE_LENGTH] = '\0';
443 while (line[offset] && !g_ascii_isxdigit(line[offset]))
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");
453 /* Avoid TCPIPTRACE-W-BUFFERSFUL, TCPIPtrace could not save n packets.
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);
462 /* There is no next line, so there's no "TCPIPtrace could not
463 * save n packets" line; not an error. */
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........
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.
486 * Returns TRUE if good hex dump, FALSE if bad.
489 parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
490 int in_off, int remaining) {
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};
499 /* Get the byte_offset directly from the record */
501 value = (int)strtoul(s + 45 + in_off, NULL, 16); /* XXX - error check? */
503 if (value != byte_offset) {
510 /* Read the octets right to left, as that is how they are displayed
514 for (i = 0; i < remaining; i++) {
515 lbuf[0] = rec[offsets[i] + in_off];
516 lbuf[1] = rec[offsets[i] + 1 + in_off];
518 buf[byte_offset + i] = (guint8) strtoul(lbuf, NULL, 16);
525 * Editor modelines - http://www.wireshark.org/tools/modelines.html
530 * indent-tabs-mode: nil
533 * vi: set shiftwidth=4 tabstop=8 expandtab:
534 * :indentSize=4:tabSize=8:noTabs=true: