Make DNS request for plat prefix detection network specific.

When plat prefix detection is done on a non-default network it
must use the network specific version of getaddrinfo() so the
plat prefix corresponds to the particular network.  The network
is specified to clatd via a NetID command line argument.

(cherry picked from commit a1c871c8efad6c0b69e27d3b85e82a27e282b8be)

Change-Id: I270c5afc4b445cf20e95fd3e58eada6bc24b16ef
diff --git a/clatd.c b/clatd.c
index 63c4d6d..7b8e43f 100644
--- a/clatd.c
+++ b/clatd.c
@@ -42,6 +42,7 @@
 #include "clatd.h"
 #include "config.h"
 #include "logging.h"
+#include "resolv_netid.h"
 #include "setif.h"
 #include "setroute.h"
 #include "mtu.h"
@@ -252,11 +253,12 @@
  * uplink_interface - network interface to use to reach the ipv6 internet
  * plat_prefix      - PLAT prefix to use
  * tunnel           - tun device data
+ * net_id           - NetID to use, NETID_UNSET indicates use of default network
  */
-void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel) {
+void configure_interface(const char *uplink_interface, const char *plat_prefix, struct tun_data *tunnel, unsigned net_id) {
   int error;
 
-  if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix)) {
+  if(!read_config("/system/etc/clatd.conf", uplink_interface, plat_prefix, net_id)) {
     logmsg(ANDROID_LOG_FATAL,"read_config failed");
     exit(1);
   }
@@ -374,6 +376,7 @@
   printf("android-clat arguments:\n");
   printf("-i [uplink interface]\n");
   printf("-p [plat prefix]\n");
+  printf("-n [NetId]\n");
 }
 
 /* function: main
@@ -382,12 +385,13 @@
 int main(int argc, char **argv) {
   struct tun_data tunnel;
   int opt;
-  char *uplink_interface = NULL, *plat_prefix = NULL;
+  char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL;
+  unsigned net_id = NETID_UNSET;
 
   strcpy(tunnel.device6, DEVICENAME6);
   strcpy(tunnel.device4, DEVICENAME4);
 
-  while((opt = getopt(argc, argv, "i:p:h")) != -1) {
+  while((opt = getopt(argc, argv, "i:p:n:h")) != -1) {
     switch(opt) {
       case 'i':
         uplink_interface = optarg;
@@ -395,6 +399,9 @@
       case 'p':
         plat_prefix = optarg;
         break;
+      case 'n':
+        net_id_str = optarg;
+        break;
       case 'h':
       default:
         print_help();
@@ -408,6 +415,14 @@
     printf("I need an interface\n");
     exit(1);
   }
+  if (net_id_str != NULL) {
+    char *end_ptr;
+    net_id = strtoul(net_id_str, &end_ptr, 0);
+    if (net_id == ULONG_MAX || *net_id_str == 0 || *end_ptr != 0) {
+      logmsg(ANDROID_LOG_FATAL, "clatd called with invalid NetID %s", net_id_str);
+      exit(1);
+    }
+  }
   logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s", CLATD_VERSION, uplink_interface);
 
   // open the tunnel device before dropping privs
@@ -438,7 +453,7 @@
   // "local", but that only works for the netd process itself.
   unsetenv("ANDROID_DNS_MODE");
 
-  configure_interface(uplink_interface, plat_prefix, &tunnel);
+  configure_interface(uplink_interface, plat_prefix, &tunnel, net_id);
 
   set_forwarding(forwarding_fd,"1\n");