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_psyc_service.h"
34 #include "gnunet_psyc_util_lib.h"
35 #include "gnunet_social_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "social-api",__VA_ARGS__)
43 struct GNUNET_SOCIAL_Ego
45 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
46 struct GNUNET_HashCode pub_key_hash;
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 an application.
64 struct GNUNET_SOCIAL_App
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 * Message to send on reconnect.
79 struct GNUNET_MessageHeader *connect_msg;
82 * Function called after disconnected from the service.
84 GNUNET_ContinuationCallback disconnect_cb;
87 * Closure for @a disconnect_cb.
97 * Hash map of all egos.
98 * pub_key_hash -> struct GNUNET_SOCIAL_Ego *
100 struct GNUNET_CONTAINER_MultiHashMap *egos;
102 GNUNET_SOCIAL_AppEgoCallback ego_cb;
103 GNUNET_SOCIAL_AppHostPlaceCallback host_cb;
104 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb;
108 * Is this place in the process of disconnecting from the service?
109 * #GNUNET_YES or #GNUNET_NO
111 uint8_t is_disconnecting;
115 struct GNUNET_SOCIAL_HostConnection
117 struct GNUNET_SOCIAL_App *app;
119 struct AppPlaceMessage plc_msg;
123 struct GNUNET_SOCIAL_GuestConnection
125 struct GNUNET_SOCIAL_App *app;
127 struct AppPlaceMessage plc_msg;
132 * Handle for a place where social interactions happen.
134 struct GNUNET_SOCIAL_Place
137 * Configuration to use.
139 const struct GNUNET_CONFIGURATION_Handle *cfg;
142 * Client connection to the service.
144 struct GNUNET_CLIENT_MANAGER_Connection *client;
147 * Transmission handle.
149 struct GNUNET_PSYC_TransmitHandle *tmit;
154 struct GNUNET_PSYC_ReceiveHandle *recv;
157 * Slicer for processing incoming methods.
159 struct GNUNET_SOCIAL_Slicer *slicer;
162 * Message to send on reconnect.
164 struct GNUNET_MessageHeader *connect_msg;
167 * Function called after disconnected from the service.
169 GNUNET_ContinuationCallback disconnect_cb;
172 * Closure for @a disconnect_cb.
174 void *disconnect_cls;
177 * Public key of the place.
179 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
182 * Public key of the ego.
184 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
187 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
192 * Is this place in the process of disconnecting from the service?
193 * #GNUNET_YES or #GNUNET_NO
195 uint8_t is_disconnecting;
200 * Host handle for a place that we entered.
202 struct GNUNET_SOCIAL_Host
204 struct GNUNET_SOCIAL_Place plc;
209 struct GNUNET_PSYC_ReceiveHandle *recv;
212 * Slicer for processing incoming methods.
214 struct GNUNET_SOCIAL_Slicer *slicer;
216 GNUNET_SOCIAL_HostEnterCallback enter_cb;
218 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
220 GNUNET_SOCIAL_FarewellCallback farewell_cb;
223 * Closure for callbacks.
227 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
228 struct GNUNET_ENV_Environment *notice_place_leave_env;
233 * Guest handle for place that we entered.
235 struct GNUNET_SOCIAL_Guest
237 struct GNUNET_SOCIAL_Place plc;
242 struct GNUNET_PSYC_ReceiveHandle *recv;
245 * Slicer for processing incoming methods.
247 struct GNUNET_SOCIAL_Slicer *slicer;
249 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
251 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
254 * Closure for callbacks.
261 * Hash map of all nyms.
262 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
264 struct GNUNET_CONTAINER_MultiHashMap *nyms;
268 * Handle for a try-and-slice instance.
270 struct GNUNET_SOCIAL_Slicer
273 * Method handlers: method_name -> SlicerMethodCallbacks
275 struct GNUNET_CONTAINER_MultiHashMap *method_handlers;
278 * Modifier handlers: modifier name -> SlicerModifierCallbacks
280 struct GNUNET_CONTAINER_MultiHashMap *modifier_handlers;
283 * Currently being processed message part.
285 const struct GNUNET_MessageHeader *msg;
288 * ID of currently being received message.
293 * Method name of currently being received message.
298 * Name of currently processed modifier.
303 * Value of currently processed modifier.
308 * Public key of the nym the current message originates from.
310 struct GNUNET_CRYPTO_EcdsaPublicKey nym_key;
313 * Size of @a method_name (including terminating \0).
315 uint16_t method_name_size;
318 * Size of @a modifier_name (including terminating \0).
320 uint16_t mod_name_size;
323 * Size of modifier value fragment.
325 uint16_t mod_value_size;
328 * Full size of modifier value.
330 uint16_t mod_full_value_size;
333 * Remaining bytes from the value of the current modifier.
335 uint16_t mod_value_remaining;
338 * Operator of currently processed modifier.
345 * Callbacks for a slicer method handler.
347 struct SlicerMethodCallbacks
349 GNUNET_SOCIAL_MethodCallback method_cb;
350 GNUNET_SOCIAL_ModifierCallback modifier_cb;
351 GNUNET_SOCIAL_DataCallback data_cb;
352 GNUNET_SOCIAL_EndOfMessageCallback eom_cb;
357 struct SlicerMethodRemoveClosure
359 struct GNUNET_SOCIAL_Slicer *slicer;
360 struct SlicerMethodCallbacks rm_cbs;
365 * Callbacks for a slicer method handler.
367 struct SlicerModifierCallbacks
369 GNUNET_SOCIAL_ModifierCallback modifier_cb;
374 struct SlicerModifierRemoveClosure
376 struct GNUNET_SOCIAL_Slicer *slicer;
377 struct SlicerModifierCallbacks rm_cbs;
382 * Handle for an announcement request.
384 struct GNUNET_SOCIAL_Announcement
393 struct GNUNET_SOCIAL_TalkRequest
402 struct GNUNET_SOCIAL_HistoryRequest
407 struct GNUNET_SOCIAL_Place *plc;
417 struct GNUNET_PSYC_ReceiveHandle *recv;
420 * Function to call when the operation finished.
422 GNUNET_ResultCallback result_cb;
425 * Closure for @a result_cb.
431 struct GNUNET_SOCIAL_LookHandle
436 struct GNUNET_SOCIAL_Place *plc;
444 * State variable result callback.
446 GNUNET_PSYC_StateVarCallback var_cb;
449 * Function to call when the operation finished.
451 GNUNET_ResultCallback result_cb;
454 * Name of current modifier being received.
459 * Size of current modifier value being received.
461 size_t mod_value_size;
464 * Remaining size of current modifier value still to be received.
466 size_t mod_value_remaining;
469 * Closure for @a result_cb.
475 struct ZoneAddPlaceHandle
477 struct ZoneAddPlaceRequest *req;
478 GNUNET_ResultCallback result_cb;
483 struct ZoneAddNymHandle
485 struct ZoneAddNymRequest *req;
486 GNUNET_ResultCallback result_cb;
493 static struct GNUNET_SOCIAL_Nym *
494 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
496 struct GNUNET_SOCIAL_Nym *nym = NULL;
497 struct GNUNET_HashCode pub_key_hash;
502 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
505 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
507 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
511 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
512 nym->pub_key = *pub_key;
513 nym->pub_key_hash = pub_key_hash;
514 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
515 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
522 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
524 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
529 /*** MESSAGE HANDLERS ***/
531 /** _notice_place_leave from guests */
534 host_recv_notice_place_leave_method (void *cls,
535 const struct GNUNET_PSYC_MessageMethod *meth,
538 const struct GNUNET_SOCIAL_Nym *nym,
539 const char *method_name)
541 struct GNUNET_SOCIAL_Host *hst = cls;
542 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
543 &nym->pub_key, sizeof (nym->pub_key)))
546 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
547 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
548 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
550 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
551 hst->notice_place_leave_env = GNUNET_ENV_environment_create ();
553 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
554 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
555 "_notice_place_leave: got method from nym %s (%s).\n",
556 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
561 host_recv_notice_place_leave_modifier (void *cls,
562 const struct GNUNET_MessageHeader *msg,
564 enum GNUNET_ENV_Operator oper,
568 uint16_t full_value_size)
570 struct GNUNET_SOCIAL_Host *hst = cls;
571 if (NULL == hst->notice_place_leave_env)
574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
575 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
577 message_id, oper, name, value_size, value);
579 /* skip _nym, it's added later in eom() */
580 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
581 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
584 GNUNET_ENV_environment_add (hst->notice_place_leave_env,
585 GNUNET_ENV_OP_SET, name, value, value_size);
590 host_recv_notice_place_leave_eom (void *cls,
591 const struct GNUNET_MessageHeader *msg,
595 struct GNUNET_SOCIAL_Host *hst = cls;
596 if (NULL == hst->notice_place_leave_env)
599 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
600 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
601 "_notice_place_leave: got EOM from nym %s (%s).\n",
602 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
604 if (GNUNET_YES != cancelled)
606 if (NULL != hst->farewell_cb)
607 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
608 hst->notice_place_leave_env);
609 /* announce leaving guest to place */
610 GNUNET_ENV_environment_add (hst->notice_place_leave_env, GNUNET_ENV_OP_SET,
611 "_nym", hst->notice_place_leave_nym,
612 sizeof (*hst->notice_place_leave_nym));
613 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
614 hst->notice_place_leave_env,
615 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
616 nym_destroy (hst->notice_place_leave_nym);
618 GNUNET_ENV_environment_destroy (hst->notice_place_leave_env);
619 hst->notice_place_leave_env = NULL;
626 * Call a method handler for an incoming message part.
629 slicer_method_handler_notify (void *cls, const struct GNUNET_HashCode *key,
632 struct GNUNET_SOCIAL_Slicer *slicer = cls;
633 const struct GNUNET_MessageHeader *msg = slicer->msg;
634 struct SlicerMethodCallbacks *cbs = value;
635 uint16_t ptype = ntohs (msg->type);
639 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
641 if (NULL == cbs->method_cb)
643 struct GNUNET_PSYC_MessageMethod *
644 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
645 cbs->method_cb (cbs->cls, meth, slicer->message_id,
647 nym_get_or_create (&slicer->nym_key),
648 slicer->method_name);
652 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
654 if (NULL == cbs->modifier_cb)
656 struct GNUNET_PSYC_MessageModifier *
657 mod = (struct GNUNET_PSYC_MessageModifier *) msg;
658 cbs->modifier_cb (cbs->cls, &mod->header, slicer->message_id,
659 mod->oper, (const char *) &mod[1],
660 (const void *) &mod[1] + ntohs (mod->name_size),
661 ntohs (mod->header.size) - sizeof (*mod) - ntohs (mod->name_size),
662 ntohs (mod->value_size));
666 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
668 if (NULL == cbs->modifier_cb)
670 cbs->modifier_cb (cbs->cls, msg, slicer->message_id,
671 slicer->mod_oper, slicer->mod_name, &msg[1],
672 ntohs (msg->size) - sizeof (*msg),
673 slicer->mod_full_value_size);
677 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
679 if (NULL == cbs->data_cb)
681 uint64_t data_offset = 0; // FIXME
682 cbs->data_cb (cbs->cls, msg, slicer->message_id,
683 data_offset, &msg[1], ntohs (msg->size) - sizeof (*msg));
687 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
688 if (NULL == cbs->eom_cb)
690 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_NO);
693 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
694 if (NULL == cbs->eom_cb)
696 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_YES);
704 * Call a method handler for an incoming message part.
707 slicer_modifier_handler_notify (void *cls, const struct GNUNET_HashCode *key,
710 struct GNUNET_SOCIAL_Slicer *slicer = cls;
711 struct SlicerModifierCallbacks *cbs = value;
713 cbs->modifier_cb (cbs->cls, slicer->msg, slicer->message_id, slicer->mod_oper,
714 slicer->mod_name, slicer->mod_value,
715 slicer->mod_value_size, slicer->mod_full_value_size);
721 * Process an incoming message part and call matching handlers.
728 * Flags for the message.
729 * @see enum GNUNET_PSYC_MessageFlags
731 * The message part. as it arrived from the network.
734 slicer_message (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
735 uint64_t message_id, uint32_t flags, uint64_t fragment_offset,
736 const struct GNUNET_MessageHeader *msg)
738 struct GNUNET_SOCIAL_Slicer *slicer = cls;
739 slicer->nym_key = *slave_key;
741 uint16_t ptype = ntohs (msg->type);
742 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
744 struct GNUNET_PSYC_MessageMethod *
745 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
746 slicer->method_name_size = ntohs (meth->header.size) - sizeof (*meth);
747 slicer->method_name = GNUNET_malloc (slicer->method_name_size);
748 memcpy (slicer->method_name, &meth[1], slicer->method_name_size);
749 slicer->message_id = message_id;
753 GNUNET_assert (message_id == slicer->message_id);
756 char *nym_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (slave_key);
757 LOG (GNUNET_ERROR_TYPE_DEBUG,
758 "Slicer received message of type %u and size %u, "
759 "with ID %" PRIu64 " and method %s from %s\n",
760 ptype, ntohs (msg->size), message_id, slicer->method_name, nym_str);
761 GNUNET_free (nym_str);
765 /* try-and-slice modifier */
769 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
771 struct GNUNET_PSYC_MessageModifier *
772 mod = (struct GNUNET_PSYC_MessageModifier *) msg;
773 slicer->mod_oper = mod->oper;
774 slicer->mod_name_size = ntohs (mod->name_size);
775 slicer->mod_name = GNUNET_malloc (slicer->mod_name_size);
776 memcpy (slicer->mod_name, &mod[1], slicer->mod_name_size);
777 slicer->mod_value = (char *) &mod[1] + slicer->mod_name_size;
778 slicer->mod_full_value_size = ntohs (mod->value_size);
779 slicer->mod_value_remaining = slicer->mod_full_value_size;
780 slicer->mod_value_size
781 = ntohs (mod->header.size) - sizeof (*mod) - slicer->mod_name_size;
783 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
784 if (ptype == GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT)
786 slicer->mod_value = (char *) &msg[1];
787 slicer->mod_value_size = ntohs (msg->size) - sizeof (*msg);
789 slicer->mod_value_remaining -= slicer->mod_value_size;
790 char *name = GNUNET_malloc (slicer->mod_name_size);
791 memcpy (name, slicer->mod_name, slicer->mod_name_size);
794 struct GNUNET_HashCode key;
795 uint16_t name_len = strlen (name);
796 GNUNET_CRYPTO_hash (name, name_len, &key);
797 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
798 slicer_modifier_handler_notify,
800 char *p = strrchr (name, '_');
808 /* try-and-slice method */
810 char *name = GNUNET_malloc (slicer->method_name_size);
811 memcpy (name, slicer->method_name, slicer->method_name_size);
814 struct GNUNET_HashCode key;
815 uint16_t name_len = strlen (name);
816 GNUNET_CRYPTO_hash (name, name_len, &key);
817 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
818 slicer_method_handler_notify,
820 char *p = strrchr (name, '_');
827 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
828 GNUNET_free (slicer->method_name);
830 if (0 == slicer->mod_value_remaining && NULL != slicer->mod_name)
832 GNUNET_free (slicer->mod_name);
833 slicer->mod_name = NULL;
834 slicer->mod_name_size = 0;
835 slicer->mod_value_size = 0;
836 slicer->mod_full_value_size = 0;
837 slicer->mod_oper = 0;
845 * Create a try-and-slice instance.
847 * A slicer processes incoming messages and notifies callbacks about matching
848 * methods or modifiers encountered.
850 * @return A new try-and-slice construct.
852 struct GNUNET_SOCIAL_Slicer *
853 GNUNET_SOCIAL_slicer_create (void)
855 struct GNUNET_SOCIAL_Slicer *slicer = GNUNET_malloc (sizeof (*slicer));
856 slicer->method_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
857 slicer->modifier_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
863 * Add a method to the try-and-slice instance.
865 * The callbacks are called for messages with a matching @a method_name prefix.
868 * The try-and-slice instance to extend.
870 * Name of the given method, use empty string to match all.
872 * Method handler invoked upon a matching message.
874 * Modifier handler, invoked after @a method_cb
875 * for each modifier in the message.
877 * Data handler, invoked after @a modifier_cb for each data fragment.
879 * Invoked upon reaching the end of a matching message.
881 * Closure for the callbacks.
884 GNUNET_SOCIAL_slicer_method_add (struct GNUNET_SOCIAL_Slicer *slicer,
885 const char *method_name,
886 GNUNET_SOCIAL_MethodCallback method_cb,
887 GNUNET_SOCIAL_ModifierCallback modifier_cb,
888 GNUNET_SOCIAL_DataCallback data_cb,
889 GNUNET_SOCIAL_EndOfMessageCallback eom_cb,
892 struct GNUNET_HashCode key;
893 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
895 struct SlicerMethodCallbacks *cbs = GNUNET_malloc (sizeof (*cbs));
896 cbs->method_cb = method_cb;
897 cbs->modifier_cb = modifier_cb;
898 cbs->data_cb = data_cb;
899 cbs->eom_cb = eom_cb;
902 GNUNET_CONTAINER_multihashmap_put (slicer->method_handlers, &key, cbs,
903 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
908 slicer_method_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
910 struct SlicerMethodRemoveClosure *rm_cls = cls;
911 struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
912 struct SlicerMethodCallbacks *rm_cbs = &rm_cls->rm_cbs;
913 struct SlicerMethodCallbacks *cbs = value;
915 if (cbs->method_cb == rm_cbs->method_cb
916 && cbs->modifier_cb == rm_cbs->modifier_cb
917 && cbs->data_cb == rm_cbs->data_cb
918 && cbs->eom_cb == rm_cbs->eom_cb)
920 GNUNET_CONTAINER_multihashmap_remove (slicer->method_handlers, key, cbs);
929 * Remove a registered method from the try-and-slice instance.
931 * Removes one matching handler registered with the given
932 * @a method_name and callbacks.
935 * The try-and-slice instance.
937 * Name of the method to remove.
945 * End of message handler.
947 * @return #GNUNET_OK if a method handler was removed,
948 * #GNUNET_NO if no handler matched the given method name and callbacks.
951 GNUNET_SOCIAL_slicer_method_remove (struct GNUNET_SOCIAL_Slicer *slicer,
952 const char *method_name,
953 GNUNET_SOCIAL_MethodCallback method_cb,
954 GNUNET_SOCIAL_ModifierCallback modifier_cb,
955 GNUNET_SOCIAL_DataCallback data_cb,
956 GNUNET_SOCIAL_EndOfMessageCallback eom_cb)
958 struct GNUNET_HashCode key;
959 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
961 struct SlicerMethodRemoveClosure rm_cls;
962 rm_cls.slicer = slicer;
963 struct SlicerMethodCallbacks *rm_cbs = &rm_cls.rm_cbs;
964 rm_cbs->method_cb = method_cb;
965 rm_cbs->modifier_cb = modifier_cb;
966 rm_cbs->data_cb = data_cb;
967 rm_cbs->eom_cb = eom_cb;
971 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
972 slicer_method_remove,
980 * Watch a place for changed objects.
983 * The try-and-slice instance.
984 * @param object_filter
985 * Object prefix to match.
987 * Function to call when encountering a state modifier.
989 * Closure for callback.
992 GNUNET_SOCIAL_slicer_modifier_add (struct GNUNET_SOCIAL_Slicer *slicer,
993 const char *object_filter,
994 GNUNET_SOCIAL_ModifierCallback modifier_cb,
997 struct SlicerModifierCallbacks *cbs = GNUNET_malloc (sizeof *cbs);
998 cbs->modifier_cb = modifier_cb;
1001 struct GNUNET_HashCode key;
1002 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
1003 GNUNET_CONTAINER_multihashmap_put (slicer->modifier_handlers, &key, cbs,
1004 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1009 slicer_modifier_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
1011 struct SlicerModifierRemoveClosure *rm_cls = cls;
1012 struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
1013 struct SlicerModifierCallbacks *rm_cbs = &rm_cls->rm_cbs;
1014 struct SlicerModifierCallbacks *cbs = value;
1016 if (cbs->modifier_cb == rm_cbs->modifier_cb)
1018 GNUNET_CONTAINER_multihashmap_remove (slicer->modifier_handlers, key, cbs);
1027 * Remove a registered modifier from the try-and-slice instance.
1029 * Removes one matching handler registered with the given
1030 * @a object_filter and @a modifier_cb.
1033 * The try-and-slice instance.
1034 * @param object_filter
1035 * Object prefix to match.
1036 * @param modifier_cb
1037 * Function to call when encountering a state modifier changes.
1040 GNUNET_SOCIAL_slicer_modifier_remove (struct GNUNET_SOCIAL_Slicer *slicer,
1041 const char *object_filter,
1042 GNUNET_SOCIAL_ModifierCallback modifier_cb)
1044 struct GNUNET_HashCode key;
1045 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
1047 struct SlicerModifierRemoveClosure rm_cls;
1048 rm_cls.slicer = slicer;
1049 struct SlicerModifierCallbacks *rm_cbs = &rm_cls.rm_cbs;
1050 rm_cbs->modifier_cb = modifier_cb;
1054 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
1055 slicer_modifier_remove,
1063 slicer_method_free (void *cls, const struct GNUNET_HashCode *key, void *value)
1065 struct SlicerMethodCallbacks *cbs = value;
1072 * Destroy a given try-and-slice instance.
1078 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer)
1080 GNUNET_CONTAINER_multihashmap_iterate (slicer->method_handlers,
1081 slicer_method_free, NULL);
1082 GNUNET_CONTAINER_multihashmap_destroy (slicer->method_handlers);
1083 GNUNET_free (slicer);
1090 app_send_connect_msg (struct GNUNET_SOCIAL_App *app)
1092 uint16_t cmsg_size = ntohs (app->connect_msg->size);
1093 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
1094 memcpy (cmsg, app->connect_msg, cmsg_size);
1095 GNUNET_CLIENT_MANAGER_transmit_now (app->client, cmsg);
1100 app_recv_disconnect (void *cls,
1101 struct GNUNET_CLIENT_MANAGER_Connection *client,
1102 const struct GNUNET_MessageHeader *msg)
1104 struct GNUNET_SOCIAL_App *
1105 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
1107 GNUNET_CLIENT_MANAGER_reconnect (client);
1108 app_send_connect_msg (app);
1116 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
1118 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
1119 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
1120 memcpy (cmsg, plc->connect_msg, cmsg_size);
1121 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
1126 place_recv_disconnect (void *cls,
1127 struct GNUNET_CLIENT_MANAGER_Connection *client,
1128 const struct GNUNET_MessageHeader *msg)
1130 struct GNUNET_SOCIAL_Place *
1131 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1133 GNUNET_CLIENT_MANAGER_reconnect (client);
1134 place_send_connect_msg (plc);
1139 place_recv_result (void *cls,
1140 struct GNUNET_CLIENT_MANAGER_Connection *client,
1141 const struct GNUNET_MessageHeader *msg)
1143 struct GNUNET_SOCIAL_Place *
1144 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1146 const struct GNUNET_OperationResultMessage *
1147 res = (const struct GNUNET_OperationResultMessage *) msg;
1149 uint16_t size = ntohs (msg->size);
1150 if (size < sizeof (*res))
1151 { /* Error, message too small. */
1156 uint16_t data_size = size - sizeof (*res);
1157 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
1158 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
1159 GNUNET_ntohll (res->result_code),
1165 app_recv_result (void *cls,
1166 struct GNUNET_CLIENT_MANAGER_Connection *client,
1167 const struct GNUNET_MessageHeader *msg)
1169 struct GNUNET_SOCIAL_App *
1170 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
1172 const struct GNUNET_OperationResultMessage *
1173 res = (const struct GNUNET_OperationResultMessage *) msg;
1175 uint16_t size = ntohs (msg->size);
1176 if (size < sizeof (*res))
1177 { /* Error, message too small. */
1182 uint16_t data_size = size - sizeof (*res);
1183 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
1184 GNUNET_CLIENT_MANAGER_op_result (app->client, GNUNET_ntohll (res->op_id),
1185 GNUNET_ntohll (res->result_code),
1191 op_recv_history_result (void *cls, int64_t result,
1192 const void *err_msg, uint16_t err_msg_size)
1194 LOG (GNUNET_ERROR_TYPE_DEBUG,
1195 "Received history replay result: %" PRId64 ".\n", result);
1197 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
1199 if (NULL != hist->result_cb)
1200 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
1202 GNUNET_PSYC_receive_destroy (hist->recv);
1208 op_recv_state_result (void *cls, int64_t result,
1209 const void *err_msg, uint16_t err_msg_size)
1211 LOG (GNUNET_ERROR_TYPE_DEBUG,
1212 "Received state request result: %" PRId64 ".\n", result);
1214 struct GNUNET_SOCIAL_LookHandle *look = cls;
1216 if (NULL != look->result_cb)
1217 look->result_cb (look->cls, result, err_msg, err_msg_size);
1224 place_recv_history_result (void *cls,
1225 struct GNUNET_CLIENT_MANAGER_Connection *client,
1226 const struct GNUNET_MessageHeader *msg)
1228 struct GNUNET_SOCIAL_Place *
1229 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1231 const struct GNUNET_OperationResultMessage *
1232 res = (const struct GNUNET_OperationResultMessage *) msg;
1233 struct GNUNET_PSYC_MessageHeader *
1234 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
1236 LOG (GNUNET_ERROR_TYPE_DEBUG,
1237 "%p Received historic fragment for message #%" PRIu64 ".\n",
1238 plc, GNUNET_ntohll (pmsg->message_id));
1240 GNUNET_ResultCallback result_cb = NULL;
1241 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
1243 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
1244 GNUNET_ntohll (res->op_id),
1245 &result_cb, (void *) &hist))
1246 { /* Operation not found. */
1247 LOG (GNUNET_ERROR_TYPE_WARNING,
1248 "%p Replay operation not found for historic fragment of message #%"
1250 plc, GNUNET_ntohll (pmsg->message_id));
1254 uint16_t size = ntohs (msg->size);
1255 if (size < sizeof (*res) + sizeof (*pmsg))
1256 { /* Error, message too small. */
1261 GNUNET_PSYC_receive_message (hist->recv,
1262 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
1267 place_recv_state_result (void *cls,
1268 struct GNUNET_CLIENT_MANAGER_Connection *client,
1269 const struct GNUNET_MessageHeader *msg)
1271 struct GNUNET_SOCIAL_Place *
1272 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1274 const struct GNUNET_OperationResultMessage *
1275 res = (const struct GNUNET_OperationResultMessage *) msg;
1277 GNUNET_ResultCallback result_cb = NULL;
1278 struct GNUNET_SOCIAL_LookHandle *look = NULL;
1280 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
1281 GNUNET_ntohll (res->op_id),
1282 &result_cb, (void *) &look))
1283 { /* Operation not found. */
1287 const struct GNUNET_MessageHeader *
1288 mod = (struct GNUNET_MessageHeader *) &res[1];
1289 uint16_t mod_size = ntohs (mod->size);
1290 if (ntohs (msg->size) - sizeof (*res) != mod_size)
1292 GNUNET_break_op (0);
1293 LOG (GNUNET_ERROR_TYPE_WARNING,
1294 "Invalid modifier size in state result: %u - %u != %u\n",
1295 ntohs (msg->size), sizeof (*res), mod_size);
1298 switch (ntohs (mod->type))
1300 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1302 const struct GNUNET_PSYC_MessageModifier *
1303 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
1305 const char *name = (const char *) &pmod[1];
1306 uint16_t name_size = ntohs (pmod->name_size);
1307 if ('\0' != name[name_size - 1])
1309 GNUNET_break_op (0);
1310 LOG (GNUNET_ERROR_TYPE_WARNING,
1311 "Invalid modifier name in state result\n");
1314 look->mod_value_size = ntohs (pmod->value_size);
1315 look->var_cb (look->cls, mod, name, name + name_size,
1316 mod_size - sizeof (*mod) - name_size,
1317 look->mod_value_size);
1318 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
1320 look->mod_value_remaining = look->mod_value_size;
1321 look->mod_name = GNUNET_malloc (name_size);
1322 memcpy (look->mod_name, name, name_size);
1327 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
1328 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
1329 mod_size - sizeof (*mod), look->mod_value_size);
1330 look->mod_value_remaining -= mod_size - sizeof (*mod);
1331 if (0 == look->mod_value_remaining)
1333 GNUNET_free (look->mod_name);
1341 place_recv_message_ack (void *cls,
1342 struct GNUNET_CLIENT_MANAGER_Connection *client,
1343 const struct GNUNET_MessageHeader *msg)
1345 struct GNUNET_SOCIAL_Place *
1346 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1347 GNUNET_PSYC_transmit_got_ack (plc->tmit);
1352 place_recv_message (void *cls,
1353 struct GNUNET_CLIENT_MANAGER_Connection *client,
1354 const struct GNUNET_MessageHeader *msg)
1356 struct GNUNET_SOCIAL_Place *
1357 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1358 GNUNET_PSYC_receive_message (plc->recv,
1359 (const struct GNUNET_PSYC_MessageHeader *) msg);
1364 host_recv_message (void *cls,
1365 struct GNUNET_CLIENT_MANAGER_Connection *client,
1366 const struct GNUNET_MessageHeader *msg)
1368 struct GNUNET_SOCIAL_Host *
1369 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
1370 GNUNET_PSYC_receive_message (hst->recv,
1371 (const struct GNUNET_PSYC_MessageHeader *) msg);
1372 GNUNET_PSYC_receive_message (hst->plc.recv,
1373 (const struct GNUNET_PSYC_MessageHeader *) msg);
1378 host_recv_enter_ack (void *cls,
1379 struct GNUNET_CLIENT_MANAGER_Connection *client,
1380 const struct GNUNET_MessageHeader *msg)
1382 struct GNUNET_SOCIAL_Host *
1383 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1384 sizeof (struct GNUNET_SOCIAL_Place));
1386 struct HostEnterAck *hack = (struct HostEnterAck *) msg;
1387 hst->plc.pub_key = hack->place_pub_key;
1389 int32_t result = ntohl (hack->result_code);
1390 if (NULL != hst->enter_cb)
1391 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
1392 GNUNET_ntohll (hack->max_message_id));
1397 host_recv_enter_request (void *cls,
1398 struct GNUNET_CLIENT_MANAGER_Connection *client,
1399 const struct GNUNET_MessageHeader *msg)
1401 struct GNUNET_SOCIAL_Host *
1402 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1403 sizeof (struct GNUNET_SOCIAL_Place));
1404 if (NULL == hst->answer_door_cb)
1407 const char *method_name = NULL;
1408 struct GNUNET_ENV_Environment *env = NULL;
1409 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
1410 const void *data = NULL;
1411 uint16_t data_size = 0;
1413 const struct GNUNET_PSYC_JoinRequestMessage *
1414 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
1415 const struct GNUNET_PSYC_Message *join_msg = NULL;
1419 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
1421 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1423 "Received join_msg of type %u and size %u.\n",
1424 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
1426 env = GNUNET_ENV_environment_create ();
1427 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
1428 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
1431 GNUNET_break_op (0);
1432 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_key);
1433 LOG (GNUNET_ERROR_TYPE_WARNING,
1434 "Ignoring invalid entry request from nym %s.\n",
1441 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_key);
1442 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
1447 GNUNET_ENV_environment_destroy (env);
1448 if (NULL != entry_pmsg)
1449 GNUNET_free (entry_pmsg);
1454 guest_recv_enter_ack (void *cls,
1455 struct GNUNET_CLIENT_MANAGER_Connection *client,
1456 const struct GNUNET_MessageHeader *msg)
1458 struct GNUNET_SOCIAL_Guest *
1459 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1460 sizeof (struct GNUNET_SOCIAL_Place));
1462 struct GNUNET_PSYC_CountersResultMessage *
1463 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
1464 int32_t result = ntohl (cres->result_code);
1465 if (NULL != gst->enter_cb)
1466 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
1471 guest_recv_join_decision (void *cls,
1472 struct GNUNET_CLIENT_MANAGER_Connection *client,
1473 const struct GNUNET_MessageHeader *msg)
1475 struct GNUNET_SOCIAL_Guest *
1476 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1477 sizeof (struct GNUNET_SOCIAL_Place));
1478 const struct GNUNET_PSYC_JoinDecisionMessage *
1479 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
1481 struct GNUNET_PSYC_Message *pmsg = NULL;
1482 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
1483 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
1485 if (NULL != gst->entry_dcsn_cb)
1486 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
1491 app_recv_ego (void *cls,
1492 struct GNUNET_CLIENT_MANAGER_Connection *client,
1493 const struct GNUNET_MessageHeader *msg)
1495 struct GNUNET_SOCIAL_App *
1496 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
1498 struct AppEgoMessage *
1499 emsg = (struct AppEgoMessage *) msg;
1501 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
1503 struct GNUNET_HashCode ego_pub_hash;
1504 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
1507 struct GNUNET_SOCIAL_Ego *
1508 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
1511 ego = GNUNET_malloc (sizeof (*ego));
1512 ego->pub_key = emsg->ego_pub_key;
1513 ego->name = GNUNET_malloc (name_size);
1514 memcpy (ego->name, &emsg[1], name_size);
1518 ego->name = GNUNET_realloc (ego->name, name_size);
1519 memcpy (ego->name, &emsg[1], name_size);
1522 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
1523 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1525 if (NULL != app->ego_cb)
1526 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
1531 app_recv_place (void *cls,
1532 struct GNUNET_CLIENT_MANAGER_Connection *client,
1533 const struct GNUNET_MessageHeader *msg)
1535 struct GNUNET_SOCIAL_App *
1536 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
1538 struct AppPlaceMessage *
1539 pmsg = (struct AppPlaceMessage *) msg;
1541 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
1542 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
1545 struct GNUNET_HashCode ego_pub_hash;
1546 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
1548 struct GNUNET_SOCIAL_Ego *
1549 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
1556 if (GNUNET_YES == pmsg->is_host)
1558 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
1560 hconn->plc_msg = *pmsg;
1561 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1565 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
1567 gconn->plc_msg = *pmsg;
1568 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1573 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1575 { host_recv_enter_ack, NULL,
1576 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1577 sizeof (struct HostEnterAck), GNUNET_NO },
1579 { host_recv_enter_request, NULL,
1580 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1581 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1583 { host_recv_message, NULL,
1584 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1585 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1587 { place_recv_message_ack, NULL,
1588 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1589 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1591 { place_recv_history_result, NULL,
1592 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1593 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1595 { place_recv_state_result, NULL,
1596 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1597 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1599 { place_recv_result, NULL,
1600 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1601 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1603 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1605 { NULL, NULL, 0, 0, GNUNET_NO }
1609 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1611 { guest_recv_enter_ack, NULL,
1612 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1613 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1615 { host_recv_enter_request, NULL,
1616 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1617 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1619 { place_recv_message, NULL,
1620 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1621 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1623 { place_recv_message_ack, NULL,
1624 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1625 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1627 { guest_recv_join_decision, NULL,
1628 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1629 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1631 { place_recv_history_result, NULL,
1632 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1633 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1635 { place_recv_state_result, NULL,
1636 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1637 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1639 { place_recv_result, NULL,
1640 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1641 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1643 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1645 { NULL, NULL, 0, 0, GNUNET_NO }
1650 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1652 { app_recv_ego, NULL,
1653 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1654 sizeof (struct AppEgoMessage), GNUNET_YES },
1656 { app_recv_place, NULL,
1657 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1658 sizeof (struct AppPlaceMessage), GNUNET_NO },
1660 { app_recv_result, NULL,
1661 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1662 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1664 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1666 { NULL, NULL, 0, 0, GNUNET_NO }
1671 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1673 if (NULL != plc->tmit)
1674 GNUNET_PSYC_transmit_destroy (plc->tmit);
1675 if (NULL != plc->recv)
1676 GNUNET_PSYC_receive_destroy (plc->recv);
1677 if (NULL != plc->connect_msg)
1678 GNUNET_free (plc->connect_msg);
1679 if (NULL != plc->disconnect_cb)
1680 plc->disconnect_cb (plc->disconnect_cls);
1685 host_cleanup (void *cls)
1687 struct GNUNET_SOCIAL_Host *hst = cls;
1688 place_cleanup (&hst->plc);
1689 if (NULL != hst->recv)
1691 GNUNET_PSYC_receive_destroy (hst->recv);
1694 if (NULL != hst->slicer)
1696 GNUNET_SOCIAL_slicer_destroy (hst->slicer);
1704 guest_cleanup (void *cls)
1706 struct GNUNET_SOCIAL_Guest *gst = cls;
1707 place_cleanup (&gst->plc);
1715 * Enter a place as host.
1717 * A place is created upon first entering, and it is active until permanently
1718 * left using GNUNET_SOCIAL_host_leave().
1721 * Application handle.
1723 * Identity of the host.
1725 * Private-public key pair of the place.
1726 * NULL to generate a key.
1728 * Policy specifying entry and history restrictions for the place.
1730 * Slicer to handle incoming messages.
1732 * Function called when the place is entered and ready to use.
1733 * @param answer_door_cb
1734 * Function to handle new nyms that want to enter.
1735 * @param farewell_cb
1736 * Function to handle departing nyms.
1738 * Closure for the callbacks.
1740 * @return Handle for the host.
1742 struct GNUNET_SOCIAL_Host *
1743 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1744 const struct GNUNET_SOCIAL_Ego *ego,
1745 enum GNUNET_PSYC_Policy policy,
1746 struct GNUNET_SOCIAL_Slicer *slicer,
1747 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1748 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1749 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1752 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1753 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1755 plc->cfg = app->cfg;
1756 plc->is_host = GNUNET_YES;
1757 plc->slicer = slicer;
1759 hst->enter_cb = enter_cb;
1760 hst->answer_door_cb = answer_door_cb;
1761 hst->farewell_cb = farewell_cb;
1764 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1765 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1767 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1768 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
1770 hst->slicer = GNUNET_SOCIAL_slicer_create ();
1771 GNUNET_SOCIAL_slicer_method_add (hst->slicer, "_notice_place_leave",
1772 host_recv_notice_place_leave_method,
1773 host_recv_notice_place_leave_modifier,
1774 NULL, host_recv_notice_place_leave_eom, hst);
1775 hst->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, hst->slicer);
1777 uint16_t app_id_size = strlen (app->id) + 1;
1778 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1779 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1780 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1781 hreq->policy = policy;
1782 hreq->ego_pub_key = ego->pub_key;
1783 memcpy (&hreq[1], app->id, app_id_size);
1785 plc->connect_msg = &hreq->header;
1786 place_send_connect_msg (plc);
1793 * Reconnect to an already entered place as host.
1796 * Host connection handle.
1797 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1799 * Slicer to handle incoming messages.
1801 * Function called when the place is entered and ready to use.
1802 * @param answer_door_cb
1803 * Function to handle new nyms that want to enter.
1804 * @param farewell_cb
1805 * Function to handle departing nyms.
1807 * Closure for the callbacks.
1809 * @return Handle for the host.
1811 struct GNUNET_SOCIAL_Host *
1812 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1813 struct GNUNET_SOCIAL_Slicer *slicer,
1814 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1815 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1816 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1819 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1820 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1822 size_t app_id_size = strlen (hconn->app->id) + 1;
1823 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1825 hst->enter_cb = enter_cb;
1826 hst->answer_door_cb = answer_door_cb;
1827 hst->farewell_cb = farewell_cb;
1830 plc->cfg = hconn->app->cfg;
1831 plc->is_host = GNUNET_YES;
1832 plc->slicer = slicer;
1833 plc->pub_key = hconn->plc_msg.place_pub_key;
1834 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1836 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1837 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1839 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1840 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
1842 hst->slicer = GNUNET_SOCIAL_slicer_create ();
1843 GNUNET_SOCIAL_slicer_method_add (hst->slicer, "_notice_place_leave",
1844 host_recv_notice_place_leave_method,
1845 host_recv_notice_place_leave_modifier,
1846 NULL, host_recv_notice_place_leave_eom, hst);
1847 hst->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, hst->slicer);
1849 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1850 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1851 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1852 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1853 memcpy (&hreq[1], hconn->app->id, app_id_size);
1855 plc->connect_msg = &hreq->header;
1856 place_send_connect_msg (plc);
1858 GNUNET_free (hconn);
1864 * Decision whether to admit @a nym into the place or refuse entry.
1867 * Host of the place.
1869 * Handle for the entity that wanted to enter.
1870 * @param is_admitted
1871 * #GNUNET_YES if @a nym is admitted,
1872 * #GNUNET_NO if @a nym is refused entry,
1873 * #GNUNET_SYSERR if we cannot answer the request.
1874 * @param method_name
1875 * Method name for the rejection message.
1877 * Environment containing variables for the message, or NULL.
1879 * Data for the rejection message to send back.
1881 * Number of bytes in @a data for method.
1882 * @return #GNUNET_OK on success,
1883 * #GNUNET_SYSERR if the message is too large.
1886 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1887 struct GNUNET_SOCIAL_Nym *nym,
1889 const struct GNUNET_PSYC_Message *entry_resp)
1891 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1892 uint16_t entry_resp_size
1893 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1895 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1896 return GNUNET_SYSERR;
1898 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1899 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1900 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1901 dcsn->is_admitted = htonl (is_admitted);
1902 dcsn->slave_key = nym->pub_key;
1904 if (0 < entry_resp_size)
1905 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1907 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1913 * Throw @a nym out of the place.
1915 * The @a nym reference will remain valid until the
1916 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1917 * which should be very soon after this call.
1920 * Host of the place.
1922 * Handle for the entity to be ejected.
1924 * Environment for the message or NULL.
1927 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1928 const struct GNUNET_SOCIAL_Nym *nym,
1929 struct GNUNET_ENV_Environment *env)
1932 env = GNUNET_ENV_environment_create ();
1933 GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_SET,
1934 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1935 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1936 GNUNET_SOCIAL_ANNOUNCE_NONE);
1941 * Get the public key of @a ego.
1946 * @return Public key of ego.
1948 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1949 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1951 return &ego->pub_key;
1956 * Get the hash of the public key of @a ego.
1961 * @return Hash of the public key of @a ego.
1963 const struct GNUNET_HashCode *
1964 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1966 return &ego->pub_key_hash;
1971 * Get the name of @a ego.
1976 * @return Public key of @a ego.
1979 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1986 * Get the public key of @a nym.
1988 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1993 * @return Public key of @a nym.
1995 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1996 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1998 return &nym->pub_key;
2003 * Get the hash of the public key of @a nym.
2008 * @return Hash of the public key of @a nym.
2010 const struct GNUNET_HashCode *
2011 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
2013 return &nym->pub_key_hash;
2018 * Send a message to all nyms that are present in the place.
2020 * This function is restricted to the host. Nyms can only send requests
2021 * to the host who can decide to relay it to everyone in the place.
2023 * @param host Host of the place.
2024 * @param method_name Method to use for the announcement.
2025 * @param env Environment containing variables for the message and operations
2026 * on objects of the place. Can be NULL.
2027 * @param notify Function to call to get the payload of the announcement.
2028 * @param notify_cls Closure for @a notify.
2029 * @param flags Flags for this announcement.
2031 * @return NULL on error (announcement already in progress?).
2033 struct GNUNET_SOCIAL_Announcement *
2034 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
2035 const char *method_name,
2036 const struct GNUNET_ENV_Environment *env,
2037 GNUNET_PSYC_TransmitNotifyData notify_data,
2038 void *notify_data_cls,
2039 enum GNUNET_SOCIAL_AnnounceFlags flags)
2042 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
2043 NULL, notify_data, notify_data_cls, flags))
2044 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
2051 * Resume transmitting announcement.
2054 * The announcement to resume.
2057 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
2059 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
2064 * Cancel announcement.
2067 * The announcement to cancel.
2070 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
2072 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
2077 * Obtain handle for a hosted place.
2079 * The returned handle can be used to access the place API.
2081 * @param host Handle for the host.
2083 * @return Handle for the hosted place, valid as long as @a host is valid.
2085 struct GNUNET_SOCIAL_Place *
2086 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
2094 place_leave (struct GNUNET_SOCIAL_Place *plc)
2096 struct GNUNET_MessageHeader msg;
2097 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
2098 msg.size = htons (sizeof (msg));
2099 GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
2104 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
2105 GNUNET_ContinuationCallback disconnect_cb,
2106 void *disconnect_cls)
2108 plc->is_disconnecting = GNUNET_YES;
2109 plc->disconnect_cb = disconnect_cb;
2110 plc->disconnect_cls = disconnect_cls;
2112 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
2113 GNUNET_YES == plc->is_host
2114 ? host_cleanup : guest_cleanup,
2120 * Disconnect from a home.
2122 * Invalidates host handle.
2125 * The host to disconnect.
2128 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
2129 GNUNET_ContinuationCallback disconnect_cb,
2132 place_disconnect (&hst->plc, disconnect_cb, cls);
2137 * Stop hosting the home.
2139 * Sends a _notice_place_closed announcement to the home.
2140 * Invalidates host handle.
2145 * Environment for the message or NULL.
2146 * _nym is set to @e nym regardless whether an @e env is provided.
2147 * @param disconnect_cb
2148 * Function called after the host left the place
2149 * and disconnected from the social service.
2151 * Closure for @a disconnect_cb.
2154 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
2155 const struct GNUNET_ENV_Environment *env,
2156 GNUNET_ContinuationCallback disconnect_cb,
2159 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closed", env, NULL, NULL,
2160 GNUNET_SOCIAL_ANNOUNCE_NONE);
2161 place_leave (&hst->plc);
2162 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
2168 static struct GuestEnterRequest *
2169 guest_enter_request_create (const char *app_id,
2170 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
2171 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2172 const struct GNUNET_PeerIdentity *origin,
2174 const struct GNUNET_PeerIdentity *relays,
2175 const struct GNUNET_PSYC_Message *join_msg)
2177 uint16_t app_id_size = strlen (app_id) + 1;
2178 uint16_t join_msg_size = ntohs (join_msg->header.size);
2179 uint16_t relay_size = relay_count * sizeof (*relays);
2181 struct GuestEnterRequest *
2182 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
2184 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
2185 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
2186 greq->place_pub_key = *place_pub_key;
2187 greq->ego_pub_key = *ego_pub_key;
2188 greq->origin = *origin;
2189 greq->relay_count = htonl (relay_count);
2191 char *p = (char *) &greq[1];
2192 memcpy (p, app_id, app_id_size);
2197 memcpy (p, relays, relay_size);
2201 memcpy (p, join_msg, join_msg_size);
2207 * Request entry to a place as a guest.
2210 * Application handle.
2212 * Identity of the guest.
2213 * @param place_pub_key
2214 * Public key of the place to enter.
2216 * Flags for the entry.
2218 * Peer identity of the origin of the underlying multicast group.
2219 * @param relay_count
2220 * Number of elements in the @a relays array.
2222 * Relays for the underlying multicast group.
2223 * @param method_name
2224 * Method name for the message.
2226 * Environment containing variables for the message, or NULL.
2228 * Payload for the message to give to the enter callback.
2230 * Number of bytes in @a data.
2232 * Slicer to use for processing incoming requests from guests.
2234 * @return NULL on errors, otherwise handle for the guest.
2236 struct GNUNET_SOCIAL_Guest *
2237 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
2238 const struct GNUNET_SOCIAL_Ego *ego,
2239 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2240 enum GNUNET_PSYC_SlaveJoinFlags flags,
2241 const struct GNUNET_PeerIdentity *origin,
2242 uint32_t relay_count,
2243 const struct GNUNET_PeerIdentity *relays,
2244 const struct GNUNET_PSYC_Message *entry_msg,
2245 struct GNUNET_SOCIAL_Slicer *slicer,
2246 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
2247 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
2250 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
2251 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2253 plc->ego_pub_key = ego->pub_key;
2254 plc->pub_key = *place_pub_key;
2255 plc->cfg = app->cfg;
2256 plc->is_host = GNUNET_YES;
2257 plc->slicer = slicer;
2259 gst->enter_cb = local_enter_cb;
2260 gst->entry_dcsn_cb = entry_dcsn_cb;
2263 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
2264 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
2266 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
2267 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2269 struct GuestEnterRequest *
2270 greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
2271 origin, relay_count, relays, entry_msg);
2272 plc->connect_msg = &greq->header;
2273 place_send_connect_msg (plc);
2279 * Request entry to a place by name as a guest.
2282 * Application handle.
2284 * Identity of the guest.
2286 * GNS name of the place to enter. Either in the form of
2287 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
2288 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
2289 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
2290 * pseudonym's place directly.
2292 * Password to decrypt the record, or NULL for cleartext records.
2294 * Entry request message or NULL.
2296 * Slicer to use for processing incoming requests from guests.
2297 * @param local_enter_cb
2298 * Called upon connection established to the social service.
2299 * @param entry_decision_cb
2300 * Called upon receiving entry decision.
2302 * @return NULL on errors, otherwise handle for the guest.
2304 struct GNUNET_SOCIAL_Guest *
2305 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
2306 const struct GNUNET_SOCIAL_Ego *ego,
2307 const char *gns_name,
2308 const char *password,
2309 const struct GNUNET_PSYC_Message *join_msg,
2310 struct GNUNET_SOCIAL_Slicer *slicer,
2311 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
2312 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
2315 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
2316 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2318 if (NULL == password)
2321 uint16_t app_id_size = strlen (app->id) + 1;
2322 uint16_t gns_name_size = strlen (gns_name) + 1;
2323 uint16_t password_size = strlen (password) + 1;
2325 uint16_t join_msg_size = 0;
2326 if (NULL != join_msg);
2327 join_msg_size = ntohs (join_msg->header.size);
2329 uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
2330 + app_id_size + gns_name_size + password_size + join_msg_size;
2331 struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
2332 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
2333 greq->header.size = htons (greq_size);
2334 greq->ego_pub_key = ego->pub_key;
2336 char *p = (char *) &greq[1];
2337 memcpy (p, app->id, app_id_size);
2339 memcpy (p, gns_name, gns_name_size);
2341 memcpy (p, password, password_size);
2343 if (NULL != join_msg)
2344 memcpy (p, join_msg, join_msg_size);
2346 gst->enter_cb = local_enter_cb;
2347 gst->entry_dcsn_cb = entry_decision_cb;
2350 plc->ego_pub_key = ego->pub_key;
2351 plc->cfg = app->cfg;
2352 plc->is_host = GNUNET_NO;
2353 plc->slicer = slicer;
2355 plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
2356 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
2358 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
2359 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2361 plc->connect_msg = &greq->header;
2362 place_send_connect_msg (plc);
2369 * Reconnect to an already entered place as guest.
2372 * Guest connection handle.
2373 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
2375 * Flags for the entry.
2377 * Slicer to use for processing incoming requests from guests.
2378 * @param local_enter_cb
2379 * Called upon connection established to the social service.
2380 * @param entry_decision_cb
2381 * Called upon receiving entry decision.
2383 * @return NULL on errors, otherwise handle for the guest.
2385 struct GNUNET_SOCIAL_Guest *
2386 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
2387 enum GNUNET_PSYC_SlaveJoinFlags flags,
2388 struct GNUNET_SOCIAL_Slicer *slicer,
2389 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
2392 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
2393 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2395 uint16_t app_id_size = strlen (gconn->app->id) + 1;
2396 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
2397 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
2398 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
2399 greq->header.size = htons (greq_size);
2400 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
2401 greq->place_pub_key = gconn->plc_msg.place_pub_key;
2402 greq->flags = htonl (flags);
2404 memcpy (&greq[1], gconn->app->id, app_id_size);
2406 gst->enter_cb = local_enter_cb;
2409 plc->cfg = gconn->app->cfg;
2410 plc->is_host = GNUNET_NO;
2411 plc->slicer = slicer;
2412 plc->pub_key = gconn->plc_msg.place_pub_key;
2413 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
2415 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
2416 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
2418 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
2419 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2421 plc->connect_msg = &greq->header;
2422 place_send_connect_msg (plc);
2424 GNUNET_free (gconn);
2430 * Talk to the host of the place.
2433 * Place where we want to talk to the host.
2434 * @param method_name
2435 * Method to invoke on the host.
2437 * Environment containing variables for the message, or NULL.
2438 * @param notify_data
2439 * Function to use to get the payload for the method.
2440 * @param notify_data_cls
2441 * Closure for @a notify_data.
2443 * Flags for the message being sent.
2445 * @return NULL if we are already trying to talk to the host,
2446 * otherwise handle to cancel the request.
2448 struct GNUNET_SOCIAL_TalkRequest *
2449 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2450 const char *method_name,
2451 const struct GNUNET_ENV_Environment *env,
2452 GNUNET_PSYC_TransmitNotifyData notify_data,
2453 void *notify_data_cls,
2454 enum GNUNET_SOCIAL_TalkFlags flags)
2456 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2457 GNUNET_assert (NULL != plc->tmit);
2460 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2461 NULL, notify_data, notify_data_cls, flags))
2462 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2469 * Resume talking to the host of the place.
2472 * Talk request to resume.
2475 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2477 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2482 * Cancel talking to the host of the place.
2485 * Talk request to cancel.
2488 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2490 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2495 * Disconnect from a place.
2497 * Invalidates guest handle.
2500 * The guest to disconnect.
2503 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2504 GNUNET_ContinuationCallback disconnect_cb,
2507 place_disconnect (&gst->plc, disconnect_cb, cls);
2512 * Leave a place temporarily or permanently.
2514 * Notifies the owner of the place about leaving, and destroys the place handle.
2518 * @param keep_active
2519 * Keep place active after last application disconnected.
2520 * #GNUNET_YES or #GNUNET_NO
2522 * Optional environment for the leave message if @a keep_active
2523 * is #GNUNET_NO. NULL if not needed.
2525 * Called upon disconnecting from the social service.
2528 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2529 struct GNUNET_ENV_Environment *env,
2530 GNUNET_ContinuationCallback disconnect_cb,
2533 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2534 GNUNET_SOCIAL_TALK_NONE);
2535 place_leave (&gst->plc);
2536 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
2541 * Obtain handle for a place entered as guest.
2543 * The returned handle can be used to access the place API.
2545 * @param guest Handle for the guest.
2547 * @return Handle for the place, valid as long as @a guest is valid.
2549 struct GNUNET_SOCIAL_Place *
2550 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2557 * Obtain the public key of a place.
2562 * @return Public key of the place.
2564 const struct GNUNET_CRYPTO_EddsaPublicKey *
2565 GNUNET_SOCIAL_place_get_key (struct GNUNET_SOCIAL_Place *plc)
2567 return &plc->pub_key;
2571 static struct GNUNET_SOCIAL_HistoryRequest *
2572 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2573 uint64_t start_message_id,
2574 uint64_t end_message_id,
2575 uint64_t message_limit,
2576 const char *method_prefix,
2578 struct GNUNET_SOCIAL_Slicer *slicer,
2579 GNUNET_ResultCallback result_cb,
2582 struct GNUNET_PSYC_HistoryRequestMessage *req;
2583 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2585 hist->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, slicer);
2586 hist->result_cb = result_cb;
2588 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2589 &op_recv_history_result, hist);
2591 GNUNET_assert (NULL != method_prefix);
2592 uint16_t method_size = strnlen (method_prefix,
2593 GNUNET_SERVER_MAX_MESSAGE_SIZE
2594 - sizeof (*req)) + 1;
2595 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2596 req = GNUNET_malloc (sizeof (*req) + method_size);
2597 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2598 req->header.size = htons (sizeof (*req) + method_size);
2599 req->start_message_id = GNUNET_htonll (start_message_id);
2600 req->end_message_id = GNUNET_htonll (end_message_id);
2601 req->message_limit = GNUNET_htonll (message_limit);
2602 req->flags = htonl (flags);
2603 req->op_id = GNUNET_htonll (hist->op_id);
2604 memcpy (&req[1], method_prefix, method_size);
2606 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2612 * Learn about the history of a place.
2614 * Messages are returned through the @a slicer function
2615 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2618 * Place we want to learn more about.
2619 * @param start_message_id
2620 * First historic message we are interested in.
2621 * @param end_message_id
2622 * Last historic message we are interested in (inclusive).
2623 * @param method_prefix
2624 * Only retrieve messages with this method prefix.
2626 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2628 * Slicer to use for retrieved messages.
2629 * Can be the same as the slicer of the place.
2631 * Function called after all messages retrieved.
2632 * NULL if not needed.
2633 * @param cls Closure for @a result_cb.
2635 struct GNUNET_SOCIAL_HistoryRequest *
2636 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2637 uint64_t start_message_id,
2638 uint64_t end_message_id,
2639 const char *method_prefix,
2641 struct GNUNET_SOCIAL_Slicer *slicer,
2642 GNUNET_ResultCallback result_cb,
2645 return place_history_replay (plc, start_message_id, end_message_id, 0,
2646 method_prefix, flags, slicer, result_cb, cls);
2651 * Learn about the history of a place.
2653 * Sends messages through the slicer function of the place where
2654 * start_message_id <= message_id <= end_message_id.
2655 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2657 * To get the latest message, use 0 for both the start and end message ID.
2660 * Place we want to learn more about.
2661 * @param message_limit
2662 * Maximum number of historic messages we are interested in.
2663 * @param method_prefix
2664 * Only retrieve messages with this method prefix.
2666 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2668 * Function called after all messages retrieved.
2669 * NULL if not needed.
2670 * @param cls Closure for @a result_cb.
2672 struct GNUNET_SOCIAL_HistoryRequest *
2673 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2674 uint64_t message_limit,
2675 const char *method_prefix,
2677 struct GNUNET_SOCIAL_Slicer *slicer,
2678 GNUNET_ResultCallback result_cb,
2681 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2682 slicer, result_cb, cls);
2687 * Cancel learning about the history of a place.
2690 * History lesson to cancel.
2693 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2695 GNUNET_PSYC_receive_destroy (hist->recv);
2696 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2702 * Request matching state variables.
2704 static struct GNUNET_SOCIAL_LookHandle *
2705 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2706 uint16_t type, const char *name,
2707 GNUNET_PSYC_StateVarCallback var_cb,
2708 GNUNET_ResultCallback result_cb, void *cls)
2710 struct GNUNET_PSYC_StateRequestMessage *req;
2711 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2713 look->var_cb = var_cb;
2714 look->result_cb = result_cb;
2716 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2717 &op_recv_state_result, look);
2719 GNUNET_assert (NULL != name);
2720 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2721 - sizeof (*req)) + 1;
2722 req = GNUNET_malloc (sizeof (*req) + name_size);
2723 req->header.type = htons (type);
2724 req->header.size = htons (sizeof (*req) + name_size);
2725 req->op_id = GNUNET_htonll (look->op_id);
2726 memcpy (&req[1], name, name_size);
2728 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2734 * Look at a particular object in the place.
2736 * The best matching object is returned (its name might be less specific than
2737 * what was requested).
2740 * The place where to look.
2742 * Full name of the object.
2744 * Set to the size of the returned value.
2746 * @return NULL if there is no such object at this place.
2748 struct GNUNET_SOCIAL_LookHandle *
2749 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2750 const char *full_name,
2751 GNUNET_PSYC_StateVarCallback var_cb,
2752 GNUNET_ResultCallback result_cb,
2755 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2756 full_name, var_cb, result_cb, cls);
2761 * Look for objects in the place with a matching name prefix.
2764 * The place where to look.
2765 * @param name_prefix
2766 * Look at objects with names beginning with this value.
2768 * Function to call for each object found.
2770 * Closure for callback function.
2772 * @return Handle that can be used to stop looking at objects.
2774 struct GNUNET_SOCIAL_LookHandle *
2775 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2776 const char *name_prefix,
2777 GNUNET_PSYC_StateVarCallback var_cb,
2778 GNUNET_ResultCallback result_cb,
2781 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2782 name_prefix, var_cb, result_cb, cls);
2787 * Cancel a state request operation.
2790 * Handle for the operation to cancel.
2793 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2795 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2801 op_recv_zone_add_place_result (void *cls, int64_t result,
2802 const void *err_msg, uint16_t err_msg_size)
2804 LOG (GNUNET_ERROR_TYPE_DEBUG,
2805 "Received zone add place result: %" PRId64 ".\n", result);
2807 struct ZoneAddPlaceHandle *add_plc = cls;
2808 if (NULL != add_plc->result_cb)
2809 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2811 GNUNET_free (add_plc->req);
2812 GNUNET_free (add_plc);
2817 * Advertise @e place in the GNS zone of @e ego.
2820 * Application handle.
2823 * @param place_pub_key
2824 * Public key of place to add.
2826 * The name for the PLACE record to put in the zone.
2828 * Password used to encrypt the record or NULL to keep it cleartext.
2829 * @param relay_count
2830 * Number of elements in the @a relays array.
2832 * List of relays to put in the PLACE record to advertise
2833 * as entry points to the place in addition to the origin.
2834 * @param expiration_time
2835 * Expiration time of the record, use 0 to remove the record.
2837 * Function called with the result of the operation.
2839 * Closure for @a result_cb
2841 * @return #GNUNET_OK if the request was sent,
2842 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2845 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2846 const struct GNUNET_SOCIAL_Ego *ego,
2848 const char *password,
2849 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2850 const struct GNUNET_PeerIdentity *origin,
2851 uint32_t relay_count,
2852 const struct GNUNET_PeerIdentity *relays,
2853 struct GNUNET_TIME_Absolute expiration_time,
2854 GNUNET_ResultCallback result_cb,
2857 struct ZoneAddPlaceRequest *preq;
2858 size_t name_size = strlen (name) + 1;
2859 size_t password_size = strlen (password) + 1;
2860 size_t relay_size = relay_count * sizeof (*relays);
2861 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2863 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2864 return GNUNET_SYSERR;
2866 preq = GNUNET_malloc (preq_size);
2867 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2868 preq->header.size = htons (preq_size);
2869 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2870 preq->ego_pub_key = ego->pub_key;
2871 preq->place_pub_key = *place_pub_key;
2872 preq->origin = *origin;
2873 preq->relay_count = htonl (relay_count);
2875 char *p = (char *) &preq[1];
2876 memcpy (p, name, name_size);
2878 memcpy (p, password, password_size);
2880 memcpy (p, relays, relay_size);
2882 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2883 add_plc->req = preq;
2884 add_plc->result_cb = result_cb;
2885 add_plc->result_cls = result_cls;
2887 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2888 op_recv_zone_add_place_result,
2890 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2896 op_recv_zone_add_nym_result (void *cls, int64_t result,
2897 const void *err_msg, uint16_t err_msg_size)
2899 LOG (GNUNET_ERROR_TYPE_DEBUG,
2900 "Received zone add nym result: %" PRId64 ".\n", result);
2902 struct ZoneAddNymHandle *add_nym = cls;
2903 if (NULL != add_nym->result_cb)
2904 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2906 GNUNET_free (add_nym->req);
2907 GNUNET_free (add_nym);
2912 * Add nym to the GNS zone of @e ego.
2919 * The name for the PKEY record to put in the zone.
2920 * @param nym_pub_key
2921 * Public key of nym to add.
2922 * @param expiration_time
2923 * Expiration time of the record, use 0 to remove the record.
2925 * Function called with the result of the operation.
2927 * Closure for @a result_cb
2929 * @return #GNUNET_OK if the request was sent,
2930 * #GNUNET_SYSERR on error, e.g. the name is too long.
2933 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2934 const struct GNUNET_SOCIAL_Ego *ego,
2936 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2937 struct GNUNET_TIME_Absolute expiration_time,
2938 GNUNET_ResultCallback result_cb,
2941 struct ZoneAddNymRequest *nreq;
2943 size_t name_size = strlen (name) + 1;
2944 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2945 return GNUNET_SYSERR;
2947 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2948 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2949 nreq->header.size = htons (sizeof (*nreq) + name_size);
2950 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2951 nreq->ego_pub_key = ego->pub_key;
2952 nreq->nym_pub_key = *nym_pub_key;
2953 memcpy (&nreq[1], name, name_size);
2955 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2956 add_nym->req = nreq;
2957 add_nym->result_cb = result_cb;
2958 add_nym->result_cls = result_cls;
2960 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2961 op_recv_zone_add_nym_result,
2963 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2969 * Connect application to the social service.
2971 * The @host_place_cb and @guest_place_cb functions are
2972 * initially called for each entered places,
2973 * then later each time a new place is entered with the current application ID.
2979 * @param notify_host
2980 * Function to notify about a place entered as host.
2981 * @param notify_guest
2982 * Function to notify about a place entered as guest..
2984 * Closure for the callbacks.
2986 * @return Handle that can be used to stop listening.
2988 struct GNUNET_SOCIAL_App *
2989 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2991 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2992 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2993 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2996 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2997 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
3001 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
3003 app->ego_cb = ego_cb;
3004 app->host_cb = host_cb;
3005 app->guest_cb = guest_cb;
3007 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
3008 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
3010 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
3012 app->id = GNUNET_malloc (app_id_size);
3013 memcpy (app->id, id, app_id_size);
3015 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
3016 creq->header.size = htons (sizeof (*creq) + app_id_size);
3017 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
3018 memcpy (&creq[1], app->id, app_id_size);
3020 app->connect_msg = &creq->header;
3021 app_send_connect_msg (app);
3028 * Disconnect application.
3031 * Application handle.
3034 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app)
3036 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO, NULL, NULL);
3041 * Detach application from a place.
3043 * Removes the place from the entered places list for this application.
3044 * Note: this does not disconnect from the place.
3046 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
3054 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
3055 struct GNUNET_SOCIAL_Place *plc)
3057 struct AppDetachRequest dreq;
3058 dreq.header.size = htons (sizeof (dreq));
3059 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
3060 dreq.place_pub_key = plc->pub_key;
3061 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
3065 /* end of social_api.c */