xref: /plan9/sys/src/libip/myipaddr.c (revision f5736e95f14e1485b3a0291fa82d86cca323ab61)
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
myipaddr(uchar * ip,char * net)21 myipaddr(uchar *ip, char *net)
22 {
23 	Ipifc *nifc;
24 	Iplifc *lifc;
25 	static Ipifc *ifc;
26 	uchar mynet[IPaddrlen];
27 
28 	ifc = readipifc(net, ifc, -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 				return 0;
38 			}
39 		}
40 	ipmove(ip, IPnoaddr);
41 	return -1;
42 }
43