Add copyright
[unix-history] / usr / src / usr.sbin / trpt / trpt.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
9a3a7863 13#ifndef lint
5ff67f98
DF
14static char sccsid[] = "@(#)trpt.c 5.1 (Berkeley) %G%";
15#endif not lint
9a3a7863
BJ
16
17#include <sys/param.h>
18#include <sys/socket.h>
19#include <sys/socketvar.h>
20#define PRUREQUESTS
21#include <sys/protosw.h>
94a2d2a7 22
9a3a7863 23#include <net/route.h>
9a3a7863 24#include <net/if.h>
94a2d2a7
SL
25
26#include <netinet/in.h>
27#include <netinet/in_pcb.h>
28#include <netinet/in_systm.h>
29#include <netinet/ip.h>
30#include <netinet/ip_var.h>
31#include <netinet/tcp.h>
9a3a7863 32#define TCPSTATES
94a2d2a7
SL
33#include <netinet/tcp_fsm.h>
34#include <netinet/tcp_seq.h>
9a3a7863 35#define TCPTIMERS
94a2d2a7
SL
36#include <netinet/tcp_timer.h>
37#include <netinet/tcp_var.h>
38#include <netinet/tcpip.h>
9a3a7863 39#define TANAMES
94a2d2a7 40#include <netinet/tcp_debug.h>
9a3a7863 41
7d3cb675
SL
42#include <arpa/inet.h>
43
eb0fbeab 44#include <stdio.h>
94a2d2a7 45#include <errno.h>
9a3a7863
BJ
46#include <nlist.h>
47
48n_time ntime;
49int sflag;
175d35a6
SL
50int tflag;
51int jflag;
49173cdd 52int aflag;
175d35a6 53int numeric();
9a3a7863
BJ
54struct nlist nl[] = {
55 { "_tcp_debug" },
56 { "_tcp_debx" },
57 0
58};
59struct tcp_debug tcp_debug[TCP_NDEBUG];
175d35a6 60caddr_t tcp_pcbs[TCP_NDEBUG];
9a3a7863
BJ
61int tcp_debx;
62
63main(argc, argv)
64 int argc;
65 char **argv;
66{
175d35a6 67 int i, mask = 0, npcbs = 0;
eb0fbeab 68 char *system = "/vmunix", *core = "/dev/kmem";
9a3a7863
BJ
69
70 argc--, argv++;
71again:
49173cdd
SL
72 if (argc > 0 && !strcmp(*argv, "-a")) {
73 aflag++, argc--, argv++;
74 goto again;
75 }
9a3a7863
BJ
76 if (argc > 0 && !strcmp(*argv, "-s")) {
77 sflag++, argc--, argv++;
78 goto again;
79 }
175d35a6
SL
80 if (argc > 0 && !strcmp(*argv, "-t")) {
81 tflag++, argc--, argv++;
82 goto again;
83 }
84 if (argc > 0 && !strcmp(*argv, "-j")) {
85 jflag++, argc--, argv++;
86 goto again;
87 }
eb0fbeab 88 if (argc > 0 && !strcmp(*argv, "-p")) {
175d35a6 89 argc--, argv++;
eb0fbeab
SL
90 if (argc < 1) {
91 fprintf(stderr, "-p: missing tcpcb address\n");
92 exit(1);
93 }
175d35a6
SL
94 if (npcbs >= TCP_NDEBUG) {
95 fprintf(stderr, "-p: too many pcb's specified\n");
96 exit(1);
97 }
98 sscanf(*argv, "%x", &tcp_pcbs[npcbs++]);
eb0fbeab
SL
99 argc--, argv++;
100 goto again;
101 }
102 if (argc > 0) {
103 system = *argv;
104 argc--, argv++;
105 mask++;
106 }
107 if (argc > 0) {
108 core = *argv;
109 argc--, argv++;
110 mask++;
111 }
112 (void) nlist(system, nl);
9a3a7863 113 if (nl[0].n_value == 0) {
eb0fbeab 114 fprintf(stderr, "trpt: %s: no namelist\n", system);
9a3a7863
BJ
115 exit(1);
116 }
eb0fbeab
SL
117 (void) close(0);
118 if (open(core, 0) < 0) {
119 fprintf(stderr, "trpt: "); perror(core);
120 exit(2);
121 }
122 if (mask) {
9a3a7863
BJ
123 nl[0].n_value &= 0x7fffffff;
124 nl[1].n_value &= 0x7fffffff;
125 }
eb0fbeab
SL
126 (void) lseek(0, nl[1].n_value, 0);
127 if (read(0, &tcp_debx, sizeof (tcp_debx)) != sizeof (tcp_debx)) {
128 fprintf(stderr, "trpt: "); perror("tcp_debx");
129 exit(3);
130 }
eb0fbeab
SL
131 (void) lseek(0, nl[0].n_value, 0);
132 if (read(0, tcp_debug, sizeof (tcp_debug)) != sizeof (tcp_debug)) {
133 fprintf(stderr, "trpt: "); perror("tcp_debug");
134 exit(3);
135 }
175d35a6
SL
136 /*
137 * If no control blocks have been specified, figure
138 * out how many distinct one we have and summarize
139 * them in tcp_pcbs for sorting the trace records
140 * below.
141 */
142 if (npcbs == 0) {
143 for (i = 0; i < TCP_NDEBUG; i++) {
144 register int j;
145 register struct tcp_debug *td = &tcp_debug[i];
146
147 if (td->td_tcb == 0)
148 continue;
149 for (j = 0; j < npcbs; j++)
150 if (tcp_pcbs[j] == td->td_tcb)
151 break;
152 if (j >= npcbs)
153 tcp_pcbs[npcbs++] = td->td_tcb;
154 }
155 }
156 qsort(tcp_pcbs, npcbs, sizeof (caddr_t), numeric);
157 if (jflag) {
158 char *cp = "";
10ce0f6a 159
175d35a6
SL
160 for (i = 0; i < npcbs; i++) {
161 printf("%s%x", cp, tcp_pcbs[i]);
162 cp = ", ";
163 }
164 if (*cp)
165 putchar('\n');
166 exit(0);
167 }
168 for (i = 0; i < npcbs; i++) {
169 printf("\n%x:\n", tcp_pcbs[i]);
170 dotrace(tcp_pcbs[i]);
171 }
172 exit(0);
173}
174
175dotrace(tcpcb)
176 register caddr_t tcpcb;
177{
178 register int i;
179 register struct tcp_debug *td;
180
7dae8914 181 for (i = tcp_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
175d35a6 182 td = &tcp_debug[i];
eb0fbeab
SL
183 if (tcpcb && td->td_tcb != tcpcb)
184 continue;
10ce0f6a 185 ntime = ntohl(td->td_time);
9a3a7863
BJ
186 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
187 &td->td_ti, td->td_req);
188 }
7dae8914 189 for (i = 0; i < tcp_debx % TCP_NDEBUG; i++) {
175d35a6 190 td = &tcp_debug[i];
eb0fbeab
SL
191 if (tcpcb && td->td_tcb != tcpcb)
192 continue;
10ce0f6a 193 ntime = ntohl(td->td_time);
9a3a7863
BJ
194 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
195 &td->td_ti, td->td_req);
196 }
9a3a7863
BJ
197}
198
199/*
200 * Tcp debug routines
201 */
202tcp_trace(act, ostate, atp, tp, ti, req)
203 short act, ostate;
204 struct tcpcb *atp, *tp;
205 struct tcpiphdr *ti;
206 int req;
207{
208 tcp_seq seq, ack;
10ce0f6a 209 int len, flags, win, timer;
9a3a7863
BJ
210 char *cp;
211
212 ptime(ntime);
175d35a6 213 printf("%s:%s ", tcpstates[ostate], tanames[act]);
9a3a7863
BJ
214 switch (act) {
215
216 case TA_INPUT:
217 case TA_OUTPUT:
3cd6f35b 218 case TA_DROP:
49173cdd 219 if (aflag) {
7d3cb675 220 printf("(src=%s,%d, ", inet_ntoa(ti->ti_src),
49173cdd 221 ntohs(ti->ti_sport));
7d3cb675 222 printf("dst=%s,%d)", inet_ntoa(ti->ti_dst),
49173cdd
SL
223 ntohs(ti->ti_dport));
224 }
9a3a7863
BJ
225 seq = ti->ti_seq;
226 ack = ti->ti_ack;
227 len = ti->ti_len;
228 win = ti->ti_win;
9a3a7863
BJ
229 if (act == TA_OUTPUT) {
230 seq = ntohl(seq);
231 ack = ntohl(ack);
232 len = ntohs(len);
233 win = ntohs(win);
234 }
9a3a7863
BJ
235 if (act == TA_OUTPUT)
236 len -= sizeof (struct tcphdr);
237 if (len)
238 printf("[%x..%x)", seq, seq+len);
239 else
240 printf("%x", seq);
241 printf("@%x", ack);
242 if (win)
23c416d7 243 printf("(win=%x)", win);
9a3a7863
BJ
244 flags = ti->ti_flags;
245 if (flags) {
246 char *cp = "<";
247#define pf(f) { if (ti->ti_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
ed06dc42 248 pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
9a3a7863
BJ
249 printf(">");
250 }
251 break;
252
253 case TA_USER:
10ce0f6a
SL
254 timer = req >> 8;
255 req &= 0xff;
256 printf("%s", prurequests[req]);
257 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
258 printf("<%s>", tcptimers[timer]);
9a3a7863
BJ
259 break;
260 }
261 printf(" -> %s", tcpstates[tp->t_state]);
262 /* print out internal state of tp !?! */
263 printf("\n");
264 if (sflag) {
49282770 265 printf("\trcv_nxt %x rcv_wnd %x snd_una %x snd_nxt %x snd_max %x\n",
eb0fbeab
SL
266 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
267 tp->snd_max);
268 printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %x\n", tp->snd_wl1,
269 tp->snd_wl2, tp->snd_wnd);
9a3a7863 270 }
175d35a6
SL
271 /* print out timers? */
272 if (tflag) {
273 char *cp = "\t";
274 register int i;
275
276 for (i = 0; i < TCPT_NTIMERS; i++) {
277 if (tp->t_timer[i] == 0)
278 continue;
279 printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
280 if (i == TCPT_REXMT)
281 printf(" (t_rxtshft=%d)", tp->t_rxtshift);
282 cp = ", ";
283 }
284 if (*cp != '\t')
285 putchar('\n');
286 }
9a3a7863
BJ
287}
288
289ptime(ms)
290 int ms;
291{
292
293 printf("%03d ", (ms/10) % 1000);
294}
175d35a6
SL
295
296numeric(c1, c2)
297 caddr_t *c1, *c2;
298{
299
300 return (*c1 - *c2);
301}