xref: /csrg-svn/sbin/routed/trace.c (revision 15099)
19022Ssam #ifndef lint
2*15099Ssam static char sccsid[] = "@(#)trace.c	4.4 (Berkeley) 09/25/83";
39022Ssam #endif
49022Ssam 
59022Ssam /*
69022Ssam  * Routing Table Management Daemon
79022Ssam  */
89022Ssam #define	RIPCMDS
910245Ssam #include "defs.h"
109022Ssam 
119022Ssam #define	NRECORDS	50		/* size of circular trace buffer */
129022Ssam #ifdef DEBUG
139022Ssam FILE	*ftrace = stdout;
149022Ssam int	tracing = 1;
159022Ssam #endif
169022Ssam 
179022Ssam traceinit(ifp)
189022Ssam 	register struct interface *ifp;
199022Ssam {
209022Ssam 
219022Ssam 	if (iftraceinit(ifp, &ifp->int_input) &&
229022Ssam 	    iftraceinit(ifp, &ifp->int_output))
239022Ssam 		return;
249022Ssam 	tracing = 0;
259022Ssam 	fprintf(stderr, "traceinit: can't init %s\n", ifp->int_name);
269022Ssam }
279022Ssam 
289022Ssam static
299022Ssam iftraceinit(ifp, ifd)
309022Ssam 	struct interface *ifp;
319022Ssam 	register struct ifdebug *ifd;
329022Ssam {
339022Ssam 	register struct iftrace *t;
349022Ssam 
359022Ssam 	ifd->ifd_records =
369022Ssam 	  (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
379022Ssam 	if (ifd->ifd_records == 0)
389022Ssam 		return (0);
399022Ssam 	ifd->ifd_front = ifd->ifd_records;
409022Ssam 	for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
419022Ssam 		t->ift_size = 0;
429022Ssam 		t->ift_packet = 0;
439022Ssam 	}
449022Ssam 	ifd->ifd_if = ifp;
459022Ssam 	return (1);
469022Ssam }
479022Ssam 
489022Ssam traceon(file)
499022Ssam 	char *file;
509022Ssam {
519022Ssam 
529022Ssam 	if (ftrace != NULL)
539022Ssam 		return;
549022Ssam 	ftrace = fopen(file, "a");
559022Ssam 	if (ftrace == NULL)
569022Ssam 		return;
579022Ssam 	dup2(fileno(ftrace), 1);
589022Ssam 	dup2(fileno(ftrace), 2);
599022Ssam 	tracing = 1;
609022Ssam }
619022Ssam 
629022Ssam traceoff()
639022Ssam {
649022Ssam 	if (!tracing)
659022Ssam 		return;
669022Ssam 	if (ftrace != NULL)
679022Ssam 		fclose(ftrace);
689022Ssam 	ftrace = NULL;
699022Ssam 	tracing = 0;
709022Ssam }
719022Ssam 
729022Ssam trace(ifd, who, p, len, m)
739022Ssam 	register struct ifdebug *ifd;
749022Ssam 	struct sockaddr *who;
759022Ssam 	char *p;
769022Ssam 	int len, m;
779022Ssam {
789022Ssam 	register struct iftrace *t;
799022Ssam 
809022Ssam 	if (ifd->ifd_records == 0)
819022Ssam 		return;
829022Ssam 	t = ifd->ifd_front++;
839022Ssam 	if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
849022Ssam 		ifd->ifd_front = ifd->ifd_records;
859022Ssam 	if (t->ift_size > 0 && t->ift_packet)
869022Ssam 		free(t->ift_packet);
879022Ssam 	t->ift_packet = 0;
889022Ssam 	t->ift_stamp = time(0);
899022Ssam 	t->ift_who = *who;
909022Ssam 	if (len > 0) {
919022Ssam 		t->ift_packet = malloc(len);
929022Ssam 		if (t->ift_packet)
939022Ssam 			bcopy(p, t->ift_packet, len);
949022Ssam 		else
959022Ssam 			len = 0;
969022Ssam 	}
979022Ssam 	t->ift_size = len;
989022Ssam 	t->ift_metric = m;
999022Ssam }
1009022Ssam 
1019022Ssam traceaction(fd, action, rt)
1029022Ssam 	FILE *fd;
1039022Ssam 	char *action;
1049022Ssam 	struct rt_entry *rt;
1059022Ssam {
1069022Ssam 	struct sockaddr_in *dst, *gate;
1079022Ssam 	static struct bits {
1089022Ssam 		int	t_bits;
1099022Ssam 		char	*t_name;
1109022Ssam 	} flagbits[] = {
1119022Ssam 		{ RTF_UP,	"UP" },
1129022Ssam 		{ RTF_GATEWAY,	"GATEWAY" },
1139022Ssam 		{ RTF_HOST,	"HOST" },
1149022Ssam 		{ 0 }
1159022Ssam 	}, statebits[] = {
1169022Ssam 		{ RTS_PASSIVE,	"PASSIVE" },
1179022Ssam 		{ RTS_REMOTE,	"REMOTE" },
1189022Ssam 		{ RTS_INTERFACE,"INTERFACE" },
1199022Ssam 		{ RTS_CHANGED,	"CHANGED" },
1209022Ssam 		{ 0 }
1219022Ssam 	};
1229022Ssam 	register struct bits *p;
1239022Ssam 	register int first;
1249022Ssam 	char *cp;
1259022Ssam 	struct interface *ifp;
1269022Ssam 
1279022Ssam 	if (fd == NULL)
1289022Ssam 		return;
1299022Ssam 	fprintf(fd, "%s ", action);
1309022Ssam 	dst = (struct sockaddr_in *)&rt->rt_dst;
1319022Ssam 	gate = (struct sockaddr_in *)&rt->rt_router;
132*15099Ssam 	fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr));
133*15099Ssam 	fprintf(fd, "router %s, metric %d, flags",
134*15099Ssam 	     inet_ntoa(gate->sin_addr), rt->rt_metric);
1359022Ssam 	cp = " %s";
1369022Ssam 	for (first = 1, p = flagbits; p->t_bits > 0; p++) {
1379022Ssam 		if ((rt->rt_flags & p->t_bits) == 0)
1389022Ssam 			continue;
1399022Ssam 		fprintf(fd, cp, p->t_name);
1409022Ssam 		if (first) {
1419022Ssam 			cp = "|%s";
1429022Ssam 			first = 0;
1439022Ssam 		}
1449022Ssam 	}
1459022Ssam 	fprintf(fd, " state");
1469022Ssam 	cp = " %s";
1479022Ssam 	for (first = 1, p = statebits; p->t_bits > 0; p++) {
1489022Ssam 		if ((rt->rt_state & p->t_bits) == 0)
1499022Ssam 			continue;
1509022Ssam 		fprintf(fd, cp, p->t_name);
1519022Ssam 		if (first) {
1529022Ssam 			cp = "|%s";
1539022Ssam 			first = 0;
1549022Ssam 		}
1559022Ssam 	}
1569022Ssam 	putc('\n', fd);
1579022Ssam 	if ((rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
1589022Ssam 		dumpif(fd, rt->rt_ifp);
1599022Ssam 	fflush(fd);
1609022Ssam }
1619022Ssam 
1629022Ssam dumpif(fd, ifp)
1639022Ssam 	register struct interface *ifp;
1649022Ssam {
1659022Ssam 	register struct ifdebug *ifd;
1669022Ssam 
1679022Ssam 	fprintf(fd, "*** Packet history for interface %s ***\n",
1689022Ssam 		ifp->int_name);
1699022Ssam 	dumptrace(fd, "to", &ifp->int_output);
1709022Ssam 	dumptrace(fd, "from", &ifp->int_output);
1719022Ssam 	fprintf(fd, "*** end packet history ***\n");
1729022Ssam }
1739022Ssam 
1749022Ssam dumptrace(fd, dir, ifd)
1759022Ssam 	FILE *fd;
1769022Ssam 	char *dir;
1779022Ssam 	register struct ifdebug *ifd;
1789022Ssam {
1799022Ssam 	register struct iftrace *t;
1809022Ssam 	char *cp = !strcmp(dir, "to") ? "Output" : "Input";
1819022Ssam 
1829022Ssam 	if (ifd->ifd_front == ifd->ifd_records &&
1839022Ssam 	    ifd->ifd_front->ift_size == 0) {
1849022Ssam 		fprintf(fd, "%s: no packets.\n", cp);
1859022Ssam 		return;
1869022Ssam 	}
1879022Ssam 	fprintf(fd, "%s trace:\n", cp);
1889022Ssam 	for (t = ifd->ifd_front; t <= ifd->ifd_records + NRECORDS; t++) {
1899022Ssam 		if (t->ift_size == 0)
1909022Ssam 			continue;
1919022Ssam 		fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
1929022Ssam 			t->ift_metric);
1939022Ssam 		dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
1949022Ssam 	}
1959022Ssam 	for (t = ifd->ifd_records; t < ifd->ifd_front; t++) {
1969022Ssam 		if (t->ift_size == 0)
1979022Ssam 			continue;
1989022Ssam 		fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
1999022Ssam 			t->ift_metric);
2009022Ssam 		dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
2019022Ssam 	}
2029022Ssam }
2039022Ssam 
2049022Ssam dumppacket(fd, dir, who, cp, size)
2059022Ssam 	FILE *fd;
2069022Ssam 	struct sockaddr_in *who;		/* should be sockaddr */
2079022Ssam 	char *dir, *cp;
2089022Ssam 	register int size;
2099022Ssam {
2109022Ssam 	register struct rip *msg = (struct rip *)cp;
2119022Ssam 	register struct netinfo *n;
2129022Ssam 
2139022Ssam 	if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
214*15099Ssam 		fprintf(fd, "%s %s %s.%d", ripcmds[msg->rip_cmd],
215*15099Ssam 		    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
2169022Ssam 	else {
2179022Ssam 		fprintf(fd, "Bad cmd 0x%x %s %x.%d\n", msg->rip_cmd,
218*15099Ssam 		    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
2199022Ssam 		fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
2209022Ssam 		return;
2219022Ssam 	}
2229022Ssam 	switch (msg->rip_cmd) {
2239022Ssam 
2249022Ssam 	case RIPCMD_REQUEST:
2259022Ssam 	case RIPCMD_RESPONSE:
2269022Ssam 		fprintf(fd, ":\n");
2279022Ssam 		size -= 4 * sizeof (char);
2289022Ssam 		n = msg->rip_nets;
2299022Ssam 		for (; size > 0; n++, size -= sizeof (struct netinfo)) {
2309022Ssam 			if (size < sizeof (struct netinfo))
2319022Ssam 				break;
232*15099Ssam 			fprintf(fd, "\tdst %s metric %d\n",
233*15099Ssam #define	satosin(sa)	((struct sockaddr_in *)&sa)
234*15099Ssam 			     inet_ntoa(satosin(n->rip_dst)->sin_addr),
235*15099Ssam 			     ntohl(n->rip_metric));
2369022Ssam 		}
2379022Ssam 		break;
2389022Ssam 
2399022Ssam 	case RIPCMD_TRACEON:
2409022Ssam 		fprintf(fd, ", file=%*s\n", size, msg->rip_tracefile);
2419022Ssam 		break;
2429022Ssam 
2439022Ssam 	case RIPCMD_TRACEOFF:
2449022Ssam 		fprintf(fd, "\n");
2459022Ssam 		break;
2469022Ssam 	}
2479022Ssam }
248