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

Annotation of src/usr.bin/netstat/inet.c, Revision 1.10

1.10    ! angelos     1: /*     $OpenBSD: inet.c,v 1.9 1997/02/21 02:46:49 deraadt Exp $        */
1.1       deraadt     2: /*     $NetBSD: inet.c,v 1.14 1995/10/03 21:42:37 thorpej Exp $        */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1988, 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[] = "from: @(#)inet.c       8.4 (Berkeley) 4/20/94";
                     40: #else
1.10    ! angelos    41: static char *rcsid = "$OpenBSD: inet.c,v 1.9 1997/02/21 02:46:49 deraadt Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46: #include <sys/queue.h>
                     47: #include <sys/socket.h>
                     48: #include <sys/socketvar.h>
                     49: #include <sys/mbuf.h>
                     50: #include <sys/protosw.h>
                     51:
                     52: #include <net/route.h>
                     53: #include <netinet/in.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/igmp_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.10    ! angelos    71: #include <netinet/ip_ah.h>
        !            72: #include <netinet/ip_esp.h>
        !            73: #include <netinet/ip_ip4.h>
1.1       deraadt    74:
                     75: #include <arpa/inet.h>
                     76: #include <netdb.h>
                     77: #include <stdio.h>
                     78: #include <string.h>
                     79: #include <unistd.h>
                     80: #include "netstat.h"
                     81:
1.5       deraadt    82: #include <rpc/rpc.h>
                     83: #include <rpc/pmap_prot.h>
                     84: #include <rpc/pmap_clnt.h>
                     85:
1.1       deraadt    86: struct inpcb inpcb;
                     87: struct tcpcb tcpcb;
                     88: struct socket sockb;
                     89:
                     90: char   *inetname __P((struct in_addr *));
