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 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
508 ntohs (op->msg->size),
509 GNUNET_TIME_UNIT_FOREVER_REL,
517 * Try again to connect to network size estimation service.
519 * @param cls the handle to the transport service
520 * @param tc scheduler context
523 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
525 struct GNUNET_IDENTITY_Handle *h = cls;
526 struct GNUNET_IDENTITY_Operation *op;
527 struct GNUNET_MessageHeader msg;
529 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Connecting to identity service.\n");
532 GNUNET_assert (NULL == h->client);
533 h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
534 GNUNET_assert (NULL != h->client);
535 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
536 sizeof (struct GNUNET_MessageHeader));
538 op->msg = (const struct GNUNET_MessageHeader *) &op[1];
539 msg.size = htons (sizeof (msg));
540 msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
541 memcpy (&op[1], &msg, sizeof (msg));
542 GNUNET_CONTAINER_DLL_insert (h->op_head,
546 GNUNET_assert (NULL != h->th);
551 * Connect to the identity service.
553 * @param cfg the configuration to use
554 * @param cb function to call on all identity events, can be NULL
555 * @param cb_cls closure for 'cb'
556 * @return handle to use
558 struct GNUNET_IDENTITY_Handle *
559 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
560 GNUNET_IDENTITY_Callback cb,
563 struct GNUNET_IDENTITY_Handle *h;
565 h = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Handle));
569 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
570 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
576 * Obtain the ECC key associated with a ego.
579 * @return associated ECC key, valid as long as the ego is valid
581 const struct GNUNET_CRYPTO_EccPrivateKey *
582 GNUNET_IDENTITY_ego_get_key (struct GNUNET_IDENTITY_Ego *ego)
589 * Obtain the identity that is currently preferred/default
592 * @param id identity service to query
593 * @param service_name for which service is an identity wanted
594 * @param cb function to call with the result (will only be called once)
595 * @param cb_cls closure for cb
596 * @return handle to abort the operation
598 struct GNUNET_IDENTITY_Operation *
599 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
600 const char *service_name,
601 GNUNET_IDENTITY_Callback cb,
604 struct GNUNET_IDENTITY_Operation *op;
605 struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
608 slen = strlen (service_name) + 1;
609 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
614 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
615 sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
619 gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
620 gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
621 gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
623 gdm->name_len = htons (slen);
624 gdm->reserved = htons (0);
625 memcpy (&gdm[1], service_name, slen);
626 op->msg = &gdm->header;
627 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
637 * Set the preferred/default identity for a service.
639 * @param id identity service to inform
640 * @param service_name for which service is an identity set
641 * @param ego new default identity to be set for this service
642 * @param cont function to call once the operation finished
643 * @param cont_cls closure for cont
644 * @return handle to abort the operation
646 struct GNUNET_IDENTITY_Operation *
647 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
648 const char *service_name,
649 struct GNUNET_IDENTITY_Ego *ego,
650 GNUNET_IDENTITY_Continuation cont,
653 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
654 struct GNUNET_IDENTITY_Operation *op;
655 struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
660 slen = strlen (service_name) + 1;
661 enc = GNUNET_CRYPTO_ecc_encode_key (ego->pk);
662 enc_len = ntohs (enc->size);
664 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) - enc_len)
670 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
671 sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
675 sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
676 sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
677 sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
679 sdm->name_len = htons (slen);
680 sdm->pk_len = htons (enc_len);
681 str = (char *) &sdm[1];
682 memcpy (str, enc, enc_len);
683 memcpy (&str[enc_len], service_name, slen);
684 op->msg = &sdm->header;
685 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
695 * Create a new identity with the given identifier.
697 * @param id identity service to use
698 * @param identifier desired identifier
699 * @param cb function to call with the result (will only be called once)
700 * @param cb_cls closure for cb
701 * @return handle to abort the operation
703 struct GNUNET_IDENTITY_Operation *
704 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
705 const char *identifier,
706 GNUNET_IDENTITY_Callback cb,
709 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
710 struct GNUNET_IDENTITY_Operation *op;
711 struct GNUNET_IDENTITY_CreateRequestMessage *crm;
712 struct GNUNET_CRYPTO_EccPrivateKey *pk;
717 slen = strlen (identifier) + 1;
718 pk = GNUNET_CRYPTO_ecc_key_create ();
719 enc = GNUNET_CRYPTO_ecc_encode_key (pk);
720 GNUNET_CRYPTO_ecc_key_free (pk);
721 enc_len = ntohs (enc->size);
723 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) - enc_len)
729 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
730 sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
734 crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
735 crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
736 crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
738 crm->name_len = htons (slen);
739 crm->pk_len = htons (enc_len);
740 str = (char *) &crm[1];
741 memcpy (str, enc, enc_len);
742 memcpy (&str[enc_len], identifier, slen);
743 op->msg = &crm->header;
744 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
754 * Renames an existing identity.
756 * @param id identity service to use
757 * @param old_identifier old identifier
758 * @param new_identifier desired new identifier
759 * @param cb function to call with the result (will only be called once)
760 * @param cb_cls closure for cb
761 * @return handle to abort the operation
763 struct GNUNET_IDENTITY_Operation *
764 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
765 const char *old_identifier,
766 const char *new_identifier,
767 GNUNET_IDENTITY_Continuation cb,
770 struct GNUNET_IDENTITY_Operation *op;
771 struct GNUNET_IDENTITY_RenameMessage *grm;
776 slen_old = strlen (old_identifier) + 1;
777 slen_new = strlen (new_identifier) + 1;
778 if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
779 (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
780 (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
785 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
786 sizeof (struct GNUNET_IDENTITY_RenameMessage) +
787 slen_old + slen_new);
790 grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
791 grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
792 grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
793 slen_old + slen_new);
794 grm->old_name_len = htons (slen_old);
795 grm->new_name_len = htons (slen_new);
796 dst = (char *) &grm[1];
797 memcpy (dst, old_identifier, slen_old);
798 memcpy (&dst[slen_old], new_identifier, slen_new);
799 op->msg = &grm->header;
800 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
810 * Delete an existing identity.
812 * @param id identity service to use
813 * @param identifier identifier of the identity to delete
814 * @param cb function to call with the result (will only be called once)
815 * @param cb_cls closure for cb
816 * @return handle to abort the operation
818 struct GNUNET_IDENTITY_Operation *
819 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
820 const char *identifier,
821 GNUNET_IDENTITY_Continuation cb,
824 struct GNUNET_IDENTITY_Operation *op;
825 struct GNUNET_IDENTITY_DeleteMessage *gdm;
828 slen = strlen (identifier) + 1;
829 if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
834 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
835 sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
839 gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
840 gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
841 gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
843 gdm->name_len = htons (slen);
844 gdm->reserved = htons (0);
845 memcpy (&gdm[1], identifier, slen);
846 op->msg = &gdm->header;
847 GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
857 * Cancel an identity operation. Note that the operation MAY still
858 * be executed; this merely cancels the continuation; if the request
859 * was already transmitted, the service may still choose to complete
862 * @param op operation to cancel
865 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
867 struct GNUNET_IDENTITY_Handle *h = op->h;
869 if ( (h->op_head != op) ||
870 (NULL == h->client) )
872 /* request not active, can simply remove */
873 GNUNET_CONTAINER_DLL_remove (h->op_head,
881 /* request active but not yet with service, can still abort */
882 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
884 GNUNET_CONTAINER_DLL_remove (h->op_head,
891 /* request active with service, simply ensure continuations are not called */
898 * Disconnect from identity service
900 * @param h handle to destroy
903 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
905 GNUNET_assert (NULL != h);
906 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
908 GNUNET_SCHEDULER_cancel (h->reconnect_task);
909 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
913 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
916 if (NULL != h->client)
918 GNUNET_CLIENT_disconnect (h->client);
924 /* end of identity_api.c */