Discard large metadata items first
[oweals/gnunet.git] / src / gns / gns_api.c
index f9e563fda1213f3b71d9f123edf06120cd910a63..1e3cb01695afcc0b472d99ea29a25d9f145e911d 100644 (file)
@@ -68,7 +68,7 @@ struct GNUNET_GNS_LookupRequest
   /**
    * request id 
    */
-  uint64_t r_id;
+  uint32_t r_id;
  
 };
 
@@ -88,18 +88,25 @@ struct GNUNET_GNS_ShortenRequest
    */
   struct GNUNET_GNS_ShortenRequest *prev;
 
-  /* handle to gns */
+  /**
+   * handle to gns
+   */
   struct GNUNET_GNS_Handle *gns_handle;
   
-  /* processor to call on shorten result */
+  /**
+   *  processor to call on shorten result
+   */
   GNUNET_GNS_ShortenResultProcessor shorten_proc;
   
-  /* processor closure */
+  /**
+   * processor closure
+   */
   void *proc_cls;
 
-  /* request id */
-  uint64_t r_id;
-  
+  /**
+   * request id
+   */
+  uint32_t r_id;
   
 };
 
@@ -119,16 +126,24 @@ struct GNUNET_GNS_GetAuthRequest
    */
   struct GNUNET_GNS_GetAuthRequest *prev;
 
-  /* handle to gns */
+  /**
+   * handle to gns
+   */
   struct GNUNET_GNS_Handle *gns_handle;
   
-  /* processor to call on authority lookup result */
+  /**
+   * processor to call on authority lookup result
+   */
   GNUNET_GNS_GetAuthResultProcessor auth_proc;
   
-  /* processor closure */
+  /**
+   * processor closure
+   */
   void *proc_cls;
   
-  /* request id */
+  /**
+   * request id
+   */
   uint32_t r_id;
   
 };
@@ -150,14 +165,14 @@ struct PendingMessage
   struct PendingMessage *next;
 
   /**
-   * request id
+   * Size of the message.
    */
-  uint64_t r_id;
+  size_t size;
 
   /**
-   * Size of the message.
+   * request id
    */
-  size_t size;
+  uint32_t r_id;
 
   /**
    * This message has been transmitted.  GNUNET_NO if the message is
@@ -234,8 +249,16 @@ struct GNUNET_GNS_Handle
    * Reconnect task
    */
   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+  
+  /**
+   * How long do we wait until we try to reconnect?
+   */
+  struct GNUNET_TIME_Relative reconnect_backoff;
 
-  uint32_t r_id;
+  /**
+   * Request Id generator.  Incremented by one for each request.
+   */
+  uint32_t r_id_gen;
   
   /**
    * Did we start our receive loop yet?
@@ -256,17 +279,17 @@ process_pending_messages (struct GNUNET_GNS_Handle *handle);
 /**
  * Reconnect to GNS service.
  *
- * @param h the handle to the GNS service
+ * @param handle the handle to the GNS service
  */
 static void
-reconnect (struct GNUNET_GNS_Handle *h)
+reconnect (struct GNUNET_GNS_Handle *handle)
 {
-  GNUNET_assert (NULL == h->client);
+  GNUNET_assert (NULL == handle->client);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Trying to connect to GNS\n");
-  h->client = GNUNET_CLIENT_connect ("gns", h->cfg);
-  GNUNET_assert (NULL != h->client);
-  process_pending_messages (h);
+  handle->client = GNUNET_CLIENT_connect ("gns", handle->cfg);
+  GNUNET_assert (NULL != handle->client);
+  process_pending_messages (handle);
 }
 
 
@@ -279,63 +302,63 @@ reconnect (struct GNUNET_GNS_Handle *h)
 static void
 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct GNUNET_GNS_Handle *h = cls;
+  struct GNUNET_GNS_Handle *handle = cls;
 
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  reconnect (h);
+  handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  reconnect (handle);
 }
 
 
 /**
  * Disconnect from service and then reconnect.
  *
- * @param h our handle
+ * @param handle our handle
  */
 static void
