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.
This commit is contained in:
parent
06912e53e4
commit
dc6772fccc
1 changed files with 22 additions and 12 deletions
|
@ -21,8 +21,8 @@ static uchar loopback6[IPaddrlen] = {
|
|||
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 @@ myipaddr(uchar *ip, char *net)
|
|||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue