xref: /csrg-svn/usr.sbin/trpt/trpt.c (revision 10411)
16463Swnj #ifndef lint
2*10411Ssam static char sccsid[] = "@(#)trpt.c	4.5 83/01/18";
36463Swnj #endif
46463Swnj 
56463Swnj #include <sys/param.h>
66463Swnj #include <sys/socket.h>
76463Swnj #include <sys/socketvar.h>
86463Swnj #define PRUREQUESTS
96463Swnj #include <sys/protosw.h>
109202Ssam 
116463Swnj #include <net/route.h>
126463Swnj #include <net/if.h>
139202Ssam 
149202Ssam #include <netinet/in.h>
159202Ssam #include <netinet/in_pcb.h>
169202Ssam #include <netinet/in_systm.h>
179202Ssam #include <netinet/ip.h>
189202Ssam #include <netinet/ip_var.h>
199202Ssam #include <netinet/tcp.h>
206463Swnj #define TCPSTATES
219202Ssam #include <netinet/tcp_fsm.h>
229202Ssam #include <netinet/tcp_seq.h>
236463Swnj #define	TCPTIMERS
249202Ssam #include <netinet/tcp_timer.h>
259202Ssam #include <netinet/tcp_var.h>
269202Ssam #include <netinet/tcpip.h>
276463Swnj #define	TANAMES
289202Ssam #include <netinet/tcp_debug.h>
299202Ssam 
3010272Ssam #include <stdio.h>
316463Swnj #include <errno.h>
326463Swnj #include <nlist.h>
336463Swnj 
346463Swnj n_time	ntime;
356463Swnj int	sflag;
366463Swnj struct	nlist nl[] = {
376463Swnj 	{ "_tcp_debug" },
386463Swnj 	{ "_tcp_debx" },
396463Swnj 	0
406463Swnj };
416463Swnj struct	tcp_debug tcp_debug[TCP_NDEBUG];
426463Swnj int	tcp_debx;
436463Swnj 
446463Swnj main(argc, argv)
456463Swnj 	int argc;
466463Swnj 	char **argv;
476463Swnj {
4810272Ssam 	int i, mask = 0;
4910272Ssam 	caddr_t tcpcb = 0;
5010272Ssam 	char *system = "/vmunix", *core = "/dev/kmem";
516463Swnj 
526463Swnj 	argc--, argv++;
536463Swnj again:
546463Swnj 	if (argc > 0 && !strcmp(*argv, "-s")) {
556463Swnj 		sflag++, argc--, argv++;
566463Swnj 		goto again;
576463Swnj 	}
5810272Ssam 	if (argc > 0 && !strcmp(*argv, "-p")) {
5910272Ssam 		if (argc < 1) {
6010272Ssam 			fprintf(stderr, "-p: missing tcpcb address\n");
6110272Ssam 			exit(1);
6210272Ssam 		}
6310272Ssam 		argc--, argv++;
6410272Ssam 		sscanf(*argv, "%x", &tcpcb);
6510272Ssam 		argc--, argv++;
6610272Ssam 		goto again;
6710272Ssam 	}
6810272Ssam 	if (argc > 0) {
6910272Ssam 		system = *argv;
7010272Ssam 		argc--, argv++;
7110272Ssam 		mask++;
7210272Ssam 	}
7310272Ssam 	if (argc > 0) {
7410272Ssam 		core = *argv;
7510272Ssam 		argc--, argv++;
7610272Ssam 		mask++;
7710272Ssam 	}
7810272Ssam 	(void) nlist(system, nl);
796463Swnj 	if (nl[0].n_value == 0) {
8010272Ssam 		fprintf(stderr, "trpt: %s: no namelist\n", system);
816463Swnj 		exit(1);
826463Swnj 	}
8310272Ssam 	(void) close(0);
8410272Ssam 	if (open(core, 0) < 0) {
8510272Ssam 		fprintf(stderr, "trpt: "); perror(core);
8610272Ssam 		exit(2);
8710272Ssam 	}
8810272Ssam 	if (mask) {
896463Swnj 		nl[0].n_value &= 0x7fffffff;
906463Swnj 		nl[1].n_value &= 0x7fffffff;
916463Swnj 	}
9210272Ssam 	(void) lseek(0, nl[1].n_value, 0);
9310272Ssam 	if (read(0, &tcp_debx, sizeof (tcp_debx)) != sizeof (tcp_debx)) {
9410272Ssam 		fprintf(stderr, "trpt: "); perror("tcp_debx");
9510272Ssam 		exit(3);
9610272Ssam 	}
976463Swnj 	printf("tcp_debx=%d\n", tcp_debx);
9810272Ssam 	(void) lseek(0, nl[0].n_value, 0);
9910272Ssam 	if (read(0, tcp_debug, sizeof (tcp_debug)) != sizeof (tcp_debug)) {
10010272Ssam 		fprintf(stderr, "trpt: "); perror("tcp_debug");
10110272Ssam 		exit(3);
10210272Ssam 	}
1036463Swnj 	for (i = tcp_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
1046463Swnj 		struct tcp_debug *td = &tcp_debug[i];
1058382Ssam 
10610272Ssam 		if (tcpcb && td->td_tcb != tcpcb)
10710272Ssam 			continue;
1088382Ssam 		ntime = ntohl(td->td_time);
1096463Swnj 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
1106463Swnj 		    &td->td_ti, td->td_req);
1116463Swnj 	}
1126463Swnj 	for (i = 0; i < tcp_debx % TCP_NDEBUG; i++) {
1136463Swnj 		struct tcp_debug *td = &tcp_debug[i];
1148382Ssam 
11510272Ssam 		if (tcpcb && td->td_tcb != tcpcb)
11610272Ssam 			continue;
1178382Ssam 		ntime = ntohl(td->td_time);
1186463Swnj 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
1196463Swnj 		    &td->td_ti, td->td_req);
1206463Swnj 	}
1216463Swnj 	exit(0);
1226463Swnj }
1236463Swnj 
1246463Swnj /*
1256463Swnj  * Tcp debug routines
1266463Swnj  */
1276463Swnj tcp_trace(act, ostate, atp, tp, ti, req)
1286463Swnj 	short act, ostate;
1296463Swnj 	struct tcpcb *atp, *tp;
1306463Swnj 	struct tcpiphdr *ti;
1316463Swnj 	int req;
1326463Swnj {
1336463Swnj 	tcp_seq seq, ack;
1348382Ssam 	int len, flags, win, timer;
1356463Swnj 	char *cp;
1366463Swnj 
1376463Swnj 	ptime(ntime);
1386463Swnj 	printf("%x %s:%s ", ((int)atp)&0xfffff,
1396463Swnj 	    tcpstates[ostate], tanames[act]);
1406463Swnj 	switch (act) {
1416463Swnj 
1426463Swnj 	case TA_INPUT:
1436463Swnj 	case TA_OUTPUT:
1446463Swnj 		seq = ti->ti_seq;
1456463Swnj 		ack = ti->ti_ack;
1466463Swnj 		len = ti->ti_len;
1476463Swnj 		win = ti->ti_win;
1486463Swnj 		if (act == TA_OUTPUT) {
1496463Swnj 			seq = ntohl(seq);
1506463Swnj 			ack = ntohl(ack);
1516463Swnj 			len = ntohs(len);
1526463Swnj 			win = ntohs(win);
1536463Swnj 		}
1546463Swnj 		if (act == TA_OUTPUT)
1556463Swnj 			len -= sizeof (struct tcphdr);
1566463Swnj 		if (len)
1576463Swnj 			printf("[%x..%x)", seq, seq+len);
1586463Swnj 		else
1596463Swnj 			printf("%x", seq);
1606463Swnj 		printf("@%x", ack);
1616463Swnj 		if (win)
1626463Swnj 			printf("(win=%d)", win);
1636463Swnj 		flags = ti->ti_flags;
1646463Swnj 		if (flags) {
1656463Swnj 			char *cp = "<";
1666463Swnj #define pf(f) { if (ti->ti_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
167*10411Ssam 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
1686463Swnj 			printf(">");
1696463Swnj 		}
1706463Swnj 		break;
1716463Swnj 
1726463Swnj 	case TA_USER:
1738382Ssam 		timer = req >> 8;
1748382Ssam 		req &= 0xff;
1758382Ssam 		printf("%s", prurequests[req]);
1768382Ssam 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
1778382Ssam 			printf("<%s>", tcptimers[timer]);
1786463Swnj 		break;
1796463Swnj 	}
1806463Swnj 	printf(" -> %s", tcpstates[tp->t_state]);
1816463Swnj 	/* print out internal state of tp !?! */
1826463Swnj 	printf("\n");
1836463Swnj 	if (sflag) {
1846463Swnj 		printf("\trcv_nxt %x rcv_wnd %d snd_una %x snd_nxt %x snd_max %x\n",
18510272Ssam 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
18610272Ssam 		    tp->snd_max);
18710272Ssam 		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %x\n", tp->snd_wl1,
18810272Ssam 		    tp->snd_wl2, tp->snd_wnd);
1896463Swnj 	}
1906463Swnj }
1916463Swnj 
1926463Swnj ptime(ms)
1936463Swnj 	int ms;
1946463Swnj {
1956463Swnj 
1966463Swnj 	printf("%03d ", (ms/10) % 1000);
1976463Swnj }
198