2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file identity/identity_api.c
23 * @brief api to interact with the identity service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
35 #include "gnunet_identity_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__)
43 struct GNUNET_IDENTITY_Ego
46 * Private key associated with this ego.
48 struct GNUNET_CRYPTO_EccPrivateKey *pk;
51 * Current identifier (name) associated with this ego.
56 * Client context associated with this ego.
61 * Hash of the public key of this ego.
63 struct GNUNET_HashCode id;
68 * Handle for an operation with the identity service.
70 struct GNUNET_IDENTITY_Operation
74 * Main identity handle.
76 struct GNUNET_IDENTITY_Handle *h;
79 * We keep operations in a DLL.
81 struct GNUNET_IDENTITY_Operation *next;
84 * We keep operations in a DLL.
86 struct GNUNET_IDENTITY_Operation *prev;
89 * Message to send to the identity service.
90 * Allocated at the end of this struct.
92 const struct GNUNET_MessageHeader *msg;
95 * Continuation to invoke with the result of the transmission; 'cb'
96 * will be NULL in this case.
98 GNUNET_IDENTITY_Continuation cont;
101 * Continuation to invoke with the result of the transmission for
102 * 'get' operations ('cont' will be NULL in this case).
104 GNUNET_IDENTITY_Callback cb;
107 * Closure for 'cont' or 'cb'.
115 * Handle for the service.
117 struct GNUNET_IDENTITY_Handle
120 * Configuration to use.
122 const struct GNUNET_CONFIGURATION_Handle *cfg;
125 * Socket (if available).
127 struct GNUNET_CLIENT_Connection *client;
130 * Hash map from the hash of the public key to the
131 * respective 'GNUNET_IDENTITY_Ego' handle.
133 struct GNUNET_CONTAINER_MultiHashMap *egos;
136 * Function to call when we receive updates.
138 GNUNET_IDENTITY_Callback cb;
146 * Head of active operations.
148 struct GNUNET_IDENTITY_Operation *op_head;
151 * Tail of active operations.
153 struct GNUNET_IDENTITY_Operation *op_tail;
156 * Currently pending transmission request, or NULL for none.
158 struct GNUNET_CLIENT_TransmitHandle *th;
161 * Task doing exponential back-off trying to reconnect.
163 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
166 * Time for next connect retry.
168 struct GNUNET_TIME_Relative reconnect_delay;
171 * Are we polling for incoming messages right now?
180 * Try again to connect to network size estimation service.
182 * @param cls the handle to the transport service
183 * @param tc scheduler context
186 reconnect (void *cls,
187 const struct GNUNET_SCHEDULER_TaskContext *tc);
191 * Reschedule a connect attempt to the service.
193 * @param h transport service to reconnect
196 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
198 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
202 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
205 if (NULL != h->client)
207 GNUNET_CLIENT_disconnect (h->client);
210 h->in_receive = GNUNET_NO;
211 LOG (GNUNET_ERROR_TYPE_DEBUG,
212 "Scheduling task to reconnect to identity service in %s.\n",
213 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
215 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
216 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
221 * Type of a function to call when we receive a message
225 * @param msg message received, NULL on timeout or fatal error
228 message_handler (void *cls,
229 const struct GNUNET_MessageHeader *msg)
231 struct GNUNET_IDENTITY_Handle *h = cls;
232 struct GNUNET_IDENTITY_Operation *op;
233 struct GNUNET_IDENTITY_Ego *ego;
234 const struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
235 const struct GNUNET_IDENTITY_UpdateMessage *um;
236 const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
237 struct GNUNET_CRYPTO_EccPrivateKey *priv;
238 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
239 struct GNUNET_HashCode id;
247 reschedule_connect (h);
250 size = ntohs (msg->size);
251 switch (ntohs (msg->type))
253 case GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE:
254 if (size < sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
257 reschedule_connect (h);
260 rcm = (const struct GNUNET_IDENTITY_ResultCodeMessage *) msg;
261 str = (const char *) &rcm[1];
262 if ( (size > sizeof (struct GNUNET_IDENTITY_ResultCodeMessage)) &&
263 ('\0' != str[size - sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) - 1]) )
266 reschedule_connect (h);
269 if (size == sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
273 GNUNET_CONTAINER_DLL_remove (h->op_head,
276 if (NULL != op->cont)
279 else if (NULL != op->cb)
280 op->cb (op->cls, NULL, NULL, NULL);
283 case GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE:
284 if (size < sizeof (struct GNUNET_IDENTITY_UpdateMessage))
287 reschedule_connect (h);
290 um = (const struct GNUNET_IDENTITY_UpdateMessage *) msg;
291 pk_len = ntohs (um->pk_len);
292 name_len = ntohs (um->name_len);
293 str = (const char *) &um[1];
294 if ( (size != pk_len + name_len + sizeof (struct GNUNET_IDENTITY_UpdateMessage)) ||
296 ('\0' != str[pk_len + name_len - 1])) )
299 reschedule_connect (h);
302 if ( (0 == pk_len) &&
305 /* end of initial list of data */
307 h->cb (h->cb_cls, NULL, NULL, NULL);
310 priv = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES);
314 reschedule_connect (h);
317 GNUNET_CRYPTO_ecc_key_get_public (priv,
319 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
324 ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
328 /* ego was created */
331 /* deletion of unknown ego? not allowed */
333 GNUNET_CRYPTO_ecc_key_free (priv);
334 reschedule_connect (h);
337 ego = GNUNET_new (struct GNUNET_IDENTITY_Ego);
339 ego->identifier = GNUNET_strdup (str);
341 GNUNET_assert (GNUNET_YES ==
342 GNUNET_CONTAINER_multihashmap_put (h->egos,
345 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
349 GNUNET_CRYPTO_ecc_key_free (priv);
351 /* inform application about change */
359 /* ego was deleted */
360 GNUNET_assert (GNUNET_YES ==
361 GNUNET_CONTAINER_multihashmap_remove (h->egos,
364 GNUNET_CRYPTO_ecc_key_free (ego->pk);
365 GNUNET_free (ego->identifier);
370 /* ego changed name */
371 GNUNET_free (ego->identifier);
372 ego->identifier = GNUNET_strdup (str);
375 case GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT:
376 if (size < sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
379 reschedule_connect (h);
382 sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) msg;
383 pk_len = ntohs (sdm->pk_len);
384 name_len = ntohs (sdm->name_len);
385 str = (const char *) &sdm[1];
386 if ( (size != pk_len + name_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage)) ||
388 ('\0' != str[pk_len + name_len - 1]) ) )
391 reschedule_connect (h);
394 priv = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES);
398 reschedule_connect (h);
401 GNUNET_CRYPTO_ecc_key_get_public (priv,
403 GNUNET_CRYPTO_ecc_key_free (priv);
404 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
409 ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
414 reschedule_connect (h);
418 GNUNET_CONTAINER_DLL_remove (h->op_head,
430 reschedule_connect (h);
433 GNUNET_CLIENT_receive (h->client, &message_handler, h,
434 GNUNET_TIME_UNIT_FOREVER_REL);
439 * Schedule transmission of the next message from our queue.
441 * @param h identity handle
444 transmit_next (struct GNUNET_IDENTITY_Handle *h);
448 * Transmit next message to service.
450 * @param cls the 'struct GNUNET_IDENTITY_Handle'.
451 * @param size number of bytes available in buf
452 * @param buf where to copy the message
453 * @return number of bytes copied to buf
456 send_next_message (void *cls,
460 struct GNUNET_IDENTITY_Handle *h = cls;
461 struct GNUNET_IDENTITY_Operation *op = h->op_head;
467 ret = ntohs (op->msg->size);
470 reschedule_connect (h);
473 memcpy (buf, op->msg, ret);
474 if ( (NULL == op->cont) &&
477 GNUNET_CONTAINER_DLL_remove (h->op_head,
483 if (GNUNET_NO == h->in_receive)
485 h->in_receive = GNUNET_YES;
486 GNUNET_CLIENT_receive (h->client,
488 GNUNET_TIME_UNIT_FOREVER_REL);
495 * Schedule transmission of the next message from our queue.
497 * @param h identity handle
500 transmit_next (struct GNUNET_IDENTITY_Handle *h)
502 struct GNUNET_IDENTITY_Operation *op = h->op_head;
504 GNUNET_assert (NULL == h->th);
507 if (NULL == h->client)
509 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
510 ntohs (op->msg->size),
511 GNUNET_TIME_UNIT_FOREVER_REL,
519 * Try again to connect to network size estimation service.
521 * @param cls the handle to the transport service
522 * @param tc scheduler context
525 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 struct GNUNET_IDENTITY_Handle *h = cls;
528 struct GNUNET_IDENTITY_Operation *op;
529 struct GNUNET_MessageHeader msg;
531 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 "Connecting to identity service.\n");
534 GNUNET_assert (NULL == h->client);
535 h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
536 GNUNET_assert (NULL != h->client);
537 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
538 sizeof (struct GNUNET_MessageHeader));
540 op->msg = (const struct GNUNET_MessageHeader *) &op[1];
541 msg.size = htons (sizeof (msg));
542 msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
543 memcpy (&op[1], &msg, sizeof (msg));
544 GNUNET_CONTAINER_DLL_insert (h->op_head,
548 GNUNET_assert (NULL != h->th);
553 * Connect to the identity service.
555 * @param cfg the configuration to use
556 * @param cb function to call on all identity events, can be NULL
557 * @param cb_cls closure for 'cb'
558 * @return handle to use
560 struct GNUNET_IDENTITY_Handle *
561 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
562 GNUNET_IDENTITY_Callback cb,
565 struct GNUNET_IDENTITY_Handle *h;
567 h = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Handle));
571 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
572 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
578 * Obtain the ECC key associated with a ego.
581 * @return associated ECC key, valid as long as the ego is valid
583 const struct GNUNET_CRYPTO_EccPrivateKey *
584 GNUNET_IDENTITY_ego_get_key (struct GNUNET_IDENTITY_Ego *ego)
591 * Obtain the identity that is currently preferred/default
594 * @param id identity service to query
595 * @param service_name for which service is an identity wanted
596 * @param cb function to call with the result (will only be called once)
597 * @param cb_cls closure for cb
598 * @return handle to abort the operation
600 struct GNUNET_IDENTITY_Operation *
601 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
602 const char *service_name,
603 GNUNET_IDENTITY_Callback cb,
606 struct GNUNET_IDENTITY_Operation *op;
607 struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
610 slen = strlen (service_name) + 1;
611 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
616 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
617 sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
622 gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
623 gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
624 gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
626 gdm->name_len = htons (slen);
627 gdm->reserved = htons (0);
628 memcpy (&gdm[1], service_name, slen);
629 op->msg = &gdm->header;
630 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
640 * Set the preferred/default identity for a service.
642 * @param id identity service to inform
643 * @param service_name for which service is an identity set
644 * @param ego new default identity to be set for this service
645 * @param cont function to call once the operation finished
646 * @param cont_cls closure for cont
647 * @return handle to abort the operation
649 struct GNUNET_IDENTITY_Operation *
650 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
651 const char *service_name,
652 struct GNUNET_IDENTITY_Ego *ego,
653 GNUNET_IDENTITY_Continuation cont,
656 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
657 struct GNUNET_IDENTITY_Operation *op;
658 struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
663 slen = strlen (service_name) + 1;
664 enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
665 enc_len = ntohs (enc->size);
667 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) - enc_len)
673 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
674 sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
679 sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
680 sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
681 sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
683 sdm->name_len = htons (slen);
684 sdm->pk_len = htons (enc_len);
685 str = (char *) &sdm[1];
686 memcpy (str, enc, enc_len);
687 memcpy (&str[enc_len], service_name, slen);
688 op->msg = &sdm->header;
689 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
699 * Create a new identity with the given identifier.
701 * @param id identity service to use
702 * @param identifier desired identifier
703 * @param cb function to call with the result (will only be called once)
704 * @param cb_cls closure for cb
705 * @return handle to abort the operation
707 struct GNUNET_IDENTITY_Operation *
708 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
709 const char *identifier,
710 GNUNET_IDENTITY_Callback cb,
713 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
714 struct GNUNET_IDENTITY_Operation *op;
715 struct GNUNET_IDENTITY_CreateRequestMessage *crm;
716 struct GNUNET_CRYPTO_EccPrivateKey *pk;
721 slen = strlen (identifier) + 1;
722 pk = GNUNET_CRYPTO_ecc_key_create ();
723 enc = GNUNET_CRYPTO_ecc_encode_key (pk);
724 GNUNET_CRYPTO_ecc_key_free (pk);
725 enc_len = ntohs (enc->size);
727 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) - enc_len)
733 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
734 sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
739 crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
740 crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
741 crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
743 crm->name_len = htons (slen);
744 crm->pk_len = htons (enc_len);
745 str = (char *) &crm[1];
746 memcpy (str, enc, enc_len);
747 memcpy (&str[enc_len], identifier, slen);
748 op->msg = &crm->header;
749 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
759 * Renames an existing identity.
761 * @param id identity service to use
762 * @param old_identifier old identifier
763 * @param new_identifier desired new identifier
764 * @param cb function to call with the result (will only be called once)
765 * @param cb_cls closure for cb
766 * @return handle to abort the operation
768 struct GNUNET_IDENTITY_Operation *
769 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
770 const char *old_identifier,
771 const char *new_identifier,
772 GNUNET_IDENTITY_Continuation cb,
775 struct GNUNET_IDENTITY_Operation *op;
776 struct GNUNET_IDENTITY_RenameMessage *grm;
781 slen_old = strlen (old_identifier) + 1;
782 slen_new = strlen (new_identifier) + 1;
783 if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
784 (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
785 (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
790 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
791 sizeof (struct GNUNET_IDENTITY_RenameMessage) +
792 slen_old + slen_new);
796 grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
797 grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
798 grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
799 slen_old + slen_new);
800 grm->old_name_len = htons (slen_old);
801 grm->new_name_len = htons (slen_new);
802 dst = (char *) &grm[1];
803 memcpy (dst, old_identifier, slen_old);
804 memcpy (&dst[slen_old], new_identifier, slen_new);
805 op->msg = &grm->header;
806 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
816 * Delete an existing identity.
818 * @param id identity service to use
819 * @param identifier identifier of the identity to delete
820 * @param cb function to call with the result (will only be called once)
821 * @param cb_cls closure for cb
822 * @return handle to abort the operation
824 struct GNUNET_IDENTITY_Operation *
825 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
826 const char *identifier,
827 GNUNET_IDENTITY_Continuation cb,
830 struct GNUNET_IDENTITY_Operation *op;
831 struct GNUNET_IDENTITY_DeleteMessage *gdm;
834 slen = strlen (identifier) + 1;
835 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
840 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
841 sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
846 gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
847 gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
848 gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
850 gdm->name_len = htons (slen);
851 gdm->reserved = htons (0);
852 memcpy (&gdm[1], identifier, slen);
853 op->msg = &gdm->header;
854 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
864 * Cancel an identity operation. Note that the operation MAY still
865 * be executed; this merely cancels the continuation; if the request
866 * was already transmitted, the service may still choose to complete
869 * @param op operation to cancel
872 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
874 struct GNUNET_IDENTITY_Handle *h = op->h;
876 if ( (h->op_head != op) ||
877 (NULL == h->client) )
879 /* request not active, can simply remove */
880 GNUNET_CONTAINER_DLL_remove (h->op_head,
888 /* request active but not yet with service, can still abort */
889 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
891 GNUNET_CONTAINER_DLL_remove (h->op_head,
898 /* request active with service, simply ensure continuations are not called */
905 * Disconnect from identity service
907 * @param h handle to destroy
910 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
912 GNUNET_assert (NULL != h);
913 GNUNET_assert (h->op_head == h->op_tail);
914 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
916 GNUNET_SCHEDULER_cancel (h->reconnect_task);
917 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
921 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
924 if (NULL != h->client)
926 GNUNET_CLIENT_disconnect (h->client);
932 /* end of identity_api.c */