xref: /csrg-svn/usr.sbin/trpt/trpt.c (revision 10272)
16463Swnj #ifndef lint
2*10272Ssam static char sccsid[] = "@(#)trpt.c	4.4 83/01/13";
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 
30*10272Ssam #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 {
48*10272Ssam 	int i, mask = 0;
49*10272Ssam 	caddr_t tcpcb = 0;
50*10272Ssam 	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 	}
58*10272Ssam 	if (argc > 0 && !strcmp(*argv, "-p")) {
59*10272Ssam 		if (argc < 1) {
60*10272Ssam 			fprintf(stderr, "-p: missing tcpcb address\n");
61*10272Ssam 			exit(1);
62*10272Ssam 		}
63*10272Ssam 		argc--, argv++;
64*10272Ssam 		sscanf(*argv, "%x", &tcpcb);
65*10272Ssam 		argc--, argv++;
66*10272Ssam 		goto again;
67*10272Ssam 	}
68*10272Ssam 	if (argc > 0) {
69*10272Ssam 		system = *argv;
70*10272Ssam 		argc--, argv++;
71*10272Ssam 		mask++;
72*10272Ssam 	}
73*10272Ssam 	if (argc > 0) {
74*10272Ssam 		core = *argv;
75*10272Ssam 		argc--, argv++;
76*10272Ssam 		mask++;
77*10272Ssam 	}
78*10272Ssam 	(void) nlist(system, nl);
796463Swnj 	if (nl[0].n_value == 0) {
80*10272Ssam 		fprintf(stderr, "trpt: %s: no namelist\n", system);
816463Swnj 		exit(1);
826463Swnj 	}
83*10272Ssam 	(void) close(0);
84*10272Ssam 	if (open(core, 0) < 0) {
85*10272Ssam 		fprintf(stderr, "trpt: "); perror(core);
86*10272Ssam 		exit(2);
87*10272Ssam 	}
88*10272Ssam 	if (mask) {
896463Swnj 		nl[0].n_value &= 0x7fffffff;
906463Swnj 		nl[1].n_value &= 0x7fffffff;
916463Swnj 	}
92*10272Ssam 	(void) lseek(0, nl[1].n_value, 0);
93*10272Ssam 	if (read(0, &tcp_debx, sizeof (tcp_debx)) != sizeof (tcp_debx)) {
94*10272Ssam 		fprintf(stderr, "trpt: "); perror("tcp_debx");
95*10272Ssam 		exit(3);
96*10272Ssam 	}
976463Swnj 	printf("tcp_debx=%d\n", tcp_debx);
98*10272Ssam 	(void) lseek(0, nl[0].n_value, 0);
99*10272Ssam 	if (read(0, tcp_debug, sizeof (tcp_debug)) != sizeof (tcp_debug)) {
100*10272Ssam 		fprintf(stderr, "trpt: "); perror("tcp_debug");
101*10272Ssam 		exit(3);
102*10272Ssam 	}
1036463Swnj 	for (i = tcp_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
1046463Swnj 		struct tcp_debug *td = &tcp_debug[i];
1058382Ssam 
106*10272Ssam 		if (tcpcb && td->td_tcb != tcpcb)
107*10272Ssam 			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 
115*10272Ssam 		if (tcpcb && td->td_tcb != tcpcb)
116*10272Ssam 			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 = ","; } }
1676463Swnj 			pf(SYN); pf(ACK); pf(FIN); pf(RST); 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",
185*10272Ssam 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
186*10272Ssam 		    tp->snd_max);
187*10272Ssam 		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %x\n", tp->snd_wl1,
188*10272Ssam 		    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