-force_reconnect (struct GNUNET_GNS_Handle *h)
+force_reconnect (struct GNUNET_GNS_Handle *handle)
 {
   struct GNUNET_GNS_ShortenRequest *st;
   struct GNUNET_GNS_LookupRequest *lh;
   struct GNUNET_GNS_GetAuthRequest *ga;
   struct PendingMessage *p;
 
-  GNUNET_CLIENT_disconnect (h->client);
-  h->client = NULL;
-  h->in_receive = GNUNET_NO;
-  for (st = h->shorten_head; NULL != st; st = st->next)
+  GNUNET_CLIENT_disconnect (handle->client);
+  handle->client = NULL;
+  handle->in_receive = GNUNET_NO;
+  for (st = handle->shorten_head; NULL != st; st = st->next)
   {
-    p = (struct PendingMessage*) &st[1];
+    p = (struct PendingMessage *) &st[1];
     if (GNUNET_NO == p->transmitted)
       continue;
     p->transmitted = GNUNET_NO;
-    GNUNET_CONTAINER_DLL_insert (h->pending_head,
-                                h->pending_tail,
+    GNUNET_CONTAINER_DLL_insert (handle->pending_head,
+                                handle->pending_tail,
                                 p);  
   }
-  for (lh = h->lookup_head; NULL != lh; lh = lh->next)
+  for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
   {
-    p = (struct PendingMessage*) &lh[1];
+    p = (struct PendingMessage *) &lh[1];
     if (GNUNET_NO == p->transmitted)
       continue;
     p->transmitted = GNUNET_NO;
-    GNUNET_CONTAINER_DLL_insert (h->pending_head,
-                                h->pending_tail,
+    GNUNET_CONTAINER_DLL_insert (handle->pending_head,
+                                handle->pending_tail,
                                 p);  
   }
-  for (ga = h->get_auth_head; NULL != ga; ga = ga->next)
+  for (ga = handle->get_auth_head; NULL != ga; ga = ga->next)
   {
-    p = (struct PendingMessage*) &ga[1];
+    p = (struct PendingMessage *) &ga[1];
     if (GNUNET_NO == p->transmitted)
       continue;
     p->transmitted = GNUNET_NO;
-    GNUNET_CONTAINER_DLL_insert (h->pending_head,
-                                h->pending_tail,
+    GNUNET_CONTAINER_DLL_insert (handle->pending_head,
+                                handle->pending_tail,
                                 p);  
   }
-  /* FIXME: 1s too long, exponential-backoff, starting at 1ms! (max = 1s might be OK) */
-  h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+  handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
+  handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
                                                     &reconnect_task,
-                                                    h);
+                                                    handle);
 }
 
 
@@ -405,43 +428,43 @@ process_pending_messages (struct GNUNET_GNS_Handle *handle)
 static size_t
 transmit_pending (void *cls, size_t size, void *buf)
 {
-  struct GNUNET_GNS_Handle *h = cls;
+  struct GNUNET_GNS_Handle *handle = cls;
   char *cbuf = buf;
   struct PendingMessage *p;
   size_t tsize;
 
-  h->th = NULL;  
+  handle->th = NULL;
   if ((0 == size) || (NULL == buf))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Transmission to GNS service failed!\n");
-    force_reconnect (h);
+    force_reconnect (handle);
     return 0;
   }  
-  if (NULL == (p = h->pending_head))
+  if (NULL == (p = handle->pending_head))
     return 0;
 
   tsize = 0;
-  while ((NULL != (p = h->pending_head)) && (p->size <= size))
+  while ((NULL != (p = handle->pending_head)) && (p->size <= size))
   {
     memcpy (&cbuf[tsize], &p[1], p->size);
     tsize += p->size;
     size -= p->size;
     p->transmitted = GNUNET_YES;
-    GNUNET_CONTAINER_DLL_remove (h->pending_head,
-                                h->pending_tail,
+    GNUNET_CONTAINER_DLL_remove (handle->pending_head,
+                                handle->pending_tail,
                                 p);
-    if (GNUNET_YES != h->in_receive)
+    if (GNUNET_YES != handle->in_receive)
     {
-      GNUNET_CLIENT_receive (h->client, &process_message, h,
+      GNUNET_CLIENT_receive (handle->client, &process_message, handle,
                              GNUNET_TIME_UNIT_FOREVER_REL);
-      h->in_receive = GNUNET_YES;
+      handle->in_receive = GNUNET_YES;
     }
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Sending %u bytes\n",
              (unsigned int) tsize);
-  process_pending_messages (h);
+  process_pending_messages (handle);
   return tsize;
 }
 
@@ -457,7 +480,7 @@ static void
 process_shorten_reply (struct GNUNET_GNS_ShortenRequest *qe,
                        const struct GNUNET_GNS_ClientShortenResultMessage *msg)
 {
-  struct GNUNET_GNS_Handle *h = qe->gns_handle;
+  struct GNUNET_GNS_Handle *handle = qe->gns_handle;
   struct PendingMessage *p = (struct PendingMessage *)&qe[1];
   const char *short_name;
   size_t mlen;
@@ -466,7 +489,7 @@ process_shorten_reply (struct GNUNET_GNS_ShortenRequest *qe,
   {
     /* service send reply to query we never managed to send!? */
     GNUNET_break (0);
-    force_reconnect (h);
+    force_reconnect (handle);
     return;
   }
   mlen = ntohs (msg->header.size);
@@ -481,11 +504,11 @@ process_shorten_reply (struct GNUNET_GNS_ShortenRequest *qe,
     if ('\0' != short_name[mlen - sizeof (struct GNUNET_GNS_ClientShortenResultMessage) - 1])
     {
       GNUNET_break (0);
-      force_reconnect (h);
+      force_reconnect (handle);
       return;
     } 
   }
-  GNUNET_CONTAINER_DLL_remove (h->shorten_head, h->shorten_tail, qe);
+  GNUNET_CONTAINER_DLL_remove (handle->shorten_head, handle->shorten_tail, qe);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received shortened reply `%s' from GNS service\n",
               short_name);
@@ -505,7 +528,7 @@ static void
 process_get_auth_reply (struct GNUNET_GNS_GetAuthRequest *qe,
                        const struct GNUNET_GNS_ClientGetAuthResultMessage *msg)
 {
-  struct GNUNET_GNS_Handle *h = qe->gns_handle;
+  struct GNUNET_GNS_Handle *handle = qe->gns_handle;
   struct PendingMessage *p = (struct PendingMessage *)&qe[1];
   const char *auth_name;
   size_t mlen;
@@ -514,7 +537,7 @@ process_get_auth_reply (struct GNUNET_GNS_GetAuthRequest *qe,
   {
     /* service send reply to query we never managed to send!? */
     GNUNET_break (0);
-    force_reconnect (h);
+    force_reconnect (handle);
     return;
   }
   mlen = ntohs (msg->header.size);
@@ -528,11 +551,11 @@ process_get_auth_reply (struct GNUNET_GNS_GetAuthRequest *qe,
     if ('\0' != auth_name[mlen - sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage) - 1])
     {
       GNUNET_break (0);
-      force_reconnect (h);
+      force_reconnect (handle);
       return;
     }
   }
-  GNUNET_CONTAINER_DLL_remove (h->get_auth_head, h->get_auth_tail, qe);
+  GNUNET_CONTAINER_DLL_remove (handle->get_auth_head, handle->get_auth_tail, qe);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received GET_AUTH reply `%s' from GNS service\n",
               auth_name);
@@ -551,7 +574,7 @@ static void
 process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
                       const struct GNUNET_GNS_ClientLookupResultMessage *msg)
 {
-  struct GNUNET_GNS_Handle *h = qe->gns_handle;
+  struct GNUNET_GNS_Handle *handle = qe->gns_handle;
   struct PendingMessage *p = (struct PendingMessage *) &qe[1];
   uint32_t rd_count = ntohl (msg->rd_count);
   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
@@ -561,7 +584,7 @@ process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
   {
     /* service send reply to query we never managed to send!? */
     GNUNET_break (0);
-    force_reconnect (h);
+    force_reconnect (handle);
     return;
   }
   mlen = ntohs (msg->header.size);
@@ -582,7 +605,7 @@ process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
                 (unsigned int) rd_count);
     qe->lookup_proc (qe->proc_cls, rd_count, rd);
   }
-  GNUNET_CONTAINER_DLL_remove (h->lookup_head, h->lookup_tail, qe);
+  GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
   GNUNET_free (qe);
 }
 
