show PUSH also
[unix-history] / usr / src / usr.sbin / trpt / trpt.c
CommitLineData
9a3a7863 1#ifndef lint
fe12a9ce 2static char sccsid[] = "@(#)trpt.c 4.5 83/01/18";
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
eb0fbeab 30#include <stdio.h>
94a2d2a7 31#include <errno.h>
9a3a7863
BJ
32#include <nlist.h>
33
34n_time ntime;
35int sflag;
36struct nlist nl[] = {
37 { "_tcp_debug" },
38 { "_tcp_debx" },
39 0
40};
41struct tcp_debug tcp_debug[TCP_NDEBUG];
42int tcp_debx;
43
44main(argc, argv)
45 int argc;
46 char **argv;
47{
eb0fbeab
SL
48 int i, mask = 0;
49 caddr_t tcpcb = 0;
50 char *system = "/vmunix", *core = "/dev/kmem";
9a3a7863
BJ
51
52 argc--, argv++;
53again:
54 if (argc > 0 && !strcmp(*argv, "-s")) {
55 sflag++, argc--, argv++;
56 goto again;
57 }
eb0fbeab
SL
58 if (argc > 0 && !strcmp(*argv, "-p")) {
59 if (argc < 1) {
60 fprintf(stderr, "-p: missing tcpcb address\n");
61 exit(1);
62 }
63 argc--, argv++;
64 sscanf(*argv, "%x", &tcpcb);
65 argc--, argv++;
66 goto again;
67 }
68 if (argc > 0) {
69 system = *argv;
70 argc--, argv++;
71 mask++;
72 }
73 if (argc > 0) {
74 core = *argv;
75 argc--, argv++;
76 mask++;
77 }
78 (void) nlist(system, nl);
9a3a7863 79 if (nl[0].n_value == 0) {
eb0fbeab 80 fprintf(stderr, "trpt: %s: no namelist\n", system);
9a3a7863
BJ
81 exit(1);
82 }
eb0fbeab
SL
83 (void) close(0);
84 if (open(core, 0) < 0) {
85 fprintf(stderr, "trpt: "); perror(core);
86 exit(2);
87 }
88 if (mask) {
9a3a7863
BJ
89 nl[0].n_value &= 0x7fffffff;
90 nl[1].n_value &= 0x7fffffff;
91 }
eb0fbeab
SL
92 (void) lseek(0, nl[1].n_value, 0);
93 if (read(0, &tcp_debx, sizeof (tcp_debx)) != sizeof (tcp_debx)) {
94 fprintf(stderr, "trpt: "); perror("tcp_debx");
95 exit(3);
96 }
9a3a7863 97 printf("tcp_debx=%d\n", tcp_debx);
eb0fbeab
SL
98 (void) lseek(0, nl[0].n_value, 0);
99 if (read(0, tcp_debug, sizeof (tcp_debug)) != sizeof (tcp_debug)) {
100 fprintf(stderr, "trpt: "); perror("tcp_debug");
101 exit(3);
102 }
9a3a7863
BJ
103 for (i = tcp_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
104 struct tcp_debug *td = &tcp_debug[i];
10ce0f6a 105
eb0fbeab
SL
106 if (tcpcb && td->td_tcb != tcpcb)
107 continue;
10ce0f6a 108 ntime = ntohl(td->td_time);
9a3a7863
BJ
109 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
110 &td->td_ti, td->td_req);
111 }
112 for (i = 0; i < tcp_debx % TCP_NDEBUG; i++) {
113 struct tcp_debug *td = &tcp_debug[i];
10ce0f6a 114
eb0fbeab
SL
115 if (tcpcb && td->td_tcb != tcpcb)
116 continue;
10ce0f6a 117 ntime = ntohl(td->td_time);
9a3a7863
BJ
118 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
119 &td->td_ti, td->td_req);
120 }
121 exit(0);
122}
123
124/*
125 * Tcp debug routines
126 */
127tcp_trace(act, ostate, atp, tp, ti, req)
128 short act, ostate;
129 struct tcpcb *atp, *tp;
130 struct tcpiphdr *ti;
131 int req;
132{
133 tcp_seq seq, ack;
10ce0f6a 134 int len, flags, win, timer;
9a3a7863
BJ
135 char *cp;
136
137 ptime(ntime);
138 printf("%x %s:%s ", ((int)atp)&0xfffff,
139 tcpstates[ostate], tanames[act]);
140 switch (act) {
141
142 case TA_INPUT:
143 case TA_OUTPUT:
144 seq = ti->ti_seq;
145 ack = ti->ti_ack;
146 len = ti->ti_len;
147 win = ti->ti_win;
9a3a7863
BJ
148 if (act == TA_OUTPUT) {
149 seq = ntohl(seq);
150 ack = ntohl(ack);
151 len = ntohs(len);
152 win = ntohs(win);
153 }
9a3a7863
BJ
154 if (act == TA_OUTPUT)
155 len -= sizeof (struct tcphdr);
156 if (len)
157 printf("[%x..%x)", seq, seq+len);
158 else
159 printf("%x", seq);
160 printf("@%x", ack);
161 if (win)
162 printf("(win=%d)", win);
163 flags = ti->ti_flags;
164 if (flags) {
165 char *cp = "<";
166#define pf(f) { if (ti->ti_flags&TH_/**/f) { printf("%s%s", cp, "f"); cp = ","; } }
ed06dc42 167 pf(SYN); pf(ACK); pf(FIN); pf(RST); pf(PUSH); pf(URG);
9a3a7863
BJ
168 printf(">");
169 }
170 break;
171
172 case TA_USER:
10ce0f6a
SL
173 timer = req >> 8;
174 req &= 0xff;
175 printf("%s", prurequests[req]);
176 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
177 printf("<%s>", tcptimers[timer]);
9a3a7863
BJ
178 break;
179 }
180 printf(" -> %s", tcpstates[tp->t_state]);
181 /* print out internal state of tp !?! */
182 printf("\n");
183 if (sflag) {
184 printf("\trcv_nxt %x rcv_wnd %d snd_una %x snd_nxt %x snd_max %x\n",
eb0fbeab
SL
185 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
186 tp->snd_max);
187 printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %x\n", tp->snd_wl1,
188 tp->snd_wl2, tp->snd_wnd);
9a3a7863
BJ
189 }
190}
191
192ptime(ms)
193 int ms;
194{
195
196 printf("%03d ", (ms/10) % 1000);
197}