fix compiler warning for format string
[oweals/gnunet.git] / src / gns / gns_api.c
index 475d5b5cdc76dc676f1172c2f79edee25a6cc94a..3f6425b42a2195af3a789f719c546d7f649f3766 100644 (file)
@@ -79,6 +79,49 @@ struct GNUNET_GNS_LookupRequest
 
 };
 
+/**
+ * Handle to a lookup request
+ */
+struct GNUNET_GNS_ReverseLookupRequest
+{
+
+  /**
+   * DLL
+   */
+  struct GNUNET_GNS_ReverseLookupRequest *next;
+
+  /**
+   * DLL
+   */
+  struct GNUNET_GNS_ReverseLookupRequest *prev;
+
+  /**
+   * handle to gns
+   */
+  struct GNUNET_GNS_Handle *gns_handle;
+
+  /**
+   * processor to call on lookup result
+   */
+  GNUNET_GNS_ReverseLookupResultProcessor lookup_proc;
+
+  /**
+   * @e lookup_proc closure
+   */
+  void *proc_cls;
+
+  /**
+   * Envelope with the message for this queue entry.
+   */
+  struct GNUNET_MQ_Envelope *env;
+
+  /**
+   * request id
+   */
+  uint32_t r_id;
+
+};
+
 
 /**
  * Connection to the GNS service.
@@ -106,6 +149,15 @@ struct GNUNET_GNS_Handle
    */
   struct GNUNET_GNS_LookupRequest *lookup_tail;
 
+  /**
+   * Head of linked list of active reverse lookup requests.
+   */
+  struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_head;
+
+  /**
+   * Tail of linked list of active reverse lookup requests.
+   */
+  struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_tail;
   /**
    * Reconnect task
    */
@@ -180,10 +232,71 @@ mq_error_handler (void *cls,
                   enum GNUNET_MQ_Error error)
 {
   struct GNUNET_GNS_Handle *handle = cls;
-
+  LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n",
+       error);
   force_reconnect (handle);
 }
 
+/**
+ * Check validity of message received from the GNS service
+ *
+ * @param cls the `struct GNUNET_GNS_Handle *`
+ * @param loookup_msg the incoming message
+ */
+static int
+check_rev_result (void *cls,
+              const struct ReverseLookupResultMessage *lookup_msg)
+{
+  size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
+  char *name;
+  
+  name = (char*) &lookup_msg[1];
+  if ('\0' != name[mlen-1])
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Handler for messages received from the GNS service
+ *
+ * @param cls the `struct GNUNET_GNS_Handle *`
+ * @param loookup_msg the incoming message
+ */
+static void
+handle_rev_result (void *cls,
+                   const struct ReverseLookupResultMessage *lookup_msg)
+{
+  struct GNUNET_GNS_Handle *handle = cls;
+  char *name;
+  uint32_t r_id = ntohl (lookup_msg->id);
+  struct GNUNET_GNS_ReverseLookupRequest *rlr;
+  GNUNET_GNS_ReverseLookupResultProcessor proc;
+  void *proc_cls;
+
+  name = (char*)&lookup_msg[1];
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received reverse lookup reply from GNS service (%s)\n",
+       name);
+  for (rlr = handle->rev_lookup_head; NULL != rlr; rlr = rlr->next)
+    if (rlr->r_id == r_id)
+      break;
+  if (NULL == rlr)
+    return;
+  proc = rlr->lookup_proc;
+  proc_cls = rlr->proc_cls;
+  GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
+                               handle->rev_lookup_tail,
+                               rlr);
+  GNUNET_free (rlr);
+  proc (proc_cls,
+        name);
+}
+
+
 
 /**
  * Check validity of message received from the GNS service
@@ -193,7 +306,7 @@ mq_error_handler (void *cls,
  */
 static int
 check_result (void *cls,
-              const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg)
+              const struct LookupResultMessage *lookup_msg)
 {
   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
   uint32_t rd_count = ntohl (lookup_msg->rd_count);
@@ -220,7 +333,7 @@ check_result (void *cls,
  */
 static void
 handle_result (void *cls,
-               const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg)
+               const struct LookupResultMessage *lookup_msg)
 {
   struct GNUNET_GNS_Handle *handle = cls;
   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
@@ -267,11 +380,16 @@ reconnect (struct GNUNET_GNS_Handle *handle)
   struct GNUNET_MQ_MessageHandler handlers[] = {
     GNUNET_MQ_hd_var_size (result,
                            GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
-                           struct GNUNET_GNS_ClientLookupResultMessage,
+                           struct LookupResultMessage,
+                           handle),
+    GNUNET_MQ_hd_var_size (rev_result,
+                           GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP_RESULT,
+                           struct ReverseLookupResultMessage,
                            handle),
     GNUNET_MQ_handler_end ()
   };
   struct GNUNET_GNS_LookupRequest *lh;
+  struct GNUNET_GNS_ReverseLookupRequest *rlh;
 
   GNUNET_assert (NULL == handle->mq);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -286,6 +404,9 @@ reconnect (struct GNUNET_GNS_Handle *handle)
   for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
     GNUNET_MQ_send_copy (handle->mq,
                          lh->env);
+  for (rlh = handle->rev_lookup_head; NULL != rlh; rlh = rlh->next)
+    GNUNET_MQ_send_copy (handle->mq,
+                         rlh->env);
 }
 
 
@@ -331,6 +452,7 @@ GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
     handle->reconnect_task = NULL;
   }
   GNUNET_assert (NULL == handle->lookup_head);
