>From fd4623ad8473a4f0c53a096b7ad8140fc583c946 Mon Sep 17 00:00:00 2001
From: Laine Stump <laine@laine.org>
Date: Tue, 4 Dec 2012 14:10:34 -0500
Subject: [PATCH] squash into 'add support for DHCPv6' part 1

---
 docs/formatnetwork.html.in |   6 +--
 src/conf/network_conf.c    | 112 +++++++++++++++++++--------------------------
 2 files changed, 51 insertions(+), 67 deletions(-)

diff --git a/docs/formatnetwork.html.in b/docs/formatnetwork.html.in
index a5f0dc7..0ce8813 100644
--- a/docs/formatnetwork.html.in
+++ b/docs/formatnetwork.html.in
@@ -601,7 +601,7 @@
         <code>prefix</code>. <span class="since">Since 0.8.7</span>  In addition
         to the one IPv4 address which has a <code>dhcp</code> definition, one IPv6
         address can have a <code>dhcp</code> definition.
-        <span class="since"> Since 1.0.1</span>
+        <span class="since">Since 1.0.1</span>
         <dl>
           <dt><code>tftp</code></dt>
           <dd>Immediately within
@@ -635,8 +635,8 @@
                 <code>ip</code> element.  There may be zero or more
                 <code>range</code> elements specified.
                 <span class="since">Since 0.3.0</span>
-                <code>Range</code> can be specified for one IPv4 address,
-                one IPv6 address, or both.   <span class="since">Since 1.0.1</span>
+                <code>range</code> can be specified for one IPv4 address,
+                one IPv6 address, or both. <span class="since">Since 1.0.1</span>
               </dd>
               <dt><code>host</code></dt>
               <dd>Within the <code>dhcp</code> element there may be zero or more
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index f3583ce..eb2aad4 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -633,7 +633,7 @@ cleanup:
 
 static int
 virNetworkDHCPHostDefParse(const char *networkName,
-                           virNetworkIpDefPtr def,
+                           const virNetworkIpDefPtr def,
                            xmlNodePtr node,
                            virNetworkDHCPHostDefPtr host,
                            bool partialOkay)
@@ -648,7 +648,7 @@ virNetworkDHCPHostDefParse(const char *networkName,
         if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET6)) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid to specify MAC address '%s' "
-                             "in IPv6 network '%s'"),
+                             "in network '%s' IPv6 static host definition"),
                            mac, networkName);
             goto cleanup;
         }
@@ -694,6 +694,9 @@ virNetworkDHCPHostDefParse(const char *networkName,
                            networkName);
         }
     } else {
+        /* normal usage - you need at least name (IPv6) or one of MAC
+         * address or name (IPv4)
+         */
         if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET6)) {
             if (!name) {
                 virReportError(VIR_ERR_XML_ERROR,
@@ -702,9 +705,7 @@ virNetworkDHCPHostDefParse(const char *networkName,
                            networkName);
                 goto cleanup;
             }
-        }
-        /* normal usage - you need at least one MAC address or one host name */
-        else if (!(mac || name)) {
+        } else if (!(mac || name)) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Static host definition in IPv4 network '%s' "
                              "must have mac or name attribute"),
@@ -772,32 +773,30 @@ virNetworkDHCPDefParse(const char *networkName,
             }
             def->nhosts++;
 
