[automerger skipped] cleanup - config_item_ip6() am: 533e31f4ea -s ours

am skip reason: Change-Id I0338f7ac29fdb0de56bd4a500e2a827cab2c47f7 with SHA-1 c9b0e6d1ab is in history

Original change: https://googleplex-android-review.googlesource.com/c/platform/external/android-clat/+/11736255

Change-Id: I4c513702d598d906a0e33b6c5f97edcaba8b73c6
diff --git a/Android.bp b/Android.bp
index bff97d3..ac24a00 100644
--- a/Android.bp
+++ b/Android.bp
@@ -20,7 +20,6 @@
 filegroup {
     name: "clatd_common",
     srcs: [
-        "config.c",
         "clatd.c",
         "dump.c",
         "getaddr.c",
@@ -66,12 +65,6 @@
     ],
 }
 
-// The configuration file.
-prebuilt_etc {
-    name: "clatd.conf",
-    src: "clatd.conf",
-}
-
 // Unit tests.
 cc_test {
     name: "clatd_test",
diff --git a/METADATA b/METADATA
new file mode 100644
index 0000000..d97975c
--- /dev/null
+++ b/METADATA
@@ -0,0 +1,3 @@
+third_party {
+  license_type: NOTICE
+}
diff --git a/clatd.c b/clatd.c
index 50693f5..7063215 100644
--- a/clatd.c
+++ b/clatd.c
@@ -50,7 +50,8 @@
 #include "ring.h"
 #include "setif.h"
 #include "translate.h"
-#include "tun.h"
+
+struct clat_config Global_Clatd_Config;
 
 /* 40 bytes IPv6 header - 20 bytes IPv4 header + 8 bytes fragment header */
 #define MTU_DELTA 28
@@ -98,7 +99,7 @@
   struct sockaddr_ll sll = {
     .sll_family   = AF_PACKET,
     .sll_protocol = htons(ETH_P_IPV6),
-    .sll_ifindex  = if_nametoindex(Global_Clatd_Config.default_pdp_interface),
+    .sll_ifindex  = if_nametoindex(Global_Clatd_Config.native_ipv6_interface),
     .sll_pkttype  = PACKET_OTHERHOST,  // The 464xlat IPv6 address is not assigned to the kernel.
   };
   if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) {
@@ -318,11 +319,7 @@
  */
 void configure_interface(const char *uplink_interface, const char *plat_prefix, const char *v4_addr,
                          const char *v6_addr, struct tun_data *tunnel, uint32_t mark) {
-  if (!read_config("/system/etc/clatd.conf", uplink_interface)) {
-    logmsg(ANDROID_LOG_FATAL, "read_config failed");
-    exit(1);
-  }
-
+  Global_Clatd_Config.native_ipv6_interface = uplink_interface;
   if (!plat_prefix || inet_pton(AF_INET6, plat_prefix, &Global_Clatd_Config.plat_subnet) <= 0) {
     logmsg(ANDROID_LOG_FATAL, "invalid IPv6 address specified for plat prefix: %s", plat_prefix);
     exit(1);
@@ -430,7 +427,7 @@
 
     time_t now = time(NULL);
     if (last_interface_poll < (now - INTERFACE_POLL_FREQUENCY)) {
-      if (ipv6_address_changed(Global_Clatd_Config.default_pdp_interface)) {
+      if (ipv6_address_changed(Global_Clatd_Config.native_ipv6_interface)) {
         break;
       }
     }
diff --git a/clatd.conf b/clatd.conf
deleted file mode 100644
index 36fcafd..0000000
--- a/clatd.conf
+++ /dev/null
@@ -1 +0,0 @@
-temporary_placeholder 0
diff --git a/clatd_test.cpp b/clatd_test.cpp
index e24b036..c16a4dd 100644
--- a/clatd_test.cpp
+++ b/clatd_test.cpp
@@ -34,7 +34,6 @@
 #include "getaddr.h"
 #include "netutils/checksum.h"
 #include "translate.h"
-#include "tun.h"
 }
 
 // For convenience.
@@ -585,7 +584,7 @@
     inet_pton(AF_INET, kIPv4LocalAddr, &Global_Clatd_Config.ipv4_local_subnet);
     inet_pton(AF_INET6, kIPv6PlatSubnet, &Global_Clatd_Config.plat_subnet);
     memset(&Global_Clatd_Config.ipv6_local_subnet, 0, sizeof(in6_addr));
-    Global_Clatd_Config.default_pdp_interface = const_cast<char *>(sTun.name().c_str());
+    Global_Clatd_Config.native_ipv6_interface = const_cast<char *>(sTun.name().c_str());
   }
 
   // Static because setting up the tun interface takes about 40ms.
@@ -631,8 +630,6 @@
 }
 
 TEST_F(ClatdTest, ConfigureTunIpManual) {
-  Global_Clatd_Config.ipv4_local_prefixlen = 29;
-
   // Create an interface for configure_tun_ip to configure and bring up.
   TunInterface v4Iface;
   ASSERT_EQ(0, v4Iface.init());
diff --git a/config.c b/config.c
deleted file mode 100644
index 554db0e..0000000
--- a/config.c
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright 2011 Daniel Drown
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * config.c - configuration settings
- */
-
-#include <arpa/inet.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <cutils/config_utils.h>
-#include <netutils/checksum.h>
-#include <netutils/ifc.h>
-
-#include "clatd.h"
-#include "config.h"
-#include "getaddr.h"
-#include "logging.h"
-
-struct clat_config Global_Clatd_Config;
-
-/* function: config_item_ip
- * locates the config item, parses the ipv4 address, and returns the pointer ret_val_ptr, or NULL on
- * failure
- *   root        - parsed configuration
- *   item_name   - name of config item to locate
- *   defaultvar  - value to use if config item isn't present
- *   ret_val_ptr - pointer for return value storage
- */
-struct in_addr *config_item_ip(cnode *root, const char *item_name, const char *defaultvar,
-                               struct in_addr *ret_val_ptr) {
-  const char *tmp;
-  int status;
-
-  if (!(tmp = config_str(root, item_name, defaultvar))) {
-    logmsg(ANDROID_LOG_FATAL, "%s config item needed", item_name);
-    return NULL;
-  }
-
-  status = inet_pton(AF_INET, tmp, ret_val_ptr);
-  if (status <= 0) {
-    logmsg(ANDROID_LOG_FATAL, "invalid IPv4 address specified for %s: %s", item_name, tmp);
-    return NULL;
-  }
-
-  return ret_val_ptr;
-}
-
-/* function: ipv6_prefix_equal
- * compares the prefixes two ipv6 addresses. assumes the prefix lengths are both /64.
- *   a1 - first address
- *   a2 - second address
- *   returns: 0 if the subnets are different, 1 if they are the same.
- */
-int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) { return !memcmp(a1, a2, 8); }
-
-/* function: read_config
- * reads the config file and parses it into the global variable Global_Clatd_Config. returns 0 on
- * failure, 1 on success
- *   file             - filename to parse
- *   uplink_interface - interface to use to reach the internet and supplier of address space
- */
-int read_config(const char *file, const char *uplink_interface) {
-  cnode *root   = config_node("", "");
-
-  if (!root) {
-    logmsg(ANDROID_LOG_FATAL, "out of memory");
-    return 0;
-  }
-
-  memset(&Global_Clatd_Config, '\0', sizeof(Global_Clatd_Config));
-
-  config_load_file(root, file);
-  if (root->first_child == NULL) {
-    logmsg(ANDROID_LOG_FATAL, "Could not read config file %s", file);
-    goto failed;
-  }
-
-  Global_Clatd_Config.default_pdp_interface = strdup(uplink_interface);
-  if (!Global_Clatd_Config.default_pdp_interface) goto failed;
-
-  if (!config_item_ip(root, "ipv4_local_subnet", DEFAULT_IPV4_LOCAL_SUBNET,
-                      &Global_Clatd_Config.ipv4_local_subnet))
-    goto failed;
-
-  Global_Clatd_Config.ipv4_local_prefixlen = 29;
-
-  return 1;
-
-failed:
-  free(root);
-  return 0;
-}
diff --git a/config.h b/config.h
index 555b968..1ba6850 100644
--- a/config.h
+++ b/config.h
@@ -21,19 +21,31 @@
 #include <linux/if.h>
 #include <netinet/in.h>
 
-#define DEFAULT_IPV4_LOCAL_SUBNET "192.0.0.4"
+#include "ring.h"
+
+struct tun_data {
+  char device4[IFNAMSIZ];
+  int read_fd6, write_fd6, fd4;
+  struct packet_ring ring;
+};
 
 struct clat_config {
   struct in6_addr ipv6_local_subnet;
   struct in_addr ipv4_local_subnet;
-  int16_t ipv4_local_prefixlen;
   struct in6_addr plat_subnet;
-  char *default_pdp_interface;
+  const char *native_ipv6_interface;
 };
 
 extern struct clat_config Global_Clatd_Config;
 
-int read_config(const char *file, const char *uplink_interface);
-int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2);
+/* function: ipv6_prefix_equal
+ * compares the /64 prefixes of two ipv6 addresses.
+ *   a1 - first address
+ *   a2 - second address
+ *   returns: 0 if the subnets are different, 1 if they are the same.
+ */
+static inline int ipv6_prefix_equal(struct in6_addr *a1, struct in6_addr *a2) {
+  return !memcmp(a1, a2, 8);
+}
 
 #endif /* __CONFIG_H__ */
