2 * This file is part of GNUnet
3 * Copyright (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 License 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 License for more details.
15 * You should have received a copy of the GNU General Public License
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.
22 * @file social/social_api.c
23 * @brief Social service; implements social interactions using the PSYC service.
24 * @author Gabor X Toth
31 #include "gnunet_util_lib.h"
32 #include "gnunet_env_lib.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_identity_service.h"
35 #include "gnunet_namestore_service.h"
36 #include "gnunet_gns_service.h"
37 #include "gnunet_psyc_service.h"
38 #include "gnunet_psyc_util_lib.h"
39 #include "gnunet_social_service.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "social-api",__VA_ARGS__)
45 static struct GNUNET_CORE_Handle *core;
46 static struct GNUNET_GNS_Handle *gns;
47 static struct GNUNET_NAMESTORE_Handle *namestore;
48 static struct GNUNET_PeerIdentity this_peer;
52 * Handle for a pseudonym of another user in the network.
54 struct GNUNET_SOCIAL_Nym
56 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
57 struct GNUNET_HashCode pub_key_hash;
62 * Handle for a place where social interactions happen.
64 struct GNUNET_SOCIAL_Place
67 * Configuration to use.
69 const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * Client connection to the service.
74 struct GNUNET_CLIENT_MANAGER_Connection *client;
77 * Transmission handle.
79 struct GNUNET_PSYC_TransmitHandle *tmit;
84 struct GNUNET_PSYC_ReceiveHandle *recv;
87 * Slicer for processing incoming methods.
89 struct GNUNET_SOCIAL_Slicer *slicer;
92 * Message to send on reconnect.
94 struct GNUNET_MessageHeader *connect_msg;
97 * Function called after disconnected from the service.
99 GNUNET_ContinuationCallback disconnect_cb;
102 * Closure for @a disconnect_cb.
104 void *disconnect_cls;
107 * Public key of the place.
109 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
112 * Private key of the ego.
114 struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;
117 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
122 * Is this place in the process of disconnecting from the service?
123 * #GNUNET_YES or #GNUNET_NO
125 uint8_t is_disconnecting;
130 * Host handle for a place that we entered.
132 struct GNUNET_SOCIAL_Host
134 struct GNUNET_SOCIAL_Place plc;
136 struct GNUNET_CRYPTO_EddsaPrivateKey place_key;
141 struct GNUNET_PSYC_ReceiveHandle *recv;
144 * Slicer for processing incoming methods.
146 struct GNUNET_SOCIAL_Slicer *slicer;
148 GNUNET_SOCIAL_HostEnterCallback enter_cb;
150 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
152 GNUNET_SOCIAL_FarewellCallback farewell_cb;
155 * Closure for callbacks.
159 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
160 struct GNUNET_ENV_Environment *notice_place_leave_env;
165 * Guest handle for place that we entered.
167 struct GNUNET_SOCIAL_Guest
169 struct GNUNET_SOCIAL_Place plc;
174 struct GNUNET_PSYC_ReceiveHandle *recv;
177 * Slicer for processing incoming methods.
179 struct GNUNET_SOCIAL_Slicer *slicer;
181 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
183 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
186 * Closure for callbacks.
193 * Hash map of all nyms.
194 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
196 struct GNUNET_CONTAINER_MultiHashMap *nyms;
200 * Handle for a try-and-slice instance.
202 struct GNUNET_SOCIAL_Slicer
205 * Method handlers: method_name -> SlicerMethodCallbacks
207 struct GNUNET_CONTAINER_MultiHashMap *method_handlers;
210 * Modifier handlers: modifier name -> SlicerModifierCallbacks
212 struct GNUNET_CONTAINER_MultiHashMap *modifier_handlers;
215 * Currently being processed message part.
217 const struct GNUNET_MessageHeader *msg;
220 * ID of currently being received message.
225 * Method name of currently being received message.
230 * Name of currently processed modifier.
235 * Value of currently processed modifier.
240 * Public key of the nym the current message originates from.
242 struct GNUNET_CRYPTO_EcdsaPublicKey nym_key;
245 * Size of @a method_name (including terminating \0).
247 uint16_t method_name_size;
250 * Size of @a modifier_name (including terminating \0).
252 uint16_t mod_name_size;
255 * Size of modifier value fragment.
257 uint16_t mod_value_size;
260 * Full size of modifier value.
262 uint16_t mod_full_value_size;
265 * Remaining bytes from the value of the current modifier.
267 uint16_t mod_value_remaining;
270 * Operator of currently processed modifier.
277 * Callbacks for a slicer method handler.
279 struct SlicerMethodCallbacks
281 GNUNET_SOCIAL_MethodCallback method_cb;
282 GNUNET_SOCIAL_ModifierCallback modifier_cb;
283 GNUNET_SOCIAL_DataCallback data_cb;
284 GNUNET_SOCIAL_EndOfMessageCallback eom_cb;
289 struct SlicerMethodRemoveClosure
291 struct GNUNET_SOCIAL_Slicer *slicer;
292 struct SlicerMethodCallbacks rm_cbs;
297 * Callbacks for a slicer method handler.
299 struct SlicerModifierCallbacks
301 GNUNET_SOCIAL_ModifierCallback modifier_cb;
306 struct SlicerModifierRemoveClosure
308 struct GNUNET_SOCIAL_Slicer *slicer;
309 struct SlicerModifierCallbacks rm_cbs;
314 * Handle for an announcement request.
316 struct GNUNET_SOCIAL_Announcement
325 struct GNUNET_SOCIAL_TalkRequest
334 struct GNUNET_SOCIAL_HistoryRequest
339 struct GNUNET_SOCIAL_Place *plc;
349 struct GNUNET_PSYC_ReceiveHandle *recv;
352 * Function to call when the operation finished.
354 GNUNET_ResultCallback result_cb;
357 * Closure for @a result_cb.
363 struct GNUNET_SOCIAL_LookHandle
368 struct GNUNET_SOCIAL_Place *plc;
376 * State variable result callback.
378 GNUNET_PSYC_StateVarCallback var_cb;
381 * Function to call when the operation finished.
383 GNUNET_ResultCallback result_cb;
386 * Name of current modifier being received.
391 * Size of current modifier value being received.
393 size_t mod_value_size;
396 * Remaining size of current modifier value still to be received.
398 size_t mod_value_remaining;
401 * Closure for @a result_cb.
409 static struct GNUNET_SOCIAL_Nym *
410 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
412 struct GNUNET_SOCIAL_Nym *nym = NULL;
413 struct GNUNET_HashCode pub_key_hash;
418 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
421 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
423 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
427 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
428 nym->pub_key = *pub_key;
429 nym->pub_key_hash = pub_key_hash;
430 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
431 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
438 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
440 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
445 /*** MESSAGE HANDLERS ***/
447 /** _notice_place_leave from guests */
450 host_recv_notice_place_leave_method (void *cls,
451 const struct GNUNET_PSYC_MessageMethod *meth,
454 const struct GNUNET_SOCIAL_Nym *nym,
455 const char *method_name)
457 struct GNUNET_SOCIAL_Host *hst = cls;
458 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
459 &nym->pub_key, sizeof (nym->pub_key)))
462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
463 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
464 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
466 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
467 hst->notice_place_leave_env = GNUNET_ENV_environment_create ();
469 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
471 "_notice_place_leave: got method from nym %s (%s).\n",
472 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
477 host_recv_notice_place_leave_modifier (void *cls,
478 const struct GNUNET_MessageHeader *msg,
480 enum GNUNET_ENV_Operator oper,
484 uint16_t full_value_size)
486 struct GNUNET_SOCIAL_Host *hst = cls;
487 if (NULL == hst->notice_place_leave_env)
490 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
491 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
493 message_id, oper, name, value_size, value);
495 /* skip _nym, it's added later in eom() */
496 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
497 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
500 GNUNET_ENV_environment_add (hst->notice_place_leave_env,
501 GNUNET_ENV_OP_SET, name, value, value_size);
506 host_recv_notice_place_leave_eom (void *cls,
507 const struct GNUNET_MessageHeader *msg,
511 struct GNUNET_SOCIAL_Host *hst = cls;
512 if (NULL == hst->notice_place_leave_env)
515 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
516 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
517 "_notice_place_leave: got EOM from nym %s (%s).\n",
518 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
520 if (GNUNET_YES != cancelled)
522 if (NULL != hst->farewell_cb)
523 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
524 hst->notice_place_leave_env);
525 /* announce leaving guest to place */
526 GNUNET_ENV_environment_add (hst->notice_place_leave_env, GNUNET_ENV_OP_SET,
527 "_nym", hst->notice_place_leave_nym,
528 sizeof (*hst->notice_place_leave_nym));
529 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
530 hst->notice_place_leave_env,
531 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
532 nym_destroy (hst->notice_place_leave_nym);
534 GNUNET_ENV_environment_destroy (hst->notice_place_leave_env);
535 hst->notice_place_leave_env = NULL;
542 * Call a method handler for an incoming message part.
545 slicer_method_handler_notify (void *cls, const struct GNUNET_HashCode *key,
548 struct GNUNET_SOCIAL_Slicer *slicer = cls;
549 const struct GNUNET_MessageHeader *msg = slicer->msg;
550 struct SlicerMethodCallbacks *cbs = value;
551 uint16_t ptype = ntohs (msg->type);
555 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
557 if (NULL == cbs->method_cb)
559 struct GNUNET_PSYC_MessageMethod *
560 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
561 cbs->method_cb (cbs->cls, meth, slicer->message_id,
563 nym_get_or_create (&slicer->nym_key),
564 slicer->method_name);
568 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
570 if (NULL == cbs->modifier_cb)
572 struct GNUNET_PSYC_MessageModifier *
573 mod = (struct GNUNET_PSYC_MessageModifier *) msg;
574 cbs->modifier_cb (cbs->cls, &mod->header, slicer->message_id,
575 mod->oper, (const char *) &mod[1],
576 (const void *) &mod[1] + ntohs (mod->name_size),
577 ntohs (mod->header.size) - sizeof (*mod) - ntohs (mod->name_size),
578 ntohs (mod->value_size));
582 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
584 if (NULL == cbs->modifier_cb)
586 cbs->modifier_cb (cbs->cls, msg, slicer->message_id,
587 slicer->mod_oper, slicer->mod_name, &msg[1],
588 ntohs (msg->size) - sizeof (*msg),
589 slicer->mod_full_value_size);
593 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
595 if (NULL == cbs->data_cb)
597 uint64_t data_offset = 0; // FIXME
598 cbs->data_cb (cbs->cls, msg, slicer->message_id,
599 data_offset, &msg[1], ntohs (msg->size) - sizeof (*msg));
603 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
604 if (NULL == cbs->eom_cb)
606 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_NO);
609 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
610 if (NULL == cbs->eom_cb)
612 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_YES);
620 * Call a method handler for an incoming message part.
623 slicer_modifier_handler_notify (void *cls, const struct GNUNET_HashCode *key,
626 struct GNUNET_SOCIAL_Slicer *slicer = cls;
627 struct SlicerModifierCallbacks *cbs = value;
629 cbs->modifier_cb (cbs->cls, slicer->msg, slicer->message_id, slicer->mod_oper,
630 slicer->mod_name, slicer->mod_value,
631 slicer->mod_value_size, slicer->mod_full_value_size);
637 * Process an incoming message part and call matching handlers.
644 * Flags for the message.
645 * @see enum GNUNET_PSYC_MessageFlags
647 * The message part. as it arrived from the network.
650 slicer_message (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
651 uint64_t message_id, uint32_t flags, uint64_t fragment_offset,
652 const struct GNUNET_MessageHeader *msg)
654 struct GNUNET_SOCIAL_Slicer *slicer = cls;
655 slicer->nym_key = *slave_key;
657 uint16_t ptype = ntohs (msg->type);
658 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
660 struct GNUNET_PSYC_MessageMethod *
661 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
662 slicer->method_name_size = ntohs (meth->header.size) - sizeof (*meth);
663 slicer->method_name = GNUNET_malloc (slicer->method_name_size);
664 memcpy (slicer->method_name, &meth[1], slicer->method_name_size);
665 slicer->message_id = message_id;
669 GNUNET_assert (message_id == slicer->message_id);
672 char *nym_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (slave_key);
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
674 "Slicer received message of type %u and size %u, "
675 "with ID %" PRIu64 " and method %s from %s\n",
676 ptype, ntohs (msg->size), message_id, slicer->method_name, nym_str);
677 GNUNET_free (nym_str);
681 /* try-and-slice modifier */
685 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
687 struct GNUNET_PSYC_MessageModifier *
688 mod = (struct GNUNET_PSYC_MessageModifier *) msg;
689 slicer->mod_oper = mod->oper;
690 slicer->mod_name_size = ntohs (mod->name_size);
691 slicer->mod_name = GNUNET_malloc (slicer->mod_name_size);
692 memcpy (slicer->mod_name, &mod[1], slicer->mod_name_size);
693 slicer->mod_value = (char *) &mod[1] + slicer->mod_name_size;
694 slicer->mod_full_value_size = ntohs (mod->value_size);
695 slicer->mod_value_remaining = slicer->mod_full_value_size;
696 slicer->mod_value_size
697 = ntohs (mod->header.size) - sizeof (*mod) - slicer->mod_name_size;
699 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
700 if (ptype == GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT)
702 slicer->mod_value = (char *) &msg[1];
703 slicer->mod_value_size = ntohs (msg->size) - sizeof (*msg);
705 slicer->mod_value_remaining -= slicer->mod_value_size;
706 char *name = GNUNET_malloc (slicer->mod_name_size);
707 memcpy (name, slicer->mod_name, slicer->mod_name_size);
710 struct GNUNET_HashCode key;
711 uint16_t name_len = strlen (name);
712 GNUNET_CRYPTO_hash (name, name_len, &key);
713 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
714 slicer_modifier_handler_notify,
716 char *p = strrchr (name, '_');
724 /* try-and-slice method */
726 char *name = GNUNET_malloc (slicer->method_name_size);
727 memcpy (name, slicer->method_name, slicer->method_name_size);
730 struct GNUNET_HashCode key;
731 uint16_t name_len = strlen (name);
732 GNUNET_CRYPTO_hash (name, name_len, &key);
733 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
734 slicer_method_handler_notify,
736 char *p = strrchr (name, '_');
743 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
744 GNUNET_free (slicer->method_name);
746 if (0 == slicer->mod_value_remaining && NULL != slicer->mod_name)
748 GNUNET_free (slicer->mod_name);
749 slicer->mod_name = NULL;
750 slicer->mod_name_size = 0;
751 slicer->mod_value_size = 0;
752 slicer->mod_full_value_size = 0;
753 slicer->mod_oper = 0;
761 * Create a try-and-slice instance.
763 * A slicer processes incoming messages and notifies callbacks about matching
764 * methods or modifiers encountered.
766 * @return A new try-and-slice construct.
768 struct GNUNET_SOCIAL_Slicer *
769 GNUNET_SOCIAL_slicer_create (void)
771 struct GNUNET_SOCIAL_Slicer *slicer = GNUNET_malloc (sizeof (*slicer));
772 slicer->method_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
773 slicer->modifier_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
779 * Add a method to the try-and-slice instance.
781 * The callbacks are called for messages with a matching @a method_name prefix.
784 * The try-and-slice instance to extend.
786 * Name of the given method, use empty string to match all.
788 * Method handler invoked upon a matching message.
790 * Modifier handler, invoked after @a method_cb
791 * for each modifier in the message.
793 * Data handler, invoked after @a modifier_cb for each data fragment.
795 * Invoked upon reaching the end of a matching message.
797 * Closure for the callbacks.
800 GNUNET_SOCIAL_slicer_method_add (struct GNUNET_SOCIAL_Slicer *slicer,
801 const char *method_name,
802 GNUNET_SOCIAL_MethodCallback method_cb,
803 GNUNET_SOCIAL_ModifierCallback modifier_cb,
804 GNUNET_SOCIAL_DataCallback data_cb,
805 GNUNET_SOCIAL_EndOfMessageCallback eom_cb,
808 struct GNUNET_HashCode key;
809 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
811 struct SlicerMethodCallbacks *cbs = GNUNET_malloc (sizeof (*cbs));
812 cbs->method_cb = method_cb;
813 cbs->modifier_cb = modifier_cb;
814 cbs->data_cb = data_cb;
815 cbs->eom_cb = eom_cb;
818 GNUNET_CONTAINER_multihashmap_put (slicer->method_handlers, &key, cbs,
819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
824 slicer_method_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
826 struct SlicerMethodRemoveClosure *rm_cls = cls;
827 struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
828 struct SlicerMethodCallbacks *rm_cbs = &rm_cls->rm_cbs;
829 struct SlicerMethodCallbacks *cbs = value;
831 if (cbs->method_cb == rm_cbs->method_cb
832 && cbs->modifier_cb == rm_cbs->modifier_cb
833 && cbs->data_cb == rm_cbs->data_cb
834 && cbs->eom_cb == rm_cbs->eom_cb)
836 GNUNET_CONTAINER_multihashmap_remove (slicer->method_handlers, key, cbs);
845 * Remove a registered method from the try-and-slice instance.
847 * Removes one matching handler registered with the given
848 * @a method_name and callbacks.
851 * The try-and-slice instance.
853 * Name of the method to remove.
861 * End of message handler.
863 * @return #GNUNET_OK if a method handler was removed,
864 * #GNUNET_NO if no handler matched the given method name and callbacks.
867 GNUNET_SOCIAL_slicer_method_remove (struct GNUNET_SOCIAL_Slicer *slicer,
868 const char *method_name,
869 GNUNET_SOCIAL_MethodCallback method_cb,
870 GNUNET_SOCIAL_ModifierCallback modifier_cb,
871 GNUNET_SOCIAL_DataCallback data_cb,
872 GNUNET_SOCIAL_EndOfMessageCallback eom_cb)
874 struct GNUNET_HashCode key;
875 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
877 struct SlicerMethodRemoveClosure rm_cls;
878 rm_cls.slicer = slicer;
879 struct SlicerMethodCallbacks *rm_cbs = &rm_cls.rm_cbs;
880 rm_cbs->method_cb = method_cb;
881 rm_cbs->modifier_cb = modifier_cb;
882 rm_cbs->data_cb = data_cb;
883 rm_cbs->eom_cb = eom_cb;
887 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
888 slicer_method_remove,
896 * Watch a place for changed objects.
899 * The try-and-slice instance.
900 * @param object_filter
901 * Object prefix to match.
903 * Function to call when encountering a state modifier.
905 * Closure for callback.
908 GNUNET_SOCIAL_slicer_modifier_add (struct GNUNET_SOCIAL_Slicer *slicer,
909 const char *object_filter,
910 GNUNET_SOCIAL_ModifierCallback modifier_cb,
913 struct SlicerModifierCallbacks *cbs = GNUNET_malloc (sizeof *cbs);
914 cbs->modifier_cb = modifier_cb;
917 struct GNUNET_HashCode key;
918 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
919 GNUNET_CONTAINER_multihashmap_put (slicer->modifier_handlers, &key, cbs,
920 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
925 slicer_modifier_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
927 struct SlicerModifierRemoveClosure *rm_cls = cls;
928 struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
929 struct SlicerModifierCallbacks *rm_cbs = &rm_cls->rm_cbs;
930 struct SlicerModifierCallbacks *cbs = value;
932 if (cbs->modifier_cb == rm_cbs->modifier_cb)
934 GNUNET_CONTAINER_multihashmap_remove (slicer->modifier_handlers, key, cbs);
943 * Remove a registered modifier from the try-and-slice instance.
945 * Removes one matching handler registered with the given
946 * @a object_filter and @a modifier_cb.
949 * The try-and-slice instance.
950 * @param object_filter
951 * Object prefix to match.
953 * Function to call when encountering a state modifier changes.
956 GNUNET_SOCIAL_slicer_modifier_remove (struct GNUNET_SOCIAL_Slicer *slicer,
957 const char *object_filter,
958 GNUNET_SOCIAL_ModifierCallback modifier_cb)
960 struct GNUNET_HashCode key;
961 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
963 struct SlicerModifierRemoveClosure rm_cls;
964 rm_cls.slicer = slicer;
965 struct SlicerModifierCallbacks *rm_cbs = &rm_cls.rm_cbs;
966 rm_cbs->modifier_cb = modifier_cb;
970 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
971 slicer_modifier_remove,
979 slicer_method_free (void *cls, const struct GNUNET_HashCode *key, void *value)
981 struct SlicerMethodCallbacks *cbs = value;
988 * Destroy a given try-and-slice instance.
994 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer)
996 GNUNET_CONTAINER_multihashmap_iterate (slicer->method_handlers,
997 slicer_method_free, NULL);
998 GNUNET_CONTAINER_multihashmap_destroy (slicer->method_handlers);
999 GNUNET_free (slicer);
1007 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
1009 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
1010 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
1011 memcpy (cmsg, plc->connect_msg, cmsg_size);
1012 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
1017 place_recv_disconnect (void *cls,
1018 struct GNUNET_CLIENT_MANAGER_Connection *client,
1019 const struct GNUNET_MessageHeader *msg)
1021 struct GNUNET_SOCIAL_Place *
1022 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1024 GNUNET_CLIENT_MANAGER_reconnect (client);
1025 place_send_connect_msg (plc);
1030 place_recv_result (void *cls,
1031 struct GNUNET_CLIENT_MANAGER_Connection *client,
1032 const struct GNUNET_MessageHeader *msg)
1034 struct GNUNET_SOCIAL_Place *
1035 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1037 const struct GNUNET_OperationResultMessage *
1038 res = (const struct GNUNET_OperationResultMessage *) msg;
1040 uint16_t size = ntohs (msg->size);
1041 if (size < sizeof (*res))
1042 { /* Error, message too small. */
1047 uint16_t data_size = size - sizeof (*res);
1048 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
1049 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
1050 GNUNET_ntohll (res->result_code),
1056 op_recv_history_result (void *cls, int64_t result,
1057 const void *err_msg, uint16_t err_msg_size)
1059 LOG (GNUNET_ERROR_TYPE_DEBUG,
1060 "Received history replay result: %" PRId64 ".\n", result);
1062 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
1064 if (NULL != hist->result_cb)
1065 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
1067 GNUNET_PSYC_receive_destroy (hist->recv);
1073 op_recv_state_result (void *cls, int64_t result,
1074 const void *err_msg, uint16_t err_msg_size)
1076 LOG (GNUNET_ERROR_TYPE_DEBUG,
1077 "Received state request result: %" PRId64 ".\n", result);
1079 struct GNUNET_SOCIAL_LookHandle *look = cls;
1081 if (NULL != look->result_cb)
1082 look->result_cb (look->cls, result, err_msg, err_msg_size);
1089 place_recv_history_result (void *cls,
1090 struct GNUNET_CLIENT_MANAGER_Connection *client,
1091 const struct GNUNET_MessageHeader *msg)
1093 struct GNUNET_SOCIAL_Place *
1094 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1096 const struct GNUNET_OperationResultMessage *
1097 res = (const struct GNUNET_OperationResultMessage *) msg;
1098 struct GNUNET_PSYC_MessageHeader *
1099 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
1101 LOG (GNUNET_ERROR_TYPE_DEBUG,
1102 "%p Received historic fragment for message #%" PRIu64 ".\n",
1103 plc, GNUNET_ntohll (pmsg->message_id));
1105 GNUNET_ResultCallback result_cb = NULL;
1106 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
1108 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
1109 GNUNET_ntohll (res->op_id),
1110 &result_cb, (void *) &hist))
1111 { /* Operation not found. */
1112 LOG (GNUNET_ERROR_TYPE_WARNING,
1113 "%p Replay operation not found for historic fragment of message #%"
1115 plc, GNUNET_ntohll (pmsg->message_id));
1119 uint16_t size = ntohs (msg->size);
1120 if (size < sizeof (*res) + sizeof (*pmsg))
1121 { /* Error, message too small. */
1126 GNUNET_PSYC_receive_message (hist->recv,
1127 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
1132 place_recv_state_result (void *cls,
1133 struct GNUNET_CLIENT_MANAGER_Connection *client,
1134 const struct GNUNET_MessageHeader *msg)
1136 struct GNUNET_SOCIAL_Place *
1137 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1139 const struct GNUNET_OperationResultMessage *
1140 res = (const struct GNUNET_OperationResultMessage *) msg;
1142 GNUNET_ResultCallback result_cb = NULL;
1143 struct GNUNET_SOCIAL_LookHandle *look = NULL;
1145 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
1146 GNUNET_ntohll (res->op_id),
1147 &result_cb, (void *) &look))
1148 { /* Operation not found. */
1152 const struct GNUNET_MessageHeader *
1153 mod = (struct GNUNET_MessageHeader *) &res[1];
1154 uint16_t mod_size = ntohs (mod->size);
1155 if (ntohs (msg->size) - sizeof (*res) != mod_size)
1157 GNUNET_break_op (0);
1158 LOG (GNUNET_ERROR_TYPE_WARNING,
1159 "Invalid modifier size in state result: %u - %u != %u\n",
1160 ntohs (msg->size), sizeof (*res), mod_size);
1163 switch (ntohs (mod->type))
1165 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1167 const struct GNUNET_PSYC_MessageModifier *
1168 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
1170 const char *name = (const char *) &pmod[1];
1171 uint16_t name_size = ntohs (pmod->name_size);
1172 if ('\0' != name[name_size - 1])
1174 GNUNET_break_op (0);
1175 LOG (GNUNET_ERROR_TYPE_WARNING,
1176 "Invalid modifier name in state result\n");
1179 look->mod_value_size = ntohs (pmod->value_size);
1180 look->var_cb (look->cls, mod, name, name + name_size,
1181 mod_size - sizeof (*mod) - name_size,
1182 look->mod_value_size);
1183 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
1185 look->mod_value_remaining = look->mod_value_size;
1186 look->mod_name = GNUNET_malloc (name_size);
1187 memcpy (look->mod_name, name, name_size);
1192 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
1193 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
1194 mod_size - sizeof (*mod), look->mod_value_size);
1195 look->mod_value_remaining -= mod_size - sizeof (*mod);
1196 if (0 == look->mod_value_remaining)
1198 GNUNET_free (look->mod_name);
1206 place_recv_message_ack (void *cls,
1207 struct GNUNET_CLIENT_MANAGER_Connection *client,
1208 const struct GNUNET_MessageHeader *msg)
1210 struct GNUNET_SOCIAL_Place *
1211 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1212 GNUNET_PSYC_transmit_got_ack (plc->tmit);
1217 place_recv_message (void *cls,
1218 struct GNUNET_CLIENT_MANAGER_Connection *client,
1219 const struct GNUNET_MessageHeader *msg)
1221 struct GNUNET_SOCIAL_Place *
1222 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1223 GNUNET_PSYC_receive_message (plc->recv,
1224 (const struct GNUNET_PSYC_MessageHeader *) msg);
1229 host_recv_message (void *cls,
1230 struct GNUNET_CLIENT_MANAGER_Connection *client,
1231 const struct GNUNET_MessageHeader *msg)
1233 struct GNUNET_SOCIAL_Host *
1234 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
1235 GNUNET_PSYC_receive_message (hst->recv,
1236 (const struct GNUNET_PSYC_MessageHeader *) msg);
1237 GNUNET_PSYC_receive_message (hst->plc.recv,
1238 (const struct GNUNET_PSYC_MessageHeader *) msg);
1243 host_recv_enter_ack (void *cls,
1244 struct GNUNET_CLIENT_MANAGER_Connection *client,
1245 const struct GNUNET_MessageHeader *msg)
1247 struct GNUNET_SOCIAL_Host *
1248 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1249 sizeof (struct GNUNET_SOCIAL_Place));
1251 struct GNUNET_PSYC_CountersResultMessage *
1252 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
1253 int32_t result = ntohl (cres->result_code);
1254 if (NULL != hst->enter_cb)
1255 hst->enter_cb (hst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
1260 host_recv_enter_request (void *cls,
1261 struct GNUNET_CLIENT_MANAGER_Connection *client,
1262 const struct GNUNET_MessageHeader *msg)
1264 struct GNUNET_SOCIAL_Host *
1265 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1266 sizeof (struct GNUNET_SOCIAL_Place));
1267 if (NULL == hst->answer_door_cb)
1270 const char *method_name = NULL;
1271 struct GNUNET_ENV_Environment *env = NULL;
1272 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
1273 const void *data = NULL;
1274 uint16_t data_size = 0;
1276 const struct GNUNET_PSYC_JoinRequestMessage *
1277 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
1278 const struct GNUNET_PSYC_Message *entry_msg = NULL;
1282 if (sizeof (*req) + sizeof (*entry_msg) <= ntohs (req->header.size))
1284 entry_msg = (struct GNUNET_PSYC_Message *) &req[1];
1285 LOG (GNUNET_ERROR_TYPE_DEBUG,
1286 "Received entry_msg of type %u and size %u.\n",
1287 ntohs (entry_msg->header.type), ntohs (entry_msg->header.size));
1289 env = GNUNET_ENV_environment_create ();
1290 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (entry_msg);
1291 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
1294 GNUNET_break_op (0);
1295 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_key);
1296 LOG (GNUNET_ERROR_TYPE_WARNING,
1297 "Ignoring invalid entry request from nym %s.\n",
1304 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_key);
1305 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
1310 GNUNET_ENV_environment_destroy (env);
1311 if (NULL != entry_pmsg)
1312 GNUNET_free (entry_pmsg);
1317 guest_recv_enter_ack (void *cls,
1318 struct GNUNET_CLIENT_MANAGER_Connection *client,
1319 const struct GNUNET_MessageHeader *msg)
1321 struct GNUNET_SOCIAL_Guest *
1322 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1323 sizeof (struct GNUNET_SOCIAL_Place));
1325 struct GNUNET_PSYC_CountersResultMessage *
1326 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
1327 int32_t result = ntohl (cres->result_code);
1328 if (NULL != gst->enter_cb)
1329 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
1334 guest_recv_join_decision (void *cls,
1335 struct GNUNET_CLIENT_MANAGER_Connection *client,
1336 const struct GNUNET_MessageHeader *msg)
1338 struct GNUNET_SOCIAL_Guest *
1339 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1340 sizeof (struct GNUNET_SOCIAL_Place));
1341 const struct GNUNET_PSYC_JoinDecisionMessage *
1342 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
1344 struct GNUNET_PSYC_Message *pmsg = NULL;
1345 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
1346 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
1348 if (NULL != gst->entry_dcsn_cb)
1349 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
1353 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1355 { host_recv_enter_ack, NULL,
1356 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1357 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1359 { host_recv_enter_request, NULL,
1360 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1361 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1363 { host_recv_message, NULL,
1364 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1365 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1367 { place_recv_message_ack, NULL,
1368 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1369 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1371 { place_recv_history_result, NULL,
1372 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1373 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1375 { place_recv_state_result, NULL,
1376 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1377 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1379 { place_recv_result, NULL,
1380 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1381 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1383 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1385 { NULL, NULL, 0, 0, GNUNET_NO }
1389 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1391 { guest_recv_enter_ack, NULL,
1392 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1393 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1395 { host_recv_enter_request, NULL,
1396 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1397 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1399 { place_recv_message, NULL,
1400 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1401 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1403 { place_recv_message_ack, NULL,
1404 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1405 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1407 { guest_recv_join_decision, NULL,
1408 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1409 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1411 { place_recv_history_result, NULL,
1412 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1413 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1415 { place_recv_state_result, NULL,
1416 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1417 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1419 { place_recv_result, NULL,
1420 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1421 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1423 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1425 { NULL, NULL, 0, 0, GNUNET_NO }
1430 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1432 if (NULL != plc->tmit)
1433 GNUNET_PSYC_transmit_destroy (plc->tmit);
1434 if (NULL != plc->recv)
1435 GNUNET_PSYC_receive_destroy (plc->recv);
1436 if (NULL != plc->connect_msg)
1437 GNUNET_free (plc->connect_msg);
1438 if (NULL != plc->disconnect_cb)
1439 plc->disconnect_cb (plc->disconnect_cls);
1443 GNUNET_CORE_disconnect (core);
1446 if (NULL != namestore)
1448 GNUNET_NAMESTORE_disconnect (namestore);
1453 GNUNET_GNS_disconnect (gns);
1460 host_cleanup (void *cls)
1462 struct GNUNET_SOCIAL_Host *hst = cls;
1463 place_cleanup (&hst->plc);
1464 GNUNET_PSYC_receive_destroy (hst->recv);
1465 GNUNET_SOCIAL_slicer_destroy (hst->slicer);
1471 guest_cleanup (void *cls)
1473 struct GNUNET_SOCIAL_Guest *gst = cls;
1474 place_cleanup (&gst->plc);
1482 * Enter a place as host.
1484 * A place is created upon first entering, and it is active until permanently
1485 * left using GNUNET_SOCIAL_host_leave().
1488 * Configuration to contact the social service.
1490 * Identity of the host.
1492 * Private-public key pair of the place.
1493 * NULL for ephemeral places.
1495 * Policy specifying entry and history restrictions for the place.
1497 * Slicer to handle incoming messages.
1498 * @param answer_door_cb
1499 * Function to handle new nyms that want to enter.
1500 * @param farewell_cb
1501 * Function to handle departing nyms.
1503 * Closure for the callbacks.
1505 * @return Handle for the host.
1507 struct GNUNET_SOCIAL_Host *
1508 GNUNET_SOCIAL_host_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1509 const struct GNUNET_IDENTITY_Ego *ego,
1510 const struct GNUNET_CRYPTO_EddsaPrivateKey *place_key,
1511 enum GNUNET_PSYC_Policy policy,
1512 struct GNUNET_SOCIAL_Slicer *slicer,
1513 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1514 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1515 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1518 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1519 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1520 struct HostEnterRequest *req = GNUNET_malloc (sizeof (*req));
1522 if (NULL != place_key)
1524 hst->place_key = *place_key;
1528 struct GNUNET_CRYPTO_EddsaPrivateKey *
1529 ephemeral_key = GNUNET_CRYPTO_eddsa_key_create ();
1530 hst->place_key = *ephemeral_key;
1531 GNUNET_CRYPTO_eddsa_key_get_public (&hst->place_key, &plc->pub_key);
1532 GNUNET_CRYPTO_eddsa_key_clear (ephemeral_key);
1533 GNUNET_free (ephemeral_key);
1536 req->header.size = htons (sizeof (*req));
1537 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1538 req->policy = policy;
1539 req->place_key = hst->place_key;
1540 req->host_key = plc->ego_key;
1542 plc->connect_msg = (struct GNUNET_MessageHeader *) req;
1544 plc->is_host = GNUNET_YES;
1545 plc->slicer = slicer;
1547 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1548 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &plc->pub_key);
1550 hst->enter_cb = enter_cb;
1551 hst->answer_door_cb = answer_door_cb;
1552 hst->farewell_cb = farewell_cb;
1555 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", host_handlers);
1556 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1558 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1559 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
1561 hst->slicer = GNUNET_SOCIAL_slicer_create ();
1562 GNUNET_SOCIAL_slicer_method_add (hst->slicer, "_notice_place_leave",
1563 host_recv_notice_place_leave_method,
1564 host_recv_notice_place_leave_modifier,
1565 NULL, host_recv_notice_place_leave_eom, hst);
1566 hst->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, hst->slicer);
1568 place_send_connect_msg (plc);
1574 * Enter a place as host.
1576 * A place is created upon first entering, and it is active until permanently
1577 * left using GNUNET_SOCIAL_host_leave().
1580 * Configuration to contact the social service.
1582 * Identity of the host.
1584 * GNS name in the zone of the @a ego that contains the
1585 * public key of the place in a PLACE record.
1587 * Policy specifying entry and history restrictions for the place.
1589 * Slicer to handle incoming messages.
1590 * @param answer_door_cb
1591 * Function to handle new nyms that want to enter.
1592 * @param farewell_cb
1593 * Function to handle departing nyms.
1595 * Closure for the callbacks.
1597 * @return Handle for the host.
1599 struct GNUNET_SOCIAL_Host *
1600 GNUNET_SOCIAL_host_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
1601 struct GNUNET_IDENTITY_Ego *ego,
1602 const char *gns_name,
1603 enum GNUNET_PSYC_Policy policy,
1604 struct GNUNET_SOCIAL_Slicer *slicer,
1605 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1606 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1607 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1610 struct GNUNET_CRYPTO_EddsaPrivateKey place_key = {};
1613 * 1. get public key by looking up PLACE entry under gns_name
1614 * in the zone of the ego.
1615 * 2. get private key from $GNUNET_DATA_HOME/social/places/PUB_KEY_HASH
1618 return GNUNET_SOCIAL_host_enter (cfg, ego, &place_key, policy, slicer,
1619 enter_cb, answer_door_cb, farewell_cb, cls);
1624 * Decision whether to admit @a nym into the place or refuse entry.
1627 * Host of the place.
1629 * Handle for the entity that wanted to enter.
1630 * @param is_admitted
1631 * #GNUNET_YES if @a nym is admitted,
1632 * #GNUNET_NO if @a nym is refused entry,
1633 * #GNUNET_SYSERR if we cannot answer the request.
1634 * @param method_name
1635 * Method name for the rejection message.
1637 * Environment containing variables for the message, or NULL.
1639 * Data for the rejection message to send back.
1641 * Number of bytes in @a data for method.
1642 * @return #GNUNET_OK on success,
1643 * #GNUNET_SYSERR if the message is too large.
1646 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1647 struct GNUNET_SOCIAL_Nym *nym,
1649 const struct GNUNET_PSYC_Message *entry_resp)
1651 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1652 uint16_t entry_resp_size
1653 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1655 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1656 return GNUNET_SYSERR;
1658 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1659 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1660 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1661 dcsn->is_admitted = htonl (is_admitted);
1662 dcsn->slave_key = nym->pub_key;
1664 if (0 < entry_resp_size)
1665 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1667 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1673 * Throw @a nym out of the place.
1675 * The @a nym reference will remain valid until the
1676 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1677 * which should be very soon after this call.
1680 * Host of the place.
1682 * Handle for the entity to be ejected.
1685 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1686 const struct GNUNET_SOCIAL_Nym *nym)
1688 struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
1689 GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_SET,
1690 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1691 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1692 GNUNET_SOCIAL_ANNOUNCE_NONE);
1697 * Get the public key of a @a nym.
1699 * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
1702 * Pseudonym to map to a cryptographic identifier.
1704 * @return Public key of nym.
1706 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1707 GNUNET_SOCIAL_nym_get_key (const struct GNUNET_SOCIAL_Nym *nym)
1709 return &nym->pub_key;
1714 * Get the hash of the public key of a @a nym.
1717 * Pseudonym to map to a cryptographic identifier.
1719 * @return Hash of the public key of nym.
1721 const struct GNUNET_HashCode *
1722 GNUNET_SOCIAL_nym_get_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1724 return &nym->pub_key_hash;
1729 * Obtain the private-public key pair of the hosted place.
1731 * The public part is suitable for storing in GNS within a PLACE record,
1732 * along with peer IDs to join at.
1735 * Host of the place.
1737 * @return Private-public key pair of the hosted place.
1739 const struct GNUNET_CRYPTO_EddsaPrivateKey *
1740 GNUNET_SOCIAL_host_get_place_key (struct GNUNET_SOCIAL_Host *hst)
1742 return &hst->place_key;
1747 * Connected to core service.
1750 core_connected_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1752 this_peer = *my_identity;
1758 * Advertise the place in the GNS zone of the @e ego of the @a host.
1761 * Host of the place.
1763 * The name for the PLACE record to put in the zone.
1765 * Number of elements in the @a peers array.
1767 * List of peers to put in the PLACE record to advertise
1768 * as entry points to the place in addition to the origin.
1769 * @param expiration_time
1770 * Expiration time of the record, use 0 to remove the record.
1772 * Password used to encrypt the record or NULL to keep it cleartext.
1774 * Function called with the result of the operation.
1776 * Closure for @a result_cb
1779 GNUNET_SOCIAL_host_advertise (struct GNUNET_SOCIAL_Host *hst,
1781 uint32_t peer_count,
1782 const struct GNUNET_PeerIdentity *peers,
1783 struct GNUNET_TIME_Absolute expiration_time,
1784 const char *password,
1785 GNUNET_NAMESTORE_ContinuationWithStatus result_cb,
1788 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1789 if (NULL == namestore)
1790 namestore = GNUNET_NAMESTORE_connect (plc->cfg);
1792 core = GNUNET_CORE_connect (plc->cfg, NULL, core_connected_cb, NULL, NULL,
1793 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
1795 struct GNUNET_GNSRECORD_Data rd = { };
1796 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
1797 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1798 rd.expiration_time = expiration_time.abs_value_us;
1800 struct GNUNET_GNSRECORD_PlaceData *
1801 rec = GNUNET_malloc (sizeof (*rec) + peer_count * sizeof (*peers));
1802 rec->place_key = plc->pub_key;
1803 rec->origin = this_peer;
1804 rec->relay_count = htonl (peer_count);
1805 memcpy (&rec[1], peers, peer_count * sizeof (*peers));
1808 rd.data_size = sizeof (*rec) + peer_count * sizeof (*peers);
1810 GNUNET_NAMESTORE_records_store (namestore, &hst->plc.ego_key,
1811 name, 1, &rd, result_cb, result_cls);
1816 * Send a message to all nyms that are present in the place.
1818 * This function is restricted to the host. Nyms can only send requests
1819 * to the host who can decide to relay it to everyone in the place.
1821 * @param host Host of the place.
1822 * @param method_name Method to use for the announcement.
1823 * @param env Environment containing variables for the message and operations
1824 * on objects of the place. Can be NULL.
1825 * @param notify Function to call to get the payload of the announcement.
1826 * @param notify_cls Closure for @a notify.
1827 * @param flags Flags for this announcement.
1829 * @return NULL on error (announcement already in progress?).
1831 struct GNUNET_SOCIAL_Announcement *
1832 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1833 const char *method_name,
1834 const struct GNUNET_ENV_Environment *env,
1835 GNUNET_PSYC_TransmitNotifyData notify_data,
1836 void *notify_data_cls,
1837 enum GNUNET_SOCIAL_AnnounceFlags flags)
1840 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1841 NULL, notify_data, notify_data_cls, flags))
1842 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1849 * Resume transmitting announcement.
1852 * The announcement to resume.
1855 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1857 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1862 * Cancel announcement.
1865 * The announcement to cancel.
1868 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1870 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1875 * Obtain handle for a hosted place.
1877 * The returned handle can be used to access the place API.
1879 * @param host Handle for the host.
1881 * @return Handle for the hosted place, valid as long as @a host is valid.
1883 struct GNUNET_SOCIAL_Place *
1884 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1891 * Stop hosting a place.
1893 * Invalidates host handle.
1895 * @param host Host leaving the place.
1896 * @param keep_active Keep the place active after last host disconnected.
1899 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1901 GNUNET_ContinuationCallback leave_cb,
1904 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1906 /* FIXME: send msg to service */
1908 plc->is_disconnecting = GNUNET_YES;
1909 plc->disconnect_cb = leave_cb;
1910 plc->disconnect_cls = leave_cls;
1912 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1913 &host_cleanup, hst);
1919 static struct GuestEnterRequest *
1920 guest_enter_request_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key,
1921 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1922 const struct GNUNET_PeerIdentity *origin,
1924 const struct GNUNET_PeerIdentity *relays,
1925 const struct GNUNET_PSYC_Message *join_msg)
1927 uint16_t join_msg_size = ntohs (join_msg->header.size);
1928 uint16_t relay_size = relay_count * sizeof (*relays);
1930 struct GuestEnterRequest *
1931 req = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size);
1933 req->header.size = htons (sizeof (*req) + relay_size + join_msg_size);
1934 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1935 req->place_key = *place_key;
1936 req->guest_key = *guest_key;
1937 req->origin = *origin;
1938 req->relay_count = htonl (relay_count);
1940 uint16_t p = sizeof (*req);
1943 memcpy ((char *) req + p, relays, relay_size);
1947 memcpy ((char *) req + p, join_msg, join_msg_size);
1952 * Request entry to a place as a guest.
1954 * @param cfg Configuration to contact the social service.
1955 * @param ego Identity of the guest.
1956 * @param crypto_address Public key of the place to enter.
1957 * @param origin Peer identity of the origin of the underlying multicast group.
1958 * @param relay_count Number of elements in the @a relays array.
1959 * @param relays Relays for the underlying multicast group.
1960 * @param method_name Method name for the message.
1961 * @param env Environment containing variables for the message, or NULL.
1962 * @param data Payload for the message to give to the enter callback.
1963 * @param data_size Number of bytes in @a data.
1964 * @param slicer Slicer to use for processing incoming requests from guests.
1966 * @return NULL on errors, otherwise handle for the guest.
1968 struct GNUNET_SOCIAL_Guest *
1969 GNUNET_SOCIAL_guest_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1970 const struct GNUNET_IDENTITY_Ego *ego,
1971 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1972 const struct GNUNET_PeerIdentity *origin,
1973 uint32_t relay_count,
1974 const struct GNUNET_PeerIdentity *relays,
1975 const struct GNUNET_PSYC_Message *entry_msg,
1976 struct GNUNET_SOCIAL_Slicer *slicer,
1977 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1978 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1981 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1982 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1984 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1985 plc->pub_key = *place_key;
1987 plc->is_host = GNUNET_YES;
1988 plc->slicer = slicer;
1990 gst->enter_cb = local_enter_cb;
1991 gst->entry_dcsn_cb = entry_dcsn_cb;
1994 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
1995 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1997 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1998 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2000 struct GuestEnterRequest *
2001 req = guest_enter_request_create (&plc->ego_key, place_key, origin,
2002 relay_count, relays, entry_msg);
2003 plc->connect_msg = &req->header;
2004 place_send_connect_msg (plc);
2010 * Result of a GNS name lookup for entering a place.
2012 * @see GNUNET_SOCIAL_guest_enter_by_name
2015 gns_result_guest_enter (void *cls, uint32_t rd_count,
2016 const struct GNUNET_GNSRECORD_Data *rd)
2018 struct GNUNET_SOCIAL_Guest *gst = cls;
2019 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2021 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2022 "%p GNS result: %u records.\n", gst, rd_count);
2024 const struct GNUNET_GNSRECORD_PlaceData *
2025 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
2029 if (NULL != gst->enter_cb)
2030 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
2034 if (rd->data_size < sizeof (*rec))
2036 GNUNET_break_op (0);
2037 if (NULL != gst->enter_cb)
2038 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
2042 uint16_t relay_count = ntohl (rec->relay_count);
2043 struct GNUNET_PeerIdentity *relays = NULL;
2045 if (0 < relay_count)
2047 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
2049 relays = (struct GNUNET_PeerIdentity *) &rec[1];
2054 GNUNET_break_op (0);
2058 struct GuestEnterRequest *
2059 req = guest_enter_request_create (&plc->ego_key, &rec->place_key,
2060 &rec->origin, relay_count, relays,
2061 (struct GNUNET_PSYC_Message *) plc->connect_msg);
2062 GNUNET_free (plc->connect_msg);
2063 plc->connect_msg = &req->header;
2064 plc->pub_key = req->place_key;
2066 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
2067 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2069 place_send_connect_msg (plc);
2074 * Request entry to a place by name as a guest.
2077 * Configuration to contact the social service.
2079 * Identity of the guest.
2081 * GNS name of the place to enter. Either in the form of
2082 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
2083 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
2084 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
2085 * pseudonym's place directly.
2087 * Password to decrypt the record, or NULL for cleartext records.
2089 * Entry request message.
2091 * Slicer to use for processing incoming requests from guests.
2092 * @param local_enter_cb
2093 * Called upon connection established to the social service.
2094 * @param entry_decision_cb
2095 * Called upon receiving entry decision.
2097 * @return NULL on errors, otherwise handle for the guest.
2099 struct GNUNET_SOCIAL_Guest *
2100 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
2101 const struct GNUNET_IDENTITY_Ego *ego,
2102 const char *gns_name, const char *password,
2103 const struct GNUNET_PSYC_Message *join_msg,
2104 struct GNUNET_SOCIAL_Slicer *slicer,
2105 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
2106 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
2109 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
2110 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2112 GNUNET_assert (NULL != join_msg);
2114 gst->enter_cb = local_enter_cb;
2115 gst->entry_dcsn_cb = entry_decision_cb;
2118 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
2120 plc->is_host = GNUNET_NO;
2121 plc->slicer = slicer;
2123 uint16_t join_msg_size = ntohs (join_msg->header.size);
2124 plc->connect_msg = GNUNET_malloc (join_msg_size);
2125 memcpy (plc->connect_msg, join_msg, join_msg_size);
2128 gns = GNUNET_GNS_connect (cfg);
2130 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
2131 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
2133 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
2134 GNUNET_IDENTITY_ego_get_public_key (ego, &ego_pub_key);
2135 GNUNET_GNS_lookup (gns, gns_name, &ego_pub_key,
2136 GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
2137 NULL, gns_result_guest_enter, gst);
2143 * Talk to the host of the place.
2146 * Place where we want to talk to the host.
2147 * @param method_name
2148 * Method to invoke on the host.
2150 * Environment containing variables for the message, or NULL.
2151 * @param notify_data
2152 * Function to use to get the payload for the method.
2153 * @param notify_data_cls
2154 * Closure for @a notify_data.
2156 * Flags for the message being sent.
2158 * @return NULL if we are already trying to talk to the host,
2159 * otherwise handle to cancel the request.
2161 struct GNUNET_SOCIAL_TalkRequest *
2162 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2163 const char *method_name,
2164 const struct GNUNET_ENV_Environment *env,
2165 GNUNET_PSYC_TransmitNotifyData notify_data,
2166 void *notify_data_cls,
2167 enum GNUNET_SOCIAL_TalkFlags flags)
2169 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2170 GNUNET_assert (NULL != plc->tmit);
2173 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2174 NULL, notify_data, notify_data_cls, flags))
2175 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2182 * Resume talking to the host of the place.
2185 * Talk request to resume.
2188 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2190 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2195 * Cancel talking to the host of the place.
2198 * Talk request to cancel.
2201 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2203 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tr);
2208 * Leave a place temporarily or permanently.
2210 * Notifies the owner of the place about leaving, and destroys the place handle.
2214 * @param keep_active
2215 * Keep place active after last application disconnected.
2216 * #GNUNET_YES or #GNUNET_NO
2218 * Optional environment for the leave message if @a keep_active
2219 * is #GNUNET_NO. NULL if not needed.
2221 * Called upon disconnecting from the social service.
2224 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2226 struct GNUNET_ENV_Environment *env,
2227 GNUNET_ContinuationCallback leave_cb,
2230 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2232 plc->is_disconnecting = GNUNET_YES;
2233 plc->disconnect_cb = leave_cb;
2234 plc->disconnect_cls = leave_cls;
2236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2237 "Guest: leaving place.\n");
2239 if (GNUNET_NO == keep_active && NULL != plc->tmit)
2241 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2242 GNUNET_SOCIAL_TALK_NONE);
2245 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
2246 &guest_cleanup, gst);
2251 * Obtain handle for a place entered as guest.
2253 * The returned handle can be used to access the place API.
2255 * @param guest Handle for the guest.
2257 * @return Handle for the place, valid as long as @a guest is valid.
2259 struct GNUNET_SOCIAL_Place *
2260 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2266 static struct GNUNET_SOCIAL_HistoryRequest *
2267 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2268 uint64_t start_message_id,
2269 uint64_t end_message_id,
2270 uint64_t message_limit,
2271 const char *method_prefix,
2273 struct GNUNET_SOCIAL_Slicer *slicer,
2274 GNUNET_ResultCallback result_cb,
2277 struct GNUNET_PSYC_HistoryRequestMessage *req;
2278 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2280 hist->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, slicer);
2281 hist->result_cb = result_cb;
2283 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2284 &op_recv_history_result, hist);
2286 GNUNET_assert (NULL != method_prefix);
2287 uint16_t method_size = strnlen (method_prefix,
2288 GNUNET_SERVER_MAX_MESSAGE_SIZE
2289 - sizeof (*req)) + 1;
2290 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2291 req = GNUNET_malloc (sizeof (*req) + method_size);
2292 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2293 req->header.size = htons (sizeof (*req) + method_size);
2294 req->start_message_id = GNUNET_htonll (start_message_id);
2295 req->end_message_id = GNUNET_htonll (end_message_id);
2296 req->message_limit = GNUNET_htonll (message_limit);
2297 req->flags = htonl (flags);
2298 req->op_id = GNUNET_htonll (hist->op_id);
2299 memcpy (&req[1], method_prefix, method_size);
2301 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2307 * Learn about the history of a place.
2309 * Messages are returned through the @a slicer function
2310 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2313 * Place we want to learn more about.
2314 * @param start_message_id
2315 * First historic message we are interested in.
2316 * @param end_message_id
2317 * Last historic message we are interested in (inclusive).
2318 * @param method_prefix
2319 * Only retrieve messages with this method prefix.
2321 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2323 * Slicer to use for retrieved messages.
2324 * Can be the same as the slicer of the place.
2326 * Function called after all messages retrieved.
2327 * NULL if not needed.
2328 * @param cls Closure for @a result_cb.
2330 struct GNUNET_SOCIAL_HistoryRequest *
2331 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2332 uint64_t start_message_id,
2333 uint64_t end_message_id,
2334 const char *method_prefix,
2336 struct GNUNET_SOCIAL_Slicer *slicer,
2337 GNUNET_ResultCallback result_cb,
2340 return place_history_replay (plc, start_message_id, end_message_id, 0,
2341 method_prefix, flags, slicer, result_cb, cls);
2346 * Learn about the history of a place.
2348 * Sends messages through the slicer function of the place where
2349 * start_message_id <= message_id <= end_message_id.
2350 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2352 * To get the latest message, use 0 for both the start and end message ID.
2355 * Place we want to learn more about.
2356 * @param message_limit
2357 * Maximum number of historic messages we are interested in.
2358 * @param method_prefix
2359 * Only retrieve messages with this method prefix.
2361 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2363 * Function called after all messages retrieved.
2364 * NULL if not needed.
2365 * @param cls Closure for @a result_cb.
2367 struct GNUNET_SOCIAL_HistoryRequest *
2368 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2369 uint64_t message_limit,
2370 const char *method_prefix,
2372 struct GNUNET_SOCIAL_Slicer *slicer,
2373 GNUNET_ResultCallback result_cb,
2376 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2377 slicer, result_cb, cls);
2382 * Cancel learning about the history of a place.
2385 * History lesson to cancel.
2388 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2390 GNUNET_PSYC_receive_destroy (hist->recv);
2391 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2397 * Request matching state variables.
2399 static struct GNUNET_SOCIAL_LookHandle *
2400 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2401 uint16_t type, const char *name,
2402 GNUNET_PSYC_StateVarCallback var_cb,
2403 GNUNET_ResultCallback result_cb, void *cls)
2405 struct GNUNET_PSYC_StateRequestMessage *req;
2406 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2408 look->var_cb = var_cb;
2409 look->result_cb = result_cb;
2411 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2412 &op_recv_state_result, look);
2414 GNUNET_assert (NULL != name);
2415 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2416 - sizeof (*req)) + 1;
2417 req = GNUNET_malloc (sizeof (*req) + name_size);
2418 req->header.type = htons (type);
2419 req->header.size = htons (sizeof (*req) + name_size);
2420 req->op_id = GNUNET_htonll (look->op_id);
2421 memcpy (&req[1], name, name_size);
2423 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2429 * Look at a particular object in the place.
2431 * The best matching object is returned (its name might be less specific than
2432 * what was requested).
2435 * The place where to look.
2437 * Full name of the object.
2439 * Set to the size of the returned value.
2441 * @return NULL if there is no such object at this place.
2443 struct GNUNET_SOCIAL_LookHandle *
2444 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2445 const char *full_name,
2446 GNUNET_PSYC_StateVarCallback var_cb,
2447 GNUNET_ResultCallback result_cb,
2450 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2451 full_name, var_cb, result_cb, cls);
2456 * Look for objects in the place with a matching name prefix.
2459 * The place where to look.
2460 * @param name_prefix
2461 * Look at objects with names beginning with this value.
2463 * Function to call for each object found.
2465 * Closure for callback function.
2467 * @return Handle that can be used to stop looking at objects.
2469 struct GNUNET_SOCIAL_LookHandle *
2470 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2471 const char *name_prefix,
2472 GNUNET_PSYC_StateVarCallback var_cb,
2473 GNUNET_ResultCallback result_cb,
2476 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2477 name_prefix, var_cb, result_cb, cls);
2482 * Cancel a state request operation.
2485 * Handle for the operation to cancel.
2488 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2490 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2496 * Add public key to the GNS zone of the @e ego.
2503 * The name for the PKEY record to put in the zone.
2505 * Public key to add.
2506 * @param expiration_time
2507 * Expiration time of the record, use 0 to remove the record.
2509 * Function called with the result of the operation.
2511 * Closure for @a result_cb
2514 GNUNET_SOCIAL_zone_add_pkey (const struct GNUNET_CONFIGURATION_Handle *cfg,
2515 const struct GNUNET_IDENTITY_Ego *ego,
2517 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key,
2518 struct GNUNET_TIME_Absolute expiration_time,
2519 GNUNET_NAMESTORE_ContinuationWithStatus result_cb,
2522 if (NULL == namestore)
2523 namestore = GNUNET_NAMESTORE_connect (cfg);
2525 struct GNUNET_GNSRECORD_Data rd = { };
2526 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
2527 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
2528 rd.expiration_time = expiration_time.abs_value_us;
2530 rd.data_size = sizeof (*pub_key);
2532 GNUNET_NAMESTORE_records_store (namestore,
2533 GNUNET_IDENTITY_ego_get_private_key (ego),
2534 name, 1, &rd, result_cb, result_cls);
2538 /* end of social_api.c */