Rearrange this function to be better organized and more correct:
* the error codes were changed from the incorrect INVALID_ARG to
XML_ERROR
* prefix still isn't required, but if present it must be valid or an
error will be logged.
* don't emit a debug log just because prefix or peer is missing - this
is valid.
* group everything related to setting peer in one place rather than
scattered through the function. Same for peer.
---
src/conf/domain_conf.c | 38 ++++++++++++++++++++------------------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 3cb21e4..f7ee52b 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -5746,21 +5746,12 @@ virDomainNetIpParseXML(xmlNodePtr node)
int family = AF_UNSPEC;
char *address = NULL, *peer = NULL;
- if (!(prefixStr = virXMLPropString(node, "prefix")) ||
- (virStrToLong_ui(prefixStr, NULL, 10, &prefixValue) < 0)) {
- // Don't shout, as some old config may not have a prefix
- VIR_DEBUG("Missing or invalid network prefix");
- }
-
if (!(address = virXMLPropString(node, "address"))) {
- virReportError(VIR_ERR_INVALID_ARG, "%s",
- _("Missing network address"));
+ virReportError(VIR_ERR_XML_ERROR, "%s",
+ _("Missing required address in <ip>"));
goto cleanup;
}
- if ((peer = virXMLPropString(node, "peer")) == NULL)
- VIR_DEBUG("Peer is empty");
-
familyStr = virXMLPropString(node, "family");
if (familyStr && STREQ(familyStr, "ipv4"))
family = AF_INET;
@@ -5773,21 +5764,32 @@ virDomainNetIpParseXML(xmlNodePtr node)
goto cleanup;
if (virSocketAddrParse(&ip->address, address, family) < 0) {
- virReportError(VIR_ERR_INVALID_ARG,
- _("Failed to parse IP address: '%s'"),
+ virReportError(VIR_ERR_XML_ERROR,
+ _("Invalid address '%s' in <ip>"),
address);
goto cleanup;
}
- if ((peer != NULL) && (virSocketAddrParse(&ip->peer, peer, family)
< 0)) {
- virReportError(VIR_ERR_INVALID_ARG,
- _("Failed to parse IP address: '%s'"),
+ prefixStr = virXMLPropString(node, "prefix");
+ if (prefixStr &&
+ ((virStrToLong_ui(prefixStr, NULL, 10, &prefixValue) < 0) ||
+ (family == AF_INET6 && prefixValue > 128) ||
+ (family == AF_INET && prefixValue > 32))) {
+ virReportError(VIR_ERR_XML_ERROR,
+ _("Invalid prefix value '%s' in <ip>"),
+ prefixStr);
+ goto cleanup;
+ }
+ ip->prefix = prefixValue;
+
+ peer = virXMLPropString(node, "peer");
+ if (peer && (virSocketAddrParse(&ip->peer, peer, family) < 0)) {
+ virReportError(VIR_ERR_XML_ERROR,
+ _("Invalid peer address '%s' in
<ip>"),
peer);
goto cleanup;
}
- ip->prefix = prefixValue;
-
ret = ip;
ip = NULL;
--
2.5.5