2 * This file is part of GNUnet
3 * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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;
108 * Is this place in the process of disconnecting from the service?
109 * #GNUNET_YES or #GNUNET_NO
111 uint8_t is_disconnecting;
115 struct GNUNET_SOCIAL_HostConnection
117 struct GNUNET_SOCIAL_App *app;
119 struct AppPlaceMessage plc_msg;
123 struct GNUNET_SOCIAL_GuestConnection
125 struct GNUNET_SOCIAL_App *app;
127 struct AppPlaceMessage plc_msg;
132 * Handle for a place where social interactions happen.
134 struct GNUNET_SOCIAL_Place
137 * Configuration to use.
139 const struct GNUNET_CONFIGURATION_Handle *cfg;
142 * Client connection to the service.
144 struct GNUNET_CLIENT_MANAGER_Connection *client;
147 * Transmission handle.
149 struct GNUNET_PSYC_TransmitHandle *tmit;
154 struct GNUNET_PSYC_ReceiveHandle *recv;
157 * Slicer for processing incoming methods.
159 struct GNUNET_PSYC_Slicer *slicer;
162 * Message to send on reconnect.
164 struct GNUNET_MessageHeader *connect_msg;
167 * Function called after disconnected from the service.
169 GNUNET_ContinuationCallback disconnect_cb;
172 * Closure for @a disconnect_cb.
174 void *disconnect_cls;
177 * Public key of the place.
179 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
182 * Public key of the ego.
184 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
187 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
192 * Is this place in the process of disconnecting from the service?
193 * #GNUNET_YES or #GNUNET_NO
195 uint8_t is_disconnecting;
200 * Host handle for a place that we entered.
202 struct GNUNET_SOCIAL_Host
204 struct GNUNET_SOCIAL_Place plc;
209 struct GNUNET_PSYC_ReceiveHandle *recv;
212 * Slicer for processing incoming methods.
214 struct GNUNET_PSYC_Slicer *slicer;
216 GNUNET_SOCIAL_HostEnterCallback enter_cb;
218 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
220 GNUNET_SOCIAL_FarewellCallback farewell_cb;
223 * Closure for callbacks.
227 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
228 struct GNUNET_PSYC_Environment *notice_place_leave_env;
233 * Guest handle for place that we entered.
235 struct GNUNET_SOCIAL_Guest
237 struct GNUNET_SOCIAL_Place plc;
242 struct GNUNET_PSYC_ReceiveHandle *recv;
245 * Slicer for processing incoming methods.
247 struct GNUNET_PSYC_Slicer *slicer;
249 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
251 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
254 * Closure for callbacks.
261 * Hash map of all nyms.
262 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
264 struct GNUNET_CONTAINER_MultiHashMap *nyms;
268 * Handle for an announcement request.
270 struct GNUNET_SOCIAL_Announcement
279 struct GNUNET_SOCIAL_TalkRequest
288 struct GNUNET_SOCIAL_HistoryRequest
293 struct GNUNET_SOCIAL_Place *plc;
303 struct GNUNET_PSYC_ReceiveHandle *recv;
306 * Function to call when the operation finished.
308 GNUNET_ResultCallback result_cb;
311 * Closure for @a result_cb.
317 struct GNUNET_SOCIAL_LookHandle
322 struct GNUNET_SOCIAL_Place *plc;
330 * State variable result callback.
332 GNUNET_PSYC_StateVarCallback var_cb;
335 * Function to call when the operation finished.
337 GNUNET_ResultCallback result_cb;
340 * Name of current modifier being received.
345 * Size of current modifier value being received.
347 size_t mod_value_size;
350 * Remaining size of current modifier value still to be received.
352 size_t mod_value_remaining;
355 * Closure for @a result_cb.
361 struct ZoneAddPlaceHandle
363 struct ZoneAddPlaceRequest *req;
364 GNUNET_ResultCallback result_cb;
369 struct ZoneAddNymHandle
371 struct ZoneAddNymRequest *req;
372 GNUNET_ResultCallback result_cb;
379 static struct GNUNET_SOCIAL_Nym *
380 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
382 struct GNUNET_SOCIAL_Nym *nym = NULL;
383 struct GNUNET_HashCode pub_key_hash;
388 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
391 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
393 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
397 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
398 nym->pub_key = *pub_key;
399 nym->pub_key_hash = pub_key_hash;
400 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
401 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
408 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
410 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
415 /*** MESSAGE HANDLERS ***/
417 /** _notice_place_leave from guests */
420 host_recv_notice_place_leave_method (void *cls,
421 const struct GNUNET_PSYC_MessageMethod *meth,
424 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
425 const char *method_name)
427 struct GNUNET_SOCIAL_Host *hst = cls;
429 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
430 nym_pub_key, sizeof (*nym_pub_key)))
433 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (nym_pub_key);
435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
436 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
437 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
439 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
440 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
442 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
443 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
444 "_notice_place_leave: got method from nym %s (%s).\n",
445 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
450 host_recv_notice_place_leave_modifier (void *cls,
451 const struct GNUNET_MessageHeader *msg,
453 enum GNUNET_PSYC_Operator oper,
457 uint16_t full_value_size)
459 struct GNUNET_SOCIAL_Host *hst = cls;
460 if (NULL == hst->notice_place_leave_env)
463 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
464 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
466 message_id, oper, name, value_size, value);
468 /* skip _nym, it's added later in eom() */
469 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
470 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
473 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
474 GNUNET_PSYC_OP_SET, name, value, value_size);
479 host_recv_notice_place_leave_eom (void *cls,
480 const struct GNUNET_MessageHeader *msg,
484 struct GNUNET_SOCIAL_Host *hst = cls;
485 if (NULL == hst->notice_place_leave_env)
488 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
490 "_notice_place_leave: got EOM from nym %s (%s).\n",
491 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
493 if (GNUNET_YES != cancelled)
495 if (NULL != hst->farewell_cb)
496 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
497 hst->notice_place_leave_env);
498 /* announce leaving guest to place */
499 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
500 "_nym", hst->notice_place_leave_nym,
501 sizeof (*hst->notice_place_leave_nym));
502 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
503 hst->notice_place_leave_env,
504 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
505 nym_destroy (hst->notice_place_leave_nym);
507 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
508 hst->notice_place_leave_env = NULL;
516 app_send_connect_msg (struct GNUNET_SOCIAL_App *app)
518 uint16_t cmsg_size = ntohs (app->connect_msg->size);
519 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
520 memcpy (cmsg, app->connect_msg, cmsg_size);
521 GNUNET_CLIENT_MANAGER_transmit_now (app->client, cmsg);
526 app_recv_disconnect (void *cls,
527 struct GNUNET_CLIENT_MANAGER_Connection *client,
528 const struct GNUNET_MessageHeader *msg)
530 struct GNUNET_SOCIAL_App *
531 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
533 GNUNET_CLIENT_MANAGER_reconnect (client);
534 app_send_connect_msg (app);
542 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
544 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
545 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
546 memcpy (cmsg, plc->connect_msg, cmsg_size);
547 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
552 place_recv_disconnect (void *cls,
553 struct GNUNET_CLIENT_MANAGER_Connection *client,
554 const struct GNUNET_MessageHeader *msg)
556 struct GNUNET_SOCIAL_Place *
557 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
559 GNUNET_CLIENT_MANAGER_reconnect (client);
560 place_send_connect_msg (plc);
565 place_recv_result (void *cls,
566 struct GNUNET_CLIENT_MANAGER_Connection *client,
567 const struct GNUNET_MessageHeader *msg)
569 struct GNUNET_SOCIAL_Place *
570 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
572 const struct GNUNET_OperationResultMessage *
573 res = (const struct GNUNET_OperationResultMessage *) msg;
575 uint16_t size = ntohs (msg->size);
576 if (size < sizeof (*res))
577 { /* Error, message too small. */
582 uint16_t data_size = size - sizeof (*res);
583 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
584 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
585 GNUNET_ntohll (res->result_code),
591 app_recv_result (void *cls,
592 struct GNUNET_CLIENT_MANAGER_Connection *client,
593 const struct GNUNET_MessageHeader *msg)
595 struct GNUNET_SOCIAL_App *
596 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
598 const struct GNUNET_OperationResultMessage *
599 res = (const struct GNUNET_OperationResultMessage *) msg;
601 uint16_t size = ntohs (msg->size);
602 if (size < sizeof (*res))
603 { /* Error, message too small. */
608 uint16_t data_size = size - sizeof (*res);
609 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
610 GNUNET_CLIENT_MANAGER_op_result (app->client, GNUNET_ntohll (res->op_id),
611 GNUNET_ntohll (res->result_code),
617 op_recv_history_result (void *cls, int64_t result,
618 const void *err_msg, uint16_t err_msg_size)
620 LOG (GNUNET_ERROR_TYPE_DEBUG,
621 "Received history replay result: %" PRId64 ".\n", result);
623 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
625 if (NULL != hist->result_cb)
626 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
628 GNUNET_PSYC_receive_destroy (hist->recv);
634 op_recv_state_result (void *cls, int64_t result,
635 const void *err_msg, uint16_t err_msg_size)
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "Received state request result: %" PRId64 ".\n", result);
640 struct GNUNET_SOCIAL_LookHandle *look = cls;
642 if (NULL != look->result_cb)
643 look->result_cb (look->cls, result, err_msg, err_msg_size);
650 place_recv_history_result (void *cls,
651 struct GNUNET_CLIENT_MANAGER_Connection *client,
652 const struct GNUNET_MessageHeader *msg)
654 struct GNUNET_SOCIAL_Place *
655 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
657 const struct GNUNET_OperationResultMessage *
658 res = (const struct GNUNET_OperationResultMessage *) msg;
659 struct GNUNET_PSYC_MessageHeader *
660 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
662 LOG (GNUNET_ERROR_TYPE_DEBUG,
663 "%p Received historic fragment for message #%" PRIu64 ".\n",
664 plc, GNUNET_ntohll (pmsg->message_id));
666 GNUNET_ResultCallback result_cb = NULL;
667 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
669 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
670 GNUNET_ntohll (res->op_id),
671 &result_cb, (void *) &hist))
672 { /* Operation not found. */
673 LOG (GNUNET_ERROR_TYPE_WARNING,
674 "%p Replay operation not found for historic fragment of message #%"
676 plc, GNUNET_ntohll (pmsg->message_id));
680 uint16_t size = ntohs (msg->size);
681 if (size < sizeof (*res) + sizeof (*pmsg))
682 { /* Error, message too small. */
687 GNUNET_PSYC_receive_message (hist->recv,
688 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
693 place_recv_state_result (void *cls,
694 struct GNUNET_CLIENT_MANAGER_Connection *client,
695 const struct GNUNET_MessageHeader *msg)
697 struct GNUNET_SOCIAL_Place *
698 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
700 const struct GNUNET_OperationResultMessage *
701 res = (const struct GNUNET_OperationResultMessage *) msg;
703 GNUNET_ResultCallback result_cb = NULL;
704 struct GNUNET_SOCIAL_LookHandle *look = NULL;
706 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
707 GNUNET_ntohll (res->op_id),
708 &result_cb, (void *) &look))
709 { /* Operation not found. */
713 const struct GNUNET_MessageHeader *
714 mod = (struct GNUNET_MessageHeader *) &res[1];
715 uint16_t mod_size = ntohs (mod->size);
716 if (ntohs (msg->size) - sizeof (*res) != mod_size)
719 LOG (GNUNET_ERROR_TYPE_WARNING,
720 "Invalid modifier size in state result: %u - %u != %u\n",
721 ntohs (msg->size), sizeof (*res), mod_size);
724 switch (ntohs (mod->type))
726 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
728 const struct GNUNET_PSYC_MessageModifier *
729 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
731 const char *name = (const char *) &pmod[1];
732 uint16_t name_size = ntohs (pmod->name_size);
733 if ('\0' != name[name_size - 1])
736 LOG (GNUNET_ERROR_TYPE_WARNING,
737 "Invalid modifier name in state result\n");
740 look->mod_value_size = ntohs (pmod->value_size);
741 look->var_cb (look->cls, mod, name, name + name_size,
742 mod_size - sizeof (*mod) - name_size,
743 look->mod_value_size);
744 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
746 look->mod_value_remaining = look->mod_value_size;
747 look->mod_name = GNUNET_malloc (name_size);
748 memcpy (look->mod_name, name, name_size);
753 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
754 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
755 mod_size - sizeof (*mod), look->mod_value_size);
756 look->mod_value_remaining -= mod_size - sizeof (*mod);
757 if (0 == look->mod_value_remaining)
759 GNUNET_free (look->mod_name);
767 place_recv_message_ack (void *cls,
768 struct GNUNET_CLIENT_MANAGER_Connection *client,
769 const struct GNUNET_MessageHeader *msg)
771 struct GNUNET_SOCIAL_Place *
772 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
773 GNUNET_PSYC_transmit_got_ack (plc->tmit);
778 place_recv_message (void *cls,
779 struct GNUNET_CLIENT_MANAGER_Connection *client,
780 const struct GNUNET_MessageHeader *msg)
782 struct GNUNET_SOCIAL_Place *
783 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
784 GNUNET_PSYC_receive_message (plc->recv,
785 (const struct GNUNET_PSYC_MessageHeader *) msg);
790 host_recv_message (void *cls,
791 struct GNUNET_CLIENT_MANAGER_Connection *client,
792 const struct GNUNET_MessageHeader *msg)
794 struct GNUNET_SOCIAL_Host *
795 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
796 GNUNET_PSYC_receive_message (hst->recv,
797 (const struct GNUNET_PSYC_MessageHeader *) msg);
798 GNUNET_PSYC_receive_message (hst->plc.recv,
799 (const struct GNUNET_PSYC_MessageHeader *) msg);
804 host_recv_enter_ack (void *cls,
805 struct GNUNET_CLIENT_MANAGER_Connection *client,
806 const struct GNUNET_MessageHeader *msg)
808 struct GNUNET_SOCIAL_Host *
809 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
810 sizeof (struct GNUNET_SOCIAL_Place));
812 struct HostEnterAck *hack = (struct HostEnterAck *) msg;
813 hst->plc.pub_key = hack->place_pub_key;
815 int32_t result = ntohl (hack->result_code);
816 if (NULL != hst->enter_cb)
817 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
818 GNUNET_ntohll (hack->max_message_id));
823 host_recv_enter_request (void *cls,
824 struct GNUNET_CLIENT_MANAGER_Connection *client,
825 const struct GNUNET_MessageHeader *msg)
827 struct GNUNET_SOCIAL_Host *
828 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
829 sizeof (struct GNUNET_SOCIAL_Place));
830 if (NULL == hst->answer_door_cb)
833 const char *method_name = NULL;
834 struct GNUNET_PSYC_Environment *env = NULL;
835 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
836 const void *data = NULL;
837 uint16_t data_size = 0;
839 const struct GNUNET_PSYC_JoinRequestMessage *
840 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
841 const struct GNUNET_PSYC_Message *join_msg = NULL;
845 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
847 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "Received join_msg of type %u and size %u.\n",
850 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
852 env = GNUNET_PSYC_env_create ();
853 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
854 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
858 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_key);
859 LOG (GNUNET_ERROR_TYPE_WARNING,
860 "Ignoring invalid entry request from nym %s.\n",
867 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_key);
868 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
873 GNUNET_PSYC_env_destroy (env);
874 if (NULL != entry_pmsg)
875 GNUNET_free (entry_pmsg);
880 guest_recv_enter_ack (void *cls,
881 struct GNUNET_CLIENT_MANAGER_Connection *client,
882 const struct GNUNET_MessageHeader *msg)
884 struct GNUNET_SOCIAL_Guest *
885 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
886 sizeof (struct GNUNET_SOCIAL_Place));
888 struct GNUNET_PSYC_CountersResultMessage *
889 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
890 int32_t result = ntohl (cres->result_code);
891 if (NULL != gst->enter_cb)
892 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
897 guest_recv_join_decision (void *cls,
898 struct GNUNET_CLIENT_MANAGER_Connection *client,
899 const struct GNUNET_MessageHeader *msg)
901 struct GNUNET_SOCIAL_Guest *
902 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
903 sizeof (struct GNUNET_SOCIAL_Place));
904 const struct GNUNET_PSYC_JoinDecisionMessage *
905 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
907 struct GNUNET_PSYC_Message *pmsg = NULL;
908 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
909 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
911 if (NULL != gst->entry_dcsn_cb)
912 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
917 app_recv_ego (void *cls,
918 struct GNUNET_CLIENT_MANAGER_Connection *client,
919 const struct GNUNET_MessageHeader *msg)
921 struct GNUNET_SOCIAL_App *
922 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
924 struct AppEgoMessage *
925 emsg = (struct AppEgoMessage *) msg;
927 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
929 struct GNUNET_HashCode ego_pub_hash;
930 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
933 struct GNUNET_SOCIAL_Ego *
934 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
937 ego = GNUNET_malloc (sizeof (*ego));
938 ego->pub_key = emsg->ego_pub_key;
939 ego->name = GNUNET_malloc (name_size);
940 memcpy (ego->name, &emsg[1], name_size);
944 ego->name = GNUNET_realloc (ego->name, name_size);
945 memcpy (ego->name, &emsg[1], name_size);
948 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
951 if (NULL != app->ego_cb)
952 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
957 app_recv_place (void *cls,
958 struct GNUNET_CLIENT_MANAGER_Connection *client,
959 const struct GNUNET_MessageHeader *msg)
961 struct GNUNET_SOCIAL_App *
962 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
964 struct AppPlaceMessage *
965 pmsg = (struct AppPlaceMessage *) msg;
967 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
968 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
971 struct GNUNET_HashCode ego_pub_hash;
972 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
974 struct GNUNET_SOCIAL_Ego *
975 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
982 if (GNUNET_YES == pmsg->is_host)
984 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
986 hconn->plc_msg = *pmsg;
987 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
991 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
993 gconn->plc_msg = *pmsg;
994 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
999 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1001 { host_recv_enter_ack, NULL,
1002 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1003 sizeof (struct HostEnterAck), GNUNET_NO },
1005 { host_recv_enter_request, NULL,
1006 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1007 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1009 { host_recv_message, NULL,
1010 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1011 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1013 { place_recv_message_ack, NULL,
1014 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1015 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1017 { place_recv_history_result, NULL,
1018 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1019 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1021 { place_recv_state_result, NULL,
1022 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1023 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1025 { place_recv_result, NULL,
1026 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1027 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1029 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1031 { NULL, NULL, 0, 0, GNUNET_NO }
1035 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1037 { guest_recv_enter_ack, NULL,
1038 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1039 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1041 { host_recv_enter_request, NULL,
1042 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1043 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1045 { place_recv_message, NULL,
1046 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1047 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1049 { place_recv_message_ack, NULL,
1050 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1051 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1053 { guest_recv_join_decision, NULL,
1054 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1055 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1057 { place_recv_history_result, NULL,
1058 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1059 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1061 { place_recv_state_result, NULL,
1062 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1063 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1065 { place_recv_result, NULL,
1066 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1067 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1069 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1071 { NULL, NULL, 0, 0, GNUNET_NO }
1076 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1078 { app_recv_ego, NULL,
1079 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1080 sizeof (struct AppEgoMessage), GNUNET_YES },
1082 { app_recv_place, NULL,
1083 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1084 sizeof (struct AppPlaceMessage), GNUNET_NO },
1086 { app_recv_result, NULL,
1087 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1088 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1090 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1092 { NULL, NULL, 0, 0, GNUNET_NO }
1097 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1099 if (NULL != plc->tmit)
1100 GNUNET_PSYC_transmit_destroy (plc->tmit);
1101 if (NULL != plc->recv)
1102 GNUNET_PSYC_receive_destroy (plc->recv);
1103 if (NULL != plc->connect_msg)
1104 GNUNET_free (plc->connect_msg);
1105 if (NULL != plc->disconnect_cb)
1106 plc->disconnect_cb (plc->disconnect_cls);
1111 host_cleanup (void *cls)
1113 struct GNUNET_SOCIAL_Host *hst = cls;
1114 place_cleanup (&hst->plc);
1115 if (NULL != hst->recv)
1117 GNUNET_PSYC_receive_destroy (hst->recv);
1120 if (NULL != hst->slicer)
1122 GNUNET_PSYC_slicer_destroy (hst->slicer);
1130 guest_cleanup (void *cls)
1132 struct GNUNET_SOCIAL_Guest *gst = cls;
1133 place_cleanup (&gst->plc);
1141 * Enter a place as host.
1143 * A place is created upon first entering, and it is active until permanently
1144 * left using GNUNET_SOCIAL_host_leave().
1147 * Application handle.
1149 * Identity of the host.
1151 * Private-public key pair of the place.
1152 * NULL to generate a key.
1154 * Policy specifying entry and history restrictions for the place.
1156 * Slicer to handle incoming messages.
1158 * Function called when the place is entered and ready to use.
1159 * @param answer_door_cb
1160 * Function to handle new nyms that want to enter.
1161 * @param farewell_cb
1162 * Function to handle departing nyms.
1164 * Closure for the callbacks.
1166 * @return Handle for the host.
1168 struct GNUNET_SOCIAL_Host *
1169 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1170 const struct GNUNET_SOCIAL_Ego *ego,
1171 enum GNUNET_PSYC_Policy policy,
1172 struct GNUNET_PSYC_Slicer *slicer,
1173 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1174 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1175 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1178 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1179 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1181 plc->cfg = app->cfg;
1182 plc->is_host = GNUNET_YES;
1183 plc->slicer = slicer;
1185 hst->enter_cb = enter_cb;
1186 hst->answer_door_cb = answer_door_cb;
1187 hst->farewell_cb = farewell_cb;
1190 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1191 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1193 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1194 plc->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, plc->slicer);
1196 hst->slicer = GNUNET_PSYC_slicer_create ();
1197 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave",
1198 host_recv_notice_place_leave_method,
1199 host_recv_notice_place_leave_modifier,
1200 NULL, host_recv_notice_place_leave_eom, hst);
1201 hst->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, hst->slicer);
1203 uint16_t app_id_size = strlen (app->id) + 1;
1204 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1205 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1206 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1207 hreq->policy = policy;
1208 hreq->ego_pub_key = ego->pub_key;
1209 memcpy (&hreq[1], app->id, app_id_size);
1211 plc->connect_msg = &hreq->header;
1212 place_send_connect_msg (plc);
1219 * Reconnect to an already entered place as host.
1222 * Host connection handle.
1223 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1225 * Slicer to handle incoming messages.
1227 * Function called when the place is entered and ready to use.
1228 * @param answer_door_cb
1229 * Function to handle new nyms that want to enter.
1230 * @param farewell_cb
1231 * Function to handle departing nyms.
1233 * Closure for the callbacks.
1235 * @return Handle for the host.
1237 struct GNUNET_SOCIAL_Host *
1238 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1239 struct GNUNET_PSYC_Slicer *slicer,
1240 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1241 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1242 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1245 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1246 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1248 size_t app_id_size = strlen (hconn->app->id) + 1;
1249 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1251 hst->enter_cb = enter_cb;
1252 hst->answer_door_cb = answer_door_cb;
1253 hst->farewell_cb = farewell_cb;
1256 plc->cfg = hconn->app->cfg;
1257 plc->is_host = GNUNET_YES;
1258 plc->slicer = slicer;
1259 plc->pub_key = hconn->plc_msg.place_pub_key;
1260 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1262 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1263 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1265 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1266 plc->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, plc->slicer);
1268 hst->slicer = GNUNET_PSYC_slicer_create ();
1269 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave",
1270 host_recv_notice_place_leave_method,
1271 host_recv_notice_place_leave_modifier,
1272 NULL, host_recv_notice_place_leave_eom, hst);
1273 hst->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, hst->slicer);
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_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);
1339 * Throw @a nym out of the place.
1341 * The @a nym reference will remain valid until the
1342 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1343 * which should be very soon after this call.
1346 * Host of the place.
1348 * Handle for the entity to be ejected.
1350 * Environment for the message or NULL.
1353 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1354 const struct GNUNET_SOCIAL_Nym *nym,
1355 struct GNUNET_PSYC_Environment *env)
1358 env = GNUNET_PSYC_env_create ();
1359 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1360 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1361 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1362 GNUNET_SOCIAL_ANNOUNCE_NONE);
1367 * Get the public key of @a ego.
1372 * @return Public key of ego.
1374 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1375 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1377 return &ego->pub_key;
1382 * Get the hash of the public key of @a ego.
1387 * @return Hash of the public key of @a ego.
1389 const struct GNUNET_HashCode *
1390 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1392 return &ego->pub_key_hash;
1397 * Get the name of @a ego.
1402 * @return Public key of @a ego.
1405 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1412 * Get the public key of @a nym.
1414 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1419 * @return Public key of @a nym.
1421 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1422 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1424 return &nym->pub_key;
1429 * Get the hash of the public key of @a nym.
1434 * @return Hash of the public key of @a nym.
1436 const struct GNUNET_HashCode *
1437 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1439 return &nym->pub_key_hash;
1444 * Send a message to all nyms that are present in the place.
1446 * This function is restricted to the host. Nyms can only send requests
1447 * to the host who can decide to relay it to everyone in the place.
1449 * @param host Host of the place.
1450 * @param method_name Method to use for the announcement.
1451 * @param env Environment containing variables for the message and operations
1452 * on objects of the place. Can be NULL.
1453 * @param notify Function to call to get the payload of the announcement.
1454 * @param notify_cls Closure for @a notify.
1455 * @param flags Flags for this announcement.
1457 * @return NULL on error (announcement already in progress?).
1459 struct GNUNET_SOCIAL_Announcement *
1460 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1461 const char *method_name,
1462 const struct GNUNET_PSYC_Environment *env,
1463 GNUNET_PSYC_TransmitNotifyData notify_data,
1464 void *notify_data_cls,
1465 enum GNUNET_SOCIAL_AnnounceFlags flags)
1468 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1469 NULL, notify_data, notify_data_cls, flags))
1470 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1477 * Resume transmitting announcement.
1480 * The announcement to resume.
1483 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1485 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1490 * Cancel announcement.
1493 * The announcement to cancel.
1496 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1498 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1503 * Obtain handle for a hosted place.
1505 * The returned handle can be used to access the place API.
1507 * @param host Handle for the host.
1509 * @return Handle for the hosted place, valid as long as @a host is valid.
1511 struct GNUNET_SOCIAL_Place *
1512 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->is_disconnecting = GNUNET_YES;
1535 plc->disconnect_cb = disconnect_cb;
1536 plc->disconnect_cls = disconnect_cls;
1538 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1539 GNUNET_YES == plc->is_host
1540 ? host_cleanup : guest_cleanup,
1546 * Disconnect from a home.
1548 * Invalidates host handle.
1551 * The host to disconnect.
1554 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1555 GNUNET_ContinuationCallback disconnect_cb,
1558 place_disconnect (&hst->plc, disconnect_cb, cls);
1563 * Stop hosting the home.
1565 * Sends a _notice_place_closed announcement to the home.
1566 * Invalidates host handle.
1571 * Environment for the message or NULL.
1572 * _nym is set to @e nym regardless whether an @e env is provided.
1573 * @param disconnect_cb
1574 * Function called after the host left the place
1575 * and disconnected from the social service.
1577 * Closure for @a disconnect_cb.
1580 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1581 const struct GNUNET_PSYC_Environment *env,
1582 GNUNET_ContinuationCallback disconnect_cb,
1585 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closed", env, NULL, NULL,
1586 GNUNET_SOCIAL_ANNOUNCE_NONE);
1587 place_leave (&hst->plc);
1588 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1594 static struct GuestEnterRequest *
1595 guest_enter_request_create (const char *app_id,
1596 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1597 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1598 const struct GNUNET_PeerIdentity *origin,
1600 const struct GNUNET_PeerIdentity *relays,
1601 const struct GNUNET_PSYC_Message *join_msg)
1603 uint16_t app_id_size = strlen (app_id) + 1;
1604 uint16_t join_msg_size = ntohs (join_msg->header.size);
1605 uint16_t relay_size = relay_count * sizeof (*relays);
1607 struct GuestEnterRequest *
1608 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1610 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1611 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1612 greq->place_pub_key = *place_pub_key;
1613 greq->ego_pub_key = *ego_pub_key;
1614 greq->origin = *origin;
1615 greq->relay_count = htonl (relay_count);
1617 char *p = (char *) &greq[1];
1618 memcpy (p, app_id, app_id_size);
1623 memcpy (p, relays, relay_size);
1627 memcpy (p, join_msg, join_msg_size);
1633 * Request entry to a place as a guest.
1636 * Application handle.
1638 * Identity of the guest.
1639 * @param place_pub_key
1640 * Public key of the place to enter.
1642 * Flags for the entry.
1644 * Peer identity of the origin of the underlying multicast group.
1645 * @param relay_count
1646 * Number of elements in the @a relays array.
1648 * Relays for the underlying multicast group.
1649 * @param method_name
1650 * Method name for the message.
1652 * Environment containing variables for the message, or NULL.
1654 * Payload for the message to give to the enter callback.
1656 * Number of bytes in @a data.
1658 * Slicer to use for processing incoming requests from guests.
1660 * @return NULL on errors, otherwise handle for the guest.
1662 struct GNUNET_SOCIAL_Guest *
1663 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1664 const struct GNUNET_SOCIAL_Ego *ego,
1665 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1666 enum GNUNET_PSYC_SlaveJoinFlags flags,
1667 const struct GNUNET_PeerIdentity *origin,
1668 uint32_t relay_count,
1669 const struct GNUNET_PeerIdentity *relays,
1670 const struct GNUNET_PSYC_Message *entry_msg,
1671 struct GNUNET_PSYC_Slicer *slicer,
1672 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1673 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1676 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1677 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1679 plc->ego_pub_key = ego->pub_key;
1680 plc->pub_key = *place_pub_key;
1681 plc->cfg = app->cfg;
1682 plc->is_host = GNUNET_YES;
1683 plc->slicer = slicer;
1685 gst->enter_cb = local_enter_cb;
1686 gst->entry_dcsn_cb = entry_dcsn_cb;
1689 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1690 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1692 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1693 plc->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, plc->slicer);
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);
1785 plc->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, plc->slicer);
1787 plc->connect_msg = &greq->header;
1788 place_send_connect_msg (plc);
1795 * Reconnect to an already entered place as guest.
1798 * Guest connection handle.
1799 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1801 * Flags for the entry.
1803 * Slicer to use for processing incoming requests from guests.
1804 * @param local_enter_cb
1805 * Called upon connection established to the social service.
1806 * @param entry_decision_cb
1807 * Called upon receiving entry decision.
1809 * @return NULL on errors, otherwise handle for the guest.
1811 struct GNUNET_SOCIAL_Guest *
1812 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1813 enum GNUNET_PSYC_SlaveJoinFlags flags,
1814 struct GNUNET_PSYC_Slicer *slicer,
1815 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1818 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1819 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1821 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1822 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1823 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1824 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1825 greq->header.size = htons (greq_size);
1826 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1827 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1828 greq->flags = htonl (flags);
1830 memcpy (&greq[1], gconn->app->id, app_id_size);
1832 gst->enter_cb = local_enter_cb;
1835 plc->cfg = gconn->app->cfg;
1836 plc->is_host = GNUNET_NO;
1837 plc->slicer = slicer;
1838 plc->pub_key = gconn->plc_msg.place_pub_key;
1839 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1841 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1842 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1844 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1845 plc->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, plc->slicer);
1847 plc->connect_msg = &greq->header;
1848 place_send_connect_msg (plc);
1850 GNUNET_free (gconn);
1856 * Talk to the host of the place.
1859 * Place where we want to talk to the host.
1860 * @param method_name
1861 * Method to invoke on the host.
1863 * Environment containing variables for the message, or NULL.
1864 * @param notify_data
1865 * Function to use to get the payload for the method.
1866 * @param notify_data_cls
1867 * Closure for @a notify_data.
1869 * Flags for the message being sent.
1871 * @return NULL if we are already trying to talk to the host,
1872 * otherwise handle to cancel the request.
1874 struct GNUNET_SOCIAL_TalkRequest *
1875 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1876 const char *method_name,
1877 const struct GNUNET_PSYC_Environment *env,
1878 GNUNET_PSYC_TransmitNotifyData notify_data,
1879 void *notify_data_cls,
1880 enum GNUNET_SOCIAL_TalkFlags flags)
1882 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1883 GNUNET_assert (NULL != plc->tmit);
1886 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1887 NULL, notify_data, notify_data_cls, flags))
1888 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1895 * Resume talking to the host of the place.
1898 * Talk request to resume.
1901 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1903 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1908 * Cancel talking to the host of the place.
1911 * Talk request to cancel.
1914 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1916 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1921 * Disconnect from a place.
1923 * Invalidates guest handle.
1926 * The guest to disconnect.
1929 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1930 GNUNET_ContinuationCallback disconnect_cb,
1933 place_disconnect (&gst->plc, disconnect_cb, cls);
1938 * Leave a place temporarily or permanently.
1940 * Notifies the owner of the place about leaving, and destroys the place handle.
1944 * @param keep_active
1945 * Keep place active after last application disconnected.
1946 * #GNUNET_YES or #GNUNET_NO
1948 * Optional environment for the leave message if @a keep_active
1949 * is #GNUNET_NO. NULL if not needed.
1951 * Called upon disconnecting from the social service.
1954 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1955 struct GNUNET_PSYC_Environment *env,
1956 GNUNET_ContinuationCallback disconnect_cb,
1959 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1960 GNUNET_SOCIAL_TALK_NONE);
1961 place_leave (&gst->plc);
1962 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1967 * Obtain handle for a place entered as guest.
1969 * The returned handle can be used to access the place API.
1971 * @param guest Handle for the guest.
1973 * @return Handle for the place, valid as long as @a guest is valid.
1975 struct GNUNET_SOCIAL_Place *
1976 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1983 * Obtain the public key of a place.
1988 * @return Public key of the place.
1990 const struct GNUNET_CRYPTO_EddsaPublicKey *
1991 GNUNET_SOCIAL_place_get_key (struct GNUNET_SOCIAL_Place *plc)
1993 return &plc->pub_key;
1997 static struct GNUNET_SOCIAL_HistoryRequest *
1998 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
1999 uint64_t start_message_id,
2000 uint64_t end_message_id,
2001 uint64_t message_limit,
2002 const char *method_prefix,
2004 struct GNUNET_PSYC_Slicer *slicer,
2005 GNUNET_ResultCallback result_cb,
2008 struct GNUNET_PSYC_HistoryRequestMessage *req;
2009 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2011 hist->recv = GNUNET_PSYC_receive_create (NULL, GNUNET_PSYC_slicer_message, slicer);
2012 hist->result_cb = result_cb;
2014 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2015 &op_recv_history_result, hist);
2017 GNUNET_assert (NULL != method_prefix);
2018 uint16_t method_size = strnlen (method_prefix,
2019 GNUNET_SERVER_MAX_MESSAGE_SIZE
2020 - sizeof (*req)) + 1;
2021 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2022 req = GNUNET_malloc (sizeof (*req) + method_size);
2023 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2024 req->header.size = htons (sizeof (*req) + method_size);
2025 req->start_message_id = GNUNET_htonll (start_message_id);
2026 req->end_message_id = GNUNET_htonll (end_message_id);
2027 req->message_limit = GNUNET_htonll (message_limit);
2028 req->flags = htonl (flags);
2029 req->op_id = GNUNET_htonll (hist->op_id);
2030 memcpy (&req[1], method_prefix, method_size);
2032 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2038 * Learn about the history of a place.
2040 * Messages are returned through the @a slicer function
2041 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2044 * Place we want to learn more about.
2045 * @param start_message_id
2046 * First historic message we are interested in.
2047 * @param end_message_id
2048 * Last historic message we are interested in (inclusive).
2049 * @param method_prefix
2050 * Only retrieve messages with this method prefix.
2052 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2054 * Slicer to use for retrieved messages.
2055 * Can be the same as the slicer of the place.
2057 * Function called after all messages retrieved.
2058 * NULL if not needed.
2059 * @param cls Closure for @a result_cb.
2061 struct GNUNET_SOCIAL_HistoryRequest *
2062 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2063 uint64_t start_message_id,
2064 uint64_t end_message_id,
2065 const char *method_prefix,
2067 struct GNUNET_PSYC_Slicer *slicer,
2068 GNUNET_ResultCallback result_cb,
2071 return place_history_replay (plc, start_message_id, end_message_id, 0,
2072 method_prefix, flags, slicer, result_cb, cls);
2077 * Learn about the history of a place.
2079 * Sends messages through the slicer function of the place where
2080 * start_message_id <= message_id <= end_message_id.
2081 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2083 * To get the latest message, use 0 for both the start and end message ID.
2086 * Place we want to learn more about.
2087 * @param message_limit
2088 * Maximum number of historic messages we are interested in.
2089 * @param method_prefix
2090 * Only retrieve messages with this method prefix.
2092 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2094 * Function called after all messages retrieved.
2095 * NULL if not needed.
2096 * @param cls Closure for @a result_cb.
2098 struct GNUNET_SOCIAL_HistoryRequest *
2099 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2100 uint64_t message_limit,
2101 const char *method_prefix,
2103 struct GNUNET_PSYC_Slicer *slicer,
2104 GNUNET_ResultCallback result_cb,
2107 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2108 slicer, result_cb, cls);
2113 * Cancel learning about the history of a place.
2116 * History lesson to cancel.
2119 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2121 GNUNET_PSYC_receive_destroy (hist->recv);
2122 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2128 * Request matching state variables.
2130 static struct GNUNET_SOCIAL_LookHandle *
2131 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2132 uint16_t type, const char *name,
2133 GNUNET_PSYC_StateVarCallback var_cb,
2134 GNUNET_ResultCallback result_cb, void *cls)
2136 struct GNUNET_PSYC_StateRequestMessage *req;
2137 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2139 look->var_cb = var_cb;
2140 look->result_cb = result_cb;
2142 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2143 &op_recv_state_result, look);
2145 GNUNET_assert (NULL != name);
2146 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2147 - sizeof (*req)) + 1;
2148 req = GNUNET_malloc (sizeof (*req) + name_size);
2149 req->header.type = htons (type);
2150 req->header.size = htons (sizeof (*req) + name_size);
2151 req->op_id = GNUNET_htonll (look->op_id);
2152 memcpy (&req[1], name, name_size);
2154 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2160 * Look at a particular object in the place.
2162 * The best matching object is returned (its name might be less specific than
2163 * what was requested).
2166 * The place where to look.
2168 * Full name of the object.
2170 * Set to the size of the returned value.
2172 * @return NULL if there is no such object at this place.
2174 struct GNUNET_SOCIAL_LookHandle *
2175 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2176 const char *full_name,
2177 GNUNET_PSYC_StateVarCallback var_cb,
2178 GNUNET_ResultCallback result_cb,
2181 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2182 full_name, var_cb, result_cb, cls);
2187 * Look for objects in the place with a matching name prefix.
2190 * The place where to look.
2191 * @param name_prefix
2192 * Look at objects with names beginning with this value.
2194 * Function to call for each object found.
2196 * Closure for callback function.
2198 * @return Handle that can be used to stop looking at objects.
2200 struct GNUNET_SOCIAL_LookHandle *
2201 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2202 const char *name_prefix,
2203 GNUNET_PSYC_StateVarCallback var_cb,
2204 GNUNET_ResultCallback result_cb,
2207 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2208 name_prefix, var_cb, result_cb, cls);
2213 * Cancel a state request operation.
2216 * Handle for the operation to cancel.
2219 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2221 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2227 op_recv_zone_add_place_result (void *cls, int64_t result,
2228 const void *err_msg, uint16_t err_msg_size)
2230 LOG (GNUNET_ERROR_TYPE_DEBUG,
2231 "Received zone add place result: %" PRId64 ".\n", result);
2233 struct ZoneAddPlaceHandle *add_plc = cls;
2234 if (NULL != add_plc->result_cb)
2235 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2237 GNUNET_free (add_plc->req);
2238 GNUNET_free (add_plc);
2243 * Advertise @e place in the GNS zone of @e ego.
2246 * Application handle.
2249 * @param place_pub_key
2250 * Public key of place to add.
2252 * The name for the PLACE record to put in the zone.
2254 * Password used to encrypt the record or NULL to keep it cleartext.
2255 * @param relay_count
2256 * Number of elements in the @a relays array.
2258 * List of relays to put in the PLACE record to advertise
2259 * as entry points to the place in addition to the origin.
2260 * @param expiration_time
2261 * Expiration time of the record, use 0 to remove the record.
2263 * Function called with the result of the operation.
2265 * Closure for @a result_cb
2267 * @return #GNUNET_OK if the request was sent,
2268 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2271 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2272 const struct GNUNET_SOCIAL_Ego *ego,
2274 const char *password,
2275 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2276 const struct GNUNET_PeerIdentity *origin,
2277 uint32_t relay_count,
2278 const struct GNUNET_PeerIdentity *relays,
2279 struct GNUNET_TIME_Absolute expiration_time,
2280 GNUNET_ResultCallback result_cb,
2283 struct ZoneAddPlaceRequest *preq;
2284 size_t name_size = strlen (name) + 1;
2285 size_t password_size = strlen (password) + 1;
2286 size_t relay_size = relay_count * sizeof (*relays);
2287 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2289 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2290 return GNUNET_SYSERR;
2292 preq = GNUNET_malloc (preq_size);
2293 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2294 preq->header.size = htons (preq_size);
2295 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2296 preq->ego_pub_key = ego->pub_key;
2297 preq->place_pub_key = *place_pub_key;
2298 preq->origin = *origin;
2299 preq->relay_count = htonl (relay_count);
2301 char *p = (char *) &preq[1];
2302 memcpy (p, name, name_size);
2304 memcpy (p, password, password_size);
2306 memcpy (p, relays, relay_size);
2308 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2309 add_plc->req = preq;
2310 add_plc->result_cb = result_cb;
2311 add_plc->result_cls = result_cls;
2313 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2314 op_recv_zone_add_place_result,
2316 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2322 op_recv_zone_add_nym_result (void *cls, int64_t result,
2323 const void *err_msg, uint16_t err_msg_size)
2325 LOG (GNUNET_ERROR_TYPE_DEBUG,
2326 "Received zone add nym result: %" PRId64 ".\n", result);
2328 struct ZoneAddNymHandle *add_nym = cls;
2329 if (NULL != add_nym->result_cb)
2330 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2332 GNUNET_free (add_nym->req);
2333 GNUNET_free (add_nym);
2338 * Add nym to the GNS zone of @e ego.
2345 * The name for the PKEY record to put in the zone.
2346 * @param nym_pub_key
2347 * Public key of nym to add.
2348 * @param expiration_time
2349 * Expiration time of the record, use 0 to remove the record.
2351 * Function called with the result of the operation.
2353 * Closure for @a result_cb
2355 * @return #GNUNET_OK if the request was sent,
2356 * #GNUNET_SYSERR on error, e.g. the name is too long.
2359 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2360 const struct GNUNET_SOCIAL_Ego *ego,
2362 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2363 struct GNUNET_TIME_Absolute expiration_time,
2364 GNUNET_ResultCallback result_cb,
2367 struct ZoneAddNymRequest *nreq;
2369 size_t name_size = strlen (name) + 1;
2370 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2371 return GNUNET_SYSERR;
2373 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2374 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2375 nreq->header.size = htons (sizeof (*nreq) + name_size);
2376 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2377 nreq->ego_pub_key = ego->pub_key;
2378 nreq->nym_pub_key = *nym_pub_key;
2379 memcpy (&nreq[1], name, name_size);
2381 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2382 add_nym->req = nreq;
2383 add_nym->result_cb = result_cb;
2384 add_nym->result_cls = result_cls;
2386 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2387 op_recv_zone_add_nym_result,
2389 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2395 * Connect application to the social service.
2397 * The @host_place_cb and @guest_place_cb functions are
2398 * initially called for each entered places,
2399 * then later each time a new place is entered with the current application ID.
2405 * @param notify_host
2406 * Function to notify about a place entered as host.
2407 * @param notify_guest
2408 * Function to notify about a place entered as guest..
2410 * Closure for the callbacks.
2412 * @return Handle that can be used to stop listening.
2414 struct GNUNET_SOCIAL_App *
2415 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2417 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2418 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2419 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2422 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2423 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2427 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2429 app->ego_cb = ego_cb;
2430 app->host_cb = host_cb;
2431 app->guest_cb = guest_cb;
2433 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2434 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2436 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2438 app->id = GNUNET_malloc (app_id_size);
2439 memcpy (app->id, id, app_id_size);
2441 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2442 creq->header.size = htons (sizeof (*creq) + app_id_size);
2443 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2444 memcpy (&creq[1], app->id, app_id_size);
2446 app->connect_msg = &creq->header;
2447 app_send_connect_msg (app);
2454 * Disconnect application.
2457 * Application handle.
2460 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app)
2462 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO, NULL, NULL);
2467 * Detach application from a place.
2469 * Removes the place from the entered places list for this application.
2470 * Note: this does not disconnect from the place.
2472 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2480 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2481 struct GNUNET_SOCIAL_Place *plc)
2483 struct AppDetachRequest dreq;
2484 dreq.header.size = htons (sizeof (dreq));
2485 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2486 dreq.place_pub_key = plc->pub_key;
2487 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2491 /* end of social_api.c */