Home | History | Annotate | Line # | Download | only in routed
trace.c revision 1.28
      1 /*	$NetBSD: trace.c,v 1.28 2003/04/21 08:54:42 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgment:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #define	RIPCMDS
     37 #include "defs.h"
     38 #include "pathnames.h"
     39 #include <sys/stat.h>
     40 #include <sys/signal.h>
     41 #include <fcntl.h>
     42 
     43 #ifdef __NetBSD__
     44 __RCSID("$NetBSD: trace.c,v 1.28 2003/04/21 08:54:42 itojun Exp $");
     45 #elif defined(__FreeBSD__)
     46 __RCSID("$FreeBSD$");
     47 #else
     48 __RCSID("Revision: 2.27 ");
     49 #ident "Revision: 2.27 "
     50 #endif
     51 
     52 
     53 #ifdef sgi
     54 /* use *stat64 for files on large filesystems */
     55 #define stat	stat64
     56 #endif
     57 
     58 #define	NRECORDS	50		/* size of circular trace buffer */
     59 
     60 int	tracelevel, new_tracelevel;
     61 FILE	*ftrace = stdout;		/* output trace file */
     62 static const char *sigtrace_pat = "%s";
     63 static char savetracename[MAXPATHLEN+1];
     64 char	inittracename[MAXPATHLEN+1];
     65 int	file_trace;			/* 1=tracing to file, not stdout */
     66 
     67 static void trace_dump(void);
     68 static void tmsg(const char *, ...) PATTRIB(1,2);
     69 
     70 
     71 /* convert string to printable characters
     72  */
     73 static char *
     74 qstring(u_char *s, int len)
     75 {
     76 	static char buf[8*20+1];
     77 	char *p;
     78 	u_char *s2, c;
     79 
     80 
     81 	for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
     82 		c = *s++;
     83 		if (c == '\0') {
     84 			for (s2 = s+1; s2 < &s[len]; s2++) {
     85 				if (*s2 != '\0')
     86 					break;
     87 			}
     88 			if (s2 >= &s[len])
     89 			    goto exit;
     90 		}
     91 
     92 		if (c >= ' ' && c < 0x7f && c != '\\') {
     93 			*p++ = c;
     94 			continue;
     95 		}
     96 		*p++ = '\\';
     97 		switch (c) {
     98 		case '\\':
     99 			*p++ = '\\';
    100 			break;
    101 		case '\n':
    102 			*p++= 'n';
    103 			break;
    104 		case '\r':
    105 			*p++= 'r';
    106 			break;
    107 		case '\t':
    108 			*p++ = 't';
    109 			break;
    110 		case '\b':
    111 			*p++ = 'b';
    112 			break;
    113 		default:
    114 			p += sprintf(p,"%o",c);
    115 			break;
    116 		}
    117 	}
    118 exit:
    119 	*p = '\0';
    120 	return buf;
    121 }
    122 
    123 
    124 /* convert IP address to a string, but not into a single buffer
    125  */
    126 char *
    127 naddr_ntoa(naddr a)
    128 {
    129 #define NUM_BUFS 4
    130 	static int bufno;
    131 	static struct {
    132 	    char    str[16];		/* xxx.xxx.xxx.xxx\0 */
    133 	} bufs[NUM_BUFS];
    134 	char *s;
    135 	struct in_addr addr;
    136 
    137 	addr.s_addr = a;
    138 	strlcpy(bufs[bufno].str, inet_ntoa(addr), sizeof(bufs[bufno].str));
    139 	s = bufs[bufno].str;
    140 	bufno = (bufno+1) % NUM_BUFS;
    141 	return s;
    142 #undef NUM_BUFS
    143 }
    144 
    145 
    146 const char *
    147 saddr_ntoa(struct sockaddr *sa)
    148 {
    149 	return (sa == 0) ? "?" : naddr_ntoa(S_ADDR(sa));
    150 }
    151 
    152 
    153 static char *
    154 ts(time_t secs) {
    155 	static char s[20];
    156 
    157 	secs += epoch.tv_sec;
    158 #ifdef sgi
    159 	(void)cftime(s, "%T", &secs);
    160 #else
    161 	memcpy(s, ctime(&secs)+11, 8);
    162 	s[8] = '\0';
    163 #endif
    164 	return s;
    165 }
    166 
    167 
    168 /* On each event, display a time stamp.
    169  * This assumes that 'now' is update once for each event, and
    170  * that at least now.tv_usec changes.
    171  */
    172 static struct timeval lastlog_time;
    173 
    174 void
    175 lastlog(void)
    176 {
    177 	if (lastlog_time.tv_sec != now.tv_sec
    178 	    || lastlog_time.tv_usec != now.tv_usec) {
    179 		(void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
    180 		lastlog_time = now;
    181 	}
    182 }
    183 
    184 
    185 static void
    186 tmsg(const char *p, ...)
    187 {
    188 	va_list args;
    189 
    190 	if (ftrace != 0) {
    191 		lastlog();
    192 		va_start(args, p);
    193 		vfprintf(ftrace, p, args);
    194 		va_end(args);
    195 		(void)fputc('\n',ftrace);
    196 		fflush(ftrace);
    197 	}
    198 }
    199 
    200 
    201 void
    202 trace_close(int zap_stdio)
    203 {
    204 	int fd;
    205 
    206 
    207 	fflush(stdout);
    208 	fflush(stderr);
    209 
    210 	if (ftrace != 0 && zap_stdio) {
    211 		if (ftrace != stdout)
    212 			fclose(ftrace);
    213 		ftrace = 0;
    214 		fd = open(_PATH_DEVNULL, O_RDWR);
    215 		if (isatty(STDIN_FILENO))
    216 			(void)dup2(fd, STDIN_FILENO);
    217 		if (isatty(STDOUT_FILENO))
    218 			(void)dup2(fd, STDOUT_FILENO);
    219 		if (isatty(STDERR_FILENO))
    220 			(void)dup2(fd, STDERR_FILENO);
    221 		(void)close(fd);
    222 	}
    223 	lastlog_time.tv_sec = 0;
    224 }
    225 
    226 
    227 void
    228 trace_flush(void)
    229 {
    230 	if (ftrace != 0) {
    231 		fflush(ftrace);
    232 		if (ferror(ftrace))
    233 			trace_off("tracing off: %s", strerror(ferror(ftrace)));
    234 	}
    235 }
    236 
    237 
    238 void
    239 trace_off(const char *p, ...)
    240 {
    241 	va_list args;
    242 
    243 
    244 	if (ftrace != 0) {
    245 		lastlog();
    246 		va_start(args, p);
    247 		vfprintf(ftrace, p, args);
    248 		va_end(args);
    249 		(void)fputc('\n',ftrace);
    250 	}
    251 	trace_close(file_trace);
    252 
    253 	new_tracelevel = tracelevel = 0;
    254 }
    255 
    256 
    257 /* log a change in tracing
    258  */
    259 void
    260 tracelevel_msg(const char *pat,
    261 	       int dump)		/* -1=no dump, 0=default, 1=force */
    262 {
    263 	static const char *off_msgs[MAX_TRACELEVEL] = {
    264 		"Tracing actions stopped",
    265 		"Tracing packets stopped",
    266 		"Tracing packet contents stopped",
    267 		"Tracing kernel changes stopped",
    268 	};
    269 	static const char *on_msgs[MAX_TRACELEVEL] = {
    270 		"Tracing actions started",
    271 		"Tracing packets started",
    272 		"Tracing packet contents started",
    273 		"Tracing kernel changes started",
    274 	};
    275 	u_int old_tracelevel = tracelevel;
    276 
    277 
    278 	if (new_tracelevel < 0)
    279 		new_tracelevel = 0;
    280 	else if (new_tracelevel > MAX_TRACELEVEL)
    281 		new_tracelevel = MAX_TRACELEVEL;
    282 
    283 	if (new_tracelevel < tracelevel) {
    284 		if (new_tracelevel <= 0) {
    285 			trace_off(pat, off_msgs[0]);
    286 		} else do {
    287 			tmsg(pat, off_msgs[tracelevel]);
    288 		}
    289 		while (--tracelevel != new_tracelevel);
    290 
    291 	} else if (new_tracelevel > tracelevel) {
    292 		do {
    293 			tmsg(pat, on_msgs[tracelevel++]);
    294 		} while (tracelevel != new_tracelevel);
    295 	}
    296 
    297 	if (dump > 0
    298 	    || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
    299 		trace_dump();
    300 }
    301 
    302 
    303 void
    304 set_tracefile(const char *filename,
    305 	      const char *pat,
    306 	      int dump)			/* -1=no dump, 0=default, 1=force */
    307 {
    308 	struct stat stbuf;
    309 	FILE *n_ftrace;
    310 	const char *fn;
    311 
    312 
    313 	/* Allow a null filename to increase the level if the trace file
    314 	 * is already open or if coming from a trusted source, such as
    315 	 * a signal or the command line.
    316 	 */
    317 	if (filename == 0 || filename[0] == '\0') {
    318 		filename = 0;
    319 		if (ftrace == 0) {
    320 			if (inittracename[0] == '\0') {
    321 				msglog("missing trace file name");
    322 				return;
    323 			}
    324 			fn = inittracename;
    325 		} else {
    326 			fn = 0;
    327 		}
    328 
    329 	} else if (!strcmp(filename,"dump/../table")) {
    330 		trace_dump();
    331 		return;
    332 
    333 	} else {
    334 		/* Allow the file specified with "-T file" to be reopened,
    335 		 * but require all other names specified over the net to
    336 		 * match the official path.  The path can specify a directory
    337 		 * in which the file is to be created.
    338 		 */
    339 		if (strcmp(filename, inittracename)
    340 #ifdef _PATH_TRACE
    341 		    && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
    342 			|| strstr(filename,"../")
    343 			|| 0 > stat(_PATH_TRACE, &stbuf))
    344 #endif
    345 		    ) {
    346 			msglog("wrong trace file \"%s\"", filename);
    347 			return;
    348 		}
    349 
    350 		/* If the new tracefile exists, it must be a regular file.
    351 		 */
    352 		if (stat(filename, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode)) {
    353 			msglog("wrong type (%#x) of trace file \"%s\"",
    354 			       stbuf.st_mode, filename);
    355 			return;
    356 		}
    357 
    358 		fn = filename;
    359 	}
    360 
    361 	if (fn != 0) {
    362 		n_ftrace = fopen(fn, "a");
    363 		if (n_ftrace == 0) {
    364 			msglog("failed to open trace file \"%s\" %s",
    365 			       fn, strerror(errno));
    366 			if (fn == inittracename)
    367 				inittracename[0] = '\0';
    368 			return;
    369 		}
    370 
    371 		tmsg("switch to trace file %s", fn);
    372 
    373 		trace_close(file_trace = 1);
    374 
    375 		if (fn != savetracename)
    376 			strlcpy(savetracename, fn, sizeof(savetracename));
    377 		ftrace = n_ftrace;
    378 
    379 		fflush(stdout);
    380 		fflush(stderr);
    381 		dup2(fileno(ftrace), STDOUT_FILENO);
    382 		dup2(fileno(ftrace), STDERR_FILENO);
    383 	}
    384 
    385 	if (new_tracelevel == 0 || filename == 0)
    386 		new_tracelevel++;
    387 	tracelevel_msg(pat, dump != 0 ? dump : (filename != 0));
    388 }
    389 
    390 
    391 /* ARGSUSED */
    392 void
    393 sigtrace_on(int s UNUSED)
    394 {
    395 	new_tracelevel++;
    396 	sigtrace_pat = "SIGUSR1: %s";
    397 }
    398 
    399 
    400 /* ARGSUSED */
    401 void
    402 sigtrace_off(int s UNUSED)
    403 {
    404 	new_tracelevel--;
    405 	sigtrace_pat = "SIGUSR2: %s";
    406 }
    407 
    408 
    409 /* Set tracing after a signal.
    410  */
    411 void
    412 set_tracelevel(void)
    413 {
    414 	if (new_tracelevel == tracelevel)
    415 		return;
    416 
    417 	/* If tracing entirely off, and there was no tracefile specified
    418 	 * on the command line, then leave it off.
    419 	 */
    420 	if (new_tracelevel > tracelevel && ftrace == 0) {
    421 		if (savetracename[0] != '\0') {
    422 			set_tracefile(savetracename,sigtrace_pat,0);
    423 		} else if (inittracename[0] != '\0') {
    424 				set_tracefile(inittracename,sigtrace_pat,0);
    425 		} else {
    426 			new_tracelevel = 0;
    427 			return;
    428 		}
    429 	} else {
    430 		tracelevel_msg(sigtrace_pat, 0);
    431 	}
    432 }
    433 
    434 
    435 /* display an address
    436  */
    437 char *
    438 addrname(naddr	addr,			/* in network byte order */
    439 	 naddr	mask,
    440 	 int	force)			/* 0=show mask if nonstandard, */
    441 {					/*	1=always show mask, 2=never */
    442 #define NUM_BUFS 4
    443 	static int bufno;
    444 	static struct {
    445 	    char    str[15+20];
    446 	} bufs[NUM_BUFS];
    447 	char *s, *sp;
    448 	naddr dmask;
    449 	size_t l;
    450 	int i;
    451 
    452 	strlcpy(bufs[bufno].str, naddr_ntoa(addr), sizeof(bufs[bufno].str));
    453 	s = bufs[bufno].str;
    454 	l = sizeof(bufs[bufno].str);
    455 	bufno = (bufno+1) % NUM_BUFS;
    456 
    457 	if (force == 1 || (force == 0 && mask != std_mask(addr))) {
    458 		sp = &s[strlen(s)];
    459 
    460 		dmask = mask & -mask;
    461 		if (mask + dmask == 0) {
    462 			for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
    463 				continue;
    464 			(void)snprintf(sp, s + l - sp, "/%d", 32-i);
    465 
    466 		} else {
    467 			(void)snprintf(sp, s + l - sp, " (mask %#x)",
    468 			    (u_int)mask);
    469 		}
    470 	}
    471 
    472 	return s;
    473 #undef NUM_BUFS
    474 }
    475 
    476 
    477 /* display a bit-field
    478  */
    479 struct bits {
    480 	u_int	bits_mask;
    481 	u_int	bits_clear;
    482 	const char *bits_name;
    483 };
    484 
    485 static struct bits if_bits[] = {
    486 	{ IFF_LOOPBACK,		0,		"LOOPBACK" },
    487 	{ IFF_POINTOPOINT,	0,		"PT-TO-PT" },
    488 	{ 0,			0,		0}
    489 };
    490 
    491 static struct bits is_bits[] = {
    492 	{ IS_ALIAS,		0,		"ALIAS" },
    493 	{ IS_SUBNET,		0,		"" },
    494 	{ IS_REMOTE,		(IS_NO_RDISC
    495 				 | IS_BCAST_RDISC), "REMOTE" },
    496 	{ IS_PASSIVE,		(IS_NO_RDISC
    497 				 | IS_NO_RIP
    498 				 | IS_NO_SUPER_AG
    499 				 | IS_PM_RDISC
    500 				 | IS_NO_AG),	"PASSIVE" },
    501 	{ IS_EXTERNAL,		0,		"EXTERNAL" },
    502 	{ IS_CHECKED,		0,		"" },
    503 	{ IS_ALL_HOSTS,		0,		"" },
    504 	{ IS_ALL_ROUTERS,	0,		"" },
    505 	{ IS_DISTRUST,		0,		"DISTRUST" },
    506 	{ IS_BROKE,		IS_SICK,	"BROKEN" },
    507 	{ IS_SICK,		0,		"SICK" },
    508 	{ IS_DUP,		0,		"DUPLICATE" },
    509 	{ IS_REDIRECT_OK,	0,		"REDIRECT_OK" },
    510 	{ IS_NEED_NET_SYN,	0,		"" },
    511 	{ IS_NO_AG,		IS_NO_SUPER_AG,	"NO_AG" },
    512 	{ IS_NO_SUPER_AG,	0,		"NO_SUPER_AG" },
    513 	{ (IS_NO_RIPV1_IN
    514 	   | IS_NO_RIPV2_IN
    515 	   | IS_NO_RIPV1_OUT
    516 	   | IS_NO_RIPV2_OUT),	0,		"NO_RIP" },
    517 	{ (IS_NO_RIPV1_IN
    518 	   | IS_NO_RIPV1_OUT),	0,		"RIPV2" },
    519 	{ IS_NO_RIPV1_IN,	0,		"NO_RIPV1_IN" },
    520 	{ IS_NO_RIPV2_IN,	0,		"NO_RIPV2_IN" },
    521 	{ IS_NO_RIPV1_OUT,	0,		"NO_RIPV1_OUT" },
    522 	{ IS_NO_RIPV2_OUT,	0,		"NO_RIPV2_OUT" },
    523 	{ (IS_NO_ADV_IN
    524 	   | IS_NO_SOL_OUT
    525 	   | IS_NO_ADV_OUT),	IS_BCAST_RDISC,	"NO_RDISC" },
    526 	{ IS_NO_SOL_OUT,	0,		"NO_SOLICIT" },
    527 	{ IS_SOL_OUT,		0,		"SEND_SOLICIT" },
    528 	{ IS_NO_ADV_OUT,	IS_BCAST_RDISC,	"NO_RDISC_ADV" },
    529 	{ IS_ADV_OUT,		0,		"RDISC_ADV" },
    530 	{ IS_BCAST_RDISC,	0,		"BCAST_RDISC" },
    531 	{ IS_PM_RDISC,		0,		"" },
    532 	{ 0,			0,		"%#x"}
    533 };
    534 
    535 static struct bits rs_bits[] = {
    536 	{ RS_IF,		0,		"IF" },
    537 	{ RS_NET_INT,		RS_NET_SYN,	"NET_INT" },
    538 	{ RS_NET_SYN,		0,		"NET_SYN" },
    539 	{ RS_SUBNET,		0,		"" },
    540 	{ RS_LOCAL,		0,		"LOCAL" },
    541 	{ RS_MHOME,		0,		"MHOME" },
    542 	{ RS_STATIC,		0,		"STATIC" },
    543 	{ RS_RDISC,		0,		"RDISC" },
    544 	{ 0,			0,		"%#x"}
    545 };
    546 
    547 
    548 static void
    549 trace_bits(const struct bits *tbl,
    550 	   u_int field,
    551 	   int force)
    552 {
    553 	u_int b;
    554 	char c;
    555 
    556 	if (force) {
    557 		(void)putc('<', ftrace);
    558 		c = 0;
    559 	} else {
    560 		c = '<';
    561 	}
    562 
    563 	while (field != 0
    564 	       && (b = tbl->bits_mask) != 0) {
    565 		if ((b & field) == b) {
    566 			if (tbl->bits_name[0] != '\0') {
    567 				if (c)
    568 					(void)putc(c, ftrace);
    569 				(void)fprintf(ftrace, "%s", tbl->bits_name);
    570 				c = '|';
    571 			}
    572 			if (0 == (field &= ~(b | tbl->bits_clear)))
    573 				break;
    574 		}
    575 		tbl++;
    576 	}
    577 	if (field != 0 && tbl->bits_name != 0) {
    578 		if (c)
    579 			(void)putc(c, ftrace);
    580 		(void)fprintf(ftrace, tbl->bits_name, field);
    581 		c = '|';
    582 	}
    583 
    584 	if (c != '<' || force)
    585 		(void)fputs("> ", ftrace);
    586 }
    587 
    588 
    589 char *
    590 rtname(naddr dst,
    591        naddr mask,
    592        naddr gate)
    593 {
    594 	static char buf[3*4+3+1+2+3	/* "xxx.xxx.xxx.xxx/xx-->" */
    595 			+3*4+3+1];	/* "xxx.xxx.xxx.xxx" */
    596 	int i;
    597 
    598 	i = snprintf(buf, sizeof(buf), "%-16s-->", addrname(dst, mask, 0));
    599 	if (i >= sizeof(buf) || i < 0)
    600 		return buf;
    601 	(void)snprintf(&buf[i], sizeof(buf) - i, "%-*s", 15+20-MAX(20, i),
    602 	    naddr_ntoa(gate));
    603 	return buf;
    604 }
    605 
    606 
    607 static void
    608 print_rts(struct rt_spare *rts,
    609 	  int force_metric,		/* -1=suppress, 0=default */
    610 	  int force_ifp,		/* -1=suppress, 0=default */
    611 	  int force_router,		/* -1=suppress, 0=default, 1=display */
    612 	  int force_tag,		/* -1=suppress, 0=default, 1=display */
    613 	  int force_time)		/* 0=suppress, 1=display */
    614 {
    615 	int i;
    616 
    617 
    618 	if (force_metric >= 0)
    619 		(void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
    620 	if (force_ifp >= 0)
    621 		(void)fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
    622 					      "if?" : rts->rts_ifp->int_name));
    623 	if (force_router > 0
    624 	    || (force_router == 0 && rts->rts_router != rts->rts_gate))
    625 		(void)fprintf(ftrace, "router=%s ",
    626 			      naddr_ntoa(rts->rts_router));
    627 	if (force_time > 0)
    628 		(void)fprintf(ftrace, "%s ", ts(rts->rts_time));
    629 	if (force_tag > 0
    630 	    || (force_tag == 0 && rts->rts_tag != 0))
    631 		(void)fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
    632 	if (rts->rts_de_ag != 0) {
    633 		for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
    634 			continue;
    635 		(void)fprintf(ftrace, "de_ag=%d ", i);
    636 	}
    637 
    638 }
    639 
    640 
    641 void
    642 trace_if(const char *act,
    643 	 struct interface *ifp)
    644 {
    645 	if (!TRACEACTIONS || ftrace == 0)
    646 		return;
    647 
    648 	lastlog();
    649 	(void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
    650 	(void)fprintf(ftrace, "%-15s-->%-15s ",
    651 		      naddr_ntoa(ifp->int_addr),
    652 		      addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
    653 				? ifp->int_dstaddr
    654 				: htonl(ifp->int_net)),
    655 			       ifp->int_mask, 1));
    656 	if (ifp->int_metric != 0)
    657 		(void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
    658 	if (ifp->int_adj_inmetric != 0)
    659 		(void)fprintf(ftrace, "adj_inmetric=%u ",
    660 			      ifp->int_adj_inmetric);
    661 	if (ifp->int_adj_outmetric != 0)
    662 		(void)fprintf(ftrace, "adj_outmetric=%u ",
    663 			      ifp->int_adj_outmetric);
    664 	if (!IS_RIP_OUT_OFF(ifp->int_state)
    665 	    && ifp->int_d_metric != 0)
    666 		(void)fprintf(ftrace, "fake_default=%u ", ifp->int_d_metric);
    667 	trace_bits(if_bits, ifp->int_if_flags, 0);
    668 	trace_bits(is_bits, ifp->int_state, 0);
    669 	(void)fputc('\n',ftrace);
    670 }
    671 
    672 
    673 void
    674 trace_upslot(struct rt_entry *rt,
    675 	     struct rt_spare *rts,
    676 	     struct rt_spare *new)
    677 {
    678 	if (!TRACEACTIONS || ftrace == 0)
    679 		return;
    680 
    681 	if (rts->rts_gate == new->rts_gate
    682 	    && rts->rts_router == new->rts_router
    683 	    && rts->rts_metric == new->rts_metric
    684 	    && rts->rts_tag == new->rts_tag
    685 	    && rts->rts_de_ag == new->rts_de_ag)
    686 		return;
    687 
    688 	lastlog();
    689 	if (new->rts_gate == 0) {
    690 		(void)fprintf(ftrace, "Del #%d %-35s ",
    691 			      (int)(rts - rt->rt_spares),
    692 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
    693 		print_rts(rts, 0,0,0,0,
    694 			  (rts != rt->rt_spares
    695 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
    696 
    697 	} else if (rts->rts_gate != RIP_DEFAULT) {
    698 		(void)fprintf(ftrace, "Chg #%d %-35s ",
    699 			      (int)(rts - rt->rt_spares),
    700 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
    701 		print_rts(rts, 0,0,
    702 			  rts->rts_gate != new->rts_gate,
    703 			  rts->rts_tag != new->rts_tag,
    704 			  rts != rt->rt_spares || AGE_RT(rt->rt_state,
    705 							rt->rt_ifp));
    706 
    707 		(void)fprintf(ftrace, "\n       %19s%-16s ", "",
    708 			      (new->rts_gate != rts->rts_gate
    709 			       ? naddr_ntoa(new->rts_gate) : ""));
    710 		print_rts(new,
    711 			  -(new->rts_metric == rts->rts_metric),
    712 			  -(new->rts_ifp == rts->rts_ifp),
    713 			  0,
    714 			  rts->rts_tag != new->rts_tag,
    715 			  (new->rts_time != rts->rts_time
    716 			   && (rts != rt->rt_spares
    717 			       || AGE_RT(rt->rt_state, new->rts_ifp))));
    718 
    719 	} else {
    720 		(void)fprintf(ftrace, "Add #%d %-35s ",
    721 			      (int)(rts - rt->rt_spares),
    722 			      rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
    723 		print_rts(new, 0,0,0,0,
    724 			  (rts != rt->rt_spares
    725 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
    726 	}
    727 	(void)fputc('\n',ftrace);
    728 }
    729 
    730 
    731 /* miscellaneous message checked by the caller
    732  */
    733 void
    734 trace_misc(const char *p, ...)
    735 {
    736 	va_list args;
    737 
    738 	if (ftrace == 0)
    739 		return;
    740 
    741 	lastlog();
    742 	va_start(args, p);
    743 	vfprintf(ftrace, p, args);
    744 	va_end(args);
    745 	(void)fputc('\n',ftrace);
    746 }
    747 
    748 
    749 /* display a message if tracing actions
    750  */
    751 void
    752 trace_act(const char *p, ...)
    753 {
    754 	va_list args;
    755 
    756 	if (!TRACEACTIONS || ftrace == 0)
    757 		return;
    758 
    759 	lastlog();
    760 	va_start(args, p);
    761 	vfprintf(ftrace, p, args);
    762 	va_end(args);
    763 	(void)fputc('\n',ftrace);
    764 }
    765 
    766 
    767 /* display a message if tracing packets
    768  */
    769 void
    770 trace_pkt(const char *p, ...)
    771 {
    772 	va_list args;
    773 
    774 	if (!TRACEPACKETS || ftrace == 0)
    775 		return;
    776 
    777 	lastlog();
    778 	va_start(args, p);
    779 	vfprintf(ftrace, p, args);
    780 	va_end(args);
    781 	(void)fputc('\n',ftrace);
    782 }
    783 
    784 
    785 void
    786 trace_change(struct rt_entry *rt,
    787 	     u_int	state,
    788 	     struct	rt_spare *new,
    789 	     const char	*label)
    790 {
    791 	if (ftrace == 0)
    792 		return;
    793 
    794 	if (rt->rt_metric == new->rts_metric
    795 	    && rt->rt_gate == new->rts_gate
    796 	    && rt->rt_router == new->rts_router
    797 	    && rt->rt_state == state
    798 	    && rt->rt_tag == new->rts_tag
    799 	    && rt->rt_de_ag == new->rts_de_ag)
    800 		return;
    801 
    802 	lastlog();
    803 	(void)fprintf(ftrace, "%s %-35s ",
    804 		      label,
    805 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
    806 	print_rts(rt->rt_spares,
    807 		  0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
    808 	trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
    809 
    810 	(void)fprintf(ftrace, "\n%*s %19s%-16s ",
    811 		      (int)strlen(label), "", "",
    812 		      (rt->rt_gate != new->rts_gate
    813 		       ? naddr_ntoa(new->rts_gate) : ""));
    814 	print_rts(new,
    815 		  -(new->rts_metric == rt->rt_metric),
    816 		  -(new->rts_ifp == rt->rt_ifp),
    817 		  0,
    818 		  rt->rt_tag != new->rts_tag,
    819 		  (rt->rt_time != new->rts_time
    820 		   && AGE_RT(rt->rt_state,new->rts_ifp)));
    821 	if (rt->rt_state != state)
    822 		trace_bits(rs_bits, state, 1);
    823 	(void)fputc('\n',ftrace);
    824 }
    825 
    826 
    827 void
    828 trace_add_del(const char * action, struct rt_entry *rt)
    829 {
    830 	if (ftrace == 0)
    831 		return;
    832 
    833 	lastlog();
    834 	(void)fprintf(ftrace, "%s    %-35s ",
    835 		      action,
    836 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
    837 	print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
    838 	trace_bits(rs_bits, rt->rt_state, 0);
    839 	(void)fputc('\n',ftrace);
    840 }
    841 
    842 
    843 /* ARGSUSED */
    844 static int
    845 walk_trace(struct radix_node *rn,
    846 	   struct walkarg *w UNUSED)
    847 {
    848 #define RT ((struct rt_entry *)rn)
    849 	struct rt_spare *rts;
    850 	int i;
    851 
    852 	(void)fprintf(ftrace, "  %-35s ",
    853 		      rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
    854 	print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
    855 	trace_bits(rs_bits, RT->rt_state, 0);
    856 	if (RT->rt_poison_time >= now_garbage
    857 	    && RT->rt_poison_metric < RT->rt_metric)
    858 		(void)fprintf(ftrace, "pm=%d@%s",
    859 			      RT->rt_poison_metric, ts(RT->rt_poison_time));
    860 
    861 	rts = &RT->rt_spares[1];
    862 	for (i = 1; i < NUM_SPARES; i++, rts++) {
    863 		if (rts->rts_gate != RIP_DEFAULT) {
    864 			(void)fprintf(ftrace,"\n    #%d%15s%-16s ",
    865 				      i, "", naddr_ntoa(rts->rts_gate));
    866 			print_rts(rts, 0,0,0,0,1);
    867 		}
    868 	}
    869 	(void)fputc('\n',ftrace);
    870 
    871 	return 0;
    872 }
    873 
    874 
    875 static void
    876 trace_dump(void)
    877 {
    878 	struct interface *ifp;
    879 
    880 	if (ftrace == 0)
    881 		return;
    882 	lastlog();
    883 
    884 	(void)fputs("current daemon state:\n", ftrace);
    885 	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next)
    886 		trace_if("", ifp);
    887 	(void)rn_walktree(rhead, walk_trace, 0);
    888 }
    889 
    890 
    891 void
    892 trace_rip(const char *dir1, const char *dir2,
    893 	  struct sockaddr_in *who,
    894 	  struct interface *ifp,
    895 	  struct rip *msg,
    896 	  int size)			/* total size of message */
    897 {
    898 	struct netinfo *n, *lim;
    899 #	define NA ((struct netauth*)n)
    900 	int i, seen_route;
    901 
    902 	if (!TRACEPACKETS || ftrace == 0)
    903 		return;
    904 
    905 	lastlog();
    906 	if (msg->rip_cmd >= RIPCMD_MAX
    907 	    || msg->rip_vers == 0) {
    908 		(void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
    909 			      " %s.%d size=%d\n",
    910 			      dir1, msg->rip_vers, msg->rip_cmd, dir2,
    911 			      naddr_ntoa(who->sin_addr.s_addr),
    912 			      ntohs(who->sin_port),
    913 			      size);
    914 		return;
    915 	}
    916 
    917 	(void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
    918 		      dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
    919 		      naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
    920 		      ifp ? " via " : "", ifp ? ifp->int_name : "");
    921 	if (!TRACECONTENTS)
    922 		return;
    923 
    924 	seen_route = 0;
    925 	switch (msg->rip_cmd) {
    926 	case RIPCMD_REQUEST:
    927 	case RIPCMD_RESPONSE:
    928 		n = msg->rip_nets;
    929 		lim = (struct netinfo *)((char*)msg + size);
    930 		for (; n < lim; n++) {
    931 			if (!seen_route
    932 			    && n->n_family == RIP_AF_UNSPEC
    933 			    && ntohl(n->n_metric) == HOPCNT_INFINITY
    934 			    && msg->rip_cmd == RIPCMD_REQUEST
    935 			    && (n+1 == lim
    936 				|| (n+2 == lim
    937 				    && (n+1)->n_family == RIP_AF_AUTH))) {
    938 				(void)fputs("\tQUERY ", ftrace);
    939 				if (n->n_dst != 0)
    940 					(void)fprintf(ftrace, "%s ",
    941 						      naddr_ntoa(n->n_dst));
    942 				if (n->n_mask != 0)
    943 					(void)fprintf(ftrace, "mask=%#x ",
    944 						      (u_int)ntohl(n->n_mask));
    945 				if (n->n_nhop != 0)
    946 					(void)fprintf(ftrace, "nhop=%s ",
    947 						      naddr_ntoa(n->n_nhop));
    948 				if (n->n_tag != 0)
    949 					(void)fprintf(ftrace, "tag=%#x ",
    950 						      ntohs(n->n_tag));
    951 				(void)fputc('\n',ftrace);
    952 				continue;
    953 			}
    954 
    955 			if (n->n_family == RIP_AF_AUTH) {
    956 				if (NA->a_type == RIP_AUTH_PW
    957 				    && n == msg->rip_nets) {
    958 					(void)fprintf(ftrace, "\tPassword"
    959 						      " Authentication:"
    960 						      " \"%s\"\n",
    961 						      qstring(NA->au.au_pw,
    962 							  RIP_AUTH_PW_LEN));
    963 					continue;
    964 				}
    965 
    966 				if (NA->a_type == RIP_AUTH_MD5
    967 				    && n == msg->rip_nets) {
    968 					(void)fprintf(ftrace,
    969 						      "\tMD5 Auth"
    970 						      " pkt_len=%d KeyID=%u"
    971 						      " auth_len=%d"
    972 						      " seqno=%#x"
    973 						      " rsvd=%#x,%#x\n",
    974 					    ntohs(NA->au.a_md5.md5_pkt_len),
    975 					    NA->au.a_md5.md5_keyid,
    976 					    NA->au.a_md5.md5_auth_len,
    977 					    (int)ntohl(NA->au.a_md5.md5_seqno),
    978 					    (int)ntohs(NA->au.a_md5.rsvd[0]),
    979 					    (int)ntohs(NA->au.a_md5.rsvd[1]));
    980 					continue;
    981 				}
    982 				(void)fprintf(ftrace,
    983 					      "\tAuthentication type %d: ",
    984 					      ntohs(NA->a_type));
    985 				for (i = 0;
    986 				     i < (int)sizeof(NA->au.au_pw);
    987 				     i++)
    988 					(void)fprintf(ftrace, "%02x ",
    989 						      NA->au.au_pw[i]);
    990 				(void)fputc('\n',ftrace);
    991 				continue;
    992 			}
    993 
    994 			seen_route = 1;
    995 			if (n->n_family != RIP_AF_INET) {
    996 				(void)fprintf(ftrace,
    997 					      "\t(af %d) %-18s mask=%#x ",
    998 					      ntohs(n->n_family),
    999 					      naddr_ntoa(n->n_dst),
   1000 					      (u_int)ntohl(n->n_mask));
   1001 			} else if (msg->rip_vers == RIPv1) {
   1002 				(void)fprintf(ftrace, "\t%-18s ",
   1003 					      addrname(n->n_dst,
   1004 						       ntohl(n->n_mask),
   1005 						       n->n_mask==0 ? 2 : 1));
   1006 			} else {
   1007 				(void)fprintf(ftrace, "\t%-18s ",
   1008 					      addrname(n->n_dst,
   1009 						       ntohl(n->n_mask),
   1010 						       n->n_mask==0 ? 2 : 0));
   1011 			}
   1012 			(void)fprintf(ftrace, "metric=%-2d ",
   1013 				      (u_int)ntohl(n->n_metric));
   1014 			if (n->n_nhop != 0)
   1015 				(void)fprintf(ftrace, " nhop=%s ",
   1016 					      naddr_ntoa(n->n_nhop));
   1017 			if (n->n_tag != 0)
   1018 				(void)fprintf(ftrace, "tag=%#x",
   1019 					      ntohs(n->n_tag));
   1020 			(void)fputc('\n',ftrace);
   1021 		}
   1022 		if (size != (char *)n - (char *)msg)
   1023 			(void)fprintf(ftrace, "truncated record, len %d\n",
   1024 				size);
   1025 		break;
   1026 
   1027 	case RIPCMD_TRACEON:
   1028 		fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
   1029 			msg->rip_tracefile);
   1030 		break;
   1031 
   1032 	case RIPCMD_TRACEOFF:
   1033 		break;
   1034 	}
   1035 }
   1036