From nobody@FreeBSD.org  Thu Jun 14 15:49:13 2012
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (unknown [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id AB1C4106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 14 Jun 2012 15:49:13 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [69.147.83.34])
	by mx1.freebsd.org (Postfix) with ESMTP id 963F58FC15
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 14 Jun 2012 15:49:13 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id q5EFnCOh068321
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 14 Jun 2012 15:49:12 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id q5EFnCg4068320;
	Thu, 14 Jun 2012 15:49:12 GMT
	(envelope-from nobody)
Message-Id: <201206141549.q5EFnCg4068320@red.freebsd.org>
Date: Thu, 14 Jun 2012 15:49:12 GMT
From: Vitaliy Turovets <corebug@corebug.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: A little change to ifconfig to display CIDR prefixes instead of hex masks
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         169072
>Category:       bin
>Synopsis:       [patch] A little change to ifconfig(8) to display CIDR prefixes instead of hex masks
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jun 14 15:50:01 UTC 2012
>Closed-Date:    
>Last-Modified:  Sun Jun 17 22:35:39 UTC 2012
>Originator:     Vitaliy Turovets
>Release:        9.0-RC3
>Organization:
Corebug.Net
>Environment:
FreeBSD db.service-it.com.ua 9.0-RC3 FreeBSD 9.0-RC3 #0: Sun Dec  4 08:56:36 UTC 2011     root@farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  amd64

>Description:
I as network administrator very much dislike how network masks (hexadecimal format) are show for IPv4 by ifconfig.
Since this i added the '-D' switch and some code allowing ifconfig to print IPv4 netmasks in CIDR format. As an example:

Original behaviour:
[18:25][db][~corebug/ifconfig_my] # ./ifconfig LAN
LAN: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=3<RXCSUM,TXCSUM>
        ether 00:12:79:d2:e4:7d
        inet 192.168.0.4 netmask 0xffffff00 broadcast 192.168.0.255
        inet6 fe80::212:79ff:fed2:e47d%LAN prefixlen 64 scopeid 0x9
        nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
        media: Ethernet autoselect (100baseTX <full-duplex>)
        status: active
        vlan: 10 parent interface: bge0

Behaviour with '-D' switch:
[18:25][db][~corebug/ifconfig_my] # ./ifconfig -D LAN
LAN: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        options=3<RXCSUM,TXCSUM>
        ether 00:12:79:d2:e4:7d
        inet 192.168.0.4/24
        inet6 fe80::212:79ff:fed2:e47d%LAN prefixlen 64 scopeid 0x9
        nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
        media: Ethernet autoselect (100baseTX <full-duplex>)
        status: active
        vlan: 10 parent interface: bge0

Note the difference between 
        inet 192.168.0.4 netmask 0xffffff00 broadcast 192.168.0.255
and
       inet 192.168.0.4/24
I think that such a switch must be present and i suppose that lot of ISP and/or enterprise network admins would agree with me.
Files affected are: ifconfig.c, ifconfig.h, af_inet.c.
The diff between original version and my one is included

>How-To-Repeat:
Just build the ifconfig:)
>Fix:


Patch attached with submission follows:

diff ifconfig/af_inet.c ifconfig_with_cidr_mode/af_inet.c
58a59,66
> int hex2cidr(int i);
> int hex2cidr(int i)
> {
>         i = i - ((i >> 1) & 0x55555555);
>             i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
>                 return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
> }
> 
70c78
< 	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
---
> 	printf("\tinet %s", inet_ntoa(sin->sin_addr));
76c84
< 		printf("--> %s ", inet_ntoa(sin->sin_addr));
---
> 		printf(" --> %s ", inet_ntoa(sin->sin_addr));
79a88
>         if((ifa->ifa_flags & IFF_POINTOPOINT) ||(!cidr)){
82,89c91,102
< 	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;
< 		if (sin != NULL && sin->sin_addr.s_addr != 0)
< 			printf("broadcast %s", inet_ntoa(sin->sin_addr));
< 	}
< 	putchar('\n');
---
> 	        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;
> 		        if (sin != NULL && sin->sin_addr.s_addr != 0)
> 			    printf("broadcast %s", inet_ntoa(sin->sin_addr));
> 	    };
> 	    putchar('\n');
>         }else{ //XXX:Here we go to print CIDR mask instead of hex mask
> 	if (sin == NULL)
> 		sin = &null_sin;
>                 printf("/%d\n", (hex2cidr((int)ntohl(sin->sin_addr.s_addr))));
>         };
diff ifconfig/ifconfig.c ifconfig_with_cidr_mode/ifconfig.c
94c94
< 
---
> int     cidr=0;
137c137,138
< 	"       ifconfig %s[-d] [-m] [-u] [-v]\n",
---
> 	"       ifconfig %s[-d] [-m] [-u] [-v]\n"
>         "       ifconfig [-D]\n",
157a159
> 
159c161
< 	strlcpy(options, "adklmnuv", sizeof(options));
---
> 	strlcpy(options, "aDdklmnuv", sizeof(options));
163a166,168
>                 case 'D':       /* output IPv4 prefixes in CIDR format */
>                         cidr++;
>                         break;
diff ifconfig/ifconfig.h ifconfig_with_cidr_mode/ifconfig.h
44a45,46
> 
> 
134a137
> extern  int cidr;


>Release-Note:
>Audit-Trail:
>Unformatted:
