[libvirt] [PATCH V2 0/2] Define macro for IPv4 loopback address and use it in libxl

V2 of https://www.redhat.com/archives/libvir-list/2017-May/msg00616.html While reviewing V1, Joao suggesting adding a macro for the various hardcoded "127.0.0.1" used throughout the code. Patch 1 does that. Patch2 is improved in V2 to include setting the listen address for spice as well as VNC. Jim Fehlig (2): maint: define a macro for IPv4 loopback address libxl: add default listen address for VNC and spice src/internal.h | 2 ++ src/libxl/libxl_conf.c | 31 +++++++++++++++++++++---------- src/qemu/qemu_conf.c | 4 ++-- src/util/virsocketaddr.c | 4 ++-- src/vz/vz_sdk.c | 2 +- 5 files changed, 28 insertions(+), 15 deletions(-) -- 2.11.0

Use a macro instead of hardcoding "127.0.0.1" throughout the sources. Signed-off-by: Jim Fehlig <jfehlig@suse.com> --- src/internal.h | 2 ++ src/qemu/qemu_conf.c | 4 ++-- src/util/virsocketaddr.c | 4 ++-- src/vz/vz_sdk.c | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/internal.h b/src/internal.h index 9e7ef553d..03a973ccd 100644 --- a/src/internal.h +++ b/src/internal.h @@ -79,6 +79,8 @@ # define INET_ADDRSTRLEN 16 # endif +# define VIR_LOOPBACK_IPV4_ADDR "127.0.0.1" + /* String equality tests, suggested by Jim Meyering. */ # define STREQ(a, b) (strcmp(a, b) == 0) # define STRCASEEQ(a, b) (c_strcasecmp(a, b) == 0) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 1b704daa5..19ddf787d 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -250,10 +250,10 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged) SYSCONFDIR "/pki/qemu") < 0) goto error; - if (VIR_STRDUP(cfg->vncListen, "127.0.0.1") < 0) + if (VIR_STRDUP(cfg->vncListen, VIR_LOOPBACK_IPV4_ADDR) < 0) goto error; - if (VIR_STRDUP(cfg->spiceListen, "127.0.0.1") < 0) + if (VIR_STRDUP(cfg->spiceListen, VIR_LOOPBACK_IPV4_ADDR) < 0) goto error; /* diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 41f75d5c2..9dffbc736 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -383,11 +383,11 @@ virSocketAddrFormatFull(const virSocketAddr *addr, * nicely for UNIX sockets */ if (addr->data.sa.sa_family == AF_UNIX) { if (withService) { - if (virAsprintf(&addrstr, "127.0.0.1%s0", + if (virAsprintf(&addrstr, VIR_LOOPBACK_IPV4_ADDR"%s0", separator ? separator : ":") < 0) goto error; } else { - if (VIR_STRDUP(addrstr, "127.0.0.1") < 0) + if (VIR_STRDUP(addrstr, VIR_LOOPBACK_IPV4_ADDR) < 0) goto error; } return addrstr; diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c index 4d2c6b0f1..4fab3b9bf 100644 --- a/src/vz/vz_sdk.c +++ b/src/vz/vz_sdk.c @@ -3084,7 +3084,7 @@ static int prlsdkApplyGraphicsParams(PRL_HANDLE sdkdom, glisten = virDomainGraphicsGetListen(gr, 0); pret = PrlVmCfg_SetVNCHostName(sdkdom, glisten && glisten->address ? - glisten->address : "127.0.0.1"); + glisten->address : VIR_LOOPBACK_IPV4_ADDR); prlsdkCheckRetGoto(pret, cleanup); ret = 0; -- 2.11.0

