gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r4131 - in GNUnet: . src/transports/upnp


From: grothoff
Subject: [GNUnet-SVN] r4131 - in GNUnet: . src/transports/upnp
Date: Sat, 30 Dec 2006 09:41:27 -0800 (PST)

Author: grothoff
Date: 2006-12-30 09:41:24 -0800 (Sat, 30 Dec 2006)
New Revision: 4131

Removed:
   GNUnet/src/transports/upnp/todo
Modified:
   GNUnet/src/transports/upnp/upnp.c
   GNUnet/src/transports/upnp/xmlnode.c
   GNUnet/src/transports/upnp/xmlnode.h
   GNUnet/todo
Log:
leaks fixed

Deleted: GNUnet/src/transports/upnp/todo
===================================================================
--- GNUnet/src/transports/upnp/todo     2006-12-30 15:29:50 UTC (rev 4130)
+++ GNUnet/src/transports/upnp/todo     2006-12-30 17:41:24 UTC (rev 4131)
@@ -1,5 +0,0 @@
-1) adjust configure to check for libxml2
-2) remove glib dependency from upnp code!
-3) fix xmlnode memory leaks (for invalid / incomplete XML responses)
-4) integrate with transports -- tricky bit: making it optional!
-   (make it a module?)

Modified: GNUnet/src/transports/upnp/upnp.c
===================================================================
--- GNUnet/src/transports/upnp/upnp.c   2006-12-30 15:29:50 UTC (rev 4130)
+++ GNUnet/src/transports/upnp/upnp.c   2006-12-30 17:41:24 UTC (rev 4131)
@@ -32,33 +32,11 @@
 
 #include <curl/curl.h>
 
-#define gchar char
-#define gboolean int
 #define TRUE YES
 #define FALSE NO
-#define gpointer void *
-#define g_free FREE
-#define gsize size_t
-#define g_strdup STRDUP
-#define g_strndup STRNDUP
-#define g_ascii_strcasecmp strcasecmp
-#define G_GSIZE_FORMAT "u"
 #define g_return_if_fail(a) if(!(a)) return;
 #define g_return_val_if_fail(a, val) if(!(a)) return (val);
 
-/**
- * The xmlnode code has a bunch of memory leaks which
- * occur with malformed XML input (i.e. XML input is
- * incomplete).  Without this extra check, the code
- * would frequently try to parse incomplete XML --
- * with it, only if the response from the NAT box is
- * odd or incorrect.  These leaks should be fixed
- * eventually (best way I can think of is to make
- * a memory pool for the xmlnodes and blow it away
- * completely at the end).
- */
-#define TEST_FOR_LEAKS NO
-
 #define HTTP_OK "200 OK"
 #define NUM_UDP_ATTEMPTS 2
 #define HTTPMU_HOST_ADDRESS "239.255.255.250"
@@ -75,8 +53,7 @@
 #define WAN_PPP_CONN_SERVICE "WANPPPConnection:1"
 #define HTTP_POST_SOAP_HEADER \
         "SOAPACTION: \"urn:schemas-upnp-org:service:%s#%s\""
-#define HTTP_POST_SIZE_HEADER \
-        "CONTENT-LENGTH: %" G_GSIZE_FORMAT ""
+#define HTTP_POST_SIZE_HEADER "CONTENT-LENGTH: %u"
 #define SOAP_ACTION \
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
        "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"; " \
@@ -116,14 +93,14 @@
 
 typedef struct {
   GaimUPnPStatus status;
-  gchar* control_url;
-  const gchar * service_type;
+  char* control_url;
+  const char * service_type;
   char publicip[16];
 } GaimUPnPControlInfo;
 
 typedef struct {
-  const gchar * service_type;
-  gchar * full_url;
+  const char * service_type;
+  char * full_url;
   char * buf;
   unsigned int buf_len;
   int sock;
@@ -143,13 +120,13 @@
 typedef size_t (*GaimUtilFetchUrlCallback)(void *url_data,
                                           size_t size,
                                           size_t nmemb,
-                                          gpointer user_data);
+                                          void * user_data);
 
 
 
