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

[libvirt] [PATCH v2 7/9] virsockaddr: Split up functionality of virSocketAddrFormatFull



Create a local API virSocketAddrGetNumericHost which can be used by a
future patch in order to obtain the numeric host nameinfo data

Signed-off-by: John Ferlan <jferlan redhat com>
---
 src/util/virsocketaddr.c | 60 ++++++++++++++++++++++++++++++------------------
 1 file changed, 38 insertions(+), 22 deletions(-)

diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 83518a0..993d460 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -333,6 +333,42 @@ virSocketAddrFormat(const virSocketAddr *addr)
 }
 
 
+static char *
+virSocketAddrGetNumericHost(const struct sockaddr *sa,
+                            socklen_t salen,
+                            bool withService,
+                            const char *separator)
+{
+    char host[NI_MAXHOST], port[NI_MAXSERV];
+    char *addrstr;
+    int err;
+    int flags = NI_NUMERICHOST;
+
+    if (withService)
+        flags |= NI_NUMERICSERV;
+
+    if ((err = getnameinfo(sa, salen,
+                           host, sizeof(host),
+                           port, sizeof(port),
+                           flags)) != 0) {
+        virReportError(VIR_ERR_SYSTEM_ERROR,
+                       _("Cannot convert socket address to string: %s"),
+                       gai_strerror(err));
+        return NULL;
+    }
+
+    if (withService) {
+        if (virAsprintf(&addrstr, "%s%s%s", host, separator, port) == -1)
+            return NULL;
+    } else {
+        if (VIR_STRDUP(addrstr, host) < 0)
+            return NULL;
+    }
+
+    return addrstr;
+}
+
+
 /*
  * virSocketAddrFormatFull:
  * @addr: an initialized virSocketAddrPtr
@@ -348,9 +384,7 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
                         bool withService,
                         const char *separator)
 {
-    char host[NI_MAXHOST], port[NI_MAXSERV];
     char *addrstr;
-    int err;
 
     if (addr == NULL) {
         virReportError(VIR_ERR_INVALID_ARG, "%s", _("Missing address"));
@@ -371,26 +405,8 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
         return addrstr;
     }
 
-    if ((err = getnameinfo(&addr->data.sa,
-                           addr->len,
-                           host, sizeof(host),
-                           port, sizeof(port),
-                           NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
-        virReportError(VIR_ERR_SYSTEM_ERROR,
-                       _("Cannot convert socket address to string: %s"),
-                       gai_strerror(err));
-        return NULL;
-    }
-
-    if (withService) {
-        if (virAsprintf(&addrstr, "%s%s%s", host, separator, port) == -1)
-            goto error;
-    } else {
-        if (VIR_STRDUP(addrstr, host) < 0)
-            goto error;
-    }
-
-    return addrstr;
+    return virSocketAddrGetNumericHost(&addr->data.sa, addr->len,
+                                       withService, separator);
 
  error:
     return NULL;
-- 
2.1.0


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