Move it to virstring.c and improve it to parse and validate ports. New
name is virStringParsePort.
---
src/libvirt_private.syms | 1 +
src/qemu/qemu_command.c | 18 +-----------------
src/util/virstring.c | 37 +++++++++++++++++++++++++++++++++++++
src/util/virstring.h | 4 ++++
4 files changed, 43 insertions(+), 17 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 187b12b32..f0c2c8446 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2656,6 +2656,7 @@ virStringListJoin;
virStringListLength;
virStringListRemove;
virStringMatch;
+virStringParsePort;
virStringReplace;
virStringSearch;
virStringSortCompare;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 6ac26af3e..be23796df 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -491,22 +491,6 @@ qemuSafeSerialParamValue(const char *value)
}
-static int
-qemuNetworkDriveGetPort(const char *port)
-{
- int ret = 0;
-
- if (virStrToLong_i(port, NULL, 10, &ret) < 0 || ret < 0) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("failed to parse port number '%s'"),
- port);
- return -1;
- }
-
- return ret;
-}
-
-
/**
* qemuBuildSecretInfoProps:
* @secinfo: pointer to the secret info object
@@ -825,7 +809,7 @@ qemuBuildNetworkDriveURI(virStorageSourcePtr src,
goto cleanup;
if (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) {
- if ((uri->port = qemuNetworkDriveGetPort(src->hosts->port)) < 0)
+ if (virStringParsePort(src->hosts->port, &uri->port) < 0)
goto cleanup;
if (VIR_STRDUP(uri->scheme,
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 9b54bd6fb..fe61a3516 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -1344,3 +1344,40 @@ void virStringTrimOptionalNewline(char *str)
if (*tmp == '\n')
*tmp = '\0';
}
+
+
+/**
+ * virStringParsePort:
+ * @str: port number to parse
+ * @port: pointer to parse port into
+ *
+ * Parses a string representation of a network port and validates it. Returns
+ * 0 on success and -1 on error.
+ */
+int
+virStringParsePort(const char *str,
+ int *port)
+{
+ unsigned int p = 0;
+
+ *port = 0;
+
+ if (!str)
+ return 0;
+
+ if (virStrToLong_uip(str, NULL, 10, &p) < 0) {
+ virReportError(VIR_ERR_INVALID_ARG,
+ _("failed to parse port number '%s'"), str);
+ return -1;
+ }
+
+ if (p > 65535) {
+ virReportError(VIR_ERR_INVALID_ARG,
+ _("port '%s' out of range"), str);
+ return -1;
+ }
+
+ *port = p;
+
+ return 0;
+}
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 5eaaaea0a..e562bf514 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -296,4 +296,8 @@ char *virStringEncodeBase64(const uint8_t *buf, size_t buflen);
void virStringTrimOptionalNewline(char *str);
+int virStringParsePort(const char *str,
+ int *port)
+ ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
+
#endif /* __VIR_STRING_H__ */
--
2.13.2