From 5174f725c2a5666d796565271dd9a8819ff06fc5 Mon Sep 17 00:00:00 2001 From: roy Date: Sat, 1 Oct 2016 15:10:58 +0000 Subject: [PATCH] Modernise the output for the address to address/prefix instead of differring outputs for INET and INET6. The hex string of the INET netmask was particulary hard to read. --- sbin/ifconfig/af_inet.c | 24 ++++++++++++++++++------ sbin/ifconfig/af_inet6.c | 11 +++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/sbin/ifconfig/af_inet.c b/sbin/ifconfig/af_inet.c index d2403e40e481..f726ea691649 100644 --- a/sbin/ifconfig/af_inet.c +++ b/sbin/ifconfig/af_inet.c @@ -1,4 +1,4 @@ -/* $NetBSD: af_inet.c,v 1.22 2016/09/30 16:52:17 roy Exp $ */ +/* $NetBSD: af_inet.c,v 1.23 2016/10/01 15:10:58 roy Exp $ */ /* * Copyright (c) 1983, 1993 @@ -31,7 +31,7 @@ #include #ifndef lint -__RCSID("$NetBSD: af_inet.c,v 1.22 2016/09/30 16:52:17 roy Exp $"); +__RCSID("$NetBSD: af_inet.c,v 1.23 2016/10/01 15:10:58 roy Exp $"); #endif /* not lint */ #include @@ -64,6 +64,7 @@ static void in_status(prop_dictionary_t, prop_dictionary_t, bool); static void in_commit_address(prop_dictionary_t, prop_dictionary_t); static bool in_addr_tentative(struct ifaddrs *); static bool in_addr_tentative_or_detached(struct ifaddrs *); +static int in_prefixlen(struct sockaddr *); static void in_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t); static struct afswtch af = { @@ -73,10 +74,23 @@ static struct afswtch af = { .af_addr_tentative_or_detached = in_addr_tentative_or_detached }; +static int +in_prefixlen(struct sockaddr *sa) +{ + struct sockaddr_in sin; + in_addr_t mask; + int cidr; + + memset(&sin, 0, sizeof(sin)); + memcpy(&sin, sa, sa->sa_len); + mask = ntohl(sin.sin_addr.s_addr); + cidr = 33 - ffs(mask); + return cidr; +} + static void in_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) { - struct sockaddr_in sin; char hbuf[NI_MAXHOST]; const int niflag = Nflag ? 0 : NI_NUMERICHOST; char fbuf[1024]; @@ -88,6 +102,7 @@ in_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) hbuf, sizeof(hbuf), NULL, 0, niflag)) strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ printf("\tinet %s", hbuf); + printf("/%d", in_prefixlen(ifa->ifa_netmask)); if (ifa->ifa_flags & IFF_POINTOPOINT) { if (getnameinfo(ifa->ifa_dstaddr, ifa->ifa_dstaddr->sa_len, @@ -96,9 +111,6 @@ in_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) printf(" -> %s", hbuf); } - memcpy(&sin, ifa->ifa_netmask, ifa->ifa_netmask->sa_len); - printf(" netmask 0x%x", ntohl(sin.sin_addr.s_addr)); - if (ifa->ifa_flags & IFF_BROADCAST) { if (getnameinfo(ifa->ifa_broadaddr, ifa->ifa_broadaddr->sa_len, hbuf, sizeof(hbuf), NULL, 0, niflag)) diff --git a/sbin/ifconfig/af_inet6.c b/sbin/ifconfig/af_inet6.c index 5b2b44b3272e..9ad454f883ec 100644 --- a/sbin/ifconfig/af_inet6.c +++ b/sbin/ifconfig/af_inet6.c @@ -1,4 +1,4 @@ -/* $NetBSD: af_inet6.c,v 1.37 2016/09/30 16:47:56 roy Exp $ */ +/* $NetBSD: af_inet6.c,v 1.38 2016/10/01 15:10:58 roy Exp $ */ /* * Copyright (c) 1983, 1993 @@ -31,7 +31,7 @@ #include #ifndef lint -__RCSID("$NetBSD: af_inet6.c,v 1.37 2016/09/30 16:47:56 roy Exp $"); +__RCSID("$NetBSD: af_inet6.c,v 1.38 2016/10/01 15:10:58 roy Exp $"); #endif /* not lint */ #include @@ -277,6 +277,9 @@ in6_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) printf("\tinet6 %s", hbuf); inet6_putscopeid(sin6, INET6_IS_ADDR_LINKLOCAL); + sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask; + printf("/%d", prefix(&sin6->sin6_addr, sizeof(struct in6_addr))); + if (ifa->ifa_flags & IFF_POINTOPOINT) { sin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr; inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL); @@ -287,10 +290,6 @@ in6_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) printf(" -> %s", hbuf); } - sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask; - printf(" prefixlen %d", prefix(&sin6->sin6_addr, - sizeof(struct in6_addr))); - (void)snprintb(fbuf, sizeof(fbuf), IN6_IFFBITS, ifa->ifa_addrflags); printf(" flags %s", fbuf);