[RFCv3 09/25] util: Add parsexml/formatbuf helper functions for virSocketAddr

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

 



Implement the parsexml/formatbuf functions for virSocketAddr.

Signed-off-by: Shi Lei <shi_lei@xxxxxxxxxxxxxx>
---
 src/libvirt_private.syms |  2 ++
 src/util/virsocketaddr.c | 42 ++++++++++++++++++++++++++++++++++++++++
 src/util/virsocketaddr.h | 23 ++++++++++++++++++++--
 3 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e78491dc..055396d0 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3213,6 +3213,7 @@ virSocketAddrBroadcastByPrefix;
 virSocketAddrCheckNetmask;
 virSocketAddrEqual;
 virSocketAddrFormat;
+virSocketAddrFormatBuf;
 virSocketAddrFormatFull;
 virSocketAddrGetIPPrefix;
 virSocketAddrGetNumNetmaskBits;
@@ -3230,6 +3231,7 @@ virSocketAddrParse;
 virSocketAddrParseAny;
 virSocketAddrParseIPv4;
 virSocketAddrParseIPv6;
+virSocketAddrParseXML;
 virSocketAddrPrefixToNetmask;
 virSocketAddrPTRDomain;
 virSocketAddrResolveService;
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 94cbfc62..fcad7f8a 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -154,6 +154,15 @@ int virSocketAddrParse(virSocketAddr *addr, const char *val, int family)
     return len;
 }
 
+int virSocketAddrParseXML(const char *val,
+                          virSocketAddr *addr,
+                          const char *instname G_GNUC_UNUSED,
+                          void *parent G_GNUC_UNUSED,
+                          void *opaque G_GNUC_UNUSED)
+{
+    return virSocketAddrParse(addr, val, AF_UNSPEC);
+}
+
 /**
  * virSocketAddrParseAny:
  * @addr: where to store the return value, optional.
@@ -1306,3 +1315,36 @@ virSocketAddrFree(virSocketAddr *addr)
 {
     g_free(addr);
 }
+
+void
+virSocketAddrClear(virSocketAddr *addr)
+{
+    memset(addr, 0, sizeof(virSocketAddr));
+}
+
+int
+virSocketAddrFormatBuf(virBuffer *buf,
+                       const char *fmt,
+                       const virSocketAddr *addr,
+                       const void *parent G_GNUC_UNUSED,
+                       void *opaque G_GNUC_UNUSED)
+{
+    g_autofree char *str = NULL;
+    if (!VIR_SOCKET_ADDR_VALID(addr))
+        return 0;
+
+    str = virSocketAddrFormatFull(addr, false, NULL);
+    if (!str)
+        return -1;
+
+    virBufferAsprintf(buf, fmt, str);
+    return 0;
+}
+
+bool
+virSocketAddrCheck(const virSocketAddr *addr,
+                   const void *parent G_GNUC_UNUSED,
+                   void *opaque G_GNUC_UNUSED)
+{
+    return VIR_SOCKET_ADDR_VALID(addr);
+}
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index f76e2297..cba87390 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -18,11 +18,13 @@
 
 #pragma once
 
+#include "virbuffer.h"
 #include "virsocket.h"
 
 #define VIR_LOOPBACK_IPV4_ADDR "127.0.0.1"
 
-typedef struct {
+typedef struct _virSocketAddr virSocketAddr;
+struct _virSocketAddr {
     union {
         struct sockaddr sa;
         struct sockaddr_storage stor;
@@ -33,7 +35,7 @@ typedef struct {
 #endif
     } data;
     socklen_t len;
-} virSocketAddr;
+};
 
 #define VIR_SOCKET_ADDR_VALID(s) \
     ((s)->data.sa.sa_family != AF_UNSPEC)
@@ -66,6 +68,12 @@ int virSocketAddrParse(virSocketAddr *addr,
                        const char *val,
                        int family);
 
+int virSocketAddrParseXML(const char *val,
+                          virSocketAddr *addr,
+                          const char *instname,
+                          void *parent,
+                          void *opaque);
+
 int virSocketAddrParseAny(virSocketAddr *addr,
                           const char *val,
                           int family,
@@ -89,6 +97,12 @@ char *virSocketAddrFormatFull(const virSocketAddr *addr,
                               bool withService,
                               const char *separator);
 
+int virSocketAddrFormatBuf(virBuffer *buf,
+                           const char *fmt,
+                           const virSocketAddr *addr,
+                           const void *parent,
+                           void *opaque);
+
 char *virSocketAddrGetPath(virSocketAddr *addr);
 
 int virSocketAddrSetPort(virSocketAddr *addr, int port);
@@ -141,5 +155,10 @@ int virSocketAddrPTRDomain(const virSocketAddr *addr,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 
 void virSocketAddrFree(virSocketAddr *addr);
+void virSocketAddrClear(virSocketAddr *addr);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSocketAddr, virSocketAddrFree);
+
+bool virSocketAddrCheck(const virSocketAddr *addr,
+                        const void *parent,
+                        void *opaque);
-- 
2.25.1





[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]

  Powered by Linux