Support more than one clatd at a time.

The only change necessary is to stop calling the clat interface
"clat4" and instead name it based on the upstream interface
(specifically, "v4-<interface>".

Bug: 12111730
Change-Id: I489ebb0e3b1408ace8b64ca09ec20366fefe8d83
diff --git a/clatd.c b/clatd.c
index 4b6b8cc..f5d625d 100644
--- a/clatd.c
+++ b/clatd.c
@@ -51,7 +51,7 @@
 #include "getaddr.h"
 #include "dump.h"
 
-#define DEVICENAME4 "clat4"
+#define DEVICEPREFIX "v4-"
 
 /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
 #define MTU_DELTA 28
@@ -156,11 +156,11 @@
  */
 void interface_poll(const struct tun_data *tunnel) {
   union anyip *interface_ip;
+  char *interface = Global_Clatd_Config.default_pdp_interface;
 
-  interface_ip = getinterface_ip(Global_Clatd_Config.default_pdp_interface, AF_INET6);
+  interface_ip = getinterface_ip(interface, AF_INET6);
   if(!interface_ip) {
-    logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s",
-           Global_Clatd_Config.default_pdp_interface);
+    logmsg(ANDROID_LOG_WARN,"unable to find an ipv6 ip on interface %s", interface);
     return;
   }
 
@@ -441,8 +441,7 @@
   char *uplink_interface = NULL, *plat_prefix = NULL, *net_id_str = NULL, *mark_str = NULL;
   unsigned net_id = NETID_UNSET;
   uint32_t mark = MARK_UNSET;
-
-  strcpy(tunnel.device4, DEVICENAME4);
+  unsigned len;
 
   while((opt = getopt(argc, argv, "i:p:n:m:h")) != -1) {
     switch(opt) {
@@ -482,6 +481,12 @@
     exit(1);
   }
 
+  len = snprintf(tunnel.device4, sizeof(tunnel.device4), "%s%s", DEVICEPREFIX, uplink_interface);
+  if (len >= sizeof(tunnel.device4)) {
+    logmsg(ANDROID_LOG_FATAL, "interface name too long '%s'", tunnel.device4);
+    exit(1);
+  }
+
   logmsg(ANDROID_LOG_INFO, "Starting clat version %s on %s netid=%s mark=%s",
          CLATD_VERSION, uplink_interface,
          net_id_str ? net_id_str : "(none)",
diff --git a/clatd.h b/clatd.h
index ca7369b..e7cef43 100644
--- a/clatd.h
+++ b/clatd.h
@@ -23,7 +23,7 @@
 
 #define MAXMTU 1500
 #define PACKETLEN (MAXMTU+sizeof(struct tun_pi))
-#define CLATD_VERSION "1.3"
+#define CLATD_VERSION "1.4"
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))