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 GNUNET_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 GNUNET_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 GNUNET_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 GNUNET_memcpy (ego->name, &emsg[1], name_size);
918 ego->name = GNUNET_realloc (ego->name, name_size);
919 GNUNET_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, "Failure to obtain ego %s.\n",
963 GNUNET_h2s (&ego_pub_hash));
968 if (GNUNET_YES == pmsg->is_host)
970 if (NULL != app->host_cb) {
971 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
973 hconn->plc_msg = *pmsg;
974 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
975 // FIXME: should this have a GNUNET_free (hconn) here?
978 else if (NULL != app->guest_cb)
980 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
982 gconn->plc_msg = *pmsg;
983 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
984 GNUNET_free (gconn); // FIXME: is this correct here?
990 app_recv_place_end (void *cls,
991 struct GNUNET_CLIENT_MANAGER_Connection *client,
992 const struct GNUNET_MessageHeader *msg)
994 struct GNUNET_SOCIAL_App *
995 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
997 if (NULL != app->connected_cb)
998 app->connected_cb (app->cb_cls);
1002 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1004 { host_recv_enter_ack, NULL,
1005 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1006 sizeof (struct HostEnterAck), GNUNET_NO },
1008 { host_recv_enter_request, NULL,
1009 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1010 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1012 { host_recv_message, NULL,
1013 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1014 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1016 { place_recv_message_ack, NULL,
1017 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1018 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1020 { place_recv_history_result, NULL,
1021 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1022 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1024 { place_recv_state_result, NULL,
1025 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1026 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1028 { place_recv_result, NULL,
1029 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1030 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1032 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1034 { NULL, NULL, 0, 0, GNUNET_NO }
1038 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1040 { guest_recv_enter_ack, NULL,
1041 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1042 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1044 { host_recv_enter_request, NULL,
1045 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1046 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1048 { place_recv_message, NULL,
1049 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1050 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1052 { place_recv_message_ack, NULL,
1053 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1054 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1056 { guest_recv_join_decision, NULL,
1057 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1058 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1060 { place_recv_history_result, NULL,
1061 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1062 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1064 { place_recv_state_result, NULL,
1065 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1066 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1068 { place_recv_result, NULL,
1069 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1070 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1072 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1074 { NULL, NULL, 0, 0, GNUNET_NO }
1078 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1080 { app_recv_ego, NULL,
1081 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1082 sizeof (struct AppEgoMessage), GNUNET_YES },
1084 { app_recv_ego_end, NULL,
1085 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
1086 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1088 { app_recv_place, NULL,
1089 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1090 sizeof (struct AppPlaceMessage), GNUNET_NO },
1092 { app_recv_place_end, NULL,
1093 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
1094 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1096 { app_recv_result, NULL,
1097 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1098 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1100 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1102 { NULL, NULL, 0, 0, GNUNET_NO }
1107 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1109 struct GNUNET_HashCode place_pub_hash;
1110 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "%s place cleanup: %s\n",
1113 GNUNET_YES == plc->is_host ? "host" : "guest",
1114 GNUNET_h2s (&place_pub_hash));
1116 if (NULL != plc->tmit)
1117 GNUNET_PSYC_transmit_destroy (plc->tmit);
1118 if (NULL != plc->connect_msg)
1119 GNUNET_free (plc->connect_msg);
1120 if (NULL != plc->disconnect_cb)
1121 plc->disconnect_cb (plc->disconnect_cls);
1126 host_cleanup (void *cls)
1128 struct GNUNET_SOCIAL_Host *hst = cls;
1129 place_cleanup (&hst->plc);
1130 if (NULL != hst->slicer)
1132 GNUNET_PSYC_slicer_destroy (hst->slicer);
1140 guest_cleanup (void *cls)
1142 struct GNUNET_SOCIAL_Guest *gst = cls;
1143 place_cleanup (&gst->plc);
1151 * Enter a place as host.
1153 * A place is created upon first entering, and it is active until permanently
1154 * left using GNUNET_SOCIAL_host_leave().
1157 * Application handle.
1159 * Identity of the host.
1161 * Private-public key pair of the place.
1162 * NULL to generate a key.
1164 * Policy specifying entry and history restrictions for the place.
1166 * Slicer to handle incoming messages.
1168 * Function called when the place is entered and ready to use.
1169 * @param answer_door_cb
1170 * Function to handle new nyms that want to enter.
1171 * @param farewell_cb
1172 * Function to handle departing nyms.
1174 * Closure for the callbacks.
1176 * @return Handle for the host.
1178 struct GNUNET_SOCIAL_Host *
1179 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1180 const struct GNUNET_SOCIAL_Ego *ego,
1181 enum GNUNET_PSYC_Policy policy,
1182 struct GNUNET_PSYC_Slicer *slicer,
1183 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1184 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1185 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1188 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1189 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1191 plc->cfg = app->cfg;
1192 plc->is_host = GNUNET_YES;
1193 plc->slicer = slicer;
1195 hst->enter_cb = enter_cb;
1196 hst->answer_door_cb = answer_door_cb;
1197 hst->farewell_cb = farewell_cb;
1200 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1201 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1203 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1205 hst->slicer = GNUNET_PSYC_slicer_create ();
1206 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1207 host_recv_notice_place_leave_method,
1208 host_recv_notice_place_leave_modifier,
1209 NULL, host_recv_notice_place_leave_eom, hst);
1211 uint16_t app_id_size = strlen (app->id) + 1;
1212 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1213 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1214 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1215 hreq->policy = policy;
1216 hreq->ego_pub_key = ego->pub_key;
1217 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1219 plc->connect_msg = &hreq->header;
1220 place_send_connect_msg (plc);
1227 * Reconnect to an already entered place as host.
1230 * Host connection handle.
1231 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1233 * Slicer to handle incoming messages.
1235 * Function called when the place is entered and ready to use.
1236 * @param answer_door_cb
1237 * Function to handle new nyms that want to enter.
1238 * @param farewell_cb
1239 * Function to handle departing nyms.
1241 * Closure for the callbacks.
1243 * @return Handle for the host.
1245 struct GNUNET_SOCIAL_Host *
1246 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1247 struct GNUNET_PSYC_Slicer *slicer,
1248 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1249 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1250 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1253 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1254 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1256 size_t app_id_size = strlen (hconn->app->id) + 1;
1257 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1259 hst->enter_cb = enter_cb;
1260 hst->answer_door_cb = answer_door_cb;
1261 hst->farewell_cb = farewell_cb;
1264 plc->cfg = hconn->app->cfg;
1265 plc->is_host = GNUNET_YES;
1266 plc->slicer = slicer;
1267 plc->pub_key = hconn->plc_msg.place_pub_key;
1268 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1270 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1271 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1273 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1275 hst->slicer = GNUNET_PSYC_slicer_create ();
1276 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1277 host_recv_notice_place_leave_method,
1278 host_recv_notice_place_leave_modifier,
1279 NULL, host_recv_notice_place_leave_eom, hst);
1281 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1282 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1283 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1284 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1285 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1287 plc->connect_msg = &hreq->header;
1288 place_send_connect_msg (plc);
1290 GNUNET_free (hconn);
1296 * Decision whether to admit @a nym into the place or refuse entry.
1299 * Host of the place.
1301 * Handle for the entity that wanted to enter.
1302 * @param is_admitted
1303 * #GNUNET_YES if @a nym is admitted,
1304 * #GNUNET_NO if @a nym is refused entry,
1305 * #GNUNET_SYSERR if we cannot answer the request.
1306 * @param method_name
1307 * Method name for the rejection message.
1309 * Environment containing variables for the message, or NULL.
1311 * Data for the rejection message to send back.
1313 * Number of bytes in @a data for method.
1314 * @return #GNUNET_OK on success,
1315 * #GNUNET_SYSERR if the message is too large.
1318 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1319 struct GNUNET_SOCIAL_Nym *nym,
1321 const struct GNUNET_PSYC_Message *entry_resp)
1323 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1324 uint16_t entry_resp_size
1325 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1327 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1328 return GNUNET_SYSERR;
1330 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1331 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1332 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1333 dcsn->is_admitted = htonl (is_admitted);
1334 dcsn->slave_pub_key = nym->pub_key;
1336 if (0 < entry_resp_size)
1337 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1339 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1346 * Throw @a nym out of the place.
1348 * The @a nym reference will remain valid until the
1349 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1350 * which should be very soon after this call.
1353 * Host of the place.
1355 * Handle for the entity to be ejected.
1357 * Environment for the message or NULL.
1360 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1361 const struct GNUNET_SOCIAL_Nym *nym,
1362 struct GNUNET_PSYC_Environment *e)
1364 struct GNUNET_PSYC_Environment *env = e;
1366 env = GNUNET_PSYC_env_create ();
1367 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1368 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1369 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1370 GNUNET_SOCIAL_ANNOUNCE_NONE);
1372 GNUNET_PSYC_env_destroy (env);
1377 * Get the public key of @a ego.
1382 * @return Public key of ego.
1384 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1385 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1387 return &ego->pub_key;
1392 * Get the hash of the public key of @a ego.
1397 * @return Hash of the public key of @a ego.
1399 const struct GNUNET_HashCode *
1400 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1402 return &ego->pub_key_hash;
1407 * Get the name of @a ego.
1412 * @return Public key of @a ego.
1415 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1422 * Get the public key of @a nym.
1424 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1429 * @return Public key of @a nym.
1431 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1432 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1434 return &nym->pub_key;
1439 * Get the hash of the public key of @a nym.
1444 * @return Hash of the public key of @a nym.
1446 const struct GNUNET_HashCode *
1447 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1449 return &nym->pub_key_hash;
1454 * Send a message to all nyms that are present in the place.
1456 * This function is restricted to the host. Nyms can only send requests
1457 * to the host who can decide to relay it to everyone in the place.
1459 * @param host Host of the place.
1460 * @param method_name Method to use for the announcement.
1461 * @param env Environment containing variables for the message and operations
1462 * on objects of the place. Can be NULL.
1463 * @param notify Function to call to get the payload of the announcement.
1464 * @param notify_cls Closure for @a notify.
1465 * @param flags Flags for this announcement.
1467 * @return NULL on error (announcement already in progress?).
1469 struct GNUNET_SOCIAL_Announcement *
1470 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1471 const char *method_name,
1472 const struct GNUNET_PSYC_Environment *env,
1473 GNUNET_PSYC_TransmitNotifyData notify_data,
1474 void *notify_data_cls,
1475 enum GNUNET_SOCIAL_AnnounceFlags flags)
1478 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1479 NULL, notify_data, notify_data_cls, flags))
1480 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1487 * Resume transmitting announcement.
1490 * The announcement to resume.
1493 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1495 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1500 * Cancel announcement.
1503 * The announcement to cancel.
1506 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1508 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1513 * Obtain handle for a hosted place.
1515 * The returned handle can be used to access the place API.
1517 * @param host Handle for the host.
1519 * @return Handle for the hosted place, valid as long as @a host is valid.
1521 struct GNUNET_SOCIAL_Place *
1522 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1529 place_leave (struct GNUNET_SOCIAL_Place *plc)
1531 struct GNUNET_MessageHeader msg;
1532 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1533 msg.size = htons (sizeof (msg));
1534 GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
1539 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1540 GNUNET_ContinuationCallback disconnect_cb,
1541 void *disconnect_cls)
1543 plc->disconnect_cb = disconnect_cb;
1544 plc->disconnect_cls = disconnect_cls;
1546 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1547 GNUNET_YES == plc->is_host
1548 ? host_cleanup : guest_cleanup,
1554 * Disconnect from a home.
1556 * Invalidates host handle.
1559 * The host to disconnect.
1562 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1563 GNUNET_ContinuationCallback disconnect_cb,
1566 place_disconnect (&hst->plc, disconnect_cb, cls);
1571 * Stop hosting the home.
1573 * Sends a _notice_place_closing announcement to the home.
1574 * Invalidates host handle.
1579 * Environment for the message or NULL.
1580 * _nym is set to @e nym regardless whether an @e env is provided.
1581 * @param disconnect_cb
1582 * Function called after the host left the place
1583 * and disconnected from the social service.
1585 * Closure for @a disconnect_cb.
1588 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1589 const struct GNUNET_PSYC_Environment *env,
1590 GNUNET_ContinuationCallback disconnect_cb,
1593 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1594 GNUNET_SOCIAL_ANNOUNCE_NONE);
1595 place_leave (&hst->plc);
1596 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1602 static struct GuestEnterRequest *
1603 guest_enter_request_create (const char *app_id,
1604 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1605 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1606 const struct GNUNET_PeerIdentity *origin,
1608 const struct GNUNET_PeerIdentity *relays,
1609 const struct GNUNET_PSYC_Message *join_msg)
1611 uint16_t app_id_size = strlen (app_id) + 1;
1612 uint16_t join_msg_size = ntohs (join_msg->header.size);
1613 uint16_t relay_size = relay_count * sizeof (*relays);
1615 struct GuestEnterRequest *
1616 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1618 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1619 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1620 greq->place_pub_key = *place_pub_key;
1621 greq->ego_pub_key = *ego_pub_key;
1622 greq->origin = *origin;
1623 greq->relay_count = htonl (relay_count);
1625 char *p = (char *) &greq[1];
1626 GNUNET_memcpy (p, app_id, app_id_size);
1631 GNUNET_memcpy (p, relays, relay_size);
1635 GNUNET_memcpy (p, join_msg, join_msg_size);
1641 * Request entry to a place as a guest.
1644 * Application handle.
1646 * Identity of the guest.
1647 * @param place_pub_key
1648 * Public key of the place to enter.
1650 * Flags for the entry.
1652 * Peer identity of the origin of the underlying multicast group.
1653 * @param relay_count
1654 * Number of elements in the @a relays array.
1656 * Relays for the underlying multicast group.
1657 * @param method_name
1658 * Method name for the message.
1660 * Environment containing variables for the message, or NULL.
1662 * Payload for the message to give to the enter callback.
1664 * Number of bytes in @a data.
1666 * Slicer to use for processing incoming requests from guests.
1668 * @return NULL on errors, otherwise handle for the guest.
1670 struct GNUNET_SOCIAL_Guest *
1671 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1672 const struct GNUNET_SOCIAL_Ego *ego,
1673 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1674 enum GNUNET_PSYC_SlaveJoinFlags flags,
1675 const struct GNUNET_PeerIdentity *origin,
1676 uint32_t relay_count,
1677 const struct GNUNET_PeerIdentity *relays,
1678 const struct GNUNET_PSYC_Message *entry_msg,
1679 struct GNUNET_PSYC_Slicer *slicer,
1680 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1681 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1684 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1685 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1687 plc->ego_pub_key = ego->pub_key;
1688 plc->pub_key = *place_pub_key;
1689 plc->cfg = app->cfg;
1690 plc->is_host = GNUNET_NO;
1691 plc->slicer = slicer;
1693 gst->enter_cb = local_enter_cb;
1694 gst->entry_dcsn_cb = entry_dcsn_cb;
1697 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1698 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1700 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1702 struct GuestEnterRequest *
1703 greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1704 origin, relay_count, relays, entry_msg);
1705 plc->connect_msg = &greq->header;
1706 place_send_connect_msg (plc);
1712 * Request entry to a place by name as a guest.
1715 * Application handle.
1717 * Identity of the guest.
1719 * GNS name of the place to enter. Either in the form of
1720 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1721 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1722 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1723 * pseudonym's place directly.
1725 * Password to decrypt the record, or NULL for cleartext records.
1727 * Entry request message or NULL.
1729 * Slicer to use for processing incoming requests from guests.
1730 * @param local_enter_cb
1731 * Called upon connection established to the social service.
1732 * @param entry_decision_cb
1733 * Called upon receiving entry decision.
1735 * @return NULL on errors, otherwise handle for the guest.
1737 struct GNUNET_SOCIAL_Guest *
1738 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1739 const struct GNUNET_SOCIAL_Ego *ego,
1740 const char *gns_name,
1741 const char *password,
1742 const struct GNUNET_PSYC_Message *join_msg,
1743 struct GNUNET_PSYC_Slicer *slicer,
1744 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1745 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1748 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1749 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1751 if (NULL == password)
1754 uint16_t app_id_size = strlen (app->id) + 1;
1755 uint16_t gns_name_size = strlen (gns_name) + 1;
1756 uint16_t password_size = strlen (password) + 1;
1758 uint16_t join_msg_size = 0;
1759 if (NULL != join_msg)
1760 join_msg_size = ntohs (join_msg->header.size);
1762 uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
1763 + app_id_size + gns_name_size + password_size + join_msg_size;
1764 struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
1765 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1766 greq->header.size = htons (greq_size);
1767 greq->ego_pub_key = ego->pub_key;
1769 char *p = (char *) &greq[1];
1770 GNUNET_memcpy (p, app->id, app_id_size);
1772 GNUNET_memcpy (p, gns_name, gns_name_size);
1774 GNUNET_memcpy (p, password, password_size);
1776 if (NULL != join_msg)
1777 GNUNET_memcpy (p, join_msg, join_msg_size);
1779 gst->enter_cb = local_enter_cb;
1780 gst->entry_dcsn_cb = entry_decision_cb;
1783 plc->ego_pub_key = ego->pub_key;
1784 plc->cfg = app->cfg;
1785 plc->is_host = GNUNET_NO;
1786 plc->slicer = slicer;
1788 plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
1789 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1791 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1793 plc->connect_msg = &greq->header;
1794 place_send_connect_msg (plc);
1801 * Reconnect to an already entered place as guest.
1804 * Guest connection handle.
1805 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1807 * Flags for the entry.
1809 * Slicer to use for processing incoming requests from guests.
1810 * @param local_enter_cb
1811 * Called upon connection established to the social service.
1812 * @param entry_decision_cb
1813 * Called upon receiving entry decision.
1815 * @return NULL on errors, otherwise handle for the guest.
1817 struct GNUNET_SOCIAL_Guest *
1818 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1819 enum GNUNET_PSYC_SlaveJoinFlags flags,
1820 struct GNUNET_PSYC_Slicer *slicer,
1821 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1824 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1825 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1827 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1828 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1829 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1830 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1831 greq->header.size = htons (greq_size);
1832 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1833 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1834 greq->flags = htonl (flags);
1836 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1838 gst->enter_cb = local_enter_cb;
1841 plc->cfg = gconn->app->cfg;
1842 plc->is_host = GNUNET_NO;
1843 plc->slicer = slicer;
1844 plc->pub_key = gconn->plc_msg.place_pub_key;
1845 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1847 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1848 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1850 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1852 plc->connect_msg = &greq->header;
1853 place_send_connect_msg (plc);
1855 GNUNET_free (gconn);
1861 * Talk to the host of the place.
1864 * Place where we want to talk to the host.
1865 * @param method_name
1866 * Method to invoke on the host.
1868 * Environment containing variables for the message, or NULL.
1869 * @param notify_data
1870 * Function to use to get the payload for the method.
1871 * @param notify_data_cls
1872 * Closure for @a notify_data.
1874 * Flags for the message being sent.
1876 * @return NULL if we are already trying to talk to the host,
1877 * otherwise handle to cancel the request.
1879 struct GNUNET_SOCIAL_TalkRequest *
1880 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1881 const char *method_name,
1882 const struct GNUNET_PSYC_Environment *env,
1883 GNUNET_PSYC_TransmitNotifyData notify_data,
1884 void *notify_data_cls,
1885 enum GNUNET_SOCIAL_TalkFlags flags)
1887 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1888 GNUNET_assert (NULL != plc->tmit);
1891 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1892 NULL, notify_data, notify_data_cls, flags))
1893 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1900 * Resume talking to the host of the place.
1903 * Talk request to resume.
1906 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1908 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1913 * Cancel talking to the host of the place.
1916 * Talk request to cancel.
1919 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1921 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1926 * Disconnect from a place.
1928 * Invalidates guest handle.
1931 * The guest to disconnect.
1934 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1935 GNUNET_ContinuationCallback disconnect_cb,
1938 place_disconnect (&gst->plc, disconnect_cb, cls);
1943 * Leave a place temporarily or permanently.
1945 * Notifies the owner of the place about leaving, and destroys the place handle.
1949 * @param keep_active
1950 * Keep place active after last application disconnected.
1951 * #GNUNET_YES or #GNUNET_NO
1953 * Optional environment for the leave message if @a keep_active
1954 * is #GNUNET_NO. NULL if not needed.
1956 * Called upon disconnecting from the social service.
1959 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1960 struct GNUNET_PSYC_Environment *env,
1961 GNUNET_ContinuationCallback disconnect_cb,
1964 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1965 GNUNET_SOCIAL_TALK_NONE);
1966 place_leave (&gst->plc);
1967 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1972 * Obtain handle for a place entered as guest.
1974 * The returned handle can be used to access the place API.
1976 * @param guest Handle for the guest.
1978 * @return Handle for the place, valid as long as @a guest is valid.
1980 struct GNUNET_SOCIAL_Place *
1981 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1988 * Obtain the public key of a place.
1993 * @return Public key of the place.
1995 const struct GNUNET_CRYPTO_EddsaPublicKey *
1996 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
1998 return &plc->pub_key;
2003 * Set message processing @a flags for a @a method_prefix.
2007 * @param method_prefix
2008 * Method prefix @a flags apply to.
2010 * The flags that apply to a matching @a method_prefix.
2013 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2014 const char *method_prefix,
2015 enum GNUNET_SOCIAL_MsgProcFlags flags)
2017 GNUNET_assert (NULL != method_prefix);
2018 struct MsgProcRequest *mpreq;
2019 uint16_t method_size = strnlen (method_prefix,
2020 GNUNET_SERVER_MAX_MESSAGE_SIZE
2021 - sizeof (*mpreq)) + 1;
2022 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2023 mpreq = GNUNET_malloc (sizeof (*mpreq) + method_size);
2025 mpreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2026 mpreq->header.size = htons (sizeof (*mpreq) + method_size);
2027 mpreq->flags = htonl (flags);
2028 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2030 GNUNET_CLIENT_MANAGER_transmit (plc->client, &mpreq->header);
2031 GNUNET_free (mpreq);
2036 * Clear all message processing flags previously set for this place.
2039 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2041 struct GNUNET_MessageHeader req;
2042 req.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2043 req.size = htons (sizeof (req));
2044 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req);
2048 static struct GNUNET_SOCIAL_HistoryRequest *
2049 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2050 uint64_t start_message_id,
2051 uint64_t end_message_id,
2052 uint64_t message_limit,
2053 const char *method_prefix,
2055 struct GNUNET_PSYC_Slicer *slicer,
2056 GNUNET_ResultCallback result_cb,
2059 struct GNUNET_PSYC_HistoryRequestMessage *req;
2060 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2062 hist->slicer = slicer;
2063 hist->result_cb = result_cb;
2065 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2066 &op_recv_history_result, hist);
2068 GNUNET_assert (NULL != method_prefix);
2069 uint16_t method_size = strnlen (method_prefix,
2070 GNUNET_SERVER_MAX_MESSAGE_SIZE
2071 - sizeof (*req)) + 1;
2072 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2073 req = GNUNET_malloc (sizeof (*req) + method_size);
2074 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2075 req->header.size = htons (sizeof (*req) + method_size);
2076 req->start_message_id = GNUNET_htonll (start_message_id);
2077 req->end_message_id = GNUNET_htonll (end_message_id);
2078 req->message_limit = GNUNET_htonll (message_limit);
2079 req->flags = htonl (flags);
2080 req->op_id = GNUNET_htonll (hist->op_id);
2081 GNUNET_memcpy (&req[1], method_prefix, method_size);
2083 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2090 * Learn about the history of a place.
2092 * Messages are returned through the @a slicer function
2093 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2096 * Place we want to learn more about.
2097 * @param start_message_id
2098 * First historic message we are interested in.
2099 * @param end_message_id
2100 * Last historic message we are interested in (inclusive).
2101 * @param method_prefix
2102 * Only retrieve messages with this method prefix.
2104 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2106 * Slicer to use for retrieved messages.
2107 * Can be the same as the slicer of the place.
2109 * Function called after all messages retrieved.
2110 * NULL if not needed.
2111 * @param cls Closure for @a result_cb.
2113 struct GNUNET_SOCIAL_HistoryRequest *
2114 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2115 uint64_t start_message_id,
2116 uint64_t end_message_id,
2117 const char *method_prefix,
2119 struct GNUNET_PSYC_Slicer *slicer,
2120 GNUNET_ResultCallback result_cb,
2123 return place_history_replay (plc, start_message_id, end_message_id, 0,
2124 method_prefix, flags, slicer, result_cb, cls);
2129 * Learn about the history of a place.
2131 * Sends messages through the slicer function of the place where
2132 * start_message_id <= message_id <= end_message_id.
2133 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2135 * To get the latest message, use 0 for both the start and end message ID.
2138 * Place we want to learn more about.
2139 * @param message_limit
2140 * Maximum number of historic messages we are interested in.
2141 * @param method_prefix
2142 * Only retrieve messages with this method prefix.
2144 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2146 * Function called after all messages retrieved.
2147 * NULL if not needed.
2148 * @param cls Closure for @a result_cb.
2150 struct GNUNET_SOCIAL_HistoryRequest *
2151 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2152 uint64_t message_limit,
2153 const char *method_prefix,
2155 struct GNUNET_PSYC_Slicer *slicer,
2156 GNUNET_ResultCallback result_cb,
2159 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2160 slicer, result_cb, cls);
2165 * Cancel learning about the history of a place.
2168 * History lesson to cancel.
2171 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2173 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2179 * Request matching state variables.
2181 static struct GNUNET_SOCIAL_LookHandle *
2182 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2183 uint16_t type, const char *name,
2184 GNUNET_PSYC_StateVarCallback var_cb,
2185 GNUNET_ResultCallback result_cb, void *cls)
2187 struct GNUNET_PSYC_StateRequestMessage *req;
2188 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2190 look->var_cb = var_cb;
2191 look->result_cb = result_cb;
2193 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2194 &op_recv_state_result, look);
2196 GNUNET_assert (NULL != name);
2197 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2198 - sizeof (*req)) + 1;
2199 req = GNUNET_malloc (sizeof (*req) + name_size);
2200 req->header.type = htons (type);
2201 req->header.size = htons (sizeof (*req) + name_size);
2202 req->op_id = GNUNET_htonll (look->op_id);
2203 GNUNET_memcpy (&req[1], name, name_size);
2205 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2212 * Look at a particular object in the place.
2214 * The best matching object is returned (its name might be less specific than
2215 * what was requested).
2218 * The place where to look.
2220 * Full name of the object.
2222 * Set to the size of the returned value.
2224 * @return NULL if there is no such object at this place.
2226 struct GNUNET_SOCIAL_LookHandle *
2227 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2228 const char *full_name,
2229 GNUNET_PSYC_StateVarCallback var_cb,
2230 GNUNET_ResultCallback result_cb,
2233 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2234 full_name, var_cb, result_cb, cls);
2239 * Look for objects in the place with a matching name prefix.
2242 * The place where to look.
2243 * @param name_prefix
2244 * Look at objects with names beginning with this value.
2246 * Function to call for each object found.
2248 * Closure for callback function.
2250 * @return Handle that can be used to stop looking at objects.
2252 struct GNUNET_SOCIAL_LookHandle *
2253 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2254 const char *name_prefix,
2255 GNUNET_PSYC_StateVarCallback var_cb,
2256 GNUNET_ResultCallback result_cb,
2259 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2260 name_prefix, var_cb, result_cb, cls);
2265 * Cancel a state request operation.
2268 * Handle for the operation to cancel.
2271 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2273 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2279 op_recv_zone_add_place_result (void *cls, int64_t result,
2280 const void *err_msg, uint16_t err_msg_size)
2282 LOG (GNUNET_ERROR_TYPE_DEBUG,
2283 "Received zone add place result: %" PRId64 ".\n", result);
2285 struct ZoneAddPlaceHandle *add_plc = cls;
2286 if (NULL != add_plc->result_cb)
2287 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2289 GNUNET_free (add_plc->req);
2290 GNUNET_free (add_plc);
2295 * Advertise @e place in the GNS zone of @e ego.
2298 * Application handle.
2301 * @param place_pub_key
2302 * Public key of place to add.
2304 * The name for the PLACE record to put in the zone.
2306 * Password used to encrypt the record or NULL to keep it cleartext.
2307 * @param relay_count
2308 * Number of elements in the @a relays array.
2310 * List of relays to put in the PLACE record to advertise
2311 * as entry points to the place in addition to the origin.
2312 * @param expiration_time
2313 * Expiration time of the record, use 0 to remove the record.
2315 * Function called with the result of the operation.
2317 * Closure for @a result_cb
2319 * @return #GNUNET_OK if the request was sent,
2320 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2323 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2324 const struct GNUNET_SOCIAL_Ego *ego,
2326 const char *password,
2327 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2328 const struct GNUNET_PeerIdentity *origin,
2329 uint32_t relay_count,
2330 const struct GNUNET_PeerIdentity *relays,
2331 struct GNUNET_TIME_Absolute expiration_time,
2332 GNUNET_ResultCallback result_cb,
2335 struct ZoneAddPlaceRequest *preq;
2336 size_t name_size = strlen (name) + 1;
2337 size_t password_size = strlen (password) + 1;
2338 size_t relay_size = relay_count * sizeof (*relays);
2339 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2341 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2342 return GNUNET_SYSERR;
2344 preq = GNUNET_malloc (preq_size);
2345 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2346 preq->header.size = htons (preq_size);
2347 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2348 preq->ego_pub_key = ego->pub_key;
2349 preq->place_pub_key = *place_pub_key;
2350 preq->origin = *origin;
2351 preq->relay_count = htonl (relay_count);
2353 char *p = (char *) &preq[1];
2354 GNUNET_memcpy (p, name, name_size);
2356 GNUNET_memcpy (p, password, password_size);
2358 GNUNET_memcpy (p, relays, relay_size);
2360 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2361 add_plc->req = preq;
2362 add_plc->result_cb = result_cb;
2363 add_plc->result_cls = result_cls;
2365 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2366 op_recv_zone_add_place_result,
2368 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2374 op_recv_zone_add_nym_result (void *cls, int64_t result,
2375 const void *err_msg, uint16_t err_msg_size)
2377 LOG (GNUNET_ERROR_TYPE_DEBUG,
2378 "Received zone add nym result: %" PRId64 ".\n", result);
2380 struct ZoneAddNymHandle *add_nym = cls;
2381 if (NULL != add_nym->result_cb)
2382 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2384 GNUNET_free (add_nym->req);
2385 GNUNET_free (add_nym);
2390 * Add nym to the GNS zone of @e ego.
2397 * The name for the PKEY record to put in the zone.
2398 * @param nym_pub_key
2399 * Public key of nym to add.
2400 * @param expiration_time
2401 * Expiration time of the record, use 0 to remove the record.
2403 * Function called with the result of the operation.
2405 * Closure for @a result_cb
2407 * @return #GNUNET_OK if the request was sent,
2408 * #GNUNET_SYSERR on error, e.g. the name is too long.
2411 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2412 const struct GNUNET_SOCIAL_Ego *ego,
2414 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2415 struct GNUNET_TIME_Absolute expiration_time,
2416 GNUNET_ResultCallback result_cb,
2419 struct ZoneAddNymRequest *nreq;
2421 size_t name_size = strlen (name) + 1;
2422 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2423 return GNUNET_SYSERR;
2425 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2426 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2427 nreq->header.size = htons (sizeof (*nreq) + name_size);
2428 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2429 nreq->ego_pub_key = ego->pub_key;
2430 nreq->nym_pub_key = *nym_pub_key;
2431 GNUNET_memcpy (&nreq[1], name, name_size);
2433 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2434 add_nym->req = nreq;
2435 add_nym->result_cb = result_cb;
2436 add_nym->result_cls = result_cls;
2438 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2439 op_recv_zone_add_nym_result,
2441 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2447 * Connect application to the social service.
2449 * The @host_place_cb and @guest_place_cb functions are
2450 * initially called for each entered places,
2451 * then later each time a new place is entered with the current application ID.
2458 * Function to notify about an available ego.
2460 * Function to notify about a place entered as host.
2462 * Function to notify about a place entered as guest.
2464 * Closure for the callbacks.
2466 * @return Handle that can be used to stop listening.
2468 struct GNUNET_SOCIAL_App *
2469 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2471 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2472 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2473 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2474 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2477 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2478 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2482 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2484 app->ego_cb = ego_cb;
2485 app->host_cb = host_cb;
2486 app->guest_cb = guest_cb;
2487 app->connected_cb = connected_cb;
2489 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2490 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2492 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2494 app->id = GNUNET_malloc (app_id_size);
2495 GNUNET_memcpy (app->id, id, app_id_size);
2497 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2498 creq->header.size = htons (sizeof (*creq) + app_id_size);
2499 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2500 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2502 app->connect_msg = &creq->header;
2503 app_send_connect_msg (app);
2510 * Disconnect application.
2513 * Application handle.
2514 * @param disconnect_cb
2515 * Disconnect callback.
2516 * @param disconnect_cls
2517 * Disconnect closure.
2520 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2521 GNUNET_ContinuationCallback disconnect_cb,
2522 void *disconnect_cls)
2524 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO,
2525 disconnect_cb, disconnect_cls);
2530 * Detach application from a place.
2532 * Removes the place from the entered places list for this application.
2533 * Note: this does not disconnect from the place.
2535 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2543 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2544 struct GNUNET_SOCIAL_Place *plc)
2546 struct AppDetachRequest dreq;
2547 dreq.header.size = htons (sizeof (dreq));
2548 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2549 dreq.place_pub_key = plc->pub_key;
2550 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2554 /* end of social_api.c */