- added check against statistics
[oweals/gnunet.git] / src / gns / gns_api.c
index 47efe5058e5ab161b8bf0b07c5c48b670ea04fe4..4b7d6b9f8a7e6cd742f541da1070e6ece3f5aafe 100644 (file)
 #include "gns.h"
 #include "gnunet_gns_service.h"
 
-#define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
-
-#define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
-
 /* TODO into gnunet_protocols */
 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
+#define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
+#define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
 
 /**
  * A QueueEntry.
@@ -67,7 +65,13 @@ struct GNUNET_GNS_QueueEntry
   struct GNUNET_GNS_Handle *gns_handle;
   
   /* processor to call on shorten result */
-  GNUNET_GNS_ShortenResultProcessor proc;
+  GNUNET_GNS_ShortenResultProcessor shorten_proc;
+  
+  /* processor to call on lookup result */
+  GNUNET_GNS_LookupResultProcessor lookup_proc;
+
+  /* processor to call on authority lookup result */
+  GNUNET_GNS_GetAuthResultProcessor auth_proc;
   
   /* processor closure */
   void *proc_cls;
@@ -151,6 +155,16 @@ struct GNUNET_GNS_Handle
    */
   struct GNUNET_GNS_QueueEntry *lookup_tail;
   
+  /**
+   * Head of linked list of authority lookup messages we would like to transmit.
+   */
+  struct GNUNET_GNS_QueueEntry *get_auth_head;
+
+  /**
+   * Tail of linked list of authority lookup messages we would like to transmit.
+   */
+  struct GNUNET_GNS_QueueEntry *get_auth_tail;
+
   /**
    * Reconnect task
    */
@@ -328,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,
@@ -348,6 +361,7 @@ process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe,
   {
     GNUNET_break (0);
     force_reconnect (h);
+    GNUNET_free(qe);
     return;
   }
   
@@ -355,24 +369,98 @@ process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe,
               "Received shortened reply `%s' from GNS service\n",
               short_name);
   
-  qe->proc(qe->proc_cls, 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);
 
 }
 
 
+/**
+ * Process a given reply that might match the given
+ * request.
+ *
+ * @param qe the handle to the request
+ * @param msg the message to process
+ */
+static void
+process_get_auth_reply (struct GNUNET_GNS_QueueEntry *qe,
+                       const struct GNUNET_GNS_ClientGetAuthResultMessage *msg)
+{
+  struct GNUNET_GNS_Handle *h = qe->gns_handle;
+  const char *auth_name;
+
+  GNUNET_CONTAINER_DLL_remove(h->get_auth_head, h->get_auth_tail, qe);
+
+  auth_name = (char*)(&msg[1]);
+
+  if (ntohs (((struct GNUNET_MessageHeader*)msg)->size) <
+      sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage))
+  {
+    GNUNET_free(qe);
+    GNUNET_break (0);
+    force_reconnect (h);
+    return;
+  }
+  
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "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,
                       const struct GNUNET_GNS_ClientLookupResultMessage *msg)
 {
+  struct GNUNET_GNS_Handle *h = qe->gns_handle;
+  int rd_count = ntohl(msg->rd_count);
+  size_t len = ntohs (((struct GNUNET_MessageHeader*)msg)->size);
+  struct GNUNET_NAMESTORE_RecordData rd[rd_count];
+
+  GNUNET_CONTAINER_DLL_remove(h->lookup_head, h->lookup_tail, qe);
+
+  if (len < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
+  {
+    GNUNET_free(qe);
+    GNUNET_break (0);
+    force_reconnect (h);
+    return;
+  }
+
+  len -= sizeof(struct GNUNET_GNS_ClientLookupResultMessage);
+
+  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
+  {
+  
+    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);
 }
 
 /**
@@ -388,7 +476,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
   struct GNUNET_GNS_QueueEntry *qe;
   const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
   const struct GNUNET_GNS_ClientShortenResultMessage *shorten_msg;
-  uint16_t size;
+  const struct GNUNET_GNS_ClientGetAuthResultMessage *get_auth_msg;
   uint16_t type;
   uint32_t r_id;
   
@@ -396,13 +484,12 @@ 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;
   }
 
-  size = ntohs (msg->size);
   type = ntohs (msg->type);
 
   if (type == GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT)
@@ -418,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)
@@ -427,13 +515,14 @@ 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)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Got shorten msg\n");
+                "Got SHORTEN_RESULT msg\n");
     shorten_msg = (struct GNUNET_GNS_ClientShortenResultMessage *) msg;
     
     r_id = ntohl (shorten_msg->id);
@@ -444,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)
@@ -453,10 +543,35 @@ 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)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Got GET_AUTH_RESULT msg\n");
+    get_auth_msg = (struct GNUNET_GNS_ClientGetAuthResultMessage *) msg;
+
+    r_id = ntohl (get_auth_msg->id);
+
+    if (r_id > handle->r_id)
+    {
+      /** no request found */
+      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)
+    {
+      if (qe->r_id == r_id)
+        break;
+    }
+    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);
@@ -468,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 *
@@ -477,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;
@@ -494,6 +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, 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 */
 }
 
