]> git.cameronkatri.com Git - apple_cmds.git/blob - network_cmds/netstat.tproj/route.c
network_cmds: Update to 606.100.3
[apple_cmds.git] / network_cmds / netstat.tproj / route.c
1 /*
2 * Copyright (c) 2008-2017 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 #include <sys/errno.h>
66
67 #include <net/if.h>
68 #include <net/if_var.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
72 #include <net/radix.h>
73
74 #include <netinet/in.h>
75
76 #include <sys/sysctl.h>
77
78 #include <arpa/inet.h>
79 #include <netdb.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <unistd.h>
84 #include <err.h>
85 #include <time.h>
86 #include "netstat.h"
87
88 /* alignment constraint for routing socket */
89 #define ROUNDUP(a) \
90 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
91 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
92
93 /*
94 * Definitions for showing gateway flags.
95 */
96 struct bits {
97 uint32_t b_mask;
98 char b_val;
99 } bits[] = {
100 { RTF_UP, 'U' },
101 { RTF_GATEWAY, 'G' },
102 { RTF_HOST, 'H' },
103 { RTF_REJECT, 'R' },
104 { RTF_DYNAMIC, 'D' },
105 { RTF_MODIFIED, 'M' },
106 { RTF_MULTICAST,'m' },
107 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
108 { RTF_CLONING, 'C' },
109 { RTF_XRESOLVE, 'X' },
110 { RTF_LLINFO, 'L' },
111 { RTF_STATIC, 'S' },
112 { RTF_PROTO1, '1' },
113 { RTF_PROTO2, '2' },
114 { RTF_WASCLONED,'W' },
115 { RTF_PRCLONING,'c' },
116 { RTF_PROTO3, '3' },
117 { RTF_BLACKHOLE,'B' },
118 { RTF_BROADCAST,'b' },
119 { RTF_IFSCOPE, 'I' },
120 { RTF_IFREF, 'i' },
121 { RTF_PROXY, 'Y' },
122 { RTF_ROUTER, 'r' },
123 #ifdef RTF_GLOBAL
124 { RTF_GLOBAL, 'g' },
125 #endif /* RTF_GLOBAL */
126 { 0 }
127 };
128
129 typedef union {
130 uint32_t dummy; /* Helps align structure. */
131 struct sockaddr u_sa;
132 u_short u_data[128];
133 } sa_u;
134
135 static void np_rtentry __P((struct rt_msghdr2 *));
136 static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int));
137 static void p_flags __P((int, char *));
138 static uint32_t forgemask __P((uint32_t));
139 static void domask __P((char *, uint32_t, uint32_t));
140
141 /*
142 * Print address family header before a section of the routing table.
143 */
144 void
145 pr_family(int af)
146 {
147 char *afname;
148
149 switch (af) {
150 case AF_INET:
151 afname = "Internet";
152 break;
153 #ifdef INET6
154 case AF_INET6:
155 afname = "Internet6";
156 break;
157 #endif /*INET6*/
158 case AF_IPX:
159 afname = "IPX";
160 break;
161 default:
162 afname = NULL;
163 break;
164 }
165 if (afname)
166 printf("\n%s:\n", afname);
167 else
168 printf("\nProtocol Family %d:\n", af);
169 }
170
171 /* column widths; each followed by one space */
172 #ifndef INET6
173 #define WID_DST(af) 18 /* width of destination column */
174 #define WID_GW(af) 18 /* width of gateway column */
175 #define WID_RT_IFA(af) 18 /* width of source column */
176 #define WID_IF(af) 7 /* width of netif column */
177 #else
178 #define WID_DST(af) \
179 ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 39: 18)) : 18)
180 #define WID_GW(af) \
181 ((af) == AF_INET6 ? (lflag ? 31 : (nflag ? 31 : 18)) : 18)
182 #define WID_RT_IFA(af) \
183 ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 39 : 18)) : 18)
184 #define WID_IF(af) ((af) == AF_INET6 ? 8 : 7)
185 #endif /*INET6*/
186
187 /*
188 * Print header for routing table columns.
189 */
190 void
191 pr_rthdr(int af)
192 {
193 if (lflag) {
194 if (lflag > 2)
195 printf("%-*.*s %-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s "
196 "%10s %10s %8s %8s %8s\n",
197 WID_DST(af), WID_DST(af), "Destination",
198 WID_GW(af), WID_GW(af), "Gateway",
199 WID_RT_IFA(af), WID_RT_IFA(af), "RT_IFA",
200 "Flags", "Refs", "Use", "Mtu",
201 WID_IF(af), WID_IF(af), "Netif", "Expire",
202 "rtt(ms)", "rttvar(ms)", "recvpipe", "sendpipe", "ssthresh");
203 else if (lflag > 1)
204 printf("%-*.*s %-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s "
205 "%10s %10s\n",
206 WID_DST(af), WID_DST(af), "Destination",
207 WID_GW(af), WID_GW(af), "Gateway",
208 WID_RT_IFA(af), WID_RT_IFA(af), "RT_IFA",
209 "Flags", "Refs", "Use", "Mtu",
210 WID_IF(af), WID_IF(af), "Netif", "Expire",
211 "rtt(ms)", "rttvar(ms)");
212 else
213 printf("%-*.*s %-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s\n",
214 WID_DST(af), WID_DST(af), "Destination",
215 WID_GW(af), WID_GW(af), "Gateway",
216 WID_RT_IFA(af), WID_RT_IFA(af), "RT_IFA",
217 "Flags", "Refs", "Use", "Mtu",
218 WID_IF(af), WID_IF(af), "Netif", "Expire");
219 } else {
220 printf("%-*.*s %-*.*s %-10.10s %*.*s %6s\n",
221 WID_DST(af), WID_DST(af), "Destination",
222 WID_GW(af), WID_GW(af), "Gateway",
223 "Flags", WID_IF(af), WID_IF(af), "Netif", "Expire");
224 }
225 }
226
227 /*
228 * Print routing tables.
229 */
230 void
231 routepr(void)
232 {
233 size_t extra_space;
234 size_t needed;
235 int mib[6];
236 char *buf, *next, *lim;
237 struct rt_msghdr2 *rtm;
238 int try = 1;
239
240 printf("Routing tables\n");
241
242 again:
243 mib[0] = CTL_NET;
244 mib[1] = PF_ROUTE;
245 mib[2] = 0;
246 mib[3] = 0;
247 mib[4] = NET_RT_DUMP2;
248 mib[5] = 0;
249 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
250 err(1, "sysctl: net.route.0.0.dump estimate");
251 }
252 /* allocate extra space in case the table grows */
253 extra_space = needed / 2;
254 if (needed <= (SIZE_MAX - extra_space)) {
255 needed += extra_space;
256 }
257 if ((buf = malloc(needed)) == 0) {
258 err(2, "malloc(%lu)", (unsigned long)needed);
259 }
260 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
261 #define MAX_TRIES 10
262 if (errno == ENOMEM && try < MAX_TRIES) {
263 /* the buffer we provided was too small, try again */
264 free(buf);
265 try++;
266 goto again;
267 }
268 err(1, "sysctl: net.route.0.0.dump");
269 }
270 lim = buf + needed;
271 for (next = buf; next < lim; next += rtm->rtm_msglen) {
272 rtm = (struct rt_msghdr2 *)next;
273 np_rtentry(rtm);
274 }
275 }
276
277 static void
278 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
279 {
280 int i;
281
282 for (i = 0; i < RTAX_MAX; i++) {
283 if (addrs & (1 << i)) {
284 rti_info[i] = sa;
285 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
286 } else {
287 rti_info[i] = NULL;
288 }
289 }
290 }
291
292 static void
293 np_rtentry(struct rt_msghdr2 *rtm)
294 {
295 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
296 struct sockaddr *rti_info[RTAX_MAX];
297 static int old_fam;
298 int fam = 0;
299 u_short lastindex = 0xffff;
300 static char ifname[IFNAMSIZ + 1];
301 sa_u addr, mask;
302
303 /*
304 * Don't print protocol-cloned routes unless -a.
305 */
306 if ((rtm->rtm_flags & RTF_WASCLONED) &&
307 (rtm->rtm_parentflags & RTF_PRCLONING) &&
308 !aflag) {
309 return;
310 }
311
312 if (lflag > 1 && zflag != 0 && rtm->rtm_rmx.rmx_rtt == 0 && rtm->rtm_rmx.rmx_rttvar == 0)
313 return;
314 fam = sa->sa_family;
315 if (af != AF_UNSPEC && af != fam)
316 return;
317 if (fam != old_fam) {
318 pr_family(fam);
319 pr_rthdr(fam);
320 old_fam = fam;
321 }
322 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
323 bzero(&addr, sizeof(addr));
324 if ((rtm->rtm_addrs & RTA_DST))
325 bcopy(rti_info[RTAX_DST], &addr, rti_info[RTAX_DST]->sa_len);
326 bzero(&mask, sizeof(mask));
327 if ((rtm->rtm_addrs & RTA_NETMASK))
328 bcopy(rti_info[RTAX_NETMASK], &mask, rti_info[RTAX_NETMASK]->sa_len);
329 p_sockaddr(&addr.u_sa, &mask.u_sa, rtm->rtm_flags,
330 WID_DST(addr.u_sa.sa_family));
331
332 p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
333 WID_GW(addr.u_sa.sa_family));
334
335 if (lflag && (rtm->rtm_addrs & RTA_IFA)) {
336 p_sockaddr(rti_info[RTAX_IFA], NULL, RTF_HOST,
337 WID_RT_IFA(addr.u_sa.sa_family));
338 }
339
340 p_flags(rtm->rtm_flags, "%-10.10s ");
341
342 if (lflag) {
343 printf("%6u %8u ", rtm->rtm_refcnt, (unsigned int)rtm->rtm_use);
344 if (rtm->rtm_rmx.rmx_mtu != 0)
345 printf("%6u ", rtm->rtm_rmx.rmx_mtu);
346 else
347 printf("%6s ", "");
348 }
349
350 if (rtm->rtm_index != lastindex) {
351 if_indextoname(rtm->rtm_index, ifname);
352 lastindex = rtm->rtm_index;
353 }
354 printf("%*.*s", WID_IF(addr.u_sa.sa_family),
355 WID_IF(addr.u_sa.sa_family), ifname);
356
357 if (rtm->rtm_rmx.rmx_expire) {
358 time_t expire_time;
359
360 if ((expire_time =
361 rtm->rtm_rmx.rmx_expire - time((time_t *)0)) > 0)
362 printf(" %6d", (int)expire_time);
363 else
364 printf(" %6s", "!");
365 } else {
366 printf(" %6s", "");
367 }
368 if (lflag > 1) {
369 if (rtm->rtm_rmx.rmx_rtt != 0)
370 printf(" %6u.%03u", rtm->rtm_rmx.rmx_rtt / 1000,
371 rtm->rtm_rmx.rmx_rtt % 1000);
372 else
373 printf(" %10s", "");
374 if (rtm->rtm_rmx.rmx_rttvar != 0)
375 printf(" %6u.%03u", rtm->rtm_rmx.rmx_rttvar / 1000,
376 rtm->rtm_rmx.rmx_rttvar % 1000);
377 else
378 printf(" %10s", "");
379 if (lflag > 2) {
380 if (rtm->rtm_rmx.rmx_recvpipe != 0)
381 printf(" %8u", rtm->rtm_rmx.rmx_recvpipe);
382 else
383 printf(" %8s", "");
384 if (rtm->rtm_rmx.rmx_sendpipe != 0)
385 printf(" %8u", rtm->rtm_rmx.rmx_sendpipe);
386 else
387 printf(" %8s", "");
388 if (rtm->rtm_rmx.rmx_ssthresh != 0)
389 printf(" %8u", rtm->rtm_rmx.rmx_ssthresh);
390 else
391 printf(" %8s", "");
392 }
393 }
394 putchar('\n');
395 }
396
397 static void
398 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
399 {
400 char workbuf[128], *cplim;
401 char *cp = workbuf;
402
403 switch(sa->sa_family) {
404 case AF_INET: {
405 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
406
407 if ((sin->sin_addr.s_addr == INADDR_ANY) &&
408 mask &&
409 (ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) == 0L || mask->sa_len == 0))
410 cp = "default" ;
411 else if (flags & RTF_HOST)
412 cp = routename(sin->sin_addr.s_addr);
413 else if (mask)
414 cp = netname(sin->sin_addr.s_addr,
415 ntohl(((struct sockaddr_in *)mask)->
416 sin_addr.s_addr));
417 else
418 cp = netname(sin->sin_addr.s_addr, 0L);
419 break;
420 }
421
422 #ifdef INET6
423 case AF_INET6: {
424 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
425 struct in6_addr *in6 = &sa6->sin6_addr;
426
427 /*
428 * XXX: This is a special workaround for KAME kernels.
429 * sin6_scope_id field of SA should be set in the future.
430 */
431 if (IN6_IS_ADDR_LINKLOCAL(in6) ||
432 IN6_IS_ADDR_MC_NODELOCAL(in6) ||
433 IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
434 /* XXX: override is ok? */
435 sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
436 *(u_short *)&in6->s6_addr[2] = 0;
437 }
438
439 if (flags & RTF_HOST)
440 cp = routename6(sa6);
441 else if (mask)
442 cp = netname6(sa6, mask);
443 else
444 cp = netname6(sa6, NULL);
445 break;
446 }
447 #endif /*INET6*/
448
449 case AF_LINK: {
450 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
451
452 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
453 sdl->sdl_slen == 0) {
454 (void) snprintf(workbuf, sizeof(workbuf), "link#%d", sdl->sdl_index);
455 } else {
456 switch (sdl->sdl_type) {
457
458 case IFT_ETHER: {
459 int i;
460 u_char *lla = (u_char *)sdl->sdl_data +
461 sdl->sdl_nlen;
462
463 cplim = "";
464 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
465 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "%s%x", cplim, *lla);
466 cplim = ":";
467 }
468 cp = workbuf;
469 break;
470 }
471
472 default:
473 cp = link_ntoa(sdl);
474 break;
475 }
476 }
477 break;
478 }
479
480 default: {
481 u_char *s = (u_char *)sa->sa_data, *slim;
482
483 slim = sa->sa_len + (u_char *) sa;
484 cplim = cp + sizeof(workbuf) - 6;
485 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "(%d)", sa->sa_family);
486 while (s < slim && cp < cplim) {
487 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), " %02x", *s++);
488 if (s < slim)
489 cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "%02x", *s++);
490 }
491 cp = workbuf;
492 }
493 }
494 if (width < 0 ) {
495 printf("%s ", cp);
496 } else {
497 if (nflag)
498 printf("%-*s ", width, cp);
499 else
500 printf("%-*.*s ", width, width, cp);
501 }
502 }
503
504 static void
505 p_flags(int f, char *format)
506 {
507 char name[33], *flags;
508 struct bits *p = bits;
509
510 for (flags = name; p->b_mask; p++)
511 if (p->b_mask & f)
512 *flags++ = p->b_val;
513 *flags = '\0';
514 printf(format, name);
515 }
516
517 char *
518 routename(uint32_t in)
519 {
520 char *cp;
521 static char line[MAXHOSTNAMELEN];
522 struct hostent *hp;
523
524 cp = 0;
525 if (!nflag) {
526 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
527 AF_INET);
528 if (hp) {
529 cp = hp->h_name;
530 //### trimdomain(cp, strlen(cp));
531 }
532 }
533 if (cp) {
534 strlcpy(line, cp, sizeof(line));
535 } else {
536 #define C(x) ((x) & 0xff)
537 in = ntohl(in);
538 snprintf(line, sizeof(line), "%u.%u.%u.%u",
539 C(in >> 24), C(in >> 16), C(in >> 8), C(in));
540 }
541 return (line);
542 }
543
544 static uint32_t
545 forgemask(uint32_t a)
546 {
547 uint32_t m;
548
549 if (IN_CLASSA(a))
550 m = IN_CLASSA_NET;
551 else if (IN_CLASSB(a))
552 m = IN_CLASSB_NET;
553 else
554 m = IN_CLASSC_NET;
555 return (m);
556 }
557
558 static void
559 domask(char *dst, uint32_t addr, uint32_t mask)
560 {
561 int b, i;
562
563 if (!mask || (forgemask(addr) == mask)) {
564 *dst = '\0';
565 return;
566 }
567 i = 0;
568 for (b = 0; b < 32; b++)
569 if (mask & (1 << b)) {
570 int bb;
571
572 i = b;
573 for (bb = b+1; bb < 32; bb++)
574 if (!(mask & (1 << bb))) {
575 i = -1; /* noncontig */
576 break;
577 }
578 break;
579 }
580 if (i == -1)
581 snprintf(dst, sizeof(dst), "&0x%x", mask);
582 else
583 snprintf(dst, sizeof(dst), "/%d", 32-i);
584 }
585
586 /*
587 * Return the name of the network whose address is given.
588 * The address is assumed to be that of a net or subnet, not a host.
589 */
590 char *
591 netname(uint32_t in, uint32_t mask)
592 {
593 char *cp = 0;
594 static char line[MAXHOSTNAMELEN];
595 struct netent *np = 0;
596 uint32_t net, omask, dmask;
597 uint32_t i;
598
599 i = ntohl(in);
600 dmask = forgemask(i);
601 omask = mask;
602 if (!nflag && i) {
603 net = i & dmask;
604 if (!(np = getnetbyaddr(i, AF_INET)) && net != i)
605 np = getnetbyaddr(net, AF_INET);
606 if (np) {
607 cp = np->n_name;
608 //### trimdomain(cp, strlen(cp));
609 }
610 }
611 if (cp)
612 strlcpy(line, cp, sizeof(line));
613 else {
614 switch (dmask) {
615 case IN_CLASSA_NET:
616 if ((i & IN_CLASSA_HOST) == 0) {
617 snprintf(line, sizeof(line), "%u", C(i >> 24));
618 break;
619 }
620 /* FALLTHROUGH */
621 case IN_CLASSB_NET:
622 if ((i & IN_CLASSB_HOST) == 0) {
623 snprintf(line, sizeof(line), "%u.%u",
624 C(i >> 24), C(i >> 16));
625 break;
626 }
627 /* FALLTHROUGH */
628 case IN_CLASSC_NET:
629 if ((i & IN_CLASSC_HOST) == 0) {
630 snprintf(line, sizeof(line), "%u.%u.%u",
631 C(i >> 24), C(i >> 16), C(i >> 8));
632 break;
633 }
634 /* FALLTHROUGH */
635 default:
636 snprintf(line, sizeof(line), "%u.%u.%u.%u",
637 C(i >> 24), C(i >> 16), C(i >> 8), C(i));
638 break;
639 }
640 }
641 domask(line+strlen(line), i, omask);
642 return (line);
643 }
644
645 #ifdef INET6
646 char *
647 netname6(struct sockaddr_in6 *sa6, struct sockaddr *sam)
648 {
649 static char line[MAXHOSTNAMELEN];
650 u_char *lim;
651 int masklen, illegal = 0, flag = NI_WITHSCOPEID;
652 struct in6_addr *mask = sam ? &((struct sockaddr_in6 *)sam)->sin6_addr : 0;
653
654 if (sam && sam->sa_len == 0) {
655 masklen = 0;
656 } else if (mask) {
657 u_char *p = (u_char *)mask;
658 for (masklen = 0, lim = p + 16; p < lim; p++) {
659 switch (*p) {
660 case 0xff:
661 masklen += 8;
662 break;
663 case 0xfe:
664 masklen += 7;
665 break;
666 case 0xfc:
667 masklen += 6;
668 break;
669 case 0xf8:
670 masklen += 5;
671 break;
672 case 0xf0:
673 masklen += 4;
674 break;
675 case 0xe0:
676 masklen += 3;
677 break;
678 case 0xc0:
679 masklen += 2;
680 break;
681 case 0x80:
682 masklen += 1;
683 break;
684 case 0x00:
685 break;
686 default:
687 illegal ++;
688 break;
689 }
690 }
691 if (illegal)
692 fprintf(stderr, "illegal prefixlen\n");
693 } else {
694 masklen = 128;
695 }
696 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
697 return("default");
698
699 if (nflag)
700 flag |= NI_NUMERICHOST;
701 getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line),
702 NULL, 0, flag);
703
704 if (nflag)
705 snprintf(&line[strlen(line)], sizeof(line) - strlen(line), "/%d", masklen);
706
707 return line;
708 }
709
710 char *
711 routename6(struct sockaddr_in6 *sa6)
712 {
713 static char line[MAXHOSTNAMELEN];
714 int flag = NI_WITHSCOPEID;
715 /* use local variable for safety */
716 struct sockaddr_in6 sa6_local = {sizeof(sa6_local), AF_INET6, };
717
718 sa6_local.sin6_addr = sa6->sin6_addr;
719 sa6_local.sin6_scope_id = sa6->sin6_scope_id;
720
721 if (nflag)
722 flag |= NI_NUMERICHOST;
723
724 getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
725 line, sizeof(line), NULL, 0, flag);
726
727 return line;
728 }
729 #endif /*INET6*/
730
731 /*
732 * Print routing statistics
733 */
734 void
735 rt_stats(void)
736 {
737 struct rtstat rtstat;
738 int rttrash;
739 int mib[6];
740 size_t len;
741
742 mib[0] = CTL_NET;
743 mib[1] = AF_ROUTE;
744 mib[2] = 0;
745 mib[3] = 0;
746 mib[4] = NET_RT_STAT;
747 mib[5] = 0;
748 len = sizeof(struct rtstat);
749 if (sysctl(mib, 6, &rtstat, &len, 0, 0) == -1)
750 return;
751
752 mib[0] = CTL_NET;
753 mib[1] = AF_ROUTE;
754 mib[2] = 0;
755 mib[3] = 0;
756 mib[4] = NET_RT_TRASH;
757 mib[5] = 0;
758 len = sizeof(rttrash);
759 if (sysctl(mib, 6, &rttrash, &len, 0, 0) == -1)
760 return;
761
762 printf("routing:\n");
763
764 #define p(f, m) if (rtstat.f || sflag <= 1) \
765 printf(m, rtstat.f, plural(rtstat.f))
766
767 p(rts_badredirect, "\t%u bad routing redirect%s\n");
768 p(rts_dynamic, "\t%u dynamically created route%s\n");
769 p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
770 p(rts_unreach, "\t%u destination%s found unreachable\n");
771 p(rts_wildcard, "\t%u use%s of a wildcard route\n");
772 p(rts_badrtgwroute, "\t%u lookup%s returned indirect "
773 "routes pointing to indirect gateway route\n");
774 #undef p
775
776 if (rttrash || sflag <= 1)
777 printf("\t%u route%s not in table but not freed\n",
778 rttrash, plural(rttrash));
779 }
780
781 void
782 upHex(char *p0)
783 {
784 char *p = p0;
785
786 for (; *p; p++)
787 switch (*p) {
788
789 case 'a':
790 case 'b':
791 case 'c':
792 case 'd':
793 case 'e':
794 case 'f':
795 *p += ('A' - 'a');
796 break;
797 }
798 }