1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

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; virStorageSourceIsEmpty;
virStorageSourceIsLocalStorage; virStorageSourceIsLocalStorage;
virStorageSourceIsRelative; virStorageSourceIsRelative;
virStorageSourceNetworkDefaultPort;
virStorageSourceNewFromBacking; virStorageSourceNewFromBacking;
virStorageSourceNewFromBackingAbsolute; virStorageSourceNewFromBackingAbsolute;
virStorageSourceParseRBDColonString; virStorageSourceParseRBDColonString;

View File

@ -482,55 +482,18 @@ qemuNetworkDriveGetPort(int protocol,
{ {
int ret = 0; int ret = 0;
if (port) { if (!port &&
if (virStrToLong_i(port, NULL, 10, &ret) < 0 || ret < 0) { !(port = virStorageSourceNetworkDefaultPort(protocol)))
virReportError(VIR_ERR_INTERNAL_ERROR, return -1;
_("failed to parse port number '%s'"),
port);
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) { return ret;
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;
} }

View File

@ -3959,3 +3959,46 @@ virStorageSourceFindByNodeName(virStorageSourcePtr top,
*idx = 0; *idx = 0;
return NULL; 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) unsigned int *index)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
const char *
virStorageSourceNetworkDefaultPort(virStorageNetProtocol protocol);
#endif /* __VIR_STORAGE_FILE_H__ */ #endif /* __VIR_STORAGE_FILE_H__ */