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;
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 GNUNET_free (entry_pmsg);
1316 guest_recv_enter_ack (void *cls,
1317 struct GNUNET_CLIENT_MANAGER_Connection *client,
1318 const struct GNUNET_MessageHeader *msg)
1320 struct GNUNET_SOCIAL_Guest *
1321 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1322 sizeof (struct GNUNET_SOCIAL_Place));
1324 struct GNUNET_PSYC_CountersResultMessage *
1325 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
1326 int32_t result = ntohl (cres->result_code);
1327 if (NULL != gst->enter_cb)
1328 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
1333 guest_recv_join_decision (void *cls,
1334 struct GNUNET_CLIENT_MANAGER_Connection *client,
1335 const struct GNUNET_MessageHeader *msg)
1337 struct GNUNET_SOCIAL_Guest *
1338 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1339 sizeof (struct GNUNET_SOCIAL_Place));
1340 const struct GNUNET_PSYC_JoinDecisionMessage *
1341 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
1343 struct GNUNET_PSYC_Message *pmsg = NULL;
1344 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
1345 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
1347 if (NULL != gst->entry_dcsn_cb)
1348 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
1352 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1354 { host_recv_enter_ack, NULL,
1355 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1356 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1358 { host_recv_enter_request, NULL,
1359 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1360 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1362 { host_recv_message, NULL,
1363 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1364 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1366 { place_recv_message_ack, NULL,
1367 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1368 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1370 { place_recv_history_result, NULL,
1371 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1372 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1374 { place_recv_state_result, NULL,
1375 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1376 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1378 { place_recv_result, NULL,
1379 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1380 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1382 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1384 { NULL, NULL, 0, 0, GNUNET_NO }
1388 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1390 { guest_recv_enter_ack, NULL,
1391 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1392 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1394 { host_recv_enter_request, NULL,
1395 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1396 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1398 { place_recv_message, NULL,
1399 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1400 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1402 { place_recv_message_ack, NULL,
1403 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1404 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1406 { guest_recv_join_decision, NULL,
1407 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1408 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1410 { place_recv_history_result, NULL,
1411 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1412 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1414 { place_recv_state_result, NULL,
1415 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1416 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1418 { place_recv_result, NULL,
1419 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1420 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1422 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1424 { NULL, NULL, 0, 0, GNUNET_NO }
1429 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1431 if (NULL != plc->tmit)
1432 GNUNET_PSYC_transmit_destroy (plc->tmit);
1433 if (NULL != plc->recv)
1434 GNUNET_PSYC_receive_destroy (plc->recv);
1435 if (NULL != plc->connect_msg)
1436 GNUNET_free (plc->connect_msg);
1437 if (NULL != plc->disconnect_cb)
1438 plc->disconnect_cb (plc->disconnect_cls);
1442 GNUNET_CORE_disconnect (core);
1445 if (NULL != namestore)
1447 GNUNET_NAMESTORE_disconnect (namestore);
1452 GNUNET_GNS_disconnect (gns);
1459 host_cleanup (void *cls)
1461 struct GNUNET_SOCIAL_Host *hst = cls;
1462 place_cleanup (&hst->plc);
1463 GNUNET_PSYC_receive_destroy (hst->recv);
1464 GNUNET_SOCIAL_slicer_destroy (hst->slicer);
1470 guest_cleanup (void *cls)
1472 struct GNUNET_SOCIAL_Guest *gst = cls;
1473 place_cleanup (&gst->plc);
1481 * Enter a place as host.
1483 * A place is created upon first entering, and it is active until permanently
1484 * left using GNUNET_SOCIAL_host_leave().
1487 * Configuration to contact the social service.
1489 * Identity of the host.
1491 * Private-public key pair of the place.
1492 * NULL for ephemeral places.
1494 * Policy specifying entry and history restrictions for the place.
1496 * Slicer to handle incoming messages.
1497 * @param answer_door_cb
1498 * Function to handle new nyms that want to enter.
1499 * @param farewell_cb
1500 * Function to handle departing nyms.
1502 * Closure for the callbacks.
1504 * @return Handle for the host.
1506 struct GNUNET_SOCIAL_Host *
1507 GNUNET_SOCIAL_host_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1508 const struct GNUNET_IDENTITY_Ego *ego,
1509 const struct GNUNET_CRYPTO_EddsaPrivateKey *place_key,
1510 enum GNUNET_PSYC_Policy policy,
1511 struct GNUNET_SOCIAL_Slicer *slicer,
1512 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1513 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1514 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1517 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1518 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1519 struct HostEnterRequest *req = GNUNET_malloc (sizeof (*req));
1521 if (NULL != place_key)
1523 hst->place_key = *place_key;
1527 struct GNUNET_CRYPTO_EddsaPrivateKey *
1528 ephemeral_key = GNUNET_CRYPTO_eddsa_key_create ();
1529 hst->place_key = *ephemeral_key;
1530 GNUNET_CRYPTO_eddsa_key_get_public (&hst->place_key, &plc->pub_key);
1531 GNUNET_CRYPTO_eddsa_key_clear (ephemeral_key);
1532 GNUNET_free (ephemeral_key);
1535 req->header.size = htons (sizeof (*req));
1536 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1537 req->policy = policy;
1538 req->place_key = hst->place_key;
1539 req->host_key = plc->ego_key;
1541 plc->connect_msg = (struct GNUNET_MessageHeader *) req;
1543 plc->is_host = GNUNET_YES;
1544 plc->slicer = slicer;
1546 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1547 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &plc->pub_key);
1549 hst->enter_cb = enter_cb;
1550 hst->answer_door_cb = answer_door_cb;
1551 hst->farewell_cb = farewell_cb;
1554 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", host_handlers);
1555 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1557 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1558 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
1560 hst->slicer = GNUNET_SOCIAL_slicer_create ();
1561 GNUNET_SOCIAL_slicer_method_add (hst->slicer, "_notice_place_leave",
1562 host_recv_notice_place_leave_method,
1563 host_recv_notice_place_leave_modifier,
1564 NULL, host_recv_notice_place_leave_eom, hst);
1565 hst->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, hst->slicer);
1567 place_send_connect_msg (plc);
1573 * Enter a place as host.
1575 * A place is created upon first entering, and it is active until permanently
1576 * left using GNUNET_SOCIAL_host_leave().
1579 * Configuration to contact the social service.
1581 * Identity of the host.
1583 * GNS name in the zone of the @a ego that contains the
1584 * public key of the place in a PLACE record.
1586 * Policy specifying entry and history restrictions for the place.
1588 * Slicer to handle incoming messages.
1589 * @param answer_door_cb
1590 * Function to handle new nyms that want to enter.
1591 * @param farewell_cb
1592 * Function to handle departing nyms.
1594 * Closure for the callbacks.
1596 * @return Handle for the host.
1598 struct GNUNET_SOCIAL_Host *
1599 GNUNET_SOCIAL_host_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
1600 struct GNUNET_IDENTITY_Ego *ego,
1601 const char *gns_name,
1602 enum GNUNET_PSYC_Policy policy,
1603 struct GNUNET_SOCIAL_Slicer *slicer,
1604 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1605 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1606 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1609 struct GNUNET_CRYPTO_EddsaPrivateKey place_key = {};
1612 * 1. get public key by looking up PLACE entry under gns_name
1613 * in the zone of the ego.
1614 * 2. get private key from $GNUNET_DATA_HOME/social/places/PUB_KEY_HASH
1617 return GNUNET_SOCIAL_host_enter (cfg, ego, &place_key, policy, slicer,
1618 enter_cb, answer_door_cb, farewell_cb, cls);
1623 * Decision whether to admit @a nym into the place or refuse entry.
1626 * Host of the place.
1628 * Handle for the entity that wanted to enter.
1629 * @param is_admitted
1630 * #GNUNET_YES if @a nym is admitted,
1631 * #GNUNET_NO if @a nym is refused entry,
1632 * #GNUNET_SYSERR if we cannot answer the request.
1633 * @param method_name
1634 * Method name for the rejection message.
1636 * Environment containing variables for the message, or NULL.
1638 * Data for the rejection message to send back.
1640 * Number of bytes in @a data for method.
1641 * @return #GNUNET_OK on success,
1642 * #GNUNET_SYSERR if the message is too large.
1645 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1646 struct GNUNET_SOCIAL_Nym *nym,
1648 const struct GNUNET_PSYC_Message *entry_resp)
1650 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1651 uint16_t entry_resp_size
1652 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1654 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1655 return GNUNET_SYSERR;
1657 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1658 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1659 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1660 dcsn->is_admitted = htonl (is_admitted);
1661 dcsn->slave_key = nym->pub_key;
1663 if (0 < entry_resp_size)
1664 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1666 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1672 * Throw @a nym out of the place.
1674 * The @a nym reference will remain valid until the
1675 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1676 * which should be very soon after this call.
1679 * Host of the place.
1681 * Handle for the entity to be ejected.
1684 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1685 const struct GNUNET_SOCIAL_Nym *nym)
1687 struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
1688 GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_SET,
1689 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1690 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1691 GNUNET_SOCIAL_ANNOUNCE_NONE);
1696 * Get the public key of a @a nym.
1698 * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
1701 * Pseudonym to map to a cryptographic identifier.
1703 * @return Public key of nym.
1705 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1706 GNUNET_SOCIAL_nym_get_key (const struct GNUNET_SOCIAL_Nym *nym)
1708 return &nym->pub_key;
1713 * Get the hash of the public key of a @a nym.
1716 * Pseudonym to map to a cryptographic identifier.
1718 * @return Hash of the public key of nym.
1720 const struct GNUNET_HashCode *
1721 GNUNET_SOCIAL_nym_get_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1723 return &nym->pub_key_hash;
1728 * Obtain the private-public key pair of the hosted place.
1730 * The public part is suitable for storing in GNS within a PLACE record,
1731 * along with peer IDs to join at.
1734 * Host of the place.
1736 * @return Private-public key pair of the hosted place.
1738 const struct GNUNET_CRYPTO_EddsaPrivateKey *
1739 GNUNET_SOCIAL_host_get_place_key (struct GNUNET_SOCIAL_Host *hst)
1741 return &hst->place_key;
1746 * Connected to core service.
1749 core_connected_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1751 this_peer = *my_identity;
1757 * Advertise the place in the GNS zone of the @e ego of the @a host.
1760 * Host of the place.
1762 * The name for the PLACE record to put in the zone.
1764 * Number of elements in the @a peers array.
1766 * List of peers to put in the PLACE record to advertise
1767 * as entry points to the place in addition to the origin.
1768 * @param expiration_time
1769 * Expiration time of the record, use 0 to remove the record.
1771 * Password used to encrypt the record or NULL to keep it cleartext.
1773 * Function called with the result of the operation.
1775 * Closure for @a result_cb
1778 GNUNET_SOCIAL_host_advertise (struct GNUNET_SOCIAL_Host *hst,
1780 uint32_t peer_count,
1781 const struct GNUNET_PeerIdentity *peers,
1782 struct GNUNET_TIME_Absolute expiration_time,
1783 const char *password,
1784 GNUNET_NAMESTORE_ContinuationWithStatus result_cb,
1787 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1788 if (NULL == namestore)
1789 namestore = GNUNET_NAMESTORE_connect (plc->cfg);
1791 core = GNUNET_CORE_connect (plc->cfg, NULL, core_connected_cb, NULL, NULL,
1792 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
1794 struct GNUNET_GNSRECORD_Data rd = { };
1795 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
1796 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1797 rd.expiration_time = expiration_time.abs_value_us;
1799 struct GNUNET_GNSRECORD_PlaceData *
1800 rec = GNUNET_malloc (sizeof (*rec) + peer_count * sizeof (*peers));
1801 rec->place_key = plc->pub_key;
1802 rec->origin = this_peer;
1803 rec->relay_count = htonl (peer_count);
1804 memcpy (&rec[1], peers, peer_count * sizeof (*peers));
1807 rd.data_size = sizeof (*rec) + peer_count * sizeof (*peers);
1809 GNUNET_NAMESTORE_records_store (namestore, &hst->plc.ego_key,
1810 name, 1, &rd, result_cb, result_cls);
1815 * Send a message to all nyms that are present in the place.
1817 * This function is restricted to the host. Nyms can only send requests
1818 * to the host who can decide to relay it to everyone in the place.
1820 * @param host Host of the place.
1821 * @param method_name Method to use for the announcement.
1822 * @param env Environment containing variables for the message and operations
1823 * on objects of the place. Can be NULL.
1824 * @param notify Function to call to get the payload of the announcement.
1825 * @param notify_cls Closure for @a notify.
1826 * @param flags Flags for this announcement.
1828 * @return NULL on error (announcement already in progress?).
1830 struct GNUNET_SOCIAL_Announcement *
1831 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1832 const char *method_name,
1833 const struct GNUNET_ENV_Environment *env,
1834 GNUNET_PSYC_TransmitNotifyData notify_data,
1835 void *notify_data_cls,
1836 enum GNUNET_SOCIAL_AnnounceFlags flags)
1839 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1840 NULL, notify_data, notify_data_cls, flags))
1841 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1848 * Resume transmitting announcement.
1851 * The announcement to resume.
1854 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1856 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1861 * Cancel announcement.
1864 * The announcement to cancel.
1867 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1869 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1874 * Obtain handle for a hosted place.
1876 * The returned handle can be used to access the place API.
1878 * @param host Handle for the host.
1880 * @return Handle for the hosted place, valid as long as @a host is valid.
1882 struct GNUNET_SOCIAL_Place *
1883 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1890 * Stop hosting a place.
1892 * Invalidates host handle.
1894 * @param host Host leaving the place.
1895 * @param keep_active Keep the place active after last host disconnected.
1898 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1900 GNUNET_ContinuationCallback leave_cb,
1903 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1905 /* FIXME: send msg to service */
1907 plc->is_disconnecting = GNUNET_YES;
1908 plc->disconnect_cb = leave_cb;
1909 plc->disconnect_cls = leave_cls;
1911 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1912 &host_cleanup, hst);
1918 static struct GuestEnterRequest *
1919 guest_enter_request_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key,
1920 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1921 const struct GNUNET_PeerIdentity *origin,
1923 const struct GNUNET_PeerIdentity *relays,
1924 const struct GNUNET_PSYC_Message *join_msg)
1926 uint16_t join_msg_size = ntohs (join_msg->header.size);
1927 uint16_t relay_size = relay_count * sizeof (*relays);
1929 struct GuestEnterRequest *
1930 req = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size);
1932 req->header.size = htons (sizeof (*req) + relay_size + join_msg_size);
1933 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1934 req->place_key = *place_key;
1935 req->guest_key = *guest_key;
1936 req->origin = *origin;
1937 req->relay_count = htonl (relay_count);
1939 uint16_t p = sizeof (*req);
1942 memcpy ((char *) req + p, relays, relay_size);
1946 memcpy ((char *) req + p, join_msg, join_msg_size);
1951 * Request entry to a place as a guest.
1953 * @param cfg Configuration to contact the social service.
1954 * @param ego Identity of the guest.
1955 * @param crypto_address Public key of the place to enter.
1956 * @param origin Peer identity of the origin of the underlying multicast group.
1957 * @param relay_count Number of elements in the @a relays array.
1958 * @param relays Relays for the underlying multicast group.
1959 * @param method_name Method name for the message.
1960 * @param env Environment containing variables for the message, or NULL.
1961 * @param data Payload for the message to give to the enter callback.
1962 * @param data_size Number of bytes in @a data.
1963 * @param slicer Slicer to use for processing incoming requests from guests.
1965 * @return NULL on errors, otherwise handle for the guest.
1967 struct GNUNET_SOCIAL_Guest *
1968 GNUNET_SOCIAL_guest_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1969 const struct GNUNET_IDENTITY_Ego *ego,
1970 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1971 const struct GNUNET_PeerIdentity *origin,
1972 uint32_t relay_count,
1973 const struct GNUNET_PeerIdentity *relays,
1974 const struct GNUNET_PSYC_Message *entry_msg,
1975 struct GNUNET_SOCIAL_Slicer *slicer,
1976 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1977 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1980 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1981 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1983 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1984 plc->pub_key = *place_key;
1986 plc->is_host = GNUNET_YES;
1987 plc->slicer = slicer;
1989 gst->enter_cb = local_enter_cb;
1990 gst->entry_dcsn_cb = entry_dcsn_cb;
1993 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
1994 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1996 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1997 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
1999 struct GuestEnterRequest *
2000 req = guest_enter_request_create (&plc->ego_key, place_key, origin,
2001 relay_count, relays, entry_msg);
2002 plc->connect_msg = &req->header;
2003 place_send_connect_msg (plc);
2009 * Result of a GNS name lookup for entering a place.
2011 * @see GNUNET_SOCIAL_guest_enter_by_name
2014 gns_result_guest_enter (void *cls, uint32_t rd_count,
2015 const struct GNUNET_GNSRECORD_Data *rd)
2017 struct GNUNET_SOCIAL_Guest *gst = cls;
2018 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2021 "%p GNS result: %u records.\n", gst, rd_count);
2023 const struct GNUNET_GNSRECORD_PlaceData *
2024 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
2028 if (NULL != gst->enter_cb)
2029 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
2033 if (rd->data_size < sizeof (*rec))
2035 GNUNET_break_op (0);
2036 if (NULL != gst->enter_cb)
2037 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
2041 uint16_t relay_count = ntohl (rec->relay_count);
2042 struct GNUNET_PeerIdentity *relays = NULL;
2044 if (0 < relay_count)
2046 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
2048 relays = (struct GNUNET_PeerIdentity *) &rec[1];
2053 GNUNET_break_op (0);
2057 struct GuestEnterRequest *
2058 req = guest_enter_request_create (&plc->ego_key, &rec->place_key,
2059 &rec->origin, relay_count, relays,
2060 (struct GNUNET_PSYC_Message *) plc->connect_msg);
2061 GNUNET_free (plc->connect_msg);
2062 plc->connect_msg = &req->header;
2063 plc->pub_key = req->place_key;
2065 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
2066 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2068 place_send_connect_msg (plc);
2073 * Request entry to a place by name as a guest.
2076 * Configuration to contact the social service.
2078 * Identity of the guest.
2080 * GNS name of the place to enter. Either in the form of
2081 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
2082 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
2083 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
2084 * pseudonym's place directly.
2086 * Password to decrypt the record, or NULL for cleartext records.
2088 * Entry request message.
2090 * Slicer to use for processing incoming requests from guests.
2091 * @param local_enter_cb
2092 * Called upon connection established to the social service.
2093 * @param entry_decision_cb
2094 * Called upon receiving entry decision.
2096 * @return NULL on errors, otherwise handle for the guest.
2098 struct GNUNET_SOCIAL_Guest *
2099 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
2100 const struct GNUNET_IDENTITY_Ego *ego,
2101 const char *gns_name, const char *password,
2102 const struct GNUNET_PSYC_Message *join_msg,
2103 struct GNUNET_SOCIAL_Slicer *slicer,
2104 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
2105 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
2108 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
2109 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2111 GNUNET_assert (NULL != join_msg);
2113 gst->enter_cb = local_enter_cb;
2114 gst->entry_dcsn_cb = entry_decision_cb;
2117 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
2119 plc->is_host = GNUNET_NO;
2120 plc->slicer = slicer;
2122 uint16_t join_msg_size = ntohs (join_msg->header.size);
2123 plc->connect_msg = GNUNET_malloc (join_msg_size);
2124 memcpy (plc->connect_msg, join_msg, join_msg_size);
2127 gns = GNUNET_GNS_connect (cfg);
2129 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
2130 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
2132 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
2133 GNUNET_IDENTITY_ego_get_public_key (ego, &ego_pub_key);
2134 GNUNET_GNS_lookup (gns, gns_name, &ego_pub_key,
2135 GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
2136 NULL, gns_result_guest_enter, gst);
2142 * Talk to the host of the place.
2145 * Place where we want to talk to the host.
2146 * @param method_name
2147 * Method to invoke on the host.
2149 * Environment containing variables for the message, or NULL.
2150 * @param notify_data
2151 * Function to use to get the payload for the method.
2152 * @param notify_data_cls
2153 * Closure for @a notify_data.
2155 * Flags for the message being sent.
2157 * @return NULL if we are already trying to talk to the host,
2158 * otherwise handle to cancel the request.
2160 struct GNUNET_SOCIAL_TalkRequest *
2161 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2162 const char *method_name,
2163 const struct GNUNET_ENV_Environment *env,
2164 GNUNET_PSYC_TransmitNotifyData notify_data,
2165 void *notify_data_cls,
2166 enum GNUNET_SOCIAL_TalkFlags flags)
2168 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2169 GNUNET_assert (NULL != plc->tmit);
2172 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2173 NULL, notify_data, notify_data_cls, flags))
2174 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2181 * Resume talking to the host of the place.
2184 * Talk request to resume.
2187 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2189 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2194 * Cancel talking to the host of the place.
2197 * Talk request to cancel.
2200 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2202 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tr);
2207 * Leave a place temporarily or permanently.
2209 * Notifies the owner of the place about leaving, and destroys the place handle.
2213 * @param keep_active
2214 * Keep place active after last application disconnected.
2215 * #GNUNET_YES or #GNUNET_NO
2217 * Optional environment for the leave message if @a keep_active
2218 * is #GNUNET_NO. NULL if not needed.
2220 * Called upon disconnecting from the social service.
2223 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2225 struct GNUNET_ENV_Environment *env,
2226 GNUNET_ContinuationCallback leave_cb,
2229 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2231 plc->is_disconnecting = GNUNET_YES;
2232 plc->disconnect_cb = leave_cb;
2233 plc->disconnect_cls = leave_cls;
2235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2236 "Guest: leaving place.\n");
2238 if (GNUNET_NO == keep_active && NULL != plc->tmit)
2240 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2241 GNUNET_SOCIAL_TALK_NONE);
2244 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
2245 &guest_cleanup, gst);
2250 * Obtain handle for a place entered as guest.
2252 * The returned handle can be used to access the place API.
2254 * @param guest Handle for the guest.
2256 * @return Handle for the place, valid as long as @a guest is valid.
2258 struct GNUNET_SOCIAL_Place *
2259 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2265 static struct GNUNET_SOCIAL_HistoryRequest *
2266 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2267 uint64_t start_message_id,
2268 uint64_t end_message_id,
2269 uint64_t message_limit,
2270 const char *method_prefix,
2272 struct GNUNET_SOCIAL_Slicer *slicer,
2273 GNUNET_ResultCallback result_cb,
2276 struct GNUNET_PSYC_HistoryRequestMessage *req;
2277 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2279 hist->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, slicer);
2280 hist->result_cb = result_cb;
2282 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2283 &op_recv_history_result, hist);
2285 GNUNET_assert (NULL != method_prefix);
2286 uint16_t method_size = strnlen (method_prefix,
2287 GNUNET_SERVER_MAX_MESSAGE_SIZE
2288 - sizeof (*req)) + 1;
2289 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2290 req = GNUNET_malloc (sizeof (*req) + method_size);
2291 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2292 req->header.size = htons (sizeof (*req) + method_size);
2293 req->start_message_id = GNUNET_htonll (start_message_id);
2294 req->end_message_id = GNUNET_htonll (end_message_id);
2295 req->message_limit = GNUNET_htonll (message_limit);
2296 req->flags = htonl (flags);
2297 req->op_id = GNUNET_htonll (hist->op_id);
2298 memcpy (&req[1], method_prefix, method_size);
2300 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2306 * Learn about the history of a place.
2308 * Messages are returned through the @a slicer function
2309 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2312 * Place we want to learn more about.
2313 * @param start_message_id
2314 * First historic message we are interested in.
2315 * @param end_message_id
2316 * Last historic message we are interested in (inclusive).
2317 * @param method_prefix
2318 * Only retrieve messages with this method prefix.
2320 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2322 * Slicer to use for retrieved messages.
2323 * Can be the same as the slicer of the place.
2325 * Function called after all messages retrieved.
2326 * NULL if not needed.
2327 * @param cls Closure for @a result_cb.
2329 struct GNUNET_SOCIAL_HistoryRequest *
2330 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2331 uint64_t start_message_id,
2332 uint64_t end_message_id,
2333 const char *method_prefix,
2335 struct GNUNET_SOCIAL_Slicer *slicer,
2336 GNUNET_ResultCallback result_cb,
2339 return place_history_replay (plc, start_message_id, end_message_id, 0,
2340 method_prefix, flags, slicer, result_cb, cls);
2345 * Learn about the history of a place.
2347 * Sends messages through the slicer function of the place where
2348 * start_message_id <= message_id <= end_message_id.
2349 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2351 * To get the latest message, use 0 for both the start and end message ID.
2354 * Place we want to learn more about.
2355 * @param message_limit
2356 * Maximum number of historic messages we are interested in.
2357 * @param method_prefix
2358 * Only retrieve messages with this method prefix.
2360 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2362 * Function called after all messages retrieved.
2363 * NULL if not needed.
2364 * @param cls Closure for @a result_cb.
2366 struct GNUNET_SOCIAL_HistoryRequest *
2367 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2368 uint64_t message_limit,
2369 const char *method_prefix,
2371 struct GNUNET_SOCIAL_Slicer *slicer,
2372 GNUNET_ResultCallback result_cb,
2375 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2376 slicer, result_cb, cls);
2381 * Cancel learning about the history of a place.
2384 * History lesson to cancel.
2387 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2389 GNUNET_PSYC_receive_destroy (hist->recv);
2390 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2396 * Request matching state variables.
2398 static struct GNUNET_SOCIAL_LookHandle *
2399 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2400 uint16_t type, const char *name,
2401 GNUNET_PSYC_StateVarCallback var_cb,
2402 GNUNET_ResultCallback result_cb, void *cls)
2404 struct GNUNET_PSYC_StateRequestMessage *req;
2405 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2407 look->var_cb = var_cb;
2408 look->result_cb = result_cb;
2410 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2411 &op_recv_state_result, look);
2413 GNUNET_assert (NULL != name);
2414 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2415 - sizeof (*req)) + 1;
2416 req = GNUNET_malloc (sizeof (*req) + name_size);
2417 req->header.type = htons (type);
2418 req->header.size = htons (sizeof (*req) + name_size);
2419 req->op_id = GNUNET_htonll (look->op_id);
2420 memcpy (&req[1], name, name_size);
2422 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2428 * Look at a particular object in the place.
2430 * The best matching object is returned (its name might be less specific than
2431 * what was requested).
2434 * The place where to look.
2436 * Full name of the object.
2438 * Set to the size of the returned value.
2440 * @return NULL if there is no such object at this place.
2442 struct GNUNET_SOCIAL_LookHandle *
2443 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2444 const char *full_name,
2445 GNUNET_PSYC_StateVarCallback var_cb,
2446 GNUNET_ResultCallback result_cb,
2449 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2450 full_name, var_cb, result_cb, cls);
2455 * Look for objects in the place with a matching name prefix.
2458 * The place where to look.
2459 * @param name_prefix
2460 * Look at objects with names beginning with this value.
2462 * Function to call for each object found.
2464 * Closure for callback function.
2466 * @return Handle that can be used to stop looking at objects.
2468 struct GNUNET_SOCIAL_LookHandle *
2469 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2470 const char *name_prefix,
2471 GNUNET_PSYC_StateVarCallback var_cb,
2472 GNUNET_ResultCallback result_cb,
2475 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2476 name_prefix, var_cb, result_cb, cls);
2481 * Cancel a state request operation.
2484 * Handle for the operation to cancel.
2487 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2489 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2495 * Add public key to the GNS zone of the @e ego.
2502 * The name for the PKEY record to put in the zone.
2504 * Public key to add.
2505 * @param expiration_time
2506 * Expiration time of the record, use 0 to remove the record.
2508 * Function called with the result of the operation.
2510 * Closure for @a result_cb
2513 GNUNET_SOCIAL_zone_add_pkey (const struct GNUNET_CONFIGURATION_Handle *cfg,
2514 const struct GNUNET_IDENTITY_Ego *ego,
2516 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key,
2517 struct GNUNET_TIME_Absolute expiration_time,
2518 GNUNET_NAMESTORE_ContinuationWithStatus result_cb,
2521 if (NULL == namestore)
2522 namestore = GNUNET_NAMESTORE_connect (cfg);
2524 struct GNUNET_GNSRECORD_Data rd = { };
2525 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
2526 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
2527 rd.expiration_time = expiration_time.abs_value_us;
2529 rd.data_size = sizeof (*pub_key);
2531 GNUNET_NAMESTORE_records_store (namestore,
2532 GNUNET_IDENTITY_ego_get_private_key (ego),
2533 name, 1, &rd, result_cb, result_cls);
2537 /* end of social_api.c */