changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > plan9front / changeset: libip: prefer v4 over v6 for myipaddr()

changeset 7036: 7bb53313aac9
parent 7035: 65dab5125d9d
child 7037: 5e221ac3a3f6
author: cinap_lenrek@felloff.net
date: Wed, 13 Feb 2019 18:56:21 +0100
files: sys/src/libip/myipaddr.c
description: libip: prefer v4 over v6 for myipaddr()

myipaddr() is used in legacy applications that assume a
single ip address per host. so prefer to retun a v4
address over a v6 one.
     1.1--- a/sys/src/libip/myipaddr.c
     1.2+++ b/sys/src/libip/myipaddr.c
     1.3@@ -21,8 +21,8 @@ static uchar loopback6[IPaddrlen] = {
     1.4 	0, 0, 0, 1
     1.5 };
     1.6 
     1.7-// find first ip addr that isn't the friggin loopback address
     1.8-// unless there are no others
     1.9+// find first ip that isn't a friggin loopback or
    1.10+// link-local address. prefer v4 over v6.
    1.11 int
    1.12 myipaddr(uchar *ip, char *net)
    1.13 {
    1.14@@ -31,29 +31,39 @@ myipaddr(uchar *ip, char *net)
    1.15 	static Ipifc *ifc;
    1.16 	uchar mynet[IPaddrlen];
    1.17 
    1.18+	ipmove(ip, IPnoaddr);
    1.19 	ifc = readipifc(net, ifc, -1);
    1.20-	for(nifc = ifc; nifc; nifc = nifc->next)
    1.21-		for(lifc = nifc->lifc; lifc; lifc = lifc->next){
    1.22+	for(nifc = ifc; nifc != nil; nifc = nifc->next){
    1.23+		for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next){
    1.24 			/* unspecified */
    1.25 			if(ipcmp(lifc->ip, IPnoaddr) == 0)
    1.26 				continue;
    1.27 
    1.28+			if(isv4(lifc->ip)){
    1.29+				/* ipv4 loopback */
    1.30+				maskip(lifc->ip, loopbackmask, mynet);
    1.31+				if(ipcmp(mynet, loopbacknet) == 0)
    1.32+					continue;
    1.33+	
    1.34+				ipmove(ip, lifc->ip);
    1.35+				return 0;
    1.36+			}
    1.37+
    1.38+			/* already got a v6 address? */
    1.39+			if(ipcmp(ip, IPnoaddr) != 0)
    1.40+				continue;
    1.41+
    1.42 			/* ipv6 loopback */
    1.43 			if(ipcmp(lifc->ip, loopback6) == 0)
    1.44 				continue;
    1.45 
    1.46-			/* ipv4 loopback */
    1.47-			maskip(lifc->ip, loopbackmask, mynet);
    1.48-			if(ipcmp(mynet, loopbacknet) == 0)
    1.49-				continue;
    1.50-	
    1.51 			/* ipv6 linklocal */
    1.52 			if(ISIPV6LINKLOCAL(lifc->ip))
    1.53 				continue;
    1.54 
    1.55+			/* save first v6 address */
    1.56 			ipmove(ip, lifc->ip);
    1.57-			return 0;
    1.58 		}
    1.59-	ipmove(ip, IPnoaddr);
    1.60-	return -1;
    1.61+	}
    1.62+	return ipcmp(ip, IPnoaddr) != 0 ? 0 : -1;
    1.63 }