-        } else if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET)) {
-            /* the following only applies to IPv4 */
-            if (cur->type == XML_ELEMENT_NODE &&
-                xmlStrEqual(cur->name, BAD_CAST "bootp")) {
-                char *file;
-                char *server;
-                virSocketAddr inaddr;
-                memset(&inaddr, 0, sizeof(inaddr));
-
-                if (!(file = virXMLPropString(cur, "file"))) {
-                    cur = cur->next;
-                    continue;
-                }
-                server = virXMLPropString(cur, "server");
-
-                if (server &&
-                    virSocketAddrParse(&inaddr, server, AF_UNSPEC) < 0) {
-                    VIR_FREE(file);
-                    VIR_FREE(server);
-                    return -1;
-                }
+        } else if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET) &&
+                   cur->type == XML_ELEMENT_NODE &&
+                   xmlStrEqual(cur->name, BAD_CAST "bootp")) {
+            char *file;
+            char *server;
+            virSocketAddr inaddr;
+            memset(&inaddr, 0, sizeof(inaddr));
+
+            if (!(file = virXMLPropString(cur, "file"))) {
+                cur = cur->next;
+                continue;
+            }
+            server = virXMLPropString(cur, "server");
 
-                def->bootfile = file;
-                def->bootserver = inaddr;
+            if (server &&
+                virSocketAddrParse(&inaddr, server, AF_UNSPEC) < 0) {
+                VIR_FREE(file);
                 VIR_FREE(server);
+                return -1;
             }
+
+            def->bootfile = file;
+            def->bootserver = inaddr;
+            VIR_FREE(server);
         }
 
         cur = cur->next;
@@ -1133,44 +1132,29 @@ virNetworkIPParseXML(const char *networkName,
         }
     }
 
-    if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET)) {
-        /* parse IPv4-related info */
-        cur = node->children;
-        while (cur != NULL) {
-            if (cur->type == XML_ELEMENT_NODE &&
-                xmlStrEqual(cur->name, BAD_CAST "dhcp")) {
-                result = virNetworkDHCPDefParse(networkName, def, cur);
-                if (result)
-                    goto error;
-
-            } else if (cur->type == XML_ELEMENT_NODE &&
-                       xmlStrEqual(cur->name, BAD_CAST "tftp")) {
-                char *root;
-
-                if (!(root = virXMLPropString(cur, "root"))) {
-                    cur = cur->next;
-                    continue;
-                }
+    cur = node->children;
+    while (cur != NULL) {
+        if (cur->type == XML_ELEMENT_NODE &&
+            xmlStrEqual(cur->name, BAD_CAST "dhcp")) {
+            if (virNetworkDHCPDefParse(networkName, def, cur) < 0)
+                goto error;
+        } else if (cur->type == XML_ELEMENT_NODE &&
+                   xmlStrEqual(cur->name, BAD_CAST "tftp")) {
+            char *root;
 
-                def->tftproot = (char *)root;
+            if (!VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET)) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                               _("Unsupported <tftp> element in an IPv6 element in network '%s'"),
+                               networkName);
+                goto error;
             }
-
-            cur = cur->next;
-        }
-    }
-
-    if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET6)) {
-        /* parse IPv6-related info */
-        cur = node->children;
-        while (cur != NULL) {
-            if (cur->type == XML_ELEMENT_NODE &&
-                xmlStrEqual(cur->name, BAD_CAST "dhcp")) {
-                result = virNetworkDHCPDefParse(networkName, def, cur);
-                if (result)
-                    goto error;
+            if (!(root = virXMLPropString(cur, "root"))) {
+                cur = cur->next;
+                continue;
             }
-            cur = cur->next;
+            def->tftproot = (char *)root;
         }
+        cur = cur->next;
     }
 
     result = 0;
@@ -2409,7 +2393,7 @@ virNetworkIpDefByIndex(virNetworkDefPtr def, int parentIndex)
         ipdef = virNetworkDefGetIpByIndex(def, AF_UNSPEC, parentIndex);
         if (!(ipdef)) {
             virReportError(VIR_ERR_OPERATION_INVALID,
-                           _("couldn't update dhcp host entry - "
+                           _("couldn't update dhcp host entry - no <ip> "
                              "element found at index %d in network '%s'"),
                            parentIndex, def->name);
         }
@@ -2432,7 +2416,7 @@ virNetworkIpDefByIndex(virNetworkDefPtr def, int parentIndex)
     }
     if (!ipdef) {
         virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("couldn't update dhcp host entry - "
+                       _("couldn't update dhcp host entry - no <ip> "
                          "element found in network '%s'"), def->name);
     }
     return ipdef;
-- 
1.7.11.7

