[BACK]Return to netstat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/netstat.c, Revision 1.22

1.22    ! deraadt     1: /*     $OpenBSD: netstat.c,v 1.21 2002/02/16 21:27:54 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.22    ! deraadt    41: static char rcsid[] = "$OpenBSD: netstat.c,v 1.21 2002/02/16 21:27:54 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>
1.16      pvalchev   76: #include <err.h>
1.1       deraadt    77: #include <nlist.h>
                     78: #include <paths.h>
                     79: #include "systat.h"
                     80: #include "extern.h"
                     81:
1.21      millert    82: static void enter(struct inpcb *, struct socket *, int, char *);
                     83: static const char *inetname(struct in_addr);
                     84: static void inetprint(struct in_addr *, int, char *);
1.12      itojun     85: #ifdef INET6
1.21      millert    86: static const char *inet6name(struct in6_addr *);
                     87: static void inet6print(struct in6_addr *, int, char *);
1.12      itojun     88: #endif
1.1       deraadt    89:
                     90: #define        streq(a,b)      (strcmp(a,b)==0)
1.2       mickey     91: #define        YMAX(w)         ((w)->_maxy-1)
1.1       deraadt    92:
                     93: WINDOW *
1.22    ! deraadt    94: opennetstat(void)
1.1       deraadt    95: {
                     96:        sethostent(1);
                     97:        setnetent(1);
                     98:        return (subwin(stdscr, LINES-5-1, 0, 5, 0));
                     99: }
                    100:
                    101: struct netinfo {
1.11      angelos   102:        struct  netinfo *nif_forw, *nif_prev;
1.12      itojun    103:        int     nif_family;
1.11      angelos   104:        short   nif_line;               /* line on screen */
                    105:        short   nif_seen;               /* 0 when not present in list */
                    106:        short   nif_flags;
1.1       deraadt   107: #define        NIF_LACHG       0x1             /* local address changed */
                    108: #define        NIF_FACHG       0x2             /* foreign address changed */
1.11      angelos   109:        short   nif_state;              /* tcp state */
                    110:        char    *nif_proto;             /* protocol */
                    111:        struct  in_addr nif_laddr;      /* local address */
1.12      itojun    112: #ifdef INET6
                    113:        struct  in6_addr nif_laddr6;    /* local address */
                    114: #endif
1.11      angelos   115:        long    nif_lport;              /* local port */
                    116:        struct  in_addr nif_faddr;      /* foreign address */
1.12      itojun    117: #ifdef INET6
                    118:        struct  in6_addr nif_faddr6;    /* foreign address */
                    119: #endif
1.11      angelos   120:        long    nif_fport;              /* foreign port */
                    121:        long    nif_rcvcc;              /* rcv buffer character count */
                    122:        long    nif_sndcc;              /* snd buffer character count */
1.1       deraadt   123: };
                    124:
                    125: static struct {
1.11      angelos   126:        struct  netinfo *nif_forw, *nif_prev;
1.1       deraadt   127: } netcb;
                    128:
                    129: static int aflag = 0;
                    130: static int nflag = 0;
                    131: static int lastrow = 1;
                    132:
                    133: void
1.22    ! deraadt   134: closenetstat(WINDOW *w)
1.1       deraadt   135: {
1.17      mpech     136:        struct netinfo *p;
1.1       deraadt   137:
                    138:        endhostent();
                    139:        endnetent();
1.11      angelos   140:        p = (struct netinfo *)netcb.nif_forw;
1.1       deraadt   141:        while (p != (struct netinfo *)&netcb) {
1.11      angelos   142:                if (p->nif_line != -1)
1.1       deraadt   143:                        lastrow--;
1.11      angelos   144:                p->nif_line = -1;
                    145:                p = p->nif_forw;
1.1       deraadt   146:        }
1.20      deraadt   147:        if (w != NULL) {
1.1       deraadt   148:                wclear(w);
                    149:                wrefresh(w);
                    150:                delwin(w);
                    151:        }
                    152: }
                    153:
                    154: static struct nlist namelist[] = {
                    155: #define        X_TCBTABLE      0
                    156:        { "_tcbtable" },
                    157: #define        X_UDBTABLE      1
                    158:        { "_udbtable" },
                    159:        { "" },
                    160: };
                    161:
                    162: int
