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.
This commit is contained in:
Peter Krempa 2017-07-07 15:21:04 +02:00
parent dbf06d907e
commit 34ffc2ff41
4 changed files with 56 additions and 46 deletions

View File

@ -2617,6 +2617,7 @@ virStorageSourceIsBlockLocal;
virStorageSourceIsEmpty;
virStorageSourceIsLocalStorage;
virStorageSourceIsRelative;
virStorageSourceNetworkDefaultPort;
virStorageSourceNewFromBacking;
virStorageSourceNewFromBackingAbsolute;
virStorageSourceParseRBDColonString;

View File

@ -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;
}
if (!port &&
!(port = virStorageSourceNetworkDefaultPort(protocol)))
return -1;
return ret;
if (virStrToLong_i(port, NULL, 10, &ret) < 0 || ret < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("failed to parse port number '%s'"),
port);
return -1;
}
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;
case VIR_STORAGE_NET_PROTOCOL_RBD:
case VIR_STORAGE_NET_PROTOCOL_LAST:
case VIR_STORAGE_NET_PROTOCOL_NONE:
/* not applicable */
return -1;
}
return -1;
return ret;
}

View File

@ -3959,3 +3959,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;
}

View File

@ -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__ */