splitting 'struct GNUNET_CRYPTO_EccPublicKey' into one struct for signing and another...
[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   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) + 
544                       sizeof (struct GNUNET_MessageHeader));
545   op->h = h;
546   op->msg = (const struct GNUNET_MessageHeader *) &op[1];
547   msg.size = htons (sizeof (msg));
548   msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
549   memcpy (&op[1], &msg, sizeof (msg));
550   GNUNET_CONTAINER_DLL_insert (h->op_head,
551                                h->op_tail,
552                                op);
553   transmit_next (h);
554   GNUNET_assert (NULL != h->th);
555 }
556
557
558 /**
559  * Connect to the identity service.
560  *
561  * @param cfg the configuration to use
562  * @param cb function to call on all identity events, can be NULL
563  * @param cb_cls closure for @a cb
564  * @return handle to use
565  */
566 struct GNUNET_IDENTITY_Handle *
567 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
568                          GNUNET_IDENTITY_Callback cb,
569                          void *cb_cls)
570 {
571   struct GNUNET_IDENTITY_Handle *h;
572
573   h = GNUNET_new (struct GNUNET_IDENTITY_Handle);
574   h->cfg = cfg;
575   h->cb = cb;
576   h->cb_cls = cb_cls;
577   h->egos = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
578   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
579   h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
580   return h;
581 }
582
583
584 /**
585  * Obtain the ECC key associated with a ego.
586  *
587  * @param ego the ego
588  * @return associated ECC key, valid as long as the ego is valid
589  */
590 const struct GNUNET_CRYPTO_EccPrivateKey *
591 GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
592 {
593   return ego->pk;
594 }
595
596
597 /**
598  * Get the identifier (public key) of an ego.
599  *
600  * @param ego identity handle with the private key
601  * @param pk set to ego's public key
602  */
603 void
604 GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
605                                     struct GNUNET_CRYPTO_EccPublicSignKey *pk)
606 {
607   GNUNET_CRYPTO_ecc_key_get_public_for_signature (ego->pk,
608                                     pk);
609 }
610
611
612 /**
613  * Obtain the identity that is currently preferred/default
614  * for a service.
615  *
616  * @param id identity service to query
617  * @param service_name for which service is an identity wanted
618  * @param cb function to call with the result (will only be called once)
619  * @param cb_cls closure for @a cb
620  * @return handle to abort the operation
621  */
622 struct GNUNET_IDENTITY_Operation *
623 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
624                      const char *service_name,
625                      GNUNET_IDENTITY_Callback cb,
626                      void *cb_cls)
627 {
628   struct GNUNET_IDENTITY_Operation *op;
629   struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
630   size_t slen;
631
632   slen = strlen (service_name) + 1; 
633   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
634   {
635     GNUNET_break (0);
636     return NULL;
637   }
638   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
639                       sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
640                       slen);  
641   op->h = id;
642   op->cb = cb;
643   op->cls = cb_cls;
644   gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
645   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
646   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
647                             slen);
648   gdm->name_len = htons (slen);
649   gdm->reserved = htons (0);
650   memcpy (&gdm[1], service_name, slen);
651   op->msg = &gdm->header;
652   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
653                                     id->op_tail,
654                                     op);
655   if (NULL == id->th)
656     transmit_next (id);
657   return op;
658 }
659
660
661 /**
662  * Set the preferred/default identity for a service.
663  *
664  * @param id identity service to inform
665  * @param service_name for which service is an identity set
666  * @param ego new default identity to be set for this service
667  * @param cont function to call once the operation finished
668  * @param cont_cls closure for @a cont
669  * @return handle to abort the operation
670  */
671 struct GNUNET_IDENTITY_Operation *
672 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
673                      const char *service_name,
674                      struct GNUNET_IDENTITY_Ego *ego,
675                      GNUNET_IDENTITY_Continuation cont,
676                      void *cont_cls)
677 {
678   struct GNUNET_IDENTITY_Operation *op;
679   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
680   size_t slen;
681
682   slen = strlen (service_name) + 1;
683   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
684   {
685     GNUNET_break (0);
686     return NULL;
687   }
688   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
689                       sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
690                       slen);  
691   op->h = id;
692   op->cont = cont;
693   op->cls = cont_cls;
694   sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
695   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
696   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
697                             slen);
698   sdm->name_len = htons (slen);
699   sdm->reserved = htons (0);
700   sdm->private_key = *ego->pk;
701   memcpy (&sdm[1], service_name, slen);
702   op->msg = &sdm->header;
703   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
704                                     id->op_tail,
705                                     op);
706   if (NULL == id->th)
707     transmit_next (id);
708   return op;
709 }
710
711
712 /** 
713  * Create a new identity with the given name.
714  *
715  * @param id identity service to use
716  * @param name desired name
717  * @param cont function to call with the result (will only be called once)
718  * @param cont_cls closure for @a cont
719  * @return handle to abort the operation
720  */
721 struct GNUNET_IDENTITY_Operation *
722 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
723                         const char *name,
724                         GNUNET_IDENTITY_Continuation cont,
725                         void *cont_cls)
726 {
727   struct GNUNET_IDENTITY_Operation *op;
728   struct GNUNET_IDENTITY_CreateRequestMessage *crm;
729   struct GNUNET_CRYPTO_EccPrivateKey *pk;
730   size_t slen;
731
732   slen = strlen (name) + 1;
733   pk = GNUNET_CRYPTO_ecc_key_create ();
734
735   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
736   {
737     GNUNET_break (0);
738     GNUNET_free (pk);
739     return NULL;
740   }
741   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
742                       sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
743                       slen);  
744   op->h = id;
745   op->cont = cont;
746   op->cls = cont_cls;
747   crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
748   crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
749   crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
750                             slen);
751   crm->name_len = htons (slen);
752   crm->reserved = htons (0);
753   crm->private_key = *pk;
754   memcpy (&crm[1], name, slen);
755   op->msg = &crm->header;
756   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
757                                     id->op_tail,
758                                     op);
759   if (NULL == id->th)
760     transmit_next (id);
761   GNUNET_free (pk);
762   return op;
763 }
764
765
766 /** 
767  * Renames an existing identity.
768  *
769  * @param id identity service to use
770  * @param old_name old name
771  * @param new_name desired new name
772  * @param cb function to call with the result (will only be called once)
773  * @param cb_cls closure for @a cb
774  * @return handle to abort the operation
775  */
776 struct GNUNET_IDENTITY_Operation *
777 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
778                         const char *old_name,
779                         const char *new_name,
780                         GNUNET_IDENTITY_Continuation cb,
781                         void *cb_cls)
782 {
783   struct GNUNET_IDENTITY_Operation *op;
784   struct GNUNET_IDENTITY_RenameMessage *grm;
785   size_t slen_old;
786   size_t slen_new;
787   char *dst;
788
789   slen_old = strlen (old_name) + 1;
790   slen_new = strlen (new_name) + 1;
791   if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
792        (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
793        (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
794   {
795     GNUNET_break (0);
796     return NULL;
797   }
798   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
799                       sizeof (struct GNUNET_IDENTITY_RenameMessage) +
800                       slen_old + slen_new);
801   op->h = id;
802   op->cont = cb;
803   op->cls = cb_cls;
804   grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
805   grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
806   grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
807                             slen_old + slen_new);
808   grm->old_name_len = htons (slen_old);
809   grm->new_name_len = htons (slen_new);
810   dst = (char *) &grm[1];
811   memcpy (dst, old_name, slen_old);
812   memcpy (&dst[slen_old], new_name, slen_new);
813   op->msg = &grm->header;
814   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
815                                     id->op_tail,
816                                     op);
817   if (NULL == id->th)
818     transmit_next (id);
819   return op;
820 }
821
822
823 /** 
824  * Delete an existing identity.
825  *
826  * @param id identity service to use
827  * @param name name of the identity to delete
828  * @param cb function to call with the result (will only be called once)
829  * @param cb_cls closure for @a cb
830  * @return handle to abort the operation
831  */
832 struct GNUNET_IDENTITY_Operation *
833 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
834                         const char *name,
835                         GNUNET_IDENTITY_Continuation cb,
836                         void *cb_cls)
837 {
838   struct GNUNET_IDENTITY_Operation *op;
839   struct GNUNET_IDENTITY_DeleteMessage *gdm;
840   size_t slen;
841
842   slen = strlen (name) + 1;
843   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
844   {
845     GNUNET_break (0);
846     return NULL;
847   }
848   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
849                       sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
850                       slen);  
851   op->h = id;
852   op->cont = cb;
853   op->cls = cb_cls;
854   gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
855   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
856   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
857                             slen);
858   gdm->name_len = htons (slen);
859   gdm->reserved = htons (0);
860   memcpy (&gdm[1], name, slen);
861   op->msg = &gdm->header;
862   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
863                                     id->op_tail,
864                                     op);
865   if (NULL == id->th)
866     transmit_next (id);
867   return op;
868 }
869
870
871 /**
872  * Cancel an identity operation. Note that the operation MAY still
873  * be executed; this merely cancels the continuation; if the request
874  * was already transmitted, the service may still choose to complete
875  * the operation.
876  *
877  * @param op operation to cancel
878  */
879 void
880 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
881 {
882   struct GNUNET_IDENTITY_Handle *h = op->h;
883
884   if ( (h->op_head != op) ||
885        (NULL == h->client) )
886   {
887     /* request not active, can simply remove */
888     GNUNET_CONTAINER_DLL_remove (h->op_head,
889                                  h->op_tail,
890                                  op);
891     GNUNET_free (op);
892     return;
893   }
894   if (NULL != h->th)
895   {
896     /* request active but not yet with service, can still abort */
897     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
898     h->th = NULL;
899     GNUNET_CONTAINER_DLL_remove (h->op_head,
900                                  h->op_tail,
901                                  op);
902     GNUNET_free (op);
903     transmit_next (h);
904     return;
905   }
906   /* request active with service, simply ensure continuations are not called */
907   op->cont = NULL;
908   op->cb = NULL;
909 }
910
911
912 /**
913  * Free ego from hash map.
914  *
915  * @param cls identity service handle
916  * @param key unused
917  * @param value ego to free
918  * @return #GNUNET_OK (continue to iterate)
919  */
920 static int
921 free_ego (void *cls,
922           const struct GNUNET_HashCode *key,
923           void *value)
924 {
925   struct GNUNET_IDENTITY_Handle *h = cls;
926   struct GNUNET_IDENTITY_Ego *ego = value;
927
928   if (NULL != h->cb)
929     h->cb (h->cb_cls,
930            ego,
931            &ego->ctx,
932            NULL);
933   GNUNET_free (ego->pk);
934   GNUNET_free (ego->name);
935   GNUNET_free (ego);
936   return GNUNET_OK;
937 }
938
939
940 /**
941  * Disconnect from identity service
942  *
943  * @param h handle to destroy
944  */
945 void
946 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
947 {
948   GNUNET_assert (NULL != h);
949   GNUNET_assert (h->op_head == h->op_tail);
950   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
951   {
952     GNUNET_SCHEDULER_cancel (h->reconnect_task);
953     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
954   }
955   if (NULL != h->th)
956   {
957     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
958     h->th = NULL;
959   }
960   if (NULL != h->egos)
961   {
962     GNUNET_CONTAINER_multihashmap_iterate (h->egos,
963                                            &free_ego,
964                                            h);
965     GNUNET_CONTAINER_multihashmap_destroy (h->egos);
966     h->egos = NULL;
967   }
968   if (NULL != h->client)
969   {
970     GNUNET_CLIENT_disconnect (h->client);
971     h->client = NULL;
972   }
973   GNUNET_free (h);
974 }
975
976 /* end of identity_api.c */