qemu: command: Rename and move qemuNetworkDriveGetPort

Move it to virstring.c and improve it to parse and validate ports. New
name is virStringParsePort.
This commit is contained in:
Peter Krempa 2017-07-13 15:31:50 +02:00
parent f36f2e463f
commit e8b69016b1
4 changed files with 43 additions and 17 deletions

View File

@ -2657,6 +2657,7 @@ virStringListJoin;
virStringListLength; virStringListLength;
virStringListRemove; virStringListRemove;
virStringMatch; virStringMatch;
virStringParsePort;
virStringReplace; virStringReplace;
virStringSearch; virStringSearch;
virStringSortCompare; virStringSortCompare;

View 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: * qemuBuildSecretInfoProps:
* @secinfo: pointer to the secret info object * @secinfo: pointer to the secret info object
@ -825,7 +809,7 @@ qemuBuildNetworkDriveURI(virStorageSourcePtr src,
goto cleanup; goto cleanup;
if (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) { 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; goto cleanup;
if (VIR_STRDUP(uri->scheme, if (VIR_STRDUP(uri->scheme,

View File

@ -1344,3 +1344,40 @@ void virStringTrimOptionalNewline(char *str)
if (*tmp == '\n') if (*tmp == '\n')
*tmp = '\0'; *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;
}

View File

@ -296,4 +296,8 @@ char *virStringEncodeBase64(const uint8_t *buf, size_t buflen);
void virStringTrimOptionalNewline(char *str); void virStringTrimOptionalNewline(char *str);
int virStringParsePort(const char *str,
int *port)
ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
#endif /* __VIR_STRING_H__ */ #endif /* __VIR_STRING_H__ */