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 uint64_t fragment_offset,
406 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
407 const char *method_name)
409 struct GNUNET_SOCIAL_Host *hst = cls;
411 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
412 nym_pub_key, sizeof (*nym_pub_key)))
415 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (nym_pub_key);
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
419 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
421 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
422 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
424 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426 "_notice_place_leave: got method from nym %s (%s).\n",
427 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
432 host_recv_notice_place_leave_modifier (void *cls,
433 const struct GNUNET_MessageHeader *msg,
436 uint64_t fragment_offset,
437 enum GNUNET_PSYC_Operator oper,
441 uint16_t full_value_size)
443 struct GNUNET_SOCIAL_Host *hst = cls;
444 if (NULL == hst->notice_place_leave_env)
447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
448 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
450 message_id, oper, name, value_size, value);
452 /* skip _nym, it's added later in eom() */
453 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
454 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
457 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
458 GNUNET_PSYC_OP_SET, name, value, value_size);
463 host_recv_notice_place_leave_eom (void *cls,
464 const struct GNUNET_MessageHeader *msg,
467 uint64_t fragment_offset,
470 struct GNUNET_SOCIAL_Host *hst = cls;
471 if (NULL == hst->notice_place_leave_env)
474 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
476 "_notice_place_leave: got EOM from nym %s (%s).\n",
477 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
479 if (GNUNET_YES != cancelled)
481 if (NULL != hst->farewell_cb)
482 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
483 hst->notice_place_leave_env);
484 /* announce leaving guest to place */
485 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
486 "_nym", hst->notice_place_leave_nym,
487 sizeof (*hst->notice_place_leave_nym));
488 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
489 hst->notice_place_leave_env,
490 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
491 nym_destroy (hst->notice_place_leave_nym);
493 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
494 hst->notice_place_leave_env = NULL;
502 app_send_connect_msg (struct GNUNET_SOCIAL_App *app)
504 uint16_t cmsg_size = ntohs (app->connect_msg->size);
505 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
506 memcpy (cmsg, app->connect_msg, cmsg_size);
507 GNUNET_CLIENT_MANAGER_transmit_now (app->client, cmsg);
513 app_recv_disconnect (void *cls,
514 struct GNUNET_CLIENT_MANAGER_Connection *client,
515 const struct GNUNET_MessageHeader *msg)
517 struct GNUNET_SOCIAL_App *
518 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
520 GNUNET_CLIENT_MANAGER_reconnect (client);
521 app_send_connect_msg (app);
529 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
531 uint16_t cmsg_size = ntohs (plc->connect_msg->size);
532 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
533 memcpy (cmsg, plc->connect_msg, cmsg_size);
534 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
540 place_recv_disconnect (void *cls,
541 struct GNUNET_CLIENT_MANAGER_Connection *client,
542 const struct GNUNET_MessageHeader *msg)
544 struct GNUNET_SOCIAL_Place *
545 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
547 GNUNET_CLIENT_MANAGER_reconnect (client);
548 place_send_connect_msg (plc);
553 place_recv_result (void *cls,
554 struct GNUNET_CLIENT_MANAGER_Connection *client,
555 const struct GNUNET_MessageHeader *msg)
557 struct GNUNET_SOCIAL_Place *
558 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
560 const struct GNUNET_OperationResultMessage *
561 res = (const struct GNUNET_OperationResultMessage *) msg;
563 uint16_t size = ntohs (msg->size);
564 if (size < sizeof (*res))
565 { /* Error, message too small. */
570 uint16_t data_size = size - sizeof (*res);
571 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
572 GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
573 GNUNET_ntohll (res->result_code),
579 app_recv_result (void *cls,
580 struct GNUNET_CLIENT_MANAGER_Connection *client,
581 const struct GNUNET_MessageHeader *msg)
583 struct GNUNET_SOCIAL_App *
584 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
586 const struct GNUNET_OperationResultMessage *
587 res = (const struct GNUNET_OperationResultMessage *) msg;
589 uint16_t size = ntohs (msg->size);
590 if (size < sizeof (*res))
591 { /* Error, message too small. */
596 uint16_t data_size = size - sizeof (*res);
597 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
598 GNUNET_CLIENT_MANAGER_op_result (app->client, GNUNET_ntohll (res->op_id),
599 GNUNET_ntohll (res->result_code),
605 op_recv_history_result (void *cls, int64_t result,
606 const void *err_msg, uint16_t err_msg_size)
608 LOG (GNUNET_ERROR_TYPE_DEBUG,
609 "Received history replay result: %" PRId64 ".\n", result);
611 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
613 if (NULL != hist->result_cb)
614 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
621 op_recv_state_result (void *cls, int64_t result,
622 const void *err_msg, uint16_t err_msg_size)
624 LOG (GNUNET_ERROR_TYPE_DEBUG,
625 "Received state request result: %" PRId64 ".\n", result);
627 struct GNUNET_SOCIAL_LookHandle *look = cls;
629 if (NULL != look->result_cb)
630 look->result_cb (look->cls, result, err_msg, err_msg_size);
637 place_recv_history_result (void *cls,
638 struct GNUNET_CLIENT_MANAGER_Connection *client,
639 const struct GNUNET_MessageHeader *msg)
641 struct GNUNET_SOCIAL_Place *
642 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
644 const struct GNUNET_OperationResultMessage *
645 res = (const struct GNUNET_OperationResultMessage *) msg;
646 struct GNUNET_PSYC_MessageHeader *
647 pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
649 LOG (GNUNET_ERROR_TYPE_DEBUG,
650 "%p Received historic fragment for message #%" PRIu64 ".\n",
651 plc, GNUNET_ntohll (pmsg->message_id));
653 GNUNET_ResultCallback result_cb = NULL;
654 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
656 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
657 GNUNET_ntohll (res->op_id),
658 &result_cb, (void *) &hist))
659 { /* Operation not found. */
660 LOG (GNUNET_ERROR_TYPE_WARNING,
661 "%p Replay operation not found for historic fragment of message #%"
663 plc, GNUNET_ntohll (pmsg->message_id));
667 uint16_t size = ntohs (msg->size);
668 if (size < sizeof (*res) + sizeof (*pmsg))
669 { /* Error, message too small. */
674 GNUNET_PSYC_slicer_message (hist->slicer,
675 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
680 place_recv_state_result (void *cls,
681 struct GNUNET_CLIENT_MANAGER_Connection *client,
682 const struct GNUNET_MessageHeader *msg)
684 struct GNUNET_SOCIAL_Place *
685 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
687 const struct GNUNET_OperationResultMessage *
688 res = (const struct GNUNET_OperationResultMessage *) msg;
690 GNUNET_ResultCallback result_cb = NULL;
691 struct GNUNET_SOCIAL_LookHandle *look = NULL;
693 if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
694 GNUNET_ntohll (res->op_id),
695 &result_cb, (void *) &look))
696 { /* Operation not found. */
700 const struct GNUNET_MessageHeader *
701 mod = (struct GNUNET_MessageHeader *) &res[1];
702 uint16_t mod_size = ntohs (mod->size);
703 if (ntohs (msg->size) - sizeof (*res) != mod_size)
706 LOG (GNUNET_ERROR_TYPE_WARNING,
707 "Invalid modifier size in state result: %u - %u != %u\n",
708 ntohs (msg->size), sizeof (*res), mod_size);
711 switch (ntohs (mod->type))
713 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
715 const struct GNUNET_PSYC_MessageModifier *
716 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
718 const char *name = (const char *) &pmod[1];
719 uint16_t name_size = ntohs (pmod->name_size);
720 if ('\0' != name[name_size - 1])
723 LOG (GNUNET_ERROR_TYPE_WARNING,
724 "Invalid modifier name in state result\n");
727 look->mod_value_size = ntohs (pmod->value_size);
728 look->var_cb (look->cls, mod, name, name + name_size,
729 mod_size - sizeof (*mod) - name_size,
730 look->mod_value_size);
731 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
733 look->mod_value_remaining = look->mod_value_size;
734 look->mod_name = GNUNET_malloc (name_size);
735 memcpy (look->mod_name, name, name_size);
740 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
741 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
742 mod_size - sizeof (*mod), look->mod_value_size);
743 look->mod_value_remaining -= mod_size - sizeof (*mod);
744 if (0 == look->mod_value_remaining)
746 GNUNET_free (look->mod_name);
754 place_recv_message_ack (void *cls,
755 struct GNUNET_CLIENT_MANAGER_Connection *client,
756 const struct GNUNET_MessageHeader *msg)
758 struct GNUNET_SOCIAL_Place *
759 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
760 GNUNET_PSYC_transmit_got_ack (plc->tmit);
765 place_recv_message (void *cls,
766 struct GNUNET_CLIENT_MANAGER_Connection *client,
767 const struct GNUNET_MessageHeader *msg)
769 struct GNUNET_SOCIAL_Place *
770 plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
771 GNUNET_PSYC_slicer_message (plc->slicer,
772 (const struct GNUNET_PSYC_MessageHeader *) msg);
777 host_recv_message (void *cls,
778 struct GNUNET_CLIENT_MANAGER_Connection *client,
779 const struct GNUNET_MessageHeader *msg)
781 struct GNUNET_SOCIAL_Host *
782 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (hst->plc));
783 GNUNET_PSYC_slicer_message (hst->slicer,
784 (const struct GNUNET_PSYC_MessageHeader *) msg);
785 GNUNET_PSYC_slicer_message (hst->plc.slicer,
786 (const struct GNUNET_PSYC_MessageHeader *) msg);
791 host_recv_enter_ack (void *cls,
792 struct GNUNET_CLIENT_MANAGER_Connection *client,
793 const struct GNUNET_MessageHeader *msg)
795 struct GNUNET_SOCIAL_Host *
796 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
797 sizeof (struct GNUNET_SOCIAL_Place));
799 struct HostEnterAck *hack = (struct HostEnterAck *) msg;
800 hst->plc.pub_key = hack->place_pub_key;
802 int32_t result = ntohl (hack->result_code);
803 if (NULL != hst->enter_cb)
804 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
805 GNUNET_ntohll (hack->max_message_id));
810 host_recv_enter_request (void *cls,
811 struct GNUNET_CLIENT_MANAGER_Connection *client,
812 const struct GNUNET_MessageHeader *msg)
814 struct GNUNET_SOCIAL_Host *
815 hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
816 sizeof (struct GNUNET_SOCIAL_Place));
817 if (NULL == hst->answer_door_cb)
820 const char *method_name = NULL;
821 struct GNUNET_PSYC_Environment *env = NULL;
822 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
823 const void *data = NULL;
824 uint16_t data_size = 0;
826 const struct GNUNET_PSYC_JoinRequestMessage *
827 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
828 const struct GNUNET_PSYC_Message *join_msg = NULL;
832 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
834 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
835 LOG (GNUNET_ERROR_TYPE_DEBUG,
836 "Received join_msg of type %u and size %u.\n",
837 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
839 env = GNUNET_PSYC_env_create ();
840 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
841 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
845 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
846 LOG (GNUNET_ERROR_TYPE_WARNING,
847 "Ignoring invalid entry request from nym %s.\n",
854 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
855 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
860 GNUNET_PSYC_env_destroy (env);
861 if (NULL != entry_pmsg)
862 GNUNET_free (entry_pmsg);
867 guest_recv_enter_ack (void *cls,
868 struct GNUNET_CLIENT_MANAGER_Connection *client,
869 const struct GNUNET_MessageHeader *msg)
871 struct GNUNET_SOCIAL_Guest *
872 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
873 sizeof (struct GNUNET_SOCIAL_Place));
875 struct GNUNET_PSYC_CountersResultMessage *
876 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
877 int32_t result = ntohl (cres->result_code);
878 if (NULL != gst->enter_cb)
879 gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
884 guest_recv_join_decision (void *cls,
885 struct GNUNET_CLIENT_MANAGER_Connection *client,
886 const struct GNUNET_MessageHeader *msg)
888 struct GNUNET_SOCIAL_Guest *
889 gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
890 sizeof (struct GNUNET_SOCIAL_Place));
891 const struct GNUNET_PSYC_JoinDecisionMessage *
892 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
894 struct GNUNET_PSYC_Message *pmsg = NULL;
895 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
896 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
898 if (NULL != gst->entry_dcsn_cb)
899 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
904 app_recv_ego (void *cls,
905 struct GNUNET_CLIENT_MANAGER_Connection *client,
906 const struct GNUNET_MessageHeader *msg)
908 struct GNUNET_SOCIAL_App *
909 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
911 struct AppEgoMessage *
912 emsg = (struct AppEgoMessage *) msg;
914 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
916 struct GNUNET_HashCode ego_pub_hash;
917 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
920 struct GNUNET_SOCIAL_Ego *
921 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
924 ego = GNUNET_malloc (sizeof (*ego));
925 ego->pub_key = emsg->ego_pub_key;
926 ego->name = GNUNET_malloc (name_size);
927 memcpy (ego->name, &emsg[1], name_size);
931 ego->name = GNUNET_realloc (ego->name, name_size);
932 memcpy (ego->name, &emsg[1], name_size);
935 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
936 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
938 if (NULL != app->ego_cb)
939 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
944 app_recv_place (void *cls,
945 struct GNUNET_CLIENT_MANAGER_Connection *client,
946 const struct GNUNET_MessageHeader *msg)
948 struct GNUNET_SOCIAL_App *
949 app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
951 struct AppPlaceMessage *
952 pmsg = (struct AppPlaceMessage *) msg;
954 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
955 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
958 struct GNUNET_HashCode ego_pub_hash;
959 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
961 struct GNUNET_SOCIAL_Ego *
962 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
969 if (GNUNET_YES == pmsg->is_host)
971 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
973 hconn->plc_msg = *pmsg;
974 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
978 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
980 gconn->plc_msg = *pmsg;
981 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
986 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
988 { host_recv_enter_ack, NULL,
989 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
990 sizeof (struct HostEnterAck), GNUNET_NO },
992 { host_recv_enter_request, NULL,
993 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
994 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
996 { host_recv_message, NULL,
997 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
998 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1000 { place_recv_message_ack, NULL,
1001 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1002 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1004 { place_recv_history_result, NULL,
1005 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1006 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1008 { place_recv_state_result, NULL,
1009 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1010 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1012 { place_recv_result, NULL,
1013 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1014 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1016 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1018 { NULL, NULL, 0, 0, GNUNET_NO }
1022 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1024 { guest_recv_enter_ack, NULL,
1025 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1026 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1028 { host_recv_enter_request, NULL,
1029 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1030 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1032 { place_recv_message, NULL,
1033 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1034 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1036 { place_recv_message_ack, NULL,
1037 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1038 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1040 { guest_recv_join_decision, NULL,
1041 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1042 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1044 { place_recv_history_result, NULL,
1045 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1046 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1048 { place_recv_state_result, NULL,
1049 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1050 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1052 { place_recv_result, NULL,
1053 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1054 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1056 { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1058 { NULL, NULL, 0, 0, GNUNET_NO }
1063 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1065 { app_recv_ego, NULL,
1066 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1067 sizeof (struct AppEgoMessage), GNUNET_YES },
1069 { app_recv_place, NULL,
1070 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1071 sizeof (struct AppPlaceMessage), GNUNET_NO },
1073 { app_recv_result, NULL,
1074 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1075 sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1077 { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1079 { NULL, NULL, 0, 0, GNUNET_NO }
1084 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1086 if (NULL != plc->tmit)
1087 GNUNET_PSYC_transmit_destroy (plc->tmit);
1088 if (NULL != plc->connect_msg)
1089 GNUNET_free (plc->connect_msg);
1090 if (NULL != plc->disconnect_cb)
1091 plc->disconnect_cb (plc->disconnect_cls);
1096 host_cleanup (void *cls)
1098 struct GNUNET_SOCIAL_Host *hst = cls;
1099 place_cleanup (&hst->plc);
1100 if (NULL != hst->slicer)
1102 GNUNET_PSYC_slicer_destroy (hst->slicer);
1110 guest_cleanup (void *cls)
1112 struct GNUNET_SOCIAL_Guest *gst = cls;
1113 place_cleanup (&gst->plc);
1121 * Enter a place as host.
1123 * A place is created upon first entering, and it is active until permanently
1124 * left using GNUNET_SOCIAL_host_leave().
1127 * Application handle.
1129 * Identity of the host.
1131 * Private-public key pair of the place.
1132 * NULL to generate a key.
1134 * Policy specifying entry and history restrictions for the place.
1136 * Slicer to handle incoming messages.
1138 * Function called when the place is entered and ready to use.
1139 * @param answer_door_cb
1140 * Function to handle new nyms that want to enter.
1141 * @param farewell_cb
1142 * Function to handle departing nyms.
1144 * Closure for the callbacks.
1146 * @return Handle for the host.
1148 struct GNUNET_SOCIAL_Host *
1149 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1150 const struct GNUNET_SOCIAL_Ego *ego,
1151 enum GNUNET_PSYC_Policy policy,
1152 struct GNUNET_PSYC_Slicer *slicer,
1153 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1154 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1155 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1158 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1159 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1161 plc->cfg = app->cfg;
1162 plc->is_host = GNUNET_YES;
1163 plc->slicer = slicer;
1165 hst->enter_cb = enter_cb;
1166 hst->answer_door_cb = answer_door_cb;
1167 hst->farewell_cb = farewell_cb;
1170 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1171 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1173 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1175 hst->slicer = GNUNET_PSYC_slicer_create ();
1176 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave",
1177 host_recv_notice_place_leave_method,
1178 host_recv_notice_place_leave_modifier,
1179 NULL, host_recv_notice_place_leave_eom, hst);
1181 uint16_t app_id_size = strlen (app->id) + 1;
1182 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1183 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1184 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1185 hreq->policy = policy;
1186 hreq->ego_pub_key = ego->pub_key;
1187 memcpy (&hreq[1], app->id, app_id_size);
1189 plc->connect_msg = &hreq->header;
1190 place_send_connect_msg (plc);
1197 * Reconnect to an already entered place as host.
1200 * Host connection handle.
1201 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1203 * Slicer to handle incoming messages.
1205 * Function called when the place is entered and ready to use.
1206 * @param answer_door_cb
1207 * Function to handle new nyms that want to enter.
1208 * @param farewell_cb
1209 * Function to handle departing nyms.
1211 * Closure for the callbacks.
1213 * @return Handle for the host.
1215 struct GNUNET_SOCIAL_Host *
1216 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1217 struct GNUNET_PSYC_Slicer *slicer,
1218 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1219 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1220 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1223 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1224 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1226 size_t app_id_size = strlen (hconn->app->id) + 1;
1227 struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1229 hst->enter_cb = enter_cb;
1230 hst->answer_door_cb = answer_door_cb;
1231 hst->farewell_cb = farewell_cb;
1234 plc->cfg = hconn->app->cfg;
1235 plc->is_host = GNUNET_YES;
1236 plc->slicer = slicer;
1237 plc->pub_key = hconn->plc_msg.place_pub_key;
1238 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1240 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1241 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1243 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1245 hst->slicer = GNUNET_PSYC_slicer_create ();
1246 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave",
1247 host_recv_notice_place_leave_method,
1248 host_recv_notice_place_leave_modifier,
1249 NULL, host_recv_notice_place_leave_eom, hst);
1251 hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1252 hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1253 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1254 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1255 memcpy (&hreq[1], hconn->app->id, app_id_size);
1257 plc->connect_msg = &hreq->header;
1258 place_send_connect_msg (plc);
1260 GNUNET_free (hconn);
1266 * Decision whether to admit @a nym into the place or refuse entry.
1269 * Host of the place.
1271 * Handle for the entity that wanted to enter.
1272 * @param is_admitted
1273 * #GNUNET_YES if @a nym is admitted,
1274 * #GNUNET_NO if @a nym is refused entry,
1275 * #GNUNET_SYSERR if we cannot answer the request.
1276 * @param method_name
1277 * Method name for the rejection message.
1279 * Environment containing variables for the message, or NULL.
1281 * Data for the rejection message to send back.
1283 * Number of bytes in @a data for method.
1284 * @return #GNUNET_OK on success,
1285 * #GNUNET_SYSERR if the message is too large.
1288 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1289 struct GNUNET_SOCIAL_Nym *nym,
1291 const struct GNUNET_PSYC_Message *entry_resp)
1293 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1294 uint16_t entry_resp_size
1295 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1297 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1298 return GNUNET_SYSERR;
1300 dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1301 dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1302 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1303 dcsn->is_admitted = htonl (is_admitted);
1304 dcsn->slave_pub_key = nym->pub_key;
1306 if (0 < entry_resp_size)
1307 memcpy (&dcsn[1], entry_resp, entry_resp_size);
1309 GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1316 * Throw @a nym out of the place.
1318 * The @a nym reference will remain valid until the
1319 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1320 * which should be very soon after this call.
1323 * Host of the place.
1325 * Handle for the entity to be ejected.
1327 * Environment for the message or NULL.
1330 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1331 const struct GNUNET_SOCIAL_Nym *nym,
1332 struct GNUNET_PSYC_Environment *env)
1335 env = GNUNET_PSYC_env_create ();
1336 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1337 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1338 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1339 GNUNET_SOCIAL_ANNOUNCE_NONE);
1344 * Get the public key of @a ego.
1349 * @return Public key of ego.
1351 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1352 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1354 return &ego->pub_key;
1359 * Get the hash of the public key of @a ego.
1364 * @return Hash of the public key of @a ego.
1366 const struct GNUNET_HashCode *
1367 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1369 return &ego->pub_key_hash;
1374 * Get the name of @a ego.
1379 * @return Public key of @a ego.
1382 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1389 * Get the public key of @a nym.
1391 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1396 * @return Public key of @a nym.
1398 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1399 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1401 return &nym->pub_key;
1406 * Get the hash of the public key of @a nym.
1411 * @return Hash of the public key of @a nym.
1413 const struct GNUNET_HashCode *
1414 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1416 return &nym->pub_key_hash;
1421 * Send a message to all nyms that are present in the place.
1423 * This function is restricted to the host. Nyms can only send requests
1424 * to the host who can decide to relay it to everyone in the place.
1426 * @param host Host of the place.
1427 * @param method_name Method to use for the announcement.
1428 * @param env Environment containing variables for the message and operations
1429 * on objects of the place. Can be NULL.
1430 * @param notify Function to call to get the payload of the announcement.
1431 * @param notify_cls Closure for @a notify.
1432 * @param flags Flags for this announcement.
1434 * @return NULL on error (announcement already in progress?).
1436 struct GNUNET_SOCIAL_Announcement *
1437 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1438 const char *method_name,
1439 const struct GNUNET_PSYC_Environment *env,
1440 GNUNET_PSYC_TransmitNotifyData notify_data,
1441 void *notify_data_cls,
1442 enum GNUNET_SOCIAL_AnnounceFlags flags)
1445 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1446 NULL, notify_data, notify_data_cls, flags))
1447 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1454 * Resume transmitting announcement.
1457 * The announcement to resume.
1460 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1462 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1467 * Cancel announcement.
1470 * The announcement to cancel.
1473 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1475 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1480 * Obtain handle for a hosted place.
1482 * The returned handle can be used to access the place API.
1484 * @param host Handle for the host.
1486 * @return Handle for the hosted place, valid as long as @a host is valid.
1488 struct GNUNET_SOCIAL_Place *
1489 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1497 place_leave (struct GNUNET_SOCIAL_Place *plc)
1499 struct GNUNET_MessageHeader msg;
1500 msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1501 msg.size = htons (sizeof (msg));
1502 GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
1507 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1508 GNUNET_ContinuationCallback disconnect_cb,
1509 void *disconnect_cls)
1511 plc->is_disconnecting = GNUNET_YES;
1512 plc->disconnect_cb = disconnect_cb;
1513 plc->disconnect_cls = disconnect_cls;
1515 GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1516 GNUNET_YES == plc->is_host
1517 ? host_cleanup : guest_cleanup,
1523 * Disconnect from a home.
1525 * Invalidates host handle.
1528 * The host to disconnect.
1531 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1532 GNUNET_ContinuationCallback disconnect_cb,
1535 place_disconnect (&hst->plc, disconnect_cb, cls);
1540 * Stop hosting the home.
1542 * Sends a _notice_place_closed announcement to the home.
1543 * Invalidates host handle.
1548 * Environment for the message or NULL.
1549 * _nym is set to @e nym regardless whether an @e env is provided.
1550 * @param disconnect_cb
1551 * Function called after the host left the place
1552 * and disconnected from the social service.
1554 * Closure for @a disconnect_cb.
1557 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1558 const struct GNUNET_PSYC_Environment *env,
1559 GNUNET_ContinuationCallback disconnect_cb,
1562 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closed", env, NULL, NULL,
1563 GNUNET_SOCIAL_ANNOUNCE_NONE);
1564 place_leave (&hst->plc);
1565 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1571 static struct GuestEnterRequest *
1572 guest_enter_request_create (const char *app_id,
1573 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1574 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1575 const struct GNUNET_PeerIdentity *origin,
1577 const struct GNUNET_PeerIdentity *relays,
1578 const struct GNUNET_PSYC_Message *join_msg)
1580 uint16_t app_id_size = strlen (app_id) + 1;
1581 uint16_t join_msg_size = ntohs (join_msg->header.size);
1582 uint16_t relay_size = relay_count * sizeof (*relays);
1584 struct GuestEnterRequest *
1585 greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1587 greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1588 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1589 greq->place_pub_key = *place_pub_key;
1590 greq->ego_pub_key = *ego_pub_key;
1591 greq->origin = *origin;
1592 greq->relay_count = htonl (relay_count);
1594 char *p = (char *) &greq[1];
1595 memcpy (p, app_id, app_id_size);
1600 memcpy (p, relays, relay_size);
1604 memcpy (p, join_msg, join_msg_size);
1610 * Request entry to a place as a guest.
1613 * Application handle.
1615 * Identity of the guest.
1616 * @param place_pub_key
1617 * Public key of the place to enter.
1619 * Flags for the entry.
1621 * Peer identity of the origin of the underlying multicast group.
1622 * @param relay_count
1623 * Number of elements in the @a relays array.
1625 * Relays for the underlying multicast group.
1626 * @param method_name
1627 * Method name for the message.
1629 * Environment containing variables for the message, or NULL.
1631 * Payload for the message to give to the enter callback.
1633 * Number of bytes in @a data.
1635 * Slicer to use for processing incoming requests from guests.
1637 * @return NULL on errors, otherwise handle for the guest.
1639 struct GNUNET_SOCIAL_Guest *
1640 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1641 const struct GNUNET_SOCIAL_Ego *ego,
1642 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1643 enum GNUNET_PSYC_SlaveJoinFlags flags,
1644 const struct GNUNET_PeerIdentity *origin,
1645 uint32_t relay_count,
1646 const struct GNUNET_PeerIdentity *relays,
1647 const struct GNUNET_PSYC_Message *entry_msg,
1648 struct GNUNET_PSYC_Slicer *slicer,
1649 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1650 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1653 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1654 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1656 plc->ego_pub_key = ego->pub_key;
1657 plc->pub_key = *place_pub_key;
1658 plc->cfg = app->cfg;
1659 plc->is_host = GNUNET_YES;
1660 plc->slicer = slicer;
1662 gst->enter_cb = local_enter_cb;
1663 gst->entry_dcsn_cb = entry_dcsn_cb;
1666 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1667 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1669 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1671 struct GuestEnterRequest *
1672 greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1673 origin, relay_count, relays, entry_msg);
1674 plc->connect_msg = &greq->header;
1675 place_send_connect_msg (plc);
1681 * Request entry to a place by name as a guest.
1684 * Application handle.
1686 * Identity of the guest.
1688 * GNS name of the place to enter. Either in the form of
1689 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1690 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1691 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1692 * pseudonym's place directly.
1694 * Password to decrypt the record, or NULL for cleartext records.
1696 * Entry request message or NULL.
1698 * Slicer to use for processing incoming requests from guests.
1699 * @param local_enter_cb
1700 * Called upon connection established to the social service.
1701 * @param entry_decision_cb
1702 * Called upon receiving entry decision.
1704 * @return NULL on errors, otherwise handle for the guest.
1706 struct GNUNET_SOCIAL_Guest *
1707 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1708 const struct GNUNET_SOCIAL_Ego *ego,
1709 const char *gns_name,
1710 const char *password,
1711 const struct GNUNET_PSYC_Message *join_msg,
1712 struct GNUNET_PSYC_Slicer *slicer,
1713 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1714 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1717 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1718 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1720 if (NULL == password)
1723 uint16_t app_id_size = strlen (app->id) + 1;
1724 uint16_t gns_name_size = strlen (gns_name) + 1;
1725 uint16_t password_size = strlen (password) + 1;
1727 uint16_t join_msg_size = 0;
1728 if (NULL != join_msg);
1729 join_msg_size = ntohs (join_msg->header.size);
1731 uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
1732 + app_id_size + gns_name_size + password_size + join_msg_size;
1733 struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
1734 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1735 greq->header.size = htons (greq_size);
1736 greq->ego_pub_key = ego->pub_key;
1738 char *p = (char *) &greq[1];
1739 memcpy (p, app->id, app_id_size);
1741 memcpy (p, gns_name, gns_name_size);
1743 memcpy (p, password, password_size);
1745 if (NULL != join_msg)
1746 memcpy (p, join_msg, join_msg_size);
1748 gst->enter_cb = local_enter_cb;
1749 gst->entry_dcsn_cb = entry_decision_cb;
1752 plc->ego_pub_key = ego->pub_key;
1753 plc->cfg = app->cfg;
1754 plc->is_host = GNUNET_NO;
1755 plc->slicer = slicer;
1757 plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
1758 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1760 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1762 plc->connect_msg = &greq->header;
1763 place_send_connect_msg (plc);
1770 * Reconnect to an already entered place as guest.
1773 * Guest connection handle.
1774 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1776 * Flags for the entry.
1778 * Slicer to use for processing incoming requests from guests.
1779 * @param local_enter_cb
1780 * Called upon connection established to the social service.
1781 * @param entry_decision_cb
1782 * Called upon receiving entry decision.
1784 * @return NULL on errors, otherwise handle for the guest.
1786 struct GNUNET_SOCIAL_Guest *
1787 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1788 enum GNUNET_PSYC_SlaveJoinFlags flags,
1789 struct GNUNET_PSYC_Slicer *slicer,
1790 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1793 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1794 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1796 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1797 uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1798 struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1799 greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1800 greq->header.size = htons (greq_size);
1801 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1802 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1803 greq->flags = htonl (flags);
1805 memcpy (&greq[1], gconn->app->id, app_id_size);
1807 gst->enter_cb = local_enter_cb;
1810 plc->cfg = gconn->app->cfg;
1811 plc->is_host = GNUNET_NO;
1812 plc->slicer = slicer;
1813 plc->pub_key = gconn->plc_msg.place_pub_key;
1814 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1816 plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1817 GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1819 plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1821 plc->connect_msg = &greq->header;
1822 place_send_connect_msg (plc);
1824 GNUNET_free (gconn);
1830 * Talk to the host of the place.
1833 * Place where we want to talk to the host.
1834 * @param method_name
1835 * Method to invoke on the host.
1837 * Environment containing variables for the message, or NULL.
1838 * @param notify_data
1839 * Function to use to get the payload for the method.
1840 * @param notify_data_cls
1841 * Closure for @a notify_data.
1843 * Flags for the message being sent.
1845 * @return NULL if we are already trying to talk to the host,
1846 * otherwise handle to cancel the request.
1848 struct GNUNET_SOCIAL_TalkRequest *
1849 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1850 const char *method_name,
1851 const struct GNUNET_PSYC_Environment *env,
1852 GNUNET_PSYC_TransmitNotifyData notify_data,
1853 void *notify_data_cls,
1854 enum GNUNET_SOCIAL_TalkFlags flags)
1856 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1857 GNUNET_assert (NULL != plc->tmit);
1860 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1861 NULL, notify_data, notify_data_cls, flags))
1862 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1869 * Resume talking to the host of the place.
1872 * Talk request to resume.
1875 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1877 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1882 * Cancel talking to the host of the place.
1885 * Talk request to cancel.
1888 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1890 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1895 * Disconnect from a place.
1897 * Invalidates guest handle.
1900 * The guest to disconnect.
1903 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1904 GNUNET_ContinuationCallback disconnect_cb,
1907 place_disconnect (&gst->plc, disconnect_cb, cls);
1912 * Leave a place temporarily or permanently.
1914 * Notifies the owner of the place about leaving, and destroys the place handle.
1918 * @param keep_active
1919 * Keep place active after last application disconnected.
1920 * #GNUNET_YES or #GNUNET_NO
1922 * Optional environment for the leave message if @a keep_active
1923 * is #GNUNET_NO. NULL if not needed.
1925 * Called upon disconnecting from the social service.
1928 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1929 struct GNUNET_PSYC_Environment *env,
1930 GNUNET_ContinuationCallback disconnect_cb,
1933 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1934 GNUNET_SOCIAL_TALK_NONE);
1935 place_leave (&gst->plc);
1936 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1941 * Obtain handle for a place entered as guest.
1943 * The returned handle can be used to access the place API.
1945 * @param guest Handle for the guest.
1947 * @return Handle for the place, valid as long as @a guest is valid.
1949 struct GNUNET_SOCIAL_Place *
1950 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1957 * Obtain the public key of a place.
1962 * @return Public key of the place.
1964 const struct GNUNET_CRYPTO_EddsaPublicKey *
1965 GNUNET_SOCIAL_place_get_key (struct GNUNET_SOCIAL_Place *plc)
1967 return &plc->pub_key;
1972 * Set message processing @a flags for a @a method_prefix.
1976 * @param method_prefix
1977 * Method prefix @a flags apply to.
1979 * The flags that apply to a matching @a method_prefix.
1982 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
1983 const char *method_prefix,
1984 enum GNUNET_SOCIAL_MsgProcFlags flags)
1986 GNUNET_assert (NULL != method_prefix);
1987 struct MsgProcRequest *mpreq;
1988 uint16_t method_size = strnlen (method_prefix,
1989 GNUNET_SERVER_MAX_MESSAGE_SIZE
1990 - sizeof (*mpreq)) + 1;
1991 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
1992 mpreq = GNUNET_malloc (sizeof (*mpreq) + method_size);
1994 mpreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
1995 mpreq->header.size = htons (sizeof (*mpreq) + method_size);
1996 mpreq->flags = htonl (flags);
1997 memcpy (&mpreq[1], method_prefix, method_size);
1999 GNUNET_CLIENT_MANAGER_transmit (plc->client, &mpreq->header);
2000 GNUNET_free (mpreq);
2005 * Clear all message processing flags previously set for this place.
2008 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2010 struct GNUNET_MessageHeader req;
2011 req.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2012 req.size = htons (sizeof (req));
2013 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req);
2017 static struct GNUNET_SOCIAL_HistoryRequest *
2018 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2019 uint64_t start_message_id,
2020 uint64_t end_message_id,
2021 uint64_t message_limit,
2022 const char *method_prefix,
2024 struct GNUNET_PSYC_Slicer *slicer,
2025 GNUNET_ResultCallback result_cb,
2028 struct GNUNET_PSYC_HistoryRequestMessage *req;
2029 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2031 hist->slicer = slicer;
2032 hist->result_cb = result_cb;
2034 hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2035 &op_recv_history_result, hist);
2037 GNUNET_assert (NULL != method_prefix);
2038 uint16_t method_size = strnlen (method_prefix,
2039 GNUNET_SERVER_MAX_MESSAGE_SIZE
2040 - sizeof (*req)) + 1;
2041 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2042 req = GNUNET_malloc (sizeof (*req) + method_size);
2043 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2044 req->header.size = htons (sizeof (*req) + method_size);
2045 req->start_message_id = GNUNET_htonll (start_message_id);
2046 req->end_message_id = GNUNET_htonll (end_message_id);
2047 req->message_limit = GNUNET_htonll (message_limit);
2048 req->flags = htonl (flags);
2049 req->op_id = GNUNET_htonll (hist->op_id);
2050 memcpy (&req[1], method_prefix, method_size);
2052 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2059 * Learn about the history of a place.
2061 * Messages are returned through the @a slicer function
2062 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2065 * Place we want to learn more about.
2066 * @param start_message_id
2067 * First historic message we are interested in.
2068 * @param end_message_id
2069 * Last historic message we are interested in (inclusive).
2070 * @param method_prefix
2071 * Only retrieve messages with this method prefix.
2073 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2075 * Slicer to use for retrieved messages.
2076 * Can be the same as the slicer of the place.
2078 * Function called after all messages retrieved.
2079 * NULL if not needed.
2080 * @param cls Closure for @a result_cb.
2082 struct GNUNET_SOCIAL_HistoryRequest *
2083 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2084 uint64_t start_message_id,
2085 uint64_t end_message_id,
2086 const char *method_prefix,
2088 struct GNUNET_PSYC_Slicer *slicer,
2089 GNUNET_ResultCallback result_cb,
2092 return place_history_replay (plc, start_message_id, end_message_id, 0,
2093 method_prefix, flags, slicer, result_cb, cls);
2098 * Learn about the history of a place.
2100 * Sends messages through the slicer function of the place where
2101 * start_message_id <= message_id <= end_message_id.
2102 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2104 * To get the latest message, use 0 for both the start and end message ID.
2107 * Place we want to learn more about.
2108 * @param message_limit
2109 * Maximum number of historic messages we are interested in.
2110 * @param method_prefix
2111 * Only retrieve messages with this method prefix.
2113 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2115 * Function called after all messages retrieved.
2116 * NULL if not needed.
2117 * @param cls Closure for @a result_cb.
2119 struct GNUNET_SOCIAL_HistoryRequest *
2120 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2121 uint64_t message_limit,
2122 const char *method_prefix,
2124 struct GNUNET_PSYC_Slicer *slicer,
2125 GNUNET_ResultCallback result_cb,
2128 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2129 slicer, result_cb, cls);
2134 * Cancel learning about the history of a place.
2137 * History lesson to cancel.
2140 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2142 GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2148 * Request matching state variables.
2150 static struct GNUNET_SOCIAL_LookHandle *
2151 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2152 uint16_t type, const char *name,
2153 GNUNET_PSYC_StateVarCallback var_cb,
2154 GNUNET_ResultCallback result_cb, void *cls)
2156 struct GNUNET_PSYC_StateRequestMessage *req;
2157 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2159 look->var_cb = var_cb;
2160 look->result_cb = result_cb;
2162 look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2163 &op_recv_state_result, look);
2165 GNUNET_assert (NULL != name);
2166 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2167 - sizeof (*req)) + 1;
2168 req = GNUNET_malloc (sizeof (*req) + name_size);
2169 req->header.type = htons (type);
2170 req->header.size = htons (sizeof (*req) + name_size);
2171 req->op_id = GNUNET_htonll (look->op_id);
2172 memcpy (&req[1], name, name_size);
2174 GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2181 * Look at a particular object in the place.
2183 * The best matching object is returned (its name might be less specific than
2184 * what was requested).
2187 * The place where to look.
2189 * Full name of the object.
2191 * Set to the size of the returned value.
2193 * @return NULL if there is no such object at this place.
2195 struct GNUNET_SOCIAL_LookHandle *
2196 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2197 const char *full_name,
2198 GNUNET_PSYC_StateVarCallback var_cb,
2199 GNUNET_ResultCallback result_cb,
2202 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2203 full_name, var_cb, result_cb, cls);
2208 * Look for objects in the place with a matching name prefix.
2211 * The place where to look.
2212 * @param name_prefix
2213 * Look at objects with names beginning with this value.
2215 * Function to call for each object found.
2217 * Closure for callback function.
2219 * @return Handle that can be used to stop looking at objects.
2221 struct GNUNET_SOCIAL_LookHandle *
2222 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2223 const char *name_prefix,
2224 GNUNET_PSYC_StateVarCallback var_cb,
2225 GNUNET_ResultCallback result_cb,
2228 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2229 name_prefix, var_cb, result_cb, cls);
2234 * Cancel a state request operation.
2237 * Handle for the operation to cancel.
2240 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2242 GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2248 op_recv_zone_add_place_result (void *cls, int64_t result,
2249 const void *err_msg, uint16_t err_msg_size)
2251 LOG (GNUNET_ERROR_TYPE_DEBUG,
2252 "Received zone add place result: %" PRId64 ".\n", result);
2254 struct ZoneAddPlaceHandle *add_plc = cls;
2255 if (NULL != add_plc->result_cb)
2256 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2258 GNUNET_free (add_plc->req);
2259 GNUNET_free (add_plc);
2264 * Advertise @e place in the GNS zone of @e ego.
2267 * Application handle.
2270 * @param place_pub_key
2271 * Public key of place to add.
2273 * The name for the PLACE record to put in the zone.
2275 * Password used to encrypt the record or NULL to keep it cleartext.
2276 * @param relay_count
2277 * Number of elements in the @a relays array.
2279 * List of relays to put in the PLACE record to advertise
2280 * as entry points to the place in addition to the origin.
2281 * @param expiration_time
2282 * Expiration time of the record, use 0 to remove the record.
2284 * Function called with the result of the operation.
2286 * Closure for @a result_cb
2288 * @return #GNUNET_OK if the request was sent,
2289 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2292 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2293 const struct GNUNET_SOCIAL_Ego *ego,
2295 const char *password,
2296 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2297 const struct GNUNET_PeerIdentity *origin,
2298 uint32_t relay_count,
2299 const struct GNUNET_PeerIdentity *relays,
2300 struct GNUNET_TIME_Absolute expiration_time,
2301 GNUNET_ResultCallback result_cb,
2304 struct ZoneAddPlaceRequest *preq;
2305 size_t name_size = strlen (name) + 1;
2306 size_t password_size = strlen (password) + 1;
2307 size_t relay_size = relay_count * sizeof (*relays);
2308 size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2310 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2311 return GNUNET_SYSERR;
2313 preq = GNUNET_malloc (preq_size);
2314 preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2315 preq->header.size = htons (preq_size);
2316 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2317 preq->ego_pub_key = ego->pub_key;
2318 preq->place_pub_key = *place_pub_key;
2319 preq->origin = *origin;
2320 preq->relay_count = htonl (relay_count);
2322 char *p = (char *) &preq[1];
2323 memcpy (p, name, name_size);
2325 memcpy (p, password, password_size);
2327 memcpy (p, relays, relay_size);
2329 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2330 add_plc->req = preq;
2331 add_plc->result_cb = result_cb;
2332 add_plc->result_cls = result_cls;
2334 preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2335 op_recv_zone_add_place_result,
2337 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2344 op_recv_zone_add_nym_result (void *cls, int64_t result,
2345 const void *err_msg, uint16_t err_msg_size)
2347 LOG (GNUNET_ERROR_TYPE_DEBUG,
2348 "Received zone add nym result: %" PRId64 ".\n", result);
2350 struct ZoneAddNymHandle *add_nym = cls;
2351 if (NULL != add_nym->result_cb)
2352 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2354 GNUNET_free (add_nym->req);
2355 GNUNET_free (add_nym);
2360 * Add nym to the GNS zone of @e ego.
2367 * The name for the PKEY record to put in the zone.
2368 * @param nym_pub_key
2369 * Public key of nym to add.
2370 * @param expiration_time
2371 * Expiration time of the record, use 0 to remove the record.
2373 * Function called with the result of the operation.
2375 * Closure for @a result_cb
2377 * @return #GNUNET_OK if the request was sent,
2378 * #GNUNET_SYSERR on error, e.g. the name is too long.
2381 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2382 const struct GNUNET_SOCIAL_Ego *ego,
2384 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2385 struct GNUNET_TIME_Absolute expiration_time,
2386 GNUNET_ResultCallback result_cb,
2389 struct ZoneAddNymRequest *nreq;
2391 size_t name_size = strlen (name) + 1;
2392 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2393 return GNUNET_SYSERR;
2395 nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2396 nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2397 nreq->header.size = htons (sizeof (*nreq) + name_size);
2398 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2399 nreq->ego_pub_key = ego->pub_key;
2400 nreq->nym_pub_key = *nym_pub_key;
2401 memcpy (&nreq[1], name, name_size);
2403 struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2404 add_nym->req = nreq;
2405 add_nym->result_cb = result_cb;
2406 add_nym->result_cls = result_cls;
2408 nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2409 op_recv_zone_add_nym_result,
2411 GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2417 * Connect application to the social service.
2419 * The @host_place_cb and @guest_place_cb functions are
2420 * initially called for each entered places,
2421 * then later each time a new place is entered with the current application ID.
2427 * @param notify_host
2428 * Function to notify about a place entered as host.
2429 * @param notify_guest
2430 * Function to notify about a place entered as guest..
2432 * Closure for the callbacks.
2434 * @return Handle that can be used to stop listening.
2436 struct GNUNET_SOCIAL_App *
2437 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2439 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2440 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2441 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2444 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2445 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2449 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2451 app->ego_cb = ego_cb;
2452 app->host_cb = host_cb;
2453 app->guest_cb = guest_cb;
2455 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2456 app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2458 GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2460 app->id = GNUNET_malloc (app_id_size);
2461 memcpy (app->id, id, app_id_size);
2463 struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2464 creq->header.size = htons (sizeof (*creq) + app_id_size);
2465 creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2466 memcpy (&creq[1], app->id, app_id_size);
2468 app->connect_msg = &creq->header;
2469 app_send_connect_msg (app);
2476 * Disconnect application.
2479 * Application handle.
2482 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app)
2484 GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO, NULL, NULL);
2489 * Detach application from a place.
2491 * Removes the place from the entered places list for this application.
2492 * Note: this does not disconnect from the place.
2494 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2502 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2503 struct GNUNET_SOCIAL_Place *plc)
2505 struct AppDetachRequest dreq;
2506 dreq.header.size = htons (sizeof (dreq));
2507 dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2508 dreq.place_pub_key = plc->pub_key;
2509 GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2513 /* end of social_api.c */