@@ -516,18 +639,58 @@ 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,
-                         const char * name,
-                         enum GNUNET_GNS_RecordType type,
-                         GNUNET_GNS_LookupIterator iter,
-                         void *iter_cls)
+                   const char * name,
+                   enum GNUNET_GNS_RecordType type,
+                   GNUNET_GNS_LookupResultProcessor proc,
+                   void *proc_cls)
 {
-  return NULL;
+  /* IPC to shorten gns names, return shorten_handle */
+  struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
+  struct GNUNET_GNS_QueueEntry *qe;
+  size_t msize;
+  struct PendingMessage *pending;
+
+  if (NULL == name)
+  {
+    return NULL;
+  }
+
+  msize = sizeof (struct GNUNET_GNS_ClientLookupMessage) + strlen(name) + 1;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to lookup %s in GNS\n", name);
+
+  qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
+  qe->gns_handle = handle;
+  qe->lookup_proc = proc;
+  qe->proc_cls = proc_cls;
+  qe->r_id = get_request_id(handle);
+  GNUNET_CONTAINER_DLL_insert_tail(handle->lookup_head,
+                                   handle->lookup_tail, qe);
+
+  pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
+  memset(pending, 0, (sizeof (struct PendingMessage) + msize));
+  
+  pending->size = msize;
+
+  lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
+  lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
+  lookup_msg->header.size = htons (msize);
+  lookup_msg->id = htonl(qe->r_id);
+  lookup_msg->type = htonl(type);
+
+  memcpy(&lookup_msg[1], name, strlen(name));
+
+  GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail,
+                               pending);
+  
+  process_pending_messages (handle);
+  return qe;
 }
 
 
@@ -562,7 +725,7 @@ GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
 
   qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
   qe->gns_handle = handle;
-  qe->proc = proc;
+  qe->shorten_proc = proc;
   qe->proc_cls = proc_cls;
   qe->r_id = get_request_id(handle);
   GNUNET_CONTAINER_DLL_insert_tail(handle->shorten_head,
@@ -580,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);
@@ -588,5 +751,61 @@ GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
 }
 
 
+/**
+ * Perform an authority lookup for a given name.
+ *
+ * @param handle handle to the GNS service
+ * @param name the name to look up authority for
+ * @param proc function to call on result
+ * @param proc_cls closure for processor
+ * @return handle to the operation
+ */
+struct GNUNET_GNS_QueueEntry *
+GNUNET_GNS_get_authority (struct GNUNET_GNS_Handle *handle,
+                    const char * name,
+                    GNUNET_GNS_GetAuthResultProcessor proc,
+                    void *proc_cls)
+{
+  struct GNUNET_GNS_ClientGetAuthMessage *get_auth_msg;
+  struct GNUNET_GNS_QueueEntry *qe;
+  size_t msize;
+  struct PendingMessage *pending;
+
+  if (NULL == name)
+  {
+    return NULL;
+  }
+
+  msize = sizeof (struct GNUNET_GNS_ClientGetAuthMessage) + strlen(name) + 1;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Trying to look up authority for %s in GNS\n", name);
+
+  qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
+  qe->gns_handle = handle;
+  qe->auth_proc = proc;
+  qe->proc_cls = proc_cls;
+  qe->r_id = get_request_id(handle);
+  GNUNET_CONTAINER_DLL_insert_tail(handle->get_auth_head,
+                                   handle->get_auth_tail, qe);
+
+  pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
+  memset(pending, 0, (sizeof (struct PendingMessage) + msize));
+  
+  pending->size = msize;
+
+  get_auth_msg = (struct GNUNET_GNS_ClientGetAuthMessage *) &pending[1];
+  get_auth_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_GET_AUTH);
+  get_auth_msg->header.size = htons (msize);
+  get_auth_msg->id = htonl(qe->r_id);
+
+  memcpy(&get_auth_msg[1], name, strlen(name));
+
+  GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail,
+                               pending);
+  
+  process_pending_messages (handle);
+  return qe;
+}
+
 
 /* end of gns_api.c */