Author: allanjude
Date: Tue May 31 17:30:08 2016
New Revision: 301059
URL: https://svnweb.freebsd.org/changeset/base/301059
Log:
ifconfig(8) now supports some output formatting options
specified by the -f flag or IFCONFIG_FORMAT environment variable, the user
can request that inet4 subnet masks be printed in CIDR or dotted-quad
notation, in addition to the traditional hex output.
inet6 prefixes can be printed in CIDR as well.
For more documentation see the ifconfig(8) man page.
PR: 169072
Requested by: seanc, marcel, brd, many others
Reviewed by: gnn, jhb (earlier version)
Relnotes: yes
Sponsored by: ScaleEngine Inc.
Differential Revision:https://reviews.freebsd.org/D2856
Modified:
head/sbin/ifconfig/af_inet.c
head/sbin/ifconfig/af_inet6.c
head/sbin/ifconfig/af_link.c
head/sbin/ifconfig/ifconfig.8
head/sbin/ifconfig/ifconfig.c
Modified: head/sbin/ifconfig/af_inet.c
==
--- head/sbin/ifconfig/af_inet.cTue May 31 17:23:27 2016
(r301058)
+++ head/sbin/ifconfig/af_inet.cTue May 31 17:30:08 2016
(r301059)
@@ -54,11 +54,14 @@ static const char rcsid[] =
static struct in_aliasreq in_addreq;
static struct ifreq in_ridreq;
+static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
+extern char *f_inet, *f_addr;
static void
in_status(int s __unused, const struct ifaddrs *ifa)
{
struct sockaddr_in *sin, null_sin;
+ int error, n_flags;
memset(_sin, 0, sizeof(null_sin));
@@ -66,19 +69,47 @@ in_status(int s __unused, const struct i
if (sin == NULL)
return;
- printf("\tinet %s ", inet_ntoa(sin->sin_addr));
+ if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
+ n_flags = 0;
+ else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
+ n_flags = NI_NOFQDN;
+ else
+ n_flags = NI_NUMERICHOST;
+
+ error = getnameinfo((struct sockaddr *)sin, sin->sin_len, addr_buf,
+ sizeof(addr_buf), NULL, 0, n_flags);
+
+ if (error)
+ inet_ntop(AF_INET, >sin_addr, addr_buf, sizeof(addr_buf));
+
+ printf("\tinet %s", addr_buf);
if (ifa->ifa_flags & IFF_POINTOPOINT) {
sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
if (sin == NULL)
sin = _sin;
- printf("--> %s ", inet_ntoa(sin->sin_addr));
+ printf(" --> %s ", inet_ntoa(sin->sin_addr));
}
sin = (struct sockaddr_in *)ifa->ifa_netmask;
if (sin == NULL)
sin = _sin;
- printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
+ if (f_inet != NULL && strcmp(f_inet, "cidr") == 0) {
+ int cidr = 32;
+ unsigned long smask;
+
+ smask = ntohl(sin->sin_addr.s_addr);
+ while ((smask & 1) == 0) {
+ smask = smask >> 1;
+ cidr--;
+ if (cidr == 0)
+ break;
+ }
+ printf("/%d ", cidr);
+ } else if (f_inet != NULL && strcmp(f_inet, "dotted") == 0)
+ printf(" netmask %s ", inet_ntoa(sin->sin_addr));
+ else
+ printf(" netmask 0x%lx ", (unsigned
long)ntohl(sin->sin_addr.s_addr));
if (ifa->ifa_flags & IFF_BROADCAST) {
sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
Modified: head/sbin/ifconfig/af_inet6.c
==
--- head/sbin/ifconfig/af_inet6.c Tue May 31 17:23:27 2016
(r301058)
+++ head/sbin/ifconfig/af_inet6.c Tue May 31 17:30:08 2016
(r301059)
@@ -65,6 +65,7 @@ staticint ip6lifetime;
static int prefix(void *, int);
static char *sec2str(time_t);
static int explicit_prefix = 0;
+extern char *f_inet6, *f_addr, *f_scope;
extern void setnd6flags(const char *, int, int, const struct afswtch *);
extern void setnd6defif(const char *, int, int, const struct afswtch *);
@@ -172,9 +173,10 @@ in6_status(int s __unused, const struct
struct in6_ifreq ifr6;
int s6;
u_int32_t flags6;
+ const u_int16_t *a;
struct in6_addrlifetime lifetime;
struct timespec now;
- int error;
+ int error, n_flags, i;
clock_gettime(CLOCK_MONOTONIC_FAST, );
@@ -206,12 +208,30 @@ in6_status(int s __unused, const struct
lifetime = ifr6.ifr_ifru.ifru_lifetime;
close(s6);
- error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
- sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
- if (error != 0)
- inet_ntop(AF_INET6, >sin6_addr, addr_buf,
- sizeof(addr_buf));
- printf("\tinet6 %s ",