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;
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;
152 * Slicer for processing incoming messages.
154 struct GNUNET_PSYC_Slicer *slicer;
157 * Message to send on reconnect.
159 struct GNUNET_MessageHeader *connect_msg;
162 * Function called after disconnected from the service.
164 GNUNET_ContinuationCallback disconnect_cb;
167 * Closure for @a disconnect_cb.
169 void *disconnect_cls;
172 * Public key of the place.
174 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
177 * Public key of the ego.
179 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
182 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
187 * Is this place in the process of disconnecting from the service?
188 * #GNUNET_YES or #GNUNET_NO
190 uint8_t is_disconnecting;
195 * Host handle for a place that we entered.
197 struct GNUNET_SOCIAL_Host
199 struct GNUNET_SOCIAL_Place plc;
202 * Slicer for processing incoming messages from guests.
204 struct GNUNET_PSYC_Slicer *slicer;
206 GNUNET_SOCIAL_HostEnterCallback enter_cb;
208 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
210 GNUNET_SOCIAL_FarewellCallback farewell_cb;
213 * Closure for callbacks.
217 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
218 struct GNUNET_PSYC_Environment *notice_place_leave_env;
223 * Guest handle for place that we entered.
225 struct GNUNET_SOCIAL_Guest
227 struct GNUNET_SOCIAL_Place plc;
229 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
231 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
234 * Closure for callbacks.
241 * Hash map of all nyms.
242 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
244 struct GNUNET_CONTAINER_MultiHashMap *nyms;
248 * Handle for an announcement request.
250 struct GNUNET_SOCIAL_Announcement
259 struct GNUNET_SOCIAL_TalkRequest
268 struct GNUNET_SOCIAL_HistoryRequest
273 struct GNUNET_SOCIAL_Place *plc;
281 * Slicer for processing incoming messages.
283 struct GNUNET_PSYC_Slicer *slicer;
286 * Function to call when the operation finished.
288 GNUNET_ResultCallback result_cb;
291 * Closure for @a result_cb.
297 struct GNUNET_SOCIAL_LookHandle
302 struct GNUNET_SOCIAL_Place *plc;
310 * State variable result callback.
312 GNUNET_PSYC_StateVarCallback var_cb;
315 * Function to call when the operation finished.
317 GNUNET_ResultCallback result_cb;
320 * Name of current modifier being received.
325 * Size of current modifier value being received.
327 size_t mod_value_size;
330 * Remaining size of current modifier value still to be received.
332 size_t mod_value_remaining;
335 * Closure for @a result_cb.
341 struct ZoneAddPlaceHandle
343 struct ZoneAddPlaceRequest *req;
344 GNUNET_ResultCallback result_cb;
349 struct ZoneAddNymHandle
351 struct ZoneAddNymRequest *req;
352 GNUNET_ResultCallback result_cb;
359 static struct GNUNET_SOCIAL_Nym *
360 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
362 struct GNUNET_SOCIAL_Nym *nym = NULL;
363 struct GNUNET_HashCode pub_key_hash;
368 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
371 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
373 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
377 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
378 nym->pub_key = *pub_key;
379 nym->pub_key_hash = pub_key_hash;
380 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
388 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
390 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
395 /*** MESSAGE HANDLERS ***/
397 /** _notice_place_leave from guests */
400 host_recv_notice_place_leave_method (void *cls,
401 const struct GNUNET_PSYC_MessageMethod *meth,
404 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
405 const char *method_name)
407 struct GNUNET_SOCIAL_Host *hst = cls;
409 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
410 nym_pub_key, sizeof (*nym_pub_key)))
413 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (nym_pub_key);
415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
416 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
417 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
419 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
420 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
422 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
424 "_notice_place_leave: got method from nym %s (%s).\n",
425 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
430 host_recv_notice_place_leave_modifier (void *cls,
431 const struct GNUNET_MessageHeader *msg,
433 enum GNUNET_PSYC_Operator oper,
437 uint16_t full_value_size)
439 struct GNUNET_SOCIAL_Host *hst = cls;
440 if (NULL == hst->notice_place_leave_env)
443 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
444 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
446 message_id, oper, name, value_size, value);
448 /* skip _nym, it's added later in eom() */
449 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
450 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
453 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
454 GNUNET_PSYC_OP_SET, name, value, value_size);
459 host_recv_notice_place_leave_eom (void *cls,
460 const struct GNUNET_MessageHeader *msg,
464 struct GNUNET_SOCIAL_Host *hst = cls;
465 if (NULL == hst->notice_place_leave_env)
468 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470 "_notice_place_leave: got EOM from nym %s (%s).\n",
471 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
473 if (GNUNET_YES != cancelled)
475 if (NULL != hst->farewell_cb)
476 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
477 hst->notice_place_leave_env);
478 /* announce leaving guest to place */
479 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
480 "_nym", hst->notice_place_leave_nym,
481 sizeof (*hst->notice_place_leave_nym));
482 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
483 hst->notice_place_leave_env,
484 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
485 nym_destroy (hst->notice_place_leave_nym);
487 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
488 hst->notice_place_leave_env = NULL;
496 app_send_connect_msg (struct GNUNET_SOCIAL_App *app)
498 uint16_t cmsg_size = ntohs (app->connect_msg->size);
499 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
500 memcpy (cmsg, app->connect_msg, cmsg_size);
501 GNUNET_CLIENT_MANAGER_transmit_now (app->client, cmsg);
507 app_recv_disconnect (void *cls,
508 struct GNUNET_CLIENT_MANAGER_Connection *client,
509 const struct GNUNET_MessageHeader *msg)
511 struct GNUNET_SOCIAL_App *
512 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
514 GNUNET_CLIENT_MANAGER_reconnect (client);
515 app_send_connect_msg (app);
523 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
525 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
526 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
527 memcpy (cmsg, plc->connect_msg, cmsg_size);
528 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
534 place_recv_disconnect (void *cls,
535 struct GNUNET_CLIENT_MANAGER_Connection *client,
536 const struct GNUNET_MessageHeader *msg)
538 struct GNUNET_SOCIAL_Place *
539 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
541 GNUNET_CLIENT_MANAGER_reconnect (client);
542 place_send_connect_msg (plc);
547 place_recv_result (void *cls,
548 struct GNUNET_CLIENT_MANAGER_Connection *client,
549 const struct GNUNET_MessageHeader *msg)
551 struct GNUNET_SOCIAL_Place *
552 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
554 const struct GNUNET_OperationResultMessage *
555 res = (const struct GNUNET_OperationResultMessage *) msg;
557 uint16_t size = ntohs (msg->size);
558 if (size < sizeof (*res))
559 { /* Error, message too small. */
564 uint16_t data_size = size - sizeof (*res);
565 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
566 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
567 GNUNET_ntohll (res->result_code),
573 app_recv_result (void *cls,
574 struct GNUNET_CLIENT_MANAGER_Connection *client,
575 const struct GNUNET_MessageHeader *msg)
577 struct GNUNET_SOCIAL_App *
578 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
580 const struct GNUNET_OperationResultMessage *
581 res = (const struct GNUNET_OperationResultMessage *) msg;
583 uint16_t size = ntohs (msg->size);
584 if (size < sizeof (*res))
585 { /* Error, message too small. */
590 uint16_t data_size = size - sizeof (*res);
591 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
592 GNUNET_CLIENT_MANAGER_op_result (app->client, GNUNET_ntohll (res->op_id),
593 GNUNET_ntohll (res->result_code),
599 op_recv_history_result (void *cls, int64_t result,
600 const void *err_msg, uint16_t err_msg_size)
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Received history replay result: %" PRId64 ".\n", result);
605 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
607 if (NULL != hist->result_cb)
608 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
615 op_recv_state_result (void *cls, int64_t result,
616 const void *err_msg, uint16_t err_msg_size)
618 LOG (GNUNET_ERROR_TYPE_DEBUG,
619 "Received state request result: %" PRId64 ".\n", result);
621 struct GNUNET_SOCIAL_LookHandle *look = cls;
623 if (NULL != look->result_cb)
624 look->result_cb (look->cls, result, err_msg, err_msg_size);
631 place_recv_history_result (void *cls,
632 struct GNUNET_CLIENT_MANAGER_Connection *client,
633 const struct GNUNET_MessageHeader *msg)
635 struct GNUNET_SOCIAL_Place *
636 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
638 const struct GNUNET_OperationResultMessage *
639 res = (const struct GNUNET_OperationResultMessage *) msg;
640 struct GNUNET_PSYC_MessageHeader *
641 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
643 LOG (GNUNET_ERROR_TYPE_DEBUG,
644 "%p Received historic fragment for message #%" PRIu64 ".\n",
645 plc, GNUNET_ntohll (pmsg->message_id));
647 GNUNET_ResultCallback result_cb = NULL;
648 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
650 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
651 GNUNET_ntohll (res->op_id),
652 &result_cb, (void *) &hist))
653 { /* Operation not found. */
654 LOG (GNUNET_ERROR_TYPE_WARNING,
655 "%p Replay operation not found for historic fragment of message #%"
657 plc, GNUNET_ntohll (pmsg->message_id));
661 uint16_t size = ntohs (msg->size);
662 if (size < sizeof (*res) + sizeof (*pmsg))
663 { /* Error, message too small. */
668 GNUNET_PSYC_slicer_message (hist->slicer,
669 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
674 place_recv_state_result (void *cls,
675 struct GNUNET_CLIENT_MANAGER_Connection *client,
676 const struct GNUNET_MessageHeader *msg)
678 struct GNUNET_SOCIAL_Place *
679 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
681 const struct GNUNET_OperationResultMessage *
682 res = (const struct GNUNET_OperationResultMessage *) msg;
684 GNUNET_ResultCallback result_cb = NULL;
685 struct GNUNET_SOCIAL_LookHandle *look = NULL;
687 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
688 GNUNET_ntohll (res->op_id),
689 &result_cb, (void *) &look))
690 { /* Operation not found. */
694 const struct GNUNET_MessageHeader *
695 mod = (struct GNUNET_MessageHeader *) &res[1];
696 uint16_t mod_size = ntohs (mod->size);
697 if (ntohs (msg->size) - sizeof (*res) != mod_size)
700 LOG (GNUNET_ERROR_TYPE_WARNING,
701 "Invalid modifier size in state result: %u - %u != %u\n",
702 ntohs (msg->size), sizeof (*res), mod_size);
705 switch (ntohs (mod->type))
707 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
709 const struct GNUNET_PSYC_MessageModifier *
710 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
712 const char *name = (const char *) &pmod[1];
713 uint16_t name_size = ntohs (pmod->name_size);
714 if ('\0' != name[name_size - 1])
717 LOG (GNUNET_ERROR_TYPE_WARNING,
718 "Invalid modifier name in state result\n");
721 look->mod_value_size = ntohs (pmod->value_size);
722 look->var_cb (look->cls, mod, name, name + name_size,
723 mod_size - sizeof (*mod) - name_size,
724 look->mod_value_size);
725 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
727 look->mod_value_remaining = look->mod_value_size;
728 look->mod_name = GNUNET_malloc (name_size);
729 memcpy (look->mod_name, name, name_size);
734 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
735 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
736 mod_size - sizeof (*mod), look->mod_value_size);
737 look->mod_value_remaining -= mod_size - sizeof (*mod);
738 if (0 == look->mod_value_remaining)
740 GNUNET_free (look->mod_name);
748 place_recv_message_ack (void *cls,
749 struct GNUNET_CLIENT_MANAGER_Connection *client,
750 const struct GNUNET_MessageHeader *msg)
752 struct GNUNET_SOCIAL_Place *
753 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
754 GNUNET_PSYC_transmit_got_ack (plc->tmit);
759 place_recv_message (void *cls,
760 struct GNUNET_CLIENT_MANAGER_Connection *client,
761 const struct GNUNET_MessageHeader *msg)
763 struct GNUNET_SOCIAL_Place *
764 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
765 GNUNET_PSYC_slicer_message (plc->slicer,
766 (const struct GNUNET_PSYC_MessageHeader *) msg);
771 host_recv_message (void *cls,
772 struct GNUNET_CLIENT_MANAGER_Connection *client,
773 const struct GNUNET_MessageHeader *msg)
775 struct GNUNET_SOCIAL_Host *
776 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
777 GNUNET_PSYC_slicer_message (hst->slicer,
778 (const struct GNUNET_PSYC_MessageHeader *) msg);
779 GNUNET_PSYC_slicer_message (hst->plc.slicer,
780 (const struct GNUNET_PSYC_MessageHeader *) msg);
785 host_recv_enter_ack (void *cls,
786 struct GNUNET_CLIENT_MANAGER_Connection *client,
787 const struct GNUNET_MessageHeader *msg)
789 struct GNUNET_SOCIAL_Host *
790 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
791 sizeof (struct GNUNET_SOCIAL_Place));
793 struct HostEnterAck *hack = (struct HostEnterAck *) msg;
794 hst->plc.pub_key = hack->place_pub_key;
796 int32_t result = ntohl (hack->result_code);
797 if (NULL != hst->enter_cb)
798 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
799 GNUNET_ntohll (hack->max_message_id));
804 host_recv_enter_request (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));
811 if (NULL == hst->answer_door_cb)
814 const char *method_name = NULL;
815 struct GNUNET_PSYC_Environment *env = NULL;
816 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
817 const void *data = NULL;
818 uint16_t data_size = 0;
820 const struct GNUNET_PSYC_JoinRequestMessage *
821 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
822 const struct GNUNET_PSYC_Message *join_msg = NULL;
826 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
828 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
829 LOG (GNUNET_ERROR_TYPE_DEBUG,
830 "Received join_msg of type %u and size %u.\n",
831 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
833 env = GNUNET_PSYC_env_create ();
834 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
835 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
839 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
840 LOG (GNUNET_ERROR_TYPE_WARNING,
841 "Ignoring invalid entry request from nym %s.\n",
848 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
849 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
854 GNUNET_PSYC_env_destroy (env);
855 if (NULL != entry_pmsg)
856 GNUNET_free (entry_pmsg);
861 guest_recv_enter_ack (void *cls,
862 struct GNUNET_CLIENT_MANAGER_Connection *client,
863 const struct GNUNET_MessageHeader *msg)
865 struct GNUNET_SOCIAL_Guest *
866 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
867 sizeof (struct GNUNET_SOCIAL_Place));
869 struct GNUNET_PSYC_CountersResultMessage *
870 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
871 int32_t result = ntohl (cres->result_code);
872 if (NULL != gst->enter_cb)
873 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
878 guest_recv_join_decision (void *cls,
879 struct GNUNET_CLIENT_MANAGER_Connection *client,
880 const struct GNUNET_MessageHeader *msg)
882 struct GNUNET_SOCIAL_Guest *
883 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
884 sizeof (struct GNUNET_SOCIAL_Place));
885 const struct GNUNET_PSYC_JoinDecisionMessage *
886 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
888 struct GNUNET_PSYC_Message *pmsg = NULL;
889 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
890 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
892 if (NULL != gst->entry_dcsn_cb)
893 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
898 app_recv_ego (void *cls,
899 struct GNUNET_CLIENT_MANAGER_Connection *client,
900 const struct GNUNET_MessageHeader *msg)
902 struct GNUNET_SOCIAL_App *
903 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
905 struct AppEgoMessage *
906 emsg = (struct AppEgoMessage *) msg;
908 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
910 struct GNUNET_HashCode ego_pub_hash;
911 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
914 struct GNUNET_SOCIAL_Ego *
915 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
918 ego = GNUNET_malloc (sizeof (*ego));
919 ego->pub_key = emsg->ego_pub_key;
920 ego->name = GNUNET_malloc (name_size);
921 memcpy (ego->name, &emsg[1], name_size);
925 ego->name = GNUNET_realloc (ego->name, name_size);
926 memcpy (ego->name, &emsg[1], name_size);
929 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
930 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
932 if (NULL != app->ego_cb)
933 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
938 app_recv_place (void *cls,
939 struct GNUNET_CLIENT_MANAGER_Connection *client,
940 const struct GNUNET_MessageHeader *msg)
942 struct GNUNET_SOCIAL_App *
943 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
945 struct AppPlaceMessage *
946 pmsg = (struct AppPlaceMessage *) msg;
948 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
949 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
952 struct GNUNET_HashCode ego_pub_hash;
953 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
955 struct GNUNET_SOCIAL_Ego *
956 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
963 if (GNUNET_YES == pmsg->is_host)
965 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
967 hconn->plc_msg = *pmsg;
968 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
972 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
974 gconn->plc_msg = *pmsg;
975 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
980 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
982 { host_recv_enter_ack, NULL,
983 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
984 sizeof (struct HostEnterAck), GNUNET_NO },
986 { host_recv_enter_request, NULL,
987 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
988 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
990 { host_recv_message, NULL,
991 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
992 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
994 { place_recv_message_ack, NULL,
995 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
996 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
998 { place_recv_history_result, NULL,
999 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1000 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1002 { place_recv_state_result, NULL,
1003 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1004 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1006 { place_recv_result, NULL,
1007 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1008 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1010 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1012 { NULL, NULL, 0, 0, GNUNET_NO }
1016 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1018 { guest_recv_enter_ack, NULL,
1019 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1020 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1022 { host_recv_enter_request, NULL,
1023 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1024 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1026 { place_recv_message, NULL,
1027 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1028 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1030 { place_recv_message_ack, NULL,
1031 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1032 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1034 { guest_recv_join_decision, NULL,
1035 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1036 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1038 { place_recv_history_result, NULL,
1039 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1040 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1042 { place_recv_state_result, NULL,
1043 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1044 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1046 { place_recv_result, NULL,
1047 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1048 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1050 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1052 { NULL, NULL, 0, 0, GNUNET_NO }
1057 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1059 { app_recv_ego, NULL,
1060 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1061 sizeof (struct AppEgoMessage), GNUNET_YES },
1063 { app_recv_place, NULL,
1064 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1065 sizeof (struct AppPlaceMessage), GNUNET_NO },
1067 { app_recv_result, NULL,
1068 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1069 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1071 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1073 { NULL, NULL, 0, 0, GNUNET_NO }
1078 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1080 if (NULL != plc->tmit)
1081 GNUNET_PSYC_transmit_destroy (plc->tmit);
1082 if (NULL != plc->connect_msg)
1083 GNUNET_free (plc->connect_msg);
1084 if (NULL != plc->disconnect_cb)
1085 plc->disconnect_cb (plc->disconnect_cls);
1090 host_cleanup (void *cls)
1092 struct GNUNET_SOCIAL_Host *hst = cls;
1093 place_cleanup (&hst->plc);
1094 if (NULL != hst->slicer)
1096 GNUNET_PSYC_slicer_destroy (hst->slicer);
1104 guest_cleanup (void *cls)
1106 struct GNUNET_SOCIAL_Guest *gst = cls;
1107 place_cleanup (&gst->plc);
1115 * Enter a place as host.
1117 * A place is created upon first entering, and it is active until permanently
1118 * left using GNUNET_SOCIAL_host_leave().
1121 * Application handle.
1123 * Identity of the host.
1125 * Private-public key pair of the place.
1126 * NULL to generate a key.
1128 * Policy specifying entry and history restrictions for the place.
1130 * Slicer to handle incoming messages.
1132 * Function called when the place is entered and ready to use.
1133 * @param answer_door_cb
1134 * Function to handle new nyms that want to enter.
1135 * @param farewell_cb
1136 * Function to handle departing nyms.
1138 * Closure for the callbacks.
1140 * @return Handle for the host.
1142 struct GNUNET_SOCIAL_Host *
1143 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1144 const struct GNUNET_SOCIAL_Ego *ego,
1145 enum GNUNET_PSYC_Policy policy,
1146 struct GNUNET_PSYC_Slicer *slicer,
1147 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1148 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1149 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1152 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1153 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1155 plc->cfg = app->cfg;
1156 plc->is_host = GNUNET_YES;
1157 plc->slicer = slicer;
1159 hst->enter_cb = enter_cb;
1160 hst->answer_door_cb = answer_door_cb;
1161 hst->farewell_cb = farewell_cb;
1164 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1165 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1167 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1169 hst->slicer = GNUNET_PSYC_slicer_create ();
1170 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave",
1171 host_recv_notice_place_leave_method,
1172 host_recv_notice_place_leave_modifier,
1173 NULL, host_recv_notice_place_leave_eom, hst);
1175 uint16_t app_id_size = strlen (app->id) + 1;
1176 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1177 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1178 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1179 hreq->policy = policy;
1180 hreq->ego_pub_key = ego->pub_key;
1181 memcpy (&hreq[1], app->id, app_id_size);
1183 plc->connect_msg = &hreq->header;
1184 place_send_connect_msg (plc);
1191 * Reconnect to an already entered place as host.
1194 * Host connection handle.
1195 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1197 * Slicer to handle incoming messages.
1199 * Function called when the place is entered and ready to use.
1200 * @param answer_door_cb
1201 * Function to handle new nyms that want to enter.
1202 * @param farewell_cb
1203 * Function to handle departing nyms.
1205 * Closure for the callbacks.
1207 * @return Handle for the host.
1209 struct GNUNET_SOCIAL_Host *
1210 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1211 struct GNUNET_PSYC_Slicer *slicer,
1212 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1213 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1214 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1217 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1218 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1220 size_t app_id_size = strlen (hconn->app->id) + 1;
1221 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1223 hst->enter_cb = enter_cb;
1224 hst->answer_door_cb = answer_door_cb;
1225 hst->farewell_cb = farewell_cb;
1228 plc->cfg = hconn->app->cfg;
1229 plc->is_host = GNUNET_YES;
1230 plc->slicer = slicer;
1231 plc->pub_key = hconn->plc_msg.place_pub_key;
1232 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1234 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1235 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1237 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1239 hst->slicer = GNUNET_PSYC_slicer_create ();
1240 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave",
1241 host_recv_notice_place_leave_method,
1242 host_recv_notice_place_leave_modifier,
1243 NULL, host_recv_notice_place_leave_eom, hst);
1245 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1246 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1247 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1248 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1249 memcpy (&hreq[1], hconn->app->id, app_id_size);
1251 plc->connect_msg = &hreq->header;
1252 place_send_connect_msg (plc);
1254 GNUNET_free (hconn);
1260 * Decision whether to admit @a nym into the place or refuse entry.
1263 * Host of the place.
1265 * Handle for the entity that wanted to enter.
1266 * @param is_admitted
1267 * #GNUNET_YES if @a nym is admitted,
1268 * #GNUNET_NO if @a nym is refused entry,
1269 * #GNUNET_SYSERR if we cannot answer the request.
1270 * @param method_name
1271 * Method name for the rejection message.
1273 * Environment containing variables for the message, or NULL.
1275 * Data for the rejection message to send back.
1277 * Number of bytes in @a data for method.
1278 * @return #GNUNET_OK on success,
1279 * #GNUNET_SYSERR if the message is too large.
1282 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1283 struct GNUNET_SOCIAL_Nym *nym,
1285 const struct GNUNET_PSYC_Message *entry_resp)
1287 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1288 uint16_t entry_resp_size
1289 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1291 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1292 return GNUNET_SYSERR;
1294 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1295 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1296 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1297 dcsn->is_admitted = htonl (is_admitted);
1298 dcsn->slave_pub_key = nym->pub_key;
1300 if (0 < entry_resp_size)
1301 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1303 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1310 * Throw @a nym out of the place.
1312 * The @a nym reference will remain valid until the
1313 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1314 * which should be very soon after this call.
1317 * Host of the place.
1319 * Handle for the entity to be ejected.
1321 * Environment for the message or NULL.
1324 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1325 const struct GNUNET_SOCIAL_Nym *nym,
1326 struct GNUNET_PSYC_Environment *env)
1329 env = GNUNET_PSYC_env_create ();
1330 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1331 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1332 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1333 GNUNET_SOCIAL_ANNOUNCE_NONE);
1338 * Get the public key of @a ego.
1343 * @return Public key of ego.
1345 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1346 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1348 return &ego->pub_key;
1353 * Get the hash of the public key of @a ego.
1358 * @return Hash of the public key of @a ego.
1360 const struct GNUNET_HashCode *
1361 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1363 return &ego->pub_key_hash;
1368 * Get the name of @a ego.
1373 * @return Public key of @a ego.
1376 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1383 * Get the public key of @a nym.
1385 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1390 * @return Public key of @a nym.
1392 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1393 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1395 return &nym->pub_key;
1400 * Get the hash of the public key of @a nym.
1405 * @return Hash of the public key of @a nym.
1407 const struct GNUNET_HashCode *
1408 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1410 return &nym->pub_key_hash;
1415 * Send a message to all nyms that are present in the place.
1417 * This function is restricted to the host. Nyms can only send requests
1418 * to the host who can decide to relay it to everyone in the place.
1420 * @param host Host of the place.
1421 * @param method_name Method to use for the announcement.
1422 * @param env Environment containing variables for the message and operations
1423 * on objects of the place. Can be NULL.
1424 * @param notify Function to call to get the payload of the announcement.
1425 * @param notify_cls Closure for @a notify.
1426 * @param flags Flags for this announcement.
1428 * @return NULL on error (announcement already in progress?).
1430 struct GNUNET_SOCIAL_Announcement *
1431 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1432 const char *method_name,
1433 const struct GNUNET_PSYC_Environment *env,
1434 GNUNET_PSYC_TransmitNotifyData notify_data,
1435 void *notify_data_cls,
1436 enum GNUNET_SOCIAL_AnnounceFlags flags)
1439 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1440 NULL, notify_data, notify_data_cls, flags))
1441 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1448 * Resume transmitting announcement.
1451 * The announcement to resume.
1454 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1456 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1461 * Cancel announcement.
1464 * The announcement to cancel.
1467 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1469 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1474 * Obtain handle for a hosted place.
1476 * The returned handle can be used to access the place API.
1478 * @param host Handle for the host.
1480 * @return Handle for the hosted place, valid as long as @a host is valid.
1482 struct GNUNET_SOCIAL_Place *
1483 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1491 place_leave (struct GNUNET_SOCIAL_Place *plc)
1493 struct GNUNET_MessageHeader msg;
1494 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1495 msg.size = htons (sizeof (msg));
1496 GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
1501 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1502 GNUNET_ContinuationCallback disconnect_cb,
1503 void *disconnect_cls)
1505 plc->is_disconnecting = GNUNET_YES;
1506 plc->disconnect_cb = disconnect_cb;
1507 plc->disconnect_cls = disconnect_cls;
1509 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1510 GNUNET_YES == plc->is_host
1511 ? host_cleanup : guest_cleanup,
1517 * Disconnect from a home.
1519 * Invalidates host handle.
1522 * The host to disconnect.
1525 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1526 GNUNET_ContinuationCallback disconnect_cb,
1529 place_disconnect (&hst->plc, disconnect_cb, cls);
1534 * Stop hosting the home.
1536 * Sends a _notice_place_closed announcement to the home.
1537 * Invalidates host handle.
1542 * Environment for the message or NULL.
1543 * _nym is set to @e nym regardless whether an @e env is provided.
1544 * @param disconnect_cb
1545 * Function called after the host left the place
1546 * and disconnected from the social service.
1548 * Closure for @a disconnect_cb.
1551 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1552 const struct GNUNET_PSYC_Environment *env,
1553 GNUNET_ContinuationCallback disconnect_cb,
1556 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closed", env, NULL, NULL,
1557 GNUNET_SOCIAL_ANNOUNCE_NONE);
1558 place_leave (&hst->plc);
1559 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1565 static struct GuestEnterRequest *
1566 guest_enter_request_create (const char *app_id,
1567 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1568 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1569 const struct GNUNET_PeerIdentity *origin,
1571 const struct GNUNET_PeerIdentity *relays,
1572 const struct GNUNET_PSYC_Message *join_msg)
1574 uint16_t app_id_size = strlen (app_id) + 1;
1575 uint16_t join_msg_size = ntohs (join_msg->header.size);
1576 uint16_t relay_size = relay_count * sizeof (*relays);
1578 struct GuestEnterRequest *
1579 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1581 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1582 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1583 greq->place_pub_key = *place_pub_key;
1584 greq->ego_pub_key = *ego_pub_key;
1585 greq->origin = *origin;
1586 greq->relay_count = htonl (relay_count);
1588 char *p = (char *) &greq[1];
1589 memcpy (p, app_id, app_id_size);
1594 memcpy (p, relays, relay_size);
1598 memcpy (p, join_msg, join_msg_size);
1604 * Request entry to a place as a guest.
1607 * Application handle.
1609 * Identity of the guest.
1610 * @param place_pub_key
1611 * Public key of the place to enter.
1613 * Flags for the entry.
1615 * Peer identity of the origin of the underlying multicast group.
1616 * @param relay_count
1617 * Number of elements in the @a relays array.
1619 * Relays for the underlying multicast group.
1620 * @param method_name
1621 * Method name for the message.
1623 * Environment containing variables for the message, or NULL.
1625 * Payload for the message to give to the enter callback.
1627 * Number of bytes in @a data.
1629 * Slicer to use for processing incoming requests from guests.
1631 * @return NULL on errors, otherwise handle for the guest.
1633 struct GNUNET_SOCIAL_Guest *
1634 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1635 const struct GNUNET_SOCIAL_Ego *ego,
1636 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1637 enum GNUNET_PSYC_SlaveJoinFlags flags,
1638 const struct GNUNET_PeerIdentity *origin,
1639 uint32_t relay_count,
1640 const struct GNUNET_PeerIdentity *relays,
1641 const struct GNUNET_PSYC_Message *entry_msg,
1642 struct GNUNET_PSYC_Slicer *slicer,
1643 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1644 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1647 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1648 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1650 plc->ego_pub_key = ego->pub_key;
1651 plc->pub_key = *place_pub_key;
1652 plc->cfg = app->cfg;
1653 plc->is_host = GNUNET_YES;
1654 plc->slicer = slicer;
1656 gst->enter_cb = local_enter_cb;
1657 gst->entry_dcsn_cb = entry_dcsn_cb;
1660 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1661 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1663 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1665 struct GuestEnterRequest *
1666 greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1667 origin, relay_count, relays, entry_msg);
1668 plc->connect_msg = &greq->header;
1669 place_send_connect_msg (plc);
1675 * Request entry to a place by name as a guest.
1678 * Application handle.
1680 * Identity of the guest.
1682 * GNS name of the place to enter. Either in the form of
1683 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1684 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1685 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1686 * pseudonym's place directly.
1688 * Password to decrypt the record, or NULL for cleartext records.
1690 * Entry request message or NULL.
1692 * Slicer to use for processing incoming requests from guests.
1693 * @param local_enter_cb
1694 * Called upon connection established to the social service.
1695 * @param entry_decision_cb
1696 * Called upon receiving entry decision.
1698 * @return NULL on errors, otherwise handle for the guest.
1700 struct GNUNET_SOCIAL_Guest *
1701 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1702 const struct GNUNET_SOCIAL_Ego *ego,
1703 const char *gns_name,
1704 const char *password,
1705 const struct GNUNET_PSYC_Message *join_msg,
1706 struct GNUNET_PSYC_Slicer *slicer,
1707 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1708 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1711 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1712 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1714 if (NULL == password)
1717 uint16_t app_id_size = strlen (app->id) + 1;
1718 uint16_t gns_name_size = strlen (gns_name) + 1;
1719 uint16_t password_size = strlen (password) + 1;
1721 uint16_t join_msg_size = 0;
1722 if (NULL != join_msg);
1723 join_msg_size = ntohs (join_msg->header.size);
1725 uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
1726 + app_id_size + gns_name_size + password_size + join_msg_size;
1727 struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
1728 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1729 greq->header.size = htons (greq_size);
1730 greq->ego_pub_key = ego->pub_key;
1732 char *p = (char *) &greq[1];
1733 memcpy (p, app->id, app_id_size);
1735 memcpy (p, gns_name, gns_name_size);
1737 memcpy (p, password, password_size);
1739 if (NULL != join_msg)
1740 memcpy (p, join_msg, join_msg_size);
1742 gst->enter_cb = local_enter_cb;
1743 gst->entry_dcsn_cb = entry_decision_cb;
1746 plc->ego_pub_key = ego->pub_key;
1747 plc->cfg = app->cfg;
1748 plc->is_host = GNUNET_NO;
1749 plc->slicer = slicer;
1751 plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
1752 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1754 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1756 plc->connect_msg = &greq->header;
1757 place_send_connect_msg (plc);
1764 * Reconnect to an already entered place as guest.
1767 * Guest connection handle.
1768 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1770 * Flags for the entry.
1772 * Slicer to use for processing incoming requests from guests.
1773 * @param local_enter_cb
1774 * Called upon connection established to the social service.
1775 * @param entry_decision_cb
1776 * Called upon receiving entry decision.
1778 * @return NULL on errors, otherwise handle for the guest.
1780 struct GNUNET_SOCIAL_Guest *
1781 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1782 enum GNUNET_PSYC_SlaveJoinFlags flags,
1783 struct GNUNET_PSYC_Slicer *slicer,
1784 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1787 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1788 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1790 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1791 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1792 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1793 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1794 greq->header.size = htons (greq_size);
1795 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1796 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1797 greq->flags = htonl (flags);
1799 memcpy (&greq[1], gconn->app->id, app_id_size);
1801 gst->enter_cb = local_enter_cb;
1804 plc->cfg = gconn->app->cfg;
1805 plc->is_host = GNUNET_NO;
1806 plc->slicer = slicer;
1807 plc->pub_key = gconn->plc_msg.place_pub_key;
1808 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1810 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1811 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1813 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1815 plc->connect_msg = &greq->header;
1816 place_send_connect_msg (plc);
1818 GNUNET_free (gconn);
1824 * Talk to the host of the place.
1827 * Place where we want to talk to the host.
1828 * @param method_name
1829 * Method to invoke on the host.
1831 * Environment containing variables for the message, or NULL.
1832 * @param notify_data
1833 * Function to use to get the payload for the method.
1834 * @param notify_data_cls
1835 * Closure for @a notify_data.
1837 * Flags for the message being sent.
1839 * @return NULL if we are already trying to talk to the host,
1840 * otherwise handle to cancel the request.
1842 struct GNUNET_SOCIAL_TalkRequest *
1843 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1844 const char *method_name,
1845 const struct GNUNET_PSYC_Environment *env,
1846 GNUNET_PSYC_TransmitNotifyData notify_data,
1847 void *notify_data_cls,
1848 enum GNUNET_SOCIAL_TalkFlags flags)
1850 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1851 GNUNET_assert (NULL != plc->tmit);
1854 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1855 NULL, notify_data, notify_data_cls, flags))
1856 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1863 * Resume talking to the host of the place.
1866 * Talk request to resume.
1869 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1871 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1876 * Cancel talking to the host of the place.
1879 * Talk request to cancel.
1882 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1884 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1889 * Disconnect from a place.
1891 * Invalidates guest handle.
1894 * The guest to disconnect.
1897 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1898 GNUNET_ContinuationCallback disconnect_cb,
1901 place_disconnect (&gst->plc, disconnect_cb, cls);
1906 * Leave a place temporarily or permanently.
1908 * Notifies the owner of the place about leaving, and destroys the place handle.
1912 * @param keep_active
1913 * Keep place active after last application disconnected.
1914 * #GNUNET_YES or #GNUNET_NO
1916 * Optional environment for the leave message if @a keep_active
1917 * is #GNUNET_NO. NULL if not needed.
1919 * Called upon disconnecting from the social service.
1922 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1923 struct GNUNET_PSYC_Environment *env,
1924 GNUNET_ContinuationCallback disconnect_cb,
1927 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1928 GNUNET_SOCIAL_TALK_NONE);
1929 place_leave (&gst->plc);
1930 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1935 * Obtain handle for a place entered as guest.
1937 * The returned handle can be used to access the place API.
1939 * @param guest Handle for the guest.
1941 * @return Handle for the place, valid as long as @a guest is valid.
1943 struct GNUNET_SOCIAL_Place *
1944 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1951 * Obtain the public key of a place.
1956 * @return Public key of the place.
1958 const struct GNUNET_CRYPTO_EddsaPublicKey *
1959 GNUNET_SOCIAL_place_get_key (struct GNUNET_SOCIAL_Place *plc)
1961 return &plc->pub_key;
1966 * Set message processing @a flags for a @a method_prefix.
1970 * @param method_prefix
1971 * Method prefix @a flags apply to.
1973 * The flags that apply to a matching @a method_prefix.
1976 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
1977 const char *method_prefix,
1978 enum GNUNET_SOCIAL_MsgProcFlags flags)
1980 GNUNET_assert (NULL != method_prefix);
1981 struct MsgProcRequest *mpreq;
1982 uint16_t method_size = strnlen (method_prefix,
1983 GNUNET_SERVER_MAX_MESSAGE_SIZE
1984 - sizeof (*mpreq)) + 1;
1985 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
1986 mpreq = GNUNET_malloc (sizeof (*mpreq) + method_size);
1988 mpreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
1989 mpreq->header.size = htons (sizeof (*mpreq) + method_size);
1990 mpreq->flags = htonl (flags);
1991 memcpy (&mpreq[1], method_prefix, method_size);
1993 GNUNET_CLIENT_MANAGER_transmit (plc->client, &mpreq->header);
1994 GNUNET_free (mpreq);
1999 * Clear all message processing flags previously set for this place.
2002 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2004 struct GNUNET_MessageHeader req;
2005 req.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2006 req.size = htons (sizeof (req));
2007 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req);
2011 static struct GNUNET_SOCIAL_HistoryRequest *
2012 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2013 uint64_t start_message_id,
2014 uint64_t end_message_id,
2015 uint64_t message_limit,
2016 const char *method_prefix,
2018 struct GNUNET_PSYC_Slicer *slicer,
2019 GNUNET_ResultCallback result_cb,
2022 struct GNUNET_PSYC_HistoryRequestMessage *req;
2023 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2025 hist->slicer = slicer;
2026 hist->result_cb = result_cb;
2028 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2029 &op_recv_history_result, hist);
2031 GNUNET_assert (NULL != method_prefix);
2032 uint16_t method_size = strnlen (method_prefix,
2033 GNUNET_SERVER_MAX_MESSAGE_SIZE
2034 - sizeof (*req)) + 1;
2035 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2036 req = GNUNET_malloc (sizeof (*req) + method_size);
2037 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2038 req->header.size = htons (sizeof (*req) + method_size);
2039 req->start_message_id = GNUNET_htonll (start_message_id);
2040 req->end_message_id = GNUNET_htonll (end_message_id);
2041 req->message_limit = GNUNET_htonll (message_limit);
2042 req->flags = htonl (flags);
2043 req->op_id = GNUNET_htonll (hist->op_id);
2044 memcpy (&req[1], method_prefix, method_size);
2046 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2053 * Learn about the history of a place.
2055 * Messages are returned through the @a slicer function
2056 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2059 * Place we want to learn more about.
2060 * @param start_message_id
2061 * First historic message we are interested in.
2062 * @param end_message_id
2063 * Last historic message we are interested in (inclusive).
2064 * @param method_prefix
2065 * Only retrieve messages with this method prefix.
2067 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2069 * Slicer to use for retrieved messages.
2070 * Can be the same as the slicer of the place.
2072 * Function called after all messages retrieved.
2073 * NULL if not needed.
2074 * @param cls Closure for @a result_cb.
2076 struct GNUNET_SOCIAL_HistoryRequest *
2077 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2078 uint64_t start_message_id,
2079 uint64_t end_message_id,
2080 const char *method_prefix,
2082 struct GNUNET_PSYC_Slicer *slicer,
2083 GNUNET_ResultCallback result_cb,
2086 return place_history_replay (plc, start_message_id, end_message_id, 0,
2087 method_prefix, flags, slicer, result_cb, cls);
2092 * Learn about the history of a place.
2094 * Sends messages through the slicer function of the place where
2095 * start_message_id <= message_id <= end_message_id.
2096 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2098 * To get the latest message, use 0 for both the start and end message ID.
2101 * Place we want to learn more about.
2102 * @param message_limit
2103 * Maximum number of historic messages we are interested in.
2104 * @param method_prefix
2105 * Only retrieve messages with this method prefix.
2107 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2109 * Function called after all messages retrieved.
2110 * NULL if not needed.
2111 * @param cls Closure for @a result_cb.
2113 struct GNUNET_SOCIAL_HistoryRequest *
2114 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2115 uint64_t message_limit,
2116 const char *method_prefix,
2118 struct GNUNET_PSYC_Slicer *slicer,
2119 GNUNET_ResultCallback result_cb,
2122 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2123 slicer, result_cb, cls);
2128 * Cancel learning about the history of a place.
2131 * History lesson to cancel.
2134 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2136 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2142 * Request matching state variables.
2144 static struct GNUNET_SOCIAL_LookHandle *
2145 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2146 uint16_t type, const char *name,
2147 GNUNET_PSYC_StateVarCallback var_cb,
2148 GNUNET_ResultCallback result_cb, void *cls)
2150 struct GNUNET_PSYC_StateRequestMessage *req;
2151 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2153 look->var_cb = var_cb;
2154 look->result_cb = result_cb;
2156 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2157 &op_recv_state_result, look);
2159 GNUNET_assert (NULL != name);
2160 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2161 - sizeof (*req)) + 1;
2162 req = GNUNET_malloc (sizeof (*req) + name_size);
2163 req->header.type = htons (type);
2164 req->header.size = htons (sizeof (*req) + name_size);
2165 req->op_id = GNUNET_htonll (look->op_id);
2166 memcpy (&req[1], name, name_size);
2168 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2175 * Look at a particular object in the place.
2177 * The best matching object is returned (its name might be less specific than
2178 * what was requested).
2181 * The place where to look.
2183 * Full name of the object.
2185 * Set to the size of the returned value.
2187 * @return NULL if there is no such object at this place.
2189 struct GNUNET_SOCIAL_LookHandle *
2190 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2191 const char *full_name,
2192 GNUNET_PSYC_StateVarCallback var_cb,
2193 GNUNET_ResultCallback result_cb,
2196 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2197 full_name, var_cb, result_cb, cls);
2202 * Look for objects in the place with a matching name prefix.
2205 * The place where to look.
2206 * @param name_prefix
2207 * Look at objects with names beginning with this value.
2209 * Function to call for each object found.
2211 * Closure for callback function.
2213 * @return Handle that can be used to stop looking at objects.
2215 struct GNUNET_SOCIAL_LookHandle *
2216 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2217 const char *name_prefix,
2218 GNUNET_PSYC_StateVarCallback var_cb,
2219 GNUNET_ResultCallback result_cb,
2222 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2223 name_prefix, var_cb, result_cb, cls);
2228 * Cancel a state request operation.
2231 * Handle for the operation to cancel.
2234 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2236 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2242 op_recv_zone_add_place_result (void *cls, int64_t result,
2243 const void *err_msg, uint16_t err_msg_size)
2245 LOG (GNUNET_ERROR_TYPE_DEBUG,
2246 "Received zone add place result: %" PRId64 ".\n", result);
2248 struct ZoneAddPlaceHandle *add_plc = cls;
2249 if (NULL != add_plc->result_cb)
2250 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2252 GNUNET_free (add_plc->req);
2253 GNUNET_free (add_plc);
2258 * Advertise @e place in the GNS zone of @e ego.
2261 * Application handle.
2264 * @param place_pub_key
2265 * Public key of place to add.
2267 * The name for the PLACE record to put in the zone.
2269 * Password used to encrypt the record or NULL to keep it cleartext.
2270 * @param relay_count
2271 * Number of elements in the @a relays array.
2273 * List of relays to put in the PLACE record to advertise
2274 * as entry points to the place in addition to the origin.
2275 * @param expiration_time
2276 * Expiration time of the record, use 0 to remove the record.
2278 * Function called with the result of the operation.
2280 * Closure for @a result_cb
2282 * @return #GNUNET_OK if the request was sent,
2283 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2286 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2287 const struct GNUNET_SOCIAL_Ego *ego,
2289 const char *password,
2290 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2291 const struct GNUNET_PeerIdentity *origin,
2292 uint32_t relay_count,
2293 const struct GNUNET_PeerIdentity *relays,
2294 struct GNUNET_TIME_Absolute expiration_time,
2295 GNUNET_ResultCallback result_cb,
2298 struct ZoneAddPlaceRequest *preq;
2299 size_t name_size = strlen (name) + 1;
2300 size_t password_size = strlen (password) + 1;
2301 size_t relay_size = relay_count * sizeof (*relays);
2302 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2304 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2305 return GNUNET_SYSERR;
2307 preq = GNUNET_malloc (preq_size);
2308 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2309 preq->header.size = htons (preq_size);
2310 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2311 preq->ego_pub_key = ego->pub_key;
2312 preq->place_pub_key = *place_pub_key;
2313 preq->origin = *origin;
2314 preq->relay_count = htonl (relay_count);
2316 char *p = (char *) &preq[1];
2317 memcpy (p, name, name_size);
2319 memcpy (p, password, password_size);
2321 memcpy (p, relays, relay_size);
2323 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2324 add_plc->req = preq;
2325 add_plc->result_cb = result_cb;
2326 add_plc->result_cls = result_cls;
2328 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2329 op_recv_zone_add_place_result,
2331 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2338 op_recv_zone_add_nym_result (void *cls, int64_t result,
2339 const void *err_msg, uint16_t err_msg_size)
2341 LOG (GNUNET_ERROR_TYPE_DEBUG,
2342 "Received zone add nym result: %" PRId64 ".\n", result);
2344 struct ZoneAddNymHandle *add_nym = cls;
2345 if (NULL != add_nym->result_cb)
2346 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2348 GNUNET_free (add_nym->req);
2349 GNUNET_free (add_nym);
2354 * Add nym to the GNS zone of @e ego.
2361 * The name for the PKEY record to put in the zone.
2362 * @param nym_pub_key
2363 * Public key of nym to add.
2364 * @param expiration_time
2365 * Expiration time of the record, use 0 to remove the record.
2367 * Function called with the result of the operation.
2369 * Closure for @a result_cb
2371 * @return #GNUNET_OK if the request was sent,
2372 * #GNUNET_SYSERR on error, e.g. the name is too long.
2375 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2376 const struct GNUNET_SOCIAL_Ego *ego,
2378 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2379 struct GNUNET_TIME_Absolute expiration_time,
2380 GNUNET_ResultCallback result_cb,
2383 struct ZoneAddNymRequest *nreq;
2385 size_t name_size = strlen (name) + 1;
2386 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2387 return GNUNET_SYSERR;
2389 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2390 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2391 nreq->header.size = htons (sizeof (*nreq) + name_size);
2392 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2393 nreq->ego_pub_key = ego->pub_key;
2394 nreq->nym_pub_key = *nym_pub_key;
2395 memcpy (&nreq[1], name, name_size);
2397 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2398 add_nym->req = nreq;
2399 add_nym->result_cb = result_cb;
2400 add_nym->result_cls = result_cls;
2402 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2403 op_recv_zone_add_nym_result,
2405 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2411 * Connect application to the social service.
2413 * The @host_place_cb and @guest_place_cb functions are
2414 * initially called for each entered places,
2415 * then later each time a new place is entered with the current application ID.
2421 * @param notify_host
2422 * Function to notify about a place entered as host.
2423 * @param notify_guest
2424 * Function to notify about a place entered as guest..
2426 * Closure for the callbacks.
2428 * @return Handle that can be used to stop listening.
2430 struct GNUNET_SOCIAL_App *
2431 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2433 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2434 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2435 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2438 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2439 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2443 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2445 app->ego_cb = ego_cb;
2446 app->host_cb = host_cb;
2447 app->guest_cb = guest_cb;
2449 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2450 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2452 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2454 app->id = GNUNET_malloc (app_id_size);
2455 memcpy (app->id, id, app_id_size);
2457 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2458 creq->header.size = htons (sizeof (*creq) + app_id_size);
2459 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2460 memcpy (&creq[1], app->id, app_id_size);
2462 app->connect_msg = &creq->header;
2463 app_send_connect_msg (app);
2470 * Disconnect application.
2473 * Application handle.
2476 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app)
2478 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO, NULL, NULL);
2483 * Detach application from a place.
2485 * Removes the place from the entered places list for this application.
2486 * Note: this does not disconnect from the place.
2488 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2496 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2497 struct GNUNET_SOCIAL_Place *plc)
2499 struct AppDetachRequest dreq;
2500 dreq.header.size = htons (sizeof (dreq));
2501 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2502 dreq.place_pub_key = plc->pub_key;
2503 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2507 /* end of social_api.c */