1.22    ! deraadt   163: initnetstat(void)
1.1       deraadt   164: {
1.14      ericj     165:        int ret;
                    166:
                    167:        if ((ret = kvm_nlist(kd, namelist)) == -1)
                    168:                errx(1, "%s", kvm_geterr(kd));
                    169:        else if (ret)
1.1       deraadt   170:                nlisterr(namelist);
                    171:        if (namelist[X_TCBTABLE].n_value == 0) {
                    172:                error("No symbols in namelist");
                    173:                return(0);
                    174:        }
1.11      angelos   175:        netcb.nif_forw = netcb.nif_prev = (struct netinfo *)&netcb;
1.1       deraadt   176:        protos = TCP|UDP;
                    177:        return(1);
                    178: }
                    179:
                    180: void
1.22    ! deraadt   181: fetchnetstat(void)
1.1       deraadt   182: {
                    183:        struct inpcbtable pcbtable;
1.17      mpech     184:        struct inpcb *head, *prev, *next;
                    185:        struct netinfo *p;
1.1       deraadt   186:        struct inpcb inpcb;
                    187:        struct socket sockb;
                    188:        struct tcpcb tcpcb;
                    189:        void *off;
                    190:        int istcp;
                    191:
                    192:        if (namelist[X_TCBTABLE].n_value == 0)
                    193:                return;
1.11      angelos   194:        for (p = netcb.nif_forw; p != (struct netinfo *)&netcb; p = p->nif_forw)
                    195:                p->nif_seen = 0;
1.1       deraadt   196:        if (protos&TCP) {
1.20      deraadt   197:                off = NPTR(X_TCBTABLE);
1.1       deraadt   198:                istcp = 1;
1.20      deraadt   199:        } else if (protos&UDP) {
                    200:                off = NPTR(X_UDBTABLE);
1.1       deraadt   201:                istcp = 0;
1.20      deraadt   202:        } else {
1.1       deraadt   203:                error("No protocols to display");
                    204:                return;
                    205:        }
                    206: again:
                    207:        KREAD(off, &pcbtable, sizeof (struct inpcbtable));
                    208:        prev = head = (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue;
                    209:        next = pcbtable.inpt_queue.cqh_first;
                    210:        while (next != head) {
                    211:                KREAD(next, &inpcb, sizeof (inpcb));
                    212:                if (inpcb.inp_queue.cqe_prev != prev) {
1.22    ! deraadt   213:                        printf("prev = %p, head = %p, next = %p, inpcb...prev = %p\n",
        !           214:                            prev, head, next, inpcb.inp_queue.cqe_prev);
1.11      angelos   215:                        p = netcb.nif_forw;
                    216:                        for (; p != (struct netinfo *)&netcb; p = p->nif_forw)
                    217:                                p->nif_seen = 1;
1.1       deraadt   218:                        error("Kernel state in transition");
                    219:                        return;
                    220:                }
                    221:                prev = next;
                    222:                next = inpcb.inp_queue.cqe_next;
                    223:
1.12      itojun    224: #ifndef INET6
                    225:                if (inpcb.inp_flags & INP_IPV6)
1.1       deraadt   226:                        continue;
1.12      itojun    227: #endif
                    228:
                    229:                if (!aflag) {
1.20      deraadt   230:                        if (!(inpcb.inp_flags & INP_IPV6) &&
                    231:                            inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
1.12      itojun    232:                                continue;
                    233: #ifdef INET6
1.20      deraadt   234:                        if ((inpcb.inp_flags & INP_IPV6) &&
                    235:                            IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6))
1.12      itojun    236:                                continue;
                    237: #endif
                    238:                }
1.1       deraadt   239:                if (nhosts && !checkhost(&inpcb))
                    240:                        continue;
                    241:                if (nports && !checkport(&inpcb))
                    242:                        continue;
                    243:                KREAD(inpcb.inp_socket, &sockb, sizeof (sockb));
                    244:                if (istcp) {
                    245:                        KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
                    246:                        enter(&inpcb, &sockb, tcpcb.t_state, "tcp");
                    247:                } else
                    248:                        enter(&inpcb, &sockb, 0, "udp");
                    249:        }
                    250:        if (istcp && (protos&UDP)) {
                    251:                istcp = 0;
                    252:                off = NPTR(X_UDBTABLE);
                    253:                goto again;
                    254:        }
                    255: }
                    256:
                    257: static void
1.22    ! deraadt   258: enter(struct inpcb *inp, struct socket *so, int state, char *proto)
1.1       deraadt   259: {
1.17      mpech     260:        struct netinfo *p;
1.1       deraadt   261:
                    262:        /*
                    263:         * Only take exact matches, any sockets with
                    264:         * previously unbound addresses will be deleted
                    265:         * below in the display routine because they
                    266:         * will appear as ``not seen'' in the kernel
                    267:         * data structures.
                    268:         */
1.12      itojun    269:        for (p = netcb.nif_forw; p != (struct netinfo *)&netcb; p = p->nif_forw) {
                    270: #ifdef INET6
                    271:                if (p->nif_family == AF_INET && (inp->inp_flags & INP_IPV6))
                    272:                        continue;
                    273:                if (p->nif_family == AF_INET6 && !(inp->inp_flags & INP_IPV6))
                    274:                        continue;
                    275: #endif
1.11      angelos   276:                if (!streq(proto, p->nif_proto))
1.1       deraadt   277:                        continue;
1.12      itojun    278:                if (p->nif_family == AF_INET) {
                    279:                        if (p->nif_lport != inp->inp_lport ||
                    280:                            p->nif_laddr.s_addr != inp->inp_laddr.s_addr)
                    281:                                continue;
                    282:                        if (p->nif_faddr.s_addr == inp->inp_faddr.s_addr &&
                    283:                            p->nif_fport == inp->inp_fport)
                    284:                                break;
                    285:
                    286:                }
                    287: #ifdef INET6
                    288:                else if (p->nif_family == AF_INET6) {
                    289:                        if (p->nif_lport != inp->inp_lport ||
                    290:                            !IN6_ARE_ADDR_EQUAL(&p->nif_laddr6, &inp->inp_laddr6))
                    291:                                continue;
1.13      itojun    292:                        if (IN6_ARE_ADDR_EQUAL(&p->nif_faddr6, &inp->inp_faddr6) &&
1.12      itojun    293:                            p->nif_fport == inp->inp_fport)
                    294:                                break;
                    295:                }
                    296: #endif
                    297:                else
1.1       deraadt   298:                        continue;
                    299:        }
                    300:        if (p == (struct netinfo *)&netcb) {
                    301:                if ((p = malloc(sizeof(*p))) == NULL) {
                    302:                        error("Out of memory");
                    303:                        return;
                    304:                }
1.11      angelos   305:                p->nif_prev = (struct netinfo *)&netcb;
                    306:                p->nif_forw = netcb.nif_forw;
                    307:                netcb.nif_forw->nif_prev = p;
                    308:                netcb.nif_forw = p;
                    309:                p->nif_line = -1;
                    310:                p->nif_lport = inp->inp_lport;
                    311:                p->nif_fport = inp->inp_fport;
                    312:                p->nif_proto = proto;
                    313:                p->nif_flags = NIF_LACHG|NIF_FACHG;
1.12      itojun    314: #ifdef INET6
                    315:                if (inp->inp_flags & INP_IPV6) {
                    316:                        p->nif_laddr6 = inp->inp_laddr6;
                    317:                        p->nif_faddr6 = inp->inp_faddr6;
                    318:                        p->nif_family = AF_INET6;
1.20      deraadt   319:                } else
1.12      itojun    320: #endif
                    321:                {
                    322:                        p->nif_laddr = inp->inp_laddr;
                    323:                        p->nif_faddr = inp->inp_faddr;
                    324:                        p->nif_family = AF_INET;
                    325:                }
1.11      angelos   326:        }
                    327:        p->nif_rcvcc = so->so_rcv.sb_cc;
                    328:        p->nif_sndcc = so->so_snd.sb_cc;
                    329:        p->nif_state = state;
                    330:        p->nif_seen = 1;
1.1       deraadt   331: }
                    332:
                    333: /* column locations */
                    334: #define        LADDR   0
                    335: #define        FADDR   LADDR+23
                    336: #define        PROTO   FADDR+23
                    337: #define        RCVCC   PROTO+6
                    338: #define        SNDCC   RCVCC+7
                    339: #define        STATE   SNDCC+7
                    340:
                    341:
                    342: void
1.22    ! deraadt   343: labelnetstat(void)
1.1       deraadt   344: {
                    345:        if (namelist[X_TCBTABLE].n_type == 0)
                    346:                return;
                    347:        wmove(wnd, 0, 0); wclrtobot(wnd);
                    348:        mvwaddstr(wnd, 0, LADDR, "Local Address");
                    349:        mvwaddstr(wnd, 0, FADDR, "Foreign Address");
                    350:        mvwaddstr(wnd, 0, PROTO, "Proto");
                    351:        mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
                    352:        mvwaddstr(wnd, 0, SNDCC, "Send-Q");
1.20      deraadt   353:        mvwaddstr(wnd, 0, STATE, "(state)");
1.1       deraadt   354: }
                    355:
                    356: void
1.22    ! deraadt   357: shownetstat(void)
1.1       deraadt   358: {
1.17      mpech     359:        struct netinfo *p, *q;
1.1       deraadt   360:
                    361:        /*
                    362:         * First, delete any connections that have gone
                    363:         * away and adjust the position of connections
                    364:         * below to reflect the deleted line.
                    365:         */
1.11      angelos   366:        p = netcb.nif_forw;
1.1       deraadt   367:        while (p != (struct netinfo *)&netcb) {
1.11      angelos   368:                if (p->nif_line == -1 || p->nif_seen) {
                    369:                        p = p->nif_forw;
1.1       deraadt   370:                        continue;
                    371:                }
1.11      angelos   372:                wmove(wnd, p->nif_line, 0); wdeleteln(wnd);
                    373:                q = netcb.nif_forw;
                    374:                for (; q != (struct netinfo *)&netcb; q = q->nif_forw)
                    375:                        if (q != p && q->nif_line > p->nif_line) {
                    376:                                q->nif_line--;
1.1       deraadt   377:                                /* this shouldn't be necessary */
1.11      angelos   378:                                q->nif_flags |= NIF_LACHG|NIF_FACHG;
1.1       deraadt   379:                        }
                    380:                lastrow--;
1.11      angelos   381:                q = p->nif_forw;
                    382:                p->nif_prev->nif_forw = p->nif_forw;
                    383:                p->nif_forw->nif_prev = p->nif_prev;
1.1       deraadt   384:                free(p);
                    385:                p = q;
                    386:        }
                    387:        /*
                    388:         * Update existing connections and add new ones.
                    389:         */
1.12      itojun    390:        for (p = netcb.nif_forw; p != (struct netinfo *)&netcb; p = p->nif_forw) {
1.11      angelos   391:                if (p->nif_line == -1) {
1.1       deraadt   392:                        /*
                    393:                         * Add a new entry if possible.
                    394:                         */
                    395:                        if (lastrow > YMAX(wnd))
                    396:                                continue;
1.11      angelos   397:                        p->nif_line = lastrow++;
                    398:                        p->nif_flags |= NIF_LACHG|NIF_FACHG;
1.1       deraadt   399:                }
1.11      angelos   400:                if (p->nif_flags & NIF_LACHG) {
                    401:                        wmove(wnd, p->nif_line, LADDR);
1.12      itojun    402:                        switch (p->nif_family) {
                    403:                        case AF_INET:
                    404:                                inetprint(&p->nif_laddr, p->nif_lport,
                    405:                                        p->nif_proto);
                    406:                                break;
                    407: #ifdef INET6
                    408:                        case AF_INET6:
                    409:                                inet6print(&p->nif_laddr6, p->nif_lport,
                    410:                                        p->nif_proto);
                    411:                                break;
                    412: #endif
                    413:                        }
1.11      angelos   414:                        p->nif_flags &= ~NIF_LACHG;
1.1       deraadt   415:                }
1.11      angelos   416:                if (p->nif_flags & NIF_FACHG) {
                    417:                        wmove(wnd, p->nif_line, FADDR);
1.12      itojun    418:                        switch (p->nif_family) {
                    419:                        case AF_INET:
                    420:                                inetprint(&p->nif_faddr, p->nif_fport,
                    421:                                        p->nif_proto);
                    422:                                break;
                    423: #ifdef INET6
                    424:                        case AF_INET6:
                    425:                                inet6print(&p->nif_faddr6, p->nif_fport,
                    426:                                        p->nif_proto);
                    427:                                break;
                    428: #endif
                    429:                        }
1.11      angelos   430:                        p->nif_flags &= ~NIF_FACHG;
1.1       deraadt   431:                }
1.11      angelos   432:                mvwaddstr(wnd, p->nif_line, PROTO, p->nif_proto);
1.12      itojun    433: #ifdef INET6
                    434:                if (p->nif_family == AF_INET6)
                    435:                        waddstr(wnd, "6");
                    436: #endif
1.11      angelos   437:                mvwprintw(wnd, p->nif_line, RCVCC, "%6d", p->nif_rcvcc);
                    438:                mvwprintw(wnd, p->nif_line, SNDCC, "%6d", p->nif_sndcc);
1.20      deraadt   439:                if (streq(p->nif_proto, "tcp")) {
1.11      angelos   440:                        if (p->nif_state < 0 || p->nif_state >= TCP_NSTATES)
                    441:                                mvwprintw(wnd, p->nif_line, STATE, "%d",
                    442:                                    p->nif_state);
1.1       deraadt   443:                        else
1.11      angelos   444:                                mvwaddstr(wnd, p->nif_line, STATE,
                    445:                                    tcpstates[p->nif_state]);
1.20      deraadt   446:                }
1.1       deraadt   447:                wclrtoeol(wnd);
                    448:        }
                    449:        if (lastrow < YMAX(wnd)) {
                    450:                wmove(wnd, lastrow, 0); wclrtobot(wnd);
                    451:                wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd);       /* XXX */
                    452:        }
                    453: }
                    454:
                    455: /*
                    456:  * Pretty print an Internet address (net address + port).
                    457:  * If the nflag was specified, use numbers instead of names.
                    458:  */
                    459: static void
