]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/route.c
network_cmds-356.9.tar.gz
[apple/network_cmds.git] / netstat.tproj / route.c
1 /*
2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #include <stdint.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <sys/time.h>
65
66 #include <net/if.h>
67 #include <net/if_var.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/route.h>
71 #include <net/radix.h>
72
73 #include <netinet/in.h>
74
75 #include <sys/sysctl.h>
76
77 #include <arpa/inet.h>
78 #include <netdb.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include <err.h>
84 #include <time.h>
85 #include "netstat.h"
86
87 /* alignment constraint for routing socket */
88 #define ROUNDUP(a) \
89 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
90 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
91
92 /*
93 * Definitions for showing gateway flags.
94 */
95 struct bits {
96 uint32_t b_mask;
97 char b_val;
98 } bits[] = {
99 { RTF_UP, 'U' },
100 { RTF_GATEWAY, 'G' },
101 { RTF_HOST, 'H' },
102 { RTF_REJECT, 'R' },
103 { RTF_DYNAMIC, 'D' },
104 { RTF_MODIFIED, 'M' },
105 { RTF_MULTICAST,'m' },
106 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
107 { RTF_CLONING, 'C' },
108 { RTF_XRESOLVE, 'X' },
109 { RTF_LLINFO, 'L' },
110 { RTF_STATIC, 'S' },
111 { RTF_PROTO1, '1' },
112 { RTF_PROTO2, '2' },
113 { RTF_WASCLONED,'W' },
114 { RTF_PRCLONING,'c' },
115 { RTF_PROTO3, '3' },
116 { RTF_BLACKHOLE,'B' },
117 { RTF_BROADCAST,'b' },
118 { RTF_IFSCOPE, 'I' },
119 #ifdef RTF_IFREF
120 { RTF_IFREF, 'i' },
121 #endif /* RTF_IFREF */
122 { 0 }
123 };
124
125 typedef union {
126 uint32_t dummy; /* Helps align structure. */
127 struct sockaddr u_sa;
128 u_short u_data[128];
129 } sa_u;
130
131 static void ntreestuff __P((void));
132 static void np_rtentry __P((struct rt_msghdr2 *));
133 static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int));
134 static void p_flags __P((int, char *));
135 static uint32_t forgemask __P((uint32_t));
136 static void domask __P((char *, uint32_t, uint32_t));
137
138 /*
139 * Print routing tables.
140 */
141 void
142 routepr(uint32_t rtree)
143 {
144 printf("Routing tables\n");
145
146 if (dflag == 0) {
147 ntreestuff();
148 } else {
149 if (rtree == 0) {
150 printf("rt_tables: symbol not in namelist\n");
151 return;
152 }
153 }
154 }
155
156 /*
157 * Print address family header before a section of the routing table.
158 */
159 void
160 pr_family(int af)
161 {
162 char *afname;
163
164 switch (af) {
165 case AF_INET:
166 afname = "Internet";
167 break;
168 #ifdef INET6
169 case AF_INET6:
170 afname = "Internet6";
171 break;
172 #endif /*INET6*/
173 case AF_IPX:
174 afname = "IPX";
175 break;
176 default:
177 afname = NULL;
178 break;
179 }
180 if (afname)
181 printf("\n%s:\n", afname);
182 else
183 printf("\nProtocol Family %d:\n", af);
184 }
185
186 /* column widths; each followed by one space */
187 #ifndef INET6
188 #define WID_DST(af) 18 /* width of destination column */
189 #define WID_GW(af) 18 /* width of gateway column */
190 #define WID_IF(af) 7 /* width of netif column */
191 #else
192 #define WID_DST(af) \
193 ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 39: 18)) : 18)
194 #define WID_GW(af) \
195 ((af) == AF_INET6 ? (lflag ? 31 : (nflag ? 31 : 18)) : 18)
196 #define WID_IF(af) ((af) == AF_INET6 ? 8 : 7)
197 #endif /*INET6*/
198
199 /*
200 * Print header for routing table columns.
201 */
202 void
203 pr_rthdr(int af)
204 {
205
206 if (Aflag)
207 printf("%-8.8s ","Address");
208 if (af == AF_INET || lflag)
209 if (lflag)
210 printf("%-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s\n",
211 WID_DST(af), WID_DST(af), "Destination",
212 WID_GW(af), WID_GW(af), "Gateway",
213 "Flags", "Refs", "Use", "Mtu",
214 WID_IF(af), WID_IF(af), "Netif", "Expire");
215 else
216 printf("%-*.*s %-*.*s %-10.10s %6.6s %8.8s %*.*s %6s\n",
217 WID_DST(af), WID_DST(af), "Destination",
218 WID_GW(af), WID_GW(af), "Gateway",
219 "Flags", "Refs", "Use",
220 WID_IF(af), WID_IF(af), "Netif", "Expire");
221 else
222 printf("%-*.*s %-*.*s %-10.10s %8.8s %6s\n",
223 WID_DST(af), WID_DST(af), "Destination",
224 WID_GW(af), WID_GW(af), "Gateway",
225 "Flags", "Netif", "Expire");
226 }
227
228 static void
229 ntreestuff(void)
230 {
231 size_t needed;
232 int mib[6];
233 char *buf, *next, *lim;
234 struct rt_msghdr2 *rtm;
235
236 mib[0] = CTL_NET;
237 mib[1] = PF_ROUTE;
238 mib[2] = 0;
239 mib[3] = 0;
240 mib[4] = NET_RT_DUMP2;
241 mib[5] = 0;
242 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
243 err(1, "sysctl: net.route.0.0.dump estimate");
244 }
245
246 if ((buf = malloc(needed)) == 0) {
247 err(2, "malloc(%lu)", (unsigned long)needed);
248 }
249 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
250 err(1, "sysctl: net.route.0.0.dump");
251 }
252 lim = buf + needed;
253 for (next = buf; next < lim; next += rtm->rtm_msglen) {
254 rtm = (struct rt_msghdr2 *)next;
255 np_rtentry(rtm);
256 }
257 }
258
259 static void
260 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
261 {
262 int i;
263
264 for (i = 0; i < RTAX_MAX; i++) {
265 if (addrs & (1 << i)) {
266 rti_info[i] = sa;
267 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
268 } else {
269 rti_info[i] = NULL;
270 }
271 }
272 }
273
274 static void
275 np_rtentry(struct rt_msghdr2 *rtm)
276 {
277 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
278 struct sockaddr *rti_info[RTAX_MAX];
279 static int old_fam;
280 int fam = 0;
281 u_short lastindex = 0xffff;
282 static char ifname[IFNAMSIZ + 1];
283 sa_u addr, mask;
284
285 /*
286 * Don't print protocol-cloned routes unless -a.
287 */
288 if ((rtm->rtm_flags & RTF_WASCLONED) &&
289 (rtm->rtm_parentflags & RTF_PRCLONING) &&
290 !aflag) {
291 return;
292 }
293
294 fam = sa->sa_family;
295 if (af != AF_UNSPEC && af != fam)
296 return;
297 if (fam != old_fam) {
298 pr_family(fam);
299 pr_rthdr(fam);
300 old_fam = fam;
301 }
302 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
303 bzero(&addr, sizeof(addr));
304 if ((rtm->rtm_addrs & RTA_DST))
305 bcopy(rti_info[RTAX_DST], &addr, rti_info[RTAX_DST]->sa_len);
306 bzero(&mask, sizeof(mask));
307 if ((rtm->rtm_addrs & RTA_NETMASK))
308 bcopy(rti_info[RTAX_NETMASK], &mask, rti_info[RTAX_NETMASK]->sa_len);
309 p_sockaddr(&addr.u_sa, &mask.u_sa, rtm->rtm_flags,
310 WID_DST(addr.u_sa.sa_family));
311
312 p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
313 WID_GW(addr.u_sa.sa_family));
314
315 p_flags(rtm->rtm_flags, "%-10.10s ");
316
317 if (addr.u_sa.sa_family == AF_INET || lflag) {
318 printf("%6u %8u ", rtm->rtm_refcnt, (unsigned int)rtm->rtm_use);
319 if (lflag) {
320 if (rtm->rtm_rmx.rmx_mtu != 0)
321 printf("%6u ", rtm->rtm_rmx.rmx_mtu);
322 else
323 printf("%6s ", "");
324 }
325 }
326 if (rtm->rtm_index != lastindex) {
327 if_indextoname(rtm->rtm_index, ifname);
328 lastindex = rtm->rtm_index;
329 }
330 printf("%*.*s", WID_IF(addr.u_sa.sa_family),
331 WID_IF(addr.u_sa.sa_family), ifname);
332
333 if (rtm->rtm_rmx.rmx_expire) {
334 time_t expire_time;
335
336 if ((expire_time =
337 rtm->rtm_rmx.rmx_expire - time((time_t *)0)) > 0)
338 printf(" %6d", (int)expire_time);
339 }
340 putchar('\n');
341 }
342
343 static void
344 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
345 {
346 char workbuf[128], *cplim;
347 char *cp = workbuf;
348
349 switch(sa->sa_family) {
350 case AF_INET: {
351 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
352
353 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
354 mask &&
355 (ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) == 0L || mask->sa_len == 0))
356 cp = "default" ;
357 else if (flags & RTF_HOST)
358 cp = routename(sin->sin_addr.s_addr);
359 else if (mask)
360 cp = netname(sin->sin_addr.s_addr,
361 ntohl(((struct sockaddr_in *)mask)->
362 sin_addr.s_addr));
363 else
364 cp = netname(sin->sin_addr.s_addr, 0L);
365 break;
366 }
367
368 #ifdef INET6
369 case AF_INET6: {
370 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
371 struct in6_addr *in6 = &sa6->sin6_addr;
372
373 /*
374 * XXX: This is a special workaround for KAME kernels.
375 * sin6_scope_id field of SA should be set in the future.
376 */
377 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
378 IN6_IS_ADDR_MC_NODELOCAL(in6) ||
379 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
380 /* XXX: override is ok? */
381 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
382 *(u_short *)&in6->s6_addr[2] = 0;
383 }
384
385 if (flags & RTF_HOST)
386 cp = routename6(sa6);
387 else if (mask)
388 cp = netname6(sa6, mask);
389 else
390 cp = netname6(sa6, NULL);
391 break;
392 }
393 #endif /*INET6*/
394
395 case AF_LINK: {
396 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
397
398 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
399 sdl->sdl_slen == 0) {
400 (void) snprintf(workbuf, sizeof(workbuf), "link#%d", sdl->sdl_index);
401 } else {
402 switch (sdl->sdl_type) {
403
404 case IFT_ETHER: {
405 int i;
406 u_char *lla = (u_char *)sdl->sdl_data +
407 sdl->sdl_nlen;
408
409 cplim = "";
410 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
411 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "%s%x", cplim, *lla);
412 cplim = ":";
413 }
414 cp = workbuf;
415 break;
416 }
417
418 default:
419 cp = link_ntoa(sdl);
420 break;
421 }
422 }
423 break;
424 }
425
426 default: {
427 u_char *s = (u_char *)sa->sa_data, *slim;
428
429 slim = sa->sa_len + (u_char *) sa;
430 cplim = cp + sizeof(workbuf) - 6;
431 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "(%d)", sa->sa_family);
432 while (s < slim && cp < cplim) {
433 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), " %02x", *s++);
434 if (s < slim)
435 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "%02x", *s++);
436 }
437 cp = workbuf;
438 }
439 }
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);
447 }
448 }
449
450 static void
451 p_flags(int f, char *format)
452 {
453 char name[33], *flags;
454 struct bits *p = bits;
455
456 for (flags = name; p->b_mask; p++)
457 if (p->b_mask & f)
458 *flags++ = p->b_val;
459 *flags = '\0';
460 printf(format, name);
461 }
462
463 char *
464 routename(uint32_t in)
465 {
466 char *cp;
467 static char line[MAXHOSTNAMELEN];
468 struct hostent *hp;
469
470 cp = 0;
471 if (!nflag) {
472 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
473 AF_INET);
474 if (hp) {
475 cp = hp->h_name;
476 //### trimdomain(cp, strlen(cp));
477 }
478 }
479 if (cp) {
480 strncpy(line, cp, sizeof(line) - 1);
481 line[sizeof(line) - 1] = '\0';
482 } else {
483 #define C(x) ((x) & 0xff)
484 in = ntohl(in);
485 snprintf(line, sizeof(line), "%u.%u.%u.%u",
486 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
487 }
488 return (line);
489 }
490
491 static uint32_t
492 forgemask(uint32_t a)
493 {
494 uint32_t m;
495
496 if (IN_CLASSA(a))
497 m = IN_CLASSA_NET;
498 else if (IN_CLASSB(a))
499 m = IN_CLASSB_NET;
500 else
501 m = IN_CLASSC_NET;
502 return (m);
503 }
504
505 static void
506 domask(char *dst, uint32_t addr, uint32_t mask)
507 {
508 int b, i;
509
510 if (!mask || (forgemask(addr) == mask)) {
511 *dst = '\0';
512 return;
513 }
514 i = 0;
515 for (b = 0; b < 32; b++)
516 if (mask & (1 << b)) {
517 int bb;
518
519 i = b;
520 for (bb = b+1; bb < 32; bb++)
521 if (!(mask & (1 << bb))) {
522 i = -1; /* noncontig */
523 break;
524 }
525 break;
526 }
527 if (i == -1)
528 snprintf(dst, sizeof(dst), "&0x%x", mask);
529 else
530 snprintf(dst, sizeof(dst), "/%d", 32-i);
531 }
532
533 /*
534 * Return the name of the network whose address is given.
535 * The address is assumed to be that of a net or subnet, not a host.
536 */
537 char *
538 netname(uint32_t in, uint32_t mask)
539 {
540 char *cp = 0;
541 static char line[MAXHOSTNAMELEN];
542 struct netent *np = 0;
543 uint32_t net, omask, dmask;
544 uint32_t i;
545
546 i = ntohl(in);
547 dmask = forgemask(i);
548 omask = mask;
549 if (!nflag && i) {
550 net = i & dmask;
551 if (!(np = getnetbyaddr(i, AF_INET)) && net != i)
552 np = getnetbyaddr(net, AF_INET);
553 if (np) {
554 cp = np->n_name;
555 //### trimdomain(cp, strlen(cp));
556 }
557 }
558 if (cp)
559 strncpy(line, cp, sizeof(line) - 1);
560 else {
561 switch (dmask) {
562 case IN_CLASSA_NET:
563 if ((i & IN_CLASSA_HOST) == 0) {
564 snprintf(line, sizeof(line), "%u", C(i >> 24));
565 break;
566 }
567 /* FALLTHROUGH */
568 case IN_CLASSB_NET:
569 if ((i & IN_CLASSB_HOST) == 0) {
570 snprintf(line, sizeof(line), "%u.%u",
571 C(i >> 24), C(i >> 16));
572 break;
573 }
574 /* FALLTHROUGH */
575 case IN_CLASSC_NET:
576 if ((i & IN_CLASSC_HOST) == 0) {
577 snprintf(line, sizeof(line), "%u.%u.%u",
578 C(i >> 24), C(i >> 16), C(i >> 8));
579 break;
580 }
581 /* FALLTHROUGH */
582 default:
583 snprintf(line, sizeof(line), "%u.%u.%u.%u",
584 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
585 break;
586 }
587 }
588 domask(line+strlen(line), i, omask);
589 return (line);
590 }
591
592 #ifdef INET6
593 char *
594 netname6(struct sockaddr_in6 *sa6, struct sockaddr *sam)
595 {
596 static char line[MAXHOSTNAMELEN];
597 u_char *lim;
598 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
599 struct in6_addr *mask = sam ? &((struct sockaddr_in6 *)sam)->sin6_addr : 0;
600
601 if (sam && sam->sa_len == 0) {
602 masklen = 0;
603 } else if (mask) {
604 u_char *p = (u_char *)mask;
605 for (masklen = 0, lim = p + 16; p < lim; p++) {
606 switch (*p) {
607 case 0xff:
608 masklen += 8;
609 break;
610 case 0xfe:
611 masklen += 7;
612 break;
613 case 0xfc:
614 masklen += 6;
615 break;
616 case 0xf8:
617 masklen += 5;
618 break;
619 case 0xf0:
620 masklen += 4;
621 break;
622 case 0xe0:
623 masklen += 3;
624 break;
625 case 0xc0:
626 masklen += 2;
627 break;
628 case 0x80:
629 masklen += 1;
630 break;
631 case 0x00:
632 break;
633 default:
634 illegal ++;
635 break;
636 }
637 }
638 if (illegal)
639 fprintf(stderr, "illegal prefixlen\n");
640 } else {
641 masklen = 128;
642 }
643 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
644 return("default");
645
646 if (nflag)
647 flag |= NI_NUMERICHOST;
648 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
649 NULL, 0, flag);
650
651 if (nflag)
652 snprintf(&line[strlen(line)], sizeof(line) - strlen(line), "/%d", masklen);
653
654 return line;
655 }
656
657 char *
658 routename6(struct sockaddr_in6 *sa6)
659 {
660 static char line[MAXHOSTNAMELEN];
661 int flag = NI_WITHSCOPEID;
662 /* use local variable for safety */
663 struct sockaddr_in6 sa6_local = {sizeof(sa6_local), AF_INET6, };
664
665 sa6_local.sin6_addr = sa6->sin6_addr;
666 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
667
668 if (nflag)
669 flag |= NI_NUMERICHOST;
670
671 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
672 line, sizeof(line), NULL, 0, flag);
673
674 return line;
675 }
676 #endif /*INET6*/
677
678 /*
679 * Print routing statistics
680 */
681 void
682 rt_stats(void)
683 {
684 struct rtstat rtstat;
685 int rttrash;
686 int mib[6];
687 size_t len;
688
689 mib[0] = CTL_NET;
690 mib[1] = AF_ROUTE;
691 mib[2] = 0;
692 mib[3] = 0;
693 mib[4] = NET_RT_STAT;
694 mib[5] = 0;
695 len = sizeof(struct rtstat);
696 if (sysctl(mib, 6, &rtstat, &len, 0, 0) == -1)
697 return;
698
699 mib[0] = CTL_NET;
700 mib[1] = AF_ROUTE;
701 mib[2] = 0;
702 mib[3] = 0;
703 mib[4] = NET_RT_TRASH;
704 mib[5] = 0;
705 len = sizeof(rttrash);
706 if (sysctl(mib, 6, &rttrash, &len, 0, 0) == -1)
707 return;
708
709 printf("routing:\n");
710
711 #define p(f, m) if (rtstat.f || sflag <= 1) \
712 printf(m, rtstat.f, plural(rtstat.f))
713
714 p(rts_badredirect, "\t%u bad routing redirect%s\n");
715 p(rts_dynamic, "\t%u dynamically created route%s\n");
716 p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
717 p(rts_unreach, "\t%u destination%s found unreachable\n");
718 p(rts_wildcard, "\t%u use%s of a wildcard route\n");
719 #undef p
720
721 if (rttrash || sflag <= 1)
722 printf("\t%u route%s not in table but not freed\n",
723 rttrash, plural(rttrash));
724 }
725
726 void
727 upHex(char *p0)
728 {
729 char *p = p0;
730
731 for (; *p; p++)
732 switch (*p) {
733
734 case 'a':
735 case 'b':
736 case 'c':
737 case 'd':
738 case 'e':
739 case 'f':
740 *p += ('A' - 'a');
741 break;
742 }
743 }