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