5 * CoSine IPNOS L2 debug output parsing
6 * Copyright (c) 2002 by Motonori Shindo <motonori@shin.do>
9 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include "file_wrappers.h"
41 IPNOS: CONFIG VPN(100) VR(1.1.1.1)# diags
42 ipnos diags: Control (1/0) :: layer-2 ?
43 Registered commands for area "layer-2"
44 apply-pkt-log-profile Configure packet logging on an interface
45 create-pkt-log-profile Set packet-log-profile to be used for packet logging (see layer-2 pkt-log)
46 detail Get Layer 2 low-level details
48 ipnos diags: Control (1/0) :: layer-2 create ?
49 create-pkt-log-profile <pkt-log-profile-id ctl-tx-trace-length ctl-rx-trace-length data-tx-trace-length data-rx-trace-length pe-logging-or-control-blade>
51 ipnos diags: Control (1/0) :: layer-2 create 1 32 32 0 0 0
52 ipnos diags: Control (1/0) :: layer-2 create 2 32 32 100 100 0
53 ipnos diags: Control (1/0) :: layer-2 apply ?
54 apply-pkt-log-profile <slot port channel subif pkt-log-profile-id>
56 ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 1
57 Successfully applied packet-log-profile on LI
59 -- Note that only the control packets are logged because the data packet size parameters are 0 in profile 1
60 IPNOS: CONFIG VPN(200) VR(3.3.3.3)# ping 20.20.20.43
61 vpn 200 : [max tries 4, timeout 5 seconds, data length 64 bytes, ttl 255]
62 ping #1 ok, RTT 0.000 seconds
63 ping #2 ok, RTT 0.000 seconds
64 ping #3 ok, RTT 0.000 seconds
65 ping #4 ok, RTT 0.000 seconds
68 IPNOS: CONFIG VPN(200) VR(3.3.3.3)# 2000-2-1,18:19:46.8: l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
71 2000-2-1,18:19:46.8: l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
73 2000-2-1,18:19:46.8: l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
75 2000-2-1,18:19:46.8: l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x8030000]
77 ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 0
78 Successfully applied packet-log-profile on LI
79 ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 2
80 Successfully applied packet-log-profile on LI
82 -- Note that both control and data packets are logged because the data packet size parameter is 100 in profile 2
83 Please ignore the event-log messages getting mixed up with the ping command
84 ping 20.20.20.43 cou2000-2-1,18:20:17.0: l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
86 00 D0 D8 D2 FF 03 C0 21 09 29 00 08 6B 60 84 AA
88 2000-2-1,18:20:17.0: l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
89 00 D0 D8 D2 FF 03 C0 21 09 29 00 08 6D FE FA AA
91 2000-2-1,18:20:17.0: l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
92 00 D0 D8 D2 FF 03 C0 21 0A 29 00 08 6B 60 84 AA
94 2000-2-1,18:20:17.0: l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x8030000]
95 00 D0 D8 D2 FF 03 C0 21 0A 29 00 08 6D FE FA AA
98 vpn 200 : [max tries 1, timeout 5 seconds, data length 500 bytes, ttl 255]
99 2000-2-1,18:20:24.1: l2-tx (PPP:3/7/1:100), Length:536, Pro:1, Off:8, Pri:7, RM:0, Err:0 [0x4070, 0x801]
100 00 D0 D8 D2 FF 03 00 21 45 00 02 10 00 27 00 00
101 FF 01 69 51 14 14 14 22 14 14 14 2B 08 00 AD B8
102 00 03 00 01 10 11 12 13 14 15 16 17 18 19 1A 1B
103 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B
104 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B
105 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B
108 ping #1 ok, RTT 0.010 seconds
109 2000-2-1,18:20:24.1: l2-rx (PPP:3/7/1:100), Length:536, Pro:1, Off:8, Pri:7, RM:0, Err:0 [0x4071, 0x30801]
110 00 D0 D8 D2 FF 03 00 21 45 00 02 10 00 23 00 00
111 FF 01 69 55 14 14 14 2B 14 14 14 22 00 00 B5 B8
112 00 03 00 01 10 11 12 13 14 15 16 17 18 19 1A 1B
113 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B
114 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B
115 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B
120 IPNOS: CONFIG VPN(200) VR(3.3.3.3)# 2000-2-1,18:20:27.0: l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
122 00 D0 D8 D2 FF 03 C0 21 09 2A 00 08 6B 60 84 AA
124 2000-2-1,18:20:27.0: l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
125 00 D0 D8 D2 FF 03 C0 21 09 2A 00 08 6D FE FA AA
127 2000-2-1,18:20:27.0: l2-tx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
128 00 D0 D8 D2 FF 03 C0 21 0A 2A 00 08 6B 60 84 AA
130 2000-2-1,18:20:27.0: l2-rx (PPP:3/7/1:100), Length:16, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4001, 0x30000]
131 00 D0 D8 D2 FF 03 C0 21 0A 2A 00 08 6D FE FA AA
134 ipnos diags: Control (1/0) :: layer-2 apply 3 0x0701 100 0 0
135 Successfully applied packet-log-profile on LI
136 ipnos diags: Control (1/0) ::
142 o Handle a case where an empty line doesn't exists as a delimiter of
143 each packet. If the output is sent to a control blade and
144 displayed as an event log, there's always an empty line between
145 each packet output, but it may not be true when it is an PE
148 o Some telnet client on Windows may put in a line break at 80
149 columns when it save the session to a text file ("CRT" is such an
150 example). I don't think it's a good idea for the telnet client to
151 do so, but CRT is widely used in Windows community, I should
152 take care of that in the future.
156 /* Magic text to check for CoSine L2 debug output */
157 #define COSINE_HDR_MAGIC_STR1 "l2-tx"
158 #define COSINE_HDR_MAGIC_STR2 "l2-rx"
160 /* Magic text for start of packet */
161 #define COSINE_REC_MAGIC_STR1 COSINE_HDR_MAGIC_STR1
162 #define COSINE_REC_MAGIC_STR2 COSINE_HDR_MAGIC_STR2
164 #define COSINE_HEADER_LINES_TO_CHECK 200
165 #define COSINE_LINE_LENGTH 240
167 #define COSINE_MAX_PACKET_LEN 65536
169 static gboolean empty_line(const gchar *line);
170 static gint64 cosine_seek_next_packet(wtap *wth, int *err, gchar **err_info,
172 static gboolean cosine_check_file_type(wtap *wth, int *err, gchar **err_info);
173 static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
174 gint64 *data_offset);
175 static gboolean cosine_seek_read(wtap *wth, gint64 seek_off,
176 union wtap_pseudo_header *pseudo_header, guint8 *pd,
177 int len, int *err, gchar **err_info);
178 static int parse_cosine_rec_hdr(wtap *wth, const char *line,
179 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
180 static int parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf,
181 int *err, gchar **err_info);
182 static int parse_single_hex_dump_line(char* rec, guint8 *buf,
185 /* Returns TRUE if the line appears to be an empty line. Otherwise it
187 static gboolean empty_line(const gchar *line)
190 if (isspace((guchar)*line)) {
203 /* Seeks to the beginning of the next packet, and returns the
204 byte offset. Copy the header line to hdr. Returns -1 on failure,
205 and sets "*err" to the error, sets "*err_info" to null or an
206 additional error string, and sets hdr to NULL. */
207 static gint64 cosine_seek_next_packet(wtap *wth, int *err, gchar **err_info,
211 char buf[COSINE_LINE_LENGTH];
214 cur_off = file_tell(wth->fh);
217 *err = file_error(wth->fh, err_info);
221 if (file_gets(buf, sizeof(buf), wth->fh) != NULL) {
222 if (strstr(buf, COSINE_REC_MAGIC_STR1) ||
223 strstr(buf, COSINE_REC_MAGIC_STR2)) {
224 g_strlcpy(hdr, buf, COSINE_LINE_LENGTH);
228 if (file_eof(wth->fh)) {
232 /* We got an error. */
233 *err = file_error(wth->fh, err_info);
242 /* Look through the first part of a file to see if this is
243 * a CoSine L2 debug output.
245 * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
246 * if we get an I/O error, "*err" will be set to a non-zero value and
247 * "*err_info" will be set to null or an additional error string.
249 static gboolean cosine_check_file_type(wtap *wth, int *err, gchar **err_info)
251 char buf[COSINE_LINE_LENGTH];
255 buf[COSINE_LINE_LENGTH-1] = '\0';
257 for (line = 0; line < COSINE_HEADER_LINES_TO_CHECK; line++) {
258 if (file_gets(buf, COSINE_LINE_LENGTH, wth->fh) != NULL) {
260 reclen = strlen(buf);
261 if (reclen < strlen(COSINE_HDR_MAGIC_STR1) ||
262 reclen < strlen(COSINE_HDR_MAGIC_STR2)) {
266 if (strstr(buf, COSINE_HDR_MAGIC_STR1) ||
267 strstr(buf, COSINE_HDR_MAGIC_STR2)) {
272 if (file_eof(wth->fh))
275 *err = file_error(wth->fh, err_info);
284 int cosine_open(wtap *wth, int *err, gchar **err_info)
286 /* Look for CoSine header */
287 if (!cosine_check_file_type(wth, err, err_info)) {
294 if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) /* rewind */
297 wth->data_offset = 0;
298 wth->file_encap = WTAP_ENCAP_COSINE;
299 wth->file_type = WTAP_FILE_COSINE;
300 wth->snapshot_length = 0; /* not known */
301 wth->subtype_read = cosine_read;
302 wth->subtype_seek_read = cosine_seek_read;
303 wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
308 /* Find the next packet and parse it; called from wtap_read(). */
309 static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
315 char line[COSINE_LINE_LENGTH];
317 /* Find the next packet */
318 offset = cosine_seek_next_packet(wth, err, err_info, line);
322 /* Parse the header */
323 pkt_len = parse_cosine_rec_hdr(wth, line, &wth->pseudo_header, err,
328 /* Make sure we have enough room for the packet */
329 buffer_assure_space(wth->frame_buffer, COSINE_MAX_PACKET_LEN);
330 buf = buffer_start_ptr(wth->frame_buffer);
332 /* Convert the ASCII hex dump to binary data */
333 if ((caplen = parse_cosine_hex_dump(wth->fh, pkt_len, buf, err,
337 wth->data_offset = offset;
338 wth->phdr.caplen = caplen;
339 *data_offset = offset;
343 /* Used to read packets in random-access fashion */
345 cosine_seek_read (wtap *wth, gint64 seek_off,
346 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
347 int *err, gchar **err_info)
349 char line[COSINE_LINE_LENGTH];
351 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
354 if (file_gets(line, COSINE_LINE_LENGTH, wth->random_fh) == NULL) {
355 *err = file_error(wth->random_fh, err_info);
357 *err = WTAP_ERR_SHORT_READ;
362 if (parse_cosine_rec_hdr(NULL, line, pseudo_header, err, err_info) == -1)
365 return parse_cosine_hex_dump(wth->random_fh, len, pd, err, err_info);
368 /* Parses a packet record header. There are two possible formats:
369 1) output to a control blade with date and time
370 2002-5-10,20:1:31.4: l2-tx (FR:3/7/1:1), Length:18, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
371 2) output to PE without date and time
372 l2-tx (FR:3/7/1:1), Length:18, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0] */
374 parse_cosine_rec_hdr(wtap *wth, const char *line,
375 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
377 int num_items_scanned;
378 int yy, mm, dd, hr, min, sec, csec, pkt_len;
379 int pro, off, pri, rm, error;
381 char if_name[COSINE_MAX_IF_NAME_LEN] = "", direction[6] = "";
384 if (sscanf(line, "%4d-%2d-%2d,%2d:%2d:%2d.%9d:",
385 &yy, &mm, &dd, &hr, &min, &sec, &csec) == 7) {
386 /* appears to be output to a control blade */
387 num_items_scanned = sscanf(line,
388 "%4d-%2d-%2d,%2d:%2d:%2d.%9d: %5s (%127[A-Za-z0-9/:]), Length:%9d, Pro:%9d, Off:%9d, Pri:%9d, RM:%9d, Err:%9d [%8x, %8x]",
389 &yy, &mm, &dd, &hr, &min, &sec, &csec,
390 direction, if_name, &pkt_len,
391 &pro, &off, &pri, &rm, &error,
394 if (num_items_scanned != 17) {
395 *err = WTAP_ERR_BAD_RECORD;
396 *err_info = g_strdup("cosine: purported control blade line doesn't have code values");
400 /* appears to be output to PE */
401 num_items_scanned = sscanf(line,
402 "%5s (%127[A-Za-z0-9/:]), Length:%9d, Pro:%9d, Off:%9d, Pri:%9d, RM:%9d, Err:%9d [%8x, %8x]",
403 direction, if_name, &pkt_len,
404 &pro, &off, &pri, &rm, &error,
407 if (num_items_scanned != 10) {
408 *err = WTAP_ERR_BAD_RECORD;
409 *err_info = g_strdup("cosine: header line is neither control blade nor PE output");
412 yy = mm = dd = hr = min = sec = csec = 0;
416 tm.tm_year = yy - 1900;
423 wth->phdr.ts.secs = mktime(&tm);
424 wth->phdr.ts.nsecs = csec * 10000000;
425 wth->phdr.len = pkt_len;
427 /* XXX need to handle other encapsulations like Cisco HDLC,
428 Frame Relay and ATM */
429 if (strncmp(if_name, "TEST:", 5) == 0) {
430 pseudo_header->cosine.encap = COSINE_ENCAP_TEST;
431 } else if (strncmp(if_name, "PPoATM:", 7) == 0) {
432 pseudo_header->cosine.encap = COSINE_ENCAP_PPoATM;
433 } else if (strncmp(if_name, "PPoFR:", 6) == 0) {
434 pseudo_header->cosine.encap = COSINE_ENCAP_PPoFR;
435 } else if (strncmp(if_name, "ATM:", 4) == 0) {
436 pseudo_header->cosine.encap = COSINE_ENCAP_ATM;
437 } else if (strncmp(if_name, "FR:", 3) == 0) {
438 pseudo_header->cosine.encap = COSINE_ENCAP_FR;
439 } else if (strncmp(if_name, "HDLC:", 5) == 0) {
440 pseudo_header->cosine.encap = COSINE_ENCAP_HDLC;
441 } else if (strncmp(if_name, "PPP:", 4) == 0) {
442 pseudo_header->cosine.encap = COSINE_ENCAP_PPP;
443 } else if (strncmp(if_name, "ETH:", 4) == 0) {
444 pseudo_header->cosine.encap = COSINE_ENCAP_ETH;
446 pseudo_header->cosine.encap = COSINE_ENCAP_UNKNOWN;
448 if (strncmp(direction, "l2-tx", 5) == 0) {
449 pseudo_header->cosine.direction = COSINE_DIR_TX;
450 } else if (strncmp(direction, "l2-rx", 5) == 0) {
451 pseudo_header->cosine.direction = COSINE_DIR_RX;
453 g_strlcpy(pseudo_header->cosine.if_name, if_name,
454 COSINE_MAX_IF_NAME_LEN);
455 pseudo_header->cosine.pro = pro;
456 pseudo_header->cosine.off = off;
457 pseudo_header->cosine.pri = pri;
458 pseudo_header->cosine.rm = rm;
459 pseudo_header->cosine.err = error;
464 /* Converts ASCII hex dump to binary data. Returns the capture length.
465 If any error is encountered, -1 is returned. */
467 parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
470 gchar line[COSINE_LINE_LENGTH];
471 int i, hex_lines, n, caplen = 0;
473 /* Calculate the number of hex dump lines, each
474 * containing 16 bytes of data */
475 hex_lines = pkt_len / 16 + ((pkt_len % 16) ? 1 : 0);
477 for (i = 0; i < hex_lines; i++) {
478 if (file_gets(line, COSINE_LINE_LENGTH, fh) == NULL) {
479 *err = file_error(fh, err_info);
481 *err = WTAP_ERR_SHORT_READ;
485 if (empty_line(line)) {
488 if ((n = parse_single_hex_dump_line(line, buf, i*16)) == -1) {
489 *err = WTAP_ERR_BAD_RECORD;
490 *err_info = g_strdup("cosine: hex dump line doesn't have 16 numbers");
499 /* Take a string representing one line from a hex dump and converts
500 * the text to binary data. We place the bytes in the buffer at the
503 * Returns number of bytes successfully read, -1 if bad. */
505 parse_single_hex_dump_line(char* rec, guint8 *buf, guint byte_offset)
507 int num_items_scanned, i;
508 unsigned int bytes[16];
510 num_items_scanned = sscanf(rec, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
511 &bytes[0], &bytes[1], &bytes[2], &bytes[3],
512 &bytes[4], &bytes[5], &bytes[6], &bytes[7],
513 &bytes[8], &bytes[9], &bytes[10], &bytes[11],
514 &bytes[12], &bytes[13], &bytes[14], &bytes[15]);
515 if (num_items_scanned == 0)
518 if (num_items_scanned > 16)
519 num_items_scanned = 16;
521 for (i=0; i<num_items_scanned; i++) {
522 buf[byte_offset + i] = (guint8)bytes[i];
525 return num_items_scanned;