diff options
| author | Beniamino Galvani <bgalvani@redhat.com> | 2020-11-11 22:55:58 +0100 |
|---|---|---|
| committer | Beniamino Galvani <bgalvani@redhat.com> | 2020-11-11 23:06:16 +0100 |
| commit | 344d190b2c842fb72291247af43820c8cb95f474 (patch) | |
| tree | 3f3b3ce79b209bf97238fa99b516de7d5d1b559a | |
| parent | a68903f05a098eaae2cfc2984cf12686a63bf6fd (diff) | |
| download | NetworkManager-344d190b2c842fb72291247af43820c8cb95f474.tar.gz | |
core: add nm_utils_shorten_hostname()
Add nm_utils_shorten_hostname() to shorten a hostname when too long.
| -rw-r--r-- | src/nm-core-utils.c | 46 | ||||
| -rw-r--r-- | src/nm-core-utils.h | 2 | ||||
| -rw-r--r-- | src/tests/test-utils.c | 41 |
3 files changed, 88 insertions, 1 deletions
diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 7adc011e30..0b62c86aff 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -34,6 +34,7 @@ #include "nm-setting-ip6-config.h" #include "nm-setting-wireless.h" #include "nm-setting-wireless-security.h" +#include "systemd/nm-sd-utils-shared.h" #ifdef __NM_SD_UTILS_H__ #error \ @@ -5119,3 +5120,48 @@ NM_UTILS_LOOKUP_STR_DEFINE(nm_activation_type_to_string, NM_UTILS_LOOKUP_STR_ITEM(NM_ACTIVATION_TYPE_MANAGED, "managed"), NM_UTILS_LOOKUP_STR_ITEM(NM_ACTIVATION_TYPE_ASSUME, "assume"), NM_UTILS_LOOKUP_STR_ITEM(NM_ACTIVATION_TYPE_EXTERNAL, "external"), ); + +/*****************************************************************************/ + +/** + * 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; + char * dot; + + nm_assert(hostname); + nm_assert(shortened); + + if (nm_sd_hostname_is_valid(hostname, FALSE)) { + NM_SET_OUT(shortened, NULL); + return TRUE; + } + + s = g_strdup(hostname); + dot = strchr(s, '.'); + if (dot) + *dot = 0; + + if (strnlen(s, HOST_NAME_MAX + 1) > HOST_NAME_MAX) + s[HOST_NAME_MAX] = 0; + + if (!nm_sd_hostname_is_valid(s, FALSE)) { + NM_SET_OUT(shortened, NULL); + return FALSE; + } + + NM_SET_OUT(shortened, g_steal_pointer(&s)); + return TRUE; +} diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index ecbf1ebbff..ab6ff4a018 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -316,7 +316,7 @@ gboolean nm_utils_sysctl_ip_conf_is_path(int addr_family, const char *ifname, const char *property); -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/tests/test-utils.c b/src/tests/test-utils.c index 4635d98285..71fc4d9e48 100644 --- a/src/tests/test-utils.c +++ b/src/tests/test-utils.c @@ -216,6 +216,46 @@ 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( + "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(); @@ -227,6 +267,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(); } |
