User libraries depend on their dependencies .a's
[akaros.git] / user / iplib / myipaddr.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 #include <stdlib.h>
10
11 #include <stdio.h>
12 #include <parlib.h>
13 #include <unistd.h>
14 #include <signal.h>
15 #include <iplib.h>
16
17 static uint8_t loopbacknet[IPaddrlen] = {
18         0, 0, 0, 0,
19         0, 0, 0, 0,
20         0, 0, 0xff, 0xff,
21         127, 0, 0, 0
22 };
23 static uint8_t loopbackmask[IPaddrlen] = {
24         0xff, 0xff, 0xff, 0xff,
25         0xff, 0xff, 0xff, 0xff,
26         0xff, 0xff, 0xff, 0xff,
27         0xff, 0, 0, 0
28 };
29
30 // find first ip addr that isn't the friggin loopback address
31 // unless there are no others
32 int
33 myipaddr(uint8_t *ip, char *net)
34 {
35         struct ipifc *nifc;
36         struct iplifc *lifc;
37         static struct ipifc *ifc;
38         uint8_t mynet[IPaddrlen];
39
40         ifc = readipifc(net, ifc, -1);
41         for(nifc = ifc; nifc; nifc = nifc->next)
42                 for(lifc = nifc->lifc; lifc; lifc = lifc->next){
43                         maskip(lifc->ip, loopbackmask, mynet);
44                         if(ipcmp(mynet, loopbacknet) == 0){
45                                 continue;
46                         }
47                         if(ipcmp(lifc->ip, IPnoaddr) != 0){
48                                 ipmove(ip, lifc->ip);
49                                 return 0;
50                         }
51                 }
52         ipmove(ip, IPnoaddr);
53         return -1;
54 }