Merge changes from topic "ebpfclat"

* changes:
  ClatUtils - implement hardwareAddressType()
  system/netd/server - add ClatUtils.{cpp,h} skeleton
diff --git a/server/Android.bp b/server/Android.bp
index da1797b..1128252 100644
--- a/server/Android.bp
+++ b/server/Android.bp
@@ -56,6 +56,7 @@
     srcs: [
         "BandwidthController.cpp",
         "ClatdController.cpp",
+        "ClatUtils.cpp",
         "Controllers.cpp",
         "Dns64Configuration.cpp",
         "NetdConstants.cpp",
@@ -185,6 +186,7 @@
     srcs: [
         "BandwidthControllerTest.cpp",
         "ClatdControllerTest.cpp",
+        "ClatUtilsTest.cpp",
         "ControllersTest.cpp",
         "FirewallControllerTest.cpp",
         "IdletimerControllerTest.cpp",
diff --git a/server/ClatUtils.cpp b/server/ClatUtils.cpp
new file mode 100644
index 0000000..6edc97a
--- /dev/null
+++ b/server/ClatUtils.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#include "ClatUtils.h"
+
+#include <errno.h>
+#include <linux/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#define LOG_TAG "ClatUtils"
+#include <log/log.h>
+
+#include "android-base/unique_fd.h"
+
+namespace android {
+namespace net {
+
+int hardwareAddressType(const std::string& interface) {
+    base::unique_fd ufd(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
+
+    if (ufd < 0) {
+        const int err = errno;
+        ALOGE("socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)");
+        return -err;
+    };
+
+    struct ifreq ifr = {};
+    // We use strncpy() instead of strlcpy() since kernel has to be able
+    // to handle non-zero terminated junk passed in by userspace anyway,
+    // and this way too long interface names (more than IFNAMSIZ-1 = 15
+    // characters plus terminating NULL) will not get truncated to 15
+    // characters and zero-terminated and thus potentially erroneously
+    // match a truncated interface if one were to exist.
+    strncpy(ifr.ifr_name, interface.c_str(), sizeof(ifr.ifr_name));
+
+    if (ioctl(ufd, SIOCGIFHWADDR, &ifr, sizeof(ifr))) return -errno;
+
+    return ifr.ifr_hwaddr.sa_family;
+}
+
+}  // namespace net
+}  // namespace android
diff --git a/server/ClatUtils.h b/server/ClatUtils.h
new file mode 100644
index 0000000..b5248f4
--- /dev/null
+++ b/server/ClatUtils.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#ifndef _CLAT_UTILS_H
+#define _CLAT_UTILS_H
+
+#include <string>
+
+namespace android {
+namespace net {
+
+int hardwareAddressType(const std::string& interface);
+
+}  // namespace net
+}  // namespace android
+
+#endif
diff --git a/server/ClatUtilsTest.cpp b/server/ClatUtilsTest.cpp
new file mode 100644
index 0000000..79d3440
--- /dev/null
+++ b/server/ClatUtilsTest.cpp
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2019 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.
+ *
+ * ClatUtilsTest.cpp - unit tests for ClatUtils.cpp
+ */
+
+#include <gtest/gtest.h>
+
+#include "ClatUtils.h"
+
+#include <linux/if_arp.h>
+
+namespace android {
+namespace net {
+
+class ClatUtilsTest : public ::testing::Test {
+  public:
+    void SetUp() {}
+};
+
+TEST_F(ClatUtilsTest, HardwareAddressTypeOfNonExistingIf) {
+    EXPECT_EQ(-ENODEV, hardwareAddressType("not_existing_if"));
+}
+
+TEST_F(ClatUtilsTest, HardwareAddressTypeOfLoopback) {
+    EXPECT_EQ(ARPHRD_LOOPBACK, hardwareAddressType("lo"));
+}
+
+// If wireless 'wlan0' interface exists it should be Ethernet.
+TEST_F(ClatUtilsTest, HardwareAddressTypeOfWireless) {
+    int type = hardwareAddressType("wlan0");
+    if (type == -ENODEV) return;
+
+    EXPECT_EQ(ARPHRD_ETHER, type);
+}
+
+// If cellular 'rmnet_data0' interface exists it should
+// *probably* not be Ethernet and instead be RawIp.
+TEST_F(ClatUtilsTest, HardwareAddressTypeOfCellular) {
+    int type = hardwareAddressType("rmnet_data0");
+    if (type == -ENODEV) return;
+
+    EXPECT_NE(ARPHRD_ETHER, type);
+
+    // ARPHRD_RAWIP is 530 on some pre-4.14 Qualcomm devices.
+    if (type == 530) return;
+
+    EXPECT_EQ(ARPHRD_RAWIP, type);
+}
+
+}  // namespace net
+}  // namespace android