Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 12 Dec 2006 12:22:12 +0000 (UTC)
From:      "Bjoern A. Zeeb" <bzeeb-lists@lists.zabbadoz.net>
To:        freebsd-net@freebsd.org
Subject:   ip6_sprintf patch
Message-ID:  <20061212111134.J91892@maildrop.int.zabbadoz.net>

next in thread | raw e-mail | index | archive | help
Hi,

I have a patch sitting around for quite some time that I'd like to
commit if noone has any objections. It tries to print the addresses
in a more common/readable format eliminating leading zeros like
in :0001 -> :1.

You can also fetch it from here:
http://sources.zabbadoz.net/freebsd/ipv6/patches/20060611-01.diff

! 
! http://perforce.freebsd.org/chv.cgi?CH=98978
! 
==== //depot/user/bz/fast_ipsec/src/sys/netinet6/in6.c#4 - /local/building/freebsd/p4/v6_fast_ipsec/src/sys/netinet6/in6.c ====
--- /tmp/tmp.47236.0	Sun Jun 11 11:48:05 2006
+++ /local/building/freebsd/p4/v6_fast_ipsec/src/sys/netinet6/in6.c	Sun Jun 11 11:47:16 2006
@@ -1852,7 +1852,7 @@
  	char *cp;
  	const u_int16_t *a = (const u_int16_t *)addr;
  	const u_int8_t *d;
-	int dcolon = 0;
+	int dcolon = 0, zero = 0;

  	cp = ip6buf;

@@ -1880,14 +1880,31 @@
  			continue;
  		}
  		d = (const u_char *)a;
-		*cp++ = digits[*d >> 4];
-		*cp++ = digits[*d++ & 0xf];
-		*cp++ = digits[*d >> 4];
-		*cp++ = digits[*d & 0xf];
+		/* Try to eliminate leading zeros in printout like in :0001. */
+		zero = 1;
+		*cp = digits[*d >> 4];
+		if (*cp != '0') {
+			zero = 0;
+			cp++;
+		}
+		*cp = digits[*d++ & 0xf];
+		if (zero == 0 || (*cp != '0')) {
+			zero = 0;
+			cp++;
+		}
+		*cp = digits[*d >> 4];
+		if (zero == 0 || (*cp != '0')) {
+			zero = 0;
+			cp++;
+		}
+		*cp = digits[*d & 0xf];
+		if (zero == 0 || (*cp != '0'))
+			cp++;
  		*cp++ = ':';
  		a++;
  	}
  	*--cp = '\0';
+
  	return (ip6buf);
  }


-- 
Bjoern A. Zeeb				bzeeb at Zabbadoz dot NeT



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20061212111134.J91892>