diff --git a/main.c b/main.c
index c4834d9..683b507 100644
--- a/main.c
+++ b/main.c
@@ -32,7 +32,6 @@
 #include "config.h"
 #include "logging.h"
 #include "setif.h"
-#include "tun.h"
 
 #define DEVICEPREFIX "v4-"
 
diff --git a/ring.c b/ring.c
index 96a50ca..7626c6d 100644
--- a/ring.c
+++ b/ring.c
@@ -24,10 +24,10 @@
 #include <sys/mman.h>
 #include <sys/socket.h>
 
+#include "config.h"
 #include "logging.h"
 #include "ring.h"
 #include "translate.h"
-#include "tun.h"
 
 int ring_create(struct tun_data *tunnel) {
   // Will eventually be bound to htons(ETH_P_IPV6) protocol,
diff --git a/translate.c b/translate.c
index 1632753..728acc3 100644
--- a/translate.c
+++ b/translate.c
@@ -26,7 +26,6 @@
 #include "icmp.h"
 #include "logging.h"
 #include "translate.h"
-#include "tun.h"
 
 /* function: packet_checksum
  * calculates the checksum over all the packet components starting from pos
@@ -524,7 +523,7 @@
     if (iov_len > 0) {
       fill_tun_header(&tun_targ, ETH_P_IP);
       out[CLAT_POS_TUNHDR].iov_len = sizeof(tun_targ);
-      send_tun(fd, out, iov_len);
+      writev(fd, out, iov_len);
     }
   }
 }
diff --git a/tun.h b/tun.h
deleted file mode 100644
index eaeebc6..0000000
--- a/tun.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * tun.h - tun device functions
- */
-#ifndef __TUN_H__
-#define __TUN_H__
-
-#include <fcntl.h>
-#include <linux/if.h>
-#include <sys/uio.h>
-#include <unistd.h>
-
-#include "common.h"
-#include "ring.h"
-
-struct tun_data {
-  char device4[IFNAMSIZ];
-  int read_fd6, write_fd6, fd4;
-  struct packet_ring ring;
-};
-
-/* function: send_tun
- * sends a clat_packet to a tun interface
- * fd      - the tun filedescriptor
- * out     - the packet to send
- * iov_len - the number of entries in the clat_packet
- * returns: number of bytes read on success, -1 on failure
- */
-static inline int send_tun(int fd, clat_packet out, int iov_len) {
-  return writev(fd, out, iov_len);
-}
-
-#endif