Implement the parsexml/formatbuf functions for virSocketAddr. Signed-off-by: Shi Lei <shi_lei@xxxxxxxxxxxxxx> --- src/util/virsocketaddr.c | 38 ++++++++++++++++++++++++++++++++++++++ src/util/virsocketaddr.h | 22 ++++++++++++++++++---- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index a858a69..fd29678 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -157,6 +157,14 @@ 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 *opaque G_GNUC_UNUSED) +{ + return virSocketAddrParse(addr, val, AF_UNSPEC); +} + /** * virSocketAddrParseAny: * @addr: where to store the return value, optional. @@ -1316,3 +1324,33 @@ 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, + 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, void *opaque G_GNUC_UNUSED) +{ + return VIR_SOCKET_ADDR_VALID(addr); +} diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h index d06e751..7ef4f35 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,11 @@ int virSocketAddrParse(virSocketAddrPtr addr, const char *val, int family); +int virSocketAddrParseXML(const char *val, + virSocketAddrPtr addr, + const char *instname, + void *opaque); + int virSocketAddrParseAny(virSocketAddrPtr addr, const char *val, int family, @@ -93,6 +99,11 @@ char *virSocketAddrFormatFull(const virSocketAddr *addr, bool withService, const char *separator); +int virSocketAddrFormatBuf(virBufferPtr buf, + const char *fmt, + const virSocketAddr *addr, + void *opaque); + char *virSocketAddrGetPath(virSocketAddrPtr addr); int virSocketAddrSetPort(virSocketAddrPtr addr, int port); @@ -145,5 +156,8 @@ 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, void *opaque); -- 2.17.1