Skip to contentSkip to conversation

net: Add l_net_hostname_is_root

PATCH In review Tim Kourt 2 participants 6 messages Dec 11, 2019 – Dec 12, 2019 Kbuild

lore
Share & tools
lore

Tim Kourt

author
#0 lore raw

PATCH 1/5 net: Add l_net_hostname_is_root

The function identifies if the given hostname is root domain name.

---
 ell/net.c | 23 +++++++++++++++++++++++
 ell/net.h |  1 +
 2 files changed, 24 insertions(+)
ell/net.c +23 -0
index b5b5f9d..3b6e0dc 100644
@@ -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;
+}
ell/net.h +1 -0
index 25b1ca2..7afcf86 100644
@@ -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
 }
Signature · 1 line
2.13.6
author
#1 lore raw

PATCH 2/5 net: Add l_net_hostname_is_localhost

Identifies if the given hostname is localhost.

---
 ell/net.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 ell/net.h |  1 +
 2 files changed, 46 insertions(+)
ell/net.c +45 -0
index 3b6e0dc..350340a 100644
@@ -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;
+}
ell/net.h +1 -0
index 7afcf86..6808e07 100644
@@ -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
 }
Signature · 1 line
2.13.6
author
#2 lore raw

PATCH 3/5 unit: Add tests for net

In reply to Tim Kourt
---
 Makefile.am     |  5 ++++-
 unit/test-net.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-net.c
Makefile.am +4 -1
index ead9fd5..3ead678 100644
@@ -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
unit/test-net.c new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.13.6
author
#3 lore raw

PATCH 4/5 dhcp-lease: Add domain name option handler

In reply to Tim Kourt

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(-)
ell/dhcp-lease.c +35 -1
index 5f801e7..8f044fb 100644
@@ -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))
ell/dhcp-private.h +1 -0
index 6554fc6..a75bb8b 100644
@@ -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);
ell/dhcp.h +1 -0
index c3a4988..b8a5b41 100644
@@ -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);
Signature · 1 line
2.13.6
author
#4 lore raw

PATCH 5/5 dhcp: Add debug message

In reply to Tim Kourt

Add debug message for failed parsing of DHCP options.

---
 ell/dhcp.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
ell/dhcp.c +4 -1
index a362b7f..f1e1595 100644
@@ -858,8 +858,11 @@ static int dhcp_client_receive_ack(struct l_dhcp_client *client,
 		return -EINVAL;
 
 	lease = _dhcp_lease_parse_options(&iter);
-	if (!lease)
+	if (!lease) {
+		CLIENT_DEBUG("Failed to parse DHCP options.");
+
 		return -ENOMSG;
+	}
 
 	lease->address = ack->yiaddr;
 
Signature · 1 line
2.13.6
#5 lore raw
In reply to Tim Kourt

Hi Tim,

Quoted 5 lines · Tim Kourt

The function identifies if the given hostname is root domain name.

---
  ell/net.c | 23 +++++++++++++++++++++++
  ell/net.h |  1 +
  2 files changed, 24 insertions(+)

All applied, thanks.

Regards,
-Denis