- fixes, intendation
[oweals/gnunet.git] / src / gns / gns_api.c
index 49d2246215aba693c71ce781259f6cab9a21d859..407276e872fa25eb97163570e0de1dcf6cad7102 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -46,32 +46,32 @@ struct GNUNET_GNS_LookupRequest
    * DLL
    */
   struct GNUNET_GNS_LookupRequest *next;
-  
+
   /**
    * DLL
    */
   struct GNUNET_GNS_LookupRequest *prev;
-  
+
   /**
-   * handle to gns 
+   * handle to gns
    */
   struct GNUNET_GNS_Handle *gns_handle;
-  
+
   /**
-   * processor to call on lookup result 
+   * processor to call on lookup result
    */
   GNUNET_GNS_LookupResultProcessor lookup_proc;
 
   /**
-   * processor closure 
+   * processor closure
    */
   void *proc_cls;
+
   /**
-   * request id 
+   * request id
    */
   uint32_t r_id;
+
 };
 
 
@@ -130,7 +130,7 @@ struct GNUNET_GNS_Handle
    * Currently pending transmission request (or NULL).
    */
   struct GNUNET_CLIENT_TransmitHandle *th;
-  
+
   /**
    * Head of linked list of shorten messages we would like to transmit.
    */
@@ -140,7 +140,7 @@ struct GNUNET_GNS_Handle
    * Tail of linked list of shorten messages we would like to transmit.
    */
   struct PendingMessage *pending_tail;
-  
+
   /**
    * Head of linked list of lookup messages we would like to transmit.
    */
@@ -154,8 +154,8 @@ struct GNUNET_GNS_Handle
   /**
    * Reconnect task
    */
-  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
-  
+  struct GNUNET_SCHEDULER_Task * reconnect_task;
+
   /**
    * How long do we wait until we try to reconnect?
    */
@@ -165,7 +165,7 @@ struct GNUNET_GNS_Handle
    * Request Id generator.  Incremented by one for each request.
    */
   uint32_t r_id_gen;
-  
+
   /**
    * Did we start our receive loop yet?
    */
@@ -210,7 +210,7 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_GNS_Handle *handle = cls;
 
-  handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  handle->reconnect_task = NULL;
   reconnect (handle);
 }
 
@@ -237,7 +237,7 @@ force_reconnect (struct GNUNET_GNS_Handle *handle)
     p->transmitted = GNUNET_NO;
     GNUNET_CONTAINER_DLL_insert (handle->pending_head,
                                 handle->pending_tail,
-                                p);  
+                                p);
   }
   handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
   handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
@@ -279,17 +279,17 @@ process_pending_messages (struct GNUNET_GNS_Handle *handle)
   struct PendingMessage *p = handle->pending_head;
 
   if (NULL == handle->client)
-    return; /* wait for reconnect */ 
+    return; /* wait for reconnect */
   if (NULL != handle->th)
-    return; /* transmission request already pending */ 
+    return; /* transmission request already pending */
 
   while ((NULL != p) && (p->transmitted == GNUNET_YES))
     p = p->next;
   if (NULL == p)
     return; /* no messages pending */
-  
+
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Trying to transmit %u bytes\n", 
+       "Trying to transmit %u bytes\n",
        (unsigned int) p->size);
   handle->th =
     GNUNET_CLIENT_notify_transmit_ready (handle->client,
@@ -324,7 +324,7 @@ transmit_pending (void *cls, size_t size, void *buf)
         "Transmission to GNS service failed!\n");
     force_reconnect (handle);
     return 0;
-  }  
+  }
   if (NULL == (p = handle->pending_head))
     return 0;
 
@@ -365,8 +365,10 @@ process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
 {
   struct GNUNET_GNS_Handle *handle = qe->gns_handle;
   struct PendingMessage *p = (struct PendingMessage *) &qe[1];
+  GNUNET_GNS_LookupResultProcessor proc;
+  void *proc_cls;
   uint32_t rd_count = ntohl (msg->rd_count);
-  struct GNUNET_NAMESTORE_RecordData rd[rd_count];
+  struct GNUNET_GNSRECORD_Data rd[rd_count];
   size_t mlen;
 
   if (GNUNET_YES != p->transmitted)
@@ -378,24 +380,26 @@ process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
   }
   mlen = ntohs (msg->header.size);
   mlen -= sizeof (struct GNUNET_GNS_ClientLookupResultMessage);
