Wireless Daemon for Linux
 help / color / mirror / Atom feed
From: Tim Kourt <tim.a.kourt@linux.intel.com>
To: iwd@lists.01.org
Subject: [PATCH 2/4] net: Add l_net_hostname_is_localhost
Date: Wed, 11 Dec 2019 15:08:55 -0800	[thread overview]
Message-ID: <20191211230857.20606-2-tim.a.kourt@linux.intel.com> (raw)
In-Reply-To: <20191211230857.20606-1-tim.a.kourt@linux.intel.com>

[-- Attachment #1: Type: text/plain, Size: 1953 bytes --]

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

  reply	other threads:[~2019-12-11 23:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-11 23:08 [PATCH 1/4] net: Add l_net_hostname_is_root Tim Kourt
2019-12-11 23:08 ` Tim Kourt [this message]
2019-12-11 23:08 ` [PATCH 3/4] unit: Add tests for net Tim Kourt
2019-12-11 23:08 ` [PATCH 4/4] dhcp-lease: Add domain name option handler Tim Kourt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191211230857.20606-2-tim.a.kourt@linux.intel.com \
    --to=tim.a.kourt@linux.intel.com \
    --cc=iwd@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox