]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/route.c
5a3bfb1957684ed22e94f0b34d34a58c52578906
[apple/network_cmds.git] / netstat.tproj / route.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1983, 1988, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #ifndef lint
58 #if 0
59 static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95";
60 #endif
61 static const char rcsid[] =
62 "$Id: route.c,v 1.3 2002/03/05 20:35:15 lindak Exp $";
63 #endif /* not lint */
64
65 #include <sys/param.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/time.h>
69
70 #include <net/if.h>
71 #include <net/if_var.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
75
76 #include <netinet/in.h>
77 #ifndef __APPLE__
78 #include <netipx/ipx.h>
79 #include <netatalk/at.h>
80 #include <netgraph/ng_socket.h>
81 #endif
82
83 #ifdef NS
84 #include <netns/ns.h>
85 #endif
86
87 #include <sys/sysctl.h>
88
89 #include <arpa/inet.h>
90 #ifndef __APPLE__
91 #include <libutil.h>
92 #endif
93 #include <netdb.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include <unistd.h>
98 #include <err.h>
99 #include <time.h>
100 #include "netstat.h"
101
102 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
103
104
105 /* alignment constraint for routing socket */
106 #define ROUNDUP(a) \
107 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
108 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
109
110 /*
111 * Definitions for showing gateway flags.
112 */
113 struct bits {
114 u_long b_mask;
115 char b_val;
116 } bits[] = {
117 { RTF_UP, 'U' },
118 { RTF_GATEWAY, 'G' },
119 { RTF_HOST, 'H' },
120 { RTF_REJECT, 'R' },
121 { RTF_DYNAMIC, 'D' },
122 { RTF_MODIFIED, 'M' },
123 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
124 { RTF_CLONING, 'C' },
125 { RTF_XRESOLVE, 'X' },
126 { RTF_LLINFO, 'L' },
127 { RTF_STATIC, 'S' },
128 { RTF_PROTO1, '1' },
129 { RTF_PROTO2, '2' },
130 { RTF_WASCLONED,'W' },
131 { RTF_PRCLONING,'c' },
132 { RTF_PROTO3, '3' },
133 { RTF_BLACKHOLE,'B' },
134 { RTF_BROADCAST,'b' },
135 { 0 }
136 };
137
138 typedef union {
139 long dummy; /* Helps align structure. */
140 struct sockaddr u_sa;
141 u_short u_data[128];
142 } sa_u;
143
144 static sa_u pt_u;
145
146 int do_rtent = 0;
147 struct rtentry rtentry;
148 struct radix_node rnode;
149 struct radix_mask rmask;
150 struct radix_node_head *rt_tables[AF_MAX+1];
151
152 int NewTree = 0;
153
154 static struct sockaddr *kgetsa __P((struct sockaddr *));
155 static void p_tree __P((struct radix_node *));
156 static void p_rtnode __P((void));
157 static void ntreestuff __P((void));
158 static void np_rtentry __P((struct rt_msghdr *));
159 static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int));
160 static void p_flags __P((int, char *));
161 static void p_rtentry __P((struct rtentry *));
162 static u_long forgemask __P((u_long));
163 static void domask __P((char *, u_long, u_long));
164
165 /*
166 * Print routing tables.
167 */
168 void
169 routepr(u_long rtree)
170 {
171 struct radix_node_head *rnh, head;
172 int i;
173
174 printf("Routing tables\n");
175
176 if (Aflag == 0 && NewTree)
177 ntreestuff();
178 else {
179 if (rtree == 0) {
180 printf("rt_tables: symbol not in namelist\n");
181 return;
182 }
183
184 kget(rtree, rt_tables);
185 for (i = 0; i <= AF_MAX; i++) {
186 if ((rnh = rt_tables[i]) == 0)
187 continue;
188 kget(rnh, head);
189 if (i == AF_UNSPEC) {
190 if (Aflag && af == 0) {
191 printf("Netmasks:\n");
192 p_tree(head.rnh_treetop);
193 }
194 } else if (af == AF_UNSPEC || af == i) {
195 pr_family(i);
196 do_rtent = 1;
197 pr_rthdr(i);
198 p_tree(head.rnh_treetop);
199 }
200 }
201 }
202 }
203
204 /*
205 * Print address family header before a section of the routing table.
206 */
207 void
208 pr_family(int af)
209 {
210 char *afname;
211
212 switch (af) {
213 case AF_INET:
214 afname = "Internet";
215 break;
216 #ifdef INET6
217 case AF_INET6:
218 afname = "Internet6";
219 break;
220 #endif /*INET6*/
221 case AF_IPX:
222 afname = "IPX";
223 break;
224 #ifdef NS
225 case AF_NS:
226 afname = "XNS";
227 break;
228 #endif
229 #ifndef __APPLE__
230 case AF_ISO:
231 afname = "ISO";
232 break;
233 case AF_APPLETALK:
234 afname = "AppleTalk";
235 break;
236 case AF_CCITT:
237 afname = "X.25";
238 break;
239 case AF_NETGRAPH:
240 afname = "Netgraph";
241 break;
242 #endif
243 default:
244 afname = NULL;
245 break;
246 }
247 if (afname)
248 printf("\n%s:\n", afname);
249 else
250 printf("\nProtocol Family %d:\n", af);
251 }
252
253 /* column widths; each followed by one space */
254 #ifndef INET6
255 #define WID_DST(af) 18 /* width of destination column */
256 #define WID_GW(af) 18 /* width of gateway column */
257 #define WID_IF(af) 6 /* width of netif column */
258 #else
259 #define WID_DST(af) \
260 ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 33: 18)) : 18)
261 #define WID_GW(af) \
262 ((af) == AF_INET6 ? (lflag ? 31 : (nflag ? 29 : 18)) : 18)
263 #define WID_IF(af) ((af) == AF_INET6 ? 8 : 6)
264 #endif /*INET6*/
265
266 /*
267 * Print header for routing table columns.
268 */
269 void
270 pr_rthdr(int af)
271 {
272
273 if (Aflag)
274 printf("%-8.8s ","Address");
275 if (af == AF_INET || lflag)
276 if (lflag)
277 printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %*.*s %6s\n",
278 WID_DST(af), WID_DST(af), "Destination",
279 WID_GW(af), WID_GW(af), "Gateway",
280 "Flags", "Refs", "Use", "Mtu",
281 WID_IF(af), WID_IF(af), "Netif", "Expire");
282 else
283 printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %*.*s %6s\n",
284 WID_DST(af), WID_DST(af), "Destination",
285 WID_GW(af), WID_GW(af), "Gateway",
286 "Flags", "Refs", "Use",
287 WID_IF(af), WID_IF(af), "Netif", "Expire");
288 else
289 printf("%-*.*s %-*.*s %-6.6s %8.8s %6s\n",
290 WID_DST(af), WID_DST(af), "Destination",
291 WID_GW(af), WID_GW(af), "Gateway",
292 "Flags", "Netif", "Expire");
293 }
294
295 static struct sockaddr *
296 kgetsa(struct sockaddr *dst)
297 {
298
299 kget(dst, pt_u.u_sa);
300 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
301 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
302 return (&pt_u.u_sa);
303 }
304
305 static void
306 p_tree(struct radix_node *rn)
307 {
308
309 again:
310 kget(rn, rnode);
311 if (rnode.rn_bit < 0) {
312 if (Aflag)
313 printf("%-8.8lx ", (u_long)rn);
314 if (rnode.rn_flags & RNF_ROOT) {
315 if (Aflag)
316 printf("(root node)%s",
317 rnode.rn_dupedkey ? " =>\n" : "\n");
318 } else if (do_rtent) {
319 kget(rn, rtentry);
320 p_rtentry(&rtentry);
321 if (Aflag)
322 p_rtnode();
323 } else {
324 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
325 NULL, 0, 44);
326 putchar('\n');
327 }
328 if ((rn = rnode.rn_dupedkey))
329 goto again;
330 } else {
331 if (Aflag && do_rtent) {
332 printf("%-8.8lx ", (u_long)rn);
333 p_rtnode();
334 }
335 rn = rnode.rn_right;
336 p_tree(rnode.rn_left);
337 p_tree(rn);
338 }
339 }
340
341 char nbuf[20];
342
343 static void
344 p_rtnode(void)
345 {
346 struct radix_mask *rm = rnode.rn_mklist;
347
348 if (rnode.rn_bit < 0) {
349 if (rnode.rn_mask) {
350 printf("\t mask ");
351 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
352 NULL, 0, -1);
353 } else if (rm == 0)
354 return;
355 } else {
356 sprintf(nbuf, "(%d)", rnode.rn_bit);
357 printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
358 }
359 while (rm) {
360 kget(rm, rmask);
361 sprintf(nbuf, " %d refs, ", rmask.rm_refs);
362 printf(" mk = %8.8lx {(%d),%s",
363 (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
364 if (rmask.rm_flags & RNF_NORMAL) {
365 struct radix_node rnode_aux;
366 printf(" <normal>, ");
367 kget(rmask.rm_leaf, rnode_aux);
368 p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
369 NULL, 0, -1);
370 } else
371 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
372 NULL, 0, -1);
373 putchar('}');
374 if ((rm = rmask.rm_mklist))
375 printf(" ->");
376 }
377 putchar('\n');
378 }
379
380 static void
381 ntreestuff(void)
382 {
383 size_t needed;
384 int mib[6];
385 char *buf, *next, *lim;
386 register struct rt_msghdr *rtm;
387
388 mib[0] = CTL_NET;
389 mib[1] = PF_ROUTE;
390 mib[2] = 0;
391 mib[3] = 0;
392 mib[4] = NET_RT_DUMP;
393 mib[5] = 0;
394 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
395 err(1, "sysctl: net.route.0.0.dump estimate");
396 }
397
398 if ((buf = malloc(needed)) == 0) {
399 err(2, "malloc(%lu)", (unsigned long)needed);
400 }
401 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
402 err(1, "sysctl: net.route.0.0.dump");
403 }
404 lim = buf + needed;
405 for (next = buf; next < lim; next += rtm->rtm_msglen) {
406 rtm = (struct rt_msghdr *)next;
407 np_rtentry(rtm);
408 }
409 }
410
411 static void
412 np_rtentry(struct rt_msghdr *rtm)
413 {
414 register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
415 #ifdef notdef
416 static int masks_done, banner_printed;
417 #endif
418 static int old_af;
419 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
420
421 #ifdef notdef
422 /* for the moment, netmasks are skipped over */
423 if (!banner_printed) {
424 printf("Netmasks:\n");
425 banner_printed = 1;
426 }
427 if (masks_done == 0) {
428 if (rtm->rtm_addrs != RTA_DST ) {
429 masks_done = 1;
430 af = sa->sa_family;
431 }
432 } else
433 #endif
434 af = sa->sa_family;
435 if (af != old_af) {
436 pr_family(af);
437 old_af = af;
438 }
439 if (rtm->rtm_addrs == RTA_DST)
440 p_sockaddr(sa, NULL, 0, 36);
441 else {
442 p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
443 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
444 p_sockaddr(sa, NULL, 0, 18);
445 }
446 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
447 putchar('\n');
448 }
449
450 static void
451 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
452 {
453 char workbuf[128], *cplim;
454 register char *cp = workbuf;
455
456 switch(sa->sa_family) {
457 case AF_INET:
458 {
459 register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
460
461 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
462 mask &&
463 ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr)
464 ==0L)
465 cp = "default" ;
466 else if (flags & RTF_HOST)
467 cp = routename(sin->sin_addr.s_addr);
468 else if (mask)
469 cp = netname(sin->sin_addr.s_addr,
470 ntohl(((struct sockaddr_in *)mask)
471 ->sin_addr.s_addr));
472 else
473 cp = netname(sin->sin_addr.s_addr, 0L);
474 break;
475 }
476
477 #ifdef INET6
478 case AF_INET6:
479 {
480 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
481 struct in6_addr *in6 = &sa6->sin6_addr;
482
483 /*
484 * XXX: This is a special workaround for KAME kernels.
485 * sin6_scope_id field of SA should be set in the future.
486 */
487 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
488 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
489 /* XXX: override is ok? */
490 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
491 *(u_short *)&in6->s6_addr[2] = 0;
492 }
493
494 if (flags & RTF_HOST)
495 cp = routename6(sa6);
496 else if (mask)
497 cp = netname6(sa6,
498 &((struct sockaddr_in6 *)mask)->sin6_addr);
499 else {
500 cp = netname6(sa6, NULL);
501 }
502 break;
503 }
504 #endif /*INET6*/
505 #ifndef __APPLE__
506 case AF_IPX:
507 {
508 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
509 if (ipx_nullnet(satoipx_addr(work)))
510 cp = "default";
511 else
512 cp = ipx_print(sa);
513 break;
514 }
515 case AF_APPLETALK:
516 {
517 if (!(flags & RTF_HOST) && mask)
518 cp = atalk_print2(sa,mask,9);
519 else
520 cp = atalk_print(sa,11);
521 break;
522 }
523 case AF_NETGRAPH:
524 {
525 printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
526 break;
527 }
528 #endif
529 #ifdef NS
530 case AF_NS:
531 cp = ns_print(sa);
532 break;
533 #endif
534
535 case AF_LINK:
536 {
537 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
538
539 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
540 sdl->sdl_slen == 0)
541 (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
542 else
543 switch (sdl->sdl_type) {
544
545 case IFT_ETHER:
546 {
547 register int i;
548 register u_char *lla = (u_char *)sdl->sdl_data +
549 sdl->sdl_nlen;
550
551 cplim = "";
552 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
553 cp += sprintf(cp, "%s%x", cplim, *lla);
554 cplim = ":";
555 }
556 cp = workbuf;
557 break;
558 }
559
560 default:
561 cp = link_ntoa(sdl);
562 break;
563 }
564 break;
565 }
566
567 default:
568 {
569 register u_char *s = (u_char *)sa->sa_data, *slim;
570
571 slim = sa->sa_len + (u_char *) sa;
572 cplim = cp + sizeof(workbuf) - 6;
573 cp += sprintf(cp, "(%d)", sa->sa_family);
574 while (s < slim && cp < cplim) {
575 cp += sprintf(cp, " %02x", *s++);
576 if (s < slim)
577 cp += sprintf(cp, "%02x", *s++);
578 }
579 cp = workbuf;
580 }
581 }
582 if (width < 0 )
583 printf("%s ", cp);
584 else {
585 if (nflag)
586 printf("%-*s ", width, cp);
587 else
588 printf("%-*.*s ", width, width, cp);
589 }
590 }
591
592 static void
593 p_flags(int f, char *format)
594 {
595 char name[33], *flags;
596 register struct bits *p = bits;
597
598 for (flags = name; p->b_mask; p++)
599 if (p->b_mask & f)
600 *flags++ = p->b_val;
601 *flags = '\0';
602 printf(format, name);
603 }
604
605 static void
606 p_rtentry(struct rtentry *rt)
607 {
608 static struct ifnet ifnet, *lastif;
609 struct rtentry parent;
610 static char name[16];
611 static char prettyname[9];
612 struct sockaddr *sa;
613 sa_u addr, mask;
614
615 /*
616 * Don't print protocol-cloned routes unless -a.
617 */
618 if (rt->rt_flags & RTF_WASCLONED && !aflag) {
619 kget(rt->rt_parent, parent);
620 if (parent.rt_flags & RTF_PRCLONING)
621 return;
622 }
623
624 bzero(&addr, sizeof(addr));
625 if ((sa = kgetsa(rt_key(rt))))
626 bcopy(sa, &addr, sa->sa_len);
627 bzero(&mask, sizeof(mask));
628 if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
629 bcopy(sa, &mask, sa->sa_len);
630 p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags,
631 WID_DST(addr.u_sa.sa_family));
632 p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST,
633 WID_GW(addr.u_sa.sa_family));
634 p_flags(rt->rt_flags, "%-6.6s ");
635 if (addr.u_sa.sa_family == AF_INET || lflag) {
636 printf("%6ld %8ld ", rt->rt_refcnt, rt->rt_use);
637 if (lflag) {
638 if (rt->rt_rmx.rmx_mtu != 0)
639 printf("%6lu ", rt->rt_rmx.rmx_mtu);
640 else
641 printf("%6s ", "");
642 }
643 }
644 if (rt->rt_ifp) {
645 if (rt->rt_ifp != lastif) {
646 kget(rt->rt_ifp, ifnet);
647 kread((u_long)ifnet.if_name, name, 16);
648 lastif = rt->rt_ifp;
649 snprintf(prettyname, sizeof prettyname,
650 "%s%d", name, ifnet.if_unit);
651 }
652 printf("%*.*s", WID_IF(addr.u_sa.sa_family),
653 WID_IF(addr.u_sa.sa_family), prettyname);
654 if (rt->rt_rmx.rmx_expire) {
655 time_t expire_time;
656
657 if ((expire_time =
658 rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0)
659 printf(" %6d", (int)expire_time);
660 }
661 if (rt->rt_nodes[0].rn_dupedkey)
662 printf(" =>");
663 }
664 putchar('\n');
665 }
666
667 char *
668 routename(u_long in)
669 {
670 register char *cp;
671 static char line[MAXHOSTNAMELEN];
672 struct hostent *hp;
673
674 cp = 0;
675 if (!nflag) {
676 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
677 AF_INET);
678 if (hp) {
679 cp = hp->h_name;
680 //### trimdomain(cp, strlen(cp));
681 }
682 }
683 if (cp) {
684 strncpy(line, cp, sizeof(line) - 1);
685 line[sizeof(line) - 1] = '\0';
686 } else {
687 #define C(x) ((x) & 0xff)
688 in = ntohl(in);
689 sprintf(line, "%lu.%lu.%lu.%lu",
690 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
691 }
692 return (line);
693 }
694
695 static u_long
696 forgemask(u_long a)
697 {
698 u_long m;
699
700 if (IN_CLASSA(a))
701 m = IN_CLASSA_NET;
702 else if (IN_CLASSB(a))
703 m = IN_CLASSB_NET;
704 else
705 m = IN_CLASSC_NET;
706 return (m);
707 }
708
709 static void
710 domask(char *dst, u_long addr, u_long mask)
711 {
712 register int b, i;
713
714 if (!mask || (forgemask(addr) == mask)) {
715 *dst = '\0';
716 return;
717 }
718 i = 0;
719 for (b = 0; b < 32; b++)
720 if (mask & (1 << b)) {
721 register int bb;
722
723 i = b;
724 for (bb = b+1; bb < 32; bb++)
725 if (!(mask & (1 << bb))) {
726 i = -1; /* noncontig */
727 break;
728 }
729 break;
730 }
731 if (i == -1)
732 sprintf(dst, "&0x%lx", mask);
733 else
734 sprintf(dst, "/%d", 32-i);
735 }
736
737 /*
738 * Return the name of the network whose address is given.
739 * The address is assumed to be that of a net or subnet, not a host.
740 */
741 char *
742 netname(u_long in, u_long mask)
743 {
744 char *cp = 0;
745 static char line[MAXHOSTNAMELEN];
746 struct netent *np = 0;
747 u_long net, omask, dmask;
748 register u_long i;
749
750 i = ntohl(in);
751 dmask = forgemask(i);
752 omask = mask;
753 if (!nflag && i) {
754 net = i & dmask;
755 if (!(np = getnetbyaddr(i, AF_INET)) && net != i)
756 np = getnetbyaddr(net, AF_INET);
757 if (np) {
758 cp = np->n_name;
759 //### trimdomain(cp, strlen(cp));
760 }
761 }
762 if (cp)
763 strncpy(line, cp, sizeof(line) - 1);
764 else {
765 switch (dmask) {
766 case IN_CLASSA_NET:
767 if ((i & IN_CLASSA_HOST) == 0) {
768 sprintf(line, "%lu", C(i >> 24));
769 break;
770 }
771 /* FALLTHROUGH */
772 case IN_CLASSB_NET:
773 if ((i & IN_CLASSB_HOST) == 0) {
774 sprintf(line, "%lu.%lu",
775 C(i >> 24), C(i >> 16));
776 break;
777 }
778 /* FALLTHROUGH */
779 case IN_CLASSC_NET:
780 if ((i & IN_CLASSC_HOST) == 0) {
781 sprintf(line, "%lu.%lu.%lu",
782 C(i >> 24), C(i >> 16), C(i >> 8));
783 break;
784 }
785 /* FALLTHROUGH */
786 default:
787 sprintf(line, "%lu.%lu.%lu.%lu",
788 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
789 break;
790 }
791 }
792 domask(line+strlen(line), i, omask);
793 return (line);
794 }
795
796 #ifdef INET6
797 char *
798 netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
799 {
800 static char line[MAXHOSTNAMELEN];
801 u_char *p = (u_char *)mask;
802 u_char *lim;
803 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
804
805 if (mask) {
806 for (masklen = 0, lim = p + 16; p < lim; p++) {
807 switch (*p) {
808 case 0xff:
809 masklen += 8;
810 break;
811 case 0xfe:
812 masklen += 7;
813 break;
814 case 0xfc:
815 masklen += 6;
816 break;
817 case 0xf8:
818 masklen += 5;
819 break;
820 case 0xf0:
821 masklen += 4;
822 break;
823 case 0xe0:
824 masklen += 3;
825 break;
826 case 0xc0:
827 masklen += 2;
828 break;
829 case 0x80:
830 masklen += 1;
831 break;
832 case 0x00:
833 break;
834 default:
835 illegal ++;
836 break;
837 }
838 }
839 if (illegal)
840 fprintf(stderr, "illegal prefixlen\n");
841 }
842 else
843 masklen = 128;
844
845 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
846 return("default");
847
848 if (nflag)
849 flag |= NI_NUMERICHOST;
850 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
851 NULL, 0, flag);
852
853 if (nflag)
854 sprintf(&line[strlen(line)], "/%d", masklen);
855
856 return line;
857 }
858
859 char *
860 routename6(struct sockaddr_in6 *sa6)
861 {
862 static char line[MAXHOSTNAMELEN];
863 int flag = NI_WITHSCOPEID;
864 /* use local variable for safety */
865 struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),};
866
867 sa6_local.sin6_addr = sa6->sin6_addr;
868 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
869
870 if (nflag)
871 flag |= NI_NUMERICHOST;
872
873 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
874 line, sizeof(line), NULL, 0, flag);
875
876 return line;
877 }
878 #endif /*INET6*/
879
880 /*
881 * Print routing statistics
882 */
883 void
884 rt_stats(u_long rtsaddr, u_long rttaddr)
885 {
886 struct rtstat rtstat;
887 int rttrash;
888
889 if (rtsaddr == 0) {
890 printf("rtstat: symbol not in namelist\n");
891 return;
892 }
893 if (rttaddr == 0) {
894 printf("rttrash: symbol not in namelist\n");
895 return;
896 }
897 kread(rtsaddr, (char *)&rtstat, sizeof (rtstat));
898 kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
899 printf("routing:\n");
900
901 #define p(f, m) if (rtstat.f || sflag <= 1) \
902 printf(m, rtstat.f, plural(rtstat.f))
903
904 p(rts_badredirect, "\t%u bad routing redirect%s\n");
905 p(rts_dynamic, "\t%u dynamically created route%s\n");
906 p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
907 p(rts_unreach, "\t%u destination%s found unreachable\n");
908 p(rts_wildcard, "\t%u use%s of a wildcard route\n");
909 #undef p
910
911 if (rttrash || sflag <= 1)
912 printf("\t%u route%s not in table but not freed\n",
913 rttrash, plural(rttrash));
914 }
915 #ifndef __APPLE__
916 char *
917 ipx_print(struct sockaddr *sa)
918 {
919 u_short port;
920 struct servent *sp = 0;
921 char *net = "", *host = "";
922 register char *p;
923 register u_char *q;
924 struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
925 static char mybuf[50];
926 char cport[10], chost[15], cnet[15];
927
928 port = ntohs(work.x_port);
929
930 if (ipx_nullnet(work) && ipx_nullhost(work)) {
931
932 if (port) {
933 if (sp)
934 sprintf(mybuf, "*.%s", sp->s_name);
935 else
936 sprintf(mybuf, "*.%x", port);
937 } else
938 sprintf(mybuf, "*.*");
939
940 return (mybuf);
941 }
942
943 if (ipx_wildnet(work))
944 net = "any";
945 else if (ipx_nullnet(work))
946 net = "*";
947 else {
948 q = work.x_net.c_net;
949 sprintf(cnet, "%02x%02x%02x%02x",
950 q[0], q[1], q[2], q[3]);
951 for (p = cnet; *p == '0' && p < cnet + 8; p++)
952 continue;
953 net = p;
954 }
955
956 if (ipx_wildhost(work))
957 host = "any";
958 else if (ipx_nullhost(work))
959 host = "*";
960 else {
961 q = work.x_host.c_host;
962 sprintf(chost, "%02x%02x%02x%02x%02x%02x",
963 q[0], q[1], q[2], q[3], q[4], q[5]);
964 for (p = chost; *p == '0' && p < chost + 12; p++)
965 continue;
966 host = p;
967 }
968
969 if (port) {
970 if (strcmp(host, "*") == 0)
971 host = "";
972 if (sp)
973 snprintf(cport, sizeof(cport),
974 "%s%s", *host ? "." : "", sp->s_name);
975 else
976 snprintf(cport, sizeof(cport),
977 "%s%x", *host ? "." : "", port);
978 } else
979 *cport = 0;
980
981 snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
982 return(mybuf);
983 }
984
985 char *
986 ipx_phost(struct sockaddr *sa)
987 {
988 register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
989 struct sockaddr_ipx work;
990 static union ipx_net ipx_zeronet;
991 char *p;
992 struct ipx_addr in;
993
994 work = *sipx;
995 in = work.sipx_addr;
996
997 work.sipx_addr.x_port = 0;
998 work.sipx_addr.x_net = ipx_zeronet;
999 p = ipx_print((struct sockaddr *)&work);
1000 if (strncmp("*.", p, 2) == 0) p += 2;
1001
1002 return(p);
1003 }
1004 #endif
1005 #ifdef NS
1006 short ns_nullh[] = {0,0,0};
1007 short ns_bh[] = {-1,-1,-1};
1008
1009 char *
1010 ns_print(struct sockaddr *sa)
1011 {
1012 register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
1013 struct ns_addr work;
1014 union { union ns_net net_e; u_long long_e; } net;
1015 u_short port;
1016 static char mybuf[50], cport[10], chost[25];
1017 char *host = "";
1018 register char *p; register u_char *q;
1019
1020 work = sns->sns_addr;
1021 port = ntohs(work.x_port);
1022 work.x_port = 0;
1023 net.net_e = work.x_net;
1024 if (ns_nullhost(work) && net.long_e == 0) {
1025 if (port ) {
1026 sprintf(mybuf, "*.%xH", port);
1027 upHex(mybuf);
1028 } else
1029 sprintf(mybuf, "*.*");
1030 return (mybuf);
1031 }
1032
1033 if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
1034 host = "any";
1035 } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
1036 host = "*";
1037 } else {
1038 q = work.x_host.c_host;
1039 sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
1040 q[0], q[1], q[2], q[3], q[4], q[5]);
1041 for (p = chost; *p == '0' && p < chost + 12; p++)
1042 continue;
1043 host = p;
1044 }
1045 if (port)
1046 sprintf(cport, ".%xH", htons(port));
1047 else
1048 *cport = 0;
1049
1050 sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
1051 upHex(mybuf);
1052 return(mybuf);
1053 }
1054
1055 char *
1056 ns_phost(struct sockaddr *sa)
1057 {
1058 register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
1059 struct sockaddr_ns work;
1060 static union ns_net ns_zeronet;
1061 char *p;
1062
1063 work = *sns;
1064 work.sns_addr.x_port = 0;
1065 work.sns_addr.x_net = ns_zeronet;
1066
1067 p = ns_print((struct sockaddr *)&work);
1068 if (strncmp("0H.", p, 3) == 0)
1069 p += 3;
1070 return(p);
1071 }
1072 #endif
1073
1074 void
1075 upHex(char *p0)
1076 {
1077 register char *p = p0;
1078
1079 for (; *p; p++)
1080 switch (*p) {
1081
1082 case 'a':
1083 case 'b':
1084 case 'c':
1085 case 'd':
1086 case 'e':
1087 case 'f':
1088 *p += ('A' - 'a');
1089 break;
1090 }
1091 }