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;
1047 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1049 "%s place cleanup: %s\n",
1050 GNUNET_YES == plc->is_host ? "host" : "guest",
1051 GNUNET_h2s (&place_pub_hash));
1053 if (NULL != plc->tmit)
1055 GNUNET_PSYC_transmit_destroy (plc->tmit);
1058 if (NULL != plc->connect_env)
1060 GNUNET_MQ_discard (plc->connect_env);
1061 plc->connect_env = NULL;
1063 if (NULL != plc->mq)
1065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1066 "destroying MQ (place_cleanup)\n");
1067 GNUNET_MQ_destroy (plc->mq);
1070 if (NULL != plc->disconnect_cb)
1072 plc->disconnect_cb (plc->disconnect_cls);
1073 plc->disconnect_cb = NULL;
1076 (GNUNET_YES == plc->is_host)
1077 ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
1078 : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
1083 place_disconnect (struct GNUNET_SOCIAL_Place *plc)
1085 struct GNUNET_HashCode place_pub_hash;
1086 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1088 "place_disconnect, plc = %s\n",
1089 GNUNET_h2s (&place_pub_hash));
1090 if (NULL != plc->mq)
1092 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (plc->mq);
1095 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) place_cleanup, plc);
1099 place_cleanup (plc);
1104 place_cleanup (plc);
1110 place_leave (struct GNUNET_SOCIAL_Place *plc)
1112 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1113 "social_api: place_leave\n");
1114 struct GNUNET_MessageHeader *msg;
1115 struct GNUNET_MQ_Envelope *
1116 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1118 GNUNET_MQ_send (plc->mq, env);
1126 host_connect (struct GNUNET_SOCIAL_Host *hst);
1130 host_reconnect (void *cls)
1137 * Host client disconnected from service.
1139 * Reconnect after backoff period.
1142 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1144 struct GNUNET_SOCIAL_Host *hst = cls;
1145 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1148 "Host client disconnected (%d), re-connecting\n",
1150 if (NULL != plc->tmit)
1152 GNUNET_PSYC_transmit_destroy (plc->tmit);
1155 if (NULL != plc->mq)
1157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1158 "destroying MQ (host_disconnected)\n");
1159 GNUNET_MQ_destroy (plc->mq);
1163 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1166 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1171 host_connect (struct GNUNET_SOCIAL_Host *hst)
1173 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1175 struct GNUNET_MQ_MessageHandler handlers[] = {
1176 GNUNET_MQ_hd_fixed_size (host_enter_ack,
1177 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1178 struct HostEnterAck,
1180 GNUNET_MQ_hd_var_size (host_enter_request,
1181 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1182 struct GNUNET_PSYC_JoinRequestMessage,
1184 GNUNET_MQ_hd_var_size (host_message,
1185 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1186 struct GNUNET_PSYC_MessageHeader,
1188 GNUNET_MQ_hd_fixed_size (place_message_ack,
1189 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1190 struct GNUNET_MessageHeader,
1192 GNUNET_MQ_hd_var_size (place_history_result,
1193 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1194 struct GNUNET_OperationResultMessage,
1196 GNUNET_MQ_hd_var_size (place_state_result,
1197 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1198 struct GNUNET_OperationResultMessage,
1200 GNUNET_MQ_hd_var_size (place_result,
1201 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1202 struct GNUNET_OperationResultMessage,
1204 GNUNET_MQ_handler_end ()
1207 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1208 handlers, host_disconnected, hst);
1209 GNUNET_assert (NULL != plc->mq);
1210 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1212 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1217 * Enter a place as host.
1219 * A place is created upon first entering, and it is active until permanently
1220 * left using GNUNET_SOCIAL_host_leave().
1223 * Application handle.
1225 * Identity of the host.
1227 * Policy specifying entry and history restrictions for the place.
1229 * Slicer to handle incoming messages.
1231 * Function called when the place is entered and ready to use.
1232 * @param answer_door_cb
1233 * Function to handle new nyms that want to enter.
1234 * @param farewell_cb
1235 * Function to handle departing nyms.
1237 * Closure for the callbacks.
1239 * @return Handle for the host. This handle contains the pubkey.
1241 struct GNUNET_SOCIAL_Host *
1242 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1243 const struct GNUNET_SOCIAL_Ego *ego,
1244 enum GNUNET_PSYC_Policy policy,
1245 struct GNUNET_PSYC_Slicer *slicer,
1246 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1247 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1248 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1251 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1252 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1254 plc->cfg = app->cfg;
1255 plc->is_host = GNUNET_YES;
1256 plc->slicer = slicer;
1258 hst->enter_cb = enter_cb;
1259 hst->answer_door_cb = answer_door_cb;
1260 hst->farewell_cb = farewell_cb;
1263 plc->op = GNUNET_OP_create ();
1265 hst->slicer = GNUNET_PSYC_slicer_create ();
1266 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1267 host_recv_notice_place_leave_method,
1268 host_recv_notice_place_leave_modifier,
1269 NULL, host_recv_notice_place_leave_eom, hst);
1271 uint16_t app_id_size = strlen (app->id) + 1;
1272 struct HostEnterRequest *hreq;
1273 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1274 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1275 hreq->policy = policy;
1276 hreq->ego_pub_key = ego->pub_key;
1277 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1285 * Reconnect to an already entered place as host.
1288 * Host connection handle.
1289 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1291 * Slicer to handle incoming messages.
1293 * Function called when the place is entered and ready to use.
1294 * @param answer_door_cb
1295 * Function to handle new nyms that want to enter.
1296 * @param farewell_cb
1297 * Function to handle departing nyms.
1299 * Closure for the callbacks.
1301 * @return Handle for the host.
1303 struct GNUNET_SOCIAL_Host *
1304 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1305 struct GNUNET_PSYC_Slicer *slicer,
1306 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1307 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1308 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1311 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1312 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1314 hst->enter_cb = enter_cb;
1315 hst->answer_door_cb = answer_door_cb;
1316 hst->farewell_cb = farewell_cb;
1319 plc->cfg = hconn->app->cfg;
1320 plc->is_host = GNUNET_YES;
1321 plc->slicer = slicer;
1322 plc->pub_key = hconn->plc_msg.place_pub_key;
1323 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1325 plc->op = GNUNET_OP_create ();
1327 hst->slicer = GNUNET_PSYC_slicer_create ();
1328 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1329 host_recv_notice_place_leave_method,
1330 host_recv_notice_place_leave_modifier,
1331 NULL, host_recv_notice_place_leave_eom, hst);
1333 size_t app_id_size = strlen (hconn->app->id) + 1;
1334 struct HostEnterRequest *hreq;
1335 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1336 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1337 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1338 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1339 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1347 * Decision whether to admit @a nym into the place or refuse entry.
1350 * Host of the place.
1352 * Handle for the entity that wanted to enter.
1353 * @param is_admitted
1354 * #GNUNET_YES if @a nym is admitted,
1355 * #GNUNET_NO if @a nym is refused entry,
1356 * #GNUNET_SYSERR if we cannot answer the request.
1357 * @param method_name
1358 * Method name for the rejection message.
1360 * Environment containing variables for the message, or NULL.
1362 * Data for the rejection message to send back.
1364 * Number of bytes in @a data for method.
1365 * @return #GNUNET_OK on success,
1366 * #GNUNET_SYSERR if the message is too large.
1369 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1370 struct GNUNET_SOCIAL_Nym *nym,
1372 const struct GNUNET_PSYC_Message *entry_resp)
1374 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1375 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1376 uint16_t entry_resp_size
1377 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1379 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1380 return GNUNET_SYSERR;
1382 struct GNUNET_MQ_Envelope *
1383 env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1384 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1385 dcsn->is_admitted = htonl (is_admitted);
1386 dcsn->slave_pub_key = nym->pub_key;
1388 if (0 < entry_resp_size)
1389 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1391 GNUNET_MQ_send (plc->mq, env);
1397 * Throw @a nym out of the place.
1399 * The @a nym reference will remain valid until the
1400 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1401 * which should be very soon after this call.
1404 * Host of the place.
1406 * Handle for the entity to be ejected.
1408 * Environment for the message or NULL.
1411 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1412 const struct GNUNET_SOCIAL_Nym *nym,
1413 struct GNUNET_PSYC_Environment *e)
1415 struct GNUNET_PSYC_Environment *env = e;
1417 env = GNUNET_PSYC_env_create ();
1418 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1419 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1420 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1421 GNUNET_SOCIAL_ANNOUNCE_NONE);
1423 GNUNET_PSYC_env_destroy (env);
1428 * Get the public key of @a ego.
1433 * @return Public key of ego.
1435 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1436 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1438 return &ego->pub_key;
1443 * Get the hash of the public key of @a ego.
1448 * @return Hash of the public key of @a ego.
1450 const struct GNUNET_HashCode *
1451 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1453 return &ego->pub_key_hash;
1458 * Get the name of @a ego.
1463 * @return Public key of @a ego.
1466 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1473 * Get the public key of @a nym.
1475 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1480 * @return Public key of @a nym.
1482 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1483 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1485 return &nym->pub_key;
1490 * Get the hash of the public key of @a nym.
1495 * @return Hash of the public key of @a nym.
1497 const struct GNUNET_HashCode *
1498 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1500 return &nym->pub_key_hash;
1505 * Send a message to all nyms that are present in the place.
1507 * This function is restricted to the host. Nyms can only send requests
1508 * to the host who can decide to relay it to everyone in the place.
1510 * @param host Host of the place.
1511 * @param method_name Method to use for the announcement.
1512 * @param env Environment containing variables for the message and operations
1513 * on objects of the place. Can be NULL.
1514 * @param notify Function to call to get the payload of the announcement.
1515 * @param notify_cls Closure for @a notify.
1516 * @param flags Flags for this announcement.
1518 * @return NULL on error (announcement already in progress?).
1520 struct GNUNET_SOCIAL_Announcement *
1521 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1522 const char *method_name,
1523 const struct GNUNET_PSYC_Environment *env,
1524 GNUNET_PSYC_TransmitNotifyData notify_data,
1525 void *notify_data_cls,
1526 enum GNUNET_SOCIAL_AnnounceFlags flags)
1529 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1530 NULL, notify_data, notify_data_cls, flags))
1531 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1538 * Resume transmitting announcement.
1541 * The announcement to resume.
1544 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1546 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1551 * Cancel announcement.
1554 * The announcement to cancel.
1557 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1559 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1564 * Obtain handle for a hosted place.
1566 * The returned handle can be used to access the place API.
1568 * @param host Handle for the host.
1570 * @return Handle for the hosted place, valid as long as @a host is valid.
1572 struct GNUNET_SOCIAL_Place *
1573 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1580 * Disconnect from a home.
1582 * Invalidates host handle.
1585 * The host to disconnect.
1588 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1589 GNUNET_ContinuationCallback disconnect_cb,
1592 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1594 plc->disconnect_cb = disconnect_cb;
1595 plc->disconnect_cls = cls;
1596 place_disconnect (plc);
1601 * Stop hosting the home.
1603 * Sends a _notice_place_closing announcement to the home.
1604 * Invalidates host handle.
1609 * Environment for the message or NULL.
1610 * _nym is set to @e nym regardless whether an @e env is provided.
1611 * @param disconnect_cb
1612 * Function called after the host left the place
1613 * and disconnected from the social service.
1615 * Closure for @a disconnect_cb.
1618 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1619 const struct GNUNET_PSYC_Environment *env,
1620 GNUNET_ContinuationCallback disconnect_cb,
1623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1624 "GNUNET_SOCIAL_host_leave\n");
1625 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1626 GNUNET_SOCIAL_ANNOUNCE_NONE);
1627 place_leave (&hst->plc);
1628 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1636 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1640 guest_reconnect (void *cls)
1642 guest_connect (cls);
1647 * Guest client disconnected from service.
1649 * Reconnect after backoff period.
1652 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1654 struct GNUNET_SOCIAL_Guest *gst = cls;
1655 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1657 LOG (GNUNET_ERROR_TYPE_DEBUG,
1658 "Guest client disconnected (%d), re-connecting\n",
1660 if (NULL != plc->tmit)
1662 GNUNET_PSYC_transmit_destroy (plc->tmit);
1665 if (NULL != plc->mq)
1667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1668 "destroying MQ (guest_disconnected)\n");
1669 GNUNET_MQ_destroy (plc->mq);
1673 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1676 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1681 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1683 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1685 struct GNUNET_MQ_MessageHandler handlers[] = {
1686 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1687 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1688 struct GNUNET_PSYC_CountersResultMessage,
1690 GNUNET_MQ_hd_var_size (guest_enter_decision,
1691 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1692 struct GNUNET_PSYC_JoinDecisionMessage,
1694 GNUNET_MQ_hd_var_size (place_message,
1695 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1696 struct GNUNET_PSYC_MessageHeader,
1698 GNUNET_MQ_hd_fixed_size (place_message_ack,
1699 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1700 struct GNUNET_MessageHeader,
1702 GNUNET_MQ_hd_var_size (place_history_result,
1703 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1704 struct GNUNET_OperationResultMessage,
1706 GNUNET_MQ_hd_var_size (place_state_result,
1707 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1708 struct GNUNET_OperationResultMessage,
1710 GNUNET_MQ_hd_var_size (place_result,
1711 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1712 struct GNUNET_OperationResultMessage,
1714 GNUNET_MQ_handler_end ()
1717 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1718 handlers, guest_disconnected, gst);
1719 GNUNET_assert (NULL != plc->mq);
1720 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1722 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1726 static struct GNUNET_MQ_Envelope *
1727 guest_enter_request_create (const char *app_id,
1728 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1729 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1730 const struct GNUNET_PeerIdentity *origin,
1732 const struct GNUNET_PeerIdentity *relays,
1733 const struct GNUNET_PSYC_Message *join_msg)
1735 uint16_t app_id_size = strlen (app_id) + 1;
1736 uint16_t join_msg_size = ntohs (join_msg->header.size);
1737 uint16_t relay_size = relay_count * sizeof (*relays);
1739 struct GuestEnterRequest *greq;
1740 struct GNUNET_MQ_Envelope *
1741 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1742 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1743 greq->place_pub_key = *place_pub_key;
1744 greq->ego_pub_key = *ego_pub_key;
1745 greq->origin = *origin;
1746 greq->relay_count = htonl (relay_count);
1748 char *p = (char *) &greq[1];
1749 GNUNET_memcpy (p, app_id, app_id_size);
1754 GNUNET_memcpy (p, relays, relay_size);
1758 GNUNET_memcpy (p, join_msg, join_msg_size);
1764 * Request entry to a place as a guest.
1767 * Application handle.
1769 * Identity of the guest.
1770 * @param place_pub_key
1771 * Public key of the place to enter.
1773 * Flags for the entry.
1775 * Peer identity of the origin of the underlying multicast group.
1776 * @param relay_count
1777 * Number of elements in the @a relays array.
1779 * Relays for the underlying multicast group.
1780 * @param method_name
1781 * Method name for the message.
1783 * Environment containing variables for the message, or NULL.
1785 * Payload for the message to give to the enter callback.
1787 * Number of bytes in @a data.
1789 * Slicer to use for processing incoming requests from guests.
1791 * @return NULL on errors, otherwise handle for the guest.
1793 struct GNUNET_SOCIAL_Guest *
1794 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1795 const struct GNUNET_SOCIAL_Ego *ego,
1796 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1797 enum GNUNET_PSYC_SlaveJoinFlags flags,
1798 const struct GNUNET_PeerIdentity *origin,
1799 uint32_t relay_count,
1800 const struct GNUNET_PeerIdentity *relays,
1801 const struct GNUNET_PSYC_Message *entry_msg,
1802 struct GNUNET_PSYC_Slicer *slicer,
1803 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1804 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1807 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1808 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1810 plc->ego_pub_key = ego->pub_key;
1811 plc->pub_key = *place_pub_key;
1812 plc->cfg = app->cfg;
1813 plc->is_host = GNUNET_NO;
1814 plc->slicer = slicer;
1816 plc->op = GNUNET_OP_create ();
1819 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1820 origin, relay_count, relays, entry_msg);
1822 gst->enter_cb = local_enter_cb;
1823 gst->entry_dcsn_cb = entry_dcsn_cb;
1826 guest_connect (gst);
1832 * Request entry to a place by name as a guest.
1835 * Application handle.
1837 * Identity of the guest.
1839 * GNS name of the place to enter. Either in the form of
1840 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1841 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1842 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1843 * pseudonym's place directly.
1845 * Password to decrypt the record, or NULL for cleartext records.
1847 * Entry request message or NULL.
1849 * Slicer to use for processing incoming requests from guests.
1850 * @param local_enter_cb
1851 * Called upon connection established to the social service.
1852 * @param entry_decision_cb
1853 * Called upon receiving entry decision.
1855 * @return NULL on errors, otherwise handle for the guest.
1857 struct GNUNET_SOCIAL_Guest *
1858 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1859 const struct GNUNET_SOCIAL_Ego *ego,
1860 const char *gns_name,
1861 const char *password,
1862 const struct GNUNET_PSYC_Message *join_msg,
1863 struct GNUNET_PSYC_Slicer *slicer,
1864 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1865 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1868 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1869 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1871 if (NULL == password)
1874 uint16_t app_id_size = strlen (app->id) + 1;
1875 uint16_t gns_name_size = strlen (gns_name) + 1;
1876 uint16_t password_size = strlen (password) + 1;
1878 uint16_t join_msg_size = 0;
1879 if (NULL != join_msg)
1880 join_msg_size = ntohs (join_msg->header.size);
1882 struct GuestEnterByNameRequest *greq;
1884 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1885 + password_size + join_msg_size,
1886 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1888 greq->ego_pub_key = ego->pub_key;
1890 char *p = (char *) &greq[1];
1891 GNUNET_memcpy (p, app->id, app_id_size);
1893 GNUNET_memcpy (p, gns_name, gns_name_size);
1895 GNUNET_memcpy (p, password, password_size);
1897 if (NULL != join_msg)
1898 GNUNET_memcpy (p, join_msg, join_msg_size);
1900 plc->ego_pub_key = ego->pub_key;
1901 plc->cfg = app->cfg;
1902 plc->is_host = GNUNET_NO;
1903 plc->slicer = slicer;
1905 plc->op = GNUNET_OP_create ();
1907 gst->enter_cb = local_enter_cb;
1908 gst->entry_dcsn_cb = entry_decision_cb;
1911 guest_connect (gst);
1917 * Reconnect to an already entered place as guest.
1920 * Guest connection handle.
1921 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1923 * Flags for the entry.
1925 * Slicer to use for processing incoming requests from guests.
1926 * @param local_enter_cb
1927 * Called upon connection established to the social service.
1928 * @param entry_decision_cb
1929 * Called upon receiving entry decision.
1931 * @return NULL on errors, otherwise handle for the guest.
1933 struct GNUNET_SOCIAL_Guest *
1934 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1935 enum GNUNET_PSYC_SlaveJoinFlags flags,
1936 struct GNUNET_PSYC_Slicer *slicer,
1937 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1940 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1941 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1943 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1944 struct GuestEnterRequest *greq;
1946 = GNUNET_MQ_msg_extra (greq, app_id_size,
1947 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1948 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1949 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1950 greq->flags = htonl (flags);
1952 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1954 plc->cfg = gconn->app->cfg;
1955 plc->is_host = GNUNET_NO;
1956 plc->slicer = slicer;
1957 plc->pub_key = gconn->plc_msg.place_pub_key;
1958 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1960 plc->op = GNUNET_OP_create ();
1962 gst->enter_cb = local_enter_cb;
1965 guest_connect (gst);
1971 * Talk to the host of the place.
1974 * Place where we want to talk to the host.
1975 * @param method_name
1976 * Method to invoke on the host.
1978 * Environment containing variables for the message, or NULL.
1979 * @param notify_data
1980 * Function to use to get the payload for the method.
1981 * @param notify_data_cls
1982 * Closure for @a notify_data.
1984 * Flags for the message being sent.
1986 * @return NULL if we are already trying to talk to the host,
1987 * otherwise handle to cancel the request.
1989 struct GNUNET_SOCIAL_TalkRequest *
1990 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1991 const char *method_name,
1992 const struct GNUNET_PSYC_Environment *env,
1993 GNUNET_PSYC_TransmitNotifyData notify_data,
1994 void *notify_data_cls,
1995 enum GNUNET_SOCIAL_TalkFlags flags)
1997 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1998 GNUNET_assert (NULL != plc->tmit);
2001 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2002 NULL, notify_data, notify_data_cls, flags))
2003 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2010 * Resume talking to the host of the place.
2013 * Talk request to resume.
2016 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2018 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2023 * Cancel talking to the host of the place.
2026 * Talk request to cancel.
2029 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2031 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2036 * Disconnect from a place.
2038 * Invalidates guest handle.
2041 * The guest to disconnect.
2044 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2045 GNUNET_ContinuationCallback disconnect_cb,
2048 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2050 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2051 "GNUNET_SOCIAL_guest_disconnect, gst = %p\n",
2053 plc->disconnect_cb = disconnect_cb;
2054 plc->disconnect_cls = cls;
2055 place_disconnect (plc);
2060 * Leave a place temporarily or permanently.
2062 * Notifies the owner of the place about leaving, and destroys the place handle.
2066 * @param keep_active
2067 * Keep place active after last application disconnected.
2068 * #GNUNET_YES or #GNUNET_NO
2070 * Optional environment for the leave message if @a keep_active
2071 * is #GNUNET_NO. NULL if not needed.
2073 * Called upon disconnecting from the social service.
2076 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2077 struct GNUNET_PSYC_Environment *env,
2078 GNUNET_ContinuationCallback disconnect_cb,
2081 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2082 GNUNET_SOCIAL_TALK_NONE);
2083 place_leave (&gst->plc);
2084 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
2089 * Obtain handle for a place entered as guest.
2091 * The returned handle can be used to access the place API.
2093 * @param guest Handle for the guest.
2095 * @return Handle for the place, valid as long as @a guest is valid.
2097 struct GNUNET_SOCIAL_Place *
2098 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2105 * Obtain the public key of a place.
2110 * @return Public key of the place.
2112 const struct GNUNET_CRYPTO_EddsaPublicKey *
2113 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2115 return &plc->pub_key;
2120 * Set message processing @a flags for a @a method_prefix.
2124 * @param method_prefix
2125 * Method prefix @a flags apply to.
2127 * The flags that apply to a matching @a method_prefix.
2130 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2131 const char *method_prefix,
2132 enum GNUNET_SOCIAL_MsgProcFlags flags)
2134 GNUNET_assert (NULL != method_prefix);
2135 struct MsgProcRequest *mpreq;
2136 uint16_t method_size = strnlen (method_prefix,
2137 GNUNET_MAX_MESSAGE_SIZE
2138 - sizeof (*mpreq)) + 1;
2139 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2141 struct GNUNET_MQ_Envelope *
2142 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2143 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2144 mpreq->flags = htonl (flags);
2145 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2147 GNUNET_MQ_send (plc->mq, env);
2152 * Clear all message processing flags previously set for this place.
2155 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2157 struct GNUNET_MessageHeader *req;
2158 struct GNUNET_MQ_Envelope *
2159 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2161 GNUNET_MQ_send (plc->mq, env);
2165 static struct GNUNET_SOCIAL_HistoryRequest *
2166 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2167 uint64_t start_message_id,
2168 uint64_t end_message_id,
2169 uint64_t message_limit,
2170 const char *method_prefix,
2172 struct GNUNET_PSYC_Slicer *slicer,
2173 GNUNET_ResultCallback result_cb,
2176 struct GNUNET_PSYC_HistoryRequestMessage *req;
2177 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2179 hist->slicer = slicer;
2180 hist->result_cb = result_cb;
2182 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2184 GNUNET_assert (NULL != method_prefix);
2185 uint16_t method_size = strnlen (method_prefix,
2186 GNUNET_MAX_MESSAGE_SIZE
2187 - sizeof (*req)) + 1;
2188 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2190 struct GNUNET_MQ_Envelope *
2191 env = GNUNET_MQ_msg_extra (req, method_size,
2192 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2193 req->start_message_id = GNUNET_htonll (start_message_id);
2194 req->end_message_id = GNUNET_htonll (end_message_id);
2195 req->message_limit = GNUNET_htonll (message_limit);
2196 req->flags = htonl (flags);
2197 req->op_id = GNUNET_htonll (hist->op_id);
2198 GNUNET_memcpy (&req[1], method_prefix, method_size);
2200 GNUNET_MQ_send (plc->mq, env);
2206 * Learn about the history of a place.
2208 * Messages are returned through the @a slicer function
2209 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2212 * Place we want to learn more about.
2213 * @param start_message_id
2214 * First historic message we are interested in.
2215 * @param end_message_id
2216 * Last historic message we are interested in (inclusive).
2217 * @param method_prefix
2218 * Only retrieve messages with this method prefix.
2220 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2222 * Slicer to use for retrieved messages.
2223 * Can be the same as the slicer of the place.
2225 * Function called after all messages retrieved.
2226 * NULL if not needed.
2227 * @param cls Closure for @a result_cb.
2229 struct GNUNET_SOCIAL_HistoryRequest *
2230 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2231 uint64_t start_message_id,
2232 uint64_t end_message_id,
2233 const char *method_prefix,
2235 struct GNUNET_PSYC_Slicer *slicer,
2236 GNUNET_ResultCallback result_cb,
2239 return place_history_replay (plc, start_message_id, end_message_id, 0,
2240 method_prefix, flags, slicer, result_cb, cls);
2245 * Learn about the history of a place.
2247 * Sends messages through the slicer function of the place where
2248 * start_message_id <= message_id <= end_message_id.
2249 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2251 * To get the latest message, use 0 for both the start and end message ID.
2254 * Place we want to learn more about.
2255 * @param message_limit
2256 * Maximum number of historic messages we are interested in.
2257 * @param method_prefix
2258 * Only retrieve messages with this method prefix.
2260 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2262 * Function called after all messages retrieved.
2263 * NULL if not needed.
2264 * @param cls Closure for @a result_cb.
2266 struct GNUNET_SOCIAL_HistoryRequest *
2267 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2268 uint64_t message_limit,
2269 const char *method_prefix,
2271 struct GNUNET_PSYC_Slicer *slicer,
2272 GNUNET_ResultCallback result_cb,
2275 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2276 slicer, result_cb, cls);
2281 * Cancel learning about the history of a place.
2284 * History lesson to cancel.
2287 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2289 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2295 * Request matching state variables.
2297 static struct GNUNET_SOCIAL_LookHandle *
2298 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2299 uint16_t type, const char *name,
2300 GNUNET_PSYC_StateVarCallback var_cb,
2301 GNUNET_ResultCallback result_cb, void *cls)
2303 struct GNUNET_PSYC_StateRequestMessage *req;
2304 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2306 look->var_cb = var_cb;
2307 look->result_cb = result_cb;
2309 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2311 GNUNET_assert (NULL != name);
2312 size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
2313 - sizeof (*req)) + 1;
2314 struct GNUNET_MQ_Envelope *
2315 env = GNUNET_MQ_msg_extra (req, name_size, type);
2316 req->op_id = GNUNET_htonll (look->op_id);
2317 GNUNET_memcpy (&req[1], name, name_size);
2319 GNUNET_MQ_send (plc->mq, env);
2325 * Look at a particular object in the place.
2327 * The best matching object is returned (its name might be less specific than
2328 * what was requested).
2331 * The place where to look.
2333 * Full name of the object.
2335 * Set to the size of the returned value.
2337 * @return NULL if there is no such object at this place.
2339 struct GNUNET_SOCIAL_LookHandle *
2340 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2341 const char *full_name,
2342 GNUNET_PSYC_StateVarCallback var_cb,
2343 GNUNET_ResultCallback result_cb,
2346 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2347 full_name, var_cb, result_cb, cls);
2352 * Look for objects in the place with a matching name prefix.
2355 * The place where to look.
2356 * @param name_prefix
2357 * Look at objects with names beginning with this value.
2359 * Function to call for each object found.
2361 * Closure for callback function.
2363 * @return Handle that can be used to stop looking at objects.
2365 struct GNUNET_SOCIAL_LookHandle *
2366 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2367 const char *name_prefix,
2368 GNUNET_PSYC_StateVarCallback var_cb,
2369 GNUNET_ResultCallback result_cb,
2372 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2373 name_prefix, var_cb, result_cb, cls);
2378 * Cancel a state request operation.
2381 * Handle for the operation to cancel.
2384 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2386 GNUNET_OP_remove (look->plc->op, look->op_id);
2392 op_recv_zone_add_place_result (void *cls, int64_t result,
2393 const void *err_msg, uint16_t err_msg_size)
2395 LOG (GNUNET_ERROR_TYPE_DEBUG,
2396 "Received zone add place result: %" PRId64 ".\n", result);
2398 struct ZoneAddPlaceHandle *add_plc = cls;
2399 if (NULL != add_plc->result_cb)
2400 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2402 GNUNET_free (add_plc);
2407 * Advertise @e place in the GNS zone of @e ego.
2410 * Application handle.
2413 * @param place_pub_key
2414 * Public key of place to add.
2416 * The name for the PLACE record to put in the zone.
2418 * Password used to encrypt the record or NULL to keep it cleartext.
2419 * @param relay_count
2420 * Number of elements in the @a relays array.
2422 * List of relays to put in the PLACE record to advertise
2423 * as entry points to the place in addition to the origin.
2424 * @param expiration_time
2425 * Expiration time of the record, use 0 to remove the record.
2427 * Function called with the result of the operation.
2429 * Closure for @a result_cb
2431 * @return #GNUNET_OK if the request was sent,
2432 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2435 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2436 const struct GNUNET_SOCIAL_Ego *ego,
2438 const char *password,
2439 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2440 const struct GNUNET_PeerIdentity *origin,
2441 uint32_t relay_count,
2442 const struct GNUNET_PeerIdentity *relays,
2443 struct GNUNET_TIME_Absolute expiration_time,
2444 GNUNET_ResultCallback result_cb,
2447 struct ZoneAddPlaceRequest *preq;
2448 size_t name_size = strlen (name) + 1;
2449 size_t password_size = strlen (password) + 1;
2450 size_t relay_size = relay_count * sizeof (*relays);
2451 size_t payload_size = name_size + password_size + relay_size;
2453 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2454 return GNUNET_SYSERR;
2456 struct GNUNET_MQ_Envelope *
2457 env = GNUNET_MQ_msg_extra (preq, payload_size,
2458 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2459 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2460 preq->ego_pub_key = ego->pub_key;
2461 preq->place_pub_key = *place_pub_key;
2462 preq->origin = *origin;
2463 preq->relay_count = htonl (relay_count);
2465 char *p = (char *) &preq[1];
2466 GNUNET_memcpy (p, name, name_size);
2468 GNUNET_memcpy (p, password, password_size);
2470 GNUNET_memcpy (p, relays, relay_size);
2472 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2473 add_plc->result_cb = result_cb;
2474 add_plc->result_cls = result_cls;
2476 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2477 op_recv_zone_add_place_result,
2480 GNUNET_MQ_send (app->mq, env);
2486 op_recv_zone_add_nym_result (void *cls, int64_t result,
2487 const void *err_msg, uint16_t err_msg_size)
2489 LOG (GNUNET_ERROR_TYPE_DEBUG,
2490 "Received zone add nym result: %" PRId64 ".\n", result);
2492 struct ZoneAddNymHandle *add_nym = cls;
2493 if (NULL != add_nym->result_cb)
2494 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2496 GNUNET_free (add_nym);
2501 * Add nym to the GNS zone of @e ego.
2508 * The name for the PKEY record to put in the zone.
2509 * @param nym_pub_key
2510 * Public key of nym to add.
2511 * @param expiration_time
2512 * Expiration time of the record, use 0 to remove the record.
2514 * Function called with the result of the operation.
2516 * Closure for @a result_cb
2518 * @return #GNUNET_OK if the request was sent,
2519 * #GNUNET_SYSERR on error, e.g. the name is too long.
2522 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2523 const struct GNUNET_SOCIAL_Ego *ego,
2525 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2526 struct GNUNET_TIME_Absolute expiration_time,
2527 GNUNET_ResultCallback result_cb,
2530 struct ZoneAddNymRequest *nreq;
2532 size_t name_size = strlen (name) + 1;
2533 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2534 return GNUNET_SYSERR;
2536 struct GNUNET_MQ_Envelope *
2537 env = GNUNET_MQ_msg_extra (nreq, name_size,
2538 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2539 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2540 nreq->ego_pub_key = ego->pub_key;
2541 nreq->nym_pub_key = *nym_pub_key;
2542 GNUNET_memcpy (&nreq[1], name, name_size);
2544 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2545 add_nym->result_cb = result_cb;
2546 add_nym->result_cls = result_cls;
2548 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2549 op_recv_zone_add_nym_result,
2552 GNUNET_MQ_send (app->mq, env);
2561 app_connect (struct GNUNET_SOCIAL_App *app);
2565 app_reconnect (void *cls)
2572 * App client disconnected from service.
2574 * Reconnect after backoff period.
2577 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2579 struct GNUNET_SOCIAL_App *app = cls;
2581 LOG (GNUNET_ERROR_TYPE_DEBUG,
2582 "App client disconnected (%d), re-connecting\n",
2584 if (NULL != app->mq)
2586 GNUNET_MQ_destroy (app->mq);
2590 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2593 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2598 app_connect (struct GNUNET_SOCIAL_App *app)
2600 struct GNUNET_MQ_MessageHandler handlers[] = {
2601 GNUNET_MQ_hd_var_size (app_ego,
2602 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2603 struct AppEgoMessage,
2605 GNUNET_MQ_hd_fixed_size (app_ego_end,
2606 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2607 struct GNUNET_MessageHeader,
2609 GNUNET_MQ_hd_var_size (app_place,
2610 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2611 struct AppPlaceMessage,
2613 GNUNET_MQ_hd_fixed_size (app_place_end,
2614 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2615 struct GNUNET_MessageHeader,
2617 GNUNET_MQ_hd_var_size (app_result,
2618 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2619 struct GNUNET_OperationResultMessage,
2621 GNUNET_MQ_handler_end ()
2624 app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
2625 handlers, app_disconnected, app);
2626 GNUNET_assert (NULL != app->mq);
2627 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2632 * Connect application to the social service.
2634 * The @host_place_cb and @guest_place_cb functions are
2635 * initially called for each entered places,
2636 * then later each time a new place is entered with the current application ID.
2643 * Function to notify about an available ego.
2645 * Function to notify about a place entered as host.
2647 * Function to notify about a place entered as guest.
2649 * Closure for the callbacks.
2651 * @return Handle that can be used to stop listening.
2653 struct GNUNET_SOCIAL_App *
2654 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2656 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2657 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2658 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2659 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2662 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2663 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2667 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2669 app->ego_cb = ego_cb;
2670 app->host_cb = host_cb;
2671 app->guest_cb = guest_cb;
2672 app->connected_cb = connected_cb;
2674 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2675 app->op = GNUNET_OP_create ();
2676 app->id = GNUNET_malloc (app_id_size);
2677 GNUNET_memcpy (app->id, id, app_id_size);
2679 struct AppConnectRequest *creq;
2680 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2681 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2682 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2690 app_cleanup (struct GNUNET_SOCIAL_App *app)
2692 if (NULL != app->mq)
2694 GNUNET_MQ_destroy (app->mq);
2697 if (NULL != app->disconnect_cb)
2699 app->disconnect_cb (app->disconnect_cls);
2700 app->disconnect_cb = NULL;
2706 * Disconnect application.
2709 * Application handle.
2710 * @param disconnect_cb
2711 * Disconnect callback.
2712 * @param disconnect_cls
2713 * Disconnect closure.
2716 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2717 GNUNET_ContinuationCallback disconnect_cb,
2718 void *disconnect_cls)
2720 app->disconnect_cb = disconnect_cb;
2721 app->disconnect_cls = disconnect_cls;
2723 if (NULL != app->mq)
2725 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2728 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
2743 * Detach application from a place.
2745 * Removes the place from the entered places list for this application.
2746 * Note: this does not disconnect from the place.
2748 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2756 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2757 struct GNUNET_SOCIAL_Place *plc)
2759 struct AppDetachRequest *dreq;
2760 struct GNUNET_MQ_Envelope *
2761 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2762 dreq->place_pub_key = plc->pub_key;
2763 dreq->ego_pub_key = plc->ego_pub_key;
2765 GNUNET_MQ_send (app->mq, env);
2769 /* end of social_api.c */