1.22    ! deraadt   460: inetprint(struct in_addr *in, int port, char *proto)
1.1       deraadt   461: {
                    462:        struct servent *sp = 0;
1.6       millert   463:        char line[80], *cp;
1.1       deraadt   464:
1.8       deraadt   465:        snprintf(line, sizeof line, "%.*s.", 16, inetname(*in));
1.6       millert   466:        cp = strchr(line, '\0');
1.1       deraadt   467:        if (!nflag && port)
                    468:                sp = getservbyport(port, proto);
                    469:        if (sp || port == 0)
1.8       deraadt   470:                snprintf(cp, sizeof line - strlen(cp), "%.8s",
                    471:                    sp ? sp->s_name : "*");
1.1       deraadt   472:        else
1.8       deraadt   473:                snprintf(cp, sizeof line - strlen(cp), "%d",
                    474:                    ntohs((u_short)port));
1.1       deraadt   475:        /* pad to full column to clear any garbage */
1.6       millert   476:        cp = strchr(line, '\0');
1.10      deraadt   477:        while (cp - line < 22 && cp - line < sizeof line-1)
1.1       deraadt   478:                *cp++ = ' ';
                    479:        *cp = '\0';
                    480:        waddstr(wnd, line);
                    481: }
                    482:
1.12      itojun    483: #ifdef INET6
                    484: static void
