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

[RFCv2 06/46] util: Add parsexml/formatbuf helper functions for virSocketAddr



Implement the parsexml/formatbuf functions for virSocketAddr.

Signed-off-by: Shi Lei <shi_lei massclouds com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virsocketaddr.c | 42 ++++++++++++++++++++++++++++++++++++++++
 src/util/virsocketaddr.h | 26 +++++++++++++++++++++----
 3 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4ad9d1e..6b5c664 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3074,6 +3074,7 @@ virSocketAddrBroadcastByPrefix;
 virSocketAddrCheckNetmask;
 virSocketAddrEqual;
 virSocketAddrFormat;
+virSocketAddrFormatBuf;
 virSocketAddrFormatFull;
 virSocketAddrGetIPPrefix;
 virSocketAddrGetNumNetmaskBits;
@@ -3091,6 +3092,7 @@ virSocketAddrParse;
 virSocketAddrParseAny;
 virSocketAddrParseIPv4;
 virSocketAddrParseIPv6;
+virSocketAddrParseXML;
 virSocketAddrPrefixToNetmask;
 virSocketAddrPTRDomain;
 virSocketAddrResolveService;
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index e0eb76d..bbb3a81 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -157,6 +157,15 @@ int virSocketAddrParse(virSocketAddrPtr addr, const char *val, int family)
     return len;
 }
 
+int virSocketAddrParseXML(const char *val,
+                          virSocketAddrPtr 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.
@@ -1307,3 +1316,36 @@ virSocketAddrFree(virSocketAddrPtr addr)
 {
     VIR_FREE(addr);
 }
+
+void
+virSocketAddrClear(virSocketAddrPtr addr)
+{
+    memset(addr, 0, sizeof(virSocketAddr));
+}
+
+int
+virSocketAddrFormatBuf(virBufferPtr 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 6c08f8b..fa51d5c 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -18,11 +18,14 @@
 
 #pragma once
 
+#include "virbuffer.h"
 #include "virsocket.h"
 
 #define VIR_LOOPBACK_IPV4_ADDR "127.0.0.1"
 
-typedef struct {
+typedef struct _virSocketAddr virSocketAddr;
+typedef virSocketAddr *virSocketAddrPtr;
+struct _virSocketAddr {
     union {
         struct sockaddr sa;
         struct sockaddr_storage stor;
@@ -33,7 +36,7 @@ typedef struct {
 #endif
     } data;
     socklen_t len;
-} virSocketAddr;
+};
 
 #define VIR_SOCKET_ADDR_VALID(s) \
     ((s)->data.sa.sa_family != AF_UNSPEC)
@@ -50,8 +53,6 @@ typedef struct {
 #define VIR_SOCKET_ADDR_IPV4_ARPA "in-addr.arpa"
 #define VIR_SOCKET_ADDR_IPV6_ARPA "ip6.arpa"
 
-typedef virSocketAddr *virSocketAddrPtr;
-
 typedef struct _virSocketAddrRange virSocketAddrRange;
 typedef virSocketAddrRange *virSocketAddrRangePtr;
 struct _virSocketAddrRange {
@@ -70,6 +71,12 @@ int virSocketAddrParse(virSocketAddrPtr addr,
                        const char *val,
                        int family);
 
+int virSocketAddrParseXML(const char *val,
+                          virSocketAddrPtr addr,
+                          const char *instname,
+                          void *parent,
+                          void *opaque);
+
 int virSocketAddrParseAny(virSocketAddrPtr addr,
                           const char *val,
                           int family,
@@ -93,6 +100,12 @@ char *virSocketAddrFormatFull(const virSocketAddr *addr,
                               bool withService,
                               const char *separator);
 
+int virSocketAddrFormatBuf(virBufferPtr buf,
+                           const char *fmt,
+                           const virSocketAddr *addr,
+                           const void *parent,
+                           void *opaque);
+
 char *virSocketAddrGetPath(virSocketAddrPtr addr);
 
 int virSocketAddrSetPort(virSocketAddrPtr addr, int port);
@@ -145,5 +158,10 @@ int virSocketAddrPTRDomain(const virSocketAddr *addr,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 
 void virSocketAddrFree(virSocketAddrPtr addr);
+void virSocketAddrClear(virSocketAddrPtr addr);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSocketAddr, virSocketAddrFree);
+
+bool virSocketAddrCheck(const virSocketAddr *addr,
+                        const void *parent,
+                        void *opaque);
-- 
2.25.1



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