[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.14

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