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

[libvirt] [PATCH 5/5] network_conf: change host field in _virNetworkDNSDef to pointer to pointer



Change the type of host filed in struct _virNetworkDNSDef from
virNetworkDNSHostsDefPtr to virNetworkDNSHostsDefPtr *.
Create new function virNetworkDNSHostsDefParseXML to parse host
xml defination.
Create virNetworkDNSHostsDefFree to free type data of type
virNetworkDNSHostsDefPtr.
---
 src/conf/network_conf.c     |   98 ++++++++++++++++++++++++------------------
 src/conf/network_conf.h     |    5 +-
 src/network/bridge_driver.c |    2 +-
 3 files changed, 60 insertions(+), 45 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index b720c9f..1d0ae50 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -132,9 +132,9 @@ static void virNetworkDNSDefFree(virNetworkDNSDefPtr def)
         VIR_FREE(def->txtrecords);
         if (def->nhosts) {
             while (def->nhosts--) {
-                while (def->hosts[def->nhosts].nnames--)
-                    VIR_FREE(def->hosts[def->nhosts].names[def->hosts[def->nhosts].nnames]);
-                VIR_FREE(def->hosts[def->nhosts].names);
+                while (def->hosts[def->nhosts]->nnames--)
+                    VIR_FREE(def->hosts[def->nhosts]->names[def->hosts[def->nhosts]->nnames]);
+                VIR_FREE(def->hosts[def->nhosts]->names);
             }
         }
         VIR_FREE(def->hosts);
@@ -550,33 +550,42 @@ error:
     return NULL;
 }
 