1.22    ! deraadt   485: inet6print(struct in6_addr *in6, int port, char *proto)
1.12      itojun    486: {
                    487:        struct servent *sp = 0;
                    488:        char line[80], *cp;
                    489:
                    490:        snprintf(line, sizeof line, "%.*s.", 16, inet6name(in6));
                    491:        cp = strchr(line, '\0');
                    492:        if (!nflag && port)
                    493:                sp = getservbyport(port, proto);
                    494:        if (sp || port == 0)
                    495:                snprintf(cp, sizeof line - strlen(cp), "%.8s",
                    496:                    sp ? sp->s_name : "*");
                    497:        else
                    498:                snprintf(cp, sizeof line - strlen(cp), "%d",
                    499:                    ntohs((u_short)port));
                    500:        /* pad to full column to clear any garbage */
                    501:        cp = strchr(line, '\0');
                    502:        while (cp - line < 22 && cp - line < sizeof line-1)
                    503:                *cp++ = ' ';
                    504:        *cp = '\0';
                    505:        waddstr(wnd, line);
                    506: }
                    507: #endif
                    508:
1.1       deraadt   509: /*
                    510:  * Construct an Internet address representation.
1.20      deraadt   511:  * If the nflag has been supplied, give
1.1       deraadt   512:  * numeric value, otherwise try for symbolic name.
                    513:  */