1.5       deraadt    91: void   inetprint __P((struct in_addr *, int, char *, int));
1.1       deraadt    92:
                     93: /*
                     94:  * Print a summary of connections related to an Internet
                     95:  * protocol.  For TCP, also give state of connection.
                     96:  * Listening processes (aflag) are suppressed unless the
                     97:  * -a (all) flag is specified.
                     98:  */
                     99: void
                    100: protopr(off, name)
                    101:        u_long off;
                    102:        char *name;
                    103: {
                    104:        struct inpcbtable table;
                    105:        register struct inpcb *head, *next, *prev;
                    106:        struct inpcb inpcb;
                    107:        int istcp;
                    108:        static int first = 1;
                    109:
                    110:        if (off == 0)
                    111:                return;
                    112:        istcp = strcmp(name, "tcp") == 0;
                    113:        kread(off, (char *)&table, sizeof table);
                    114:        prev = head =
                    115:            (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
                    116:        next = table.inpt_queue.cqh_first;
                    117:
                    118:        while (next != head) {
                    119:                kread((u_long)next, (char *)&inpcb, sizeof inpcb);
                    120:                if (inpcb.inp_queue.cqe_prev != prev) {
                    121:                        printf("???\n");
                    122:                        break;
                    123:                }
                    124:                prev = next;
                    125:                next = inpcb.inp_queue.cqe_next;
                    126:
                    127:                if (!aflag &&
                    128:                    inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
                    129:                        continue;
                    130:                kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
                    131:                if (istcp) {
                    132:                        kread((u_long)inpcb.inp_ppcb,
                    133:                            (char *)&tcpcb, sizeof (tcpcb));
                    134:                }
                    135:                if (first) {
                    136:                        printf("Active Internet connections");
                    137:                        if (aflag)
                    138:                                printf(" (including servers)");
                    139:                        putchar('\n');
                    140:                        if (Aflag)
                    141:                                printf("%-8.8s ", "PCB");
                    142:                        printf(Aflag ?
                    143:                                "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
                    144:                                "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
                    145:                                "Proto", "Recv-Q", "Send-Q",
                    146:                                "Local Address", "Foreign Address", "(state)");
                    147:                        first = 0;
                    148:                }
                    149:                if (Aflag)
                    150:                        if (istcp)
                    151:                                printf("%8x ", inpcb.inp_ppcb);
                    152:                        else
                    153:                                printf("%8x ", prev);
                    154:                printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
                    155:                        sockb.so_snd.sb_cc);
1.5       deraadt   156:                inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, name, 1);
                    157:                inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, name, 0);
1.1       deraadt   158:                if (istcp) {
                    159:                        if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
                    160:                                printf(" %d", tcpcb.t_state);
                    161:                        else
                    162:                                printf(" %s", tcpstates[tcpcb.t_state]);
                    163:                }
                    164:                putchar('\n');
                    165:        }
                    166: }
                    167:
                    168: /*
                    169:  * Dump TCP statistics structure.
                    170:  */
                    171: void
                    172: tcp_stats(off, name)
                    173:        u_long off;
                    174:        char *name;
                    175: {
                    176:        struct tcpstat tcpstat;
                    177:
                    178:        if (off == 0)
                    179:                return;
                    180:        printf ("%s:\n", name);
                    181:        kread(off, (char *)&tcpstat, sizeof (tcpstat));
                    182:
                    183: #define        p(f, m) if (tcpstat.f || sflag <= 1) \
                    184:     printf(m, tcpstat.f, plural(tcpstat.f))
                    185: #define        p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
                    186:     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
                    187: #define        p3(f, m) if (tcpstat.f || sflag <= 1) \
                    188:     printf(m, tcpstat.f, plurales(tcpstat.f))
                    189:
                    190:        p(tcps_sndtotal, "\t%d packet%s sent\n");
                    191:        p2(tcps_sndpack,tcps_sndbyte,
                    192:                "\t\t%d data packet%s (%d byte%s)\n");
                    193:        p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
                    194:                "\t\t%d data packet%s (%d byte%s) retransmitted\n");
                    195:        p2(tcps_sndacks, tcps_delack,
                    196:                "\t\t%d ack-only packet%s (%d delayed)\n");
                    197:        p(tcps_sndurg, "\t\t%d URG only packet%s\n");
                    198:        p(tcps_sndprobe, "\t\t%d window probe packet%s\n");
                    199:        p(tcps_sndwinup, "\t\t%d window update packet%s\n");
                    200:        p(tcps_sndctrl, "\t\t%d control packet%s\n");
                    201:        p(tcps_rcvtotal, "\t%d packet%s received\n");
                    202:        p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n");
                    203:        p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n");
                    204:        p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n");
                    205:        p2(tcps_rcvpack, tcps_rcvbyte,
                    206:                "\t\t%d packet%s (%d byte%s) received in-sequence\n");
                    207:        p2(tcps_rcvduppack, tcps_rcvdupbyte,
                    208:                "\t\t%d completely duplicate packet%s (%d byte%s)\n");
                    209:        p(tcps_pawsdrop, "\t\t%d old duplicate packet%s\n");
                    210:        p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
                    211:                "\t\t%d packet%s with some dup. data (%d byte%s duped)\n");
                    212:        p2(tcps_rcvoopack, tcps_rcvoobyte,
                    213:                "\t\t%d out-of-order packet%s (%d byte%s)\n");
                    214:        p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
                    215:                "\t\t%d packet%s (%d byte%s) of data after window\n");
                    216:        p(tcps_rcvwinprobe, "\t\t%d window probe%s\n");
                    217:        p(tcps_rcvwinupd, "\t\t%d window update packet%s\n");
                    218:        p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n");
                    219:        p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n");
                    220:        p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n");
                    221:        p(tcps_rcvshort, "\t\t%d discarded because packet too short\n");
                    222:        p(tcps_connattempt, "\t%d connection request%s\n");
                    223:        p(tcps_accepts, "\t%d connection accept%s\n");
                    224:        p(tcps_connects, "\t%d connection%s established (including accepts)\n");
                    225:        p2(tcps_closed, tcps_drops,
                    226:                "\t%d connection%s closed (including %d drop%s)\n");
                    227:        p(tcps_conndrops, "\t%d embryonic connection%s dropped\n");
                    228:        p2(tcps_rttupdated, tcps_segstimed,
                    229:                "\t%d segment%s updated rtt (of %d attempt%s)\n");
                    230:        p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n");
                    231:        p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n");
                    232:        p(tcps_persisttimeo, "\t%d persist timeout%s\n");
                    233:        p(tcps_keeptimeo, "\t%d keepalive timeout%s\n");
                    234:        p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n");
                    235:        p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n");
                    236:        p(tcps_predack, "\t%d correct ACK header prediction%s\n");
                    237:        p(tcps_preddat, "\t%d correct data packet header prediction%s\n");
