[libvirt] [PATCH v2 2/9] socketaddr: Introduce virSocketAddrParseName

John Ferlan jferlan at redhat.com
Thu Apr 23 20:12:10 UTC 2015


Add a new function virSocketAddrParseName which unlike virSocketAddrParse
will be capable of processing a network address that needs to be looked
up and resolved.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virsocketaddr.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++--
 src/util/virsocketaddr.h |  7 ++++++-
 3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8c50ea2..848b44a 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2065,6 +2065,7 @@ virSocketAddrNumericFamily;
 virSocketAddrParse;
 virSocketAddrParseIPv4;
 virSocketAddrParseIPv6;
+virSocketAddrParseName;
 virSocketAddrPrefixToNetmask;
 virSocketAddrSetIPv4Addr;
 virSocketAddrSetPort;
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 0e9a39c..83518a0 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -80,6 +80,7 @@ static int
 virSocketAddrParseInternal(struct addrinfo **res,
                            const char *val,
                            int family,
+                           int protocol,
                            bool isNumeric,
                            bool reportError)
 {
@@ -93,6 +94,7 @@ virSocketAddrParseInternal(struct addrinfo **res,
 
     memset(&hints, 0, sizeof(hints));
     hints.ai_family = family;
+    hints.ai_protocol = protocol;
     if (isNumeric)
         hints.ai_flags = AI_NUMERICHOST;
     if ((err = getaddrinfo(val, NULL, &hints, res)) != 0) {
@@ -123,7 +125,7 @@ int virSocketAddrParse(virSocketAddrPtr addr, const char *val, int family)
     int len;
     struct addrinfo *res;
 
-    if (virSocketAddrParseInternal(&res, val, family, true, true) < 0)
+    if (virSocketAddrParseInternal(&res, val, family, 0, true, true) < 0)
         return -1;
 
     if (res == NULL) {
@@ -143,6 +145,51 @@ int virSocketAddrParse(virSocketAddrPtr addr, const char *val, int family)
     return len;
 }
 
+/**
+ * virSocketAddrParseName:
+ * @addr: where to store the return value, optional.
+ * @val: either a numeric network address IPv4 or IPv6 or a network
+ *       hostname to be looked up and resolved.
+ * @family: address family to pass down to getaddrinfo
+ * @protocol: specific protocol to use
+ *
+ * Mostly a wrapper for getaddrinfo() extracting the address storage
+ * from the numeric string like 1.2.3.4 or 2001:db8:85a3:0:0:8a2e:370:7334
+ * or a resolvable network hostname such as "redhat.com"
+ *
+ * Returns the length of the network address or -1 in case of error.
+ */
+int
+virSocketAddrParseName(virSocketAddrPtr addr,
+                       const char *val,
+                       int family,
+                       int protocol)
+{
+    int len;
+    struct addrinfo *res;
+
+    if (virSocketAddrParseInternal(&res, val, family,
+                                   protocol, false, true) < 0)
+        return -1;
+
+    if (res == NULL) {
+        virReportError(VIR_ERR_SYSTEM_ERROR,
+                       _("No addresses found for '%s' using family='%d' "
+                         "and protocol='%d'"),
+                       val, family, protocol);
+        return -1;
+    }
+
+    len = res->ai_addrlen;
+    if (addr != NULL) {
+        memcpy(&addr->data.stor, res->ai_addr, len);
+        addr->len = res->ai_addrlen;
+    }
+
+    freeaddrinfo(res);
+    return len;
+}
+
 /*
  * virSocketAddrParseIPv4:
  * @val: an IPv4 numeric address
@@ -880,7 +927,8 @@ virSocketAddrNumericFamily(const char *address)
     struct addrinfo *res;
     unsigned short family;
 
-    if (virSocketAddrParseInternal(&res, address, AF_UNSPEC, true, false) < 0)
+    if (virSocketAddrParseInternal(&res, address, AF_UNSPEC,
+                                   0, true, false) < 0)
         return -1;
 
     family = res->ai_addr->sa_family;
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 99ab46f..3bbf119 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2013 Red Hat, Inc.
+ * Copyright (C) 2009-2015 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -78,6 +78,11 @@ int virSocketAddrParse(virSocketAddrPtr addr,
                        const char *val,
                        int family);
 
+int virSocketAddrParseName(virSocketAddrPtr addr,
+                           const char *val,
+                           int family,
+                           int protocol);
+
 int virSocketAddrParseIPv4(virSocketAddrPtr addr,
                            const char *val);
 
-- 
2.1.0




More information about the libvir-list mailing list