1.12      itojun    514: static const char *
1.22    ! deraadt   515: inetname(struct in_addr in)
1.1       deraadt   516: {
                    517:        char *cp = 0;
                    518:        static char line[50];
                    519:        struct hostent *hp;
                    520:        struct netent *np;
                    521:
                    522:        if (!nflag && in.s_addr != INADDR_ANY) {
                    523:                int net = inet_netof(in);
                    524:                int lna = inet_lnaof(in);
                    525:
                    526:                if (lna == INADDR_ANY) {
                    527:                        np = getnetbyaddr(net, AF_INET);
                    528:                        if (np)
                    529:                                cp = np->n_name;
                    530:                }
                    531:                if (cp == 0) {
                    532:                        hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
                    533:                        if (hp)
                    534:                                cp = hp->h_name;
                    535:                }
                    536:        }
1.9       deraadt   537:        if (in.s_addr == INADDR_ANY) {
1.15      lebel     538:                strlcpy(line, "*", sizeof line);
1.9       deraadt   539:        } else if (cp) {
1.15      lebel     540:                strlcpy(line, cp, sizeof line);
1.9       deraadt   541:        } else {
1.1       deraadt   542:                in.s_addr = ntohl(in.s_addr);
                    543: #define C(x)   ((x) & 0xff)
1.8       deraadt   544:                snprintf(line, sizeof line, "%u.%u.%u.%u", C(in.s_addr >> 24),
1.20      deraadt   545:                    C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
1.1       deraadt   546:        }
                    547:        return (line);
                    548: }
1.12      itojun    549:
                    550: #ifdef INET6
                    551: static const char *
1.22    ! deraadt   552: inet6name(struct in6_addr *in6)
1.12      itojun    553: {
                    554:        static char line[NI_MAXHOST];
                    555:        struct sockaddr_in6 sin6;
                    556:        int flags;
                    557:
                    558:        if (nflag)
                    559:                flags = NI_NUMERICHOST;
                    560:        else
                    561:                flags = 0;
                    562:        if (IN6_IS_ADDR_UNSPECIFIED(in6))
                    563:                return "*";
                    564:        memset(&sin6, 0, sizeof(sin6));
                    565:        sin6.sin6_family = AF_INET6;
                    566:        sin6.sin6_len = sizeof(struct sockaddr_in6);
                    567:        sin6.sin6_addr = *in6;
                    568:        if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1.20      deraadt   569:            line, sizeof(line), NULL, 0, flags) == 0)