+  GNUNET_assert (NULL == handle->rev_lookup_head);
   GNUNET_free (handle);
 }
 
@@ -352,6 +474,22 @@ GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
   GNUNET_free (lr);
 }
 
+/**
+ * Cancel pending reverse lookup request
+ *
+ * @param lr the lookup request to cancel
+ */
+void
+GNUNET_GNS_reverse_lookup_cancel (struct GNUNET_GNS_ReverseLookupRequest *lr)
+{
+  struct GNUNET_GNS_Handle *handle = lr->gns_handle;
+
+  GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
+                               handle->rev_lookup_tail,
+                               lr);
+  GNUNET_MQ_discard (lr->env);
+  GNUNET_free (lr);
+}
 
 /**
  * Perform an asynchronous lookup operation on the GNS.
@@ -368,16 +506,16 @@ GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
  */
 struct GNUNET_GNS_LookupRequest*
 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
-                  const char *name,
-                  const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
-                  uint32_t type,
-                  enum GNUNET_GNS_LocalOptions options,
-                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
-                  GNUNET_GNS_LookupResultProcessor proc,
-                  void *proc_cls)
+                   const char *name,
+                   const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
+                   uint32_t type,
+                   enum GNUNET_GNS_LocalOptions options,
+                   const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
+                   GNUNET_GNS_LookupResultProcessor proc,
+                   void *proc_cls)
 {
   /* IPC to shorten gns names, return shorten_handle */
-  struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
+  struct LookupMessage *lookup_msg;
   struct GNUNET_GNS_LookupRequest *lr;
   size_t nlen;
 
@@ -413,8 +551,8 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
     lookup_msg->shorten_key = *shorten_zone_key;
   }
   GNUNET_memcpy (&lookup_msg[1],
-          name,
-          nlen);
+                 name,
+                 nlen);
   GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
                                handle->lookup_tail,
                                lr);
@@ -424,5 +562,50 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
   return lr;
 }
 
+/**
+ * Perform an asynchronous reverse lookup operation on the GNS.
+ *
+ * @param handle handle to the GNS service
+ * @param zone_key zone to find a name for
+ * @param root_key our zone
+ * @param proc processor to call on result
+ * @param proc_cls closure for @a proc
+ * @return handle to the request
+ */
+struct GNUNET_GNS_ReverseLookupRequest*
+GNUNET_GNS_reverse_lookup (struct GNUNET_GNS_Handle *handle,
+                           const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
+                           const struct GNUNET_CRYPTO_EcdsaPublicKey *root_key,
+                           GNUNET_GNS_ReverseLookupResultProcessor proc,
+                           void *proc_cls)
+{
+  /* IPC to shorten gns names, return shorten_handle */
+  struct ReverseLookupMessage *rev_lookup_msg;
+  struct GNUNET_GNS_ReverseLookupRequest *lr;
 
+  if ((NULL == zone_key) || (NULL == root_key))
+  {
+    GNUNET_break (0);
+    return NULL;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Trying to reverse lookup in GNS\n");
+  lr = GNUNET_new (struct GNUNET_GNS_ReverseLookupRequest);
+  lr->gns_handle = handle;
+  lr->lookup_proc = proc;
+  lr->proc_cls = proc_cls;
+  lr->r_id = handle->r_id_gen++;
+  lr->env = GNUNET_MQ_msg (rev_lookup_msg,
+                           GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP);
+  rev_lookup_msg->id = htonl (lr->r_id);
+  rev_lookup_msg->zone_pkey = *zone_key;
+  rev_lookup_msg->root_pkey = *root_key;
+  GNUNET_CONTAINER_DLL_insert (handle->rev_lookup_head,
+                               handle->rev_lookup_tail,
+                               lr);
+  if (NULL != handle->mq)
+    GNUNET_MQ_send_copy (handle->mq,
+                         lr->env);
+  return lr;
+}
 /* end of gns_api.c */