-make static
[oweals/gnunet.git] / src / identity / identity_api.c
index 87ed361c410fc398e22af844b615670ac5ab2d41..a31086f5db88498b8288ea4f3cd347e7edfc3c0f 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 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 Liceidentity as published
@@ -32,7 +32,7 @@
 
 #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__)
 
-/** 
+/**
  * Handle for an ego.
  */
 struct GNUNET_IDENTITY_Ego
@@ -40,7 +40,7 @@ struct GNUNET_IDENTITY_Ego
   /**
    * Private key associated with this ego.
    */
-  struct GNUNET_CRYPTO_EccPrivateKey *pk;
+  struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
 
   /**
    * Current name associated with this ego.
@@ -59,7 +59,7 @@ struct GNUNET_IDENTITY_Ego
 };
 
 
-/** 
+/**
  * Handle for an operation with the identity service.
  */
 struct GNUNET_IDENTITY_Operation
@@ -69,7 +69,7 @@ struct GNUNET_IDENTITY_Operation
    * Main identity handle.
    */
   struct GNUNET_IDENTITY_Handle *h;
-  
+
   /**
    * We keep operations in a DLL.
    */
@@ -87,19 +87,19 @@ struct GNUNET_IDENTITY_Operation
   const struct GNUNET_MessageHeader *msg;
 
   /**
-   * Continuation to invoke with the result of the transmission; 'cb'
+   * Continuation to invoke with the result of the transmission; @e cb
    * will be NULL in this case.
    */
   GNUNET_IDENTITY_Continuation cont;
 
   /**
    * Continuation to invoke with the result of the transmission for
-   * 'get' operations ('cont' will be NULL in this case).
+   * 'get' operations (@e cont will be NULL in this case).
    */
   GNUNET_IDENTITY_Callback cb;
 
   /**
-   * Closure for 'cont' or 'cb'.
+   * Closure for @e cont or @e cb.
    */
   void *cls;
 
@@ -139,12 +139,12 @@ struct GNUNET_IDENTITY_Handle
 
   /**
    * Head of active operations.
-   */ 
+   */
   struct GNUNET_IDENTITY_Operation *op_head;
 
   /**
    * Tail of active operations.
-   */ 
+   */
   struct GNUNET_IDENTITY_Operation *op_tail;
 
   /**
@@ -155,7 +155,7 @@ struct GNUNET_IDENTITY_Handle
   /**
    * Task doing exponential back-off trying to reconnect.
    */
-  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+  struct GNUNET_SCHEDULER_Task * reconnect_task;
 
   /**
    * Time for next connect retry.
@@ -172,17 +172,19 @@ struct GNUNET_IDENTITY_Handle
 
 /**
  * Obtain the ego representing 'anonymous' users.
+ *
+ * @return handle for the anonymous user, must not be freed
  */
 const struct GNUNET_IDENTITY_Ego *
 GNUNET_IDENTITY_ego_get_anonymous ()
 {
   static struct GNUNET_IDENTITY_Ego anon;
-  struct GNUNET_CRYPTO_EccPublicKey pub;
+  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
 
   if (NULL != anon.pk)
     return &anon;
-  anon.pk = (struct GNUNET_CRYPTO_EccPrivateKey *) GNUNET_CRYPTO_ecc_key_get_anonymous ();
-  GNUNET_CRYPTO_ecc_key_get_public (anon.pk,
+  anon.pk = (struct GNUNET_CRYPTO_EcdsaPrivateKey *) GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
+  GNUNET_CRYPTO_ecdsa_key_get_public (anon.pk,
                                    &pub);
   GNUNET_CRYPTO_hash (&pub, sizeof (pub), &anon.id);
   return &anon;
@@ -208,7 +210,7 @@ reconnect (void *cls,
 static void
 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
 {
-  GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
+  GNUNET_assert (h->reconnect_task == NULL);
 
   if (NULL != h->th)
   {
@@ -238,7 +240,7 @@ reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
  * @param msg message received, NULL on timeout or fatal error
  */
 static void
-message_handler (void *cls, 
+message_handler (void *cls,
                 const struct GNUNET_MessageHeader *msg)
 {
   struct GNUNET_IDENTITY_Handle *h = cls;
@@ -247,7 +249,7 @@ message_handler (void *cls,
   const struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
   const struct GNUNET_IDENTITY_UpdateMessage *um;
   const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
-  struct GNUNET_CRYPTO_EccPublicKey pub;
+  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
   struct GNUNET_HashCode id;
   const char *str;
   uint16_t size;
@@ -305,7 +307,7 @@ message_handler (void *cls,
     }
     um = (const struct GNUNET_IDENTITY_UpdateMessage *) msg;
     name_len = ntohs (um->name_len);
-    
+
     str = (const char *) &um[1];
     if ( (size != name_len + sizeof (struct GNUNET_IDENTITY_UpdateMessage)) ||
         ( (0 != name_len) &&
@@ -324,7 +326,7 @@ message_handler (void *cls,
        h->cb (h->cb_cls, NULL, NULL, NULL);
       break;
     }
-    GNUNET_CRYPTO_ecc_key_get_public (&um->private_key,
+    GNUNET_CRYPTO_ecdsa_key_get_public (&um->private_key,
                                      &pub);
     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
     if (0 == name_len)
@@ -344,7 +346,7 @@ message_handler (void *cls,
        return;
       }
       ego = GNUNET_new (struct GNUNET_IDENTITY_Ego);
-      ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
+      ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
       *ego->pk = um->private_key;
       ego->name = GNUNET_strdup (str);
       ego->id = id;
@@ -367,7 +369,7 @@ message_handler (void *cls,
       /* ego changed name */
       GNUNET_free (ego->name);
       ego->name = GNUNET_strdup (str);
-    }    
+    }
     GNUNET_CLIENT_receive (h->client, &message_handler, h,
                           GNUNET_TIME_UNIT_FOREVER_REL);
     /* inform application about change */
@@ -404,7 +406,7 @@ message_handler (void *cls,
     }
     /* Note: we know which service this should be for, so we're not
        really using 'str' henceforth */
-    GNUNET_CRYPTO_ecc_key_get_public (&sdm->private_key,
+    GNUNET_CRYPTO_ecdsa_key_get_public (&sdm->private_key,
                                      &pub);
     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
     ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
@@ -416,6 +418,14 @@ message_handler (void *cls,
       return;
     }
     op = h->op_head;
+    if (NULL == op)
+    {
+      GNUNET_break (0);
+      reschedule_connect (h);
+      return;
+    }
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received SET_DEFAULT message from identity service\n");
     GNUNET_CONTAINER_DLL_remove (h->op_head,
                                 h->op_tail,
                                 op);
@@ -448,20 +458,20 @@ transmit_next (struct GNUNET_IDENTITY_Handle *h);
 /**
  * Transmit next message to service.
  *
- * @param cls the 'struct GNUNET_IDENTITY_Handle'.
- * @param size number of bytes available in buf
+ * @param cls the `struct GNUNET_IDENTITY_Handle`.
+ * @param size number of bytes available in @a buf
  * @param buf where to copy the message
  * @return number of bytes copied to buf
  */
 static size_t
-send_next_message (void *cls, 
-                  size_t size, 
+send_next_message (void *cls,
+                  size_t size,
                   void *buf)
 {
   struct GNUNET_IDENTITY_Handle *h = cls;
   struct GNUNET_IDENTITY_Operation *op = h->op_head;
   size_t ret;
-  
+
   h->th = NULL;
   if (NULL == op)
     return 0;
@@ -470,7 +480,7 @@ send_next_message (void *cls,
   {
     reschedule_connect (h);
     return 0;
-  }  
+  }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Sending message of type %d to identity service\n",
        ntohs (op->msg->type));
@@ -532,22 +542,26 @@ reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_IDENTITY_Operation *op;
   struct GNUNET_MessageHeader msg;
 
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  h->reconnect_task = NULL;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Connecting to identity service.\n");
   GNUNET_assert (NULL == h->client);
   h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
   GNUNET_assert (NULL != h->client);
-  op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) + 
-                     sizeof (struct GNUNET_MessageHeader));
-  op->h = h;
-  op->msg = (const struct GNUNET_MessageHeader *) &op[1];
-  msg.size = htons (sizeof (msg));
-  msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
-  memcpy (&op[1], &msg, sizeof (msg));
-  GNUNET_CONTAINER_DLL_insert (h->op_head,
-                              h->op_tail,
-                              op);
+  if ( (NULL == h->op_head) ||
+       (GNUNET_MESSAGE_TYPE_IDENTITY_START != ntohs (h->op_head->msg->type)) )
+  {
+    op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
+                       sizeof (struct GNUNET_MessageHeader));
+    op->h = h;
+    op->msg = (const struct GNUNET_MessageHeader *) &op[1];
+    msg.size = htons (sizeof (msg));
+    msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
+    memcpy (&op[1], &msg, sizeof (msg));
+    GNUNET_CONTAINER_DLL_insert (h->op_head,
+                                h->op_tail,
+                                op);
+  }
   transmit_next (h);
   GNUNET_assert (NULL != h->th);
 }
@@ -558,7 +572,7 @@ reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  *
  * @param cfg the configuration to use
  * @param cb function to call on all identity events, can be NULL
- * @param cb_cls closure for 'cb'
+ * @param cb_cls closure for @a cb
  * @return handle to use
  */
 struct GNUNET_IDENTITY_Handle *
@@ -585,7 +599,7 @@ GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  * @param ego the ego
  * @return associated ECC key, valid as long as the ego is valid
  */
-const struct GNUNET_CRYPTO_EccPrivateKey *
+const struct GNUNET_CRYPTO_EcdsaPrivateKey *
 GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
 {
   return ego->pk;
@@ -600,9 +614,9 @@ GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
  */
 void
 GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
-                                   struct GNUNET_CRYPTO_EccPublicKey *pk)
+                                   struct GNUNET_CRYPTO_EcdsaPublicKey *pk)
 {
-  GNUNET_CRYPTO_ecc_key_get_public (ego->pk,
+  GNUNET_CRYPTO_ecdsa_key_get_public (ego->pk,
                                    pk);
 }
 
@@ -614,7 +628,7 @@ GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
  * @param id identity service to query
  * @param service_name for which service is an identity wanted
  * @param cb function to call with the result (will only be called once)
- * @param cb_cls closure for cb
+ * @param cb_cls closure for @a cb
  * @return handle to abort the operation
  */
 struct GNUNET_IDENTITY_Operation *
@@ -627,7 +641,7 @@ GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
   struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
   size_t slen;
 
-  slen = strlen (service_name) + 1; 
+  slen = strlen (service_name) + 1;
   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
   {
     GNUNET_break (0);
@@ -635,7 +649,7 @@ GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
   }
   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
                      sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
-                     slen);  
+                     slen);
   op->h = id;
   op->cb = cb;
   op->cls = cb_cls;
@@ -663,7 +677,7 @@ GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
  * @param service_name for which service is an identity set
  * @param ego new default identity to be set for this service
  * @param cont function to call once the operation finished
- * @param cont_cls closure for cont
+ * @param cont_cls closure for @a cont
  * @return handle to abort the operation
  */
 struct GNUNET_IDENTITY_Operation *
@@ -685,7 +699,7 @@ GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
   }
   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
                      sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
-                     slen);  
+                     slen);
   op->h = id;
   op->cont = cont;
   op->cls = cont_cls;
@@ -707,13 +721,13 @@ GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
 }
 
 
-/** 
+/**
  * Create a new identity with the given name.
  *
  * @param id identity service to use
  * @param name desired name
  * @param cont function to call with the result (will only be called once)
- * @param cont_cls closure for cont
+ * @param cont_cls closure for @a cont
  * @return handle to abort the operation
  */
 struct GNUNET_IDENTITY_Operation *
@@ -724,11 +738,11 @@ GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
 {
   struct GNUNET_IDENTITY_Operation *op;
   struct GNUNET_IDENTITY_CreateRequestMessage *crm;
-  struct GNUNET_CRYPTO_EccPrivateKey *pk;
+  struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
   size_t slen;
 
   slen = strlen (name) + 1;
-  pk = GNUNET_CRYPTO_ecc_key_create ();
+  pk = GNUNET_CRYPTO_ecdsa_key_create ();
 
   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
   {
@@ -738,7 +752,7 @@ GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
   }
   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
                      sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
-                     slen);  
+                     slen);
   op->h = id;
   op->cont = cont;
   op->cls = cont_cls;
@@ -761,14 +775,14 @@ GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
 }
 
 
-/** 
+/**
  * Renames an existing identity.
  *
  * @param id identity service to use
  * @param old_name old name
  * @param new_name desired new name
  * @param cb function to call with the result (will only be called once)
- * @param cb_cls closure for cb
+ * @param cb_cls closure for @a cb
  * @return handle to abort the operation
  */
 struct GNUNET_IDENTITY_Operation *
@@ -818,7 +832,7 @@ GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
 }
 
 
-/** 
+/**
  * Delete an existing identity.
  *
  * @param id identity service to use
@@ -845,7 +859,7 @@ GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
   }
   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
                      sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
-                     slen);  
+                     slen);
   op->h = id;
   op->cont = cb;
   op->cls = cb_cls;
@@ -883,6 +897,8 @@ GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
        (NULL == h->client) )
   {
     /* request not active, can simply remove */
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Client aborted non-head operation, simply removing it\n");
     GNUNET_CONTAINER_DLL_remove (h->op_head,
                                 h->op_tail,
                                 op);
@@ -892,6 +908,8 @@ GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
   if (NULL != h->th)
   {
     /* request active but not yet with service, can still abort */
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Client aborted head operation prior to transmission, aborting it\n");
     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
     h->th = NULL;
     GNUNET_CONTAINER_DLL_remove (h->op_head,
@@ -902,6 +920,8 @@ GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
     return;
   }
   /* request active with service, simply ensure continuations are not called */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Client aborted active request, NULLing continuation\n");
   op->cont = NULL;
   op->cb = NULL;
 }
@@ -923,10 +943,11 @@ free_ego (void *cls,
   struct GNUNET_IDENTITY_Handle *h = cls;
   struct GNUNET_IDENTITY_Ego *ego = value;
 
-  h->cb (h->cb_cls,
-        ego,
-        &ego->ctx,
-        NULL);
+  if (NULL != h->cb)
+    h->cb (h->cb_cls,
+          ego,
+          &ego->ctx,
+          NULL);
   GNUNET_free (ego->pk);
   GNUNET_free (ego->name);
   GNUNET_free (ego);
@@ -942,12 +963,13 @@ free_ego (void *cls,
 void
 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
 {
+  struct GNUNET_IDENTITY_Operation *op;
+
   GNUNET_assert (NULL != h);
-  GNUNET_assert (h->op_head == h->op_tail);
-  if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
+  if (h->reconnect_task != NULL)
   {
     GNUNET_SCHEDULER_cancel (h->reconnect_task);
-    h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    h->reconnect_task = NULL;
   }
   if (NULL != h->th)
   {
@@ -962,6 +984,14 @@ GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
     GNUNET_CONTAINER_multihashmap_destroy (h->egos);
     h->egos = NULL;
   }
+  while (NULL != (op = h->op_head))
+  {
+    GNUNET_break (NULL == op->cont);
+    GNUNET_CONTAINER_DLL_remove (h->op_head,
+                                h->op_tail,
+                                op);
+    GNUNET_free (op);
+  }
   if (NULL != h->client)
   {
     GNUNET_CLIENT_disconnect (h->client);