-  if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (mlen,
+  proc = qe->lookup_proc;
+  proc_cls = qe->proc_cls;
+  GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
+  GNUNET_free (qe);
+  if (GNUNET_SYSERR == GNUNET_GNSRECORD_records_deserialize (mlen,
                                                              (const char*) &msg[1],
                                                              rd_count,
                                                              rd))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
-        _("Failed to serialize lookup reply from GNS service!\n"));
-    qe->lookup_proc (qe->proc_cls, 0, NULL);
+        _("Failed to deserialize lookup reply from GNS service!\n"));
+    proc (proc_cls, 0, NULL);
   }
   else
-  { 
+  {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Received lookup reply from GNS service (%u records)\n",
         (unsigned int) rd_count);
-    qe->lookup_proc (qe->proc_cls, rd_count, rd);
+    proc (proc_cls, rd_count, rd);
   }
-  GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
-  GNUNET_free (qe);
 }
 
 
@@ -412,12 +416,15 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
   struct GNUNET_GNS_LookupRequest *lr;
   const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
   uint32_t r_id;
-  
+
   if (NULL == msg)
   {
     force_reconnect (handle);
     return;
   }
+
+  GNUNET_CLIENT_receive (handle->client, &process_message, handle,
+                        GNUNET_TIME_UNIT_FOREVER_REL);
   switch (ntohs (msg->type))
   {
   case GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT:
@@ -430,11 +437,11 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
       return;
     }
     lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
-    r_id = ntohl (lookup_msg->id);   
-    for (lr = handle->lookup_head; NULL != lr; lr = lr->next)    
+    r_id = ntohl (lookup_msg->id);
+    for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
       if (lr->r_id == r_id)
       {
-       process_lookup_reply(lr, lookup_msg);    
+       process_lookup_reply(lr, lookup_msg);
        break;
       }
     break;
@@ -443,8 +450,6 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
     force_reconnect (handle);
     return;
   }
-  GNUNET_CLIENT_receive (handle->client, &process_message, handle,
-                        GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
 
@@ -474,11 +479,15 @@ GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 void
 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
 {
-  GNUNET_CLIENT_disconnect (handle->client);
-  if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
+  if (NULL != handle->client)
+  {
+    GNUNET_CLIENT_disconnect (handle->client);
+    handle->client = NULL;
+  }
+  if (NULL != handle->reconnect_task)
   {
     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
-    handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    handle->reconnect_task = NULL;
   }
   GNUNET_assert (NULL == handle->lookup_head);
   GNUNET_free (handle);
@@ -495,7 +504,7 @@ GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
 {
   struct PendingMessage *p = (struct PendingMessage*) &lr[1];
 
-  GNUNET_assert (NULL != lr->gns_handle); 
+  GNUNET_assert (NULL != lr->gns_handle);
   if (GNUNET_NO == p->transmitted)
     GNUNET_CONTAINER_DLL_remove (lr->gns_handle->pending_head,
                                  lr->gns_handle->pending_tail,
@@ -514,19 +523,19 @@ GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
  * @param name the name to look up
  * @param zone the zone to start the resolution in
  * @param type the record type to look up
- * @param only_cached #GNUNET_YES to only check locally (not in the DHT)
+ * @param options local options for the lookup
  * @param shorten_zone_key the private key of the shorten zone (can be NULL)
  * @param proc processor to call on result
- * @param proc_cls closure for processor
+ * @param proc_cls closure for @a proc
  * @return handle to the get request
  */
 struct GNUNET_GNS_LookupRequest*
 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
                   const char *name,
-                  const struct GNUNET_CRYPTO_EccPublicSignKey *zone,
-                  int type,
-                  int only_cached,
-                  const struct GNUNET_CRYPTO_EccPrivateKey *shorten_zone_key,
+                  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)
 {
@@ -540,9 +549,9 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
   {
     GNUNET_break (0);
     return NULL;
-  } 
+  }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Trying to lookup `%s' in GNS\n", 
+       "Trying to lookup `%s' in GNS\n",
        name);
   msize = sizeof (struct GNUNET_GNS_ClientLookupMessage)
     + strlen (name) + 1;
@@ -567,12 +576,12 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
   lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
   lookup_msg->header.size = htons (msize);
   lookup_msg->id = htonl (lr->r_id);
-  lookup_msg->only_cached = htonl (only_cached);
+  lookup_msg->options = htons ((uint16_t) options);
   lookup_msg->zone = *zone;
   lookup_msg->type = htonl (type);
   if (NULL != shorten_zone_key)
   {
-    lookup_msg->have_key = htonl (GNUNET_YES);
+    lookup_msg->have_key = htons (GNUNET_YES);
     lookup_msg->shorten_key = *shorten_zone_key;
   }
   memcpy (&lookup_msg[1], name, strlen (name) + 1);