- added check against statistics
[oweals/gnunet.git] / src / gns / gns_api.c
index 9a4c4cff0f4a95d55a9b46281f562c09ef6628fb..4b7d6b9f8a7e6cd742f541da1070e6ece3f5aafe 100644 (file)
@@ -342,9 +342,8 @@ transmit_pending (void *cls, size_t size, void *buf)
  * Process a given reply that might match the given
  * request.
  *
- * @param cls the 'struct GNUNET_GNS_ClientResultMessage'
- * @param key query of the request
- * @param value the 'struct GNUNET_GNS_LookupHandle' of a request matching the same key
+ * @param qe a queue entry
+ * @param msg the shorten msg received
  */
 static void
 process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe,
@@ -362,6 +361,7 @@ process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe,
   {
     GNUNET_break (0);
     force_reconnect (h);
+    GNUNET_free(qe);
     return;
   }
   
@@ -369,7 +369,10 @@ process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe,
               "Received shortened reply `%s' from GNS service\n",
               short_name);
   
+  GNUNET_CLIENT_receive (h->client, &process_message, h,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
   qe->shorten_proc(qe->proc_cls, short_name);
+  GNUNET_free(qe);
 
 }
 
@@ -395,6 +398,7 @@ process_get_auth_reply (struct GNUNET_GNS_QueueEntry *qe,
   if (ntohs (((struct GNUNET_MessageHeader*)msg)->size) <
       sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage))
   {
+    GNUNET_free(qe);
     GNUNET_break (0);
     force_reconnect (h);
     return;
@@ -404,17 +408,17 @@ process_get_auth_reply (struct GNUNET_GNS_QueueEntry *qe,
               "Received GET_AUTH reply `%s' from GNS service\n",
               auth_name);
   
+  GNUNET_CLIENT_receive (h->client, &process_message, h,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
   qe->auth_proc(qe->proc_cls, auth_name);
+  GNUNET_free(qe);
 
 }
 /**
  * Process a given reply to the lookup request
  *
- * @param cls the 'struct GNUNET_GNS_ClientResultMessage'
- * @param key query of the request
- * @param value the 'struct GNUNET_GNS_LookupHandle' of a request matching the same key
- * @return GNUNET_YES to continue to iterate over all results,
- *         GNUNET_NO if the reply is malformed
+ * @param qe a queue entry
+ * @param msg the lookup message received
  */
 static void
 process_lookup_reply (struct GNUNET_GNS_QueueEntry *qe,
@@ -429,6 +433,7 @@ process_lookup_reply (struct GNUNET_GNS_QueueEntry *qe,
 
   if (len < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
   {
+    GNUNET_free(qe);
     GNUNET_break (0);
     force_reconnect (h);
     return;
@@ -436,15 +441,26 @@ process_lookup_reply (struct GNUNET_GNS_QueueEntry *qe,
 
   len -= sizeof(struct GNUNET_GNS_ClientLookupResultMessage);
 
-  GNUNET_NAMESTORE_records_deserialize (len, (char*)&msg[1],
-                                        rd_count,
-                                        rd);
-  
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received lookup reply from GNS service (count=%d)\n",
-              ntohl(msg->rd_count));
+  GNUNET_CLIENT_receive (h->client, &process_message, h,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
+  if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (len,
+                                                             (char*)&msg[1],
+                                                             rd_count,
+                                                             rd))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to serialize lookup reply from GNS service!\n");
+    qe->lookup_proc(qe->proc_cls, 0, NULL);
+  }
+  else
+  {
   
-  qe->lookup_proc(qe->proc_cls, rd_count, rd);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received lookup reply from GNS service (count=%d)\n",
+                ntohl(msg->rd_count));
+    qe->lookup_proc(qe->proc_cls, rd_count, rd);
+  }
+  GNUNET_free(qe);
 }
 
 /**
@@ -468,7 +484,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
               "Got message\n");
   if (msg == NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
          "Error receiving data from GNS service, reconnecting\n");
     force_reconnect (handle);
     return;
@@ -489,6 +505,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
       GNUNET_break_op (0);
       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
                              GNUNET_TIME_UNIT_FOREVER_REL);
+      return;
     }
 
     for (qe = handle->lookup_head; qe != NULL; qe = qe->next)
@@ -498,7 +515,8 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
     }
     if (qe)
       process_lookup_reply(qe, lookup_msg);
-
+    
+    return;
 
   }
   else if (type == GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT)
@@ -515,6 +533,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
       GNUNET_break_op (0);
       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
                              GNUNET_TIME_UNIT_FOREVER_REL);
+      return;
     }
 
     for (qe = handle->shorten_head; qe != NULL; qe = qe->next)
@@ -524,6 +543,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
     }
     if (qe)
       process_shorten_reply(qe, shorten_msg);
+    return;
   }
   else if (type == GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT)
   {
@@ -539,6 +559,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
       GNUNET_break_op (0);
       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
                              GNUNET_TIME_UNIT_FOREVER_REL);
+      return;
     }
 
     for (qe = handle->get_auth_head; qe != NULL; qe = qe->next)
@@ -548,10 +569,9 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
     }
     if (qe)
       process_get_auth_reply(qe, get_auth_msg);
+    return;
   }
 
-  GNUNET_CLIENT_receive (handle->client, &process_message, handle,
-                         GNUNET_TIME_UNIT_FOREVER_REL);
 
   if (GNUNET_YES == handle->reconnect)
     force_reconnect (handle);
@@ -563,7 +583,6 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
  * Initialize the connection with the GNS service.
  *
  * @param cfg configuration to use
- * @param ht_len size of the internal hash table to use for parallel requests
  * @return handle to the GNS service, or NULL on error
  */
 struct GNUNET_GNS_Handle *
@@ -572,9 +591,11 @@ GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
   struct GNUNET_GNS_Handle *handle;
 
   handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_Handle));
+  handle->reconnect = GNUNET_NO;
   handle->cfg = cfg;
   reconnect (handle);
   //handle->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, handle);
+  handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
   handle->r_id = 0;
   handle->in_receive = GNUNET_NO;
   return handle;
@@ -589,7 +610,13 @@ GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 void
 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
 {
-  GNUNET_CLIENT_disconnect (handle->client, 0);
+  GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
+  if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
+  {
+    GNUNET_SCHEDULER_cancel (handle->reconnect_task);
+    handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  GNUNET_free(handle);
   /* disco from GNS */
 }
 
@@ -612,9 +639,10 @@ get_request_id (struct GNUNET_GNS_Handle *h)
  *
  * @param handle handle to the GNS service
  * @param name the name to look up
- * @param iter function to call on each result
- * @param iter_cls closure for iter
- * @return handle to stop the async get
+ * @param type the record type to look up
+ * @param proc processor to call on result
+ * @param proc_cls closure for processor
+ * @return handle to the get
  */
 struct GNUNET_GNS_QueueEntry *
 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
@@ -658,7 +686,7 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
 
   memcpy(&lookup_msg[1], name, strlen(name));
 
-  GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
+  GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail,
                                pending);
   
   process_pending_messages (handle);
@@ -715,7 +743,7 @@ GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
 
   memcpy(&shorten_msg[1], name, strlen(name));
 
-  GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
+  GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail,
                                pending);
   
   process_pending_messages (handle);
@@ -772,7 +800,7 @@ GNUNET_GNS_get_authority (struct GNUNET_GNS_Handle *handle,
 
   memcpy(&get_auth_msg[1], name, strlen(name));
 
-  GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
+  GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail,
                                pending);
   
   process_pending_messages (handle);