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