Currently, we are building InetSocketAddress qemu json type
within the qemuMonitorJSONNBDServerStart function. However, other
future functions may profit from the code as well. So it should
be moved into a static function.
---
src/qemu/qemu_monitor_json.c | 44 +++++++++++++++++++++++++++++---------------
1 file changed, 29 insertions(+), 15 deletions(-)
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index d326551..626db8b 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4738,6 +4738,31 @@ cleanup:
return ret;
}
+static virJSONValuePtr
+qemuMonitorJSONBuildInetSocketAddress(const char *host,
+ const char *port)
+{
+ virJSONValuePtr addr = NULL;
+ virJSONValuePtr data = NULL;
+
+ if (!(data = virJSONValueNewObject()) ||
+ !(addr = virJSONValueNewObject()))
+ goto error;
+
+ /* port is really expected as a string here by qemu */
+ if (virJSONValueObjectAppendString(data, "host", host) < 0 ||
+ virJSONValueObjectAppendString(data, "port", port) < 0 ||
+ virJSONValueObjectAppendString(addr, "type", "inet") < 0
||
+ virJSONValueObjectAppend(addr, "data", data) < 0)
+ goto error;
+
+ return addr;
+error:
+ virJSONValueFree(data);
+ virJSONValueFree(addr);
+ return NULL;
+}
+
int
qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon,
const char *host,
@@ -4746,24 +4771,14 @@ qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon,
int ret = -1;
virJSONValuePtr cmd = NULL;
virJSONValuePtr reply = NULL;
- virJSONValuePtr data = NULL;
virJSONValuePtr addr = NULL;
char *port_str = NULL;
- if (!(data = virJSONValueNewObject()) ||
- !(addr = virJSONValueNewObject()) ||
- (virAsprintf(&port_str, "%u", port) < 0))
- goto cleanup;
-
- /* port is really expected as a string here by qemu */
- if (virJSONValueObjectAppendString(data, "host", host) < 0 ||
- virJSONValueObjectAppendString(data, "port", port_str) < 0 ||
- virJSONValueObjectAppendString(addr, "type", "inet") < 0
||
- virJSONValueObjectAppend(addr, "data", data) < 0)
- goto cleanup;
+ if (virAsprintf(&port_str, "%u", port) < 0)
+ return ret;
- /* From now on, @data is part of @addr */
- data = NULL;
+ if (!(addr = qemuMonitorJSONBuildInetSocketAddress(host, port_str)))
+ return ret;
if (!(cmd = qemuMonitorJSONMakeCommand("nbd-server-start",
"a:addr", addr,
@@ -4786,7 +4801,6 @@ cleanup:
virJSONValueFree(reply);
virJSONValueFree(cmd);
virJSONValueFree(addr);
- virJSONValueFree(data);
return ret;
}
--
1.8.1.5