87ed361c410fc398e22af844b615670ac5ab2d41
[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 const struct GNUNET_IDENTITY_Ego *
177 GNUNET_IDENTITY_ego_get_anonymous ()
178 {
179   static struct GNUNET_IDENTITY_Ego anon;
180   struct GNUNET_CRYPTO_EccPublicKey pub;
181
182   if (NULL != anon.pk)
183     return &anon;
184   anon.pk = (struct GNUNET_CRYPTO_EccPrivateKey *) GNUNET_CRYPTO_ecc_key_get_anonymous ();
185   GNUNET_CRYPTO_ecc_key_get_public (anon.pk,
186                                     &pub);
187   GNUNET_CRYPTO_hash (&pub, sizeof (pub), &anon.id);
188   return &anon;
189 }
190
191
192 /**
193  * Try again to connect to the identity service.
194  *
195  * @param cls handle to the identity service.
196  * @param tc scheduler context
197  */
198 static void
199 reconnect (void *cls,
200            const struct GNUNET_SCHEDULER_TaskContext *tc);
201
202
203 /**
204  * Reschedule a connect attempt to the service.
205  *
206  * @param h transport service to reconnect
207  */
208 static void
209 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
210 {
211   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
212
213   if (NULL != h->th)
214   {
215     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
216     h->th = NULL;
217   }
218   if (NULL != h->client)
219   {
220     GNUNET_CLIENT_disconnect (h->client);
221     h->client = NULL;
222   }
223   h->in_receive = GNUNET_NO;
224   LOG (GNUNET_ERROR_TYPE_DEBUG,
225        "Scheduling task to reconnect to identity service in %s.\n",
226        GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
227   h->reconnect_task =
228       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
229   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
230 }
231
232
233 /**
234  * Type of a function to call when we receive a message
235  * from the service.
236  *
237  * @param cls closure
238  * @param msg message received, NULL on timeout or fatal error
239  */
240 static void
241 message_handler (void *cls, 
242                  const struct GNUNET_MessageHeader *msg)
243 {
244   struct GNUNET_IDENTITY_Handle *h = cls;
245   struct GNUNET_IDENTITY_Operation *op;
246   struct GNUNET_IDENTITY_Ego *ego;
247   const struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
248   const struct GNUNET_IDENTITY_UpdateMessage *um;
249   const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
250   struct GNUNET_CRYPTO_EccPublicKey pub;
251   struct GNUNET_HashCode id;
252   const char *str;
253   uint16_t size;
254   uint16_t name_len;
255
256   if (NULL == msg)
257   {
258     reschedule_connect (h);
259     return;
260   }
261   LOG (GNUNET_ERROR_TYPE_DEBUG,
262        "Received message of type %d from identity service\n",
263        ntohs (msg->type));
264   size = ntohs (msg->size);
265   switch (ntohs (msg->type))
266   {
267   case GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE:
268     if (size < sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
269     {
270       GNUNET_break (0);
271       reschedule_connect (h);
272       return;
273     }
274     rcm = (const struct GNUNET_IDENTITY_ResultCodeMessage *) msg;
275     str = (const char *) &rcm[1];
276     if ( (size > sizeof (struct GNUNET_IDENTITY_ResultCodeMessage)) &&
277          ('\0' != str[size - sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) - 1]) )
278     {
279       GNUNET_break (0);
280       reschedule_connect (h);
281       return;
282     }
283     if (size == sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
284       str = NULL;
285
286     op = h->op_head;
287     GNUNET_CONTAINER_DLL_remove (h->op_head,
288                                  h->op_tail,
289                                  op);
290     GNUNET_CLIENT_receive (h->client, &message_handler, h,
291                            GNUNET_TIME_UNIT_FOREVER_REL);
292     if (NULL != op->cont)
293       op->cont (op->cls,
294                 str);
295     else if (NULL != op->cb)
296       op->cb (op->cls, NULL, NULL, NULL);
297     GNUNET_free (op);
298     break;
299   case GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE:
300     if (size < sizeof (struct GNUNET_IDENTITY_UpdateMessage))
301     {
302       GNUNET_break (0);
303       reschedule_connect (h);
304       return;
305     }
306     um = (const struct GNUNET_IDENTITY_UpdateMessage *) msg;
307     name_len = ntohs (um->name_len);
308     
309     str = (const char *) &um[1];
310     if ( (size != name_len + sizeof (struct GNUNET_IDENTITY_UpdateMessage)) ||
311          ( (0 != name_len) &&
312            ('\0' != str[name_len - 1])) )
313     {
314       GNUNET_break (0);
315       reschedule_connect (h);
316       return;
317     }
318     if (GNUNET_YES == ntohs (um->end_of_list))
319     {
320       /* end of initial list of data */
321       GNUNET_CLIENT_receive (h->client, &message_handler, h,
322                              GNUNET_TIME_UNIT_FOREVER_REL);
323       if (NULL != h->cb)
324         h->cb (h->cb_cls, NULL, NULL, NULL);
325       break;
326     }
327     GNUNET_CRYPTO_ecc_key_get_public (&um->private_key,
328                                       &pub);
329     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
330     if (0 == name_len)
331       str = NULL;
332     else
333       str = (const char *) &um[1];
334     ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
335                                              &id);
336     if (NULL == ego)
337     {
338       /* ego was created */
339       if (NULL == str)
340       {
341         /* deletion of unknown ego? not allowed */
342         GNUNET_break (0);
343         reschedule_connect (h);
344         return;
345       }
346       ego = GNUNET_new (struct GNUNET_IDENTITY_Ego);
347       ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
348       *ego->pk = um->private_key;
349       ego->name = GNUNET_strdup (str);
350       ego->id = id;
351       GNUNET_assert (GNUNET_YES ==
352                      GNUNET_CONTAINER_multihashmap_put (h->egos,
353                                                         &ego->id,
354                                                         ego,
355                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
356     }
357     if (NULL == str)
358     {
359       /* ego was deleted */
360       GNUNET_assert (GNUNET_YES ==
361                      GNUNET_CONTAINER_multihashmap_remove (h->egos,
362                                                            &ego->id,
363                                                            ego));
364     }
365     else
366     {
367       /* ego changed name */
368       GNUNET_free (ego->name);
369       ego->name = GNUNET_strdup (str);
370     }    
371     GNUNET_CLIENT_receive (h->client, &message_handler, h,
372                            GNUNET_TIME_UNIT_FOREVER_REL);
373     /* inform application about change */
374     if (NULL != h->cb)
375       h->cb (h->cb_cls,
376              ego,
377              &ego->ctx,
378              str);
379     if (NULL == str)
380     {
381       GNUNET_free (ego->pk);
382       GNUNET_free (ego->name);
383       GNUNET_free (ego);
384     }
385     break;
386   case GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT:
387     if (size < sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
388     {
389       GNUNET_break (0);
390       reschedule_connect (h);
391       return;
392     }
393     sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) msg;
394     GNUNET_break (0 == ntohs (sdm->reserved));
395     name_len = ntohs (sdm->name_len);
396     str = (const char *) &sdm[1];
397     if ( (size != name_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage)) ||
398          ( (0 != name_len) &&
399            ('\0' != str[name_len - 1]) ) )
400     {
401       GNUNET_break (0);
402       reschedule_connect (h);
403       return;
404     }
405     /* Note: we know which service this should be for, so we're not
406        really using 'str' henceforth */
407     GNUNET_CRYPTO_ecc_key_get_public (&sdm->private_key,
408                                       &pub);
409     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
410     ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
411                                              &id);
412     if (NULL == ego)
413     {
414       GNUNET_break (0);
415       reschedule_connect (h);
416       return;
417     }
418     op = h->op_head;
419     GNUNET_CONTAINER_DLL_remove (h->op_head,
420                                  h->op_tail,
421                                  op);
422     GNUNET_CLIENT_receive (h->client, &message_handler, h,
423                            GNUNET_TIME_UNIT_FOREVER_REL);
424     if (NULL != op->cb)
425       op->cb (op->cls,
426               ego,
427               &ego->ctx,
428               ego->name);
429     GNUNET_free (op);
430     break;
431   default:
432     GNUNET_break (0);
433     reschedule_connect (h);
434     return;
435   }
436 }
437
438
439 /**
440  * Schedule transmission of the next message from our queue.
441  *
442  * @param h identity handle
443  */
444 static void
445 transmit_next (struct GNUNET_IDENTITY_Handle *h);
446
447
448 /**
449  * Transmit next message to service.
450  *
451  * @param cls the 'struct GNUNET_IDENTITY_Handle'.
452  * @param size number of bytes available in buf
453  * @param buf where to copy the message
454  * @return number of bytes copied to buf
455  */
456 static size_t
457 send_next_message (void *cls, 
458                    size_t size, 
459                    void *buf)
460 {
461   struct GNUNET_IDENTITY_Handle *h = cls;
462   struct GNUNET_IDENTITY_Operation *op = h->op_head;
463   size_t ret;
464   
465   h->th = NULL;
466   if (NULL == op)
467     return 0;
468   ret = ntohs (op->msg->size);
469   if (ret > size)
470   {
471     reschedule_connect (h);
472     return 0;
473   }  
474   LOG (GNUNET_ERROR_TYPE_DEBUG,
475        "Sending message of type %d to identity service\n",
476        ntohs (op->msg->type));
477   memcpy (buf, op->msg, ret);
478   if ( (NULL == op->cont) &&
479        (NULL == op->cb) )
480   {
481     GNUNET_CONTAINER_DLL_remove (h->op_head,
482                                  h->op_tail,
483                                  op);
484     GNUNET_free (op);
485     transmit_next (h);
486   }
487   if (GNUNET_NO == h->in_receive)
488   {
489     h->in_receive = GNUNET_YES;
490     GNUNET_CLIENT_receive (h->client,
491                            &message_handler, h,
492                            GNUNET_TIME_UNIT_FOREVER_REL);
493   }
494   return ret;
495 }
496
497
498 /**
499  * Schedule transmission of the next message from our queue.
500  *
501  * @param h identity handle
502  */
503 static void
504 transmit_next (struct GNUNET_IDENTITY_Handle *h)
505 {
506   struct GNUNET_IDENTITY_Operation *op = h->op_head;
507
508   GNUNET_assert (NULL == h->th);
509   if (NULL == op)
510     return;
511   if (NULL == h->client)
512     return;
513   h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
514                                                ntohs (op->msg->size),
515                                                GNUNET_TIME_UNIT_FOREVER_REL,
516                                                GNUNET_NO,
517                                                &send_next_message,
518                                                h);
519 }
520
521
522 /**
523  * Try again to connect to the identity service.
524  *
525  * @param cls handle to the identity service.
526  * @param tc scheduler context
527  */
528 static void
529 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
530 {
531   struct GNUNET_IDENTITY_Handle *h = cls;
532   struct GNUNET_IDENTITY_Operation *op;
533   struct GNUNET_MessageHeader msg;
534
535   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
536   LOG (GNUNET_ERROR_TYPE_DEBUG,
537        "Connecting to identity service.\n");
538   GNUNET_assert (NULL == h->client);
539   h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
540   GNUNET_assert (NULL != h->client);
541   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) + 
542                       sizeof (struct GNUNET_MessageHeader));
543   op->h = h;
544   op->msg = (const struct GNUNET_MessageHeader *) &op[1];
545   msg.size = htons (sizeof (msg));
546   msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
547   memcpy (&op[1], &msg, sizeof (msg));
548   GNUNET_CONTAINER_DLL_insert (h->op_head,
549                                h->op_tail,
550                                op);
551   transmit_next (h);
552   GNUNET_assert (NULL != h->th);
553 }
554
555
556 /**
557  * Connect to the identity service.
558  *
559  * @param cfg the configuration to use
560  * @param cb function to call on all identity events, can be NULL
561  * @param cb_cls closure for 'cb'
562  * @return handle to use
563  */
564 struct GNUNET_IDENTITY_Handle *
565 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
566                          GNUNET_IDENTITY_Callback cb,
567                          void *cb_cls)
568 {
569   struct GNUNET_IDENTITY_Handle *h;
570
571   h = GNUNET_new (struct GNUNET_IDENTITY_Handle);
572   h->cfg = cfg;
573   h->cb = cb;
574   h->cb_cls = cb_cls;
575   h->egos = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
576   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
577   h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
578   return h;
579 }
580
581
582 /**
583  * Obtain the ECC key associated with a ego.
584  *
585  * @param ego the ego
586  * @return associated ECC key, valid as long as the ego is valid
587  */
588 const struct GNUNET_CRYPTO_EccPrivateKey *
589 GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
590 {
591   return ego->pk;
592 }
593
594
595 /**
596  * Get the identifier (public key) of an ego.
597  *
598  * @param ego identity handle with the private key
599  * @param pk set to ego's public key
600  */
601 void
602 GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
603                                     struct GNUNET_CRYPTO_EccPublicKey *pk)
604 {
605   GNUNET_CRYPTO_ecc_key_get_public (ego->pk,
606                                     pk);
607 }
608
609
610 /**
611  * Obtain the identity that is currently preferred/default
612  * for a service.
613  *
614  * @param id identity service to query
615  * @param service_name for which service is an identity wanted
616  * @param cb function to call with the result (will only be called once)
617  * @param cb_cls closure for cb
618  * @return handle to abort the operation
619  */
620 struct GNUNET_IDENTITY_Operation *
621 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
622                      const char *service_name,
623                      GNUNET_IDENTITY_Callback cb,
624                      void *cb_cls)
625 {
626   struct GNUNET_IDENTITY_Operation *op;
627   struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
628   size_t slen;
629
630   slen = strlen (service_name) + 1; 
631   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
632   {
633     GNUNET_break (0);
634     return NULL;
635   }
636   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
637                       sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
638                       slen);  
639   op->h = id;
640   op->cb = cb;
641   op->cls = cb_cls;
642   gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
643   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
644   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
645                             slen);
646   gdm->name_len = htons (slen);
647   gdm->reserved = htons (0);
648   memcpy (&gdm[1], service_name, slen);
649   op->msg = &gdm->header;
650   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
651                                     id->op_tail,
652                                     op);
653   if (NULL == id->th)
654     transmit_next (id);
655   return op;
656 }
657
658
659 /**
660  * Set the preferred/default identity for a service.
661  *
662  * @param id identity service to inform
663  * @param service_name for which service is an identity set
664  * @param ego new default identity to be set for this service
665  * @param cont function to call once the operation finished
666  * @param cont_cls closure for cont
667  * @return handle to abort the operation
668  */
669 struct GNUNET_IDENTITY_Operation *
670 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
671                      const char *service_name,
672                      struct GNUNET_IDENTITY_Ego *ego,
673                      GNUNET_IDENTITY_Continuation cont,
674                      void *cont_cls)
675 {
676   struct GNUNET_IDENTITY_Operation *op;
677   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
678   size_t slen;
679
680   slen = strlen (service_name) + 1;
681   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
682   {
683     GNUNET_break (0);
684     return NULL;
685   }
686   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
687                       sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
688                       slen);  
689   op->h = id;
690   op->cont = cont;
691   op->cls = cont_cls;
692   sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
693   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
694   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
695                             slen);
696   sdm->name_len = htons (slen);
697   sdm->reserved = htons (0);
698   sdm->private_key = *ego->pk;
699   memcpy (&sdm[1], service_name, slen);
700   op->msg = &sdm->header;
701   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
702                                     id->op_tail,
703                                     op);
704   if (NULL == id->th)
705     transmit_next (id);
706   return op;
707 }
708
709
710 /** 
711  * Create a new identity with the given name.
712  *
713  * @param id identity service to use
714  * @param name desired name
715  * @param cont function to call with the result (will only be called once)
716  * @param cont_cls closure for cont
717  * @return handle to abort the operation
718  */
719 struct GNUNET_IDENTITY_Operation *
720 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
721                         const char *name,
722                         GNUNET_IDENTITY_Continuation cont,
723                         void *cont_cls)
724 {
725   struct GNUNET_IDENTITY_Operation *op;
726   struct GNUNET_IDENTITY_CreateRequestMessage *crm;
727   struct GNUNET_CRYPTO_EccPrivateKey *pk;
728   size_t slen;
729
730   slen = strlen (name) + 1;
731   pk = GNUNET_CRYPTO_ecc_key_create ();
732
733   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
734   {
735     GNUNET_break (0);
736     GNUNET_free (pk);
737     return NULL;
738   }
739   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
740                       sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
741                       slen);  
742   op->h = id;
743   op->cont = cont;
744   op->cls = cont_cls;
745   crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
746   crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
747   crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
748                             slen);
749   crm->name_len = htons (slen);
750   crm->reserved = htons (0);
751   crm->private_key = *pk;
752   memcpy (&crm[1], name, slen);
753   op->msg = &crm->header;
754   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
755                                     id->op_tail,
756                                     op);
757   if (NULL == id->th)
758     transmit_next (id);
759   GNUNET_free (pk);
760   return op;
761 }
762
763
764 /** 
765  * Renames an existing identity.
766  *
767  * @param id identity service to use
768  * @param old_name old name
769  * @param new_name desired new name
770  * @param cb function to call with the result (will only be called once)
771  * @param cb_cls closure for cb
772  * @return handle to abort the operation
773  */
774 struct GNUNET_IDENTITY_Operation *
775 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
776                         const char *old_name,
777                         const char *new_name,
778                         GNUNET_IDENTITY_Continuation cb,
779                         void *cb_cls)
780 {
781   struct GNUNET_IDENTITY_Operation *op;
782   struct GNUNET_IDENTITY_RenameMessage *grm;
783   size_t slen_old;
784   size_t slen_new;
785   char *dst;
786
787   slen_old = strlen (old_name) + 1;
788   slen_new = strlen (new_name) + 1;
789   if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
790        (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
791        (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
792   {
793     GNUNET_break (0);
794     return NULL;
795   }
796   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
797                       sizeof (struct GNUNET_IDENTITY_RenameMessage) +
798                       slen_old + slen_new);
799   op->h = id;
800   op->cont = cb;
801   op->cls = cb_cls;
802   grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
803   grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
804   grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
805                             slen_old + slen_new);
806   grm->old_name_len = htons (slen_old);
807   grm->new_name_len = htons (slen_new);
808   dst = (char *) &grm[1];
809   memcpy (dst, old_name, slen_old);
810   memcpy (&dst[slen_old], new_name, slen_new);
811   op->msg = &grm->header;
812   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
813                                     id->op_tail,
814                                     op);
815   if (NULL == id->th)
816     transmit_next (id);
817   return op;
818 }
819
820
821 /** 
822  * Delete an existing identity.
823  *
824  * @param id identity service to use
825  * @param name name of the identity to delete
826  * @param cb function to call with the result (will only be called once)
827  * @param cb_cls closure for @a cb
828  * @return handle to abort the operation
829  */
830 struct GNUNET_IDENTITY_Operation *
831 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
832                         const char *name,
833                         GNUNET_IDENTITY_Continuation cb,
834                         void *cb_cls)
835 {
836   struct GNUNET_IDENTITY_Operation *op;
837   struct GNUNET_IDENTITY_DeleteMessage *gdm;
838   size_t slen;
839
840   slen = strlen (name) + 1;
841   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
842   {
843     GNUNET_break (0);
844     return NULL;
845   }
846   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
847                       sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
848                       slen);  
849   op->h = id;
850   op->cont = cb;
851   op->cls = cb_cls;
852   gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
853   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
854   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
855                             slen);
856   gdm->name_len = htons (slen);
857   gdm->reserved = htons (0);
858   memcpy (&gdm[1], name, slen);
859   op->msg = &gdm->header;
860   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
861                                     id->op_tail,
862                                     op);
863   if (NULL == id->th)
864     transmit_next (id);
865   return op;
866 }
867
868
869 /**
870  * Cancel an identity operation. Note that the operation MAY still
871  * be executed; this merely cancels the continuation; if the request
872  * was already transmitted, the service may still choose to complete
873  * the operation.
874  *
875  * @param op operation to cancel
876  */
877 void
878 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
879 {
880   struct GNUNET_IDENTITY_Handle *h = op->h;
881
882   if ( (h->op_head != op) ||
883        (NULL == h->client) )
884   {
885     /* request not active, can simply remove */
886     GNUNET_CONTAINER_DLL_remove (h->op_head,
887                                  h->op_tail,
888                                  op);
889     GNUNET_free (op);
890     return;
891   }
892   if (NULL != h->th)
893   {
894     /* request active but not yet with service, can still abort */
895     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
896     h->th = NULL;
897     GNUNET_CONTAINER_DLL_remove (h->op_head,
898                                  h->op_tail,
899                                  op);
900     GNUNET_free (op);
901     transmit_next (h);
902     return;
903   }
904   /* request active with service, simply ensure continuations are not called */
905   op->cont = NULL;
906   op->cb = NULL;
907 }
908
909
910 /**
911  * Free ego from hash map.
912  *
913  * @param cls identity service handle
914  * @param key unused
915  * @param value ego to free
916  * @return #GNUNET_OK (continue to iterate)
917  */
918 static int
919 free_ego (void *cls,
920           const struct GNUNET_HashCode *key,
921           void *value)
922 {
923   struct GNUNET_IDENTITY_Handle *h = cls;
924   struct GNUNET_IDENTITY_Ego *ego = value;
925
926   h->cb (h->cb_cls,
927          ego,
928          &ego->ctx,
929          NULL);
930   GNUNET_free (ego->pk);
931   GNUNET_free (ego->name);
932   GNUNET_free (ego);
933   return GNUNET_OK;
934 }
935
936
937 /**
938  * Disconnect from identity service
939  *
940  * @param h handle to destroy
941  */
942 void
943 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
944 {
945   GNUNET_assert (NULL != h);
946   GNUNET_assert (h->op_head == h->op_tail);
947   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
948   {
949     GNUNET_SCHEDULER_cancel (h->reconnect_task);
950     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
951   }
952   if (NULL != h->th)
953   {
954     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
955     h->th = NULL;
956   }
957   if (NULL != h->egos)
958   {
959     GNUNET_CONTAINER_multihashmap_iterate (h->egos,
960                                            &free_ego,
961                                            h);
962     GNUNET_CONTAINER_multihashmap_destroy (h->egos);
963     h->egos = NULL;
964   }
965   if (NULL != h->client)
966   {
967     GNUNET_CLIENT_disconnect (h->client);
968     h->client = NULL;
969   }
970   GNUNET_free (h);
971 }
972
973 /* end of identity_api.c */