1.2       mickey    238:        p3(tcps_pcbhashmiss, "\t%d PCB cache miss%s\n");
1.1       deraadt   239: #undef p
                    240: #undef p2
                    241: #undef p3
                    242: }
                    243:
                    244: /*
                    245:  * Dump UDP statistics structure.
                    246:  */
                    247: void
                    248: udp_stats(off, name)
                    249:        u_long off;
                    250:        char *name;
                    251: {
                    252:        struct udpstat udpstat;
                    253:        u_long delivered;
                    254:
                    255:        if (off == 0)
                    256:                return;
                    257:        kread(off, (char *)&udpstat, sizeof (udpstat));
                    258:        printf("%s:\n", name);
                    259: #define        p(f, m) if (udpstat.f || sflag <= 1) \
                    260:     printf(m, udpstat.f, plural(udpstat.f))
                    261:        p(udps_ipackets, "\t%u datagram%s received\n");
                    262:        p(udps_hdrops, "\t%u with incomplete header\n");
                    263:        p(udps_badlen, "\t%u with bad data length field\n");
                    264:        p(udps_badsum, "\t%u with bad checksum\n");
                    265:        p(udps_noport, "\t%u dropped due to no socket\n");
                    266:        p(udps_noportbcast, "\t%u broadcast/multicast datagram%s dropped due to no socket\n");
                    267:        p(udps_fullsock, "\t%u dropped due to full socket buffers\n");
                    268:        delivered = udpstat.udps_ipackets -
                    269:                    udpstat.udps_hdrops -
                    270:                    udpstat.udps_badlen -
                    271:                    udpstat.udps_badsum -
                    272:                    udpstat.udps_noport -
                    273:                    udpstat.udps_noportbcast -
                    274:                    udpstat.udps_fullsock;
                    275:        if (delivered || sflag <= 1)
                    276:                printf("\t%u delivered\n", delivered);
                    277:        p(udps_opackets, "\t%u datagram%s output\n");
                    278: #undef p
                    279: }
                    280:
                    281: /*
                    282:  * Dump IP statistics structure.
                    283:  */
                    284: void
                    285: ip_stats(off, name)
                    286:        u_long off;
                    287:        char *name;
                    288: {
                    289:        struct ipstat ipstat;
                    290:
                    291:        if (off == 0)
                    292:                return;
                    293:        kread(off, (char *)&ipstat, sizeof (ipstat));
                    294:        printf("%s:\n", name);
                    295:
                    296: #define        p(f, m) if (ipstat.f || sflag <= 1) \
                    297:     printf(m, ipstat.f, plural(ipstat.f))
                    298:
                    299:        p(ips_total, "\t%u total packet%s received\n");
                    300:        p(ips_badsum, "\t%u bad header checksum%s\n");
                    301:        p(ips_toosmall, "\t%u with size smaller than minimum\n");
                    302:        p(ips_tooshort, "\t%u with data size < data length\n");
                    303:        p(ips_badhlen, "\t%u with header length < data size\n");
                    304:        p(ips_badlen, "\t%u with data length < header length\n");
                    305:        p(ips_badoptions, "\t%u with bad options\n");
                    306:        p(ips_badvers, "\t%u with incorrect version number\n");
                    307:        p(ips_fragments, "\t%u fragment%s received\n");
                    308:        p(ips_fragdropped, "\t%u fragment%s dropped (dup or out of space)\n");
                    309:        p(ips_badfrags, "\t%u malformed fragment%s dropped\n");
                    310:        p(ips_fragtimeout, "\t%u fragment%s dropped after timeout\n");
                    311:        p(ips_reassembled, "\t%u packet%s reassembled ok\n");
                    312:        p(ips_delivered, "\t%u packet%s for this host\n");
                    313:        p(ips_noproto, "\t%u packet%s for unknown/unsupported protocol\n");
                    314:        p(ips_forward, "\t%u packet%s forwarded\n");
                    315:        p(ips_cantforward, "\t%u packet%s not forwardable\n");
                    316:        p(ips_redirectsent, "\t%u redirect%s sent\n");
                    317:        p(ips_localout, "\t%u packet%s sent from this host\n");
                    318:        p(ips_rawout, "\t%u packet%s sent with fabricated ip header\n");
                    319:        p(ips_odropped, "\t%u output packet%s dropped due to no bufs, etc.\n");
                    320:        p(ips_noroute, "\t%u output packet%s discarded due to no route\n");
                    321:        p(ips_fragmented, "\t%u output datagram%s fragmented\n");
                    322:        p(ips_ofragments, "\t%u fragment%s created\n");
                    323:        p(ips_cantfrag, "\t%u datagram%s that can't be fragmented\n");
                    324: #undef p
                    325: }
                    326:
                    327: static char *icmpnames[] = {
                    328:        "echo reply",
                    329:        "#1",
                    330:        "#2",
                    331:        "destination unreachable",
                    332:        "source quench",
                    333:        "routing redirect",
                    334:        "#6",
                    335:        "#7",
                    336:        "echo",
1.9       deraadt   337:        "router advertisement",
                    338:        "router solicitation",
1.1       deraadt   339:        "time exceeded",
                    340:        "parameter problem",
                    341:        "time stamp",
                    342:        "time stamp reply",
                    343:        "information request",
                    344:        "information request reply",
                    345:        "address mask request",
                    346:        "address mask reply",
                    347: };
                    348:
                    349: /*
                    350:  * Dump ICMP statistics.
                    351:  */
                    352: void
                    353: icmp_stats(off, name)
                    354:        u_long off;
                    355:        char *name;
                    356: {
                    357:        struct icmpstat icmpstat;
                    358:        register int i, first;
                    359:
                    360:        if (off == 0)
                    361:                return;
                    362:        kread(off, (char *)&icmpstat, sizeof (icmpstat));
                    363:        printf("%s:\n", name);
                    364:
                    365: #define        p(f, m) if (icmpstat.f || sflag <= 1) \
                    366:     printf(m, icmpstat.f, plural(icmpstat.f))
                    367:
                    368:        p(icps_error, "\t%u call%s to icmp_error\n");
                    369:        p(icps_oldicmp,
                    370:            "\t%u error%s not generated 'cuz old message was icmp\n");
                    371:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    372:                if (icmpstat.icps_outhist[i] != 0) {
                    373:                        if (first) {
                    374:                                printf("\tOutput histogram:\n");
                    375:                                first = 0;
                    376:                        }
                    377:                        printf("\t\t%s: %u\n", icmpnames[i],
                    378:                                icmpstat.icps_outhist[i]);
                    379:                }
                    380:        p(icps_badcode, "\t%u message%s with bad code fields\n");
                    381:        p(icps_tooshort, "\t%u message%s < minimum length\n");
                    382:        p(icps_checksum, "\t%u bad checksum%s\n");
                    383:        p(icps_badlen, "\t%u message%s with bad length\n");
                    384:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
                    385:                if (icmpstat.icps_inhist[i] != 0) {
                    386:                        if (first) {
                    387:                                printf("\tInput histogram:\n");
                    388:                                first = 0;
                    389:                        }
                    390:                        printf("\t\t%s: %u\n", icmpnames[i],
                    391:                                icmpstat.icps_inhist[i]);
                    392:                }
                    393:        p(icps_reflect, "\t%u message response%s generated\n");
                    394: #undef p
                    395: }
                    396:
                    397: /*
                    398:  * Dump IGMP statistics structure.
                    399:  */
                    400: void
                    401: igmp_stats(off, name)
                    402:        u_long off;
                    403:        char *name;
                    404: {
                    405:        struct igmpstat igmpstat;
                    406:
                    407:        if (off == 0)
                    408:                return;
                    409:        kread(off, (char *)&igmpstat, sizeof (igmpstat));
                    410:        printf("%s:\n", name);
                    411:
                    412: #define        p(f, m) if (igmpstat.f || sflag <= 1) \
                    413:     printf(m, igmpstat.f, plural(igmpstat.f))
                    414: #define        py(f, m) if (igmpstat.f || sflag <= 1) \
                    415:     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
                    416:        p(igps_rcv_total, "\t%u message%s received\n");
                    417:         p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
                    418:         p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
                    419:         py(igps_rcv_queries, "\t%u membership quer%s received\n");
                    420:         py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
                    421:         p(igps_rcv_reports, "\t%u membership report%s received\n");
                    422:         p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
                    423:         p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
                    424:         p(igps_snd_reports, "\t%u membership report%s sent\n");
                    425: #undef p
                    426: #undef py
                    427: }
                    428:
