Don't go into infinite loop once only uncancellable head is left
[oweals/gnunet.git] / src / identity / identity_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public Liceidentity as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public Liceidentity for more details.
14
15      You should have received a copy of the GNU General Public Liceidentity
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file identity/identity_api.c
23  * @brief api to interact with the identity service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_identity_service.h"
31 #include "identity.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__)
34
35 /** 
36  * Handle for an ego.
37  */
38 struct GNUNET_IDENTITY_Ego
39 {
40   /**
41    * Private key associated with this ego.
42    */
43   struct GNUNET_CRYPTO_EccPrivateKey *pk;
44
45   /**
46    * Current name associated with this ego.
47    */
48   char *name;
49
50   /**
51    * Client context associated with this ego.
52    */
53   void *ctx;
54
55   /**
56    * Hash of the public key of this ego.
57    */
58   struct GNUNET_HashCode id;
59 };
60
61
62 /** 
63  * Handle for an operation with the identity service.
64  */
65 struct GNUNET_IDENTITY_Operation
66 {
67
68   /**
69    * Main identity handle.
70    */
71   struct GNUNET_IDENTITY_Handle *h;
72   
73   /**
74    * We keep operations in a DLL.
75    */
76   struct GNUNET_IDENTITY_Operation *next;
77
78   /**
79    * We keep operations in a DLL.
80    */
81   struct GNUNET_IDENTITY_Operation *prev;
82
83   /**
84    * Message to send to the identity service.
85    * Allocated at the end of this struct.
86    */
87   const struct GNUNET_MessageHeader *msg;
88
89   /**
90    * Continuation to invoke with the result of the transmission; 'cb'
91    * will be NULL in this case.
92    */
93   GNUNET_IDENTITY_Continuation cont;
94
95   /**
96    * Continuation to invoke with the result of the transmission for
97    * 'get' operations ('cont' will be NULL in this case).
98    */
99   GNUNET_IDENTITY_Callback cb;
100
101   /**
102    * Closure for 'cont' or 'cb'.
103    */
104   void *cls;
105
106 };
107
108
109 /**
110  * Handle for the service.
111  */
112 struct GNUNET_IDENTITY_Handle
113 {
114   /**
115    * Configuration to use.
116    */
117   const struct GNUNET_CONFIGURATION_Handle *cfg;
118
119   /**
120    * Socket (if available).
121    */
122   struct GNUNET_CLIENT_Connection *client;
123
124   /**
125    * Hash map from the hash of the public key to the
126    * respective 'GNUNET_IDENTITY_Ego' handle.
127    */
128   struct GNUNET_CONTAINER_MultiHashMap *egos;
129
130   /**
131    * Function to call when we receive updates.
132    */
133   GNUNET_IDENTITY_Callback cb;
134
135   /**
136    * Closure for 'cb'.
137    */
138   void *cb_cls;
139
140   /**
141    * Head of active operations.
142    */ 
143   struct GNUNET_IDENTITY_Operation *op_head;
144
145   /**
146    * Tail of active operations.
147    */ 
148   struct GNUNET_IDENTITY_Operation *op_tail;
149
150   /**
151    * Currently pending transmission request, or NULL for none.
152    */
153   struct GNUNET_CLIENT_TransmitHandle *th;
154
155   /**
156    * Task doing exponential back-off trying to reconnect.
157    */
158   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
159
160   /**
161    * Time for next connect retry.
162    */
163   struct GNUNET_TIME_Relative reconnect_delay;
164
165   /**
166    * Are we polling for incoming messages right now?
167    */
168   int in_receive;
169
170 };
171
172
173 /**
174  * Obtain the ego representing 'anonymous' users.
175  * 
176  * @return handle for the anonymous user, must not be freed
177  */
178 const struct GNUNET_IDENTITY_Ego *
179 GNUNET_IDENTITY_ego_get_anonymous ()
180 {
181   static struct GNUNET_IDENTITY_Ego anon;
182   struct GNUNET_CRYPTO_EccPublicSignKey pub;
183
184   if (NULL != anon.pk)
185     return &anon;
186   anon.pk = (struct GNUNET_CRYPTO_EccPrivateKey *) GNUNET_CRYPTO_ecc_key_get_anonymous ();
187   GNUNET_CRYPTO_ecc_key_get_public_for_signature (anon.pk,
188                                     &pub);
189   GNUNET_CRYPTO_hash (&pub, sizeof (pub), &anon.id);
190   return &anon;
191 }
192
193
194 /**
195  * Try again to connect to the identity service.
196  *
197  * @param cls handle to the identity service.
198  * @param tc scheduler context
199  */
200 static void
201 reconnect (void *cls,
202            const struct GNUNET_SCHEDULER_TaskContext *tc);
203
204
205 /**
206  * Reschedule a connect attempt to the service.
207  *
208  * @param h transport service to reconnect
209  */
210 static void
211 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
212 {
213   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
214
215   if (NULL != h->th)
216   {
217     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
218     h->th = NULL;
219   }
220   if (NULL != h->client)
221   {
222     GNUNET_CLIENT_disconnect (h->client);
223     h->client = NULL;
224   }
225   h->in_receive = GNUNET_NO;
226   LOG (GNUNET_ERROR_TYPE_DEBUG,
227        "Scheduling task to reconnect to identity service in %s.\n",
228        GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
229   h->reconnect_task =
230       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
231   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
232 }
233
234
235 /**
236  * Type of a function to call when we receive a message
237  * from the service.
238  *
239  * @param cls closure
240  * @param msg message received, NULL on timeout or fatal error
241  */
242 static void
243 message_handler (void *cls, 
244                  const struct GNUNET_MessageHeader *msg)
245 {
246   struct GNUNET_IDENTITY_Handle *h = cls;
247   struct GNUNET_IDENTITY_Operation *op;
248   struct GNUNET_IDENTITY_Ego *ego;
249   const struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
250   const struct GNUNET_IDENTITY_UpdateMessage *um;
251   const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
252   struct GNUNET_CRYPTO_EccPublicSignKey pub;
253   struct GNUNET_HashCode id;
254   const char *str;
255   uint16_t size;
256   uint16_t name_len;
257
258   if (NULL == msg)
259   {
260     reschedule_connect (h);
261     return;
262   }
263   LOG (GNUNET_ERROR_TYPE_DEBUG,
264        "Received message of type %d from identity service\n",
265        ntohs (msg->type));
266   size = ntohs (msg->size);
267   switch (ntohs (msg->type))
268   {
269   case GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE:
270     if (size < sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
271     {
272       GNUNET_break (0);
273       reschedule_connect (h);
274       return;
275     }
276     rcm = (const struct GNUNET_IDENTITY_ResultCodeMessage *) msg;
277     str = (const char *) &rcm[1];
278     if ( (size > sizeof (struct GNUNET_IDENTITY_ResultCodeMessage)) &&
279          ('\0' != str[size - sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) - 1]) )
280     {
281       GNUNET_break (0);
282       reschedule_connect (h);
283       return;
284     }
285     if (size == sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
286       str = NULL;
287
288     op = h->op_head;
289     GNUNET_CONTAINER_DLL_remove (h->op_head,
290                                  h->op_tail,
291                                  op);
292     GNUNET_CLIENT_receive (h->client, &message_handler, h,
293                            GNUNET_TIME_UNIT_FOREVER_REL);
294     if (NULL != op->cont)
295       op->cont (op->cls,
296                 str);
297     else if (NULL != op->cb)
298       op->cb (op->cls, NULL, NULL, NULL);
299     GNUNET_free (op);
300     break;
301   case GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE:
302     if (size < sizeof (struct GNUNET_IDENTITY_UpdateMessage))
303     {
304       GNUNET_break (0);
305       reschedule_connect (h);
306       return;
307     }
308     um = (const struct GNUNET_IDENTITY_UpdateMessage *) msg;
309     name_len = ntohs (um->name_len);
310     
311     str = (const char *) &um[1];
312     if ( (size != name_len + sizeof (struct GNUNET_IDENTITY_UpdateMessage)) ||
313          ( (0 != name_len) &&
314            ('\0' != str[name_len - 1])) )
315     {
316       GNUNET_break (0);
317       reschedule_connect (h);
318       return;
319     }
320     if (GNUNET_YES == ntohs (um->end_of_list))
321     {
322       /* end of initial list of data */
323       GNUNET_CLIENT_receive (h->client, &message_handler, h,
324                              GNUNET_TIME_UNIT_FOREVER_REL);
325       if (NULL != h->cb)
326         h->cb (h->cb_cls, NULL, NULL, NULL);
327       break;
328     }
329     GNUNET_CRYPTO_ecc_key_get_public_for_signature (&um->private_key,
330                                       &pub);
331     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
332     if (0 == name_len)
333       str = NULL;
334     else
335       str = (const char *) &um[1];
336     ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
337                                              &id);
338     if (NULL == ego)
339     {
340       /* ego was created */
341       if (NULL == str)
342       {
343         /* deletion of unknown ego? not allowed */
344         GNUNET_break (0);
345         reschedule_connect (h);
346         return;
347       }
348       ego = GNUNET_new (struct GNUNET_IDENTITY_Ego);
349       ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
350       *ego->pk = um->private_key;
351       ego->name = GNUNET_strdup (str);
352       ego->id = id;
353       GNUNET_assert (GNUNET_YES ==
354                      GNUNET_CONTAINER_multihashmap_put (h->egos,
355                                                         &ego->id,
356                                                         ego,
357                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
358     }
359     if (NULL == str)
360     {
361       /* ego was deleted */
362       GNUNET_assert (GNUNET_YES ==
363                      GNUNET_CONTAINER_multihashmap_remove (h->egos,
364                                                            &ego->id,
365                                                            ego));
366     }
367     else
368     {
369       /* ego changed name */
370       GNUNET_free (ego->name);
371       ego->name = GNUNET_strdup (str);
372     }    
373     GNUNET_CLIENT_receive (h->client, &message_handler, h,
374                            GNUNET_TIME_UNIT_FOREVER_REL);
375     /* inform application about change */
376     if (NULL != h->cb)
377       h->cb (h->cb_cls,
378              ego,
379              &ego->ctx,
380              str);
381     if (NULL == str)
382     {
383       GNUNET_free (ego->pk);
384       GNUNET_free (ego->name);
385       GNUNET_free (ego);
386     }
387     break;
388   case GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT:
389     if (size < sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
390     {
391       GNUNET_break (0);
392       reschedule_connect (h);
393       return;
394     }
395     sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) msg;
396     GNUNET_break (0 == ntohs (sdm->reserved));
397     name_len = ntohs (sdm->name_len);
398     str = (const char *) &sdm[1];
399     if ( (size != name_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage)) ||
400          ( (0 != name_len) &&
401            ('\0' != str[name_len - 1]) ) )
402     {
403       GNUNET_break (0);
404       reschedule_connect (h);
405       return;
406     }
407     /* Note: we know which service this should be for, so we're not
408        really using 'str' henceforth */
409     GNUNET_CRYPTO_ecc_key_get_public_for_signature (&sdm->private_key,
410                                       &pub);
411     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
412     ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
413                                              &id);
414     if (NULL == ego)
415     {
416       GNUNET_break (0);
417       reschedule_connect (h);
418       return;
419     }
420     op = h->op_head;
421     GNUNET_CONTAINER_DLL_remove (h->op_head,
422                                  h->op_tail,
423                                  op);
424     GNUNET_CLIENT_receive (h->client, &message_handler, h,
425                            GNUNET_TIME_UNIT_FOREVER_REL);
426     if (NULL != op->cb)
427       op->cb (op->cls,
428               ego,
429               &ego->ctx,
430               ego->name);
431     GNUNET_free (op);
432     break;
433   default:
434     GNUNET_break (0);
435     reschedule_connect (h);
436     return;
437   }
438 }
439
440
441 /**
442  * Schedule transmission of the next message from our queue.
443  *
444  * @param h identity handle
445  */
446 static void
447 transmit_next (struct GNUNET_IDENTITY_Handle *h);
448
449
450 /**
451  * Transmit next message to service.
452  *
453  * @param cls the `struct GNUNET_IDENTITY_Handle`.
454  * @param size number of bytes available in @a buf
455  * @param buf where to copy the message
456  * @return number of bytes copied to buf
457  */
458 static size_t
459 send_next_message (void *cls, 
460                    size_t size, 
461                    void *buf)
462 {
463   struct GNUNET_IDENTITY_Handle *h = cls;
464   struct GNUNET_IDENTITY_Operation *op = h->op_head;
465   size_t ret;
466   
467   h->th = NULL;
468   if (NULL == op)
469     return 0;
470   ret = ntohs (op->msg->size);
471   if (ret > size)
472   {
473     reschedule_connect (h);
474     return 0;
475   }  
476   LOG (GNUNET_ERROR_TYPE_DEBUG,
477        "Sending message of type %d to identity service\n",
478        ntohs (op->msg->type));
479   memcpy (buf, op->msg, ret);
480   if ( (NULL == op->cont) &&
481        (NULL == op->cb) )
482   {
483     GNUNET_CONTAINER_DLL_remove (h->op_head,
484                                  h->op_tail,
485                                  op);
486     GNUNET_free (op);
487     transmit_next (h);
488   }
489   if (GNUNET_NO == h->in_receive)
490   {
491     h->in_receive = GNUNET_YES;
492     GNUNET_CLIENT_receive (h->client,
493                            &message_handler, h,
494                            GNUNET_TIME_UNIT_FOREVER_REL);
495   }
496   return ret;
497 }
498
499
500 /**
501  * Schedule transmission of the next message from our queue.
502  *
503  * @param h identity handle
504  */
505 static void
506 transmit_next (struct GNUNET_IDENTITY_Handle *h)
507 {
508   struct GNUNET_IDENTITY_Operation *op = h->op_head;
509
510   GNUNET_assert (NULL == h->th);
511   if (NULL == op)
512     return;
513   if (NULL == h->client)
514     return;
515   h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
516                                                ntohs (op->msg->size),
517                                                GNUNET_TIME_UNIT_FOREVER_REL,
518                                                GNUNET_NO,
519                                                &send_next_message,
520                                                h);
521 }
522
523
524 /**
525  * Try again to connect to the identity service.
526  *
527  * @param cls handle to the identity service.
528  * @param tc scheduler context
529  */
530 static void
531 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
532 {
533   struct GNUNET_IDENTITY_Handle *h = cls;
534   struct GNUNET_IDENTITY_Operation *op;
535   struct GNUNET_MessageHeader msg;
536
537   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
538   LOG (GNUNET_ERROR_TYPE_DEBUG,
539        "Connecting to identity service.\n");
540   GNUNET_assert (NULL == h->client);
541   h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
542   GNUNET_assert (NULL != h->client);
543   if ( (NULL == h->op_head) ||
544        (GNUNET_MESSAGE_TYPE_IDENTITY_START != ntohs (h->op_head->msg->type)) )
545   {
546     op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) + 
547                         sizeof (struct GNUNET_MessageHeader));
548     op->h = h;
549     op->msg = (const struct GNUNET_MessageHeader *) &op[1];
550     msg.size = htons (sizeof (msg));
551     msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
552     memcpy (&op[1], &msg, sizeof (msg));
553     GNUNET_CONTAINER_DLL_insert (h->op_head,
554                                  h->op_tail,
555                                  op);
556   }
557   transmit_next (h);
558   GNUNET_assert (NULL != h->th);
559 }
560
561
562 /**
563  * Connect to the identity service.
564  *
565  * @param cfg the configuration to use
566  * @param cb function to call on all identity events, can be NULL
567  * @param cb_cls closure for @a cb
568  * @return handle to use
569  */
570 struct GNUNET_IDENTITY_Handle *
571 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
572                          GNUNET_IDENTITY_Callback cb,
573                          void *cb_cls)
574 {
575   struct GNUNET_IDENTITY_Handle *h;
576
577   h = GNUNET_new (struct GNUNET_IDENTITY_Handle);
578   h->cfg = cfg;
579   h->cb = cb;
580   h->cb_cls = cb_cls;
581   h->egos = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
582   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
583   h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
584   return h;
585 }
586
587
588 /**
589  * Obtain the ECC key associated with a ego.
590  *
591  * @param ego the ego
592  * @return associated ECC key, valid as long as the ego is valid
593  */
594 const struct GNUNET_CRYPTO_EccPrivateKey *
595 GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
596 {
597   return ego->pk;
598 }
599
600
601 /**
602  * Get the identifier (public key) of an ego.
603  *
604  * @param ego identity handle with the private key
605  * @param pk set to ego's public key
606  */
607 void
608 GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
609                                     struct GNUNET_CRYPTO_EccPublicSignKey *pk)
610 {
611   GNUNET_CRYPTO_ecc_key_get_public_for_signature (ego->pk,
612                                     pk);
613 }
614
615
616 /**
617  * Obtain the identity that is currently preferred/default
618  * for a service.
619  *
620  * @param id identity service to query
621  * @param service_name for which service is an identity wanted
622  * @param cb function to call with the result (will only be called once)
623  * @param cb_cls closure for @a cb
624  * @return handle to abort the operation
625  */
626 struct GNUNET_IDENTITY_Operation *
627 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
628                      const char *service_name,
629                      GNUNET_IDENTITY_Callback cb,
630                      void *cb_cls)
631 {
632   struct GNUNET_IDENTITY_Operation *op;
633   struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
634   size_t slen;
635
636   slen = strlen (service_name) + 1; 
637   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
638   {
639     GNUNET_break (0);
640     return NULL;
641   }
642   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
643                       sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
644                       slen);  
645   op->h = id;
646   op->cb = cb;
647   op->cls = cb_cls;
648   gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
649   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
650   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
651                             slen);
652   gdm->name_len = htons (slen);
653   gdm->reserved = htons (0);
654   memcpy (&gdm[1], service_name, slen);
655   op->msg = &gdm->header;
656   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
657                                     id->op_tail,
658                                     op);
659   if (NULL == id->th)
660     transmit_next (id);
661   return op;
662 }
663
664
665 /**
666  * Set the preferred/default identity for a service.
667  *
668  * @param id identity service to inform
669  * @param service_name for which service is an identity set
670  * @param ego new default identity to be set for this service
671  * @param cont function to call once the operation finished
672  * @param cont_cls closure for @a cont
673  * @return handle to abort the operation
674  */
675 struct GNUNET_IDENTITY_Operation *
676 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
677                      const char *service_name,
678                      struct GNUNET_IDENTITY_Ego *ego,
679                      GNUNET_IDENTITY_Continuation cont,
680                      void *cont_cls)
681 {
682   struct GNUNET_IDENTITY_Operation *op;
683   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
684   size_t slen;
685
686   slen = strlen (service_name) + 1;
687   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
688   {
689     GNUNET_break (0);
690     return NULL;
691   }
692   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
693                       sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
694                       slen);  
695   op->h = id;
696   op->cont = cont;
697   op->cls = cont_cls;
698   sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
699   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
700   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
701                             slen);
702   sdm->name_len = htons (slen);
703   sdm->reserved = htons (0);
704   sdm->private_key = *ego->pk;
705   memcpy (&sdm[1], service_name, slen);
706   op->msg = &sdm->header;
707   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
708                                     id->op_tail,
709                                     op);
710   if (NULL == id->th)
711     transmit_next (id);
712   return op;
713 }
714
715
716 /** 
717  * Create a new identity with the given name.
718  *
719  * @param id identity service to use
720  * @param name desired name
721  * @param cont function to call with the result (will only be called once)
722  * @param cont_cls closure for @a cont
723  * @return handle to abort the operation
724  */
725 struct GNUNET_IDENTITY_Operation *
726 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
727                         const char *name,
728                         GNUNET_IDENTITY_Continuation cont,
729                         void *cont_cls)
730 {
731   struct GNUNET_IDENTITY_Operation *op;
732   struct GNUNET_IDENTITY_CreateRequestMessage *crm;
733   struct GNUNET_CRYPTO_EccPrivateKey *pk;
734   size_t slen;
735
736   slen = strlen (name) + 1;
737   pk = GNUNET_CRYPTO_ecc_key_create ();
738
739   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
740   {
741     GNUNET_break (0);
742     GNUNET_free (pk);
743     return NULL;
744   }
745   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
746                       sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
747                       slen);  
748   op->h = id;
749   op->cont = cont;
750   op->cls = cont_cls;
751   crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
752   crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
753   crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
754                             slen);
755   crm->name_len = htons (slen);
756   crm->reserved = htons (0);
757   crm->private_key = *pk;
758   memcpy (&crm[1], name, slen);
759   op->msg = &crm->header;
760   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
761                                     id->op_tail,
762                                     op);
763   if (NULL == id->th)
764     transmit_next (id);
765   GNUNET_free (pk);
766   return op;
767 }
768
769
770 /** 
771  * Renames an existing identity.
772  *
773  * @param id identity service to use
774  * @param old_name old name
775  * @param new_name desired new name
776  * @param cb function to call with the result (will only be called once)
777  * @param cb_cls closure for @a cb
778  * @return handle to abort the operation
779  */
780 struct GNUNET_IDENTITY_Operation *
781 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
782                         const char *old_name,
783                         const char *new_name,
784                         GNUNET_IDENTITY_Continuation cb,
785                         void *cb_cls)
786 {
787   struct GNUNET_IDENTITY_Operation *op;
788   struct GNUNET_IDENTITY_RenameMessage *grm;
789   size_t slen_old;
790   size_t slen_new;
791   char *dst;
792
793   slen_old = strlen (old_name) + 1;
794   slen_new = strlen (new_name) + 1;
795   if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
796        (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
797        (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
798   {
799     GNUNET_break (0);
800     return NULL;
801   }
802   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
803                       sizeof (struct GNUNET_IDENTITY_RenameMessage) +
804                       slen_old + slen_new);
805   op->h = id;
806   op->cont = cb;
807   op->cls = cb_cls;
808   grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
809   grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
810   grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
811                             slen_old + slen_new);
812   grm->old_name_len = htons (slen_old);
813   grm->new_name_len = htons (slen_new);
814   dst = (char *) &grm[1];
815   memcpy (dst, old_name, slen_old);
816   memcpy (&dst[slen_old], new_name, slen_new);
817   op->msg = &grm->header;
818   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
819                                     id->op_tail,
820                                     op);
821   if (NULL == id->th)
822     transmit_next (id);
823   return op;
824 }
825
826
827 /** 
828  * Delete an existing identity.
829  *
830  * @param id identity service to use
831  * @param name name of the identity to delete
832  * @param cb function to call with the result (will only be called once)
833  * @param cb_cls closure for @a cb
834  * @return handle to abort the operation
835  */
836 struct GNUNET_IDENTITY_Operation *
837 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
838                         const char *name,
839                         GNUNET_IDENTITY_Continuation cb,
840                         void *cb_cls)
841 {
842   struct GNUNET_IDENTITY_Operation *op;
843   struct GNUNET_IDENTITY_DeleteMessage *gdm;
844   size_t slen;
845
846   slen = strlen (name) + 1;
847   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
848   {
849     GNUNET_break (0);
850     return NULL;
851   }
852   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
853                       sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
854                       slen);  
855   op->h = id;
856   op->cont = cb;
857   op->cls = cb_cls;
858   gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
859   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
860   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
861                             slen);
862   gdm->name_len = htons (slen);
863   gdm->reserved = htons (0);
864   memcpy (&gdm[1], name, slen);
865   op->msg = &gdm->header;
866   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
867                                     id->op_tail,
868                                     op);
869   if (NULL == id->th)
870     transmit_next (id);
871   return op;
872 }
873
874
875 /**
876  * Cancel an identity operation. Note that the operation MAY still
877  * be executed; this merely cancels the continuation; if the request
878  * was already transmitted, the service may still choose to complete
879  * the operation.
880  *
881  * @param op operation to cancel
882  */
883 void
884 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
885 {
886   struct GNUNET_IDENTITY_Handle *h = op->h;
887
888   if ( (h->op_head != op) ||
889        (NULL == h->client) )
890   {
891     /* request not active, can simply remove */
892     GNUNET_CONTAINER_DLL_remove (h->op_head,
893                                  h->op_tail,
894                                  op);
895     GNUNET_free (op);
896     return;
897   }
898   if (NULL != h->th)
899   {
900     /* request active but not yet with service, can still abort */
901     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
902     h->th = NULL;
903     GNUNET_CONTAINER_DLL_remove (h->op_head,
904                                  h->op_tail,
905                                  op);
906     GNUNET_free (op);
907     transmit_next (h);
908     return;
909   }
910   /* request active with service, simply ensure continuations are not called */
911   op->cont = NULL;
912   op->cb = NULL;
913 }
914
915
916 /**
917  * Free ego from hash map.
918  *
919  * @param cls identity service handle
920  * @param key unused
921  * @param value ego to free
922  * @return #GNUNET_OK (continue to iterate)
923  */
924 static int
925 free_ego (void *cls,
926           const struct GNUNET_HashCode *key,
927           void *value)
928 {
929   struct GNUNET_IDENTITY_Handle *h = cls;
930   struct GNUNET_IDENTITY_Ego *ego = value;
931
932   if (NULL != h->cb)
933     h->cb (h->cb_cls,
934            ego,
935            &ego->ctx,
936            NULL);
937   GNUNET_free (ego->pk);
938   GNUNET_free (ego->name);
939   GNUNET_free (ego);
940   return GNUNET_OK;
941 }
942
943
944 /**
945  * Disconnect from identity service
946  *
947  * @param h handle to destroy
948  */
949 void
950 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
951 {
952   struct GNUNET_IDENTITY_Operation *op;
953
954   GNUNET_assert (NULL != h);
955   while ((NULL != (op = h->op_head) && (NULL != op->next)))
956     GNUNET_IDENTITY_cancel (op);
957   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
958   {
959     GNUNET_SCHEDULER_cancel (h->reconnect_task);
960     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
961   }
962   if (NULL != h->th)
963   {
964     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
965     h->th = NULL;
966   }
967   if (NULL != h->egos)
968   {
969     GNUNET_CONTAINER_multihashmap_iterate (h->egos,
970                                            &free_ego,
971                                            h);
972     GNUNET_CONTAINER_multihashmap_destroy (h->egos);
973     h->egos = NULL;
974   }
975   if (NULL != h->client)
976   {
977     GNUNET_CLIENT_disconnect (h->client);
978     h->client = NULL;
979   }
980   GNUNET_free (h);
981 }
982
983 /* end of identity_api.c */