iplib: Add get_first_noloop_iplifc() helper
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 22 Dec 2016 16:25:52 +0000 (11:25 -0500)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 10 Jan 2017 00:01:40 +0000 (19:01 -0500)
This is the guts of myipaddr().  I need to get a few more things from the
iplifc, such as the mask.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/iplib/include/iplib/iplib.h
user/iplib/myipaddr.c
user/iplib/readipifc.c

index 157e639..3dad453 100644 (file)
@@ -196,6 +196,7 @@ int equivip4(uint8_t*, uint8_t*);
 int    equivip6(uint8_t*, uint8_t*);
 
 struct ipifc *readipifc(char *net, struct ipifc *to_free, int index);
+struct iplifc *get_first_noloop_iplifc(char *net, struct ipifc **ifc);
 void free_ipifc(struct ipifc *ifc);
 
 void   hnputv(void*, uint64_t);
index 05873ab..edbdec5 100644 (file)
 #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) {
-                               continue;
-                       }
-                       if (ipcmp(lifc->ip, IPnoaddr) != 0) {
-                               ipmove(ip, lifc->ip);
-                               free_ipifc(ifc);
-                               return 0;
-                       }
-               }
-       ipmove(ip, IPnoaddr);
+       lifc = get_first_noloop_iplifc(net, &ifc);
+       if (!lifc) {
+               ipmove(ip, IPnoaddr);
+               return -1;
+       }
+       ipmove(ip, lifc->ip);
        free_ipifc(ifc);
-       return -1;
+       return 0;
 }
index f25b438..606fd32 100644 (file)
 #include <sys/stat.h>
 #include <sys/types.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
+};
+
 #define NFIELD 200
 #define nelem(x) (sizeof(x) / sizeof(x[0]))
 static struct ipifc **_readoldipifc(char *buf, struct ipifc **l, int index)
@@ -216,3 +229,25 @@ struct ipifc *readipifc(char *net, struct ipifc *ifc, int index)
 
        return ifc;
 }
+
+/* Gets the local interface that isn't the friggin' loopback address.  When
+ * you're done, free_ipifc(ifc).  Returns 0 on failure. */
+struct iplifc *get_first_noloop_iplifc(char *net, struct ipifc **ifc)
+{
+       struct ipifc *nifc;
+       struct iplifc *lifc;
+       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)
+                               continue;
+                       if (ipcmp(lifc->ip, IPnoaddr) != 0)
+                               return lifc;
+               }
+       }
+       free_ipifc(*ifc);
+       return 0;
+}