From a767af1a7c6221346b16e46a409bfde9744e55ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Mon, 24 Jun 2019 11:25:48 +0100 Subject: [PATCH] util: add helper API for getting UNIX path from socket address MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Ján Tomko Signed-off-by: Daniel P. Berrangé --- src/libvirt_private.syms | 1 + src/util/virsocketaddr.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/util/virsocketaddr.h | 2 ++ 3 files changed, 44 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0545c08428..e226b85121 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2887,6 +2887,7 @@ virSocketAddrFormat; virSocketAddrFormatFull; virSocketAddrGetIPPrefix; virSocketAddrGetNumNetmaskBits; +virSocketAddrGetPath; virSocketAddrGetPort; virSocketAddrGetRange; virSocketAddrIsNetmask; diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 182e18aa8c..f961b413d3 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -522,6 +522,47 @@ virSocketAddrGetPort(virSocketAddrPtr addr) return -1; } + +/* + * virSocketGetPath: + * @addr: an initialized virSocketAddrPtr + * + * Returns the UNIX socket path of the given virtSocketAddr + * + * Returns -1 if @addr is invalid or does not refer to an + * address of type AF_UNIX; + */ +char * +virSocketAddrGetPath(virSocketAddrPtr addr ATTRIBUTE_UNUSED) +{ +#ifndef WIN32 + char *path = NULL; + if (addr == NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("No socket address provided")); + return NULL; + } + + if (addr->data.sa.sa_family != AF_UNIX) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("UNIX socket address is required")); + return NULL; + } + + if (VIR_STRNDUP(path, + addr->data.un.sun_path, + sizeof(addr->data.un.sun_path)) < 0) + return NULL; + + return path; +#else + virReportError(VIR_ERR_NO_SUPPORT, "%s", + _("UNIX sockets not supported on this platform")); + return NULL; +#endif +} + + /** * virSocketAddrIsNetmask: * @netmask: the netmask address diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h index b2ecb3c748..9dbd8caa0d 100644 --- a/src/util/virsocketaddr.h +++ b/src/util/virsocketaddr.h @@ -108,6 +108,8 @@ char *virSocketAddrFormatFull(const virSocketAddr *addr, bool withService, const char *separator); +char *virSocketAddrGetPath(virSocketAddrPtr addr); + int virSocketAddrSetPort(virSocketAddrPtr addr, int port); int virSocketAddrGetPort(virSocketAddrPtr addr); -- GitLab