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
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * option) any later version.
10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @author Gabor X Toth
25 * Social service; implements social interactions using the PSYC service.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_psyc_service.h"
34 #include "gnunet_psyc_util_lib.h"
35 #include "gnunet_social_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "social-api",__VA_ARGS__)
43 struct GNUNET_SOCIAL_Ego
45 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
46 struct GNUNET_HashCode pub_key_hash;
52 * Handle for a pseudonym of another user in the network.
54 struct GNUNET_SOCIAL_Nym
56 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
57 struct GNUNET_HashCode pub_key_hash;
62 * Handle for an application.
64 struct GNUNET_SOCIAL_App
67 * Configuration to use.
69 const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * Client connection to the service.
74 struct GNUNET_MQ_Handle *mq;
77 * Message to send on connect.
79 struct GNUNET_MQ_Envelope *connect_env;
82 * Time to wait until we try to reconnect on failure.
84 struct GNUNET_TIME_Relative reconnect_delay;
87 * Task for reconnecting when the listener fails.
89 struct GNUNET_SCHEDULER_Task *reconnect_task;
94 struct GNUNET_OP_Handle *op;
97 * Function called after disconnected from the service.
99 GNUNET_ContinuationCallback disconnect_cb;
102 * Closure for @a disconnect_cb.
104 void *disconnect_cls;
112 * Hash map of all egos.
113 * pub_key_hash -> struct GNUNET_SOCIAL_Ego *
115 struct GNUNET_CONTAINER_MultiHashMap *egos;
117 GNUNET_SOCIAL_AppEgoCallback ego_cb;
118 GNUNET_SOCIAL_AppHostPlaceCallback host_cb;
119 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb;
120 GNUNET_SOCIAL_AppConnectedCallback connected_cb;
125 struct GNUNET_SOCIAL_HostConnection
127 struct GNUNET_SOCIAL_App *app;
129 struct AppPlaceMessage plc_msg;
133 struct GNUNET_SOCIAL_GuestConnection
135 struct GNUNET_SOCIAL_App *app;
137 struct AppPlaceMessage plc_msg;
142 * Handle for a place where social interactions happen.
144 struct GNUNET_SOCIAL_Place
147 * Configuration to use.
149 const struct GNUNET_CONFIGURATION_Handle *cfg;
152 * Client connection to the service.
154 struct GNUNET_MQ_Handle *mq;
157 * Message to send on connect.
159 struct GNUNET_MQ_Envelope *connect_env;
162 * Time to wait until we try to reconnect on failure.
164 struct GNUNET_TIME_Relative reconnect_delay;
167 * Task for reconnecting when the listener fails.
169 struct GNUNET_SCHEDULER_Task *reconnect_task;
174 struct GNUNET_OP_Handle *op;
177 * Transmission handle.
179 struct GNUNET_PSYC_TransmitHandle *tmit;
182 * Slicer for processing incoming messages.
184 struct GNUNET_PSYC_Slicer *slicer;
186 // FIXME: do we need is_disconnecing like on the psyc and multicast APIs?
188 * Function called after disconnected from the service.
190 GNUNET_ContinuationCallback disconnect_cb;
193 * Closure for @a disconnect_cb.
195 void *disconnect_cls;
198 * Public key of the place.
200 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
203 * Public key of the ego.
205 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
208 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
215 * Host handle for a place that we entered.
217 struct GNUNET_SOCIAL_Host
219 struct GNUNET_SOCIAL_Place plc;
222 * Slicer for processing incoming messages from guests.
224 struct GNUNET_PSYC_Slicer *slicer;
226 GNUNET_SOCIAL_HostEnterCallback enter_cb;
228 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
230 GNUNET_SOCIAL_FarewellCallback farewell_cb;
233 * Closure for callbacks.
237 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
238 struct GNUNET_PSYC_Environment *notice_place_leave_env;
243 * Guest handle for place that we entered.
245 struct GNUNET_SOCIAL_Guest
247 struct GNUNET_SOCIAL_Place plc;
249 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
251 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
254 * Closure for callbacks.
261 * Hash map of all nyms.
262 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
264 struct GNUNET_CONTAINER_MultiHashMap *nyms;
268 * Handle for an announcement request.
270 struct GNUNET_SOCIAL_Announcement
279 struct GNUNET_SOCIAL_TalkRequest
288 struct GNUNET_SOCIAL_HistoryRequest
293 struct GNUNET_SOCIAL_Place *plc;
301 * Slicer for processing incoming messages.
303 struct GNUNET_PSYC_Slicer *slicer;
306 * Function to call when the operation finished.
308 GNUNET_ResultCallback result_cb;
311 * Closure for @a result_cb.
317 struct GNUNET_SOCIAL_LookHandle
322 struct GNUNET_SOCIAL_Place *plc;
330 * State variable result callback.
332 GNUNET_PSYC_StateVarCallback var_cb;
335 * Function to call when the operation finished.
337 GNUNET_ResultCallback result_cb;
340 * Name of current modifier being received.
345 * Size of current modifier value being received.
347 size_t mod_value_size;
350 * Remaining size of current modifier value still to be received.
352 size_t mod_value_remaining;
355 * Closure for @a result_cb.
361 struct ZoneAddPlaceHandle
363 GNUNET_ResultCallback result_cb;
368 struct ZoneAddNymHandle
370 GNUNET_ResultCallback result_cb;
375 /*** CLEANUP / DISCONNECT ***/
379 host_cleanup (struct GNUNET_SOCIAL_Host *hst)
381 if (NULL != hst->slicer)
383 GNUNET_PSYC_slicer_destroy (hst->slicer);
391 guest_cleanup (struct GNUNET_SOCIAL_Guest *gst)
398 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
400 struct GNUNET_HashCode place_pub_hash;
402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
405 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
406 if (NULL != plc->tmit)
408 GNUNET_PSYC_transmit_destroy (plc->tmit);
411 if (NULL != plc->connect_env)
413 GNUNET_MQ_discard (plc->connect_env);
414 plc->connect_env = NULL;
418 GNUNET_MQ_destroy (plc->mq);
421 if (NULL != plc->disconnect_cb)
423 plc->disconnect_cb (plc->disconnect_cls);
424 plc->disconnect_cb = NULL;
427 (GNUNET_YES == plc->is_host)
428 ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
429 : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
434 place_disconnect (struct GNUNET_SOCIAL_Place *plc)
436 struct GNUNET_HashCode place_pub_hash;
438 GNUNET_CRYPTO_hash (&plc->pub_key,
439 sizeof (plc->pub_key),
447 static struct GNUNET_SOCIAL_Nym *
448 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
450 struct GNUNET_SOCIAL_Nym *nym = NULL;
451 struct GNUNET_HashCode pub_key_hash;
456 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
459 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
461 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
465 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
466 nym->pub_key = *pub_key;
467 nym->pub_key_hash = pub_key_hash;
468 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
469 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
476 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
478 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
483 /*** MESSAGE HANDLERS ***/
485 /** _notice_place_leave from guests */
488 host_recv_notice_place_leave_method (void *cls,
489 const struct GNUNET_PSYC_MessageHeader *msg,
490 const struct GNUNET_PSYC_MessageMethod *meth,
492 const char *method_name)
494 struct GNUNET_SOCIAL_Host *hst = cls;
496 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
497 &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
500 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
504 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
506 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
507 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
509 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "_notice_place_leave: got method from nym %s (%s).\n",
512 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
518 host_recv_notice_place_leave_modifier (void *cls,
519 const struct GNUNET_PSYC_MessageHeader *msg,
520 const struct GNUNET_MessageHeader *pmsg,
522 enum GNUNET_PSYC_Operator oper,
526 uint16_t full_value_size)
528 struct GNUNET_SOCIAL_Host *hst = cls;
529 if (NULL == hst->notice_place_leave_env)
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
535 message_id, oper, name, value_size, (const char *) value);
537 /* skip _nym, it's added later in eom() */
538 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
539 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
542 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
543 GNUNET_PSYC_OP_SET, name, value, value_size);
548 host_recv_notice_place_leave_eom (void *cls,
549 const struct GNUNET_PSYC_MessageHeader *msg,
550 const struct GNUNET_MessageHeader *pmsg,
552 uint8_t is_cancelled)
554 struct GNUNET_SOCIAL_Host *hst = cls;
555 if (NULL == hst->notice_place_leave_env)
558 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "_notice_place_leave: got EOM from nym %s (%s).\n",
561 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
564 if (GNUNET_YES != is_cancelled)
566 if (NULL != hst->farewell_cb)
567 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
568 hst->notice_place_leave_env);
569 /* announce leaving guest to place */
570 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
571 "_nym", hst->notice_place_leave_nym,
572 sizeof (*hst->notice_place_leave_nym));
573 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
574 hst->notice_place_leave_env,
575 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
576 nym_destroy (hst->notice_place_leave_nym);
578 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
579 hst->notice_place_leave_env = NULL;
587 check_place_result (void *cls,
588 const struct GNUNET_OperationResultMessage *res)
590 uint16_t size = ntohs (res->header.size);
591 if (size < sizeof (*res))
592 { /* Error, message too small. */
594 return GNUNET_SYSERR;
601 handle_place_result (void *cls,
602 const struct GNUNET_OperationResultMessage *res)
604 struct GNUNET_SOCIAL_Place *plc = cls;
606 uint16_t size = ntohs (res->header.size);
607 uint16_t data_size = size - sizeof (*res);
608 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
610 GNUNET_OP_result (plc->op, GNUNET_ntohll (res->op_id),
611 GNUNET_ntohll (res->result_code),
612 data, data_size, NULL);
617 check_app_result (void *cls,
618 const struct GNUNET_OperationResultMessage *res)
620 uint16_t size = ntohs (res->header.size);
621 if (size < sizeof (*res))
622 { /* Error, message too small. */
624 return GNUNET_SYSERR;
631 handle_app_result (void *cls,
632 const struct GNUNET_OperationResultMessage *res)
634 struct GNUNET_SOCIAL_App *app = cls;
636 uint16_t size = ntohs (res->header.size);
637 uint16_t data_size = size - sizeof (*res);
638 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
640 GNUNET_OP_result (app->op, GNUNET_ntohll (res->op_id),
641 GNUNET_ntohll (res->result_code),
642 data, data_size, NULL);
647 op_recv_history_result (void *cls, int64_t result,
648 const void *err_msg, uint16_t err_msg_size)
650 LOG (GNUNET_ERROR_TYPE_DEBUG,
651 "Received history replay result: %" PRId64 ".\n", result);
653 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
655 if (NULL != hist->result_cb)
656 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
663 op_recv_state_result (void *cls, int64_t result,
664 const void *err_msg, uint16_t err_msg_size)
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "Received state request result: %" PRId64 ".\n", result);
669 struct GNUNET_SOCIAL_LookHandle *look = cls;
671 if (NULL != look->result_cb)
672 look->result_cb (look->cls, result, err_msg, err_msg_size);
679 check_place_history_result (void *cls,
680 const struct GNUNET_OperationResultMessage *res)
682 struct GNUNET_PSYC_MessageHeader *
683 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
684 uint16_t size = ntohs (res->header.size);
686 if (NULL == pmsg || size < sizeof (*res) + sizeof (*pmsg))
687 { /* Error, message too small. */
689 return GNUNET_SYSERR;
696 handle_place_history_result (void *cls,
697 const struct GNUNET_OperationResultMessage *res)
699 struct GNUNET_SOCIAL_Place *plc = cls;
700 struct GNUNET_PSYC_MessageHeader *
701 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
703 LOG (GNUNET_ERROR_TYPE_DEBUG,
704 "%p Received historic fragment for message #%" PRIu64 ".\n",
705 plc, GNUNET_ntohll (pmsg->message_id));
707 GNUNET_ResultCallback result_cb = NULL;
708 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
710 if (GNUNET_YES != GNUNET_OP_get (plc->op,
711 GNUNET_ntohll (res->op_id),
712 &result_cb, (void *) &hist, NULL))
713 { /* Operation not found. */
714 LOG (GNUNET_ERROR_TYPE_WARNING,
715 "%p Replay operation not found for historic fragment of message #%"
717 plc, GNUNET_ntohll (pmsg->message_id));
721 GNUNET_PSYC_slicer_message (hist->slicer,
722 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
727 check_place_state_result (void *cls,
728 const struct GNUNET_OperationResultMessage *res)
730 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
734 LOG (GNUNET_ERROR_TYPE_WARNING,
735 "Invalid modifier in state result\n");
736 return GNUNET_SYSERR;
739 uint16_t size = ntohs (res->header.size);
740 uint16_t mod_size = ntohs (mod->size);
741 if (size - sizeof (*res) != mod_size)
744 LOG (GNUNET_ERROR_TYPE_WARNING,
745 "Invalid modifier size in state result: %u - %u != %u\n",
746 ntohs (res->header.size), sizeof (*res), mod_size);
747 return GNUNET_SYSERR;
754 handle_place_state_result (void *cls,
755 const struct GNUNET_OperationResultMessage *res)
757 struct GNUNET_SOCIAL_Place *plc = cls;
759 GNUNET_ResultCallback result_cb = NULL;
760 struct GNUNET_SOCIAL_LookHandle *look = NULL;
762 if (GNUNET_YES != GNUNET_OP_get (plc->op,
763 GNUNET_ntohll (res->op_id),
764 &result_cb, (void *) &look, NULL))
765 { /* Operation not found. */
769 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
770 uint16_t mod_size = ntohs (mod->size);
772 switch (ntohs (mod->type))
774 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
776 const struct GNUNET_PSYC_MessageModifier *
777 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
779 const char *name = (const char *) &pmod[1];
780 uint16_t name_size = ntohs (pmod->name_size);
782 || mod_size - sizeof (*pmod) < name_size
783 || '\0' != name[name_size - 1])
786 LOG (GNUNET_ERROR_TYPE_WARNING,
787 "Invalid modifier name in state result\n");
790 look->mod_value_size = ntohs (pmod->value_size);
791 look->var_cb (look->cls, mod, name, name + name_size,
792 mod_size - sizeof (*mod) - name_size,
793 look->mod_value_size);
794 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
796 look->mod_value_remaining = look->mod_value_size;
797 look->mod_name = GNUNET_malloc (name_size);
798 GNUNET_memcpy (look->mod_name, name, name_size);
803 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
804 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
805 mod_size - sizeof (*mod), look->mod_value_size);
806 look->mod_value_remaining -= mod_size - sizeof (*mod);
807 if (0 == look->mod_value_remaining)
809 GNUNET_free (look->mod_name);
817 handle_place_message_ack (void *cls,
818 const struct GNUNET_MessageHeader *msg)
820 struct GNUNET_SOCIAL_Place *plc = cls;
822 GNUNET_PSYC_transmit_got_ack (plc->tmit);
827 check_place_message (void *cls,
828 const struct GNUNET_PSYC_MessageHeader *pmsg)
835 handle_place_message (void *cls,
836 const struct GNUNET_PSYC_MessageHeader *pmsg)
838 struct GNUNET_SOCIAL_Place *plc = cls;
840 GNUNET_PSYC_slicer_message (plc->slicer, pmsg);
845 check_host_message (void *cls,
846 const struct GNUNET_PSYC_MessageHeader *pmsg)
853 handle_host_message (void *cls,
854 const struct GNUNET_PSYC_MessageHeader *pmsg)
856 struct GNUNET_SOCIAL_Host *hst = cls;
858 GNUNET_PSYC_slicer_message (hst->slicer, pmsg);
859 GNUNET_PSYC_slicer_message (hst->plc.slicer, pmsg);
864 handle_host_enter_ack (void *cls,
865 const struct HostEnterAck *hack)
867 struct GNUNET_SOCIAL_Host *hst = cls;
869 hst->plc.pub_key = hack->place_pub_key;
871 int32_t result = ntohl (hack->result_code);
872 if (NULL != hst->enter_cb)
873 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
874 GNUNET_ntohll (hack->max_message_id));
879 check_host_enter_request (void *cls,
880 const struct GNUNET_PSYC_JoinRequestMessage *req)
887 handle_host_enter_request (void *cls,
888 const struct GNUNET_PSYC_JoinRequestMessage *req)
890 struct GNUNET_SOCIAL_Host *hst = cls;
892 if (NULL == hst->answer_door_cb)
895 const char *method_name = NULL;
896 struct GNUNET_PSYC_Environment *env = NULL;
897 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
898 const void *data = NULL;
899 uint16_t data_size = 0;
901 const struct GNUNET_PSYC_Message *join_msg = NULL;
905 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
907 join_msg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (req);
908 LOG (GNUNET_ERROR_TYPE_DEBUG,
909 "Received join_msg of type %u and size %u.\n",
910 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
912 env = GNUNET_PSYC_env_create ();
913 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
914 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
918 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
919 LOG (GNUNET_ERROR_TYPE_WARNING,
920 "Ignoring invalid entry request from nym %s.\n",
927 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
928 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
933 GNUNET_PSYC_env_destroy (env);
934 if (NULL != entry_pmsg)
935 GNUNET_free (entry_pmsg);
940 handle_guest_enter_ack (void *cls,
941 const struct GNUNET_PSYC_CountersResultMessage *cres)
943 struct GNUNET_SOCIAL_Guest *gst = cls;
945 int32_t result = ntohl (cres->result_code);
946 if (NULL != gst->enter_cb)
947 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
948 GNUNET_ntohll (cres->max_message_id));
953 check_guest_enter_decision (void *cls,
954 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
961 handle_guest_enter_decision (void *cls,
962 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
964 struct GNUNET_SOCIAL_Guest *gst = cls;
966 struct GNUNET_PSYC_Message *pmsg = NULL;
967 if (ntohs (dcsn->header.size) > sizeof (*dcsn))
968 pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
970 if (NULL != gst->entry_dcsn_cb)
971 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
976 check_app_ego (void *cls,
977 const struct AppEgoMessage *emsg)
984 handle_app_ego (void *cls,
985 const struct AppEgoMessage *emsg)
987 struct GNUNET_SOCIAL_App *app = cls;
989 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
991 struct GNUNET_HashCode ego_pub_hash;
992 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
995 struct GNUNET_SOCIAL_Ego *
996 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
999 ego = GNUNET_malloc (sizeof (*ego));
1000 ego->pub_key = emsg->ego_pub_key;
1001 ego->name = GNUNET_malloc (name_size);
1002 GNUNET_memcpy (ego->name, &emsg[1], name_size);
1006 ego->name = GNUNET_realloc (ego->name, name_size);
1007 GNUNET_memcpy (ego->name, &emsg[1], name_size);
1010 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
1011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1013 if (NULL != app->ego_cb)
1014 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
1019 handle_app_ego_end (void *cls,
1020 const struct GNUNET_MessageHeader *msg)
1022 //struct GNUNET_SOCIAL_App *app = cls;
1027 check_app_place (void *cls,
1028 const struct AppPlaceMessage *pmsg)
1035 handle_app_place (void *cls,
1036 const struct AppPlaceMessage *pmsg)
1038 struct GNUNET_SOCIAL_App *app = cls;
1040 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
1041 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
1044 struct GNUNET_HashCode ego_pub_hash;
1045 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
1047 struct GNUNET_SOCIAL_Ego *
1048 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
1051 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failure to obtain ego %s.\n",
1052 GNUNET_h2s (&ego_pub_hash));
1057 if (GNUNET_YES == pmsg->is_host)
1059 if (NULL != app->host_cb) {
1060 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
1062 hconn->plc_msg = *pmsg;
1063 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1064 GNUNET_free (hconn);
1067 else if (NULL != app->guest_cb)
1069 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
1071 gconn->plc_msg = *pmsg;
1072 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1073 GNUNET_free (gconn);
1079 handle_app_place_end (void *cls,
1080 const struct GNUNET_MessageHeader *msg)
1082 struct GNUNET_SOCIAL_App *app = cls;
1084 if (NULL != app->connected_cb)
1085 app->connected_cb (app->cb_cls);
1090 * Handler for a #GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK message received
1091 * from the social service.
1093 * @param cls the place of type `struct GNUNET_SOCIAL_Place`
1094 * @param msg the message received from the service
1097 handle_place_leave_ack (void *cls,
1098 const struct GNUNET_MessageHeader *msg)
1100 struct GNUNET_SOCIAL_Place *plc = cls;
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "%s left place %p\n",
1104 plc->is_host ? "host" : "guest",
1106 place_disconnect (plc);
1114 host_connect (struct GNUNET_SOCIAL_Host *hst);
1118 host_reconnect (void *cls)
1125 * Host client disconnected from service.
1127 * Reconnect after backoff period.
1130 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1132 struct GNUNET_SOCIAL_Host *hst = cls;
1133 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "Host client disconnected (%d), re-connecting\n",
1138 if (NULL != plc->tmit)
1140 GNUNET_PSYC_transmit_destroy (plc->tmit);
1143 if (NULL != plc->mq)
1145 GNUNET_MQ_destroy (plc->mq);
1149 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1152 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1157 host_connect (struct GNUNET_SOCIAL_Host *hst)
1159 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1161 struct GNUNET_MQ_MessageHandler handlers[] = {
1162 GNUNET_MQ_hd_fixed_size (host_enter_ack,
1163 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1164 struct HostEnterAck,
1166 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1167 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1168 struct GNUNET_MessageHeader,
1170 GNUNET_MQ_hd_var_size (host_enter_request,
1171 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1172 struct GNUNET_PSYC_JoinRequestMessage,
1174 GNUNET_MQ_hd_var_size (host_message,
1175 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1176 struct GNUNET_PSYC_MessageHeader,
1178 GNUNET_MQ_hd_fixed_size (place_message_ack,
1179 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1180 struct GNUNET_MessageHeader,
1182 GNUNET_MQ_hd_var_size (place_history_result,
1183 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1184 struct GNUNET_OperationResultMessage,
1186 GNUNET_MQ_hd_var_size (place_state_result,
1187 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1188 struct GNUNET_OperationResultMessage,
1190 GNUNET_MQ_hd_var_size (place_result,
1191 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1192 struct GNUNET_OperationResultMessage,
1194 GNUNET_MQ_handler_end ()
1197 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1198 handlers, host_disconnected, hst);
1199 GNUNET_assert (NULL != plc->mq);
1200 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1202 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1207 * Enter a place as host.
1209 * A place is created upon first entering, and it is active until permanently
1210 * left using GNUNET_SOCIAL_host_leave().
1213 * Application handle.
1215 * Identity of the host.
1217 * Policy specifying entry and history restrictions for the place.
1219 * Slicer to handle incoming messages.
1221 * Function called when the place is entered and ready to use.
1222 * @param answer_door_cb
1223 * Function to handle new nyms that want to enter.
1224 * @param farewell_cb
1225 * Function to handle departing nyms.
1227 * Closure for the callbacks.
1229 * @return Handle for the host. This handle contains the pubkey.
1231 struct GNUNET_SOCIAL_Host *
1232 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1233 const struct GNUNET_SOCIAL_Ego *ego,
1234 enum GNUNET_PSYC_Policy policy,
1235 struct GNUNET_PSYC_Slicer *slicer,
1236 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1237 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1238 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1241 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1242 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1244 plc->cfg = app->cfg;
1245 plc->is_host = GNUNET_YES;
1246 plc->slicer = slicer;
1248 hst->enter_cb = enter_cb;
1249 hst->answer_door_cb = answer_door_cb;
1250 hst->farewell_cb = farewell_cb;
1253 plc->op = GNUNET_OP_create ();
1255 hst->slicer = GNUNET_PSYC_slicer_create ();
1256 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1257 host_recv_notice_place_leave_method,
1258 host_recv_notice_place_leave_modifier,
1259 NULL, host_recv_notice_place_leave_eom, hst);
1261 uint16_t app_id_size = strlen (app->id) + 1;
1262 struct HostEnterRequest *hreq;
1263 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1264 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1265 hreq->policy = policy;
1266 hreq->ego_pub_key = ego->pub_key;
1267 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1275 * Reconnect to an already entered place as host.
1278 * Host connection handle.
1279 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1281 * Slicer to handle incoming messages.
1283 * Function called when the place is entered and ready to use.
1284 * @param answer_door_cb
1285 * Function to handle new nyms that want to enter.
1286 * @param farewell_cb
1287 * Function to handle departing nyms.
1289 * Closure for the callbacks.
1291 * @return Handle for the host.
1293 struct GNUNET_SOCIAL_Host *
1294 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1295 struct GNUNET_PSYC_Slicer *slicer,
1296 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1297 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1298 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1301 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1302 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1304 hst->enter_cb = enter_cb;
1305 hst->answer_door_cb = answer_door_cb;
1306 hst->farewell_cb = farewell_cb;
1309 plc->cfg = hconn->app->cfg;
1310 plc->is_host = GNUNET_YES;
1311 plc->slicer = slicer;
1312 plc->pub_key = hconn->plc_msg.place_pub_key;
1313 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1315 plc->op = GNUNET_OP_create ();
1317 hst->slicer = GNUNET_PSYC_slicer_create ();
1318 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1319 host_recv_notice_place_leave_method,
1320 host_recv_notice_place_leave_modifier,
1321 NULL, host_recv_notice_place_leave_eom, hst);
1323 size_t app_id_size = strlen (hconn->app->id) + 1;
1324 struct HostEnterRequest *hreq;
1325 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1326 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1327 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1328 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1329 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1337 * Decision whether to admit @a nym into the place or refuse entry.
1340 * Host of the place.
1342 * Handle for the entity that wanted to enter.
1343 * @param is_admitted
1344 * #GNUNET_YES if @a nym is admitted,
1345 * #GNUNET_NO if @a nym is refused entry,
1346 * #GNUNET_SYSERR if we cannot answer the request.
1347 * @param method_name
1348 * Method name for the rejection message.
1350 * Environment containing variables for the message, or NULL.
1352 * Data for the rejection message to send back.
1354 * Number of bytes in @a data for method.
1355 * @return #GNUNET_OK on success,
1356 * #GNUNET_SYSERR if the message is too large.
1359 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1360 struct GNUNET_SOCIAL_Nym *nym,
1362 const struct GNUNET_PSYC_Message *entry_resp)
1364 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1365 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1366 uint16_t entry_resp_size
1367 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1369 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1370 return GNUNET_SYSERR;
1372 struct GNUNET_MQ_Envelope *
1373 env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1374 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1375 dcsn->is_admitted = htonl (is_admitted);
1376 dcsn->slave_pub_key = nym->pub_key;
1378 if (0 < entry_resp_size)
1379 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1381 GNUNET_MQ_send (plc->mq, env);
1387 * Throw @a nym out of the place.
1389 * The @a nym reference will remain valid until the
1390 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1391 * which should be very soon after this call.
1394 * Host of the place.
1396 * Handle for the entity to be ejected.
1398 * Environment for the message or NULL.
1401 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1402 const struct GNUNET_SOCIAL_Nym *nym,
1403 struct GNUNET_PSYC_Environment *e)
1405 struct GNUNET_PSYC_Environment *env = e;
1407 env = GNUNET_PSYC_env_create ();
1408 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1409 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1410 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1411 GNUNET_SOCIAL_ANNOUNCE_NONE);
1413 GNUNET_PSYC_env_destroy (env);
1418 * Get the public key of @a ego.
1423 * @return Public key of ego.
1425 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1426 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1428 return &ego->pub_key;
1433 * Get the hash of the public key of @a ego.
1438 * @return Hash of the public key of @a ego.
1440 const struct GNUNET_HashCode *
1441 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1443 return &ego->pub_key_hash;
1448 * Get the name of @a ego.
1453 * @return Public key of @a ego.
1456 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1463 * Get the public key of @a nym.
1465 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1470 * @return Public key of @a nym.
1472 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1473 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1475 return &nym->pub_key;
1480 * Get the hash of the public key of @a nym.
1485 * @return Hash of the public key of @a nym.
1487 const struct GNUNET_HashCode *
1488 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1490 return &nym->pub_key_hash;
1495 * Send a message to all nyms that are present in the place.
1497 * This function is restricted to the host. Nyms can only send requests
1498 * to the host who can decide to relay it to everyone in the place.
1500 * @param host Host of the place.
1501 * @param method_name Method to use for the announcement.
1502 * @param env Environment containing variables for the message and operations
1503 * on objects of the place. Can be NULL.
1504 * @param notify Function to call to get the payload of the announcement.
1505 * @param notify_cls Closure for @a notify.
1506 * @param flags Flags for this announcement.
1508 * @return NULL on error (announcement already in progress?).
1510 struct GNUNET_SOCIAL_Announcement *
1511 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1512 const char *method_name,
1513 const struct GNUNET_PSYC_Environment *env,
1514 GNUNET_PSYC_TransmitNotifyData notify_data,
1515 void *notify_data_cls,
1516 enum GNUNET_SOCIAL_AnnounceFlags flags)
1518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1519 "PSYC_transmit_message for host, method: %s\n",
1522 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1523 NULL, notify_data, notify_data_cls, flags))
1524 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1531 * Resume transmitting announcement.
1534 * The announcement to resume.
1537 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1539 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1544 * Cancel announcement.
1547 * The announcement to cancel.
1550 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1552 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1557 * Obtain handle for a hosted place.
1559 * The returned handle can be used to access the place API.
1561 * @param host Handle for the host.
1563 * @return Handle for the hosted place, valid as long as @a host is valid.
1565 struct GNUNET_SOCIAL_Place *
1566 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1573 * Disconnect from a home.
1575 * Invalidates host handle.
1578 * The host to disconnect.
1581 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1582 GNUNET_ContinuationCallback disconnect_cb,
1585 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1587 plc->disconnect_cb = disconnect_cb;
1588 plc->disconnect_cls = cls;
1589 place_disconnect (plc);
1594 * Stop hosting the home.
1596 * Sends a _notice_place_closing announcement to the home.
1597 * Invalidates host handle.
1602 * Environment for the message or NULL.
1603 * _nym is set to @e nym regardless whether an @e env is provided.
1604 * @param disconnect_cb
1605 * Function called after the host left the place
1606 * and disconnected from the social service.
1608 * Closure for @a disconnect_cb.
1611 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1612 const struct GNUNET_PSYC_Environment *env,
1613 GNUNET_ContinuationCallback disconnect_cb,
1616 struct GNUNET_MQ_Envelope *envelope;
1618 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1619 GNUNET_SOCIAL_ANNOUNCE_NONE);
1620 hst->plc.disconnect_cb = disconnect_cb;
1621 hst->plc.disconnect_cls = cls;
1622 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1623 GNUNET_MQ_send (hst->plc.mq,
1632 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1636 guest_reconnect (void *cls)
1638 guest_connect (cls);
1643 * Guest client disconnected from service.
1645 * Reconnect after backoff period.
1648 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1650 struct GNUNET_SOCIAL_Guest *gst = cls;
1651 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1653 LOG (GNUNET_ERROR_TYPE_DEBUG,
1654 "Guest client disconnected (%d), re-connecting\n",
1656 if (NULL != plc->tmit)
1658 GNUNET_PSYC_transmit_destroy (plc->tmit);
1661 if (NULL != plc->mq)
1663 GNUNET_MQ_destroy (plc->mq);
1667 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1670 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1675 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1677 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1679 struct GNUNET_MQ_MessageHandler handlers[] = {
1680 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1681 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1682 struct GNUNET_PSYC_CountersResultMessage,
1684 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1685 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1686 struct GNUNET_MessageHeader,
1688 GNUNET_MQ_hd_var_size (guest_enter_decision,
1689 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1690 struct GNUNET_PSYC_JoinDecisionMessage,
1692 GNUNET_MQ_hd_var_size (place_message,
1693 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1694 struct GNUNET_PSYC_MessageHeader,
1696 GNUNET_MQ_hd_fixed_size (place_message_ack,
1697 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1698 struct GNUNET_MessageHeader,
1700 GNUNET_MQ_hd_var_size (place_history_result,
1701 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1702 struct GNUNET_OperationResultMessage,
1704 GNUNET_MQ_hd_var_size (place_state_result,
1705 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1706 struct GNUNET_OperationResultMessage,
1708 GNUNET_MQ_hd_var_size (place_result,
1709 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1710 struct GNUNET_OperationResultMessage,
1712 GNUNET_MQ_handler_end ()
1715 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1716 handlers, guest_disconnected, gst);
1717 GNUNET_assert (NULL != plc->mq);
1718 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1720 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1724 static struct GNUNET_MQ_Envelope *
1725 guest_enter_request_create (const char *app_id,
1726 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1727 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1728 const struct GNUNET_PeerIdentity *origin,
1730 const struct GNUNET_PeerIdentity *relays,
1731 const struct GNUNET_PSYC_Message *join_msg)
1733 uint16_t app_id_size = strlen (app_id) + 1;
1734 uint16_t join_msg_size = ntohs (join_msg->header.size);
1735 uint16_t relay_size = relay_count * sizeof (*relays);
1737 struct GuestEnterRequest *greq;
1738 struct GNUNET_MQ_Envelope *
1739 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1740 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1741 greq->place_pub_key = *place_pub_key;
1742 greq->ego_pub_key = *ego_pub_key;
1743 greq->origin = *origin;
1744 greq->relay_count = htonl (relay_count);
1746 char *p = (char *) &greq[1];
1747 GNUNET_memcpy (p, app_id, app_id_size);
1752 GNUNET_memcpy (p, relays, relay_size);
1756 GNUNET_memcpy (p, join_msg, join_msg_size);
1762 * Request entry to a place as a guest.
1765 * Application handle.
1767 * Identity of the guest.
1768 * @param place_pub_key
1769 * Public key of the place to enter.
1771 * Flags for the entry.
1773 * Peer identity of the origin of the underlying multicast group.
1774 * @param relay_count
1775 * Number of elements in the @a relays array.
1777 * Relays for the underlying multicast group.
1778 * @param method_name
1779 * Method name for the message.
1781 * Environment containing variables for the message, or NULL.
1783 * Payload for the message to give to the enter callback.
1785 * Number of bytes in @a data.
1787 * Slicer to use for processing incoming requests from guests.
1789 * @return NULL on errors, otherwise handle for the guest.
1791 struct GNUNET_SOCIAL_Guest *
1792 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1793 const struct GNUNET_SOCIAL_Ego *ego,
1794 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1795 enum GNUNET_PSYC_SlaveJoinFlags flags,
1796 const struct GNUNET_PeerIdentity *origin,
1797 uint32_t relay_count,
1798 const struct GNUNET_PeerIdentity *relays,
1799 const struct GNUNET_PSYC_Message *entry_msg,
1800 struct GNUNET_PSYC_Slicer *slicer,
1801 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1802 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1805 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1806 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1808 plc->ego_pub_key = ego->pub_key;
1809 plc->pub_key = *place_pub_key;
1810 plc->cfg = app->cfg;
1811 plc->is_host = GNUNET_NO;
1812 plc->slicer = slicer;
1814 plc->op = GNUNET_OP_create ();
1817 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1818 origin, relay_count, relays, entry_msg);
1820 gst->enter_cb = local_enter_cb;
1821 gst->entry_dcsn_cb = entry_dcsn_cb;
1824 guest_connect (gst);
1830 * Request entry to a place by name as a guest.
1833 * Application handle.
1835 * Identity of the guest.
1837 * GNS name of the place to enter. Either in the form of
1838 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1839 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1840 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1841 * pseudonym's place directly.
1843 * Password to decrypt the record, or NULL for cleartext records.
1845 * Entry request message or NULL.
1847 * Slicer to use for processing incoming requests from guests.
1848 * @param local_enter_cb
1849 * Called upon connection established to the social service.
1850 * @param entry_decision_cb
1851 * Called upon receiving entry decision.
1853 * @return NULL on errors, otherwise handle for the guest.
1855 struct GNUNET_SOCIAL_Guest *
1856 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1857 const struct GNUNET_SOCIAL_Ego *ego,
1858 const char *gns_name,
1859 const char *password,
1860 const struct GNUNET_PSYC_Message *join_msg,
1861 struct GNUNET_PSYC_Slicer *slicer,
1862 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1863 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1866 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1867 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1869 if (NULL == password)
1872 uint16_t app_id_size = strlen (app->id) + 1;
1873 uint16_t gns_name_size = strlen (gns_name) + 1;
1874 uint16_t password_size = strlen (password) + 1;
1876 uint16_t join_msg_size = 0;
1877 if (NULL != join_msg)
1878 join_msg_size = ntohs (join_msg->header.size);
1880 struct GuestEnterByNameRequest *greq;
1882 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1883 + password_size + join_msg_size,
1884 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1886 greq->ego_pub_key = ego->pub_key;
1888 char *p = (char *) &greq[1];
1889 GNUNET_memcpy (p, app->id, app_id_size);
1891 GNUNET_memcpy (p, gns_name, gns_name_size);
1893 GNUNET_memcpy (p, password, password_size);
1895 if (NULL != join_msg)
1896 GNUNET_memcpy (p, join_msg, join_msg_size);
1898 plc->ego_pub_key = ego->pub_key;
1899 plc->cfg = app->cfg;
1900 plc->is_host = GNUNET_NO;
1901 plc->slicer = slicer;
1903 plc->op = GNUNET_OP_create ();
1905 gst->enter_cb = local_enter_cb;
1906 gst->entry_dcsn_cb = entry_decision_cb;
1909 guest_connect (gst);
1914 struct ReconnectContext
1916 struct GNUNET_SOCIAL_Guest *guest;
1918 int64_t *max_message_id;
1919 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
1925 guest_enter_reconnect_cb (void *cls,
1927 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1928 uint64_t max_message_id)
1930 struct ReconnectContext *reconnect_ctx = cls;
1932 GNUNET_assert (NULL != reconnect_ctx);
1933 reconnect_ctx->result = GNUNET_new (int);
1934 *(reconnect_ctx->result) = result;
1935 reconnect_ctx->max_message_id = GNUNET_new (int64_t);
1936 *(reconnect_ctx->max_message_id) = max_message_id;
1941 guest_entry_dcsn_reconnect_cb (void *cls,
1943 const struct GNUNET_PSYC_Message *entry_resp)
1945 struct ReconnectContext *reconnect_ctx = cls;
1946 struct GNUNET_SOCIAL_Guest *gst = reconnect_ctx->guest;
1948 GNUNET_assert (NULL != reconnect_ctx);
1949 GNUNET_assert (NULL != reconnect_ctx->result);
1950 GNUNET_assert (NULL != reconnect_ctx->max_message_id);
1951 if (GNUNET_YES != is_admitted)
1953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1954 "Guest was rejected after calling "
1955 "GNUNET_SOCIAL_guest_enter_reconnect ()\n");
1957 else if (NULL != reconnect_ctx->enter_cb)
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 "guest reconnected!\n");
1961 reconnect_ctx->enter_cb (reconnect_ctx->enter_cls,
1962 *(reconnect_ctx->result),
1964 *(reconnect_ctx->max_message_id));
1966 GNUNET_free (reconnect_ctx->result);
1967 GNUNET_free (reconnect_ctx->max_message_id);
1968 GNUNET_free (reconnect_ctx);
1973 * Reconnect to an already entered place as guest.
1976 * Guest connection handle.
1977 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1979 * Flags for the entry.
1981 * Slicer to use for processing incoming requests from guests.
1983 * Called upon re-entering is complete.
1984 * @param entry_decision_cb
1985 * Called upon receiving entry decision.
1987 * @return NULL on errors, otherwise handle for the guest.
1989 struct GNUNET_SOCIAL_Guest *
1990 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1991 enum GNUNET_PSYC_SlaveJoinFlags flags,
1992 struct GNUNET_PSYC_Slicer *slicer,
1993 GNUNET_SOCIAL_GuestEnterCallback enter_cb,
1996 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1997 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1998 struct ReconnectContext *reconnect_ctx;
2000 uint16_t app_id_size = strlen (gconn->app->id) + 1;
2001 struct GuestEnterRequest *greq;
2003 = GNUNET_MQ_msg_extra (greq, app_id_size,
2004 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
2005 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
2006 greq->place_pub_key = gconn->plc_msg.place_pub_key;
2007 greq->flags = htonl (flags);
2009 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
2011 plc->cfg = gconn->app->cfg;
2012 plc->is_host = GNUNET_NO;
2013 plc->slicer = slicer;
2014 plc->pub_key = gconn->plc_msg.place_pub_key;
2015 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
2017 reconnect_ctx = GNUNET_new (struct ReconnectContext);
2018 reconnect_ctx->guest = gst;
2019 reconnect_ctx->enter_cb = enter_cb;
2020 reconnect_ctx->enter_cls = cls;
2022 plc->op = GNUNET_OP_create ();
2023 gst->enter_cb = &guest_enter_reconnect_cb;
2024 gst->entry_dcsn_cb = &guest_entry_dcsn_reconnect_cb;
2025 gst->cb_cls = reconnect_ctx;
2027 guest_connect (gst);
2033 * Talk to the host of the place.
2036 * Place where we want to talk to the host.
2037 * @param method_name
2038 * Method to invoke on the host.
2040 * Environment containing variables for the message, or NULL.
2041 * @param notify_data
2042 * Function to use to get the payload for the method.
2043 * @param notify_data_cls
2044 * Closure for @a notify_data.
2046 * Flags for the message being sent.
2048 * @return NULL if we are already trying to talk to the host,
2049 * otherwise handle to cancel the request.
2051 struct GNUNET_SOCIAL_TalkRequest *
2052 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2053 const char *method_name,
2054 const struct GNUNET_PSYC_Environment *env,
2055 GNUNET_PSYC_TransmitNotifyData notify_data,
2056 void *notify_data_cls,
2057 enum GNUNET_SOCIAL_TalkFlags flags)
2059 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2060 GNUNET_assert (NULL != plc->tmit);
2063 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2064 NULL, notify_data, notify_data_cls, flags))
2065 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2072 * Resume talking to the host of the place.
2075 * Talk request to resume.
2078 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2080 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2085 * Cancel talking to the host of the place.
2088 * Talk request to cancel.
2091 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2093 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2098 * Disconnect from a place.
2100 * Invalidates guest handle.
2103 * The guest to disconnect.
2106 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2107 GNUNET_ContinuationCallback disconnect_cb,
2110 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2112 plc->disconnect_cb = disconnect_cb;
2113 plc->disconnect_cls = cls;
2114 place_disconnect (plc);
2119 * Leave a place temporarily or permanently.
2121 * Notifies the owner of the place about leaving, and destroys the place handle.
2125 * @param keep_active
2126 * Keep place active after last application disconnected.
2127 * #GNUNET_YES or #GNUNET_NO
2129 * Optional environment for the leave message if @a keep_active
2130 * is #GNUNET_NO. NULL if not needed.
2132 * Called upon disconnecting from the social service.
2135 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2136 struct GNUNET_PSYC_Environment *env,
2137 GNUNET_ContinuationCallback disconnect_cb,
2140 struct GNUNET_MQ_Envelope *envelope;
2142 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2143 GNUNET_SOCIAL_TALK_NONE);
2144 gst->plc.disconnect_cb = disconnect_cb;
2145 gst->plc.disconnect_cls = cls;
2146 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
2147 GNUNET_MQ_send (gst->plc.mq,
2153 * Obtain handle for a place entered as guest.
2155 * The returned handle can be used to access the place API.
2157 * @param guest Handle for the guest.
2159 * @return Handle for the place, valid as long as @a guest is valid.
2161 struct GNUNET_SOCIAL_Place *
2162 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2169 * Obtain the public key of a place.
2174 * @return Public key of the place.
2176 const struct GNUNET_CRYPTO_EddsaPublicKey *
2177 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2179 return &plc->pub_key;
2184 * Set message processing @a flags for a @a method_prefix.
2188 * @param method_prefix
2189 * Method prefix @a flags apply to.
2191 * The flags that apply to a matching @a method_prefix.
2194 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2195 const char *method_prefix,
2196 enum GNUNET_SOCIAL_MsgProcFlags flags)
2198 GNUNET_assert (NULL != method_prefix);
2199 struct MsgProcRequest *mpreq;
2200 uint16_t method_size = strnlen (method_prefix,
2201 GNUNET_MAX_MESSAGE_SIZE
2202 - sizeof (*mpreq)) + 1;
2203 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2205 struct GNUNET_MQ_Envelope *
2206 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2207 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2208 mpreq->flags = htonl (flags);
2209 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2211 GNUNET_MQ_send (plc->mq, env);
2216 * Clear all message processing flags previously set for this place.
2219 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2221 struct GNUNET_MessageHeader *req;
2222 struct GNUNET_MQ_Envelope *
2223 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2225 GNUNET_MQ_send (plc->mq, env);
2229 static struct GNUNET_SOCIAL_HistoryRequest *
2230 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2231 uint64_t start_message_id,
2232 uint64_t end_message_id,
2233 uint64_t message_limit,
2234 const char *method_prefix,
2236 struct GNUNET_PSYC_Slicer *slicer,
2237 GNUNET_ResultCallback result_cb,
2240 struct GNUNET_PSYC_HistoryRequestMessage *req;
2241 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2243 hist->slicer = slicer;
2244 hist->result_cb = result_cb;
2246 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2248 GNUNET_assert (NULL != method_prefix);
2249 uint16_t method_size = strnlen (method_prefix,
2250 GNUNET_MAX_MESSAGE_SIZE
2251 - sizeof (*req)) + 1;
2252 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2254 struct GNUNET_MQ_Envelope *
2255 env = GNUNET_MQ_msg_extra (req, method_size,
2256 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2257 req->start_message_id = GNUNET_htonll (start_message_id);
2258 req->end_message_id = GNUNET_htonll (end_message_id);
2259 req->message_limit = GNUNET_htonll (message_limit);
2260 req->flags = htonl (flags);
2261 req->op_id = GNUNET_htonll (hist->op_id);
2262 GNUNET_memcpy (&req[1], method_prefix, method_size);
2264 GNUNET_MQ_send (plc->mq, env);
2270 * Learn about the history of a place.
2272 * Messages are returned through the @a slicer function
2273 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2276 * Place we want to learn more about.
2277 * @param start_message_id
2278 * First historic message we are interested in.
2279 * @param end_message_id
2280 * Last historic message we are interested in (inclusive).
2281 * @param method_prefix
2282 * Only retrieve messages with this method prefix.
2284 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2286 * Slicer to use for retrieved messages.
2287 * Can be the same as the slicer of the place.
2289 * Function called after all messages retrieved.
2290 * NULL if not needed.
2291 * @param cls Closure for @a result_cb.
2293 struct GNUNET_SOCIAL_HistoryRequest *
2294 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2295 uint64_t start_message_id,
2296 uint64_t end_message_id,
2297 const char *method_prefix,
2299 struct GNUNET_PSYC_Slicer *slicer,
2300 GNUNET_ResultCallback result_cb,
2303 return place_history_replay (plc, start_message_id, end_message_id, 0,
2304 method_prefix, flags, slicer, result_cb, cls);
2309 * Learn about the history of a place.
2311 * Sends messages through the slicer function of the place where
2312 * start_message_id <= message_id <= end_message_id.
2313 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2315 * To get the latest message, use 0 for both the start and end message ID.
2318 * Place we want to learn more about.
2319 * @param message_limit
2320 * Maximum number of historic messages we are interested in.
2321 * @param method_prefix
2322 * Only retrieve messages with this method prefix.
2324 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2326 * Function called after all messages retrieved.
2327 * NULL if not needed.
2328 * @param cls Closure for @a result_cb.
2330 struct GNUNET_SOCIAL_HistoryRequest *
2331 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2332 uint64_t message_limit,
2333 const char *method_prefix,
2335 struct GNUNET_PSYC_Slicer *slicer,
2336 GNUNET_ResultCallback result_cb,
2339 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2340 slicer, result_cb, cls);
2345 * Cancel learning about the history of a place.
2348 * History lesson to cancel.
2351 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2353 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2359 * Request matching state variables.
2361 static struct GNUNET_SOCIAL_LookHandle *
2362 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2363 uint16_t type, const char *name,
2364 GNUNET_PSYC_StateVarCallback var_cb,
2365 GNUNET_ResultCallback result_cb, void *cls)
2367 struct GNUNET_PSYC_StateRequestMessage *req;
2368 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2370 look->var_cb = var_cb;
2371 look->result_cb = result_cb;
2373 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2375 GNUNET_assert (NULL != name);
2376 size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
2377 - sizeof (*req)) + 1;
2378 struct GNUNET_MQ_Envelope *
2379 env = GNUNET_MQ_msg_extra (req, name_size, type);
2380 req->op_id = GNUNET_htonll (look->op_id);
2381 GNUNET_memcpy (&req[1], name, name_size);
2383 GNUNET_MQ_send (plc->mq, env);
2389 * Look at a particular object in the place.
2391 * The best matching object is returned (its name might be less specific than
2392 * what was requested).
2395 * The place where to look.
2397 * Full name of the object.
2399 * Set to the size of the returned value.
2401 * @return NULL if there is no such object at this place.
2403 struct GNUNET_SOCIAL_LookHandle *
2404 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2405 const char *full_name,
2406 GNUNET_PSYC_StateVarCallback var_cb,
2407 GNUNET_ResultCallback result_cb,
2410 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2411 full_name, var_cb, result_cb, cls);
2416 * Look for objects in the place with a matching name prefix.
2419 * The place where to look.
2420 * @param name_prefix
2421 * Look at objects with names beginning with this value.
2423 * Function to call for each object found.
2425 * Closure for callback function.
2427 * @return Handle that can be used to stop looking at objects.
2429 struct GNUNET_SOCIAL_LookHandle *
2430 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2431 const char *name_prefix,
2432 GNUNET_PSYC_StateVarCallback var_cb,
2433 GNUNET_ResultCallback result_cb,
2436 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2437 name_prefix, var_cb, result_cb, cls);
2442 * Cancel a state request operation.
2445 * Handle for the operation to cancel.
2448 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2450 GNUNET_OP_remove (look->plc->op, look->op_id);
2456 op_recv_zone_add_place_result (void *cls, int64_t result,
2457 const void *err_msg, uint16_t err_msg_size)
2459 LOG (GNUNET_ERROR_TYPE_DEBUG,
2460 "Received zone add place result: %" PRId64 ".\n", result);
2462 struct ZoneAddPlaceHandle *add_plc = cls;
2463 if (NULL != add_plc->result_cb)
2464 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2466 GNUNET_free (add_plc);
2471 * Advertise @e place in the GNS zone of @e ego.
2474 * Application handle.
2477 * @param place_pub_key
2478 * Public key of place to add.
2480 * The name for the PLACE record to put in the zone.
2482 * Password used to encrypt the record or NULL to keep it cleartext.
2483 * @param relay_count
2484 * Number of elements in the @a relays array.
2486 * List of relays to put in the PLACE record to advertise
2487 * as entry points to the place in addition to the origin.
2488 * @param expiration_time
2489 * Expiration time of the record, use 0 to remove the record.
2491 * Function called with the result of the operation.
2493 * Closure for @a result_cb
2495 * @return #GNUNET_OK if the request was sent,
2496 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2499 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2500 const struct GNUNET_SOCIAL_Ego *ego,
2502 const char *password,
2503 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2504 const struct GNUNET_PeerIdentity *origin,
2505 uint32_t relay_count,
2506 const struct GNUNET_PeerIdentity *relays,
2507 struct GNUNET_TIME_Absolute expiration_time,
2508 GNUNET_ResultCallback result_cb,
2511 struct ZoneAddPlaceRequest *preq;
2512 size_t name_size = strlen (name) + 1;
2513 size_t password_size = strlen (password) + 1;
2514 size_t relay_size = relay_count * sizeof (*relays);
2515 size_t payload_size = name_size + password_size + relay_size;
2517 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2518 return GNUNET_SYSERR;
2520 struct GNUNET_MQ_Envelope *
2521 env = GNUNET_MQ_msg_extra (preq, payload_size,
2522 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2523 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2524 preq->ego_pub_key = ego->pub_key;
2525 preq->place_pub_key = *place_pub_key;
2526 preq->origin = *origin;
2527 preq->relay_count = htonl (relay_count);
2529 char *p = (char *) &preq[1];
2530 GNUNET_memcpy (p, name, name_size);
2532 GNUNET_memcpy (p, password, password_size);
2534 GNUNET_memcpy (p, relays, relay_size);
2536 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2537 add_plc->result_cb = result_cb;
2538 add_plc->result_cls = result_cls;
2540 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2541 op_recv_zone_add_place_result,
2544 GNUNET_MQ_send (app->mq, env);
2550 op_recv_zone_add_nym_result (void *cls, int64_t result,
2551 const void *err_msg, uint16_t err_msg_size)
2553 LOG (GNUNET_ERROR_TYPE_DEBUG,
2554 "Received zone add nym result: %" PRId64 ".\n", result);
2556 struct ZoneAddNymHandle *add_nym = cls;
2557 if (NULL != add_nym->result_cb)
2558 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2560 GNUNET_free (add_nym);
2565 * Add nym to the GNS zone of @e ego.
2572 * The name for the PKEY record to put in the zone.
2573 * @param nym_pub_key
2574 * Public key of nym to add.
2575 * @param expiration_time
2576 * Expiration time of the record, use 0 to remove the record.
2578 * Function called with the result of the operation.
2580 * Closure for @a result_cb
2582 * @return #GNUNET_OK if the request was sent,
2583 * #GNUNET_SYSERR on error, e.g. the name is too long.
2586 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2587 const struct GNUNET_SOCIAL_Ego *ego,
2589 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2590 struct GNUNET_TIME_Absolute expiration_time,
2591 GNUNET_ResultCallback result_cb,
2594 struct ZoneAddNymRequest *nreq;
2596 size_t name_size = strlen (name) + 1;
2597 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2598 return GNUNET_SYSERR;
2600 struct GNUNET_MQ_Envelope *
2601 env = GNUNET_MQ_msg_extra (nreq, name_size,
2602 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2603 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2604 nreq->ego_pub_key = ego->pub_key;
2605 nreq->nym_pub_key = *nym_pub_key;
2606 GNUNET_memcpy (&nreq[1], name, name_size);
2608 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2609 add_nym->result_cb = result_cb;
2610 add_nym->result_cls = result_cls;
2612 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2613 op_recv_zone_add_nym_result,
2616 GNUNET_MQ_send (app->mq, env);
2625 app_connect (struct GNUNET_SOCIAL_App *app);
2629 app_reconnect (void *cls)
2636 * App client disconnected from service.
2638 * Reconnect after backoff period.
2641 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2643 struct GNUNET_SOCIAL_App *app = cls;
2645 LOG (GNUNET_ERROR_TYPE_DEBUG,
2646 "App client disconnected (%d), re-connecting\n",
2648 if (NULL != app->mq)
2650 GNUNET_MQ_destroy (app->mq);
2654 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2657 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2662 app_connect (struct GNUNET_SOCIAL_App *app)
2664 struct GNUNET_MQ_MessageHandler handlers[] = {
2665 GNUNET_MQ_hd_var_size (app_ego,
2666 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2667 struct AppEgoMessage,
2669 GNUNET_MQ_hd_fixed_size (app_ego_end,
2670 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2671 struct GNUNET_MessageHeader,
2673 GNUNET_MQ_hd_var_size (app_place,
2674 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2675 struct AppPlaceMessage,
2677 GNUNET_MQ_hd_fixed_size (app_place_end,
2678 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2679 struct GNUNET_MessageHeader,
2681 GNUNET_MQ_hd_var_size (app_result,
2682 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2683 struct GNUNET_OperationResultMessage,
2685 GNUNET_MQ_handler_end ()
2688 app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
2689 handlers, app_disconnected, app);
2690 GNUNET_assert (NULL != app->mq);
2691 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2696 * Connect application to the social service.
2698 * The @host_place_cb and @guest_place_cb functions are
2699 * initially called for each entered places,
2700 * then later each time a new place is entered with the current application ID.
2707 * Function to notify about an available ego.
2709 * Function to notify about a place entered as host.
2711 * Function to notify about a place entered as guest.
2713 * Closure for the callbacks.
2715 * @return Handle that can be used to stop listening.
2717 struct GNUNET_SOCIAL_App *
2718 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2720 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2721 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2722 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2723 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2726 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2727 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2731 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2733 app->ego_cb = ego_cb;
2734 app->host_cb = host_cb;
2735 app->guest_cb = guest_cb;
2736 app->connected_cb = connected_cb;
2738 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2739 app->op = GNUNET_OP_create ();
2740 app->id = GNUNET_malloc (app_id_size);
2741 GNUNET_memcpy (app->id, id, app_id_size);
2743 struct AppConnectRequest *creq;
2744 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2745 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2746 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2754 app_cleanup (struct GNUNET_SOCIAL_App *app)
2756 if (NULL != app->mq)
2758 GNUNET_MQ_destroy (app->mq);
2761 if (NULL != app->disconnect_cb)
2763 app->disconnect_cb (app->disconnect_cls);
2764 app->disconnect_cb = NULL;
2770 * Disconnect application.
2773 * Application handle.
2774 * @param disconnect_cb
2775 * Disconnect callback.
2776 * @param disconnect_cls
2777 * Disconnect closure.
2780 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2781 GNUNET_ContinuationCallback disconnect_cb,
2782 void *disconnect_cls)
2784 app->disconnect_cb = disconnect_cb;
2785 app->disconnect_cls = disconnect_cls;
2787 if (NULL != app->mq)
2789 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2792 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
2807 * Detach application from a place.
2809 * Removes the place from the entered places list for this application.
2810 * Note: this does not disconnect from the place.
2812 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2820 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2821 struct GNUNET_SOCIAL_Place *plc)
2823 struct AppDetachRequest *dreq;
2824 struct GNUNET_MQ_Envelope *
2825 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2826 dreq->place_pub_key = plc->pub_key;
2827 dreq->ego_pub_key = plc->ego_pub_key;
2829 GNUNET_MQ_send (app->mq, env);
2833 /* end of social_api.c */