-static int
-virNetworkDNSHostsDefParseXML(virNetworkDNSDefPtr def,
-                              xmlNodePtr node)
+void virNetworkDNSHostsDefFree(virNetworkDNSHostsDefPtr host)
+{
+    int i;
+
+    if (!host)
+        return;
+
+    for (i = 0; i < host->nnames; i++)
+        VIR_FREE(host->names[i]);
+
+    VIR_FREE(host->names);
+    VIR_FREE(host);
+}
+
+static virNetworkDNSHostsDefPtr
+virNetworkDNSHostsDefParseXML(xmlNodePtr node)
 {
     xmlNodePtr cur;
-    char *ip;
+    char *ip = NULL;
     virSocketAddr inaddr;
-    int ret = -1;
+    virNetworkDNSHostsDefPtr host = NULL;
+
+    if (VIR_ALLOC(host) < 0) {
+        virReportOOMError();
+        goto error;
+    }
 
     if (!(ip = virXMLPropString(node, "ip")) ||
         (virSocketAddrParse(&inaddr, ip, AF_UNSPEC) < 0)) {
         virNetworkReportError(VIR_ERR_XML_DETAIL,
                               _("Missing IP address in DNS host definition"));
-        VIR_FREE(ip);
         goto error;
     }
-    VIR_FREE(ip);
+    host->ip = inaddr;
 
-    if (VIR_REALLOC_N(def->hosts, def->nhosts + 1) < 0) {
-        virReportOOMError();
-        goto error;
-    }
-
-    def->hosts[def->nhosts].ip = inaddr;
-    def->hosts[def->nhosts].nnames = 0;
-
-    if (VIR_ALLOC(def->hosts[def->nhosts].names) < 0) {
+    if (VIR_ALLOC(host->names) < 0) {
         virReportOOMError();
         goto error;
     }
@@ -586,25 +595,24 @@ virNetworkDNSHostsDefParseXML(virNetworkDNSDefPtr def,
         if (cur->type == XML_ELEMENT_NODE &&
             xmlStrEqual(cur->name, BAD_CAST "hostname")) {
               if (cur->children != NULL) {
-                  if (VIR_REALLOC_N(def->hosts[def->nhosts].names, def->hosts[def->nhosts].nnames + 1) < 0) {
+                  if (VIR_REALLOC_N(host->names, host->nnames + 1) < 0) {
                       virReportOOMError();
                       goto error;
                   }
 
-                  def->hosts[def->nhosts].names[def->hosts[def->nhosts].nnames] = strdup((char *)cur->children->content);
-                  def->hosts[def->nhosts].nnames++;
+                  host->names[host->nnames] = strdup((char *)cur->children->content);
+                  host->nnames++;
               }
         }
 
         cur = cur->next;
     }
 
-    def->nhosts++;
-
-    ret = 0;
-
+    return host;
 error:
-    return ret;
+    VIR_FREE(ip);
+    virNetworkDNSHostsDefFree(host);
+    return NULL;
 }
 
 void
@@ -782,11 +790,9 @@ error:
 
 static int
 virNetworkDNSDefParseXML(virNetworkDNSDefPtr *dnsdef,
-                         xmlNodePtr node,
                          xmlXPathContextPtr ctxt)
 {
     xmlNodePtr *nodes = NULL;
-    xmlNodePtr cur;
     int i, n;
     int ret = -1;
     virNetworkDNSDefPtr def = NULL;
@@ -838,18 +844,26 @@ virNetworkDNSDefParseXML(virNetworkDNSDefPtr *dnsdef,
     }
     VIR_FREE(nodes);
 
+    if ((n = virXPathNodeSet("./dns/host", ctxt, &nodes)) < 0) {
+        virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
+                              "%s", _("cannot extract host nodes"));
+        goto error;
+    }
 
-    cur = node->children;
-    while (cur != NULL) {
-        if (cur->type == XML_ELEMENT_NODE &&
-            xmlStrEqual(cur->name, BAD_CAST "host")) {
-            ret = virNetworkDNSHostsDefParseXML(def, cur);
-            if (ret < 0)
-                goto error;
-        }
+    if (n && VIR_REALLOC_N(def->hosts, def->nhosts + n) < 0) {
+        virReportOOMError();
+        goto error;
+    }
+    for (i = 0; i < n; i++) {
+        virNetworkDNSHostsDefPtr host;
 
-        cur = cur->next;
+        host = virNetworkDNSHostsDefParseXML(nodes[i]);
+        if (!host)
+            goto error;
+
+        def->hosts[def->nhosts++] = host;
     }
+    VIR_FREE(nodes);
 
     ret = 0;
 error:
@@ -1136,7 +1150,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
 
     dnsNode = virXPathNode("./dns", ctxt);
     if (dnsNode != NULL) {
-        if (virNetworkDNSDefParseXML(&def->dns, dnsNode, ctxt) < 0)
+        if (virNetworkDNSDefParseXML(&def->dns, ctxt) < 0)
             goto error;
     }
 
@@ -1457,13 +1471,13 @@ virNetworkDNSDefFormat(virBufferPtr buf,
         int ii, j;
 
         for (ii = 0 ; ii < def->nhosts; ii++) {
-            char *ip = virSocketAddrFormat(&def->hosts[ii].ip);
+            char *ip = virSocketAddrFormat(&def->hosts[ii]->ip);
 
             virBufferAsprintf(buf, "    <host ip='%s'>\n", ip);
 
-            for (j = 0; j < def->hosts[ii].nnames; j++)
+            for (j = 0; j < def->hosts[ii]->nnames; j++)
                 virBufferAsprintf(buf, "      <hostname>%s</hostname>\n",
-                                               def->hosts[ii].names[j]);
+                                               def->hosts[ii]->names[j]);
 
             virBufferAsprintf(buf, "    </host>\n");
             VIR_FREE(ip);
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 16fbe81..b11fc3c 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -89,19 +89,20 @@ struct _virNetworkDNSSrvRecordsDef {
 void virNetworkDNSSrvDefFree(virNetworkDNSSrvRecordsDefPtr srv);
 virNetworkDNSSrvRecordsDefPtr virNetworkDNSSrvDefAlloc(void);
 
+typedef struct _virNetworkDNSHostsDef *virNetworkDNSHostsDefPtr;
 struct _virNetworkDNSHostsDef {
     virSocketAddr ip;
     int nnames;
     char **names;
 };
 
-typedef struct _virNetworkDNSHostsDef *virNetworkDNSHostsDefPtr;
+void virNetworkDNSHostsDefFree(virNetworkDNSHostsDefPtr host);
 
 struct _virNetworkDNSDef {
     unsigned int ntxtrecords;
     virNetworkDNSTxtRecordsDefPtr *txtrecords;
     unsigned int nhosts;
-    virNetworkDNSHostsDefPtr hosts;
+    virNetworkDNSHostsDefPtr *hosts;
     unsigned int nsrvrecords;
     virNetworkDNSSrvRecordsDefPtr *srvrecords;
 };
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index c0b05bd..ca64d1d 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -436,7 +436,7 @@ networkBuildDnsmasqHostsfile(dnsmasqContext *dctx,
 
     if (dnsdef) {
         for (i = 0; i < dnsdef->nhosts; i++) {
-            virNetworkDNSHostsDefPtr host = &(dnsdef->hosts[i]);
+            virNetworkDNSHostsDefPtr host = dnsdef->hosts[i];
             if (VIR_SOCKET_ADDR_VALID(&host->ip)) {
                 for (j = 0; j < host->nnames; j++)
                     if (dnsmasqAddHost(dctx, &host->ip, host->names[j]) < 0)
-- 
1.7.7.5


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