xref: /csrg-svn/sbin/routed/trace.c (revision 22001)
1*22001Sdist /*
2*22001Sdist  * Copyright (c) 1983 Regents of the University of California.
3*22001Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22001Sdist  * specifies the terms and conditions for redistribution.
5*22001Sdist  */
6*22001Sdist 
79022Ssam #ifndef lint
8*22001Sdist static char sccsid[] = "@(#)trace.c	5.1 (Berkeley) 06/04/85";
9*22001Sdist #endif not lint
109022Ssam 
119022Ssam /*
129022Ssam  * Routing Table Management Daemon
139022Ssam  */
149022Ssam #define	RIPCMDS
1510245Ssam #include "defs.h"
169022Ssam 
179022Ssam #define	NRECORDS	50		/* size of circular trace buffer */
189022Ssam #ifdef DEBUG
199022Ssam FILE	*ftrace = stdout;
209022Ssam int	tracing = 1;
219022Ssam #endif
229022Ssam 
239022Ssam traceinit(ifp)
249022Ssam 	register struct interface *ifp;
259022Ssam {
269022Ssam 
279022Ssam 	if (iftraceinit(ifp, &ifp->int_input) &&
289022Ssam 	    iftraceinit(ifp, &ifp->int_output))
299022Ssam 		return;
309022Ssam 	tracing = 0;
319022Ssam 	fprintf(stderr, "traceinit: can't init %s\n", ifp->int_name);
329022Ssam }
339022Ssam 
349022Ssam static
359022Ssam iftraceinit(ifp, ifd)
369022Ssam 	struct interface *ifp;
379022Ssam 	register struct ifdebug *ifd;
389022Ssam {
399022Ssam 	register struct iftrace *t;
409022Ssam 
419022Ssam 	ifd->ifd_records =
429022Ssam 	  (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
439022Ssam 	if (ifd->ifd_records == 0)
449022Ssam 		return (0);
459022Ssam 	ifd->ifd_front = ifd->ifd_records;
4616314Skarels 	ifd->ifd_count = 0;
479022Ssam 	for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
489022Ssam 		t->ift_size = 0;
499022Ssam 		t->ift_packet = 0;
509022Ssam 	}
519022Ssam 	ifd->ifd_if = ifp;
529022Ssam 	return (1);
539022Ssam }
549022Ssam 
559022Ssam traceon(file)
569022Ssam 	char *file;
579022Ssam {
589022Ssam 
599022Ssam 	if (ftrace != NULL)
609022Ssam 		return;
619022Ssam 	ftrace = fopen(file, "a");
629022Ssam 	if (ftrace == NULL)
639022Ssam 		return;
649022Ssam 	dup2(fileno(ftrace), 1);
659022Ssam 	dup2(fileno(ftrace), 2);
669022Ssam 	tracing = 1;
679022Ssam }
689022Ssam 
699022Ssam traceoff()
709022Ssam {
719022Ssam 	if (!tracing)
729022Ssam 		return;
739022Ssam 	if (ftrace != NULL)
749022Ssam 		fclose(ftrace);
759022Ssam 	ftrace = NULL;
769022Ssam 	tracing = 0;
779022Ssam }
789022Ssam 
799022Ssam trace(ifd, who, p, len, m)
809022Ssam 	register struct ifdebug *ifd;
819022Ssam 	struct sockaddr *who;
829022Ssam 	char *p;
839022Ssam 	int len, m;
849022Ssam {
859022Ssam 	register struct iftrace *t;
869022Ssam 
879022Ssam 	if (ifd->ifd_records == 0)
889022Ssam 		return;
899022Ssam 	t = ifd->ifd_front++;
909022Ssam 	if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
919022Ssam 		ifd->ifd_front = ifd->ifd_records;
9216314Skarels 	if (ifd->ifd_count < NRECORDS)
9316314Skarels 		ifd->ifd_count++;
949022Ssam 	if (t->ift_size > 0 && t->ift_packet)
959022Ssam 		free(t->ift_packet);
969022Ssam 	t->ift_packet = 0;
979022Ssam 	t->ift_stamp = time(0);
989022Ssam 	t->ift_who = *who;
999022Ssam 	if (len > 0) {
1009022Ssam 		t->ift_packet = malloc(len);
1019022Ssam 		if (t->ift_packet)
1029022Ssam 			bcopy(p, t->ift_packet, len);
1039022Ssam 		else
1049022Ssam 			len = 0;
1059022Ssam 	}
1069022Ssam 	t->ift_size = len;
1079022Ssam 	t->ift_metric = m;
1089022Ssam }
1099022Ssam 
1109022Ssam traceaction(fd, action, rt)
1119022Ssam 	FILE *fd;
1129022Ssam 	char *action;
1139022Ssam 	struct rt_entry *rt;
1149022Ssam {
1159022Ssam 	struct sockaddr_in *dst, *gate;
1169022Ssam 	static struct bits {
1179022Ssam 		int	t_bits;
1189022Ssam 		char	*t_name;
1199022Ssam 	} flagbits[] = {
1209022Ssam 		{ RTF_UP,	"UP" },
1219022Ssam 		{ RTF_GATEWAY,	"GATEWAY" },
1229022Ssam 		{ RTF_HOST,	"HOST" },
1239022Ssam 		{ 0 }
1249022Ssam 	}, statebits[] = {
1259022Ssam 		{ RTS_PASSIVE,	"PASSIVE" },
1269022Ssam 		{ RTS_REMOTE,	"REMOTE" },
1279022Ssam 		{ RTS_INTERFACE,"INTERFACE" },
1289022Ssam 		{ RTS_CHANGED,	"CHANGED" },
1299022Ssam 		{ 0 }
1309022Ssam 	};
1319022Ssam 	register struct bits *p;
1329022Ssam 	register int first;
1339022Ssam 	char *cp;
1349022Ssam 	struct interface *ifp;
1359022Ssam 
1369022Ssam 	if (fd == NULL)
1379022Ssam 		return;
1389022Ssam 	fprintf(fd, "%s ", action);
1399022Ssam 	dst = (struct sockaddr_in *)&rt->rt_dst;
1409022Ssam 	gate = (struct sockaddr_in *)&rt->rt_router;
14115099Ssam 	fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr));
14215099Ssam 	fprintf(fd, "router %s, metric %d, flags",
14315099Ssam 	     inet_ntoa(gate->sin_addr), rt->rt_metric);
1449022Ssam 	cp = " %s";
1459022Ssam 	for (first = 1, p = flagbits; p->t_bits > 0; p++) {
1469022Ssam 		if ((rt->rt_flags & p->t_bits) == 0)
1479022Ssam 			continue;
1489022Ssam 		fprintf(fd, cp, p->t_name);
1499022Ssam 		if (first) {
1509022Ssam 			cp = "|%s";
1519022Ssam 			first = 0;
1529022Ssam 		}
1539022Ssam 	}
1549022Ssam 	fprintf(fd, " state");
1559022Ssam 	cp = " %s";
1569022Ssam 	for (first = 1, p = statebits; p->t_bits > 0; p++) {
1579022Ssam 		if ((rt->rt_state & p->t_bits) == 0)
1589022Ssam 			continue;
1599022Ssam 		fprintf(fd, cp, p->t_name);
1609022Ssam 		if (first) {
1619022Ssam 			cp = "|%s";
1629022Ssam 			first = 0;
1639022Ssam 		}
1649022Ssam 	}
1659022Ssam 	putc('\n', fd);
16616128Skarels 	if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
1679022Ssam 		dumpif(fd, rt->rt_ifp);
1689022Ssam 	fflush(fd);
1699022Ssam }
1709022Ssam 
1719022Ssam dumpif(fd, ifp)
1729022Ssam 	register struct interface *ifp;
1739022Ssam {
17416314Skarels 	if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
17516314Skarels 		fprintf(fd, "*** Packet history for interface %s ***\n",
17616314Skarels 			ifp->int_name);
17716314Skarels 		dumptrace(fd, "to", &ifp->int_output);
17816314Skarels 		dumptrace(fd, "from", &ifp->int_input);
17916314Skarels 		fprintf(fd, "*** end packet history ***\n");
18016314Skarels 	}
1819022Ssam }
1829022Ssam 
1839022Ssam dumptrace(fd, dir, ifd)
1849022Ssam 	FILE *fd;
1859022Ssam 	char *dir;
1869022Ssam 	register struct ifdebug *ifd;
1879022Ssam {
1889022Ssam 	register struct iftrace *t;
1899022Ssam 	char *cp = !strcmp(dir, "to") ? "Output" : "Input";
1909022Ssam 
1919022Ssam 	if (ifd->ifd_front == ifd->ifd_records &&
1929022Ssam 	    ifd->ifd_front->ift_size == 0) {
1939022Ssam 		fprintf(fd, "%s: no packets.\n", cp);
1949022Ssam 		return;
1959022Ssam 	}
1969022Ssam 	fprintf(fd, "%s trace:\n", cp);
19716314Skarels 	t = ifd->ifd_front - ifd->ifd_count;
19816314Skarels 	if (t < ifd->ifd_records)
19916314Skarels 		t += NRECORDS;
20016314Skarels 	for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
20116314Skarels 		if (t >= ifd->ifd_records + NRECORDS)
20216314Skarels 			t = ifd->ifd_records;
2039022Ssam 		if (t->ift_size == 0)
2049022Ssam 			continue;
2059022Ssam 		fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
2069022Ssam 			t->ift_metric);
2079022Ssam 		dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
2089022Ssam 	}
2099022Ssam }
2109022Ssam 
2119022Ssam dumppacket(fd, dir, who, cp, size)
2129022Ssam 	FILE *fd;
2139022Ssam 	struct sockaddr_in *who;		/* should be sockaddr */
2149022Ssam 	char *dir, *cp;
2159022Ssam 	register int size;
2169022Ssam {
2179022Ssam 	register struct rip *msg = (struct rip *)cp;
2189022Ssam 	register struct netinfo *n;
2199022Ssam 
2209022Ssam 	if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
22115099Ssam 		fprintf(fd, "%s %s %s.%d", ripcmds[msg->rip_cmd],
22215099Ssam 		    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
2239022Ssam 	else {
2249022Ssam 		fprintf(fd, "Bad cmd 0x%x %s %x.%d\n", msg->rip_cmd,
22515099Ssam 		    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port));
2269022Ssam 		fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
2279022Ssam 		return;
2289022Ssam 	}
2299022Ssam 	switch (msg->rip_cmd) {
2309022Ssam 
2319022Ssam 	case RIPCMD_REQUEST:
2329022Ssam 	case RIPCMD_RESPONSE:
2339022Ssam 		fprintf(fd, ":\n");
2349022Ssam 		size -= 4 * sizeof (char);
2359022Ssam 		n = msg->rip_nets;
2369022Ssam 		for (; size > 0; n++, size -= sizeof (struct netinfo)) {
2379022Ssam 			if (size < sizeof (struct netinfo))
2389022Ssam 				break;
23915099Ssam 			fprintf(fd, "\tdst %s metric %d\n",
24015099Ssam #define	satosin(sa)	((struct sockaddr_in *)&sa)
24115099Ssam 			     inet_ntoa(satosin(n->rip_dst)->sin_addr),
24215099Ssam 			     ntohl(n->rip_metric));
2439022Ssam 		}
2449022Ssam 		break;
2459022Ssam 
2469022Ssam 	case RIPCMD_TRACEON:
2479022Ssam 		fprintf(fd, ", file=%*s\n", size, msg->rip_tracefile);
2489022Ssam 		break;
2499022Ssam 
2509022Ssam 	case RIPCMD_TRACEOFF:
2519022Ssam 		fprintf(fd, "\n");
2529022Ssam 		break;
2539022Ssam 	}
2549022Ssam }
255