1.5       deraadt   429: struct rpcnams {
                    430:        struct rpcnams *next;
                    431:        int     port;
                    432:        char    *rpcname;
                    433: };
                    434:
                    435: char *
                    436: getrpcportnam(port)
                    437:        int port;
                    438: {
                    439:        struct sockaddr_in server_addr;
                    440:        register struct hostent *hp;
                    441:        static struct pmaplist *head;
                    442:        int socket = RPC_ANYSOCK;
                    443:        struct timeval minutetimeout;
                    444:        register CLIENT *client;
                    445:        struct rpcent *rpc;
                    446:        static int first;
                    447:        static struct rpcnams *rpcn;
                    448:        struct rpcnams *n;
                    449:        char num[10];
                    450:
                    451:        if (first == 0) {
                    452:                first = 1;
1.7       deraadt   453:                memset((char *)&server_addr, 0, sizeof server_addr);
1.5       deraadt   454:                server_addr.sin_family = AF_INET;
                    455:                if ((hp = gethostbyname("localhost")) != NULL)
1.7       deraadt   456:                        memmove((caddr_t)&server_addr.sin_addr, hp->h_addr,
1.5       deraadt   457:                            hp->h_length);
                    458:                else
                    459:                        (void) inet_aton("0.0.0.0", &server_addr.sin_addr);
                    460:
                    461:                minutetimeout.tv_sec = 60;
                    462:                minutetimeout.tv_usec = 0;
                    463:                server_addr.sin_port = htons(PMAPPORT);
                    464:                if ((client = clnttcp_create(&server_addr, PMAPPROG,
1.7       deraadt   465:                    PMAPVERS, &socket, 50, 500)) == NULL)
                    466:                        return (NULL);
1.5       deraadt   467:                if (clnt_call(client, PMAPPROC_DUMP, xdr_void, NULL,
1.8       deraadt   468:                    xdr_pmaplist, &head, minutetimeout) != RPC_SUCCESS) {
1.7       deraadt   469:                        clnt_destroy(client);
                    470:                        return (NULL);
1.5       deraadt   471:                }
                    472:                for (; head != NULL; head = head->pml_next) {
                    473:                        n = (struct rpcnams *)malloc(sizeof(struct rpcnams));
                    474:                        if (n == NULL)
                    475:                                continue;
                    476:                        n->next = rpcn;
                    477:                        rpcn = n;
                    478:                        n->port = head->pml_map.pm_port;
                    479:
                    480:                        rpc = getrpcbynumber(head->pml_map.pm_prog);
                    481:                        if (rpc)
                    482:                                n->rpcname = strdup(rpc->r_name);
                    483:                        else {
                    484:                                sprintf(num, "%d", head->pml_map.pm_prog);
                    485:                                n->rpcname = strdup(num);
                    486:                        }
                    487:                }
                    488:                clnt_destroy(client);
                    489:        }
                    490:
                    491:        for (n = rpcn; n; n = n->next)
                    492:                if (n->port == port)
                    493:                        return (n->rpcname);
                    494:        return (NULL);
                    495: }
                    496:
