| 1 | /* |
| 2 | * Copyright (c) 1983, 1988 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | /* From: static char sccsid[] = "@(#)route.c 5.20 (Berkeley) 11/29/90"; */ |
| 36 | static const char route_c_rcsid[] = |
| 37 | "$Id: route.c,v 1.8 1993/11/17 23:02:57 wollman Exp $"; |
| 38 | #endif /* not lint */ |
| 39 | |
| 40 | #include <sys/param.h> |
| 41 | #include <sys/socket.h> |
| 42 | #include <sys/mbuf.h> |
| 43 | |
| 44 | #include <net/if.h> |
| 45 | #define KERNEL |
| 46 | #include <net/route.h> |
| 47 | #undef KERNEL |
| 48 | #include <netinet/in.h> |
| 49 | |
| 50 | #ifdef NS |
| 51 | #include <netns/ns.h> |
| 52 | #endif |
| 53 | |
| 54 | #include <netdb.h> |
| 55 | #include <sys/kinfo.h> |
| 56 | |
| 57 | #include <stdio.h> |
| 58 | #include <string.h> |
| 59 | #include <stdlib.h> |
| 60 | |
| 61 | extern int nflag, aflag, Aflag, af; |
| 62 | int do_rtent; |
| 63 | extern char *routename(), *netname(), *plural(); |
| 64 | #ifdef NS |
| 65 | extern char *ns_print(); |
| 66 | #endif |
| 67 | |
| 68 | #define kget(p, d) \ |
| 69 | (kvm_read((off_t)(p), (char *)&(d), sizeof (d))) |
| 70 | |
| 71 | /* |
| 72 | * Definitions for showing gateway flags. |
| 73 | */ |
| 74 | struct bits { |
| 75 | short b_mask; |
| 76 | char b_val; |
| 77 | } bits[] = { |
| 78 | { RTF_UP, 'U' }, |
| 79 | { RTF_GATEWAY, 'G' }, |
| 80 | { RTF_HOST, 'H' }, |
| 81 | { RTF_DYNAMIC, 'D' }, |
| 82 | { RTF_MODIFIED, 'M' }, |
| 83 | { RTF_CLONING, 'C' }, |
| 84 | { RTF_XRESOLVE, 'X' }, |
| 85 | { RTF_LLINFO, 'L' }, |
| 86 | { RTF_REJECT, 'R' }, |
| 87 | { RTF_PROTO2, '2' }, |
| 88 | { RTF_PROTO1, '1' }, |
| 89 | { 0 } |
| 90 | }; |
| 91 | |
| 92 | /* |
| 93 | * Print address family. |
| 94 | */ |
| 95 | void |
| 96 | p_proto(proto) |
| 97 | int proto; |
| 98 | { |
| 99 | switch (proto) |
| 100 | { |
| 101 | case AF_INET: |
| 102 | printf("inet"); |
| 103 | break; |
| 104 | case AF_NS: |
| 105 | printf("NS"); |
| 106 | break; |
| 107 | case AF_OSI: |
| 108 | printf("OSI"); |
| 109 | break; |
| 110 | case AF_CCITT: |
| 111 | printf("CCITT"); |
| 112 | break; |
| 113 | case AF_APPLETALK: |
| 114 | printf("AppleTalk"); |
| 115 | break; |
| 116 | default: |
| 117 | printf("%d", proto); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * Print routing tables. |
| 125 | */ |
| 126 | int |
| 127 | routepr(hostaddr, netaddr, hashsizeaddr, treeaddr) |
| 128 | off_t hostaddr, netaddr, hashsizeaddr, treeaddr; |
| 129 | { |
| 130 | struct mbuf mb; |
| 131 | register struct ortentry *rt; |
| 132 | register struct mbuf *m; |
| 133 | char name[16], *flags; |
| 134 | struct mbuf **routehash; |
| 135 | int hashsize; |
| 136 | int i, doinghost = 1; |
| 137 | |
| 138 | if (!treeaddr) { |
| 139 | printf("Could not find routing tables\n"); |
| 140 | return 1; |
| 141 | } |
| 142 | printf("Routing tables\n"); |
| 143 | if (Aflag) |
| 144 | printf("%-8.8s ","Address"); |
| 145 | printf("%-16.16s %-18.18s %-6.6s %6.6s%8.8s %-5.5s", |
| 146 | "Destination", "Gateway", |
| 147 | "Flags", "Refs", "Use", "Iface"); |
| 148 | if(!aflag) { |
| 149 | printf("%-6.6s %-6.6s\n", "MTU", "Rtt"); |
| 150 | } else { |
| 151 | printf("\n %8s %8s %8s %8s %8s %8s %8s %8s\n", |
| 152 | "MTU", "Hopcount", "Expire", "recvpipe", |
| 153 | "sendpipe", "ssthresh", "RTT", "RTT var."); |
| 154 | } |
| 155 | |
| 156 | return treestuff(treeaddr); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | static union { |
| 161 | struct sockaddr u_sa; |
| 162 | u_short u_data[128]; |
| 163 | } pt_u; |
| 164 | int do_rtent = 0; |
| 165 | struct rtentry rtentry; |
| 166 | struct radix_node rnode; |
| 167 | struct radix_mask rmask; |
| 168 | |
| 169 | int NewTree = 0; |
| 170 | treestuff(rtree) |
| 171 | off_t rtree; |
| 172 | { |
| 173 | struct radix_node_head *rnh, head; |
| 174 | |
| 175 | if (Aflag == 0 && NewTree) |
| 176 | return(ntreestuff()); |
| 177 | for (kget(rtree, rnh); rnh; rnh = head.rnh_next) { |
| 178 | kget(rnh, head); |
| 179 | if (head.rnh_af == 0) { |
| 180 | if (Aflag || af == AF_UNSPEC) { |
| 181 | printf("Netmasks:\n"); |
| 182 | p_tree(head.rnh_treetop); |
| 183 | } |
| 184 | } else if (af == AF_UNSPEC || af == head.rnh_af) { |
| 185 | printf("\nRoute Tree for Protocol Family "); |
| 186 | p_proto(head.rnh_af); |
| 187 | printf(":\n"); |
| 188 | do_rtent = 1; |
| 189 | p_tree(head.rnh_treetop); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | struct sockaddr * |
| 195 | kgetsa(dst) |
| 196 | register struct sockaddr *dst; |
| 197 | { |
| 198 | kget(dst, pt_u.u_sa); |
| 199 | if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) { |
| 200 | kvm_read((off_t)dst, pt_u.u_data, pt_u.u_sa.sa_len); |
| 201 | } |
| 202 | return (&pt_u.u_sa); |
| 203 | } |
| 204 | |
| 205 | p_tree(rn) |
| 206 | struct radix_node *rn; |
| 207 | { |
| 208 | |
| 209 | again: |
| 210 | kget(rn, rnode); |
| 211 | if (rnode.rn_b < 0) { |
| 212 | if (Aflag) |
| 213 | printf("%-8.8x ", rn); |
| 214 | if (rnode.rn_flags & RNF_ROOT) |
| 215 | printf("(root node)%s", |
| 216 | rnode.rn_dupedkey ? " =>\n" : "\n"); |
| 217 | else if (do_rtent) { |
| 218 | kget(rn, rtentry); |
| 219 | p_rtentry(&rtentry); |
| 220 | if (Aflag) |
| 221 | p_rtnode(); |
| 222 | } else { |
| 223 | p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), |
| 224 | 0, 44); |
| 225 | putchar('\n'); |
| 226 | } |
| 227 | if (rn = rnode.rn_dupedkey) |
| 228 | goto again; |
| 229 | } else { |
| 230 | if (Aflag && do_rtent) { |
| 231 | printf("%-8.8x ", rn); |
| 232 | p_rtnode(); |
| 233 | } |
| 234 | rn = rnode.rn_r; |
| 235 | p_tree(rnode.rn_l); |
| 236 | p_tree(rn); |
| 237 | } |
| 238 | } |
| 239 | char nbuf[20]; |
| 240 | |
| 241 | p_rtnode() |
| 242 | { |
| 243 | |
| 244 | struct radix_mask *rm = rnode.rn_mklist; |
| 245 | if (rnode.rn_b < 0) { |
| 246 | if (rnode.rn_mask) { |
| 247 | printf("\t mask "); |
| 248 | p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), |
| 249 | 0, -1); |
| 250 | } else if (rm == 0) |
| 251 | return; |
| 252 | } else { |
| 253 | sprintf(nbuf, "(%d)", rnode.rn_b); |
| 254 | printf("%6.6s %8.8x : %8.8x", nbuf, rnode.rn_l, rnode.rn_r); |
| 255 | } |
| 256 | while (rm) { |
| 257 | kget(rm, rmask); |
| 258 | sprintf(nbuf, " %d refs, ", rmask.rm_refs); |
| 259 | printf(" mk = %8.8x {(%d),%s", |
| 260 | rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " "); |
| 261 | p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 0, -1); |
| 262 | putchar('}'); |
| 263 | if (rm = rmask.rm_mklist) |
| 264 | printf(" ->"); |
| 265 | } |
| 266 | putchar('\n'); |
| 267 | } |
| 268 | |
| 269 | ntreestuff() |
| 270 | { |
| 271 | int needed; |
| 272 | char *buf, *next, *lim; |
| 273 | register struct rt_msghdr *rtm; |
| 274 | |
| 275 | if ((needed = getkerninfo(KINFO_RT_DUMP, 0, 0, 0)) < 0) |
| 276 | { perror("route-getkerninfo-estimate"); exit(1);} |
| 277 | if ((buf = malloc(needed)) == 0) |
| 278 | { printf("out of space\n"); exit(1);} |
| 279 | if (getkerninfo(KINFO_RT_DUMP, buf, &needed, 0) < 0) |
| 280 | { perror("actual retrieval of routing table"); exit(1);} |
| 281 | lim = buf + needed; |
| 282 | for (next = buf; next < lim; next += rtm->rtm_msglen) { |
| 283 | rtm = (struct rt_msghdr *)next; |
| 284 | np_rtentry(rtm); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | np_rtentry(rtm) |
| 289 | register struct rt_msghdr *rtm; |
| 290 | { |
| 291 | register struct sockaddr *sa = (struct sockaddr *)(rtm + 1); |
| 292 | static int masks_done, old_af, banner_printed; |
| 293 | int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST; |
| 294 | |
| 295 | #ifdef notdef |
| 296 | /* for the moment, netmasks are skipped over */ |
| 297 | if (!banner_printed) { |
| 298 | printf("Netmasks:\n"); |
| 299 | banner_printed = 1; |
| 300 | } |
| 301 | if (masks_done == 0) { |
| 302 | if (rtm->rtm_addrs != RTA_DST ) { |
| 303 | masks_done = 1; |
| 304 | af = sa->sa_family; |
| 305 | } |
| 306 | } else |
| 307 | #endif |
| 308 | af = sa->sa_family; |
| 309 | if (af != old_af) { |
| 310 | printf("\nRoute Tree for Protocol Family %d:\n", af); |
| 311 | old_af = af; |
| 312 | } |
| 313 | if (rtm->rtm_addrs == RTA_DST) |
| 314 | p_sockaddr(sa, 0, 36); |
| 315 | else { |
| 316 | p_sockaddr(sa, rtm->rtm_flags, 16); |
| 317 | if (sa->sa_len == 0) |
| 318 | sa->sa_len = sizeof(long); |
| 319 | sa = (struct sockaddr *)(sa->sa_len + (char *)sa); |
| 320 | p_sockaddr(sa, 0, 18); |
| 321 | } |
| 322 | p_flags(rtm->rtm_flags & interesting, "%-6.6s "); |
| 323 | putchar('\n'); |
| 324 | } |
| 325 | |
| 326 | p_sockaddr(sa, flags, width) |
| 327 | struct sockaddr *sa; |
| 328 | int flags, width; |
| 329 | { |
| 330 | char format[20], workbuf[128], *cp, *cplim; |
| 331 | register char *cpout; |
| 332 | |
| 333 | switch(sa->sa_family) { |
| 334 | case AF_INET: |
| 335 | { |
| 336 | register struct sockaddr_in *sin = (struct sockaddr_in *)sa; |
| 337 | |
| 338 | cp = (sin->sin_addr.s_addr == 0) ? "default" : |
| 339 | ((flags & RTF_HOST) ? |
| 340 | routename(sin->sin_addr) : netname(sin->sin_addr, 0L)); |
| 341 | } |
| 342 | break; |
| 343 | |
| 344 | #ifdef NS |
| 345 | case AF_NS: |
| 346 | cp = ns_print((struct sockaddr_ns *)sa); |
| 347 | break; |
| 348 | #endif |
| 349 | |
| 350 | default: |
| 351 | { |
| 352 | register u_char *s = ((u_char *)sa->sa_data), *slim; |
| 353 | |
| 354 | slim = (u_char *) sa + sa->sa_len; |
| 355 | cp = workbuf; |
| 356 | cplim = cp + sizeof(workbuf) - 6; |
| 357 | cp += sprintf(cp, "(%d)", sa->sa_family); |
| 358 | while (s < slim && cp < cplim) |
| 359 | cp += sprintf(cp, " %02x%02x", s[0], s[1]), s += 2; |
| 360 | cp = workbuf; |
| 361 | } |
| 362 | } |
| 363 | if (width < 0 ) |
| 364 | printf("%s ", cp); |
| 365 | else { |
| 366 | if (nflag) |
| 367 | printf("%-*s ", width, cp); |
| 368 | else |
| 369 | printf("%-*.*s ", width, width, cp); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | p_flags(f, format) |
| 374 | register int f; |
| 375 | char *format; |
| 376 | { |
| 377 | char name[33], *flags; |
| 378 | register struct bits *p = bits; |
| 379 | for (flags = name; p->b_mask; p++) |
| 380 | if (p->b_mask & f) |
| 381 | *flags++ = p->b_val; |
| 382 | *flags = '\0'; |
| 383 | printf(format, name); |
| 384 | } |
| 385 | |
| 386 | p_rtentry(rt) |
| 387 | register struct rtentry *rt; |
| 388 | { |
| 389 | char name[16]; |
| 390 | register struct sockaddr *sa; |
| 391 | struct ifnet ifnet; |
| 392 | |
| 393 | p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, 16); |
| 394 | p_sockaddr(kgetsa(rt->rt_gateway), RTF_HOST, 18); |
| 395 | p_flags(rt->rt_flags, "%-6.6s "); |
| 396 | printf("%6d %8d ", rt->rt_refcnt, rt->rt_use); |
| 397 | if (rt->rt_ifp == 0) { |
| 398 | putchar('\n'); |
| 399 | return; |
| 400 | } |
| 401 | kget(rt->rt_ifp, ifnet); |
| 402 | kvm_read((off_t)ifnet.if_name, name, 16); |
| 403 | printf(" %.2s%d", name, ifnet.if_unit); |
| 404 | if(aflag) { |
| 405 | /* |
| 406 | * MTU |
| 407 | */ |
| 408 | if(rt->rt_rmx.rmx_mtu) |
| 409 | printf("\n %7d%c", rt->rt_rmx.rmx_mtu, |
| 410 | (rt->rt_rmx.rmx_locks & RTV_MTU) ? '*' : ' '); |
| 411 | else |
| 412 | printf("\n %7s ", "-"); |
| 413 | |
| 414 | /* |
| 415 | * Hop count |
| 416 | */ |
| 417 | if(rt->rt_rmx.rmx_hopcount) |
| 418 | printf(" %7d%c", rt->rt_rmx.rmx_hopcount, |
| 419 | (rt->rt_rmx.rmx_locks & RTV_HOPCOUNT) ? '*' : ' '); |
| 420 | else |
| 421 | printf(" %7s ", "-"); |
| 422 | |
| 423 | /* |
| 424 | * Expiration time |
| 425 | */ |
| 426 | if(rt->rt_rmx.rmx_expire) |
| 427 | printf(" %7d%c", rt->rt_rmx.rmx_expire, |
| 428 | (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? '*' : ' '); |
| 429 | else |
| 430 | printf(" %7s ", "-"); |
| 431 | |
| 432 | /* |
| 433 | * Receive pipe size (bytes) |
| 434 | */ |
| 435 | if(rt->rt_rmx.rmx_recvpipe) |
| 436 | printf(" %7d%c", rt->rt_rmx.rmx_recvpipe, |
| 437 | (rt->rt_rmx.rmx_locks & RTV_RPIPE) ? '*' : ' '); |
| 438 | else |
| 439 | printf(" %7s ", "-"); |
| 440 | |
| 441 | /* |
| 442 | * Send pipe size (bytes) |
| 443 | */ |
| 444 | if(rt->rt_rmx.rmx_sendpipe) |
| 445 | printf(" %7d%c", rt->rt_rmx.rmx_sendpipe, |
| 446 | (rt->rt_rmx.rmx_locks & RTV_SPIPE) ? '*' : ' '); |
| 447 | else |
| 448 | printf(" %7s ", "-"); |
| 449 | |
| 450 | /* |
| 451 | * Slow-start threshold (bytes) |
| 452 | */ |
| 453 | if(rt->rt_rmx.rmx_ssthresh) |
| 454 | printf(" %7d%c", rt->rt_rmx.rmx_ssthresh, |
| 455 | (rt->rt_rmx.rmx_locks & RTV_SSTHRESH) ? '*' : ' '); |
| 456 | else |
| 457 | printf(" %7s ", "-"); |
| 458 | |
| 459 | /* |
| 460 | * Round-trip time (seconds) |
| 461 | */ |
| 462 | if(rt->rt_rmx.rmx_rtt) |
| 463 | printf(" %7.4f%c", (1.0 * rt->rt_rmx.rmx_rtt) / RTM_RTTUNIT, |
| 464 | (rt->rt_rmx.rmx_locks & RTV_RTT) ? '*' : ' '); |
| 465 | else |
| 466 | printf(" %7s ", "-"); |
| 467 | |
| 468 | /* |
| 469 | * Round-trip time variance (seconds) |
| 470 | */ |
| 471 | if(rt->rt_rmx.rmx_rttvar) |
| 472 | printf(" %7.4f%c", (1.0 * rt->rt_rmx.rmx_rttvar) / RTM_RTTUNIT, |
| 473 | (rt->rt_rmx.rmx_locks & RTV_RTTVAR) ? '*' : ' '); |
| 474 | else |
| 475 | printf(" %7s ", "-"); |
| 476 | } else { /* no -a flag */ |
| 477 | if(rt->rt_rmx.rmx_mtu) |
| 478 | printf(" %6d", rt->rt_rmx.rmx_mtu); |
| 479 | else |
| 480 | printf(" %-6s", "-"); |
| 481 | if(rt->rt_rmx.rmx_rtt) |
| 482 | printf(" %6.3f", (1. * rt->rt_rmx.rmx_rtt) / RTM_RTTUNIT); |
| 483 | else |
| 484 | printf(" %-6s", "-"); |
| 485 | } |
| 486 | printf(rt->rt_nodes[0].rn_dupedkey ? " =>\n" : "\n"); |
| 487 | } |
| 488 | |
| 489 | char * |
| 490 | routename(in) |
| 491 | struct in_addr in; |
| 492 | { |
| 493 | register char *cp; |
| 494 | static char line[MAXHOSTNAMELEN + 1]; |
| 495 | struct hostent *hp; |
| 496 | static char domain[MAXHOSTNAMELEN + 1]; |
| 497 | static int first = 1; |
| 498 | char *index(); |
| 499 | |
| 500 | if (first) { |
| 501 | first = 0; |
| 502 | if (gethostname(domain, MAXHOSTNAMELEN) == 0 && |
| 503 | (cp = index(domain, '.'))) |
| 504 | (void) strcpy(domain, cp + 1); |
| 505 | else |
| 506 | domain[0] = 0; |
| 507 | } |
| 508 | cp = 0; |
| 509 | if (!nflag) { |
| 510 | hp = gethostbyaddr((char *)&in, sizeof (struct in_addr), |
| 511 | AF_INET); |
| 512 | if (hp) { |
| 513 | if ((cp = index(hp->h_name, '.')) && |
| 514 | !strcmp(cp + 1, domain)) |
| 515 | *cp = 0; |
| 516 | cp = hp->h_name; |
| 517 | } |
| 518 | } |
| 519 | if (cp) |
| 520 | strncpy(line, cp, sizeof(line) - 1); |
| 521 | else { |
| 522 | #define C(x) ((x) & 0xff) |
| 523 | in.s_addr = ntohl(in.s_addr); |
| 524 | sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), |
| 525 | C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr)); |
| 526 | } |
| 527 | return (line); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Return the name of the network whose address is given. |
| 532 | * The address is assumed to be that of a net or subnet, not a host. |
| 533 | */ |
| 534 | char * |
| 535 | netname(in, mask) |
| 536 | struct in_addr in; |
| 537 | u_long mask; |
| 538 | { |
| 539 | char *cp = 0; |
| 540 | static char line[MAXHOSTNAMELEN + 1]; |
| 541 | struct netent *np = 0; |
| 542 | u_long net; |
| 543 | register i; |
| 544 | int subnetshift; |
| 545 | |
| 546 | i = ntohl(in.s_addr); |
| 547 | if (!nflag && i) { |
| 548 | if (mask == 0) { |
| 549 | if (IN_CLASSA(i)) { |
| 550 | mask = IN_CLASSA_NET; |
| 551 | subnetshift = 8; |
| 552 | } else if (IN_CLASSB(i)) { |
| 553 | mask = IN_CLASSB_NET; |
| 554 | subnetshift = 8; |
| 555 | } else { |
| 556 | mask = IN_CLASSC_NET; |
| 557 | subnetshift = 4; |
| 558 | } |
| 559 | /* |
| 560 | * If there are more bits than the standard mask |
| 561 | * would suggest, subnets must be in use. |
| 562 | * Guess at the subnet mask, assuming reasonable |
| 563 | * width subnet fields. |
| 564 | */ |
| 565 | while (i &~ mask) |
| 566 | mask = (long)mask >> subnetshift; |
| 567 | } |
| 568 | net = i & mask; |
| 569 | while ((mask & 1) == 0) |
| 570 | mask >>= 1, net >>= 1; |
| 571 | np = getnetbyaddr(net, AF_INET); |
| 572 | if (np) |
| 573 | cp = np->n_name; |
| 574 | } |
| 575 | if (cp) |
| 576 | strncpy(line, cp, sizeof(line) - 1); |
| 577 | else if ((i & 0xffffff) == 0) |
| 578 | sprintf(line, "%u", C(i >> 24)); |
| 579 | else if ((i & 0xffff) == 0) |
| 580 | sprintf(line, "%u.%u", C(i >> 24) , C(i >> 16)); |
| 581 | else if ((i & 0xff) == 0) |
| 582 | sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8)); |
| 583 | else |
| 584 | sprintf(line, "%u.%u.%u.%u", C(i >> 24), |
| 585 | C(i >> 16), C(i >> 8), C(i)); |
| 586 | return (line); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Print routing statistics |
| 591 | */ |
| 592 | rt_stats(off) |
| 593 | off_t off; |
| 594 | { |
| 595 | struct rtstat rtstat; |
| 596 | |
| 597 | if (off == 0) { |
| 598 | printf("rtstat: symbol not in namelist\n"); |
| 599 | return; |
| 600 | } |
| 601 | kvm_read(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 | } |
| 614 | #ifdef NS |
| 615 | short ns_nullh[] = {0,0,0}; |
| 616 | short ns_bh[] = {-1,-1,-1}; |
| 617 | |
| 618 | char * |
| 619 | ns_print(sns) |
| 620 | struct sockaddr_ns *sns; |
| 621 | { |
| 622 | struct ns_addr work; |
| 623 | union { union ns_net net_e; u_long long_e; } net; |
| 624 | u_short port; |
| 625 | static char mybuf[50], cport[10], chost[25]; |
| 626 | char *host = ""; |
| 627 | register char *p; register u_char *q; |
| 628 | |
| 629 | work = sns->sns_addr; |
| 630 | port = ntohs(work.x_port); |
| 631 | work.x_port = 0; |
| 632 | net.net_e = work.x_net; |
| 633 | if (ns_nullhost(work) && net.long_e == 0) { |
| 634 | if (port ) { |
| 635 | sprintf(mybuf, "*.%xH", port); |
| 636 | upHex(mybuf); |
| 637 | } else |
| 638 | sprintf(mybuf, "*.*"); |
| 639 | return (mybuf); |
| 640 | } |
| 641 | |
| 642 | if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) { |
| 643 | host = "any"; |
| 644 | } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) { |
| 645 | host = "*"; |
| 646 | } else { |
| 647 | q = work.x_host.c_host; |
| 648 | sprintf(chost, "%02x%02x%02x%02x%02x%02xH", |
| 649 | q[0], q[1], q[2], q[3], q[4], q[5]); |
| 650 | for (p = chost; *p == '0' && p < chost + 12; p++); |
| 651 | host = p; |
| 652 | } |
| 653 | if (port) |
| 654 | sprintf(cport, ".%xH", htons(port)); |
| 655 | else |
| 656 | *cport = 0; |
| 657 | |
| 658 | sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport); |
| 659 | upHex(mybuf); |
| 660 | return(mybuf); |
| 661 | } |
| 662 | |
| 663 | char * |
| 664 | ns_phost(sns) |
| 665 | struct sockaddr_ns *sns; |
| 666 | { |
| 667 | struct sockaddr_ns work; |
| 668 | static union ns_net ns_zeronet; |
| 669 | char *p; |
| 670 | |
| 671 | work = *sns; |
| 672 | work.sns_addr.x_port = 0; |
| 673 | work.sns_addr.x_net = ns_zeronet; |
| 674 | |
| 675 | p = ns_print(&work); |
| 676 | if (strncmp("0H.", p, 3) == 0) p += 3; |
| 677 | return(p); |
| 678 | } |
| 679 | upHex(p0) |
| 680 | char *p0; |
| 681 | { |
| 682 | register char *p = p0; |
| 683 | for (; *p; p++) switch (*p) { |
| 684 | |
| 685 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': |
| 686 | *p += ('A' - 'a'); |
| 687 | } |
| 688 | } |
| 689 | #endif /* NS */ |