[PATCH 1/7] virutil: Introduce virIsValidHostname

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Similar to virGetHostname, but this time taking a parameter which is a
hostname or ipaddress from a <source ... <host name ='%s'.../>... />
XML property and validating that the name can be resolved.

Return true or false depending on whether we can ascertain the hostname
address from calls to 'getnameinfo' and 'getaddrinfo'. Subsequent patches
will be validating a proposed pool hostname definition against existing
pool hostnames to ensure they are not the same hostname (and thus having
two pools looking at the same data)

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virutil.c       | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virutil.h       |  1 +
 3 files changed, 51 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8c37303..5ba9635 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2324,6 +2324,7 @@ virIsCapableFCHost;
 virIsCapableVport;
 virIsDevMapperDevice;
 virIsSUID;
+virIsValidHostname;
 virManageVport;
 virMemoryLimitIsSet;
 virMemoryLimitTruncate;
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 79cdb7a..f6cc9af 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -690,6 +690,55 @@ char *virGetHostname(void)
 }
 
 
+/*
+ * virIsValidHostname:
+ *
+ * Unlike virGetHostname, this variant of the code receives a hostname
+ * retrieves the getaddrinfo. If the passed hostname can be successfully
+ * and if successfully resolved via getaddrinfo, then success is declared.
+ *
+ * On error in lookup, if an IP Address is not found, or error formatting
+ * the name, an error is generated and a NULL is returned.
+ *
+ * On success, a pointer to a character representation of the numeric IP
+ * Address is returned.
+ *
+ * It is the caller's responsibility to check for a non NULL return and
+ * VIR_FREE the resultant string.
+ */
+bool
+virIsValidHostname(const char *hostname)
+{
+    int r;
+    struct addrinfo hints, *info;
+
+    if (STRPREFIX(hostname, "localhost") ||
+        STREQ(hostname, "127.0.0.1") || STREQ(hostname, "::1"))
+        return true;
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_UNSPEC;
+    hints.ai_protocol = IPPROTO_TCP;
+
+    if ((r = getaddrinfo(hostname, NULL, &hints, &info)) != 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("IP address lookup for host '%s' failed: %s"),
+                       hostname, gai_strerror(r));
+        return false;
+    }
+
+    if (!info) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("No IP address for host '%s' found"),
+                       hostname);
+        return false;
+    }
+    freeaddrinfo(info);
+
+    return true;
+}
+
+
 char *
 virGetUserDirectory(void)
 {
diff --git a/src/util/virutil.h b/src/util/virutil.h
index 55a3bd6..73ad147 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -131,6 +131,7 @@ static inline int pthread_sigmask(int how,
 # endif
 
 char *virGetHostname(void);
+bool virIsValidHostname(const char *hostname) ATTRIBUTE_NONNULL(1);
 
 char *virGetUserDirectory(void);
 char *virGetUserDirectoryByUID(uid_t uid);
-- 
2.1.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]