libc/locale: Fix type breakage in __collate_range_cmp().
[freebsd-src.git] / usr.sbin / trpt / trpt.c
blobd85d41d42275adb4bde1263282ea6d43e16a6249
1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #if 0
31 #ifndef lint
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1988, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
35 #endif /* not lint */
37 #ifndef lint
38 static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #define PRUREQUESTS
49 #include <sys/protosw.h>
50 #include <sys/file.h>
51 #include <sys/time.h>
53 #include <net/route.h>
54 #include <net/if.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #ifdef INET6
60 #include <netinet/ip6.h>
61 #endif
62 #include <netinet/ip_var.h>
63 #include <netinet/tcp.h>
64 #define TCPSTATES
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_seq.h>
67 #define TCPTIMERS
68 #include <netinet/tcp_timer.h>
69 #include <netinet/tcp_var.h>
70 #include <netinet/tcpip.h>
71 #define TANAMES
72 #include <netinet/tcp_debug.h>
74 #include <arpa/inet.h>
76 #include <err.h>
77 #include <nlist.h>
78 #include <paths.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
84 struct nlist nl[3];
85 #define N_TCP_DEBUG 0
86 #define N_TCP_DEBX 1
88 static caddr_t tcp_pcbs[TCP_NDEBUG];
89 static n_time ntime;
90 static int aflag, kflag, memf, follow, sflag, tflag;
92 void dotrace(caddr_t);
93 void klseek(int, off_t, int);
94 int numeric(const void *, const void *);
95 void tcp_trace(short, short, struct tcpcb *, int, void *, struct tcphdr *, int);
96 static void usage(void);
98 int
99 main(int argc, char **argv)
101 int ch, i, jflag, npcbs;
102 const char *core, *syst;
104 nl[0].n_name = strdup("_tcp_debug");
105 nl[1].n_name = strdup("_tcp_debx");
107 jflag = npcbs = 0;
108 while ((ch = getopt(argc, argv, "afjp:st")) != -1)
109 switch (ch) {
110 case 'a':
111 ++aflag;
112 break;
113 case 'f':
114 ++follow;
115 setlinebuf(stdout);
116 break;
117 case 'j':
118 ++jflag;
119 break;
120 case 'p':
121 if (npcbs >= TCP_NDEBUG)
122 errx(1, "too many pcb's specified");
123 (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
124 break;
125 case 's':
126 ++sflag;
127 break;
128 case 't':
129 ++tflag;
130 break;
131 case '?':
132 default:
133 usage();
135 argc -= optind;
136 argv += optind;
138 core = _PATH_KMEM;
139 if (argc > 0) {
140 syst = *argv;
141 argc--, argv++;
142 if (argc > 0) {
143 core = *argv;
144 argc--, argv++;
145 ++kflag;
148 * Discard setgid privileges if not the running kernel so that
149 * bad guys can't print interesting stuff from kernel memory.
151 if (setgid(getgid()) != 0)
152 err(1, "setgid");
154 else
155 syst = getbootfile();
157 if (nlist(syst, nl) < 0 || !nl[0].n_value)
158 errx(1, "%s: no namelist", syst);
159 if ((memf = open(core, O_RDONLY)) < 0)
160 err(2, "%s", core);
161 if (setgid(getgid()) != 0)
162 err(1, "setgid");
163 if (kflag)
164 errx(1, "can't do core files yet");
165 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
166 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
167 sizeof(tcp_debx))
168 err(3, "tcp_debx");
169 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
170 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
171 sizeof(tcp_debug))
172 err(3, "tcp_debug");
174 * If no control blocks have been specified, figure
175 * out how many distinct one we have and summarize
176 * them in tcp_pcbs for sorting the trace records
177 * below.
179 if (!npcbs) {
180 for (i = 0; i < TCP_NDEBUG; i++) {
181 register struct tcp_debug *td = &tcp_debug[i];
182 register int j;
184 if (td->td_tcb == 0)
185 continue;
186 for (j = 0; j < npcbs; j++)
187 if (tcp_pcbs[j] == td->td_tcb)
188 break;
189 if (j >= npcbs)
190 tcp_pcbs[npcbs++] = td->td_tcb;
192 if (!npcbs)
193 exit(0);
195 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
196 if (jflag) {
197 for (i = 0;;) {
198 printf("%p", (void *)tcp_pcbs[i]);
199 if (++i == npcbs)
200 break;
201 fputs(", ", stdout);
203 putchar('\n');
205 else for (i = 0; i < npcbs; i++) {
206 printf("\n%p:\n", tcp_pcbs[i]);
207 dotrace(tcp_pcbs[i]);
209 exit(0);
212 static void
213 usage()
215 (void)fprintf(stderr,
216 "usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
217 exit(1);
220 void
221 dotrace(tcpcb)
222 register caddr_t tcpcb;
224 register struct tcp_debug *td;
225 register int i;
226 int prev_debx = tcp_debx, family;
228 again: if (--tcp_debx < 0)
229 tcp_debx = TCP_NDEBUG - 1;
230 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
231 td = &tcp_debug[i];
232 if (tcpcb && td->td_tcb != tcpcb)
233 continue;
234 ntime = ntohl(td->td_time);
235 #ifdef INET6
236 family = td->td_family;
237 #else
238 family = AF_INET;
239 #endif
240 switch(family) {
241 case AF_INET:
242 tcp_trace(td->td_act, td->td_ostate,
243 &td->td_cb, td->td_family, &td->td_ti.ti_i,
244 &td->td_ti.ti_t, td->td_req);
245 break;
246 #ifdef INET6
247 case AF_INET6:
248 tcp_trace(td->td_act, td->td_ostate,
249 &td->td_cb, td->td_family, &td->td_ti6.ip6,
250 &td->td_ti6.th, td->td_req);
251 break;
252 #endif
254 if (i == tcp_debx)
255 goto done;
257 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
258 td = &tcp_debug[i];
259 if (tcpcb && td->td_tcb != tcpcb)
260 continue;
261 ntime = ntohl(td->td_time);
262 #ifdef INET6
263 family = td->td_family;
264 #else
265 family = AF_INET;
266 #endif
267 switch(family) {
268 case AF_INET:
269 tcp_trace(td->td_act, td->td_ostate,
270 &td->td_cb, td->td_family, &td->td_ti.ti_i,
271 &td->td_ti.ti_t, td->td_req);
272 break;
273 #ifdef INET6
274 case AF_INET6:
275 tcp_trace(td->td_act, td->td_ostate,
276 &td->td_cb, td->td_family, &td->td_ti6.ip6,
277 &td->td_ti6.th, td->td_req);
278 break;
279 #endif
282 done: if (follow) {
283 prev_debx = tcp_debx + 1;
284 if (prev_debx >= TCP_NDEBUG)
285 prev_debx = 0;
286 do {
287 sleep(1);
288 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
289 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
290 sizeof(tcp_debx))
291 err(3, "tcp_debx");
292 } while (tcp_debx == prev_debx);
293 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
294 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
295 sizeof(tcp_debug))
296 err(3, "tcp_debug");
297 goto again;
302 * Tcp debug routines
304 /*ARGSUSED*/
305 void
306 tcp_trace(short act, short ostate, struct tcpcb *tp, int family __unused,
307 void *ip, struct tcphdr *th, int req)
309 tcp_seq seq, ack;
310 int flags, len, win, timer;
311 struct ip *ip4;
312 #ifdef INET6
313 int isipv6, nopkt = 1;
314 struct ip6_hdr *ip6;
315 char ntop_buf[INET6_ADDRSTRLEN];
316 #endif
318 #ifdef INET6
319 switch (family) {
320 case AF_INET:
321 nopkt = 0;
322 isipv6 = 0;
323 ip4 = (struct ip *)ip;
324 break;
325 case AF_INET6:
326 nopkt = 0;
327 isipv6 = 1;
328 ip6 = (struct ip6_hdr *)ip;
329 case 0:
330 default:
331 break;
333 #else
334 ip4 = (struct ip *)ip;
335 #endif
336 printf("%03ld %s:%s ", (long)((ntime/10) % 1000), tcpstates[ostate],
337 tanames[act]);
338 switch (act) {
339 case TA_INPUT:
340 case TA_OUTPUT:
341 case TA_DROP:
342 #ifdef INET6
343 if (nopkt != 0)
344 break;
345 #endif
346 if (aflag) {
347 printf("(src=%s,%u, ",
349 #ifdef INET6
350 isipv6
351 ? inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf,
352 sizeof(ntop_buf)) :
353 #endif
354 inet_ntoa(ip4->ip_src),
355 ntohs(th->th_sport));
356 printf("dst=%s,%u)",
357 #ifdef INET6
358 isipv6
359 ? inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf,
360 sizeof(ntop_buf)) :
361 #endif
362 inet_ntoa(ip4->ip_dst),
363 ntohs(th->th_dport));
365 seq = th->th_seq;
366 ack = th->th_ack;
368 len =
369 #ifdef INET6
370 isipv6 ? ip6->ip6_plen :
371 #endif
372 ip4->ip_len;
373 win = th->th_win;
374 if (act == TA_OUTPUT) {
375 seq = ntohl(seq);
376 ack = ntohl(ack);
377 len = ntohs(len);
378 win = ntohs(win);
380 if (act == TA_OUTPUT)
381 len -= sizeof(struct tcphdr);
382 if (len)
383 printf("[%lx..%lx)", (u_long)seq, (u_long)(seq + len));
384 else
385 printf("%lx", (u_long)seq);
386 printf("@%lx", (u_long)ack);
387 if (win)
388 printf("(win=%x)", win);
389 flags = th->th_flags;
390 if (flags) {
391 const char *cp = "<";
392 #define pf(flag, string) { \
393 if (th->th_flags&flag) { \
394 (void)printf("%s%s", cp, string); \
395 cp = ","; \
398 pf(TH_SYN, "SYN");
399 pf(TH_ACK, "ACK");
400 pf(TH_FIN, "FIN");
401 pf(TH_RST, "RST");
402 pf(TH_PUSH, "PUSH");
403 pf(TH_URG, "URG");
404 printf(">");
406 break;
407 case TA_USER:
408 timer = req >> 8;
409 req &= 0xff;
410 printf("%s", prurequests[req]);
411 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
412 printf("<%s>", tcptimers[timer]);
413 break;
415 printf(" -> %s", tcpstates[tp->t_state]);
416 /* print out internal state of tp !?! */
417 printf("\n");
418 if (sflag) {
419 printf("\trcv_nxt %lx rcv_wnd %lx snd_una %lx snd_nxt %lx snd_max %lx\n",
420 (u_long)tp->rcv_nxt, tp->rcv_wnd,
421 (u_long)tp->snd_una, (u_long)tp->snd_nxt,
422 (u_long)tp->snd_max);
423 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %lx\n",
424 (u_long)tp->snd_wl1,
425 (u_long)tp->snd_wl2, tp->snd_wnd);
427 /* print out timers? */
428 #if 0
430 * XXX
431 * kernel now uses callouts, not integer time values.
433 if (tflag) {
434 register char *cp = "\t";
435 register int i;
437 for (i = 0; i < TCPT_NTIMERS; i++) {
438 if (tp->t_timer[i] == 0)
439 continue;
440 printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
441 if (i == TCPT_REXMT)
442 printf(" (t_rxtshft=%d)", tp->t_rxtshift);
443 cp = ", ";
445 if (*cp != '\t')
446 putchar('\n');
448 #endif
452 numeric(v1, v2)
453 const void *v1, *v2;
455 const caddr_t *c1 = v1, *c2 = v2;
456 return(*c1 - *c2);
459 void
460 klseek(fd, base, off)
461 int fd, off;
462 off_t base;
464 (void)lseek(fd, base, off);