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_MQ_Handle *mq;
77 * Message to send on connect.
79 struct GNUNET_MQ_Envelope *connect_env;
82 * Time to wait until we try to reconnect on failure.
84 struct GNUNET_TIME_Relative reconnect_delay;
87 * Task for reconnecting when the listener fails.
89 struct GNUNET_SCHEDULER_Task *reconnect_task;
94 struct GNUNET_OP_Handle *op;
97 * Function called after disconnected from the service.
99 GNUNET_ContinuationCallback disconnect_cb;
102 * Closure for @a disconnect_cb.
104 void *disconnect_cls;
112 * Hash map of all egos.
113 * pub_key_hash -> struct GNUNET_SOCIAL_Ego *
115 struct GNUNET_CONTAINER_MultiHashMap *egos;
117 GNUNET_SOCIAL_AppEgoCallback ego_cb;
118 GNUNET_SOCIAL_AppHostPlaceCallback host_cb;
119 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb;
120 GNUNET_SOCIAL_AppConnectedCallback connected_cb;
125 struct GNUNET_SOCIAL_HostConnection
127 struct GNUNET_SOCIAL_App *app;
129 struct AppPlaceMessage plc_msg;
133 struct GNUNET_SOCIAL_GuestConnection
135 struct GNUNET_SOCIAL_App *app;
137 struct AppPlaceMessage plc_msg;
142 * Handle for a place where social interactions happen.
144 struct GNUNET_SOCIAL_Place
147 * Configuration to use.
149 const struct GNUNET_CONFIGURATION_Handle *cfg;
152 * Client connection to the service.
154 struct GNUNET_MQ_Handle *mq;
157 * Message to send on connect.
159 struct GNUNET_MQ_Envelope *connect_env;
162 * Time to wait until we try to reconnect on failure.
164 struct GNUNET_TIME_Relative reconnect_delay;
167 * Task for reconnecting when the listener fails.
169 struct GNUNET_SCHEDULER_Task *reconnect_task;
174 struct GNUNET_OP_Handle *op;
177 * Transmission handle.
179 struct GNUNET_PSYC_TransmitHandle *tmit;
182 * Slicer for processing incoming messages.
184 struct GNUNET_PSYC_Slicer *slicer;
187 * Function called after disconnected from the service.
189 GNUNET_ContinuationCallback disconnect_cb;
192 * Closure for @a disconnect_cb.
194 void *disconnect_cls;
197 * Public key of the place.
199 struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
202 * Public key of the ego.
204 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
207 * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
214 * Host handle for a place that we entered.
216 struct GNUNET_SOCIAL_Host
218 struct GNUNET_SOCIAL_Place plc;
221 * Slicer for processing incoming messages from guests.
223 struct GNUNET_PSYC_Slicer *slicer;
225 GNUNET_SOCIAL_HostEnterCallback enter_cb;
227 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
229 GNUNET_SOCIAL_FarewellCallback farewell_cb;
232 * Closure for callbacks.
236 struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
237 struct GNUNET_PSYC_Environment *notice_place_leave_env;
242 * Guest handle for place that we entered.
244 struct GNUNET_SOCIAL_Guest
246 struct GNUNET_SOCIAL_Place plc;
248 GNUNET_SOCIAL_GuestEnterCallback enter_cb;
250 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
253 * Closure for callbacks.
260 * Hash map of all nyms.
261 * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
263 struct GNUNET_CONTAINER_MultiHashMap *nyms;
267 * Handle for an announcement request.
269 struct GNUNET_SOCIAL_Announcement
278 struct GNUNET_SOCIAL_TalkRequest
287 struct GNUNET_SOCIAL_HistoryRequest
292 struct GNUNET_SOCIAL_Place *plc;
300 * Slicer for processing incoming messages.
302 struct GNUNET_PSYC_Slicer *slicer;
305 * Function to call when the operation finished.
307 GNUNET_ResultCallback result_cb;
310 * Closure for @a result_cb.
316 struct GNUNET_SOCIAL_LookHandle
321 struct GNUNET_SOCIAL_Place *plc;
329 * State variable result callback.
331 GNUNET_PSYC_StateVarCallback var_cb;
334 * Function to call when the operation finished.
336 GNUNET_ResultCallback result_cb;
339 * Name of current modifier being received.
344 * Size of current modifier value being received.
346 size_t mod_value_size;
349 * Remaining size of current modifier value still to be received.
351 size_t mod_value_remaining;
354 * Closure for @a result_cb.
360 struct ZoneAddPlaceHandle
362 GNUNET_ResultCallback result_cb;
367 struct ZoneAddNymHandle
369 GNUNET_ResultCallback result_cb;
374 /*** CLEANUP / DISCONNECT ***/
378 host_cleanup (struct GNUNET_SOCIAL_Host *hst)
380 if (NULL != hst->slicer)
382 GNUNET_PSYC_slicer_destroy (hst->slicer);
390 guest_cleanup (struct GNUNET_SOCIAL_Guest *gst)
397 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
399 struct GNUNET_HashCode place_pub_hash;
401 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
403 "%s place cleanup: %s\n",
404 GNUNET_YES == plc->is_host ? "host" : "guest",
405 GNUNET_h2s (&place_pub_hash));
407 if (NULL != plc->tmit)
409 GNUNET_PSYC_transmit_destroy (plc->tmit);
412 if (NULL != plc->connect_env)
414 GNUNET_MQ_discard (plc->connect_env);
415 plc->connect_env = NULL;
419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
420 "destroying MQ (place_cleanup)\n");
421 GNUNET_MQ_destroy (plc->mq);
424 if (NULL != plc->disconnect_cb)
426 plc->disconnect_cb (plc->disconnect_cls);
427 plc->disconnect_cb = NULL;
430 (GNUNET_YES == plc->is_host)
431 ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
432 : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
437 place_disconnect (struct GNUNET_SOCIAL_Place *plc)
439 struct GNUNET_HashCode place_pub_hash;
441 GNUNET_CRYPTO_hash (&plc->pub_key,
442 sizeof (plc->pub_key),
444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
445 "place_disconnect, plc = %s\n",
446 GNUNET_h2s (&place_pub_hash));
453 static struct GNUNET_SOCIAL_Nym *
454 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
456 struct GNUNET_SOCIAL_Nym *nym = NULL;
457 struct GNUNET_HashCode pub_key_hash;
462 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
465 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
467 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
471 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
472 nym->pub_key = *pub_key;
473 nym->pub_key_hash = pub_key_hash;
474 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
475 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
482 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
484 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
489 /*** MESSAGE HANDLERS ***/
491 /** _notice_place_leave from guests */
494 host_recv_notice_place_leave_method (void *cls,
495 const struct GNUNET_PSYC_MessageHeader *msg,
496 const struct GNUNET_PSYC_MessageMethod *meth,
498 const char *method_name)
500 struct GNUNET_SOCIAL_Host *hst = cls;
502 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
503 &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
506 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
509 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
510 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
512 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
513 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
515 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
516 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
517 "_notice_place_leave: got method from nym %s (%s).\n",
518 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
524 host_recv_notice_place_leave_modifier (void *cls,
525 const struct GNUNET_PSYC_MessageHeader *msg,
526 const struct GNUNET_MessageHeader *pmsg,
528 enum GNUNET_PSYC_Operator oper,
532 uint16_t full_value_size)
534 struct GNUNET_SOCIAL_Host *hst = cls;
535 if (NULL == hst->notice_place_leave_env)
538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
539 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
541 message_id, oper, name, value_size, (const char *) value);
543 /* skip _nym, it's added later in eom() */
544 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
545 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
548 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
549 GNUNET_PSYC_OP_SET, name, value, value_size);
554 host_recv_notice_place_leave_eom (void *cls,
555 const struct GNUNET_PSYC_MessageHeader *msg,
556 const struct GNUNET_MessageHeader *pmsg,
558 uint8_t is_cancelled)
560 struct GNUNET_SOCIAL_Host *hst = cls;
561 if (NULL == hst->notice_place_leave_env)
564 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
566 "_notice_place_leave: got EOM from nym %s (%s).\n",
567 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
570 if (GNUNET_YES != is_cancelled)
572 if (NULL != hst->farewell_cb)
573 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
574 hst->notice_place_leave_env);
575 /* announce leaving guest to place */
576 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
577 "_nym", hst->notice_place_leave_nym,
578 sizeof (*hst->notice_place_leave_nym));
579 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
580 hst->notice_place_leave_env,
581 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
582 nym_destroy (hst->notice_place_leave_nym);
584 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
585 hst->notice_place_leave_env = NULL;
593 check_place_result (void *cls,
594 const struct GNUNET_OperationResultMessage *res)
596 uint16_t size = ntohs (res->header.size);
597 if (size < sizeof (*res))
598 { /* Error, message too small. */
600 return GNUNET_SYSERR;
607 handle_place_result (void *cls,
608 const struct GNUNET_OperationResultMessage *res)
610 struct GNUNET_SOCIAL_Place *plc = cls;
612 uint16_t size = ntohs (res->header.size);
613 uint16_t data_size = size - sizeof (*res);
614 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
616 GNUNET_OP_result (plc->op, GNUNET_ntohll (res->op_id),
617 GNUNET_ntohll (res->result_code),
618 data, data_size, NULL);
623 check_app_result (void *cls,
624 const struct GNUNET_OperationResultMessage *res)
626 uint16_t size = ntohs (res->header.size);
627 if (size < sizeof (*res))
628 { /* Error, message too small. */
630 return GNUNET_SYSERR;
637 handle_app_result (void *cls,
638 const struct GNUNET_OperationResultMessage *res)
640 struct GNUNET_SOCIAL_App *app = cls;
642 uint16_t size = ntohs (res->header.size);
643 uint16_t data_size = size - sizeof (*res);
644 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
646 GNUNET_OP_result (app->op, GNUNET_ntohll (res->op_id),
647 GNUNET_ntohll (res->result_code),
648 data, data_size, NULL);
653 op_recv_history_result (void *cls, int64_t result,
654 const void *err_msg, uint16_t err_msg_size)
656 LOG (GNUNET_ERROR_TYPE_DEBUG,
657 "Received history replay result: %" PRId64 ".\n", result);
659 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
661 if (NULL != hist->result_cb)
662 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
669 op_recv_state_result (void *cls, int64_t result,
670 const void *err_msg, uint16_t err_msg_size)
672 LOG (GNUNET_ERROR_TYPE_DEBUG,
673 "Received state request result: %" PRId64 ".\n", result);
675 struct GNUNET_SOCIAL_LookHandle *look = cls;
677 if (NULL != look->result_cb)
678 look->result_cb (look->cls, result, err_msg, err_msg_size);
685 check_place_history_result (void *cls,
686 const struct GNUNET_OperationResultMessage *res)
688 struct GNUNET_PSYC_MessageHeader *
689 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
690 uint16_t size = ntohs (res->header.size);
692 if (NULL == pmsg || size < sizeof (*res) + sizeof (*pmsg))
693 { /* Error, message too small. */
695 return GNUNET_SYSERR;
702 handle_place_history_result (void *cls,
703 const struct GNUNET_OperationResultMessage *res)
705 struct GNUNET_SOCIAL_Place *plc = cls;
706 struct GNUNET_PSYC_MessageHeader *
707 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
709 LOG (GNUNET_ERROR_TYPE_DEBUG,
710 "%p Received historic fragment for message #%" PRIu64 ".\n",
711 plc, GNUNET_ntohll (pmsg->message_id));
713 GNUNET_ResultCallback result_cb = NULL;
714 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
716 if (GNUNET_YES != GNUNET_OP_get (plc->op,
717 GNUNET_ntohll (res->op_id),
718 &result_cb, (void *) &hist, NULL))
719 { /* Operation not found. */
720 LOG (GNUNET_ERROR_TYPE_WARNING,
721 "%p Replay operation not found for historic fragment of message #%"
723 plc, GNUNET_ntohll (pmsg->message_id));
727 GNUNET_PSYC_slicer_message (hist->slicer,
728 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
733 check_place_state_result (void *cls,
734 const struct GNUNET_OperationResultMessage *res)
736 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
740 LOG (GNUNET_ERROR_TYPE_WARNING,
741 "Invalid modifier in state result\n");
742 return GNUNET_SYSERR;
745 uint16_t size = ntohs (res->header.size);
746 uint16_t mod_size = ntohs (mod->size);
747 if (size - sizeof (*res) != mod_size)
750 LOG (GNUNET_ERROR_TYPE_WARNING,
751 "Invalid modifier size in state result: %u - %u != %u\n",
752 ntohs (res->header.size), sizeof (*res), mod_size);
753 return GNUNET_SYSERR;
760 handle_place_state_result (void *cls,
761 const struct GNUNET_OperationResultMessage *res)
763 struct GNUNET_SOCIAL_Place *plc = cls;
765 GNUNET_ResultCallback result_cb = NULL;
766 struct GNUNET_SOCIAL_LookHandle *look = NULL;
768 if (GNUNET_YES != GNUNET_OP_get (plc->op,
769 GNUNET_ntohll (res->op_id),
770 &result_cb, (void *) &look, NULL))
771 { /* Operation not found. */
775 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
776 uint16_t mod_size = ntohs (mod->size);
778 switch (ntohs (mod->type))
780 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
782 const struct GNUNET_PSYC_MessageModifier *
783 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
785 const char *name = (const char *) &pmod[1];
786 uint16_t name_size = ntohs (pmod->name_size);
788 || mod_size - sizeof (*pmod) < name_size
789 || '\0' != name[name_size - 1])
792 LOG (GNUNET_ERROR_TYPE_WARNING,
793 "Invalid modifier name in state result\n");
796 look->mod_value_size = ntohs (pmod->value_size);
797 look->var_cb (look->cls, mod, name, name + name_size,
798 mod_size - sizeof (*mod) - name_size,
799 look->mod_value_size);
800 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
802 look->mod_value_remaining = look->mod_value_size;
803 look->mod_name = GNUNET_malloc (name_size);
804 GNUNET_memcpy (look->mod_name, name, name_size);
809 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
810 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
811 mod_size - sizeof (*mod), look->mod_value_size);
812 look->mod_value_remaining -= mod_size - sizeof (*mod);
813 if (0 == look->mod_value_remaining)
815 GNUNET_free (look->mod_name);
823 handle_place_message_ack (void *cls,
824 const struct GNUNET_MessageHeader *msg)
826 struct GNUNET_SOCIAL_Place *plc = cls;
828 GNUNET_PSYC_transmit_got_ack (plc->tmit);
833 check_place_message (void *cls,
834 const struct GNUNET_PSYC_MessageHeader *pmsg)
841 handle_place_message (void *cls,
842 const struct GNUNET_PSYC_MessageHeader *pmsg)
844 struct GNUNET_SOCIAL_Place *plc = cls;
846 GNUNET_PSYC_slicer_message (plc->slicer, pmsg);
851 check_host_message (void *cls,
852 const struct GNUNET_PSYC_MessageHeader *pmsg)
859 handle_host_message (void *cls,
860 const struct GNUNET_PSYC_MessageHeader *pmsg)
862 struct GNUNET_SOCIAL_Host *hst = cls;
864 GNUNET_PSYC_slicer_message (hst->slicer, pmsg);
865 GNUNET_PSYC_slicer_message (hst->plc.slicer, pmsg);
870 handle_host_enter_ack (void *cls,
871 const struct HostEnterAck *hack)
873 struct GNUNET_SOCIAL_Host *hst = cls;
875 hst->plc.pub_key = hack->place_pub_key;
877 int32_t result = ntohl (hack->result_code);
878 if (NULL != hst->enter_cb)
879 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
880 GNUNET_ntohll (hack->max_message_id));
885 check_host_enter_request (void *cls,
886 const struct GNUNET_PSYC_JoinRequestMessage *req)
893 handle_host_enter_request (void *cls,
894 const struct GNUNET_PSYC_JoinRequestMessage *req)
896 struct GNUNET_SOCIAL_Host *hst = cls;
898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
899 "handle_host_enter_request\n");
901 if (NULL == hst->answer_door_cb)
904 const char *method_name = NULL;
905 struct GNUNET_PSYC_Environment *env = NULL;
906 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
907 const void *data = NULL;
908 uint16_t data_size = 0;
910 const struct GNUNET_PSYC_Message *join_msg = NULL;
914 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
916 join_msg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (req);
917 LOG (GNUNET_ERROR_TYPE_DEBUG,
918 "Received join_msg of type %u and size %u.\n",
919 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
921 env = GNUNET_PSYC_env_create ();
922 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
923 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
927 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
928 LOG (GNUNET_ERROR_TYPE_WARNING,
929 "Ignoring invalid entry request from nym %s.\n",
936 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
937 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
942 GNUNET_PSYC_env_destroy (env);
943 if (NULL != entry_pmsg)
944 GNUNET_free (entry_pmsg);
949 handle_guest_enter_ack (void *cls,
950 const struct GNUNET_PSYC_CountersResultMessage *cres)
952 struct GNUNET_SOCIAL_Guest *gst = cls;
954 int32_t result = ntohl (cres->result_code);
955 if (NULL != gst->enter_cb)
956 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
957 GNUNET_ntohll (cres->max_message_id));
962 check_guest_enter_decision (void *cls,
963 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
970 handle_guest_enter_decision (void *cls,
971 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
973 struct GNUNET_SOCIAL_Guest *gst = cls;
975 struct GNUNET_PSYC_Message *pmsg = NULL;
976 if (ntohs (dcsn->header.size) > sizeof (*dcsn))
977 pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
979 if (NULL != gst->entry_dcsn_cb)
980 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
985 check_app_ego (void *cls,
986 const struct AppEgoMessage *emsg)
993 handle_app_ego (void *cls,
994 const struct AppEgoMessage *emsg)
996 struct GNUNET_SOCIAL_App *app = cls;
998 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
1000 struct GNUNET_HashCode ego_pub_hash;
1001 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
1004 struct GNUNET_SOCIAL_Ego *
1005 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
1008 ego = GNUNET_malloc (sizeof (*ego));
1009 ego->pub_key = emsg->ego_pub_key;
1010 ego->name = GNUNET_malloc (name_size);
1011 GNUNET_memcpy (ego->name, &emsg[1], name_size);
1015 ego->name = GNUNET_realloc (ego->name, name_size);
1016 GNUNET_memcpy (ego->name, &emsg[1], name_size);
1019 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
1020 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1022 if (NULL != app->ego_cb)
1023 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
1028 handle_app_ego_end (void *cls,
1029 const struct GNUNET_MessageHeader *msg)
1031 //struct GNUNET_SOCIAL_App *app = cls;
1036 check_app_place (void *cls,
1037 const struct AppPlaceMessage *pmsg)
1044 handle_app_place (void *cls,
1045 const struct AppPlaceMessage *pmsg)
1047 struct GNUNET_SOCIAL_App *app = cls;
1049 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
1050 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
1053 struct GNUNET_HashCode ego_pub_hash;
1054 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
1056 struct GNUNET_SOCIAL_Ego *
1057 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
1060 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failure to obtain ego %s.\n",
1061 GNUNET_h2s (&ego_pub_hash));
1066 if (GNUNET_YES == pmsg->is_host)
1068 if (NULL != app->host_cb) {
1069 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
1071 hconn->plc_msg = *pmsg;
1072 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1073 GNUNET_free (hconn);
1076 else if (NULL != app->guest_cb)
1078 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
1080 gconn->plc_msg = *pmsg;
1081 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1082 GNUNET_free (gconn);
1088 handle_app_place_end (void *cls,
1089 const struct GNUNET_MessageHeader *msg)
1091 struct GNUNET_SOCIAL_App *app = cls;
1093 if (NULL != app->connected_cb)
1094 app->connected_cb (app->cb_cls);
1099 * Handler for a #GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK message received
1100 * from the social service.
1102 * @param cls the place of type `struct GNUNET_SOCIAL_Place`
1103 * @param msg the message received from the service
1106 handle_place_leave_ack (void *cls,
1107 const struct GNUNET_MessageHeader *msg)
1109 struct GNUNET_SOCIAL_Place *plc = cls;
1111 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1112 "%s left place %p\n",
1113 plc->is_host ? "host" : "guest",
1115 place_disconnect (plc);
1123 host_connect (struct GNUNET_SOCIAL_Host *hst);
1127 host_reconnect (void *cls)
1134 * Host client disconnected from service.
1136 * Reconnect after backoff period.
1139 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1141 struct GNUNET_SOCIAL_Host *hst = cls;
1142 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1144 LOG (GNUNET_ERROR_TYPE_DEBUG,
1145 "Host client disconnected (%d), re-connecting\n",
1147 if (NULL != plc->tmit)
1149 GNUNET_PSYC_transmit_destroy (plc->tmit);
1152 if (NULL != plc->mq)
1154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1155 "destroying MQ (host_disconnected)\n");
1156 GNUNET_MQ_destroy (plc->mq);
1160 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1163 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1168 host_connect (struct GNUNET_SOCIAL_Host *hst)
1170 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1172 struct GNUNET_MQ_MessageHandler handlers[] = {
1173 GNUNET_MQ_hd_fixed_size (host_enter_ack,
1174 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1175 struct HostEnterAck,
1177 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1178 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1179 struct GNUNET_MessageHeader,
1181 GNUNET_MQ_hd_var_size (host_enter_request,
1182 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1183 struct GNUNET_PSYC_JoinRequestMessage,
1185 GNUNET_MQ_hd_var_size (host_message,
1186 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1187 struct GNUNET_PSYC_MessageHeader,
1189 GNUNET_MQ_hd_fixed_size (place_message_ack,
1190 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1191 struct GNUNET_MessageHeader,
1193 GNUNET_MQ_hd_var_size (place_history_result,
1194 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1195 struct GNUNET_OperationResultMessage,
1197 GNUNET_MQ_hd_var_size (place_state_result,
1198 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1199 struct GNUNET_OperationResultMessage,
1201 GNUNET_MQ_hd_var_size (place_result,
1202 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1203 struct GNUNET_OperationResultMessage,
1205 GNUNET_MQ_handler_end ()
1208 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1209 handlers, host_disconnected, hst);
1210 GNUNET_assert (NULL != plc->mq);
1211 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1213 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1218 * Enter a place as host.
1220 * A place is created upon first entering, and it is active until permanently
1221 * left using GNUNET_SOCIAL_host_leave().
1224 * Application handle.
1226 * Identity of the host.
1228 * Policy specifying entry and history restrictions for the place.
1230 * Slicer to handle incoming messages.
1232 * Function called when the place is entered and ready to use.
1233 * @param answer_door_cb
1234 * Function to handle new nyms that want to enter.
1235 * @param farewell_cb
1236 * Function to handle departing nyms.
1238 * Closure for the callbacks.
1240 * @return Handle for the host. This handle contains the pubkey.
1242 struct GNUNET_SOCIAL_Host *
1243 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1244 const struct GNUNET_SOCIAL_Ego *ego,
1245 enum GNUNET_PSYC_Policy policy,
1246 struct GNUNET_PSYC_Slicer *slicer,
1247 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1248 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1249 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1252 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1253 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1255 plc->cfg = app->cfg;
1256 plc->is_host = GNUNET_YES;
1257 plc->slicer = slicer;
1259 hst->enter_cb = enter_cb;
1260 hst->answer_door_cb = answer_door_cb;
1261 hst->farewell_cb = farewell_cb;
1264 plc->op = GNUNET_OP_create ();
1266 hst->slicer = GNUNET_PSYC_slicer_create ();
1267 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1268 host_recv_notice_place_leave_method,
1269 host_recv_notice_place_leave_modifier,
1270 NULL, host_recv_notice_place_leave_eom, hst);
1272 uint16_t app_id_size = strlen (app->id) + 1;
1273 struct HostEnterRequest *hreq;
1274 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1275 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1276 hreq->policy = policy;
1277 hreq->ego_pub_key = ego->pub_key;
1278 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1286 * Reconnect to an already entered place as host.
1289 * Host connection handle.
1290 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1292 * Slicer to handle incoming messages.
1294 * Function called when the place is entered and ready to use.
1295 * @param answer_door_cb
1296 * Function to handle new nyms that want to enter.
1297 * @param farewell_cb
1298 * Function to handle departing nyms.
1300 * Closure for the callbacks.
1302 * @return Handle for the host.
1304 struct GNUNET_SOCIAL_Host *
1305 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1306 struct GNUNET_PSYC_Slicer *slicer,
1307 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1308 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1309 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1312 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1313 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1315 hst->enter_cb = enter_cb;
1316 hst->answer_door_cb = answer_door_cb;
1317 hst->farewell_cb = farewell_cb;
1320 plc->cfg = hconn->app->cfg;
1321 plc->is_host = GNUNET_YES;
1322 plc->slicer = slicer;
1323 plc->pub_key = hconn->plc_msg.place_pub_key;
1324 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1326 plc->op = GNUNET_OP_create ();
1328 hst->slicer = GNUNET_PSYC_slicer_create ();
1329 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1330 host_recv_notice_place_leave_method,
1331 host_recv_notice_place_leave_modifier,
1332 NULL, host_recv_notice_place_leave_eom, hst);
1334 size_t app_id_size = strlen (hconn->app->id) + 1;
1335 struct HostEnterRequest *hreq;
1336 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1337 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1338 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1339 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1340 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1348 * Decision whether to admit @a nym into the place or refuse entry.
1351 * Host of the place.
1353 * Handle for the entity that wanted to enter.
1354 * @param is_admitted
1355 * #GNUNET_YES if @a nym is admitted,
1356 * #GNUNET_NO if @a nym is refused entry,
1357 * #GNUNET_SYSERR if we cannot answer the request.
1358 * @param method_name
1359 * Method name for the rejection message.
1361 * Environment containing variables for the message, or NULL.
1363 * Data for the rejection message to send back.
1365 * Number of bytes in @a data for method.
1366 * @return #GNUNET_OK on success,
1367 * #GNUNET_SYSERR if the message is too large.
1370 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1371 struct GNUNET_SOCIAL_Nym *nym,
1373 const struct GNUNET_PSYC_Message *entry_resp)
1375 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1376 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1377 uint16_t entry_resp_size
1378 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1380 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1381 return GNUNET_SYSERR;
1383 struct GNUNET_MQ_Envelope *
1384 env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1385 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1386 dcsn->is_admitted = htonl (is_admitted);
1387 dcsn->slave_pub_key = nym->pub_key;
1389 if (0 < entry_resp_size)
1390 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1392 GNUNET_MQ_send (plc->mq, env);
1398 * Throw @a nym out of the place.
1400 * The @a nym reference will remain valid until the
1401 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1402 * which should be very soon after this call.
1405 * Host of the place.
1407 * Handle for the entity to be ejected.
1409 * Environment for the message or NULL.
1412 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1413 const struct GNUNET_SOCIAL_Nym *nym,
1414 struct GNUNET_PSYC_Environment *e)
1416 struct GNUNET_PSYC_Environment *env = e;
1418 env = GNUNET_PSYC_env_create ();
1419 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1420 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1421 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1422 GNUNET_SOCIAL_ANNOUNCE_NONE);
1424 GNUNET_PSYC_env_destroy (env);
1429 * Get the public key of @a ego.
1434 * @return Public key of ego.
1436 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1437 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1439 return &ego->pub_key;
1444 * Get the hash of the public key of @a ego.
1449 * @return Hash of the public key of @a ego.
1451 const struct GNUNET_HashCode *
1452 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1454 return &ego->pub_key_hash;
1459 * Get the name of @a ego.
1464 * @return Public key of @a ego.
1467 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1474 * Get the public key of @a nym.
1476 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1481 * @return Public key of @a nym.
1483 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1484 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1486 return &nym->pub_key;
1491 * Get the hash of the public key of @a nym.
1496 * @return Hash of the public key of @a nym.
1498 const struct GNUNET_HashCode *
1499 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1501 return &nym->pub_key_hash;
1506 * Send a message to all nyms that are present in the place.
1508 * This function is restricted to the host. Nyms can only send requests
1509 * to the host who can decide to relay it to everyone in the place.
1511 * @param host Host of the place.
1512 * @param method_name Method to use for the announcement.
1513 * @param env Environment containing variables for the message and operations
1514 * on objects of the place. Can be NULL.
1515 * @param notify Function to call to get the payload of the announcement.
1516 * @param notify_cls Closure for @a notify.
1517 * @param flags Flags for this announcement.
1519 * @return NULL on error (announcement already in progress?).
1521 struct GNUNET_SOCIAL_Announcement *
1522 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1523 const char *method_name,
1524 const struct GNUNET_PSYC_Environment *env,
1525 GNUNET_PSYC_TransmitNotifyData notify_data,
1526 void *notify_data_cls,
1527 enum GNUNET_SOCIAL_AnnounceFlags flags)
1530 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1531 NULL, notify_data, notify_data_cls, flags))
1532 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1539 * Resume transmitting announcement.
1542 * The announcement to resume.
1545 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1547 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1552 * Cancel announcement.
1555 * The announcement to cancel.
1558 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1560 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1565 * Obtain handle for a hosted place.
1567 * The returned handle can be used to access the place API.
1569 * @param host Handle for the host.
1571 * @return Handle for the hosted place, valid as long as @a host is valid.
1573 struct GNUNET_SOCIAL_Place *
1574 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1581 * Disconnect from a home.
1583 * Invalidates host handle.
1586 * The host to disconnect.
1589 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1590 GNUNET_ContinuationCallback disconnect_cb,
1593 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1595 plc->disconnect_cb = disconnect_cb;
1596 plc->disconnect_cls = cls;
1597 place_disconnect (plc);
1602 * Stop hosting the home.
1604 * Sends a _notice_place_closing announcement to the home.
1605 * Invalidates host handle.
1610 * Environment for the message or NULL.
1611 * _nym is set to @e nym regardless whether an @e env is provided.
1612 * @param disconnect_cb
1613 * Function called after the host left the place
1614 * and disconnected from the social service.
1616 * Closure for @a disconnect_cb.
1619 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1620 const struct GNUNET_PSYC_Environment *env,
1621 GNUNET_ContinuationCallback disconnect_cb,
1624 struct GNUNET_MessageHeader *msg;
1625 struct GNUNET_MQ_Envelope *envelope;
1627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1628 "GNUNET_SOCIAL_host_leave\n");
1629 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1630 GNUNET_SOCIAL_ANNOUNCE_NONE);
1631 hst->plc.disconnect_cb = disconnect_cb;
1632 hst->plc.disconnect_cls = cls;
1633 envelope = GNUNET_MQ_msg (msg,
1634 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1635 GNUNET_MQ_send (hst->plc.mq,
1644 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1648 guest_reconnect (void *cls)
1650 guest_connect (cls);
1655 * Guest client disconnected from service.
1657 * Reconnect after backoff period.
1660 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1662 struct GNUNET_SOCIAL_Guest *gst = cls;
1663 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1666 "Guest client disconnected (%d), re-connecting\n",
1668 if (NULL != plc->tmit)
1670 GNUNET_PSYC_transmit_destroy (plc->tmit);
1673 if (NULL != plc->mq)
1675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1676 "destroying MQ (guest_disconnected)\n");
1677 GNUNET_MQ_destroy (plc->mq);
1681 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1684 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1689 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1691 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1693 struct GNUNET_MQ_MessageHandler handlers[] = {
1694 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1695 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1696 struct GNUNET_PSYC_CountersResultMessage,
1698 GNUNET_MQ_hd_fixed_size (place_leave_ack,
1699 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE_ACK,
1700 struct GNUNET_MessageHeader,
1702 GNUNET_MQ_hd_var_size (guest_enter_decision,
1703 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1704 struct GNUNET_PSYC_JoinDecisionMessage,
1706 GNUNET_MQ_hd_var_size (place_message,
1707 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1708 struct GNUNET_PSYC_MessageHeader,
1710 GNUNET_MQ_hd_fixed_size (place_message_ack,
1711 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1712 struct GNUNET_MessageHeader,
1714 GNUNET_MQ_hd_var_size (place_history_result,
1715 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1716 struct GNUNET_OperationResultMessage,
1718 GNUNET_MQ_hd_var_size (place_state_result,
1719 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1720 struct GNUNET_OperationResultMessage,
1722 GNUNET_MQ_hd_var_size (place_result,
1723 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1724 struct GNUNET_OperationResultMessage,
1726 GNUNET_MQ_handler_end ()
1729 plc->mq = GNUNET_CLIENT_connect (plc->cfg, "social",
1730 handlers, guest_disconnected, gst);
1731 GNUNET_assert (NULL != plc->mq);
1732 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1734 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1738 static struct GNUNET_MQ_Envelope *
1739 guest_enter_request_create (const char *app_id,
1740 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1741 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1742 const struct GNUNET_PeerIdentity *origin,
1744 const struct GNUNET_PeerIdentity *relays,
1745 const struct GNUNET_PSYC_Message *join_msg)
1747 uint16_t app_id_size = strlen (app_id) + 1;
1748 uint16_t join_msg_size = ntohs (join_msg->header.size);
1749 uint16_t relay_size = relay_count * sizeof (*relays);
1751 struct GuestEnterRequest *greq;
1752 struct GNUNET_MQ_Envelope *
1753 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1754 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1755 greq->place_pub_key = *place_pub_key;
1756 greq->ego_pub_key = *ego_pub_key;
1757 greq->origin = *origin;
1758 greq->relay_count = htonl (relay_count);
1760 char *p = (char *) &greq[1];
1761 GNUNET_memcpy (p, app_id, app_id_size);
1766 GNUNET_memcpy (p, relays, relay_size);
1770 GNUNET_memcpy (p, join_msg, join_msg_size);
1776 * Request entry to a place as a guest.
1779 * Application handle.
1781 * Identity of the guest.
1782 * @param place_pub_key
1783 * Public key of the place to enter.
1785 * Flags for the entry.
1787 * Peer identity of the origin of the underlying multicast group.
1788 * @param relay_count
1789 * Number of elements in the @a relays array.
1791 * Relays for the underlying multicast group.
1792 * @param method_name
1793 * Method name for the message.
1795 * Environment containing variables for the message, or NULL.
1797 * Payload for the message to give to the enter callback.
1799 * Number of bytes in @a data.
1801 * Slicer to use for processing incoming requests from guests.
1803 * @return NULL on errors, otherwise handle for the guest.
1805 struct GNUNET_SOCIAL_Guest *
1806 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1807 const struct GNUNET_SOCIAL_Ego *ego,
1808 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1809 enum GNUNET_PSYC_SlaveJoinFlags flags,
1810 const struct GNUNET_PeerIdentity *origin,
1811 uint32_t relay_count,
1812 const struct GNUNET_PeerIdentity *relays,
1813 const struct GNUNET_PSYC_Message *entry_msg,
1814 struct GNUNET_PSYC_Slicer *slicer,
1815 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1816 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1819 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1820 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1822 plc->ego_pub_key = ego->pub_key;
1823 plc->pub_key = *place_pub_key;
1824 plc->cfg = app->cfg;
1825 plc->is_host = GNUNET_NO;
1826 plc->slicer = slicer;
1828 plc->op = GNUNET_OP_create ();
1831 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1832 origin, relay_count, relays, entry_msg);
1834 gst->enter_cb = local_enter_cb;
1835 gst->entry_dcsn_cb = entry_dcsn_cb;
1838 guest_connect (gst);
1844 * Request entry to a place by name as a guest.
1847 * Application handle.
1849 * Identity of the guest.
1851 * GNS name of the place to enter. Either in the form of
1852 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1853 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1854 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1855 * pseudonym's place directly.
1857 * Password to decrypt the record, or NULL for cleartext records.
1859 * Entry request message or NULL.
1861 * Slicer to use for processing incoming requests from guests.
1862 * @param local_enter_cb
1863 * Called upon connection established to the social service.
1864 * @param entry_decision_cb
1865 * Called upon receiving entry decision.
1867 * @return NULL on errors, otherwise handle for the guest.
1869 struct GNUNET_SOCIAL_Guest *
1870 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1871 const struct GNUNET_SOCIAL_Ego *ego,
1872 const char *gns_name,
1873 const char *password,
1874 const struct GNUNET_PSYC_Message *join_msg,
1875 struct GNUNET_PSYC_Slicer *slicer,
1876 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1877 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1880 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1881 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1883 if (NULL == password)
1886 uint16_t app_id_size = strlen (app->id) + 1;
1887 uint16_t gns_name_size = strlen (gns_name) + 1;
1888 uint16_t password_size = strlen (password) + 1;
1890 uint16_t join_msg_size = 0;
1891 if (NULL != join_msg)
1892 join_msg_size = ntohs (join_msg->header.size);
1894 struct GuestEnterByNameRequest *greq;
1896 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1897 + password_size + join_msg_size,
1898 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1900 greq->ego_pub_key = ego->pub_key;
1902 char *p = (char *) &greq[1];
1903 GNUNET_memcpy (p, app->id, app_id_size);
1905 GNUNET_memcpy (p, gns_name, gns_name_size);
1907 GNUNET_memcpy (p, password, password_size);
1909 if (NULL != join_msg)
1910 GNUNET_memcpy (p, join_msg, join_msg_size);
1912 plc->ego_pub_key = ego->pub_key;
1913 plc->cfg = app->cfg;
1914 plc->is_host = GNUNET_NO;
1915 plc->slicer = slicer;
1917 plc->op = GNUNET_OP_create ();
1919 gst->enter_cb = local_enter_cb;
1920 gst->entry_dcsn_cb = entry_decision_cb;
1923 guest_connect (gst);
1929 * Reconnect to an already entered place as guest.
1932 * Guest connection handle.
1933 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1935 * Flags for the entry.
1937 * Slicer to use for processing incoming requests from guests.
1938 * @param local_enter_cb
1939 * Called upon connection established to the social service.
1940 * @param entry_decision_cb
1941 * Called upon receiving entry decision.
1943 * @return NULL on errors, otherwise handle for the guest.
1945 struct GNUNET_SOCIAL_Guest *
1946 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1947 enum GNUNET_PSYC_SlaveJoinFlags flags,
1948 struct GNUNET_PSYC_Slicer *slicer,
1949 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1952 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1953 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1955 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1956 struct GuestEnterRequest *greq;
1958 = GNUNET_MQ_msg_extra (greq, app_id_size,
1959 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1960 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1961 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1962 greq->flags = htonl (flags);
1964 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1966 plc->cfg = gconn->app->cfg;
1967 plc->is_host = GNUNET_NO;
1968 plc->slicer = slicer;
1969 plc->pub_key = gconn->plc_msg.place_pub_key;
1970 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1972 plc->op = GNUNET_OP_create ();
1974 gst->enter_cb = local_enter_cb;
1977 guest_connect (gst);
1983 * Talk to the host of the place.
1986 * Place where we want to talk to the host.
1987 * @param method_name
1988 * Method to invoke on the host.
1990 * Environment containing variables for the message, or NULL.
1991 * @param notify_data
1992 * Function to use to get the payload for the method.
1993 * @param notify_data_cls
1994 * Closure for @a notify_data.
1996 * Flags for the message being sent.
1998 * @return NULL if we are already trying to talk to the host,
1999 * otherwise handle to cancel the request.
2001 struct GNUNET_SOCIAL_TalkRequest *
2002 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
2003 const char *method_name,
2004 const struct GNUNET_PSYC_Environment *env,
2005 GNUNET_PSYC_TransmitNotifyData notify_data,
2006 void *notify_data_cls,
2007 enum GNUNET_SOCIAL_TalkFlags flags)
2009 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2010 GNUNET_assert (NULL != plc->tmit);
2013 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
2014 NULL, notify_data, notify_data_cls, flags))
2015 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
2022 * Resume talking to the host of the place.
2025 * Talk request to resume.
2028 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2030 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2035 * Cancel talking to the host of the place.
2038 * Talk request to cancel.
2041 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2043 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2048 * Disconnect from a place.
2050 * Invalidates guest handle.
2053 * The guest to disconnect.
2056 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2057 GNUNET_ContinuationCallback disconnect_cb,
2060 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
2062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2063 "GNUNET_SOCIAL_guest_disconnect, gst = %p\n",
2065 plc->disconnect_cb = disconnect_cb;
2066 plc->disconnect_cls = cls;
2067 place_disconnect (plc);
2072 * Leave a place temporarily or permanently.
2074 * Notifies the owner of the place about leaving, and destroys the place handle.
2078 * @param keep_active
2079 * Keep place active after last application disconnected.
2080 * #GNUNET_YES or #GNUNET_NO
2082 * Optional environment for the leave message if @a keep_active
2083 * is #GNUNET_NO. NULL if not needed.
2085 * Called upon disconnecting from the social service.
2088 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2089 struct GNUNET_PSYC_Environment *env,
2090 GNUNET_ContinuationCallback disconnect_cb,
2093 struct GNUNET_MessageHeader *msg;
2094 struct GNUNET_MQ_Envelope *envelope;
2096 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2097 GNUNET_SOCIAL_TALK_NONE);
2100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2101 "social_api: place_leave\n");
2102 gst->plc.disconnect_cb = disconnect_cb;
2103 gst->plc.disconnect_cls = cls;
2104 envelope = GNUNET_MQ_msg (msg,
2105 GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
2106 GNUNET_MQ_send (gst->plc.mq,
2112 * Obtain handle for a place entered as guest.
2114 * The returned handle can be used to access the place API.
2116 * @param guest Handle for the guest.
2118 * @return Handle for the place, valid as long as @a guest is valid.
2120 struct GNUNET_SOCIAL_Place *
2121 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2128 * Obtain the public key of a place.
2133 * @return Public key of the place.
2135 const struct GNUNET_CRYPTO_EddsaPublicKey *
2136 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2138 return &plc->pub_key;
2143 * Set message processing @a flags for a @a method_prefix.
2147 * @param method_prefix
2148 * Method prefix @a flags apply to.
2150 * The flags that apply to a matching @a method_prefix.
2153 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2154 const char *method_prefix,
2155 enum GNUNET_SOCIAL_MsgProcFlags flags)
2157 GNUNET_assert (NULL != method_prefix);
2158 struct MsgProcRequest *mpreq;
2159 uint16_t method_size = strnlen (method_prefix,
2160 GNUNET_MAX_MESSAGE_SIZE
2161 - sizeof (*mpreq)) + 1;
2162 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2164 struct GNUNET_MQ_Envelope *
2165 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2166 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2167 mpreq->flags = htonl (flags);
2168 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2170 GNUNET_MQ_send (plc->mq, env);
2175 * Clear all message processing flags previously set for this place.
2178 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2180 struct GNUNET_MessageHeader *req;
2181 struct GNUNET_MQ_Envelope *
2182 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2184 GNUNET_MQ_send (plc->mq, env);
2188 static struct GNUNET_SOCIAL_HistoryRequest *
2189 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2190 uint64_t start_message_id,
2191 uint64_t end_message_id,
2192 uint64_t message_limit,
2193 const char *method_prefix,
2195 struct GNUNET_PSYC_Slicer *slicer,
2196 GNUNET_ResultCallback result_cb,
2199 struct GNUNET_PSYC_HistoryRequestMessage *req;
2200 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2202 hist->slicer = slicer;
2203 hist->result_cb = result_cb;
2205 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2207 GNUNET_assert (NULL != method_prefix);
2208 uint16_t method_size = strnlen (method_prefix,
2209 GNUNET_MAX_MESSAGE_SIZE
2210 - sizeof (*req)) + 1;
2211 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2213 struct GNUNET_MQ_Envelope *
2214 env = GNUNET_MQ_msg_extra (req, method_size,
2215 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2216 req->start_message_id = GNUNET_htonll (start_message_id);
2217 req->end_message_id = GNUNET_htonll (end_message_id);
2218 req->message_limit = GNUNET_htonll (message_limit);
2219 req->flags = htonl (flags);
2220 req->op_id = GNUNET_htonll (hist->op_id);
2221 GNUNET_memcpy (&req[1], method_prefix, method_size);
2223 GNUNET_MQ_send (plc->mq, env);
2229 * Learn about the history of a place.
2231 * Messages are returned through the @a slicer function
2232 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2235 * Place we want to learn more about.
2236 * @param start_message_id
2237 * First historic message we are interested in.
2238 * @param end_message_id
2239 * Last historic message we are interested in (inclusive).
2240 * @param method_prefix
2241 * Only retrieve messages with this method prefix.
2243 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2245 * Slicer to use for retrieved messages.
2246 * Can be the same as the slicer of the place.
2248 * Function called after all messages retrieved.
2249 * NULL if not needed.
2250 * @param cls Closure for @a result_cb.
2252 struct GNUNET_SOCIAL_HistoryRequest *
2253 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2254 uint64_t start_message_id,
2255 uint64_t end_message_id,
2256 const char *method_prefix,
2258 struct GNUNET_PSYC_Slicer *slicer,
2259 GNUNET_ResultCallback result_cb,
2262 return place_history_replay (plc, start_message_id, end_message_id, 0,
2263 method_prefix, flags, slicer, result_cb, cls);
2268 * Learn about the history of a place.
2270 * Sends messages through the slicer function of the place where
2271 * start_message_id <= message_id <= end_message_id.
2272 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2274 * To get the latest message, use 0 for both the start and end message ID.
2277 * Place we want to learn more about.
2278 * @param message_limit
2279 * Maximum number of historic messages we are interested in.
2280 * @param method_prefix
2281 * Only retrieve messages with this method prefix.
2283 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2285 * Function called after all messages retrieved.
2286 * NULL if not needed.
2287 * @param cls Closure for @a result_cb.
2289 struct GNUNET_SOCIAL_HistoryRequest *
2290 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2291 uint64_t message_limit,
2292 const char *method_prefix,
2294 struct GNUNET_PSYC_Slicer *slicer,
2295 GNUNET_ResultCallback result_cb,
2298 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2299 slicer, result_cb, cls);
2304 * Cancel learning about the history of a place.
2307 * History lesson to cancel.
2310 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2312 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2318 * Request matching state variables.
2320 static struct GNUNET_SOCIAL_LookHandle *
2321 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2322 uint16_t type, const char *name,
2323 GNUNET_PSYC_StateVarCallback var_cb,
2324 GNUNET_ResultCallback result_cb, void *cls)
2326 struct GNUNET_PSYC_StateRequestMessage *req;
2327 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2329 look->var_cb = var_cb;
2330 look->result_cb = result_cb;
2332 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2334 GNUNET_assert (NULL != name);
2335 size_t name_size = strnlen (name, GNUNET_MAX_MESSAGE_SIZE
2336 - sizeof (*req)) + 1;
2337 struct GNUNET_MQ_Envelope *
2338 env = GNUNET_MQ_msg_extra (req, name_size, type);
2339 req->op_id = GNUNET_htonll (look->op_id);
2340 GNUNET_memcpy (&req[1], name, name_size);
2342 GNUNET_MQ_send (plc->mq, env);
2348 * Look at a particular object in the place.
2350 * The best matching object is returned (its name might be less specific than
2351 * what was requested).
2354 * The place where to look.
2356 * Full name of the object.
2358 * Set to the size of the returned value.
2360 * @return NULL if there is no such object at this place.
2362 struct GNUNET_SOCIAL_LookHandle *
2363 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2364 const char *full_name,
2365 GNUNET_PSYC_StateVarCallback var_cb,
2366 GNUNET_ResultCallback result_cb,
2369 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2370 full_name, var_cb, result_cb, cls);
2375 * Look for objects in the place with a matching name prefix.
2378 * The place where to look.
2379 * @param name_prefix
2380 * Look at objects with names beginning with this value.
2382 * Function to call for each object found.
2384 * Closure for callback function.
2386 * @return Handle that can be used to stop looking at objects.
2388 struct GNUNET_SOCIAL_LookHandle *
2389 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2390 const char *name_prefix,
2391 GNUNET_PSYC_StateVarCallback var_cb,
2392 GNUNET_ResultCallback result_cb,
2395 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2396 name_prefix, var_cb, result_cb, cls);
2401 * Cancel a state request operation.
2404 * Handle for the operation to cancel.
2407 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2409 GNUNET_OP_remove (look->plc->op, look->op_id);
2415 op_recv_zone_add_place_result (void *cls, int64_t result,
2416 const void *err_msg, uint16_t err_msg_size)
2418 LOG (GNUNET_ERROR_TYPE_DEBUG,
2419 "Received zone add place result: %" PRId64 ".\n", result);
2421 struct ZoneAddPlaceHandle *add_plc = cls;
2422 if (NULL != add_plc->result_cb)
2423 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2425 GNUNET_free (add_plc);
2430 * Advertise @e place in the GNS zone of @e ego.
2433 * Application handle.
2436 * @param place_pub_key
2437 * Public key of place to add.
2439 * The name for the PLACE record to put in the zone.
2441 * Password used to encrypt the record or NULL to keep it cleartext.
2442 * @param relay_count
2443 * Number of elements in the @a relays array.
2445 * List of relays to put in the PLACE record to advertise
2446 * as entry points to the place in addition to the origin.
2447 * @param expiration_time
2448 * Expiration time of the record, use 0 to remove the record.
2450 * Function called with the result of the operation.
2452 * Closure for @a result_cb
2454 * @return #GNUNET_OK if the request was sent,
2455 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2458 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2459 const struct GNUNET_SOCIAL_Ego *ego,
2461 const char *password,
2462 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2463 const struct GNUNET_PeerIdentity *origin,
2464 uint32_t relay_count,
2465 const struct GNUNET_PeerIdentity *relays,
2466 struct GNUNET_TIME_Absolute expiration_time,
2467 GNUNET_ResultCallback result_cb,
2470 struct ZoneAddPlaceRequest *preq;
2471 size_t name_size = strlen (name) + 1;
2472 size_t password_size = strlen (password) + 1;
2473 size_t relay_size = relay_count * sizeof (*relays);
2474 size_t payload_size = name_size + password_size + relay_size;
2476 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2477 return GNUNET_SYSERR;
2479 struct GNUNET_MQ_Envelope *
2480 env = GNUNET_MQ_msg_extra (preq, payload_size,
2481 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2482 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2483 preq->ego_pub_key = ego->pub_key;
2484 preq->place_pub_key = *place_pub_key;
2485 preq->origin = *origin;
2486 preq->relay_count = htonl (relay_count);
2488 char *p = (char *) &preq[1];
2489 GNUNET_memcpy (p, name, name_size);
2491 GNUNET_memcpy (p, password, password_size);
2493 GNUNET_memcpy (p, relays, relay_size);
2495 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2496 add_plc->result_cb = result_cb;
2497 add_plc->result_cls = result_cls;
2499 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2500 op_recv_zone_add_place_result,
2503 GNUNET_MQ_send (app->mq, env);
2509 op_recv_zone_add_nym_result (void *cls, int64_t result,
2510 const void *err_msg, uint16_t err_msg_size)
2512 LOG (GNUNET_ERROR_TYPE_DEBUG,
2513 "Received zone add nym result: %" PRId64 ".\n", result);
2515 struct ZoneAddNymHandle *add_nym = cls;
2516 if (NULL != add_nym->result_cb)
2517 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2519 GNUNET_free (add_nym);
2524 * Add nym to the GNS zone of @e ego.
2531 * The name for the PKEY record to put in the zone.
2532 * @param nym_pub_key
2533 * Public key of nym to add.
2534 * @param expiration_time
2535 * Expiration time of the record, use 0 to remove the record.
2537 * Function called with the result of the operation.
2539 * Closure for @a result_cb
2541 * @return #GNUNET_OK if the request was sent,
2542 * #GNUNET_SYSERR on error, e.g. the name is too long.
2545 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2546 const struct GNUNET_SOCIAL_Ego *ego,
2548 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2549 struct GNUNET_TIME_Absolute expiration_time,
2550 GNUNET_ResultCallback result_cb,
2553 struct ZoneAddNymRequest *nreq;
2555 size_t name_size = strlen (name) + 1;
2556 if (GNUNET_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2557 return GNUNET_SYSERR;
2559 struct GNUNET_MQ_Envelope *
2560 env = GNUNET_MQ_msg_extra (nreq, name_size,
2561 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2562 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2563 nreq->ego_pub_key = ego->pub_key;
2564 nreq->nym_pub_key = *nym_pub_key;
2565 GNUNET_memcpy (&nreq[1], name, name_size);
2567 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2568 add_nym->result_cb = result_cb;
2569 add_nym->result_cls = result_cls;
2571 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2572 op_recv_zone_add_nym_result,
2575 GNUNET_MQ_send (app->mq, env);
2584 app_connect (struct GNUNET_SOCIAL_App *app);
2588 app_reconnect (void *cls)
2595 * App client disconnected from service.
2597 * Reconnect after backoff period.
2600 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2602 struct GNUNET_SOCIAL_App *app = cls;
2604 LOG (GNUNET_ERROR_TYPE_DEBUG,
2605 "App client disconnected (%d), re-connecting\n",
2607 if (NULL != app->mq)
2609 GNUNET_MQ_destroy (app->mq);
2613 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2616 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2621 app_connect (struct GNUNET_SOCIAL_App *app)
2623 struct GNUNET_MQ_MessageHandler handlers[] = {
2624 GNUNET_MQ_hd_var_size (app_ego,
2625 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2626 struct AppEgoMessage,
2628 GNUNET_MQ_hd_fixed_size (app_ego_end,
2629 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2630 struct GNUNET_MessageHeader,
2632 GNUNET_MQ_hd_var_size (app_place,
2633 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2634 struct AppPlaceMessage,
2636 GNUNET_MQ_hd_fixed_size (app_place_end,
2637 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2638 struct GNUNET_MessageHeader,
2640 GNUNET_MQ_hd_var_size (app_result,
2641 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2642 struct GNUNET_OperationResultMessage,
2644 GNUNET_MQ_handler_end ()
2647 app->mq = GNUNET_CLIENT_connect (app->cfg, "social",
2648 handlers, app_disconnected, app);
2649 GNUNET_assert (NULL != app->mq);
2650 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2655 * Connect application to the social service.
2657 * The @host_place_cb and @guest_place_cb functions are
2658 * initially called for each entered places,
2659 * then later each time a new place is entered with the current application ID.
2666 * Function to notify about an available ego.
2668 * Function to notify about a place entered as host.
2670 * Function to notify about a place entered as guest.
2672 * Closure for the callbacks.
2674 * @return Handle that can be used to stop listening.
2676 struct GNUNET_SOCIAL_App *
2677 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2679 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2680 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2681 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2682 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2685 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2686 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2690 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2692 app->ego_cb = ego_cb;
2693 app->host_cb = host_cb;
2694 app->guest_cb = guest_cb;
2695 app->connected_cb = connected_cb;
2697 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2698 app->op = GNUNET_OP_create ();
2699 app->id = GNUNET_malloc (app_id_size);
2700 GNUNET_memcpy (app->id, id, app_id_size);
2702 struct AppConnectRequest *creq;
2703 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2704 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2705 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2713 app_cleanup (struct GNUNET_SOCIAL_App *app)
2715 if (NULL != app->mq)
2717 GNUNET_MQ_destroy (app->mq);
2720 if (NULL != app->disconnect_cb)
2722 app->disconnect_cb (app->disconnect_cls);
2723 app->disconnect_cb = NULL;
2729 * Disconnect application.
2732 * Application handle.
2733 * @param disconnect_cb
2734 * Disconnect callback.
2735 * @param disconnect_cls
2736 * Disconnect closure.
2739 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2740 GNUNET_ContinuationCallback disconnect_cb,
2741 void *disconnect_cls)
2743 app->disconnect_cb = disconnect_cb;
2744 app->disconnect_cls = disconnect_cls;
2746 if (NULL != app->mq)
2748 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2751 GNUNET_MQ_notify_sent (env, (GNUNET_SCHEDULER_TaskCallback) app_cleanup, app);
2766 * Detach application from a place.
2768 * Removes the place from the entered places list for this application.
2769 * Note: this does not disconnect from the place.
2771 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2779 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2780 struct GNUNET_SOCIAL_Place *plc)
2782 struct AppDetachRequest *dreq;
2783 struct GNUNET_MQ_Envelope *
2784 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2785 dreq->place_pub_key = plc->pub_key;
2786 dreq->ego_pub_key = plc->ego_pub_key;
2788 GNUNET_MQ_send (app->mq, env);
2792 /* end of social_api.c */