]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virNetworkDNSHostDefParseXML: Refactor parsing
authorPeter Krempa <pkrempa@redhat.com>
Tue, 11 Apr 2023 20:34:59 +0000 (22:34 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 4 Sep 2023 08:31:52 +0000 (10:31 +0200)
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>
src/conf/network_conf.c

index 368f9618a0e218f59def80bf3f0440d2de654ac7..1a6fd86180109bc0cd0d2f7da17025313cd6a89a 100644 (file)
@@ -655,63 +655,61 @@ virNetworkDNSHostDefParseXML(const char *networkName,
                              virNetworkDNSHostDef *def,
                              bool partialOkay)
 {
-    xmlNodePtr cur;
-    g_autofree char *ip = NULL;
-
-    if (!(ip = virXMLPropString(node, "ip")) && !partialOkay) {
-        virReportError(VIR_ERR_XML_DETAIL,
-                       _("Missing IP address in network '%1$s' DNS HOST record"),
-                       networkName);
-        goto error;
-    }
-
-    if (ip && (virSocketAddrParse(&def->ip, ip, AF_UNSPEC) < 0)) {
-        virReportError(VIR_ERR_XML_DETAIL,
-                       _("Invalid IP address in network '%1$s' DNS HOST record"),
-                       networkName);
-        goto error;
-    }
+    g_autofree xmlNodePtr *hostnameNodes = NULL;
+    size_t nhostnameNodes = virXMLNodeGetSubelementList(node, "hostname", &hostnameNodes);
+    size_t i;
+    g_auto(GStrv) hostnames = NULL;
+    g_autofree char *ip = virXMLPropString(node, "ip");
 
-    cur = node->children;
-    while (cur != NULL) {
-        if (cur->type == XML_ELEMENT_NODE &&
-            virXMLNodeNameEqual(cur, "hostname")) {
-              if (cur->children != NULL) {
-                  g_autofree char *name = virXMLNodeContentString(cur);
+    if (nhostnameNodes > 0) {
+        hostnames = g_new0(char *, nhostnameNodes + 1);
 
-                  if (!name)
-                      goto error;
+        for (i = 0; i < nhostnameNodes; i++) {
+            if (!(hostnames[i] = virXMLNodeContentString(hostnameNodes[i])))
+                return -1;
 
-                  if (!name[0]) {
-                      virReportError(VIR_ERR_XML_DETAIL,
-                                     _("Missing hostname in network '%1$s' DNS HOST record"),
-                                     networkName);
-                      goto error;
-                  }
-                  VIR_APPEND_ELEMENT(def->names, def->nnames, name);
-              }
+            if (*hostnames[i] == '\0') {
+                virReportError(VIR_ERR_XML_DETAIL,
+                               _("Missing hostname in network '%1$s' DNS HOST record"),
+                               networkName);
+                return -1;
+            }
+        }
+    } else {
+        if (!partialOkay) {
+            virReportError(VIR_ERR_XML_DETAIL,
+                           _("Missing hostname in network '%1$s' DNS HOST record"),
+                           networkName);
+            return -1;
         }
-        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) {
-        virReportError(VIR_ERR_XML_DETAIL,
-                       _("Missing ip and hostname in network '%1$s' DNS HOST record"),
-                       networkName);
-        goto error;
+    if (ip) {
+        if (virSocketAddrParse(&def->ip, ip, AF_UNSPEC) < 0) {
+            virReportError(VIR_ERR_XML_DETAIL,
+                           _("Invalid IP address in network '%1$s' DNS HOST record"),
+                           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;
-
- error:
-    virNetworkDNSHostDefClear(def);
-    return -1;
 }