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

Annotation of src/usr.bin/netstat/route.c, Revision 1.8

1.8     ! tholo       1: /*     $OpenBSD: route.c,v 1.7 1997/01/17 07:13:00 millert Exp $       */
1.2       deraadt     2: /*     $NetBSD: route.c,v 1.15 1996/05/07 02:55:06 thorpej Exp $       */
1.1       deraadt     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: @(#)route.c      8.3 (Berkeley) 3/9/94";
                     40: #else
1.8     ! tholo      41: static char *rcsid = "$OpenBSD: route.c,v 1.7 1997/01/17 07:13:00 millert Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46: #include <sys/protosw.h>
                     47: #include <sys/socket.h>
                     48: #include <sys/mbuf.h>
                     49:
                     50: #include <net/if.h>
                     51: #include <net/if_dl.h>
                     52: #include <net/if_types.h>
                     53: #define _KERNEL
                     54: #include <net/route.h>
                     55: #undef _KERNEL
                     56: #include <netinet/in.h>
                     57:
                     58: #include <netns/ns.h>
                     59:
1.5       mickey     60: #include <netipx/ipx.h>
                     61:
1.1       deraadt    62: #include <sys/sysctl.h>
                     63:
                     64: #include <netdb.h>
                     65: #include <stdio.h>
                     66: #include <stdlib.h>
                     67: #include <string.h>
                     68: #include <unistd.h>
                     69: #include "netstat.h"
                     70:
                     71: #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
                     72:
                     73: /*
                     74:  * Definitions for showing gateway flags.
                     75:  */
                     76: struct bits {
                     77:        short   b_mask;
                     78:        char    b_val;
                     79: } bits[] = {
                     80:        { RTF_UP,       'U' },
                     81:        { RTF_GATEWAY,  'G' },
                     82:        { RTF_HOST,     'H' },
                     83:        { RTF_REJECT,   'R' },
                     84:        { RTF_DYNAMIC,  'D' },
                     85:        { RTF_MODIFIED, 'M' },
                     86:        { RTF_DONE,     'd' }, /* Completed -- for routing messages only */
                     87:        { RTF_MASK,     'm' }, /* Mask Present -- for routing messages only */
                     88:        { RTF_CLONING,  'C' },
                     89:        { RTF_XRESOLVE, 'X' },
                     90:        { RTF_LLINFO,   'L' },
                     91:        { RTF_STATIC,   'S' },
                     92:        { RTF_PROTO1,   '1' },
                     93:        { RTF_PROTO2,   '2' },
                     94:        { 0 }
                     95: };
                     96:
                     97: static union {
1.6       deraadt    98:        struct          sockaddr u_sa;
                     99:        u_int32_t       u_data[64];
1.1       deraadt   100: } pt_u;
                    101:
                    102: int    do_rtent = 0;
                    103: struct rtentry rtentry;
                    104: struct radix_node rnode;
                    105: struct radix_mask rmask;
                    106:
                    107: int    NewTree = 0;
                    108:
                    109: static struct sockaddr *kgetsa __P((struct sockaddr *));
                    110: static void p_tree __P((struct radix_node *));
                    111: static void p_rtnode __P(());
                    112: static void ntreestuff __P(());
                    113: static void np_rtentry __P((struct rt_msghdr *));
                    114: static void p_sockaddr __P((struct sockaddr *, int, int));
                    115: static void p_flags __P((int, char *));
                    116: static void p_rtentry __P((struct rtentry *));
                    117:
                    118: /*
                    119:  * Print routing tables.
                    120:  */
                    121: void
                    122: routepr(rtree)
                    123:        u_long rtree;
                    124: {
                    125:        struct radix_node_head *rnh, head;
                    126:        int i;
                    127:
                    128:        printf("Routing tables\n");
                    129:
                    130:        if (Aflag == 0 && NewTree)
                    131:                ntreestuff();
                    132:        else {
                    133:                if (rtree == 0) {
                    134:                        printf("rt_tables: symbol not in namelist\n");
                    135:                        return;
                    136:                }
                    137:
                    138:                kget(rtree, rt_tables);
                    139:                for (i = 0; i <= AF_MAX; i++) {
                    140:                        if ((rnh = rt_tables[i]) == 0)
                    141:                                continue;
                    142:                        kget(rnh, head);
                    143:                        if (i == AF_UNSPEC) {
                    144:                                if (Aflag && af == 0) {
                    145:                                        printf("Netmasks:\n");
                    146:                                        p_tree(head.rnh_treetop);
                    147:                                }
                    148:                        } else if (af == AF_UNSPEC || af == i) {
                    149:                                pr_family(i);
                    150:                                do_rtent = 1;
                    151:                                pr_rthdr();
                    152:                                p_tree(head.rnh_treetop);
                    153:                        }
                    154:                }
                    155:        }
                    156: }
                    157:
                    158: /*
                    159:  * Print address family header before a section of the routing table.
                    160:  */
                    161: void
                    162: pr_family(af)
                    163:        int af;
                    164: {
                    165:        char *afname;
                    166:
                    167:        switch (af) {
                    168:        case AF_INET:
                    169:                afname = "Internet";
                    170:                break;
                    171:        case AF_NS:
                    172:                afname = "XNS";
                    173:                break;
1.5       mickey    174:        case AF_IPX:
                    175:                afname = "IPX";
                    176:                break;
1.1       deraadt   177:        case AF_ISO:
                    178:                afname = "ISO";
                    179:                break;
                    180:        case AF_CCITT:
                    181:                afname = "X.25";
                    182:                break;
                    183:        default:
                    184:                afname = NULL;
                    185:                break;
                    186:        }
                    187:        if (afname)
                    188:                printf("\n%s:\n", afname);
                    189:        else
                    190:                printf("\nProtocol Family %d:\n", af);
                    191: }
                    192:
                    193: /* column widths; each followed by one space */
                    194: #define        WID_DST         16      /* width of destination column */
                    195: #define        WID_GW          18      /* width of gateway column */
                    196:
                    197: /*
                    198:  * Print header for routing table columns.
                    199:  */
                    200: void
                    201: pr_rthdr()
                    202: {
                    203:
                    204:        if (Aflag)
                    205:                printf("%-8.8s ","Address");
                    206:        printf("%-*.*s %-*.*s %-6.6s  %6.6s%8.8s %6.6s  %s\n",
                    207:                WID_DST, WID_DST, "Destination",
                    208:                WID_GW, WID_GW, "Gateway",
                    209:                "Flags", "Refs", "Use", "Mtu", "Interface");
                    210: }
                    211:
                    212: static struct sockaddr *
                    213: kgetsa(dst)
                    214:        register struct sockaddr *dst;
                    215: {
                    216:
                    217:        kget(dst, pt_u.u_sa);
                    218:        if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
                    219:                kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
                    220:        return (&pt_u.u_sa);
                    221: }
                    222:
                    223: static void
                    224: p_tree(rn)
                    225:        struct radix_node *rn;
                    226: {
                    227:
                    228: again:
                    229:        kget(rn, rnode);
                    230:        if (rnode.rn_b < 0) {
                    231:                if (Aflag)
                    232:                        printf("%-8.8x ", rn);
                    233:                if (rnode.rn_flags & RNF_ROOT) {
                    234:                        if (Aflag)
                    235:                                printf("(root node)%s",
                    236:                                    rnode.rn_dupedkey ? " =>\n" : "\n");
                    237:                } else if (do_rtent) {
                    238:                        kget(rn, rtentry);
                    239:                        p_rtentry(&rtentry);
                    240:                        if (Aflag)
                    241:                                p_rtnode();
                    242:                } else {
                    243:                        p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
                    244:                            0, 44);
                    245:                        putchar('\n');
                    246:                }
                    247:                if (rn = rnode.rn_dupedkey)
                    248:                        goto again;
                    249:        } else {
                    250:                if (Aflag && do_rtent) {
                    251:                        printf("%-8.8x ", rn);
                    252:                        p_rtnode();
                    253:                }
                    254:                rn = rnode.rn_r;
                    255:                p_tree(rnode.rn_l);
                    256:                p_tree(rn);
                    257:        }
                    258: }
                    259:
                    260: char   nbuf[20];
                    261:
                    262: static void
                    263: p_rtnode()
                    264: {
                    265:        struct radix_mask *rm = rnode.rn_mklist;
                    266:
                    267:        if (rnode.rn_b < 0) {
                    268:                if (rnode.rn_mask) {
                    269:                        printf("\t  mask ");
                    270:                        p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
                    271:                                    0, -1);
                    272:                } else if (rm == 0)
                    273:                        return;
                    274:        } else {
                    275:                sprintf(nbuf, "(%d)", rnode.rn_b);
                    276:                printf("%6.6s %8.8x : %8.8x", nbuf, rnode.rn_l, rnode.rn_r);
                    277:        }
                    278:        while (rm) {
                    279:                kget(rm, rmask);
                    280:                sprintf(nbuf, " %d refs, ", rmask.rm_refs);
                    281:                printf(" mk = %8.8x {(%d),%s",
                    282:                        rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
                    283:                p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 0, -1);
                    284:                putchar('}');
                    285:                if (rm = rmask.rm_mklist)
                    286:                        printf(" ->");
                    287:        }
                    288:        putchar('\n');
                    289: }
                    290:
                    291: static void
                    292: ntreestuff()
                    293: {
                    294:        size_t needed;
                    295:        int mib[6];
                    296:        char *buf, *next, *lim;
                    297:        register struct rt_msghdr *rtm;
                    298:
                    299:         mib[0] = CTL_NET;
                    300:         mib[1] = PF_ROUTE;
                    301:         mib[2] = 0;
                    302:         mib[3] = 0;
                    303:         mib[4] = NET_RT_DUMP;
                    304:         mib[5] = 0;
                    305:         if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
                    306:                { perror("route-sysctl-estimate"); exit(1);}
                    307:        if ((buf = malloc(needed)) == 0)
                    308:                { printf("out of space\n"); exit(1);}
                    309:         if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
                    310:                { perror("sysctl of routing table"); exit(1);}
                    311:        lim  = buf + needed;
                    312:        for (next = buf; next < lim; next += rtm->rtm_msglen) {
                    313:                rtm = (struct rt_msghdr *)next;
                    314:                np_rtentry(rtm);
                    315:        }
                    316: }
                    317:
                    318: static void
                    319: np_rtentry(rtm)
                    320:        register struct rt_msghdr *rtm;
                    321: {
                    322:        register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
                    323: #ifdef notdef
                    324:        static int masks_done, banner_printed;
                    325: #endif
                    326:        static int old_af;
                    327:        int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
                    328:
                    329: #ifdef notdef
                    330:        /* for the moment, netmasks are skipped over */
                    331:        if (!banner_printed) {
                    332:                printf("Netmasks:\n");
                    333:                banner_printed = 1;
                    334:        }
                    335:        if (masks_done == 0) {
                    336:                if (rtm->rtm_addrs != RTA_DST ) {
                    337:                        masks_done = 1;
                    338:                        af = sa->sa_family;
                    339:                }
                    340:        } else
                    341: #endif
                    342:                af = sa->sa_family;
                    343:        if (af != old_af) {
                    344:                pr_family(af);
                    345:                old_af = af;
                    346:        }
                    347:        if (rtm->rtm_addrs == RTA_DST)
                    348:                p_sockaddr(sa, 0, 36);
                    349:        else {
                    350:                p_sockaddr(sa, rtm->rtm_flags, 16);
                    351:                if (sa->sa_len == 0)
                    352:                        sa->sa_len = sizeof(long);
                    353:                sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
                    354:                p_sockaddr(sa, 0, 18);
                    355:        }
                    356:        p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
                    357:        putchar('\n');
                    358: }
                    359:
                    360: static void
                    361: p_sockaddr(sa, flags, width)
                    362:        struct sockaddr *sa;
                    363:        int flags, width;
                    364: {
                    365:        char workbuf[128], *cplim;
                    366:        register char *cp = workbuf;
                    367:
                    368:        switch(sa->sa_family) {
                    369:        case AF_INET:
                    370:            {
                    371:                register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
                    372:
                    373:                cp = (sin->sin_addr.s_addr == 0) ? "default" :
                    374:                      ((flags & RTF_HOST) ?
                    375:                        routename(sin->sin_addr.s_addr) :
                    376:                        netname(sin->sin_addr.s_addr, INADDR_ANY));
                    377:                break;
                    378:            }
                    379:
                    380:        case AF_NS:
                    381:                cp = ns_print(sa);
                    382:                break;
                    383:
1.5       mickey    384:        case AF_IPX:
                    385:                cp = ipx_print(sa);
                    386:                break;
                    387:
1.1       deraadt   388:        case AF_LINK:
                    389:            {
                    390:                register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
                    391:
                    392:                if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
                    393:                    sdl->sdl_slen == 0)
                    394:                        (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
                    395:                else switch (sdl->sdl_type) {
                    396:                case IFT_ETHER:
                    397:                    {
                    398:                        register int i;
                    399:                        register u_char *lla = (u_char *)sdl->sdl_data +
                    400:                            sdl->sdl_nlen;
                    401:
                    402:                        cplim = "";
                    403:                        for (i = 0; i < sdl->sdl_alen; i++, lla++) {
1.4       deraadt   404:                                cp += snprintf(cp,
                    405:                                    workbuf + sizeof (workbuf) - cp,
                    406:                                    "%s%x", cplim, *lla);
1.1       deraadt   407:                                cplim = ":";
                    408:                        }
                    409:                        cp = workbuf;
                    410:                        break;
                    411:                    }
                    412:                default:
                    413:                        cp = link_ntoa(sdl);
                    414:                        break;
                    415:                }
                    416:                break;
                    417:            }
                    418:
                    419:        default:
                    420:            {
                    421:                register u_char *s = (u_char *)sa->sa_data, *slim;
                    422:
                    423:                slim =  sa->sa_len + (u_char *) sa;
                    424:                cplim = cp + sizeof(workbuf) - 6;
                    425:                cp += sprintf(cp, "(%d)", sa->sa_family);
                    426:                while (s < slim && cp < cplim) {
1.4       deraadt   427:                        cp += snprintf(cp, workbuf + sizeof (workbuf) - cp,
                    428:                            " %02x", *s++);
1.1       deraadt   429:                        if (s < slim)
1.4       deraadt   430:                                cp += snprintf(cp,
                    431:                                    workbuf + sizeof (workbuf) - cp,
                    432:                                    "%02x", *s++);
1.1       deraadt   433:                }
                    434:                cp = workbuf;
                    435:            }
                    436:        }
                    437:        if (width < 0 )
                    438:                printf("%s ", cp);
                    439:        else {
                    440:                if (nflag)
                    441:                        printf("%-*s ", width, cp);
                    442:                else
                    443:                        printf("%-*.*s ", width, width, cp);
                    444:        }
                    445: }
                    446:
                    447: static void
                    448: p_flags(f, format)
                    449:        register int f;
                    450:        char *format;
                    451: {
                    452:        char name[33], *flags;
                    453:        register struct bits *p = bits;
                    454:
                    455:        for (flags = name; p->b_mask; p++)
                    456:                if (p->b_mask & f)
                    457:                        *flags++ = p->b_val;
                    458:        *flags = '\0';
                    459:        printf(format, name);
                    460: }
                    461:
                    462: static void
                    463: p_rtentry(rt)
                    464:        register struct rtentry *rt;
                    465: {
                    466:        static struct ifnet ifnet, *lastif;
                    467:
                    468:        p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, WID_DST);
                    469:        p_sockaddr(kgetsa(rt->rt_gateway), RTF_HOST, WID_GW);
                    470:        p_flags(rt->rt_flags, "%-6.6s ");
                    471:        printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
                    472:        if (rt->rt_rmx.rmx_mtu)
                    473:                printf("%6d ", rt->rt_rmx.rmx_mtu);
                    474:        else
                    475:                printf("%6s ", "-");
                    476:        if (rt->rt_ifp) {
                    477:                if (rt->rt_ifp != lastif) {
                    478:                        kget(rt->rt_ifp, ifnet);
                    479:                        lastif = rt->rt_ifp;
                    480:                }
1.2       deraadt   481:                printf(" %.16s%s", ifnet.if_xname,
1.1       deraadt   482:                        rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
                    483:        }
                    484:        putchar('\n');
                    485: }
                    486:
                    487: char *
                    488: routename(in)
                    489:        u_int32_t in;
                    490: {
                    491:        register char *cp;
                    492:        static char line[MAXHOSTNAMELEN + 1];
                    493:        struct hostent *hp;
                    494:        static char domain[MAXHOSTNAMELEN + 1];
                    495:        static int first = 1;
                    496:
                    497:        if (first) {
                    498:                first = 0;
                    499:                if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
1.7       millert   500:                    (cp = strchr(domain, '.')))
1.1       deraadt   501:                        (void) strcpy(domain, cp + 1);
                    502:                else
                    503:                        domain[0] = 0;
                    504:        }
                    505:        cp = 0;
                    506:        if (!nflag) {
                    507:                hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
                    508:                        AF_INET);
                    509:                if (hp) {
1.7       millert   510:                        if ((cp = strchr(hp->h_name, '.')) &&
1.1       deraadt   511:                            !strcmp(cp + 1, domain))
                    512:                                *cp = 0;
                    513:                        cp = hp->h_name;
                    514:                }
                    515:        }
                    516:        if (cp)
                    517:                strncpy(line, cp, sizeof(line) - 1);
                    518:        else {
                    519: #define C(x)   ((x) & 0xff)
                    520:                in = ntohl(in);
                    521:                sprintf(line, "%u.%u.%u.%u",
                    522:                    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
                    523:        }
                    524:        return (line);
                    525: }
                    526:
                    527: /*
                    528:  * Return the name of the network whose address is given.
                    529:  * The address is assumed to be that of a net or subnet, not a host.
                    530:  */
                    531: char *
                    532: netname(in, mask)
                    533:        u_int32_t in, mask;
                    534: {
                    535:        char *cp = 0;
                    536:        static char line[MAXHOSTNAMELEN + 1];
                    537:        struct netent *np = 0;
                    538:        u_int32_t net;
                    539:        int subnetshift;
                    540:
                    541:        in = ntohl(in);
                    542:        mask = ntohl(mask);
                    543:        if (!nflag && in != INADDR_ANY) {
1.8     ! tholo     544:                np = getnetbyaddr(in, AF_INET);
        !           545:                if (np == NULL) {
        !           546:                        if (mask == INADDR_ANY) {
        !           547:                                if (IN_CLASSA(in)) {
        !           548:                                        mask = IN_CLASSA_NET;
        !           549:                                        subnetshift = 8;
        !           550:                                } else if (IN_CLASSB(in)) {
        !           551:                                        mask = IN_CLASSB_NET;
        !           552:                                        subnetshift = 8;
        !           553:                                } else {
        !           554:                                        mask = IN_CLASSC_NET;
        !           555:                                        subnetshift = 4;
        !           556:                                }
        !           557:                                /*
        !           558:                                * If there are more bits than the standard mask
        !           559:                                * would suggest, subnets must be in use.
        !           560:                                * Guess at the subnet mask, assuming reasonable
        !           561:                                * width subnet fields.
        !           562:                                */
        !           563:                                while (in &~ mask)
        !           564:                                        mask = (long)mask >> subnetshift;
1.1       deraadt   565:                        }
1.8     ! tholo     566:                        net = in & mask;
        !           567:                        while ((mask & 1) == 0)
        !           568:                                mask >>= 1, net >>= 1;
        !           569:                        np = getnetbyaddr(net, AF_INET);
1.1       deraadt   570:                }
                    571:                if (np)
                    572:                        cp = np->n_name;
                    573:        }
                    574:        if (cp)
                    575:                strncpy(line, cp, sizeof(line) - 1);
                    576:        else if ((in & 0xffffff) == 0)
                    577:                sprintf(line, "%u", C(in >> 24));
                    578:        else if ((in & 0xffff) == 0)
                    579:                sprintf(line, "%u.%u", C(in >> 24) , C(in >> 16));
                    580:        else if ((in & 0xff) == 0)
                    581:                sprintf(line, "%u.%u.%u", C(in >> 24), C(in >> 16), C(in >> 8));
                    582:        else
                    583:                sprintf(line, "%u.%u.%u.%u", C(in >> 24),
                    584:                        C(in >> 16), C(in >> 8), C(in));
                    585:        return (line);
                    586: }
                    587:
                    588: /*
                    589:  * Print routing statistics
                    590:  */
                    591: void
                    592: rt_stats(off)
                    593:        u_long off;
                    594: {
                    595:        struct rtstat rtstat;
                    596:
                    597:        if (off == 0) {
                    598:                printf("rtstat: symbol not in namelist\n");
                    599:                return;
                    600:        }
                    601:        kread(off, (char *)&rtstat, sizeof (rtstat));
                    602:        printf("routing:\n");
                    603:        printf("\t%u bad routing redirect%s\n",
                    604:                rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
                    605:        printf("\t%u dynamically created route%s\n",
                    606:                rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
                    607:        printf("\t%u new gateway%s due to redirects\n",
                    608:                rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
                    609:        printf("\t%u destination%s found unreachable\n",
                    610:                rtstat.rts_unreach, plural(rtstat.rts_unreach));
                    611:        printf("\t%u use%s of a wildcard route\n",
                    612:                rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
                    613: }
1.5       mickey    614:
1.1       deraadt   615: short ns_nullh[] = {0,0,0};
                    616: short ns_bh[] = {-1,-1,-1};
                    617:
                    618: char *
                    619: ns_print(sa)
                    620:        register struct sockaddr *sa;
                    621: {
                    622:        register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
                    623:        struct ns_addr work;
                    624:        union { union ns_net net_e; u_long long_e; } net;
                    625:        u_short port;
                    626:        static char mybuf[50], cport[10], chost[25];
                    627:        char *host = "";
                    628:        register char *p; register u_char *q;
                    629:
                    630:        work = sns->sns_addr;
                    631:        port = ntohs(work.x_port);
                    632:        work.x_port = 0;
                    633:        net.net_e  = work.x_net;
                    634:        if (ns_nullhost(work) && net.long_e == 0) {
                    635:                if (port ) {
                    636:                        sprintf(mybuf, "*.%xH", port);
                    637:                        upHex(mybuf);
                    638:                } else
                    639:                        sprintf(mybuf, "*.*");
                    640:                return (mybuf);
                    641:        }
                    642:
                    643:        if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
                    644:                host = "any";
                    645:        } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
                    646:                host = "*";
                    647:        } else {
                    648:                q = work.x_host.c_host;
                    649:                sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
                    650:                        q[0], q[1], q[2], q[3], q[4], q[5]);
                    651:                for (p = chost; *p == '0' && p < chost + 12; p++)
                    652:                        continue;
                    653:                host = p;
                    654:        }
                    655:        if (port)
                    656:                sprintf(cport, ".%xH", htons(port));
                    657:        else
                    658:                *cport = 0;
                    659:
                    660:        sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
                    661:        upHex(mybuf);
                    662:        return(mybuf);
                    663: }
                    664:
                    665: char *
                    666: ns_phost(sa)
                    667:        struct sockaddr *sa;
                    668: {
                    669:        register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
                    670:        struct sockaddr_ns work;
                    671:        static union ns_net ns_zeronet;
                    672:        char *p;
                    673:
                    674:        work = *sns;
                    675:        work.sns_addr.x_port = 0;
                    676:        work.sns_addr.x_net = ns_zeronet;
                    677:
                    678:        p = ns_print((struct sockaddr *)&work);
1.5       mickey    679:        if (strncmp("0H.", p, 3) == 0) p += 3;
                    680:        return(p);
                    681: }
                    682:
                    683: u_short ipx_nullh[] = {0,0,0};
                    684: u_short ipx_bh[] = {0xffff,0xffff,0xffff};
                    685:
                    686: char *
                    687: ipx_print(sa)
                    688:        register struct sockaddr *sa;
                    689: {
                    690:        register struct sockaddr_ipx *sipx = (struct sockaddr_ipx*)sa;
                    691:        struct ipx_addr work;
                    692:        union { union ipx_net net_e; u_long long_e; } net;
                    693:        u_short port;
                    694:        static char mybuf[50], cport[10], chost[25];
                    695:        char *host = "";
                    696:        register char *p; register u_char *q;
                    697:
                    698:        work = sipx->sipx_addr;
                    699:        port = ntohs(work.ipx_port);
                    700:        work.ipx_port = 0;
                    701:        net.net_e  = work.ipx_net;
                    702:        if (ipx_nullhost(work) && net.long_e == 0) {
                    703:                if (port != 0) {
                    704:                        sprintf(mybuf, "*.%xH", port);
                    705:                        upHex(mybuf);
                    706:                } else
                    707:                        sprintf(mybuf, "*.*");
                    708:                return (mybuf);
                    709:        }
                    710:
                    711:        if (bcmp(ipx_bh, work.ipx_host.c_host, 6) == 0) {
                    712:                host = "any";
                    713:        } else if (bcmp(ipx_nullh, work.ipx_host.c_host, 6) == 0) {
                    714:                host = "*";
                    715:        } else {
                    716:                q = work.ipx_host.c_host;
                    717:                sprintf(chost, "%02x:%02x:%02x:%02x:%02x:%02x",
                    718:                        q[0], q[1], q[2], q[3], q[4], q[5]);
                    719:                host = chost;
                    720:        }
                    721:        if (port)
                    722:                sprintf(cport, ".%xH", htons(port));
                    723:        else
                    724:                *cport = 0;
                    725:
                    726:        sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
                    727:        upHex(mybuf);
                    728:        return(mybuf);
                    729: }
                    730:
                    731: char *
                    732: ipx_phost(sa)
                    733:        struct sockaddr *sa;
                    734: {
                    735:        register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
                    736:        struct sockaddr_ipx work;
                    737:        static union ipx_net ipx_zeronet;
                    738:        char *p;
                    739:
                    740:        work = *sipx;
                    741:        work.sipx_addr.ipx_port = 0;
                    742:        work.sipx_addr.ipx_net = ipx_zeronet;
                    743:
                    744:        p = ipx_print((struct sockaddr *)&work);
1.1       deraadt   745:        if (strncmp("0H.", p, 3) == 0) p += 3;
                    746:        return(p);
                    747: }
                    748:
                    749: void
                    750: upHex(p0)
                    751:        char *p0;
                    752: {
                    753:        register char *p = p0;
                    754:        for (; *p; p++) switch (*p) {
                    755:
                    756:        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
                    757:                *p += ('A' - 'a');
                    758:        }
                    759: }