]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: command: Rename and move qemuNetworkDriveGetPort
authorPeter Krempa <pkrempa@redhat.com>
Thu, 13 Jul 2017 13:31:50 +0000 (15:31 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 24 Jul 2017 08:55:20 +0000 (10:55 +0200)
Move it to virstring.c and improve it to parse and validate ports. New
name is virStringParsePort.

src/libvirt_private.syms
src/qemu/qemu_command.c
src/util/virstring.c
src/util/virstring.h

index eda4269b70e72fcd8d5ee587682cf7e89b2167db..b6569e2430241138b54225796e0590dd333e6acd 100644 (file)
@@ -2657,6 +2657,7 @@ virStringListJoin;
 virStringListLength;
 virStringListRemove;
 virStringMatch;
+virStringParsePort;
 virStringReplace;
 virStringSearch;
 virStringSortCompare;
index 0ce5aa590686e2ffeeedc4b7fdc155df579abebf..d7f101a9e757aa55e1b16d0e5cf61af4b32fe7c2 100644 (file)
@@ -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,
index 9b54bd6fb6da071b328621281d771dfad5cc0cb9..b9fbdb58d9f99aac469dadb4f7c7db48ba137cd1 100644 (file)
@@ -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 > UINT16_MAX) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("port '%s' out of range"), str);
+        return -1;
+    }
+
+    *port = p;
+
+    return 0;
+}
index 5eaaaea0a2fb21db8aac6d8fe644e167c687f0e4..e562bf5143efef67b06d77a6a6481cb6687237f7 100644 (file)
@@ -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__ */