1.1       deraadt   497: /*
                    498:  * Pretty print an Internet address (net address + port).
                    499:  * If the nflag was specified, use numbers instead of names.
                    500:  */
                    501: void
1.5       deraadt   502: inetprint(in, port, proto, local)
1.1       deraadt   503:        register struct in_addr *in;
                    504:        int port;
                    505:        char *proto;
1.5       deraadt   506:        int local;
1.1       deraadt   507: {
                    508:        struct servent *sp = 0;
1.5       deraadt   509:        char line[80], *cp, *nam;
1.1       deraadt   510:        int width;
                    511:
                    512:        sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in));
1.4       millert   513:        cp = strchr(line, '\0');
1.1       deraadt   514:        if (!nflag && port)
                    515:                sp = getservbyport((int)port, proto);
                    516:        if (sp || port == 0)
                    517:                sprintf(cp, "%.8s", sp ? sp->s_name : "*");
1.9       deraadt   518:        else if (local && !nflag && (nam = getrpcportnam(ntohs((u_short)port))))
1.6       deraadt   519:                sprintf(cp, "%d[%.8s]", ntohs((u_short)port), nam);
1.1       deraadt   520:        else
                    521:                sprintf(cp, "%d", ntohs((u_short)port));
                    522:        width = Aflag ? 18 : 22;
                    523:        printf(" %-*.*s", width, width, line);
                    524: }
                    525:
                    526: /*
                    527:  * Construct an Internet address representation.
                    528:  * If the nflag has been supplied, give
                    529:  * numeric value, otherwise try for symbolic name.
                    530:  */
                    531: char *
                    532: inetname(inp)
                    533:        struct in_addr *inp;
                    534: {
                    535:        register char *cp;
                    536:        static char line[50];
                    537:        struct hostent *hp;
                    538:        struct netent *np;
                    539:        static char domain[MAXHOSTNAMELEN + 1];
                    540:        static int first = 1;
                    541:
                    542:        if (first && !nflag) {
                    543:                first = 0;
                    544:                if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1.4       millert   545:                    (cp = strchr(domain, '.')))
1.1       deraadt   546:                        (void) strcpy(domain, cp + 1);
                    547:                else
                    548:                        domain[0] = 0;
                    549:        }
                    550:        cp = 0;
                    551:        if (!nflag && inp->s_addr != INADDR_ANY) {
                    552:                int net = inet_netof(*inp);
                    553:                int lna = inet_lnaof(*inp);
                    554:
                    555:                if (lna == INADDR_ANY) {
                    556:                        np = getnetbyaddr(net, AF_INET);
                    557:                        if (np)
                    558:                                cp = np->n_name;
                    559:                }
                    560:                if (cp == 0) {
                    561:                        hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
                    562:                        if (hp) {
1.4       millert   563:                                if ((cp = strchr(hp->h_name, '.')) &&
1.1       deraadt   564:                                    !strcmp(cp + 1, domain))
                    565:                                        *cp = 0;
                    566:                                cp = hp->h_name;
                    567:                        }
                    568:                }
                    569:        }
                    570:        if (inp->s_addr == INADDR_ANY)
                    571:                strcpy(line, "*");
                    572:        else if (cp)
                    573:                strcpy(line, cp);
                    574:        else {
                    575:                inp->s_addr = ntohl(inp->s_addr);
                    576: #define C(x)   ((x) & 0xff)
                    577:                sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
                    578:                    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
                    579:        }
                    580:        return (line);
