2 * Copyright (c) 1980, 1992, 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
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.
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
36 static char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.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 #include <netinet/tcp_var.h>
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>
80 static struct netinfo *enter __P((struct inpcb *, int, char *));
81 static void enter_kvm __P((struct inpcb *, struct socket *, int, char *));
82 static void enter_sysctl __P((struct inpcb *, struct xsocket *, int, char *));
83 static void fetchnetstat_kvm __P((void));
84 static void fetchnetstat_sysctl __P((void));
85 static char *inetname __P((struct in_addr));
86 static void inetprint __P((struct in_addr *, int, char *));
88 #define streq(a,b) (strcmp(a,b)==0)
89 #define YMAX(w) ((w)->_maxy-1)
96 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
100 TAILQ_ENTRY(netinfo) chain;
101 short ni_line; /* line on screen */
102 short ni_seen; /* 0 when not present in list */
104 #define NIF_LACHG 0x1 /* local address changed */
105 #define NIF_FACHG 0x2 /* foreign address changed */
106 short ni_state; /* tcp state */
107 char *ni_proto; /* protocol */
108 struct in_addr ni_laddr; /* local address */
109 long ni_lport; /* local port */
110 struct in_addr ni_faddr; /* foreign address */
111 long ni_fport; /* foreign port */
112 long ni_rcvcc; /* rcv buffer character count */
113 long ni_sndcc; /* snd buffer character count */
116 TAILQ_HEAD(netinfohead, netinfo) netcb = TAILQ_HEAD_INITIALIZER(netcb);
118 static int aflag = 0;
119 static int nflag = 0;
120 static int lastrow = 1;
121 static char *inetname();
127 register struct netinfo *p;
131 TAILQ_FOREACH(p, &netcb, chain) {
132 if (p->ni_line != -1)
143 static char *miblist[] = {
144 "net.inet.tcp.pcblist",
145 "net.inet.udp.pcblist"
148 struct nlist namelist[] = {
169 fetchnetstat_sysctl();
175 register struct inpcb *next;
176 register struct netinfo *p;
177 struct inpcbhead head;
184 if (namelist[X_TCB].n_value == 0)
186 TAILQ_FOREACH(p, &netcb, chain)
192 else if (protos&UDP) {
197 error("No protocols to display");
201 KREAD(off, &head, sizeof (struct inpcbhead));
202 LIST_FOREACH(next, &head, inp_list) {
203 KREAD(next, &inpcb, sizeof (inpcb));
205 if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
207 if (nhosts && !checkhost(&inpcb))
209 if (nports && !checkport(&inpcb))
211 KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
213 KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
214 enter_kvm(&inpcb, &sockb, tcpcb.t_state, "tcp");
216 enter_kvm(&inpcb, &sockb, 0, "udp");
218 if (istcp && (protos&UDP)) {
226 fetchnetstat_sysctl()
228 register struct netinfo *p;
230 struct xinpgen *inpg;
238 TAILQ_FOREACH(p, &netcb, chain)
242 } else if (protos&UDP) {
245 error("No protocols to display");
249 for (;idx < 2; idx++) {
250 if (idx == 1 && !(protos&UDP))
252 inpg = (struct xinpgen *)sysctl_dynread(miblist[idx], &lsz);
254 error("sysctl(%s...) failed", miblist[idx]);
258 * We currently do no require a consistent pcb list.
259 * Try to be robust in case of struct size changes
261 cur = ((char *)inpg) + inpg->xig_len;
262 /* There is also a trailing struct xinpgen */
263 end = ((char *)inpg) + lsz - inpg->xig_len;
268 if (idx == 0) { /* TCP */
269 xtp = (struct xtcpcb *)cur;
272 xip = (struct xinpcb *)cur;
275 while (cur + plen <= end) {
276 if (idx == 0) { /* TCP */
277 xtp = (struct xtcpcb *)cur;
278 inpcb = &xtp->xt_inp;
280 xip = (struct xinpcb *)cur;
281 inpcb = &xip->xi_inp;
285 if (!aflag && inet_lnaof(inpcb->inp_laddr) ==
288 if (nhosts && !checkhost(inpcb))
290 if (nports && !checkport(inpcb))
292 if (idx == 0) /* TCP */
293 enter_sysctl(inpcb, &xtp->xt_socket,
294 xtp->xt_tp.t_state, "tcp");
296 enter_sysctl(inpcb, &xip->xi_socket, 0, "udp");
303 enter_kvm(inp, so, state, proto)
304 register struct inpcb *inp;
305 register struct socket *so;
309 register struct netinfo *p;
311 if ((p = enter(inp, state, proto)) != NULL) {
312 p->ni_rcvcc = so->so_rcv.sb_cc;
313 p->ni_sndcc = so->so_snd.sb_cc;
318 enter_sysctl(inp, so, state, proto)
319 register struct inpcb *inp;
320 register struct xsocket *so;
324 register struct netinfo *p;
326 if ((p = enter(inp, state, proto)) != NULL) {
327 p->ni_rcvcc = so->so_rcv.sb_cc;
328 p->ni_sndcc = so->so_snd.sb_cc;
333 static struct netinfo *
334 enter(inp, state, proto)
335 register struct inpcb *inp;
339 register struct netinfo *p;
342 * Only take exact matches, any sockets with
343 * previously unbound addresses will be deleted
344 * below in the display routine because they
345 * will appear as ``not seen'' in the kernel
348 TAILQ_FOREACH(p, &netcb, chain) {
349 if (!streq(proto, p->ni_proto))
351 if (p->ni_lport != inp->inp_lport ||
352 p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
354 if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
355 p->ni_fport == inp->inp_fport)
359 if ((p = malloc(sizeof(*p))) == NULL) {
360 error("Out of memory");
363 TAILQ_INSERT_HEAD(&netcb, p, chain);
365 p->ni_laddr = inp->inp_laddr;
366 p->ni_lport = inp->inp_lport;
367 p->ni_faddr = inp->inp_faddr;
368 p->ni_fport = inp->inp_fport;
370 p->ni_flags = NIF_LACHG|NIF_FACHG;
377 /* column locations */
379 #define FADDR LADDR+23
380 #define PROTO FADDR+23
381 #define RCVCC PROTO+6
382 #define SNDCC RCVCC+7
383 #define STATE SNDCC+7
389 if (use_kvm && namelist[X_TCB].n_type == 0)
391 wmove(wnd, 0, 0); wclrtobot(wnd);
392 mvwaddstr(wnd, 0, LADDR, "Local Address");
393 mvwaddstr(wnd, 0, FADDR, "Foreign Address");
394 mvwaddstr(wnd, 0, PROTO, "Proto");
395 mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
396 mvwaddstr(wnd, 0, SNDCC, "Send-Q");
397 mvwaddstr(wnd, 0, STATE, "(state)");
403 register struct netinfo *p, *q;
406 * First, delete any connections that have gone
407 * away and adjust the position of connections
408 * below to reflect the deleted line.
410 p = TAILQ_FIRST(&netcb);
412 if (p->ni_line == -1 || p->ni_seen) {
413 p = TAILQ_NEXT(p, chain);
416 wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
417 TAILQ_FOREACH(q, &netcb, chain)
418 if (q != p && q->ni_line > p->ni_line) {
420 /* this shouldn't be necessary */
421 q->ni_flags |= NIF_LACHG|NIF_FACHG;
424 q = TAILQ_NEXT(p, chain);
425 TAILQ_REMOVE(&netcb, p, chain);
430 * Update existing connections and add new ones.
432 TAILQ_FOREACH(p, &netcb, chain) {
433 if (p->ni_line == -1) {
435 * Add a new entry if possible.
437 if (lastrow > YMAX(wnd))
439 p->ni_line = lastrow++;
440 p->ni_flags |= NIF_LACHG|NIF_FACHG;
442 if (p->ni_flags & NIF_LACHG) {
443 wmove(wnd, p->ni_line, LADDR);
444 inetprint(&p->ni_laddr, p->ni_lport, p->ni_proto);
445 p->ni_flags &= ~NIF_LACHG;
447 if (p->ni_flags & NIF_FACHG) {
448 wmove(wnd, p->ni_line, FADDR);
449 inetprint(&p->ni_faddr, p->ni_fport, p->ni_proto);
450 p->ni_flags &= ~NIF_FACHG;
452 mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
453 mvwprintw(wnd, p->ni_line, RCVCC, "%6d", p->ni_rcvcc);
454 mvwprintw(wnd, p->ni_line, SNDCC, "%6d", p->ni_sndcc);
455 if (streq(p->ni_proto, "tcp"))
456 if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
457 mvwprintw(wnd, p->ni_line, STATE, "%d",
460 mvwaddstr(wnd, p->ni_line, STATE,
461 tcpstates[p->ni_state]);
464 if (lastrow < YMAX(wnd)) {
465 wmove(wnd, lastrow, 0); wclrtobot(wnd);
466 wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd); /* XXX */
471 * Pretty print an Internet address (net address + port).
472 * If the nflag was specified, use numbers instead of names.
475 inetprint(in, port, proto)
476 register struct in_addr *in;
480 struct servent *sp = 0;
481 char line[80], *cp, *index();
483 snprintf(line, sizeof(line), "%.*s.", 16, inetname(*in));
484 cp = index(line, '\0');
486 sp = getservbyport(port, proto);
488 snprintf(cp, sizeof(line) - (cp - line), "%.8s",
489 sp ? sp->s_name : "*");
491 snprintf(cp, sizeof(line) - (cp - line), "%d",
492 ntohs((u_short)port));
493 /* pad to full column to clear any garbage */
494 cp = index(line, '\0');
495 while (cp - line < 22)
502 * Construct an Internet address representation.
503 * If the nflag has been supplied, give
504 * numeric value, otherwise try for symbolic name.
511 static char line[50];
515 if (!nflag && in.s_addr != INADDR_ANY) {
516 int net = inet_netof(in);
517 int lna = inet_lnaof(in);
519 if (lna == INADDR_ANY) {
520 np = getnetbyaddr(net, AF_INET);
525 hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
530 if (in.s_addr == INADDR_ANY)
533 snprintf(line, sizeof(line), "%s", cp);
535 in.s_addr = ntohl(in.s_addr);
536 #define C(x) ((x) & 0xff)
537 snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
538 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
544 cmdnetstat(cmd, args)
547 if (prefix(cmd, "all")) {
551 if (prefix(cmd, "numbers") || prefix(cmd, "names")) {
555 new = prefix(cmd, "numbers");
558 TAILQ_FOREACH(p, &netcb, chain) {
559 if (p->ni_line == -1)
561 p->ni_flags |= NIF_LACHG|NIF_FACHG;
566 if (!netcmd(cmd, args))