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

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