[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 02/10] util: Extract helper to retrieve default port for network protocol



Make the stuff hardcoded in qemu a global helper so that other parts of
the code can determine the default port too.
---
 src/libvirt_private.syms  |  1 +
 src/qemu/qemu_command.c   | 55 ++++++++---------------------------------------
 src/util/virstoragefile.c | 43 ++++++++++++++++++++++++++++++++++++
 src/util/virstoragefile.h |  3 +++
 4 files changed, 56 insertions(+), 46 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 888412ac7..44b5dc1e0 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2614,6 +2614,7 @@ virStorageSourceIsBlockLocal;
 virStorageSourceIsEmpty;
 virStorageSourceIsLocalStorage;
 virStorageSourceIsRelative;
+virStorageSourceNetworkDefaultPort;
 virStorageSourceNewFromBacking;
 virStorageSourceNewFromBackingAbsolute;
 virStorageSourceParseRBDColonString;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index f77ae91c3..e5f4bdab7 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -482,55 +482,18 @@ qemuNetworkDriveGetPort(int protocol,
 {
     int ret = 0;

-    if (port) {
-        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;
-    }
-
-    switch ((virStorageNetProtocol) protocol) {
-        case VIR_STORAGE_NET_PROTOCOL_HTTP:
-            return 80;
-
-        case VIR_STORAGE_NET_PROTOCOL_HTTPS:
-            return 443;
-
-        case VIR_STORAGE_NET_PROTOCOL_FTP:
-            return 21;
-
-        case VIR_STORAGE_NET_PROTOCOL_FTPS:
-            return 990;
-
-        case VIR_STORAGE_NET_PROTOCOL_TFTP:
-            return 69;
-
-        case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
-            return 7000;
-
-        case VIR_STORAGE_NET_PROTOCOL_NBD:
-            return 10809;
-
-        case VIR_STORAGE_NET_PROTOCOL_SSH:
-            return 22;
-
-        case VIR_STORAGE_NET_PROTOCOL_ISCSI:
-        case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
-            /* no default port specified */
-            return 0;
+    if (!port &&
+        !(port = virStorageSourceNetworkDefaultPort(protocol)))
+        return -1;

-        case VIR_STORAGE_NET_PROTOCOL_RBD:
-        case VIR_STORAGE_NET_PROTOCOL_LAST:
-        case VIR_STORAGE_NET_PROTOCOL_NONE:
-            /* not applicable */
-            return -1;
+    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 -1;
+    return ret;
 }


diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index f0ed5c6bd..cae20eccc 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -4018,3 +4018,46 @@ virStorageSourceFindByNodeName(virStorageSourcePtr top,
         *idx = 0;
     return NULL;
 }
+
+
+const char *
+virStorageSourceNetworkDefaultPort(virStorageNetProtocol protocol)
+{
+    switch (protocol) {
+        case VIR_STORAGE_NET_PROTOCOL_HTTP:
+            return "80";
+
+        case VIR_STORAGE_NET_PROTOCOL_HTTPS:
+            return "443";
+
+        case VIR_STORAGE_NET_PROTOCOL_FTP:
+            return "21";
+
+        case VIR_STORAGE_NET_PROTOCOL_FTPS:
+            return "990";
+
+        case VIR_STORAGE_NET_PROTOCOL_TFTP:
+            return "69";
+
+        case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
+            return "7000";
+
+        case VIR_STORAGE_NET_PROTOCOL_NBD:
+            return "10809";
+
+        case VIR_STORAGE_NET_PROTOCOL_SSH:
+            return "22";
+
+        case VIR_STORAGE_NET_PROTOCOL_ISCSI:
+        case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
+            /* no default port specified */
+            return "0";
+
+        case VIR_STORAGE_NET_PROTOCOL_RBD:
+        case VIR_STORAGE_NET_PROTOCOL_LAST:
+        case VIR_STORAGE_NET_PROTOCOL_NONE:
+            return NULL;
+    }
+
+    return NULL;
+}
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index 0bff8671f..877c3af4d 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -406,4 +406,7 @@ virStorageSourceFindByNodeName(virStorageSourcePtr top,
                                unsigned int *index)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

+const char *
+virStorageSourceNetworkDefaultPort(virStorageNetProtocol protocol);
+
 #endif /* __VIR_STORAGE_FILE_H__ */
-- 
2.12.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]