提交 e8b69016 编写于 作者: P Peter Krempa

qemu: command: Rename and move qemuNetworkDriveGetPort

Move it to virstring.c and improve it to parse and validate ports. New
name is virStringParsePort.
上级 f36f2e46
...@@ -2657,6 +2657,7 @@ virStringListJoin; ...@@ -2657,6 +2657,7 @@ virStringListJoin;
virStringListLength; virStringListLength;
virStringListRemove; virStringListRemove;
virStringMatch; virStringMatch;
virStringParsePort;
virStringReplace; virStringReplace;
virStringSearch; virStringSearch;
virStringSortCompare; virStringSortCompare;
......
...@@ -491,22 +491,6 @@ qemuSafeSerialParamValue(const char *value) ...@@ -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, ...@@ -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,
......
...@@ -1344,3 +1344,40 @@ void virStringTrimOptionalNewline(char *str) ...@@ -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;
}
...@@ -296,4 +296,8 @@ char *virStringEncodeBase64(const uint8_t *buf, size_t buflen); ...@@ -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__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册