ref: dc6772fccc90d90c700eb679b9dfcde4a71fc8cb
parent: 06912e53e491dedb495ca4e6a308831ef7af544f
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Feb 13 13:56:21 EST 2019
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.
--- a/sys/src/libip/myipaddr.c
+++ b/sys/src/libip/myipaddr.c
@@ -21,8 +21,8 @@
0, 0, 0, 1
};
-// find first ip addr that isn't the friggin loopback address
-// unless there are no others
+// find first ip that isn't a friggin loopback or
+// link-local address. prefer v4 over v6.
int
myipaddr(uchar *ip, char *net)
{
@@ -31,29 +31,39 @@
static Ipifc *ifc;
uchar mynet[IPaddrlen];
+ ipmove(ip, IPnoaddr);
ifc = readipifc(net, ifc, -1);
- for(nifc = ifc; nifc; nifc = nifc->next)
- for(lifc = nifc->lifc; lifc; lifc = lifc->next){
+ for(nifc = ifc; nifc != nil; nifc = nifc->next){
+ for(lifc = nifc->lifc; lifc != nil; lifc = lifc->next){
/* unspecified */
if(ipcmp(lifc->ip, IPnoaddr) == 0)
continue;
+ if(isv4(lifc->ip)){
+ /* ipv4 loopback */
+ maskip(lifc->ip, loopbackmask, mynet);
+ if(ipcmp(mynet, loopbacknet) == 0)
+ continue;
+
+ ipmove(ip, lifc->ip);
+ return 0;
+ }
+
+ /* already got a v6 address? */
+ if(ipcmp(ip, IPnoaddr) != 0)
+ continue;
+
/* ipv6 loopback */
if(ipcmp(lifc->ip, loopback6) == 0)
continue;
- /* ipv4 loopback */
- maskip(lifc->ip, loopbackmask, mynet);
- if(ipcmp(mynet, loopbacknet) == 0)
- continue;
-
/* ipv6 linklocal */
if(ISIPV6LINKLOCAL(lifc->ip))
continue;
+ /* save first v6 address */
ipmove(ip, lifc->ip);
- return 0;
}
- ipmove(ip, IPnoaddr);
- return -1;
+ }
+ return ipcmp(ip, IPnoaddr) != 0 ? 0 : -1;
}