[PATCH 28/37] virDomainNetDefParseXML: Refactor parsing of data for VIR_DOMAIN_NET_TYPE_UDP/MCAST/SERVER/CLIENT

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This also removes the confusing use of variables named 'tmpNode' and
'tmp_node' right next to each other.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/conf/domain_conf.c | 101 +++++++++++++----------------------------
 1 file changed, 31 insertions(+), 70 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index c14f3f2910..cc28bc9684 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8994,7 +8994,6 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,
     xmlNodePtr filterref_node = NULL;
     xmlNodePtr vlan_node = NULL;
     xmlNodePtr bandwidth_node = NULL;
-    xmlNodePtr tmpNode;
     xmlNodePtr mac_node = NULL;
     g_autoptr(GHashTable) filterparams = NULL;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
@@ -9002,10 +9001,6 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,
     g_autofree char *macaddr = NULL;
     g_autofree char *dev = NULL;
     g_autofree char *managed_tap = NULL;
-    g_autofree char *address = NULL;
-    g_autofree char *port = NULL;
-    g_autofree char *localaddr = NULL;
-    g_autofree char *localport = NULL;
     g_autofree char *model = NULL;
     g_autofree char *filter = NULL;
     g_autofree char *linkstate = NULL;
@@ -9199,18 +9194,38 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,
     case VIR_DOMAIN_NET_TYPE_SERVER:
     case VIR_DOMAIN_NET_TYPE_MCAST:
     case VIR_DOMAIN_NET_TYPE_UDP:
-        if (source_node) {
-            address = virXMLPropString(source_node, "address");
-            port = virXMLPropString(source_node, "port");
-            if (def->type == VIR_DOMAIN_NET_TYPE_UDP) {
-                xmlNodePtr tmp_node = ctxt->node;
-                ctxt->node = source_node;
-                if ((tmpNode = virXPathNode("./local", ctxt))) {
-                    localaddr = virXMLPropString(tmpNode, "address");
-                    localport = virXMLPropString(tmpNode, "port");
-                }
-                ctxt->node = tmp_node;
+        if (virDomainNetDefParseXMLRequireSource(def, source_node) < 0)
+            return NULL;
+
+        if (def->type != VIR_DOMAIN_NET_TYPE_SERVER) {
+            if (!(def->data.socket.address = virXMLPropStringRequired(source_node, "address")))
+                return NULL;
+        } else {
+            def->data.socket.address = virXMLPropString(source_node, "address");
+        }
+
+        if (virXMLPropInt(source_node, "port", 10, VIR_XML_PROP_REQUIRED,
+                          &def->data.socket.port, def->data.socket.port) < 0)
+            return NULL;
+
+        if (def->type == VIR_DOMAIN_NET_TYPE_UDP) {
+            VIR_XPATH_NODE_AUTORESTORE_NAME(localCtxt, ctxt)
+            xmlNodePtr local_node;
+
+            ctxt->node = source_node;
+
+            if (!(local_node = virXPathNode("./local", ctxt))) {
+                virReportError(VIR_ERR_XML_ERROR, "%s",
+                               _("'<local>' element missing for 'udp' socket interface"));
+                return NULL;
             }
+
+            if (!(def->data.socket.localaddr = virXMLPropStringRequired(local_node, "address")))
+                return NULL;
+
+            if (virXMLPropInt(local_node, "port", 10, VIR_XML_PROP_REQUIRED,
+                              &def->data.socket.localport, def->data.socket.localport) < 0)
+                return NULL;
         }
         break;

@@ -9334,64 +9349,10 @@ virDomainNetDefParseXML(virDomainXMLOption *xmlopt,

     case VIR_DOMAIN_NET_TYPE_VDPA:
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
-        break;
-
     case VIR_DOMAIN_NET_TYPE_CLIENT:
     case VIR_DOMAIN_NET_TYPE_SERVER:
     case VIR_DOMAIN_NET_TYPE_MCAST:
     case VIR_DOMAIN_NET_TYPE_UDP:
-        if (port == NULL) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("No <source> 'port' attribute "
-                             "specified with socket interface"));
-            return NULL;
-        }
-        if (virStrToLong_i(port, NULL, 10, &def->data.socket.port) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("Cannot parse <source> 'port' attribute "
-                             "with socket interface"));
-            return NULL;
-        }
-
-        if (address == NULL) {
-            if (def->type == VIR_DOMAIN_NET_TYPE_CLIENT ||
-                def->type == VIR_DOMAIN_NET_TYPE_MCAST ||
-                def->type == VIR_DOMAIN_NET_TYPE_UDP) {
-                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                               _("No <source> 'address' attribute "
-                                 "specified with socket interface"));
-                return NULL;
-            }
-        } else {
-            def->data.socket.address = g_steal_pointer(&address);
-        }
-
-        if (def->type != VIR_DOMAIN_NET_TYPE_UDP)
-            break;
-
-        if (localport == NULL) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("No <local> 'port' attribute "
-                             "specified with socket interface"));
-            return NULL;
-        }
-        if (virStrToLong_i(localport, NULL, 10, &def->data.socket.localport) < 0) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("Cannot parse <local> 'port' attribute "
-                             "with socket interface"));
-            return NULL;
-        }
-
-        if (localaddr == NULL) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("No <local> 'address' attribute "
-                             "specified with socket interface"));
-            return NULL;
-        } else {
-            def->data.socket.localaddr = g_steal_pointer(&localaddr);
-        }
-        break;
-
     case VIR_DOMAIN_NET_TYPE_INTERNAL:
     case VIR_DOMAIN_NET_TYPE_DIRECT:
         break;
-- 
2.37.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux