From c04904a4f54f8949a6a7821a0859e2732366259b Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Tue, 24 Nov 2020 19:57:38 +0800
Subject: [PATCH] treat underscore as valid hostname char

---
 src/basic/hostname-util.c                 | 14 ++++++++++++--
 test/test-network-generator-conversion.sh |  2 +-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c
index e743033..8464164 100644
--- a/src/basic/hostname-util.c
+++ b/src/basic/hostname-util.c
@@ -80,6 +80,16 @@ bool valid_ldh_char(char c) {
                 c == '-';
 }
 
+static bool hostname_valid_char(char c) {
+        return
+                (c >= 'a' && c <= 'z') ||
+                (c >= 'A' && c <= 'Z') ||
+                (c >= '0' && c <= '9') ||
+                c == '-' ||
+                c == '_' ||
+                c == '.';
+}
+
 bool hostname_is_valid(const char *s, ValidHostnameFlags flags) {
         unsigned n_dots = 0;
         const char *p;
@@ -116,7 +126,7 @@ bool hostname_is_valid(const char *s, ValidHostnameFlags flags) {
                         hyphen = true;
 
                 } else {
-                        if (!valid_ldh_char(*p))
+                        if (!hostname_valid_char(*p))
                                 return false;
 
                         dot = false;
@@ -158,7 +168,7 @@ char* hostname_cleanup(char *s) {
                         dot = false;
                         hyphen = true;
 
-                } else if (valid_ldh_char(*p)) {
+                } else if (hostname_valid_char(*p)) {
                         *(d++) = *p;
                         dot = false;
                         hyphen = false;
diff --git a/test/test-network-generator-conversion.sh b/test/test-network-generator-conversion.sh
index 6224a4d..05ef833 100755
--- a/test/test-network-generator-conversion.sh
+++ b/test/test-network-generator-conversion.sh
@@ -283,6 +283,7 @@ COMMAND_LINES=(
     "ip=:::::dhcp99:dhcp6:10.0.0.128:[fdef:c400:bd01:1096::bbbb]"
     "ip=::::::any"
     "ip=::::::ibft"
+    "ip=10.0.0.1:::255.255.255.0:valid_hostname:foo99:off"
 )
 for cmdline in "${COMMAND_LINES[@]}"; do
     check_one_long "$cmdline"
@@ -294,7 +295,6 @@ INVALID_COMMAND_LINES=(
     "ip=:::::::foo"
     "ip=10.0.0:::255.255.255.0::foo99:off"
     "ip=10.0.0.1:::255.255.255::foo99:off"
-    "ip=10.0.0.1:::255.255.255.0:invalid_hostname:foo99:off"
     "ip=10.0.0.1:::255.255.255.0::verylonginterfacename:off"
     "ip=:::::dhcp99:dhcp6:0"
     "ip=:::::dhcp99:dhcp6:-1"
-- 
2.39.1

