+#define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
+
+#define NEXT_SA(p) (struct sockaddr *) \
+ ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(uint32_t)) : \
+ sizeof(uint32_t)))
+
+static void sidewaysintpr ();
+static void catchalarm (int);
+
+#ifdef INET6
+char *netname6 (struct sockaddr_in6 *, struct sockaddr *);
+static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
+#endif
+
+/*
+ * Display a formatted value, or a '-' in the same space.
+ */
+static void
+show_stat(const char *fmt, int width, u_int64_t value, short showvalue)
+{
+ char newfmt[32];
+
+ /* Construct the format string */
+ if (showvalue) {
+ sprintf(newfmt, "%%%d%s", width, fmt);
+ printf(newfmt, value);
+ } else {
+ sprintf(newfmt, "%%%ds", width);
+ printf(newfmt, "-");
+ }
+}
+
+size_t
+get_rti_info(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
+{
+ int i;
+ size_t len = 0;
+
+ for (i = 0; i < RTAX_MAX; i++) {
+ if (addrs & (1 << i)) {
+ rti_info[i] = sa;
+ if (sa->sa_len < sizeof(struct sockaddr))
+ len += sizeof(struct sockaddr);
+ else
+ len += sa->sa_len;
+ sa = NEXT_SA(sa);
+ } else {
+ rti_info[i] = NULL;
+ }
+ }
+ return len;
+}
+
+static void
+multipr(int family, char *buf, char *lim)
+{
+ char *next;
+
+ for (next = buf; next < lim; ) {
+ struct ifma_msghdr2 *ifmam = (struct ifma_msghdr2 *)next;
+ struct sockaddr *rti_info[RTAX_MAX];
+ struct sockaddr *sa;
+ const char *fmt = 0;
+
+ next += ifmam->ifmam_msglen;
+ if (ifmam->ifmam_type == RTM_IFINFO2)
+ break;
+ else if (ifmam->ifmam_type != RTM_NEWMADDR2)
+ continue;
+ get_rti_info(ifmam->ifmam_addrs, (struct sockaddr*)(ifmam + 1), rti_info);
+ sa = rti_info[RTAX_IFA];
+
+ if (sa->sa_family != family)
+ continue;
+ switch (sa->sa_family) {
+ case AF_INET: {
+ struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+
+ fmt = routename(sin->sin_addr.s_addr);
+ break;
+ }
+ #ifdef INET6
+ case AF_INET6: {
+ struct sockaddr_in6 sin6;
+
+ memcpy(&sin6, sa, sizeof(struct sockaddr_in6));
+
+ if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
+ IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) {
+ sin6.sin6_scope_id = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
+ sin6.sin6_addr.s6_addr[2] = 0;
+ sin6.sin6_addr.s6_addr[3] = 0;
+ }
+
+ printf("%23s %-19.19s(refs: %d)\n", "",
+ inet_ntop(AF_INET6, &sin6.sin6_addr,
+ ntop_buf, sizeof(ntop_buf)),
+ ifmam->ifmam_refcount);
+ break;
+ }
+ #endif /* INET6 */
+ case AF_LINK: {
+ struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
+
+ switch (sdl->sdl_type) {
+ case IFT_ETHER:
+ case IFT_FDDI:
+ fmt = ether_ntoa((struct ether_addr *)
+ LLADDR(sdl));
+ break;
+ }
+ break;
+ }
+ }
+ if (fmt)
+ printf("%23s %s\n", "", fmt);
+ }
+}