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 GNUNET_break (NULL == op->cb);
282 case GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE:
283 if (size < sizeof (struct GNUNET_IDENTITY_UpdateMessage))
286 reschedule_connect (h);
289 um = (const struct GNUNET_IDENTITY_UpdateMessage *) msg;
290 pk_len = ntohs (um->pk_len);
291 name_len = ntohs (um->name_len);
292 str = (const char *) &um[1];
293 if ( (size != pk_len + name_len + sizeof (struct GNUNET_IDENTITY_UpdateMessage)) ||
295 ('\0' != str[pk_len + name_len - 1])) )
298 reschedule_connect (h);
301 priv = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES);
305 reschedule_connect (h);
308 GNUNET_CRYPTO_ecc_key_get_public (priv,
310 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
315 ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
319 /* ego was created */
322 /* deletion of unknown ego? not allowed */
324 GNUNET_CRYPTO_ecc_key_free (priv);
325 reschedule_connect (h);
328 ego = GNUNET_new (struct GNUNET_IDENTITY_Ego);
330 ego->identifier = GNUNET_strdup (str);
332 GNUNET_assert (GNUNET_YES ==
333 GNUNET_CONTAINER_multihashmap_put (h->egos,
336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
340 GNUNET_CRYPTO_ecc_key_free (priv);
342 /* inform application about change */
349 /* ego was deleted */
350 GNUNET_assert (GNUNET_YES ==
351 GNUNET_CONTAINER_multihashmap_remove (h->egos,
354 GNUNET_CRYPTO_ecc_key_free (ego->pk);
355 GNUNET_free (ego->identifier);
360 /* ego changed name */
361 GNUNET_free (ego->identifier);
362 ego->identifier = GNUNET_strdup (str);
365 case GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT:
366 if (size < sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
369 reschedule_connect (h);
372 sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) msg;
373 pk_len = ntohs (sdm->pk_len);
374 name_len = ntohs (sdm->name_len);
375 str = (const char *) &sdm[1];
376 if ( (size != pk_len + name_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage)) ||
378 ('\0' != str[pk_len + name_len - 1]) ) )
381 reschedule_connect (h);
384 priv = GNUNET_CRYPTO_ecc_decode_key (str, pk_len, GNUNET_YES);
388 reschedule_connect (h);
391 GNUNET_CRYPTO_ecc_key_get_public (priv,
393 GNUNET_CRYPTO_ecc_key_free (priv);
394 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
399 ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
404 reschedule_connect (h);
408 GNUNET_CONTAINER_DLL_remove (h->op_head,
416 GNUNET_break (NULL == op->cont);
421 reschedule_connect (h);
424 GNUNET_CLIENT_receive (h->client, &message_handler, h,
425 GNUNET_TIME_UNIT_FOREVER_REL);
430 * Schedule transmission of the next message from our queue.
432 * @param h identity handle
435 transmit_next (struct GNUNET_IDENTITY_Handle *h);
439 * Transmit next message to service.
441 * @param cls the 'struct GNUNET_IDENTITY_Handle'.
442 * @param size number of bytes available in buf
443 * @param buf where to copy the message
444 * @return number of bytes copied to buf
447 send_next_message (void *cls,
451 struct GNUNET_IDENTITY_Handle *h = cls;
452 struct GNUNET_IDENTITY_Operation *op = h->op_head;
458 ret = ntohs (op->msg->size);
461 reschedule_connect (h);
464 memcpy (buf, op->msg, ret);
465 if ( (NULL == op->cont) &&
468 GNUNET_CONTAINER_DLL_remove (h->op_head,
474 if (GNUNET_NO == h->in_receive)
476 h->in_receive = GNUNET_YES;
477 GNUNET_CLIENT_receive (h->client,
479 GNUNET_TIME_UNIT_FOREVER_REL);
486 * Schedule transmission of the next message from our queue.
488 * @param h identity handle
491 transmit_next (struct GNUNET_IDENTITY_Handle *h)
493 struct GNUNET_IDENTITY_Operation *op = h->op_head;
495 GNUNET_assert (NULL == h->th);
498 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
499 ntohs (op->msg->size),
500 GNUNET_TIME_UNIT_FOREVER_REL,
509 * Try again to connect to network size estimation service.
511 * @param cls the handle to the transport service
512 * @param tc scheduler context
515 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
517 struct GNUNET_IDENTITY_Handle *h = cls;
518 struct GNUNET_IDENTITY_Operation *op;
519 struct GNUNET_MessageHeader msg;
521 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
522 LOG (GNUNET_ERROR_TYPE_DEBUG,
523 "Connecting to identity service.\n");
524 GNUNET_assert (NULL == h->client);
525 h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
526 GNUNET_assert (NULL != h->client);
527 op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
528 sizeof (struct GNUNET_MessageHeader));
530 op->msg = (const struct GNUNET_MessageHeader *) &op[1];
531 msg.size = htons (sizeof (msg));
532 msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
533 memcpy (&op[1], &msg, sizeof (msg));
534 GNUNET_CONTAINER_DLL_insert (h->op_head,
538 GNUNET_assert (NULL != h->th);
543 * Connect to the identity service.
545 * @param cfg the configuration to use
546 * @param cb function to call on all identity events, can be NULL
547 * @param cb_cls closure for 'cb'
548 * @return handle to use
550 struct GNUNET_IDENTITY_Handle *
551 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
552 GNUNET_IDENTITY_Callback cb,
555 struct GNUNET_IDENTITY_Handle *h;
557 h = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Handle));
561 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
562 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
568 * Obtain the ECC key associated with a ego.
571 * @return associated ECC key, valid as long as the ego is valid
573 const struct GNUNET_CRYPTO_EccPrivateKey *
574 GNUNET_IDENTITY_ego_get_key (struct GNUNET_IDENTITY_Ego *ego)
581 * Obtain the identity that is currently preferred/default
584 * @param id identity service to query
585 * @param service_name for which service is an identity wanted
586 * @param cb function to call with the result (will only be called once)
587 * @param cb_cls closure for cb
588 * @return handle to abort the operation
590 struct GNUNET_IDENTITY_Operation *
591 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
592 const char *service_name,
593 GNUNET_IDENTITY_Callback cb,
596 GNUNET_break (0); // FIXME
602 * Set the preferred/default identity for a service.
604 * @param id identity service to inform
605 * @param service_name for which service is an identity set
606 * @param ego new default identity to be set for this service
607 * @param cont function to call once the operation finished
608 * @param cont_cls closure for cont
609 * @return handle to abort the operation
611 struct GNUNET_IDENTITY_Operation *
612 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
613 const char *service_name,
614 struct GNUNET_IDENTITY_Ego *ego,
615 GNUNET_IDENTITY_Continuation cont,
618 GNUNET_break (0); // FIXME
624 * Create a new identity with the given identifier.
626 * @param id identity service to use
627 * @param identifier desired identifier
628 * @param cb function to call with the result (will only be called once)
629 * @param cb_cls closure for cb
630 * @return handle to abort the operation
632 struct GNUNET_IDENTITY_Operation *
633 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
634 const char *identifier,
635 GNUNET_IDENTITY_Callback cb,
638 GNUNET_break (0); // FIXME
644 * Renames an existing identity.
646 * @param id identity service to use
647 * @param old_identifier old identifier
648 * @param new_identifier desired new identifier
649 * @param cb function to call with the result (will only be called once)
650 * @param cb_cls closure for cb
651 * @return handle to abort the operation
653 struct GNUNET_IDENTITY_Operation *
654 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
655 const char *old_identifier,
656 const char *new_identifier,
657 GNUNET_IDENTITY_Continuation cb,
660 GNUNET_break (0); // FIXME
666 * Delete an existing identity.
668 * @param id identity service to use
669 * @param identifier identifier of the identity to delete
670 * @param cb function to call with the result (will only be called once)
671 * @param cb_cls closure for cb
672 * @return handle to abort the operation
674 struct GNUNET_IDENTITY_Operation *
675 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
676 const char *identifier,
677 GNUNET_IDENTITY_Continuation cb,
680 GNUNET_break (0); // FIXME
686 * Cancel an identity operation. Note that the operation MAY still
687 * be executed; this merely cancels the continuation; if the request
688 * was already transmitted, the service may still choose to complete
691 * @param op operation to cancel
694 GNUNET_IDENITY_cancel (struct GNUNET_IDENTITY_Operation *op)
696 struct GNUNET_IDENTITY_Handle *h = op->h;
698 if ( (h->op_head != op) ||
699 (NULL == h->client) )
701 /* request not active, can simply remove */
702 GNUNET_CONTAINER_DLL_remove (h->op_head,
710 /* request active but not yet with service, can still abort */
711 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
713 GNUNET_CONTAINER_DLL_remove (h->op_head,
720 /* request active with service, simply ensure continuations are not called */
727 * Disconnect from identity service
729 * @param h handle to destroy
732 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
734 GNUNET_assert (NULL != h);
735 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
737 GNUNET_SCHEDULER_cancel (h->reconnect_task);
738 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
742 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
745 if (NULL != h->client)
747 GNUNET_CLIENT_disconnect (h->client);
753 /* end of identity_api.c */