summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2022-03-04 08:09:39 +0100
committerBeniamino Galvani <bgalvani@redhat.com>2022-03-09 08:34:26 +0100
commit949870224283ad00073c1685de1568c683467424 (patch)
tree0dc6a836c74b013613d083a552b367f7f640b025
parentc41ad0ebe3f607798a770090d60816c86c826a1c (diff)
downloadNetworkManager-949870224283ad00073c1685de1568c683467424.tar.gz
core: add nm_utils_shorten_hostname()
Add a function to shorten a overlong hostname, truncating it to the first dot or 64 characters.
-rw-r--r--src/core/nm-core-utils.c48
-rw-r--r--src/core/nm-core-utils.h1
-rw-r--r--src/core/tests/test-utils.c57
3 files changed, 106 insertions, 0 deletions
diff --git a/src/core/nm-core-utils.c b/src/core/nm-core-utils.c
index e05638e43c..ed7ad691c7 100644
--- a/src/core/nm-core-utils.c
+++ b/src/core/nm-core-utils.c
@@ -30,6 +30,7 @@
#include "libnm-glib-aux/nm-secret-utils.h"
#include "libnm-glib-aux/nm-time-utils.h"
#include "libnm-glib-aux/nm-str-buf.h"
+#include "libnm-systemd-shared/nm-sd-utils-shared.h"
#include "nm-utils.h"
#include "libnm-core-intern/nm-core-internal.h"
#include "nm-setting-connection.h"
@@ -5201,3 +5202,50 @@ again:
return g;
}
+
+/*****************************************************************************/
+
+/**
+ * nm_utils_shorten_hostname:
+ * @hostname: the input hostname
+ * @shortened: (out) (transfer full): on return, the shortened hostname
+ *
+ * Checks whether the input hostname is valid. If not, tries to shorten it
+ * to HOST_NAME_MAX or to the first dot, whatever comes earlier.
+ * The new hostname is returned in @shortened.
+ *
+ * Returns: %TRUE if the input hostname was already valid or if was shortened
+ * successfully; %FALSE otherwise
+ */
+gboolean
+nm_utils_shorten_hostname(const char *hostname, char **shortened)
+{
+ gs_free char *s = NULL;
+ const char *dot;
+ gsize l;
+
+ nm_assert(hostname);
+ nm_assert(shortened);
+
+ if (nm_sd_hostname_is_valid(hostname, FALSE)) {
+ *shortened = NULL;
+ return TRUE;
+ }
+
+ dot = strchr(hostname, '.');
+ if (dot)
+ l = (dot - hostname);
+ else
+ l = strlen(hostname);
+ l = MIN(l, (gsize) HOST_NAME_MAX);
+
+ s = g_strndup(hostname, l);
+
+ if (!nm_sd_hostname_is_valid(s, FALSE)) {
+ *shortened = NULL;
+ return FALSE;
+ }
+
+ *shortened = g_steal_pointer(&s);
+ return TRUE;
+}
diff --git a/src/core/nm-core-utils.h b/src/core/nm-core-utils.h
index 7079ab2f25..5594e7371d 100644
--- a/src/core/nm-core-utils.h
+++ b/src/core/nm-core-utils.h
@@ -233,6 +233,7 @@ void nm_utils_log_connection_diff(NMConnection *connection,
const char *dbus_path);
gboolean nm_utils_is_specific_hostname(const char *name);
+gboolean nm_utils_shorten_hostname(const char *hostname, char **shortened);
struct _NMUuid;
diff --git a/src/core/tests/test-utils.c b/src/core/tests/test-utils.c
index 2d2b73404c..5642f26b73 100644
--- a/src/core/tests/test-utils.c
+++ b/src/core/tests/test-utils.c
@@ -212,6 +212,62 @@ test_hw_addr_gen_stable_eth(void)
"04:0D:CD:0C:9E:2C");
}
+static void
+test_shorten_hostname(void)
+{
+ gboolean res;
+ char *shortened = NULL;
+
+ res = nm_utils_shorten_hostname("name1", &shortened);
+ g_assert_cmpint(res, ==, TRUE);
+ g_assert_cmpstr(shortened, ==, NULL);
+ nm_clear_g_free(&shortened);
+
+ res = nm_utils_shorten_hostname("name1.example.com", &shortened);
+ g_assert_cmpint(res, ==, TRUE);
+ g_assert_cmpstr(shortened, ==, NULL);
+ nm_clear_g_free(&shortened);
+
+ res = nm_utils_shorten_hostname(
+ "123456789-123456789-123456789-123456789-123456789-123456789-1234",
+ &shortened);
+ g_assert_cmpint(res, ==, TRUE);
+ g_assert_cmpstr(shortened, ==, NULL);
+ nm_clear_g_free(&shortened);
+
+ res = nm_utils_shorten_hostname(
+ "123456789-123456789-123456789-123456789-123456789-123456789-12345",
+ &shortened);
+ g_assert_cmpint(res, ==, TRUE);
+ g_assert_cmpstr(shortened,
+ ==,
+ "123456789-123456789-123456789-123456789-123456789-123456789-1234");
+ nm_clear_g_free(&shortened);
+
+ res = nm_utils_shorten_hostname(
+ "name1.test-dhcp-this-one-here-is-a-very-very-long-domain.example.com",
+ &shortened);
+ g_assert_cmpint(res, ==, TRUE);
+ g_assert_cmpstr(shortened, ==, "name1");
+ nm_clear_g_free(&shortened);
+
+ res = nm_utils_shorten_hostname(
+ "test-dhcp-this-one-here-is-a-very-very-long-hostname-without-domainname",
+ &shortened);
+ g_assert_cmpint(res, ==, TRUE);
+ g_assert_cmpstr(shortened,
+ ==,
+ "test-dhcp-this-one-here-is-a-very-very-long-hostname-without-dom");
+ nm_clear_g_free(&shortened);
+
+ res = nm_utils_shorten_hostname(
+ ".test-dhcp-this-one-here-is-a-very-very-long-hostname.example.com",
+ &shortened);
+ g_assert_cmpint(res, ==, FALSE);
+ g_assert_cmpstr(shortened, ==, NULL);
+ nm_clear_g_free(&shortened);
+}
+
/*****************************************************************************/
NMTST_DEFINE();
@@ -223,6 +279,7 @@ main(int argc, char **argv)
g_test_add_func("/utils/stable_privacy", test_stable_privacy);
g_test_add_func("/utils/hw_addr_gen_stable_eth", test_hw_addr_gen_stable_eth);
+ g_test_add_func("/utils/shorten-hostname", test_shorten_hostname);
return g_test_run();
}