core: add nm_utils_shorten_hostname()
Add a function to shorten a overlong hostname, truncating it to the first dot or 64 characters.
This commit is contained in:
@@ -30,6 +30,7 @@
|
|||||||
#include "libnm-glib-aux/nm-secret-utils.h"
|
#include "libnm-glib-aux/nm-secret-utils.h"
|
||||||
#include "libnm-glib-aux/nm-time-utils.h"
|
#include "libnm-glib-aux/nm-time-utils.h"
|
||||||
#include "libnm-glib-aux/nm-str-buf.h"
|
#include "libnm-glib-aux/nm-str-buf.h"
|
||||||
|
#include "libnm-systemd-shared/nm-sd-utils-shared.h"
|
||||||
#include "nm-utils.h"
|
#include "nm-utils.h"
|
||||||
#include "libnm-core-intern/nm-core-internal.h"
|
#include "libnm-core-intern/nm-core-internal.h"
|
||||||
#include "nm-setting-connection.h"
|
#include "nm-setting-connection.h"
|
||||||
@@ -5201,3 +5202,50 @@ again:
|
|||||||
|
|
||||||
return g;
|
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;
|
||||||
|
}
|
||||||
|
@@ -233,6 +233,7 @@ void nm_utils_log_connection_diff(NMConnection *connection,
|
|||||||
const char *dbus_path);
|
const char *dbus_path);
|
||||||
|
|
||||||
gboolean nm_utils_is_specific_hostname(const char *name);
|
gboolean nm_utils_is_specific_hostname(const char *name);
|
||||||
|
gboolean nm_utils_shorten_hostname(const char *hostname, char **shortened);
|
||||||
|
|
||||||
struct _NMUuid;
|
struct _NMUuid;
|
||||||
|
|
||||||
|
@@ -212,6 +212,62 @@ test_hw_addr_gen_stable_eth(void)
|
|||||||
"04:0D:CD:0C:9E:2C");
|
"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();
|
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/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/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();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user