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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "cleaning up place %p\n",
403 if (NULL != plc->tmit)
405 GNUNET_PSYC_transmit_destroy (plc->tmit);
408 if (NULL != plc->connect_env)
410 GNUNET_MQ_discard (plc->connect_env);
411 plc->connect_env = NULL;
415 GNUNET_MQ_destroy (plc->mq);
418 if (NULL != plc->disconnect_cb)
420 plc->disconnect_cb (plc->disconnect_cls);
421 plc->disconnect_cb = NULL;
424 (GNUNET_YES == plc->is_host)
425 ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
426 : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
431 place_disconnect (struct GNUNET_SOCIAL_Place *plc)
439 static struct GNUNET_SOCIAL_Nym *
440 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
442 struct GNUNET_SOCIAL_Nym *nym = NULL;
443 struct GNUNET_HashCode pub_key_hash;
448 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
451 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
453 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
457 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
458 nym->pub_key = *pub_key;
459 nym->pub_key_hash = pub_key_hash;
460 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
461 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
468 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
470 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
475 /*** MESSAGE HANDLERS ***/
477 /** _notice_place_leave from guests */
480 host_recv_notice_place_leave_method (void *cls,
481 const struct GNUNET_PSYC_MessageHeader *msg,
482 const struct GNUNET_PSYC_MessageMethod *meth,
484 const char *method_name)
486 struct GNUNET_SOCIAL_Host *hst = cls;
488 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
489 &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
492 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
496 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
498 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
499 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
501 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "_notice_place_leave: got method from nym %s (%s).\n",
504 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
510 host_recv_notice_place_leave_modifier (void *cls,
511 const struct GNUNET_PSYC_MessageHeader *msg,
512 const struct GNUNET_MessageHeader *pmsg,
514 enum GNUNET_PSYC_Operator oper,
518 uint16_t full_value_size)
520 struct GNUNET_SOCIAL_Host *hst = cls;
521 if (NULL == hst->notice_place_leave_env)
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
527 message_id, oper, name, value_size, (const char *) value);
529 /* skip _nym, it's added later in eom() */
530 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
531 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
534 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
535 GNUNET_PSYC_OP_SET, name, value, value_size);
540 host_recv_notice_place_leave_eom (void *cls,
541 const struct GNUNET_PSYC_MessageHeader *msg,
542 const struct GNUNET_MessageHeader *pmsg,
544 uint8_t is_cancelled)
546 struct GNUNET_SOCIAL_Host *hst = cls;
547 if (NULL == hst->notice_place_leave_env)
550 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "_notice_place_leave: got EOM from nym %s (%s).\n",
553 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
556 if (GNUNET_YES != is_cancelled)
558 if (NULL != hst->farewell_cb)
559 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
560 hst->notice_place_leave_env);
561 /* announce leaving guest to place */
562 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
563 "_nym", hst->notice_place_leave_nym,
564 sizeof (*hst->notice_place_leave_nym));
565 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
566 hst->notice_place_leave_env,
567 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
568 nym_destroy (hst->notice_place_leave_nym);
570 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
571 hst->notice_place_leave_env = NULL;
579 check_place_result (void *cls,
580 const struct GNUNET_OperationResultMessage *res)
582 uint16_t size = ntohs (res->header.size);
583 if (size < sizeof (*res))
584 { /* Error, message too small. */
586 return GNUNET_SYSERR;
593 handle_place_result (void *cls,
594 const struct GNUNET_OperationResultMessage *res)
596 struct GNUNET_SOCIAL_Place *plc = cls;
598 uint16_t size = ntohs (res->header.size);
599 uint16_t data_size = size - sizeof (*res);
600 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
602 GNUNET_OP_result (plc->op, GNUNET_ntohll (res->op_id),
603 GNUNET_ntohll (res->result_code),
604 data, data_size, NULL);
609 check_app_result (void *cls,
610 const struct GNUNET_OperationResultMessage *res)
612 uint16_t size = ntohs (res->header.size);
613 if (size < sizeof (*res))
614 { /* Error, message too small. */
616 return GNUNET_SYSERR;
623 handle_app_result (void *cls,
624 const struct GNUNET_OperationResultMessage *res)
626 struct GNUNET_SOCIAL_App *app = cls;
628 uint16_t size = ntohs (res->header.size);
629 uint16_t data_size = size - sizeof (*res);
630 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
632 GNUNET_OP_result (app->op, GNUNET_ntohll (res->op_id),
633 GNUNET_ntohll (res->result_code),
634 data, data_size, NULL);
639 op_recv_history_result (void *cls, int64_t result,
640 const void *err_msg, uint16_t err_msg_size)
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "Received history replay result: %" PRId64 ".\n", result);
645 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
647 if (NULL != hist->result_cb)
648 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
655 op_recv_state_result (void *cls, int64_t result,
656 const void *err_msg, uint16_t err_msg_size)
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Received state request result: %" PRId64 ".\n", result);
661 struct GNUNET_SOCIAL_LookHandle *look = cls;
663 if (NULL != look->result_cb)
664 look->result_cb (look->cls, result, err_msg, err_msg_size);
671 check_place_history_result (void *cls,
672 const struct GNUNET_OperationResultMessage *res)
674 struct GNUNET_PSYC_MessageHeader *
675 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
676 uint16_t size = ntohs (res->header.size);
678 if (NULL == pmsg || size < sizeof (*res) + sizeof (*pmsg))
679 { /* Error, message too small. */
681 return GNUNET_SYSERR;
688 handle_place_history_result (void *cls,
689 const struct GNUNET_OperationResultMessage *res)
691 struct GNUNET_SOCIAL_Place *plc = cls;
692 struct GNUNET_PSYC_MessageHeader *
693 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "%p Received historic fragment for message #%" PRIu64 ".\n",
697 plc, GNUNET_ntohll (pmsg->message_id));
699 GNUNET_ResultCallback result_cb = NULL;
700 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
702 if (GNUNET_YES != GNUNET_OP_get (plc->op,
703 GNUNET_ntohll (res->op_id),
704 &result_cb, (void *) &hist, NULL))
705 { /* Operation not found. */
706 LOG (GNUNET_ERROR_TYPE_WARNING,
707 "%p Replay operation not found for historic fragment of message #%"
709 plc, GNUNET_ntohll (pmsg->message_id));
713 GNUNET_PSYC_slicer_message (hist->slicer,
714 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
719 check_place_state_result (void *cls,
720 const struct GNUNET_OperationResultMessage *res)
722 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
726 LOG (GNUNET_ERROR_TYPE_WARNING,
727 "Invalid modifier in state result\n");
728 return GNUNET_SYSERR;
731 uint16_t size = ntohs (res->header.size);
732 uint16_t mod_size = ntohs (mod->size);
733 if (size - sizeof (*res) != mod_size)
736 LOG (GNUNET_ERROR_TYPE_WARNING,
737 "Invalid modifier size in state result: %u - %u != %u\n",
738 ntohs (res->header.size), sizeof (*res), mod_size);
739 return GNUNET_SYSERR;
746 handle_place_state_result (void *cls,
747 const struct GNUNET_OperationResultMessage *res)
749 struct GNUNET_SOCIAL_Place *plc = cls;
751 GNUNET_ResultCallback result_cb = NULL;
752 struct GNUNET_SOCIAL_LookHandle *look = NULL;
754 if (GNUNET_YES != GNUNET_OP_get (plc->op,
755 GNUNET_ntohll (res->op_id),
756 &result_cb, (void *) &look, NULL))
757 { /* Operation not found. */
761 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
762 uint16_t mod_size = ntohs (mod->size);
764 switch (ntohs (mod->type))
766 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
768 const struct GNUNET_PSYC_MessageModifier *
769 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
771 const char *name = (const char *) &pmod[1];
772 uint16_t name_size = ntohs (pmod->name_size);
774 || mod_size - sizeof (*pmod) < name_size
775 || '\0' != name[name_size - 1])
778 LOG (GNUNET_ERROR_TYPE_WARNING,
779 "Invalid modifier name in state result\n");
782 look->mod_value_size = ntohs (pmod->value_size);
783 look->var_cb (look->cls, mod, name, name + name_size,
784 mod_size - sizeof (*mod) - name_size,
785 look->mod_value_size);
786 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
788 look->mod_value_remaining = look->mod_value_size;
789 look->mod_name = GNUNET_malloc (name_size);
790 GNUNET_memcpy (look->mod_name, name, name_size);
795 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
796 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
797 mod_size - sizeof (*mod), look->mod_value_size);
798 look->mod_value_remaining -= mod_size - sizeof (*mod);
799 if (0 == look->mod_value_remaining)
801 GNUNET_free (look->mod_name);
809 handle_place_message_ack (void *cls,
810 const struct GNUNET_MessageHeader *msg)
812 struct GNUNET_SOCIAL_Place *plc = cls;
814 GNUNET_PSYC_transmit_got_ack (plc->tmit);
819 check_place_message (void *cls,
820 const struct GNUNET_PSYC_MessageHeader *pmsg)
827 handle_place_message (void *cls,
828 const struct GNUNET_PSYC_MessageHeader *pmsg)
830 struct GNUNET_SOCIAL_Place *plc = cls;
832 GNUNET_PSYC_slicer_message (plc->slicer, pmsg);
837 check_host_message (void *cls,
838 const struct GNUNET_PSYC_MessageHeader *pmsg)
845 handle_host_message (void *cls,
846 const struct GNUNET_PSYC_MessageHeader *pmsg)
848 struct GNUNET_SOCIAL_Host *hst = cls;
850 GNUNET_PSYC_slicer_message (hst->slicer, pmsg);
851 GNUNET_PSYC_slicer_message (hst->plc.slicer, pmsg);
856 handle_host_enter_ack (void *cls,
857 const struct HostEnterAck *hack)
859 struct GNUNET_SOCIAL_Host *hst = cls;
861 hst->plc.pub_key = hack->place_pub_key;
863 int32_t result = ntohl (hack->result_code);
864 if (NULL != hst->enter_cb)
865 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
866 GNUNET_ntohll (hack->max_message_id));
871 check_host_enter_request (void *cls,
872 const struct GNUNET_PSYC_JoinRequestMessage *req)
879 handle_host_enter_request (void *cls,
880 const struct GNUNET_PSYC_JoinRequestMessage *req)
882 struct GNUNET_SOCIAL_Host *hst = cls;
884 if (NULL == hst->answer_door_cb)
887 const char *method_name = NULL;
888 struct GNUNET_PSYC_Environment *env = NULL;
889 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
890 const void *data = NULL;
891 uint16_t data_size = 0;
893 const struct GNUNET_PSYC_Message *join_msg = NULL;
897 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
899 join_msg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (req);
900 LOG (GNUNET_ERROR_TYPE_DEBUG,
901 "Received join_msg of type %u and size %u.\n",
902 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
904 env = GNUNET_PSYC_env_create ();
905 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
906 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
910 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
911 LOG (GNUNET_ERROR_TYPE_WARNING,
912 "Ignoring invalid entry request from nym %s.\n",
919 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
920 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
925 GNUNET_PSYC_env_destroy (env);
926 if (NULL != entry_pmsg)
927 GNUNET_free (entry_pmsg);
932 handle_guest_enter_ack (void *cls,
933 const struct GNUNET_PSYC_CountersResultMessage *cres)
935 struct GNUNET_SOCIAL_Guest *gst = cls;
937 int32_t result = ntohl (cres->result_code);
938 if (NULL != gst->enter_cb)
939 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
940 GNUNET_ntohll (cres->max_message_id));
945 check_guest_enter_decision (void *cls,
946 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
953 handle_guest_enter_decision (void *cls,
954 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
956 struct GNUNET_SOCIAL_Guest *gst = cls;
958 struct GNUNET_PSYC_Message *pmsg = NULL;
959 if (ntohs (dcsn->header.size) > sizeof (*dcsn))
960 pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
962 if (NULL != gst->entry_dcsn_cb)
963 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
968 check_app_ego (void *cls,
969 const struct AppEgoMessage *emsg)
976 handle_app_ego (void *cls,
977 const struct AppEgoMessage *emsg)
979 struct GNUNET_SOCIAL_App *app = cls;
981 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
983 struct GNUNET_HashCode ego_pub_hash;
984 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
987 struct GNUNET_SOCIAL_Ego *
988 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
991 ego = GNUNET_malloc (sizeof (*ego));
992 ego->pub_key = emsg->ego_pub_key;
993 ego->name = GNUNET_malloc (name_size);
994 GNUNET_memcpy (ego->name, &emsg[1], name_size);
998 ego->name = GNUNET_realloc (ego->name, name_size);
999 GNUNET_memcpy (ego->name, &emsg[1], name_size);
1002 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
1003 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1005 if (NULL != app->ego_cb)
1006 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
1011 handle_app_ego_end (void *cls,
1012 const struct GNUNET_MessageHeader *msg)
1014 //struct GNUNET_SOCIAL_App *app = cls;
1019 check_app_place (void *cls,
1020 const struct AppPlaceMessage *pmsg)
1027 handle_app_place (void *cls,
1028 const struct AppPlaceMessage *pmsg)
1030 struct GNUNET_SOCIAL_App *app = cls;
1032 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
1033 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
1036 struct GNUNET_HashCode ego_pub_hash;
1037 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
1039 struct GNUNET_SOCIAL_Ego *
1040 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
1043 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failure to obtain ego %s.\n",
1044 GNUNET_h2s (&ego_pub_hash));
1049 if (GNUNET_YES == pmsg->is_host)
1051 if (NULL != app->host_cb) {
1052 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
1054 hconn->plc_msg = *pmsg;
1055 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1056 GNUNET_free (hconn);
1059 else if (NULL != app->guest_cb)
1061 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
1063 gconn->plc_msg = *pmsg;
1064 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1065 GNUNET_free (gconn);
1071 handle_app_place_end (void *cls,
1072 const struct GNUNET_MessageHeader *msg)
1074 struct GNUNET_SOCIAL_App *app = cls;
1076 if (NULL != app->connected_cb)
1077 app->connected_cb (app->cb_cls);
1082 * Handler for a #GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK message received
1083 * from the social service.
1085 * @param cls the place of type `struct GNUNET_SOCIAL_Place`
1086 * @param msg the message received from the service
1089 handle_place_leave_ack (void *cls,
1090 const struct GNUNET_MessageHeader *msg)
1092 struct GNUNET_SOCIAL_Place *plc = cls;
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "%s left place %p\n",
1096 plc->is_host ? "host" : "guest",
1098 place_disconnect (plc);
1106 host_connect (struct GNUNET_SOCIAL_Host *hst);
1110 host_reconnect (void *cls)
1117 * Host client disconnected from service.
1119 * Reconnect after backoff period.
1122 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1124 struct GNUNET_SOCIAL_Host *hst = cls;
1125 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1127 LOG (GNUNET_ERROR_TYPE_DEBUG,
1128 "Host client disconnected (%d), re-connecting\n",
1130 if (NULL != plc->tmit)
1132 GNUNET_PSYC_transmit_destroy (plc->tmit);
1135 if (NULL != plc->mq)
1137 GNUNET_MQ_destroy (plc->mq);
1141 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1144 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1149 host_connect (struct GNUNET_SOCIAL_Host *hst)
1151 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1153 struct GNUNET_MQ_MessageHandler handlers[] = {
1154 GNUNET_MQ_hd_fixed_size (host_enter_ack,
1155 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1156 struct HostEnterAck,
1158 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1159 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1160 struct GNUNET_MessageHeader,
1162 GNUNET_MQ_hd_var_size (host_enter_request,
1163 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1164 struct GNUNET_PSYC_JoinRequestMessage,
1166 GNUNET_MQ_hd_var_size (host_message,
1167 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1168 struct GNUNET_PSYC_MessageHeader,
1170 GNUNET_MQ_hd_fixed_size (place_message_ack,
1171 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1172 struct GNUNET_MessageHeader,
1174 GNUNET_MQ_hd_var_size (place_history_result,
1175 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1176 struct GNUNET_OperationResultMessage,
1178 GNUNET_MQ_hd_var_size (place_state_result,
1179 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1180 struct GNUNET_OperationResultMessage,
1182 GNUNET_MQ_hd_var_size (place_result,
1183 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1184 struct GNUNET_OperationResultMessage,
1186 GNUNET_MQ_handler_end ()
1189 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1190 handlers, host_disconnected, hst);
1191 GNUNET_assert (NULL != plc->mq);
1192 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1194 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1199 * Enter a place as host.
1201 * A place is created upon first entering, and it is active until permanently
1202 * left using GNUNET_SOCIAL_host_leave().
1205 * Application handle.
1207 * Identity of the host.
1209 * Policy specifying entry and history restrictions for the place.
1211 * Slicer to handle incoming messages.
1213 * Function called when the place is entered and ready to use.
1214 * @param answer_door_cb
1215 * Function to handle new nyms that want to enter.
1216 * @param farewell_cb
1217 * Function to handle departing nyms.
1219 * Closure for the callbacks.
1221 * @return Handle for the host. This handle contains the pubkey.
1223 struct GNUNET_SOCIAL_Host *
1224 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1225 const struct GNUNET_SOCIAL_Ego *ego,
1226 enum GNUNET_PSYC_Policy policy,
1227 struct GNUNET_PSYC_Slicer *slicer,
1228 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1229 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1230 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1233 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1234 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1236 plc->cfg = app->cfg;
1237 plc->is_host = GNUNET_YES;
1238 plc->slicer = slicer;
1240 hst->enter_cb = enter_cb;
1241 hst->answer_door_cb = answer_door_cb;
1242 hst->farewell_cb = farewell_cb;
1245 plc->op = GNUNET_OP_create ();
1247 hst->slicer = GNUNET_PSYC_slicer_create ();
1248 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1249 host_recv_notice_place_leave_method,
1250 host_recv_notice_place_leave_modifier,
1251 NULL, host_recv_notice_place_leave_eom, hst);
1253 uint16_t app_id_size = strlen (app->id) + 1;
1254 struct HostEnterRequest *hreq;
1255 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1256 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1257 hreq->policy = policy;
1258 hreq->ego_pub_key = ego->pub_key;
1259 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1267 * Reconnect to an already entered place as host.
1270 * Host connection handle.
1271 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1273 * Slicer to handle incoming messages.
1275 * Function called when the place is entered and ready to use.
1276 * @param answer_door_cb
1277 * Function to handle new nyms that want to enter.
1278 * @param farewell_cb
1279 * Function to handle departing nyms.
1281 * Closure for the callbacks.
1283 * @return Handle for the host.
1285 struct GNUNET_SOCIAL_Host *
1286 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1287 struct GNUNET_PSYC_Slicer *slicer,
1288 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1289 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1290 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1293 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1294 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1296 hst->enter_cb = enter_cb;
1297 hst->answer_door_cb = answer_door_cb;
1298 hst->farewell_cb = farewell_cb;
1301 plc->cfg = hconn->app->cfg;
1302 plc->is_host = GNUNET_YES;
1303 plc->slicer = slicer;
1304 plc->pub_key = hconn->plc_msg.place_pub_key;
1305 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1307 plc->op = GNUNET_OP_create ();
1309 hst->slicer = GNUNET_PSYC_slicer_create ();
1310 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1311 host_recv_notice_place_leave_method,
1312 host_recv_notice_place_leave_modifier,
1313 NULL, host_recv_notice_place_leave_eom, hst);
1315 size_t app_id_size = strlen (hconn->app->id) + 1;
1316 struct HostEnterRequest *hreq;
1317 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1318 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1319 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1320 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1321 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1329 * Decision whether to admit @a nym into the place or refuse entry.
1332 * Host of the place.
1334 * Handle for the entity that wanted to enter.
1335 * @param is_admitted
1336 * #GNUNET_YES if @a nym is admitted,
1337 * #GNUNET_NO if @a nym is refused entry,
1338 * #GNUNET_SYSERR if we cannot answer the request.
1339 * @param method_name
1340 * Method name for the rejection message.
1342 * Environment containing variables for the message, or NULL.
1344 * Data for the rejection message to send back.
1346 * Number of bytes in @a data for method.
1347 * @return #GNUNET_OK on success,
1348 * #GNUNET_SYSERR if the message is too large.
1351 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1352 struct GNUNET_SOCIAL_Nym *nym,
1354 const struct GNUNET_PSYC_Message *entry_resp)
1356 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1357 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1358 uint16_t entry_resp_size
1359 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1361 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1362 return GNUNET_SYSERR;
1364 struct GNUNET_MQ_Envelope *
1365 env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1366 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1367 dcsn->is_admitted = htonl (is_admitted);
1368 dcsn->slave_pub_key = nym->pub_key;
1370 if (0 < entry_resp_size)
1371 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1373 GNUNET_MQ_send (plc->mq, env);
1379 * Throw @a nym out of the place.
1381 * The @a nym reference will remain valid until the
1382 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1383 * which should be very soon after this call.
1386 * Host of the place.
1388 * Handle for the entity to be ejected.
1390 * Environment for the message or NULL.
1393 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1394 const struct GNUNET_SOCIAL_Nym *nym,
1395 struct GNUNET_PSYC_Environment *e)
1397 struct GNUNET_PSYC_Environment *env = e;
1399 env = GNUNET_PSYC_env_create ();
1400 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1401 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1402 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1403 GNUNET_SOCIAL_ANNOUNCE_NONE);
1405 GNUNET_PSYC_env_destroy (env);
1410 * Get the public key of @a ego.
1415 * @return Public key of ego.
1417 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1418 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1420 return &ego->pub_key;
1425 * Get the hash of the public key of @a ego.
1430 * @return Hash of the public key of @a ego.
1432 const struct GNUNET_HashCode *
1433 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1435 return &ego->pub_key_hash;
1440 * Get the name of @a ego.
1445 * @return Public key of @a ego.
1448 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1455 * Get the public key of @a nym.
1457 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1462 * @return Public key of @a nym.
1464 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1465 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1467 return &nym->pub_key;
1472 * Get the hash of the public key of @a nym.
1477 * @return Hash of the public key of @a nym.
1479 const struct GNUNET_HashCode *
1480 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1482 return &nym->pub_key_hash;
1487 * Send a message to all nyms that are present in the place.
1489 * This function is restricted to the host. Nyms can only send requests
1490 * to the host who can decide to relay it to everyone in the place.
1492 * @param host Host of the place.
1493 * @param method_name Method to use for the announcement.
1494 * @param env Environment containing variables for the message and operations
1495 * on objects of the place. Can be NULL.
1496 * @param notify Function to call to get the payload of the announcement.
1497 * @param notify_cls Closure for @a notify.
1498 * @param flags Flags for this announcement.
1500 * @return NULL on error (announcement already in progress?).
1502 struct GNUNET_SOCIAL_Announcement *
1503 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1504 const char *method_name,
1505 const struct GNUNET_PSYC_Environment *env,
1506 GNUNET_PSYC_TransmitNotifyData notify_data,
1507 void *notify_data_cls,
1508 enum GNUNET_SOCIAL_AnnounceFlags flags)
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "PSYC_transmit_message for host, method: %s\n",
1514 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1515 NULL, notify_data, notify_data_cls, flags))
1516 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1523 * Resume transmitting announcement.
1526 * The announcement to resume.
1529 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1531 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1536 * Cancel announcement.
1539 * The announcement to cancel.
1542 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1544 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1549 * Obtain handle for a hosted place.
1551 * The returned handle can be used to access the place API.
1553 * @param host Handle for the host.
1555 * @return Handle for the hosted place, valid as long as @a host is valid.
1557 struct GNUNET_SOCIAL_Place *
1558 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1565 * Disconnect from a home.
1567 * Invalidates host handle.
1570 * The host to disconnect.
1573 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1574 GNUNET_ContinuationCallback disconnect_cb,
1577 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1579 plc->disconnect_cb = disconnect_cb;
1580 plc->disconnect_cls = cls;
1581 place_disconnect (plc);
1586 * Stop hosting the home.
1588 * Sends a _notice_place_closing announcement to the home.
1589 * Invalidates host handle.
1594 * Environment for the message or NULL.
1595 * _nym is set to @e nym regardless whether an @e env is provided.
1596 * @param disconnect_cb
1597 * Function called after the host left the place
1598 * and disconnected from the social service.
1600 * Closure for @a disconnect_cb.
1603 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1604 const struct GNUNET_PSYC_Environment *env,
1605 GNUNET_ContinuationCallback disconnect_cb,
1608 struct GNUNET_MQ_Envelope *envelope;
1610 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1611 GNUNET_SOCIAL_ANNOUNCE_NONE);
1612 hst->plc.disconnect_cb = disconnect_cb;
1613 hst->plc.disconnect_cls = cls;
1614 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1615 GNUNET_MQ_send (hst->plc.mq,
1624 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1628 guest_reconnect (void *cls)
1630 guest_connect (cls);
1635 * Guest client disconnected from service.
1637 * Reconnect after backoff period.
1640 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1642 struct GNUNET_SOCIAL_Guest *gst = cls;
1643 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1645 LOG (GNUNET_ERROR_TYPE_DEBUG,
1646 "Guest client disconnected (%d), re-connecting\n",
1648 if (NULL != plc->tmit)
1650 GNUNET_PSYC_transmit_destroy (plc->tmit);
1653 if (NULL != plc->mq)
1655 GNUNET_MQ_destroy (plc->mq);
1659 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1662 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1667 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1669 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1671 struct GNUNET_MQ_MessageHandler handlers[] = {
1672 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1673 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1674 struct GNUNET_PSYC_CountersResultMessage,
1676 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1677 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1678 struct GNUNET_MessageHeader,
1680 GNUNET_MQ_hd_var_size (guest_enter_decision,
1681 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1682 struct GNUNET_PSYC_JoinDecisionMessage,
1684 GNUNET_MQ_hd_var_size (place_message,
1685 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1686 struct GNUNET_PSYC_MessageHeader,
1688 GNUNET_MQ_hd_fixed_size (place_message_ack,
1689 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1690 struct GNUNET_MessageHeader,
1692 GNUNET_MQ_hd_var_size (place_history_result,
1693 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1694 struct GNUNET_OperationResultMessage,
1696 GNUNET_MQ_hd_var_size (place_state_result,
1697 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1698 struct GNUNET_OperationResultMessage,
1700 GNUNET_MQ_hd_var_size (place_result,
1701 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1702 struct GNUNET_OperationResultMessage,
1704 GNUNET_MQ_handler_end ()
1707 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1708 handlers, guest_disconnected, gst);
1709 GNUNET_assert (NULL != plc->mq);
1710 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1712 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1716 static struct GNUNET_MQ_Envelope *
1717 guest_enter_request_create (const char *app_id,
1718 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1719 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1720 const struct GNUNET_PeerIdentity *origin,
1722 const struct GNUNET_PeerIdentity *relays,
1723 const struct GNUNET_PSYC_Message *join_msg)
1725 uint16_t app_id_size = strlen (app_id) + 1;
1726 uint16_t join_msg_size = ntohs (join_msg->header.size);
1727 uint16_t relay_size = relay_count * sizeof (*relays);
1729 struct GuestEnterRequest *greq;
1730 struct GNUNET_MQ_Envelope *
1731 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1732 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1733 greq->place_pub_key = *place_pub_key;
1734 greq->ego_pub_key = *ego_pub_key;
1735 greq->origin = *origin;
1736 greq->relay_count = htonl (relay_count);
1738 char *p = (char *) &greq[1];
1739 GNUNET_memcpy (p, app_id, app_id_size);
1744 GNUNET_memcpy (p, relays, relay_size);
1748 GNUNET_memcpy (p, join_msg, join_msg_size);
1754 * Request entry to a place as a guest.
1757 * Application handle.
1759 * Identity of the guest.
1760 * @param place_pub_key
1761 * Public key of the place to enter.
1763 * Flags for the entry.
1765 * Peer identity of the origin of the underlying multicast group.
1766 * @param relay_count
1767 * Number of elements in the @a relays array.
1769 * Relays for the underlying multicast group.
1770 * @param method_name
1771 * Method name for the message.
1773 * Environment containing variables for the message, or NULL.
1775 * Payload for the message to give to the enter callback.
1777 * Number of bytes in @a data.
1779 * Slicer to use for processing incoming requests from guests.
1781 * @return NULL on errors, otherwise handle for the guest.
1783 struct GNUNET_SOCIAL_Guest *
1784 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1785 const struct GNUNET_SOCIAL_Ego *ego,
1786 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1787 enum GNUNET_PSYC_SlaveJoinFlags flags,
1788 const struct GNUNET_PeerIdentity *origin,
1789 uint32_t relay_count,
1790 const struct GNUNET_PeerIdentity *relays,
1791 const struct GNUNET_PSYC_Message *entry_msg,
1792 struct GNUNET_PSYC_Slicer *slicer,
1793 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1794 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1797 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1798 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1800 plc->ego_pub_key = ego->pub_key;
1801 plc->pub_key = *place_pub_key;
1802 plc->cfg = app->cfg;
1803 plc->is_host = GNUNET_NO;
1804 plc->slicer = slicer;
1806 plc->op = GNUNET_OP_create ();
1809 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1810 origin, relay_count, relays, entry_msg);
1812 gst->enter_cb = local_enter_cb;
1813 gst->entry_dcsn_cb = entry_dcsn_cb;
1816 guest_connect (gst);
1822 * Request entry to a place by name as a guest.
1825 * Application handle.
1827 * Identity of the guest.
1829 * GNS name of the place to enter. Either in the form of
1830 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1831 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1832 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1833 * pseudonym's place directly.
1835 * Password to decrypt the record, or NULL for cleartext records.
1837 * Entry request message or NULL.
1839 * Slicer to use for processing incoming requests from guests.
1840 * @param local_enter_cb
1841 * Called upon connection established to the social service.
1842 * @param entry_decision_cb
1843 * Called upon receiving entry decision.
1845 * @return NULL on errors, otherwise handle for the guest.
1847 struct GNUNET_SOCIAL_Guest *
1848 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1849 const struct GNUNET_SOCIAL_Ego *ego,
1850 const char *gns_name,
1851 const char *password,
1852 const struct GNUNET_PSYC_Message *join_msg,
1853 struct GNUNET_PSYC_Slicer *slicer,
1854 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1855 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1858 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1859 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1861 if (NULL == password)
1864 uint16_t app_id_size = strlen (app->id) + 1;
1865 uint16_t gns_name_size = strlen (gns_name) + 1;
1866 uint16_t password_size = strlen (password) + 1;
1868 uint16_t join_msg_size = 0;
1869 if (NULL != join_msg)
1870 join_msg_size = ntohs (join_msg->header.size);
1872 struct GuestEnterByNameRequest *greq;
1874 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1875 + password_size + join_msg_size,
1876 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1878 greq->ego_pub_key = ego->pub_key;
1880 char *p = (char *) &greq[1];
1881 GNUNET_memcpy (p, app->id, app_id_size);
1883 GNUNET_memcpy (p, gns_name, gns_name_size);
1885 GNUNET_memcpy (p, password, password_size);
1887 if (NULL != join_msg)
1888 GNUNET_memcpy (p, join_msg, join_msg_size);
1890 plc->ego_pub_key = ego->pub_key;
1891 plc->cfg = app->cfg;
1892 plc->is_host = GNUNET_NO;
1893 plc->slicer = slicer;
1895 plc->op = GNUNET_OP_create ();
1897 gst->enter_cb = local_enter_cb;
1898 gst->entry_dcsn_cb = entry_decision_cb;
1901 guest_connect (gst);
1906 struct ReconnectContext
1908 struct GNUNET_SOCIAL_Guest *guest;
1910 int64_t *max_message_id;
1911 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
1917 guest_enter_reconnect_cb (void *cls,
1919 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1920 uint64_t max_message_id)
1922 struct ReconnectContext *reconnect_ctx = cls;
1924 GNUNET_assert (NULL != reconnect_ctx);
1925 reconnect_ctx->result = GNUNET_new (int);
1926 *(reconnect_ctx->result) = result;
1927 reconnect_ctx->max_message_id = GNUNET_new (int64_t);
1928 *(reconnect_ctx->max_message_id) = max_message_id;
1933 guest_entry_dcsn_reconnect_cb (void *cls,
1935 const struct GNUNET_PSYC_Message *entry_resp)
1937 struct ReconnectContext *reconnect_ctx = cls;
1938 struct GNUNET_SOCIAL_Guest *gst = reconnect_ctx->guest;
1940 GNUNET_assert (NULL != reconnect_ctx);
1941 GNUNET_assert (NULL != reconnect_ctx->result);
1942 GNUNET_assert (NULL != reconnect_ctx->max_message_id);
1943 if (GNUNET_YES != is_admitted)
1945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1946 "Guest was rejected after calling "
1947 "GNUNET_SOCIAL_guest_enter_reconnect ()\n");
1949 else if (NULL != reconnect_ctx->enter_cb)
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1952 "guest reconnected!\n");
1953 reconnect_ctx->enter_cb (reconnect_ctx->enter_cls,
1954 *(reconnect_ctx->result),
1956 *(reconnect_ctx->max_message_id));
1958 GNUNET_free (reconnect_ctx->result);
1959 GNUNET_free (reconnect_ctx->max_message_id);
1960 GNUNET_free (reconnect_ctx);
1965 * Reconnect to an already entered place as guest.
1968 * Guest connection handle.
1969 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1971 * Flags for the entry.
1973 * Slicer to use for processing incoming requests from guests.
1975 * Called upon re-entering is complete.
1976 * @param entry_decision_cb
1977 * Called upon receiving entry decision.
1979 * @return NULL on errors, otherwise handle for the guest.
1981 struct GNUNET_SOCIAL_Guest *
1982 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1983 enum GNUNET_PSYC_SlaveJoinFlags flags,
1984 struct GNUNET_PSYC_Slicer *slicer,
1985 GNUNET_SOCIAL_GuestEnterCallback enter_cb,
1988 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1989 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1990 struct ReconnectContext *reconnect_ctx;
1992 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1993 struct GuestEnterRequest *greq;
1995 = GNUNET_MQ_msg_extra (greq, app_id_size,
1996 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1997 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1998 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1999 greq->flags = htonl (flags);
2001 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
2003 plc->cfg = gconn->app->cfg;
2004 plc->is_host = GNUNET_NO;
2005 plc->slicer = slicer;
2006 plc->pub_key = gconn->plc_msg.place_pub_key;
2007 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
2009 reconnect_ctx = GNUNET_new (struct ReconnectContext);
2010 reconnect_ctx->guest = gst;
2011 reconnect_ctx->enter_cb = enter_cb;
2012 reconnect_ctx->enter_cls = cls;
2014 plc->op = GNUNET_OP_create ();
2015 gst->enter_cb = &guest_enter_reconnect_cb;
2016 gst->entry_dcsn_cb = &guest_entry_dcsn_reconnect_cb;
2017 gst->cb_cls = reconnect_ctx;
2019 guest_connect (gst);
2025 * Talk to the host of the place.
2028 * Place where we want to talk to the host.
2029 * @param method_name
2030 * Method to invoke on the host.
2032 * Environment containing variables for the message, or NULL.
2033 * @param notify_data
2034 * Function to use to get the payload for the method.
2035 * @param notify_data_cls
2036 * Closure for @a notify_data.
2038 * Flags for the message being sent.
2040 * @return NULL if we are already trying to talk to the host,
2041 * otherwise handle to cancel the request.
2043 struct GNUNET_SOCIAL_TalkRequest *
2044 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2045 const char *method_name,
2046 const struct GNUNET_PSYC_Environment *env,
2047 GNUNET_PSYC_TransmitNotifyData notify_data,
2048 void *notify_data_cls,
2049 enum GNUNET_SOCIAL_TalkFlags flags)
2051 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2052 GNUNET_assert (NULL != plc->tmit);
2055 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2056 NULL, notify_data, notify_data_cls, flags))
2057 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2064 * Resume talking to the host of the place.
2067 * Talk request to resume.
2070 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2072 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2077 * Cancel talking to the host of the place.
2080 * Talk request to cancel.
2083 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2085 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2090 * Disconnect from a place.
2092 * Invalidates guest handle.
2095 * The guest to disconnect.
2098 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2099 GNUNET_ContinuationCallback disconnect_cb,
2102 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2104 plc->disconnect_cb = disconnect_cb;
2105 plc->disconnect_cls = cls;
2106 place_disconnect (plc);
2111 * Leave a place temporarily or permanently.
2113 * Notifies the owner of the place about leaving, and destroys the place handle.
2117 * @param keep_active
2118 * Keep place active after last application disconnected.
2119 * #GNUNET_YES or #GNUNET_NO
2121 * Optional environment for the leave message if @a keep_active
2122 * is #GNUNET_NO. NULL if not needed.
2124 * Called upon disconnecting from the social service.
2127 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2128 struct GNUNET_PSYC_Environment *env,
2129 GNUNET_ContinuationCallback disconnect_cb,
2132 struct GNUNET_MQ_Envelope *envelope;
2134 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2135 GNUNET_SOCIAL_TALK_NONE);
2136 gst->plc.disconnect_cb = disconnect_cb;
2137 gst->plc.disconnect_cls = cls;
2138 envelope = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
2139 GNUNET_MQ_send (gst->plc.mq,
2145 * Obtain handle for a place entered as guest.
2147 * The returned handle can be used to access the place API.
2149 * @param guest Handle for the guest.
2151 * @return Handle for the place, valid as long as @a guest is valid.
2153 struct GNUNET_SOCIAL_Place *
2154 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2161 * Obtain the public key of a place.
2166 * @return Public key of the place.
2168 const struct GNUNET_CRYPTO_EddsaPublicKey *
2169 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2171 return &plc->pub_key;
2176 * Set message processing @a flags for a @a method_prefix.
2180 * @param method_prefix
2181 * Method prefix @a flags apply to.
2183 * The flags that apply to a matching @a method_prefix.
2186 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2187 const char *method_prefix,
2188 enum GNUNET_SOCIAL_MsgProcFlags flags)
2190 GNUNET_assert (NULL != method_prefix);
2191 struct MsgProcRequest *mpreq;
2192 uint16_t method_size = strnlen (method_prefix,
2193 GNUNET_MAX_MESSAGE_SIZE
2194 - sizeof (*mpreq)) + 1;
2195 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2197 struct GNUNET_MQ_Envelope *
2198 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2199 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2200 mpreq->flags = htonl (flags);
2201 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2203 GNUNET_MQ_send (plc->mq, env);
2208 * Clear all message processing flags previously set for this place.
2211 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2213 struct GNUNET_MessageHeader *req;
2214 struct GNUNET_MQ_Envelope *
2215 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2217 GNUNET_MQ_send (plc->mq, env);
2221 static struct GNUNET_SOCIAL_HistoryRequest *
2222 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2223 uint64_t start_message_id,
2224 uint64_t end_message_id,
2225 uint64_t message_limit,
2226 const char *method_prefix,
2228 struct GNUNET_PSYC_Slicer *slicer,
2229 GNUNET_ResultCallback result_cb,
2232 struct GNUNET_PSYC_HistoryRequestMessage *req;
2233 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2235 hist->slicer = slicer;
2236 hist->result_cb = result_cb;
2238 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2240 GNUNET_assert (NULL != method_prefix);
2241 uint16_t method_size = strnlen (method_prefix,
2242 GNUNET_MAX_MESSAGE_SIZE
2243 - sizeof (*req)) + 1;
2244 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2246 struct GNUNET_MQ_Envelope *
2247 env = GNUNET_MQ_msg_extra (req, method_size,
2248 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2249 req->start_message_id = GNUNET_htonll (start_message_id);
2250 req->end_message_id = GNUNET_htonll (end_message_id);
2251 req->message_limit = GNUNET_htonll (message_limit);
2252 req->flags = htonl (flags);
2253 req->op_id = GNUNET_htonll (hist->op_id);
2254 GNUNET_memcpy (&req[1], method_prefix, method_size);
2256 GNUNET_MQ_send (plc->mq, env);
2262 * Learn about the history of a place.
2264 * Messages are returned through the @a slicer function
2265 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2268 * Place we want to learn more about.
2269 * @param start_message_id
2270 * First historic message we are interested in.
2271 * @param end_message_id
2272 * Last historic message we are interested in (inclusive).
2273 * @param method_prefix
2274 * Only retrieve messages with this method prefix.
2276 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2278 * Slicer to use for retrieved messages.
2279 * Can be the same as the slicer of the place.
2281 * Function called after all messages retrieved.
2282 * NULL if not needed.
2283 * @param cls Closure for @a result_cb.
2285 struct GNUNET_SOCIAL_HistoryRequest *
2286 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2287 uint64_t start_message_id,
2288 uint64_t end_message_id,
2289 const char *method_prefix,
2291 struct GNUNET_PSYC_Slicer *slicer,
2292 GNUNET_ResultCallback result_cb,
2295 return place_history_replay (plc, start_message_id, end_message_id, 0,
2296 method_prefix, flags, slicer, result_cb, cls);
2301 * Learn about the history of a place.
2303 * Sends messages through the slicer function of the place where
2304 * start_message_id <= message_id <= end_message_id.
2305 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2307 * To get the latest message, use 0 for both the start and end message ID.
2310 * Place we want to learn more about.
2311 * @param message_limit
2312 * Maximum number of historic messages we are interested in.
2313 * @param method_prefix
2314 * Only retrieve messages with this method prefix.
2316 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2318 * Function called after all messages retrieved.
2319 * NULL if not needed.
2320 * @param cls Closure for @a result_cb.
2322 struct GNUNET_SOCIAL_HistoryRequest *
2323 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2324 uint64_t message_limit,
2325 const char *method_prefix,
2327 struct GNUNET_PSYC_Slicer *slicer,
2328 GNUNET_ResultCallback result_cb,
2331 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2332 slicer, result_cb, cls);
2337 * Cancel learning about the history of a place.
2340 * History lesson to cancel.
2343 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2345 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2351 * Request matching state variables.
2353 static struct GNUNET_SOCIAL_LookHandle *
2354 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2355 uint16_t type, const char *name,
2356 GNUNET_PSYC_StateVarCallback var_cb,
2357 GNUNET_ResultCallback result_cb, void *cls)
2359 struct GNUNET_PSYC_StateRequestMessage *req;
2360 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2362 look->var_cb = var_cb;
2363 look->result_cb = result_cb;
2365 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2367 GNUNET_assert (NULL != name);
2368 size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
2369 - sizeof (*req)) + 1;
2370 struct GNUNET_MQ_Envelope *
2371 env = GNUNET_MQ_msg_extra (req, name_size, type);
2372 req->op_id = GNUNET_htonll (look->op_id);
2373 GNUNET_memcpy (&req[1], name, name_size);
2375 GNUNET_MQ_send (plc->mq, env);
2381 * Look at a particular object in the place.
2383 * The best matching object is returned (its name might be less specific than
2384 * what was requested).
2387 * The place where to look.
2389 * Full name of the object.
2391 * Set to the size of the returned value.
2393 * @return NULL if there is no such object at this place.
2395 struct GNUNET_SOCIAL_LookHandle *
2396 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2397 const char *full_name,
2398 GNUNET_PSYC_StateVarCallback var_cb,
2399 GNUNET_ResultCallback result_cb,
2402 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2403 full_name, var_cb, result_cb, cls);
2408 * Look for objects in the place with a matching name prefix.
2411 * The place where to look.
2412 * @param name_prefix
2413 * Look at objects with names beginning with this value.
2415 * Function to call for each object found.
2417 * Closure for callback function.
2419 * @return Handle that can be used to stop looking at objects.
2421 struct GNUNET_SOCIAL_LookHandle *
2422 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2423 const char *name_prefix,
2424 GNUNET_PSYC_StateVarCallback var_cb,
2425 GNUNET_ResultCallback result_cb,
2428 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2429 name_prefix, var_cb, result_cb, cls);
2434 * Cancel a state request operation.
2437 * Handle for the operation to cancel.
2440 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2442 GNUNET_OP_remove (look->plc->op, look->op_id);
2448 op_recv_zone_add_place_result (void *cls, int64_t result,
2449 const void *err_msg, uint16_t err_msg_size)
2451 LOG (GNUNET_ERROR_TYPE_DEBUG,
2452 "Received zone add place result: %" PRId64 ".\n", result);
2454 struct ZoneAddPlaceHandle *add_plc = cls;
2455 if (NULL != add_plc->result_cb)
2456 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2458 GNUNET_free (add_plc);
2463 * Advertise @e place in the GNS zone of @e ego.
2466 * Application handle.
2469 * @param place_pub_key
2470 * Public key of place to add.
2472 * The name for the PLACE record to put in the zone.
2474 * Password used to encrypt the record or NULL to keep it cleartext.
2475 * @param relay_count
2476 * Number of elements in the @a relays array.
2478 * List of relays to put in the PLACE record to advertise
2479 * as entry points to the place in addition to the origin.
2480 * @param expiration_time
2481 * Expiration time of the record, use 0 to remove the record.
2483 * Function called with the result of the operation.
2485 * Closure for @a result_cb
2487 * @return #GNUNET_OK if the request was sent,
2488 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2491 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2492 const struct GNUNET_SOCIAL_Ego *ego,
2494 const char *password,
2495 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2496 const struct GNUNET_PeerIdentity *origin,
2497 uint32_t relay_count,
2498 const struct GNUNET_PeerIdentity *relays,
2499 struct GNUNET_TIME_Absolute expiration_time,
2500 GNUNET_ResultCallback result_cb,
2503 struct ZoneAddPlaceRequest *preq;
2504 size_t name_size = strlen (name) + 1;
2505 size_t password_size = strlen (password) + 1;
2506 size_t relay_size = relay_count * sizeof (*relays);
2507 size_t payload_size = name_size + password_size + relay_size;
2509 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2510 return GNUNET_SYSERR;
2512 struct GNUNET_MQ_Envelope *
2513 env = GNUNET_MQ_msg_extra (preq, payload_size,
2514 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2515 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2516 preq->ego_pub_key = ego->pub_key;
2517 preq->place_pub_key = *place_pub_key;
2518 preq->origin = *origin;
2519 preq->relay_count = htonl (relay_count);
2521 char *p = (char *) &preq[1];
2522 GNUNET_memcpy (p, name, name_size);
2524 GNUNET_memcpy (p, password, password_size);
2526 GNUNET_memcpy (p, relays, relay_size);
2528 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2529 add_plc->result_cb = result_cb;
2530 add_plc->result_cls = result_cls;
2532 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2533 op_recv_zone_add_place_result,
2536 GNUNET_MQ_send (app->mq, env);
2542 op_recv_zone_add_nym_result (void *cls, int64_t result,
2543 const void *err_msg, uint16_t err_msg_size)
2545 LOG (GNUNET_ERROR_TYPE_DEBUG,
2546 "Received zone add nym result: %" PRId64 ".\n", result);
2548 struct ZoneAddNymHandle *add_nym = cls;
2549 if (NULL != add_nym->result_cb)
2550 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2552 GNUNET_free (add_nym);
2557 * Add nym to the GNS zone of @e ego.
2564 * The name for the PKEY record to put in the zone.
2565 * @param nym_pub_key
2566 * Public key of nym to add.
2567 * @param expiration_time
2568 * Expiration time of the record, use 0 to remove the record.
2570 * Function called with the result of the operation.
2572 * Closure for @a result_cb
2574 * @return #GNUNET_OK if the request was sent,
2575 * #GNUNET_SYSERR on error, e.g. the name is too long.
2578 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2579 const struct GNUNET_SOCIAL_Ego *ego,
2581 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2582 struct GNUNET_TIME_Absolute expiration_time,
2583 GNUNET_ResultCallback result_cb,
2586 struct ZoneAddNymRequest *nreq;
2588 size_t name_size = strlen (name) + 1;
2589 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2590 return GNUNET_SYSERR;
2592 struct GNUNET_MQ_Envelope *
2593 env = GNUNET_MQ_msg_extra (nreq, name_size,
2594 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2595 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2596 nreq->ego_pub_key = ego->pub_key;
2597 nreq->nym_pub_key = *nym_pub_key;
2598 GNUNET_memcpy (&nreq[1], name, name_size);
2600 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2601 add_nym->result_cb = result_cb;
2602 add_nym->result_cls = result_cls;
2604 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2605 op_recv_zone_add_nym_result,
2608 GNUNET_MQ_send (app->mq, env);
2617 app_connect (struct GNUNET_SOCIAL_App *app);
2621 app_reconnect (void *cls)
2628 * App client disconnected from service.
2630 * Reconnect after backoff period.
2633 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2635 struct GNUNET_SOCIAL_App *app = cls;
2637 LOG (GNUNET_ERROR_TYPE_DEBUG,
2638 "App client disconnected (%d), re-connecting\n",
2640 if (NULL != app->mq)
2642 GNUNET_MQ_destroy (app->mq);
2646 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2649 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2654 app_connect (struct GNUNET_SOCIAL_App *app)
2656 struct GNUNET_MQ_MessageHandler handlers[] = {
2657 GNUNET_MQ_hd_var_size (app_ego,
2658 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2659 struct AppEgoMessage,
2661 GNUNET_MQ_hd_fixed_size (app_ego_end,
2662 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2663 struct GNUNET_MessageHeader,
2665 GNUNET_MQ_hd_var_size (app_place,
2666 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2667 struct AppPlaceMessage,
2669 GNUNET_MQ_hd_fixed_size (app_place_end,
2670 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2671 struct GNUNET_MessageHeader,
2673 GNUNET_MQ_hd_var_size (app_result,
2674 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2675 struct GNUNET_OperationResultMessage,
2677 GNUNET_MQ_handler_end ()
2680 app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
2681 handlers, app_disconnected, app);
2682 GNUNET_assert (NULL != app->mq);
2683 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2688 * Connect application to the social service.
2690 * The @host_place_cb and @guest_place_cb functions are
2691 * initially called for each entered places,
2692 * then later each time a new place is entered with the current application ID.
2699 * Function to notify about an available ego.
2701 * Function to notify about a place entered as host.
2703 * Function to notify about a place entered as guest.
2705 * Closure for the callbacks.
2707 * @return Handle that can be used to stop listening.
2709 struct GNUNET_SOCIAL_App *
2710 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2712 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2713 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2714 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2715 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2718 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2719 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2723 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2725 app->ego_cb = ego_cb;
2726 app->host_cb = host_cb;
2727 app->guest_cb = guest_cb;
2728 app->connected_cb = connected_cb;
2730 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2731 app->op = GNUNET_OP_create ();
2732 app->id = GNUNET_malloc (app_id_size);
2733 GNUNET_memcpy (app->id, id, app_id_size);
2735 struct AppConnectRequest *creq;
2736 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2737 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2738 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2746 app_cleanup (struct GNUNET_SOCIAL_App *app)
2748 if (NULL != app->mq)
2750 GNUNET_MQ_destroy (app->mq);
2753 if (NULL != app->disconnect_cb)
2755 app->disconnect_cb (app->disconnect_cls);
2756 app->disconnect_cb = NULL;
2762 * Disconnect application.
2765 * Application handle.
2766 * @param disconnect_cb
2767 * Disconnect callback.
2768 * @param disconnect_cls
2769 * Disconnect closure.
2772 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2773 GNUNET_ContinuationCallback disconnect_cb,
2774 void *disconnect_cls)
2776 if (NULL == app) return;
2778 app->disconnect_cb = disconnect_cb;
2779 app->disconnect_cls = disconnect_cls;
2781 if (NULL != app->mq)
2783 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2786 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
2801 * Detach application from a place.
2803 * Removes the place from the entered places list for this application.
2804 * Note: this does not disconnect from the place.
2806 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2814 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2815 struct GNUNET_SOCIAL_Place *plc)
2817 struct AppDetachRequest *dreq;
2818 struct GNUNET_MQ_Envelope *
2819 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2820 dreq->place_pub_key = plc->pub_key;
2821 dreq->ego_pub_key = plc->ego_pub_key;
2823 GNUNET_MQ_send (app->mq, env);
2827 /* end of social_api.c */