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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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 place where social interactions happen.
54 struct GNUNET_SOCIAL_Place
57 * Configuration to use.
59 const struct GNUNET_CONFIGURATION_Handle *cfg;
62 * Client connection to the service.
64 struct GNUNET_CLIENT_MANAGER_Connection *client;
67 * Transmission handle;
69 struct GNUNET_PSYC_TransmitHandle *tmit;
74 struct GNUNET_PSYC_ReceiveHandle *recv;
77 * Message to send on reconnect.
79 struct GNUNET_MessageHeader *connect_msg;
82 * Slicer for processing incoming methods.
84 struct GNUNET_SOCIAL_Slicer *slicer;
87 * Function called after disconnected from the service.
89 GNUNET_ContinuationCallback disconnect_cb;
92 * Closure for @a disconnect_cb.
97 * Public key of the place.
99 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
102 * Private key of the ego.
104 struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;
107 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
112 * Is this place in the process of disconnecting from the service?
113 * #GNUNET_YES or #GNUNET_NO
115 uint8_t is_disconnecting;
120 * Host handle for a place that we entered.
122 struct GNUNET_SOCIAL_Host
124 struct GNUNET_SOCIAL_Place plc;
126 struct GNUNET_CRYPTO_EddsaPrivateKey place_key;
128 GNUNET_SOCIAL_HostEnterCallback enter_cb;
130 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
132 GNUNET_SOCIAL_FarewellCallback farewell_cb;
135 * Closure for callbacks.
142 * Guest handle for place that we entered.
144 struct GNUNET_SOCIAL_Guest
146 struct GNUNET_SOCIAL_Place plc;
148 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
150 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
153 * Closure for callbacks.
160 * Handle for a pseudonym of another user in the network.
162 struct GNUNET_SOCIAL_Nym
164 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
165 struct GNUNET_HashCode pub_key_hash;
170 * Hash map of all nyms.
171 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
173 struct GNUNET_CONTAINER_MultiHashMap *nyms;
177 * Handle for a try-and-slice instance.
179 struct GNUNET_SOCIAL_Slicer
182 * Message handlers: method_name -> SlicerCallbacks
184 struct GNUNET_CONTAINER_MultiHashMap *handlers;
188 * Currently being processed message part.
190 const struct GNUNET_MessageHeader *msg;
193 * ID of currently being received message.
198 * Method name of currently being received message.
203 * Public key of the nym the current message originates from.
205 struct GNUNET_CRYPTO_EcdsaPublicKey nym_key;
208 * Size of @a method_name (including terminating \0).
210 uint16_t method_name_size;
215 * Callbacks for a slicer method handler.
217 struct SlicerCallbacks
219 GNUNET_SOCIAL_MethodCallback method_cb;
220 GNUNET_SOCIAL_ModifierCallback modifier_cb;
221 GNUNET_SOCIAL_DataCallback data_cb;
222 GNUNET_SOCIAL_EndOfMessageCallback eom_cb;
227 struct SlicerRemoveClosure
229 struct GNUNET_SOCIAL_Slicer *slicer;
230 struct SlicerCallbacks rm_cbs;
235 * Handle for an announcement request.
237 struct GNUNET_SOCIAL_Announcement
246 struct GNUNET_SOCIAL_TalkRequest
252 struct GNUNET_SOCIAL_WatchHandle
261 struct GNUNET_SOCIAL_HistoryRequest
266 struct GNUNET_SOCIAL_Place *plc;
276 struct GNUNET_PSYC_ReceiveHandle *recv;
279 * Function to call when the operation finished.
281 GNUNET_ResultCallback result_cb;
284 * Closure for @a result_cb.
290 struct GNUNET_SOCIAL_LookHandle
295 struct GNUNET_SOCIAL_Place *plc;
303 * State variable result callback.
305 GNUNET_PSYC_StateVarCallback var_cb;
308 * Function to call when the operation finished.
310 GNUNET_ResultCallback result_cb;
313 * Closure for @a result_cb.
319 static struct GNUNET_SOCIAL_Nym *
320 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
322 struct GNUNET_SOCIAL_Nym *nym = NULL;
323 struct GNUNET_HashCode pub_key_hash;
328 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
331 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
333 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
337 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
338 nym->pub_key = *pub_key;
339 nym->pub_key_hash = pub_key_hash;
340 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
348 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
350 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
356 * Call a handler for an incoming message part.
365 slicer_handler_notify (void *cls, const struct GNUNET_HashCode *key,
368 struct GNUNET_SOCIAL_Slicer *slicer = cls;
369 const struct GNUNET_MessageHeader *msg = slicer->msg;
370 struct SlicerCallbacks *cbs = value;
371 uint16_t ptype = ntohs (msg->type);
375 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
377 if (NULL == cbs->method_cb)
379 struct GNUNET_PSYC_MessageMethod *
380 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
381 cbs->method_cb (cbs->cls, meth, slicer->message_id,
383 nym_get_or_create (&slicer->nym_key),
384 slicer->method_name);
388 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
390 if (NULL == cbs->modifier_cb)
392 struct GNUNET_PSYC_MessageModifier *
393 mod = (struct GNUNET_PSYC_MessageModifier *) msg;
394 cbs->modifier_cb (cbs->cls, mod, slicer->message_id,
395 mod->oper, (const char *) &mod[1],
396 (const void *) &mod[1] + ntohs (mod->name_size),
397 ntohs (mod->value_size));
401 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
403 if (NULL == cbs->modifier_cb)
405 /* FIXME: concatenate until done */
409 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
411 if (NULL == cbs->data_cb)
413 uint64_t data_offset = 0; // FIXME
414 cbs->data_cb (cbs->cls, msg, slicer->message_id,
415 data_offset, &msg[1], ntohs (msg->size) - sizeof (*msg));
419 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
420 if (NULL == cbs->eom_cb)
422 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_NO);
425 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
426 if (NULL == cbs->eom_cb)
428 cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_YES);
436 * Process an incoming message part and call matching handlers.
443 * Flags for the message.
444 * @see enum GNUNET_PSYC_MessageFlags
446 * The message part. as it arrived from the network.
449 slicer_message (void *cls, uint64_t message_id, uint64_t fragment_offset,
450 uint32_t flags, const struct GNUNET_MessageHeader *msg)
452 struct GNUNET_SOCIAL_Slicer *slicer = cls;
453 uint16_t ptype = ntohs (msg->type);
454 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
456 struct GNUNET_PSYC_MessageMethod *
457 meth = (struct GNUNET_PSYC_MessageMethod *) msg;
458 slicer->method_name_size = ntohs (meth->header.size) - sizeof (*meth);
459 slicer->method_name = GNUNET_malloc (slicer->method_name_size);
460 memcpy (slicer->method_name, &meth[1], slicer->method_name_size);
461 slicer->message_id = message_id;
465 GNUNET_assert (message_id == slicer->message_id);
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "Slicer received message of type %u and size %u, "
470 "with ID %" PRIu64 " and method %s\n",
471 ptype, ntohs (msg->size), message_id, slicer->method_name);
474 char *name = GNUNET_malloc (slicer->method_name_size);
475 memcpy (name, slicer->method_name, slicer->method_name_size);
478 struct GNUNET_HashCode key;
479 uint16_t name_len = strlen (name);
480 GNUNET_CRYPTO_hash (name, name_len, &key);
481 GNUNET_CONTAINER_multihashmap_get_multiple (slicer->handlers, &key,
482 &slicer_handler_notify, slicer);
483 char *p = strrchr (name, '_');
491 if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
492 GNUNET_free (slicer->method_name);
497 * Create a try-and-slice instance.
499 * @return A new try-and-slice construct.
501 struct GNUNET_SOCIAL_Slicer *
502 GNUNET_SOCIAL_slicer_create (void)
504 struct GNUNET_SOCIAL_Slicer *slicer = GNUNET_malloc (sizeof (*slicer));
505 slicer->handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
511 * Add a method to the try-and-slice instance.
513 * A slicer processes messages and calls methods that match a message. A match
514 * happens whenever the method name of a message starts with the method_name
515 * parameter given here.
517 * @param slicer The try-and-slice instance to extend.
518 * @param method_name Name of the given method, use empty string for default.
519 * @param method Method to invoke.
520 * @param method_cls Closure for method.
523 GNUNET_SOCIAL_slicer_add (struct GNUNET_SOCIAL_Slicer *slicer,
524 const char *method_name,
525 GNUNET_SOCIAL_MethodCallback method_cb,
526 GNUNET_SOCIAL_ModifierCallback modifier_cb,
527 GNUNET_SOCIAL_DataCallback data_cb,
528 GNUNET_SOCIAL_EndOfMessageCallback eom_cb,
531 struct GNUNET_HashCode key;
532 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
534 struct SlicerCallbacks *cbs = GNUNET_malloc (sizeof (*cbs));
535 cbs->method_cb = method_cb;
536 cbs->modifier_cb = modifier_cb;
537 cbs->data_cb = data_cb;
538 cbs->eom_cb = eom_cb;
541 GNUNET_CONTAINER_multihashmap_put (slicer->handlers, &key, cbs,
542 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
547 slicer_remove_handler (void *cls, const struct GNUNET_HashCode *key, void *value)
549 struct SlicerRemoveClosure *rm_cls = cls;
550 struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
551 struct SlicerCallbacks *rm_cbs = &rm_cls->rm_cbs;
552 struct SlicerCallbacks *cbs = value;
554 if (cbs->method_cb == rm_cbs->method_cb
555 && cbs->modifier_cb == rm_cbs->modifier_cb
556 && cbs->data_cb == rm_cbs->data_cb
557 && cbs->eom_cb == rm_cbs->eom_cb)
559 GNUNET_CONTAINER_multihashmap_remove (slicer->handlers, key, cbs);
568 * Remove a registered method from the try-and-slice instance.
570 * Removes the first matching handler registered with @a method and the given callbacks.
572 * @param slicer The try-and-slice instance.
573 * @param method_name Name of the method to remove.
574 * @param method Method handler.
576 * @return #GNUNET_OK if a method handler was removed,
577 * #GNUNET_NO if no handler matched the given method name and callbacks.
580 GNUNET_SOCIAL_slicer_remove (struct GNUNET_SOCIAL_Slicer *slicer,
581 const char *method_name,
582 GNUNET_SOCIAL_MethodCallback method_cb,
583 GNUNET_SOCIAL_ModifierCallback modifier_cb,
584 GNUNET_SOCIAL_DataCallback data_cb,
585 GNUNET_SOCIAL_EndOfMessageCallback eom_cb)
587 struct GNUNET_HashCode key;
588 GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
590 struct SlicerRemoveClosure rm_cls;
591 rm_cls.slicer = slicer;
592 struct SlicerCallbacks *rm_cbs = &rm_cls.rm_cbs;
593 rm_cbs->method_cb = method_cb;
594 rm_cbs->modifier_cb = modifier_cb;
595 rm_cbs->data_cb = data_cb;
596 rm_cbs->eom_cb = eom_cb;
600 == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->handlers, &key,
601 &slicer_remove_handler,
609 slicer_free_handler (void *cls, const struct GNUNET_HashCode *key, void *value)
611 struct SlicerCallbacks *cbs = value;
618 * Destroy a given try-and-slice instance.
624 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer)
626 GNUNET_CONTAINER_multihashmap_iterate (slicer->handlers, &slicer_free_handler,
628 GNUNET_CONTAINER_multihashmap_destroy (slicer->handlers);
629 GNUNET_free (slicer);
634 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
636 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
637 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
638 memcpy (cmsg, plc->connect_msg, cmsg_size);
639 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
644 place_recv_result (void *cls,
645 struct GNUNET_CLIENT_MANAGER_Connection *client,
646 const struct GNUNET_MessageHeader *msg)
648 struct GNUNET_SOCIAL_Place *
649 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
651 const struct GNUNET_OperationResultMessage *
652 res = (const struct GNUNET_OperationResultMessage *) msg;
654 uint16_t size = ntohs (msg->size);
655 if (size < sizeof (*res))
656 { /* Error, message too small. */
661 uint16_t data_size = size - sizeof (*res);
662 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
663 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
664 GNUNET_ntohll_signed (res->result_code),
670 op_recv_history_result (void *cls, int64_t result,
671 const void *err_msg, uint16_t err_msg_size)
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
674 "Received history replay result: %" PRId64 ".\n", result);
676 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
678 if (NULL != hist->result_cb)
679 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
681 GNUNET_PSYC_receive_destroy (hist->recv);
687 op_recv_state_result (void *cls, int64_t result,
688 const void *err_msg, uint16_t err_msg_size)
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 "Received state request result: %" PRId64 ".\n", result);
693 struct GNUNET_SOCIAL_LookHandle *look = cls;
695 if (NULL != look->result_cb)
696 look->result_cb (look->cls, result, err_msg, err_msg_size);
703 place_recv_history_result (void *cls,
704 struct GNUNET_CLIENT_MANAGER_Connection *client,
705 const struct GNUNET_MessageHeader *msg)
707 struct GNUNET_SOCIAL_Place *
708 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
710 const struct GNUNET_OperationResultMessage *
711 res = (const struct GNUNET_OperationResultMessage *) msg;
712 struct GNUNET_PSYC_MessageHeader *
713 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
715 LOG (GNUNET_ERROR_TYPE_DEBUG,
716 "%p Received historic fragment for message #%" PRIu64 ".\n",
717 plc, GNUNET_ntohll (pmsg->message_id));
719 GNUNET_ResultCallback result_cb = NULL;
720 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
722 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
723 GNUNET_ntohll (res->op_id),
724 &result_cb, (void *) &hist))
725 { /* Operation not found. */
726 LOG (GNUNET_ERROR_TYPE_WARNING,
727 "%p Replay operation not found for historic fragment of message #%"
729 plc, GNUNET_ntohll (pmsg->message_id));
733 uint16_t size = ntohs (msg->size);
734 if (size < sizeof (*res) + sizeof (*pmsg))
735 { /* Error, message too small. */
740 GNUNET_PSYC_receive_message (hist->recv,
741 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
746 place_recv_state_result (void *cls,
747 struct GNUNET_CLIENT_MANAGER_Connection *client,
748 const struct GNUNET_MessageHeader *msg)
750 struct GNUNET_SOCIAL_Place *
751 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
753 const struct GNUNET_OperationResultMessage *
754 res = (const struct GNUNET_OperationResultMessage *) msg;
757 GNUNET_ResultCallback result_cb = NULL;
758 struct GNUNET_PSYC_StateRequest *sr = NULL;
760 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
761 GNUNET_ntohll (res->op_id),
762 &result_cb, (void *) &sr))
763 { /* Operation not found. */
767 const struct GNUNET_MessageHeader *
768 modc = (struct GNUNET_MessageHeader *) &res[1];
769 uint16_t modc_size = ntohs (modc->size);
770 if (ntohs (msg->size) - sizeof (*msg) != modc_size)
775 switch (ntohs (modc->type))
777 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
779 const struct GNUNET_PSYC_MessageModifier *
780 mod = (const struct GNUNET_PSYC_MessageModifier *) modc;
782 const char *name = (const char *) &mod[1];
783 uint16_t name_size = ntohs (mod->name_size);
784 if ('\0' != name[name_size - 1])
789 sr->var_cb (sr->cls, name, name + name_size, ntohs (mod->value_size));
793 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
794 sr->var_cb (sr->cls, NULL, (const char *) &modc[1],
795 modc_size - sizeof (*modc));
803 place_recv_message_ack (void *cls,
804 struct GNUNET_CLIENT_MANAGER_Connection *client,
805 const struct GNUNET_MessageHeader *msg)
807 struct GNUNET_SOCIAL_Place *
808 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
809 GNUNET_PSYC_transmit_got_ack (plc->tmit);
814 place_recv_message (void *cls,
815 struct GNUNET_CLIENT_MANAGER_Connection *client,
816 const struct GNUNET_MessageHeader *msg)
818 struct GNUNET_SOCIAL_Place *
819 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
820 GNUNET_PSYC_receive_message (plc->recv,
821 (const struct GNUNET_PSYC_MessageHeader *) msg);
826 place_recv_disconnect (void *cls,
827 struct GNUNET_CLIENT_MANAGER_Connection *client,
828 const struct GNUNET_MessageHeader *msg)
830 struct GNUNET_SOCIAL_Place *
831 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
833 GNUNET_CLIENT_MANAGER_reconnect (client);
834 place_send_connect_msg (plc);
839 host_recv_enter_ack (void *cls,
840 struct GNUNET_CLIENT_MANAGER_Connection *client,
841 const struct GNUNET_MessageHeader *msg)
843 struct GNUNET_SOCIAL_Host *
844 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
845 sizeof (struct GNUNET_SOCIAL_Place));
847 struct GNUNET_PSYC_CountersResultMessage *
848 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
849 int32_t result = ntohl (cres->result_code) + INT32_MIN;
850 if (NULL != hst->enter_cb)
851 hst->enter_cb (hst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
856 host_recv_enter_request (void *cls,
857 struct GNUNET_CLIENT_MANAGER_Connection *client,
858 const struct GNUNET_MessageHeader *msg)
860 struct GNUNET_SOCIAL_Host *
861 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
862 sizeof (struct GNUNET_SOCIAL_Place));
863 if (NULL == hst->answer_door_cb)
866 const char *method_name = NULL;
867 struct GNUNET_ENV_Environment *env = NULL;
868 const void *data = NULL;
869 uint16_t data_size = 0;
871 const struct GNUNET_PSYC_JoinRequestMessage *
872 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
873 const struct GNUNET_PSYC_Message *entry_msg = NULL;
875 if (sizeof (*req) + sizeof (*entry_msg) <= ntohs (req->header.size))
877 entry_msg = (struct GNUNET_PSYC_Message *) &req[1];
878 LOG (GNUNET_ERROR_TYPE_DEBUG,
879 "Received entry_msg of type %u and size %u.\n",
880 ntohs (entry_msg->header.type), ntohs (entry_msg->header.size));
882 env = GNUNET_ENV_environment_create ();
883 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_msg, &method_name, env,
887 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_key);
888 LOG (GNUNET_ERROR_TYPE_WARNING,
889 "Ignoring invalid entry request from nym %s.\n",
892 GNUNET_ENV_environment_destroy (env);
897 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_key);
898 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
902 GNUNET_ENV_environment_destroy (env);
907 guest_recv_enter_ack (void *cls,
908 struct GNUNET_CLIENT_MANAGER_Connection *client,
909 const struct GNUNET_MessageHeader *msg)
911 struct GNUNET_SOCIAL_Guest *
912 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
913 sizeof (struct GNUNET_SOCIAL_Place));
915 struct GNUNET_PSYC_CountersResultMessage *
916 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
917 int32_t result = ntohl (cres->result_code) + INT32_MIN;
918 if (NULL != gst->enter_cb)
919 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
924 guest_recv_join_decision (void *cls,
925 struct GNUNET_CLIENT_MANAGER_Connection *client,
926 const struct GNUNET_MessageHeader *msg)
928 struct GNUNET_SOCIAL_Guest *
929 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
930 sizeof (struct GNUNET_SOCIAL_Place));
931 const struct GNUNET_PSYC_JoinDecisionMessage *
932 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
934 struct GNUNET_PSYC_Message *pmsg = NULL;
935 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
936 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
938 if (NULL != gst->entry_dcsn_cb)
939 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
943 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
945 { &host_recv_enter_ack, NULL,
946 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
947 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
949 { &host_recv_enter_request, NULL,
950 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
951 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
953 { &place_recv_message, NULL,
954 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
955 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
957 { &place_recv_message_ack, NULL,
958 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
959 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
961 { &place_recv_history_result, NULL,
962 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
963 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
965 { &place_recv_state_result, NULL,
966 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
967 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
969 { &place_recv_result, NULL,
970 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
971 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
973 { &place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
975 { NULL, NULL, 0, 0, GNUNET_NO }
979 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
981 { &guest_recv_enter_ack, NULL,
982 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
983 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
985 { &host_recv_enter_request, NULL,
986 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
987 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
989 { &place_recv_message, NULL,
990 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
991 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
993 { &place_recv_message_ack, NULL,
994 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
995 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
997 { &guest_recv_join_decision, NULL,
998 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
999 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1001 { &place_recv_history_result, NULL,
1002 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1003 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1005 { &place_recv_state_result, NULL,
1006 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1007 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1009 { &place_recv_result, NULL,
1010 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1011 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1013 { &place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1015 { NULL, NULL, 0, 0, GNUNET_NO }
1020 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1022 GNUNET_PSYC_transmit_destroy (plc->tmit);
1023 GNUNET_PSYC_receive_destroy (plc->recv);
1024 GNUNET_free (plc->connect_msg);
1025 if (NULL != plc->disconnect_cb)
1026 plc->disconnect_cb (plc->disconnect_cls);
1031 host_cleanup (void *cls)
1033 struct GNUNET_SOCIAL_Host *hst = cls;
1034 place_cleanup (&hst->plc);
1040 guest_cleanup (void *cls)
1042 struct GNUNET_SOCIAL_Guest *gst = cls;
1043 place_cleanup (&gst->plc);
1049 * Enter a place as host.
1051 * A place is created upon first entering, and it is active until permanently
1052 * left using GNUNET_SOCIAL_host_leave().
1055 * Configuration to contact the social service.
1057 * Identity of the host.
1059 * Private-public key pair of the place.
1060 * NULL for ephemeral places.
1062 * Policy specifying entry and history restrictions for the place.
1064 * Slicer to handle incoming messages.
1065 * @param answer_door_cb
1066 * Function to handle new nyms that want to enter.
1067 * @param farewell_cb
1068 * Function to handle departing nyms.
1070 * Closure for the callbacks.
1072 * @return Handle for the host.
1074 struct GNUNET_SOCIAL_Host *
1075 GNUNET_SOCIAL_host_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1076 const struct GNUNET_IDENTITY_Ego *ego,
1077 const struct GNUNET_CRYPTO_EddsaPrivateKey *place_key,
1078 enum GNUNET_PSYC_Policy policy,
1079 struct GNUNET_SOCIAL_Slicer *slicer,
1080 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1081 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1082 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1085 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1086 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1087 struct HostEnterRequest *req = GNUNET_malloc (sizeof (*req));
1089 if (NULL != place_key)
1091 hst->place_key = *place_key;
1095 struct GNUNET_CRYPTO_EddsaPrivateKey *
1096 ephemeral_key = GNUNET_CRYPTO_eddsa_key_create ();
1097 hst->place_key = *ephemeral_key;
1098 GNUNET_CRYPTO_eddsa_key_get_public (&hst->place_key, &plc->pub_key);
1099 GNUNET_CRYPTO_eddsa_key_clear (ephemeral_key);
1100 GNUNET_free (ephemeral_key);
1102 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1104 req->header.size = htons (sizeof (*req));
1105 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1106 req->policy = policy;
1107 req->place_key = hst->place_key;
1108 req->host_key = plc->ego_key;
1110 plc->connect_msg = (struct GNUNET_MessageHeader *) req;
1112 plc->is_host = GNUNET_YES;
1113 plc->slicer = slicer;
1115 hst->plc.ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1116 hst->enter_cb = enter_cb;
1117 hst->answer_door_cb = answer_door_cb;
1120 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", host_handlers);
1121 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1123 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1124 plc->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, plc->slicer);
1126 place_send_connect_msg (plc);
1132 * Enter a place as host.
1134 * A place is created upon first entering, and it is active until permanently
1135 * left using GNUNET_SOCIAL_host_leave().
1138 * Configuration to contact the social service.
1140 * Identity of the host.
1142 * GNS name in the zone of the @a ego that contains the
1143 * public key of the place in a PLACE record.
1145 * Policy specifying entry and history restrictions for the place.
1147 * Slicer to handle incoming messages.
1148 * @param answer_door_cb
1149 * Function to handle new nyms that want to enter.
1150 * @param farewell_cb
1151 * Function to handle departing nyms.
1153 * Closure for the callbacks.
1155 * @return Handle for the host.
1157 struct GNUNET_SOCIAL_Host *
1158 GNUNET_SOCIAL_host_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
1159 struct GNUNET_IDENTITY_Ego *ego,
1160 const char *gns_name,
1161 enum GNUNET_PSYC_Policy policy,
1162 struct GNUNET_SOCIAL_Slicer *slicer,
1163 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1164 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1165 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1168 struct GNUNET_CRYPTO_EddsaPrivateKey place_key = {};
1171 * 1. get public key by looking up PLACE entry under gns_name
1172 * in the zone of the ego.
1173 * 2. get private key from $GNUNET_DATA_HOME/social/places/PUB_KEY_HASH
1176 return GNUNET_SOCIAL_host_enter (cfg, ego, &place_key, policy, slicer,
1177 enter_cb, answer_door_cb, farewell_cb, cls);
1182 * Decision whether to admit @a nym into the place or refuse entry.
1185 * Host of the place.
1187 * Handle for the entity that wanted to enter.
1188 * @param is_admitted
1189 * #GNUNET_YES if @a nym is admitted,
1190 * #GNUNET_NO if @a nym is refused entry,
1191 * #GNUNET_SYSERR if we cannot answer the request.
1192 * @param method_name
1193 * Method name for the rejection message.
1195 * Environment containing variables for the message, or NULL.
1197 * Data for the rejection message to send back.
1199 * Number of bytes in @a data for method.
1200 * @return #GNUNET_OK on success,
1201 * #GNUNET_SYSERR if the message is too large.
1204 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1205 struct GNUNET_SOCIAL_Nym *nym,
1207 const struct GNUNET_PSYC_Message *entry_resp)
1209 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1210 uint16_t entry_resp_size
1211 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1213 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1214 return GNUNET_SYSERR;
1216 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1217 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1218 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1219 dcsn->is_admitted = htonl (is_admitted);
1220 dcsn->slave_key = nym->pub_key;
1222 if (0 < entry_resp_size)
1223 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1225 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1231 * Throw @a nym out of the place.
1233 * The @a nym reference will remain valid until the
1234 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1235 * which should be very soon after this call.
1237 * @param host Host of the place.
1238 * @param nym Handle for the entity to be ejected.
1241 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *host,
1242 struct GNUNET_SOCIAL_Nym *nym)
1249 * Get the public key of a @a nym.
1251 * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
1253 * @param nym Pseudonym to map to a cryptographic identifier.
1254 * @param[out] nym_key Set to the public key of the nym.
1256 struct GNUNET_CRYPTO_EcdsaPublicKey *
1257 GNUNET_SOCIAL_nym_get_key (struct GNUNET_SOCIAL_Nym *nym)
1259 return &nym->pub_key;
1264 * Obtain the private-public key pair of the hosted place.
1266 * The public part is suitable for storing in GNS within a PLACE record,
1267 * along with peer IDs to join at.
1270 * Host of the place.
1272 * @return Private-public key pair of the hosted place.
1274 const struct GNUNET_CRYPTO_EddsaPrivateKey *
1275 GNUNET_SOCIAL_host_get_place_key (struct GNUNET_SOCIAL_Host *hst)
1277 return &hst->place_key;
1282 namestore_result_host_advertise (void *cls, int32_t success, const char *emsg)
1289 * Connected to core service.
1292 core_connected_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1294 this_peer = *my_identity;
1300 * Advertise the place in the GNS zone of the @e ego of the @a host.
1302 * @param hst Host of the place.
1303 * @param name The name for the PLACE record to put in the zone.
1304 * @param peer_count Number of elements in the @a peers array.
1305 * @param peers List of peers in the PLACE record that can be used to send join
1307 * @param expiration_time Expiration time of the record, use 0 to remove the record.
1308 * @param password Password used to encrypt the record.
1311 GNUNET_SOCIAL_host_advertise (struct GNUNET_SOCIAL_Host *hst,
1314 const struct GNUNET_PeerIdentity *peers,
1315 struct GNUNET_TIME_Relative expiration_time,
1316 const char *password)
1318 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1319 if (NULL == namestore)
1320 namestore = GNUNET_NAMESTORE_connect (plc->cfg);
1322 core = GNUNET_CORE_connect (plc->cfg, NULL, core_connected_cb, NULL, NULL,
1323 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
1325 struct GNUNET_GNSRECORD_Data rd = { 0 };
1326 rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
1327 rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1329 = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_WEEKS, 1).rel_value_us;
1331 struct GNUNET_GNSRECORD_PlaceData *rec = GNUNET_malloc (sizeof (*rec));
1332 rec->place_key = plc->pub_key;
1333 rec->origin = this_peer;
1334 rec->relay_count = htons (0); // FIXME
1336 rd.data_size = sizeof (*rec);
1339 GNUNET_NAMESTORE_records_store (namestore, &hst->plc.ego_key,
1340 name, 1, &rd, namestore_result_host_advertise,
1346 * Send a message to all nyms that are present in the place.
1348 * This function is restricted to the host. Nyms can only send requests
1349 * to the host who can decide to relay it to everyone in the place.
1351 * @param host Host of the place.
1352 * @param method_name Method to use for the announcement.
1353 * @param env Environment containing variables for the message and operations
1354 * on objects of the place. Can be NULL.
1355 * @param notify Function to call to get the payload of the announcement.
1356 * @param notify_cls Closure for @a notify.
1357 * @param flags Flags for this announcement.
1359 * @return NULL on error (announcement already in progress?).
1361 struct GNUNET_SOCIAL_Announcement *
1362 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1363 const char *method_name,
1364 const struct GNUNET_ENV_Environment *env,
1365 GNUNET_PSYC_TransmitNotifyData notify_data,
1366 void *notify_data_cls,
1367 enum GNUNET_SOCIAL_AnnounceFlags flags)
1370 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1371 NULL, notify_data, notify_data_cls, flags));
1372 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1377 * Resume transmitting announcement.
1380 * The announcement to resume.
1383 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1385 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1390 * Cancel announcement.
1393 * The announcement to cancel.
1396 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1398 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1403 * Obtain handle for a hosted place.
1405 * The returned handle can be used to access the place API.
1407 * @param host Handle for the host.
1409 * @return Handle for the hosted place, valid as long as @a host is valid.
1411 struct GNUNET_SOCIAL_Place *
1412 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1419 * Stop hosting a place.
1421 * Invalidates host handle.
1423 * @param host Host leaving the place.
1424 * @param keep_active Keep the place active after last host disconnected.
1427 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1429 GNUNET_ContinuationCallback leave_cb,
1432 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1434 /* FIXME: send msg to service */
1436 plc->is_disconnecting = GNUNET_YES;
1437 plc->disconnect_cb = leave_cb;
1438 plc->disconnect_cls = leave_cls;
1440 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1441 &host_cleanup, hst);
1445 static struct GuestEnterRequest *
1446 guest_enter_request_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key,
1447 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1448 const struct GNUNET_PeerIdentity *origin,
1450 const struct GNUNET_PeerIdentity *relays,
1451 const struct GNUNET_PSYC_Message *join_msg)
1453 uint16_t join_msg_size = ntohs (join_msg->header.size);
1454 uint16_t relay_size = relay_count * sizeof (*relays);
1456 struct GuestEnterRequest *
1457 req = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size);
1459 req->header.size = htons (sizeof (*req) + relay_size + join_msg_size);
1460 req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1461 req->place_key = *place_key;
1462 req->guest_key = *guest_key;
1463 req->origin = *origin;
1464 req->relay_count = relay_count;
1466 uint16_t p = sizeof (*req);
1469 memcpy ((char *) req + p, relays, relay_size);
1473 memcpy ((char *) req + p, join_msg, join_msg_size);
1478 * Request entry to a place as a guest.
1480 * @param cfg Configuration to contact the social service.
1481 * @param ego Identity of the guest.
1482 * @param crypto_address Public key of the place to enter.
1483 * @param origin Peer identity of the origin of the underlying multicast group.
1484 * @param relay_count Number of elements in the @a relays array.
1485 * @param relays Relays for the underlying multicast group.
1486 * @param method_name Method name for the message.
1487 * @param env Environment containing variables for the message, or NULL.
1488 * @param data Payload for the message to give to the enter callback.
1489 * @param data_size Number of bytes in @a data.
1490 * @param slicer Slicer to use for processing incoming requests from guests.
1492 * @return NULL on errors, otherwise handle for the guest.
1494 struct GNUNET_SOCIAL_Guest *
1495 GNUNET_SOCIAL_guest_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1496 const struct GNUNET_IDENTITY_Ego *ego,
1497 const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1498 const struct GNUNET_PeerIdentity *origin,
1499 uint32_t relay_count,
1500 const struct GNUNET_PeerIdentity *relays,
1501 const struct GNUNET_PSYC_Message *entry_msg,
1502 struct GNUNET_SOCIAL_Slicer *slicer,
1503 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1504 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1507 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1508 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1510 struct GuestEnterRequest *
1511 req = guest_enter_request_create (&plc->ego_key, place_key, origin,
1512 relay_count, relays, entry_msg);
1513 plc->connect_msg = &req->header;
1514 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1515 plc->pub_key = *place_key;
1517 plc->is_host = GNUNET_YES;
1518 plc->slicer = slicer;
1520 gst->enter_cb = local_enter_cb;
1521 gst->entry_dcsn_cb = entry_dcsn_cb;
1524 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
1525 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1527 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1528 plc->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, plc->slicer);
1530 place_send_connect_msg (plc);
1536 * Result of a GNS name lookup for entering a place.
1538 * @see GNUNET_SOCIAL_guest_enter_by_name
1541 gns_result_guest_enter (void *cls, uint32_t rd_count,
1542 const struct GNUNET_GNSRECORD_Data *rd)
1544 struct GNUNET_SOCIAL_Guest *gst = cls;
1545 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1547 const struct GNUNET_GNSRECORD_PlaceData *
1548 rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
1552 if (NULL != gst->enter_cb)
1553 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
1558 if (rd->data_size < sizeof (*rec))
1560 GNUNET_break_op (0);
1561 if (NULL != gst->enter_cb)
1562 gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
1566 struct GuestEnterRequest *
1567 req = (struct GuestEnterRequest *) plc->connect_msg;
1568 uint16_t req_size = ntohs (req->header.size);
1570 struct GNUNET_PeerIdentity *relays = NULL;
1571 uint16_t relay_count = ntohs (rec->relay_count);
1573 if (0 < relay_count)
1575 uint16_t relay_size = relay_count * sizeof (struct GNUNET_PeerIdentity);
1576 struct GuestEnterRequest *
1577 req2 = GNUNET_malloc (req_size + relay_size);
1579 req2->header.size = htons (req_size + relay_size);
1580 req2->header.type = req->header.type;
1581 req2->guest_key = req->guest_key;
1583 uint16_t p = sizeof (*req);
1586 memcpy ((char *) req2 + p, relays, relay_size);
1590 memcpy ((char *) req + p, &req[1], req_size - sizeof (*req));
1592 plc->connect_msg = &req2->header;
1597 req->place_key = rec->place_key;
1598 req->origin = rec->origin;
1599 req->relay_count = rec->relay_count;
1600 memcpy (&req[1], &rec[1],
1601 ntohl (rec->relay_count) * sizeof (struct GNUNET_PeerIdentity));
1603 plc->connect_msg = &req->header;
1604 plc->pub_key = req->place_key;
1606 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1607 plc->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, plc);
1609 place_send_connect_msg (plc);
1613 * Request entry to a place as a guest.
1615 * @param cfg Configuration to contact the social service.
1616 * @param ego Identity of the guest.
1617 * @param address GNS name of the place to enter. Either in the form of
1618 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1619 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1620 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1621 * pseudonym's place directly.
1622 * @param method_name Method name for the message.
1623 * @param env Environment containing variables for the message, or NULL.
1624 * @param data Payload for the message to give to the enter callback.
1625 * @param data_size Number of bytes in @a data.
1626 * @param slicer Slicer to use for processing incoming requests from guests.
1628 * @return NULL on errors, otherwise handle for the guest.
1630 struct GNUNET_SOCIAL_Guest *
1631 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
1632 struct GNUNET_IDENTITY_Ego *ego,
1634 const struct GNUNET_PSYC_Message *join_msg,
1635 struct GNUNET_SOCIAL_Slicer *slicer,
1636 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1637 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1640 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1641 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1643 gst->enter_cb = local_enter_cb;
1646 plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1648 plc->is_host = GNUNET_NO;
1649 plc->slicer = slicer;
1651 struct GuestEnterRequest *
1652 req = guest_enter_request_create (&plc->ego_key, NULL, NULL, 0, NULL,
1654 plc->connect_msg = &req->header;
1656 /* FIXME: get the public key of the origin and relays
1657 * by looking up the PLACE record of gns_name.
1660 gns = GNUNET_GNS_connect (cfg);
1662 plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
1663 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1665 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
1666 GNUNET_IDENTITY_ego_get_public_key (ego, &ego_pub_key);
1667 GNUNET_GNS_lookup (gns, gns_name, &ego_pub_key,
1668 GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
1669 NULL, gns_result_guest_enter, gst);
1676 * Talk to the host of the place.
1679 * Place where we want to talk to the host.
1680 * @param method_name
1681 * Method to invoke on the host.
1683 * Environment containing variables for the message, or NULL.
1684 * @param notify_data
1685 * Function to use to get the payload for the method.
1686 * @param notify_data_cls
1687 * Closure for @a notify_data.
1689 * Flags for the message being sent.
1691 * @return NULL if we are already trying to talk to the host,
1692 * otherwise handle to cancel the request.
1694 struct GNUNET_SOCIAL_TalkRequest *
1695 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1696 const char *method_name,
1697 const struct GNUNET_ENV_Environment *env,
1698 GNUNET_PSYC_TransmitNotifyData notify_data,
1699 void *notify_data_cls,
1700 enum GNUNET_SOCIAL_TalkFlags flags)
1703 GNUNET_PSYC_transmit_message (gst->plc.tmit, method_name, env,
1704 NULL, notify_data, notify_data_cls, flags));
1705 return (struct GNUNET_SOCIAL_TalkRequest *) gst->plc.tmit;
1710 * Resume talking to the host of the place.
1713 * Talk request to resume.
1716 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1718 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1723 * Cancel talking to the host of the place.
1726 * Talk request to cancel.
1729 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1731 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tr);
1736 * Leave a place permanently.
1738 * Notifies the owner of the place about leaving, and destroys the place handle.
1740 * @param place Place to leave permanently.
1741 * @param keep_active Keep place active after last application disconnected.
1744 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1746 GNUNET_ContinuationCallback leave_cb,
1749 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1751 /* FIXME: send msg to service */
1753 plc->is_disconnecting = GNUNET_YES;
1754 plc->disconnect_cb = leave_cb;
1755 plc->disconnect_cls = leave_cls;
1757 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1758 &guest_cleanup, gst);
1763 * Obtain handle for a place entered as guest.
1765 * The returned handle can be used to access the place API.
1767 * @param guest Handle for the guest.
1769 * @return Handle for the place, valid as long as @a guest is valid.
1771 struct GNUNET_SOCIAL_Place *
1772 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1779 * Watch a place for changed objects.
1783 * @param object_filter
1784 * Object prefix to match.
1786 * Function to call when an object/state var changes.
1788 * Closure for callback.
1790 * @return Handle that can be used to cancel watching.
1792 struct GNUNET_SOCIAL_WatchHandle *
1793 GNUNET_SOCIAL_place_watch (struct GNUNET_SOCIAL_Place *place,
1794 const char *object_filter,
1795 GNUNET_PSYC_StateVarCallback var_cb,
1803 * Cancel watching a place for changed objects.
1805 * @param wh Watch handle to cancel.
1808 GNUNET_SOCIAL_place_watch_cancel (struct GNUNET_SOCIAL_WatchHandle *wh)
1814 static struct GNUNET_SOCIAL_HistoryRequest *
1815 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
1816 uint64_t start_message_id,
1817 uint64_t end_message_id,
1818 uint64_t message_limit,
1819 const char *method_prefix,
1821 struct GNUNET_SOCIAL_Slicer *slicer,
1822 GNUNET_ResultCallback result_cb,
1825 struct GNUNET_PSYC_HistoryRequestMessage *req;
1826 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
1828 hist->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, slicer);
1829 hist->result_cb = result_cb;
1831 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
1832 &op_recv_history_result, hist);
1834 GNUNET_assert (NULL != method_prefix);
1835 uint16_t method_size = strnlen (method_prefix,
1836 GNUNET_SERVER_MAX_MESSAGE_SIZE
1837 - sizeof (*req)) + 1;
1838 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
1839 req = GNUNET_malloc (sizeof (*req) + method_size);
1840 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
1841 req->header.size = htons (sizeof (*req) + method_size);
1842 req->start_message_id = GNUNET_htonll (start_message_id);
1843 req->end_message_id = GNUNET_htonll (end_message_id);
1844 req->message_limit = GNUNET_htonll (message_limit);
1845 req->flags = htonl (flags);
1846 req->op_id = GNUNET_htonll (hist->op_id);
1847 memcpy (&req[1], method_prefix, method_size);
1849 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
1855 * Learn about the history of a place.
1857 * Messages are returned through the @a slicer function
1858 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
1861 * Place we want to learn more about.
1862 * @param start_message_id
1863 * First historic message we are interested in.
1864 * @param end_message_id
1865 * Last historic message we are interested in (inclusive).
1866 * @param method_prefix
1867 * Only retrieve messages with this method prefix.
1869 * OR'ed GNUNET_PSYC_HistoryReplayFlags
1871 * Slicer to use for retrieved messages.
1872 * Can be the same as the slicer of the place.
1874 * Function called after all messages retrieved.
1875 * NULL if not needed.
1876 * @param cls Closure for @a result_cb.
1878 struct GNUNET_SOCIAL_HistoryRequest *
1879 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
1880 uint64_t start_message_id,
1881 uint64_t end_message_id,
1882 const char *method_prefix,
1884 struct GNUNET_SOCIAL_Slicer *slicer,
1885 GNUNET_ResultCallback result_cb,
1888 return place_history_replay (plc, start_message_id, end_message_id, 0,
1889 method_prefix, flags, slicer, result_cb, cls);
1894 * Learn about the history of a place.
1896 * Sends messages through the slicer function of the place where
1897 * start_message_id <= message_id <= end_message_id.
1898 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
1900 * To get the latest message, use 0 for both the start and end message ID.
1903 * Place we want to learn more about.
1904 * @param message_limit
1905 * Maximum number of historic messages we are interested in.
1906 * @param method_prefix
1907 * Only retrieve messages with this method prefix.
1909 * OR'ed GNUNET_PSYC_HistoryReplayFlags
1911 * Function called after all messages retrieved.
1912 * NULL if not needed.
1913 * @param cls Closure for @a result_cb.
1915 struct GNUNET_SOCIAL_HistoryRequest *
1916 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
1917 uint64_t message_limit,
1918 const char *method_prefix,
1920 struct GNUNET_SOCIAL_Slicer *slicer,
1921 GNUNET_ResultCallback result_cb,
1924 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
1925 slicer, result_cb, cls);
1930 * Cancel learning about the history of a place.
1933 * History lesson to cancel.
1936 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
1938 GNUNET_PSYC_receive_destroy (hist->recv);
1939 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
1945 * Request matching state variables.
1947 static struct GNUNET_SOCIAL_LookHandle *
1948 place_state_get (struct GNUNET_SOCIAL_Place *plc,
1949 uint16_t type, const char *name,
1950 GNUNET_PSYC_StateVarCallback var_cb,
1951 GNUNET_ResultCallback result_cb, void *cls)
1953 struct GNUNET_PSYC_StateRequestMessage *req;
1954 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
1956 look->var_cb = var_cb;
1957 look->result_cb = result_cb;
1959 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
1960 &op_recv_state_result, look);
1962 GNUNET_assert (NULL != name);
1963 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
1964 - sizeof (*req)) + 1;
1965 req = GNUNET_malloc (sizeof (*req) + name_size);
1966 req->header.type = htons (type);
1967 req->header.size = htons (sizeof (*req) + name_size);
1968 req->op_id = GNUNET_htonll (look->op_id);
1969 memcpy (&req[1], name, name_size);
1971 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
1977 * Look at a particular object in the place.
1979 * The best matching object is returned (its name might be less specific than
1980 * what was requested).
1983 * The place to look the object at.
1985 * Full name of the object.
1987 * Set to the size of the returned value.
1989 * @return NULL if there is no such object at this place.
1991 struct GNUNET_SOCIAL_LookHandle *
1992 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
1993 const char *full_name,
1994 GNUNET_PSYC_StateVarCallback var_cb,
1995 GNUNET_ResultCallback result_cb,
1998 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
1999 full_name, var_cb, result_cb, cls);
2004 * Look for objects in the place with a matching name prefix.
2007 * The place to look its objects at.
2008 * @param name_prefix
2009 * Look at objects with names beginning with this value.
2011 * Function to call for each object found.
2013 * Closure for callback function.
2015 * @return Handle that can be used to stop looking at objects.
2017 struct GNUNET_SOCIAL_LookHandle *
2018 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2019 const char *name_prefix,
2020 GNUNET_PSYC_StateVarCallback var_cb,
2021 GNUNET_ResultCallback result_cb,
2024 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2025 name_prefix, var_cb, result_cb, cls);
2030 * Cancel a state request operation.
2033 * Handle for the operation to cancel.
2036 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2038 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2043 /* end of social_api.c */