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);
418 host_recv_notice_place_leave_modifier (void *cls,
419 const struct GNUNET_PSYC_MessageHeader *msg,
420 const struct GNUNET_MessageHeader *pmsg,
422 enum GNUNET_PSYC_Operator oper,
426 uint16_t full_value_size)
428 struct GNUNET_SOCIAL_Host *hst = cls;
429 if (NULL == hst->notice_place_leave_env)
432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
433 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
435 message_id, oper, name, value_size, (const char *) value);
437 /* skip _nym, it's added later in eom() */
438 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
439 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
442 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
443 GNUNET_PSYC_OP_SET, name, value, value_size);
448 host_recv_notice_place_leave_eom (void *cls,
449 const struct GNUNET_PSYC_MessageHeader *msg,
450 const struct GNUNET_MessageHeader *pmsg,
452 uint8_t is_cancelled)
454 struct GNUNET_SOCIAL_Host *hst = cls;
455 if (NULL == hst->notice_place_leave_env)
458 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
460 "_notice_place_leave: got EOM from nym %s (%s).\n",
461 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
463 if (GNUNET_YES != is_cancelled)
465 if (NULL != hst->farewell_cb)
466 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
467 hst->notice_place_leave_env);
468 /* announce leaving guest to place */
469 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
470 "_nym", hst->notice_place_leave_nym,
471 sizeof (*hst->notice_place_leave_nym));
472 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
473 hst->notice_place_leave_env,
474 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
475 nym_destroy (hst->notice_place_leave_nym);
477 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
478 hst->notice_place_leave_env = NULL;
486 app_send_connect_msg (struct GNUNET_SOCIAL_App *app)
488 uint16_t cmsg_size = ntohs (app->connect_msg->size);
489 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
490 memcpy (cmsg, app->connect_msg, cmsg_size);
491 GNUNET_CLIENT_MANAGER_transmit_now (app->client, cmsg);
497 app_recv_disconnect (void *cls,
498 struct GNUNET_CLIENT_MANAGER_Connection *client,
499 const struct GNUNET_MessageHeader *msg)
501 struct GNUNET_SOCIAL_App *
502 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
504 GNUNET_CLIENT_MANAGER_reconnect (client);
505 app_send_connect_msg (app);
513 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
515 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
516 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
517 memcpy (cmsg, plc->connect_msg, cmsg_size);
518 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
524 place_recv_disconnect (void *cls,
525 struct GNUNET_CLIENT_MANAGER_Connection *client,
526 const struct GNUNET_MessageHeader *msg)
528 struct GNUNET_SOCIAL_Place *
529 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
531 GNUNET_CLIENT_MANAGER_reconnect (client);
532 place_send_connect_msg (plc);
537 place_recv_result (void *cls,
538 struct GNUNET_CLIENT_MANAGER_Connection *client,
539 const struct GNUNET_MessageHeader *msg)
541 struct GNUNET_SOCIAL_Place *
542 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
544 const struct GNUNET_OperationResultMessage *
545 res = (const struct GNUNET_OperationResultMessage *) msg;
547 uint16_t size = ntohs (msg->size);
548 if (size < sizeof (*res))
549 { /* Error, message too small. */
554 uint16_t data_size = size - sizeof (*res);
555 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
556 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
557 GNUNET_ntohll (res->result_code),
563 app_recv_result (void *cls,
564 struct GNUNET_CLIENT_MANAGER_Connection *client,
565 const struct GNUNET_MessageHeader *msg)
567 struct GNUNET_SOCIAL_App *
568 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
570 const struct GNUNET_OperationResultMessage *
571 res = (const struct GNUNET_OperationResultMessage *) msg;
573 uint16_t size = ntohs (msg->size);
574 if (size < sizeof (*res))
575 { /* Error, message too small. */
580 uint16_t data_size = size - sizeof (*res);
581 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
582 GNUNET_CLIENT_MANAGER_op_result (app->client, GNUNET_ntohll (res->op_id),
583 GNUNET_ntohll (res->result_code),
589 op_recv_history_result (void *cls, int64_t result,
590 const void *err_msg, uint16_t err_msg_size)
592 LOG (GNUNET_ERROR_TYPE_DEBUG,
593 "Received history replay result: %" PRId64 ".\n", result);
595 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
597 if (NULL != hist->result_cb)
598 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
605 op_recv_state_result (void *cls, int64_t result,
606 const void *err_msg, uint16_t err_msg_size)
608 LOG (GNUNET_ERROR_TYPE_DEBUG,
609 "Received state request result: %" PRId64 ".\n", result);
611 struct GNUNET_SOCIAL_LookHandle *look = cls;
613 if (NULL != look->result_cb)
614 look->result_cb (look->cls, result, err_msg, err_msg_size);
621 place_recv_history_result (void *cls,
622 struct GNUNET_CLIENT_MANAGER_Connection *client,
623 const struct GNUNET_MessageHeader *msg)
625 struct GNUNET_SOCIAL_Place *
626 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
628 const struct GNUNET_OperationResultMessage *
629 res = (const struct GNUNET_OperationResultMessage *) msg;
630 struct GNUNET_PSYC_MessageHeader *
631 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
633 LOG (GNUNET_ERROR_TYPE_DEBUG,
634 "%p Received historic fragment for message #%" PRIu64 ".\n",
635 plc, GNUNET_ntohll (pmsg->message_id));
637 GNUNET_ResultCallback result_cb = NULL;
638 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
640 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
641 GNUNET_ntohll (res->op_id),
642 &result_cb, (void *) &hist))
643 { /* Operation not found. */
644 LOG (GNUNET_ERROR_TYPE_WARNING,
645 "%p Replay operation not found for historic fragment of message #%"
647 plc, GNUNET_ntohll (pmsg->message_id));
651 uint16_t size = ntohs (msg->size);
652 if (size < sizeof (*res) + sizeof (*pmsg))
653 { /* Error, message too small. */
658 GNUNET_PSYC_slicer_message (hist->slicer,
659 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
664 place_recv_state_result (void *cls,
665 struct GNUNET_CLIENT_MANAGER_Connection *client,
666 const struct GNUNET_MessageHeader *msg)
668 struct GNUNET_SOCIAL_Place *
669 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
671 const struct GNUNET_OperationResultMessage *
672 res = (const struct GNUNET_OperationResultMessage *) msg;
674 GNUNET_ResultCallback result_cb = NULL;
675 struct GNUNET_SOCIAL_LookHandle *look = NULL;
677 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
678 GNUNET_ntohll (res->op_id),
679 &result_cb, (void *) &look))
680 { /* Operation not found. */
684 const struct GNUNET_MessageHeader *
685 mod = (struct GNUNET_MessageHeader *) &res[1];
686 uint16_t mod_size = ntohs (mod->size);
687 if (ntohs (msg->size) - sizeof (*res) != mod_size)
690 LOG (GNUNET_ERROR_TYPE_WARNING,
691 "Invalid modifier size in state result: %u - %u != %u\n",
692 ntohs (msg->size), sizeof (*res), mod_size);
695 switch (ntohs (mod->type))
697 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
699 const struct GNUNET_PSYC_MessageModifier *
700 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
702 const char *name = (const char *) &pmod[1];
703 uint16_t name_size = ntohs (pmod->name_size);
704 if ('\0' != name[name_size - 1])
707 LOG (GNUNET_ERROR_TYPE_WARNING,
708 "Invalid modifier name in state result\n");
711 look->mod_value_size = ntohs (pmod->value_size);
712 look->var_cb (look->cls, mod, name, name + name_size,
713 mod_size - sizeof (*mod) - name_size,
714 look->mod_value_size);
715 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
717 look->mod_value_remaining = look->mod_value_size;
718 look->mod_name = GNUNET_malloc (name_size);
719 memcpy (look->mod_name, name, name_size);
724 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
725 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
726 mod_size - sizeof (*mod), look->mod_value_size);
727 look->mod_value_remaining -= mod_size - sizeof (*mod);
728 if (0 == look->mod_value_remaining)
730 GNUNET_free (look->mod_name);
738 place_recv_message_ack (void *cls,
739 struct GNUNET_CLIENT_MANAGER_Connection *client,
740 const struct GNUNET_MessageHeader *msg)
742 struct GNUNET_SOCIAL_Place *
743 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
744 GNUNET_PSYC_transmit_got_ack (plc->tmit);
749 place_recv_message (void *cls,
750 struct GNUNET_CLIENT_MANAGER_Connection *client,
751 const struct GNUNET_MessageHeader *msg)
753 struct GNUNET_SOCIAL_Place *
754 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
755 GNUNET_PSYC_slicer_message (plc->slicer,
756 (const struct GNUNET_PSYC_MessageHeader *) msg);
761 host_recv_message (void *cls,
762 struct GNUNET_CLIENT_MANAGER_Connection *client,
763 const struct GNUNET_MessageHeader *msg)
765 struct GNUNET_SOCIAL_Host *
766 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
767 GNUNET_PSYC_slicer_message (hst->slicer,
768 (const struct GNUNET_PSYC_MessageHeader *) msg);
769 GNUNET_PSYC_slicer_message (hst->plc.slicer,
770 (const struct GNUNET_PSYC_MessageHeader *) msg);
775 host_recv_enter_ack (void *cls,
776 struct GNUNET_CLIENT_MANAGER_Connection *client,
777 const struct GNUNET_MessageHeader *msg)
779 struct GNUNET_SOCIAL_Host *
780 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
781 sizeof (struct GNUNET_SOCIAL_Place));
783 struct HostEnterAck *hack = (struct HostEnterAck *) msg;
784 hst->plc.pub_key = hack->place_pub_key;
786 int32_t result = ntohl (hack->result_code);
787 if (NULL != hst->enter_cb)
788 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
789 GNUNET_ntohll (hack->max_message_id));
794 host_recv_enter_request (void *cls,
795 struct GNUNET_CLIENT_MANAGER_Connection *client,
796 const struct GNUNET_MessageHeader *msg)
798 struct GNUNET_SOCIAL_Host *
799 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
800 sizeof (struct GNUNET_SOCIAL_Place));
801 if (NULL == hst->answer_door_cb)
804 const char *method_name = NULL;
805 struct GNUNET_PSYC_Environment *env = NULL;
806 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
807 const void *data = NULL;
808 uint16_t data_size = 0;
810 const struct GNUNET_PSYC_JoinRequestMessage *
811 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
812 const struct GNUNET_PSYC_Message *join_msg = NULL;
816 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
818 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
819 LOG (GNUNET_ERROR_TYPE_DEBUG,
820 "Received join_msg of type %u and size %u.\n",
821 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
823 env = GNUNET_PSYC_env_create ();
824 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
825 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
829 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
830 LOG (GNUNET_ERROR_TYPE_WARNING,
831 "Ignoring invalid entry request from nym %s.\n",
838 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
839 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
844 GNUNET_PSYC_env_destroy (env);
845 if (NULL != entry_pmsg)
846 GNUNET_free (entry_pmsg);
851 guest_recv_enter_ack (void *cls,
852 struct GNUNET_CLIENT_MANAGER_Connection *client,
853 const struct GNUNET_MessageHeader *msg)
855 struct GNUNET_SOCIAL_Guest *
856 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
857 sizeof (struct GNUNET_SOCIAL_Place));
859 struct GNUNET_PSYC_CountersResultMessage *
860 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
861 int32_t result = ntohl (cres->result_code);
862 if (NULL != gst->enter_cb)
863 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
864 GNUNET_ntohll (cres->max_message_id));
869 guest_recv_join_decision (void *cls,
870 struct GNUNET_CLIENT_MANAGER_Connection *client,
871 const struct GNUNET_MessageHeader *msg)
873 struct GNUNET_SOCIAL_Guest *
874 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
875 sizeof (struct GNUNET_SOCIAL_Place));
876 const struct GNUNET_PSYC_JoinDecisionMessage *
877 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
879 struct GNUNET_PSYC_Message *pmsg = NULL;
880 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
881 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
883 if (NULL != gst->entry_dcsn_cb)
884 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
889 app_recv_ego (void *cls,
890 struct GNUNET_CLIENT_MANAGER_Connection *client,
891 const struct GNUNET_MessageHeader *msg)
893 struct GNUNET_SOCIAL_App *
894 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
896 struct AppEgoMessage *
897 emsg = (struct AppEgoMessage *) msg;
899 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
901 struct GNUNET_HashCode ego_pub_hash;
902 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
905 struct GNUNET_SOCIAL_Ego *
906 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
909 ego = GNUNET_malloc (sizeof (*ego));
910 ego->pub_key = emsg->ego_pub_key;
911 ego->name = GNUNET_malloc (name_size);
912 memcpy (ego->name, &emsg[1], name_size);
916 ego->name = GNUNET_realloc (ego->name, name_size);
917 memcpy (ego->name, &emsg[1], name_size);
920 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
921 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
923 if (NULL != app->ego_cb)
924 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
929 app_recv_ego_end (void *cls,
930 struct GNUNET_CLIENT_MANAGER_Connection *client,
931 const struct GNUNET_MessageHeader *msg)
933 struct GNUNET_SOCIAL_App *
934 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
939 app_recv_place (void *cls,
940 struct GNUNET_CLIENT_MANAGER_Connection *client,
941 const struct GNUNET_MessageHeader *msg)
943 struct GNUNET_SOCIAL_App *
944 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
946 struct AppPlaceMessage *
947 pmsg = (struct AppPlaceMessage *) msg;
949 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
950 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
953 struct GNUNET_HashCode ego_pub_hash;
954 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
956 struct GNUNET_SOCIAL_Ego *
957 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
964 if (GNUNET_YES == pmsg->is_host)
966 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
968 hconn->plc_msg = *pmsg;
969 if (NULL != app->host_cb)
970 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
974 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
976 gconn->plc_msg = *pmsg;
977 if (NULL != app->guest_cb)
978 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
984 app_recv_place_end (void *cls,
985 struct GNUNET_CLIENT_MANAGER_Connection *client,
986 const struct GNUNET_MessageHeader *msg)
988 struct GNUNET_SOCIAL_App *
989 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
991 if (NULL != app->connected_cb)
992 app->connected_cb (app->cb_cls);
996 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
998 { host_recv_enter_ack, NULL,
999 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1000 sizeof (struct HostEnterAck), GNUNET_NO },
1002 { host_recv_enter_request, NULL,
1003 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1004 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1006 { host_recv_message, NULL,
1007 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1008 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1010 { place_recv_message_ack, NULL,
1011 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1012 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1014 { place_recv_history_result, NULL,
1015 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1016 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1018 { place_recv_state_result, NULL,
1019 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1020 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1022 { place_recv_result, NULL,
1023 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1024 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1026 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1028 { NULL, NULL, 0, 0, GNUNET_NO }
1032 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1034 { guest_recv_enter_ack, NULL,
1035 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1036 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1038 { host_recv_enter_request, NULL,
1039 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1040 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1042 { place_recv_message, NULL,
1043 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1044 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1046 { place_recv_message_ack, NULL,
1047 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1048 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1050 { guest_recv_join_decision, NULL,
1051 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1052 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1054 { place_recv_history_result, NULL,
1055 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1056 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1058 { place_recv_state_result, NULL,
1059 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1060 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1062 { place_recv_result, NULL,
1063 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1064 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1066 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1068 { NULL, NULL, 0, 0, GNUNET_NO }
1072 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1074 { app_recv_ego, NULL,
1075 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1076 sizeof (struct AppEgoMessage), GNUNET_YES },
1078 { app_recv_ego_end, NULL,
1079 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
1080 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1082 { app_recv_place, NULL,
1083 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1084 sizeof (struct AppPlaceMessage), GNUNET_NO },
1086 { app_recv_place_end, NULL,
1087 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
1088 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1090 { app_recv_result, NULL,
1091 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1092 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1094 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1096 { NULL, NULL, 0, 0, GNUNET_NO }
1101 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1103 struct GNUNET_HashCode place_pub_hash;
1104 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "%s place cleanup: %s\n",
1107 GNUNET_YES == plc->is_host ? "host" : "guest",
1108 GNUNET_h2s (&place_pub_hash));
1110 if (NULL != plc->tmit)
1111 GNUNET_PSYC_transmit_destroy (plc->tmit);
1112 if (NULL != plc->connect_msg)
1113 GNUNET_free (plc->connect_msg);
1114 if (NULL != plc->disconnect_cb)
1115 plc->disconnect_cb (plc->disconnect_cls);
1120 host_cleanup (void *cls)
1122 struct GNUNET_SOCIAL_Host *hst = cls;
1123 place_cleanup (&hst->plc);
1124 if (NULL != hst->slicer)
1126 GNUNET_PSYC_slicer_destroy (hst->slicer);
1134 guest_cleanup (void *cls)
1136 struct GNUNET_SOCIAL_Guest *gst = cls;
1137 place_cleanup (&gst->plc);
1145 * Enter a place as host.
1147 * A place is created upon first entering, and it is active until permanently
1148 * left using GNUNET_SOCIAL_host_leave().
1151 * Application handle.
1153 * Identity of the host.
1155 * Private-public key pair of the place.
1156 * NULL to generate a key.
1158 * Policy specifying entry and history restrictions for the place.
1160 * Slicer to handle incoming messages.
1162 * Function called when the place is entered and ready to use.
1163 * @param answer_door_cb
1164 * Function to handle new nyms that want to enter.
1165 * @param farewell_cb
1166 * Function to handle departing nyms.
1168 * Closure for the callbacks.
1170 * @return Handle for the host.
1172 struct GNUNET_SOCIAL_Host *
1173 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1174 const struct GNUNET_SOCIAL_Ego *ego,
1175 enum GNUNET_PSYC_Policy policy,
1176 struct GNUNET_PSYC_Slicer *slicer,
1177 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1178 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1179 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1182 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1183 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1185 plc->cfg = app->cfg;
1186 plc->is_host = GNUNET_YES;
1187 plc->slicer = slicer;
1189 hst->enter_cb = enter_cb;
1190 hst->answer_door_cb = answer_door_cb;
1191 hst->farewell_cb = farewell_cb;
1194 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1195 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1197 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1199 hst->slicer = GNUNET_PSYC_slicer_create ();
1200 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1201 host_recv_notice_place_leave_method,
1202 host_recv_notice_place_leave_modifier,
1203 NULL, host_recv_notice_place_leave_eom, hst);
1205 uint16_t app_id_size = strlen (app->id) + 1;
1206 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1207 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1208 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1209 hreq->policy = policy;
1210 hreq->ego_pub_key = ego->pub_key;
1211 memcpy (&hreq[1], app->id, app_id_size);
1213 plc->connect_msg = &hreq->header;
1214 place_send_connect_msg (plc);
1221 * Reconnect to an already entered place as host.
1224 * Host connection handle.
1225 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1227 * Slicer to handle incoming messages.
1229 * Function called when the place is entered and ready to use.
1230 * @param answer_door_cb
1231 * Function to handle new nyms that want to enter.
1232 * @param farewell_cb
1233 * Function to handle departing nyms.
1235 * Closure for the callbacks.
1237 * @return Handle for the host.
1239 struct GNUNET_SOCIAL_Host *
1240 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1241 struct GNUNET_PSYC_Slicer *slicer,
1242 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1243 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1244 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1247 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1248 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1250 size_t app_id_size = strlen (hconn->app->id) + 1;
1251 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1253 hst->enter_cb = enter_cb;
1254 hst->answer_door_cb = answer_door_cb;
1255 hst->farewell_cb = farewell_cb;
1258 plc->cfg = hconn->app->cfg;
1259 plc->is_host = GNUNET_YES;
1260 plc->slicer = slicer;
1261 plc->pub_key = hconn->plc_msg.place_pub_key;
1262 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1264 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1265 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1267 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1269 hst->slicer = GNUNET_PSYC_slicer_create ();
1270 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1271 host_recv_notice_place_leave_method,
1272 host_recv_notice_place_leave_modifier,
1273 NULL, host_recv_notice_place_leave_eom, hst);
1275 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1276 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1277 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1278 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1279 memcpy (&hreq[1], hconn->app->id, app_id_size);
1281 plc->connect_msg = &hreq->header;
1282 place_send_connect_msg (plc);
1284 GNUNET_free (hconn);
1290 * Decision whether to admit @a nym into the place or refuse entry.
1293 * Host of the place.
1295 * Handle for the entity that wanted to enter.
1296 * @param is_admitted
1297 * #GNUNET_YES if @a nym is admitted,
1298 * #GNUNET_NO if @a nym is refused entry,
1299 * #GNUNET_SYSERR if we cannot answer the request.
1300 * @param method_name
1301 * Method name for the rejection message.
1303 * Environment containing variables for the message, or NULL.
1305 * Data for the rejection message to send back.
1307 * Number of bytes in @a data for method.
1308 * @return #GNUNET_OK on success,
1309 * #GNUNET_SYSERR if the message is too large.
1312 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1313 struct GNUNET_SOCIAL_Nym *nym,
1315 const struct GNUNET_PSYC_Message *entry_resp)
1317 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1318 uint16_t entry_resp_size
1319 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1321 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1322 return GNUNET_SYSERR;
1324 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1325 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1326 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1327 dcsn->is_admitted = htonl (is_admitted);
1328 dcsn->slave_pub_key = nym->pub_key;
1330 if (0 < entry_resp_size)
1331 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1333 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1340 * Throw @a nym out of the place.
1342 * The @a nym reference will remain valid until the
1343 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1344 * which should be very soon after this call.
1347 * Host of the place.
1349 * Handle for the entity to be ejected.
1351 * Environment for the message or NULL.
1354 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1355 const struct GNUNET_SOCIAL_Nym *nym,
1356 struct GNUNET_PSYC_Environment *env)
1359 env = GNUNET_PSYC_env_create ();
1360 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1361 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1362 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1363 GNUNET_SOCIAL_ANNOUNCE_NONE);
1368 * Get the public key of @a ego.
1373 * @return Public key of ego.
1375 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1376 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1378 return &ego->pub_key;
1383 * Get the hash of the public key of @a ego.
1388 * @return Hash of the public key of @a ego.
1390 const struct GNUNET_HashCode *
1391 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1393 return &ego->pub_key_hash;
1398 * Get the name of @a ego.
1403 * @return Public key of @a ego.
1406 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1413 * Get the public key of @a nym.
1415 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1420 * @return Public key of @a nym.
1422 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1423 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1425 return &nym->pub_key;
1430 * Get the hash of the public key of @a nym.
1435 * @return Hash of the public key of @a nym.
1437 const struct GNUNET_HashCode *
1438 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1440 return &nym->pub_key_hash;
1445 * Send a message to all nyms that are present in the place.
1447 * This function is restricted to the host. Nyms can only send requests
1448 * to the host who can decide to relay it to everyone in the place.
1450 * @param host Host of the place.
1451 * @param method_name Method to use for the announcement.
1452 * @param env Environment containing variables for the message and operations
1453 * on objects of the place. Can be NULL.
1454 * @param notify Function to call to get the payload of the announcement.
1455 * @param notify_cls Closure for @a notify.
1456 * @param flags Flags for this announcement.
1458 * @return NULL on error (announcement already in progress?).
1460 struct GNUNET_SOCIAL_Announcement *
1461 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1462 const char *method_name,
1463 const struct GNUNET_PSYC_Environment *env,
1464 GNUNET_PSYC_TransmitNotifyData notify_data,
1465 void *notify_data_cls,
1466 enum GNUNET_SOCIAL_AnnounceFlags flags)
1469 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1470 NULL, notify_data, notify_data_cls, flags))
1471 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1478 * Resume transmitting announcement.
1481 * The announcement to resume.
1484 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1486 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1491 * Cancel announcement.
1494 * The announcement to cancel.
1497 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1499 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1504 * Obtain handle for a hosted place.
1506 * The returned handle can be used to access the place API.
1508 * @param host Handle for the host.
1510 * @return Handle for the hosted place, valid as long as @a host is valid.
1512 struct GNUNET_SOCIAL_Place *
1513 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1520 place_leave (struct GNUNET_SOCIAL_Place *plc)
1522 struct GNUNET_MessageHeader msg;
1523 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1524 msg.size = htons (sizeof (msg));
1525 GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
1530 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1531 GNUNET_ContinuationCallback disconnect_cb,
1532 void *disconnect_cls)
1534 plc->disconnect_cb = disconnect_cb;
1535 plc->disconnect_cls = disconnect_cls;
1537 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1538 GNUNET_YES == plc->is_host
1539 ? host_cleanup : guest_cleanup,
1545 * Disconnect from a home.
1547 * Invalidates host handle.
1550 * The host to disconnect.
1553 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1554 GNUNET_ContinuationCallback disconnect_cb,
1557 place_disconnect (&hst->plc, disconnect_cb, cls);
1562 * Stop hosting the home.
1564 * Sends a _notice_place_closing announcement to the home.
1565 * Invalidates host handle.
1570 * Environment for the message or NULL.
1571 * _nym is set to @e nym regardless whether an @e env is provided.
1572 * @param disconnect_cb
1573 * Function called after the host left the place
1574 * and disconnected from the social service.
1576 * Closure for @a disconnect_cb.
1579 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1580 const struct GNUNET_PSYC_Environment *env,
1581 GNUNET_ContinuationCallback disconnect_cb,
1584 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1585 GNUNET_SOCIAL_ANNOUNCE_NONE);
1586 place_leave (&hst->plc);
1587 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1593 static struct GuestEnterRequest *
1594 guest_enter_request_create (const char *app_id,
1595 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1596 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1597 const struct GNUNET_PeerIdentity *origin,
1599 const struct GNUNET_PeerIdentity *relays,
1600 const struct GNUNET_PSYC_Message *join_msg)
1602 uint16_t app_id_size = strlen (app_id) + 1;
1603 uint16_t join_msg_size = ntohs (join_msg->header.size);
1604 uint16_t relay_size = relay_count * sizeof (*relays);
1606 struct GuestEnterRequest *
1607 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1609 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1610 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1611 greq->place_pub_key = *place_pub_key;
1612 greq->ego_pub_key = *ego_pub_key;
1613 greq->origin = *origin;
1614 greq->relay_count = htonl (relay_count);
1616 char *p = (char *) &greq[1];
1617 memcpy (p, app_id, app_id_size);
1622 memcpy (p, relays, relay_size);
1626 memcpy (p, join_msg, join_msg_size);
1632 * Request entry to a place as a guest.
1635 * Application handle.
1637 * Identity of the guest.
1638 * @param place_pub_key
1639 * Public key of the place to enter.
1641 * Flags for the entry.
1643 * Peer identity of the origin of the underlying multicast group.
1644 * @param relay_count
1645 * Number of elements in the @a relays array.
1647 * Relays for the underlying multicast group.
1648 * @param method_name
1649 * Method name for the message.
1651 * Environment containing variables for the message, or NULL.
1653 * Payload for the message to give to the enter callback.
1655 * Number of bytes in @a data.
1657 * Slicer to use for processing incoming requests from guests.
1659 * @return NULL on errors, otherwise handle for the guest.
1661 struct GNUNET_SOCIAL_Guest *
1662 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1663 const struct GNUNET_SOCIAL_Ego *ego,
1664 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1665 enum GNUNET_PSYC_SlaveJoinFlags flags,
1666 const struct GNUNET_PeerIdentity *origin,
1667 uint32_t relay_count,
1668 const struct GNUNET_PeerIdentity *relays,
1669 const struct GNUNET_PSYC_Message *entry_msg,
1670 struct GNUNET_PSYC_Slicer *slicer,
1671 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1672 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1675 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1676 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1678 plc->ego_pub_key = ego->pub_key;
1679 plc->pub_key = *place_pub_key;
1680 plc->cfg = app->cfg;
1681 plc->is_host = GNUNET_NO;
1682 plc->slicer = slicer;
1684 gst->enter_cb = local_enter_cb;
1685 gst->entry_dcsn_cb = entry_dcsn_cb;
1688 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1689 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1691 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1693 struct GuestEnterRequest *
1694 greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1695 origin, relay_count, relays, entry_msg);
1696 plc->connect_msg = &greq->header;
1697 place_send_connect_msg (plc);
1703 * Request entry to a place by name as a guest.
1706 * Application handle.
1708 * Identity of the guest.
1710 * GNS name of the place to enter. Either in the form of
1711 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1712 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1713 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1714 * pseudonym's place directly.
1716 * Password to decrypt the record, or NULL for cleartext records.
1718 * Entry request message or NULL.
1720 * Slicer to use for processing incoming requests from guests.
1721 * @param local_enter_cb
1722 * Called upon connection established to the social service.
1723 * @param entry_decision_cb
1724 * Called upon receiving entry decision.
1726 * @return NULL on errors, otherwise handle for the guest.
1728 struct GNUNET_SOCIAL_Guest *
1729 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1730 const struct GNUNET_SOCIAL_Ego *ego,
1731 const char *gns_name,
1732 const char *password,
1733 const struct GNUNET_PSYC_Message *join_msg,
1734 struct GNUNET_PSYC_Slicer *slicer,
1735 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1736 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1739 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1740 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1742 if (NULL == password)
1745 uint16_t app_id_size = strlen (app->id) + 1;
1746 uint16_t gns_name_size = strlen (gns_name) + 1;
1747 uint16_t password_size = strlen (password) + 1;
1749 uint16_t join_msg_size = 0;
1750 if (NULL != join_msg);
1751 join_msg_size = ntohs (join_msg->header.size);
1753 uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
1754 + app_id_size + gns_name_size + password_size + join_msg_size;
1755 struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
1756 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1757 greq->header.size = htons (greq_size);
1758 greq->ego_pub_key = ego->pub_key;
1760 char *p = (char *) &greq[1];
1761 memcpy (p, app->id, app_id_size);
1763 memcpy (p, gns_name, gns_name_size);
1765 memcpy (p, password, password_size);
1767 if (NULL != join_msg)
1768 memcpy (p, join_msg, join_msg_size);
1770 gst->enter_cb = local_enter_cb;
1771 gst->entry_dcsn_cb = entry_decision_cb;
1774 plc->ego_pub_key = ego->pub_key;
1775 plc->cfg = app->cfg;
1776 plc->is_host = GNUNET_NO;
1777 plc->slicer = slicer;
1779 plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
1780 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1782 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1784 plc->connect_msg = &greq->header;
1785 place_send_connect_msg (plc);
1792 * Reconnect to an already entered place as guest.
1795 * Guest connection handle.
1796 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1798 * Flags for the entry.
1800 * Slicer to use for processing incoming requests from guests.
1801 * @param local_enter_cb
1802 * Called upon connection established to the social service.
1803 * @param entry_decision_cb
1804 * Called upon receiving entry decision.
1806 * @return NULL on errors, otherwise handle for the guest.
1808 struct GNUNET_SOCIAL_Guest *
1809 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1810 enum GNUNET_PSYC_SlaveJoinFlags flags,
1811 struct GNUNET_PSYC_Slicer *slicer,
1812 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1815 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1816 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1818 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1819 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1820 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1821 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1822 greq->header.size = htons (greq_size);
1823 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1824 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1825 greq->flags = htonl (flags);
1827 memcpy (&greq[1], gconn->app->id, app_id_size);
1829 gst->enter_cb = local_enter_cb;
1832 plc->cfg = gconn->app->cfg;
1833 plc->is_host = GNUNET_NO;
1834 plc->slicer = slicer;
1835 plc->pub_key = gconn->plc_msg.place_pub_key;
1836 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1838 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1839 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1841 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1843 plc->connect_msg = &greq->header;
1844 place_send_connect_msg (plc);
1846 GNUNET_free (gconn);
1852 * Talk to the host of the place.
1855 * Place where we want to talk to the host.
1856 * @param method_name
1857 * Method to invoke on the host.
1859 * Environment containing variables for the message, or NULL.
1860 * @param notify_data
1861 * Function to use to get the payload for the method.
1862 * @param notify_data_cls
1863 * Closure for @a notify_data.
1865 * Flags for the message being sent.
1867 * @return NULL if we are already trying to talk to the host,
1868 * otherwise handle to cancel the request.
1870 struct GNUNET_SOCIAL_TalkRequest *
1871 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1872 const char *method_name,
1873 const struct GNUNET_PSYC_Environment *env,
1874 GNUNET_PSYC_TransmitNotifyData notify_data,
1875 void *notify_data_cls,
1876 enum GNUNET_SOCIAL_TalkFlags flags)
1878 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1879 GNUNET_assert (NULL != plc->tmit);
1882 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1883 NULL, notify_data, notify_data_cls, flags))
1884 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1891 * Resume talking to the host of the place.
1894 * Talk request to resume.
1897 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1899 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1904 * Cancel talking to the host of the place.
1907 * Talk request to cancel.
1910 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1912 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1917 * Disconnect from a place.
1919 * Invalidates guest handle.
1922 * The guest to disconnect.
1925 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1926 GNUNET_ContinuationCallback disconnect_cb,
1929 place_disconnect (&gst->plc, disconnect_cb, cls);
1934 * Leave a place temporarily or permanently.
1936 * Notifies the owner of the place about leaving, and destroys the place handle.
1940 * @param keep_active
1941 * Keep place active after last application disconnected.
1942 * #GNUNET_YES or #GNUNET_NO
1944 * Optional environment for the leave message if @a keep_active
1945 * is #GNUNET_NO. NULL if not needed.
1947 * Called upon disconnecting from the social service.
1950 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1951 struct GNUNET_PSYC_Environment *env,
1952 GNUNET_ContinuationCallback disconnect_cb,
1955 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1956 GNUNET_SOCIAL_TALK_NONE);
1957 place_leave (&gst->plc);
1958 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1963 * Obtain handle for a place entered as guest.
1965 * The returned handle can be used to access the place API.
1967 * @param guest Handle for the guest.
1969 * @return Handle for the place, valid as long as @a guest is valid.
1971 struct GNUNET_SOCIAL_Place *
1972 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1979 * Obtain the public key of a place.
1984 * @return Public key of the place.
1986 const struct GNUNET_CRYPTO_EddsaPublicKey *
1987 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
1989 return &plc->pub_key;
1994 * Set message processing @a flags for a @a method_prefix.
1998 * @param method_prefix
1999 * Method prefix @a flags apply to.
2001 * The flags that apply to a matching @a method_prefix.
2004 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2005 const char *method_prefix,
2006 enum GNUNET_SOCIAL_MsgProcFlags flags)
2008 GNUNET_assert (NULL != method_prefix);
2009 struct MsgProcRequest *mpreq;
2010 uint16_t method_size = strnlen (method_prefix,
2011 GNUNET_SERVER_MAX_MESSAGE_SIZE
2012 - sizeof (*mpreq)) + 1;
2013 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2014 mpreq = GNUNET_malloc (sizeof (*mpreq) + method_size);
2016 mpreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2017 mpreq->header.size = htons (sizeof (*mpreq) + method_size);
2018 mpreq->flags = htonl (flags);
2019 memcpy (&mpreq[1], method_prefix, method_size);
2021 GNUNET_CLIENT_MANAGER_transmit (plc->client, &mpreq->header);
2022 GNUNET_free (mpreq);
2027 * Clear all message processing flags previously set for this place.
2030 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2032 struct GNUNET_MessageHeader req;
2033 req.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2034 req.size = htons (sizeof (req));
2035 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req);
2039 static struct GNUNET_SOCIAL_HistoryRequest *
2040 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2041 uint64_t start_message_id,
2042 uint64_t end_message_id,
2043 uint64_t message_limit,
2044 const char *method_prefix,
2046 struct GNUNET_PSYC_Slicer *slicer,
2047 GNUNET_ResultCallback result_cb,
2050 struct GNUNET_PSYC_HistoryRequestMessage *req;
2051 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2053 hist->slicer = slicer;
2054 hist->result_cb = result_cb;
2056 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2057 &op_recv_history_result, hist);
2059 GNUNET_assert (NULL != method_prefix);
2060 uint16_t method_size = strnlen (method_prefix,
2061 GNUNET_SERVER_MAX_MESSAGE_SIZE
2062 - sizeof (*req)) + 1;
2063 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2064 req = GNUNET_malloc (sizeof (*req) + method_size);
2065 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2066 req->header.size = htons (sizeof (*req) + method_size);
2067 req->start_message_id = GNUNET_htonll (start_message_id);
2068 req->end_message_id = GNUNET_htonll (end_message_id);
2069 req->message_limit = GNUNET_htonll (message_limit);
2070 req->flags = htonl (flags);
2071 req->op_id = GNUNET_htonll (hist->op_id);
2072 memcpy (&req[1], method_prefix, method_size);
2074 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2081 * Learn about the history of a place.
2083 * Messages are returned through the @a slicer function
2084 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2087 * Place we want to learn more about.
2088 * @param start_message_id
2089 * First historic message we are interested in.
2090 * @param end_message_id
2091 * Last historic message we are interested in (inclusive).
2092 * @param method_prefix
2093 * Only retrieve messages with this method prefix.
2095 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2097 * Slicer to use for retrieved messages.
2098 * Can be the same as the slicer of the place.
2100 * Function called after all messages retrieved.
2101 * NULL if not needed.
2102 * @param cls Closure for @a result_cb.
2104 struct GNUNET_SOCIAL_HistoryRequest *
2105 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2106 uint64_t start_message_id,
2107 uint64_t end_message_id,
2108 const char *method_prefix,
2110 struct GNUNET_PSYC_Slicer *slicer,
2111 GNUNET_ResultCallback result_cb,
2114 return place_history_replay (plc, start_message_id, end_message_id, 0,
2115 method_prefix, flags, slicer, result_cb, cls);
2120 * Learn about the history of a place.
2122 * Sends messages through the slicer function of the place where
2123 * start_message_id <= message_id <= end_message_id.
2124 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2126 * To get the latest message, use 0 for both the start and end message ID.
2129 * Place we want to learn more about.
2130 * @param message_limit
2131 * Maximum number of historic messages we are interested in.
2132 * @param method_prefix
2133 * Only retrieve messages with this method prefix.
2135 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2137 * Function called after all messages retrieved.
2138 * NULL if not needed.
2139 * @param cls Closure for @a result_cb.
2141 struct GNUNET_SOCIAL_HistoryRequest *
2142 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2143 uint64_t message_limit,
2144 const char *method_prefix,
2146 struct GNUNET_PSYC_Slicer *slicer,
2147 GNUNET_ResultCallback result_cb,
2150 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2151 slicer, result_cb, cls);
2156 * Cancel learning about the history of a place.
2159 * History lesson to cancel.
2162 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2164 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2170 * Request matching state variables.
2172 static struct GNUNET_SOCIAL_LookHandle *
2173 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2174 uint16_t type, const char *name,
2175 GNUNET_PSYC_StateVarCallback var_cb,
2176 GNUNET_ResultCallback result_cb, void *cls)
2178 struct GNUNET_PSYC_StateRequestMessage *req;
2179 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2181 look->var_cb = var_cb;
2182 look->result_cb = result_cb;
2184 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2185 &op_recv_state_result, look);
2187 GNUNET_assert (NULL != name);
2188 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2189 - sizeof (*req)) + 1;
2190 req = GNUNET_malloc (sizeof (*req) + name_size);
2191 req->header.type = htons (type);
2192 req->header.size = htons (sizeof (*req) + name_size);
2193 req->op_id = GNUNET_htonll (look->op_id);
2194 memcpy (&req[1], name, name_size);
2196 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2203 * Look at a particular object in the place.
2205 * The best matching object is returned (its name might be less specific than
2206 * what was requested).
2209 * The place where to look.
2211 * Full name of the object.
2213 * Set to the size of the returned value.
2215 * @return NULL if there is no such object at this place.
2217 struct GNUNET_SOCIAL_LookHandle *
2218 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2219 const char *full_name,
2220 GNUNET_PSYC_StateVarCallback var_cb,
2221 GNUNET_ResultCallback result_cb,
2224 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2225 full_name, var_cb, result_cb, cls);
2230 * Look for objects in the place with a matching name prefix.
2233 * The place where to look.
2234 * @param name_prefix
2235 * Look at objects with names beginning with this value.
2237 * Function to call for each object found.
2239 * Closure for callback function.
2241 * @return Handle that can be used to stop looking at objects.
2243 struct GNUNET_SOCIAL_LookHandle *
2244 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2245 const char *name_prefix,
2246 GNUNET_PSYC_StateVarCallback var_cb,
2247 GNUNET_ResultCallback result_cb,
2250 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2251 name_prefix, var_cb, result_cb, cls);
2256 * Cancel a state request operation.
2259 * Handle for the operation to cancel.
2262 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2264 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2270 op_recv_zone_add_place_result (void *cls, int64_t result,
2271 const void *err_msg, uint16_t err_msg_size)
2273 LOG (GNUNET_ERROR_TYPE_DEBUG,
2274 "Received zone add place result: %" PRId64 ".\n", result);
2276 struct ZoneAddPlaceHandle *add_plc = cls;
2277 if (NULL != add_plc->result_cb)
2278 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2280 GNUNET_free (add_plc->req);
2281 GNUNET_free (add_plc);
2286 * Advertise @e place in the GNS zone of @e ego.
2289 * Application handle.
2292 * @param place_pub_key
2293 * Public key of place to add.
2295 * The name for the PLACE record to put in the zone.
2297 * Password used to encrypt the record or NULL to keep it cleartext.
2298 * @param relay_count
2299 * Number of elements in the @a relays array.
2301 * List of relays to put in the PLACE record to advertise
2302 * as entry points to the place in addition to the origin.
2303 * @param expiration_time
2304 * Expiration time of the record, use 0 to remove the record.
2306 * Function called with the result of the operation.
2308 * Closure for @a result_cb
2310 * @return #GNUNET_OK if the request was sent,
2311 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2314 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2315 const struct GNUNET_SOCIAL_Ego *ego,
2317 const char *password,
2318 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2319 const struct GNUNET_PeerIdentity *origin,
2320 uint32_t relay_count,
2321 const struct GNUNET_PeerIdentity *relays,
2322 struct GNUNET_TIME_Absolute expiration_time,
2323 GNUNET_ResultCallback result_cb,
2326 struct ZoneAddPlaceRequest *preq;
2327 size_t name_size = strlen (name) + 1;
2328 size_t password_size = strlen (password) + 1;
2329 size_t relay_size = relay_count * sizeof (*relays);
2330 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2332 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2333 return GNUNET_SYSERR;
2335 preq = GNUNET_malloc (preq_size);
2336 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2337 preq->header.size = htons (preq_size);
2338 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2339 preq->ego_pub_key = ego->pub_key;
2340 preq->place_pub_key = *place_pub_key;
2341 preq->origin = *origin;
2342 preq->relay_count = htonl (relay_count);
2344 char *p = (char *) &preq[1];
2345 memcpy (p, name, name_size);
2347 memcpy (p, password, password_size);
2349 memcpy (p, relays, relay_size);
2351 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2352 add_plc->req = preq;
2353 add_plc->result_cb = result_cb;
2354 add_plc->result_cls = result_cls;
2356 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2357 op_recv_zone_add_place_result,
2359 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2365 op_recv_zone_add_nym_result (void *cls, int64_t result,
2366 const void *err_msg, uint16_t err_msg_size)
2368 LOG (GNUNET_ERROR_TYPE_DEBUG,
2369 "Received zone add nym result: %" PRId64 ".\n", result);
2371 struct ZoneAddNymHandle *add_nym = cls;
2372 if (NULL != add_nym->result_cb)
2373 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2375 GNUNET_free (add_nym->req);
2376 GNUNET_free (add_nym);
2381 * Add nym to the GNS zone of @e ego.
2388 * The name for the PKEY record to put in the zone.
2389 * @param nym_pub_key
2390 * Public key of nym to add.
2391 * @param expiration_time
2392 * Expiration time of the record, use 0 to remove the record.
2394 * Function called with the result of the operation.
2396 * Closure for @a result_cb
2398 * @return #GNUNET_OK if the request was sent,
2399 * #GNUNET_SYSERR on error, e.g. the name is too long.
2402 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2403 const struct GNUNET_SOCIAL_Ego *ego,
2405 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2406 struct GNUNET_TIME_Absolute expiration_time,
2407 GNUNET_ResultCallback result_cb,
2410 struct ZoneAddNymRequest *nreq;
2412 size_t name_size = strlen (name) + 1;
2413 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2414 return GNUNET_SYSERR;
2416 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2417 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2418 nreq->header.size = htons (sizeof (*nreq) + name_size);
2419 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2420 nreq->ego_pub_key = ego->pub_key;
2421 nreq->nym_pub_key = *nym_pub_key;
2422 memcpy (&nreq[1], name, name_size);
2424 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2425 add_nym->req = nreq;
2426 add_nym->result_cb = result_cb;
2427 add_nym->result_cls = result_cls;
2429 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2430 op_recv_zone_add_nym_result,
2432 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2438 * Connect application to the social service.
2440 * The @host_place_cb and @guest_place_cb functions are
2441 * initially called for each entered places,
2442 * then later each time a new place is entered with the current application ID.
2448 * @param notify_host
2449 * Function to notify about a place entered as host.
2450 * @param notify_guest
2451 * Function to notify about a place entered as guest..
2453 * Closure for the callbacks.
2455 * @return Handle that can be used to stop listening.
2457 struct GNUNET_SOCIAL_App *
2458 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2460 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2461 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2462 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2463 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2466 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2467 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2471 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2473 app->ego_cb = ego_cb;
2474 app->host_cb = host_cb;
2475 app->guest_cb = guest_cb;
2476 app->connected_cb = connected_cb;
2478 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2479 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2481 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2483 app->id = GNUNET_malloc (app_id_size);
2484 memcpy (app->id, id, app_id_size);
2486 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2487 creq->header.size = htons (sizeof (*creq) + app_id_size);
2488 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2489 memcpy (&creq[1], app->id, app_id_size);
2491 app->connect_msg = &creq->header;
2492 app_send_connect_msg (app);
2499 * Disconnect application.
2502 * Application handle.
2505 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2506 GNUNET_ContinuationCallback disconnect_cb,
2507 void *disconnect_cls)
2509 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO,
2510 disconnect_cb, disconnect_cls);
2515 * Detach application from a place.
2517 * Removes the place from the entered places list for this application.
2518 * Note: this does not disconnect from the place.
2520 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2528 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2529 struct GNUNET_SOCIAL_Place *plc)
2531 struct AppDetachRequest dreq;
2532 dreq.header.size = htons (sizeof (dreq));
2533 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2534 dreq.place_pub_key = plc->pub_key;
2535 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2539 /* end of social_api.c */