netstat.c revision 1.2 1 /* $NetBSD: netstat.c,v 1.2 1995/01/20 08:52:05 jtc Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: netstat.c,v 1.2 1995/01/20 08:52:05 jtc Exp $";
41 #endif /* not lint */
42
43 /*
44 * netstat
45 */
46 #include <sys/param.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/mbuf.h>
50 #include <sys/protosw.h>
51
52 #include <netinet/in.h>
53 #include <net/route.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_icmp.h>
58 #include <netinet/icmp_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcpip.h>
62 #include <netinet/tcp_seq.h>
63 #define TCPSTATES
64 #include <netinet/tcp_fsm.h>
65 #include <netinet/tcp_timer.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet/tcp_debug.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70
71 #include <netdb.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <nlist.h>
75 #include <paths.h>
76 #include "systat.h"
77 #include "extern.h"
78
79 static void enter __P((struct inpcb *, struct socket *, int, char *));
80 static char *inetname __P((struct in_addr));
81 static void inetprint __P((struct in_addr *, int, char *));
82
83 #define streq(a,b) (strcmp(a,b)==0)
84 #define YMAX(w) ((w)->maxy-1)
85
86 WINDOW *
87 opennetstat()
88 {
89 sethostent(1);
90 setnetent(1);
91 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
92 }
93
94 struct netinfo {
95 struct netinfo *ni_forw, *ni_prev;
96 short ni_line; /* line on screen */
97 short ni_seen; /* 0 when not present in list */
98 short ni_flags;
99 #define NIF_LACHG 0x1 /* local address changed */
100 #define NIF_FACHG 0x2 /* foreign address changed */
101 short ni_state; /* tcp state */
102 char *ni_proto; /* protocol */
103 struct in_addr ni_laddr; /* local address */
104 long ni_lport; /* local port */
105 struct in_addr ni_faddr; /* foreign address */
106 long ni_fport; /* foreign port */
107 long ni_rcvcc; /* rcv buffer character count */
108 long ni_sndcc; /* snd buffer character count */
109 };
110
111 static struct {
112 struct netinfo *ni_forw, *ni_prev;
113 } netcb;
114
115 static int aflag = 0;
116 static int nflag = 0;
117 static int lastrow = 1;
118 static void enter(), inetprint();
119 static char *inetname();
120
121 void
122 closenetstat(w)
123 WINDOW *w;
124 {
125 register struct netinfo *p;
126
127 endhostent();
128 endnetent();
129 p = (struct netinfo *)netcb.ni_forw;
130 while (p != (struct netinfo *)&netcb) {
131 if (p->ni_line != -1)
132 lastrow--;
133 p->ni_line = -1;
134 p = p->ni_forw;
135 }
136 if (w != NULL) {
137 wclear(w);
138 wrefresh(w);
139 delwin(w);
140 }
141 }
142
143 static struct nlist namelist[] = {
144 #define X_TCB 0
145 { "_tcb" },
146 #define X_UDB 1
147 { "_udb" },
148 { "" },
149 };
150
151 int
152 initnetstat()
153 {
154 if (kvm_nlist(kd, namelist)) {
155 nlisterr(namelist);
156 return(0);
157 }
158 if (namelist[X_TCB].n_value == 0) {
159 error("No symbols in namelist");
160 return(0);
161 }
162 netcb.ni_forw = netcb.ni_prev = (struct netinfo *)&netcb;
163 protos = TCP|UDP;
164 return(1);
165 }
166
167 void
168 fetchnetstat()
169 {
170 register struct inpcb *prev, *next;
171 register struct netinfo *p;
172 struct inpcb inpcb;
173 struct socket sockb;
174 struct tcpcb tcpcb;
175 void *off;
176 int istcp;
177
178 if (namelist[X_TCB].n_value == 0)
179 return;
180 for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw)
181 p->ni_seen = 0;
182 if (protos&TCP) {
183 off = NPTR(X_TCB);
184 istcp = 1;
185 }
186 else if (protos&UDP) {
187 off = NPTR(X_UDB);
188 istcp = 0;
189 }
190 else {
191 error("No protocols to display");
192 return;
193 }
194 again:
195 KREAD(off, &inpcb, sizeof (struct inpcb));
196 prev = off;
197 for (; inpcb.inp_next != off; prev = next) {
198 next = inpcb.inp_next;
199 KREAD(next, &inpcb, sizeof (inpcb));
200 if (inpcb.inp_prev != prev) {
201 p = netcb.ni_forw;
202 for (; p != (struct netinfo *)&netcb; p = p->ni_forw)
203 p->ni_seen = 1;
204 error("Kernel state in transition");
205 return;
206 }
207 if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
208 continue;
209 if (nhosts && !checkhost(&inpcb))
210 continue;
211 if (nports && !checkport(&inpcb))
212 continue;
213 KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
214 if (istcp) {
215 KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
216 enter(&inpcb, &sockb, tcpcb.t_state, "tcp");
217 } else
218 enter(&inpcb, &sockb, 0, "udp");
219 }
220 if (istcp && (protos&UDP)) {
221 istcp = 0;
222 off = NPTR(X_UDB);
223 goto again;
224 }
225 }
226
227 static void
228 enter(inp, so, state, proto)
229 register struct inpcb *inp;
230 register struct socket *so;
231 int state;
232 char *proto;
233 {
234 register struct netinfo *p;
235
236 /*
237 * Only take exact matches, any sockets with
238 * previously unbound addresses will be deleted
239 * below in the display routine because they
240 * will appear as ``not seen'' in the kernel
241 * data structures.
242 */
243 for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
244 if (!streq(proto, p->ni_proto))
245 continue;
246 if (p->ni_lport != inp->inp_lport ||
247 p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
248 continue;
249 if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
250 p->ni_fport == inp->inp_fport)
251 break;
252 }
253 if (p == (struct netinfo *)&netcb) {
254 if ((p = malloc(sizeof(*p))) == NULL) {
255 error("Out of memory");
256 return;
257 }
258 p->ni_prev = (struct netinfo *)&netcb;
259 p->ni_forw = netcb.ni_forw;
260 netcb.ni_forw->ni_prev = p;
261 netcb.ni_forw = p;
262 p->ni_line = -1;
263 p->ni_laddr = inp->inp_laddr;
264 p->ni_lport = inp->inp_lport;
265 p->ni_faddr = inp->inp_faddr;
266 p->ni_fport = inp->inp_fport;
267 p->ni_proto = proto;
268 p->ni_flags = NIF_LACHG|NIF_FACHG;
269 }
270 p->ni_rcvcc = so->so_rcv.sb_cc;
271 p->ni_sndcc = so->so_snd.sb_cc;
272 p->ni_state = state;
273 p->ni_seen = 1;
274 }
275
276 /* column locations */
277 #define LADDR 0
278 #define FADDR LADDR+23
279 #define PROTO FADDR+23
280 #define RCVCC PROTO+6
281 #define SNDCC RCVCC+7
282 #define STATE SNDCC+7
283
284
285 void
286 labelnetstat()
287 {
288 if (namelist[X_TCB].n_type == 0)
289 return;
290 wmove(wnd, 0, 0); wclrtobot(wnd);
291 mvwaddstr(wnd, 0, LADDR, "Local Address");
292 mvwaddstr(wnd, 0, FADDR, "Foreign Address");
293 mvwaddstr(wnd, 0, PROTO, "Proto");
294 mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
295 mvwaddstr(wnd, 0, SNDCC, "Send-Q");
296 mvwaddstr(wnd, 0, STATE, "(state)");
297 }
298
299 void
300 shownetstat()
301 {
302 register struct netinfo *p, *q;
303
304 /*
305 * First, delete any connections that have gone
306 * away and adjust the position of connections
307 * below to reflect the deleted line.
308 */
309 p = netcb.ni_forw;
310 while (p != (struct netinfo *)&netcb) {
311 if (p->ni_line == -1 || p->ni_seen) {
312 p = p->ni_forw;
313 continue;
314 }
315 wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
316 q = netcb.ni_forw;
317 for (; q != (struct netinfo *)&netcb; q = q->ni_forw)
318 if (q != p && q->ni_line > p->ni_line) {
319 q->ni_line--;
320 /* this shouldn't be necessary */
321 q->ni_flags |= NIF_LACHG|NIF_FACHG;
322 }
323 lastrow--;
324 q = p->ni_forw;
325 p->ni_prev->ni_forw = p->ni_forw;
326 p->ni_forw->ni_prev = p->ni_prev;
327 free(p);
328 p = q;
329 }
330 /*
331 * Update existing connections and add new ones.
332 */
333 for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
334 if (p->ni_line == -1) {
335 /*
336 * Add a new entry if possible.
337 */
338 if (lastrow > YMAX(wnd))
339 continue;
340 p->ni_line = lastrow++;
341 p->ni_flags |= NIF_LACHG|NIF_FACHG;
342 }
343 if (p->ni_flags & NIF_LACHG) {
344 wmove(wnd, p->ni_line, LADDR);
345 inetprint(&p->ni_laddr, p->ni_lport, p->ni_proto);
346 p->ni_flags &= ~NIF_LACHG;
347 }
348 if (p->ni_flags & NIF_FACHG) {
349 wmove(wnd, p->ni_line, FADDR);
350 inetprint(&p->ni_faddr, p->ni_fport, p->ni_proto);
351 p->ni_flags &= ~NIF_FACHG;
352 }
353 mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
354 mvwprintw(wnd, p->ni_line, RCVCC, "%6d", p->ni_rcvcc);
355 mvwprintw(wnd, p->ni_line, SNDCC, "%6d", p->ni_sndcc);
356 if (streq(p->ni_proto, "tcp"))
357 if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
358 mvwprintw(wnd, p->ni_line, STATE, "%d",
359 p->ni_state);
360 else
361 mvwaddstr(wnd, p->ni_line, STATE,
362 tcpstates[p->ni_state]);
363 wclrtoeol(wnd);
364 }
365 if (lastrow < YMAX(wnd)) {
366 wmove(wnd, lastrow, 0); wclrtobot(wnd);
367 wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd); /* XXX */
368 }
369 }
370
371 /*
372 * Pretty print an Internet address (net address + port).
373 * If the nflag was specified, use numbers instead of names.
374 */
375 static void
376 inetprint(in, port, proto)
377 register struct in_addr *in;
378 int port;
379 char *proto;
380 {
381 struct servent *sp = 0;
382 char line[80], *cp, *index();
383
384 sprintf(line, "%.*s.", 16, inetname(*in));
385 cp = index(line, '\0');
386 if (!nflag && port)
387 sp = getservbyport(port, proto);
388 if (sp || port == 0)
389 sprintf(cp, "%.8s", sp ? sp->s_name : "*");
390 else
391 sprintf(cp, "%d", ntohs((u_short)port));
392 /* pad to full column to clear any garbage */
393 cp = index(line, '\0');
394 while (cp - line < 22)
395 *cp++ = ' ';
396 *cp = '\0';
397 waddstr(wnd, line);
398 }
399
400 /*
401 * Construct an Internet address representation.
402 * If the nflag has been supplied, give
403 * numeric value, otherwise try for symbolic name.
404 */
405 static char *
406 inetname(in)
407 struct in_addr in;
408 {
409 char *cp = 0;
410 static char line[50];
411 struct hostent *hp;
412 struct netent *np;
413
414 if (!nflag && in.s_addr != INADDR_ANY) {
415 int net = inet_netof(in);
416 int lna = inet_lnaof(in);
417
418 if (lna == INADDR_ANY) {
419 np = getnetbyaddr(net, AF_INET);
420 if (np)
421 cp = np->n_name;
422 }
423 if (cp == 0) {
424 hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
425 if (hp)
426 cp = hp->h_name;
427 }
428 }
429 if (in.s_addr == INADDR_ANY)
430 strcpy(line, "*");
431 else if (cp)
432 strcpy(line, cp);
433 else {
434 in.s_addr = ntohl(in.s_addr);
435 #define C(x) ((x) & 0xff)
436 sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
437 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
438 }
439 return (line);
440 }
441
442 int
443 cmdnetstat(cmd, args)
444 char *cmd, *args;
445 {
446 register struct netinfo *p;
447
448 if (prefix(cmd, "all")) {
449 aflag = !aflag;
450 goto fixup;
451 }
452 if (prefix(cmd, "numbers") || prefix(cmd, "names")) {
453 int new;
454
455 new = prefix(cmd, "numbers");
456 if (new == nflag)
457 return (1);
458 p = netcb.ni_forw;
459 for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
460 if (p->ni_line == -1)
461 continue;
462 p->ni_flags |= NIF_LACHG|NIF_FACHG;
463 }
464 nflag = new;
465 goto redisplay;
466 }
467 if (!netcmd(cmd, args))
468 return (0);
469 fixup:
470 fetchnetstat();
471 redisplay:
472 shownetstat();
473 refresh();
474 return (1);
475 }
476