On 05/19/2017 11:33 PM, Jim Fehlig wrote:
Use a macro instead of hardcoding "127.0.0.1" throughout the sources.
Signed-off-by: Jim Fehlig <jfehlig@suse.com>
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
--- src/internal.h | 2 ++ src/qemu/qemu_conf.c | 4 ++-- src/util/virsocketaddr.c | 4 ++-- src/vz/vz_sdk.c | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/src/internal.h b/src/internal.h index 9e7ef553d..03a973ccd 100644 --- a/src/internal.h +++ b/src/internal.h @@ -79,6 +79,8 @@ # define INET_ADDRSTRLEN 16 # endif
+# define VIR_LOOPBACK_IPV4_ADDR "127.0.0.1" + /* String equality tests, suggested by Jim Meyering. */ # define STREQ(a, b) (strcmp(a, b) == 0) # define STRCASEEQ(a, b) (c_strcasecmp(a, b) == 0) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 1b704daa5..19ddf787d 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -250,10 +250,10 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged) SYSCONFDIR "/pki/qemu") < 0) goto error;
- if (VIR_STRDUP(cfg->vncListen, "127.0.0.1") < 0) + if (VIR_STRDUP(cfg->vncListen, VIR_LOOPBACK_IPV4_ADDR) < 0) goto error;
- if (VIR_STRDUP(cfg->spiceListen, "127.0.0.1") < 0) + if (VIR_STRDUP(cfg->spiceListen, VIR_LOOPBACK_IPV4_ADDR) < 0) goto error;
/* diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c index 41f75d5c2..9dffbc736 100644 --- a/src/util/virsocketaddr.c +++ b/src/util/virsocketaddr.c @@ -383,11 +383,11 @@ virSocketAddrFormatFull(const virSocketAddr *addr, * nicely for UNIX sockets */ if (addr->data.sa.sa_family == AF_UNIX) { if (withService) { - if (virAsprintf(&addrstr, "127.0.0.1%s0", + if (virAsprintf(&addrstr, VIR_LOOPBACK_IPV4_ADDR"%s0", separator ? separator : ":") < 0) goto error; } else { - if (VIR_STRDUP(addrstr, "127.0.0.1") < 0) + if (VIR_STRDUP(addrstr, VIR_LOOPBACK_IPV4_ADDR) < 0) goto error; } return addrstr; diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c index 4d2c6b0f1..4fab3b9bf 100644 --- a/src/vz/vz_sdk.c +++ b/src/vz/vz_sdk.c @@ -3084,7 +3084,7 @@ static int prlsdkApplyGraphicsParams(PRL_HANDLE sdkdom,
glisten = virDomainGraphicsGetListen(gr, 0); pret = PrlVmCfg_SetVNCHostName(sdkdom, glisten && glisten->address ? - glisten->address : "127.0.0.1"); + glisten->address : VIR_LOOPBACK_IPV4_ADDR); prlsdkCheckRetGoto(pret, cleanup);
ret = 0;

If a VNC listen address is not specified in domXML, libxl will default to 127.0.0.1, but this is never reflected in the domXML. In the case of spice, a missing listen address resulted in listening on all interfaces, i.e. '0.0.0.0'. If not specified, set the listen address in virDomainGraphicsDef struct to the libxl default when creating the frame buffer device. Additionally, set default spice listen address to 127.0.0.1. Signed-off-by: Jim Fehlig <jfehlig@suse.com> --- V2: include setting the listen address for spice as well as VNC src/libxl/libxl_conf.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index 56bc09719..b04dffcfa 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -1227,13 +1227,18 @@ libxlMakeVfb(virPortAllocatorPtr graphicsports, } x_vfb->vnc.display = l_vfb->data.vnc.port - LIBXL_VNC_PORT_MIN; - if ((glisten = virDomainGraphicsGetListen(l_vfb, 0)) && - glisten->address) { - /* libxl_device_vfb_init() does VIR_STRDUP("127.0.0.1") */ - VIR_FREE(x_vfb->vnc.listen); - if (VIR_STRDUP(x_vfb->vnc.listen, glisten->address) < 0) - return -1; + if ((glisten = virDomainGraphicsGetListen(l_vfb, 0))) { + if (glisten->address) { + /* libxl_device_vfb_init() does VIR_STRDUP("127.0.0.1") */ + VIR_FREE(x_vfb->vnc.listen); + if (VIR_STRDUP(x_vfb->vnc.listen, glisten->address) < 0) + return -1; + } else { + if (VIR_STRDUP(glisten->address, VIR_LOOPBACK_IPV4_ADDR) < 0) + return -1; + } } + if (VIR_STRDUP(x_vfb->vnc.passwd, l_vfb->data.vnc.auth.passwd) < 0) return -1; if (VIR_STRDUP(x_vfb->keymap, l_vfb->data.vnc.keymap) < 0) @@ -1335,10 +1340,16 @@ libxlMakeBuildInfoVfb(virPortAllocatorPtr graphicsports, } b_info->u.hvm.spice.port = l_vfb->data.spice.port; - if ((glisten = virDomainGraphicsGetListen(l_vfb, 0)) && - glisten->address && - VIR_STRDUP(b_info->u.hvm.spice.host, glisten->address) < 0) - return -1; + if ((glisten = virDomainGraphicsGetListen(l_vfb, 0))) { + if (glisten->address) { + if (VIR_STRDUP(b_info->u.hvm.spice.host, glisten->address) < 0) + return -1; + } else { + if (VIR_STRDUP(b_info->u.hvm.spice.host, VIR_LOOPBACK_IPV4_ADDR) < 0 || + VIR_STRDUP(glisten->address, VIR_LOOPBACK_IPV4_ADDR) < 0) + return -1; + } + } if (VIR_STRDUP(b_info->u.hvm.keymap, l_vfb->data.spice.keymap) < 0) return -1; -- 2.11.0

On 05/19/2017 11:33 PM, Jim Fehlig wrote:
If a VNC listen address is not specified in domXML, libxl will default to 127.0.0.1, but this is never reflected in the domXML. In the case of spice, a missing listen address resulted in listening on all interfaces, i.e. '0.0.0.0'. If not specified, set the listen address in virDomainGraphicsDef struct to the libxl default when creating the frame buffer device. Additionally, set default spice listen address to 127.0.0.1.
Signed-off-by: Jim Fehlig <jfehlig@suse.com>
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
---
V2: include setting the listen address for spice as well as VNC
src/libxl/libxl_conf.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index 56bc09719..b04dffcfa 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -1227,13 +1227,18 @@ libxlMakeVfb(virPortAllocatorPtr graphicsports, } x_vfb->vnc.display = l_vfb->data.vnc.port - LIBXL_VNC_PORT_MIN;
- if ((glisten = virDomainGraphicsGetListen(l_vfb, 0)) && - glisten->address) { - /* libxl_device_vfb_init() does VIR_STRDUP("127.0.0.1") */ - VIR_FREE(x_vfb->vnc.listen); - if (VIR_STRDUP(x_vfb->vnc.listen, glisten->address) < 0) - return -1; + if ((glisten = virDomainGraphicsGetListen(l_vfb, 0))) { + if (glisten->address) { + /* libxl_device_vfb_init() does VIR_STRDUP("127.0.0.1") */ + VIR_FREE(x_vfb->vnc.listen); + if (VIR_STRDUP(x_vfb->vnc.listen, glisten->address) < 0) + return -1; + } else { + if (VIR_STRDUP(glisten->address, VIR_LOOPBACK_IPV4_ADDR) < 0) + return -1; + } } + if (VIR_STRDUP(x_vfb->vnc.passwd, l_vfb->data.vnc.auth.passwd) < 0) return -1; if (VIR_STRDUP(x_vfb->keymap, l_vfb->data.vnc.keymap) < 0) @@ -1335,10 +1340,16 @@ libxlMakeBuildInfoVfb(virPortAllocatorPtr graphicsports, } b_info->u.hvm.spice.port = l_vfb->data.spice.port;
- if ((glisten = virDomainGraphicsGetListen(l_vfb, 0)) && - glisten->address && - VIR_STRDUP(b_info->u.hvm.spice.host, glisten->address) < 0) - return -1; + if ((glisten = virDomainGraphicsGetListen(l_vfb, 0))) { + if (glisten->address) { + if (VIR_STRDUP(b_info->u.hvm.spice.host, glisten->address) < 0) + return -1; + } else { + if (VIR_STRDUP(b_info->u.hvm.spice.host, VIR_LOOPBACK_IPV4_ADDR) < 0 || + VIR_STRDUP(glisten->address, VIR_LOOPBACK_IPV4_ADDR) < 0) + return -1; + } + }
if (VIR_STRDUP(b_info->u.hvm.keymap, l_vfb->data.spice.keymap) < 0) return -1;

On 05/22/2017 03:31 AM, Joao Martins wrote:
On 05/19/2017 11:33 PM, Jim Fehlig wrote:
If a VNC listen address is not specified in domXML, libxl will default to 127.0.0.1, but this is never reflected in the domXML. In the case of spice, a missing listen address resulted in listening on all interfaces, i.e. '0.0.0.0'. If not specified, set the listen address in virDomainGraphicsDef struct to the libxl default when creating the frame buffer device. Additionally, set default spice listen address to 127.0.0.1.
Signed-off-by: Jim Fehlig <jfehlig@suse.com>
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
Thanks! Both patches have been pushed now. Regards, Jim
participants (2)
-
Jim Fehlig
-
Joao Martins