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