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;
376 static struct GNUNET_SOCIAL_Nym *
377 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
379 struct GNUNET_SOCIAL_Nym *nym = NULL;
380 struct GNUNET_HashCode pub_key_hash;
385 GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
388 nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
390 nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
394 nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
395 nym->pub_key = *pub_key;
396 nym->pub_key_hash = pub_key_hash;
397 GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
398 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
405 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
407 GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
412 /*** MESSAGE HANDLERS ***/
414 /** _notice_place_leave from guests */
417 host_recv_notice_place_leave_method (void *cls,
418 const struct GNUNET_PSYC_MessageHeader *msg,
419 const struct GNUNET_PSYC_MessageMethod *meth,
421 const char *method_name)
423 struct GNUNET_SOCIAL_Host *hst = cls;
425 if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
426 &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
429 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
432 "Host received method for message ID %" PRIu64 " from nym %s: %s\n",
433 message_id, GNUNET_h2s (&nym->pub_key_hash), method_name);
435 hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
436 hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
438 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
440 "_notice_place_leave: got method from nym %s (%s).\n",
441 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
447 host_recv_notice_place_leave_modifier (void *cls,
448 const struct GNUNET_PSYC_MessageHeader *msg,
449 const struct GNUNET_MessageHeader *pmsg,
451 enum GNUNET_PSYC_Operator oper,
455 uint16_t full_value_size)
457 struct GNUNET_SOCIAL_Host *hst = cls;
458 if (NULL == hst->notice_place_leave_env)
461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
462 "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
464 message_id, oper, name, value_size, (const char *) value);
466 /* skip _nym, it's added later in eom() */
467 if (0 == memcmp (name, "_nym", sizeof ("_nym"))
468 || 0 == memcmp (name, "_nym_", sizeof ("_nym_") - 1))
471 GNUNET_PSYC_env_add (hst->notice_place_leave_env,
472 GNUNET_PSYC_OP_SET, name, value, value_size);
477 host_recv_notice_place_leave_eom (void *cls,
478 const struct GNUNET_PSYC_MessageHeader *msg,
479 const struct GNUNET_MessageHeader *pmsg,
481 uint8_t is_cancelled)
483 struct GNUNET_SOCIAL_Host *hst = cls;
484 if (NULL == hst->notice_place_leave_env)
487 char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&hst->notice_place_leave_nym->pub_key);
488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
489 "_notice_place_leave: got EOM from nym %s (%s).\n",
490 GNUNET_h2s (&hst->notice_place_leave_nym->pub_key_hash), str);
493 if (GNUNET_YES != is_cancelled)
495 if (NULL != hst->farewell_cb)
496 hst->farewell_cb (hst->cb_cls, hst->notice_place_leave_nym,
497 hst->notice_place_leave_env);
498 /* announce leaving guest to place */
499 GNUNET_PSYC_env_add (hst->notice_place_leave_env, GNUNET_PSYC_OP_SET,
500 "_nym", hst->notice_place_leave_nym,
501 sizeof (*hst->notice_place_leave_nym));
502 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave",
503 hst->notice_place_leave_env,
504 NULL, NULL, GNUNET_SOCIAL_ANNOUNCE_NONE);
505 nym_destroy (hst->notice_place_leave_nym);
507 GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
508 hst->notice_place_leave_env = NULL;
516 check_place_result (void *cls,
517 const struct GNUNET_OperationResultMessage *res)
519 uint16_t size = ntohs (res->header.size);
520 if (size < sizeof (*res))
521 { /* Error, message too small. */
523 return GNUNET_SYSERR;
530 handle_place_result (void *cls,
531 const struct GNUNET_OperationResultMessage *res)
533 struct GNUNET_SOCIAL_Place *plc = cls;
535 uint16_t size = ntohs (res->header.size);
536 uint16_t data_size = size - sizeof (*res);
537 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
539 GNUNET_OP_result (plc->op, GNUNET_ntohll (res->op_id),
540 GNUNET_ntohll (res->result_code),
541 data, data_size, NULL);
546 check_app_result (void *cls,
547 const struct GNUNET_OperationResultMessage *res)
549 uint16_t size = ntohs (res->header.size);
550 if (size < sizeof (*res))
551 { /* Error, message too small. */
553 return GNUNET_SYSERR;
560 handle_app_result (void *cls,
561 const struct GNUNET_OperationResultMessage *res)
563 struct GNUNET_SOCIAL_App *app = cls;
565 uint16_t size = ntohs (res->header.size);
566 uint16_t data_size = size - sizeof (*res);
567 const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
569 GNUNET_OP_result (app->op, GNUNET_ntohll (res->op_id),
570 GNUNET_ntohll (res->result_code),
571 data, data_size, NULL);
576 op_recv_history_result (void *cls, int64_t result,
577 const void *err_msg, uint16_t err_msg_size)
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "Received history replay result: %" PRId64 ".\n", result);
582 struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
584 if (NULL != hist->result_cb)
585 hist->result_cb (hist->cls, result, err_msg, err_msg_size);
592 op_recv_state_result (void *cls, int64_t result,
593 const void *err_msg, uint16_t err_msg_size)
595 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 "Received state request result: %" PRId64 ".\n", result);
598 struct GNUNET_SOCIAL_LookHandle *look = cls;
600 if (NULL != look->result_cb)
601 look->result_cb (look->cls, result, err_msg, err_msg_size);
608 check_place_history_result (void *cls,
609 const struct GNUNET_OperationResultMessage *res)
611 struct GNUNET_PSYC_MessageHeader *
612 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
613 uint16_t size = ntohs (res->header.size);
615 if (NULL == pmsg || size < sizeof (*res) + sizeof (*pmsg))
616 { /* Error, message too small. */
618 return GNUNET_SYSERR;
625 handle_place_history_result (void *cls,
626 const struct GNUNET_OperationResultMessage *res)
628 struct GNUNET_SOCIAL_Place *plc = cls;
629 struct GNUNET_PSYC_MessageHeader *
630 pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "%p Received historic fragment for message #%" PRIu64 ".\n",
634 plc, GNUNET_ntohll (pmsg->message_id));
636 GNUNET_ResultCallback result_cb = NULL;
637 struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
639 if (GNUNET_YES != GNUNET_OP_get (plc->op,
640 GNUNET_ntohll (res->op_id),
641 &result_cb, (void *) &hist, NULL))
642 { /* Operation not found. */
643 LOG (GNUNET_ERROR_TYPE_WARNING,
644 "%p Replay operation not found for historic fragment of message #%"
646 plc, GNUNET_ntohll (pmsg->message_id));
650 GNUNET_PSYC_slicer_message (hist->slicer,
651 (const struct GNUNET_PSYC_MessageHeader *) pmsg);
656 check_place_state_result (void *cls,
657 const struct GNUNET_OperationResultMessage *res)
659 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
660 uint16_t mod_size = ntohs (mod->size);
661 uint16_t size = ntohs (res->header.size);
663 if (NULL == mod || size - sizeof (*res) != mod_size)
666 LOG (GNUNET_ERROR_TYPE_WARNING,
667 "Invalid modifier size in state result: %u - %u != %u\n",
668 ntohs (res->header.size), sizeof (*res), mod_size);
669 return GNUNET_SYSERR;
676 handle_place_state_result (void *cls,
677 const struct GNUNET_OperationResultMessage *res)
679 struct GNUNET_SOCIAL_Place *plc = cls;
681 GNUNET_ResultCallback result_cb = NULL;
682 struct GNUNET_SOCIAL_LookHandle *look = NULL;
684 if (GNUNET_YES != GNUNET_OP_get (plc->op,
685 GNUNET_ntohll (res->op_id),
686 &result_cb, (void *) &look, NULL))
687 { /* Operation not found. */
691 const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
692 uint16_t mod_size = ntohs (mod->size);
694 switch (ntohs (mod->type))
696 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
698 const struct GNUNET_PSYC_MessageModifier *
699 pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
701 const char *name = (const char *) &pmod[1];
702 uint16_t name_size = ntohs (pmod->name_size);
703 if ('\0' != name[name_size - 1])
706 LOG (GNUNET_ERROR_TYPE_WARNING,
707 "Invalid modifier name in state result\n");
710 look->mod_value_size = ntohs (pmod->value_size);
711 look->var_cb (look->cls, mod, name, name + name_size,
712 mod_size - sizeof (*mod) - name_size,
713 look->mod_value_size);
714 if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
716 look->mod_value_remaining = look->mod_value_size;
717 look->mod_name = GNUNET_malloc (name_size);
718 GNUNET_memcpy (look->mod_name, name, name_size);
723 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
724 look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
725 mod_size - sizeof (*mod), look->mod_value_size);
726 look->mod_value_remaining -= mod_size - sizeof (*mod);
727 if (0 == look->mod_value_remaining)
729 GNUNET_free (look->mod_name);
737 handle_place_message_ack (void *cls,
738 const struct GNUNET_MessageHeader *msg)
740 struct GNUNET_SOCIAL_Place *plc = cls;
742 GNUNET_PSYC_transmit_got_ack (plc->tmit);
747 check_place_message (void *cls,
748 const struct GNUNET_PSYC_MessageHeader *pmsg)
755 handle_place_message (void *cls,
756 const struct GNUNET_PSYC_MessageHeader *pmsg)
758 struct GNUNET_SOCIAL_Place *plc = cls;
760 GNUNET_PSYC_slicer_message (plc->slicer, pmsg);
765 check_host_message (void *cls,
766 const struct GNUNET_PSYC_MessageHeader *pmsg)
773 handle_host_message (void *cls,
774 const struct GNUNET_PSYC_MessageHeader *pmsg)
776 struct GNUNET_SOCIAL_Host *hst = cls;
778 GNUNET_PSYC_slicer_message (hst->slicer, pmsg);
779 GNUNET_PSYC_slicer_message (hst->plc.slicer, pmsg);
784 handle_host_enter_ack (void *cls,
785 const struct HostEnterAck *hack)
787 struct GNUNET_SOCIAL_Host *hst = cls;
789 hst->plc.pub_key = hack->place_pub_key;
791 int32_t result = ntohl (hack->result_code);
792 if (NULL != hst->enter_cb)
793 hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
794 GNUNET_ntohll (hack->max_message_id));
799 check_host_enter_request (void *cls,
800 const struct GNUNET_PSYC_JoinRequestMessage *req)
807 handle_host_enter_request (void *cls,
808 const struct GNUNET_PSYC_JoinRequestMessage *req)
810 struct GNUNET_SOCIAL_Host *hst = cls;
812 if (NULL == hst->answer_door_cb)
815 const char *method_name = NULL;
816 struct GNUNET_PSYC_Environment *env = NULL;
817 struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
818 const void *data = NULL;
819 uint16_t data_size = 0;
821 const struct GNUNET_PSYC_Message *join_msg = NULL;
825 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
827 join_msg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (req);
828 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 "Received join_msg of type %u and size %u.\n",
830 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
832 env = GNUNET_PSYC_env_create ();
833 entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
834 if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
838 str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
839 LOG (GNUNET_ERROR_TYPE_WARNING,
840 "Ignoring invalid entry request from nym %s.\n",
847 struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
848 hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
853 GNUNET_PSYC_env_destroy (env);
854 if (NULL != entry_pmsg)
855 GNUNET_free (entry_pmsg);
860 handle_guest_enter_ack (void *cls,
861 const struct GNUNET_PSYC_CountersResultMessage *cres)
863 struct GNUNET_SOCIAL_Guest *gst = cls;
865 int32_t result = ntohl (cres->result_code);
866 if (NULL != gst->enter_cb)
867 gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
868 GNUNET_ntohll (cres->max_message_id));
873 check_guest_enter_decision (void *cls,
874 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
881 handle_guest_enter_decision (void *cls,
882 const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
884 struct GNUNET_SOCIAL_Guest *gst = cls;
886 struct GNUNET_PSYC_Message *pmsg = NULL;
887 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
888 pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
890 if (NULL != gst->entry_dcsn_cb)
891 gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
896 check_app_ego (void *cls,
897 const struct AppEgoMessage *emsg)
904 handle_app_ego (void *cls,
905 const struct AppEgoMessage *emsg)
907 struct GNUNET_SOCIAL_App *app = cls;
909 uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
911 struct GNUNET_HashCode ego_pub_hash;
912 GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
915 struct GNUNET_SOCIAL_Ego *
916 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
919 ego = GNUNET_malloc (sizeof (*ego));
920 ego->pub_key = emsg->ego_pub_key;
921 ego->name = GNUNET_malloc (name_size);
922 GNUNET_memcpy (ego->name, &emsg[1], name_size);
926 ego->name = GNUNET_realloc (ego->name, name_size);
927 GNUNET_memcpy (ego->name, &emsg[1], name_size);
930 GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
931 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
933 if (NULL != app->ego_cb)
934 app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
939 handle_app_ego_end (void *cls,
940 const struct GNUNET_MessageHeader *msg)
942 //struct GNUNET_SOCIAL_App *app = cls;
947 check_app_place (void *cls,
948 const struct AppPlaceMessage *pmsg)
955 handle_app_place (void *cls,
956 const struct AppPlaceMessage *pmsg)
958 struct GNUNET_SOCIAL_App *app = cls;
960 if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
961 || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
964 struct GNUNET_HashCode ego_pub_hash;
965 GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
967 struct GNUNET_SOCIAL_Ego *
968 ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
971 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failure to obtain ego %s.\n",
972 GNUNET_h2s (&ego_pub_hash));
977 if (GNUNET_YES == pmsg->is_host)
979 if (NULL != app->host_cb) {
980 struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
982 hconn->plc_msg = *pmsg;
983 app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
987 else if (NULL != app->guest_cb)
989 struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
991 gconn->plc_msg = *pmsg;
992 app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
999 handle_app_place_end (void *cls,
1000 const struct GNUNET_MessageHeader *msg)
1002 struct GNUNET_SOCIAL_App *app = cls;
1004 if (NULL != app->connected_cb)
1005 app->connected_cb (app->cb_cls);
1009 /*** CLEANUP / DISCONNECT ***/
1013 host_cleanup (struct GNUNET_SOCIAL_Host *hst)
1015 if (NULL != hst->slicer)
1017 GNUNET_PSYC_slicer_destroy (hst->slicer);
1025 guest_cleanup (struct GNUNET_SOCIAL_Guest *gst)
1032 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1034 struct GNUNET_HashCode place_pub_hash;
1035 GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "%s place cleanup: %s\n",
1038 GNUNET_YES == plc->is_host ? "host" : "guest",
1039 GNUNET_h2s (&place_pub_hash));
1041 if (NULL != plc->tmit)
1043 GNUNET_PSYC_transmit_destroy (plc->tmit);
1046 if (NULL != plc->connect_env)
1048 GNUNET_MQ_discard (plc->connect_env);
1049 plc->connect_env = NULL;
1051 if (NULL != plc->mq)
1053 GNUNET_MQ_destroy (plc->mq);
1056 if (NULL != plc->disconnect_cb)
1058 plc->disconnect_cb (plc->disconnect_cls);
1059 plc->disconnect_cb = NULL;
1062 (GNUNET_YES == plc->is_host)
1063 ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
1064 : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
1069 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1070 GNUNET_ContinuationCallback cb,
1073 plc->disconnect_cb = cb;
1074 plc->disconnect_cls = cls;
1076 if (NULL != plc->mq)
1078 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (plc->mq);
1081 GNUNET_MQ_notify_sent (env, (GNUNET_MQ_NotifyCallback) place_cleanup, plc);
1085 place_cleanup (plc);
1090 place_cleanup (plc);
1096 place_leave (struct GNUNET_SOCIAL_Place *plc)
1098 struct GNUNET_MessageHeader *msg;
1099 struct GNUNET_MQ_Envelope *
1100 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1102 GNUNET_MQ_send (plc->mq, env);
1110 host_connect (struct GNUNET_SOCIAL_Host *hst);
1114 host_reconnect (void *cls)
1121 * Host client disconnected from service.
1123 * Reconnect after backoff period.
1126 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1128 struct GNUNET_SOCIAL_Host *hst = cls;
1129 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1131 LOG (GNUNET_ERROR_TYPE_DEBUG,
1132 "Host client disconnected (%d), re-connecting\n",
1134 if (NULL != plc->tmit)
1136 GNUNET_PSYC_transmit_destroy (plc->tmit);
1139 if (NULL != plc->mq)
1141 GNUNET_MQ_destroy (plc->mq);
1145 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1148 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1153 host_connect (struct GNUNET_SOCIAL_Host *hst)
1155 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1157 struct GNUNET_MQ_MessageHandler handlers[] = {
1158 GNUNET_MQ_hd_fixed_size (host_enter_ack,
1159 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1160 struct HostEnterAck,
1162 GNUNET_MQ_hd_var_size (host_enter_request,
1163 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1164 struct GNUNET_PSYC_JoinRequestMessage,
1166 GNUNET_MQ_hd_var_size (host_message,
1167 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1168 struct GNUNET_PSYC_MessageHeader,
1170 GNUNET_MQ_hd_fixed_size (place_message_ack,
1171 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1172 struct GNUNET_MessageHeader,
1174 GNUNET_MQ_hd_var_size (place_history_result,
1175 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1176 struct GNUNET_OperationResultMessage,
1178 GNUNET_MQ_hd_var_size (place_state_result,
1179 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1180 struct GNUNET_OperationResultMessage,
1182 GNUNET_MQ_hd_var_size (place_result,
1183 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1184 struct GNUNET_OperationResultMessage,
1186 GNUNET_MQ_handler_end ()
1189 plc->mq = GNUNET_CLIENT_connecT (plc->cfg, "social",
1190 handlers, host_disconnected, hst);
1191 GNUNET_assert (NULL != plc->mq);
1192 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1194 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1199 * Enter a place as host.
1201 * A place is created upon first entering, and it is active until permanently
1202 * left using GNUNET_SOCIAL_host_leave().
1205 * Application handle.
1207 * Identity of the host.
1209 * Policy specifying entry and history restrictions for the place.
1211 * Slicer to handle incoming messages.
1213 * Function called when the place is entered and ready to use.
1214 * @param answer_door_cb
1215 * Function to handle new nyms that want to enter.
1216 * @param farewell_cb
1217 * Function to handle departing nyms.
1219 * Closure for the callbacks.
1221 * @return Handle for the host. This handle contains the pubkey.
1223 struct GNUNET_SOCIAL_Host *
1224 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1225 const struct GNUNET_SOCIAL_Ego *ego,
1226 enum GNUNET_PSYC_Policy policy,
1227 struct GNUNET_PSYC_Slicer *slicer,
1228 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1229 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1230 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1233 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1234 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1236 plc->cfg = app->cfg;
1237 plc->is_host = GNUNET_YES;
1238 plc->slicer = slicer;
1240 hst->enter_cb = enter_cb;
1241 hst->answer_door_cb = answer_door_cb;
1242 hst->farewell_cb = farewell_cb;
1245 plc->op = GNUNET_OP_create ();
1247 hst->slicer = GNUNET_PSYC_slicer_create ();
1248 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1249 host_recv_notice_place_leave_method,
1250 host_recv_notice_place_leave_modifier,
1251 NULL, host_recv_notice_place_leave_eom, hst);
1253 uint16_t app_id_size = strlen (app->id) + 1;
1254 struct HostEnterRequest *hreq;
1255 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1256 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1257 hreq->policy = policy;
1258 hreq->ego_pub_key = ego->pub_key;
1259 GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1267 * Reconnect to an already entered place as host.
1270 * Host connection handle.
1271 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1273 * Slicer to handle incoming messages.
1275 * Function called when the place is entered and ready to use.
1276 * @param answer_door_cb
1277 * Function to handle new nyms that want to enter.
1278 * @param farewell_cb
1279 * Function to handle departing nyms.
1281 * Closure for the callbacks.
1283 * @return Handle for the host.
1285 struct GNUNET_SOCIAL_Host *
1286 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1287 struct GNUNET_PSYC_Slicer *slicer,
1288 GNUNET_SOCIAL_HostEnterCallback enter_cb,
1289 GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1290 GNUNET_SOCIAL_FarewellCallback farewell_cb,
1293 struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1294 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1296 hst->enter_cb = enter_cb;
1297 hst->answer_door_cb = answer_door_cb;
1298 hst->farewell_cb = farewell_cb;
1301 plc->cfg = hconn->app->cfg;
1302 plc->is_host = GNUNET_YES;
1303 plc->slicer = slicer;
1304 plc->pub_key = hconn->plc_msg.place_pub_key;
1305 plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1307 plc->op = GNUNET_OP_create ();
1309 hst->slicer = GNUNET_PSYC_slicer_create ();
1310 GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1311 host_recv_notice_place_leave_method,
1312 host_recv_notice_place_leave_modifier,
1313 NULL, host_recv_notice_place_leave_eom, hst);
1315 size_t app_id_size = strlen (hconn->app->id) + 1;
1316 struct HostEnterRequest *hreq;
1317 plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1318 GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1319 hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1320 hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1321 GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1329 * Decision whether to admit @a nym into the place or refuse entry.
1332 * Host of the place.
1334 * Handle for the entity that wanted to enter.
1335 * @param is_admitted
1336 * #GNUNET_YES if @a nym is admitted,
1337 * #GNUNET_NO if @a nym is refused entry,
1338 * #GNUNET_SYSERR if we cannot answer the request.
1339 * @param method_name
1340 * Method name for the rejection message.
1342 * Environment containing variables for the message, or NULL.
1344 * Data for the rejection message to send back.
1346 * Number of bytes in @a data for method.
1347 * @return #GNUNET_OK on success,
1348 * #GNUNET_SYSERR if the message is too large.
1351 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1352 struct GNUNET_SOCIAL_Nym *nym,
1354 const struct GNUNET_PSYC_Message *entry_resp)
1356 struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1357 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1358 uint16_t entry_resp_size
1359 = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1361 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1362 return GNUNET_SYSERR;
1364 struct GNUNET_MQ_Envelope *
1365 env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1366 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1367 dcsn->is_admitted = htonl (is_admitted);
1368 dcsn->slave_pub_key = nym->pub_key;
1370 if (0 < entry_resp_size)
1371 GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1373 GNUNET_MQ_send (plc->mq, env);
1379 * Throw @a nym out of the place.
1381 * The @a nym reference will remain valid until the
1382 * #GNUNET_SOCIAL_FarewellCallback is invoked,
1383 * which should be very soon after this call.
1386 * Host of the place.
1388 * Handle for the entity to be ejected.
1390 * Environment for the message or NULL.
1393 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1394 const struct GNUNET_SOCIAL_Nym *nym,
1395 struct GNUNET_PSYC_Environment *e)
1397 struct GNUNET_PSYC_Environment *env = e;
1399 env = GNUNET_PSYC_env_create ();
1400 GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1401 "_nym", &nym->pub_key, sizeof (nym->pub_key));
1402 GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1403 GNUNET_SOCIAL_ANNOUNCE_NONE);
1405 GNUNET_PSYC_env_destroy (env);
1410 * Get the public key of @a ego.
1415 * @return Public key of ego.
1417 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1418 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1420 return &ego->pub_key;
1425 * Get the hash of the public key of @a ego.
1430 * @return Hash of the public key of @a ego.
1432 const struct GNUNET_HashCode *
1433 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1435 return &ego->pub_key_hash;
1440 * Get the name of @a ego.
1445 * @return Public key of @a ego.
1448 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1455 * Get the public key of @a nym.
1457 * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1462 * @return Public key of @a nym.
1464 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1465 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1467 return &nym->pub_key;
1472 * Get the hash of the public key of @a nym.
1477 * @return Hash of the public key of @a nym.
1479 const struct GNUNET_HashCode *
1480 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1482 return &nym->pub_key_hash;
1487 * Send a message to all nyms that are present in the place.
1489 * This function is restricted to the host. Nyms can only send requests
1490 * to the host who can decide to relay it to everyone in the place.
1492 * @param host Host of the place.
1493 * @param method_name Method to use for the announcement.
1494 * @param env Environment containing variables for the message and operations
1495 * on objects of the place. Can be NULL.
1496 * @param notify Function to call to get the payload of the announcement.
1497 * @param notify_cls Closure for @a notify.
1498 * @param flags Flags for this announcement.
1500 * @return NULL on error (announcement already in progress?).
1502 struct GNUNET_SOCIAL_Announcement *
1503 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1504 const char *method_name,
1505 const struct GNUNET_PSYC_Environment *env,
1506 GNUNET_PSYC_TransmitNotifyData notify_data,
1507 void *notify_data_cls,
1508 enum GNUNET_SOCIAL_AnnounceFlags flags)
1511 GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1512 NULL, notify_data, notify_data_cls, flags))
1513 return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1520 * Resume transmitting announcement.
1523 * The announcement to resume.
1526 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1528 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1533 * Cancel announcement.
1536 * The announcement to cancel.
1539 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1541 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1546 * Obtain handle for a hosted place.
1548 * The returned handle can be used to access the place API.
1550 * @param host Handle for the host.
1552 * @return Handle for the hosted place, valid as long as @a host is valid.
1554 struct GNUNET_SOCIAL_Place *
1555 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1562 * Disconnect from a home.
1564 * Invalidates host handle.
1567 * The host to disconnect.
1570 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1571 GNUNET_ContinuationCallback disconnect_cb,
1574 place_disconnect (&hst->plc, disconnect_cb, cls);
1579 * Stop hosting the home.
1581 * Sends a _notice_place_closing announcement to the home.
1582 * Invalidates host handle.
1587 * Environment for the message or NULL.
1588 * _nym is set to @e nym regardless whether an @e env is provided.
1589 * @param disconnect_cb
1590 * Function called after the host left the place
1591 * and disconnected from the social service.
1593 * Closure for @a disconnect_cb.
1596 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1597 const struct GNUNET_PSYC_Environment *env,
1598 GNUNET_ContinuationCallback disconnect_cb,
1601 GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1602 GNUNET_SOCIAL_ANNOUNCE_NONE);
1603 place_leave (&hst->plc);
1604 GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1612 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1616 guest_reconnect (void *cls)
1618 guest_connect (cls);
1623 * Guest client disconnected from service.
1625 * Reconnect after backoff period.
1628 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1630 struct GNUNET_SOCIAL_Guest *gst = cls;
1631 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1633 LOG (GNUNET_ERROR_TYPE_DEBUG,
1634 "Guest client disconnected (%d), re-connecting\n",
1636 if (NULL != plc->tmit)
1638 GNUNET_PSYC_transmit_destroy (plc->tmit);
1641 if (NULL != plc->mq)
1643 GNUNET_MQ_destroy (plc->mq);
1647 plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1650 plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1655 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1657 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1659 struct GNUNET_MQ_MessageHandler handlers[] = {
1660 GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1661 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1662 struct GNUNET_PSYC_CountersResultMessage,
1664 GNUNET_MQ_hd_var_size (guest_enter_decision,
1665 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1666 struct GNUNET_PSYC_JoinDecisionMessage,
1668 GNUNET_MQ_hd_var_size (place_message,
1669 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1670 struct GNUNET_PSYC_MessageHeader,
1672 GNUNET_MQ_hd_fixed_size (place_message_ack,
1673 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1674 struct GNUNET_MessageHeader,
1676 GNUNET_MQ_hd_var_size (place_history_result,
1677 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1678 struct GNUNET_OperationResultMessage,
1680 GNUNET_MQ_hd_var_size (place_state_result,
1681 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1682 struct GNUNET_OperationResultMessage,
1684 GNUNET_MQ_hd_var_size (place_result,
1685 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1686 struct GNUNET_OperationResultMessage,
1688 GNUNET_MQ_handler_end ()
1691 plc->mq = GNUNET_CLIENT_connecT (plc->cfg, "social",
1692 handlers, guest_disconnected, gst);
1693 GNUNET_assert (NULL != plc->mq);
1694 plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1696 GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1700 static struct GNUNET_MQ_Envelope *
1701 guest_enter_request_create (const char *app_id,
1702 const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1703 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1704 const struct GNUNET_PeerIdentity *origin,
1706 const struct GNUNET_PeerIdentity *relays,
1707 const struct GNUNET_PSYC_Message *join_msg)
1709 uint16_t app_id_size = strlen (app_id) + 1;
1710 uint16_t join_msg_size = ntohs (join_msg->header.size);
1711 uint16_t relay_size = relay_count * sizeof (*relays);
1713 struct GuestEnterRequest *greq;
1714 struct GNUNET_MQ_Envelope *
1715 env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1716 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1717 greq->place_pub_key = *place_pub_key;
1718 greq->ego_pub_key = *ego_pub_key;
1719 greq->origin = *origin;
1720 greq->relay_count = htonl (relay_count);
1722 char *p = (char *) &greq[1];
1723 GNUNET_memcpy (p, app_id, app_id_size);
1728 GNUNET_memcpy (p, relays, relay_size);
1732 GNUNET_memcpy (p, join_msg, join_msg_size);
1738 * Request entry to a place as a guest.
1741 * Application handle.
1743 * Identity of the guest.
1744 * @param place_pub_key
1745 * Public key of the place to enter.
1747 * Flags for the entry.
1749 * Peer identity of the origin of the underlying multicast group.
1750 * @param relay_count
1751 * Number of elements in the @a relays array.
1753 * Relays for the underlying multicast group.
1754 * @param method_name
1755 * Method name for the message.
1757 * Environment containing variables for the message, or NULL.
1759 * Payload for the message to give to the enter callback.
1761 * Number of bytes in @a data.
1763 * Slicer to use for processing incoming requests from guests.
1765 * @return NULL on errors, otherwise handle for the guest.
1767 struct GNUNET_SOCIAL_Guest *
1768 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1769 const struct GNUNET_SOCIAL_Ego *ego,
1770 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1771 enum GNUNET_PSYC_SlaveJoinFlags flags,
1772 const struct GNUNET_PeerIdentity *origin,
1773 uint32_t relay_count,
1774 const struct GNUNET_PeerIdentity *relays,
1775 const struct GNUNET_PSYC_Message *entry_msg,
1776 struct GNUNET_PSYC_Slicer *slicer,
1777 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1778 GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1781 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1782 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1784 plc->ego_pub_key = ego->pub_key;
1785 plc->pub_key = *place_pub_key;
1786 plc->cfg = app->cfg;
1787 plc->is_host = GNUNET_NO;
1788 plc->slicer = slicer;
1790 plc->op = GNUNET_OP_create ();
1793 = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1794 origin, relay_count, relays, entry_msg);
1796 gst->enter_cb = local_enter_cb;
1797 gst->entry_dcsn_cb = entry_dcsn_cb;
1800 guest_connect (gst);
1806 * Request entry to a place by name as a guest.
1809 * Application handle.
1811 * Identity of the guest.
1813 * GNS name of the place to enter. Either in the form of
1814 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
1815 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
1816 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
1817 * pseudonym's place directly.
1819 * Password to decrypt the record, or NULL for cleartext records.
1821 * Entry request message or NULL.
1823 * Slicer to use for processing incoming requests from guests.
1824 * @param local_enter_cb
1825 * Called upon connection established to the social service.
1826 * @param entry_decision_cb
1827 * Called upon receiving entry decision.
1829 * @return NULL on errors, otherwise handle for the guest.
1831 struct GNUNET_SOCIAL_Guest *
1832 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1833 const struct GNUNET_SOCIAL_Ego *ego,
1834 const char *gns_name,
1835 const char *password,
1836 const struct GNUNET_PSYC_Message *join_msg,
1837 struct GNUNET_PSYC_Slicer *slicer,
1838 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1839 GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1842 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1843 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1845 if (NULL == password)
1848 uint16_t app_id_size = strlen (app->id) + 1;
1849 uint16_t gns_name_size = strlen (gns_name) + 1;
1850 uint16_t password_size = strlen (password) + 1;
1852 uint16_t join_msg_size = 0;
1853 if (NULL != join_msg)
1854 join_msg_size = ntohs (join_msg->header.size);
1856 struct GuestEnterByNameRequest *greq;
1858 = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1859 + password_size + join_msg_size,
1860 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1862 greq->ego_pub_key = ego->pub_key;
1864 char *p = (char *) &greq[1];
1865 GNUNET_memcpy (p, app->id, app_id_size);
1867 GNUNET_memcpy (p, gns_name, gns_name_size);
1869 GNUNET_memcpy (p, password, password_size);
1871 if (NULL != join_msg)
1872 GNUNET_memcpy (p, join_msg, join_msg_size);
1874 plc->ego_pub_key = ego->pub_key;
1875 plc->cfg = app->cfg;
1876 plc->is_host = GNUNET_NO;
1877 plc->slicer = slicer;
1879 plc->op = GNUNET_OP_create ();
1881 gst->enter_cb = local_enter_cb;
1882 gst->entry_dcsn_cb = entry_decision_cb;
1885 guest_connect (gst);
1891 * Reconnect to an already entered place as guest.
1894 * Guest connection handle.
1895 * @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1897 * Flags for the entry.
1899 * Slicer to use for processing incoming requests from guests.
1900 * @param local_enter_cb
1901 * Called upon connection established to the social service.
1902 * @param entry_decision_cb
1903 * Called upon receiving entry decision.
1905 * @return NULL on errors, otherwise handle for the guest.
1907 struct GNUNET_SOCIAL_Guest *
1908 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1909 enum GNUNET_PSYC_SlaveJoinFlags flags,
1910 struct GNUNET_PSYC_Slicer *slicer,
1911 GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1914 struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1915 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1917 uint16_t app_id_size = strlen (gconn->app->id) + 1;
1918 struct GuestEnterRequest *greq;
1920 = GNUNET_MQ_msg_extra (greq, app_id_size,
1921 GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1922 greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1923 greq->place_pub_key = gconn->plc_msg.place_pub_key;
1924 greq->flags = htonl (flags);
1926 GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1928 plc->cfg = gconn->app->cfg;
1929 plc->is_host = GNUNET_NO;
1930 plc->slicer = slicer;
1931 plc->pub_key = gconn->plc_msg.place_pub_key;
1932 plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1934 plc->op = GNUNET_OP_create ();
1936 gst->enter_cb = local_enter_cb;
1939 guest_connect (gst);
1945 * Talk to the host of the place.
1948 * Place where we want to talk to the host.
1949 * @param method_name
1950 * Method to invoke on the host.
1952 * Environment containing variables for the message, or NULL.
1953 * @param notify_data
1954 * Function to use to get the payload for the method.
1955 * @param notify_data_cls
1956 * Closure for @a notify_data.
1958 * Flags for the message being sent.
1960 * @return NULL if we are already trying to talk to the host,
1961 * otherwise handle to cancel the request.
1963 struct GNUNET_SOCIAL_TalkRequest *
1964 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1965 const char *method_name,
1966 const struct GNUNET_PSYC_Environment *env,
1967 GNUNET_PSYC_TransmitNotifyData notify_data,
1968 void *notify_data_cls,
1969 enum GNUNET_SOCIAL_TalkFlags flags)
1971 struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1972 GNUNET_assert (NULL != plc->tmit);
1975 GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1976 NULL, notify_data, notify_data_cls, flags))
1977 return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1984 * Resume talking to the host of the place.
1987 * Talk request to resume.
1990 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1992 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1997 * Cancel talking to the host of the place.
2000 * Talk request to cancel.
2003 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2005 GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2010 * Disconnect from a place.
2012 * Invalidates guest handle.
2015 * The guest to disconnect.
2018 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2019 GNUNET_ContinuationCallback disconnect_cb,
2022 place_disconnect (&gst->plc, disconnect_cb, cls);
2027 * Leave a place temporarily or permanently.
2029 * Notifies the owner of the place about leaving, and destroys the place handle.
2033 * @param keep_active
2034 * Keep place active after last application disconnected.
2035 * #GNUNET_YES or #GNUNET_NO
2037 * Optional environment for the leave message if @a keep_active
2038 * is #GNUNET_NO. NULL if not needed.
2040 * Called upon disconnecting from the social service.
2043 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2044 struct GNUNET_PSYC_Environment *env,
2045 GNUNET_ContinuationCallback disconnect_cb,
2048 GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2049 GNUNET_SOCIAL_TALK_NONE);
2050 place_leave (&gst->plc);
2051 GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
2056 * Obtain handle for a place entered as guest.
2058 * The returned handle can be used to access the place API.
2060 * @param guest Handle for the guest.
2062 * @return Handle for the place, valid as long as @a guest is valid.
2064 struct GNUNET_SOCIAL_Place *
2065 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2072 * Obtain the public key of a place.
2077 * @return Public key of the place.
2079 const struct GNUNET_CRYPTO_EddsaPublicKey *
2080 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2082 return &plc->pub_key;
2087 * Set message processing @a flags for a @a method_prefix.
2091 * @param method_prefix
2092 * Method prefix @a flags apply to.
2094 * The flags that apply to a matching @a method_prefix.
2097 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2098 const char *method_prefix,
2099 enum GNUNET_SOCIAL_MsgProcFlags flags)
2101 GNUNET_assert (NULL != method_prefix);
2102 struct MsgProcRequest *mpreq;
2103 uint16_t method_size = strnlen (method_prefix,
2104 GNUNET_SERVER_MAX_MESSAGE_SIZE
2105 - sizeof (*mpreq)) + 1;
2106 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2108 struct GNUNET_MQ_Envelope *
2109 env = GNUNET_MQ_msg_extra (mpreq, method_size,
2110 GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2111 mpreq->flags = htonl (flags);
2112 GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2114 GNUNET_MQ_send (plc->mq, env);
2119 * Clear all message processing flags previously set for this place.
2122 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2124 struct GNUNET_MessageHeader *req;
2125 struct GNUNET_MQ_Envelope *
2126 env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2128 GNUNET_MQ_send (plc->mq, env);
2132 static struct GNUNET_SOCIAL_HistoryRequest *
2133 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2134 uint64_t start_message_id,
2135 uint64_t end_message_id,
2136 uint64_t message_limit,
2137 const char *method_prefix,
2139 struct GNUNET_PSYC_Slicer *slicer,
2140 GNUNET_ResultCallback result_cb,
2143 struct GNUNET_PSYC_HistoryRequestMessage *req;
2144 struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2146 hist->slicer = slicer;
2147 hist->result_cb = result_cb;
2149 hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2151 GNUNET_assert (NULL != method_prefix);
2152 uint16_t method_size = strnlen (method_prefix,
2153 GNUNET_SERVER_MAX_MESSAGE_SIZE
2154 - sizeof (*req)) + 1;
2155 GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2157 struct GNUNET_MQ_Envelope *
2158 env = GNUNET_MQ_msg_extra (req, method_size,
2159 GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2160 req->start_message_id = GNUNET_htonll (start_message_id);
2161 req->end_message_id = GNUNET_htonll (end_message_id);
2162 req->message_limit = GNUNET_htonll (message_limit);
2163 req->flags = htonl (flags);
2164 req->op_id = GNUNET_htonll (hist->op_id);
2165 GNUNET_memcpy (&req[1], method_prefix, method_size);
2167 GNUNET_MQ_send (plc->mq, env);
2173 * Learn about the history of a place.
2175 * Messages are returned through the @a slicer function
2176 * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2179 * Place we want to learn more about.
2180 * @param start_message_id
2181 * First historic message we are interested in.
2182 * @param end_message_id
2183 * Last historic message we are interested in (inclusive).
2184 * @param method_prefix
2185 * Only retrieve messages with this method prefix.
2187 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2189 * Slicer to use for retrieved messages.
2190 * Can be the same as the slicer of the place.
2192 * Function called after all messages retrieved.
2193 * NULL if not needed.
2194 * @param cls Closure for @a result_cb.
2196 struct GNUNET_SOCIAL_HistoryRequest *
2197 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2198 uint64_t start_message_id,
2199 uint64_t end_message_id,
2200 const char *method_prefix,
2202 struct GNUNET_PSYC_Slicer *slicer,
2203 GNUNET_ResultCallback result_cb,
2206 return place_history_replay (plc, start_message_id, end_message_id, 0,
2207 method_prefix, flags, slicer, result_cb, cls);
2212 * Learn about the history of a place.
2214 * Sends messages through the slicer function of the place where
2215 * start_message_id <= message_id <= end_message_id.
2216 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2218 * To get the latest message, use 0 for both the start and end message ID.
2221 * Place we want to learn more about.
2222 * @param message_limit
2223 * Maximum number of historic messages we are interested in.
2224 * @param method_prefix
2225 * Only retrieve messages with this method prefix.
2227 * OR'ed GNUNET_PSYC_HistoryReplayFlags
2229 * Function called after all messages retrieved.
2230 * NULL if not needed.
2231 * @param cls Closure for @a result_cb.
2233 struct GNUNET_SOCIAL_HistoryRequest *
2234 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2235 uint64_t message_limit,
2236 const char *method_prefix,
2238 struct GNUNET_PSYC_Slicer *slicer,
2239 GNUNET_ResultCallback result_cb,
2242 return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2243 slicer, result_cb, cls);
2248 * Cancel learning about the history of a place.
2251 * History lesson to cancel.
2254 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2256 GNUNET_OP_remove (hist->plc->op, hist->op_id);
2262 * Request matching state variables.
2264 static struct GNUNET_SOCIAL_LookHandle *
2265 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2266 uint16_t type, const char *name,
2267 GNUNET_PSYC_StateVarCallback var_cb,
2268 GNUNET_ResultCallback result_cb, void *cls)
2270 struct GNUNET_PSYC_StateRequestMessage *req;
2271 struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2273 look->var_cb = var_cb;
2274 look->result_cb = result_cb;
2276 look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2278 GNUNET_assert (NULL != name);
2279 size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2280 - sizeof (*req)) + 1;
2281 struct GNUNET_MQ_Envelope *
2282 env = GNUNET_MQ_msg_extra (req, name_size, type);
2283 req->op_id = GNUNET_htonll (look->op_id);
2284 GNUNET_memcpy (&req[1], name, name_size);
2286 GNUNET_MQ_send (plc->mq, env);
2292 * Look at a particular object in the place.
2294 * The best matching object is returned (its name might be less specific than
2295 * what was requested).
2298 * The place where to look.
2300 * Full name of the object.
2302 * Set to the size of the returned value.
2304 * @return NULL if there is no such object at this place.
2306 struct GNUNET_SOCIAL_LookHandle *
2307 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2308 const char *full_name,
2309 GNUNET_PSYC_StateVarCallback var_cb,
2310 GNUNET_ResultCallback result_cb,
2313 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2314 full_name, var_cb, result_cb, cls);
2319 * Look for objects in the place with a matching name prefix.
2322 * The place where to look.
2323 * @param name_prefix
2324 * Look at objects with names beginning with this value.
2326 * Function to call for each object found.
2328 * Closure for callback function.
2330 * @return Handle that can be used to stop looking at objects.
2332 struct GNUNET_SOCIAL_LookHandle *
2333 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2334 const char *name_prefix,
2335 GNUNET_PSYC_StateVarCallback var_cb,
2336 GNUNET_ResultCallback result_cb,
2339 return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2340 name_prefix, var_cb, result_cb, cls);
2345 * Cancel a state request operation.
2348 * Handle for the operation to cancel.
2351 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2353 GNUNET_OP_remove (look->plc->op, look->op_id);
2359 op_recv_zone_add_place_result (void *cls, int64_t result,
2360 const void *err_msg, uint16_t err_msg_size)
2362 LOG (GNUNET_ERROR_TYPE_DEBUG,
2363 "Received zone add place result: %" PRId64 ".\n", result);
2365 struct ZoneAddPlaceHandle *add_plc = cls;
2366 if (NULL != add_plc->result_cb)
2367 add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2369 GNUNET_free (add_plc);
2374 * Advertise @e place in the GNS zone of @e ego.
2377 * Application handle.
2380 * @param place_pub_key
2381 * Public key of place to add.
2383 * The name for the PLACE record to put in the zone.
2385 * Password used to encrypt the record or NULL to keep it cleartext.
2386 * @param relay_count
2387 * Number of elements in the @a relays array.
2389 * List of relays to put in the PLACE record to advertise
2390 * as entry points to the place in addition to the origin.
2391 * @param expiration_time
2392 * Expiration time of the record, use 0 to remove the record.
2394 * Function called with the result of the operation.
2396 * Closure for @a result_cb
2398 * @return #GNUNET_OK if the request was sent,
2399 * #GNUNET_SYSERR on error, e.g. the name/password is too long.
2402 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2403 const struct GNUNET_SOCIAL_Ego *ego,
2405 const char *password,
2406 const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2407 const struct GNUNET_PeerIdentity *origin,
2408 uint32_t relay_count,
2409 const struct GNUNET_PeerIdentity *relays,
2410 struct GNUNET_TIME_Absolute expiration_time,
2411 GNUNET_ResultCallback result_cb,
2414 struct ZoneAddPlaceRequest *preq;
2415 size_t name_size = strlen (name) + 1;
2416 size_t password_size = strlen (password) + 1;
2417 size_t relay_size = relay_count * sizeof (*relays);
2418 size_t payload_size = name_size + password_size + relay_size;
2420 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2421 return GNUNET_SYSERR;
2423 struct GNUNET_MQ_Envelope *
2424 env = GNUNET_MQ_msg_extra (preq, payload_size,
2425 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2426 preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2427 preq->ego_pub_key = ego->pub_key;
2428 preq->place_pub_key = *place_pub_key;
2429 preq->origin = *origin;
2430 preq->relay_count = htonl (relay_count);
2432 char *p = (char *) &preq[1];
2433 GNUNET_memcpy (p, name, name_size);
2435 GNUNET_memcpy (p, password, password_size);
2437 GNUNET_memcpy (p, relays, relay_size);
2439 struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2440 add_plc->result_cb = result_cb;
2441 add_plc->result_cls = result_cls;
2443 preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2444 op_recv_zone_add_place_result,
2447 GNUNET_MQ_send (app->mq, env);
2453 op_recv_zone_add_nym_result (void *cls, int64_t result,
2454 const void *err_msg, uint16_t err_msg_size)
2456 LOG (GNUNET_ERROR_TYPE_DEBUG,
2457 "Received zone add nym result: %" PRId64 ".\n", result);
2459 struct ZoneAddNymHandle *add_nym = cls;
2460 if (NULL != add_nym->result_cb)
2461 add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2463 GNUNET_free (add_nym);
2468 * Add nym to the GNS zone of @e ego.
2475 * The name for the PKEY record to put in the zone.
2476 * @param nym_pub_key
2477 * Public key of nym to add.
2478 * @param expiration_time
2479 * Expiration time of the record, use 0 to remove the record.
2481 * Function called with the result of the operation.
2483 * Closure for @a result_cb
2485 * @return #GNUNET_OK if the request was sent,
2486 * #GNUNET_SYSERR on error, e.g. the name is too long.
2489 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2490 const struct GNUNET_SOCIAL_Ego *ego,
2492 const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2493 struct GNUNET_TIME_Absolute expiration_time,
2494 GNUNET_ResultCallback result_cb,
2497 struct ZoneAddNymRequest *nreq;
2499 size_t name_size = strlen (name) + 1;
2500 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2501 return GNUNET_SYSERR;
2503 struct GNUNET_MQ_Envelope *
2504 env = GNUNET_MQ_msg_extra (nreq, name_size,
2505 GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2506 nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2507 nreq->ego_pub_key = ego->pub_key;
2508 nreq->nym_pub_key = *nym_pub_key;
2509 GNUNET_memcpy (&nreq[1], name, name_size);
2511 struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2512 add_nym->result_cb = result_cb;
2513 add_nym->result_cls = result_cls;
2515 nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2516 op_recv_zone_add_nym_result,
2519 GNUNET_MQ_send (app->mq, env);
2528 app_connect (struct GNUNET_SOCIAL_App *app);
2532 app_reconnect (void *cls)
2539 * App client disconnected from service.
2541 * Reconnect after backoff period.
2544 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2546 struct GNUNET_SOCIAL_App *app = cls;
2548 LOG (GNUNET_ERROR_TYPE_DEBUG,
2549 "App client disconnected (%d), re-connecting\n",
2551 if (NULL != app->mq)
2553 GNUNET_MQ_destroy (app->mq);
2557 app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2560 app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2565 app_connect (struct GNUNET_SOCIAL_App *app)
2567 struct GNUNET_MQ_MessageHandler handlers[] = {
2568 GNUNET_MQ_hd_var_size (app_ego,
2569 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2570 struct AppEgoMessage,
2572 GNUNET_MQ_hd_fixed_size (app_ego_end,
2573 GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2574 struct GNUNET_MessageHeader,
2576 GNUNET_MQ_hd_var_size (app_place,
2577 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2578 struct AppPlaceMessage,
2580 GNUNET_MQ_hd_fixed_size (app_place_end,
2581 GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2582 struct GNUNET_MessageHeader,
2584 GNUNET_MQ_hd_var_size (app_result,
2585 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2586 struct GNUNET_OperationResultMessage,
2588 GNUNET_MQ_handler_end ()
2591 app->mq = GNUNET_CLIENT_connecT (app->cfg, "social",
2592 handlers, app_disconnected, app);
2593 GNUNET_assert (NULL != app->mq);
2594 GNUNET_MQ_send_copy (app->mq, app->connect_env);
2599 * Connect application to the social service.
2601 * The @host_place_cb and @guest_place_cb functions are
2602 * initially called for each entered places,
2603 * then later each time a new place is entered with the current application ID.
2610 * Function to notify about an available ego.
2612 * Function to notify about a place entered as host.
2614 * Function to notify about a place entered as guest.
2616 * Closure for the callbacks.
2618 * @return Handle that can be used to stop listening.
2620 struct GNUNET_SOCIAL_App *
2621 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2623 GNUNET_SOCIAL_AppEgoCallback ego_cb,
2624 GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2625 GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2626 GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2629 uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2630 if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2634 struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2636 app->ego_cb = ego_cb;
2637 app->host_cb = host_cb;
2638 app->guest_cb = guest_cb;
2639 app->connected_cb = connected_cb;
2641 app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2642 app->op = GNUNET_OP_create ();
2643 app->id = GNUNET_malloc (app_id_size);
2644 GNUNET_memcpy (app->id, id, app_id_size);
2646 struct AppConnectRequest *creq;
2647 app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2648 GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2649 GNUNET_memcpy (&creq[1], app->id, app_id_size);
2657 app_cleanup (struct GNUNET_SOCIAL_App *app)
2659 if (NULL != app->mq)
2661 GNUNET_MQ_destroy (app->mq);
2664 if (NULL != app->disconnect_cb)
2666 app->disconnect_cb (app->disconnect_cls);
2667 app->disconnect_cb = NULL;
2673 * Disconnect application.
2676 * Application handle.
2677 * @param disconnect_cb
2678 * Disconnect callback.
2679 * @param disconnect_cls
2680 * Disconnect closure.
2683 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2684 GNUNET_ContinuationCallback disconnect_cb,
2685 void *disconnect_cls)
2687 app->disconnect_cb = disconnect_cb;
2688 app->disconnect_cls = disconnect_cls;
2690 if (NULL != app->mq)
2692 struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2695 GNUNET_MQ_notify_sent (env, (GNUNET_MQ_NotifyCallback) app_cleanup, app);
2710 * Detach application from a place.
2712 * Removes the place from the entered places list for this application.
2713 * Note: this does not disconnect from the place.
2715 * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2723 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2724 struct GNUNET_SOCIAL_Place *plc)
2726 struct AppDetachRequest *dreq;
2727 struct GNUNET_MQ_Envelope *
2728 env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2729 dreq->place_pub_key = plc->pub_key;
2731 GNUNET_MQ_send (app->mq, env);
2735 /* end of social_api.c */