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);
960 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
961 "Failure to obtain ego. Is identity service running?\n");
966 if (GNUNET_YES == pmsg->is_host)
968 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
970 hconn->plc_msg = *pmsg;
971 if (NULL != app->host_cb)
972 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
976 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
978 gconn->plc_msg = *pmsg;
979 if (NULL != app->guest_cb)
980 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
986 app_recv_place_end (void *cls,
987 struct GNUNET_CLIENT_MANAGER_Connection *client,
988 const struct GNUNET_MessageHeader *msg)
990 struct GNUNET_SOCIAL_App *
991 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
993 if (NULL != app->connected_cb)
994 app->connected_cb (app->cb_cls);
998 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1000 { host_recv_enter_ack, NULL,
1001 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1002 sizeof (struct HostEnterAck), GNUNET_NO },
1004 { host_recv_enter_request, NULL,
1005 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1006 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1008 { host_recv_message, NULL,
1009 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1010 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1012 { place_recv_message_ack, NULL,
1013 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1014 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1016 { place_recv_history_result, NULL,
1017 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1018 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1020 { place_recv_state_result, NULL,
1021 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1022 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1024 { place_recv_result, NULL,
1025 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1026 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1028 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1030 { NULL, NULL, 0, 0, GNUNET_NO }
1034 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1036 { guest_recv_enter_ack, NULL,
1037 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1038 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1040 { host_recv_enter_request, NULL,
1041 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1042 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1044 { place_recv_message, NULL,
1045 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1046 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1048 { place_recv_message_ack, NULL,
1049 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1050 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1052 { guest_recv_join_decision, NULL,
1053 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1054 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1056 { place_recv_history_result, NULL,
1057 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1058 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1060 { place_recv_state_result, NULL,
1061 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1062 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1064 { place_recv_result, NULL,
1065 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1066 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1068 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1070 { NULL, NULL, 0, 0, GNUNET_NO }
1074 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1076 { app_recv_ego, NULL,
1077 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1078 sizeof (struct AppEgoMessage), GNUNET_YES },
1080 { app_recv_ego_end, NULL,
1081 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
1082 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1084 { app_recv_place, NULL,
1085 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1086 sizeof (struct AppPlaceMessage), GNUNET_NO },
1088 { app_recv_place_end, NULL,
1089 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
1090 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1092 { app_recv_result, NULL,
1093 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1094 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1096 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1098 { NULL, NULL, 0, 0, GNUNET_NO }
1103 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1105 struct GNUNET_HashCode place_pub_hash;
1106 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "%s place cleanup: %s\n",
1109 GNUNET_YES == plc->is_host ? "host" : "guest",
1110 GNUNET_h2s (&place_pub_hash));
1112 if (NULL != plc->tmit)
1113 GNUNET_PSYC_transmit_destroy (plc->tmit);
1114 if (NULL != plc->connect_msg)
1115 GNUNET_free (plc->connect_msg);
1116 if (NULL != plc->disconnect_cb)
1117 plc->disconnect_cb (plc->disconnect_cls);
1122 host_cleanup (void *cls)
1124 struct GNUNET_SOCIAL_Host *hst = cls;
1125 place_cleanup (&hst->plc);
1126 if (NULL != hst->slicer)
1128 GNUNET_PSYC_slicer_destroy (hst->slicer);
1136 guest_cleanup (void *cls)
1138 struct GNUNET_SOCIAL_Guest *gst = cls;
1139 place_cleanup (&gst->plc);
1147 * Enter a place as host.
1149 * A place is created upon first entering, and it is active until permanently
1150 * left using GNUNET_SOCIAL_host_leave().
1153 * Application handle.
1155 * Identity of the host.
1157 * Private-public key pair of the place.
1158 * NULL to generate a key.
1160 * Policy specifying entry and history restrictions for the place.
1162 * Slicer to handle incoming messages.
1164 * Function called when the place is entered and ready to use.
1165 * @param answer_door_cb
1166 * Function to handle new nyms that want to enter.
1167 * @param farewell_cb
1168 * Function to handle departing nyms.
1170 * Closure for the callbacks.
1172 * @return Handle for the host.
1174 struct GNUNET_SOCIAL_Host *
1175 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1176 const struct GNUNET_SOCIAL_Ego *ego,
1177 enum GNUNET_PSYC_Policy policy,
1178 struct GNUNET_PSYC_Slicer *slicer,
1179 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1180 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1181 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1184 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1185 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1187 plc->cfg = app->cfg;
1188 plc->is_host = GNUNET_YES;
1189 plc->slicer = slicer;
1191 hst->enter_cb = enter_cb;
1192 hst->answer_door_cb = answer_door_cb;
1193 hst->farewell_cb = farewell_cb;
1196 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1197 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1199 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1201 hst->slicer = GNUNET_PSYC_slicer_create ();
1202 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1203 host_recv_notice_place_leave_method,
1204 host_recv_notice_place_leave_modifier,
1205 NULL, host_recv_notice_place_leave_eom, hst);
1207 uint16_t app_id_size = strlen (app->id) + 1;
1208 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1209 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1210 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1211 hreq->policy = policy;
1212 hreq->ego_pub_key = ego->pub_key;
1213 memcpy (&hreq[1], app->id, app_id_size);
1215 plc->connect_msg = &hreq->header;
1216 place_send_connect_msg (plc);
1223 * Reconnect to an already entered place as host.
1226 * Host connection handle.
1227 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1229 * Slicer to handle incoming messages.
1231 * Function called when the place is entered and ready to use.
1232 * @param answer_door_cb
1233 * Function to handle new nyms that want to enter.
1234 * @param farewell_cb
1235 * Function to handle departing nyms.
1237 * Closure for the callbacks.
1239 * @return Handle for the host.
1241 struct GNUNET_SOCIAL_Host *
1242 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1243 struct GNUNET_PSYC_Slicer *slicer,
1244 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1245 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1246 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1249 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1250 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1252 size_t app_id_size = strlen (hconn->app->id) + 1;
1253 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1255 hst->enter_cb = enter_cb;
1256 hst->answer_door_cb = answer_door_cb;
1257 hst->farewell_cb = farewell_cb;
1260 plc->cfg = hconn->app->cfg;
1261 plc->is_host = GNUNET_YES;
1262 plc->slicer = slicer;
1263 plc->pub_key = hconn->plc_msg.place_pub_key;
1264 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1266 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1267 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1269 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1271 hst->slicer = GNUNET_PSYC_slicer_create ();
1272 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1273 host_recv_notice_place_leave_method,
1274 host_recv_notice_place_leave_modifier,
1275 NULL, host_recv_notice_place_leave_eom, hst);
1277 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1278 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1279 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1280 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1281 memcpy (&hreq[1], hconn->app->id, app_id_size);
1283 plc->connect_msg = &hreq->header;
1284 place_send_connect_msg (plc);
1286 GNUNET_free (hconn);
1292 * Decision whether to admit @a nym into the place or refuse entry.
1295 * Host of the place.
1297 * Handle for the entity that wanted to enter.
1298 * @param is_admitted
1299 * #GNUNET_YES if @a nym is admitted,
1300 * #GNUNET_NO if @a nym is refused entry,
1301 * #GNUNET_SYSERR if we cannot answer the request.
1302 * @param method_name
1303 * Method name for the rejection message.
1305 * Environment containing variables for the message, or NULL.
1307 * Data for the rejection message to send back.
1309 * Number of bytes in @a data for method.
1310 * @return #GNUNET_OK on success,
1311 * #GNUNET_SYSERR if the message is too large.
1314 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1315 struct GNUNET_SOCIAL_Nym *nym,
1317 const struct GNUNET_PSYC_Message *entry_resp)
1319 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1320 uint16_t entry_resp_size
1321 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1323 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1324 return GNUNET_SYSERR;
1326 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1327 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1328 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1329 dcsn->is_admitted = htonl (is_admitted);
1330 dcsn->slave_pub_key = nym->pub_key;
1332 if (0 < entry_resp_size)
1333 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1335 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1342 * Throw @a nym out of the place.
1344 * The @a nym reference will remain valid until the
1345 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1346 * which should be very soon after this call.
1349 * Host of the place.
1351 * Handle for the entity to be ejected.
1353 * Environment for the message or NULL.
1356 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1357 const struct GNUNET_SOCIAL_Nym *nym,
1358 struct GNUNET_PSYC_Environment *env)
1361 env = GNUNET_PSYC_env_create ();
1362 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1363 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1364 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1365 GNUNET_SOCIAL_ANNOUNCE_NONE);
1370 * Get the public key of @a ego.
1375 * @return Public key of ego.
1377 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1378 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1380 return &ego->pub_key;
1385 * Get the hash of the public key of @a ego.
1390 * @return Hash of the public key of @a ego.
1392 const struct GNUNET_HashCode *
1393 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1395 return &ego->pub_key_hash;
1400 * Get the name of @a ego.
1405 * @return Public key of @a ego.
1408 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1415 * Get the public key of @a nym.
1417 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1422 * @return Public key of @a nym.
1424 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1425 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1427 return &nym->pub_key;
1432 * Get the hash of the public key of @a nym.
1437 * @return Hash of the public key of @a nym.
1439 const struct GNUNET_HashCode *
1440 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1442 return &nym->pub_key_hash;
1447 * Send a message to all nyms that are present in the place.
1449 * This function is restricted to the host. Nyms can only send requests
1450 * to the host who can decide to relay it to everyone in the place.
1452 * @param host Host of the place.
1453 * @param method_name Method to use for the announcement.
1454 * @param env Environment containing variables for the message and operations
1455 * on objects of the place. Can be NULL.
1456 * @param notify Function to call to get the payload of the announcement.
1457 * @param notify_cls Closure for @a notify.
1458 * @param flags Flags for this announcement.
1460 * @return NULL on error (announcement already in progress?).
1462 struct GNUNET_SOCIAL_Announcement *
1463 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1464 const char *method_name,
1465 const struct GNUNET_PSYC_Environment *env,
1466 GNUNET_PSYC_TransmitNotifyData notify_data,
1467 void *notify_data_cls,
1468 enum GNUNET_SOCIAL_AnnounceFlags flags)
1471 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1472 NULL, notify_data, notify_data_cls, flags))
1473 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1480 * Resume transmitting announcement.
1483 * The announcement to resume.
1486 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1488 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1493 * Cancel announcement.
1496 * The announcement to cancel.
1499 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1501 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1506 * Obtain handle for a hosted place.
1508 * The returned handle can be used to access the place API.
1510 * @param host Handle for the host.
1512 * @return Handle for the hosted place, valid as long as @a host is valid.
1514 struct GNUNET_SOCIAL_Place *
1515 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1522 place_leave (struct GNUNET_SOCIAL_Place *plc)
1524 struct GNUNET_MessageHeader msg;
1525 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1526 msg.size = htons (sizeof (msg));
1527 GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
1532 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1533 GNUNET_ContinuationCallback disconnect_cb,
1534 void *disconnect_cls)
1536 plc->disconnect_cb = disconnect_cb;
1537 plc->disconnect_cls = disconnect_cls;
1539 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1540 GNUNET_YES == plc->is_host
1541 ? host_cleanup : guest_cleanup,
1547 * Disconnect from a home.
1549 * Invalidates host handle.
1552 * The host to disconnect.
1555 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1556 GNUNET_ContinuationCallback disconnect_cb,
1559 place_disconnect (&hst->plc, disconnect_cb, cls);
1564 * Stop hosting the home.
1566 * Sends a _notice_place_closing announcement to the home.
1567 * Invalidates host handle.
1572 * Environment for the message or NULL.
1573 * _nym is set to @e nym regardless whether an @e env is provided.
1574 * @param disconnect_cb
1575 * Function called after the host left the place
1576 * and disconnected from the social service.
1578 * Closure for @a disconnect_cb.
1581 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1582 const struct GNUNET_PSYC_Environment *env,
1583 GNUNET_ContinuationCallback disconnect_cb,
1586 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1587 GNUNET_SOCIAL_ANNOUNCE_NONE);
1588 place_leave (&hst->plc);
1589 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1595 static struct GuestEnterRequest *
1596 guest_enter_request_create (const char *app_id,
1597 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1598 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1599 const struct GNUNET_PeerIdentity *origin,
1601 const struct GNUNET_PeerIdentity *relays,
1602 const struct GNUNET_PSYC_Message *join_msg)
1604 uint16_t app_id_size = strlen (app_id) + 1;
1605 uint16_t join_msg_size = ntohs (join_msg->header.size);
1606 uint16_t relay_size = relay_count * sizeof (*relays);
1608 struct GuestEnterRequest *
1609 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1611 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1612 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1613 greq->place_pub_key = *place_pub_key;
1614 greq->ego_pub_key = *ego_pub_key;
1615 greq->origin = *origin;
1616 greq->relay_count = htonl (relay_count);
1618 char *p = (char *) &greq[1];
1619 memcpy (p, app_id, app_id_size);
1624 memcpy (p, relays, relay_size);
1628 memcpy (p, join_msg, join_msg_size);
1634 * Request entry to a place as a guest.
1637 * Application handle.
1639 * Identity of the guest.
1640 * @param place_pub_key
1641 * Public key of the place to enter.
1643 * Flags for the entry.
1645 * Peer identity of the origin of the underlying multicast group.
1646 * @param relay_count
1647 * Number of elements in the @a relays array.
1649 * Relays for the underlying multicast group.
1650 * @param method_name
1651 * Method name for the message.
1653 * Environment containing variables for the message, or NULL.
1655 * Payload for the message to give to the enter callback.
1657 * Number of bytes in @a data.
1659 * Slicer to use for processing incoming requests from guests.
1661 * @return NULL on errors, otherwise handle for the guest.
1663 struct GNUNET_SOCIAL_Guest *
1664 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1665 const struct GNUNET_SOCIAL_Ego *ego,
1666 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1667 enum GNUNET_PSYC_SlaveJoinFlags flags,
1668 const struct GNUNET_PeerIdentity *origin,
1669 uint32_t relay_count,
1670 const struct GNUNET_PeerIdentity *relays,
1671 const struct GNUNET_PSYC_Message *entry_msg,
1672 struct GNUNET_PSYC_Slicer *slicer,
1673 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1674 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1677 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1678 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1680 plc->ego_pub_key = ego->pub_key;
1681 plc->pub_key = *place_pub_key;
1682 plc->cfg = app->cfg;
1683 plc->is_host = GNUNET_NO;
1684 plc->slicer = slicer;
1686 gst->enter_cb = local_enter_cb;
1687 gst->entry_dcsn_cb = entry_dcsn_cb;
1690 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1691 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1693 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1695 struct GuestEnterRequest *
1696 greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1697 origin, relay_count, relays, entry_msg);
1698 plc->connect_msg = &greq->header;
1699 place_send_connect_msg (plc);
1705 * Request entry to a place by name as a guest.
1708 * Application handle.
1710 * Identity of the guest.
1712 * GNS name of the place to enter. Either in the form of
1713 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1714 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1715 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1716 * pseudonym's place directly.
1718 * Password to decrypt the record, or NULL for cleartext records.
1720 * Entry request message or NULL.
1722 * Slicer to use for processing incoming requests from guests.
1723 * @param local_enter_cb
1724 * Called upon connection established to the social service.
1725 * @param entry_decision_cb
1726 * Called upon receiving entry decision.
1728 * @return NULL on errors, otherwise handle for the guest.
1730 struct GNUNET_SOCIAL_Guest *
1731 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1732 const struct GNUNET_SOCIAL_Ego *ego,
1733 const char *gns_name,
1734 const char *password,
1735 const struct GNUNET_PSYC_Message *join_msg,
1736 struct GNUNET_PSYC_Slicer *slicer,
1737 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1738 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1741 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1742 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1744 if (NULL == password)
1747 uint16_t app_id_size = strlen (app->id) + 1;
1748 uint16_t gns_name_size = strlen (gns_name) + 1;
1749 uint16_t password_size = strlen (password) + 1;
1751 uint16_t join_msg_size = 0;
1752 if (NULL != join_msg);
1753 join_msg_size = ntohs (join_msg->header.size);
1755 uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
1756 + app_id_size + gns_name_size + password_size + join_msg_size;
1757 struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
1758 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1759 greq->header.size = htons (greq_size);
1760 greq->ego_pub_key = ego->pub_key;
1762 char *p = (char *) &greq[1];
1763 memcpy (p, app->id, app_id_size);
1765 memcpy (p, gns_name, gns_name_size);
1767 memcpy (p, password, password_size);
1769 if (NULL != join_msg)
1770 memcpy (p, join_msg, join_msg_size);
1772 gst->enter_cb = local_enter_cb;
1773 gst->entry_dcsn_cb = entry_decision_cb;
1776 plc->ego_pub_key = ego->pub_key;
1777 plc->cfg = app->cfg;
1778 plc->is_host = GNUNET_NO;
1779 plc->slicer = slicer;
1781 plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
1782 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1784 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1786 plc->connect_msg = &greq->header;
1787 place_send_connect_msg (plc);
1794 * Reconnect to an already entered place as guest.
1797 * Guest connection handle.
1798 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1800 * Flags for the entry.
1802 * Slicer to use for processing incoming requests from guests.
1803 * @param local_enter_cb
1804 * Called upon connection established to the social service.
1805 * @param entry_decision_cb
1806 * Called upon receiving entry decision.
1808 * @return NULL on errors, otherwise handle for the guest.
1810 struct GNUNET_SOCIAL_Guest *
1811 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1812 enum GNUNET_PSYC_SlaveJoinFlags flags,
1813 struct GNUNET_PSYC_Slicer *slicer,
1814 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1817 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1818 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1820 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1821 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1822 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1823 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1824 greq->header.size = htons (greq_size);
1825 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1826 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1827 greq->flags = htonl (flags);
1829 memcpy (&greq[1], gconn->app->id, app_id_size);
1831 gst->enter_cb = local_enter_cb;
1834 plc->cfg = gconn->app->cfg;
1835 plc->is_host = GNUNET_NO;
1836 plc->slicer = slicer;
1837 plc->pub_key = gconn->plc_msg.place_pub_key;
1838 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1840 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1841 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1843 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1845 plc->connect_msg = &greq->header;
1846 place_send_connect_msg (plc);
1848 GNUNET_free (gconn);
1854 * Talk to the host of the place.
1857 * Place where we want to talk to the host.
1858 * @param method_name
1859 * Method to invoke on the host.
1861 * Environment containing variables for the message, or NULL.
1862 * @param notify_data
1863 * Function to use to get the payload for the method.
1864 * @param notify_data_cls
1865 * Closure for @a notify_data.
1867 * Flags for the message being sent.
1869 * @return NULL if we are already trying to talk to the host,
1870 * otherwise handle to cancel the request.
1872 struct GNUNET_SOCIAL_TalkRequest *
1873 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1874 const char *method_name,
1875 const struct GNUNET_PSYC_Environment *env,
1876 GNUNET_PSYC_TransmitNotifyData notify_data,
1877 void *notify_data_cls,
1878 enum GNUNET_SOCIAL_TalkFlags flags)
1880 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1881 GNUNET_assert (NULL != plc->tmit);
1884 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1885 NULL, notify_data, notify_data_cls, flags))
1886 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1893 * Resume talking to the host of the place.
1896 * Talk request to resume.
1899 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1901 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1906 * Cancel talking to the host of the place.
1909 * Talk request to cancel.
1912 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1914 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1919 * Disconnect from a place.
1921 * Invalidates guest handle.
1924 * The guest to disconnect.
1927 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1928 GNUNET_ContinuationCallback disconnect_cb,
1931 place_disconnect (&gst->plc, disconnect_cb, cls);
1936 * Leave a place temporarily or permanently.
1938 * Notifies the owner of the place about leaving, and destroys the place handle.
1942 * @param keep_active
1943 * Keep place active after last application disconnected.
1944 * #GNUNET_YES or #GNUNET_NO
1946 * Optional environment for the leave message if @a keep_active
1947 * is #GNUNET_NO. NULL if not needed.
1949 * Called upon disconnecting from the social service.
1952 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1953 struct GNUNET_PSYC_Environment *env,
1954 GNUNET_ContinuationCallback disconnect_cb,
1957 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1958 GNUNET_SOCIAL_TALK_NONE);
1959 place_leave (&gst->plc);
1960 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1965 * Obtain handle for a place entered as guest.
1967 * The returned handle can be used to access the place API.
1969 * @param guest Handle for the guest.
1971 * @return Handle for the place, valid as long as @a guest is valid.
1973 struct GNUNET_SOCIAL_Place *
1974 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1981 * Obtain the public key of a place.
1986 * @return Public key of the place.
1988 const struct GNUNET_CRYPTO_EddsaPublicKey *
1989 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
1991 return &plc->pub_key;
1996 * Set message processing @a flags for a @a method_prefix.
2000 * @param method_prefix
2001 * Method prefix @a flags apply to.
2003 * The flags that apply to a matching @a method_prefix.
2006 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2007 const char *method_prefix,
2008 enum GNUNET_SOCIAL_MsgProcFlags flags)
2010 GNUNET_assert (NULL != method_prefix);
2011 struct MsgProcRequest *mpreq;
2012 uint16_t method_size = strnlen (method_prefix,
2013 GNUNET_SERVER_MAX_MESSAGE_SIZE
2014 - sizeof (*mpreq)) + 1;
2015 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2016 mpreq = GNUNET_malloc (sizeof (*mpreq) + method_size);
2018 mpreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2019 mpreq->header.size = htons (sizeof (*mpreq) + method_size);
2020 mpreq->flags = htonl (flags);
2021 memcpy (&mpreq[1], method_prefix, method_size);
2023 GNUNET_CLIENT_MANAGER_transmit (plc->client, &mpreq->header);
2024 GNUNET_free (mpreq);
2029 * Clear all message processing flags previously set for this place.
2032 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2034 struct GNUNET_MessageHeader req;
2035 req.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2036 req.size = htons (sizeof (req));
2037 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req);
2041 static struct GNUNET_SOCIAL_HistoryRequest *
2042 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2043 uint64_t start_message_id,
2044 uint64_t end_message_id,
2045 uint64_t message_limit,
2046 const char *method_prefix,
2048 struct GNUNET_PSYC_Slicer *slicer,
2049 GNUNET_ResultCallback result_cb,
2052 struct GNUNET_PSYC_HistoryRequestMessage *req;
2053 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2055 hist->slicer = slicer;
2056 hist->result_cb = result_cb;
2058 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2059 &op_recv_history_result, hist);
2061 GNUNET_assert (NULL != method_prefix);
2062 uint16_t method_size = strnlen (method_prefix,
2063 GNUNET_SERVER_MAX_MESSAGE_SIZE
2064 - sizeof (*req)) + 1;
2065 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2066 req = GNUNET_malloc (sizeof (*req) + method_size);
2067 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2068 req->header.size = htons (sizeof (*req) + method_size);
2069 req->start_message_id = GNUNET_htonll (start_message_id);
2070 req->end_message_id = GNUNET_htonll (end_message_id);
2071 req->message_limit = GNUNET_htonll (message_limit);
2072 req->flags = htonl (flags);
2073 req->op_id = GNUNET_htonll (hist->op_id);
2074 memcpy (&req[1], method_prefix, method_size);
2076 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2083 * Learn about the history of a place.
2085 * Messages are returned through the @a slicer function
2086 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2089 * Place we want to learn more about.
2090 * @param start_message_id
2091 * First historic message we are interested in.
2092 * @param end_message_id
2093 * Last historic message we are interested in (inclusive).
2094 * @param method_prefix
2095 * Only retrieve messages with this method prefix.
2097 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2099 * Slicer to use for retrieved messages.
2100 * Can be the same as the slicer of the place.
2102 * Function called after all messages retrieved.
2103 * NULL if not needed.
2104 * @param cls Closure for @a result_cb.
2106 struct GNUNET_SOCIAL_HistoryRequest *
2107 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2108 uint64_t start_message_id,
2109 uint64_t end_message_id,
2110 const char *method_prefix,
2112 struct GNUNET_PSYC_Slicer *slicer,
2113 GNUNET_ResultCallback result_cb,
2116 return place_history_replay (plc, start_message_id, end_message_id, 0,
2117 method_prefix, flags, slicer, result_cb, cls);
2122 * Learn about the history of a place.
2124 * Sends messages through the slicer function of the place where
2125 * start_message_id <= message_id <= end_message_id.
2126 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2128 * To get the latest message, use 0 for both the start and end message ID.
2131 * Place we want to learn more about.
2132 * @param message_limit
2133 * Maximum number of historic messages we are interested in.
2134 * @param method_prefix
2135 * Only retrieve messages with this method prefix.
2137 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2139 * Function called after all messages retrieved.
2140 * NULL if not needed.
2141 * @param cls Closure for @a result_cb.
2143 struct GNUNET_SOCIAL_HistoryRequest *
2144 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2145 uint64_t message_limit,
2146 const char *method_prefix,
2148 struct GNUNET_PSYC_Slicer *slicer,
2149 GNUNET_ResultCallback result_cb,
2152 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2153 slicer, result_cb, cls);
2158 * Cancel learning about the history of a place.
2161 * History lesson to cancel.
2164 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2166 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2172 * Request matching state variables.
2174 static struct GNUNET_SOCIAL_LookHandle *
2175 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2176 uint16_t type, const char *name,
2177 GNUNET_PSYC_StateVarCallback var_cb,
2178 GNUNET_ResultCallback result_cb, void *cls)
2180 struct GNUNET_PSYC_StateRequestMessage *req;
2181 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2183 look->var_cb = var_cb;
2184 look->result_cb = result_cb;
2186 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2187 &op_recv_state_result, look);
2189 GNUNET_assert (NULL != name);
2190 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2191 - sizeof (*req)) + 1;
2192 req = GNUNET_malloc (sizeof (*req) + name_size);
2193 req->header.type = htons (type);
2194 req->header.size = htons (sizeof (*req) + name_size);
2195 req->op_id = GNUNET_htonll (look->op_id);
2196 memcpy (&req[1], name, name_size);
2198 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2205 * Look at a particular object in the place.
2207 * The best matching object is returned (its name might be less specific than
2208 * what was requested).
2211 * The place where to look.
2213 * Full name of the object.
2215 * Set to the size of the returned value.
2217 * @return NULL if there is no such object at this place.
2219 struct GNUNET_SOCIAL_LookHandle *
2220 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2221 const char *full_name,
2222 GNUNET_PSYC_StateVarCallback var_cb,
2223 GNUNET_ResultCallback result_cb,
2226 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2227 full_name, var_cb, result_cb, cls);
2232 * Look for objects in the place with a matching name prefix.
2235 * The place where to look.
2236 * @param name_prefix
2237 * Look at objects with names beginning with this value.
2239 * Function to call for each object found.
2241 * Closure for callback function.
2243 * @return Handle that can be used to stop looking at objects.
2245 struct GNUNET_SOCIAL_LookHandle *
2246 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2247 const char *name_prefix,
2248 GNUNET_PSYC_StateVarCallback var_cb,
2249 GNUNET_ResultCallback result_cb,
2252 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2253 name_prefix, var_cb, result_cb, cls);
2258 * Cancel a state request operation.
2261 * Handle for the operation to cancel.
2264 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2266 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2272 op_recv_zone_add_place_result (void *cls, int64_t result,
2273 const void *err_msg, uint16_t err_msg_size)
2275 LOG (GNUNET_ERROR_TYPE_DEBUG,
2276 "Received zone add place result: %" PRId64 ".\n", result);
2278 struct ZoneAddPlaceHandle *add_plc = cls;
2279 if (NULL != add_plc->result_cb)
2280 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2282 GNUNET_free (add_plc->req);
2283 GNUNET_free (add_plc);
2288 * Advertise @e place in the GNS zone of @e ego.
2291 * Application handle.
2294 * @param place_pub_key
2295 * Public key of place to add.
2297 * The name for the PLACE record to put in the zone.
2299 * Password used to encrypt the record or NULL to keep it cleartext.
2300 * @param relay_count
2301 * Number of elements in the @a relays array.
2303 * List of relays to put in the PLACE record to advertise
2304 * as entry points to the place in addition to the origin.
2305 * @param expiration_time
2306 * Expiration time of the record, use 0 to remove the record.
2308 * Function called with the result of the operation.
2310 * Closure for @a result_cb
2312 * @return #GNUNET_OK if the request was sent,
2313 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2316 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2317 const struct GNUNET_SOCIAL_Ego *ego,
2319 const char *password,
2320 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2321 const struct GNUNET_PeerIdentity *origin,
2322 uint32_t relay_count,
2323 const struct GNUNET_PeerIdentity *relays,
2324 struct GNUNET_TIME_Absolute expiration_time,
2325 GNUNET_ResultCallback result_cb,
2328 struct ZoneAddPlaceRequest *preq;
2329 size_t name_size = strlen (name) + 1;
2330 size_t password_size = strlen (password) + 1;
2331 size_t relay_size = relay_count * sizeof (*relays);
2332 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2334 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2335 return GNUNET_SYSERR;
2337 preq = GNUNET_malloc (preq_size);
2338 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2339 preq->header.size = htons (preq_size);
2340 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2341 preq->ego_pub_key = ego->pub_key;
2342 preq->place_pub_key = *place_pub_key;
2343 preq->origin = *origin;
2344 preq->relay_count = htonl (relay_count);
2346 char *p = (char *) &preq[1];
2347 memcpy (p, name, name_size);
2349 memcpy (p, password, password_size);
2351 memcpy (p, relays, relay_size);
2353 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2354 add_plc->req = preq;
2355 add_plc->result_cb = result_cb;
2356 add_plc->result_cls = result_cls;
2358 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2359 op_recv_zone_add_place_result,
2361 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2367 op_recv_zone_add_nym_result (void *cls, int64_t result,
2368 const void *err_msg, uint16_t err_msg_size)
2370 LOG (GNUNET_ERROR_TYPE_DEBUG,
2371 "Received zone add nym result: %" PRId64 ".\n", result);
2373 struct ZoneAddNymHandle *add_nym = cls;
2374 if (NULL != add_nym->result_cb)
2375 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2377 GNUNET_free (add_nym->req);
2378 GNUNET_free (add_nym);
2383 * Add nym to the GNS zone of @e ego.
2390 * The name for the PKEY record to put in the zone.
2391 * @param nym_pub_key
2392 * Public key of nym to add.
2393 * @param expiration_time
2394 * Expiration time of the record, use 0 to remove the record.
2396 * Function called with the result of the operation.
2398 * Closure for @a result_cb
2400 * @return #GNUNET_OK if the request was sent,
2401 * #GNUNET_SYSERR on error, e.g. the name is too long.
2404 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2405 const struct GNUNET_SOCIAL_Ego *ego,
2407 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2408 struct GNUNET_TIME_Absolute expiration_time,
2409 GNUNET_ResultCallback result_cb,
2412 struct ZoneAddNymRequest *nreq;
2414 size_t name_size = strlen (name) + 1;
2415 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2416 return GNUNET_SYSERR;
2418 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2419 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2420 nreq->header.size = htons (sizeof (*nreq) + name_size);
2421 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2422 nreq->ego_pub_key = ego->pub_key;
2423 nreq->nym_pub_key = *nym_pub_key;
2424 memcpy (&nreq[1], name, name_size);
2426 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2427 add_nym->req = nreq;
2428 add_nym->result_cb = result_cb;
2429 add_nym->result_cls = result_cls;
2431 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2432 op_recv_zone_add_nym_result,
2434 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2440 * Connect application to the social service.
2442 * The @host_place_cb and @guest_place_cb functions are
2443 * initially called for each entered places,
2444 * then later each time a new place is entered with the current application ID.
2450 * @param notify_host
2451 * Function to notify about a place entered as host.
2452 * @param notify_guest
2453 * Function to notify about a place entered as guest..
2455 * Closure for the callbacks.
2457 * @return Handle that can be used to stop listening.
2459 struct GNUNET_SOCIAL_App *
2460 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2462 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2463 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2464 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2465 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2468 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2469 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2473 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2475 app->ego_cb = ego_cb;
2476 app->host_cb = host_cb;
2477 app->guest_cb = guest_cb;
2478 app->connected_cb = connected_cb;
2480 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2481 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2483 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2485 app->id = GNUNET_malloc (app_id_size);
2486 memcpy (app->id, id, app_id_size);
2488 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2489 creq->header.size = htons (sizeof (*creq) + app_id_size);
2490 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2491 memcpy (&creq[1], app->id, app_id_size);
2493 app->connect_msg = &creq->header;
2494 app_send_connect_msg (app);
2501 * Disconnect application.
2504 * Application handle.
2507 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2508 GNUNET_ContinuationCallback disconnect_cb,
2509 void *disconnect_cls)
2511 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO,
2512 disconnect_cb, disconnect_cls);
2517 * Detach application from a place.
2519 * Removes the place from the entered places list for this application.
2520 * Note: this does not disconnect from the place.
2522 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2530 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2531 struct GNUNET_SOCIAL_Place *plc)
2533 struct AppDetachRequest dreq;
2534 dreq.header.size = htons (sizeof (dreq));
2535 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2536 dreq.place_pub_key = plc->pub_key;
2537 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2541 /* end of social_api.c */