[RFCLAT#9] refactor getinterface_ip() to stop using libnl

The interface address query doesn't use libnl anymore.
Needed for reducing the library dependency. Once the last libnl user
del_anycast_address() is removed, clatd can remove libnl. Less
dependency helps moving clatd to mainline.

parse_ifaddrmsg() is used to replaced with getaddr_cb() which updates
struct target variable until walking through all messages.
parse_ifaddrmsg() keeps the same behavior as getaddr_cb().

Bug: 212345928
Test: atest clatd_test and manual test
1. Connect to ipv6-only wifi.
2. Try IPv4 traffic.
   $ ping 8.8.8.8

Change-Id: I56591ec851d3202e18cce9fce3995d658d9f07e2
diff --git a/getaddr.c b/getaddr.c
index 6f4bc86..f25efc1 100644
--- a/getaddr.c
+++ b/getaddr.c
@@ -15,21 +15,27 @@
  *
  * getaddr.c - get a locally configured address
  */
-#include <net/if.h>
-#include <netinet/in.h>
-#include <string.h>
-#include <strings.h>
+#include "getaddr.h"
 
+#include <errno.h>
 #include <linux/if_addr.h>
 #include <linux/rtnetlink.h>
+#include <net/if.h>
+#include <netinet/in.h>
 #include <netlink/handlers.h>
 #include <netlink/msg.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
 
-#include "getaddr.h"
 #include "logging.h"
-#include "netlink_msg.h"
 
-// shared state between getinterface_ip and getaddr_cb
+// Kernel suggests that keep the packet under 8KiB (NLMSG_GOODSIZE) in include/linux/netlink.h.
+#define NLMSG_SIZE 8192
+
+// shared state between getinterface_ip and parse_ifaddrmsg
+// TODO: refactor the communication between getinterface_ip and parse_ifaddrmsg because there
+// is no netlink callback anymore.
 struct target {
   int family;
   unsigned int ifindex;
@@ -37,57 +43,97 @@
   int foundip;
 };
 
-/* function: getaddr_cb
- * callback for getinterface_ip
- *   msg  - netlink message
- *   data - (struct target) info for which address we're looking for
+/* function: parse_ifaddrmsg
+ * parse ifaddrmsg for getinterface_ip
+ *   nh  - netlink message header
+ *   targ_p - (struct target) info for which address we're looking for
+ *            and the parsed result if any.
  */
-static int getaddr_cb(struct nl_msg *msg, void *data) {
+static void parse_ifaddrmsg(struct nlmsghdr *nh, struct target *targ_p) {
   struct ifaddrmsg *ifa_p;
   struct rtattr *rta_p;
   int rta_len;
-  struct target *targ_p = (struct target *)data;
 
-  ifa_p = (struct ifaddrmsg *)nlmsg_data(nlmsg_hdr(msg));
+  ifa_p = (struct ifaddrmsg *)NLMSG_DATA(nh);
   rta_p = (struct rtattr *)IFA_RTA(ifa_p);
 
-  if (ifa_p->ifa_index != targ_p->ifindex) return NL_OK;
+  if (ifa_p->ifa_index != targ_p->ifindex) return;
 
-  if (ifa_p->ifa_scope != RT_SCOPE_UNIVERSE) return NL_OK;
+  if (ifa_p->ifa_scope != RT_SCOPE_UNIVERSE) return;
 
-  rta_len = RTM_PAYLOAD(nlmsg_hdr(msg));
+  rta_len = IFA_PAYLOAD(nh);
   for (; RTA_OK(rta_p, rta_len); rta_p = RTA_NEXT(rta_p, rta_len)) {
     switch (rta_p->rta_type) {
       case IFA_ADDRESS:
         if ((targ_p->family == AF_INET6) && !(ifa_p->ifa_flags & IFA_F_SECONDARY)) {
           memcpy(&targ_p->ip.ip6, RTA_DATA(rta_p), rta_p->rta_len - sizeof(struct rtattr));
           targ_p->foundip = 1;
-          return NL_OK;
+          return;
         }
         break;
       case IFA_LOCAL:
         if (targ_p->family == AF_INET) {
           memcpy(&targ_p->ip.ip4, RTA_DATA(rta_p), rta_p->rta_len - sizeof(struct rtattr));
           targ_p->foundip = 1;
-          return NL_OK;
+          return;
         }
         break;
     }
   }
-
-  return NL_OK;
 }
 
-/* function: error_handler
- * error callback for getinterface_ip
- *   nla  - source of the error message
- *   err  - netlink message
- *   arg  - (struct target) info for which address we're looking for
- */
-static int error_handler(__attribute__((unused)) struct sockaddr_nl *nla,
-                         __attribute__((unused)) struct nlmsgerr *err,
-                         __attribute__((unused)) void *arg) {
-  return NL_OK;
+void sendrecv_ifaddrmsg(struct target *targ_p) {
+  int s = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
+  if (s < 0) {
+    logmsg(ANDROID_LOG_ERROR, "open NETLINK_ROUTE socket failed %s", strerror(errno));
+    return;
+  }
+
+  // Fill in netlink structures.
+  struct {
+    struct nlmsghdr n;
+    struct ifaddrmsg r;
+  } req = {
+    // Netlink message header.
+    .n.nlmsg_len   = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
+    .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT,
+    .n.nlmsg_type  = RTM_GETADDR,
+
+    // Interface address message header.
+    .r.ifa_family = targ_p->family,
+  };
+
+  // Send interface address message.
+  if ((send(s, &req, req.n.nlmsg_len, 0)) < 0) {
+    logmsg(ANDROID_LOG_ERROR, "send netlink socket failed %s", strerror(errno));
+    close(s);
+    return;
+  }
+
+  // Read interface address message and parse the result if any.
+  ssize_t bytes_read;
+  char buf[NLMSG_SIZE];
+  while ((bytes_read = recv(s, buf, sizeof(buf), 0)) > 0) {
+    struct nlmsghdr *nh = (struct nlmsghdr *)buf;
+    for (; NLMSG_OK(nh, bytes_read); nh = NLMSG_NEXT(nh, bytes_read)) {
+      if (nh->nlmsg_type == NLMSG_DONE) {
+        close(s);
+        return;
+      }
+      if (nh->nlmsg_type == NLMSG_ERROR) {
+        logmsg(ANDROID_LOG_ERROR, "netlink message error");
+        close(s);
+        return;
+      }
+      if (nh->nlmsg_type == RTM_NEWADDR) {
+        // Walk through the all messages and update struct target variable as the deleted
+        // callback behavior of getaddr_cb() which always returns NL_OK.
+        // TODO: review if this can early return once address has been found.
+        parse_ifaddrmsg(nh, targ_p);
+      }
+    }
+  }
+  close(s);
 }
 
 /* function: getinterface_ip
@@ -97,42 +143,28 @@
  *   family    - family
  */
 union anyip *getinterface_ip(const char *interface, int family) {
-  struct ifaddrmsg ifa;
-  struct nl_cb *callbacks = NULL;
-  struct target targ;
   union anyip *retval = NULL;
+  struct target targ  = {
+    .family  = family,
+    .foundip = 0,
+    .ifindex = if_nametoindex(interface),
+  };
 
-  targ.family  = family;
-  targ.foundip = 0;
-  targ.ifindex = if_nametoindex(interface);
   if (targ.ifindex == 0) {
     return NULL;  // interface not found
   }
 
-  memset(&ifa, 0, sizeof(ifa));
-  ifa.ifa_family = targ.family;
-
-  callbacks = nl_cb_alloc(NL_CB_DEFAULT);
-  if (!callbacks) {
-    goto cleanup;
-  }
-  nl_cb_set(callbacks, NL_CB_VALID, NL_CB_CUSTOM, getaddr_cb, &targ);
-  nl_cb_err(callbacks, NL_CB_CUSTOM, error_handler, &targ);
-
-  // sends message and waits for a response
-  send_ifaddrmsg(RTM_GETADDR, NLM_F_REQUEST | NLM_F_ROOT, &ifa, callbacks);
+  // sends message and receives the response.
+  sendrecv_ifaddrmsg(&targ);
 
   if (targ.foundip) {
     retval = malloc(sizeof(union anyip));
     if (!retval) {
       logmsg(ANDROID_LOG_FATAL, "getinterface_ip/out of memory");
-      goto cleanup;
+      return NULL;
     }
     memcpy(retval, &targ.ip, sizeof(union anyip));
   }
 
-cleanup:
-  if (callbacks) nl_cb_put(callbacks);
-
   return retval;
 }