]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libip/myipaddr.c
factotum: remove legacy wep protocol
[plan9front.git] / sys / src / libip / myipaddr.c
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 static uchar loopback6[IPaddrlen] = {
18         0, 0, 0, 0,
19         0, 0, 0, 0,
20         0, 0, 0, 0,
21         0, 0, 0, 1
22 };
23
24 // find first ip addr that isn't the friggin loopback address
25 // unless there are no others
26 int
27 myipaddr(uchar *ip, char *net)
28 {
29         Ipifc *nifc;
30         Iplifc *lifc;
31         static Ipifc *ifc;
32         uchar mynet[IPaddrlen];
33
34         ifc = readipifc(net, ifc, -1);
35         for(nifc = ifc; nifc; nifc = nifc->next)
36                 for(lifc = nifc->lifc; lifc; lifc = lifc->next){
37                         /* unspecified */
38                         if(ipcmp(lifc->ip, IPnoaddr) == 0)
39                                 continue;
40
41                         /* ipv6 loopback */
42                         if(ipcmp(lifc->ip, loopback6) == 0)
43                                 continue;
44
45                         /* ipv4 loopback */
46                         maskip(lifc->ip, loopbackmask, mynet);
47                         if(ipcmp(mynet, loopbacknet) == 0)
48                                 continue;
49
50                         ipmove(ip, lifc->ip);
51                         return 0;
52                 }
53         ipmove(ip, IPnoaddr);
54         return -1;
55 }