myipaddr.c (877B)
1 #include <u.h> 2 #include <libc.h> 3 #include <ip.h> 4 5 static uchar loopbacknet[IPaddrlen] = { 6 0, 0, 0, 0, 7 0, 0, 0, 0, 8 0, 0, 0xff, 0xff, 9 127, 0, 0, 0 10 }; 11 static uchar loopbackmask[IPaddrlen] = { 12 0xff, 0xff, 0xff, 0xff, 13 0xff, 0xff, 0xff, 0xff, 14 0xff, 0xff, 0xff, 0xff, 15 0xff, 0, 0, 0 16 }; 17 18 /* find first ip addr that isn't the friggin loopback address */ 19 /* unless there are no others */ 20 int 21 myipaddr(uchar *ip, char *net) 22 { 23 Ipifc *nifc; 24 Iplifc *lifc; 25 Ipifc *ifc; 26 uchar mynet[IPaddrlen]; 27 28 ifc = readipifc(net, nil, -1); 29 for(nifc = ifc; nifc; nifc = nifc->next) 30 for(lifc = nifc->lifc; lifc; lifc = lifc->next){ 31 maskip(lifc->ip, loopbackmask, mynet); 32 if(ipcmp(mynet, loopbacknet) == 0){ 33 continue; 34 } 35 if(ipcmp(lifc->ip, IPnoaddr) != 0){ 36 ipmove(ip, lifc->ip); 37 freeipifc(ifc); 38 return 0; 39 } 40 } 41 ipmove(ip, IPnoaddr); 42 freeipifc(ifc); 43 return -1; 44 }