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