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