netstat.c revision 1.16 1 1.16 itojun /* $NetBSD: netstat.c,v 1.16 2000/01/07 04:47:24 itojun Exp $ */
2 1.2 jtc
3 1.1 jtc /*-
4 1.1 jtc * Copyright (c) 1980, 1992, 1993
5 1.1 jtc * The Regents of the University of California. All rights reserved.
6 1.1 jtc *
7 1.1 jtc * Redistribution and use in source and binary forms, with or without
8 1.1 jtc * modification, are permitted provided that the following conditions
9 1.1 jtc * are met:
10 1.1 jtc * 1. Redistributions of source code must retain the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer.
12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer in the
14 1.1 jtc * documentation and/or other materials provided with the distribution.
15 1.1 jtc * 3. All advertising materials mentioning features or use of this software
16 1.1 jtc * must display the following acknowledgement:
17 1.1 jtc * This product includes software developed by the University of
18 1.1 jtc * California, Berkeley and its contributors.
19 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
20 1.1 jtc * may be used to endorse or promote products derived from this software
21 1.1 jtc * without specific prior written permission.
22 1.1 jtc *
23 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 jtc * SUCH DAMAGE.
34 1.1 jtc */
35 1.1 jtc
36 1.7 mrg #include <sys/cdefs.h>
37 1.1 jtc #ifndef lint
38 1.2 jtc #if 0
39 1.1 jtc static char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
40 1.2 jtc #endif
41 1.16 itojun __RCSID("$NetBSD: netstat.c,v 1.16 2000/01/07 04:47:24 itojun Exp $");
42 1.1 jtc #endif /* not lint */
43 1.1 jtc
44 1.1 jtc /*
45 1.1 jtc * netstat
46 1.1 jtc */
47 1.1 jtc #include <sys/param.h>
48 1.1 jtc #include <sys/socket.h>
49 1.1 jtc #include <sys/socketvar.h>
50 1.1 jtc #include <sys/mbuf.h>
51 1.1 jtc #include <sys/protosw.h>
52 1.1 jtc
53 1.1 jtc #include <netinet/in.h>
54 1.7 mrg
55 1.7 mrg #include <arpa/inet.h>
56 1.1 jtc #include <net/route.h>
57 1.7 mrg
58 1.1 jtc #include <netinet/in_systm.h>
59 1.1 jtc #include <netinet/ip.h>
60 1.1 jtc #include <netinet/in_pcb.h>
61 1.1 jtc #include <netinet/ip_icmp.h>
62 1.1 jtc #include <netinet/icmp_var.h>
63 1.1 jtc #include <netinet/ip_var.h>
64 1.15 itojun #ifdef INET6
65 1.15 itojun #include <netinet/ip6.h>
66 1.15 itojun #include <netinet6/in6_pcb.h>
67 1.15 itojun #endif
68 1.1 jtc #include <netinet/tcp.h>
69 1.1 jtc #include <netinet/tcpip.h>
70 1.1 jtc #include <netinet/tcp_seq.h>
71 1.1 jtc #define TCPSTATES
72 1.1 jtc #include <netinet/tcp_fsm.h>
73 1.1 jtc #include <netinet/tcp_timer.h>
74 1.1 jtc #include <netinet/tcp_var.h>
75 1.1 jtc #include <netinet/tcp_debug.h>
76 1.1 jtc #include <netinet/udp.h>
77 1.1 jtc #include <netinet/udp_var.h>
78 1.1 jtc
79 1.1 jtc #include <netdb.h>
80 1.1 jtc #include <stdlib.h>
81 1.1 jtc #include <string.h>
82 1.1 jtc #include <nlist.h>
83 1.1 jtc #include <paths.h>
84 1.1 jtc #include "systat.h"
85 1.1 jtc #include "extern.h"
86 1.1 jtc
87 1.1 jtc static void enter __P((struct inpcb *, struct socket *, int, char *));
88 1.15 itojun static const char *inetname __P((struct in_addr));
89 1.1 jtc static void inetprint __P((struct in_addr *, int, char *));
90 1.15 itojun #ifdef INET6
91 1.15 itojun static void enter6 __P((struct in6pcb *, struct socket *, int, char *));
92 1.15 itojun static const char *inet6name __P((struct in6_addr *));
93 1.15 itojun static void inet6print __P((struct in6_addr *, int, char *));
94 1.15 itojun #endif
95 1.1 jtc
96 1.1 jtc #define streq(a,b) (strcmp(a,b)==0)
97 1.1 jtc
98 1.1 jtc struct netinfo {
99 1.1 jtc struct netinfo *ni_forw, *ni_prev;
100 1.15 itojun int ni_family;
101 1.1 jtc short ni_line; /* line on screen */
102 1.1 jtc short ni_seen; /* 0 when not present in list */
103 1.1 jtc short ni_flags;
104 1.1 jtc #define NIF_LACHG 0x1 /* local address changed */
105 1.1 jtc #define NIF_FACHG 0x2 /* foreign address changed */
106 1.1 jtc short ni_state; /* tcp state */
107 1.1 jtc char *ni_proto; /* protocol */
108 1.1 jtc struct in_addr ni_laddr; /* local address */
109 1.15 itojun #ifdef INET6
110 1.15 itojun struct in6_addr ni_laddr6; /* local address */
111 1.15 itojun #endif
112 1.1 jtc long ni_lport; /* local port */
113 1.1 jtc struct in_addr ni_faddr; /* foreign address */
114 1.15 itojun #ifdef INET6
115 1.15 itojun struct in6_addr ni_faddr6; /* foreign address */
116 1.15 itojun #endif
117 1.1 jtc long ni_fport; /* foreign port */
118 1.1 jtc long ni_rcvcc; /* rcv buffer character count */
119 1.1 jtc long ni_sndcc; /* snd buffer character count */
120 1.1 jtc };
121 1.1 jtc
122 1.1 jtc static struct {
123 1.1 jtc struct netinfo *ni_forw, *ni_prev;
124 1.1 jtc } netcb;
125 1.1 jtc
126 1.1 jtc static int aflag = 0;
127 1.16 itojun int nflag = 0;
128 1.1 jtc static int lastrow = 1;
129 1.1 jtc
130 1.9 mrg WINDOW *
131 1.9 mrg opennetstat()
132 1.9 mrg {
133 1.9 mrg
134 1.9 mrg sethostent(1);
135 1.9 mrg setnetent(1);
136 1.9 mrg return (subwin(stdscr, LINES-5-1, 0, 5, 0));
137 1.9 mrg }
138 1.9 mrg
139 1.1 jtc void
140 1.1 jtc closenetstat(w)
141 1.5 scottr WINDOW *w;
142 1.1 jtc {
143 1.8 lukem struct netinfo *p;
144 1.1 jtc
145 1.1 jtc endhostent();
146 1.1 jtc endnetent();
147 1.1 jtc p = (struct netinfo *)netcb.ni_forw;
148 1.1 jtc while (p != (struct netinfo *)&netcb) {
149 1.1 jtc if (p->ni_line != -1)
150 1.1 jtc lastrow--;
151 1.1 jtc p->ni_line = -1;
152 1.1 jtc p = p->ni_forw;
153 1.1 jtc }
154 1.5 scottr if (w != NULL) {
155 1.1 jtc wclear(w);
156 1.1 jtc wrefresh(w);
157 1.1 jtc delwin(w);
158 1.1 jtc }
159 1.1 jtc }
160 1.1 jtc
161 1.1 jtc static struct nlist namelist[] = {
162 1.3 cgd #define X_TCBTABLE 0
163 1.3 cgd { "_tcbtable" },
164 1.3 cgd #define X_UDBTABLE 1
165 1.3 cgd { "_udbtable" },
166 1.15 itojun #ifdef INET6
167 1.15 itojun #define X_TCB6 2
168 1.15 itojun { "_tcb6" },
169 1.15 itojun #define X_UDB6 3
170 1.15 itojun { "_udb6" },
171 1.15 itojun #endif
172 1.1 jtc { "" },
173 1.1 jtc };
174 1.1 jtc
175 1.1 jtc int
176 1.1 jtc initnetstat()
177 1.1 jtc {
178 1.1 jtc if (kvm_nlist(kd, namelist)) {
179 1.1 jtc nlisterr(namelist);
180 1.1 jtc return(0);
181 1.1 jtc }
182 1.3 cgd if (namelist[X_TCBTABLE].n_value == 0) {
183 1.1 jtc error("No symbols in namelist");
184 1.1 jtc return(0);
185 1.1 jtc }
186 1.1 jtc netcb.ni_forw = netcb.ni_prev = (struct netinfo *)&netcb;
187 1.1 jtc protos = TCP|UDP;
188 1.1 jtc return(1);
189 1.1 jtc }
190 1.1 jtc
191 1.1 jtc void
192 1.1 jtc fetchnetstat()
193 1.1 jtc {
194 1.3 cgd struct inpcbtable pcbtable;
195 1.8 lukem struct inpcb *head, *prev, *next;
196 1.8 lukem struct netinfo *p;
197 1.1 jtc struct inpcb inpcb;
198 1.1 jtc struct socket sockb;
199 1.1 jtc struct tcpcb tcpcb;
200 1.15 itojun #ifdef INET6
201 1.15 itojun struct in6pcb in6pcb;
202 1.15 itojun struct in6pcb *head6, *prev6, *next6;
203 1.15 itojun #endif
204 1.1 jtc void *off;
205 1.1 jtc int istcp;
206 1.1 jtc
207 1.3 cgd if (namelist[X_TCBTABLE].n_value == 0)
208 1.1 jtc return;
209 1.1 jtc for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw)
210 1.1 jtc p->ni_seen = 0;
211 1.1 jtc if (protos&TCP) {
212 1.3 cgd off = NPTR(X_TCBTABLE);
213 1.1 jtc istcp = 1;
214 1.1 jtc }
215 1.1 jtc else if (protos&UDP) {
216 1.3 cgd off = NPTR(X_UDBTABLE);
217 1.1 jtc istcp = 0;
218 1.1 jtc }
219 1.1 jtc else {
220 1.1 jtc error("No protocols to display");
221 1.1 jtc return;
222 1.1 jtc }
223 1.1 jtc again:
224 1.11 ross KREAD(off, &pcbtable, sizeof pcbtable);
225 1.3 cgd prev = head = (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue;
226 1.3 cgd next = pcbtable.inpt_queue.cqh_first;
227 1.3 cgd while (next != head) {
228 1.1 jtc KREAD(next, &inpcb, sizeof (inpcb));
229 1.3 cgd if (inpcb.inp_queue.cqe_prev != prev) {
230 1.7 mrg printf("prev = %p, head = %p, next = %p, inpcb...prev = %p\n", prev, head, next, inpcb.inp_queue.cqe_prev);
231 1.1 jtc p = netcb.ni_forw;
232 1.1 jtc for (; p != (struct netinfo *)&netcb; p = p->ni_forw)
233 1.1 jtc p->ni_seen = 1;
234 1.1 jtc error("Kernel state in transition");
235 1.1 jtc return;
236 1.1 jtc }
237 1.3 cgd prev = next;
238 1.3 cgd next = inpcb.inp_queue.cqe_next;
239 1.3 cgd
240 1.1 jtc if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
241 1.1 jtc continue;
242 1.1 jtc if (nhosts && !checkhost(&inpcb))
243 1.1 jtc continue;
244 1.1 jtc if (nports && !checkport(&inpcb))
245 1.1 jtc continue;
246 1.1 jtc KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
247 1.1 jtc if (istcp) {
248 1.1 jtc KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
249 1.1 jtc enter(&inpcb, &sockb, tcpcb.t_state, "tcp");
250 1.1 jtc } else
251 1.1 jtc enter(&inpcb, &sockb, 0, "udp");
252 1.1 jtc }
253 1.1 jtc if (istcp && (protos&UDP)) {
254 1.1 jtc istcp = 0;
255 1.3 cgd off = NPTR(X_UDBTABLE);
256 1.1 jtc goto again;
257 1.1 jtc }
258 1.15 itojun
259 1.15 itojun #ifdef INET6
260 1.15 itojun if (protos&TCP) {
261 1.15 itojun off = NPTR(X_TCB6);
262 1.15 itojun istcp = 1;
263 1.15 itojun }
264 1.15 itojun else if (protos&UDP) {
265 1.15 itojun off = NPTR(X_UDB6);
266 1.15 itojun istcp = 0;
267 1.15 itojun }
268 1.15 itojun else {
269 1.15 itojun error("No protocols to display");
270 1.15 itojun return;
271 1.15 itojun }
272 1.15 itojun again6:
273 1.15 itojun KREAD(off, &in6pcb, sizeof (struct in6pcb));
274 1.15 itojun prev6 = head6 = (struct in6pcb *)off;
275 1.15 itojun next6 = in6pcb.in6p_next;
276 1.15 itojun while (next6 != head6) {
277 1.15 itojun KREAD(next6, &in6pcb, sizeof (in6pcb));
278 1.15 itojun if (in6pcb.in6p_prev != prev6) {
279 1.15 itojun printf("prev = %p, head = %p, next = %p, inpcb...prev = %p\n", prev6, head6, next6, in6pcb.in6p_prev);
280 1.15 itojun p = netcb.ni_forw;
281 1.15 itojun for (; p != (struct netinfo *)&netcb; p = p->ni_forw)
282 1.15 itojun p->ni_seen = 1;
283 1.15 itojun error("Kernel state in transition");
284 1.15 itojun return;
285 1.15 itojun }
286 1.15 itojun prev6 = next6;
287 1.15 itojun next6 = in6pcb.in6p_next;
288 1.15 itojun
289 1.15 itojun if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr))
290 1.15 itojun continue;
291 1.15 itojun if (nhosts && !checkhost6(&in6pcb))
292 1.15 itojun continue;
293 1.15 itojun if (nports && !checkport6(&in6pcb))
294 1.15 itojun continue;
295 1.15 itojun KREAD(in6pcb.in6p_socket, &sockb, sizeof (sockb));
296 1.15 itojun if (istcp) {
297 1.15 itojun KREAD(in6pcb.in6p_ppcb, &tcpcb, sizeof (tcpcb));
298 1.15 itojun enter6(&in6pcb, &sockb, tcpcb.t_state, "tcp");
299 1.15 itojun } else
300 1.15 itojun enter6(&in6pcb, &sockb, 0, "udp");
301 1.15 itojun }
302 1.15 itojun if (istcp && (protos&UDP)) {
303 1.15 itojun istcp = 0;
304 1.15 itojun off = NPTR(X_UDB6);
305 1.15 itojun goto again6;
306 1.15 itojun }
307 1.15 itojun #endif /*INET6*/
308 1.1 jtc }
309 1.1 jtc
310 1.1 jtc static void
311 1.1 jtc enter(inp, so, state, proto)
312 1.7 mrg struct inpcb *inp;
313 1.7 mrg struct socket *so;
314 1.1 jtc int state;
315 1.1 jtc char *proto;
316 1.1 jtc {
317 1.8 lukem struct netinfo *p;
318 1.1 jtc
319 1.1 jtc /*
320 1.1 jtc * Only take exact matches, any sockets with
321 1.1 jtc * previously unbound addresses will be deleted
322 1.1 jtc * below in the display routine because they
323 1.1 jtc * will appear as ``not seen'' in the kernel
324 1.1 jtc * data structures.
325 1.1 jtc */
326 1.1 jtc for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
327 1.15 itojun if (p->ni_family != AF_INET)
328 1.15 itojun continue;
329 1.1 jtc if (!streq(proto, p->ni_proto))
330 1.1 jtc continue;
331 1.1 jtc if (p->ni_lport != inp->inp_lport ||
332 1.1 jtc p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
333 1.1 jtc continue;
334 1.1 jtc if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
335 1.1 jtc p->ni_fport == inp->inp_fport)
336 1.1 jtc break;
337 1.1 jtc }
338 1.1 jtc if (p == (struct netinfo *)&netcb) {
339 1.1 jtc if ((p = malloc(sizeof(*p))) == NULL) {
340 1.1 jtc error("Out of memory");
341 1.1 jtc return;
342 1.1 jtc }
343 1.1 jtc p->ni_prev = (struct netinfo *)&netcb;
344 1.1 jtc p->ni_forw = netcb.ni_forw;
345 1.1 jtc netcb.ni_forw->ni_prev = p;
346 1.1 jtc netcb.ni_forw = p;
347 1.1 jtc p->ni_line = -1;
348 1.1 jtc p->ni_laddr = inp->inp_laddr;
349 1.1 jtc p->ni_lport = inp->inp_lport;
350 1.1 jtc p->ni_faddr = inp->inp_faddr;
351 1.1 jtc p->ni_fport = inp->inp_fport;
352 1.1 jtc p->ni_proto = proto;
353 1.1 jtc p->ni_flags = NIF_LACHG|NIF_FACHG;
354 1.15 itojun p->ni_family = AF_INET;
355 1.1 jtc }
356 1.1 jtc p->ni_rcvcc = so->so_rcv.sb_cc;
357 1.1 jtc p->ni_sndcc = so->so_snd.sb_cc;
358 1.1 jtc p->ni_state = state;
359 1.1 jtc p->ni_seen = 1;
360 1.1 jtc }
361 1.1 jtc
362 1.15 itojun #ifdef INET6
363 1.15 itojun static void
364 1.15 itojun enter6(in6p, so, state, proto)
365 1.15 itojun struct in6pcb *in6p;
366 1.15 itojun struct socket *so;
367 1.15 itojun int state;
368 1.15 itojun char *proto;
369 1.15 itojun {
370 1.15 itojun struct netinfo *p;
371 1.15 itojun
372 1.15 itojun /*
373 1.15 itojun * Only take exact matches, any sockets with
374 1.15 itojun * previously unbound addresses will be deleted
375 1.15 itojun * below in the display routine because they
376 1.15 itojun * will appear as ``not seen'' in the kernel
377 1.15 itojun * data structures.
378 1.15 itojun */
379 1.15 itojun for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
380 1.15 itojun if (p->ni_family != AF_INET6)
381 1.15 itojun continue;
382 1.15 itojun if (!streq(proto, p->ni_proto))
383 1.15 itojun continue;
384 1.15 itojun if (p->ni_lport != in6p->in6p_lport ||
385 1.15 itojun !IN6_ARE_ADDR_EQUAL(&p->ni_laddr6, &in6p->in6p_laddr))
386 1.15 itojun continue;
387 1.15 itojun if (IN6_ARE_ADDR_EQUAL(&p->ni_faddr6, &in6p->in6p_faddr) &&
388 1.15 itojun p->ni_fport == in6p->in6p_fport)
389 1.15 itojun break;
390 1.15 itojun }
391 1.15 itojun if (p == (struct netinfo *)&netcb) {
392 1.15 itojun if ((p = malloc(sizeof(*p))) == NULL) {
393 1.15 itojun error("Out of memory");
394 1.15 itojun return;
395 1.15 itojun }
396 1.15 itojun p->ni_prev = (struct netinfo *)&netcb;
397 1.15 itojun p->ni_forw = netcb.ni_forw;
398 1.15 itojun netcb.ni_forw->ni_prev = p;
399 1.15 itojun netcb.ni_forw = p;
400 1.15 itojun p->ni_line = -1;
401 1.15 itojun p->ni_laddr6 = in6p->in6p_laddr;
402 1.15 itojun p->ni_lport = in6p->in6p_lport;
403 1.15 itojun p->ni_faddr6 = in6p->in6p_faddr;
404 1.15 itojun p->ni_fport = in6p->in6p_fport;
405 1.15 itojun p->ni_proto = proto;
406 1.15 itojun p->ni_flags = NIF_LACHG|NIF_FACHG;
407 1.15 itojun p->ni_family = AF_INET6;
408 1.15 itojun }
409 1.15 itojun p->ni_rcvcc = so->so_rcv.sb_cc;
410 1.15 itojun p->ni_sndcc = so->so_snd.sb_cc;
411 1.15 itojun p->ni_state = state;
412 1.15 itojun p->ni_seen = 1;
413 1.15 itojun }
414 1.15 itojun #endif
415 1.15 itojun
416 1.1 jtc /* column locations */
417 1.1 jtc #define LADDR 0
418 1.1 jtc #define FADDR LADDR+23
419 1.1 jtc #define PROTO FADDR+23
420 1.1 jtc #define RCVCC PROTO+6
421 1.1 jtc #define SNDCC RCVCC+7
422 1.1 jtc #define STATE SNDCC+7
423 1.1 jtc
424 1.1 jtc void
425 1.1 jtc labelnetstat()
426 1.1 jtc {
427 1.9 mrg
428 1.3 cgd if (namelist[X_TCBTABLE].n_type == 0)
429 1.1 jtc return;
430 1.1 jtc wmove(wnd, 0, 0); wclrtobot(wnd);
431 1.1 jtc mvwaddstr(wnd, 0, LADDR, "Local Address");
432 1.1 jtc mvwaddstr(wnd, 0, FADDR, "Foreign Address");
433 1.1 jtc mvwaddstr(wnd, 0, PROTO, "Proto");
434 1.1 jtc mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
435 1.1 jtc mvwaddstr(wnd, 0, SNDCC, "Send-Q");
436 1.1 jtc mvwaddstr(wnd, 0, STATE, "(state)");
437 1.1 jtc }
438 1.1 jtc
439 1.1 jtc void
440 1.1 jtc shownetstat()
441 1.1 jtc {
442 1.8 lukem struct netinfo *p, *q;
443 1.1 jtc
444 1.1 jtc /*
445 1.1 jtc * First, delete any connections that have gone
446 1.1 jtc * away and adjust the position of connections
447 1.1 jtc * below to reflect the deleted line.
448 1.1 jtc */
449 1.1 jtc p = netcb.ni_forw;
450 1.1 jtc while (p != (struct netinfo *)&netcb) {
451 1.1 jtc if (p->ni_line == -1 || p->ni_seen) {
452 1.1 jtc p = p->ni_forw;
453 1.1 jtc continue;
454 1.1 jtc }
455 1.1 jtc wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
456 1.1 jtc q = netcb.ni_forw;
457 1.1 jtc for (; q != (struct netinfo *)&netcb; q = q->ni_forw)
458 1.1 jtc if (q != p && q->ni_line > p->ni_line) {
459 1.1 jtc q->ni_line--;
460 1.1 jtc /* this shouldn't be necessary */
461 1.1 jtc q->ni_flags |= NIF_LACHG|NIF_FACHG;
462 1.1 jtc }
463 1.1 jtc lastrow--;
464 1.1 jtc q = p->ni_forw;
465 1.1 jtc p->ni_prev->ni_forw = p->ni_forw;
466 1.1 jtc p->ni_forw->ni_prev = p->ni_prev;
467 1.1 jtc free(p);
468 1.1 jtc p = q;
469 1.1 jtc }
470 1.1 jtc /*
471 1.1 jtc * Update existing connections and add new ones.
472 1.1 jtc */
473 1.1 jtc for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
474 1.1 jtc if (p->ni_line == -1) {
475 1.1 jtc /*
476 1.1 jtc * Add a new entry if possible.
477 1.1 jtc */
478 1.6 jtc if (lastrow > getmaxy(wnd))
479 1.1 jtc continue;
480 1.1 jtc p->ni_line = lastrow++;
481 1.1 jtc p->ni_flags |= NIF_LACHG|NIF_FACHG;
482 1.1 jtc }
483 1.1 jtc if (p->ni_flags & NIF_LACHG) {
484 1.1 jtc wmove(wnd, p->ni_line, LADDR);
485 1.15 itojun switch (p->ni_family) {
486 1.15 itojun case AF_INET:
487 1.15 itojun inetprint(&p->ni_laddr, p->ni_lport,
488 1.15 itojun p->ni_proto);
489 1.15 itojun break;
490 1.15 itojun #ifdef INET6
491 1.15 itojun case AF_INET6:
492 1.15 itojun inet6print(&p->ni_laddr6, p->ni_lport,
493 1.15 itojun p->ni_proto);
494 1.15 itojun break;
495 1.15 itojun #endif
496 1.15 itojun }
497 1.1 jtc p->ni_flags &= ~NIF_LACHG;
498 1.1 jtc }
499 1.1 jtc if (p->ni_flags & NIF_FACHG) {
500 1.1 jtc wmove(wnd, p->ni_line, FADDR);
501 1.15 itojun switch (p->ni_family) {
502 1.15 itojun case AF_INET:
503 1.15 itojun inetprint(&p->ni_faddr, p->ni_fport,
504 1.15 itojun p->ni_proto);
505 1.15 itojun break;
506 1.15 itojun #ifdef INET6
507 1.15 itojun case AF_INET6:
508 1.15 itojun inet6print(&p->ni_faddr6, p->ni_fport,
509 1.15 itojun p->ni_proto);
510 1.15 itojun break;
511 1.15 itojun #endif
512 1.15 itojun }
513 1.1 jtc p->ni_flags &= ~NIF_FACHG;
514 1.1 jtc }
515 1.1 jtc mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
516 1.15 itojun #ifdef INET6
517 1.15 itojun if (p->ni_family == AF_INET6)
518 1.15 itojun waddstr(wnd, "6");
519 1.15 itojun #endif
520 1.1 jtc mvwprintw(wnd, p->ni_line, RCVCC, "%6d", p->ni_rcvcc);
521 1.1 jtc mvwprintw(wnd, p->ni_line, SNDCC, "%6d", p->ni_sndcc);
522 1.10 ross if (streq(p->ni_proto, "tcp")) {
523 1.1 jtc if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
524 1.1 jtc mvwprintw(wnd, p->ni_line, STATE, "%d",
525 1.1 jtc p->ni_state);
526 1.1 jtc else
527 1.1 jtc mvwaddstr(wnd, p->ni_line, STATE,
528 1.1 jtc tcpstates[p->ni_state]);
529 1.10 ross }
530 1.1 jtc wclrtoeol(wnd);
531 1.1 jtc }
532 1.6 jtc if (lastrow < getmaxy(wnd)) {
533 1.1 jtc wmove(wnd, lastrow, 0); wclrtobot(wnd);
534 1.6 jtc wmove(wnd, getmaxy(wnd), 0); wdeleteln(wnd); /* XXX */
535 1.1 jtc }
536 1.1 jtc }
537 1.1 jtc
538 1.1 jtc /*
539 1.1 jtc * Pretty print an Internet address (net address + port).
540 1.1 jtc * If the nflag was specified, use numbers instead of names.
541 1.1 jtc */
542 1.1 jtc static void
543 1.1 jtc inetprint(in, port, proto)
544 1.7 mrg struct in_addr *in;
545 1.1 jtc int port;
546 1.1 jtc char *proto;
547 1.1 jtc {
548 1.1 jtc struct servent *sp = 0;
549 1.7 mrg char line[80], *cp;
550 1.1 jtc
551 1.9 mrg (void)snprintf(line, sizeof line, "%.*s.", 16, inetname(*in));
552 1.8 lukem cp = strchr(line, '\0');
553 1.1 jtc if (!nflag && port)
554 1.1 jtc sp = getservbyport(port, proto);
555 1.1 jtc if (sp || port == 0)
556 1.9 mrg (void)snprintf(cp, line + sizeof line - cp, "%.8s",
557 1.9 mrg sp ? sp->s_name : "*");
558 1.1 jtc else
559 1.9 mrg (void)snprintf(cp, line + sizeof line - cp, "%d",
560 1.9 mrg ntohs((u_short)port));
561 1.1 jtc /* pad to full column to clear any garbage */
562 1.8 lukem cp = strchr(line, '\0');
563 1.1 jtc while (cp - line < 22)
564 1.1 jtc *cp++ = ' ';
565 1.1 jtc *cp = '\0';
566 1.1 jtc waddstr(wnd, line);
567 1.1 jtc }
568 1.1 jtc
569 1.15 itojun #ifdef INET6
570 1.15 itojun static void
571 1.15 itojun inet6print(in6, port, proto)
572 1.15 itojun struct in6_addr *in6;
573 1.15 itojun int port;
574 1.15 itojun char *proto;
575 1.15 itojun {
576 1.15 itojun struct servent *sp = 0;
577 1.15 itojun char line[80], *cp;
578 1.15 itojun
579 1.15 itojun (void)snprintf(line, sizeof line, "%.*s.", 16, inet6name(in6));
580 1.15 itojun cp = strchr(line, '\0');
581 1.15 itojun if (!nflag && port)
582 1.15 itojun sp = getservbyport(port, proto);
583 1.15 itojun if (sp || port == 0)
584 1.15 itojun (void)snprintf(cp, line + sizeof line - cp, "%.8s",
585 1.15 itojun sp ? sp->s_name : "*");
586 1.15 itojun else
587 1.15 itojun (void)snprintf(cp, line + sizeof line - cp, "%d",
588 1.15 itojun ntohs((u_short)port));
589 1.15 itojun /* pad to full column to clear any garbage */
590 1.15 itojun cp = strchr(line, '\0');
591 1.15 itojun while (cp - line < 22)
592 1.15 itojun *cp++ = ' ';
593 1.15 itojun *cp = '\0';
594 1.15 itojun waddstr(wnd, line);
595 1.15 itojun }
596 1.15 itojun #endif
597 1.15 itojun
598 1.1 jtc /*
599 1.1 jtc * Construct an Internet address representation.
600 1.1 jtc * If the nflag has been supplied, give
601 1.1 jtc * numeric value, otherwise try for symbolic name.
602 1.1 jtc */
603 1.15 itojun static const char *
604 1.1 jtc inetname(in)
605 1.1 jtc struct in_addr in;
606 1.1 jtc {
607 1.1 jtc char *cp = 0;
608 1.1 jtc static char line[50];
609 1.1 jtc struct hostent *hp;
610 1.1 jtc struct netent *np;
611 1.1 jtc
612 1.1 jtc if (!nflag && in.s_addr != INADDR_ANY) {
613 1.1 jtc int net = inet_netof(in);
614 1.1 jtc int lna = inet_lnaof(in);
615 1.1 jtc
616 1.1 jtc if (lna == INADDR_ANY) {
617 1.1 jtc np = getnetbyaddr(net, AF_INET);
618 1.1 jtc if (np)
619 1.1 jtc cp = np->n_name;
620 1.1 jtc }
621 1.1 jtc if (cp == 0) {
622 1.1 jtc hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
623 1.1 jtc if (hp)
624 1.1 jtc cp = hp->h_name;
625 1.1 jtc }
626 1.1 jtc }
627 1.1 jtc if (in.s_addr == INADDR_ANY)
628 1.9 mrg strncpy(line, "*", sizeof(line) - 1);
629 1.1 jtc else if (cp)
630 1.9 mrg strncpy(line, cp, sizeof(line) - 1);
631 1.1 jtc else {
632 1.1 jtc in.s_addr = ntohl(in.s_addr);
633 1.1 jtc #define C(x) ((x) & 0xff)
634 1.9 mrg (void)snprintf(line, sizeof line, "%u.%u.%u.%u",
635 1.9 mrg C(in.s_addr >> 24), C(in.s_addr >> 16),
636 1.9 mrg C(in.s_addr >> 8), C(in.s_addr));
637 1.9 mrg #undef C
638 1.1 jtc }
639 1.9 mrg line[sizeof(line) - 1] = '\0';
640 1.1 jtc return (line);
641 1.1 jtc }
642 1.15 itojun
643 1.15 itojun #ifdef INET6
644 1.15 itojun static const char *
645 1.15 itojun inet6name(in6)
646 1.15 itojun struct in6_addr *in6;
647 1.15 itojun {
648 1.15 itojun static char line[NI_MAXHOST];
649 1.15 itojun struct sockaddr_in6 sin6;
650 1.15 itojun int flags;
651 1.15 itojun
652 1.15 itojun if (nflag)
653 1.15 itojun flags = NI_NUMERICHOST;
654 1.15 itojun else
655 1.15 itojun flags = 0;
656 1.15 itojun if (IN6_IS_ADDR_UNSPECIFIED(in6))
657 1.15 itojun return "*";
658 1.15 itojun memset(&sin6, 0, sizeof(sin6));
659 1.15 itojun sin6.sin6_family = AF_INET6;
660 1.15 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
661 1.15 itojun sin6.sin6_addr = *in6;
662 1.15 itojun if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
663 1.15 itojun line, sizeof(line), NULL, 0, flags) == 0)
664 1.15 itojun return line;
665 1.15 itojun return "?";
666 1.15 itojun }
667 1.15 itojun #endif
668 1.1 jtc
669 1.12 jwise /* please note: there are also some netstat commands in netcmds.c */
670 1.12 jwise
671 1.12 jwise void
672 1.12 jwise netstat_all (args)
673 1.12 jwise char *args;
674 1.12 jwise {
675 1.12 jwise aflag = !aflag;
676 1.12 jwise fetchnetstat();
677 1.12 jwise shownetstat();
678 1.12 jwise refresh();
679 1.12 jwise }
680 1.12 jwise
681 1.12 jwise void
682 1.12 jwise netstat_names (args)
683 1.12 jwise char *args;
684 1.12 jwise {
685 1.12 jwise struct netinfo *p;
686 1.12 jwise
687 1.12 jwise if (nflag == 0)
688 1.12 jwise return;
689 1.12 jwise
690 1.12 jwise p = netcb.ni_forw;
691 1.12 jwise for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
692 1.12 jwise if (p->ni_line == -1)
693 1.12 jwise continue;
694 1.12 jwise p->ni_flags |= NIF_LACHG|NIF_FACHG;
695 1.12 jwise }
696 1.12 jwise nflag = 0;
697 1.12 jwise wclear(wnd);
698 1.12 jwise labelnetstat();
699 1.12 jwise shownetstat();
700 1.12 jwise refresh();
701 1.12 jwise }
702 1.12 jwise
703 1.12 jwise void
704 1.12 jwise netstat_numbers (args)
705 1.12 jwise char *args;
706 1.1 jtc {
707 1.8 lukem struct netinfo *p;
708 1.1 jtc
709 1.13 itojun if (nflag != 0)
710 1.12 jwise return;
711 1.12 jwise
712 1.12 jwise p = netcb.ni_forw;
713 1.12 jwise for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
714 1.12 jwise if (p->ni_line == -1)
715 1.12 jwise continue;
716 1.12 jwise p->ni_flags |= NIF_LACHG|NIF_FACHG;
717 1.12 jwise }
718 1.13 itojun nflag = 1;
719 1.12 jwise wclear(wnd);
720 1.12 jwise labelnetstat();
721 1.1 jtc shownetstat();
722 1.1 jtc refresh();
723 1.1 jtc }
724