-more convenient API to lookup egos
[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 a 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 network size estimation service.
194  *
195  * @param cls the handle to the transport 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_free (ego->name);
423     GNUNET_CLIENT_receive (h->client, &message_handler, h,
424                            GNUNET_TIME_UNIT_FOREVER_REL);
425     if (NULL != op->cb)
426       op->cb (op->cls,
427               ego,
428               &ego->ctx,
429               ego->name);
430     GNUNET_free (op);
431     break;
432   default:
433     GNUNET_break (0);
434     reschedule_connect (h);
435     return;
436   }
437 }
438
439
440 /**
441  * Schedule transmission of the next message from our queue.
442  *
443  * @param h identity handle
444  */
445 static void
446 transmit_next (struct GNUNET_IDENTITY_Handle *h);
447
448
449 /**
450  * Transmit next message to service.
451  *
452  * @param cls the 'struct GNUNET_IDENTITY_Handle'.
453  * @param size number of bytes available in buf
454  * @param buf where to copy the message
455  * @return number of bytes copied to buf
456  */
457 static size_t
458 send_next_message (void *cls, 
459                    size_t size, 
460                    void *buf)
461 {
462   struct GNUNET_IDENTITY_Handle *h = cls;
463   struct GNUNET_IDENTITY_Operation *op = h->op_head;
464   size_t ret;
465   
466   h->th = NULL;
467   if (NULL == op)
468     return 0;
469   ret = ntohs (op->msg->size);
470   if (ret > size)
471   {
472     reschedule_connect (h);
473     return 0;
474   }  
475   LOG (GNUNET_ERROR_TYPE_DEBUG,
476        "Sending message of type %d to identity service\n",
477        ntohs (op->msg->type));
478   memcpy (buf, op->msg, ret);
479   if ( (NULL == op->cont) &&
480        (NULL == op->cb) )
481   {
482     GNUNET_CONTAINER_DLL_remove (h->op_head,
483                                  h->op_tail,
484                                  op);
485     GNUNET_free (op);
486     transmit_next (h);
487   }
488   if (GNUNET_NO == h->in_receive)
489   {
490     h->in_receive = GNUNET_YES;
491     GNUNET_CLIENT_receive (h->client,
492                            &message_handler, h,
493                            GNUNET_TIME_UNIT_FOREVER_REL);
494   }
495   return ret;
496 }
497
498
499 /**
500  * Schedule transmission of the next message from our queue.
501  *
502  * @param h identity handle
503  */
504 static void
505 transmit_next (struct GNUNET_IDENTITY_Handle *h)
506 {
507   struct GNUNET_IDENTITY_Operation *op = h->op_head;
508
509   GNUNET_assert (NULL == h->th);
510   if (NULL == op)
511     return;
512   if (NULL == h->client)
513     return;
514   h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
515                                                ntohs (op->msg->size),
516                                                GNUNET_TIME_UNIT_FOREVER_REL,
517                                                GNUNET_NO,
518                                                &send_next_message,
519                                                h);
520 }
521
522
523 /**
524  * Try again to connect to network size estimation service.
525  *
526  * @param cls the handle to the transport service
527  * @param tc scheduler context
528  */
529 static void
530 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
531 {
532   struct GNUNET_IDENTITY_Handle *h = cls;
533   struct GNUNET_IDENTITY_Operation *op;
534   struct GNUNET_MessageHeader msg;
535
536   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
537   LOG (GNUNET_ERROR_TYPE_DEBUG,
538        "Connecting to identity service.\n");
539   GNUNET_assert (NULL == h->client);
540   h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
541   GNUNET_assert (NULL != h->client);
542   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) + 
543                       sizeof (struct GNUNET_MessageHeader));
544   op->h = h;
545   op->msg = (const struct GNUNET_MessageHeader *) &op[1];
546   msg.size = htons (sizeof (msg));
547   msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
548   memcpy (&op[1], &msg, sizeof (msg));
549   GNUNET_CONTAINER_DLL_insert (h->op_head,
550                                h->op_tail,
551                                op);
552   transmit_next (h);
553   GNUNET_assert (NULL != h->th);
554 }
555
556
557 /**
558  * Connect to the identity service.
559  *
560  * @param cfg the configuration to use
561  * @param cb function to call on all identity events, can be NULL
562  * @param cb_cls closure for 'cb'
563  * @return handle to use
564  */
565 struct GNUNET_IDENTITY_Handle *
566 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
567                          GNUNET_IDENTITY_Callback cb,
568                          void *cb_cls)
569 {
570   struct GNUNET_IDENTITY_Handle *h;
571
572   h = GNUNET_new (struct GNUNET_IDENTITY_Handle);
573   h->cfg = cfg;
574   h->cb = cb;
575   h->cb_cls = cb_cls;
576   h->egos = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
577   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
578   h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
579   return h;
580 }
581
582
583 /**
584  * Obtain the ECC key associated with a ego.
585  *
586  * @param ego the ego
587  * @return associated ECC key, valid as long as the ego is valid
588  */
589 const struct GNUNET_CRYPTO_EccPrivateKey *
590 GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
591 {
592   return ego->pk;
593 }
594
595
596 /**
597  * Get the identifier (public key) of an ego.
598  *
599  * @param ego identity handle with the private key
600  * @param pk set to ego's public key
601  */
602 void
603 GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
604                                     struct GNUNET_CRYPTO_EccPublicKey *pk)
605 {
606   GNUNET_CRYPTO_ecc_key_get_public (ego->pk,
607                                     pk);
608 }
609
610
611 /**
612  * Obtain the identity that is currently preferred/default
613  * for a service.
614  *
615  * @param id identity service to query
616  * @param service_name for which service is an identity wanted
617  * @param cb function to call with the result (will only be called once)
618  * @param cb_cls closure for cb
619  * @return handle to abort the operation
620  */
621 struct GNUNET_IDENTITY_Operation *
622 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
623                      const char *service_name,
624                      GNUNET_IDENTITY_Callback cb,
625                      void *cb_cls)
626 {
627   struct GNUNET_IDENTITY_Operation *op;
628   struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
629   size_t slen;
630
631   slen = strlen (service_name) + 1; 
632   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
633   {
634     GNUNET_break (0);
635     return NULL;
636   }
637   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
638                       sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
639                       slen);  
640   op->h = id;
641   op->cb = cb;
642   op->cls = cb_cls;
643   gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
644   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
645   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
646                             slen);
647   gdm->name_len = htons (slen);
648   gdm->reserved = htons (0);
649   memcpy (&gdm[1], service_name, slen);
650   op->msg = &gdm->header;
651   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
652                                     id->op_tail,
653                                     op);
654   if (NULL == id->th)
655     transmit_next (id);
656   return op;
657 }
658
659
660 /**
661  * Set the preferred/default identity for a service.
662  *
663  * @param id identity service to inform
664  * @param service_name for which service is an identity set
665  * @param ego new default identity to be set for this service
666  * @param cont function to call once the operation finished
667  * @param cont_cls closure for cont
668  * @return handle to abort the operation
669  */
670 struct GNUNET_IDENTITY_Operation *
671 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
672                      const char *service_name,
673                      struct GNUNET_IDENTITY_Ego *ego,
674                      GNUNET_IDENTITY_Continuation cont,
675                      void *cont_cls)
676 {
677   struct GNUNET_IDENTITY_Operation *op;
678   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
679   size_t slen;
680
681   slen = strlen (service_name) + 1;
682   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
683   {
684     GNUNET_break (0);
685     return NULL;
686   }
687   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
688                       sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
689                       slen);  
690   op->h = id;
691   op->cont = cont;
692   op->cls = cont_cls;
693   sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
694   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
695   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
696                             slen);
697   sdm->name_len = htons (slen);
698   sdm->reserved = htons (0);
699   sdm->private_key = *ego->pk;
700   memcpy (&sdm[1], service_name, slen);
701   op->msg = &sdm->header;
702   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
703                                     id->op_tail,
704                                     op);
705   if (NULL == id->th)
706     transmit_next (id);
707   return op;
708 }
709
710
711 /** 
712  * Create a new identity with the given name.
713  *
714  * @param id identity service to use
715  * @param name desired name
716  * @param cont function to call with the result (will only be called once)
717  * @param cont_cls closure for cont
718  * @return handle to abort the operation
719  */
720 struct GNUNET_IDENTITY_Operation *
721 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
722                         const char *name,
723                         GNUNET_IDENTITY_Continuation cont,
724                         void *cont_cls)
725 {
726   struct GNUNET_IDENTITY_Operation *op;
727   struct GNUNET_IDENTITY_CreateRequestMessage *crm;
728   struct GNUNET_CRYPTO_EccPrivateKey *pk;
729   size_t slen;
730
731   slen = strlen (name) + 1;
732   pk = GNUNET_CRYPTO_ecc_key_create ();
733
734   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
735   {
736     GNUNET_break (0);
737     GNUNET_free (pk);
738     return NULL;
739   }
740   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
741                       sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
742                       slen);  
743   op->h = id;
744   op->cont = cont;
745   op->cls = cont_cls;
746   crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
747   crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
748   crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
749                             slen);
750   crm->name_len = htons (slen);
751   crm->reserved = htons (0);
752   crm->private_key = *pk;
753   memcpy (&crm[1], name, slen);
754   op->msg = &crm->header;
755   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
756                                     id->op_tail,
757                                     op);
758   if (NULL == id->th)
759     transmit_next (id);
760   GNUNET_free (pk);
761   return op;
762 }
763
764
765 /** 
766  * Renames an existing identity.
767  *
768  * @param id identity service to use
769  * @param old_name old name
770  * @param new_name desired new name
771  * @param cb function to call with the result (will only be called once)
772  * @param cb_cls closure for cb
773  * @return handle to abort the operation
774  */
775 struct GNUNET_IDENTITY_Operation *
776 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
777                         const char *old_name,
778                         const char *new_name,
779                         GNUNET_IDENTITY_Continuation cb,
780                         void *cb_cls)
781 {
782   struct GNUNET_IDENTITY_Operation *op;
783   struct GNUNET_IDENTITY_RenameMessage *grm;
784   size_t slen_old;
785   size_t slen_new;
786   char *dst;
787
788   slen_old = strlen (old_name) + 1;
789   slen_new = strlen (new_name) + 1;
790   if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
791        (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
792        (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
793   {
794     GNUNET_break (0);
795     return NULL;
796   }
797   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
798                       sizeof (struct GNUNET_IDENTITY_RenameMessage) +
799                       slen_old + slen_new);
800   op->h = id;
801   op->cont = cb;
802   op->cls = cb_cls;
803   grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
804   grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
805   grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
806                             slen_old + slen_new);
807   grm->old_name_len = htons (slen_old);
808   grm->new_name_len = htons (slen_new);
809   dst = (char *) &grm[1];
810   memcpy (dst, old_name, slen_old);
811   memcpy (&dst[slen_old], new_name, slen_new);
812   op->msg = &grm->header;
813   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
814                                     id->op_tail,
815                                     op);
816   if (NULL == id->th)
817     transmit_next (id);
818   return op;
819 }
820
821
822 /** 
823  * Delete an existing identity.
824  *
825  * @param id identity service to use
826  * @param name name of the identity to delete
827  * @param cb function to call with the result (will only be called once)
828  * @param cb_cls closure for @a cb
829  * @return handle to abort the operation
830  */
831 struct GNUNET_IDENTITY_Operation *
832 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
833                         const char *name,
834                         GNUNET_IDENTITY_Continuation cb,
835                         void *cb_cls)
836 {
837   struct GNUNET_IDENTITY_Operation *op;
838   struct GNUNET_IDENTITY_DeleteMessage *gdm;
839   size_t slen;
840
841   slen = strlen (name) + 1;
842   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
843   {
844     GNUNET_break (0);
845     return NULL;
846   }
847   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
848                       sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
849                       slen);  
850   op->h = id;
851   op->cont = cb;
852   op->cls = cb_cls;
853   gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
854   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
855   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
856                             slen);
857   gdm->name_len = htons (slen);
858   gdm->reserved = htons (0);
859   memcpy (&gdm[1], name, slen);
860   op->msg = &gdm->header;
861   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
862                                     id->op_tail,
863                                     op);
864   if (NULL == id->th)
865     transmit_next (id);
866   return op;
867 }
868
869
870 /**
871  * Cancel an identity operation. Note that the operation MAY still
872  * be executed; this merely cancels the continuation; if the request
873  * was already transmitted, the service may still choose to complete
874  * the operation.
875  *
876  * @param op operation to cancel
877  */
878 void
879 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
880 {
881   struct GNUNET_IDENTITY_Handle *h = op->h;
882
883   if ( (h->op_head != op) ||
884        (NULL == h->client) )
885   {
886     /* request not active, can simply remove */
887     GNUNET_CONTAINER_DLL_remove (h->op_head,
888                                  h->op_tail,
889                                  op);
890     GNUNET_free (op);
891     return;
892   }
893   if (NULL != h->th)
894   {
895     /* request active but not yet with service, can still abort */
896     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
897     h->th = NULL;
898     GNUNET_CONTAINER_DLL_remove (h->op_head,
899                                  h->op_tail,
900                                  op);
901     GNUNET_free (op);
902     transmit_next (h);
903     return;
904   }
905   /* request active with service, simply ensure continuations are not called */
906   op->cont = NULL;
907   op->cb = NULL;
908 }
909
910
911 /**
912  * Free ego from hash map.
913  *
914  * @param cls identity service handle
915  * @param key unused
916  * @param value ego to free
917  * @return #GNUNET_OK (continue to iterate)
918  */
919 static int
920 free_ego (void *cls,
921           const struct GNUNET_HashCode *key,
922           void *value)
923 {
924   struct GNUNET_IDENTITY_Handle *h = cls;
925   struct GNUNET_IDENTITY_Ego *ego = value;
926
927   h->cb (h->cb_cls,
928          ego,
929          &ego->ctx,
930          NULL);
931   GNUNET_free (ego->pk);
932   GNUNET_free (ego->name);
933   GNUNET_free (ego);
934   return GNUNET_OK;
935 }
936
937
938 /**
939  * Disconnect from identity service
940  *
941  * @param h handle to destroy
942  */
943 void
944 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
945 {
946   GNUNET_assert (NULL != h);
947   GNUNET_assert (h->op_head == h->op_tail);
948   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
949   {
950     GNUNET_SCHEDULER_cancel (h->reconnect_task);
951     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
952   }
953   if (NULL != h->th)
954   {
955     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
956     h->th = NULL;
957   }
958   if (NULL != h->egos)
959   {
960     GNUNET_CONTAINER_multihashmap_iterate (h->egos,
961                                            &free_ego,
962                                            h);
963     GNUNET_CONTAINER_multihashmap_destroy (h->egos);
964     h->egos = NULL;
965   }
966   if (NULL != h->client)
967   {
968     GNUNET_CLIENT_disconnect (h->client);
969     h->client = NULL;
970   }
971   GNUNET_free (h);
972 }
973
974 /* end of identity_api.c */