[RFC 07/29] util: Add some helper functions for virSocketAddr

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

 



Add virSocketAddrParse, virSocketAddrFormat, virSocketAddrClear and
virSocketAddrCheck. In generated codes by RNG2C, these functions are
called for handling virSocketAddr.

Signed-off-by: Shi Lei <shi_lei@xxxxxxxxxxxxxx>
---
 docs/schemas/basictypes.rng |  1 +
 src/util/virsocketaddr.c    | 27 +++++++++++++++++++++++++++
 src/util/virsocketaddr.h    | 10 ++++++++++
 3 files changed, 38 insertions(+)

diff --git a/docs/schemas/basictypes.rng b/docs/schemas/basictypes.rng
index e602715..a6c3fc8 100644
--- a/docs/schemas/basictypes.rng
+++ b/docs/schemas/basictypes.rng
@@ -222,6 +222,7 @@
 
   <!-- An ipv4 "dotted quad" address -->
   <define name="ipv4Addr">
+    <!-- VIRT:DIRECTIVE {"name": "virSocketAddr"} -->
     <data type="string">
       <param name="pattern">(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([1-9][0-9])|([0-9]))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([1-9][0-9])|([0-9]))</param>
     </data>
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index 4cad329..f502978 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -157,6 +157,11 @@ int virSocketAddrParse(virSocketAddrPtr addr, const char *val, int family)
     return len;
 }
 
+int virSocketAddrParseXML(const char *val, virSocketAddrPtr addr)
+{
+    return virSocketAddrParse(addr, val, AF_UNSPEC);
+}
+
 /**
  * virSocketAddrParseAny:
  * @addr: where to store the return value, optional.
@@ -444,6 +449,22 @@ virSocketAddrFormat(const virSocketAddr *addr)
     return virSocketAddrFormatFull(addr, false, NULL);
 }
 
+int
+virSocketAddrFormatBuf(virBufferPtr buf,
+                       const char *fmt,
+                       const virSocketAddr *addr)
+{
+    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;
+}
 
 /*
  * virSocketAddrFormatFull:
@@ -1319,3 +1340,9 @@ virSocketAddrFree(virSocketAddrPtr addr)
 {
     VIR_FREE(addr);
 }
+
+void
+virSocketAddrClear(virSocketAddrPtr addr)
+{
+    memset(addr, 0, sizeof(virSocketAddr));
+}
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index d06e751..f255107 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -18,6 +18,7 @@
 
 #pragma once
 
+#include "virbuffer.h"
 #include "virsocket.h"
 
 #define VIR_LOOPBACK_IPV4_ADDR "127.0.0.1"
@@ -44,6 +45,8 @@ typedef struct {
 #define VIR_SOCKET_ADDR_FAMILY(s) \
     ((s)->data.sa.sa_family)
 
+#define virSocketAddrCheck VIR_SOCKET_ADDR_VALID
+
 #define VIR_SOCKET_ADDR_IPV4_ALL "0.0.0.0"
 #define VIR_SOCKET_ADDR_IPV6_ALL "::"
 
@@ -70,6 +73,8 @@ int virSocketAddrParse(virSocketAddrPtr addr,
                        const char *val,
                        int family);
 
+int virSocketAddrParseXML(const char *val, virSocketAddrPtr addr);
+
 int virSocketAddrParseAny(virSocketAddrPtr addr,
                           const char *val,
                           int family,
@@ -93,6 +98,10 @@ char *virSocketAddrFormatFull(const virSocketAddr *addr,
                               bool withService,
                               const char *separator);
 
+int virSocketAddrFormatBuf(virBufferPtr buf,
+                           const char *fmt,
+                           const virSocketAddr *addr);
+
 char *virSocketAddrGetPath(virSocketAddrPtr addr);
 
 int virSocketAddrSetPort(virSocketAddrPtr addr, int port);
@@ -145,5 +154,6 @@ 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);
-- 
2.17.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