plan9port/src/libip/myipaddr.c

46 lines
872 B
C
Raw Normal View History

2003-11-23 18:23:20 +00:00
#include <u.h>
#include <libc.h>
#include <ip.h>
2005-12-31 19:33:03 +00:00
static uchar loopbacknet[IPaddrlen] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0xff, 0xff,
127, 0, 0, 0
};
static uchar loopbackmask[IPaddrlen] = {
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0, 0, 0
};
// find first ip addr that isn't the friggin loopback address
// unless there are no others
2003-11-23 18:23:20 +00:00
int
myipaddr(uchar *ip, char *net)
{
Ipifc *nifc;
Iplifc *lifc;
2005-12-31 19:33:03 +00:00
Ipifc *ifc;
uchar mynet[IPaddrlen];
2003-11-23 18:23:20 +00:00
2005-12-31 19:33:03 +00:00
ifc = readipifc(net, nil, -1);
2003-11-23 18:23:20 +00:00
for(nifc = ifc; nifc; nifc = nifc->next)
2005-12-31 19:33:03 +00:00
for(lifc = nifc->lifc; lifc; lifc = lifc->next){
maskip(lifc->ip, loopbackmask, mynet);
if(ipcmp(mynet, loopbacknet) == 0){
continue;
}
2003-11-23 18:23:20 +00:00
if(ipcmp(lifc->ip, IPnoaddr) != 0){
ipmove(ip, lifc->ip);
2005-12-31 19:33:03 +00:00
freeipifc(ifc);
2003-11-23 18:23:20 +00:00
return 0;
}
2005-12-31 19:33:03 +00:00
}
2003-11-23 18:23:20 +00:00
ipmove(ip, IPnoaddr);
2005-12-31 19:33:03 +00:00
freeipifc(ifc);
2003-11-23 18:23:20 +00:00
return -1;
}
2005-12-31 19:33:03 +00:00