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_CLIENT_MANAGER_Connection *client;
77 * Message to send on reconnect.
79 struct GNUNET_MessageHeader *connect_msg;
82 * Function called after disconnected from the service.
84 GNUNET_ContinuationCallback disconnect_cb;
87 * Closure for @a disconnect_cb.
97 * Hash map of all egos.
98 * pub_key_hash -> struct GNUNET_SOCIAL_Ego *
100 struct GNUNET_CONTAINER_MultiHashMap *egos;
102 GNUNET_SOCIAL_AppEgoCallback ego_cb;
103 GNUNET_SOCIAL_AppHostPlaceCallback host_cb;
104 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb;
105 GNUNET_SOCIAL_AppConnectedCallback connected_cb;
110 struct GNUNET_SOCIAL_HostConnection
112 struct GNUNET_SOCIAL_App *app;
114 struct AppPlaceMessage plc_msg;
118 struct GNUNET_SOCIAL_GuestConnection
120 struct GNUNET_SOCIAL_App *app;
122 struct AppPlaceMessage plc_msg;
127 * Handle for a place where social interactions happen.
129 struct GNUNET_SOCIAL_Place
132 * Configuration to use.
134 const struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Client connection to the service.
139 struct GNUNET_CLIENT_MANAGER_Connection *client;
142 * Transmission handle.
144 struct GNUNET_PSYC_TransmitHandle *tmit;
147 * Slicer for processing incoming messages.
149 struct GNUNET_PSYC_Slicer *slicer;
152 * Message to send on reconnect.
154 struct GNUNET_MessageHeader *connect_msg;
157 * Function called after disconnected from the service.
159 GNUNET_ContinuationCallback disconnect_cb;
162 * Closure for @a disconnect_cb.
164 void *disconnect_cls;
167 * Public key of the place.
169 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
172 * Public key of the ego.
174 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
177 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
184 * Host handle for a place that we entered.
186 struct GNUNET_SOCIAL_Host
188 struct GNUNET_SOCIAL_Place plc;
191 * Slicer for processing incoming messages from guests.
193 struct GNUNET_PSYC_Slicer *slicer;
195 GNUNET_SOCIAL_HostEnterCallback enter_cb;
197 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
199 GNUNET_SOCIAL_FarewellCallback farewell_cb;
202 * Closure for callbacks.
206 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
207 struct GNUNET_PSYC_Environment *notice_place_leave_env;
212 * Guest handle for place that we entered.
214 struct GNUNET_SOCIAL_Guest
216 struct GNUNET_SOCIAL_Place plc;
218 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
220 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
223 * Closure for callbacks.
230 * Hash map of all nyms.
231 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
233 struct GNUNET_CONTAINER_MultiHashMap *nyms;
237 * Handle for an announcement request.
239 struct GNUNET_SOCIAL_Announcement
248 struct GNUNET_SOCIAL_TalkRequest
257 struct GNUNET_SOCIAL_HistoryRequest
262 struct GNUNET_SOCIAL_Place *plc;
270 * Slicer for processing incoming messages.
272 struct GNUNET_PSYC_Slicer *slicer;
275 * Function to call when the operation finished.
277 GNUNET_ResultCallback result_cb;
280 * Closure for @a result_cb.
286 struct GNUNET_SOCIAL_LookHandle
291 struct GNUNET_SOCIAL_Place *plc;
299 * State variable result callback.
301 GNUNET_PSYC_StateVarCallback var_cb;
304 * Function to call when the operation finished.
306 GNUNET_ResultCallback result_cb;
309 * Name of current modifier being received.
314 * Size of current modifier value being received.
316 size_t mod_value_size;
319 * Remaining size of current modifier value still to be received.
321 size_t mod_value_remaining;
324 * Closure for @a result_cb.
330 struct ZoneAddPlaceHandle
332 struct ZoneAddPlaceRequest *req;
333 GNUNET_ResultCallback result_cb;
338 struct ZoneAddNymHandle
340 struct ZoneAddNymRequest *req;
341 GNUNET_ResultCallback result_cb;
348 static struct GNUNET_SOCIAL_Nym *
349 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
351 struct GNUNET_SOCIAL_Nym *nym = NULL;
352 struct GNUNET_HashCode pub_key_hash;
357 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
360 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
362 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
366 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
367 nym->pub_key = *pub_key;
368 nym->pub_key_hash = pub_key_hash;
369 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
377 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
379 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
384 /*** MESSAGE HANDLERS ***/
386 /** _notice_place_leave from guests */
389 host_recv_notice_place_leave_method (void *cls,
390 const struct GNUNET_PSYC_MessageHeader *msg,
391 const struct GNUNET_PSYC_MessageMethod *meth,
393 const char *method_name)
395 struct GNUNET_SOCIAL_Host *hst = cls;
397 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
398 &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
401 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
404 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
405 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
407 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
408 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
410 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
411 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
412 "_notice_place_leave: got method from nym %s (%s).\n",
413 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
419 host_recv_notice_place_leave_modifier (void *cls,
420 const struct GNUNET_PSYC_MessageHeader *msg,
421 const struct GNUNET_MessageHeader *pmsg,
423 enum GNUNET_PSYC_Operator oper,
427 uint16_t full_value_size)
429 struct GNUNET_SOCIAL_Host *hst = cls;
430 if (NULL == hst->notice_place_leave_env)
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
434 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
436 message_id, oper, name, value_size, (const char *) value);
438 /* skip _nym, it's added later in eom() */
439 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
440 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
443 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
444 GNUNET_PSYC_OP_SET, name, value, value_size);
449 host_recv_notice_place_leave_eom (void *cls,
450 const struct GNUNET_PSYC_MessageHeader *msg,
451 const struct GNUNET_MessageHeader *pmsg,
453 uint8_t is_cancelled)
455 struct GNUNET_SOCIAL_Host *hst = cls;
456 if (NULL == hst->notice_place_leave_env)
459 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
461 "_notice_place_leave: got EOM from nym %s (%s).\n",
462 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
465 if (GNUNET_YES != is_cancelled)
467 if (NULL != hst->farewell_cb)
468 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
469 hst->notice_place_leave_env);
470 /* announce leaving guest to place */
471 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
472 "_nym", hst->notice_place_leave_nym,
473 sizeof (*hst->notice_place_leave_nym));
474 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
475 hst->notice_place_leave_env,
476 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
477 nym_destroy (hst->notice_place_leave_nym);
479 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
480 hst->notice_place_leave_env = NULL;
488 app_send_connect_msg (struct GNUNET_SOCIAL_App *app)
490 uint16_t cmsg_size = ntohs (app->connect_msg->size);
491 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
492 memcpy (cmsg, app->connect_msg, cmsg_size);
493 GNUNET_CLIENT_MANAGER_transmit_now (app->client, cmsg);
499 app_recv_disconnect (void *cls,
500 struct GNUNET_CLIENT_MANAGER_Connection *client,
501 const struct GNUNET_MessageHeader *msg)
503 struct GNUNET_SOCIAL_App *
504 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
506 GNUNET_CLIENT_MANAGER_reconnect (client);
507 app_send_connect_msg (app);
515 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
517 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
518 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
519 memcpy (cmsg, plc->connect_msg, cmsg_size);
520 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
526 place_recv_disconnect (void *cls,
527 struct GNUNET_CLIENT_MANAGER_Connection *client,
528 const struct GNUNET_MessageHeader *msg)
530 struct GNUNET_SOCIAL_Place *
531 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
533 GNUNET_CLIENT_MANAGER_reconnect (client);
534 place_send_connect_msg (plc);
539 place_recv_result (void *cls,
540 struct GNUNET_CLIENT_MANAGER_Connection *client,
541 const struct GNUNET_MessageHeader *msg)
543 struct GNUNET_SOCIAL_Place *
544 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
546 const struct GNUNET_OperationResultMessage *
547 res = (const struct GNUNET_OperationResultMessage *) msg;
549 uint16_t size = ntohs (msg->size);
550 if (size < sizeof (*res))
551 { /* Error, message too small. */
556 uint16_t data_size = size - sizeof (*res);
557 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
558 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
559 GNUNET_ntohll (res->result_code),
565 app_recv_result (void *cls,
566 struct GNUNET_CLIENT_MANAGER_Connection *client,
567 const struct GNUNET_MessageHeader *msg)
569 struct GNUNET_SOCIAL_App *
570 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
572 const struct GNUNET_OperationResultMessage *
573 res = (const struct GNUNET_OperationResultMessage *) msg;
575 uint16_t size = ntohs (msg->size);
576 if (size < sizeof (*res))
577 { /* Error, message too small. */
582 uint16_t data_size = size - sizeof (*res);
583 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
584 GNUNET_CLIENT_MANAGER_op_result (app->client, GNUNET_ntohll (res->op_id),
585 GNUNET_ntohll (res->result_code),
591 op_recv_history_result (void *cls, int64_t result,
592 const void *err_msg, uint16_t err_msg_size)
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Received history replay result: %" PRId64 ".\n", result);
597 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
599 if (NULL != hist->result_cb)
600 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
607 op_recv_state_result (void *cls, int64_t result,
608 const void *err_msg, uint16_t err_msg_size)
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "Received state request result: %" PRId64 ".\n", result);
613 struct GNUNET_SOCIAL_LookHandle *look = cls;
615 if (NULL != look->result_cb)
616 look->result_cb (look->cls, result, err_msg, err_msg_size);
623 place_recv_history_result (void *cls,
624 struct GNUNET_CLIENT_MANAGER_Connection *client,
625 const struct GNUNET_MessageHeader *msg)
627 struct GNUNET_SOCIAL_Place *
628 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
630 const struct GNUNET_OperationResultMessage *
631 res = (const struct GNUNET_OperationResultMessage *) msg;
632 struct GNUNET_PSYC_MessageHeader *
633 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
635 LOG (GNUNET_ERROR_TYPE_DEBUG,
636 "%p Received historic fragment for message #%" PRIu64 ".\n",
637 plc, GNUNET_ntohll (pmsg->message_id));
639 GNUNET_ResultCallback result_cb = NULL;
640 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
642 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
643 GNUNET_ntohll (res->op_id),
644 &result_cb, (void *) &hist))
645 { /* Operation not found. */
646 LOG (GNUNET_ERROR_TYPE_WARNING,
647 "%p Replay operation not found for historic fragment of message #%"
649 plc, GNUNET_ntohll (pmsg->message_id));
653 uint16_t size = ntohs (msg->size);
654 if (size < sizeof (*res) + sizeof (*pmsg))
655 { /* Error, message too small. */
660 GNUNET_PSYC_slicer_message (hist->slicer,
661 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
666 place_recv_state_result (void *cls,
667 struct GNUNET_CLIENT_MANAGER_Connection *client,
668 const struct GNUNET_MessageHeader *msg)
670 struct GNUNET_SOCIAL_Place *
671 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
673 const struct GNUNET_OperationResultMessage *
674 res = (const struct GNUNET_OperationResultMessage *) msg;
676 GNUNET_ResultCallback result_cb = NULL;
677 struct GNUNET_SOCIAL_LookHandle *look = NULL;
679 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
680 GNUNET_ntohll (res->op_id),
681 &result_cb, (void *) &look))
682 { /* Operation not found. */
686 const struct GNUNET_MessageHeader *
687 mod = (struct GNUNET_MessageHeader *) &res[1];
688 uint16_t mod_size = ntohs (mod->size);
689 if (ntohs (msg->size) - sizeof (*res) != mod_size)
692 LOG (GNUNET_ERROR_TYPE_WARNING,
693 "Invalid modifier size in state result: %u - %u != %u\n",
694 ntohs (msg->size), sizeof (*res), mod_size);
697 switch (ntohs (mod->type))
699 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
701 const struct GNUNET_PSYC_MessageModifier *
702 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
704 const char *name = (const char *) &pmod[1];
705 uint16_t name_size = ntohs (pmod->name_size);
706 if ('\0' != name[name_size - 1])
709 LOG (GNUNET_ERROR_TYPE_WARNING,
710 "Invalid modifier name in state result\n");
713 look->mod_value_size = ntohs (pmod->value_size);
714 look->var_cb (look->cls, mod, name, name + name_size,
715 mod_size - sizeof (*mod) - name_size,
716 look->mod_value_size);
717 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
719 look->mod_value_remaining = look->mod_value_size;
720 look->mod_name = GNUNET_malloc (name_size);
721 memcpy (look->mod_name, name, name_size);
726 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
727 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
728 mod_size - sizeof (*mod), look->mod_value_size);
729 look->mod_value_remaining -= mod_size - sizeof (*mod);
730 if (0 == look->mod_value_remaining)
732 GNUNET_free (look->mod_name);
740 place_recv_message_ack (void *cls,
741 struct GNUNET_CLIENT_MANAGER_Connection *client,
742 const struct GNUNET_MessageHeader *msg)
744 struct GNUNET_SOCIAL_Place *
745 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
746 GNUNET_PSYC_transmit_got_ack (plc->tmit);
751 place_recv_message (void *cls,
752 struct GNUNET_CLIENT_MANAGER_Connection *client,
753 const struct GNUNET_MessageHeader *msg)
755 struct GNUNET_SOCIAL_Place *
756 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
757 GNUNET_PSYC_slicer_message (plc->slicer,
758 (const struct GNUNET_PSYC_MessageHeader *) msg);
763 host_recv_message (void *cls,
764 struct GNUNET_CLIENT_MANAGER_Connection *client,
765 const struct GNUNET_MessageHeader *msg)
767 struct GNUNET_SOCIAL_Host *
768 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
769 GNUNET_PSYC_slicer_message (hst->slicer,
770 (const struct GNUNET_PSYC_MessageHeader *) msg);
771 GNUNET_PSYC_slicer_message (hst->plc.slicer,
772 (const struct GNUNET_PSYC_MessageHeader *) msg);
777 host_recv_enter_ack (void *cls,
778 struct GNUNET_CLIENT_MANAGER_Connection *client,
779 const struct GNUNET_MessageHeader *msg)
781 struct GNUNET_SOCIAL_Host *
782 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
783 sizeof (struct GNUNET_SOCIAL_Place));
785 struct HostEnterAck *hack = (struct HostEnterAck *) msg;
786 hst->plc.pub_key = hack->place_pub_key;
788 int32_t result = ntohl (hack->result_code);
789 if (NULL != hst->enter_cb)
790 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
791 GNUNET_ntohll (hack->max_message_id));
796 host_recv_enter_request (void *cls,
797 struct GNUNET_CLIENT_MANAGER_Connection *client,
798 const struct GNUNET_MessageHeader *msg)
800 struct GNUNET_SOCIAL_Host *
801 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
802 sizeof (struct GNUNET_SOCIAL_Place));
803 if (NULL == hst->answer_door_cb)
806 const char *method_name = NULL;
807 struct GNUNET_PSYC_Environment *env = NULL;
808 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
809 const void *data = NULL;
810 uint16_t data_size = 0;
812 const struct GNUNET_PSYC_JoinRequestMessage *
813 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
814 const struct GNUNET_PSYC_Message *join_msg = NULL;
818 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
820 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Received join_msg of type %u and size %u.\n",
823 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
825 env = GNUNET_PSYC_env_create ();
826 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
827 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
831 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
832 LOG (GNUNET_ERROR_TYPE_WARNING,
833 "Ignoring invalid entry request from nym %s.\n",
840 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
841 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
846 GNUNET_PSYC_env_destroy (env);
847 if (NULL != entry_pmsg)
848 GNUNET_free (entry_pmsg);
853 guest_recv_enter_ack (void *cls,
854 struct GNUNET_CLIENT_MANAGER_Connection *client,
855 const struct GNUNET_MessageHeader *msg)
857 struct GNUNET_SOCIAL_Guest *
858 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
859 sizeof (struct GNUNET_SOCIAL_Place));
861 struct GNUNET_PSYC_CountersResultMessage *
862 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
863 int32_t result = ntohl (cres->result_code);
864 if (NULL != gst->enter_cb)
865 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
866 GNUNET_ntohll (cres->max_message_id));
871 guest_recv_join_decision (void *cls,
872 struct GNUNET_CLIENT_MANAGER_Connection *client,
873 const struct GNUNET_MessageHeader *msg)
875 struct GNUNET_SOCIAL_Guest *
876 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
877 sizeof (struct GNUNET_SOCIAL_Place));
878 const struct GNUNET_PSYC_JoinDecisionMessage *
879 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
881 struct GNUNET_PSYC_Message *pmsg = NULL;
882 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
883 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
885 if (NULL != gst->entry_dcsn_cb)
886 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
891 app_recv_ego (void *cls,
892 struct GNUNET_CLIENT_MANAGER_Connection *client,
893 const struct GNUNET_MessageHeader *msg)
895 struct GNUNET_SOCIAL_App *
896 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
898 struct AppEgoMessage *
899 emsg = (struct AppEgoMessage *) msg;
901 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
903 struct GNUNET_HashCode ego_pub_hash;
904 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
907 struct GNUNET_SOCIAL_Ego *
908 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
911 ego = GNUNET_malloc (sizeof (*ego));
912 ego->pub_key = emsg->ego_pub_key;
913 ego->name = GNUNET_malloc (name_size);
914 memcpy (ego->name, &emsg[1], name_size);
918 ego->name = GNUNET_realloc (ego->name, name_size);
919 memcpy (ego->name, &emsg[1], name_size);
922 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
923 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
925 if (NULL != app->ego_cb)
926 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
931 app_recv_ego_end (void *cls,
932 struct GNUNET_CLIENT_MANAGER_Connection *client,
933 const struct GNUNET_MessageHeader *msg)
935 struct GNUNET_SOCIAL_App *
936 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
941 app_recv_place (void *cls,
942 struct GNUNET_CLIENT_MANAGER_Connection *client,
943 const struct GNUNET_MessageHeader *msg)
945 struct GNUNET_SOCIAL_App *
946 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
948 struct AppPlaceMessage *
949 pmsg = (struct AppPlaceMessage *) msg;
951 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
952 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
955 struct GNUNET_HashCode ego_pub_hash;
956 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
958 struct GNUNET_SOCIAL_Ego *
959 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
962 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
963 "Failure to obtain ego. Is identity service running?\n");
968 if (GNUNET_YES == pmsg->is_host)
970 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
972 hconn->plc_msg = *pmsg;
973 if (NULL != app->host_cb)
974 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
978 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
980 gconn->plc_msg = *pmsg;
981 if (NULL != app->guest_cb)
982 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
988 app_recv_place_end (void *cls,
989 struct GNUNET_CLIENT_MANAGER_Connection *client,
990 const struct GNUNET_MessageHeader *msg)
992 struct GNUNET_SOCIAL_App *
993 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
995 if (NULL != app->connected_cb)
996 app->connected_cb (app->cb_cls);
1000 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1002 { host_recv_enter_ack, NULL,
1003 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1004 sizeof (struct HostEnterAck), GNUNET_NO },
1006 { host_recv_enter_request, NULL,
1007 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1008 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1010 { host_recv_message, NULL,
1011 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1012 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1014 { place_recv_message_ack, NULL,
1015 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1016 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1018 { place_recv_history_result, NULL,
1019 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1020 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1022 { place_recv_state_result, NULL,
1023 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1024 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1026 { place_recv_result, NULL,
1027 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1028 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1030 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1032 { NULL, NULL, 0, 0, GNUNET_NO }
1036 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1038 { guest_recv_enter_ack, NULL,
1039 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1040 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1042 { host_recv_enter_request, NULL,
1043 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1044 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1046 { place_recv_message, NULL,
1047 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1048 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1050 { place_recv_message_ack, NULL,
1051 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1052 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1054 { guest_recv_join_decision, NULL,
1055 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1056 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1058 { place_recv_history_result, NULL,
1059 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1060 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1062 { place_recv_state_result, NULL,
1063 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1064 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1066 { place_recv_result, NULL,
1067 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1068 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1070 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1072 { NULL, NULL, 0, 0, GNUNET_NO }
1076 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1078 { app_recv_ego, NULL,
1079 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1080 sizeof (struct AppEgoMessage), GNUNET_YES },
1082 { app_recv_ego_end, NULL,
1083 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
1084 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1086 { app_recv_place, NULL,
1087 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1088 sizeof (struct AppPlaceMessage), GNUNET_NO },
1090 { app_recv_place_end, NULL,
1091 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
1092 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1094 { app_recv_result, NULL,
1095 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1096 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1098 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1100 { NULL, NULL, 0, 0, GNUNET_NO }
1105 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1107 struct GNUNET_HashCode place_pub_hash;
1108 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "%s place cleanup: %s\n",
1111 GNUNET_YES == plc->is_host ? "host" : "guest",
1112 GNUNET_h2s (&place_pub_hash));
1114 if (NULL != plc->tmit)
1115 GNUNET_PSYC_transmit_destroy (plc->tmit);
1116 if (NULL != plc->connect_msg)
1117 GNUNET_free (plc->connect_msg);
1118 if (NULL != plc->disconnect_cb)
1119 plc->disconnect_cb (plc->disconnect_cls);
1124 host_cleanup (void *cls)
1126 struct GNUNET_SOCIAL_Host *hst = cls;
1127 place_cleanup (&hst->plc);
1128 if (NULL != hst->slicer)
1130 GNUNET_PSYC_slicer_destroy (hst->slicer);
1138 guest_cleanup (void *cls)
1140 struct GNUNET_SOCIAL_Guest *gst = cls;
1141 place_cleanup (&gst->plc);
1149 * Enter a place as host.
1151 * A place is created upon first entering, and it is active until permanently
1152 * left using GNUNET_SOCIAL_host_leave().
1155 * Application handle.
1157 * Identity of the host.
1159 * Private-public key pair of the place.
1160 * NULL to generate a key.
1162 * Policy specifying entry and history restrictions for the place.
1164 * Slicer to handle incoming messages.
1166 * Function called when the place is entered and ready to use.
1167 * @param answer_door_cb
1168 * Function to handle new nyms that want to enter.
1169 * @param farewell_cb
1170 * Function to handle departing nyms.
1172 * Closure for the callbacks.
1174 * @return Handle for the host.
1176 struct GNUNET_SOCIAL_Host *
1177 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1178 const struct GNUNET_SOCIAL_Ego *ego,
1179 enum GNUNET_PSYC_Policy policy,
1180 struct GNUNET_PSYC_Slicer *slicer,
1181 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1182 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1183 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1186 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1187 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1189 plc->cfg = app->cfg;
1190 plc->is_host = GNUNET_YES;
1191 plc->slicer = slicer;
1193 hst->enter_cb = enter_cb;
1194 hst->answer_door_cb = answer_door_cb;
1195 hst->farewell_cb = farewell_cb;
1198 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1199 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1201 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1203 hst->slicer = GNUNET_PSYC_slicer_create ();
1204 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1205 host_recv_notice_place_leave_method,
1206 host_recv_notice_place_leave_modifier,
1207 NULL, host_recv_notice_place_leave_eom, hst);
1209 uint16_t app_id_size = strlen (app->id) + 1;
1210 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1211 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1212 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1213 hreq->policy = policy;
1214 hreq->ego_pub_key = ego->pub_key;
1215 memcpy (&hreq[1], app->id, app_id_size);
1217 plc->connect_msg = &hreq->header;
1218 place_send_connect_msg (plc);
1225 * Reconnect to an already entered place as host.
1228 * Host connection handle.
1229 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1231 * Slicer to handle incoming messages.
1233 * Function called when the place is entered and ready to use.
1234 * @param answer_door_cb
1235 * Function to handle new nyms that want to enter.
1236 * @param farewell_cb
1237 * Function to handle departing nyms.
1239 * Closure for the callbacks.
1241 * @return Handle for the host.
1243 struct GNUNET_SOCIAL_Host *
1244 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
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 size_t app_id_size = strlen (hconn->app->id) + 1;
1255 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1257 hst->enter_cb = enter_cb;
1258 hst->answer_door_cb = answer_door_cb;
1259 hst->farewell_cb = farewell_cb;
1262 plc->cfg = hconn->app->cfg;
1263 plc->is_host = GNUNET_YES;
1264 plc->slicer = slicer;
1265 plc->pub_key = hconn->plc_msg.place_pub_key;
1266 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1268 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1269 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1271 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1273 hst->slicer = GNUNET_PSYC_slicer_create ();
1274 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1275 host_recv_notice_place_leave_method,
1276 host_recv_notice_place_leave_modifier,
1277 NULL, host_recv_notice_place_leave_eom, hst);
1279 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1280 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1281 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1282 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1283 memcpy (&hreq[1], hconn->app->id, app_id_size);
1285 plc->connect_msg = &hreq->header;
1286 place_send_connect_msg (plc);
1288 GNUNET_free (hconn);
1294 * Decision whether to admit @a nym into the place or refuse entry.
1297 * Host of the place.
1299 * Handle for the entity that wanted to enter.
1300 * @param is_admitted
1301 * #GNUNET_YES if @a nym is admitted,
1302 * #GNUNET_NO if @a nym is refused entry,
1303 * #GNUNET_SYSERR if we cannot answer the request.
1304 * @param method_name
1305 * Method name for the rejection message.
1307 * Environment containing variables for the message, or NULL.
1309 * Data for the rejection message to send back.
1311 * Number of bytes in @a data for method.
1312 * @return #GNUNET_OK on success,
1313 * #GNUNET_SYSERR if the message is too large.
1316 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1317 struct GNUNET_SOCIAL_Nym *nym,
1319 const struct GNUNET_PSYC_Message *entry_resp)
1321 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1322 uint16_t entry_resp_size
1323 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1325 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1326 return GNUNET_SYSERR;
1328 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1329 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1330 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1331 dcsn->is_admitted = htonl (is_admitted);
1332 dcsn->slave_pub_key = nym->pub_key;
1334 if (0 < entry_resp_size)
1335 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1337 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1344 * Throw @a nym out of the place.
1346 * The @a nym reference will remain valid until the
1347 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1348 * which should be very soon after this call.
1351 * Host of the place.
1353 * Handle for the entity to be ejected.
1355 * Environment for the message or NULL.
1358 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1359 const struct GNUNET_SOCIAL_Nym *nym,
1360 struct GNUNET_PSYC_Environment *e)
1362 struct GNUNET_PSYC_Environment *env = e;
1364 env = GNUNET_PSYC_env_create ();
1365 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1366 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1367 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1368 GNUNET_SOCIAL_ANNOUNCE_NONE);
1370 GNUNET_PSYC_env_destroy (env);
1375 * Get the public key of @a ego.
1380 * @return Public key of ego.
1382 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1383 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1385 return &ego->pub_key;
1390 * Get the hash of the public key of @a ego.
1395 * @return Hash of the public key of @a ego.
1397 const struct GNUNET_HashCode *
1398 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1400 return &ego->pub_key_hash;
1405 * Get the name of @a ego.
1410 * @return Public key of @a ego.
1413 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1420 * Get the public key of @a nym.
1422 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1427 * @return Public key of @a nym.
1429 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1430 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1432 return &nym->pub_key;
1437 * Get the hash of the public key of @a nym.
1442 * @return Hash of the public key of @a nym.
1444 const struct GNUNET_HashCode *
1445 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1447 return &nym->pub_key_hash;
1452 * Send a message to all nyms that are present in the place.
1454 * This function is restricted to the host. Nyms can only send requests
1455 * to the host who can decide to relay it to everyone in the place.
1457 * @param host Host of the place.
1458 * @param method_name Method to use for the announcement.
1459 * @param env Environment containing variables for the message and operations
1460 * on objects of the place. Can be NULL.
1461 * @param notify Function to call to get the payload of the announcement.
1462 * @param notify_cls Closure for @a notify.
1463 * @param flags Flags for this announcement.
1465 * @return NULL on error (announcement already in progress?).
1467 struct GNUNET_SOCIAL_Announcement *
1468 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1469 const char *method_name,
1470 const struct GNUNET_PSYC_Environment *env,
1471 GNUNET_PSYC_TransmitNotifyData notify_data,
1472 void *notify_data_cls,
1473 enum GNUNET_SOCIAL_AnnounceFlags flags)
1476 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1477 NULL, notify_data, notify_data_cls, flags))
1478 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1485 * Resume transmitting announcement.
1488 * The announcement to resume.
1491 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1493 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1498 * Cancel announcement.
1501 * The announcement to cancel.
1504 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1506 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1511 * Obtain handle for a hosted place.
1513 * The returned handle can be used to access the place API.
1515 * @param host Handle for the host.
1517 * @return Handle for the hosted place, valid as long as @a host is valid.
1519 struct GNUNET_SOCIAL_Place *
1520 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1527 place_leave (struct GNUNET_SOCIAL_Place *plc)
1529 struct GNUNET_MessageHeader msg;
1530 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1531 msg.size = htons (sizeof (msg));
1532 GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
1537 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1538 GNUNET_ContinuationCallback disconnect_cb,
1539 void *disconnect_cls)
1541 plc->disconnect_cb = disconnect_cb;
1542 plc->disconnect_cls = disconnect_cls;
1544 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1545 GNUNET_YES == plc->is_host
1546 ? host_cleanup : guest_cleanup,
1552 * Disconnect from a home.
1554 * Invalidates host handle.
1557 * The host to disconnect.
1560 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1561 GNUNET_ContinuationCallback disconnect_cb,
1564 place_disconnect (&hst->plc, disconnect_cb, cls);
1569 * Stop hosting the home.
1571 * Sends a _notice_place_closing announcement to the home.
1572 * Invalidates host handle.
1577 * Environment for the message or NULL.
1578 * _nym is set to @e nym regardless whether an @e env is provided.
1579 * @param disconnect_cb
1580 * Function called after the host left the place
1581 * and disconnected from the social service.
1583 * Closure for @a disconnect_cb.
1586 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1587 const struct GNUNET_PSYC_Environment *env,
1588 GNUNET_ContinuationCallback disconnect_cb,
1591 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1592 GNUNET_SOCIAL_ANNOUNCE_NONE);
1593 place_leave (&hst->plc);
1594 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1600 static struct GuestEnterRequest *
1601 guest_enter_request_create (const char *app_id,
1602 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1603 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1604 const struct GNUNET_PeerIdentity *origin,
1606 const struct GNUNET_PeerIdentity *relays,
1607 const struct GNUNET_PSYC_Message *join_msg)
1609 uint16_t app_id_size = strlen (app_id) + 1;
1610 uint16_t join_msg_size = ntohs (join_msg->header.size);
1611 uint16_t relay_size = relay_count * sizeof (*relays);
1613 struct GuestEnterRequest *
1614 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1616 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1617 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1618 greq->place_pub_key = *place_pub_key;
1619 greq->ego_pub_key = *ego_pub_key;
1620 greq->origin = *origin;
1621 greq->relay_count = htonl (relay_count);
1623 char *p = (char *) &greq[1];
1624 memcpy (p, app_id, app_id_size);
1629 memcpy (p, relays, relay_size);
1633 memcpy (p, join_msg, join_msg_size);
1639 * Request entry to a place as a guest.
1642 * Application handle.
1644 * Identity of the guest.
1645 * @param place_pub_key
1646 * Public key of the place to enter.
1648 * Flags for the entry.
1650 * Peer identity of the origin of the underlying multicast group.
1651 * @param relay_count
1652 * Number of elements in the @a relays array.
1654 * Relays for the underlying multicast group.
1655 * @param method_name
1656 * Method name for the message.
1658 * Environment containing variables for the message, or NULL.
1660 * Payload for the message to give to the enter callback.
1662 * Number of bytes in @a data.
1664 * Slicer to use for processing incoming requests from guests.
1666 * @return NULL on errors, otherwise handle for the guest.
1668 struct GNUNET_SOCIAL_Guest *
1669 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1670 const struct GNUNET_SOCIAL_Ego *ego,
1671 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1672 enum GNUNET_PSYC_SlaveJoinFlags flags,
1673 const struct GNUNET_PeerIdentity *origin,
1674 uint32_t relay_count,
1675 const struct GNUNET_PeerIdentity *relays,
1676 const struct GNUNET_PSYC_Message *entry_msg,
1677 struct GNUNET_PSYC_Slicer *slicer,
1678 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1679 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1682 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1683 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1685 plc->ego_pub_key = ego->pub_key;
1686 plc->pub_key = *place_pub_key;
1687 plc->cfg = app->cfg;
1688 plc->is_host = GNUNET_NO;
1689 plc->slicer = slicer;
1691 gst->enter_cb = local_enter_cb;
1692 gst->entry_dcsn_cb = entry_dcsn_cb;
1695 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1696 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1698 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1700 struct GuestEnterRequest *
1701 greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1702 origin, relay_count, relays, entry_msg);
1703 plc->connect_msg = &greq->header;
1704 place_send_connect_msg (plc);
1710 * Request entry to a place by name as a guest.
1713 * Application handle.
1715 * Identity of the guest.
1717 * GNS name of the place to enter. Either in the form of
1718 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1719 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1720 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1721 * pseudonym's place directly.
1723 * Password to decrypt the record, or NULL for cleartext records.
1725 * Entry request message or NULL.
1727 * Slicer to use for processing incoming requests from guests.
1728 * @param local_enter_cb
1729 * Called upon connection established to the social service.
1730 * @param entry_decision_cb
1731 * Called upon receiving entry decision.
1733 * @return NULL on errors, otherwise handle for the guest.
1735 struct GNUNET_SOCIAL_Guest *
1736 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1737 const struct GNUNET_SOCIAL_Ego *ego,
1738 const char *gns_name,
1739 const char *password,
1740 const struct GNUNET_PSYC_Message *join_msg,
1741 struct GNUNET_PSYC_Slicer *slicer,
1742 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1743 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1746 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1747 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1749 if (NULL == password)
1752 uint16_t app_id_size = strlen (app->id) + 1;
1753 uint16_t gns_name_size = strlen (gns_name) + 1;
1754 uint16_t password_size = strlen (password) + 1;
1756 uint16_t join_msg_size = 0;
1757 if (NULL != join_msg)
1758 join_msg_size = ntohs (join_msg->header.size);
1760 uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
1761 + app_id_size + gns_name_size + password_size + join_msg_size;
1762 struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
1763 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1764 greq->header.size = htons (greq_size);
1765 greq->ego_pub_key = ego->pub_key;
1767 char *p = (char *) &greq[1];
1768 memcpy (p, app->id, app_id_size);
1770 memcpy (p, gns_name, gns_name_size);
1772 memcpy (p, password, password_size);
1774 if (NULL != join_msg)
1775 memcpy (p, join_msg, join_msg_size);
1777 gst->enter_cb = local_enter_cb;
1778 gst->entry_dcsn_cb = entry_decision_cb;
1781 plc->ego_pub_key = ego->pub_key;
1782 plc->cfg = app->cfg;
1783 plc->is_host = GNUNET_NO;
1784 plc->slicer = slicer;
1786 plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
1787 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1789 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1791 plc->connect_msg = &greq->header;
1792 place_send_connect_msg (plc);
1799 * Reconnect to an already entered place as guest.
1802 * Guest connection handle.
1803 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1805 * Flags for the entry.
1807 * Slicer to use for processing incoming requests from guests.
1808 * @param local_enter_cb
1809 * Called upon connection established to the social service.
1810 * @param entry_decision_cb
1811 * Called upon receiving entry decision.
1813 * @return NULL on errors, otherwise handle for the guest.
1815 struct GNUNET_SOCIAL_Guest *
1816 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1817 enum GNUNET_PSYC_SlaveJoinFlags flags,
1818 struct GNUNET_PSYC_Slicer *slicer,
1819 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1822 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1823 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1825 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1826 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1827 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1828 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1829 greq->header.size = htons (greq_size);
1830 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1831 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1832 greq->flags = htonl (flags);
1834 memcpy (&greq[1], gconn->app->id, app_id_size);
1836 gst->enter_cb = local_enter_cb;
1839 plc->cfg = gconn->app->cfg;
1840 plc->is_host = GNUNET_NO;
1841 plc->slicer = slicer;
1842 plc->pub_key = gconn->plc_msg.place_pub_key;
1843 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1845 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1846 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1848 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1850 plc->connect_msg = &greq->header;
1851 place_send_connect_msg (plc);
1853 GNUNET_free (gconn);
1859 * Talk to the host of the place.
1862 * Place where we want to talk to the host.
1863 * @param method_name
1864 * Method to invoke on the host.
1866 * Environment containing variables for the message, or NULL.
1867 * @param notify_data
1868 * Function to use to get the payload for the method.
1869 * @param notify_data_cls
1870 * Closure for @a notify_data.
1872 * Flags for the message being sent.
1874 * @return NULL if we are already trying to talk to the host,
1875 * otherwise handle to cancel the request.
1877 struct GNUNET_SOCIAL_TalkRequest *
1878 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1879 const char *method_name,
1880 const struct GNUNET_PSYC_Environment *env,
1881 GNUNET_PSYC_TransmitNotifyData notify_data,
1882 void *notify_data_cls,
1883 enum GNUNET_SOCIAL_TalkFlags flags)
1885 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1886 GNUNET_assert (NULL != plc->tmit);
1889 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1890 NULL, notify_data, notify_data_cls, flags))
1891 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1898 * Resume talking to the host of the place.
1901 * Talk request to resume.
1904 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1906 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1911 * Cancel talking to the host of the place.
1914 * Talk request to cancel.
1917 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1919 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1924 * Disconnect from a place.
1926 * Invalidates guest handle.
1929 * The guest to disconnect.
1932 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1933 GNUNET_ContinuationCallback disconnect_cb,
1936 place_disconnect (&gst->plc, disconnect_cb, cls);
1941 * Leave a place temporarily or permanently.
1943 * Notifies the owner of the place about leaving, and destroys the place handle.
1947 * @param keep_active
1948 * Keep place active after last application disconnected.
1949 * #GNUNET_YES or #GNUNET_NO
1951 * Optional environment for the leave message if @a keep_active
1952 * is #GNUNET_NO. NULL if not needed.
1954 * Called upon disconnecting from the social service.
1957 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1958 struct GNUNET_PSYC_Environment *env,
1959 GNUNET_ContinuationCallback disconnect_cb,
1962 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1963 GNUNET_SOCIAL_TALK_NONE);
1964 place_leave (&gst->plc);
1965 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1970 * Obtain handle for a place entered as guest.
1972 * The returned handle can be used to access the place API.
1974 * @param guest Handle for the guest.
1976 * @return Handle for the place, valid as long as @a guest is valid.
1978 struct GNUNET_SOCIAL_Place *
1979 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1986 * Obtain the public key of a place.
1991 * @return Public key of the place.
1993 const struct GNUNET_CRYPTO_EddsaPublicKey *
1994 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
1996 return &plc->pub_key;
2001 * Set message processing @a flags for a @a method_prefix.
2005 * @param method_prefix
2006 * Method prefix @a flags apply to.
2008 * The flags that apply to a matching @a method_prefix.
2011 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2012 const char *method_prefix,
2013 enum GNUNET_SOCIAL_MsgProcFlags flags)
2015 GNUNET_assert (NULL != method_prefix);
2016 struct MsgProcRequest *mpreq;
2017 uint16_t method_size = strnlen (method_prefix,
2018 GNUNET_SERVER_MAX_MESSAGE_SIZE
2019 - sizeof (*mpreq)) + 1;
2020 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2021 mpreq = GNUNET_malloc (sizeof (*mpreq) + method_size);
2023 mpreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2024 mpreq->header.size = htons (sizeof (*mpreq) + method_size);
2025 mpreq->flags = htonl (flags);
2026 memcpy (&mpreq[1], method_prefix, method_size);
2028 GNUNET_CLIENT_MANAGER_transmit (plc->client, &mpreq->header);
2029 GNUNET_free (mpreq);
2034 * Clear all message processing flags previously set for this place.
2037 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2039 struct GNUNET_MessageHeader req;
2040 req.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2041 req.size = htons (sizeof (req));
2042 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req);
2046 static struct GNUNET_SOCIAL_HistoryRequest *
2047 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2048 uint64_t start_message_id,
2049 uint64_t end_message_id,
2050 uint64_t message_limit,
2051 const char *method_prefix,
2053 struct GNUNET_PSYC_Slicer *slicer,
2054 GNUNET_ResultCallback result_cb,
2057 struct GNUNET_PSYC_HistoryRequestMessage *req;
2058 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2060 hist->slicer = slicer;
2061 hist->result_cb = result_cb;
2063 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2064 &op_recv_history_result, hist);
2066 GNUNET_assert (NULL != method_prefix);
2067 uint16_t method_size = strnlen (method_prefix,
2068 GNUNET_SERVER_MAX_MESSAGE_SIZE
2069 - sizeof (*req)) + 1;
2070 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2071 req = GNUNET_malloc (sizeof (*req) + method_size);
2072 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2073 req->header.size = htons (sizeof (*req) + method_size);
2074 req->start_message_id = GNUNET_htonll (start_message_id);
2075 req->end_message_id = GNUNET_htonll (end_message_id);
2076 req->message_limit = GNUNET_htonll (message_limit);
2077 req->flags = htonl (flags);
2078 req->op_id = GNUNET_htonll (hist->op_id);
2079 memcpy (&req[1], method_prefix, method_size);
2081 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2088 * Learn about the history of a place.
2090 * Messages are returned through the @a slicer function
2091 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2094 * Place we want to learn more about.
2095 * @param start_message_id
2096 * First historic message we are interested in.
2097 * @param end_message_id
2098 * Last historic message we are interested in (inclusive).
2099 * @param method_prefix
2100 * Only retrieve messages with this method prefix.
2102 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2104 * Slicer to use for retrieved messages.
2105 * Can be the same as the slicer of the place.
2107 * Function called after all messages retrieved.
2108 * NULL if not needed.
2109 * @param cls Closure for @a result_cb.
2111 struct GNUNET_SOCIAL_HistoryRequest *
2112 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2113 uint64_t start_message_id,
2114 uint64_t end_message_id,
2115 const char *method_prefix,
2117 struct GNUNET_PSYC_Slicer *slicer,
2118 GNUNET_ResultCallback result_cb,
2121 return place_history_replay (plc, start_message_id, end_message_id, 0,
2122 method_prefix, flags, slicer, result_cb, cls);
2127 * Learn about the history of a place.
2129 * Sends messages through the slicer function of the place where
2130 * start_message_id <= message_id <= end_message_id.
2131 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2133 * To get the latest message, use 0 for both the start and end message ID.
2136 * Place we want to learn more about.
2137 * @param message_limit
2138 * Maximum number of historic messages we are interested in.
2139 * @param method_prefix
2140 * Only retrieve messages with this method prefix.
2142 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2144 * Function called after all messages retrieved.
2145 * NULL if not needed.
2146 * @param cls Closure for @a result_cb.
2148 struct GNUNET_SOCIAL_HistoryRequest *
2149 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2150 uint64_t message_limit,
2151 const char *method_prefix,
2153 struct GNUNET_PSYC_Slicer *slicer,
2154 GNUNET_ResultCallback result_cb,
2157 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2158 slicer, result_cb, cls);
2163 * Cancel learning about the history of a place.
2166 * History lesson to cancel.
2169 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2171 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2177 * Request matching state variables.
2179 static struct GNUNET_SOCIAL_LookHandle *
2180 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2181 uint16_t type, const char *name,
2182 GNUNET_PSYC_StateVarCallback var_cb,
2183 GNUNET_ResultCallback result_cb, void *cls)
2185 struct GNUNET_PSYC_StateRequestMessage *req;
2186 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2188 look->var_cb = var_cb;
2189 look->result_cb = result_cb;
2191 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2192 &op_recv_state_result, look);
2194 GNUNET_assert (NULL != name);
2195 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2196 - sizeof (*req)) + 1;
2197 req = GNUNET_malloc (sizeof (*req) + name_size);
2198 req->header.type = htons (type);
2199 req->header.size = htons (sizeof (*req) + name_size);
2200 req->op_id = GNUNET_htonll (look->op_id);
2201 memcpy (&req[1], name, name_size);
2203 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2210 * Look at a particular object in the place.
2212 * The best matching object is returned (its name might be less specific than
2213 * what was requested).
2216 * The place where to look.
2218 * Full name of the object.
2220 * Set to the size of the returned value.
2222 * @return NULL if there is no such object at this place.
2224 struct GNUNET_SOCIAL_LookHandle *
2225 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2226 const char *full_name,
2227 GNUNET_PSYC_StateVarCallback var_cb,
2228 GNUNET_ResultCallback result_cb,
2231 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2232 full_name, var_cb, result_cb, cls);
2237 * Look for objects in the place with a matching name prefix.
2240 * The place where to look.
2241 * @param name_prefix
2242 * Look at objects with names beginning with this value.
2244 * Function to call for each object found.
2246 * Closure for callback function.
2248 * @return Handle that can be used to stop looking at objects.
2250 struct GNUNET_SOCIAL_LookHandle *
2251 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2252 const char *name_prefix,
2253 GNUNET_PSYC_StateVarCallback var_cb,
2254 GNUNET_ResultCallback result_cb,
2257 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2258 name_prefix, var_cb, result_cb, cls);
2263 * Cancel a state request operation.
2266 * Handle for the operation to cancel.
2269 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2271 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2277 op_recv_zone_add_place_result (void *cls, int64_t result,
2278 const void *err_msg, uint16_t err_msg_size)
2280 LOG (GNUNET_ERROR_TYPE_DEBUG,
2281 "Received zone add place result: %" PRId64 ".\n", result);
2283 struct ZoneAddPlaceHandle *add_plc = cls;
2284 if (NULL != add_plc->result_cb)
2285 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2287 GNUNET_free (add_plc->req);
2288 GNUNET_free (add_plc);
2293 * Advertise @e place in the GNS zone of @e ego.
2296 * Application handle.
2299 * @param place_pub_key
2300 * Public key of place to add.
2302 * The name for the PLACE record to put in the zone.
2304 * Password used to encrypt the record or NULL to keep it cleartext.
2305 * @param relay_count
2306 * Number of elements in the @a relays array.
2308 * List of relays to put in the PLACE record to advertise
2309 * as entry points to the place in addition to the origin.
2310 * @param expiration_time
2311 * Expiration time of the record, use 0 to remove the record.
2313 * Function called with the result of the operation.
2315 * Closure for @a result_cb
2317 * @return #GNUNET_OK if the request was sent,
2318 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2321 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2322 const struct GNUNET_SOCIAL_Ego *ego,
2324 const char *password,
2325 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2326 const struct GNUNET_PeerIdentity *origin,
2327 uint32_t relay_count,
2328 const struct GNUNET_PeerIdentity *relays,
2329 struct GNUNET_TIME_Absolute expiration_time,
2330 GNUNET_ResultCallback result_cb,
2333 struct ZoneAddPlaceRequest *preq;
2334 size_t name_size = strlen (name) + 1;
2335 size_t password_size = strlen (password) + 1;
2336 size_t relay_size = relay_count * sizeof (*relays);
2337 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2339 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2340 return GNUNET_SYSERR;
2342 preq = GNUNET_malloc (preq_size);
2343 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2344 preq->header.size = htons (preq_size);
2345 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2346 preq->ego_pub_key = ego->pub_key;
2347 preq->place_pub_key = *place_pub_key;
2348 preq->origin = *origin;
2349 preq->relay_count = htonl (relay_count);
2351 char *p = (char *) &preq[1];
2352 memcpy (p, name, name_size);
2354 memcpy (p, password, password_size);
2356 memcpy (p, relays, relay_size);
2358 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2359 add_plc->req = preq;
2360 add_plc->result_cb = result_cb;
2361 add_plc->result_cls = result_cls;
2363 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2364 op_recv_zone_add_place_result,
2366 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2372 op_recv_zone_add_nym_result (void *cls, int64_t result,
2373 const void *err_msg, uint16_t err_msg_size)
2375 LOG (GNUNET_ERROR_TYPE_DEBUG,
2376 "Received zone add nym result: %" PRId64 ".\n", result);
2378 struct ZoneAddNymHandle *add_nym = cls;
2379 if (NULL != add_nym->result_cb)
2380 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2382 GNUNET_free (add_nym->req);
2383 GNUNET_free (add_nym);
2388 * Add nym to the GNS zone of @e ego.
2395 * The name for the PKEY record to put in the zone.
2396 * @param nym_pub_key
2397 * Public key of nym to add.
2398 * @param expiration_time
2399 * Expiration time of the record, use 0 to remove the record.
2401 * Function called with the result of the operation.
2403 * Closure for @a result_cb
2405 * @return #GNUNET_OK if the request was sent,
2406 * #GNUNET_SYSERR on error, e.g. the name is too long.
2409 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2410 const struct GNUNET_SOCIAL_Ego *ego,
2412 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2413 struct GNUNET_TIME_Absolute expiration_time,
2414 GNUNET_ResultCallback result_cb,
2417 struct ZoneAddNymRequest *nreq;
2419 size_t name_size = strlen (name) + 1;
2420 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2421 return GNUNET_SYSERR;
2423 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2424 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2425 nreq->header.size = htons (sizeof (*nreq) + name_size);
2426 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2427 nreq->ego_pub_key = ego->pub_key;
2428 nreq->nym_pub_key = *nym_pub_key;
2429 memcpy (&nreq[1], name, name_size);
2431 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2432 add_nym->req = nreq;
2433 add_nym->result_cb = result_cb;
2434 add_nym->result_cls = result_cls;
2436 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2437 op_recv_zone_add_nym_result,
2439 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2445 * Connect application to the social service.
2447 * The @host_place_cb and @guest_place_cb functions are
2448 * initially called for each entered places,
2449 * then later each time a new place is entered with the current application ID.
2455 * @param notify_host
2456 * Function to notify about a place entered as host.
2457 * @param notify_guest
2458 * Function to notify about a place entered as guest..
2460 * Closure for the callbacks.
2462 * @return Handle that can be used to stop listening.
2464 struct GNUNET_SOCIAL_App *
2465 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2467 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2468 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2469 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2470 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2473 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2474 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2478 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2480 app->ego_cb = ego_cb;
2481 app->host_cb = host_cb;
2482 app->guest_cb = guest_cb;
2483 app->connected_cb = connected_cb;
2485 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2486 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2488 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2490 app->id = GNUNET_malloc (app_id_size);
2491 memcpy (app->id, id, app_id_size);
2493 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2494 creq->header.size = htons (sizeof (*creq) + app_id_size);
2495 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2496 memcpy (&creq[1], app->id, app_id_size);
2498 app->connect_msg = &creq->header;
2499 app_send_connect_msg (app);
2506 * Disconnect application.
2509 * Application handle.
2512 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2513 GNUNET_ContinuationCallback disconnect_cb,
2514 void *disconnect_cls)
2516 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO,
2517 disconnect_cb, disconnect_cls);
2522 * Detach application from a place.
2524 * Removes the place from the entered places list for this application.
2525 * Note: this does not disconnect from the place.
2527 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2535 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2536 struct GNUNET_SOCIAL_Place *plc)
2538 struct AppDetachRequest dreq;
2539 dreq.header.size = htons (sizeof (dreq));
2540 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2541 dreq.place_pub_key = plc->pub_key;
2542 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2546 /* end of social_api.c */