xref: /original-bsd/usr.sbin/trpt/trpt.c (revision f0fd5f8a)
1 #ifndef lint
2 static char sccsid[] = "@(#)trpt.c	4.3 82/11/14";
3 #endif
4 
5 #include <sys/param.h>
6 #include <sys/socket.h>
7 #include <sys/socketvar.h>
8 #define PRUREQUESTS
9 #include <sys/protosw.h>
10 
11 #include <net/route.h>
12 #include <net/if.h>
13 
14 #include <netinet/in.h>
15 #include <netinet/in_pcb.h>
16 #include <netinet/in_systm.h>
17 #include <netinet/ip.h>
18 #include <netinet/ip_var.h>
19 #include <netinet/tcp.h>
20 #define TCPSTATES
21 #include <netinet/tcp_fsm.h>
22 #include <netinet/tcp_seq.h>
23 #define	TCPTIMERS
24 #include <netinet/tcp_timer.h>
25 #include <netinet/tcp_var.h>
26 #include <netinet/tcpip.h>
27 #define	TANAMES
28 #include <netinet/tcp_debug.h>
29 
30 #include <errno.h>
31 #include <nlist.h>
32 
33 n_time	ntime;
34 int	sflag;
35 struct	nlist nl[] = {
36 	{ "_tcp_debug" },
37 	{ "_tcp_debx" },
38 	0
39 };
40 struct	tcp_debug tcp_debug[TCP_NDEBUG];
41 int	tcp_debx;
42 
43 main(argc, argv)
44 	int argc;
45 	char **argv;
46 {
47 	int i;
48 
49 	argc--, argv++;
50 again:
51 	if (argc > 0 && !strcmp(*argv, "-s")) {
52 		sflag++, argc--, argv++;
53 		goto again;
54 	}
55 	nlist(argc > 0 ? *argv : "/vmunix", nl);
56 	if (nl[0].n_value == 0) {
57 		printf("no namelist\n");
58 		exit(1);
59 	}
60 	close(0);
61 	open(argc > 1 ? argv[1] : "/dev/kmem", 0);
62 	if (argc > 1) {
63 		nl[0].n_value &= 0x7fffffff;
64 		nl[1].n_value &= 0x7fffffff;
65 	}
66 	lseek(0, nl[1].n_value, 0);
67 	read(0, &tcp_debx, sizeof (tcp_debx));
68 	printf("tcp_debx=%d\n", tcp_debx);
69 	lseek(0, nl[0].n_value, 0);
70 	read(0, tcp_debug, sizeof (tcp_debug));
71 	for (i = tcp_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
72 		struct tcp_debug *td = &tcp_debug[i];
73 
74 		ntime = ntohl(td->td_time);
75 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
76 		    &td->td_ti, td->td_req);
77 	}
78 	for (i = 0; i < tcp_debx % TCP_NDEBUG; i++) {
79 		struct tcp_debug *td = &tcp_debug[i];
80 
81 		ntime = ntohl(td->td_time);
82 		tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
83 		    &td->td_ti, td->td_req);
84 	}
85 	exit(0);
86 }
87 
88 /*
89  * Tcp debug routines
90  */
91 tcp_trace(act, ostate, atp, tp, ti, req)
92 	short act, ostate;
93 	struct tcpcb *atp, *tp;
94 	struct tcpiphdr *ti;
95 	int req;
96 {
97 	tcp_seq seq, ack;
98 	int len, flags, win, timer;
99 	char *cp;
100 
101 	ptime(ntime);
102 	printf("%x %s:%s ", ((int)atp)&0xfffff,
103 	    tcpstates[ostate], tanames[act]);
104 	switch (act) {
105 
106 	case TA_INPUT:
107 	case TA_OUTPUT:
108 		seq = ti->ti_seq;
109 		ack = ti->ti_ack;
110 		len = ti->ti_len;
111 		win = ti->ti_win;
112 #if vax || pdp11
113 		if (act == TA_OUTPUT) {
114 			seq = ntohl(seq);
115 			ack = ntohl(ack);
116 			len = ntohs(len);
117 			win = ntohs(win);
118 		}
119 #endif
120 		if (act == TA_OUTPUT)
121 			len -= sizeof (struct tcphdr);
122 		if (len)
123 			printf("[%x..%x)", seq, seq+len);
124 		else
125 			printf("%x", seq);
126 		printf("@%x", ack);
127 		if (win)
128 			printf("(win=%d)", win);
129 		flags = ti->ti_flags;
130 		if (flags) {
131 			char *cp = "<";
132 #define pf(f) { if (ti->ti_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
133 			pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(URG);
134 			printf(">");
135 		}
136 		break;
137 
138 	case TA_USER:
139 		timer = req >> 8;
140 		req &= 0xff;
141 		printf("%s", prurequests[req]);
142 		if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
143 			printf("<%s>", tcptimers[timer]);
144 		break;
145 	}
146 	printf(" -> %s", tcpstates[tp->t_state]);
147 	/* print out internal state of tp !?! */
148 	printf("\n");
149 	if (sflag) {
150 		printf("\trcv_nxt %x rcv_wnd %d snd_una %x snd_nxt %x snd_max %x\n",
151 		    tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt, tp->snd_max);
152 		printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %x\n", tp->snd_wl1, tp->snd_wl2, tp->snd_wnd);
153 	}
154 }
155 
156 ptime(ms)
157 	int ms;
158 {
159 
160 	printf("%03d ", (ms/10) % 1000);
161 }
162