1.10    ! angelos   581: }
        !           582:
        !           583: /*
        !           584:  * Dump AH statistics structure.
        !           585:  */
        !           586: void
        !           587: ah_stats(off, name)
        !           588:         u_long off;
        !           589:         char *name;
        !           590: {
        !           591:         struct ahstat ahstat;
        !           592:
        !           593:         if (off == 0)
        !           594:                 return;
        !           595:         kread(off, (char *)&ahstat, sizeof (ahstat));
        !           596:         printf("%s:\n", name);
        !           597:
        !           598: #define p(f, m) if (ahstat.f || sflag <= 1) \
        !           599:     printf(m, ahstat.f, plural(ahstat.f))
        !           600:
        !           601:         p(ahs_hdrops, "\t%u packet%s shorter than header shows\n");
        !           602:         p(ahs_notdb, "\t%u packet%s for which no TDB was found\n");
        !           603:         p(ahs_badkcr, "\t%u input packet%s that failed to be processed\n");
        !           604:         p(ahs_badauth, "\t%u packet%s that failed verification received\n");
        !           605:         p(ahs_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
        !           606:         p(ahs_qfull, "\t%u packet%s were dropeed due to full output queue\n");
        !           607:         p(ahs_wrap, "\t%u packet%s where counter wrapping was detected\n");
        !           608:         p(ahs_replay, "\t%u possibly replayed packet%s received\n");
        !           609:         p(ahs_badauthl, "\t%u packet%s with bad authenticator length received\n");
        !           610: #undef p
        !           611: }
        !           612:
        !           613: /*
        !           614:  * Dump ESP statistics structure.
        !           615:  */
        !           616: void
        !           617: esp_stats(off, name)
        !           618:         u_long off;
        !           619:         char *name;
        !           620: {
        !           621:         struct espstat espstat;
        !           622:
        !           623:         if (off == 0)
        !           624:                 return;
        !           625:         kread(off, (char *)&espstat, sizeof (espstat));
        !           626:         printf("%s:\n", name);
        !           627:
        !           628: #define p(f, m) if (espstat.f || sflag <= 1) \
        !           629:     printf(m, espstat.f, plural(espstat.f))
        !           630:
        !           631:         p(esps_hdrops, "\t%u packet%s shorter than header shows\n");
        !           632:         p(esps_notdb, "\t%u packet%s for which no TDB was found\n");
        !           633:         p(esps_badkcr, "\t%u input packet%s that failed to be processed\n");
        !           634:         p(esps_badauth, "\t%u packet%s that failed verification received\n");
        !           635:         p(esps_noxform, "\t%u packet%s for which no XFORM was set in TDB received\n");
        !           636:         p(esps_qfull, "\t%u packet%s were dropeed due to full output queue\n");
        !           637:         p(esps_wrap, "\t%u packet%s where counter wrapping was detected\n");
        !           638:         p(esps_replay, "\t%u possibly replayed packet%s received\n");
        !           639:         p(esps_badilen, "\t%u packet%s with payload not a multiple of 8 received\n");
        !           640:
        !           641: #undef p
        !           642: }
        !           643:
        !           644: /*
        !           645:  * Dump ESP statistics structure.
        !           646:  */
        !           647: void
        !           648: ip4_stats(off, name)
        !           649:         u_long off;
        !           650:         char *name;
        !           651: {
        !           652:         struct ip4stat ip4stat;
        !           653:
        !           654:         if (off == 0)
        !           655:                 return;
        !           656:         kread(off, (char *)&ip4stat, sizeof (ip4stat));
        !           657:         printf("%s:\n", name);
        !           658:
        !           659: #define p(f, m) if (ip4stat.f || sflag <= 1) \
        !           660:     printf(m, ip4stat.f, plural(ip4stat.f))
        !           661:
        !           662:         p(ip4s_ipackets, "\t%u total input packet%s\n");
        !           663:         p(ip4s_hdrops, "\t%u packet%s shorter than header shows\n");
        !           664:         p(ip4s_notip4, "\t%u packet%s with internal header not IPv4 received\n");
        !           665:         p(ip4s_qfull, "\t%u packet%s were dropeed due to full output queue\n");
        !           666:
        !           667: #undef p
1.1       deraadt   668: }