From 30b63f19fedde7b60bf99f3a68c0437a0c93b64e Mon Sep 17 00:00:00 2001
From: "A. Wilcox" <AWilcox@Wilcox-Tech.com>
Date: Thu, 24 Oct 2019 01:34:51 -0500
Subject: hscript: Finally implement R.V.network.netaddress.Interface

---
 hscript/network.cc                                 | 31 ++++++++++++++++++++--
 tests/fixtures/0064-netssid-simple-wpa.installfile |  1 +
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/hscript/network.cc b/hscript/network.cc
index e8f27b9..14e33b4 100644
--- a/hscript/network.cc
+++ b/hscript/network.cc
@@ -231,8 +231,35 @@ Key *NetAddress::parseFromData(const std::string &data, int lineno, int *errors,
     }
 }
 
-bool NetAddress::validate(ScriptOptions) const {
-    /* possible to validate an address in the Installation Environment? */
+bool NetAddress::validate(ScriptOptions opts) const {
+    if(!opts.test(InstallEnvironment)) {
+        return true;
+    }
+
+#ifdef HAS_INSTALL_ENV
+    /* Retrieving the index is always valid, and is not even privileged. */
+    struct ifreq request;
+    int my_sock = ::socket(AF_INET, SOCK_STREAM, 0);
+    if(my_sock == -1) {
+        output_error("installfile:" + std::to_string(this->lineno()),
+                     "netaddress: can't open socket", ::strerror(errno));
+        return false;
+    }
+    memset(&request, 0, sizeof(request));
+    memcpy(&request.ifr_name, _iface.c_str(), _iface.size());
+    errno = 0;
+    if(ioctl(my_sock, SIOCGIFFLAGS, &request) == -1) {
+        if(errno == ENODEV) {
+            output_warning("installfile:" + std::to_string(this->lineno()),
+                           "netaddress: specified interface does not exist");
+            return true;
+        }
+        output_error("installfile:" + std::to_string(this->lineno()),
+                     "netaddress: trouble communicating with interface",
+                     ::strerror(errno));
+        return false;
+    }
+#endif
     return true;
 }
 
diff --git a/tests/fixtures/0064-netssid-simple-wpa.installfile b/tests/fixtures/0064-netssid-simple-wpa.installfile
index c49c428..1cec2e1 100644
--- a/tests/fixtures/0064-netssid-simple-wpa.installfile
+++ b/tests/fixtures/0064-netssid-simple-wpa.installfile
@@ -4,3 +4,4 @@ pkginstall adelie-base
 rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/
 mount /dev/sda1 /
 netssid wlan0 "Fox5G" wpa shhsekrit
+netaddress wlan0 dhcp
-- 
cgit v1.2.3-70-g09d2