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

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