parlib: Don't require zeroing for uthread_init()
[akaros.git] / user / iplib / myipaddr.c
index 05873ab..ff3ffb9 100644 (file)
@@ -6,6 +6,7 @@
  * modified, propagated, or distributed except according to the terms contained
  * in the LICENSE file.
  */
+#define _GNU_SOURCE
 #include <stdlib.h>
 
 #include <iplib/iplib.h>
 #include <stdio.h>
 #include <unistd.h>
 
-static uint8_t loopbacknet[IPaddrlen] = {
-       0, 0, 0, 0,
-       0, 0, 0, 0,
-       0, 0, 0xff, 0xff,
-       127, 0, 0, 0
-};
-static uint8_t 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(uint8_t *ip, char *net)
 {
-       struct ipifc *nifc;
        struct iplifc *lifc;
        struct ipifc *ifc;
-       uint8_t mynet[IPaddrlen];
 
-       ifc = readipifc(net, NULL, -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) {
+       lifc = get_first_noloop_iplifc(net, &ifc);
+       if (!lifc) {
+               ipmove(ip, IPnoaddr);
+               return -1;
+       }
+       ipmove(ip, lifc->ip);
+       free_ipifc(ifc);
+       return 0;
+}
+
+/* Finds the default router from net/iproute, stores the full IP address
+ * (IPaddrlen) in addr.  Returns 0 on success. */
+int my_router_addr(uint8_t *addr, char *net)
+{
+       char *line = NULL;
+       size_t len = 0;
+       ssize_t read;
+       FILE *fp;
+       char *p, *str;
+       uint8_t ipaddr[IPaddrlen];
+       uint8_t v4addr[IPaddrlen];
+       char netpath[MAX_PATH_LEN];
+       int ret = -1;
+
+       if (!net)
+               net = "/net";
+       snprintf(netpath, sizeof(netpath), "%s/iproute", net);
+       fp = fopen(netpath, "r");
+       if (!fp)
+               return -1;
+       while ((read = getline(&line, &len, fp)) != -1) {
+               p = strchr(line, ' ');
+               if (!p) {
+                       werrstr("Malformed line, no initial space");
+                       goto out;
+               }
+               *p++ = 0;
+               parseip(ipaddr, line);
+               if (isv4(ipaddr)) {
+                       v6tov4(v4addr, ipaddr);
+                       if (!equivip4(v4addr, IPnoaddr))
+                               continue;
+               } else {
+                       if (!equivip6(ipaddr, IPnoaddr))
                                continue;
-                       }
-                       if (ipcmp(lifc->ip, IPnoaddr) != 0) {
-                               ipmove(ip, lifc->ip);
-                               free_ipifc(ifc);
-                               return 0;
-                       }
                }
-       ipmove(ip, IPnoaddr);
-       free_ipifc(ifc);
-       return -1;
+               p = strchr(p, ' ');
+               if (!p) {
+                       werrstr("Malformed line, no second space %s", line);
+                       goto out;
+               }
+               p++;
+               str = p;
+               p = strchr(p, ' ');
+               if (!p) {
+                       werrstr("Malformed line, no third space %s", line);
+                       goto out;
+               }
+               *p++ = 0;
+               parseip(addr, str);
+               ret = 0;
+       }
+out:
+       free(line);
+       fclose(fp);
+       return ret;
 }