-static char * g_strstr_len (const gchar *haystack,
+static char * g_strstr_len (const char *haystack,
                            int haystack_len,
-                           const gchar *needle) {
+                           const char *needle) {
   int i;
 
   g_return_val_if_fail (haystack != NULL, NULL);
@@ -182,27 +159,27 @@
   return NULL;
 }
 
-static gboolean
+static int
 gaim_upnp_compare_device(const xmlnode* device, 
-                        const gchar* deviceType) {
+                        const char* deviceType) {
   xmlnode * deviceTypeNode = xmlnode_get_child(device, "deviceType");
   char * tmp;
-  gboolean ret;
+  int ret;
   
   if (deviceTypeNode == NULL) 
     return FALSE;      
   tmp = xmlnode_get_data(deviceTypeNode);
-  ret = !g_ascii_strcasecmp(tmp, deviceType);
-  g_free(tmp);  
+  ret = !strcasecmp(tmp, deviceType);
+  FREE(tmp);  
   return ret;
 }
 
-static gboolean
+static int
 gaim_upnp_compare_service(const xmlnode* service, 
-                         const gchar* serviceType) {
+                         const char* serviceType) {
   xmlnode * serviceTypeNode;
   char *tmp;
-  gboolean ret;
+  int ret;
   
   if (service == NULL) 
     return FALSE;      
@@ -210,17 +187,17 @@
   if(serviceTypeNode == NULL) 
     return FALSE;
   tmp = xmlnode_get_data(serviceTypeNode);
-  ret = !g_ascii_strcasecmp(tmp, serviceType);
-  g_free(tmp); 
+  ret = !strcasecmp(tmp, serviceType);
+  FREE(tmp); 
   return ret;
 }
 
-static gchar*
-gaim_upnp_parse_description_response(const gchar* httpResponse, 
-                                    gsize len,
-                                    const gchar* httpURL,
-                                    const gchar* serviceType) {
-  gchar *xmlRoot, *baseURL, *controlURL, *service;
+static char*
+gaim_upnp_parse_description_response(const char* httpResponse, 
+                                    size_t len,
+                                    const char* httpURL,
+                                    const char* serviceType) {
+  char *xmlRoot, *baseURL, *controlURL, *service;
   xmlnode *xmlRootNode, *serviceTypeNode, *controlURLNode, *baseURLNode;
   char *tmp;
   
@@ -228,10 +205,8 @@
   xmlRoot = g_strstr_len(httpResponse, len, "<root");
   if (xmlRoot == NULL)
     return NULL;  
-#if TEST_FOR_LEAKS
   if (g_strstr_len(httpResponse, len, "</root") == NULL) 
     return NULL;  
-#endif
 
   /* create the xml root node */
   xmlRootNode = xmlnode_from_str(xmlRoot,
@@ -244,7 +219,7 @@
   if (baseURLNode != NULL) {
     baseURL = xmlnode_get_data(baseURLNode);
   } else {
-    baseURL = g_strdup(httpURL);
+    baseURL = STRDUP(httpURL);
   }
  
   /* get the serviceType child that has the service type as its data */  
@@ -256,13 +231,13 @@
     serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
   }
   if(serviceTypeNode == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
   serviceTypeNode = xmlnode_get_child(serviceTypeNode, "deviceList");
   if(serviceTypeNode == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
@@ -275,13 +250,13 @@
     serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
   }
   if(serviceTypeNode == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
   serviceTypeNode = xmlnode_get_child(serviceTypeNode, "deviceList");
   if(serviceTypeNode == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
@@ -293,13 +268,13 @@
     serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
   }
   if(serviceTypeNode == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
   serviceTypeNode = xmlnode_get_child(serviceTypeNode, "serviceList");
   if(serviceTypeNode == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
@@ -312,9 +287,9 @@
     serviceTypeNode = xmlnode_get_next_twin(serviceTypeNode);
   }
   
-  g_free(service);
+  FREE(service);
   if (serviceTypeNode == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
@@ -322,7 +297,7 @@
   /* get the controlURL of the service */
   if((controlURLNode = xmlnode_get_child(serviceTypeNode,
                                         "controlURL")) == NULL) {
-    g_free(baseURL);
+    FREE(baseURL);
     xmlnode_free(xmlRootNode);
     return NULL;
   }
@@ -347,11 +322,11 @@
     } else {
       controlURL = g_strdup_printf("%s%s", baseURL, tmp);
     }
-    g_free(tmp);
+    FREE(tmp);
   } else{
     controlURL = tmp;
   }
-  g_free(baseURL);
+  FREE(baseURL);
   xmlnode_free(xmlRootNode);
   
   return controlURL;
@@ -390,15 +365,15 @@
 
 static int
 gaim_upnp_generate_action_message_and_send(const char * proxy,
-                                          const gchar* actionName,
-                                          const gchar* actionParams, 
+                                          const char* actionName,
+                                          const char* actionParams, 
                                           GaimUtilFetchUrlCallback cb,
-                                          gpointer cb_data) {
+                                          void * cb_data) {
   CURL * curl; 
   int ret;
-  gchar * soapHeader;
-  gchar * sizeHeader;
-  gchar * soapMessage;
+  char * soapHeader;
+  char * sizeHeader;
+  char * soapMessage;
   struct curl_slist * headers = NULL;
 
   GE_ASSERT(NULL, cb != NULL);
@@ -461,9 +436,9 @@
   curl_slist_free_all(headers);
   curl_easy_cleanup(curl);
   curl_global_cleanup();
-  g_free(sizeHeader);
-  g_free(soapMessage);
-  g_free(soapHeader);
+  FREE(sizeHeader);
+  FREE(soapMessage);
+  FREE(soapHeader);
   if (ret != CURLE_OK)
     return SYSERR;
   return OK;
@@ -474,11 +449,11 @@
 looked_up_public_ip_cb(void *url_data, 
                       size_t size,
                       size_t nmemb,                  
-                      gpointer user_data) {
+                      void * user_data) {
   UPnPDiscoveryData * dd = user_data;
   size_t len = size * nmemb;
-  const gchar * temp;
-  const gchar * temp2;
+  const char * temp;
+  const char * temp2;
 
   if (len + dd->buf_len > 1024 * 1024 * 4)
     return len; /* refuse to process - too big! */
@@ -521,7 +496,7 @@
 ignore_response(void *url_data, 
                size_t size,
                size_t nmemb,                 
-               gpointer user_data) {
+               void * user_data) {
   return size * nmemb;
 }
 
@@ -534,8 +509,8 @@
                          size_t nmemb,
                          void * user_data) {
   UPnPDiscoveryData * dd = user_data;
-  gsize len = size * nmemb;
-  gchar * control_url = NULL;
+  size_t len = size * nmemb;
+  char * control_url = NULL;
   
   if (len + dd->buf_len > 1024 * 1024 * 4)
     return len; /* refuse to process - too big! */
@@ -602,13 +577,13 @@
   struct hostent* hp;
   struct sockaddr_in server;
   int retry_count;
-  gchar * sendMessage;
-  gsize totalSize;
-  gboolean sentSuccess;
-  gchar buf[65536];
+  char * sendMessage;
+  size_t totalSize;
+  int sentSuccess;
+  char buf[65536];
   int buf_len;
-  const gchar * startDescURL;
-  const gchar * endDescURL;
+  const char * startDescURL;
+  const char * endDescURL;
   int ret;
   UPnPDiscoveryData dd;
 
@@ -657,7 +632,7 @@
       }
     } while ( ((errno == EINTR) || (errno == EAGAIN)) &&
              (GNUNET_SHUTDOWN_TEST() == NO));
-    g_free(sendMessage);    
+    FREE(sendMessage);    
     if (sentSuccess) 
       break;
   }
@@ -695,7 +670,7 @@
     return SYSERR;  
   if (endDescURL == startDescURL) 
     return SYSERR;    
-  dd.full_url = g_strndup(startDescURL,
+  dd.full_url = STRNDUP(startDescURL,
                          endDescURL - startDescURL); 
   proxy = NULL; 
   GC_get_configuration_value_string(cfg,
@@ -705,7 +680,7 @@
                                    &proxy);
   ret = gaim_upnp_parse_description(proxy,
                                    &dd);  
-  g_free(dd.full_url);
+  FREE(dd.full_url);
   GROW(dd.buf,
        dd.buf_len,
        0);
@@ -723,7 +698,7 @@
   return ret;
 }
 
-const gchar *
+const char *
 gaim_upnp_get_public_ip() {
   if (control_info.status == GAIM_UPNP_STATUS_DISCOVERED
       && control_info.publicip
@@ -737,9 +712,9 @@
                              struct GC_Configuration * cfg,
                              int do_add,
                              unsigned short portmap, 
-                             const gchar* protocol) {
-  const gchar * action_name;
-  gchar * action_params;
+                             const char* protocol) {
+  const char * action_name;
+  char * action_params;
   char * internal_ip;
   char * proxy;
   int ret;
@@ -779,7 +754,7 @@
                                                   &ignore_response,
                                                   NULL);
   
-  g_free(action_params);
+  FREE(action_params);
   FREE(proxy);
   return ret; 
 }

Modified: GNUnet/src/transports/upnp/xmlnode.c
===================================================================
--- GNUnet/src/transports/upnp/xmlnode.c        2006-12-30 15:29:50 UTC (rev 
4130)
+++ GNUnet/src/transports/upnp/xmlnode.c        2006-12-30 17:41:24 UTC (rev 
4131)
@@ -29,10 +29,14 @@
 
 #include "platform.h"
 
-#include "xmlnode.h"
 #include "util.h"
 #include "gnunet_util.h"
+#include "xmlnode.h"
 
+#include <libxml/parser.h>
+#include <string.h>
+
+
 #ifdef _WIN32
 # define NEWLINE_S "\r\n"
 #else
@@ -42,16 +46,40 @@
 #define TRUE YES
 #define FALSE NO
 
-#define g_strdup STRDUP
-#define g_malloc MALLOC
-#define g_free FREE
 #define g_return_if_fail(a) if(!(a)) return;
 #define g_return_val_if_fail(a, val) if(!(a)) return (val);
-#define gsize size_t
-#define gboolean int
-#define GString char 
-#define g_string_new(a) STRDUP(a)
 
+/**
+ * The valid types for an xmlnode
+ */
+typedef enum _XMLNodeType {
+  XMLNODE_TYPE_TAG,            /**< Just a tag */
+  XMLNODE_TYPE_ATTRIB,         /**< Has attributes */
+  XMLNODE_TYPE_DATA            /**< Has data */
+} XMLNodeType;
+
+typedef struct {
+  xmlnode * current;
+  xmlnode ** nodes;
+  unsigned int pos;
+  unsigned int size;
+} XMLNodePool;
+
+struct _xmlnode {
+  char *name;                  /**< The name of the node. */
+  char *xmlns;         /**< The namespace of the node */
+  XMLNodeType type;            /**< The type of the node. */
+  char *data;                  /**< The data for the node. */
+  size_t data_sz;                      /**< The size of the data. */
+  struct _xmlnode *parent;     /**< The parent node or @c NULL.*/
+  struct _xmlnode *child;              /**< The child node or @c NULL.*/
+  struct _xmlnode *lastchild;  /**< The last child node or @c NULL.*/
+  struct _xmlnode *next;               /**< The next node or @c NULL. */
+  XMLNodePool * pool;
+  int free_pool; /* set to YES for the root node, which must free the pool */
+};
+
+
 static void * g_memdup(const void * data,
                       size_t s) {
   void * ret;
@@ -75,291 +103,289 @@
 }
 
 static xmlnode*
-new_node(const char *name, XMLNodeType type) {
+new_node(const char *name, 
+        XMLNodeType type,
+        void * user_data) {
   xmlnode *node = MALLOC(sizeof(xmlnode));
   
-  node->name = g_strdup(name);
+  node->name = name == NULL ? NULL : STRDUP(name);
   node->type = type;
+  node->pool = user_data;
+  if (node->pool->size == node->pool->pos)
+    GROW(node->pool->nodes,
+        node->pool->size,
+        node->pool->size * 2 + 64);
+  node->pool->nodes[node->pool->pos++] = node;
   return node;
 }
 
-xmlnode*
-xmlnode_new(const char *name)
-{
-       g_return_val_if_fail(name != NULL, NULL);
+static xmlnode*
+xmlnode_new(const char *name,
+           void * user_data) {
+  g_return_val_if_fail(name != NULL, NULL);  
+  return new_node(name, 
+                 XMLNODE_TYPE_TAG, 
+                 user_data);
+}
 
-       return new_node(name, XMLNODE_TYPE_TAG);
+static void
+xmlnode_insert_child(xmlnode *parent, 
+                    xmlnode *child) {
+  g_return_if_fail(parent != NULL);
+  g_return_if_fail(child != NULL);
+  
+  child->parent = parent;  
+  if(parent->lastchild) 
+    parent->lastchild->next = child;
+  else 
+    parent->child = child;
+  parent->lastchild = child;
 }
 
-xmlnode *
-xmlnode_new_child(xmlnode *parent, const char *name)
-{
-       xmlnode *node;
-
-       g_return_val_if_fail(parent != NULL, NULL);
-       g_return_val_if_fail(name != NULL, NULL);
-
-       node = new_node(name, XMLNODE_TYPE_TAG);
-
-       xmlnode_insert_child(parent, node);
-
-       return node;
+static xmlnode *
+xmlnode_new_child(xmlnode *parent,
+                 const char *name,
+                 void * user_data) {
+  xmlnode *node;
+  
+  g_return_val_if_fail(parent != NULL, NULL);
+  g_return_val_if_fail(name != NULL, NULL);  
+  node = new_node(name, XMLNODE_TYPE_TAG, user_data);  
+  xmlnode_insert_child(parent, node);  
+  return node;
 }
 
-void
-xmlnode_insert_child(xmlnode *parent, xmlnode *child)
-{
-       g_return_if_fail(parent != NULL);
-       g_return_if_fail(child != NULL);
-
-       child->parent = parent;
-
-       if(parent->lastchild) {
-               parent->lastchild->next = child;
-       } else {
-               parent->child = child;
-       }
-
-       parent->lastchild = child;
+static void
+xmlnode_insert_data(xmlnode *node, 
+                   const char *data, 
+                   int size, 
+                   void * user_data) {
+  xmlnode *child;
+  size_t real_size;
+  
+  g_return_if_fail(node != NULL);
+  g_return_if_fail(data != NULL);
+  g_return_if_fail(size != 0);  
+  real_size = size == -1 ? strlen(data) : size;  
+  child = new_node(NULL, XMLNODE_TYPE_DATA, user_data);  
+  child->data = g_memdup(data, real_size);
+  child->data_sz = real_size;  
+  xmlnode_insert_child(node, child);
 }
 
-void
-xmlnode_insert_data(xmlnode *node, const char *data, int size)
-{
-       xmlnode *child;
-       gsize real_size;
-
-       g_return_if_fail(node != NULL);
-       g_return_if_fail(data != NULL);
-       g_return_if_fail(size != 0);
-
-       real_size = size == -1 ? strlen(data) : size;
-
-       child = new_node(NULL, XMLNODE_TYPE_DATA);
-
-       child->data = g_memdup(data, real_size);
-       child->data_sz = real_size;
-
-       xmlnode_insert_child(node, child);
+static void
+xmlnode_remove_attrib(xmlnode *node, 
+                     const char *attr) {
+  xmlnode *attr_node, *sibling = NULL;
+  
+  g_return_if_fail(node != NULL);
+  g_return_if_fail(attr != NULL);
+  
+  for(attr_node = node->child; attr_node; attr_node = attr_node->next) {
+    if(attr_node->type == XMLNODE_TYPE_ATTRIB &&
+       !strcmp(attr_node->name, attr)) {
+      if(node->child == attr_node) {
+       node->child = attr_node->next;
+      } else {
+       sibling->next = attr_node->next;
+      }
+      if (node->lastchild == attr_node) {
+       node->lastchild = sibling;
+      }
+      xmlnode_free(attr_node);
+      return;
+    }
+    sibling = attr_node;
+  }
 }
 
 static void
-xmlnode_remove_attrib(xmlnode *node, const char *attr)
-{
-       xmlnode *attr_node, *sibling = NULL;
+xmlnode_set_attrib(xmlnode *node, 
+                  const char *attr, 
+                  const char *value, 
+                  void * user_data) {
+  xmlnode *attrib_node;
 
-       g_return_if_fail(node != NULL);
-       g_return_if_fail(attr != NULL);
-
-       for(attr_node = node->child; attr_node; attr_node = attr_node->next)
-       {
-               if(attr_node->type == XMLNODE_TYPE_ATTRIB &&
-                               !strcmp(attr_node->name, attr))
-               {
-                       if(node->child == attr_node) {
-                               node->child = attr_node->next;
-                       } else {
-                               sibling->next = attr_node->next;
-                       }
-                       if (node->lastchild == attr_node) {
-                               node->lastchild = sibling;
-                       }
-                       xmlnode_free(attr_node);
-                       return;
-               }
-               sibling = attr_node;
-       }
+  g_return_if_fail(node != NULL);
+  g_return_if_fail(attr != NULL);
+  g_return_if_fail(value != NULL);
+  xmlnode_remove_attrib(node, attr);  
+  attrib_node = new_node(attr, XMLNODE_TYPE_ATTRIB, user_data);  
+  attrib_node->data = STRDUP(value);  
+  xmlnode_insert_child(node, attrib_node);
 }
 
-
-
-void
-xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value)
-{
-       xmlnode *attrib_node;
-
-       g_return_if_fail(node != NULL);
-       g_return_if_fail(attr != NULL);
-       g_return_if_fail(value != NULL);
-
-       xmlnode_remove_attrib(node, attr);
-
-       attrib_node = new_node(attr, XMLNODE_TYPE_ATTRIB);
-
-       attrib_node->data = g_strdup(value);
-
-       xmlnode_insert_child(node, attrib_node);
+static void xmlnode_set_namespace(xmlnode *node, 
+                                 const char *xmlns) {
+  g_return_if_fail(node != NULL);  
+  FREENONNULL(node->xmlns);
+  node->xmlns = STRDUP(xmlns);
 }
 
-static void xmlnode_set_namespace(xmlnode *node, const char *xmlns)
-{
-       g_return_if_fail(node != NULL);
-
-       g_free(node->xmlns);
-       node->xmlns = g_strdup(xmlns);
+static const char *xmlnode_get_namespace(xmlnode *node) {
+  g_return_val_if_fail(node != NULL, NULL);  
+  return node->xmlns;
 }
 
-static const char *xmlnode_get_namespace(xmlnode *node)
-{
-       g_return_val_if_fail(node != NULL, NULL);
+static void
+freePool(XMLNodePool * pool) {
+  unsigned int i;
+  xmlnode * x;
 
-       return node->xmlns;
+  for (i=0;i<pool->pos;i++) {
+    x = pool->nodes[i];    
+    FREENONNULL(x->name);
+    FREENONNULL(x->data);
+    FREENONNULL(x->xmlns);
+    FREE(x);
+  }
+  GROW(pool->nodes,
+       pool->size,
+       0);
+  FREE(pool);
 }
 
 void
-xmlnode_free(xmlnode *node)
-{
-       xmlnode *x, *y;
+xmlnode_free(xmlnode *node) {
+  g_return_if_fail(node != NULL);
+  if (node->free_pool != YES) 
+    return;
+  freePool(node->pool);
+}
 
-       g_return_if_fail(node != NULL);
-
-       x = node->child;
-       while(x) {
-               y = x->next;
-               xmlnode_free(x);
-               x = y;
-       }
-
-       g_free(node->name);
-       g_free(node->data);
-       g_free(node->xmlns);
-       g_free(node);
+static xmlnode *
+xmlnode_get_child_with_namespace(const xmlnode *parent, 
+                                const char *name, 
+                                const char *ns) {
+  xmlnode *x;
+  xmlnode *ret = NULL;
+  char *parent_name;
+  char *child_name;
+  
+  if (parent == NULL)
+    return NULL;
+  if (name == NULL)
+    return NULL;
+  
+  parent_name = STRDUP(name);
+  child_name = strstr(parent_name, "/");
+  if (child_name != NULL) {
+    child_name[0] = '\0';
+    child_name++;
+  }      
+  
+  for(x = parent->child; x; x = x->next) {
+    const char *xmlns = NULL;
+    if(ns)
+      xmlns = xmlnode_get_namespace(x);
+    
+    if(x->type == XMLNODE_TYPE_TAG && name && !strcmp(parent_name, x->name)
+       && (!ns || (xmlns && !strcmp(ns, xmlns)))) {
+      ret = x;
+      break;
+    }
+  }
+  
+  if (child_name && ret)
+    ret = xmlnode_get_child(ret, child_name);
+  
+  FREE(parent_name);
+  return ret;
 }
 
 xmlnode*
-xmlnode_get_child(const xmlnode *parent, const char *name)
-{
-       return xmlnode_get_child_with_namespace(parent, name, NULL);
+xmlnode_get_child(const xmlnode *parent, 
+                 const char *name) {
+  return xmlnode_get_child_with_namespace(parent,
+                                         name, 
+                                         NULL);
 }
 
-xmlnode *
-xmlnode_get_child_with_namespace(const xmlnode *parent, const char *name, 
const char *ns)
-{
-       xmlnode *x, *ret = NULL;
-       char *parent_name, *child_name;
-
-       if (parent == NULL)
-         return NULL;
-       if (name == NULL)
-         return NULL;
-
-       parent_name = STRDUP(name);
-       child_name = strstr(parent_name, "/");
-       if (child_name != NULL) {
-         child_name[0] = '\0';
-         child_name++;
-       }         
-
-       for(x = parent->child; x; x = x->next) {
-               const char *xmlns = NULL;
-               if(ns)
-                       xmlns = xmlnode_get_namespace(x);
-
-               if(x->type == XMLNODE_TYPE_TAG && name && !strcmp(parent_name, 
x->name)
-                               && (!ns || (xmlns && !strcmp(ns, xmlns)))) {
-                       ret = x;
-                       break;
-               }
-       }
-
-       if (child_name && ret)
-               ret = xmlnode_get_child(ret, child_name);
-
-       FREE(parent_name);
-       return ret;
-}
-
 char *
-xmlnode_get_data(xmlnode *node)
-{
-       GString *str = NULL;
-       xmlnode *c;
-       
-       if (node == NULL)
-         return NULL;
-
-       for(c = node->child; c; c = c->next) {
-               if(c->type == XMLNODE_TYPE_DATA) {
-                       if(!str)
-                               str = g_string_new("");
-                       str = g_string_append_len(str, c->data, c->data_sz);
-               }
-       }
-
-       if (str == NULL)
-               return NULL;
-
-       return str;
+xmlnode_get_data(xmlnode *node) {
+  char *str = NULL;
+  xmlnode *c;
+  
+  if (node == NULL)
+    return NULL;  
+  for(c = node->child; c; c = c->next) {
+    if(c->type == XMLNODE_TYPE_DATA) {
+      if(!str)
+       str = STRDUP("");
+      str = g_string_append_len(str, c->data, c->data_sz);
+    }
+  }  
+  if (str == NULL)
+    return NULL;
+  
+  return str;
 }
 
-struct _xmlnode_parser_data {
-       xmlnode *current;
-};
-
 static void
 xmlnode_parser_element_start_libxml(void *user_data,
-                                  const xmlChar *element_name, const xmlChar 
*prefix, const xmlChar *xmlns,
-                                  int nb_namespaces, const xmlChar 
**namespaces,
-                                  int nb_attributes, int nb_defaulted, const 
xmlChar **attributes)
-{
-       struct _xmlnode_parser_data *xpd = user_data;
-       xmlnode *node;
-       int i;
-
-       if(!element_name) {
-               return;
-       } else {
-         if(xpd->current)
-                       node = xmlnode_new_child(xpd->current, (const char*) 
element_name);
-               else
-                       node = xmlnode_new((const char *) element_name);
-
-               xmlnode_set_namespace(node, (const char *) xmlns);
-
-               for(i=0; i < nb_attributes * 5; i+=5) {
-                       char *txt;
-                       int attrib_len = attributes[i+4] - attributes[i+3];
-                       char *attrib = g_malloc(attrib_len + 1);
-                       memcpy(attrib, attributes[i+3], attrib_len);
-                       attrib[attrib_len] = '\0';
-                       txt = attrib;
-                       attrib = gaim_unescape_html(txt);
-                       g_free(txt);
-                       xmlnode_set_attrib(node, (const char*) attributes[i], 
attrib);
-                       g_free(attrib);
-               }
-
-               xpd->current = node;
-       }
+                                   const xmlChar *element_name, 
+                                   const xmlChar *prefix, 
+                                   const xmlChar *xmlns,
+                                   int nb_namespaces,
+                                   const xmlChar **namespaces,
+                                   int nb_attributes, 
+                                   int nb_defaulted, 
+                                   const xmlChar **attributes) {
+  XMLNodePool *xpd = user_data;
+  xmlnode *node;
+  int i;
+  
+  if (!element_name) 
+    return;
+  if (xpd->current)
+    node = xmlnode_new_child(xpd->current, (const char*) element_name, 
user_data);
+  else
+    node = xmlnode_new((const char *) element_name, user_data);
+  
+  xmlnode_set_namespace(node, (const char *) xmlns);
+  
+  for(i=0; i < nb_attributes * 5; i+=5) {
+    char *txt;
+    int attrib_len = attributes[i+4] - attributes[i+3];
+    char *attrib = MALLOC(attrib_len + 1);
+    memcpy(attrib, attributes[i+3], attrib_len);
+    attrib[attrib_len] = '\0';
+    txt = attrib;
+    attrib = gaim_unescape_html(txt);
+    FREE(txt);
+    xmlnode_set_attrib(node, (const char*) attributes[i], attrib, user_data);
+    FREE(attrib);
+  }  
+  xpd->current = node;
 }
 
 static void
-xmlnode_parser_element_end_libxml(void *user_data, const xmlChar *element_name,
-                                const xmlChar *prefix, const xmlChar *xmlns)
-{
-       struct _xmlnode_parser_data *xpd = user_data;
-
-       if(!element_name || !xpd->current)
-               return;
-
-       if(xpd->current->parent) {
-               if(!xmlStrcmp((xmlChar*) xpd->current->name, element_name))
-                       xpd->current = xpd->current->parent;
-       }
+xmlnode_parser_element_end_libxml(void *user_data, 
+                                 const xmlChar *element_name,
+                                 const xmlChar *prefix,
+                                 const xmlChar *xmlns) {
+  XMLNodePool *xpd = user_data;
+  
+  if(!element_name || !xpd->current)
+    return;  
+  if(xpd->current->parent) {
+    if(!xmlStrcmp((xmlChar*) xpd->current->name, element_name))
+      xpd->current = xpd->current->parent;
+  }
 }
 
 static void
-xmlnode_parser_element_text_libxml(void *user_data, const xmlChar *text, int 
text_len)
-{
-       struct _xmlnode_parser_data *xpd = user_data;
-
-       if(!xpd->current)
-               return;
-
-       if(!text || !text_len)
-               return;
-
-       xmlnode_insert_data(xpd->current, (const char*) text, text_len);
+xmlnode_parser_element_text_libxml(void *user_data, 
+                                  const xmlChar *text, 
+                                  int text_len) {
+  XMLNodePool *xpd = user_data;
+  
+  if (!xpd->current || !text || !text_len)
+    return;  
+  xmlnode_insert_data(xpd->current, 
+                     (const char*) text,
+                     text_len, 
+                     user_data);
 }
 
 static xmlSAXHandler xmlnode_parser_libxml = {
@@ -398,47 +424,47 @@
 };
 
 xmlnode *
-xmlnode_from_str(const char *str, int size)
-{
-       struct _xmlnode_parser_data *xpd;
-       xmlnode *ret;
-       gsize real_size;
-
-       g_return_val_if_fail(str != NULL, NULL);
-
-       real_size = size < 0 ? strlen(str) : size;
-       xpd = MALLOC(sizeof(struct _xmlnode_parser_data));
-
-       if (xmlSAXUserParseMemory(&xmlnode_parser_libxml, xpd, str, real_size) 
< 0) {
-               while(xpd->current && xpd->current->parent)
-                       xpd->current = xpd->current->parent;
-               if(xpd->current)
-                       xmlnode_free(xpd->current);
-               xpd->current = NULL;
-       }
-       ret = xpd->current;
-       g_free(xpd);
-       return ret;
+xmlnode_from_str(const char *str,
+                int size) {
+  XMLNodePool *xpd;
+  xmlnode *ret;
+  size_t real_size;
+  
+  g_return_val_if_fail(str != NULL, NULL);
+  
+  real_size = size < 0 ? strlen(str) : size;
+  xpd = MALLOC(sizeof(XMLNodePool));  
+  memset(xpd, 
+        0,
+        sizeof(XMLNodePool));
+  if (xmlSAXUserParseMemory(&xmlnode_parser_libxml, 
+                           xpd, 
+                           str, 
+                           real_size) < 0) {
+    freePool(xpd);
+    return NULL;
+  }
+  ret = xpd->current;
+  ret->free_pool = YES;
+  return ret;
 }
 
 xmlnode *
-xmlnode_get_next_twin(xmlnode *node)
-{
-       xmlnode *sibling;
-       const char *ns = xmlnode_get_namespace(node);
-
-       g_return_val_if_fail(node != NULL, NULL);
-       g_return_val_if_fail(node->type == XMLNODE_TYPE_TAG, NULL);
-
-       for(sibling = node->next; sibling; sibling = sibling->next) {
-               const char *xmlns = NULL;
-               if(ns)
-                       xmlns = xmlnode_get_namespace(sibling);
-
-               if(sibling->type == XMLNODE_TYPE_TAG && !strcmp(node->name, 
sibling->name) &&
-                               (!ns || (xmlns && !strcmp(ns, xmlns))))
-                       return sibling;
-       }
-
-       return NULL;
+xmlnode_get_next_twin(xmlnode *node) {
+  xmlnode *sibling;
+  const char *ns = xmlnode_get_namespace(node);
+  
+  g_return_val_if_fail(node != NULL, NULL);
+  g_return_val_if_fail(node->type == XMLNODE_TYPE_TAG, NULL);
+  
+  for(sibling = node->next; sibling; sibling = sibling->next) {
+    const char *xmlns = NULL;
+    if(ns)
+      xmlns = xmlnode_get_namespace(sibling);
+    
+    if(sibling->type == XMLNODE_TYPE_TAG && !strcmp(node->name, sibling->name) 
&&
+       (!ns || (xmlns && !strcmp(ns, xmlns))))
+      return sibling;
+  }  
+  return NULL;
 }

Modified: GNUnet/src/transports/upnp/xmlnode.h
===================================================================
--- GNUnet/src/transports/upnp/xmlnode.h        2006-12-30 15:29:50 UTC (rev 
4130)
+++ GNUnet/src/transports/upnp/xmlnode.h        2006-12-30 17:41:24 UTC (rev 
4131)
@@ -22,70 +22,20 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-#ifndef _GAIM_XMLNODE_H_
-#define _GAIM_XMLNODE_H_
+#ifndef _GGAIM_XMLNODE_H_
+#define _GGAIM_XMLNODE_H_
 
-#include <libxml/parser.h>
-#include <string.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 /**
- * The valid types for an xmlnode
- */
-typedef enum _XMLNodeType {
-  XMLNODE_TYPE_TAG,            /**< Just a tag */
-  XMLNODE_TYPE_ATTRIB,         /**< Has attributes */
-  XMLNODE_TYPE_DATA            /**< Has data */
-} XMLNodeType;
-
-/**
  * An xmlnode.
  */
 typedef struct _xmlnode xmlnode;
-struct _xmlnode
-{
-  char *name;                  /**< The name of the node. */
-  char *xmlns;         /**< The namespace of the node */
-  XMLNodeType type;            /**< The type of the node. */
-  char *data;                  /**< The data for the node. */
-  size_t data_sz;                      /**< The size of the data. */
-  struct _xmlnode *parent;     /**< The parent node or @c NULL.*/
-  struct _xmlnode *child;              /**< The child node or @c NULL.*/
-  struct _xmlnode *lastchild;  /**< The last child node or @c NULL.*/
-  struct _xmlnode *next;               /**< The next node or @c NULL. */
-};
 
 /**
- * Creates a new xmlnode.
- *
- * @param name The name of the node.
- *
- * @return The new node.
- */
-xmlnode *xmlnode_new(const char *name);
-
-/**
- * Creates a new xmlnode child.
- *
- * @param parent The parent node.
- * @param name   The name of the child node.
- *
- * @return The new child node.
- */
-xmlnode *xmlnode_new_child(xmlnode *parent, const char *name);
-
-/**
- * Inserts a node into a node as a child.
- *
- * @param parent The parent node to insert child into.
- * @param child  The child node to insert into parent.
- */
-void xmlnode_insert_child(xmlnode *parent, xmlnode *child);
-
-/**
  * Gets a child node named name.
  *
  * @param parent The parent node.
@@ -93,20 +43,10 @@
  *
  * @return The child or NULL.
  */
-xmlnode *xmlnode_get_child(const xmlnode *parent, const char *name);
+xmlnode * xmlnode_get_child(const xmlnode *parent, 
+                           const char *name);
 
 /**
- * Gets a child node named name in a namespace.
- *
- * @param parent The parent node.
- * @param name   The child's name.
- * @param xmlns  The namespace.
- *
- * @return The child or NULL.
- */
-xmlnode *xmlnode_get_child_with_namespace(const xmlnode *parent, const char 
*name, const char *xmlns);
-
-/**
  * Gets the next node with the same name as node.
  *
  * @param node The node of a twin to find.
@@ -116,16 +56,6 @@
 xmlnode *xmlnode_get_next_twin(xmlnode *node);
 
 /**
- * Inserts data into a node.
- *
- * @param node   The node to insert data into.
- * @param data   The data to insert.
- * @param size   The size of the data to insert.  If data is
- *               null-terminated you can pass in -1.
- */
-void xmlnode_insert_data(xmlnode *node, const char *data, int size);
-
-/**
  * Gets data from a node.
  *
  * @param node The node to get data from.
@@ -136,38 +66,6 @@
 char *xmlnode_get_data(xmlnode *node);
 
 /**
- * Sets an attribute for a node.
- *
- * @param node  The node to set an attribute for.
- * @param attr  The name of the attribute.
- * @param value The value of the attribute.
- */
-void xmlnode_set_attrib(xmlnode *node, const char *attr, const char *value);
-
-#if 0
-/**
- * Gets an attribute from a node.
- *
- * @param node The node to get an attribute from.
- * @param attr The attribute to get.
- *
- * @return The value of the attribute.
- */
-const char *xmlnode_get_attrib(xmlnode *node, const char *attr);
-
-/**
- * Gets a namespaced attribute from a node
- *
- * @param node  The node to get an attribute from.
- * @param attr  The attribute to get
- * @param xmlns The namespace of the attribute to get
- *
- * @return The value of the attribute/
- */
-const char *xmlnode_get_attrib_with_namespace(xmlnode *node, const char *attr, 
const char *xmlns);
-#endif
-
-/**
  * Creates a node from a string of XML.  Calling this on the
  * root node of an XML document will parse the entire document
  * into a tree of nodes, and return the xmlnode of the root.
@@ -178,7 +76,8 @@
  *
  * @return The new node.
  */
-xmlnode *xmlnode_from_str(const char *str, int size);
+xmlnode *xmlnode_from_str(const char *str, 
+                         int size);
 
 /**
  * Frees a node and all of it's children.

Modified: GNUnet/todo
===================================================================
--- GNUnet/todo 2006-12-30 15:29:50 UTC (rev 4130)
+++ GNUnet/todo 2006-12-30 17:41:24 UTC (rev 4131)
@@ -15,8 +15,6 @@
 - Windows installer [Nils]
   * uninstall: Remove account
   * libcurl, guile
-- UPnP support:
-  * fix xmlnode memory leaks (for invalid / incomplete XML responses)
 
 0.7.2 [3'07]:
 - new features:





reply via email to

[Prev in Thread] Current Thread [Next in Thread]