@@ -603,7 +626,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
   const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
   const struct GNUNET_GNS_ClientShortenResultMessage *shorten_msg;
   const struct GNUNET_GNS_ClientGetAuthResultMessage *get_auth_msg;
-  uint64_t r_id;
+  uint32_t r_id;
   
   if (NULL == msg)
   {
@@ -837,6 +860,7 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
   if (msize > UINT16_MAX)
   {
     GNUNET_break (0);
+    GNUNET_free (pkey_enc);
     return NULL;
   }
   lr = GNUNET_malloc (sizeof (struct GNUNET_GNS_LookupRequest) +
@@ -844,7 +868,7 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
   lr->gns_handle = handle;
   lr->lookup_proc = proc;
   lr->proc_cls = proc_cls;
-  lr->r_id = handle->r_id++;
+  lr->r_id = handle->r_id_gen++;
   pending = (struct PendingMessage *)&lr[1];
   pending->size = msize;
   pending->r_id = lr->r_id;
@@ -898,7 +922,7 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
  * @param proc_cls closure for processor
  * @return handle to the lookup request
  */
-struct GNUNET_GNS_LookupRequest*
+struct GNUNET_GNS_LookupRequest *
 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
                    const char *name,
                    enum GNUNET_GNS_RecordType type,
@@ -959,7 +983,7 @@ GNUNET_GNS_shorten_zone (struct GNUNET_GNS_Handle *handle,
   sr->gns_handle = handle;
   sr->shorten_proc = proc;
   sr->proc_cls = proc_cls;
-  sr->r_id = handle->r_id++;
+  sr->r_id = handle->r_id_gen++;
   GNUNET_CONTAINER_DLL_insert_tail (handle->shorten_head,
                                     handle->shorten_tail, sr);
   pending = (struct PendingMessage *)&sr[1];
@@ -1054,7 +1078,7 @@ GNUNET_GNS_get_authority (struct GNUNET_GNS_Handle *handle,
   gar->gns_handle = handle;
   gar->auth_proc = proc;
   gar->proc_cls = proc_cls;
-  gar->r_id = handle->r_id++;
+  gar->r_id = handle->r_id_gen++;
   GNUNET_CONTAINER_DLL_insert_tail (handle->get_auth_head,
                                     handle->get_auth_tail, gar);