The function identifies if the given hostname is root domain name.
---
ell/net.c | 23 +++++++++++++++++++++++
ell/net.h | 1 +
2 files changed, 24 insertions(+)
diff --git a/ell/net.c b/ell/net.c
index b5b5f9d..3b6e0dc 100644
--- a/ell/net.c
+++ b/ell/net.c
@@ -110,3 +110,26 @@ LIB_EXPORT char *l_net_get_name(uint32_t ifindex)
return l_strdup(ifr.ifr_name);
}
+
+/**
+ * l_net_hostname_is_root:
+ * @hostname: Hostname to validate
+ *
+ * Identifies if the hostname given by @hostname is root domain name or
+ * not.
+ *
+ * Returns: #true if the given hostname is root and #false otherwise.
+ **/
+LIB_EXPORT bool l_net_hostname_is_root(const char *hostname)
+{
+ if (unlikely(!hostname))
+ return false;
+
+ if (!strcmp(hostname, ""))
+ return true;
+
+ if (!strcmp(hostname, "."))
+ return true;
+
+ return false;
+}
diff --git a/ell/net.h b/ell/net.h
index 25b1ca2..7afcf86 100644
--- a/ell/net.h
+++ b/ell/net.h
@@ -32,6 +32,7 @@ extern "C" {
bool l_net_get_mac_address(uint32_t ifindex, uint8_t *out_addr);
char *l_net_get_name(uint32_t ifindex);
+bool l_net_hostname_is_root(const char *hostname);
#ifdef __cplusplus
}
--
2.13.6
Show replies by date
Identifies if the given hostname is localhost.
---
ell/net.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
ell/net.h | 1 +
2 files changed, 46 insertions(+)
diff --git a/ell/net.c b/ell/net.c
index 3b6e0dc..350340a 100644
--- a/ell/net.c
+++ b/ell/net.c
@@ -133,3 +133,48 @@ LIB_EXPORT bool l_net_hostname_is_root(const char *hostname)
return false;
}
+
+static bool str_has_suffix(const char *str, const char *suffix)
+{
+ size_t str_len;
+ size_t suffix_len;
+ size_t len_diff;
+
+ str_len = strlen(str);
+ suffix_len = strlen(suffix);
+
+ if (str_len < suffix_len)
+ return false;
+
+ len_diff = str_len - suffix_len;
+
+ return !strcasecmp(&str[len_diff], suffix);
+}
+
+/**
+ * l_net_hostname_is_localhost:
+ * @hostname: Hostname to validate
+ *
+ * Identifies if the hostname given by @hostname is localhost or not.
+ *
+ * Returns: #true if the given hostname is localhost and #false otherwise.
+ **/
+LIB_EXPORT bool l_net_hostname_is_localhost(const char *hostname)
+{
+ if (unlikely(!hostname))
+ return false;
+
+ if (!strcasecmp(hostname, "localhost") ||
+ !strcasecmp(hostname, "localhost.") ||
+ !strcasecmp(hostname, "localhost.localdomain") ||
+ !strcasecmp(hostname, "localhost.localdomain."))
+ return true;
+
+ if (str_has_suffix(hostname, ".localhost") ||
+ str_has_suffix(hostname, ".localhost.") ||
+ str_has_suffix(hostname, ".localhost.localdomain") ||
+ str_has_suffix(hostname, ".localhost.localdomain."))
+ return true;
+
+ return false;
+}
diff --git a/ell/net.h b/ell/net.h
index 7afcf86..6808e07 100644
--- a/ell/net.h
+++ b/ell/net.h
@@ -33,6 +33,7 @@ extern "C" {
bool l_net_get_mac_address(uint32_t ifindex, uint8_t *out_addr);
char *l_net_get_name(uint32_t ifindex);
bool l_net_hostname_is_root(const char *hostname);
+bool l_net_hostname_is_localhost(const char *hostname);
#ifdef __cplusplus
}
--
2.13.6
---
Makefile.am | 5 ++++-
unit/test-net.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 1 deletion(-)
create mode 100644 unit/test-net.c
diff --git a/Makefile.am b/Makefile.am
index ead9fd5..3ead678 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -178,7 +178,8 @@ unit_tests = unit/test-unit \
unit/test-ecc \
unit/test-ecdh \
unit/test-time \
- unit/test-path
+ unit/test-path \
+ unit/test-net
dbus_tests = unit/test-hwdb \
unit/test-dbus \
@@ -304,6 +305,8 @@ unit_test_time_LDADD = ell/libell-private.la
unit_test_path_LDADD = ell/libell-private.la
+unit_test_net_LDADD = ell/libell-private.la
+
if MAINTAINER_MODE
noinst_LTLIBRARIES += unit/example-plugin.la
endif
diff --git a/unit/test-net.c b/unit/test-net.c
new file mode 100644
index 0000000..2f7bf48
--- /dev/null
+++ b/unit/test-net.c
@@ -0,0 +1,66 @@
+/*
+ *
+ * Embedded Linux library
+ *
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+
+#include <ell/ell.h>
+
+static void test_net_hostname_is_localhost(const void *data)
+{
+ assert(l_net_hostname_is_localhost("localhost"));
+ assert(l_net_hostname_is_localhost("localhost."));
+ assert(l_net_hostname_is_localhost("localhost.localdomain"));
+ assert(l_net_hostname_is_localhost("localhost.localdomain."));
+ assert(l_net_hostname_is_localhost("other.localhost"));
+ assert(l_net_hostname_is_localhost("other.localhost."));
+ assert(l_net_hostname_is_localhost("other.localhost.localdomain"));
+ assert(l_net_hostname_is_localhost("other.localhost.localdomain."));
+
+ assert(l_net_hostname_is_localhost("LOCALHOST"));
+
+ assert(!l_net_hostname_is_localhost("notsolocalhost"));
+ assert(!l_net_hostname_is_localhost("localhost.com"));
+ assert(!l_net_hostname_is_localhost(""));
+}
+
+static void test_net_hostname_is_root(const void *data)
+{
+ assert(l_net_hostname_is_root(""));
+ assert(l_net_hostname_is_root("."));
+ assert(!l_net_hostname_is_root("notsoroot"));
+}
+
+int main(int argc, char *argv[])
+{
+ l_test_init(&argc, &argv);
+
+ l_test_add("net/hostname_is_localhost", test_net_hostname_is_localhost,
+ NULL);
+ l_test_add("net/hostname_is_root", test_net_hostname_is_root,
+ NULL);
+
+ return l_test_run();
+}
--
2.13.6
Add parser and accessor for the domain name lease option.
The parser validates value by disallowing embedded NUL bytes,
non-utf8 characters, usage of root and localhost domain names.
---
ell/dhcp-lease.c | 36 +++++++++++++++++++++++++++++++++++-
ell/dhcp-private.h | 1 +
ell/dhcp.h | 1 +
3 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/ell/dhcp-lease.c b/ell/dhcp-lease.c
index 5f801e7..8f044fb 100644
--- a/ell/dhcp-lease.c
+++ b/ell/dhcp-lease.c
@@ -31,6 +31,8 @@
#include "private.h"
#include "dhcp.h"
#include "dhcp-private.h"
+#include "utf8.h"
+#include "net.h"
struct l_dhcp_lease *_dhcp_lease_new(void)
{
@@ -45,6 +47,8 @@ void _dhcp_lease_free(struct l_dhcp_lease *lease)
return;
l_free(lease->dns);
+ l_free(lease->domain_name);
+
l_free(lease);
}
@@ -99,6 +103,28 @@ struct l_dhcp_lease *_dhcp_lease_parse_options(struct
dhcp_message_iter *iter)
}
}
break;
+ case L_DHCP_OPTION_DOMAIN_NAME:
+ if (l < 1 && l > 253)
+ goto error;
+
+ /* Disallow embedded NUL bytes. */
+ if (memchr(v, 0, l - 1))
+ goto error;
+
+ if (!l_utf8_validate(v, l, NULL))
+ goto error;
+
+ lease->domain_name = l_new(char, l + 1);
+
+ memcpy(lease->domain_name, v, l);
+
+ if (l_net_hostname_is_root(lease->domain_name))
+ goto error;
+
+ if (l_net_hostname_is_localhost(lease->domain_name))
+ goto error;
+
+ break;
default:
break;
}
@@ -135,7 +161,7 @@ struct l_dhcp_lease *_dhcp_lease_parse_options(struct
dhcp_message_iter *iter)
return lease;
error:
- l_free(lease);
+ _dhcp_lease_free(lease);
return NULL;
}
@@ -212,6 +238,14 @@ LIB_EXPORT char **l_dhcp_lease_get_dns(const struct l_dhcp_lease
*lease)
return dns_list;
}
+LIB_EXPORT char *l_dhcp_lease_get_domain_name(const struct l_dhcp_lease *lease)
+{
+ if (unlikely(!lease))
+ return NULL;
+
+ return l_strdup(lease->domain_name);
+}
+
LIB_EXPORT uint32_t l_dhcp_lease_get_t1(const struct l_dhcp_lease *lease)
{
if (unlikely(!lease))
diff --git a/ell/dhcp-private.h b/ell/dhcp-private.h
index 6554fc6..a75bb8b 100644
--- a/ell/dhcp-private.h
+++ b/ell/dhcp-private.h
@@ -120,6 +120,7 @@ struct l_dhcp_lease {
uint32_t t2;
uint32_t router;
uint32_t *dns;
+ char *domain_name;
};
struct l_dhcp_lease *_dhcp_lease_new(void);
diff --git a/ell/dhcp.h b/ell/dhcp.h
index c3a4988..b8a5b41 100644
--- a/ell/dhcp.h
+++ b/ell/dhcp.h
@@ -95,6 +95,7 @@ char *l_dhcp_lease_get_netmask(const struct l_dhcp_lease *lease);
char *l_dhcp_lease_get_broadcast(const struct l_dhcp_lease *lease);
char *l_dhcp_lease_get_server_id(const struct l_dhcp_lease *lease);
char **l_dhcp_lease_get_dns(const struct l_dhcp_lease *lease);
+char *l_dhcp_lease_get_domain_name(const struct l_dhcp_lease *lease);
uint32_t l_dhcp_lease_get_t1(const struct l_dhcp_lease *lease);
uint32_t l_dhcp_lease_get_t2(const struct l_dhcp_lease *lease);
--
2.13.6