mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-14 16:45:20 +00:00
virNetworkDNSHostDefParseXML: Refactor parsing
Use 'virXMLNodeGetSubelementList' instead of looping through XML nodes and modernize the code. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
24b769a25b
commit
d0ee4e39bf
@ -655,63 +655,61 @@ virNetworkDNSHostDefParseXML(const char *networkName,
|
|||||||
virNetworkDNSHostDef *def,
|
virNetworkDNSHostDef *def,
|
||||||
bool partialOkay)
|
bool partialOkay)
|
||||||
{
|
{
|
||||||
xmlNodePtr cur;
|
g_autofree xmlNodePtr *hostnameNodes = NULL;
|
||||||
g_autofree char *ip = NULL;
|
size_t nhostnameNodes = virXMLNodeGetSubelementList(node, "hostname", &hostnameNodes);
|
||||||
|
size_t i;
|
||||||
|
g_auto(GStrv) hostnames = NULL;
|
||||||
|
g_autofree char *ip = virXMLPropString(node, "ip");
|
||||||
|
|
||||||
if (!(ip = virXMLPropString(node, "ip")) && !partialOkay) {
|
if (nhostnameNodes > 0) {
|
||||||
virReportError(VIR_ERR_XML_DETAIL,
|
hostnames = g_new0(char *, nhostnameNodes + 1);
|
||||||
_("Missing IP address in network '%1$s' DNS HOST record"),
|
|
||||||
networkName);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ip && (virSocketAddrParse(&def->ip, ip, AF_UNSPEC) < 0)) {
|
for (i = 0; i < nhostnameNodes; i++) {
|
||||||
virReportError(VIR_ERR_XML_DETAIL,
|
if (!(hostnames[i] = virXMLNodeContentString(hostnameNodes[i])))
|
||||||
_("Invalid IP address in network '%1$s' DNS HOST record"),
|
return -1;
|
||||||
networkName);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
cur = node->children;
|
if (*hostnames[i] == '\0') {
|
||||||
while (cur != NULL) {
|
virReportError(VIR_ERR_XML_DETAIL,
|
||||||
if (cur->type == XML_ELEMENT_NODE &&
|
_("Missing hostname in network '%1$s' DNS HOST record"),
|
||||||
virXMLNodeNameEqual(cur, "hostname")) {
|
networkName);
|
||||||
if (cur->children != NULL) {
|
return -1;
|
||||||
g_autofree char *name = virXMLNodeContentString(cur);
|
}
|
||||||
|
}
|
||||||
if (!name)
|
} else {
|
||||||
goto error;
|
if (!partialOkay) {
|
||||||
|
virReportError(VIR_ERR_XML_DETAIL,
|
||||||
if (!name[0]) {
|
_("Missing hostname in network '%1$s' DNS HOST record"),
|
||||||
virReportError(VIR_ERR_XML_DETAIL,
|
networkName);
|
||||||
_("Missing hostname in network '%1$s' DNS HOST record"),
|
return -1;
|
||||||
networkName);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
VIR_APPEND_ELEMENT(def->names, def->nnames, name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
cur = cur->next;
|
|
||||||
}
|
|
||||||
if (def->nnames == 0 && !partialOkay) {
|
|
||||||
virReportError(VIR_ERR_XML_DETAIL,
|
|
||||||
_("Missing hostname in network '%1$s' DNS HOST record"),
|
|
||||||
networkName);
|
|
||||||
goto error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!VIR_SOCKET_ADDR_VALID(&def->ip) && def->nnames == 0) {
|
if (ip) {
|
||||||
virReportError(VIR_ERR_XML_DETAIL,
|
if (virSocketAddrParse(&def->ip, ip, AF_UNSPEC) < 0) {
|
||||||
_("Missing ip and hostname in network '%1$s' DNS HOST record"),
|
virReportError(VIR_ERR_XML_DETAIL,
|
||||||
networkName);
|
_("Invalid IP address in network '%1$s' DNS HOST record"),
|
||||||
goto error;
|
networkName);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!partialOkay) {
|
||||||
|
virReportError(VIR_ERR_XML_DETAIL,
|
||||||
|
_("Missing IP address in network '%1$s' DNS HOST record"),
|
||||||
|
networkName);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nhostnameNodes == 0) {
|
||||||
|
virReportError(VIR_ERR_XML_DETAIL,
|
||||||
|
_("Missing ip and hostname in network '%1$s' DNS HOST record"),
|
||||||
|
networkName);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def->names = g_steal_pointer(&hostnames);
|
||||||
|
def->nnames = nhostnameNodes;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error:
|
|
||||||
virNetworkDNSHostDefClear(def);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user