Network: Move common utilities to the common area
[AROS.git] / workbench / network / common / C / netstat / route.c
blobba2262e8c50506ef03249e6a35ade9ba526d1a61
1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2006
5 * Pavel Fedin
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #if 0
37 static char sccsid[] = "From: @(#)route.c 8.3 (Berkeley) 3/9/94";
38 static const char rcsid[] =
39 "$Id$";
40 #endif /* not lint */
42 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/mbuf.h>
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #define KERNEL
51 #include <net/route.h>
52 #undef KERNEL
53 #include <netinet/in.h>
55 #ifdef NETNS
56 #include <netns/ns.h>
57 #endif
59 #ifdef ENABLE_SYSCTL
60 #include <sys/sysctl.h>
61 #endif
63 #include <netdb.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <err.h>
69 #include <time.h>
70 #include "netstat.h"
72 #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
75 * Definitions for showing gateway flags.
77 struct bits {
78 u_long b_mask;
79 char b_val;
80 } bits[] = {
81 { RTF_UP, 'U' },
82 { RTF_GATEWAY, 'G' },
83 { RTF_HOST, 'H' },
84 { RTF_REJECT, 'R' },
85 { RTF_DYNAMIC, 'D' },
86 { RTF_MODIFIED, 'M' },
87 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
88 { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
89 { RTF_CLONING, 'C' },
90 { RTF_XRESOLVE, 'X' },
91 { RTF_LLINFO, 'L' },
92 { RTF_STATIC, 'S' },
93 { RTF_PROTO1, '1' },
94 { RTF_PROTO2, '2' },
95 { RTF_WASCLONED,'W' },
96 { RTF_PRCLONING,'c' },
97 { RTF_PROTO3, '3' },
98 { 0 }
101 static union {
102 struct sockaddr u_sa;
103 u_short u_data[128];
104 } pt_u;
106 int do_rtent = 0;
107 struct rtentry rtentry;
108 struct radix_node rnode;
109 struct radix_mask rmask;
110 struct radix_node_head *rt_tables[AF_MAX+1];
112 int NewTree = 0;
114 static struct sockaddr *kgetsa __P((struct sockaddr *));
115 static void p_tree __P((struct radix_node *));
116 static void p_rtnode __P(());
117 static void ntreestuff __P(());
118 static void np_rtentry __P((struct rt_msghdr *));
119 static void p_sockaddr __P((struct sockaddr *, int, int));
120 static void p_flags __P((int, char *));
121 static void p_rtentry __P((struct rtentry *));
124 * Print routing tables.
126 void
127 routepr(rtree)
128 u_long rtree;
130 struct radix_node_head *rnh, head;
131 int i;
133 printf("Routing tables\n");
135 #ifdef ENABLE_SYSCTL
136 if (Aflag == 0 && NewTree)
137 ntreestuff();
138 else {
139 #endif
140 if (rtree == 0) {
141 printf("rt_tables: symbol not in namelist\n");
142 return;
145 kget(rtree, rt_tables);
146 for (i = 0; i <= AF_MAX; i++) {
147 if ((rnh = rt_tables[i]) == 0)
148 continue;
149 kget(rnh, head);
150 if (i == AF_UNSPEC) {
151 if (Aflag && af == 0) {
152 printf("Netmasks:\n");
153 p_tree(head.rnh_treetop);
155 } else if (af == AF_UNSPEC || af == i) {
156 pr_family(i);
157 do_rtent = 1;
158 pr_rthdr();
159 p_tree(head.rnh_treetop);
162 #ifdef ENABLE_SYSCTL
164 #endif
168 * Print address family header before a section of the routing table.
170 void
171 pr_family(af)
172 int af;
174 char *afname;
176 switch (af) {
177 case AF_INET:
178 afname = "Internet";
179 break;
180 case AF_NS:
181 afname = "XNS";
182 break;
183 case AF_ISO:
184 afname = "ISO";
185 break;
186 case AF_CCITT:
187 afname = "X.25";
188 break;
189 default:
190 afname = NULL;
191 break;
193 if (afname)
194 printf("\n%s:\n", afname);
195 else
196 printf("\nProtocol Family %d:\n", af);
199 /* column widths; each followed by one space */
200 #define WID_DST 16 /* width of destination column */
201 #define WID_GW 18 /* width of gateway column */
204 * Print header for routing table columns.
206 void
207 pr_rthdr()
210 if (Aflag)
211 printf("%-8.8s ","Address");
212 printf("%-*.*s %-*.*s %-6.6s %6.6s%8.8s %8.8s %6s\n",
213 WID_DST, WID_DST, "Destination",
214 WID_GW, WID_GW, "Gateway",
215 "Flags", "Refs", "Use", "Netif", "Expire");
218 static struct sockaddr *
219 kgetsa(dst)
220 register struct sockaddr *dst;
223 kget(dst, pt_u.u_sa);
224 if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
225 kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
226 return (&pt_u.u_sa);
229 static void
230 p_tree(rn)
231 struct radix_node *rn;
234 again:
235 kget(rn, rnode);
236 if (rnode.rn_b < 0) {
237 if (Aflag)
238 printf("%-8.8x ", rn);
239 if (rnode.rn_flags & RNF_ROOT) {
240 if (Aflag)
241 printf("(root node)%s",
242 rnode.rn_dupedkey ? " =>\n" : "\n");
243 } else if (do_rtent) {
244 kget(rn, rtentry);
245 p_rtentry(&rtentry);
246 if (Aflag)
247 p_rtnode();
248 } else {
249 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
250 0, 44);
251 putchar('\n');
253 if (rn = rnode.rn_dupedkey)
254 goto again;
255 } else {
256 if (Aflag && do_rtent) {
257 printf("%-8.8x ", rn);
258 p_rtnode();
260 rn = rnode.rn_r;
261 p_tree(rnode.rn_l);
262 p_tree(rn);
266 char nbuf[20];
268 static void
269 p_rtnode()
271 struct radix_mask *rm = rnode.rn_mklist;
273 if (rnode.rn_b < 0) {
274 if (rnode.rn_mask) {
275 printf("\t mask ");
276 p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
277 0, -1);
278 } else if (rm == 0)
279 return;
280 } else {
281 sprintf(nbuf, "(%d)", rnode.rn_b);
282 printf("%6.6s %8.8x : %8.8x", nbuf, rnode.rn_l, rnode.rn_r);
284 while (rm) {
285 kget(rm, rmask);
286 sprintf(nbuf, " %d refs, ", rmask.rm_refs);
287 printf(" mk = %8.8x {(%d),%s",
288 rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
289 p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 0, -1);
290 putchar('}');
291 if (rm = rmask.rm_mklist)
292 printf(" ->");
294 putchar('\n');
297 #ifdef ENABLE_SYSCTL
298 static void
299 ntreestuff()
301 size_t needed;
302 int mib[6];
303 char *buf, *next, *lim;
304 register struct rt_msghdr *rtm;
306 mib[0] = CTL_NET;
307 mib[1] = PF_ROUTE;
308 mib[2] = 0;
309 mib[3] = 0;
310 mib[4] = NET_RT_DUMP;
311 mib[5] = 0;
312 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
313 err(1, "sysctl: net.route.0.0.dump estimate");
316 if ((buf = malloc(needed)) == 0) {
317 err(2, "malloc(%lu)", (unsigned long)needed);
319 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
320 err(1, "sysctl: net.route.0.0.dump");
322 lim = buf + needed;
323 for (next = buf; next < lim; next += rtm->rtm_msglen) {
324 rtm = (struct rt_msghdr *)next;
325 np_rtentry(rtm);
328 #endif
330 static void
331 np_rtentry(rtm)
332 register struct rt_msghdr *rtm;
334 register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
335 #ifdef notdef
336 static int masks_done, banner_printed;
337 #endif
338 static int old_af;
339 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
341 #ifdef notdef
342 /* for the moment, netmasks are skipped over */
343 if (!banner_printed) {
344 printf("Netmasks:\n");
345 banner_printed = 1;
347 if (masks_done == 0) {
348 if (rtm->rtm_addrs != RTA_DST ) {
349 masks_done = 1;
350 af = sa->sa_family;
352 } else
353 #endif
354 af = sa->sa_family;
355 if (af != old_af) {
356 pr_family(af);
357 old_af = af;
359 if (rtm->rtm_addrs == RTA_DST)
360 p_sockaddr(sa, 0, 36);
361 else {
362 p_sockaddr(sa, rtm->rtm_flags, 16);
363 if (sa->sa_len == 0)
364 sa->sa_len = sizeof(long);
365 sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
366 p_sockaddr(sa, 0, 18);
368 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
369 putchar('\n');
372 static void
373 p_sockaddr(sa, flags, width)
374 struct sockaddr *sa;
375 int flags, width;
377 char workbuf[128], *cplim;
378 register char *cp = workbuf;
380 switch(sa->sa_family) {
381 case AF_INET:
383 register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
385 cp = (sin->sin_addr.s_addr == 0) ? "default" :
386 ((flags & RTF_HOST) ?
387 routename(sin->sin_addr.s_addr) :
388 netname(sin->sin_addr.s_addr, 0L));
389 break;
391 #ifdef NETNS
392 case AF_NS:
393 cp = ns_print(sa);
394 break;
395 #endif
396 case AF_LINK:
398 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
400 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
401 sdl->sdl_slen == 0)
402 (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
403 else switch (sdl->sdl_type) {
404 case IFT_ETHER:
406 register int i;
407 register u_char *lla = (u_char *)sdl->sdl_data +
408 sdl->sdl_nlen;
410 cplim = "";
411 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
412 cp += sprintf(cp, "%s%x", cplim, *lla);
413 cplim = ":";
415 cp = workbuf;
416 break;
418 default:
419 cp = link_ntoa(sdl);
420 break;
422 break;
425 default:
427 register u_char *s = (u_char *)sa->sa_data, *slim;
429 slim = sa->sa_len + (u_char *) sa;
430 cplim = cp + sizeof(workbuf) - 6;
431 cp += sprintf(cp, "(%d)", sa->sa_family);
432 while (s < slim && cp < cplim) {
433 cp += sprintf(cp, " %02x", *s++);
434 if (s < slim)
435 cp += sprintf(cp, "%02x", *s++);
437 cp = workbuf;
440 if (width < 0 )
441 printf("%s ", cp);
442 else {
443 if (nflag)
444 printf("%-*s ", width, cp);
445 else
446 printf("%-*.*s ", width, width, cp);
450 static void
451 p_flags(f, format)
452 register int f;
453 char *format;
455 char name[33], *flags;
456 register struct bits *p = bits;
458 for (flags = name; p->b_mask; p++)
459 if (p->b_mask & f)
460 *flags++ = p->b_val;
461 *flags = '\0';
462 printf(format, name);
465 static void
466 p_rtentry(rt)
467 register struct rtentry *rt;
469 static struct ifnet ifnet, *lastif;
470 static char name[16];
471 static char prettyname[9];
474 * Don't print protocol-cloned routes unless -a.
476 if(rt->rt_parent && !aflag)
477 return;
479 p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, WID_DST);
480 p_sockaddr(kgetsa(rt->rt_gateway), RTF_HOST, WID_GW);
481 p_flags(rt->rt_flags, "%-6.6s ");
482 printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
483 if (rt->rt_ifp) {
484 if (rt->rt_ifp != lastif) {
485 kget(rt->rt_ifp, ifnet);
486 kread((u_long)ifnet.if_name, name, 16);
487 lastif = rt->rt_ifp;
488 snprintf(prettyname, sizeof prettyname,
489 "%.6s%d", name, ifnet.if_unit);
491 if(rt->rt_rmx.rmx_expire) {
492 time_t expire_time;
494 if ((expire_time
495 =rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0)
496 printf(" %8.8s %6d%s", prettyname,
497 expire_time,
498 rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
499 } else {
500 printf(" %8.8s%s", prettyname,
501 rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
505 putchar('\n');
508 char *
509 routename(in)
510 u_long in;
512 register char *cp;
513 static char line[MAXHOSTNAMELEN + 1];
514 struct hostent *hp;
515 static char domain[MAXHOSTNAMELEN + 1];
516 static int first = 1;
518 if (first) {
519 first = 0;
520 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
521 (cp = index(domain, '.')))
522 (void) strcpy(domain, cp + 1);
523 else
524 domain[0] = 0;
526 cp = 0;
527 if (!nflag) {
528 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
529 AF_INET);
530 if (hp) {
531 if ((cp = index(hp->h_name, '.')) &&
532 !strcmp(cp + 1, domain))
533 *cp = 0;
534 cp = hp->h_name;
537 if (cp)
538 strncpy(line, cp, sizeof(line) - 1);
539 else {
540 #define C(x) ((x) & 0xff)
541 in = ntohl(in);
542 sprintf(line, "%u.%u.%u.%u",
543 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
545 return (line);
549 * Return the name of the network whose address is given.
550 * The address is assumed to be that of a net or subnet, not a host.
552 char *
553 netname(in, mask)
554 u_long in, mask;
556 char *cp = 0;
557 static char line[MAXHOSTNAMELEN + 1];
558 struct netent *np = 0;
559 u_long net;
560 register int i;
561 int subnetshift;
563 i = ntohl(in);
564 if (!nflag && i) {
565 if (mask == 0) {
566 if (IN_CLASSA(i)) {
567 mask = IN_CLASSA_NET;
568 subnetshift = 8;
569 } else if (IN_CLASSB(i)) {
570 mask = IN_CLASSB_NET;
571 subnetshift = 8;
572 } else {
573 mask = IN_CLASSC_NET;
574 subnetshift = 4;
577 * If there are more bits than the standard mask
578 * would suggest, subnets must be in use.
579 * Guess at the subnet mask, assuming reasonable
580 * width subnet fields.
582 while (i &~ mask)
583 mask = (long)mask >> subnetshift;
585 net = i & mask;
586 while ((mask & 1) == 0)
587 mask >>= 1, net >>= 1;
588 np = getnetbyaddr(net, AF_INET);
589 if (np)
590 cp = np->n_name;
592 if (cp)
593 strncpy(line, cp, sizeof(line) - 1);
594 else if ((i & 0xffffff) == 0)
595 sprintf(line, "%u", C(i >> 24));
596 else if ((i & 0xffff) == 0)
597 sprintf(line, "%u.%u", C(i >> 24) , C(i >> 16));
598 else if ((i & 0xff) == 0)
599 sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8));
600 else
601 sprintf(line, "%u.%u.%u.%u", C(i >> 24),
602 C(i >> 16), C(i >> 8), C(i));
603 return (line);
607 * Print routing statistics
609 void
610 rt_stats(off)
611 u_long off;
613 struct rtstat rtstat;
615 if (off == 0) {
616 printf("rtstat: symbol not in namelist\n");
617 return;
619 kread(off, (char *)&rtstat, sizeof (rtstat));
620 printf("routing:\n");
621 printf("\t%u bad routing redirect%s\n",
622 rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
623 printf("\t%u dynamically created route%s\n",
624 rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
625 printf("\t%u new gateway%s due to redirects\n",
626 rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
627 printf("\t%u destination%s found unreachable\n",
628 rtstat.rts_unreach, plural(rtstat.rts_unreach));
629 printf("\t%u use%s of a wildcard route\n",
630 rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
633 #ifdef NS
634 short ns_nullh[] = {0,0,0};
635 short ns_bh[] = {-1,-1,-1};
637 char *
638 ns_print(sa)
639 register struct sockaddr *sa;
641 register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
642 struct ns_addr work;
643 union { union ns_net net_e; u_long long_e; } net;
644 u_short port;
645 static char mybuf[50], cport[10], chost[25];
646 char *host = "";
647 register char *p; register u_char *q;
649 work = sns->sns_addr;
650 port = ntohs(work.x_port);
651 work.x_port = 0;
652 net.net_e = work.x_net;
653 if (ns_nullhost(work) && net.long_e == 0) {
654 if (port ) {
655 sprintf(mybuf, "*.%xH", port);
656 upHex(mybuf);
657 } else
658 sprintf(mybuf, "*.*");
659 return (mybuf);
662 if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
663 host = "any";
664 } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
665 host = "*";
666 } else {
667 q = work.x_host.c_host;
668 sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
669 q[0], q[1], q[2], q[3], q[4], q[5]);
670 for (p = chost; *p == '0' && p < chost + 12; p++)
671 continue;
672 host = p;
674 if (port)
675 sprintf(cport, ".%xH", htons(port));
676 else
677 *cport = 0;
679 sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
680 upHex(mybuf);
681 return(mybuf);
684 char *
685 ns_phost(sa)
686 struct sockaddr *sa;
688 register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
689 struct sockaddr_ns work;
690 static union ns_net ns_zeronet;
691 char *p;
693 work = *sns;
694 work.sns_addr.x_port = 0;
695 work.sns_addr.x_net = ns_zeronet;
697 p = ns_print((struct sockaddr *)&work);
698 if (strncmp("0H.", p, 3) == 0) p += 3;
699 return(p);
702 void
703 upHex(p0)
704 char *p0;
706 register char *p = p0;
707 for (; *p; p++) switch (*p) {
709 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
710 *p += ('A' - 'a');
713 #endif