summaryrefslogtreecommitdiff
path: root/sys/src/libip/myipaddr.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libip/myipaddr.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libip/myipaddr.c')
-rwxr-xr-xsys/src/libip/myipaddr.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/sys/src/libip/myipaddr.c b/sys/src/libip/myipaddr.c
new file mode 100755
index 000000000..e1a2773bd
--- /dev/null
+++ b/sys/src/libip/myipaddr.c
@@ -0,0 +1,42 @@
+#include <u.h>
+#include <libc.h>
+#include <ip.h>
+
+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
+int
+myipaddr(uchar *ip, char *net)
+{
+ Ipifc *nifc;
+ Iplifc *lifc;
+ static Ipifc *ifc;
+ uchar mynet[IPaddrlen];
+
+ ifc = readipifc(net, ifc, -1);
+ for(nifc = ifc; nifc; nifc = nifc->next)
+ for(lifc = nifc->lifc; lifc; lifc = lifc->next){
+ maskip(lifc->ip, loopbackmask, mynet);
+ if(ipcmp(mynet, loopbacknet) == 0){
+ continue;
+ }
+ if(ipcmp(lifc->ip, IPnoaddr) != 0){
+ ipmove(ip, lifc->ip);
+ return 0;
+ }
+ }
+ ipmove(ip, IPnoaddr);
+ return -1;
+}