2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
5 * GNUnet is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your 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 * Affero General Public License for more details.
17 * @author Gabor X Toth
20 * Social service; implements social interactions using the PSYC service.
27 #include "gnunet_util_lib.h"
28 #include "gnunet_psyc_service.h"
29 #include "gnunet_psyc_util_lib.h"
30 #include "gnunet_social_service.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "social-api",__VA_ARGS__)
38 struct GNUNET_SOCIAL_Ego
40 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
41 struct GNUNET_HashCode pub_key_hash;
47 * Handle for a pseudonym of another user in the network.
49 struct GNUNET_SOCIAL_Nym
51 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
52 struct GNUNET_HashCode pub_key_hash;
57 * Handle for an application.
59 struct GNUNET_SOCIAL_App
62 * Configuration to use.
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
67 * Client connection to the service.
69 struct GNUNET_MQ_Handle *mq;
72 * Message to send on connect.
74 struct GNUNET_MQ_Envelope *connect_env;
77 * Time to wait until we try to reconnect on failure.
79 struct GNUNET_TIME_Relative reconnect_delay;
82 * Task for reconnecting when the listener fails.
84 struct GNUNET_SCHEDULER_Task *reconnect_task;
89 struct GNUNET_OP_Handle *op;
92 * Function called after disconnected from the service.
94 GNUNET_ContinuationCallback disconnect_cb;
97 * Closure for @a disconnect_cb.
107 * Hash map of all egos.
108 * pub_key_hash -> struct GNUNET_SOCIAL_Ego *
110 struct GNUNET_CONTAINER_MultiHashMap *egos;
112 GNUNET_SOCIAL_AppEgoCallback ego_cb;
113 GNUNET_SOCIAL_AppHostPlaceCallback host_cb;
114 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb;
115 GNUNET_SOCIAL_AppConnectedCallback connected_cb;
120 struct GNUNET_SOCIAL_HostConnection
122 struct GNUNET_SOCIAL_App *app;
124 struct AppPlaceMessage plc_msg;
128 struct GNUNET_SOCIAL_GuestConnection
130 struct GNUNET_SOCIAL_App *app;
132 struct AppPlaceMessage plc_msg;
137 * Handle for a place where social interactions happen.
139 struct GNUNET_SOCIAL_Place
142 * Configuration to use.
144 const struct GNUNET_CONFIGURATION_Handle *cfg;
147 * Client connection to the service.
149 struct GNUNET_MQ_Handle *mq;
152 * Message to send on connect.
154 struct GNUNET_MQ_Envelope *connect_env;
157 * Time to wait until we try to reconnect on failure.
159 struct GNUNET_TIME_Relative reconnect_delay;
162 * Task for reconnecting when the listener fails.
164 struct GNUNET_SCHEDULER_Task *reconnect_task;
169 struct GNUNET_OP_Handle *op;
172 * Transmission handle.
174 struct GNUNET_PSYC_TransmitHandle *tmit;
177 * Slicer for processing incoming messages.
179 struct GNUNET_PSYC_Slicer *slicer;
181 // FIXME: do we need is_disconnecing like on the psyc and multicast APIs?
183 * Function called after disconnected from the service.
185 GNUNET_ContinuationCallback disconnect_cb;
188 * Closure for @a disconnect_cb.
190 void *disconnect_cls;
193 * Public key of the place.
195 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
198 * Public key of the ego.
200 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
203 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
210 * Host handle for a place that we entered.
212 struct GNUNET_SOCIAL_Host
214 struct GNUNET_SOCIAL_Place plc;
217 * Slicer for processing incoming messages from guests.
219 struct GNUNET_PSYC_Slicer *slicer;
221 GNUNET_SOCIAL_HostEnterCallback enter_cb;
223 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
225 GNUNET_SOCIAL_FarewellCallback farewell_cb;
228 * Closure for callbacks.
232 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
233 struct GNUNET_PSYC_Environment *notice_place_leave_env;
238 * Guest handle for place that we entered.
240 struct GNUNET_SOCIAL_Guest
242 struct GNUNET_SOCIAL_Place plc;
244 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
246 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
249 * Closure for callbacks.
256 * Hash map of all nyms.
257 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
259 struct GNUNET_CONTAINER_MultiHashMap *nyms;
263 * Handle for an announcement request.
265 struct GNUNET_SOCIAL_Announcement
274 struct GNUNET_SOCIAL_TalkRequest
283 struct GNUNET_SOCIAL_HistoryRequest
288 struct GNUNET_SOCIAL_Place *plc;
296 * Slicer for processing incoming messages.
298 struct GNUNET_PSYC_Slicer *slicer;
301 * Function to call when the operation finished.
303 GNUNET_ResultCallback result_cb;
306 * Closure for @a result_cb.
312 struct GNUNET_SOCIAL_LookHandle
317 struct GNUNET_SOCIAL_Place *plc;
325 * State variable result callback.
327 GNUNET_PSYC_StateVarCallback var_cb;
330 * Function to call when the operation finished.
332 GNUNET_ResultCallback result_cb;
335 * Name of current modifier being received.
340 * Size of current modifier value being received.
342 size_t mod_value_size;
345 * Remaining size of current modifier value still to be received.
347 size_t mod_value_remaining;
350 * Closure for @a result_cb.
356 struct ZoneAddPlaceHandle
358 GNUNET_ResultCallback result_cb;
363 struct ZoneAddNymHandle
365 GNUNET_ResultCallback result_cb;
370 /*** CLEANUP / DISCONNECT ***/
374 host_cleanup (struct GNUNET_SOCIAL_Host *hst)
376 if (NULL != hst->slicer)
378 GNUNET_PSYC_slicer_destroy (hst->slicer);
386 guest_cleanup (struct GNUNET_SOCIAL_Guest *gst)
393 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "cleaning up place %p\n",
398 if (NULL != plc->tmit)
400 GNUNET_PSYC_transmit_destroy (plc->tmit);
403 if (NULL != plc->connect_env)
405 GNUNET_MQ_discard (plc->connect_env);
406 plc->connect_env = NULL;
410 GNUNET_MQ_destroy (plc->mq);
413 if (NULL != plc->disconnect_cb)
415 plc->disconnect_cb (plc->disconnect_cls);
416 plc->disconnect_cb = NULL;
419 (GNUNET_YES == plc->is_host)
420 ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
421 : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
426 place_disconnect (struct GNUNET_SOCIAL_Place *plc)
434 static struct GNUNET_SOCIAL_Nym *
435 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
437 struct GNUNET_SOCIAL_Nym *nym = NULL;
438 struct GNUNET_HashCode pub_key_hash;
443 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
446 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
448 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
452 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
453 nym->pub_key = *pub_key;
454 nym->pub_key_hash = pub_key_hash;
455 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
463 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
465 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
470 /*** MESSAGE HANDLERS ***/
472 /** _notice_place_leave from guests */
475 host_recv_notice_place_leave_method (void *cls,
476 const struct GNUNET_PSYC_MessageHeader *msg,
477 const struct GNUNET_PSYC_MessageMethod *meth,
479 const char *method_name)
481 struct GNUNET_SOCIAL_Host *hst = cls;
483 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
484 &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
487 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
491 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
493 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
494 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
496 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "_notice_place_leave: got method from nym %s (%s).\n",
499 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
505 host_recv_notice_place_leave_modifier (void *cls,
506 const struct GNUNET_PSYC_MessageHeader *msg,
507 const struct GNUNET_MessageHeader *pmsg,
509 enum GNUNET_PSYC_Operator oper,
513 uint16_t full_value_size)
515 struct GNUNET_SOCIAL_Host *hst = cls;
516 if (NULL == hst->notice_place_leave_env)
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
522 message_id, oper, name, value_size, (const char *) value);
524 /* skip _nym, it's added later in eom() */
525 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
526 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
529 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
530 GNUNET_PSYC_OP_SET, name, value, value_size);
535 host_recv_notice_place_leave_eom (void *cls,
536 const struct GNUNET_PSYC_MessageHeader *msg,
537 const struct GNUNET_MessageHeader *pmsg,
539 uint8_t is_cancelled)
541 struct GNUNET_SOCIAL_Host *hst = cls;
542 if (NULL == hst->notice_place_leave_env)
545 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "_notice_place_leave: got EOM from nym %s (%s).\n",
548 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
551 if (GNUNET_YES != is_cancelled)
553 if (NULL != hst->farewell_cb)
554 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
555 hst->notice_place_leave_env);
556 /* announce leaving guest to place */
557 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
558 "_nym", hst->notice_place_leave_nym,
559 sizeof (*hst->notice_place_leave_nym));
560 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
561 hst->notice_place_leave_env,
562 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
563 nym_destroy (hst->notice_place_leave_nym);
565 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
566 hst->notice_place_leave_env = NULL;
574 check_place_result (void *cls,
575 const struct GNUNET_OperationResultMessage *res)
577 uint16_t size = ntohs (res->header.size);
578 if (size < sizeof (*res))
579 { /* Error, message too small. */
581 return GNUNET_SYSERR;
588 handle_place_result (void *cls,
589 const struct GNUNET_OperationResultMessage *res)
591 struct GNUNET_SOCIAL_Place *plc = cls;
593 uint16_t size = ntohs (res->header.size);
594 uint16_t data_size = size - sizeof (*res);
595 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
597 GNUNET_OP_result (plc->op, GNUNET_ntohll (res->op_id),
598 GNUNET_ntohll (res->result_code),
599 data, data_size, NULL);
604 check_app_result (void *cls,
605 const struct GNUNET_OperationResultMessage *res)
607 uint16_t size = ntohs (res->header.size);
608 if (size < sizeof (*res))
609 { /* Error, message too small. */
611 return GNUNET_SYSERR;
618 handle_app_result (void *cls,
619 const struct GNUNET_OperationResultMessage *res)
621 struct GNUNET_SOCIAL_App *app = cls;
623 uint16_t size = ntohs (res->header.size);
624 uint16_t data_size = size - sizeof (*res);
625 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
627 GNUNET_OP_result (app->op, GNUNET_ntohll (res->op_id),
628 GNUNET_ntohll (res->result_code),
629 data, data_size, NULL);
634 op_recv_history_result (void *cls, int64_t result,
635 const void *err_msg, uint16_t err_msg_size)
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "Received history replay result: %" PRId64 ".\n", result);
640 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
642 if (NULL != hist->result_cb)
643 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
650 op_recv_state_result (void *cls, int64_t result,
651 const void *err_msg, uint16_t err_msg_size)
653 LOG (GNUNET_ERROR_TYPE_DEBUG,
654 "Received state request result: %" PRId64 ".\n", result);
656 struct GNUNET_SOCIAL_LookHandle *look = cls;
658 if (NULL != look->result_cb)
659 look->result_cb (look->cls, result, err_msg, err_msg_size);
666 check_place_history_result (void *cls,
667 const struct GNUNET_OperationResultMessage *res)
669 struct GNUNET_PSYC_MessageHeader *
670 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
671 uint16_t size = ntohs (res->header.size);
673 if (NULL == pmsg || size < sizeof (*res) + sizeof (*pmsg))
674 { /* Error, message too small. */
676 return GNUNET_SYSERR;
683 handle_place_history_result (void *cls,
684 const struct GNUNET_OperationResultMessage *res)
686 struct GNUNET_SOCIAL_Place *plc = cls;
687 struct GNUNET_PSYC_MessageHeader *
688 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 "%p Received historic fragment for message #%" PRIu64 ".\n",
692 plc, GNUNET_ntohll (pmsg->message_id));
694 GNUNET_ResultCallback result_cb = NULL;
695 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
697 if (GNUNET_YES != GNUNET_OP_get (plc->op,
698 GNUNET_ntohll (res->op_id),
699 &result_cb, (void *) &hist, NULL))
700 { /* Operation not found. */
701 LOG (GNUNET_ERROR_TYPE_WARNING,
702 "%p Replay operation not found for historic fragment of message #%"
704 plc, GNUNET_ntohll (pmsg->message_id));
708 GNUNET_PSYC_slicer_message (hist->slicer,
709 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
714 check_place_state_result (void *cls,
715 const struct GNUNET_OperationResultMessage *res)
717 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
721 LOG (GNUNET_ERROR_TYPE_WARNING,
722 "Invalid modifier in state result\n");
723 return GNUNET_SYSERR;
726 uint16_t size = ntohs (res->header.size);
727 uint16_t mod_size = ntohs (mod->size);
728 if (size - sizeof (*res) != mod_size)
731 LOG (GNUNET_ERROR_TYPE_WARNING,
732 "Invalid modifier size in state result: %u - %u != %u\n",
733 ntohs (res->header.size), sizeof (*res), mod_size);
734 return GNUNET_SYSERR;
741 handle_place_state_result (void *cls,
742 const struct GNUNET_OperationResultMessage *res)
744 struct GNUNET_SOCIAL_Place *plc = cls;
746 GNUNET_ResultCallback result_cb = NULL;
747 struct GNUNET_SOCIAL_LookHandle *look = NULL;
749 if (GNUNET_YES != GNUNET_OP_get (plc->op,
750 GNUNET_ntohll (res->op_id),
751 &result_cb, (void *) &look, NULL))
752 { /* Operation not found. */
756 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
757 uint16_t mod_size = ntohs (mod->size);
759 switch (ntohs (mod->type))
761 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
763 const struct GNUNET_PSYC_MessageModifier *
764 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
766 const char *name = (const char *) &pmod[1];
767 uint16_t name_size = ntohs (pmod->name_size);
769 || mod_size - sizeof (*pmod) < name_size
770 || '\0' != name[name_size - 1])
773 LOG (GNUNET_ERROR_TYPE_WARNING,
774 "Invalid modifier name in state result\n");
777 look->mod_value_size = ntohs (pmod->value_size);
778 look->var_cb (look->cls, mod, name, name + name_size,
779 mod_size - sizeof (*mod) - name_size,
780 look->mod_value_size);
781 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
783 look->mod_value_remaining = look->mod_value_size;
784 look->mod_name = GNUNET_malloc (name_size);
785 GNUNET_memcpy (look->mod_name, name, name_size);
790 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
791 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
792 mod_size - sizeof (*mod), look->mod_value_size);
793 look->mod_value_remaining -= mod_size - sizeof (*mod);
794 if (0 == look->mod_value_remaining)
796 GNUNET_free (look->mod_name);
804 handle_place_message_ack (void *cls,
805 const struct GNUNET_MessageHeader *msg)
807 struct GNUNET_SOCIAL_Place *plc = cls;
809 GNUNET_PSYC_transmit_got_ack (plc->tmit);
814 check_place_message (void *cls,
815 const struct GNUNET_PSYC_MessageHeader *pmsg)
822 handle_place_message (void *cls,
823 const struct GNUNET_PSYC_MessageHeader *pmsg)
825 struct GNUNET_SOCIAL_Place *plc = cls;
827 GNUNET_PSYC_slicer_message (plc->slicer, pmsg);
832 check_host_message (void *cls,
833 const struct GNUNET_PSYC_MessageHeader *pmsg)
840 handle_host_message (void *cls,
841 const struct GNUNET_PSYC_MessageHeader *pmsg)
843 struct GNUNET_SOCIAL_Host *hst = cls;
845 GNUNET_PSYC_slicer_message (hst->slicer, pmsg);
846 GNUNET_PSYC_slicer_message (hst->plc.slicer, pmsg);
851 handle_host_enter_ack (void *cls,
852 const struct HostEnterAck *hack)
854 struct GNUNET_SOCIAL_Host *hst = cls;
856 hst->plc.pub_key = hack->place_pub_key;
858 int32_t result = ntohl (hack->result_code);
859 if (NULL != hst->enter_cb)
860 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
861 GNUNET_ntohll (hack->max_message_id));
866 check_host_enter_request (void *cls,
867 const struct GNUNET_PSYC_JoinRequestMessage *req)
874 handle_host_enter_request (void *cls,
875 const struct GNUNET_PSYC_JoinRequestMessage *req)
877 struct GNUNET_SOCIAL_Host *hst = cls;
879 if (NULL == hst->answer_door_cb)
882 const char *method_name = NULL;
883 struct GNUNET_PSYC_Environment *env = NULL;
884 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
885 const void *data = NULL;
886 uint16_t data_size = 0;
888 const struct GNUNET_PSYC_Message *join_msg = NULL;
892 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
894 join_msg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (req);
895 LOG (GNUNET_ERROR_TYPE_DEBUG,
896 "Received join_msg of type %u and size %u.\n",
897 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
899 env = GNUNET_PSYC_env_create ();
900 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
901 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
905 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
906 LOG (GNUNET_ERROR_TYPE_WARNING,
907 "Ignoring invalid entry request from nym %s.\n",
914 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
915 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
920 GNUNET_PSYC_env_destroy (env);
921 if (NULL != entry_pmsg)
922 GNUNET_free (entry_pmsg);
927 handle_guest_enter_ack (void *cls,
928 const struct GNUNET_PSYC_CountersResultMessage *cres)
930 struct GNUNET_SOCIAL_Guest *gst = cls;
932 int32_t result = ntohl (cres->result_code);
933 if (NULL != gst->enter_cb)
934 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
935 GNUNET_ntohll (cres->max_message_id));
940 check_guest_enter_decision (void *cls,
941 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
948 handle_guest_enter_decision (void *cls,
949 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
951 struct GNUNET_SOCIAL_Guest *gst = cls;
953 struct GNUNET_PSYC_Message *pmsg = NULL;
954 if (ntohs (dcsn->header.size) > sizeof (*dcsn))
955 pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
957 if (NULL != gst->entry_dcsn_cb)
958 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
963 check_app_ego (void *cls,
964 const struct AppEgoMessage *emsg)
971 handle_app_ego (void *cls,
972 const struct AppEgoMessage *emsg)
974 struct GNUNET_SOCIAL_App *app = cls;
976 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
978 struct GNUNET_HashCode ego_pub_hash;
979 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
982 struct GNUNET_SOCIAL_Ego *
983 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
986 ego = GNUNET_malloc (sizeof (*ego));
987 ego->pub_key = emsg->ego_pub_key;
988 ego->name = GNUNET_malloc (name_size);
989 GNUNET_memcpy (ego->name, &emsg[1], name_size);
993 ego->name = GNUNET_realloc (ego->name, name_size);
994 GNUNET_memcpy (ego->name, &emsg[1], name_size);
997 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
998 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1000 if (NULL != app->ego_cb)
1001 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
1006 handle_app_ego_end (void *cls,
1007 const struct GNUNET_MessageHeader *msg)
1009 //struct GNUNET_SOCIAL_App *app = cls;
1014 check_app_place (void *cls,
1015 const struct AppPlaceMessage *pmsg)
1022 handle_app_place (void *cls,
1023 const struct AppPlaceMessage *pmsg)
1025 struct GNUNET_SOCIAL_App *app = cls;
1027 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
1028 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
1031 struct GNUNET_HashCode ego_pub_hash;
1032 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
1034 struct GNUNET_SOCIAL_Ego *
1035 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
1038 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failure to obtain ego %s.\n",
1039 GNUNET_h2s (&ego_pub_hash));
1044 if (GNUNET_YES == pmsg->is_host)
1046 if (NULL != app->host_cb) {
1047 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
1049 hconn->plc_msg = *pmsg;
1050 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1051 GNUNET_free (hconn);
1054 else if (NULL != app->guest_cb)
1056 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
1058 gconn->plc_msg = *pmsg;
1059 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1060 GNUNET_free (gconn);
1066 handle_app_place_end (void *cls,
1067 const struct GNUNET_MessageHeader *msg)
1069 struct GNUNET_SOCIAL_App *app = cls;
1071 if (NULL != app->connected_cb)
1072 app->connected_cb (app->cb_cls);
1077 * Handler for a #GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK message received
1078 * from the social service.
1080 * @param cls the place of type `struct GNUNET_SOCIAL_Place`
1081 * @param msg the message received from the service
1084 handle_place_leave_ack (void *cls,
1085 const struct GNUNET_MessageHeader *msg)
1087 struct GNUNET_SOCIAL_Place *plc = cls;
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "%s left place %p\n",
1091 plc->is_host ? "host" : "guest",
1093 place_disconnect (plc);
1101 host_connect (struct GNUNET_SOCIAL_Host *hst);
1105 host_reconnect (void *cls)
1112 * Host client disconnected from service.
1114 * Reconnect after backoff period.
1117 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1119 struct GNUNET_SOCIAL_Host *hst = cls;
1120 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "Host client disconnected (%d), re-connecting\n",
1125 if (NULL != plc->tmit)
1127 GNUNET_PSYC_transmit_destroy (plc->tmit);
1130 if (NULL != plc->mq)
1132 GNUNET_MQ_destroy (plc->mq);
1136 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1139 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1144 host_connect (struct GNUNET_SOCIAL_Host *hst)
1146 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1148 struct GNUNET_MQ_MessageHandler handlers[] = {
1149 GNUNET_MQ_hd_fixed_size (host_enter_ack,
1150 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1151 struct HostEnterAck,
1153 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1154 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1155 struct GNUNET_MessageHeader,
1157 GNUNET_MQ_hd_var_size (host_enter_request,
1158 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1159 struct GNUNET_PSYC_JoinRequestMessage,
1161 GNUNET_MQ_hd_var_size (host_message,
1162 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1163 struct GNUNET_PSYC_MessageHeader,
1165 GNUNET_MQ_hd_fixed_size (place_message_ack,
1166 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1167 struct GNUNET_MessageHeader,
1169 GNUNET_MQ_hd_var_size (place_history_result,
1170 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1171 struct GNUNET_OperationResultMessage,
1173 GNUNET_MQ_hd_var_size (place_state_result,
1174 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1175 struct GNUNET_OperationResultMessage,
1177 GNUNET_MQ_hd_var_size (place_result,
1178 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1179 struct GNUNET_OperationResultMessage,
1181 GNUNET_MQ_handler_end ()
1184 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1185 handlers, host_disconnected, hst);
1186 GNUNET_assert (NULL != plc->mq);
1187 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1189 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1194 * Enter a place as host.
1196 * A place is created upon first entering, and it is active until permanently
1197 * left using GNUNET_SOCIAL_host_leave().
1200 * Application handle.
1202 * Identity of the host.
1204 * Policy specifying entry and history restrictions for the place.
1206 * Slicer to handle incoming messages.
1208 * Function called when the place is entered and ready to use.
1209 * @param answer_door_cb
1210 * Function to handle new nyms that want to enter.
1211 * @param farewell_cb
1212 * Function to handle departing nyms.
1214 * Closure for the callbacks.
1216 * @return Handle for the host. This handle contains the pubkey.
1218 struct GNUNET_SOCIAL_Host *
1219 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1220 const struct GNUNET_SOCIAL_Ego *ego,
1221 enum GNUNET_PSYC_Policy policy,
1222 struct GNUNET_PSYC_Slicer *slicer,
1223 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1224 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1225 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1228 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1229 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1231 plc->cfg = app->cfg;
1232 plc->is_host = GNUNET_YES;
1233 plc->slicer = slicer;
1235 hst->enter_cb = enter_cb;
1236 hst->answer_door_cb = answer_door_cb;
1237 hst->farewell_cb = farewell_cb;
1240 plc->op = GNUNET_OP_create ();
1242 hst->slicer = GNUNET_PSYC_slicer_create ();
1243 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1244 host_recv_notice_place_leave_method,
1245 host_recv_notice_place_leave_modifier,
1246 NULL, host_recv_notice_place_leave_eom, hst);
1248 uint16_t app_id_size = strlen (app->id) + 1;
1249 struct HostEnterRequest *hreq;
1250 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1251 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1252 hreq->policy = policy;
1253 hreq->ego_pub_key = ego->pub_key;
1254 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1262 * Reconnect to an already entered place as host.
1265 * Host connection handle.
1266 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1268 * Slicer to handle incoming messages.
1270 * Function called when the place is entered and ready to use.
1271 * @param answer_door_cb
1272 * Function to handle new nyms that want to enter.
1273 * @param farewell_cb
1274 * Function to handle departing nyms.
1276 * Closure for the callbacks.
1278 * @return Handle for the host.
1280 struct GNUNET_SOCIAL_Host *
1281 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1282 struct GNUNET_PSYC_Slicer *slicer,
1283 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1284 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1285 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1288 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1289 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1291 hst->enter_cb = enter_cb;
1292 hst->answer_door_cb = answer_door_cb;
1293 hst->farewell_cb = farewell_cb;
1296 plc->cfg = hconn->app->cfg;
1297 plc->is_host = GNUNET_YES;
1298 plc->slicer = slicer;
1299 plc->pub_key = hconn->plc_msg.place_pub_key;
1300 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1302 plc->op = GNUNET_OP_create ();
1304 hst->slicer = GNUNET_PSYC_slicer_create ();
1305 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1306 host_recv_notice_place_leave_method,
1307 host_recv_notice_place_leave_modifier,
1308 NULL, host_recv_notice_place_leave_eom, hst);
1310 size_t app_id_size = strlen (hconn->app->id) + 1;
1311 struct HostEnterRequest *hreq;
1312 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1313 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1314 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1315 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1316 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1324 * Decision whether to admit @a nym into the place or refuse entry.
1327 * Host of the place.
1329 * Handle for the entity that wanted to enter.
1330 * @param is_admitted
1331 * #GNUNET_YES if @a nym is admitted,
1332 * #GNUNET_NO if @a nym is refused entry,
1333 * #GNUNET_SYSERR if we cannot answer the request.
1334 * @param method_name
1335 * Method name for the rejection message.
1337 * Environment containing variables for the message, or NULL.
1339 * Data for the rejection message to send back.
1341 * Number of bytes in @a data for method.
1342 * @return #GNUNET_OK on success,
1343 * #GNUNET_SYSERR if the message is too large.
1346 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1347 struct GNUNET_SOCIAL_Nym *nym,
1349 const struct GNUNET_PSYC_Message *entry_resp)
1351 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1352 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1353 uint16_t entry_resp_size
1354 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1356 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1357 return GNUNET_SYSERR;
1359 struct GNUNET_MQ_Envelope *
1360 env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1361 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1362 dcsn->is_admitted = htonl (is_admitted);
1363 dcsn->slave_pub_key = nym->pub_key;
1365 if (0 < entry_resp_size)
1366 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1368 GNUNET_MQ_send (plc->mq, env);
1374 * Throw @a nym out of the place.
1376 * The @a nym reference will remain valid until the
1377 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1378 * which should be very soon after this call.
1381 * Host of the place.
1383 * Handle for the entity to be ejected.
1385 * Environment for the message or NULL.
1388 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1389 const struct GNUNET_SOCIAL_Nym *nym,
1390 struct GNUNET_PSYC_Environment *e)
1392 struct GNUNET_PSYC_Environment *env = e;
1394 env = GNUNET_PSYC_env_create ();
1395 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1396 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1397 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1398 GNUNET_SOCIAL_ANNOUNCE_NONE);
1400 GNUNET_PSYC_env_destroy (env);
1405 * Get the public key of @a ego.
1410 * @return Public key of ego.
1412 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1413 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1415 return &ego->pub_key;
1420 * Get the hash of the public key of @a ego.
1425 * @return Hash of the public key of @a ego.
1427 const struct GNUNET_HashCode *
1428 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1430 return &ego->pub_key_hash;
1435 * Get the name of @a ego.
1440 * @return Public key of @a ego.
1443 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1450 * Get the public key of @a nym.
1452 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1457 * @return Public key of @a nym.
1459 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1460 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1462 return &nym->pub_key;
1467 * Get the hash of the public key of @a nym.
1472 * @return Hash of the public key of @a nym.
1474 const struct GNUNET_HashCode *
1475 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1477 return &nym->pub_key_hash;
1482 * Send a message to all nyms that are present in the place.
1484 * This function is restricted to the host. Nyms can only send requests
1485 * to the host who can decide to relay it to everyone in the place.
1487 * @param host Host of the place.
1488 * @param method_name Method to use for the announcement.
1489 * @param env Environment containing variables for the message and operations
1490 * on objects of the place. Can be NULL.
1491 * @param notify Function to call to get the payload of the announcement.
1492 * @param notify_cls Closure for @a notify.
1493 * @param flags Flags for this announcement.
1495 * @return NULL on error (announcement already in progress?).
1497 struct GNUNET_SOCIAL_Announcement *
1498 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1499 const char *method_name,
1500 const struct GNUNET_PSYC_Environment *env,
1501 GNUNET_PSYC_TransmitNotifyData notify_data,
1502 void *notify_data_cls,
1503 enum GNUNET_SOCIAL_AnnounceFlags flags)
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 "PSYC_transmit_message for host, method: %s\n",
1509 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1510 NULL, notify_data, notify_data_cls, flags))
1511 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1518 * Resume transmitting announcement.
1521 * The announcement to resume.
1524 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1526 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1531 * Cancel announcement.
1534 * The announcement to cancel.
1537 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1539 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1544 * Obtain handle for a hosted place.
1546 * The returned handle can be used to access the place API.
1548 * @param host Handle for the host.
1550 * @return Handle for the hosted place, valid as long as @a host is valid.
1552 struct GNUNET_SOCIAL_Place *
1553 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1560 * Disconnect from a home.
1562 * Invalidates host handle.
1565 * The host to disconnect.
1568 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1569 GNUNET_ContinuationCallback disconnect_cb,
1572 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1574 plc->disconnect_cb = disconnect_cb;
1575 plc->disconnect_cls = cls;
1576 place_disconnect (plc);
1581 * Stop hosting the home.
1583 * Sends a _notice_place_closing announcement to the home.
1584 * Invalidates host handle.
1589 * Environment for the message or NULL.
1590 * _nym is set to @e nym regardless whether an @e env is provided.
1591 * @param disconnect_cb
1592 * Function called after the host left the place
1593 * and disconnected from the social service.
1595 * Closure for @a disconnect_cb.
1598 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1599 const struct GNUNET_PSYC_Environment *env,
1600 GNUNET_ContinuationCallback disconnect_cb,
1603 struct GNUNET_MQ_Envelope *envelope;
1605 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1606 GNUNET_SOCIAL_ANNOUNCE_NONE);
1607 hst->plc.disconnect_cb = disconnect_cb;
1608 hst->plc.disconnect_cls = cls;
1609 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1610 GNUNET_MQ_send (hst->plc.mq,
1619 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1623 guest_reconnect (void *cls)
1625 guest_connect (cls);
1630 * Guest client disconnected from service.
1632 * Reconnect after backoff period.
1635 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1637 struct GNUNET_SOCIAL_Guest *gst = cls;
1638 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1640 LOG (GNUNET_ERROR_TYPE_DEBUG,
1641 "Guest client disconnected (%d), re-connecting\n",
1643 if (NULL != plc->tmit)
1645 GNUNET_PSYC_transmit_destroy (plc->tmit);
1648 if (NULL != plc->mq)
1650 GNUNET_MQ_destroy (plc->mq);
1654 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1657 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1662 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1664 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1666 struct GNUNET_MQ_MessageHandler handlers[] = {
1667 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1668 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1669 struct GNUNET_PSYC_CountersResultMessage,
1671 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1672 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1673 struct GNUNET_MessageHeader,
1675 GNUNET_MQ_hd_var_size (guest_enter_decision,
1676 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1677 struct GNUNET_PSYC_JoinDecisionMessage,
1679 GNUNET_MQ_hd_var_size (place_message,
1680 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1681 struct GNUNET_PSYC_MessageHeader,
1683 GNUNET_MQ_hd_fixed_size (place_message_ack,
1684 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1685 struct GNUNET_MessageHeader,
1687 GNUNET_MQ_hd_var_size (place_history_result,
1688 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1689 struct GNUNET_OperationResultMessage,
1691 GNUNET_MQ_hd_var_size (place_state_result,
1692 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1693 struct GNUNET_OperationResultMessage,
1695 GNUNET_MQ_hd_var_size (place_result,
1696 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1697 struct GNUNET_OperationResultMessage,
1699 GNUNET_MQ_handler_end ()
1702 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1703 handlers, guest_disconnected, gst);
1704 GNUNET_assert (NULL != plc->mq);
1705 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1707 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1711 static struct GNUNET_MQ_Envelope *
1712 guest_enter_request_create (const char *app_id,
1713 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1714 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1715 const struct GNUNET_PeerIdentity *origin,
1717 const struct GNUNET_PeerIdentity *relays,
1718 const struct GNUNET_PSYC_Message *join_msg)
1720 uint16_t app_id_size = strlen (app_id) + 1;
1721 uint16_t join_msg_size = ntohs (join_msg->header.size);
1722 uint16_t relay_size = relay_count * sizeof (*relays);
1724 struct GuestEnterRequest *greq;
1725 struct GNUNET_MQ_Envelope *
1726 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1727 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1728 greq->place_pub_key = *place_pub_key;
1729 greq->ego_pub_key = *ego_pub_key;
1730 greq->origin = *origin;
1731 greq->relay_count = htonl (relay_count);
1733 char *p = (char *) &greq[1];
1734 GNUNET_memcpy (p, app_id, app_id_size);
1739 GNUNET_memcpy (p, relays, relay_size);
1743 GNUNET_memcpy (p, join_msg, join_msg_size);
1749 * Request entry to a place as a guest.
1752 * Application handle.
1754 * Identity of the guest.
1755 * @param place_pub_key
1756 * Public key of the place to enter.
1758 * Flags for the entry.
1760 * Peer identity of the origin of the underlying multicast group.
1761 * @param relay_count
1762 * Number of elements in the @a relays array.
1764 * Relays for the underlying multicast group.
1765 * @param method_name
1766 * Method name for the message.
1768 * Environment containing variables for the message, or NULL.
1770 * Payload for the message to give to the enter callback.
1772 * Number of bytes in @a data.
1774 * Slicer to use for processing incoming requests from guests.
1776 * @return NULL on errors, otherwise handle for the guest.
1778 struct GNUNET_SOCIAL_Guest *
1779 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1780 const struct GNUNET_SOCIAL_Ego *ego,
1781 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1782 enum GNUNET_PSYC_SlaveJoinFlags flags,
1783 const struct GNUNET_PeerIdentity *origin,
1784 uint32_t relay_count,
1785 const struct GNUNET_PeerIdentity *relays,
1786 const struct GNUNET_PSYC_Message *entry_msg,
1787 struct GNUNET_PSYC_Slicer *slicer,
1788 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1789 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1792 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1793 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1795 plc->ego_pub_key = ego->pub_key;
1796 plc->pub_key = *place_pub_key;
1797 plc->cfg = app->cfg;
1798 plc->is_host = GNUNET_NO;
1799 plc->slicer = slicer;
1801 plc->op = GNUNET_OP_create ();
1804 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1805 origin, relay_count, relays, entry_msg);
1807 gst->enter_cb = local_enter_cb;
1808 gst->entry_dcsn_cb = entry_dcsn_cb;
1811 guest_connect (gst);
1817 * Request entry to a place by name as a guest.
1820 * Application handle.
1822 * Identity of the guest.
1824 * GNS name of the place to enter. Either in the form of
1825 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1826 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1827 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1828 * pseudonym's place directly.
1830 * Password to decrypt the record, or NULL for cleartext records.
1832 * Entry request message or NULL.
1834 * Slicer to use for processing incoming requests from guests.
1835 * @param local_enter_cb
1836 * Called upon connection established to the social service.
1837 * @param entry_decision_cb
1838 * Called upon receiving entry decision.
1840 * @return NULL on errors, otherwise handle for the guest.
1842 struct GNUNET_SOCIAL_Guest *
1843 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1844 const struct GNUNET_SOCIAL_Ego *ego,
1845 const char *gns_name,
1846 const char *password,
1847 const struct GNUNET_PSYC_Message *join_msg,
1848 struct GNUNET_PSYC_Slicer *slicer,
1849 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1850 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1853 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1854 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1856 if (NULL == password)
1859 uint16_t app_id_size = strlen (app->id) + 1;
1860 uint16_t gns_name_size = strlen (gns_name) + 1;
1861 uint16_t password_size = strlen (password) + 1;
1863 uint16_t join_msg_size = 0;
1864 if (NULL != join_msg)
1865 join_msg_size = ntohs (join_msg->header.size);
1867 struct GuestEnterByNameRequest *greq;
1869 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1870 + password_size + join_msg_size,
1871 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1873 greq->ego_pub_key = ego->pub_key;
1875 char *p = (char *) &greq[1];
1876 GNUNET_memcpy (p, app->id, app_id_size);
1878 GNUNET_memcpy (p, gns_name, gns_name_size);
1880 GNUNET_memcpy (p, password, password_size);
1882 if (NULL != join_msg)
1883 GNUNET_memcpy (p, join_msg, join_msg_size);
1885 plc->ego_pub_key = ego->pub_key;
1886 plc->cfg = app->cfg;
1887 plc->is_host = GNUNET_NO;
1888 plc->slicer = slicer;
1890 plc->op = GNUNET_OP_create ();
1892 gst->enter_cb = local_enter_cb;
1893 gst->entry_dcsn_cb = entry_decision_cb;
1896 guest_connect (gst);
1901 struct ReconnectContext
1903 struct GNUNET_SOCIAL_Guest *guest;
1905 int64_t *max_message_id;
1906 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
1912 guest_enter_reconnect_cb (void *cls,
1914 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1915 uint64_t max_message_id)
1917 struct ReconnectContext *reconnect_ctx = cls;
1919 GNUNET_assert (NULL != reconnect_ctx);
1920 reconnect_ctx->result = GNUNET_new (int);
1921 *(reconnect_ctx->result) = result;
1922 reconnect_ctx->max_message_id = GNUNET_new (int64_t);
1923 *(reconnect_ctx->max_message_id) = max_message_id;
1928 guest_entry_dcsn_reconnect_cb (void *cls,
1930 const struct GNUNET_PSYC_Message *entry_resp)
1932 struct ReconnectContext *reconnect_ctx = cls;
1933 struct GNUNET_SOCIAL_Guest *gst = reconnect_ctx->guest;
1935 GNUNET_assert (NULL != reconnect_ctx);
1936 GNUNET_assert (NULL != reconnect_ctx->result);
1937 GNUNET_assert (NULL != reconnect_ctx->max_message_id);
1938 if (GNUNET_YES != is_admitted)
1940 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1941 "Guest was rejected after calling "
1942 "GNUNET_SOCIAL_guest_enter_reconnect ()\n");
1944 else if (NULL != reconnect_ctx->enter_cb)
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "guest reconnected!\n");
1948 reconnect_ctx->enter_cb (reconnect_ctx->enter_cls,
1949 *(reconnect_ctx->result),
1951 *(reconnect_ctx->max_message_id));
1953 GNUNET_free (reconnect_ctx->result);
1954 GNUNET_free (reconnect_ctx->max_message_id);
1955 GNUNET_free (reconnect_ctx);
1960 * Reconnect to an already entered place as guest.
1963 * Guest connection handle.
1964 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1966 * Flags for the entry.
1968 * Slicer to use for processing incoming requests from guests.
1970 * Called upon re-entering is complete.
1971 * @param entry_decision_cb
1972 * Called upon receiving entry decision.
1974 * @return NULL on errors, otherwise handle for the guest.
1976 struct GNUNET_SOCIAL_Guest *
1977 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1978 enum GNUNET_PSYC_SlaveJoinFlags flags,
1979 struct GNUNET_PSYC_Slicer *slicer,
1980 GNUNET_SOCIAL_GuestEnterCallback enter_cb,
1983 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1984 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1985 struct ReconnectContext *reconnect_ctx;
1987 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1988 struct GuestEnterRequest *greq;
1990 = GNUNET_MQ_msg_extra (greq, app_id_size,
1991 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1992 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1993 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1994 greq->flags = htonl (flags);
1996 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1998 plc->cfg = gconn->app->cfg;
1999 plc->is_host = GNUNET_NO;
2000 plc->slicer = slicer;
2001 plc->pub_key = gconn->plc_msg.place_pub_key;
2002 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
2004 reconnect_ctx = GNUNET_new (struct ReconnectContext);
2005 reconnect_ctx->guest = gst;
2006 reconnect_ctx->enter_cb = enter_cb;
2007 reconnect_ctx->enter_cls = cls;
2009 plc->op = GNUNET_OP_create ();
2010 gst->enter_cb = &guest_enter_reconnect_cb;
2011 gst->entry_dcsn_cb = &guest_entry_dcsn_reconnect_cb;
2012 gst->cb_cls = reconnect_ctx;
2014 guest_connect (gst);
2020 * Talk to the host of the place.
2023 * Place where we want to talk to the host.
2024 * @param method_name
2025 * Method to invoke on the host.
2027 * Environment containing variables for the message, or NULL.
2028 * @param notify_data
2029 * Function to use to get the payload for the method.
2030 * @param notify_data_cls
2031 * Closure for @a notify_data.
2033 * Flags for the message being sent.
2035 * @return NULL if we are already trying to talk to the host,
2036 * otherwise handle to cancel the request.
2038 struct GNUNET_SOCIAL_TalkRequest *
2039 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2040 const char *method_name,
2041 const struct GNUNET_PSYC_Environment *env,
2042 GNUNET_PSYC_TransmitNotifyData notify_data,
2043 void *notify_data_cls,
2044 enum GNUNET_SOCIAL_TalkFlags flags)
2046 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2047 GNUNET_assert (NULL != plc->tmit);
2050 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2051 NULL, notify_data, notify_data_cls, flags))
2052 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2059 * Resume talking to the host of the place.
2062 * Talk request to resume.
2065 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2067 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2072 * Cancel talking to the host of the place.
2075 * Talk request to cancel.
2078 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2080 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2085 * Disconnect from a place.
2087 * Invalidates guest handle.
2090 * The guest to disconnect.
2093 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2094 GNUNET_ContinuationCallback disconnect_cb,
2097 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2099 plc->disconnect_cb = disconnect_cb;
2100 plc->disconnect_cls = cls;
2101 place_disconnect (plc);
2106 * Leave a place temporarily or permanently.
2108 * Notifies the owner of the place about leaving, and destroys the place handle.
2112 * @param keep_active
2113 * Keep place active after last application disconnected.
2114 * #GNUNET_YES or #GNUNET_NO
2116 * Optional environment for the leave message if @a keep_active
2117 * is #GNUNET_NO. NULL if not needed.
2119 * Called upon disconnecting from the social service.
2122 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2123 struct GNUNET_PSYC_Environment *env,
2124 GNUNET_ContinuationCallback disconnect_cb,
2127 struct GNUNET_MQ_Envelope *envelope;
2129 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2130 GNUNET_SOCIAL_TALK_NONE);
2131 gst->plc.disconnect_cb = disconnect_cb;
2132 gst->plc.disconnect_cls = cls;
2133 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
2134 GNUNET_MQ_send (gst->plc.mq,
2140 * Obtain handle for a place entered as guest.
2142 * The returned handle can be used to access the place API.
2144 * @param guest Handle for the guest.
2146 * @return Handle for the place, valid as long as @a guest is valid.
2148 struct GNUNET_SOCIAL_Place *
2149 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2156 * Obtain the public key of a place.
2161 * @return Public key of the place.
2163 const struct GNUNET_CRYPTO_EddsaPublicKey *
2164 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2166 return &plc->pub_key;
2171 * Set message processing @a flags for a @a method_prefix.
2175 * @param method_prefix
2176 * Method prefix @a flags apply to.
2178 * The flags that apply to a matching @a method_prefix.
2181 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2182 const char *method_prefix,
2183 enum GNUNET_SOCIAL_MsgProcFlags flags)
2185 GNUNET_assert (NULL != method_prefix);
2186 struct MsgProcRequest *mpreq;
2187 uint16_t method_size = strnlen (method_prefix,
2188 GNUNET_MAX_MESSAGE_SIZE
2189 - sizeof (*mpreq)) + 1;
2190 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2192 struct GNUNET_MQ_Envelope *
2193 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2194 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2195 mpreq->flags = htonl (flags);
2196 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2198 GNUNET_MQ_send (plc->mq, env);
2203 * Clear all message processing flags previously set for this place.
2206 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2208 struct GNUNET_MessageHeader *req;
2209 struct GNUNET_MQ_Envelope *
2210 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2212 GNUNET_MQ_send (plc->mq, env);
2216 static struct GNUNET_SOCIAL_HistoryRequest *
2217 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2218 uint64_t start_message_id,
2219 uint64_t end_message_id,
2220 uint64_t message_limit,
2221 const char *method_prefix,
2223 struct GNUNET_PSYC_Slicer *slicer,
2224 GNUNET_ResultCallback result_cb,
2227 struct GNUNET_PSYC_HistoryRequestMessage *req;
2228 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2230 hist->slicer = slicer;
2231 hist->result_cb = result_cb;
2233 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2235 GNUNET_assert (NULL != method_prefix);
2236 uint16_t method_size = strnlen (method_prefix,
2237 GNUNET_MAX_MESSAGE_SIZE
2238 - sizeof (*req)) + 1;
2239 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2241 struct GNUNET_MQ_Envelope *
2242 env = GNUNET_MQ_msg_extra (req, method_size,
2243 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2244 req->start_message_id = GNUNET_htonll (start_message_id);
2245 req->end_message_id = GNUNET_htonll (end_message_id);
2246 req->message_limit = GNUNET_htonll (message_limit);
2247 req->flags = htonl (flags);
2248 req->op_id = GNUNET_htonll (hist->op_id);
2249 GNUNET_memcpy (&req[1], method_prefix, method_size);
2251 GNUNET_MQ_send (plc->mq, env);
2257 * Learn about the history of a place.
2259 * Messages are returned through the @a slicer function
2260 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2263 * Place we want to learn more about.
2264 * @param start_message_id
2265 * First historic message we are interested in.
2266 * @param end_message_id
2267 * Last historic message we are interested in (inclusive).
2268 * @param method_prefix
2269 * Only retrieve messages with this method prefix.
2271 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2273 * Slicer to use for retrieved messages.
2274 * Can be the same as the slicer of the place.
2276 * Function called after all messages retrieved.
2277 * NULL if not needed.
2278 * @param cls Closure for @a result_cb.
2280 struct GNUNET_SOCIAL_HistoryRequest *
2281 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2282 uint64_t start_message_id,
2283 uint64_t end_message_id,
2284 const char *method_prefix,
2286 struct GNUNET_PSYC_Slicer *slicer,
2287 GNUNET_ResultCallback result_cb,
2290 return place_history_replay (plc, start_message_id, end_message_id, 0,
2291 method_prefix, flags, slicer, result_cb, cls);
2296 * Learn about the history of a place.
2298 * Sends messages through the slicer function of the place where
2299 * start_message_id <= message_id <= end_message_id.
2300 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2302 * To get the latest message, use 0 for both the start and end message ID.
2305 * Place we want to learn more about.
2306 * @param message_limit
2307 * Maximum number of historic messages we are interested in.
2308 * @param method_prefix
2309 * Only retrieve messages with this method prefix.
2311 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2313 * Function called after all messages retrieved.
2314 * NULL if not needed.
2315 * @param cls Closure for @a result_cb.
2317 struct GNUNET_SOCIAL_HistoryRequest *
2318 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2319 uint64_t message_limit,
2320 const char *method_prefix,
2322 struct GNUNET_PSYC_Slicer *slicer,
2323 GNUNET_ResultCallback result_cb,
2326 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2327 slicer, result_cb, cls);
2332 * Cancel learning about the history of a place.
2335 * History lesson to cancel.
2338 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2340 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2346 * Request matching state variables.
2348 static struct GNUNET_SOCIAL_LookHandle *
2349 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2350 uint16_t type, const char *name,
2351 GNUNET_PSYC_StateVarCallback var_cb,
2352 GNUNET_ResultCallback result_cb, void *cls)
2354 struct GNUNET_PSYC_StateRequestMessage *req;
2355 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2357 look->var_cb = var_cb;
2358 look->result_cb = result_cb;
2360 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2362 GNUNET_assert (NULL != name);
2363 size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
2364 - sizeof (*req)) + 1;
2365 struct GNUNET_MQ_Envelope *
2366 env = GNUNET_MQ_msg_extra (req, name_size, type);
2367 req->op_id = GNUNET_htonll (look->op_id);
2368 GNUNET_memcpy (&req[1], name, name_size);
2370 GNUNET_MQ_send (plc->mq, env);
2376 * Look at a particular object in the place.
2378 * The best matching object is returned (its name might be less specific than
2379 * what was requested).
2382 * The place where to look.
2384 * Full name of the object.
2386 * Set to the size of the returned value.
2388 * @return NULL if there is no such object at this place.
2390 struct GNUNET_SOCIAL_LookHandle *
2391 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2392 const char *full_name,
2393 GNUNET_PSYC_StateVarCallback var_cb,
2394 GNUNET_ResultCallback result_cb,
2397 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2398 full_name, var_cb, result_cb, cls);
2403 * Look for objects in the place with a matching name prefix.
2406 * The place where to look.
2407 * @param name_prefix
2408 * Look at objects with names beginning with this value.
2410 * Function to call for each object found.
2412 * Closure for callback function.
2414 * @return Handle that can be used to stop looking at objects.
2416 struct GNUNET_SOCIAL_LookHandle *
2417 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2418 const char *name_prefix,
2419 GNUNET_PSYC_StateVarCallback var_cb,
2420 GNUNET_ResultCallback result_cb,
2423 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2424 name_prefix, var_cb, result_cb, cls);
2429 * Cancel a state request operation.
2432 * Handle for the operation to cancel.
2435 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2437 GNUNET_OP_remove (look->plc->op, look->op_id);
2443 op_recv_zone_add_place_result (void *cls, int64_t result,
2444 const void *err_msg, uint16_t err_msg_size)
2446 LOG (GNUNET_ERROR_TYPE_DEBUG,
2447 "Received zone add place result: %" PRId64 ".\n", result);
2449 struct ZoneAddPlaceHandle *add_plc = cls;
2450 if (NULL != add_plc->result_cb)
2451 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2453 GNUNET_free (add_plc);
2458 * Advertise @e place in the GNS zone of @e ego.
2461 * Application handle.
2464 * @param place_pub_key
2465 * Public key of place to add.
2467 * The name for the PLACE record to put in the zone.
2469 * Password used to encrypt the record or NULL to keep it cleartext.
2470 * @param relay_count
2471 * Number of elements in the @a relays array.
2473 * List of relays to put in the PLACE record to advertise
2474 * as entry points to the place in addition to the origin.
2475 * @param expiration_time
2476 * Expiration time of the record, use 0 to remove the record.
2478 * Function called with the result of the operation.
2480 * Closure for @a result_cb
2482 * @return #GNUNET_OK if the request was sent,
2483 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2486 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2487 const struct GNUNET_SOCIAL_Ego *ego,
2489 const char *password,
2490 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2491 const struct GNUNET_PeerIdentity *origin,
2492 uint32_t relay_count,
2493 const struct GNUNET_PeerIdentity *relays,
2494 struct GNUNET_TIME_Absolute expiration_time,
2495 GNUNET_ResultCallback result_cb,
2498 struct ZoneAddPlaceRequest *preq;
2499 size_t name_size = strlen (name) + 1;
2500 size_t password_size = strlen (password) + 1;
2501 size_t relay_size = relay_count * sizeof (*relays);
2502 size_t payload_size = name_size + password_size + relay_size;
2504 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2505 return GNUNET_SYSERR;
2507 struct GNUNET_MQ_Envelope *
2508 env = GNUNET_MQ_msg_extra (preq, payload_size,
2509 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2510 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2511 preq->ego_pub_key = ego->pub_key;
2512 preq->place_pub_key = *place_pub_key;
2513 preq->origin = *origin;
2514 preq->relay_count = htonl (relay_count);
2516 char *p = (char *) &preq[1];
2517 GNUNET_memcpy (p, name, name_size);
2519 GNUNET_memcpy (p, password, password_size);
2521 GNUNET_memcpy (p, relays, relay_size);
2523 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2524 add_plc->result_cb = result_cb;
2525 add_plc->result_cls = result_cls;
2527 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2528 op_recv_zone_add_place_result,
2531 GNUNET_MQ_send (app->mq, env);
2537 op_recv_zone_add_nym_result (void *cls, int64_t result,
2538 const void *err_msg, uint16_t err_msg_size)
2540 LOG (GNUNET_ERROR_TYPE_DEBUG,
2541 "Received zone add nym result: %" PRId64 ".\n", result);
2543 struct ZoneAddNymHandle *add_nym = cls;
2544 if (NULL != add_nym->result_cb)
2545 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2547 GNUNET_free (add_nym);
2552 * Add nym to the GNS zone of @e ego.
2559 * The name for the PKEY record to put in the zone.
2560 * @param nym_pub_key
2561 * Public key of nym to add.
2562 * @param expiration_time
2563 * Expiration time of the record, use 0 to remove the record.
2565 * Function called with the result of the operation.
2567 * Closure for @a result_cb
2569 * @return #GNUNET_OK if the request was sent,
2570 * #GNUNET_SYSERR on error, e.g. the name is too long.
2573 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2574 const struct GNUNET_SOCIAL_Ego *ego,
2576 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2577 struct GNUNET_TIME_Absolute expiration_time,
2578 GNUNET_ResultCallback result_cb,
2581 struct ZoneAddNymRequest *nreq;
2583 size_t name_size = strlen (name) + 1;
2584 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2585 return GNUNET_SYSERR;
2587 struct GNUNET_MQ_Envelope *
2588 env = GNUNET_MQ_msg_extra (nreq, name_size,
2589 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2590 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2591 nreq->ego_pub_key = ego->pub_key;
2592 nreq->nym_pub_key = *nym_pub_key;
2593 GNUNET_memcpy (&nreq[1], name, name_size);
2595 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2596 add_nym->result_cb = result_cb;
2597 add_nym->result_cls = result_cls;
2599 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2600 op_recv_zone_add_nym_result,
2603 GNUNET_MQ_send (app->mq, env);
2612 app_connect (struct GNUNET_SOCIAL_App *app);
2616 app_reconnect (void *cls)
2623 * App client disconnected from service.
2625 * Reconnect after backoff period.
2628 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2630 struct GNUNET_SOCIAL_App *app = cls;
2632 LOG (GNUNET_ERROR_TYPE_DEBUG,
2633 "App client disconnected (%d), re-connecting\n",
2635 if (NULL != app->mq)
2637 GNUNET_MQ_destroy (app->mq);
2641 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2644 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2649 app_connect (struct GNUNET_SOCIAL_App *app)
2651 struct GNUNET_MQ_MessageHandler handlers[] = {
2652 GNUNET_MQ_hd_var_size (app_ego,
2653 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2654 struct AppEgoMessage,
2656 GNUNET_MQ_hd_fixed_size (app_ego_end,
2657 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2658 struct GNUNET_MessageHeader,
2660 GNUNET_MQ_hd_var_size (app_place,
2661 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2662 struct AppPlaceMessage,
2664 GNUNET_MQ_hd_fixed_size (app_place_end,
2665 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2666 struct GNUNET_MessageHeader,
2668 GNUNET_MQ_hd_var_size (app_result,
2669 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2670 struct GNUNET_OperationResultMessage,
2672 GNUNET_MQ_handler_end ()
2675 app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
2676 handlers, app_disconnected, app);
2677 GNUNET_assert (NULL != app->mq);
2678 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2683 * Connect application to the social service.
2685 * The @host_place_cb and @guest_place_cb functions are
2686 * initially called for each entered places,
2687 * then later each time a new place is entered with the current application ID.
2694 * Function to notify about an available ego.
2696 * Function to notify about a place entered as host.
2698 * Function to notify about a place entered as guest.
2700 * Closure for the callbacks.
2702 * @return Handle that can be used to stop listening.
2704 struct GNUNET_SOCIAL_App *
2705 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2707 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2708 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2709 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2710 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2713 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2714 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2718 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2720 app->ego_cb = ego_cb;
2721 app->host_cb = host_cb;
2722 app->guest_cb = guest_cb;
2723 app->connected_cb = connected_cb;
2725 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2726 app->op = GNUNET_OP_create ();
2727 app->id = GNUNET_malloc (app_id_size);
2728 GNUNET_memcpy (app->id, id, app_id_size);
2730 struct AppConnectRequest *creq;
2731 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2732 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2733 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2741 app_cleanup (struct GNUNET_SOCIAL_App *app)
2743 if (NULL != app->mq)
2745 GNUNET_MQ_destroy (app->mq);
2748 if (NULL != app->disconnect_cb)
2750 app->disconnect_cb (app->disconnect_cls);
2751 app->disconnect_cb = NULL;
2757 * Disconnect application.
2760 * Application handle.
2761 * @param disconnect_cb
2762 * Disconnect callback.
2763 * @param disconnect_cls
2764 * Disconnect closure.
2767 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2768 GNUNET_ContinuationCallback disconnect_cb,
2769 void *disconnect_cls)
2771 if (NULL == app) return;
2773 app->disconnect_cb = disconnect_cb;
2774 app->disconnect_cls = disconnect_cls;
2776 if (NULL != app->mq)
2778 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2781 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
2796 * Detach application from a place.
2798 * Removes the place from the entered places list for this application.
2799 * Note: this does not disconnect from the place.
2801 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2809 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2810 struct GNUNET_SOCIAL_Place *plc)
2812 struct AppDetachRequest *dreq;
2813 struct GNUNET_MQ_Envelope *
2814 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2815 dreq->place_pub_key = plc->pub_key;
2816 dreq->ego_pub_key = plc->ego_pub_key;
2818 GNUNET_MQ_send (app->mq, env);
2822 /* end of social_api.c */