Annotation of src/usr.bin/netstat/show.c, Revision 1.59
1.59 ! claudio 1: /* $OpenBSD: show.c,v 1.58 2021/01/24 08:58:50 florian Exp $ */
1.1 claudio 2: /* $NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1988, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
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.
19: *
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.
31: */
32:
33: #include <sys/socket.h>
34: #include <sys/sysctl.h>
35:
36: #include <net/if.h>
37: #include <net/if_dl.h>
38: #include <net/if_types.h>
39: #include <net/route.h>
40: #include <netinet/in.h>
41: #include <netinet/if_ether.h>
1.13 claudio 42: #include <netmpls/mpls.h>
1.1 claudio 43: #include <arpa/inet.h>
44:
45: #include <err.h>
46: #include <errno.h>
47: #include <netdb.h>
48: #include <stdio.h>
49: #include <stddef.h>
50: #include <stdlib.h>
51: #include <string.h>
52: #include <unistd.h>
1.44 deraadt 53: #include <limits.h>
1.1 claudio 54:
55: #include "netstat.h"
56:
57: char *any_ntoa(const struct sockaddr *);
58: char *link_print(struct sockaddr *);
1.30 claudio 59: char *label_print(struct sockaddr *);
1.1 claudio 60:
61: #define ROUNDUP(a) \
62: ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
63: #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
64:
65: /*
66: * Definitions for showing gateway flags.
67: */
68: struct bits {
69: int b_mask;
70: char b_val;
71: };
72: static const struct bits bits[] = {
73: { RTF_UP, 'U' },
74: { RTF_GATEWAY, 'G' },
75: { RTF_HOST, 'H' },
76: { RTF_REJECT, 'R' },
77: { RTF_DYNAMIC, 'D' },
78: { RTF_MODIFIED, 'M' },
79: { RTF_CLONING, 'C' },
1.50 mpi 80: { RTF_MULTICAST,'m' },
1.1 claudio 81: { RTF_LLINFO, 'L' },
82: { RTF_STATIC, 'S' },
1.51 bluhm 83: { RTF_BLACKHOLE,'B' },
84: { RTF_PROTO3, '3' },
85: { RTF_PROTO2, '2' },
1.1 claudio 86: { RTF_PROTO1, '1' },
87: { RTF_CLONED, 'c' },
1.51 bluhm 88: { RTF_CACHED, 'h' },
1.18 claudio 89: { RTF_MPATH, 'P' },
1.25 michele 90: { RTF_MPLS, 'T' },
1.42 mpi 91: { RTF_LOCAL, 'l' },
1.52 phessler 92: { RTF_BFD, 'F' },
1.51 bluhm 93: { RTF_BROADCAST,'b' },
94: { RTF_CONNECTED,'n' },
1.1 claudio 95: { 0 }
96: };
97:
1.41 claudio 98: int WID_DST(int);
1.1 claudio 99: void p_rtentry(struct rt_msghdr *);
100: void pr_family(int);
101: void p_sockaddr(struct sockaddr *, struct sockaddr *, int, int);
1.17 michele 102: void p_sockaddr_mpls(struct sockaddr *, struct sockaddr *, int, int);
1.1 claudio 103: void p_flags(int, char *);
104: char *routename4(in_addr_t);
105: char *routename6(struct sockaddr_in6 *);
106:
1.46 claudio 107: size_t
108: get_sysctl(const int *mib, u_int mcnt, char **buf)
109: {
110: size_t needed;
111:
112: while (1) {
113: if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1)
114: err(1, "sysctl-estimate");
115: if (needed == 0)
116: break;
117: if ((*buf = realloc(*buf, needed)) == NULL)
118: err(1, NULL);
119: if (sysctl(mib, mcnt, *buf, &needed, NULL, 0) == -1) {
120: if (errno == ENOMEM)
121: continue;
122: err(1, "sysctl");
123: }
124: break;
125: }
126:
127: return needed;
128: }
129:
1.1 claudio 130: /*
131: * Print routing tables.
132: */
133: void
1.35 mikeb 134: p_rttables(int af, u_int tableid)
1.1 claudio 135: {
136: struct rt_msghdr *rtm;
137: char *buf = NULL, *next, *lim = NULL;
138: size_t needed;
1.34 claudio 139: int mib[7], mcnt;
1.1 claudio 140:
141: mib[0] = CTL_NET;
142: mib[1] = PF_ROUTE;
143: mib[2] = 0;
144: mib[3] = af;
145: mib[4] = NET_RT_DUMP;
146: mib[5] = 0;
1.35 mikeb 147: mib[6] = tableid;
148: mcnt = 7;
1.46 claudio 149:
150: needed = get_sysctl(mib, mcnt, &buf);
151: lim = buf + needed;
1.1 claudio 152:
153: printf("Routing tables\n");
154:
155: if (buf) {
156: for (next = buf; next < lim; next += rtm->rtm_msglen) {
157: rtm = (struct rt_msghdr *)next;
1.8 claudio 158: if (rtm->rtm_version != RTM_VERSION)
159: continue;
1.1 claudio 160: p_rtentry(rtm);
161: }
162: }
1.47 claudio 163: free(buf);
1.1 claudio 164: }
165:
1.12 deraadt 166: /*
1.1 claudio 167: * column widths; each followed by one space
168: * width of destination/gateway column
169: * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4
170: */
1.56 pamela 171: #define WID_GW(af) ((af) == AF_INET6 ? 30 : 18)
1.1 claudio 172:
1.41 claudio 173: int
1.13 claudio 174: WID_DST(int af)
175: {
176:
1.56 pamela 177: switch (af) {
178: case AF_MPLS:
179: return 9;
180: case AF_INET6:
181: return 34;
182: default:
183: return 18;
184: }
1.13 claudio 185: }
186:
1.1 claudio 187: /*
188: * Print header for routing table columns.
189: */
190: void
191: pr_rthdr(int af, int Aflag)
192: {
193: if (Aflag)
194: printf("%-*.*s ", PLEN, PLEN, "Address");
1.13 claudio 195: switch (af) {
196: case PF_KEY:
1.1 claudio 197: printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
198: "Source", "Port", "Destination",
199: "Port", "Proto", "SA(Address/Proto/Type/Direction)");
1.13 claudio 200: break;
201: case PF_MPLS:
1.30 claudio 202: printf("%-9s %-9s %-6s %-18s %-6.6s %5.5s %8.8s %5.5s %4.4s %s\n",
1.13 claudio 203: "In label", "Out label", "Op", "Gateway",
204: "Flags", "Refs", "Use", "Mtu", "Prio", "Interface");
205: break;
206: default:
1.32 bluhm 207: printf("%-*.*s %-*.*s %-6.6s %5.5s %8.8s %5.5s %4.4s %s",
1.13 claudio 208: WID_DST(af), WID_DST(af), "Destination",
209: WID_GW(af), WID_GW(af), "Gateway",
210: "Flags", "Refs", "Use", "Mtu", "Prio", "Iface");
1.33 bluhm 211: if (vflag && !Aflag)
1.32 bluhm 212: printf(" %s", "Label");
213: putchar('\n');
1.13 claudio 214: break;
215: }
1.1 claudio 216: }
217:
218: static void
219: get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
220: {
221: int i;
222:
223: for (i = 0; i < RTAX_MAX; i++) {
224: if (addrs & (1 << i)) {
225: rti_info[i] = sa;
226: sa = (struct sockaddr *)((char *)(sa) +
227: ROUNDUP(sa->sa_len));
228: } else
229: rti_info[i] = NULL;
230: }
231: }
232:
233: /*
234: * Print a routing table entry.
235: */
236: void
237: p_rtentry(struct rt_msghdr *rtm)
238: {
239: static int old_af = -1;
1.22 chris 240: struct sockaddr *sa = (struct sockaddr *)((char *)rtm + rtm->rtm_hdrlen);
1.1 claudio 241: struct sockaddr *mask, *rti_info[RTAX_MAX];
242: char ifbuf[IF_NAMESIZE];
243:
244: if (sa->sa_family == AF_KEY)
245: return;
246:
1.5 pyr 247: get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
1.41 claudio 248:
1.5 pyr 249: if (Fflag && rti_info[RTAX_GATEWAY]->sa_family != sa->sa_family) {
250: return;
251: }
1.1 claudio 252: if (old_af != sa->sa_family) {
253: old_af = sa->sa_family;
254: pr_family(sa->sa_family);
255: pr_rthdr(sa->sa_family, 0);
256: }
257:
258: mask = rti_info[RTAX_NETMASK];
259: if ((sa = rti_info[RTAX_DST]) == NULL)
260: return;
261:
262: p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family));
1.19 michele 263: p_sockaddr_mpls(sa, rti_info[RTAX_SRC], rtm->rtm_mpls,
1.17 michele 264: WID_DST(sa->sa_family));
265:
1.1 claudio 266: p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
267: WID_GW(sa->sa_family));
1.30 claudio 268:
1.1 claudio 269: p_flags(rtm->rtm_flags, "%-6.6s ");
1.13 claudio 270: printf("%5u %8llu ", rtm->rtm_rmx.rmx_refcnt,
1.1 claudio 271: rtm->rtm_rmx.rmx_pksent);
272: if (rtm->rtm_rmx.rmx_mtu)
1.13 claudio 273: printf("%5u ", rtm->rtm_rmx.rmx_mtu);
1.1 claudio 274: else
1.13 claudio 275: printf("%5s ", "-");
1.1 claudio 276: putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
1.32 bluhm 277: printf(" %2d %-5.16s", rtm->rtm_priority,
1.13 claudio 278: if_indextoname(rtm->rtm_index, ifbuf));
1.32 bluhm 279: if (vflag && rti_info[RTAX_LABEL])
280: printf(" %s", ((struct sockaddr_rtlabel *)
281: rti_info[RTAX_LABEL])->sr_label);
1.1 claudio 282: putchar('\n');
283: }
284:
285: /*
286: * Print address family header before a section of the routing table.
287: */
288: void
289: pr_family(int af)
290: {
291: char *afname;
292:
293: switch (af) {
294: case AF_INET:
295: afname = "Internet";
296: break;
297: case AF_INET6:
298: afname = "Internet6";
299: break;
300: case PF_KEY:
301: afname = "Encap";
302: break;
1.13 claudio 303: case AF_MPLS:
304: afname = "MPLS";
305: break;
1.1 claudio 306: default:
307: afname = NULL;
308: break;
309: }
310: if (afname)
311: printf("\n%s:\n", afname);
312: else
313: printf("\nProtocol Family %d:\n", af);
314: }
315:
316: void
317: p_addr(struct sockaddr *sa, struct sockaddr *mask, int flags)
318: {
319: p_sockaddr(sa, mask, flags, WID_DST(sa->sa_family));
320: }
321:
322: void
323: p_gwaddr(struct sockaddr *sa, int af)
324: {
325: p_sockaddr(sa, 0, RTF_HOST, WID_GW(af));
326: }
327:
328: void
329: p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
330: {
331: char *cp;
332:
333: switch (sa->sa_family) {
334: case AF_INET6:
335: {
336: struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
1.58 florian 337: #ifdef __KAME__
1.1 claudio 338: struct in6_addr *in6 = &sa6->sin6_addr;
339:
340: /*
341: * XXX: This is a special workaround for KAME kernels.
342: * sin6_scope_id field of SA should be set in the future.
343: */
1.57 claudio 344: if ((IN6_IS_ADDR_LINKLOCAL(in6) ||
1.3 itojun 345: IN6_IS_ADDR_MC_LINKLOCAL(in6) ||
1.57 claudio 346: IN6_IS_ADDR_MC_INTFACELOCAL(in6)) &&
347: sa6->sin6_scope_id == 0) {
1.1 claudio 348: /* XXX: override is ok? */
349: sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)
350: &in6->s6_addr[2]);
351: *(u_short *)&in6->s6_addr[2] = 0;
352: }
1.58 florian 353: #endif
1.1 claudio 354: if (flags & RTF_HOST)
355: cp = routename((struct sockaddr *)sa6);
356: else
357: cp = netname((struct sockaddr *)sa6, mask);
358: break;
359: }
1.16 michele 360: case AF_MPLS:
1.17 michele 361: return;
1.1 claudio 362: default:
363: if ((flags & RTF_HOST) || mask == NULL)
364: cp = routename(sa);
365: else
366: cp = netname(sa, mask);
367: break;
368: }
369: if (width < 0)
370: printf("%s", cp);
371: else {
372: if (nflag)
373: printf("%-*s ", width, cp);
374: else
375: printf("%-*.*s ", width, width, cp);
376: }
377: }
378:
1.44 deraadt 379: static char line[HOST_NAME_MAX+1];
380: static char domain[HOST_NAME_MAX+1];
1.17 michele 381:
382: void
383: p_sockaddr_mpls(struct sockaddr *in, struct sockaddr *out, int flags, int width)
384: {
385: if (in->sa_family != AF_MPLS)
386: return;
387:
1.30 claudio 388: if (flags & MPLS_OP_POP || flags == MPLS_OP_LOCAL) {
389: printf("%-*s ", width, label_print(in));
390: printf("%-*s ", width, label_print(NULL));
391: } else {
392: printf("%-*s ", width, label_print(in));
393: printf("%-*s ", width, label_print(out));
394: }
1.17 michele 395:
1.30 claudio 396: printf("%-6s ", mpls_op(flags));
1.17 michele 397: }
398:
1.1 claudio 399: void
400: p_flags(int f, char *format)
401: {
402: char name[33], *flags;
403: const struct bits *p = bits;
404:
405: for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
406: if (p->b_mask & f)
407: *flags++ = p->b_val;
408: *flags = '\0';
409: printf(format, name);
410: }
411:
412: char *
413: routename(struct sockaddr *sa)
414: {
415: char *cp = NULL;
416: static int first = 1;
417:
418: if (first) {
419: first = 0;
420: if (gethostname(domain, sizeof(domain)) == 0 &&
421: (cp = strchr(domain, '.')))
422: (void)strlcpy(domain, cp + 1, sizeof(domain));
423: else
424: domain[0] = '\0';
425: cp = NULL;
426: }
427:
428: if (sa->sa_len == 0) {
429: (void)strlcpy(line, "default", sizeof(line));
430: return (line);
431: }
432:
433: switch (sa->sa_family) {
434: case AF_INET:
435: return
436: (routename4(((struct sockaddr_in *)sa)->sin_addr.s_addr));
437:
438: case AF_INET6:
439: {
440: struct sockaddr_in6 sin6;
441:
442: memset(&sin6, 0, sizeof(sin6));
443: memcpy(&sin6, sa, sa->sa_len);
444: sin6.sin6_family = AF_INET6;
1.58 florian 445: #ifdef __KAME__
1.1 claudio 446: if (sa->sa_len == sizeof(struct sockaddr_in6) &&
447: (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
1.3 itojun 448: IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
449: IN6_IS_ADDR_MC_INTFACELOCAL(&sin6.sin6_addr)) &&
1.1 claudio 450: sin6.sin6_scope_id == 0) {
451: sin6.sin6_scope_id =
452: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
453: sin6.sin6_addr.s6_addr[2] = 0;
454: sin6.sin6_addr.s6_addr[3] = 0;
455: }
1.58 florian 456: #endif
1.1 claudio 457: return (routename6(&sin6));
458: }
459:
460: case AF_LINK:
461: return (link_print(sa));
1.13 claudio 462: case AF_MPLS:
1.30 claudio 463: return (label_print(sa));
1.1 claudio 464: case AF_UNSPEC:
465: if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
466: static char name[RTLABEL_LEN];
467: struct sockaddr_rtlabel *sr;
468:
469: sr = (struct sockaddr_rtlabel *)sa;
1.9 claudio 470: (void)strlcpy(name, sr->sr_label, sizeof(name));
1.1 claudio 471: return (name);
472: }
473: /* FALLTHROUGH */
474: default:
475: (void)snprintf(line, sizeof(line), "(%d) %s",
476: sa->sa_family, any_ntoa(sa));
477: break;
478: }
479: return (line);
480: }
481:
482: char *
483: routename4(in_addr_t in)
484: {
485: char *cp = NULL;
486: struct in_addr ina;
487: struct hostent *hp;
488:
489: if (!cp && !nflag) {
490: if ((hp = gethostbyaddr((char *)&in,
491: sizeof(in), AF_INET)) != NULL) {
492: if ((cp = strchr(hp->h_name, '.')) &&
493: !strcmp(cp + 1, domain))
494: *cp = '\0';
495: cp = hp->h_name;
496: }
497: }
498: ina.s_addr = in;
499: strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
500:
501: return (line);
502: }
503:
504: char *
505: routename6(struct sockaddr_in6 *sin6)
506: {
507: int niflags = 0;
508:
509: if (nflag)
510: niflags |= NI_NUMERICHOST;
511: else
512: niflags |= NI_NOFQDN;
513:
1.53 guenther 514: if (getnameinfo((struct sockaddr *)sin6, sizeof(*sin6),
1.1 claudio 515: line, sizeof(line), NULL, 0, niflags) != 0)
516: strncpy(line, "invalid", sizeof(line));
517:
518: return (line);
519: }
520:
521: /*
522: * Return the name of the network whose address is given.
523: * The address is assumed to be that of a net or subnet, not a host.
524: */
525: char *
526: netname4(in_addr_t in, in_addr_t mask)
527: {
528: int mbits;
529:
530: in = ntohl(in);
531: mask = ntohl(mask);
1.43 schwarze 532: mbits = mask ? 33 - ffs(mask) : 0;
1.28 dlg 533: if (in == INADDR_ANY && mask == INADDR_ANY)
1.43 schwarze 534: strlcpy(line, "default", sizeof(line));
1.1 claudio 535: #define C(x) ((x) & 0xff)
536: else if (mbits < 9)
537: snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
538: else if (mbits < 17)
539: snprintf(line, sizeof(line), "%u.%u/%d",
540: C(in >> 24) , C(in >> 16), mbits);
541: else if (mbits < 25)
542: snprintf(line, sizeof(line), "%u.%u.%u/%d",
543: C(in >> 24), C(in >> 16), C(in >> 8), mbits);
544: else
545: snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
546: C(in >> 16), C(in >> 8), C(in), mbits);
547: #undef C
548: return (line);
549: }
550:
551: char *
552: netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
553: {
554: struct sockaddr_in6 sin6;
555: u_char *p;
556: int masklen, final = 0, illegal = 0;
557: int i, lim, flag, error;
558: char hbuf[NI_MAXHOST];
559:
560: sin6 = *sa6;
561:
562: flag = 0;
563: masklen = 0;
564: if (mask) {
565: lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1.4 claudio 566: lim = lim < (int)sizeof(struct in6_addr) ?
1.41 claudio 567: lim : (int)sizeof(struct in6_addr);
1.1 claudio 568: for (p = (u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
569: if (final && *p) {
570: illegal++;
571: sin6.sin6_addr.s6_addr[i++] = 0x00;
572: continue;
573: }
574:
575: switch (*p & 0xff) {
576: case 0xff:
577: masklen += 8;
578: break;
579: case 0xfe:
580: masklen += 7;
581: final++;
582: break;
583: case 0xfc:
584: masklen += 6;
585: final++;
586: break;
587: case 0xf8:
588: masklen += 5;
589: final++;
590: break;
591: case 0xf0:
592: masklen += 4;
593: final++;
594: break;
595: case 0xe0:
596: masklen += 3;
597: final++;
598: break;
599: case 0xc0:
600: masklen += 2;
601: final++;
602: break;
603: case 0x80:
604: masklen += 1;
605: final++;
606: break;
607: case 0x00:
608: final++;
609: break;
610: default:
611: final++;
612: illegal++;
613: break;
614: }
615:
616: if (!illegal)
617: sin6.sin6_addr.s6_addr[i++] &= *p;
618: else
619: sin6.sin6_addr.s6_addr[i++] = 0x00;
620: }
1.41 claudio 621: while (i < (int)sizeof(struct in6_addr))
1.1 claudio 622: sin6.sin6_addr.s6_addr[i++] = 0x00;
623: } else
624: masklen = 128;
625:
626: if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr))
627: return ("default");
628:
629: if (illegal)
630: warnx("illegal prefixlen");
631:
632: if (nflag)
633: flag |= NI_NUMERICHOST;
1.53 guenther 634: error = getnameinfo((struct sockaddr *)&sin6, sizeof(sin6),
1.1 claudio 635: hbuf, sizeof(hbuf), NULL, 0, flag);
636: if (error)
637: snprintf(hbuf, sizeof(hbuf), "invalid");
638:
639: snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
640: return (line);
641: }
642:
643: /*
644: * Return the name of the network whose address is given.
645: * The address is assumed to be that of a net or subnet, not a host.
646: */
647: char *
648: netname(struct sockaddr *sa, struct sockaddr *mask)
649: {
650: switch (sa->sa_family) {
651: case AF_INET:
652: return netname4(((struct sockaddr_in *)sa)->sin_addr.s_addr,
1.29 claudio 653: mask->sa_len == 0 ? 0 :
1.1 claudio 654: ((struct sockaddr_in *)mask)->sin_addr.s_addr);
655: case AF_INET6:
656: return netname6((struct sockaddr_in6 *)sa,
657: (struct sockaddr_in6 *)mask);
658: case AF_LINK:
659: return (link_print(sa));
1.13 claudio 660: case AF_MPLS:
1.30 claudio 661: return (label_print(sa));
1.1 claudio 662: default:
663: snprintf(line, sizeof(line), "af %d: %s",
664: sa->sa_family, any_ntoa(sa));
665: break;
666: }
667: return (line);
668: }
669:
670: static const char hexlist[] = "0123456789abcdef";
671:
672: char *
673: any_ntoa(const struct sockaddr *sa)
674: {
675: static char obuf[240];
676: const char *in = sa->sa_data;
677: char *out = obuf;
678: int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
679:
680: *out++ = 'Q';
681: do {
682: *out++ = hexlist[(*in >> 4) & 15];
683: *out++ = hexlist[(*in++) & 15];
684: *out++ = '.';
685: } while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
686: out[-1] = '\0';
687: return (obuf);
688: }
689:
690: char *
691: link_print(struct sockaddr *sa)
692: {
693: struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
694: u_char *lla = (u_char *)sdl->sdl_data + sdl->sdl_nlen;
695:
696: if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
697: sdl->sdl_slen == 0) {
698: (void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
699: return (line);
700: }
701: switch (sdl->sdl_type) {
702: case IFT_ETHER:
703: case IFT_CARP:
704: return (ether_ntoa((struct ether_addr *)lla));
705: default:
706: return (link_ntoa(sdl));
707: }
1.13 claudio 708: }
709:
710: char *
1.30 claudio 711: mpls_op(u_int32_t type)
1.13 claudio 712: {
1.16 michele 713: switch (type & (MPLS_OP_PUSH | MPLS_OP_POP | MPLS_OP_SWAP)) {
1.21 michele 714: case MPLS_OP_LOCAL:
715: return ("LOCAL");
1.13 claudio 716: case MPLS_OP_POP:
717: return ("POP");
718: case MPLS_OP_SWAP:
719: return ("SWAP");
720: case MPLS_OP_PUSH:
721: return ("PUSH");
722: default:
723: return ("?");
724: }
725: }
726:
727: char *
1.30 claudio 728: label_print(struct sockaddr *sa)
1.13 claudio 729: {
1.30 claudio 730: struct sockaddr_mpls *smpls = (struct sockaddr_mpls *)sa;
1.13 claudio 731:
1.30 claudio 732: if (smpls)
733: (void)snprintf(line, sizeof(line), "%u",
734: ntohl(smpls->smpls_label) >> MPLS_LABEL_OFFSET);
735: else
736: (void)snprintf(line, sizeof(line), "-");
1.13 claudio 737:
738: return (line);
1.1 claudio 739: }