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 * Handle for place notifications.
195 struct GNUNET_SOCIAL_PlaceListenHandle
197 struct GNUNET_SOCIAL_Place plc;
199 GNUNET_SOCIAL_PlaceNotifyHostCallback notify_host;
200 GNUNET_SOCIAL_PlaceNotifyGuestCallback notify_guest;
206 * Hash map of all nyms.
207 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
209 struct GNUNET_CONTAINER_MultiHashMap *nyms;
213 * Handle for a try-and-slice instance.
215 struct GNUNET_SOCIAL_Slicer
218 * Method handlers: method_name -> SlicerMethodCallbacks
220 struct GNUNET_CONTAINER_MultiHashMap *method_handlers;
223 * Modifier handlers: modifier name -> SlicerModifierCallbacks
225 struct GNUNET_CONTAINER_MultiHashMap *modifier_handlers;
228 * Currently being processed message part.
230 const struct GNUNET_MessageHeader *msg;
233 * ID of currently being received message.
238 * Method name of currently being received message.
243 * Name of currently processed modifier.
248 * Value of currently processed modifier.
253 * Public key of the nym the current message originates from.
255 struct GNUNET_CRYPTO_EcdsaPublicKey nym_key;
258 * Size of @a method_name (including terminating \0).
260 uint16_t method_name_size;
263 * Size of @a modifier_name (including terminating \0).
265 uint16_t mod_name_size;
268 * Size of modifier value fragment.
270 uint16_t mod_value_size;
273 * Full size of modifier value.
275 uint16_t mod_full_value_size;
278 * Remaining bytes from the value of the current modifier.
280 uint16_t mod_value_remaining;
283 * Operator of currently processed modifier.
290 * Callbacks for a slicer method handler.
292 struct SlicerMethodCallbacks
294 GNUNET_SOCIAL_MethodCallback method_cb;
295 GNUNET_SOCIAL_ModifierCallback modifier_cb;
296 GNUNET_SOCIAL_DataCallback data_cb;
297 GNUNET_SOCIAL_EndOfMessageCallback eom_cb;
302 struct SlicerMethodRemoveClosure
304 struct GNUNET_SOCIAL_Slicer *slicer;
305 struct SlicerMethodCallbacks rm_cbs;
310 * Callbacks for a slicer method handler.
312 struct SlicerModifierCallbacks
314 GNUNET_SOCIAL_ModifierCallback modifier_cb;
319 struct SlicerModifierRemoveClosure
321 struct GNUNET_SOCIAL_Slicer *slicer;
322 struct SlicerModifierCallbacks rm_cbs;
327 * Handle for an announcement request.
329 struct GNUNET_SOCIAL_Announcement
338 struct GNUNET_SOCIAL_TalkRequest
347 struct GNUNET_SOCIAL_HistoryRequest
352 struct GNUNET_SOCIAL_Place *plc;
362 struct GNUNET_PSYC_ReceiveHandle *recv;
365 * Function to call when the operation finished.
367 GNUNET_ResultCallback result_cb;
370 * Closure for @a result_cb.
376 struct GNUNET_SOCIAL_LookHandle
381 struct GNUNET_SOCIAL_Place *plc;
389 * State variable result callback.
391 GNUNET_PSYC_StateVarCallback var_cb;
394 * Function to call when the operation finished.
396 GNUNET_ResultCallback result_cb;
399 * Name of current modifier being received.
404 * Size of current modifier value being received.
406 size_t mod_value_size;
409 * Remaining size of current modifier value still to be received.
411 size_t mod_value_remaining;
414 * Closure for @a result_cb.
422 static struct GNUNET_SOCIAL_Nym *
423 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
425 struct GNUNET_SOCIAL_Nym *nym = NULL;
426 struct GNUNET_HashCode pub_key_hash;
431 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
434 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
436 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
440 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
441 nym->pub_key = *pub_key;
442 nym->pub_key_hash = pub_key_hash;
443 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
444 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
451 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
453 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
458 /*** MESSAGE HANDLERS ***/
460 /** _notice_place_leave from guests */
463 host_recv_notice_place_leave_method (void *cls,
464 const struct GNUNET_PSYC_MessageMethod *meth,
467 const struct GNUNET_SOCIAL_Nym *nym,
468 const char *method_name)
470 struct GNUNET_SOCIAL_Host *hst = cls;
471 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
472 &nym->pub_key, sizeof (nym->pub_key)))
475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
476 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
477 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
479 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
480 hst->notice_place_leave_env = GNUNET_ENV_environment_create ();
482 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
483 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
484 "_notice_place_leave: got method from nym %s (%s).\n",
485 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
490 host_recv_notice_place_leave_modifier (void *cls,
491 const struct GNUNET_MessageHeader *msg,
493 enum GNUNET_ENV_Operator oper,
497 uint16_t full_value_size)
499 struct GNUNET_SOCIAL_Host *hst = cls;
500 if (NULL == hst->notice_place_leave_env)
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
504 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
506 message_id, oper, name, value_size, value);
508 /* skip _nym, it's added later in eom() */
509 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
510 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
513 GNUNET_ENV_environment_add (hst->notice_place_leave_env,
514 GNUNET_ENV_OP_SET, name, value, value_size);
519 host_recv_notice_place_leave_eom (void *cls,
520 const struct GNUNET_MessageHeader *msg,
524 struct GNUNET_SOCIAL_Host *hst = cls;
525 if (NULL == hst->notice_place_leave_env)
528 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
529 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
530 "_notice_place_leave: got EOM from nym %s (%s).\n",
531 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
533 if (GNUNET_YES != cancelled)
535 if (NULL != hst->farewell_cb)
536 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
537 hst->notice_place_leave_env);
538 /* announce leaving guest to place */
539 GNUNET_ENV_environment_add (hst->notice_place_leave_env, GNUNET_ENV_OP_SET,
540 "_nym", hst->notice_place_leave_nym,
541 sizeof (*hst->notice_place_leave_nym));
542 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
543 hst->notice_place_leave_env,
544 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
545 nym_destroy (hst->notice_place_leave_nym);
547 GNUNET_ENV_environment_destroy (hst->notice_place_leave_env);
548 hst->notice_place_leave_env = NULL;
555 * Call a method handler for an incoming message part.
558 slicer_method_handler_notify (void *cls, const struct GNUNET_HashCode *key,
561 struct GNUNET_SOCIAL_Slicer *slicer = cls;
562 const struct GNUNET_MessageHeader *msg = slicer->msg;
563 struct SlicerMethodCallbacks *cbs = value;
564 uint16_t ptype = ntohs (msg->type);
568 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
570 if (NULL == cbs->method_cb)
572 struct GNUNET_PSYC_MessageMethod *
573 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
574 cbs->method_cb (cbs->cls, meth, slicer->message_id,
576 nym_get_or_create (&slicer->nym_key),
577 slicer->method_name);
581 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
583 if (NULL == cbs->modifier_cb)
585 struct GNUNET_PSYC_MessageModifier *
586 mod = (struct GNUNET_PSYC_MessageModifier *) msg;
587 cbs->modifier_cb (cbs->cls, &mod->header, slicer->message_id,
588 mod->oper, (const char *) &mod[1],
589 (const void *) &mod[1] + ntohs (mod->name_size),
590 ntohs (mod->header.size) - sizeof (*mod) - ntohs (mod->name_size),
591 ntohs (mod->value_size));
595 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
597 if (NULL == cbs->modifier_cb)
599 cbs->modifier_cb (cbs->cls, msg, slicer->message_id,
600 slicer->mod_oper, slicer->mod_name, &msg[1],
601 ntohs (msg->size) - sizeof (*msg),
602 slicer->mod_full_value_size);
606 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
608 if (NULL == cbs->data_cb)
610 uint64_t data_offset = 0; // FIXME
611 cbs->data_cb (cbs->cls, msg, slicer->message_id,
612 data_offset, &msg[1], ntohs (msg->size) - sizeof (*msg));
616 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
617 if (NULL == cbs->eom_cb)
619 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_NO);
622 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
623 if (NULL == cbs->eom_cb)
625 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_YES);
633 * Call a method handler for an incoming message part.
636 slicer_modifier_handler_notify (void *cls, const struct GNUNET_HashCode *key,
639 struct GNUNET_SOCIAL_Slicer *slicer = cls;
640 struct SlicerModifierCallbacks *cbs = value;
642 cbs->modifier_cb (cbs->cls, slicer->msg, slicer->message_id, slicer->mod_oper,
643 slicer->mod_name, slicer->mod_value,
644 slicer->mod_value_size, slicer->mod_full_value_size);
650 * Process an incoming message part and call matching handlers.
657 * Flags for the message.
658 * @see enum GNUNET_PSYC_MessageFlags
660 * The message part. as it arrived from the network.
663 slicer_message (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
664 uint64_t message_id, uint32_t flags, uint64_t fragment_offset,
665 const struct GNUNET_MessageHeader *msg)
667 struct GNUNET_SOCIAL_Slicer *slicer = cls;
668 slicer->nym_key = *slave_key;
670 uint16_t ptype = ntohs (msg->type);
671 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
673 struct GNUNET_PSYC_MessageMethod *
674 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
675 slicer->method_name_size = ntohs (meth->header.size) - sizeof (*meth);
676 slicer->method_name = GNUNET_malloc (slicer->method_name_size);
677 memcpy (slicer->method_name, &meth[1], slicer->method_name_size);
678 slicer->message_id = message_id;
682 GNUNET_assert (message_id == slicer->message_id);
685 char *nym_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (slave_key);
686 LOG (GNUNET_ERROR_TYPE_DEBUG,
687 "Slicer received message of type %u and size %u, "
688 "with ID %" PRIu64 " and method %s from %s\n",
689 ptype, ntohs (msg->size), message_id, slicer->method_name, nym_str);
690 GNUNET_free (nym_str);
694 /* try-and-slice modifier */
698 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
700 struct GNUNET_PSYC_MessageModifier *
701 mod = (struct GNUNET_PSYC_MessageModifier *) msg;
702 slicer->mod_oper = mod->oper;
703 slicer->mod_name_size = ntohs (mod->name_size);
704 slicer->mod_name = GNUNET_malloc (slicer->mod_name_size);
705 memcpy (slicer->mod_name, &mod[1], slicer->mod_name_size);
706 slicer->mod_value = (char *) &mod[1] + slicer->mod_name_size;
707 slicer->mod_full_value_size = ntohs (mod->value_size);
708 slicer->mod_value_remaining = slicer->mod_full_value_size;
709 slicer->mod_value_size
710 = ntohs (mod->header.size) - sizeof (*mod) - slicer->mod_name_size;
712 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
713 if (ptype == GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT)
715 slicer->mod_value = (char *) &msg[1];
716 slicer->mod_value_size = ntohs (msg->size) - sizeof (*msg);
718 slicer->mod_value_remaining -= slicer->mod_value_size;
719 char *name = GNUNET_malloc (slicer->mod_name_size);
720 memcpy (name, slicer->mod_name, slicer->mod_name_size);
723 struct GNUNET_HashCode key;
724 uint16_t name_len = strlen (name);
725 GNUNET_CRYPTO_hash (name, name_len, &key);
726 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
727 slicer_modifier_handler_notify,
729 char *p = strrchr (name, '_');
737 /* try-and-slice method */
739 char *name = GNUNET_malloc (slicer->method_name_size);
740 memcpy (name, slicer->method_name, slicer->method_name_size);
743 struct GNUNET_HashCode key;
744 uint16_t name_len = strlen (name);
745 GNUNET_CRYPTO_hash (name, name_len, &key);
746 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
747 slicer_method_handler_notify,
749 char *p = strrchr (name, '_');
756 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
757 GNUNET_free (slicer->method_name);
759 if (0 == slicer->mod_value_remaining && NULL != slicer->mod_name)
761 GNUNET_free (slicer->mod_name);
762 slicer->mod_name = NULL;
763 slicer->mod_name_size = 0;
764 slicer->mod_value_size = 0;
765 slicer->mod_full_value_size = 0;
766 slicer->mod_oper = 0;
774 * Create a try-and-slice instance.
776 * A slicer processes incoming messages and notifies callbacks about matching
777 * methods or modifiers encountered.
779 * @return A new try-and-slice construct.
781 struct GNUNET_SOCIAL_Slicer *
782 GNUNET_SOCIAL_slicer_create (void)
784 struct GNUNET_SOCIAL_Slicer *slicer = GNUNET_malloc (sizeof (*slicer));
785 slicer->method_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
786 slicer->modifier_handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
792 * Add a method to the try-and-slice instance.
794 * The callbacks are called for messages with a matching @a method_name prefix.
797 * The try-and-slice instance to extend.
799 * Name of the given method, use empty string to match all.
801 * Method handler invoked upon a matching message.
803 * Modifier handler, invoked after @a method_cb
804 * for each modifier in the message.
806 * Data handler, invoked after @a modifier_cb for each data fragment.
808 * Invoked upon reaching the end of a matching message.
810 * Closure for the callbacks.
813 GNUNET_SOCIAL_slicer_method_add (struct GNUNET_SOCIAL_Slicer *slicer,
814 const char *method_name,
815 GNUNET_SOCIAL_MethodCallback method_cb,
816 GNUNET_SOCIAL_ModifierCallback modifier_cb,
817 GNUNET_SOCIAL_DataCallback data_cb,
818 GNUNET_SOCIAL_EndOfMessageCallback eom_cb,
821 struct GNUNET_HashCode key;
822 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
824 struct SlicerMethodCallbacks *cbs = GNUNET_malloc (sizeof (*cbs));
825 cbs->method_cb = method_cb;
826 cbs->modifier_cb = modifier_cb;
827 cbs->data_cb = data_cb;
828 cbs->eom_cb = eom_cb;
831 GNUNET_CONTAINER_multihashmap_put (slicer->method_handlers, &key, cbs,
832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
837 slicer_method_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
839 struct SlicerMethodRemoveClosure *rm_cls = cls;
840 struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
841 struct SlicerMethodCallbacks *rm_cbs = &rm_cls->rm_cbs;
842 struct SlicerMethodCallbacks *cbs = value;
844 if (cbs->method_cb == rm_cbs->method_cb
845 && cbs->modifier_cb == rm_cbs->modifier_cb
846 && cbs->data_cb == rm_cbs->data_cb
847 && cbs->eom_cb == rm_cbs->eom_cb)
849 GNUNET_CONTAINER_multihashmap_remove (slicer->method_handlers, key, cbs);
858 * Remove a registered method from the try-and-slice instance.
860 * Removes one matching handler registered with the given
861 * @a method_name and callbacks.
864 * The try-and-slice instance.
866 * Name of the method to remove.
874 * End of message handler.
876 * @return #GNUNET_OK if a method handler was removed,
877 * #GNUNET_NO if no handler matched the given method name and callbacks.
880 GNUNET_SOCIAL_slicer_method_remove (struct GNUNET_SOCIAL_Slicer *slicer,
881 const char *method_name,
882 GNUNET_SOCIAL_MethodCallback method_cb,
883 GNUNET_SOCIAL_ModifierCallback modifier_cb,
884 GNUNET_SOCIAL_DataCallback data_cb,
885 GNUNET_SOCIAL_EndOfMessageCallback eom_cb)
887 struct GNUNET_HashCode key;
888 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
890 struct SlicerMethodRemoveClosure rm_cls;
891 rm_cls.slicer = slicer;
892 struct SlicerMethodCallbacks *rm_cbs = &rm_cls.rm_cbs;
893 rm_cbs->method_cb = method_cb;
894 rm_cbs->modifier_cb = modifier_cb;
895 rm_cbs->data_cb = data_cb;
896 rm_cbs->eom_cb = eom_cb;
900 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->method_handlers, &key,
901 slicer_method_remove,
909 * Watch a place for changed objects.
912 * The try-and-slice instance.
913 * @param object_filter
914 * Object prefix to match.
916 * Function to call when encountering a state modifier.
918 * Closure for callback.
921 GNUNET_SOCIAL_slicer_modifier_add (struct GNUNET_SOCIAL_Slicer *slicer,
922 const char *object_filter,
923 GNUNET_SOCIAL_ModifierCallback modifier_cb,
926 struct SlicerModifierCallbacks *cbs = GNUNET_malloc (sizeof *cbs);
927 cbs->modifier_cb = modifier_cb;
930 struct GNUNET_HashCode key;
931 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
932 GNUNET_CONTAINER_multihashmap_put (slicer->modifier_handlers, &key, cbs,
933 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
938 slicer_modifier_remove (void *cls, const struct GNUNET_HashCode *key, void *value)
940 struct SlicerModifierRemoveClosure *rm_cls = cls;
941 struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
942 struct SlicerModifierCallbacks *rm_cbs = &rm_cls->rm_cbs;
943 struct SlicerModifierCallbacks *cbs = value;
945 if (cbs->modifier_cb == rm_cbs->modifier_cb)
947 GNUNET_CONTAINER_multihashmap_remove (slicer->modifier_handlers, key, cbs);
956 * Remove a registered modifier from the try-and-slice instance.
958 * Removes one matching handler registered with the given
959 * @a object_filter and @a modifier_cb.
962 * The try-and-slice instance.
963 * @param object_filter
964 * Object prefix to match.
966 * Function to call when encountering a state modifier changes.
969 GNUNET_SOCIAL_slicer_modifier_remove (struct GNUNET_SOCIAL_Slicer *slicer,
970 const char *object_filter,
971 GNUNET_SOCIAL_ModifierCallback modifier_cb)
973 struct GNUNET_HashCode key;
974 GNUNET_CRYPTO_hash (object_filter, strlen (object_filter), &key);
976 struct SlicerModifierRemoveClosure rm_cls;
977 rm_cls.slicer = slicer;
978 struct SlicerModifierCallbacks *rm_cbs = &rm_cls.rm_cbs;
979 rm_cbs->modifier_cb = modifier_cb;
983 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->modifier_handlers, &key,
984 slicer_modifier_remove,
992 slicer_method_free (void *cls, const struct GNUNET_HashCode *key, void *value)
994 struct SlicerMethodCallbacks *cbs = value;
1001 * Destroy a given try-and-slice instance.
1007 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer)
1009 GNUNET_CONTAINER_multihashmap_iterate (slicer->method_handlers,
1010 slicer_method_free, NULL);
1011 GNUNET_CONTAINER_multihashmap_destroy (slicer->method_handlers);
1012 GNUNET_free (slicer);
1020 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
1022 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
1023 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
1024 memcpy (cmsg, plc->connect_msg, cmsg_size);
1025 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
1030 place_recv_disconnect (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 GNUNET_CLIENT_MANAGER_reconnect (client);
1038 place_send_connect_msg (plc);
1043 place_recv_result (void *cls,
1044 struct GNUNET_CLIENT_MANAGER_Connection *client,
1045 const struct GNUNET_MessageHeader *msg)
1047 struct GNUNET_SOCIAL_Place *
1048 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1050 const struct GNUNET_OperationResultMessage *
1051 res = (const struct GNUNET_OperationResultMessage *) msg;
1053 uint16_t size = ntohs (msg->size);
1054 if (size < sizeof (*res))
1055 { /* Error, message too small. */
1060 uint16_t data_size = size - sizeof (*res);
1061 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
1062 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
1063 GNUNET_ntohll (res->result_code),
1069 op_recv_history_result (void *cls, int64_t result,
1070 const void *err_msg, uint16_t err_msg_size)
1072 LOG (GNUNET_ERROR_TYPE_DEBUG,
1073 "Received history replay result: %" PRId64 ".\n", result);
1075 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
1077 if (NULL != hist->result_cb)
1078 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
1080 GNUNET_PSYC_receive_destroy (hist->recv);
1086 op_recv_state_result (void *cls, int64_t result,
1087 const void *err_msg, uint16_t err_msg_size)
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "Received state request result: %" PRId64 ".\n", result);
1092 struct GNUNET_SOCIAL_LookHandle *look = cls;
1094 if (NULL != look->result_cb)
1095 look->result_cb (look->cls, result, err_msg, err_msg_size);
1102 place_recv_history_result (void *cls,
1103 struct GNUNET_CLIENT_MANAGER_Connection *client,
1104 const struct GNUNET_MessageHeader *msg)
1106 struct GNUNET_SOCIAL_Place *
1107 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1109 const struct GNUNET_OperationResultMessage *
1110 res = (const struct GNUNET_OperationResultMessage *) msg;
1111 struct GNUNET_PSYC_MessageHeader *
1112 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
1114 LOG (GNUNET_ERROR_TYPE_DEBUG,
1115 "%p Received historic fragment for message #%" PRIu64 ".\n",
1116 plc, GNUNET_ntohll (pmsg->message_id));
1118 GNUNET_ResultCallback result_cb = NULL;
1119 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
1121 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
1122 GNUNET_ntohll (res->op_id),
1123 &result_cb, (void *) &hist))
1124 { /* Operation not found. */
1125 LOG (GNUNET_ERROR_TYPE_WARNING,
1126 "%p Replay operation not found for historic fragment of message #%"
1128 plc, GNUNET_ntohll (pmsg->message_id));
1132 uint16_t size = ntohs (msg->size);
1133 if (size < sizeof (*res) + sizeof (*pmsg))
1134 { /* Error, message too small. */
1139 GNUNET_PSYC_receive_message (hist->recv,
1140 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
1145 place_recv_state_result (void *cls,
1146 struct GNUNET_CLIENT_MANAGER_Connection *client,
1147 const struct GNUNET_MessageHeader *msg)
1149 struct GNUNET_SOCIAL_Place *
1150 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1152 const struct GNUNET_OperationResultMessage *
1153 res = (const struct GNUNET_OperationResultMessage *) msg;
1155 GNUNET_ResultCallback result_cb = NULL;
1156 struct GNUNET_SOCIAL_LookHandle *look = NULL;
1158 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
1159 GNUNET_ntohll (res->op_id),
1160 &result_cb, (void *) &look))
1161 { /* Operation not found. */
1165 const struct GNUNET_MessageHeader *
1166 mod = (struct GNUNET_MessageHeader *) &res[1];
1167 uint16_t mod_size = ntohs (mod->size);
1168 if (ntohs (msg->size) - sizeof (*res) != mod_size)
1170 GNUNET_break_op (0);
1171 LOG (GNUNET_ERROR_TYPE_WARNING,
1172 "Invalid modifier size in state result: %u - %u != %u\n",
1173 ntohs (msg->size), sizeof (*res), mod_size);
1176 switch (ntohs (mod->type))
1178 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
1180 const struct GNUNET_PSYC_MessageModifier *
1181 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
1183 const char *name = (const char *) &pmod[1];
1184 uint16_t name_size = ntohs (pmod->name_size);
1185 if ('\0' != name[name_size - 1])
1187 GNUNET_break_op (0);
1188 LOG (GNUNET_ERROR_TYPE_WARNING,
1189 "Invalid modifier name in state result\n");
1192 look->mod_value_size = ntohs (pmod->value_size);
1193 look->var_cb (look->cls, mod, name, name + name_size,
1194 mod_size - sizeof (*mod) - name_size,
1195 look->mod_value_size);
1196 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
1198 look->mod_value_remaining = look->mod_value_size;
1199 look->mod_name = GNUNET_malloc (name_size);
1200 memcpy (look->mod_name, name, name_size);
1205 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
1206 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
1207 mod_size - sizeof (*mod), look->mod_value_size);
1208 look->mod_value_remaining -= mod_size - sizeof (*mod);
1209 if (0 == look->mod_value_remaining)
1211 GNUNET_free (look->mod_name);
1219 place_recv_message_ack (void *cls,
1220 struct GNUNET_CLIENT_MANAGER_Connection *client,
1221 const struct GNUNET_MessageHeader *msg)
1223 struct GNUNET_SOCIAL_Place *
1224 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1225 GNUNET_PSYC_transmit_got_ack (plc->tmit);
1230 place_recv_message (void *cls,
1231 struct GNUNET_CLIENT_MANAGER_Connection *client,
1232 const struct GNUNET_MessageHeader *msg)
1234 struct GNUNET_SOCIAL_Place *
1235 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
1236 GNUNET_PSYC_receive_message (plc->recv,
1237 (const struct GNUNET_PSYC_MessageHeader *) msg);
1242 host_recv_message (void *cls,
1243 struct GNUNET_CLIENT_MANAGER_Connection *client,
1244 const struct GNUNET_MessageHeader *msg)
1246 struct GNUNET_SOCIAL_Host *
1247 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
1248 GNUNET_PSYC_receive_message (hst->recv,
1249 (const struct GNUNET_PSYC_MessageHeader *) msg);
1250 GNUNET_PSYC_receive_message (hst->plc.recv,
1251 (const struct GNUNET_PSYC_MessageHeader *) msg);
1256 host_recv_enter_ack (void *cls,
1257 struct GNUNET_CLIENT_MANAGER_Connection *client,
1258 const struct GNUNET_MessageHeader *msg)
1260 struct GNUNET_SOCIAL_Host *
1261 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1262 sizeof (struct GNUNET_SOCIAL_Place));
1264 struct GNUNET_PSYC_CountersResultMessage *
1265 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
1266 int32_t result = ntohl (cres->result_code);
1267 if (NULL != hst->enter_cb)
1268 hst->enter_cb (hst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
1273 host_recv_enter_request (void *cls,
1274 struct GNUNET_CLIENT_MANAGER_Connection *client,
1275 const struct GNUNET_MessageHeader *msg)
1277 struct GNUNET_SOCIAL_Host *
1278 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1279 sizeof (struct GNUNET_SOCIAL_Place));
1280 if (NULL == hst->answer_door_cb)
1283 const char *method_name = NULL;
1284 struct GNUNET_ENV_Environment *env = NULL;
1285 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
1286 const void *data = NULL;
1287 uint16_t data_size = 0;
1289 const struct GNUNET_PSYC_JoinRequestMessage *
1290 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
1291 const struct GNUNET_PSYC_Message *join_msg = NULL;
1295 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
1297 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
1298 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 "Received join_msg of type %u and size %u.\n",
1300 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
1302 env = GNUNET_ENV_environment_create ();
1303 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
1304 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
1307 GNUNET_break_op (0);
1308 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_key);
1309 LOG (GNUNET_ERROR_TYPE_WARNING,
1310 "Ignoring invalid entry request from nym %s.\n",
1317 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_key);
1318 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
1323 GNUNET_ENV_environment_destroy (env);
1324 if (NULL != entry_pmsg)
1325 GNUNET_free (entry_pmsg);
1330 guest_recv_enter_ack (void *cls,
1331 struct GNUNET_CLIENT_MANAGER_Connection *client,
1332 const struct GNUNET_MessageHeader *msg)
1334 struct GNUNET_SOCIAL_Guest *
1335 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1336 sizeof (struct GNUNET_SOCIAL_Place));
1338 struct GNUNET_PSYC_CountersResultMessage *
1339 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
1340 int32_t result = ntohl (cres->result_code);
1341 if (NULL != gst->enter_cb)
1342 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
1347 guest_recv_join_decision (void *cls,
1348 struct GNUNET_CLIENT_MANAGER_Connection *client,
1349 const struct GNUNET_MessageHeader *msg)
1351 struct GNUNET_SOCIAL_Guest *
1352 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
1353 sizeof (struct GNUNET_SOCIAL_Place));
1354 const struct GNUNET_PSYC_JoinDecisionMessage *
1355 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
1357 struct GNUNET_PSYC_Message *pmsg = NULL;
1358 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
1359 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
1361 if (NULL != gst->entry_dcsn_cb)
1362 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
1367 notify_recv_place_host (void *cls,
1368 struct GNUNET_CLIENT_MANAGER_Connection *client,
1369 const struct GNUNET_MessageHeader *msg)
1371 struct GNUNET_SOCIAL_PlaceListenHandle *
1372 pl = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*pl));
1373 if (NULL == pl->notify_host)
1376 struct HostEnterRequest *
1377 hreq = (struct HostEnterRequest *) msg;
1379 pl->notify_host (pl->notify_cls, &hreq->place_key, ntohl (hreq->policy));
1384 notify_recv_place_guest (void *cls,
1385 struct GNUNET_CLIENT_MANAGER_Connection *client,
1386 const struct GNUNET_MessageHeader *msg)
1388 struct GNUNET_SOCIAL_PlaceListenHandle *
1389 pl = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*pl));
1390 if (NULL == pl->notify_guest)
1393 struct GuestEnterRequest *
1394 greq = (struct GuestEnterRequest *) msg;
1395 uint16_t greq_size = ntohs (greq->header.size);
1397 const struct GNUNET_PeerIdentity *relays = NULL;
1398 uint16_t relay_count = ntohs (greq->relay_count);
1399 uint16_t relay_size = relay_count * sizeof (*relays);
1401 relays = (const struct GNUNET_PeerIdentity *) &greq[1];
1402 struct GNUNET_PSYC_Message *join_msg = NULL;
1404 if (sizeof (*greq) + relay_size + sizeof (struct GNUNET_MessageHeader)
1407 join_msg = (struct GNUNET_PSYC_Message *)
1408 (((char *) &greq[1]) + relay_size);
1411 pl->notify_guest (pl->notify_cls, &greq->place_key, &greq->origin,
1412 relay_count, relays, join_msg);
1416 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1418 { host_recv_enter_ack, NULL,
1419 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1420 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1422 { host_recv_enter_request, NULL,
1423 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1424 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1426 { host_recv_message, NULL,
1427 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1428 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1430 { place_recv_message_ack, NULL,
1431 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1432 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1434 { place_recv_history_result, NULL,
1435 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1436 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1438 { place_recv_state_result, NULL,
1439 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1440 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1442 { place_recv_result, NULL,
1443 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1444 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1446 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1448 { NULL, NULL, 0, 0, GNUNET_NO }
1452 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1454 { guest_recv_enter_ack, NULL,
1455 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1456 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1458 { host_recv_enter_request, NULL,
1459 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1460 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1462 { place_recv_message, NULL,
1463 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1464 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1466 { place_recv_message_ack, NULL,
1467 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1468 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1470 { guest_recv_join_decision, NULL,
1471 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1472 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1474 { place_recv_history_result, NULL,
1475 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1476 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1478 { place_recv_state_result, NULL,
1479 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1480 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1482 { place_recv_result, NULL,
1483 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1484 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1486 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1488 { NULL, NULL, 0, 0, GNUNET_NO }
1493 static struct GNUNET_CLIENT_MANAGER_MessageHandler notify_handlers[] =
1495 { notify_recv_place_host, NULL,
1496 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER,
1497 sizeof (struct HostEnterRequest), GNUNET_NO },
1499 { notify_recv_place_guest, NULL,
1500 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER,
1501 sizeof (struct GuestEnterRequest), GNUNET_YES },
1503 { NULL, NULL, 0, 0, GNUNET_NO }
1508 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1510 if (NULL != plc->tmit)
1511 GNUNET_PSYC_transmit_destroy (plc->tmit);
1512 if (NULL != plc->recv)
1513 GNUNET_PSYC_receive_destroy (plc->recv);
1514 if (NULL != plc->connect_msg)
1515 GNUNET_free (plc->connect_msg);
1516 if (NULL != plc->disconnect_cb)
1517 plc->disconnect_cb (plc->disconnect_cls);
1521 GNUNET_CORE_disconnect (core);
1524 if (NULL != namestore)
1526 GNUNET_NAMESTORE_disconnect (namestore);
1531 GNUNET_GNS_disconnect (gns);
1538 host_cleanup (void *cls)
1540 struct GNUNET_SOCIAL_Host *hst = cls;
1541 place_cleanup (&hst->plc);
1542 GNUNET_PSYC_receive_destroy (hst->recv);
1543 GNUNET_SOCIAL_slicer_destroy (hst->slicer);
1549 guest_cleanup (void *cls)
1551 struct GNUNET_SOCIAL_Guest *gst = cls;
1552 place_cleanup (&gst->plc);
1560 * Enter a place as host.
1562 * A place is created upon first entering, and it is active until permanently
1563 * left using GNUNET_SOCIAL_host_leave().
1566 * Configuration to contact the social service.
1568 * Identity of the host.
1570 * Private-public key pair of the place.
1571 * NULL for ephemeral places.
1573 * Policy specifying entry and history restrictions for the place.
1575 * Slicer to handle incoming messages.
1576 * @param answer_door_cb
1577 * Function to handle new nyms that want to enter.
1578 * @param farewell_cb
1579 * Function to handle departing nyms.
1581 * Closure for the callbacks.
1583 * @return Handle for the host.
1585 struct GNUNET_SOCIAL_Host *
1586 GNUNET_SOCIAL_host_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1587 const struct GNUNET_IDENTITY_Ego *ego,
1588 const struct GNUNET_CRYPTO_EddsaPrivateKey *place_key,
1589 enum GNUNET_PSYC_Policy policy,
1590 struct GNUNET_SOCIAL_Slicer *slicer,
1591 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1592 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1593 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1596 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1597 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1598 struct HostEnterRequest *req = GNUNET_malloc (sizeof (*req));
1600 if (NULL != place_key)
1602 hst->place_key = *place_key;
1606 struct GNUNET_CRYPTO_EddsaPrivateKey *
1607 ephemeral_key = GNUNET_CRYPTO_eddsa_key_create ();
1608 hst->place_key = *ephemeral_key;
1609 GNUNET_CRYPTO_eddsa_key_get_public (&hst->place_key, &plc->pub_key);
1610 GNUNET_CRYPTO_eddsa_key_clear (ephemeral_key);
1611 GNUNET_free (ephemeral_key);
1615 plc->is_host = GNUNET_YES;
1616 plc->slicer = slicer;
1618 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1619 GNUNET_CRYPTO_eddsa_key_get_public (place_key, &plc->pub_key);
1621 hst->enter_cb = enter_cb;
1622 hst->answer_door_cb = answer_door_cb;
1623 hst->farewell_cb = farewell_cb;
1626 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", host_handlers);
1627 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1629 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1630 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
1632 hst->slicer = GNUNET_SOCIAL_slicer_create ();
1633 GNUNET_SOCIAL_slicer_method_add (hst->slicer, "_notice_place_leave",
1634 host_recv_notice_place_leave_method,
1635 host_recv_notice_place_leave_modifier,
1636 NULL, host_recv_notice_place_leave_eom, hst);
1637 hst->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, hst->slicer);
1639 req->header.size = htons (sizeof (*req));
1640 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1641 req->policy = policy;
1642 req->place_key = hst->place_key;
1643 req->host_key = plc->ego_key;
1645 plc->connect_msg = (struct GNUNET_MessageHeader *) req;
1646 place_send_connect_msg (plc);
1653 * Enter a place as host.
1655 * A place is created upon first entering, and it is active until permanently
1656 * left using GNUNET_SOCIAL_host_leave().
1659 * Configuration to contact the social service.
1661 * Identity of the host.
1663 * GNS name in the zone of the @a ego that contains the
1664 * public key of the place in a PLACE record.
1666 * Policy specifying entry and history restrictions for the place.
1668 * Slicer to handle incoming messages.
1669 * @param answer_door_cb
1670 * Function to handle new nyms that want to enter.
1671 * @param farewell_cb
1672 * Function to handle departing nyms.
1674 * Closure for the callbacks.
1676 * @return Handle for the host.
1678 struct GNUNET_SOCIAL_Host *
1679 GNUNET_SOCIAL_host_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
1680 struct GNUNET_IDENTITY_Ego *ego,
1681 const char *gns_name,
1682 enum GNUNET_PSYC_Policy policy,
1683 struct GNUNET_SOCIAL_Slicer *slicer,
1684 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1685 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1686 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1689 struct GNUNET_CRYPTO_EddsaPrivateKey place_key = {};
1692 * 1. get public key by looking up PLACE entry under gns_name
1693 * in the zone of the ego.
1694 * 2. get private key from $GNUNET_DATA_HOME/social/places/PUB_KEY_HASH
1697 return GNUNET_SOCIAL_host_enter (cfg, ego, &place_key, policy, slicer,
1698 enter_cb, answer_door_cb, farewell_cb, cls);
1703 * Decision whether to admit @a nym into the place or refuse entry.
1706 * Host of the place.
1708 * Handle for the entity that wanted to enter.
1709 * @param is_admitted
1710 * #GNUNET_YES if @a nym is admitted,
1711 * #GNUNET_NO if @a nym is refused entry,
1712 * #GNUNET_SYSERR if we cannot answer the request.
1713 * @param method_name
1714 * Method name for the rejection message.
1716 * Environment containing variables for the message, or NULL.
1718 * Data for the rejection message to send back.
1720 * Number of bytes in @a data for method.
1721 * @return #GNUNET_OK on success,
1722 * #GNUNET_SYSERR if the message is too large.
1725 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1726 struct GNUNET_SOCIAL_Nym *nym,
1728 const struct GNUNET_PSYC_Message *entry_resp)
1730 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1731 uint16_t entry_resp_size
1732 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1734 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1735 return GNUNET_SYSERR;
1737 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1738 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1739 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1740 dcsn->is_admitted = htonl (is_admitted);
1741 dcsn->slave_key = nym->pub_key;
1743 if (0 < entry_resp_size)
1744 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1746 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1752 * Throw @a nym out of the place.
1754 * The @a nym reference will remain valid until the
1755 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1756 * which should be very soon after this call.
1759 * Host of the place.
1761 * Handle for the entity to be ejected.
1764 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1765 const struct GNUNET_SOCIAL_Nym *nym)
1767 struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
1768 GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_SET,
1769 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1770 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1771 GNUNET_SOCIAL_ANNOUNCE_NONE);
1776 * Get the public key of a @a nym.
1778 * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
1781 * Pseudonym to map to a cryptographic identifier.
1783 * @return Public key of nym.
1785 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1786 GNUNET_SOCIAL_nym_get_key (const struct GNUNET_SOCIAL_Nym *nym)
1788 return &nym->pub_key;
1793 * Get the hash of the public key of a @a nym.
1796 * Pseudonym to map to a cryptographic identifier.
1798 * @return Hash of the public key of nym.
1800 const struct GNUNET_HashCode *
1801 GNUNET_SOCIAL_nym_get_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1803 return &nym->pub_key_hash;
1808 * Obtain the private-public key pair of the hosted place.
1810 * The public part is suitable for storing in GNS within a PLACE record,
1811 * along with peer IDs to join at.
1814 * Host of the place.
1816 * @return Private-public key pair of the hosted place.
1818 const struct GNUNET_CRYPTO_EddsaPrivateKey *
1819 GNUNET_SOCIAL_host_get_place_key (struct GNUNET_SOCIAL_Host *hst)
1821 return &hst->place_key;
1826 * Connected to core service.
1829 core_connected_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1831 this_peer = *my_identity;
1837 * Advertise the place in the GNS zone of the @e ego of the @a host.
1840 * Host of the place.
1842 * The name for the PLACE record to put in the zone.
1844 * Number of elements in the @a peers array.
1846 * List of peers to put in the PLACE record to advertise
1847 * as entry points to the place in addition to the origin.
1848 * @param expiration_time
1849 * Expiration time of the record, use 0 to remove the record.
1851 * Password used to encrypt the record or NULL to keep it cleartext.
1853 * Function called with the result of the operation.
1855 * Closure for @a result_cb
1858 GNUNET_SOCIAL_host_advertise (struct GNUNET_SOCIAL_Host *hst,
1860 uint32_t peer_count,
1861 const struct GNUNET_PeerIdentity *peers,
1862 struct GNUNET_TIME_Absolute expiration_time,
1863 const char *password,
1864 GNUNET_NAMESTORE_ContinuationWithStatus result_cb,
1867 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1868 if (NULL == namestore)
1869 namestore = GNUNET_NAMESTORE_connect (plc->cfg);
1871 core = GNUNET_CORE_connect (plc->cfg, NULL, core_connected_cb, NULL, NULL,
1872 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
1874 struct GNUNET_GNSRECORD_Data rd = { };
1875 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
1876 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1877 rd.expiration_time = expiration_time.abs_value_us;
1879 struct GNUNET_GNSRECORD_PlaceData *
1880 rec = GNUNET_malloc (sizeof (*rec) + peer_count * sizeof (*peers));
1881 rec->place_key = plc->pub_key;
1882 rec->origin = this_peer;
1883 rec->relay_count = htonl (peer_count);
1884 memcpy (&rec[1], peers, peer_count * sizeof (*peers));
1887 rd.data_size = sizeof (*rec) + peer_count * sizeof (*peers);
1889 GNUNET_NAMESTORE_records_store (namestore, &hst->plc.ego_key,
1890 name, 1, &rd, result_cb, result_cls);
1895 * Send a message to all nyms that are present in the place.
1897 * This function is restricted to the host. Nyms can only send requests
1898 * to the host who can decide to relay it to everyone in the place.
1900 * @param host Host of the place.
1901 * @param method_name Method to use for the announcement.
1902 * @param env Environment containing variables for the message and operations
1903 * on objects of the place. Can be NULL.
1904 * @param notify Function to call to get the payload of the announcement.
1905 * @param notify_cls Closure for @a notify.
1906 * @param flags Flags for this announcement.
1908 * @return NULL on error (announcement already in progress?).
1910 struct GNUNET_SOCIAL_Announcement *
1911 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1912 const char *method_name,
1913 const struct GNUNET_ENV_Environment *env,
1914 GNUNET_PSYC_TransmitNotifyData notify_data,
1915 void *notify_data_cls,
1916 enum GNUNET_SOCIAL_AnnounceFlags flags)
1919 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1920 NULL, notify_data, notify_data_cls, flags))
1921 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1928 * Resume transmitting announcement.
1931 * The announcement to resume.
1934 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1936 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1941 * Cancel announcement.
1944 * The announcement to cancel.
1947 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1949 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1954 * Obtain handle for a hosted place.
1956 * The returned handle can be used to access the place API.
1958 * @param host Handle for the host.
1960 * @return Handle for the hosted place, valid as long as @a host is valid.
1962 struct GNUNET_SOCIAL_Place *
1963 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1970 * Stop hosting a place.
1972 * Invalidates host handle.
1974 * @param host Host leaving the place.
1975 * @param keep_active Keep the place active after last host disconnected.
1978 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1980 GNUNET_ContinuationCallback leave_cb,
1983 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1985 /* FIXME: send msg to service */
1987 plc->is_disconnecting = GNUNET_YES;
1988 plc->disconnect_cb = leave_cb;
1989 plc->disconnect_cls = leave_cls;
1991 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1992 &host_cleanup, hst);
1998 static struct GuestEnterRequest *
1999 guest_enter_request_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key,
2000 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
2001 const struct GNUNET_PeerIdentity *origin,
2003 const struct GNUNET_PeerIdentity *relays,
2004 const struct GNUNET_PSYC_Message *join_msg)
2006 uint16_t join_msg_size = ntohs (join_msg->header.size);
2007 uint16_t relay_size = relay_count * sizeof (*relays);
2009 struct GuestEnterRequest *
2010 req = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size);
2012 req->header.size = htons (sizeof (*req) + relay_size + join_msg_size);
2013 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
2014 req->place_key = *place_key;
2015 req->guest_key = *guest_key;
2016 req->origin = *origin;
2017 req->relay_count = htonl (relay_count);
2019 uint16_t p = sizeof (*req);
2022 memcpy ((char *) req + p, relays, relay_size);
2026 memcpy ((char *) req + p, join_msg, join_msg_size);
2031 * Request entry to a place as a guest.
2033 * @param cfg Configuration to contact the social service.
2034 * @param ego Identity of the guest.
2035 * @param crypto_address Public key of the place to enter.
2036 * @param origin Peer identity of the origin of the underlying multicast group.
2037 * @param relay_count Number of elements in the @a relays array.
2038 * @param relays Relays for the underlying multicast group.
2039 * @param method_name Method name for the message.
2040 * @param env Environment containing variables for the message, or NULL.
2041 * @param data Payload for the message to give to the enter callback.
2042 * @param data_size Number of bytes in @a data.
2043 * @param slicer Slicer to use for processing incoming requests from guests.
2045 * @return NULL on errors, otherwise handle for the guest.
2047 struct GNUNET_SOCIAL_Guest *
2048 GNUNET_SOCIAL_guest_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
2049 const struct GNUNET_IDENTITY_Ego *ego,
2050 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
2051 const struct GNUNET_PeerIdentity *origin,
2052 uint32_t relay_count,
2053 const struct GNUNET_PeerIdentity *relays,
2054 const struct GNUNET_PSYC_Message *join_msg,
2055 struct GNUNET_SOCIAL_Slicer *slicer,
2056 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
2057 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
2060 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
2061 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2063 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
2064 plc->pub_key = *place_key;
2066 plc->is_host = GNUNET_YES;
2067 plc->slicer = slicer;
2069 gst->enter_cb = local_enter_cb;
2070 gst->entry_dcsn_cb = entry_dcsn_cb;
2073 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
2074 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
2076 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
2077 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2079 struct GuestEnterRequest *
2080 req = guest_enter_request_create (&plc->ego_key, place_key, origin,
2081 relay_count, relays, join_msg);
2082 plc->connect_msg = &req->header;
2083 place_send_connect_msg (plc);
2089 * Result of a GNS name lookup for entering a place.
2091 * @see GNUNET_SOCIAL_guest_enter_by_name
2094 gns_result_guest_enter (void *cls, uint32_t rd_count,
2095 const struct GNUNET_GNSRECORD_Data *rd)
2097 struct GNUNET_SOCIAL_Guest *gst = cls;
2098 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2101 "%p GNS result: %u records.\n", gst, rd_count);
2103 const struct GNUNET_GNSRECORD_PlaceData *
2104 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
2108 if (NULL != gst->enter_cb)
2109 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
2113 if (rd->data_size < sizeof (*rec))
2115 GNUNET_break_op (0);
2116 if (NULL != gst->enter_cb)
2117 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
2121 uint16_t relay_count = ntohl (rec->relay_count);
2122 struct GNUNET_PeerIdentity *relays = NULL;
2124 if (0 < relay_count)
2126 if (rd->data_size == sizeof (*rec) + relay_count * sizeof (struct GNUNET_PeerIdentity))
2128 relays = (struct GNUNET_PeerIdentity *) &rec[1];
2133 GNUNET_break_op (0);
2137 struct GuestEnterRequest *
2138 req = guest_enter_request_create (&plc->ego_key, &rec->place_key,
2139 &rec->origin, relay_count, relays,
2140 (struct GNUNET_PSYC_Message *) plc->connect_msg);
2141 GNUNET_free (plc->connect_msg);
2142 plc->connect_msg = &req->header;
2143 plc->pub_key = req->place_key;
2145 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
2146 plc->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, plc->slicer);
2148 place_send_connect_msg (plc);
2153 * Request entry to a place by name as a guest.
2156 * Configuration to contact the social service.
2158 * Identity of the guest.
2160 * GNS name of the place to enter. Either in the form of
2161 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
2162 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
2163 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
2164 * pseudonym's place directly.
2166 * Password to decrypt the record, or NULL for cleartext records.
2168 * Entry request message.
2170 * Slicer to use for processing incoming requests from guests.
2171 * @param local_enter_cb
2172 * Called upon connection established to the social service.
2173 * @param entry_decision_cb
2174 * Called upon receiving entry decision.
2176 * @return NULL on errors, otherwise handle for the guest.
2178 struct GNUNET_SOCIAL_Guest *
2179 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
2180 const struct GNUNET_IDENTITY_Ego *ego,
2181 const char *gns_name, const char *password,
2182 const struct GNUNET_PSYC_Message *join_msg,
2183 struct GNUNET_SOCIAL_Slicer *slicer,
2184 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
2185 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
2188 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
2189 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2191 GNUNET_assert (NULL != join_msg);
2193 gst->enter_cb = local_enter_cb;
2194 gst->entry_dcsn_cb = entry_decision_cb;
2197 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
2199 plc->is_host = GNUNET_NO;
2200 plc->slicer = slicer;
2202 uint16_t join_msg_size = ntohs (join_msg->header.size);
2203 plc->connect_msg = GNUNET_malloc (join_msg_size);
2204 memcpy (plc->connect_msg, join_msg, join_msg_size);
2207 gns = GNUNET_GNS_connect (cfg);
2209 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
2210 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
2212 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
2213 GNUNET_IDENTITY_ego_get_public_key (ego, &ego_pub_key);
2214 GNUNET_GNS_lookup (gns, gns_name, &ego_pub_key,
2215 GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
2216 NULL, gns_result_guest_enter, gst);
2222 * Talk to the host of the place.
2225 * Place where we want to talk to the host.
2226 * @param method_name
2227 * Method to invoke on the host.
2229 * Environment containing variables for the message, or NULL.
2230 * @param notify_data
2231 * Function to use to get the payload for the method.
2232 * @param notify_data_cls
2233 * Closure for @a notify_data.
2235 * Flags for the message being sent.
2237 * @return NULL if we are already trying to talk to the host,
2238 * otherwise handle to cancel the request.
2240 struct GNUNET_SOCIAL_TalkRequest *
2241 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2242 const char *method_name,
2243 const struct GNUNET_ENV_Environment *env,
2244 GNUNET_PSYC_TransmitNotifyData notify_data,
2245 void *notify_data_cls,
2246 enum GNUNET_SOCIAL_TalkFlags flags)
2248 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2249 GNUNET_assert (NULL != plc->tmit);
2252 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2253 NULL, notify_data, notify_data_cls, flags))
2254 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2261 * Resume talking to the host of the place.
2264 * Talk request to resume.
2267 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2269 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2274 * Cancel talking to the host of the place.
2277 * Talk request to cancel.
2280 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2282 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2287 * Leave a place temporarily or permanently.
2289 * Notifies the owner of the place about leaving, and destroys the place handle.
2293 * @param keep_active
2294 * Keep place active after last application disconnected.
2295 * #GNUNET_YES or #GNUNET_NO
2297 * Optional environment for the leave message if @a keep_active
2298 * is #GNUNET_NO. NULL if not needed.
2300 * Called upon disconnecting from the social service.
2303 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2305 struct GNUNET_ENV_Environment *env,
2306 GNUNET_ContinuationCallback leave_cb,
2309 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2311 plc->is_disconnecting = GNUNET_YES;
2312 plc->disconnect_cb = leave_cb;
2313 plc->disconnect_cls = leave_cls;
2315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2316 "Guest: leaving place.\n");
2318 if (GNUNET_NO == keep_active && NULL != plc->tmit)
2320 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2321 GNUNET_SOCIAL_TALK_NONE);
2324 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
2325 &guest_cleanup, gst);
2330 * Obtain handle for a place entered as guest.
2332 * The returned handle can be used to access the place API.
2334 * @param guest Handle for the guest.
2336 * @return Handle for the place, valid as long as @a guest is valid.
2338 struct GNUNET_SOCIAL_Place *
2339 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2345 static struct GNUNET_SOCIAL_HistoryRequest *
2346 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2347 uint64_t start_message_id,
2348 uint64_t end_message_id,
2349 uint64_t message_limit,
2350 const char *method_prefix,
2352 struct GNUNET_SOCIAL_Slicer *slicer,
2353 GNUNET_ResultCallback result_cb,
2356 struct GNUNET_PSYC_HistoryRequestMessage *req;
2357 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2359 hist->recv = GNUNET_PSYC_receive_create (NULL, slicer_message, slicer);
2360 hist->result_cb = result_cb;
2362 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2363 &op_recv_history_result, hist);
2365 GNUNET_assert (NULL != method_prefix);
2366 uint16_t method_size = strnlen (method_prefix,
2367 GNUNET_SERVER_MAX_MESSAGE_SIZE
2368 - sizeof (*req)) + 1;
2369 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2370 req = GNUNET_malloc (sizeof (*req) + method_size);
2371 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2372 req->header.size = htons (sizeof (*req) + method_size);
2373 req->start_message_id = GNUNET_htonll (start_message_id);
2374 req->end_message_id = GNUNET_htonll (end_message_id);
2375 req->message_limit = GNUNET_htonll (message_limit);
2376 req->flags = htonl (flags);
2377 req->op_id = GNUNET_htonll (hist->op_id);
2378 memcpy (&req[1], method_prefix, method_size);
2380 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2386 * Learn about the history of a place.
2388 * Messages are returned through the @a slicer function
2389 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2392 * Place we want to learn more about.
2393 * @param start_message_id
2394 * First historic message we are interested in.
2395 * @param end_message_id
2396 * Last historic message we are interested in (inclusive).
2397 * @param method_prefix
2398 * Only retrieve messages with this method prefix.
2400 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2402 * Slicer to use for retrieved messages.
2403 * Can be the same as the slicer of the place.
2405 * Function called after all messages retrieved.
2406 * NULL if not needed.
2407 * @param cls Closure for @a result_cb.
2409 struct GNUNET_SOCIAL_HistoryRequest *
2410 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2411 uint64_t start_message_id,
2412 uint64_t end_message_id,
2413 const char *method_prefix,
2415 struct GNUNET_SOCIAL_Slicer *slicer,
2416 GNUNET_ResultCallback result_cb,
2419 return place_history_replay (plc, start_message_id, end_message_id, 0,
2420 method_prefix, flags, slicer, result_cb, cls);
2425 * Learn about the history of a place.
2427 * Sends messages through the slicer function of the place where
2428 * start_message_id <= message_id <= end_message_id.
2429 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2431 * To get the latest message, use 0 for both the start and end message ID.
2434 * Place we want to learn more about.
2435 * @param message_limit
2436 * Maximum number of historic messages we are interested in.
2437 * @param method_prefix
2438 * Only retrieve messages with this method prefix.
2440 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2442 * Function called after all messages retrieved.
2443 * NULL if not needed.
2444 * @param cls Closure for @a result_cb.
2446 struct GNUNET_SOCIAL_HistoryRequest *
2447 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2448 uint64_t message_limit,
2449 const char *method_prefix,
2451 struct GNUNET_SOCIAL_Slicer *slicer,
2452 GNUNET_ResultCallback result_cb,
2455 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2456 slicer, result_cb, cls);
2461 * Cancel learning about the history of a place.
2464 * History lesson to cancel.
2467 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2469 GNUNET_PSYC_receive_destroy (hist->recv);
2470 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2476 * Request matching state variables.
2478 static struct GNUNET_SOCIAL_LookHandle *
2479 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2480 uint16_t type, const char *name,
2481 GNUNET_PSYC_StateVarCallback var_cb,
2482 GNUNET_ResultCallback result_cb, void *cls)
2484 struct GNUNET_PSYC_StateRequestMessage *req;
2485 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2487 look->var_cb = var_cb;
2488 look->result_cb = result_cb;
2490 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2491 &op_recv_state_result, look);
2493 GNUNET_assert (NULL != name);
2494 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2495 - sizeof (*req)) + 1;
2496 req = GNUNET_malloc (sizeof (*req) + name_size);
2497 req->header.type = htons (type);
2498 req->header.size = htons (sizeof (*req) + name_size);
2499 req->op_id = GNUNET_htonll (look->op_id);
2500 memcpy (&req[1], name, name_size);
2502 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2508 * Look at a particular object in the place.
2510 * The best matching object is returned (its name might be less specific than
2511 * what was requested).
2514 * The place where to look.
2516 * Full name of the object.
2518 * Set to the size of the returned value.
2520 * @return NULL if there is no such object at this place.
2522 struct GNUNET_SOCIAL_LookHandle *
2523 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2524 const char *full_name,
2525 GNUNET_PSYC_StateVarCallback var_cb,
2526 GNUNET_ResultCallback result_cb,
2529 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2530 full_name, var_cb, result_cb, cls);
2535 * Look for objects in the place with a matching name prefix.
2538 * The place where to look.
2539 * @param name_prefix
2540 * Look at objects with names beginning with this value.
2542 * Function to call for each object found.
2544 * Closure for callback function.
2546 * @return Handle that can be used to stop looking at objects.
2548 struct GNUNET_SOCIAL_LookHandle *
2549 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2550 const char *name_prefix,
2551 GNUNET_PSYC_StateVarCallback var_cb,
2552 GNUNET_ResultCallback result_cb,
2555 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2556 name_prefix, var_cb, result_cb, cls);
2561 * Cancel a state request operation.
2564 * Handle for the operation to cancel.
2567 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2569 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2575 * Add public key to the GNS zone of the @e ego.
2582 * The name for the PKEY record to put in the zone.
2583 * @param nym_pub_key
2584 * Public key of nym to add.
2585 * @param expiration_time
2586 * Expiration time of the record, use 0 to remove the record.
2588 * Function called with the result of the operation.
2590 * Closure for @a result_cb
2593 GNUNET_SOCIAL_zone_add_pkey (const struct GNUNET_CONFIGURATION_Handle *cfg,
2594 const struct GNUNET_IDENTITY_Ego *ego,
2596 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2597 struct GNUNET_TIME_Absolute expiration_time,
2598 GNUNET_NAMESTORE_ContinuationWithStatus result_cb,
2601 if (NULL == namestore)
2602 namestore = GNUNET_NAMESTORE_connect (cfg);
2604 struct GNUNET_GNSRECORD_Data rd = { };
2605 rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
2606 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
2607 rd.expiration_time = expiration_time.abs_value_us;
2608 rd.data = nym_pub_key;
2609 rd.data_size = sizeof (*nym_pub_key);
2611 GNUNET_NAMESTORE_records_store (namestore,
2612 GNUNET_IDENTITY_ego_get_private_key (ego),
2613 name, 1, &rd, result_cb, result_cls);
2618 * Start listening for entered places as host or guest.
2620 * The @notify_host and @notify_guest functions are
2621 * initially called with the full list of entered places,
2622 * then later each time a new place is entered.
2627 * Listen for places of this ego.
2628 * @param notify_host
2629 * Function to notify about a place entered as host.
2630 * @param notify_guest
2631 * Function to notify about a place entered as guest..
2633 * Closure for the callbacks.
2635 * @return Handle that can be used to stop listening.
2637 struct GNUNET_SOCIAL_PlaceListenHandle *
2638 GNUNET_SOCIAL_place_listen_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
2639 const struct GNUNET_IDENTITY_Ego *ego,
2640 GNUNET_SOCIAL_PlaceNotifyHostCallback notify_host,
2641 GNUNET_SOCIAL_PlaceNotifyGuestCallback notify_guest,
2644 struct GNUNET_SOCIAL_PlaceListenHandle *pl = GNUNET_malloc (sizeof *pl);
2645 pl->notify_host = notify_host;
2646 pl->notify_guest = notify_guest;
2647 pl->notify_cls = notify_cls;
2649 struct GNUNET_SOCIAL_Place *plc = &pl->plc;
2651 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
2653 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2655 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, pl, sizeof (*pl));
2657 struct PlaceListenRequest *req = GNUNET_malloc (sizeof (*req));
2658 req->ego_key = plc->ego_key;
2659 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LISTEN);
2660 req->header.size = htons (sizeof (*req));
2662 plc->connect_msg = (struct GNUNET_MessageHeader *) req;
2663 place_send_connect_msg (plc);
2670 * Stop listening for entered places.
2673 * Place listen handle.
2676 GNUNET_SOCIAL_place_listen_stop (struct GNUNET_SOCIAL_PlaceListenHandle *pl)
2678 GNUNET_CLIENT_MANAGER_disconnect (pl->plc.client, GNUNET_NO, NULL, NULL);
2681 /* end of social_api.c */