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;
187 * Function called after disconnected from the service.
189 GNUNET_ContinuationCallback disconnect_cb;
192 * Closure for @a disconnect_cb.
194 void *disconnect_cls;
197 * Public key of the place.
199 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
202 * Public key of the ego.
204 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
207 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
214 * Host handle for a place that we entered.
216 struct GNUNET_SOCIAL_Host
218 struct GNUNET_SOCIAL_Place plc;
221 * Slicer for processing incoming messages from guests.
223 struct GNUNET_PSYC_Slicer *slicer;
225 GNUNET_SOCIAL_HostEnterCallback enter_cb;
227 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
229 GNUNET_SOCIAL_FarewellCallback farewell_cb;
232 * Closure for callbacks.
236 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
237 struct GNUNET_PSYC_Environment *notice_place_leave_env;
242 * Guest handle for place that we entered.
244 struct GNUNET_SOCIAL_Guest
246 struct GNUNET_SOCIAL_Place plc;
248 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
250 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
253 * Closure for callbacks.
260 * Hash map of all nyms.
261 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
263 struct GNUNET_CONTAINER_MultiHashMap *nyms;
267 * Handle for an announcement request.
269 struct GNUNET_SOCIAL_Announcement
278 struct GNUNET_SOCIAL_TalkRequest
287 struct GNUNET_SOCIAL_HistoryRequest
292 struct GNUNET_SOCIAL_Place *plc;
300 * Slicer for processing incoming messages.
302 struct GNUNET_PSYC_Slicer *slicer;
305 * Function to call when the operation finished.
307 GNUNET_ResultCallback result_cb;
310 * Closure for @a result_cb.
316 struct GNUNET_SOCIAL_LookHandle
321 struct GNUNET_SOCIAL_Place *plc;
329 * State variable result callback.
331 GNUNET_PSYC_StateVarCallback var_cb;
334 * Function to call when the operation finished.
336 GNUNET_ResultCallback result_cb;
339 * Name of current modifier being received.
344 * Size of current modifier value being received.
346 size_t mod_value_size;
349 * Remaining size of current modifier value still to be received.
351 size_t mod_value_remaining;
354 * Closure for @a result_cb.
360 struct ZoneAddPlaceHandle
362 GNUNET_ResultCallback result_cb;
367 struct ZoneAddNymHandle
369 GNUNET_ResultCallback result_cb;
376 static struct GNUNET_SOCIAL_Nym *
377 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
379 struct GNUNET_SOCIAL_Nym *nym = NULL;
380 struct GNUNET_HashCode pub_key_hash;
385 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
388 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
390 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
394 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
395 nym->pub_key = *pub_key;
396 nym->pub_key_hash = pub_key_hash;
397 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
398 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
405 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
407 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
412 /*** MESSAGE HANDLERS ***/
414 /** _notice_place_leave from guests */
417 host_recv_notice_place_leave_method (void *cls,
418 const struct GNUNET_PSYC_MessageHeader *msg,
419 const struct GNUNET_PSYC_MessageMethod *meth,
421 const char *method_name)
423 struct GNUNET_SOCIAL_Host *hst = cls;
425 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
426 &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
429 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
432 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
433 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
435 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
436 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
438 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
440 "_notice_place_leave: got method from nym %s (%s).\n",
441 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
447 host_recv_notice_place_leave_modifier (void *cls,
448 const struct GNUNET_PSYC_MessageHeader *msg,
449 const struct GNUNET_MessageHeader *pmsg,
451 enum GNUNET_PSYC_Operator oper,
455 uint16_t full_value_size)
457 struct GNUNET_SOCIAL_Host *hst = cls;
458 if (NULL == hst->notice_place_leave_env)
461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
462 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
464 message_id, oper, name, value_size, (const char *) value);
466 /* skip _nym, it's added later in eom() */
467 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
468 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
471 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
472 GNUNET_PSYC_OP_SET, name, value, value_size);
477 host_recv_notice_place_leave_eom (void *cls,
478 const struct GNUNET_PSYC_MessageHeader *msg,
479 const struct GNUNET_MessageHeader *pmsg,
481 uint8_t is_cancelled)
483 struct GNUNET_SOCIAL_Host *hst = cls;
484 if (NULL == hst->notice_place_leave_env)
487 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
489 "_notice_place_leave: got EOM from nym %s (%s).\n",
490 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
493 if (GNUNET_YES != is_cancelled)
495 if (NULL != hst->farewell_cb)
496 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
497 hst->notice_place_leave_env);
498 /* announce leaving guest to place */
499 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
500 "_nym", hst->notice_place_leave_nym,
501 sizeof (*hst->notice_place_leave_nym));
502 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
503 hst->notice_place_leave_env,
504 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
505 nym_destroy (hst->notice_place_leave_nym);
507 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
508 hst->notice_place_leave_env = NULL;
516 check_place_result (void *cls,
517 const struct GNUNET_OperationResultMessage *res)
519 uint16_t size = ntohs (res->header.size);
520 if (size < sizeof (*res))
521 { /* Error, message too small. */
523 return GNUNET_SYSERR;
530 handle_place_result (void *cls,
531 const struct GNUNET_OperationResultMessage *res)
533 struct GNUNET_SOCIAL_Place *plc = cls;
535 uint16_t size = ntohs (res->header.size);
536 uint16_t data_size = size - sizeof (*res);
537 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
539 GNUNET_OP_result (plc->op, GNUNET_ntohll (res->op_id),
540 GNUNET_ntohll (res->result_code),
541 data, data_size, NULL);
546 check_app_result (void *cls,
547 const struct GNUNET_OperationResultMessage *res)
549 uint16_t size = ntohs (res->header.size);
550 if (size < sizeof (*res))
551 { /* Error, message too small. */
553 return GNUNET_SYSERR;
560 handle_app_result (void *cls,
561 const struct GNUNET_OperationResultMessage *res)
563 struct GNUNET_SOCIAL_App *app = cls;
565 uint16_t size = ntohs (res->header.size);
566 uint16_t data_size = size - sizeof (*res);
567 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
569 GNUNET_OP_result (app->op, GNUNET_ntohll (res->op_id),
570 GNUNET_ntohll (res->result_code),
571 data, data_size, NULL);
576 op_recv_history_result (void *cls, int64_t result,
577 const void *err_msg, uint16_t err_msg_size)
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "Received history replay result: %" PRId64 ".\n", result);
582 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
584 if (NULL != hist->result_cb)
585 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
592 op_recv_state_result (void *cls, int64_t result,
593 const void *err_msg, uint16_t err_msg_size)
595 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 "Received state request result: %" PRId64 ".\n", result);
598 struct GNUNET_SOCIAL_LookHandle *look = cls;
600 if (NULL != look->result_cb)
601 look->result_cb (look->cls, result, err_msg, err_msg_size);
608 check_place_history_result (void *cls,
609 const struct GNUNET_OperationResultMessage *res)
611 struct GNUNET_PSYC_MessageHeader *
612 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
613 uint16_t size = ntohs (res->header.size);
615 if (NULL == pmsg || size < sizeof (*res) + sizeof (*pmsg))
616 { /* Error, message too small. */
618 return GNUNET_SYSERR;
625 handle_place_history_result (void *cls,
626 const struct GNUNET_OperationResultMessage *res)
628 struct GNUNET_SOCIAL_Place *plc = cls;
629 struct GNUNET_PSYC_MessageHeader *
630 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "%p Received historic fragment for message #%" PRIu64 ".\n",
634 plc, GNUNET_ntohll (pmsg->message_id));
636 GNUNET_ResultCallback result_cb = NULL;
637 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
639 if (GNUNET_YES != GNUNET_OP_get (plc->op,
640 GNUNET_ntohll (res->op_id),
641 &result_cb, (void *) &hist, NULL))
642 { /* Operation not found. */
643 LOG (GNUNET_ERROR_TYPE_WARNING,
644 "%p Replay operation not found for historic fragment of message #%"
646 plc, GNUNET_ntohll (pmsg->message_id));
650 GNUNET_PSYC_slicer_message (hist->slicer,
651 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
656 check_place_state_result (void *cls,
657 const struct GNUNET_OperationResultMessage *res)
659 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
663 LOG (GNUNET_ERROR_TYPE_WARNING,
664 "Invalid modifier in state result\n");
665 return GNUNET_SYSERR;
668 uint16_t size = ntohs (res->header.size);
669 uint16_t mod_size = ntohs (mod->size);
670 if (size - sizeof (*res) != mod_size)
673 LOG (GNUNET_ERROR_TYPE_WARNING,
674 "Invalid modifier size in state result: %u - %u != %u\n",
675 ntohs (res->header.size), sizeof (*res), mod_size);
676 return GNUNET_SYSERR;
683 handle_place_state_result (void *cls,
684 const struct GNUNET_OperationResultMessage *res)
686 struct GNUNET_SOCIAL_Place *plc = cls;
688 GNUNET_ResultCallback result_cb = NULL;
689 struct GNUNET_SOCIAL_LookHandle *look = NULL;
691 if (GNUNET_YES != GNUNET_OP_get (plc->op,
692 GNUNET_ntohll (res->op_id),
693 &result_cb, (void *) &look, NULL))
694 { /* Operation not found. */
698 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
699 uint16_t mod_size = ntohs (mod->size);
701 switch (ntohs (mod->type))
703 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
705 const struct GNUNET_PSYC_MessageModifier *
706 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
708 const char *name = (const char *) &pmod[1];
709 uint16_t name_size = ntohs (pmod->name_size);
711 || mod_size - sizeof (*pmod) < name_size
712 || '\0' != name[name_size - 1])
715 LOG (GNUNET_ERROR_TYPE_WARNING,
716 "Invalid modifier name in state result\n");
719 look->mod_value_size = ntohs (pmod->value_size);
720 look->var_cb (look->cls, mod, name, name + name_size,
721 mod_size - sizeof (*mod) - name_size,
722 look->mod_value_size);
723 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
725 look->mod_value_remaining = look->mod_value_size;
726 look->mod_name = GNUNET_malloc (name_size);
727 GNUNET_memcpy (look->mod_name, name, name_size);
732 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
733 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
734 mod_size - sizeof (*mod), look->mod_value_size);
735 look->mod_value_remaining -= mod_size - sizeof (*mod);
736 if (0 == look->mod_value_remaining)
738 GNUNET_free (look->mod_name);
746 handle_place_message_ack (void *cls,
747 const struct GNUNET_MessageHeader *msg)
749 struct GNUNET_SOCIAL_Place *plc = cls;
751 GNUNET_PSYC_transmit_got_ack (plc->tmit);
756 check_place_message (void *cls,
757 const struct GNUNET_PSYC_MessageHeader *pmsg)
764 handle_place_message (void *cls,
765 const struct GNUNET_PSYC_MessageHeader *pmsg)
767 struct GNUNET_SOCIAL_Place *plc = cls;
769 GNUNET_PSYC_slicer_message (plc->slicer, pmsg);
774 check_host_message (void *cls,
775 const struct GNUNET_PSYC_MessageHeader *pmsg)
782 handle_host_message (void *cls,
783 const struct GNUNET_PSYC_MessageHeader *pmsg)
785 struct GNUNET_SOCIAL_Host *hst = cls;
787 GNUNET_PSYC_slicer_message (hst->slicer, pmsg);
788 GNUNET_PSYC_slicer_message (hst->plc.slicer, pmsg);
793 handle_host_enter_ack (void *cls,
794 const struct HostEnterAck *hack)
796 struct GNUNET_SOCIAL_Host *hst = cls;
798 hst->plc.pub_key = hack->place_pub_key;
800 int32_t result = ntohl (hack->result_code);
801 if (NULL != hst->enter_cb)
802 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
803 GNUNET_ntohll (hack->max_message_id));
808 check_host_enter_request (void *cls,
809 const struct GNUNET_PSYC_JoinRequestMessage *req)
816 handle_host_enter_request (void *cls,
817 const struct GNUNET_PSYC_JoinRequestMessage *req)
819 struct GNUNET_SOCIAL_Host *hst = cls;
821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
822 "handle_host_enter_request\n");
824 if (NULL == hst->answer_door_cb)
827 const char *method_name = NULL;
828 struct GNUNET_PSYC_Environment *env = NULL;
829 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
830 const void *data = NULL;
831 uint16_t data_size = 0;
833 const struct GNUNET_PSYC_Message *join_msg = NULL;
837 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
839 join_msg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (req);
840 LOG (GNUNET_ERROR_TYPE_DEBUG,
841 "Received join_msg of type %u and size %u.\n",
842 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
844 env = GNUNET_PSYC_env_create ();
845 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
846 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
850 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
851 LOG (GNUNET_ERROR_TYPE_WARNING,
852 "Ignoring invalid entry request from nym %s.\n",
859 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
860 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
865 GNUNET_PSYC_env_destroy (env);
866 if (NULL != entry_pmsg)
867 GNUNET_free (entry_pmsg);
872 handle_guest_enter_ack (void *cls,
873 const struct GNUNET_PSYC_CountersResultMessage *cres)
875 struct GNUNET_SOCIAL_Guest *gst = cls;
877 int32_t result = ntohl (cres->result_code);
878 if (NULL != gst->enter_cb)
879 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
880 GNUNET_ntohll (cres->max_message_id));
885 check_guest_enter_decision (void *cls,
886 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
893 handle_guest_enter_decision (void *cls,
894 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
896 struct GNUNET_SOCIAL_Guest *gst = cls;
898 struct GNUNET_PSYC_Message *pmsg = NULL;
899 if (ntohs (dcsn->header.size) > sizeof (*dcsn))
900 pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
902 if (NULL != gst->entry_dcsn_cb)
903 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
908 check_app_ego (void *cls,
909 const struct AppEgoMessage *emsg)
916 handle_app_ego (void *cls,
917 const struct AppEgoMessage *emsg)
919 struct GNUNET_SOCIAL_App *app = cls;
921 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
923 struct GNUNET_HashCode ego_pub_hash;
924 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
927 struct GNUNET_SOCIAL_Ego *
928 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
931 ego = GNUNET_malloc (sizeof (*ego));
932 ego->pub_key = emsg->ego_pub_key;
933 ego->name = GNUNET_malloc (name_size);
934 GNUNET_memcpy (ego->name, &emsg[1], name_size);
938 ego->name = GNUNET_realloc (ego->name, name_size);
939 GNUNET_memcpy (ego->name, &emsg[1], name_size);
942 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
943 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
945 if (NULL != app->ego_cb)
946 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
951 handle_app_ego_end (void *cls,
952 const struct GNUNET_MessageHeader *msg)
954 //struct GNUNET_SOCIAL_App *app = cls;
959 check_app_place (void *cls,
960 const struct AppPlaceMessage *pmsg)
967 handle_app_place (void *cls,
968 const struct AppPlaceMessage *pmsg)
970 struct GNUNET_SOCIAL_App *app = cls;
972 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
973 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
976 struct GNUNET_HashCode ego_pub_hash;
977 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
979 struct GNUNET_SOCIAL_Ego *
980 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
983 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failure to obtain ego %s.\n",
984 GNUNET_h2s (&ego_pub_hash));
989 if (GNUNET_YES == pmsg->is_host)
991 if (NULL != app->host_cb) {
992 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
994 hconn->plc_msg = *pmsg;
995 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
999 else if (NULL != app->guest_cb)
1001 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
1003 gconn->plc_msg = *pmsg;
1004 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1005 GNUNET_free (gconn);
1011 handle_app_place_end (void *cls,
1012 const struct GNUNET_MessageHeader *msg)
1014 struct GNUNET_SOCIAL_App *app = cls;
1016 if (NULL != app->connected_cb)
1017 app->connected_cb (app->cb_cls);
1021 /*** CLEANUP / DISCONNECT ***/
1025 host_cleanup (struct GNUNET_SOCIAL_Host *hst)
1027 if (NULL != hst->slicer)
1029 GNUNET_PSYC_slicer_destroy (hst->slicer);
1037 guest_cleanup (struct GNUNET_SOCIAL_Guest *gst)
1044 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1046 struct GNUNET_HashCode place_pub_hash;
1048 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1050 "%s place cleanup: %s\n",
1051 GNUNET_YES == plc->is_host ? "host" : "guest",
1052 GNUNET_h2s (&place_pub_hash));
1054 if (NULL != plc->tmit)
1056 GNUNET_PSYC_transmit_destroy (plc->tmit);
1059 if (NULL != plc->connect_env)
1061 GNUNET_MQ_discard (plc->connect_env);
1062 plc->connect_env = NULL;
1064 if (NULL != plc->mq)
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "destroying MQ (place_cleanup)\n");
1068 GNUNET_MQ_destroy (plc->mq);
1071 if (NULL != plc->disconnect_cb)
1073 plc->disconnect_cb (plc->disconnect_cls);
1074 plc->disconnect_cb = NULL;
1077 (GNUNET_YES == plc->is_host)
1078 ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
1079 : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
1084 place_disconnect (struct GNUNET_SOCIAL_Place *plc)
1086 struct GNUNET_HashCode place_pub_hash;
1088 GNUNET_CRYPTO_hash (&plc->pub_key,
1089 sizeof (plc->pub_key),
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "place_disconnect, plc = %s\n",
1093 GNUNET_h2s (&place_pub_hash));
1094 place_cleanup (plc);
1102 host_connect (struct GNUNET_SOCIAL_Host *hst);
1106 host_reconnect (void *cls)
1113 * Host client disconnected from service.
1115 * Reconnect after backoff period.
1118 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1120 struct GNUNET_SOCIAL_Host *hst = cls;
1121 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1123 LOG (GNUNET_ERROR_TYPE_DEBUG,
1124 "Host client disconnected (%d), re-connecting\n",
1126 if (NULL != plc->tmit)
1128 GNUNET_PSYC_transmit_destroy (plc->tmit);
1131 if (NULL != plc->mq)
1133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1134 "destroying MQ (host_disconnected)\n");
1135 GNUNET_MQ_destroy (plc->mq);
1139 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1142 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1147 host_connect (struct GNUNET_SOCIAL_Host *hst)
1149 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1151 struct GNUNET_MQ_MessageHandler handlers[] = {
1152 GNUNET_MQ_hd_fixed_size (host_enter_ack,
1153 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1154 struct HostEnterAck,
1156 GNUNET_MQ_hd_var_size (host_enter_request,
1157 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1158 struct GNUNET_PSYC_JoinRequestMessage,
1160 GNUNET_MQ_hd_var_size (host_message,
1161 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1162 struct GNUNET_PSYC_MessageHeader,
1164 GNUNET_MQ_hd_fixed_size (place_message_ack,
1165 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1166 struct GNUNET_MessageHeader,
1168 GNUNET_MQ_hd_var_size (place_history_result,
1169 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1170 struct GNUNET_OperationResultMessage,
1172 GNUNET_MQ_hd_var_size (place_state_result,
1173 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1174 struct GNUNET_OperationResultMessage,
1176 GNUNET_MQ_hd_var_size (place_result,
1177 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1178 struct GNUNET_OperationResultMessage,
1180 GNUNET_MQ_handler_end ()
1183 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1184 handlers, host_disconnected, hst);
1185 GNUNET_assert (NULL != plc->mq);
1186 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1188 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1193 * Enter a place as host.
1195 * A place is created upon first entering, and it is active until permanently
1196 * left using GNUNET_SOCIAL_host_leave().
1199 * Application handle.
1201 * Identity of the host.
1203 * Policy specifying entry and history restrictions for the place.
1205 * Slicer to handle incoming messages.
1207 * Function called when the place is entered and ready to use.
1208 * @param answer_door_cb
1209 * Function to handle new nyms that want to enter.
1210 * @param farewell_cb
1211 * Function to handle departing nyms.
1213 * Closure for the callbacks.
1215 * @return Handle for the host. This handle contains the pubkey.
1217 struct GNUNET_SOCIAL_Host *
1218 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1219 const struct GNUNET_SOCIAL_Ego *ego,
1220 enum GNUNET_PSYC_Policy policy,
1221 struct GNUNET_PSYC_Slicer *slicer,
1222 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1223 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1224 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1227 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1228 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1230 plc->cfg = app->cfg;
1231 plc->is_host = GNUNET_YES;
1232 plc->slicer = slicer;
1234 hst->enter_cb = enter_cb;
1235 hst->answer_door_cb = answer_door_cb;
1236 hst->farewell_cb = farewell_cb;
1239 plc->op = GNUNET_OP_create ();
1241 hst->slicer = GNUNET_PSYC_slicer_create ();
1242 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1243 host_recv_notice_place_leave_method,
1244 host_recv_notice_place_leave_modifier,
1245 NULL, host_recv_notice_place_leave_eom, hst);
1247 uint16_t app_id_size = strlen (app->id) + 1;
1248 struct HostEnterRequest *hreq;
1249 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1250 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1251 hreq->policy = policy;
1252 hreq->ego_pub_key = ego->pub_key;
1253 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1261 * Reconnect to an already entered place as host.
1264 * Host connection handle.
1265 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1267 * Slicer to handle incoming messages.
1269 * Function called when the place is entered and ready to use.
1270 * @param answer_door_cb
1271 * Function to handle new nyms that want to enter.
1272 * @param farewell_cb
1273 * Function to handle departing nyms.
1275 * Closure for the callbacks.
1277 * @return Handle for the host.
1279 struct GNUNET_SOCIAL_Host *
1280 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1281 struct GNUNET_PSYC_Slicer *slicer,
1282 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1283 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1284 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1287 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1288 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1290 hst->enter_cb = enter_cb;
1291 hst->answer_door_cb = answer_door_cb;
1292 hst->farewell_cb = farewell_cb;
1295 plc->cfg = hconn->app->cfg;
1296 plc->is_host = GNUNET_YES;
1297 plc->slicer = slicer;
1298 plc->pub_key = hconn->plc_msg.place_pub_key;
1299 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1301 plc->op = GNUNET_OP_create ();
1303 hst->slicer = GNUNET_PSYC_slicer_create ();
1304 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1305 host_recv_notice_place_leave_method,
1306 host_recv_notice_place_leave_modifier,
1307 NULL, host_recv_notice_place_leave_eom, hst);
1309 size_t app_id_size = strlen (hconn->app->id) + 1;
1310 struct HostEnterRequest *hreq;
1311 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1312 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1313 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1314 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1315 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1323 * Decision whether to admit @a nym into the place or refuse entry.
1326 * Host of the place.
1328 * Handle for the entity that wanted to enter.
1329 * @param is_admitted
1330 * #GNUNET_YES if @a nym is admitted,
1331 * #GNUNET_NO if @a nym is refused entry,
1332 * #GNUNET_SYSERR if we cannot answer the request.
1333 * @param method_name
1334 * Method name for the rejection message.
1336 * Environment containing variables for the message, or NULL.
1338 * Data for the rejection message to send back.
1340 * Number of bytes in @a data for method.
1341 * @return #GNUNET_OK on success,
1342 * #GNUNET_SYSERR if the message is too large.
1345 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1346 struct GNUNET_SOCIAL_Nym *nym,
1348 const struct GNUNET_PSYC_Message *entry_resp)
1350 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1351 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1352 uint16_t entry_resp_size
1353 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1355 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1356 return GNUNET_SYSERR;
1358 struct GNUNET_MQ_Envelope *
1359 env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1360 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1361 dcsn->is_admitted = htonl (is_admitted);
1362 dcsn->slave_pub_key = nym->pub_key;
1364 if (0 < entry_resp_size)
1365 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1367 GNUNET_MQ_send (plc->mq, env);
1373 * Throw @a nym out of the place.
1375 * The @a nym reference will remain valid until the
1376 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1377 * which should be very soon after this call.
1380 * Host of the place.
1382 * Handle for the entity to be ejected.
1384 * Environment for the message or NULL.
1387 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1388 const struct GNUNET_SOCIAL_Nym *nym,
1389 struct GNUNET_PSYC_Environment *e)
1391 struct GNUNET_PSYC_Environment *env = e;
1393 env = GNUNET_PSYC_env_create ();
1394 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1395 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1396 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1397 GNUNET_SOCIAL_ANNOUNCE_NONE);
1399 GNUNET_PSYC_env_destroy (env);
1404 * Get the public key of @a ego.
1409 * @return Public key of ego.
1411 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1412 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1414 return &ego->pub_key;
1419 * Get the hash of the public key of @a ego.
1424 * @return Hash of the public key of @a ego.
1426 const struct GNUNET_HashCode *
1427 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1429 return &ego->pub_key_hash;
1434 * Get the name of @a ego.
1439 * @return Public key of @a ego.
1442 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1449 * Get the public key of @a nym.
1451 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1456 * @return Public key of @a nym.
1458 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1459 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1461 return &nym->pub_key;
1466 * Get the hash of the public key of @a nym.
1471 * @return Hash of the public key of @a nym.
1473 const struct GNUNET_HashCode *
1474 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1476 return &nym->pub_key_hash;
1481 * Send a message to all nyms that are present in the place.
1483 * This function is restricted to the host. Nyms can only send requests
1484 * to the host who can decide to relay it to everyone in the place.
1486 * @param host Host of the place.
1487 * @param method_name Method to use for the announcement.
1488 * @param env Environment containing variables for the message and operations
1489 * on objects of the place. Can be NULL.
1490 * @param notify Function to call to get the payload of the announcement.
1491 * @param notify_cls Closure for @a notify.
1492 * @param flags Flags for this announcement.
1494 * @return NULL on error (announcement already in progress?).
1496 struct GNUNET_SOCIAL_Announcement *
1497 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1498 const char *method_name,
1499 const struct GNUNET_PSYC_Environment *env,
1500 GNUNET_PSYC_TransmitNotifyData notify_data,
1501 void *notify_data_cls,
1502 enum GNUNET_SOCIAL_AnnounceFlags flags)
1505 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1506 NULL, notify_data, notify_data_cls, flags))
1507 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1514 * Resume transmitting announcement.
1517 * The announcement to resume.
1520 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1522 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1527 * Cancel announcement.
1530 * The announcement to cancel.
1533 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1535 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1540 * Obtain handle for a hosted place.
1542 * The returned handle can be used to access the place API.
1544 * @param host Handle for the host.
1546 * @return Handle for the hosted place, valid as long as @a host is valid.
1548 struct GNUNET_SOCIAL_Place *
1549 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1556 * Disconnect from a home.
1558 * Invalidates host handle.
1561 * The host to disconnect.
1564 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1565 GNUNET_ContinuationCallback disconnect_cb,
1568 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1570 plc->disconnect_cb = disconnect_cb;
1571 plc->disconnect_cls = cls;
1572 place_disconnect (plc);
1577 * Closure for #host_leave_cont.
1579 struct HostLeaveContext
1581 struct GNUNET_SOCIAL_Host *hst;
1582 GNUNET_ContinuationCallback disconnect_cb;
1583 void *disconnect_cb_cls;
1591 host_leave_cont (void *cls)
1593 struct HostLeaveContext *hlc = cls;
1595 GNUNET_SOCIAL_host_disconnect (hlc->hst,
1597 hlc->disconnect_cb_cls);
1603 * Stop hosting the home.
1605 * Sends a _notice_place_closing announcement to the home.
1606 * Invalidates host handle.
1611 * Environment for the message or NULL.
1612 * _nym is set to @e nym regardless whether an @e env is provided.
1613 * @param disconnect_cb
1614 * Function called after the host left the place
1615 * and disconnected from the social service.
1617 * Closure for @a disconnect_cb.
1620 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1621 const struct GNUNET_PSYC_Environment *env,
1622 GNUNET_ContinuationCallback disconnect_cb,
1625 struct GNUNET_MessageHeader *msg;
1626 struct GNUNET_MQ_Envelope *envelope;
1627 struct HostLeaveContext *hlc;
1629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1630 "GNUNET_SOCIAL_host_leave\n");
1631 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1632 GNUNET_SOCIAL_ANNOUNCE_NONE);
1633 envelope = GNUNET_MQ_msg (msg,
1634 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1635 hlc = GNUNET_new (struct HostLeaveContext);
1637 hlc->disconnect_cb = disconnect_cb;
1638 hlc->disconnect_cb_cls = cls;
1639 GNUNET_MQ_notify_sent (envelope,
1642 GNUNET_MQ_send (hst->plc.mq,
1651 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1655 guest_reconnect (void *cls)
1657 guest_connect (cls);
1662 * Guest client disconnected from service.
1664 * Reconnect after backoff period.
1667 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1669 struct GNUNET_SOCIAL_Guest *gst = cls;
1670 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1672 LOG (GNUNET_ERROR_TYPE_DEBUG,
1673 "Guest client disconnected (%d), re-connecting\n",
1675 if (NULL != plc->tmit)
1677 GNUNET_PSYC_transmit_destroy (plc->tmit);
1680 if (NULL != plc->mq)
1682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1683 "destroying MQ (guest_disconnected)\n");
1684 GNUNET_MQ_destroy (plc->mq);
1688 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1691 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1696 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1698 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1700 struct GNUNET_MQ_MessageHandler handlers[] = {
1701 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1702 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1703 struct GNUNET_PSYC_CountersResultMessage,
1705 GNUNET_MQ_hd_var_size (guest_enter_decision,
1706 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1707 struct GNUNET_PSYC_JoinDecisionMessage,
1709 GNUNET_MQ_hd_var_size (place_message,
1710 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1711 struct GNUNET_PSYC_MessageHeader,
1713 GNUNET_MQ_hd_fixed_size (place_message_ack,
1714 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1715 struct GNUNET_MessageHeader,
1717 GNUNET_MQ_hd_var_size (place_history_result,
1718 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1719 struct GNUNET_OperationResultMessage,
1721 GNUNET_MQ_hd_var_size (place_state_result,
1722 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1723 struct GNUNET_OperationResultMessage,
1725 GNUNET_MQ_hd_var_size (place_result,
1726 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1727 struct GNUNET_OperationResultMessage,
1729 GNUNET_MQ_handler_end ()
1732 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1733 handlers, guest_disconnected, gst);
1734 GNUNET_assert (NULL != plc->mq);
1735 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1737 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1741 static struct GNUNET_MQ_Envelope *
1742 guest_enter_request_create (const char *app_id,
1743 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1744 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1745 const struct GNUNET_PeerIdentity *origin,
1747 const struct GNUNET_PeerIdentity *relays,
1748 const struct GNUNET_PSYC_Message *join_msg)
1750 uint16_t app_id_size = strlen (app_id) + 1;
1751 uint16_t join_msg_size = ntohs (join_msg->header.size);
1752 uint16_t relay_size = relay_count * sizeof (*relays);
1754 struct GuestEnterRequest *greq;
1755 struct GNUNET_MQ_Envelope *
1756 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1757 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1758 greq->place_pub_key = *place_pub_key;
1759 greq->ego_pub_key = *ego_pub_key;
1760 greq->origin = *origin;
1761 greq->relay_count = htonl (relay_count);
1763 char *p = (char *) &greq[1];
1764 GNUNET_memcpy (p, app_id, app_id_size);
1769 GNUNET_memcpy (p, relays, relay_size);
1773 GNUNET_memcpy (p, join_msg, join_msg_size);
1779 * Request entry to a place as a guest.
1782 * Application handle.
1784 * Identity of the guest.
1785 * @param place_pub_key
1786 * Public key of the place to enter.
1788 * Flags for the entry.
1790 * Peer identity of the origin of the underlying multicast group.
1791 * @param relay_count
1792 * Number of elements in the @a relays array.
1794 * Relays for the underlying multicast group.
1795 * @param method_name
1796 * Method name for the message.
1798 * Environment containing variables for the message, or NULL.
1800 * Payload for the message to give to the enter callback.
1802 * Number of bytes in @a data.
1804 * Slicer to use for processing incoming requests from guests.
1806 * @return NULL on errors, otherwise handle for the guest.
1808 struct GNUNET_SOCIAL_Guest *
1809 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1810 const struct GNUNET_SOCIAL_Ego *ego,
1811 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1812 enum GNUNET_PSYC_SlaveJoinFlags flags,
1813 const struct GNUNET_PeerIdentity *origin,
1814 uint32_t relay_count,
1815 const struct GNUNET_PeerIdentity *relays,
1816 const struct GNUNET_PSYC_Message *entry_msg,
1817 struct GNUNET_PSYC_Slicer *slicer,
1818 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1819 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1822 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1823 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1825 plc->ego_pub_key = ego->pub_key;
1826 plc->pub_key = *place_pub_key;
1827 plc->cfg = app->cfg;
1828 plc->is_host = GNUNET_NO;
1829 plc->slicer = slicer;
1831 plc->op = GNUNET_OP_create ();
1834 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1835 origin, relay_count, relays, entry_msg);
1837 gst->enter_cb = local_enter_cb;
1838 gst->entry_dcsn_cb = entry_dcsn_cb;
1841 guest_connect (gst);
1847 * Request entry to a place by name as a guest.
1850 * Application handle.
1852 * Identity of the guest.
1854 * GNS name of the place to enter. Either in the form of
1855 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1856 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1857 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1858 * pseudonym's place directly.
1860 * Password to decrypt the record, or NULL for cleartext records.
1862 * Entry request message or NULL.
1864 * Slicer to use for processing incoming requests from guests.
1865 * @param local_enter_cb
1866 * Called upon connection established to the social service.
1867 * @param entry_decision_cb
1868 * Called upon receiving entry decision.
1870 * @return NULL on errors, otherwise handle for the guest.
1872 struct GNUNET_SOCIAL_Guest *
1873 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1874 const struct GNUNET_SOCIAL_Ego *ego,
1875 const char *gns_name,
1876 const char *password,
1877 const struct GNUNET_PSYC_Message *join_msg,
1878 struct GNUNET_PSYC_Slicer *slicer,
1879 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1880 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1883 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1884 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1886 if (NULL == password)
1889 uint16_t app_id_size = strlen (app->id) + 1;
1890 uint16_t gns_name_size = strlen (gns_name) + 1;
1891 uint16_t password_size = strlen (password) + 1;
1893 uint16_t join_msg_size = 0;
1894 if (NULL != join_msg)
1895 join_msg_size = ntohs (join_msg->header.size);
1897 struct GuestEnterByNameRequest *greq;
1899 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1900 + password_size + join_msg_size,
1901 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1903 greq->ego_pub_key = ego->pub_key;
1905 char *p = (char *) &greq[1];
1906 GNUNET_memcpy (p, app->id, app_id_size);
1908 GNUNET_memcpy (p, gns_name, gns_name_size);
1910 GNUNET_memcpy (p, password, password_size);
1912 if (NULL != join_msg)
1913 GNUNET_memcpy (p, join_msg, join_msg_size);
1915 plc->ego_pub_key = ego->pub_key;
1916 plc->cfg = app->cfg;
1917 plc->is_host = GNUNET_NO;
1918 plc->slicer = slicer;
1920 plc->op = GNUNET_OP_create ();
1922 gst->enter_cb = local_enter_cb;
1923 gst->entry_dcsn_cb = entry_decision_cb;
1926 guest_connect (gst);
1932 * Reconnect to an already entered place as guest.
1935 * Guest connection handle.
1936 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1938 * Flags for the entry.
1940 * Slicer to use for processing incoming requests from guests.
1941 * @param local_enter_cb
1942 * Called upon connection established to the social service.
1943 * @param entry_decision_cb
1944 * Called upon receiving entry decision.
1946 * @return NULL on errors, otherwise handle for the guest.
1948 struct GNUNET_SOCIAL_Guest *
1949 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1950 enum GNUNET_PSYC_SlaveJoinFlags flags,
1951 struct GNUNET_PSYC_Slicer *slicer,
1952 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1955 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1956 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1958 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1959 struct GuestEnterRequest *greq;
1961 = GNUNET_MQ_msg_extra (greq, app_id_size,
1962 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1963 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1964 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1965 greq->flags = htonl (flags);
1967 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1969 plc->cfg = gconn->app->cfg;
1970 plc->is_host = GNUNET_NO;
1971 plc->slicer = slicer;
1972 plc->pub_key = gconn->plc_msg.place_pub_key;
1973 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1975 plc->op = GNUNET_OP_create ();
1977 gst->enter_cb = local_enter_cb;
1980 guest_connect (gst);
1986 * Talk to the host of the place.
1989 * Place where we want to talk to the host.
1990 * @param method_name
1991 * Method to invoke on the host.
1993 * Environment containing variables for the message, or NULL.
1994 * @param notify_data
1995 * Function to use to get the payload for the method.
1996 * @param notify_data_cls
1997 * Closure for @a notify_data.
1999 * Flags for the message being sent.
2001 * @return NULL if we are already trying to talk to the host,
2002 * otherwise handle to cancel the request.
2004 struct GNUNET_SOCIAL_TalkRequest *
2005 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2006 const char *method_name,
2007 const struct GNUNET_PSYC_Environment *env,
2008 GNUNET_PSYC_TransmitNotifyData notify_data,
2009 void *notify_data_cls,
2010 enum GNUNET_SOCIAL_TalkFlags flags)
2012 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2013 GNUNET_assert (NULL != plc->tmit);
2016 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2017 NULL, notify_data, notify_data_cls, flags))
2018 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2025 * Resume talking to the host of the place.
2028 * Talk request to resume.
2031 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2033 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2038 * Cancel talking to the host of the place.
2041 * Talk request to cancel.
2044 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2046 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2051 * Disconnect from a place.
2053 * Invalidates guest handle.
2056 * The guest to disconnect.
2059 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2060 GNUNET_ContinuationCallback disconnect_cb,
2063 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2066 "GNUNET_SOCIAL_guest_disconnect, gst = %p\n",
2068 plc->disconnect_cb = disconnect_cb;
2069 plc->disconnect_cls = cls;
2070 place_disconnect (plc);
2075 * Closure for #leave_done_cont.
2079 struct GNUNET_SOCIAL_Guest *gst;
2080 GNUNET_ContinuationCallback disconnect_cb;
2081 void *disconnect_cb_cls;
2086 * The leave message was transmitted, now complete the
2087 * disconnection process.
2089 * @param cls a `struct LeaveContext`
2092 leave_done_cont (void *cls)
2094 struct LeaveContext *lc = cls;
2096 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2097 "social_api: guest's place_leave message sent!\n");
2098 GNUNET_SOCIAL_guest_disconnect (lc->gst,
2100 lc->disconnect_cb_cls);
2106 * Leave a place temporarily or permanently.
2108 * Notifies the owner of the place about leaving, and destroys the place handle.
2112 * @param keep_active
2113 * Keep place active after last application disconnected.
2114 * #GNUNET_YES or #GNUNET_NO
2116 * Optional environment for the leave message if @a keep_active
2117 * is #GNUNET_NO. NULL if not needed.
2119 * Called upon disconnecting from the social service.
2122 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2123 struct GNUNET_PSYC_Environment *env,
2124 GNUNET_ContinuationCallback disconnect_cb,
2127 struct GNUNET_MessageHeader *msg;
2128 struct GNUNET_MQ_Envelope *envelope;
2129 struct LeaveContext *lc;
2131 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2132 GNUNET_SOCIAL_TALK_NONE);
2135 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2136 "social_api: place_leave\n");
2137 envelope = GNUNET_MQ_msg (msg,
2138 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
2139 lc = GNUNET_new (struct LeaveContext);
2141 lc->disconnect_cb = disconnect_cb;
2142 lc->disconnect_cb_cls = cls;
2143 GNUNET_MQ_notify_sent (envelope,
2146 GNUNET_MQ_send (gst->plc.mq,
2152 * Obtain handle for a place entered as guest.
2154 * The returned handle can be used to access the place API.
2156 * @param guest Handle for the guest.
2158 * @return Handle for the place, valid as long as @a guest is valid.
2160 struct GNUNET_SOCIAL_Place *
2161 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2168 * Obtain the public key of a place.
2173 * @return Public key of the place.
2175 const struct GNUNET_CRYPTO_EddsaPublicKey *
2176 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2178 return &plc->pub_key;
2183 * Set message processing @a flags for a @a method_prefix.
2187 * @param method_prefix
2188 * Method prefix @a flags apply to.
2190 * The flags that apply to a matching @a method_prefix.
2193 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2194 const char *method_prefix,
2195 enum GNUNET_SOCIAL_MsgProcFlags flags)
2197 GNUNET_assert (NULL != method_prefix);
2198 struct MsgProcRequest *mpreq;
2199 uint16_t method_size = strnlen (method_prefix,
2200 GNUNET_MAX_MESSAGE_SIZE
2201 - sizeof (*mpreq)) + 1;
2202 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2204 struct GNUNET_MQ_Envelope *
2205 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2206 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2207 mpreq->flags = htonl (flags);
2208 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2210 GNUNET_MQ_send (plc->mq, env);
2215 * Clear all message processing flags previously set for this place.
2218 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2220 struct GNUNET_MessageHeader *req;
2221 struct GNUNET_MQ_Envelope *
2222 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2224 GNUNET_MQ_send (plc->mq, env);
2228 static struct GNUNET_SOCIAL_HistoryRequest *
2229 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2230 uint64_t start_message_id,
2231 uint64_t end_message_id,
2232 uint64_t message_limit,
2233 const char *method_prefix,
2235 struct GNUNET_PSYC_Slicer *slicer,
2236 GNUNET_ResultCallback result_cb,
2239 struct GNUNET_PSYC_HistoryRequestMessage *req;
2240 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2242 hist->slicer = slicer;
2243 hist->result_cb = result_cb;
2245 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2247 GNUNET_assert (NULL != method_prefix);
2248 uint16_t method_size = strnlen (method_prefix,
2249 GNUNET_MAX_MESSAGE_SIZE
2250 - sizeof (*req)) + 1;
2251 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2253 struct GNUNET_MQ_Envelope *
2254 env = GNUNET_MQ_msg_extra (req, method_size,
2255 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2256 req->start_message_id = GNUNET_htonll (start_message_id);
2257 req->end_message_id = GNUNET_htonll (end_message_id);
2258 req->message_limit = GNUNET_htonll (message_limit);
2259 req->flags = htonl (flags);
2260 req->op_id = GNUNET_htonll (hist->op_id);
2261 GNUNET_memcpy (&req[1], method_prefix, method_size);
2263 GNUNET_MQ_send (plc->mq, env);
2269 * Learn about the history of a place.
2271 * Messages are returned through the @a slicer function
2272 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2275 * Place we want to learn more about.
2276 * @param start_message_id
2277 * First historic message we are interested in.
2278 * @param end_message_id
2279 * Last historic message we are interested in (inclusive).
2280 * @param method_prefix
2281 * Only retrieve messages with this method prefix.
2283 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2285 * Slicer to use for retrieved messages.
2286 * Can be the same as the slicer of the place.
2288 * Function called after all messages retrieved.
2289 * NULL if not needed.
2290 * @param cls Closure for @a result_cb.
2292 struct GNUNET_SOCIAL_HistoryRequest *
2293 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2294 uint64_t start_message_id,
2295 uint64_t end_message_id,
2296 const char *method_prefix,
2298 struct GNUNET_PSYC_Slicer *slicer,
2299 GNUNET_ResultCallback result_cb,
2302 return place_history_replay (plc, start_message_id, end_message_id, 0,
2303 method_prefix, flags, slicer, result_cb, cls);
2308 * Learn about the history of a place.
2310 * Sends messages through the slicer function of the place where
2311 * start_message_id <= message_id <= end_message_id.
2312 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2314 * To get the latest message, use 0 for both the start and end message ID.
2317 * Place we want to learn more about.
2318 * @param message_limit
2319 * Maximum number of historic messages we are interested in.
2320 * @param method_prefix
2321 * Only retrieve messages with this method prefix.
2323 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2325 * Function called after all messages retrieved.
2326 * NULL if not needed.
2327 * @param cls Closure for @a result_cb.
2329 struct GNUNET_SOCIAL_HistoryRequest *
2330 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2331 uint64_t message_limit,
2332 const char *method_prefix,
2334 struct GNUNET_PSYC_Slicer *slicer,
2335 GNUNET_ResultCallback result_cb,
2338 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2339 slicer, result_cb, cls);
2344 * Cancel learning about the history of a place.
2347 * History lesson to cancel.
2350 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2352 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2358 * Request matching state variables.
2360 static struct GNUNET_SOCIAL_LookHandle *
2361 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2362 uint16_t type, const char *name,
2363 GNUNET_PSYC_StateVarCallback var_cb,
2364 GNUNET_ResultCallback result_cb, void *cls)
2366 struct GNUNET_PSYC_StateRequestMessage *req;
2367 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2369 look->var_cb = var_cb;
2370 look->result_cb = result_cb;
2372 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2374 GNUNET_assert (NULL != name);
2375 size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
2376 - sizeof (*req)) + 1;
2377 struct GNUNET_MQ_Envelope *
2378 env = GNUNET_MQ_msg_extra (req, name_size, type);
2379 req->op_id = GNUNET_htonll (look->op_id);
2380 GNUNET_memcpy (&req[1], name, name_size);
2382 GNUNET_MQ_send (plc->mq, env);
2388 * Look at a particular object in the place.
2390 * The best matching object is returned (its name might be less specific than
2391 * what was requested).
2394 * The place where to look.
2396 * Full name of the object.
2398 * Set to the size of the returned value.
2400 * @return NULL if there is no such object at this place.
2402 struct GNUNET_SOCIAL_LookHandle *
2403 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2404 const char *full_name,
2405 GNUNET_PSYC_StateVarCallback var_cb,
2406 GNUNET_ResultCallback result_cb,
2409 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2410 full_name, var_cb, result_cb, cls);
2415 * Look for objects in the place with a matching name prefix.
2418 * The place where to look.
2419 * @param name_prefix
2420 * Look at objects with names beginning with this value.
2422 * Function to call for each object found.
2424 * Closure for callback function.
2426 * @return Handle that can be used to stop looking at objects.
2428 struct GNUNET_SOCIAL_LookHandle *
2429 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2430 const char *name_prefix,
2431 GNUNET_PSYC_StateVarCallback var_cb,
2432 GNUNET_ResultCallback result_cb,
2435 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2436 name_prefix, var_cb, result_cb, cls);
2441 * Cancel a state request operation.
2444 * Handle for the operation to cancel.
2447 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2449 GNUNET_OP_remove (look->plc->op, look->op_id);
2455 op_recv_zone_add_place_result (void *cls, int64_t result,
2456 const void *err_msg, uint16_t err_msg_size)
2458 LOG (GNUNET_ERROR_TYPE_DEBUG,
2459 "Received zone add place result: %" PRId64 ".\n", result);
2461 struct ZoneAddPlaceHandle *add_plc = cls;
2462 if (NULL != add_plc->result_cb)
2463 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2465 GNUNET_free (add_plc);
2470 * Advertise @e place in the GNS zone of @e ego.
2473 * Application handle.
2476 * @param place_pub_key
2477 * Public key of place to add.
2479 * The name for the PLACE record to put in the zone.
2481 * Password used to encrypt the record or NULL to keep it cleartext.
2482 * @param relay_count
2483 * Number of elements in the @a relays array.
2485 * List of relays to put in the PLACE record to advertise
2486 * as entry points to the place in addition to the origin.
2487 * @param expiration_time
2488 * Expiration time of the record, use 0 to remove the record.
2490 * Function called with the result of the operation.
2492 * Closure for @a result_cb
2494 * @return #GNUNET_OK if the request was sent,
2495 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2498 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2499 const struct GNUNET_SOCIAL_Ego *ego,
2501 const char *password,
2502 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2503 const struct GNUNET_PeerIdentity *origin,
2504 uint32_t relay_count,
2505 const struct GNUNET_PeerIdentity *relays,
2506 struct GNUNET_TIME_Absolute expiration_time,
2507 GNUNET_ResultCallback result_cb,
2510 struct ZoneAddPlaceRequest *preq;
2511 size_t name_size = strlen (name) + 1;
2512 size_t password_size = strlen (password) + 1;
2513 size_t relay_size = relay_count * sizeof (*relays);
2514 size_t payload_size = name_size + password_size + relay_size;
2516 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2517 return GNUNET_SYSERR;
2519 struct GNUNET_MQ_Envelope *
2520 env = GNUNET_MQ_msg_extra (preq, payload_size,
2521 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2522 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2523 preq->ego_pub_key = ego->pub_key;
2524 preq->place_pub_key = *place_pub_key;
2525 preq->origin = *origin;
2526 preq->relay_count = htonl (relay_count);
2528 char *p = (char *) &preq[1];
2529 GNUNET_memcpy (p, name, name_size);
2531 GNUNET_memcpy (p, password, password_size);
2533 GNUNET_memcpy (p, relays, relay_size);
2535 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2536 add_plc->result_cb = result_cb;
2537 add_plc->result_cls = result_cls;
2539 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2540 op_recv_zone_add_place_result,
2543 GNUNET_MQ_send (app->mq, env);
2549 op_recv_zone_add_nym_result (void *cls, int64_t result,
2550 const void *err_msg, uint16_t err_msg_size)
2552 LOG (GNUNET_ERROR_TYPE_DEBUG,
2553 "Received zone add nym result: %" PRId64 ".\n", result);
2555 struct ZoneAddNymHandle *add_nym = cls;
2556 if (NULL != add_nym->result_cb)
2557 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2559 GNUNET_free (add_nym);
2564 * Add nym to the GNS zone of @e ego.
2571 * The name for the PKEY record to put in the zone.
2572 * @param nym_pub_key
2573 * Public key of nym to add.
2574 * @param expiration_time
2575 * Expiration time of the record, use 0 to remove the record.
2577 * Function called with the result of the operation.
2579 * Closure for @a result_cb
2581 * @return #GNUNET_OK if the request was sent,
2582 * #GNUNET_SYSERR on error, e.g. the name is too long.
2585 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2586 const struct GNUNET_SOCIAL_Ego *ego,
2588 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2589 struct GNUNET_TIME_Absolute expiration_time,
2590 GNUNET_ResultCallback result_cb,
2593 struct ZoneAddNymRequest *nreq;
2595 size_t name_size = strlen (name) + 1;
2596 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2597 return GNUNET_SYSERR;
2599 struct GNUNET_MQ_Envelope *
2600 env = GNUNET_MQ_msg_extra (nreq, name_size,
2601 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2602 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2603 nreq->ego_pub_key = ego->pub_key;
2604 nreq->nym_pub_key = *nym_pub_key;
2605 GNUNET_memcpy (&nreq[1], name, name_size);
2607 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2608 add_nym->result_cb = result_cb;
2609 add_nym->result_cls = result_cls;
2611 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2612 op_recv_zone_add_nym_result,
2615 GNUNET_MQ_send (app->mq, env);
2624 app_connect (struct GNUNET_SOCIAL_App *app);
2628 app_reconnect (void *cls)
2635 * App client disconnected from service.
2637 * Reconnect after backoff period.
2640 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2642 struct GNUNET_SOCIAL_App *app = cls;
2644 LOG (GNUNET_ERROR_TYPE_DEBUG,
2645 "App client disconnected (%d), re-connecting\n",
2647 if (NULL != app->mq)
2649 GNUNET_MQ_destroy (app->mq);
2653 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2656 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2661 app_connect (struct GNUNET_SOCIAL_App *app)
2663 struct GNUNET_MQ_MessageHandler handlers[] = {
2664 GNUNET_MQ_hd_var_size (app_ego,
2665 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2666 struct AppEgoMessage,
2668 GNUNET_MQ_hd_fixed_size (app_ego_end,
2669 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2670 struct GNUNET_MessageHeader,
2672 GNUNET_MQ_hd_var_size (app_place,
2673 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2674 struct AppPlaceMessage,
2676 GNUNET_MQ_hd_fixed_size (app_place_end,
2677 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2678 struct GNUNET_MessageHeader,
2680 GNUNET_MQ_hd_var_size (app_result,
2681 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2682 struct GNUNET_OperationResultMessage,
2684 GNUNET_MQ_handler_end ()
2687 app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
2688 handlers, app_disconnected, app);
2689 GNUNET_assert (NULL != app->mq);
2690 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2695 * Connect application to the social service.
2697 * The @host_place_cb and @guest_place_cb functions are
2698 * initially called for each entered places,
2699 * then later each time a new place is entered with the current application ID.
2706 * Function to notify about an available ego.
2708 * Function to notify about a place entered as host.
2710 * Function to notify about a place entered as guest.
2712 * Closure for the callbacks.
2714 * @return Handle that can be used to stop listening.
2716 struct GNUNET_SOCIAL_App *
2717 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2719 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2720 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2721 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2722 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2725 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2726 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2730 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2732 app->ego_cb = ego_cb;
2733 app->host_cb = host_cb;
2734 app->guest_cb = guest_cb;
2735 app->connected_cb = connected_cb;
2737 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2738 app->op = GNUNET_OP_create ();
2739 app->id = GNUNET_malloc (app_id_size);
2740 GNUNET_memcpy (app->id, id, app_id_size);
2742 struct AppConnectRequest *creq;
2743 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2744 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2745 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2753 app_cleanup (struct GNUNET_SOCIAL_App *app)
2755 if (NULL != app->mq)
2757 GNUNET_MQ_destroy (app->mq);
2760 if (NULL != app->disconnect_cb)
2762 app->disconnect_cb (app->disconnect_cls);
2763 app->disconnect_cb = NULL;
2769 * Disconnect application.
2772 * Application handle.
2773 * @param disconnect_cb
2774 * Disconnect callback.
2775 * @param disconnect_cls
2776 * Disconnect closure.
2779 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2780 GNUNET_ContinuationCallback disconnect_cb,
2781 void *disconnect_cls)
2783 app->disconnect_cb = disconnect_cb;
2784 app->disconnect_cls = disconnect_cls;
2786 if (NULL != app->mq)
2788 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2791 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
2806 * Detach application from a place.
2808 * Removes the place from the entered places list for this application.
2809 * Note: this does not disconnect from the place.
2811 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2819 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2820 struct GNUNET_SOCIAL_Place *plc)
2822 struct AppDetachRequest *dreq;
2823 struct GNUNET_MQ_Envelope *
2824 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2825 dreq->place_pub_key = plc->pub_key;
2826 dreq->ego_pub_key = plc->ego_pub_key;
2828 GNUNET_MQ_send (app->mq, env);
2832 /* end of social_api.c */