[netcf-devel] [PATCH 2/2] Remove all config-origined info from ncf_if_xml_state output.

Laine Stump laine at laine.org
Wed Oct 7 13:50:53 UTC 2009


Modify ncf_if_xml_state() to return *only* information queried from
the running device/kernel. Because this makes the "source='device'"
attribute redundant, also remove that.

Currently, we only query for the ipv4 address of the interface; if
there is none, don't even add a protocol node to the tree - just
return an "interface" document that has only a name, and nothing else.
---
 src/drv_initscripts.c |   39 +++++++++++++++++++++++----------------
 src/dutil.c           |   43 ++++++++++++++++++++++++-------------------
 2 files changed, 47 insertions(+), 35 deletions(-)

diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 90103e9..98d3280 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -756,27 +756,34 @@ char *drv_xml_state(struct netcf_if *nif) {
     char *result = NULL;
     int r, result_len;
     struct netcf *ncf;
-    xmlDocPtr aug_xml = NULL, ncf_xml = NULL;
+    xmlDocPtr ncf_xml;
+    xmlNodePtr root;
+    xmlAttrPtr prop;
     unsigned int ipv4;
     int prefix;
 
     ncf = nif->ncf;
-    aug_xml = aug_get_xml_for_nif(nif);
-    ERR_BAIL(ncf);
-
-    ncf_xml = apply_stylesheet(ncf, ncf->driver->put, aug_xml);
-    ERR_BAIL(ncf);
 
-    /* get the current IP address. If it's non-zero, also get the
-     * current prefix, and add both to the document */
+    /* start out with an empty tree rather than the config tree. Just
+     * put in the interface node and its name
+     */
+    ncf_xml = xmlNewDoc(BAD_CAST "1.0");
+    ERR_NOMEM(ncf_xml == NULL, ncf);
+    root = xmlNewNode(NULL, BAD_CAST "interface");
+    ERR_NOMEM(root == NULL, ncf);
+    xmlDocSetRootElement(ncf_xml, root);
+    prop = xmlNewProp(root, BAD_CAST "name", BAD_CAST nif->name);
+    ERR_NOMEM(prop == NULL, ncf);
+
+    /* get the current IP address and prefix, and add both to the
+     * document.
+     */
     ipv4 = if_ipv4_address(ncf, nif->name);
     ERR_BAIL(ncf);
-    if (ipv4 != 0) {
-        prefix = if_ipv4_prefix(ncf, nif->name);
-        ERR_BAIL(ncf);
-        add_state_to_xml_doc(ncf_xml, ncf, ipv4, prefix);
-        ERR_BAIL(ncf);
-    }
+    prefix = if_ipv4_prefix(ncf, nif->name);
+    ERR_BAIL(ncf);
+    add_state_to_xml_doc(ncf_xml, ncf, ipv4, prefix);
+    ERR_BAIL(ncf);
 
     r = xsltSaveResultToString((xmlChar **)&result, &result_len,
                                ncf_xml, ncf->driver->put);
@@ -784,10 +791,10 @@ char *drv_xml_state(struct netcf_if *nif) {
 
 done:
     xmlFreeDoc(ncf_xml);
-    xmlFreeDoc(aug_xml);
     return result;
- error:
+error:
     FREE(result);
+    result = 0;
     goto done;
 }
 
diff --git a/src/dutil.c b/src/dutil.c
index 6b9d279..92d0bcc 100644
--- a/src/dutil.c
+++ b/src/dutil.c
@@ -593,7 +593,7 @@ void add_state_to_xml_doc(xmlDocPtr doc, struct netcf *ncf ATTRIBUTE_UNUSED,
         }
     }
 
-    if (proto == NULL) {
+    if ((ipv4 != 0) && (proto == NULL)) {
         proto = xmlNewDocNode(doc, NULL, BAD_CAST "protocol", NULL);
         ERR_NOMEM(proto == NULL, ncf);
 
@@ -604,32 +604,37 @@ void add_state_to_xml_doc(xmlDocPtr doc, struct netcf *ncf ATTRIBUTE_UNUSED,
 
     }
 
-    for (cur = proto->children; cur != NULL; cur = cur->next) {
+    if (proto == NULL)
+        return;
+
+    /* remove all existing ip nodes from the protocol node - we only
+     * want addresses retrieved from the device in the output.
+     */
+    cur = proto->children;
+    while(cur != NULL) {
+        xmlNodePtr next = cur->next;
         if ((cur->type == XML_ELEMENT_NODE) &&
-            xmlStrEqual(cur->name, BAD_CAST "ip")) {
-            break;
+            (xmlStrEqual(cur->name, BAD_CAST "ip"))) {
+            xmlUnlinkNode(cur);
+            xmlFreeNode(cur);
         }
+        cur = next;
     }
 
-    if (ip == NULL) {
+    if (ipv4 != 0) {
         ip = xmlNewDocNode(doc, NULL, BAD_CAST "ip", NULL);
-        ERR_NOMEM(ip == NULL, ncf);
-
+        ERR_NOMEM((ip == NULL), ncf);
         cur = xmlAddChild(proto, ip);
-        ERR_NOMEM(cur == NULL, ncf);
+        ERR_NOMEM((cur == NULL), ncf);
+
+        inet_ntop(AF_INET, (struct in_addr *)&ipv4, ip_str, sizeof(ip_str));
+        prop = xmlSetProp(ip, BAD_CAST "address", BAD_CAST ip_str);
+        ERR_NOMEM((prop == NULL), ncf);
+        snprintf(prefix_str, sizeof(prefix_str), "%d", prefix);
+        prop = xmlSetProp(ip, BAD_CAST "prefix", BAD_CAST prefix_str);
+        ERR_NOMEM((prop == NULL), ncf);
     }
 
-    inet_ntop(AF_INET, (struct in_addr *)&ipv4, ip_str, sizeof(ip_str));
-    prop = xmlSetProp(ip, BAD_CAST "address", BAD_CAST ip_str);
-    ERR_NOMEM(prop == NULL, ncf);
-
-    snprintf(prefix_str, sizeof(prefix_str), "%d", prefix);
-    prop = xmlSetProp(ip, BAD_CAST "prefix", BAD_CAST prefix_str);
-    ERR_NOMEM(prop == NULL, ncf);
-
-    prop = xmlSetProp(ip, BAD_CAST "source", BAD_CAST "device");
-    ERR_NOMEM(prop == NULL, ncf);
-
 error:
     return;
 }
-- 
1.6.2.5



More information about the netcf-devel mailing list