Remove building with NOCRYPTO option
[minix3.git] / sbin / route / rtutil.c
blobf7cfbacfb91acdff290b3cfd9173d6a0604b0409
1 /* $NetBSD: rtutil.c,v 1.6 2015/03/23 18:33:17 roy Exp $ */
2 /* $OpenBSD: show.c,v 1.1 2006/05/27 19:16:37 claudio Exp $ */
4 /*
5 * Copyright (c) 1983, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
36 #include <sys/mbuf.h>
37 #include <sys/sysctl.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_types.h>
42 #include <net/pfvar.h>
43 #include <net/pfkeyv2.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47 #include <netatalk/at.h>
48 #include <netmpls/mpls.h>
49 #include <arpa/inet.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <netdb.h>
54 #include <stdio.h>
55 #include <stddef.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
60 #include "prog_ops.h"
61 #include "rtutil.h"
64 #define PLEN (LONG_BIT / 4 + 2)
65 #define PFKEYV2_CHUNK sizeof(u_int64_t)
66 static char *link_print(const struct sockaddr *);
69 * Definitions for showing gateway flags.
71 struct bits {
72 int b_mask;
73 char b_val;
75 static const struct bits bits[] = {
76 { RTF_UP, 'U' },
77 { RTF_GATEWAY, 'G' },
78 { RTF_HOST, 'H' },
79 { RTF_REJECT, 'R' },
80 { RTF_BLACKHOLE, 'B' },
81 { RTF_DYNAMIC, 'D' },
82 { RTF_MODIFIED, 'M' },
83 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
84 { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
85 { RTF_CLONING, 'C' },
86 { RTF_XRESOLVE, 'X' },
87 { RTF_LLINFO, 'L' },
88 { RTF_STATIC, 'S' },
89 { RTF_PROTO1, '1' },
90 { RTF_PROTO2, '2' },
91 /* { RTF_PROTO3, '3' }, */
92 { RTF_CLONED, 'c' },
93 /* { RTF_JUMBO, 'J' }, */
94 { RTF_ANNOUNCE, 'p' },
95 { RTF_LOCAL, 'l'},
96 { RTF_BROADCAST, 'b'},
97 { 0, 0 }
100 #ifndef SMALL
101 static void p_tag(const struct sockaddr *sa);
102 #endif
103 static void p_rtentry(struct rt_msghdr *, int, int);
106 * Print routing tables.
108 void
109 p_rttables(int paf, int flags, int pflags, int interesting)
111 struct rt_msghdr *rtm;
112 char *buf = NULL, *next, *lim = NULL;
113 size_t needed;
114 int mib[6];
115 struct sockaddr *sa;
117 mib[0] = CTL_NET;
118 mib[1] = PF_ROUTE;
119 mib[2] = 0;
120 mib[3] = paf;
121 mib[4] = NET_RT_DUMP;
122 mib[5] = 0;
123 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
124 err(1, "route-sysctl-estimate");
125 if (needed > 0) {
126 if ((buf = malloc(needed)) == 0)
127 err(1, NULL);
128 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
129 err(1, "sysctl of routing table");
130 lim = buf + needed;
133 printf("Routing tables\n");
135 if (buf) {
136 for (next = buf; next < lim; next += rtm->rtm_msglen) {
137 rtm = (struct rt_msghdr *)next;
138 sa = (struct sockaddr *)(rtm + 1);
139 if ((rtm->rtm_flags & pflags) != pflags)
140 continue;
141 if (paf != AF_UNSPEC && sa->sa_family != paf)
142 continue;
143 p_rtentry(rtm, flags, interesting);
145 free(buf);
146 buf = NULL;
149 if (paf != 0 && paf != PF_KEY)
150 return;
152 #if 0 /* XXX-elad */
153 mib[0] = CTL_NET;
154 mib[1] = PF_KEY;
155 mib[2] = PF_KEY_V2;
156 mib[3] = NET_KEY_SPD_DUMP;
157 mib[4] = mib[5] = 0;
159 if (prog_sysctl(mib, 4, NULL, &needed, NULL, 0) == -1) {
160 if (errno == ENOPROTOOPT)
161 return;
162 err(1, "spd-sysctl-estimate");
164 if (needed > 0) {
165 if ((buf = malloc(needed)) == 0)
166 err(1, NULL);
167 if (prog_sysctl(mib, 4, buf, &needed, NULL, 0) == -1)
168 err(1,"sysctl of spd");
169 lim = buf + needed;
172 if (buf) {
173 printf("\nEncap:\n");
175 for (next = buf; next < lim; next += msg->sadb_msg_len *
176 PFKEYV2_CHUNK) {
177 msg = (struct sadb_msg *)next;
178 if (msg->sadb_msg_len == 0)
179 break;
180 p_pfkentry(msg);
182 free(buf);
183 buf = NULL;
185 #endif /* 0 */
189 * column widths; each followed by one space
190 * width of destination/gateway column
191 * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 = 34
192 * strlen("aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh") == 39
194 #ifndef INET6
195 #define WID_DST(af) 18 /* width of destination column */
196 #define WID_GW(af) 18 /* width of gateway column */
197 #else
198 #define WID_DST(af) ((af) == AF_INET6 ? ((flags & RT_NFLAG) ? 39 : 18) : 18)
199 #define WID_GW(af) ((af) == AF_INET6 ? ((flags & RT_NFLAG) ? 30 : 18) : 18)
200 #endif
203 * Print header for routing table columns.
205 void
206 p_rthdr(int paf, int flags)
208 #ifndef SMALL
209 if (flags & RT_AFLAG)
210 printf("%-*.*s ", PLEN, PLEN, "Address");
211 if (paf == PF_KEY) {
212 printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
213 "Source", "Port", "Destination",
214 "Port", "Proto", "SA(Address/Proto/Type/Direction)");
215 return;
217 if (flags & RT_TFLAG) {
218 printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %7.7s"
219 " %s\n", WID_DST(paf), WID_DST(paf), "Destination",
220 WID_GW(paf), WID_GW(paf), "Gateway",
221 "Flags", "Refs", "Use", "Mtu", "Tag", "Interface");
222 return;
224 #endif
225 #ifndef SMALL
226 printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %s\n",
227 WID_DST(paf), WID_DST(paf), "Destination",
228 WID_GW(paf), WID_GW(paf), "Gateway",
229 "Flags", "Refs", "Use", "Mtu", "Interface");
230 #else
231 printf("%-*.*s %-*.*s %-6.6s\n",
232 WID_DST(paf), WID_DST(paf), "Destination",
233 WID_GW(paf), WID_GW(paf), "Gateway",
234 "Flags");
235 #endif
238 static void
239 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
241 int i;
243 for (i = 0; i < RTAX_MAX; i++) {
244 if (addrs & (1 << i)) {
245 rti_info[i] = sa;
246 sa = (struct sockaddr *)((char *)(sa) +
247 RT_ROUNDUP(sa->sa_len));
248 } else
249 rti_info[i] = NULL;
254 * Print a routing table entry.
256 static void
257 p_rtentry(struct rt_msghdr *rtm, int flags, int interesting)
259 static int old_af = -1;
260 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
261 struct sockaddr *mask, *rti_info[RTAX_MAX];
262 #ifndef SMALL
263 char ifbuf[IF_NAMESIZE];
264 #endif
266 if ((flags & RT_LFLAG) && (rtm->rtm_flags & RTF_LLINFO))
267 return;
269 if (old_af != sa->sa_family) {
270 old_af = sa->sa_family;
271 p_family(sa->sa_family);
272 p_rthdr(sa->sa_family, flags);
274 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
276 mask = rti_info[RTAX_NETMASK];
277 if ((sa = rti_info[RTAX_DST]) == NULL)
278 return;
280 p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family), flags);
281 p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
282 WID_GW(sa->sa_family), flags);
283 p_flags(rtm->rtm_flags & interesting);
284 #if 0 /* XXX-elad */
285 printf("%6d %8"PRId64" ", (int)rtm->rtm_rmx.rmx_refcnt,
286 rtm->rtm_rmx.rmx_pksent);
287 #else
288 printf("%6s %8s ", "-", "-");
289 #endif
290 #ifndef SMALL
291 if (rtm->rtm_rmx.rmx_mtu)
292 printf("%6"PRId64, rtm->rtm_rmx.rmx_mtu);
293 else
294 printf("%6s", "-");
295 putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
296 if (flags & RT_TFLAG)
297 p_tag(rti_info[RTAX_TAG]);
298 printf(" %.16s", if_indextoname(rtm->rtm_index, ifbuf));
299 putchar('\n');
300 if (flags & RT_VFLAG)
301 p_rtrmx(&rtm->rtm_rmx);
302 #endif
306 * Print address family header before a section of the routing table.
308 void
309 p_family(int paf)
311 const char *afname;
313 switch (paf) {
314 case AF_INET:
315 afname = "Internet";
316 break;
317 #ifdef INET6
318 case AF_INET6:
319 afname = "Internet6";
320 break;
321 #endif
322 case PF_KEY:
323 afname = "Encap";
324 break;
325 case AF_APPLETALK:
326 afname = "AppleTalk";
327 break;
328 #ifndef SMALL
329 case AF_MPLS:
330 afname = "MPLS";
331 break;
332 #endif
333 default:
334 afname = NULL;
335 break;
337 if (afname)
338 printf("\n%s:\n", afname);
339 else
340 printf("\nProtocol Family %d:\n", paf);
343 void
344 p_sockaddr(const struct sockaddr *sa, const struct sockaddr *mask, int rflags,
345 int width, int flags)
347 char *cp;
349 switch (sa->sa_family) {
350 #ifdef INET6
351 case AF_INET6:
353 struct sockaddr_in6 sa6 = *(const struct sockaddr_in6 *)sa;
355 inet6_getscopeid(&sa6, INET6_IS_ADDR_LINKLOCAL|
356 INET6_IS_ADDR_MC_LINKLOCAL);
357 if (rflags & RTF_HOST)
358 cp = routename((const struct sockaddr *)&sa6, flags);
359 else
360 cp = netname((const struct sockaddr *)&sa6, mask, flags);
361 break;
363 #endif
364 default:
365 if ((rflags & RTF_HOST) || mask == NULL)
366 cp = routename(sa, flags);
367 else
368 cp = netname(sa, mask, flags);
369 break;
371 if (width < 0)
372 printf("%s", cp);
373 else {
374 if (flags & RT_NFLAG)
375 printf("%-*s ", width, cp);
376 else
377 printf("%-*.*s ", width, width, cp);
381 void
382 p_flags(int f)
384 char name[33], *flags;
385 const struct bits *p = bits;
387 for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
388 if (p->b_mask & f)
389 *flags++ = p->b_val;
390 *flags = '\0';
391 printf("%-6.6s ", name);
394 #ifndef SMALL
395 void
396 p_rtrmx(const struct rt_metrics *rmx)
398 printf("\texpire %10"PRId64"%c recvpipe %10"PRIu64"%c "
399 "sendpipe %10"PRIu64"%c\n",
400 (int64_t)rmx->rmx_expire,
401 (rmx->rmx_locks & RTV_EXPIRE) ? 'L' : ' ', rmx->rmx_recvpipe,
402 (rmx->rmx_locks & RTV_RPIPE) ? 'L' : ' ', rmx->rmx_sendpipe,
403 (rmx->rmx_locks & RTV_SPIPE) ? 'L' : ' ');
404 printf("\tssthresh %10"PRIu64"%c rtt %10"PRIu64"%c "
405 "rttvar %10"PRIu64"%c\n", rmx->rmx_ssthresh,
406 (rmx->rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
407 rmx->rmx_rtt, (rmx->rmx_locks & RTV_RTT) ? 'L' : ' ',
408 rmx->rmx_rttvar, (rmx->rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
409 printf("\thopcount %10"PRIu64"%c\n",
410 rmx->rmx_hopcount, (rmx->rmx_locks & RTV_HOPCOUNT) ? 'L' : ' ');
413 static void
414 p_tag(const struct sockaddr *sa)
416 char *line;
418 if (sa == NULL || sa->sa_family != AF_MPLS) {
419 printf("%7s", "-");
420 return;
422 line = mpls_ntoa(sa);
423 if (strlen(line) < 7)
424 printf("%7s", line);
425 else
426 printf("%s", line);
428 #endif
430 static char line[MAXHOSTNAMELEN];
431 static char domain[MAXHOSTNAMELEN];
433 char *
434 routename(const struct sockaddr *sa, int flags)
436 char *cp = NULL;
437 static int first = 1;
439 if (first) {
440 first = 0;
441 if (gethostname(domain, sizeof(domain)) == 0 &&
442 (cp = strchr(domain, '.')))
443 (void)strlcpy(domain, cp + 1, sizeof(domain));
444 else
445 domain[0] = '\0';
446 cp = NULL;
449 if (sa->sa_len == 0) {
450 (void)strlcpy(line, "default", sizeof(line));
451 return (line);
454 switch (sa->sa_family) {
455 case AF_INET:
456 return routename4(
457 ((const struct sockaddr_in *)sa)->sin_addr.s_addr,
458 flags);
459 #ifdef INET6
460 case AF_INET6:
462 struct sockaddr_in6 sin6;
464 memset(&sin6, 0, sizeof(sin6));
465 memcpy(&sin6, sa, sa->sa_len);
466 sin6.sin6_len = sizeof(struct sockaddr_in6);
467 sin6.sin6_family = AF_INET6;
468 if (sa->sa_len == sizeof(struct sockaddr_in6))
469 #if defined(__minix)
471 * Local MINIX3 fix for a NetBSD issue: inet6_getscopeid()
472 * is sometimes called twice in a row when printing routing
473 * tables, discarding the scope ID the second time..
475 if (sin6.sin6_scope_id == 0)
476 #endif /* defined(__minix) */
477 inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL|
478 INET6_IS_ADDR_MC_LINKLOCAL);
479 return routename6(&sin6, flags);
481 #endif
482 case AF_LINK:
483 return link_print(sa);
485 #ifndef SMALL
486 case AF_MPLS:
487 return mpls_ntoa(sa);
489 case AF_APPLETALK:
490 (void)snprintf(line, sizeof(line), "atalk %d.%d",
491 ((const struct sockaddr_at *)sa)->sat_addr.s_net,
492 ((const struct sockaddr_at *)sa)->sat_addr.s_node);
493 break;
494 #endif
496 #if 0 /* XXX-elad */
497 case AF_UNSPEC:
498 if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
499 static char name[RTLABEL_LEN];
500 struct sockaddr_rtlabel *sr;
502 sr = (struct sockaddr_rtlabel *)sa;
503 strlcpy(name, sr->sr_label, sizeof(name));
504 return (name);
506 /* FALLTHROUGH */
507 #endif
508 default:
509 (void)snprintf(line, sizeof(line), "(%d) %s",
510 sa->sa_family, any_ntoa(sa));
511 break;
513 return (line);
516 char *
517 routename4(in_addr_t in, int flags)
519 const char *cp = NULL;
520 struct in_addr ina;
521 struct hostent *hp;
523 if (in == INADDR_ANY)
524 cp = "default";
525 if (!cp && (flags & RT_NFLAG) == 0) {
526 if ((hp = gethostbyaddr((char *)&in,
527 sizeof(in), AF_INET)) != NULL) {
528 char *p;
529 if ((p = strchr(hp->h_name, '.')) &&
530 !strcmp(p + 1, domain))
531 *p = '\0';
532 cp = hp->h_name;
535 ina.s_addr = in;
536 strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
538 return (line);
541 #ifdef INET6
542 char *
543 routename6(const struct sockaddr_in6 *sin6, int flags)
545 int niflags = 0;
547 if ((flags & RT_NFLAG))
548 niflags |= NI_NUMERICHOST;
549 else
550 niflags |= NI_NOFQDN;
552 if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
553 line, sizeof(line), NULL, 0, niflags) != 0)
554 strncpy(line, "invalid", sizeof(line));
556 return (line);
558 #endif
561 * Return the name of the network whose address is given.
562 * The address is assumed to be that of a net or subnet, not a host.
564 char *
565 netname4(const struct sockaddr_in* sa4, const struct sockaddr_in *mask, int flags)
567 const char *cp = NULL;
568 struct netent *np = NULL;
569 int mbits;
570 in_addr_t in = sa4->sin_addr.s_addr;
572 if (mask) {
573 in_addr_t m = mask->sin_addr.s_addr ;
574 m = ntohl(m);
575 mbits = m ? 33 - ffs(m) : 0;
576 } else
577 mbits = 0;
579 in = ntohl(in);
580 if (in == INADDR_ANY && !mbits)
581 cp = "default";
582 else if (!(flags & RT_NFLAG) && in != INADDR_ANY) {
583 if ((np = getnetbyaddr(in, AF_INET)) != NULL)
584 cp = np->n_name;
586 if (cp)
587 strlcpy(line, cp, sizeof(line));
588 #define C(x) ((x) & 0xff)
589 else if (mbits < 9)
590 snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
591 else if (mbits < 17)
592 snprintf(line, sizeof(line), "%u.%u/%d",
593 C(in >> 24) , C(in >> 16), mbits);
594 else if (mbits < 25)
595 snprintf(line, sizeof(line), "%u.%u.%u/%d",
596 C(in >> 24), C(in >> 16), C(in >> 8), mbits);
597 else
598 snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
599 C(in >> 16), C(in >> 8), C(in), mbits);
600 #undef C
601 return line;
604 #ifdef INET6
605 char *
606 netname6(const struct sockaddr_in6 *sa6, const struct sockaddr_in6 *mask, int flags)
608 struct sockaddr_in6 sin6;
609 const u_char *p;
610 int masklen, final = 0, illegal = 0;
611 int i, lim, flag, error;
612 char hbuf[NI_MAXHOST];
614 sin6 = *sa6;
616 flag = 0;
617 masklen = 0;
618 if (mask) {
619 lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
620 if (lim < 0)
621 lim = 0;
622 else if (lim > (int)sizeof(struct in6_addr))
623 lim = sizeof(struct in6_addr);
624 for (p = (const u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
625 if (final && *p) {
626 illegal++;
627 sin6.sin6_addr.s6_addr[i++] = 0x00;
628 continue;
631 switch (*p & 0xff) {
632 case 0xff:
633 masklen += 8;
634 break;
635 case 0xfe:
636 masklen += 7;
637 final++;
638 break;
639 case 0xfc:
640 masklen += 6;
641 final++;
642 break;
643 case 0xf8:
644 masklen += 5;
645 final++;
646 break;
647 case 0xf0:
648 masklen += 4;
649 final++;
650 break;
651 case 0xe0:
652 masklen += 3;
653 final++;
654 break;
655 case 0xc0:
656 masklen += 2;
657 final++;
658 break;
659 case 0x80:
660 masklen += 1;
661 final++;
662 break;
663 case 0x00:
664 final++;
665 break;
666 default:
667 final++;
668 illegal++;
669 break;
672 if (!illegal)
673 sin6.sin6_addr.s6_addr[i++] &= *p;
674 else
675 sin6.sin6_addr.s6_addr[i++] = 0x00;
677 while (i < (int)sizeof(struct in6_addr))
678 sin6.sin6_addr.s6_addr[i++] = 0x00;
679 } else
680 masklen = 128;
682 if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
683 snprintf(line, sizeof(line), "default");
684 return (line);
687 if (illegal)
688 warnx("illegal prefixlen");
690 if (flags & RT_NFLAG)
691 flag |= NI_NUMERICHOST;
692 error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
693 hbuf, sizeof(hbuf), NULL, 0, flag);
694 if (error)
695 snprintf(hbuf, sizeof(hbuf), "invalid");
697 snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
698 return (line);
700 #endif
703 * Return the name of the network whose address is given.
704 * The address is assumed to be that of a net or subnet, not a host.
706 char *
707 netname(const struct sockaddr *sa, const struct sockaddr *mask, int flags)
709 switch (sa->sa_family) {
711 case AF_INET:
712 return netname4((const struct sockaddr_in *)sa,
713 (const struct sockaddr_in *)mask, flags);
714 #ifdef INET6
715 case AF_INET6:
716 return netname6((const struct sockaddr_in6 *)sa,
717 (const struct sockaddr_in6 *)mask, flags);
718 #endif
719 case AF_LINK:
720 return link_print(sa);
721 default:
722 snprintf(line, sizeof(line), "af %d: %s",
723 sa->sa_family, any_ntoa(sa));
724 break;
726 return (line);
729 static const char hexlist[] = "0123456789abcdef";
731 char *
732 any_ntoa(const struct sockaddr *sa)
734 static char obuf[240];
735 const char *in = sa->sa_data;
736 char *out = obuf;
737 int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
739 *out++ = 'Q';
740 do {
741 *out++ = hexlist[(*in >> 4) & 15];
742 *out++ = hexlist[(*in++) & 15];
743 *out++ = '.';
744 } while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
745 out[-1] = '\0';
746 return (obuf);
749 static char *
750 link_print(const struct sockaddr *sa)
752 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)sa;
753 const u_char *lla = (const u_char *)sdl->sdl_data + sdl->sdl_nlen;
755 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
756 sdl->sdl_slen == 0) {
757 (void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
758 return (line);
760 switch (sdl->sdl_type) {
761 case IFT_ETHER:
762 case IFT_CARP:
763 return ether_ntoa((const struct ether_addr *)lla);
764 default:
765 return link_ntoa(sdl);
769 #ifndef SMALL
770 char *
771 mpls_ntoa(const struct sockaddr *sa)
773 static char obuf[16];
774 size_t olen;
775 const union mpls_shim *pms;
776 union mpls_shim ms;
777 int psize = sizeof(struct sockaddr_mpls);
779 pms = &((const struct sockaddr_mpls*)sa)->smpls_addr;
780 ms.s_addr = ntohl(pms->s_addr);
782 snprintf(obuf, sizeof(obuf), "%u", ms.shim.label);
784 while(psize < sa->sa_len) {
785 pms++;
786 ms.s_addr = ntohl(pms->s_addr);
787 olen = strlen(obuf);
788 snprintf(obuf + olen, sizeof(obuf) - olen, ",%u",
789 ms.shim.label);
790 psize+=sizeof(ms);
792 return obuf;
794 #endif
796 void
797 p_addr(const struct sockaddr *sa, const struct sockaddr *mask, int rflags, int flags)
799 p_sockaddr(sa, mask, rflags, WID_DST(sa->sa_family), flags);
802 void
803 p_gwaddr(const struct sockaddr *sa, int gwaf, int flags)
805 p_sockaddr(sa, 0, RTF_HOST, WID_GW(gwaf), flags);