1.12      itojun    570:                return line;
                    571:        return "?";
                    572: }
                    573: #endif
1.1       deraadt   574:
                    575: int
1.22    ! deraadt   576: cmdnetstat(char *cmd, char *args)
1.1       deraadt   577: {
1.17      mpech     578:        struct netinfo *p;
1.1       deraadt   579:
                    580:        if (prefix(cmd, "all")) {
                    581:                aflag = !aflag;
                    582:                goto fixup;
                    583:        }
                    584:        if  (prefix(cmd, "numbers") || prefix(cmd, "names")) {
                    585:                int new;
                    586:
                    587:                new = prefix(cmd, "numbers");
                    588:                if (new == nflag)
                    589:                        return (1);
1.11      angelos   590:                p = netcb.nif_forw;
                    591:                for (; p != (struct netinfo *)&netcb; p = p->nif_forw) {
                    592:                        if (p->nif_line == -1)
1.1       deraadt   593:                                continue;
1.11      angelos   594:                        p->nif_flags |= NIF_LACHG|NIF_FACHG;
1.1       deraadt   595:                }
                    596:                nflag = new;
1.4       deraadt   597:                wclear(wnd);
                    598:                labelnetstat();
1.1       deraadt   599:                goto redisplay;
                    600:        }
                    601:        if (!netcmd(cmd, args))
                    602:                return (0);
                    603: fixup:
                    604:        fetchnetstat();
                    605: redisplay:
                    606:        shownetstat();
                    607:        refresh();
                    608:        return (1);
                    609: }