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\n");
1521 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1522 NULL, notify_data, notify_data_cls, flags))
1523 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1530 * Resume transmitting announcement.
1533 * The announcement to resume.
1536 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1538 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1543 * Cancel announcement.
1546 * The announcement to cancel.
1549 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1551 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1556 * Obtain handle for a hosted place.
1558 * The returned handle can be used to access the place API.
1560 * @param host Handle for the host.
1562 * @return Handle for the hosted place, valid as long as @a host is valid.
1564 struct GNUNET_SOCIAL_Place *
1565 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1572 * Disconnect from a home.
1574 * Invalidates host handle.
1577 * The host to disconnect.
1580 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1581 GNUNET_ContinuationCallback disconnect_cb,
1584 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1586 plc->disconnect_cb = disconnect_cb;
1587 plc->disconnect_cls = cls;
1588 place_disconnect (plc);
1593 * Stop hosting the home.
1595 * Sends a _notice_place_closing announcement to the home.
1596 * Invalidates host handle.
1601 * Environment for the message or NULL.
1602 * _nym is set to @e nym regardless whether an @e env is provided.
1603 * @param disconnect_cb
1604 * Function called after the host left the place
1605 * and disconnected from the social service.
1607 * Closure for @a disconnect_cb.
1610 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1611 const struct GNUNET_PSYC_Environment *env,
1612 GNUNET_ContinuationCallback disconnect_cb,
1615 struct GNUNET_MQ_Envelope *envelope;
1617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1618 "sending _notice_place_closing\n");
1619 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1620 GNUNET_SOCIAL_ANNOUNCE_NONE);
1621 hst->plc.disconnect_cb = disconnect_cb;
1622 hst->plc.disconnect_cls = cls;
1623 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1624 GNUNET_MQ_send (hst->plc.mq,
1633 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1637 guest_reconnect (void *cls)
1639 guest_connect (cls);
1644 * Guest client disconnected from service.
1646 * Reconnect after backoff period.
1649 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1651 struct GNUNET_SOCIAL_Guest *gst = cls;
1652 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1654 LOG (GNUNET_ERROR_TYPE_DEBUG,
1655 "Guest client disconnected (%d), re-connecting\n",
1657 if (NULL != plc->tmit)
1659 GNUNET_PSYC_transmit_destroy (plc->tmit);
1662 if (NULL != plc->mq)
1664 GNUNET_MQ_destroy (plc->mq);
1668 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1671 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1676 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1678 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1680 struct GNUNET_MQ_MessageHandler handlers[] = {
1681 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1682 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1683 struct GNUNET_PSYC_CountersResultMessage,
1685 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1686 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1687 struct GNUNET_MessageHeader,
1689 GNUNET_MQ_hd_var_size (guest_enter_decision,
1690 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1691 struct GNUNET_PSYC_JoinDecisionMessage,
1693 GNUNET_MQ_hd_var_size (place_message,
1694 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1695 struct GNUNET_PSYC_MessageHeader,
1697 GNUNET_MQ_hd_fixed_size (place_message_ack,
1698 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1699 struct GNUNET_MessageHeader,
1701 GNUNET_MQ_hd_var_size (place_history_result,
1702 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1703 struct GNUNET_OperationResultMessage,
1705 GNUNET_MQ_hd_var_size (place_state_result,
1706 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1707 struct GNUNET_OperationResultMessage,
1709 GNUNET_MQ_hd_var_size (place_result,
1710 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1711 struct GNUNET_OperationResultMessage,
1713 GNUNET_MQ_handler_end ()
1716 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1717 handlers, guest_disconnected, gst);
1718 GNUNET_assert (NULL != plc->mq);
1719 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1721 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1725 static struct GNUNET_MQ_Envelope *
1726 guest_enter_request_create (const char *app_id,
1727 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1728 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1729 const struct GNUNET_PeerIdentity *origin,
1731 const struct GNUNET_PeerIdentity *relays,
1732 const struct GNUNET_PSYC_Message *join_msg)
1734 uint16_t app_id_size = strlen (app_id) + 1;
1735 uint16_t join_msg_size = ntohs (join_msg->header.size);
1736 uint16_t relay_size = relay_count * sizeof (*relays);
1738 struct GuestEnterRequest *greq;
1739 struct GNUNET_MQ_Envelope *
1740 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1741 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1742 greq->place_pub_key = *place_pub_key;
1743 greq->ego_pub_key = *ego_pub_key;
1744 greq->origin = *origin;
1745 greq->relay_count = htonl (relay_count);
1747 char *p = (char *) &greq[1];
1748 GNUNET_memcpy (p, app_id, app_id_size);
1753 GNUNET_memcpy (p, relays, relay_size);
1757 GNUNET_memcpy (p, join_msg, join_msg_size);
1763 * Request entry to a place as a guest.
1766 * Application handle.
1768 * Identity of the guest.
1769 * @param place_pub_key
1770 * Public key of the place to enter.
1772 * Flags for the entry.
1774 * Peer identity of the origin of the underlying multicast group.
1775 * @param relay_count
1776 * Number of elements in the @a relays array.
1778 * Relays for the underlying multicast group.
1779 * @param method_name
1780 * Method name for the message.
1782 * Environment containing variables for the message, or NULL.
1784 * Payload for the message to give to the enter callback.
1786 * Number of bytes in @a data.
1788 * Slicer to use for processing incoming requests from guests.
1790 * @return NULL on errors, otherwise handle for the guest.
1792 struct GNUNET_SOCIAL_Guest *
1793 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1794 const struct GNUNET_SOCIAL_Ego *ego,
1795 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1796 enum GNUNET_PSYC_SlaveJoinFlags flags,
1797 const struct GNUNET_PeerIdentity *origin,
1798 uint32_t relay_count,
1799 const struct GNUNET_PeerIdentity *relays,
1800 const struct GNUNET_PSYC_Message *entry_msg,
1801 struct GNUNET_PSYC_Slicer *slicer,
1802 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1803 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1806 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1807 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1809 plc->ego_pub_key = ego->pub_key;
1810 plc->pub_key = *place_pub_key;
1811 plc->cfg = app->cfg;
1812 plc->is_host = GNUNET_NO;
1813 plc->slicer = slicer;
1815 plc->op = GNUNET_OP_create ();
1818 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1819 origin, relay_count, relays, entry_msg);
1821 gst->enter_cb = local_enter_cb;
1822 gst->entry_dcsn_cb = entry_dcsn_cb;
1825 guest_connect (gst);
1831 * Request entry to a place by name as a guest.
1834 * Application handle.
1836 * Identity of the guest.
1838 * GNS name of the place to enter. Either in the form of
1839 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1840 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1841 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1842 * pseudonym's place directly.
1844 * Password to decrypt the record, or NULL for cleartext records.
1846 * Entry request message or NULL.
1848 * Slicer to use for processing incoming requests from guests.
1849 * @param local_enter_cb
1850 * Called upon connection established to the social service.
1851 * @param entry_decision_cb
1852 * Called upon receiving entry decision.
1854 * @return NULL on errors, otherwise handle for the guest.
1856 struct GNUNET_SOCIAL_Guest *
1857 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1858 const struct GNUNET_SOCIAL_Ego *ego,
1859 const char *gns_name,
1860 const char *password,
1861 const struct GNUNET_PSYC_Message *join_msg,
1862 struct GNUNET_PSYC_Slicer *slicer,
1863 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1864 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1867 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1868 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1870 if (NULL == password)
1873 uint16_t app_id_size = strlen (app->id) + 1;
1874 uint16_t gns_name_size = strlen (gns_name) + 1;
1875 uint16_t password_size = strlen (password) + 1;
1877 uint16_t join_msg_size = 0;
1878 if (NULL != join_msg)
1879 join_msg_size = ntohs (join_msg->header.size);
1881 struct GuestEnterByNameRequest *greq;
1883 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1884 + password_size + join_msg_size,
1885 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1887 greq->ego_pub_key = ego->pub_key;
1889 char *p = (char *) &greq[1];
1890 GNUNET_memcpy (p, app->id, app_id_size);
1892 GNUNET_memcpy (p, gns_name, gns_name_size);
1894 GNUNET_memcpy (p, password, password_size);
1896 if (NULL != join_msg)
1897 GNUNET_memcpy (p, join_msg, join_msg_size);
1899 plc->ego_pub_key = ego->pub_key;
1900 plc->cfg = app->cfg;
1901 plc->is_host = GNUNET_NO;
1902 plc->slicer = slicer;
1904 plc->op = GNUNET_OP_create ();
1906 gst->enter_cb = local_enter_cb;
1907 gst->entry_dcsn_cb = entry_decision_cb;
1910 guest_connect (gst);
1916 * Reconnect to an already entered place as guest.
1919 * Guest connection handle.
1920 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1922 * Flags for the entry.
1924 * Slicer to use for processing incoming requests from guests.
1925 * @param local_enter_cb
1926 * Called upon connection established to the social service.
1927 * @param entry_decision_cb
1928 * Called upon receiving entry decision.
1930 * @return NULL on errors, otherwise handle for the guest.
1932 struct GNUNET_SOCIAL_Guest *
1933 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1934 enum GNUNET_PSYC_SlaveJoinFlags flags,
1935 struct GNUNET_PSYC_Slicer *slicer,
1936 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1939 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1940 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1942 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1943 struct GuestEnterRequest *greq;
1945 = GNUNET_MQ_msg_extra (greq, app_id_size,
1946 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1947 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1948 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1949 greq->flags = htonl (flags);
1951 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1953 plc->cfg = gconn->app->cfg;
1954 plc->is_host = GNUNET_NO;
1955 plc->slicer = slicer;
1956 plc->pub_key = gconn->plc_msg.place_pub_key;
1957 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1959 plc->op = GNUNET_OP_create ();
1961 gst->enter_cb = local_enter_cb;
1964 guest_connect (gst);
1970 * Talk to the host of the place.
1973 * Place where we want to talk to the host.
1974 * @param method_name
1975 * Method to invoke on the host.
1977 * Environment containing variables for the message, or NULL.
1978 * @param notify_data
1979 * Function to use to get the payload for the method.
1980 * @param notify_data_cls
1981 * Closure for @a notify_data.
1983 * Flags for the message being sent.
1985 * @return NULL if we are already trying to talk to the host,
1986 * otherwise handle to cancel the request.
1988 struct GNUNET_SOCIAL_TalkRequest *
1989 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1990 const char *method_name,
1991 const struct GNUNET_PSYC_Environment *env,
1992 GNUNET_PSYC_TransmitNotifyData notify_data,
1993 void *notify_data_cls,
1994 enum GNUNET_SOCIAL_TalkFlags flags)
1996 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1997 "PSYC_transmit_message for guest\n");
1999 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2000 GNUNET_assert (NULL != plc->tmit);
2003 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2004 NULL, notify_data, notify_data_cls, flags))
2005 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2012 * Resume talking to the host of the place.
2015 * Talk request to resume.
2018 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2020 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2025 * Cancel talking to the host of the place.
2028 * Talk request to cancel.
2031 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2033 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2038 * Disconnect from a place.
2040 * Invalidates guest handle.
2043 * The guest to disconnect.
2046 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2047 GNUNET_ContinuationCallback disconnect_cb,
2050 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2052 plc->disconnect_cb = disconnect_cb;
2053 plc->disconnect_cls = cls;
2054 place_disconnect (plc);
2059 * Leave a place temporarily or permanently.
2061 * Notifies the owner of the place about leaving, and destroys the place handle.
2065 * @param keep_active
2066 * Keep place active after last application disconnected.
2067 * #GNUNET_YES or #GNUNET_NO
2069 * Optional environment for the leave message if @a keep_active
2070 * is #GNUNET_NO. NULL if not needed.
2072 * Called upon disconnecting from the social service.
2075 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2076 struct GNUNET_PSYC_Environment *env,
2077 GNUNET_ContinuationCallback disconnect_cb,
2080 struct GNUNET_MQ_Envelope *envelope;
2082 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2083 GNUNET_SOCIAL_TALK_NONE);
2084 gst->plc.disconnect_cb = disconnect_cb;
2085 gst->plc.disconnect_cls = cls;
2086 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
2087 GNUNET_MQ_send (gst->plc.mq,
2093 * Obtain handle for a place entered as guest.
2095 * The returned handle can be used to access the place API.
2097 * @param guest Handle for the guest.
2099 * @return Handle for the place, valid as long as @a guest is valid.
2101 struct GNUNET_SOCIAL_Place *
2102 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2109 * Obtain the public key of a place.
2114 * @return Public key of the place.
2116 const struct GNUNET_CRYPTO_EddsaPublicKey *
2117 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2119 return &plc->pub_key;
2124 * Set message processing @a flags for a @a method_prefix.
2128 * @param method_prefix
2129 * Method prefix @a flags apply to.
2131 * The flags that apply to a matching @a method_prefix.
2134 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2135 const char *method_prefix,
2136 enum GNUNET_SOCIAL_MsgProcFlags flags)
2138 GNUNET_assert (NULL != method_prefix);
2139 struct MsgProcRequest *mpreq;
2140 uint16_t method_size = strnlen (method_prefix,
2141 GNUNET_MAX_MESSAGE_SIZE
2142 - sizeof (*mpreq)) + 1;
2143 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2145 struct GNUNET_MQ_Envelope *
2146 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2147 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2148 mpreq->flags = htonl (flags);
2149 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2151 GNUNET_MQ_send (plc->mq, env);
2156 * Clear all message processing flags previously set for this place.
2159 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2161 struct GNUNET_MessageHeader *req;
2162 struct GNUNET_MQ_Envelope *
2163 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2165 GNUNET_MQ_send (plc->mq, env);
2169 static struct GNUNET_SOCIAL_HistoryRequest *
2170 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2171 uint64_t start_message_id,
2172 uint64_t end_message_id,
2173 uint64_t message_limit,
2174 const char *method_prefix,
2176 struct GNUNET_PSYC_Slicer *slicer,
2177 GNUNET_ResultCallback result_cb,
2180 struct GNUNET_PSYC_HistoryRequestMessage *req;
2181 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2183 hist->slicer = slicer;
2184 hist->result_cb = result_cb;
2186 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2188 GNUNET_assert (NULL != method_prefix);
2189 uint16_t method_size = strnlen (method_prefix,
2190 GNUNET_MAX_MESSAGE_SIZE
2191 - sizeof (*req)) + 1;
2192 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2194 struct GNUNET_MQ_Envelope *
2195 env = GNUNET_MQ_msg_extra (req, method_size,
2196 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2197 req->start_message_id = GNUNET_htonll (start_message_id);
2198 req->end_message_id = GNUNET_htonll (end_message_id);
2199 req->message_limit = GNUNET_htonll (message_limit);
2200 req->flags = htonl (flags);
2201 req->op_id = GNUNET_htonll (hist->op_id);
2202 GNUNET_memcpy (&req[1], method_prefix, method_size);
2204 GNUNET_MQ_send (plc->mq, env);
2210 * Learn about the history of a place.
2212 * Messages are returned through the @a slicer function
2213 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2216 * Place we want to learn more about.
2217 * @param start_message_id
2218 * First historic message we are interested in.
2219 * @param end_message_id
2220 * Last historic message we are interested in (inclusive).
2221 * @param method_prefix
2222 * Only retrieve messages with this method prefix.
2224 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2226 * Slicer to use for retrieved messages.
2227 * Can be the same as the slicer of the place.
2229 * Function called after all messages retrieved.
2230 * NULL if not needed.
2231 * @param cls Closure for @a result_cb.
2233 struct GNUNET_SOCIAL_HistoryRequest *
2234 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2235 uint64_t start_message_id,
2236 uint64_t end_message_id,
2237 const char *method_prefix,
2239 struct GNUNET_PSYC_Slicer *slicer,
2240 GNUNET_ResultCallback result_cb,
2243 return place_history_replay (plc, start_message_id, end_message_id, 0,
2244 method_prefix, flags, slicer, result_cb, cls);
2249 * Learn about the history of a place.
2251 * Sends messages through the slicer function of the place where
2252 * start_message_id <= message_id <= end_message_id.
2253 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2255 * To get the latest message, use 0 for both the start and end message ID.
2258 * Place we want to learn more about.
2259 * @param message_limit
2260 * Maximum number of historic messages we are interested in.
2261 * @param method_prefix
2262 * Only retrieve messages with this method prefix.
2264 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2266 * Function called after all messages retrieved.
2267 * NULL if not needed.
2268 * @param cls Closure for @a result_cb.
2270 struct GNUNET_SOCIAL_HistoryRequest *
2271 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2272 uint64_t message_limit,
2273 const char *method_prefix,
2275 struct GNUNET_PSYC_Slicer *slicer,
2276 GNUNET_ResultCallback result_cb,
2279 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2280 slicer, result_cb, cls);
2285 * Cancel learning about the history of a place.
2288 * History lesson to cancel.
2291 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2293 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2299 * Request matching state variables.
2301 static struct GNUNET_SOCIAL_LookHandle *
2302 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2303 uint16_t type, const char *name,
2304 GNUNET_PSYC_StateVarCallback var_cb,
2305 GNUNET_ResultCallback result_cb, void *cls)
2307 struct GNUNET_PSYC_StateRequestMessage *req;
2308 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2310 look->var_cb = var_cb;
2311 look->result_cb = result_cb;
2313 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2315 GNUNET_assert (NULL != name);
2316 size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
2317 - sizeof (*req)) + 1;
2318 struct GNUNET_MQ_Envelope *
2319 env = GNUNET_MQ_msg_extra (req, name_size, type);
2320 req->op_id = GNUNET_htonll (look->op_id);
2321 GNUNET_memcpy (&req[1], name, name_size);
2323 GNUNET_MQ_send (plc->mq, env);
2329 * Look at a particular object in the place.
2331 * The best matching object is returned (its name might be less specific than
2332 * what was requested).
2335 * The place where to look.
2337 * Full name of the object.
2339 * Set to the size of the returned value.
2341 * @return NULL if there is no such object at this place.
2343 struct GNUNET_SOCIAL_LookHandle *
2344 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2345 const char *full_name,
2346 GNUNET_PSYC_StateVarCallback var_cb,
2347 GNUNET_ResultCallback result_cb,
2350 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2351 full_name, var_cb, result_cb, cls);
2356 * Look for objects in the place with a matching name prefix.
2359 * The place where to look.
2360 * @param name_prefix
2361 * Look at objects with names beginning with this value.
2363 * Function to call for each object found.
2365 * Closure for callback function.
2367 * @return Handle that can be used to stop looking at objects.
2369 struct GNUNET_SOCIAL_LookHandle *
2370 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2371 const char *name_prefix,
2372 GNUNET_PSYC_StateVarCallback var_cb,
2373 GNUNET_ResultCallback result_cb,
2376 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2377 name_prefix, var_cb, result_cb, cls);
2382 * Cancel a state request operation.
2385 * Handle for the operation to cancel.
2388 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2390 GNUNET_OP_remove (look->plc->op, look->op_id);
2396 op_recv_zone_add_place_result (void *cls, int64_t result,
2397 const void *err_msg, uint16_t err_msg_size)
2399 LOG (GNUNET_ERROR_TYPE_DEBUG,
2400 "Received zone add place result: %" PRId64 ".\n", result);
2402 struct ZoneAddPlaceHandle *add_plc = cls;
2403 if (NULL != add_plc->result_cb)
2404 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2406 GNUNET_free (add_plc);
2411 * Advertise @e place in the GNS zone of @e ego.
2414 * Application handle.
2417 * @param place_pub_key
2418 * Public key of place to add.
2420 * The name for the PLACE record to put in the zone.
2422 * Password used to encrypt the record or NULL to keep it cleartext.
2423 * @param relay_count
2424 * Number of elements in the @a relays array.
2426 * List of relays to put in the PLACE record to advertise
2427 * as entry points to the place in addition to the origin.
2428 * @param expiration_time
2429 * Expiration time of the record, use 0 to remove the record.
2431 * Function called with the result of the operation.
2433 * Closure for @a result_cb
2435 * @return #GNUNET_OK if the request was sent,
2436 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2439 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2440 const struct GNUNET_SOCIAL_Ego *ego,
2442 const char *password,
2443 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2444 const struct GNUNET_PeerIdentity *origin,
2445 uint32_t relay_count,
2446 const struct GNUNET_PeerIdentity *relays,
2447 struct GNUNET_TIME_Absolute expiration_time,
2448 GNUNET_ResultCallback result_cb,
2451 struct ZoneAddPlaceRequest *preq;
2452 size_t name_size = strlen (name) + 1;
2453 size_t password_size = strlen (password) + 1;
2454 size_t relay_size = relay_count * sizeof (*relays);
2455 size_t payload_size = name_size + password_size + relay_size;
2457 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2458 return GNUNET_SYSERR;
2460 struct GNUNET_MQ_Envelope *
2461 env = GNUNET_MQ_msg_extra (preq, payload_size,
2462 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2463 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2464 preq->ego_pub_key = ego->pub_key;
2465 preq->place_pub_key = *place_pub_key;
2466 preq->origin = *origin;
2467 preq->relay_count = htonl (relay_count);
2469 char *p = (char *) &preq[1];
2470 GNUNET_memcpy (p, name, name_size);
2472 GNUNET_memcpy (p, password, password_size);
2474 GNUNET_memcpy (p, relays, relay_size);
2476 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2477 add_plc->result_cb = result_cb;
2478 add_plc->result_cls = result_cls;
2480 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2481 op_recv_zone_add_place_result,
2484 GNUNET_MQ_send (app->mq, env);
2490 op_recv_zone_add_nym_result (void *cls, int64_t result,
2491 const void *err_msg, uint16_t err_msg_size)
2493 LOG (GNUNET_ERROR_TYPE_DEBUG,
2494 "Received zone add nym result: %" PRId64 ".\n", result);
2496 struct ZoneAddNymHandle *add_nym = cls;
2497 if (NULL != add_nym->result_cb)
2498 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2500 GNUNET_free (add_nym);
2505 * Add nym to the GNS zone of @e ego.
2512 * The name for the PKEY record to put in the zone.
2513 * @param nym_pub_key
2514 * Public key of nym to add.
2515 * @param expiration_time
2516 * Expiration time of the record, use 0 to remove the record.
2518 * Function called with the result of the operation.
2520 * Closure for @a result_cb
2522 * @return #GNUNET_OK if the request was sent,
2523 * #GNUNET_SYSERR on error, e.g. the name is too long.
2526 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2527 const struct GNUNET_SOCIAL_Ego *ego,
2529 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2530 struct GNUNET_TIME_Absolute expiration_time,
2531 GNUNET_ResultCallback result_cb,
2534 struct ZoneAddNymRequest *nreq;
2536 size_t name_size = strlen (name) + 1;
2537 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2538 return GNUNET_SYSERR;
2540 struct GNUNET_MQ_Envelope *
2541 env = GNUNET_MQ_msg_extra (nreq, name_size,
2542 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2543 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2544 nreq->ego_pub_key = ego->pub_key;
2545 nreq->nym_pub_key = *nym_pub_key;
2546 GNUNET_memcpy (&nreq[1], name, name_size);
2548 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2549 add_nym->result_cb = result_cb;
2550 add_nym->result_cls = result_cls;
2552 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2553 op_recv_zone_add_nym_result,
2556 GNUNET_MQ_send (app->mq, env);
2565 app_connect (struct GNUNET_SOCIAL_App *app);
2569 app_reconnect (void *cls)
2576 * App client disconnected from service.
2578 * Reconnect after backoff period.
2581 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2583 struct GNUNET_SOCIAL_App *app = cls;
2585 LOG (GNUNET_ERROR_TYPE_DEBUG,
2586 "App client disconnected (%d), re-connecting\n",
2588 if (NULL != app->mq)
2590 GNUNET_MQ_destroy (app->mq);
2594 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2597 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2602 app_connect (struct GNUNET_SOCIAL_App *app)
2604 struct GNUNET_MQ_MessageHandler handlers[] = {
2605 GNUNET_MQ_hd_var_size (app_ego,
2606 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2607 struct AppEgoMessage,
2609 GNUNET_MQ_hd_fixed_size (app_ego_end,
2610 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2611 struct GNUNET_MessageHeader,
2613 GNUNET_MQ_hd_var_size (app_place,
2614 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2615 struct AppPlaceMessage,
2617 GNUNET_MQ_hd_fixed_size (app_place_end,
2618 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2619 struct GNUNET_MessageHeader,
2621 GNUNET_MQ_hd_var_size (app_result,
2622 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2623 struct GNUNET_OperationResultMessage,
2625 GNUNET_MQ_handler_end ()
2628 app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
2629 handlers, app_disconnected, app);
2630 GNUNET_assert (NULL != app->mq);
2631 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2636 * Connect application to the social service.
2638 * The @host_place_cb and @guest_place_cb functions are
2639 * initially called for each entered places,
2640 * then later each time a new place is entered with the current application ID.
2647 * Function to notify about an available ego.
2649 * Function to notify about a place entered as host.
2651 * Function to notify about a place entered as guest.
2653 * Closure for the callbacks.
2655 * @return Handle that can be used to stop listening.
2657 struct GNUNET_SOCIAL_App *
2658 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2660 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2661 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2662 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2663 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2666 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2667 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2671 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2673 app->ego_cb = ego_cb;
2674 app->host_cb = host_cb;
2675 app->guest_cb = guest_cb;
2676 app->connected_cb = connected_cb;
2678 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2679 app->op = GNUNET_OP_create ();
2680 app->id = GNUNET_malloc (app_id_size);
2681 GNUNET_memcpy (app->id, id, app_id_size);
2683 struct AppConnectRequest *creq;
2684 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2685 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2686 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2694 app_cleanup (struct GNUNET_SOCIAL_App *app)
2696 if (NULL != app->mq)
2698 GNUNET_MQ_destroy (app->mq);
2701 if (NULL != app->disconnect_cb)
2703 app->disconnect_cb (app->disconnect_cls);
2704 app->disconnect_cb = NULL;
2710 * Disconnect application.
2713 * Application handle.
2714 * @param disconnect_cb
2715 * Disconnect callback.
2716 * @param disconnect_cls
2717 * Disconnect closure.
2720 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2721 GNUNET_ContinuationCallback disconnect_cb,
2722 void *disconnect_cls)
2724 app->disconnect_cb = disconnect_cb;
2725 app->disconnect_cls = disconnect_cls;
2727 if (NULL != app->mq)
2729 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2732 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
2747 * Detach application from a place.
2749 * Removes the place from the entered places list for this application.
2750 * Note: this does not disconnect from the place.
2752 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2760 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2761 struct GNUNET_SOCIAL_Place *plc)
2763 struct AppDetachRequest *dreq;
2764 struct GNUNET_MQ_Envelope *
2765 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2766 dreq->place_pub_key = plc->pub_key;
2767 dreq->ego_pub_key = plc->ego_pub_key;
2769 GNUNET_MQ_send (app->mq, env);
2773 /* end of social_api.c */