f78cb9a85c52a2a8e547316837705349ce0bda83
[oweals/gnunet.git] / src / social / social_api.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2013 GNUnet e.V.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * @author Gabor X Toth
23  *
24  * @file
25  * Social service; implements social interactions using the PSYC service.
26  */
27
28 #include <inttypes.h>
29 #include <string.h>
30
31 #include "platform.h"
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"
36 #include "social.h"
37
38 #define LOG(kind,...) GNUNET_log_from (kind, "social-api",__VA_ARGS__)
39
40 /**
41  * Handle for an ego.
42  */
43 struct GNUNET_SOCIAL_Ego
44 {
45   struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
46   struct GNUNET_HashCode pub_key_hash;
47   char *name;
48 };
49
50
51 /**
52  * Handle for a pseudonym of another user in the network.
53  */
54 struct GNUNET_SOCIAL_Nym
55 {
56   struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
57   struct GNUNET_HashCode pub_key_hash;
58 };
59
60
61 /**
62  * Handle for an application.
63  */
64 struct GNUNET_SOCIAL_App
65 {
66   /**
67    * Configuration to use.
68    */
69   const struct GNUNET_CONFIGURATION_Handle *cfg;
70
71   /**
72    * Client connection to the service.
73    */
74   struct GNUNET_MQ_Handle *mq;
75
76   /**
77    * Message to send on connect.
78    */
79   struct GNUNET_MQ_Envelope *connect_env;
80
81   /**
82    * Time to wait until we try to reconnect on failure.
83    */
84   struct GNUNET_TIME_Relative reconnect_delay;
85
86   /**
87    * Task for reconnecting when the listener fails.
88    */
89   struct GNUNET_SCHEDULER_Task *reconnect_task;
90
91   /**
92    * Async operations.
93    */
94   struct GNUNET_OP_Handle *op;
95
96   /**
97    * Function called after disconnected from the service.
98    */
99   GNUNET_ContinuationCallback disconnect_cb;
100
101   /**
102    * Closure for @a disconnect_cb.
103    */
104   void *disconnect_cls;
105
106   /**
107    * Application ID.
108    */
109   char *id;
110
111   /**
112    * Hash map of all egos.
113    * pub_key_hash -> struct GNUNET_SOCIAL_Ego *
114    */
115   struct GNUNET_CONTAINER_MultiHashMap *egos;
116
117   GNUNET_SOCIAL_AppEgoCallback ego_cb;
118   GNUNET_SOCIAL_AppHostPlaceCallback host_cb;
119   GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb;
120   GNUNET_SOCIAL_AppConnectedCallback connected_cb;
121   void *cb_cls;
122 };
123
124
125 struct GNUNET_SOCIAL_HostConnection
126 {
127   struct GNUNET_SOCIAL_App *app;
128
129   struct AppPlaceMessage plc_msg;
130 };
131
132
133 struct GNUNET_SOCIAL_GuestConnection
134 {
135   struct GNUNET_SOCIAL_App *app;
136
137   struct AppPlaceMessage plc_msg;
138 };
139
140
141 /**
142  * Handle for a place where social interactions happen.
143  */
144 struct GNUNET_SOCIAL_Place
145 {
146   /**
147    * Configuration to use.
148    */
149   const struct GNUNET_CONFIGURATION_Handle *cfg;
150
151   /**
152    * Client connection to the service.
153    */
154   struct GNUNET_MQ_Handle *mq;
155
156   /**
157    * Message to send on connect.
158    */
159   struct GNUNET_MQ_Envelope *connect_env;
160
161   /**
162    * Time to wait until we try to reconnect on failure.
163    */
164   struct GNUNET_TIME_Relative reconnect_delay;
165
166   /**
167    * Task for reconnecting when the listener fails.
168    */
169   struct GNUNET_SCHEDULER_Task *reconnect_task;
170
171   /**
172    * Async operations.
173    */
174   struct GNUNET_OP_Handle *op;
175
176   /**
177    * Transmission handle.
178    */
179   struct GNUNET_PSYC_TransmitHandle *tmit;
180
181   /**
182    * Slicer for processing incoming messages.
183    */
184   struct GNUNET_PSYC_Slicer *slicer;
185
186   /**
187    * Function called after disconnected from the service.
188    */
189   GNUNET_ContinuationCallback disconnect_cb;
190
191   /**
192    * Closure for @a disconnect_cb.
193    */
194   void *disconnect_cls;
195
196   /**
197    * Public key of the place.
198    */
199   struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
200
201   /**
202    * Public key of the ego.
203    */
204   struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
205
206   /**
207    * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
208    */
209   uint8_t is_host;
210 };
211
212
213 /**
214  * Host handle for a place that we entered.
215  */
216 struct GNUNET_SOCIAL_Host
217 {
218   struct GNUNET_SOCIAL_Place plc;
219
220   /**
221    * Slicer for processing incoming messages from guests.
222    */
223   struct GNUNET_PSYC_Slicer *slicer;
224
225   GNUNET_SOCIAL_HostEnterCallback enter_cb;
226
227   GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
228
229   GNUNET_SOCIAL_FarewellCallback farewell_cb;
230
231   /**
232    * Closure for callbacks.
233    */
234   void *cb_cls;
235
236   struct GNUNET_SOCIAL_Nym *notice_place_leave_nym;
237   struct GNUNET_PSYC_Environment *notice_place_leave_env;
238 };
239
240
241 /**
242  * Guest handle for place that we entered.
243  */
244 struct GNUNET_SOCIAL_Guest
245 {
246   struct GNUNET_SOCIAL_Place plc;
247
248   GNUNET_SOCIAL_GuestEnterCallback enter_cb;
249
250   GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
251
252   /**
253    * Closure for callbacks.
254    */
255   void *cb_cls;
256 };
257
258
259 /**
260  * Hash map of all nyms.
261  * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
262  */
263 struct GNUNET_CONTAINER_MultiHashMap *nyms;
264
265
266 /**
267  * Handle for an announcement request.
268  */
269 struct GNUNET_SOCIAL_Announcement
270 {
271
272 };
273
274
275 /**
276  * A talk request.
277  */
278 struct GNUNET_SOCIAL_TalkRequest
279 {
280
281 };
282
283
284 /**
285  * A history lesson.
286  */
287 struct GNUNET_SOCIAL_HistoryRequest
288 {
289   /**
290    * Place.
291    */
292   struct GNUNET_SOCIAL_Place *plc;
293
294   /**
295    * Operation ID.
296    */
297   uint64_t op_id;
298
299   /**
300    * Slicer for processing incoming messages.
301    */
302   struct GNUNET_PSYC_Slicer *slicer;
303
304   /**
305    * Function to call when the operation finished.
306    */
307   GNUNET_ResultCallback result_cb;
308
309   /**
310    * Closure for @a result_cb.
311    */
312   void *cls;
313 };
314
315
316 struct GNUNET_SOCIAL_LookHandle
317 {
318   /**
319    * Place.
320    */
321   struct GNUNET_SOCIAL_Place *plc;
322
323   /**
324    * Operation ID.
325    */
326   uint64_t op_id;
327
328   /**
329    * State variable result callback.
330    */
331   GNUNET_PSYC_StateVarCallback var_cb;
332
333   /**
334    * Function to call when the operation finished.
335    */
336   GNUNET_ResultCallback result_cb;
337
338   /**
339    * Name of current modifier being received.
340    */
341   char *mod_name;
342
343   /**
344    * Size of current modifier value being received.
345    */
346   size_t mod_value_size;
347
348   /**
349    * Remaining size of current modifier value still to be received.
350    */
351   size_t mod_value_remaining;
352
353   /**
354    * Closure for @a result_cb.
355    */
356   void *cls;
357 };
358
359
360 struct ZoneAddPlaceHandle
361 {
362   GNUNET_ResultCallback result_cb;
363   void *result_cls;
364 };
365
366
367 struct ZoneAddNymHandle
368 {
369   GNUNET_ResultCallback result_cb;
370   void *result_cls;
371 };
372
373
374 /*** NYM ***/
375
376 static struct GNUNET_SOCIAL_Nym *
377 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
378 {
379   struct GNUNET_SOCIAL_Nym *nym = NULL;
380   struct GNUNET_HashCode pub_key_hash;
381
382   if (NULL == pub_key)
383     return NULL;
384
385   GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
386
387   if (NULL == nyms)
388     nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
389   else
390     nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
391
392   if (NULL == nym)
393   {
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);
399   }
400   return nym;
401 }
402
403
404 static void
405 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
406 {
407   GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
408   GNUNET_free (nym);
409 }
410
411
412 /*** MESSAGE HANDLERS ***/
413
414 /** _notice_place_leave from guests */
415
416 static void
417 host_recv_notice_place_leave_method (void *cls,
418                                      const struct GNUNET_PSYC_MessageHeader *msg,
419                                      const struct GNUNET_PSYC_MessageMethod *meth,
420                                      uint64_t message_id,
421                                      const char *method_name)
422 {
423   struct GNUNET_SOCIAL_Host *hst = cls;
424
425   if (0 == memcmp (&(struct GNUNET_CRYPTO_EcdsaPublicKey) {},
426                    &msg->slave_pub_key, sizeof (msg->slave_pub_key)))
427     return;
428
429   struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&msg->slave_pub_key);
430
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);
434
435   hst->notice_place_leave_nym = (struct GNUNET_SOCIAL_Nym *) nym;
436   hst->notice_place_leave_env = GNUNET_PSYC_env_create ();
437
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);
442   GNUNET_free (str);
443 }
444
445
446 static void
447 host_recv_notice_place_leave_modifier (void *cls,
448                                        const struct GNUNET_PSYC_MessageHeader *msg,
449                                        const struct GNUNET_MessageHeader *pmsg,
450                                        uint64_t message_id,
451                                        enum GNUNET_PSYC_Operator oper,
452                                        const char *name,
453                                        const void *value,
454                                        uint16_t value_size,
455                                        uint16_t full_value_size)
456 {
457   struct GNUNET_SOCIAL_Host *hst = cls;
458   if (NULL == hst->notice_place_leave_env)
459     return;
460
461   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
462               "Host received modifier for _notice_place_leave message with ID %" PRIu64 ":\n"
463               "%c%s: %.*s\n",
464               message_id, oper, name, value_size, (const char *) value);
465
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))
469     return;
470
471   GNUNET_PSYC_env_add (hst->notice_place_leave_env,
472                        GNUNET_PSYC_OP_SET, name, value, value_size);
473 }
474
475
476 static void
477 host_recv_notice_place_leave_eom (void *cls,
478                                   const struct GNUNET_PSYC_MessageHeader *msg,
479                                   const struct GNUNET_MessageHeader *pmsg,
480                                   uint64_t message_id,
481                                   uint8_t is_cancelled)
482 {
483   struct GNUNET_SOCIAL_Host *hst = cls;
484   if (NULL == hst->notice_place_leave_env)
485     return;
486
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);
491   GNUNET_free (str);
492
493   if (GNUNET_YES != is_cancelled)
494   {
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);
506   }
507   GNUNET_PSYC_env_destroy (hst->notice_place_leave_env);
508   hst->notice_place_leave_env = NULL;
509 }
510
511
512 /*** PLACE ***/
513
514
515 static int
516 check_place_result (void *cls,
517                     const struct GNUNET_OperationResultMessage *res)
518 {
519   uint16_t size = ntohs (res->header.size);
520   if (size < sizeof (*res))
521   { /* Error, message too small. */
522     GNUNET_break (0);
523     return GNUNET_SYSERR;
524   }
525   return GNUNET_OK;
526 }
527
528
529 static void
530 handle_place_result (void *cls,
531                      const struct GNUNET_OperationResultMessage *res)
532 {
533   struct GNUNET_SOCIAL_Place *plc = cls;
534
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;
538
539   GNUNET_OP_result (plc->op, GNUNET_ntohll (res->op_id),
540                     GNUNET_ntohll (res->result_code),
541                     data, data_size, NULL);
542 }
543
544
545 static int
546 check_app_result (void *cls,
547                   const struct GNUNET_OperationResultMessage *res)
548 {
549   uint16_t size = ntohs (res->header.size);
550   if (size < sizeof (*res))
551   { /* Error, message too small. */
552     GNUNET_break (0);
553     return GNUNET_SYSERR;
554   }
555   return GNUNET_OK;
556 }
557
558
559 static void
560 handle_app_result (void *cls,
561                    const struct GNUNET_OperationResultMessage *res)
562 {
563   struct GNUNET_SOCIAL_App *app = cls;
564
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;
568
569   GNUNET_OP_result (app->op, GNUNET_ntohll (res->op_id),
570                     GNUNET_ntohll (res->result_code),
571                     data, data_size, NULL);
572 }
573
574
575 static void
576 op_recv_history_result (void *cls, int64_t result,
577                         const void *err_msg, uint16_t err_msg_size)
578 {
579   LOG (GNUNET_ERROR_TYPE_DEBUG,
580        "Received history replay result: %" PRId64 ".\n", result);
581
582   struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
583
584   if (NULL != hist->result_cb)
585     hist->result_cb (hist->cls, result, err_msg, err_msg_size);
586
587   GNUNET_free (hist);
588 }
589
590
591 static void
592 op_recv_state_result (void *cls, int64_t result,
593                       const void *err_msg, uint16_t err_msg_size)
594 {
595   LOG (GNUNET_ERROR_TYPE_DEBUG,
596        "Received state request result: %" PRId64 ".\n", result);
597
598   struct GNUNET_SOCIAL_LookHandle *look = cls;
599
600   if (NULL != look->result_cb)
601     look->result_cb (look->cls, result, err_msg, err_msg_size);
602
603   GNUNET_free (look);
604 }
605
606
607 static int
608 check_place_history_result (void *cls,
609                             const struct GNUNET_OperationResultMessage *res)
610 {
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);
614
615   if (NULL == pmsg || size < sizeof (*res) + sizeof (*pmsg))
616   { /* Error, message too small. */
617     GNUNET_break (0);
618     return GNUNET_SYSERR;
619   }
620   return GNUNET_OK;
621 }
622
623
624 static void
625 handle_place_history_result (void *cls,
626                              const struct GNUNET_OperationResultMessage *res)
627 {
628   struct GNUNET_SOCIAL_Place *plc = cls;
629   struct GNUNET_PSYC_MessageHeader *
630     pmsg = (struct GNUNET_PSYC_MessageHeader *) GNUNET_MQ_extract_nested_mh (res);
631
632   LOG (GNUNET_ERROR_TYPE_DEBUG,
633        "%p Received historic fragment for message #%" PRIu64 ".\n",
634        plc, GNUNET_ntohll (pmsg->message_id));
635
636   GNUNET_ResultCallback result_cb = NULL;
637   struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
638
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 #%"
645          PRIu64 ".\n",
646          plc, GNUNET_ntohll (pmsg->message_id));
647     return;
648   }
649
650   GNUNET_PSYC_slicer_message (hist->slicer,
651                               (const struct GNUNET_PSYC_MessageHeader *) pmsg);
652 }
653
654
655 static int
656 check_place_state_result (void *cls,
657                           const struct GNUNET_OperationResultMessage *res)
658 {
659   const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
660   if (NULL == mod)
661   {
662     GNUNET_break_op (0);
663     LOG (GNUNET_ERROR_TYPE_WARNING,
664          "Invalid modifier in state result\n");
665     return GNUNET_SYSERR;
666   }
667
668   uint16_t size = ntohs (res->header.size);
669   uint16_t mod_size = ntohs (mod->size);
670   if (size - sizeof (*res) != mod_size)
671   {
672     GNUNET_break_op (0);
673     LOG (GNUNET_ERROR_TYPE_WARNING,
674          "Invalid modifier size in state result: %u - %u != %u\n",
675          ntohs (res->header.size), sizeof (*res), mod_size);
676     return GNUNET_SYSERR;
677   }
678   return GNUNET_OK;
679 }
680
681
682 static void
683 handle_place_state_result (void *cls,
684                            const struct GNUNET_OperationResultMessage *res)
685 {
686   struct GNUNET_SOCIAL_Place *plc = cls;
687
688   GNUNET_ResultCallback result_cb = NULL;
689   struct GNUNET_SOCIAL_LookHandle *look = NULL;
690
691   if (GNUNET_YES != GNUNET_OP_get (plc->op,
692                                    GNUNET_ntohll (res->op_id),
693                                    &result_cb, (void *) &look, NULL))
694   { /* Operation not found. */
695     return;
696   }
697
698   const struct GNUNET_MessageHeader *mod = GNUNET_MQ_extract_nested_mh (res);
699   uint16_t mod_size = ntohs (mod->size);
700
701   switch (ntohs (mod->type))
702   {
703   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
704   {
705     const struct GNUNET_PSYC_MessageModifier *
706       pmod = (const struct GNUNET_PSYC_MessageModifier *) mod;
707
708     const char *name = (const char *) &pmod[1];
709     uint16_t name_size = ntohs (pmod->name_size);
710     if (0 == name_size
711         || mod_size - sizeof (*pmod) < name_size
712         || '\0' != name[name_size - 1])
713     {
714       GNUNET_break_op (0);
715       LOG (GNUNET_ERROR_TYPE_WARNING,
716            "Invalid modifier name in state result\n");
717       return;
718     }
719     look->mod_value_size = ntohs (pmod->value_size);
720     look->var_cb (look->cls, mod, name, name + name_size,
721                   mod_size - sizeof (*mod) - name_size,
722                   look->mod_value_size);
723     if (look->mod_value_size > mod_size - sizeof (*mod) - name_size)
724     {
725         look->mod_value_remaining = look->mod_value_size;
726         look->mod_name = GNUNET_malloc (name_size);
727         GNUNET_memcpy (look->mod_name, name, name_size);
728     }
729     break;
730   }
731
732   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
733     look->var_cb (look->cls, mod, look->mod_name, (const char *) &mod[1],
734                   mod_size - sizeof (*mod), look->mod_value_size);
735     look->mod_value_remaining -= mod_size - sizeof (*mod);
736     if (0 == look->mod_value_remaining)
737     {
738         GNUNET_free (look->mod_name);
739     }
740     break;
741   }
742 }
743
744
745 static void
746 handle_place_message_ack (void *cls,
747                           const struct GNUNET_MessageHeader *msg)
748 {
749   struct GNUNET_SOCIAL_Place *plc = cls;
750
751   GNUNET_PSYC_transmit_got_ack (plc->tmit);
752 }
753
754
755 static int
756 check_place_message (void *cls,
757                      const struct GNUNET_PSYC_MessageHeader *pmsg)
758 {
759   return GNUNET_OK;
760 }
761
762
763 static void
764 handle_place_message (void *cls,
765                       const struct GNUNET_PSYC_MessageHeader *pmsg)
766 {
767   struct GNUNET_SOCIAL_Place *plc = cls;
768
769   GNUNET_PSYC_slicer_message (plc->slicer, pmsg);
770 }
771
772
773 static int
774 check_host_message (void *cls,
775                     const struct GNUNET_PSYC_MessageHeader *pmsg)
776 {
777   return GNUNET_OK;
778 }
779
780
781 static void
782 handle_host_message (void *cls,
783                      const struct GNUNET_PSYC_MessageHeader *pmsg)
784 {
785   struct GNUNET_SOCIAL_Host *hst = cls;
786
787   GNUNET_PSYC_slicer_message (hst->slicer, pmsg);
788   GNUNET_PSYC_slicer_message (hst->plc.slicer, pmsg);
789 }
790
791
792 static void
793 handle_host_enter_ack (void *cls,
794                        const struct HostEnterAck *hack)
795 {
796   struct GNUNET_SOCIAL_Host *hst = cls;
797
798   hst->plc.pub_key = hack->place_pub_key;
799
800   int32_t result = ntohl (hack->result_code);
801   if (NULL != hst->enter_cb)
802     hst->enter_cb (hst->cb_cls, result, &hack->place_pub_key,
803                    GNUNET_ntohll (hack->max_message_id));
804 }
805
806
807 static int
808 check_host_enter_request (void *cls,
809                           const struct GNUNET_PSYC_JoinRequestMessage *req)
810 {
811   return GNUNET_OK;
812 }
813
814
815 static void
816 handle_host_enter_request (void *cls,
817                            const struct GNUNET_PSYC_JoinRequestMessage *req)
818 {
819   struct GNUNET_SOCIAL_Host *hst = cls;
820
821   if (NULL == hst->answer_door_cb)
822      return;
823
824   const char *method_name = NULL;
825   struct GNUNET_PSYC_Environment *env = NULL;
826   struct GNUNET_PSYC_MessageHeader *entry_pmsg = NULL;
827   const void *data = NULL;
828   uint16_t data_size = 0;
829   char *str;
830   const struct GNUNET_PSYC_Message *join_msg = NULL;
831
832   do
833   {
834     if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
835     {
836       join_msg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (req);
837       LOG (GNUNET_ERROR_TYPE_DEBUG,
838            "Received join_msg of type %u and size %u.\n",
839            ntohs (join_msg->header.type), ntohs (join_msg->header.size));
840
841       env = GNUNET_PSYC_env_create ();
842       entry_pmsg = GNUNET_PSYC_message_header_create_from_psyc (join_msg);
843       if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_pmsg, &method_name, env,
844                                                   &data, &data_size))
845       {
846         GNUNET_break_op (0);
847         str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_pub_key);
848         LOG (GNUNET_ERROR_TYPE_WARNING,
849              "Ignoring invalid entry request from nym %s.\n",
850              str);
851         GNUNET_free (str);
852         break;
853       }
854     }
855
856     struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_pub_key);
857     hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
858                          data, data_size);
859   } while (0);
860
861   if (NULL != env)
862     GNUNET_PSYC_env_destroy (env);
863   if (NULL != entry_pmsg)
864     GNUNET_free (entry_pmsg);
865 }
866
867
868 static void
869 handle_guest_enter_ack (void *cls,
870                         const struct GNUNET_PSYC_CountersResultMessage *cres)
871 {
872   struct GNUNET_SOCIAL_Guest *gst = cls;
873
874   int32_t result = ntohl (cres->result_code);
875   if (NULL != gst->enter_cb)
876     gst->enter_cb (gst->cb_cls, result, &gst->plc.pub_key,
877                    GNUNET_ntohll (cres->max_message_id));
878 }
879
880
881 static int
882 check_guest_enter_decision (void *cls,
883                             const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
884 {
885   return GNUNET_OK;
886 }
887
888
889 static void
890 handle_guest_enter_decision (void *cls,
891                              const struct GNUNET_PSYC_JoinDecisionMessage *dcsn)
892 {
893   struct GNUNET_SOCIAL_Guest *gst = cls;
894
895   struct GNUNET_PSYC_Message *pmsg = NULL;
896   if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
897     pmsg = (struct GNUNET_PSYC_Message *) GNUNET_MQ_extract_nested_mh (dcsn);
898
899   if (NULL != gst->entry_dcsn_cb)
900     gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
901 }
902
903
904 static int
905 check_app_ego (void *cls,
906                const struct AppEgoMessage *emsg)
907 {
908   return GNUNET_OK;
909 }
910
911
912 static void
913 handle_app_ego (void *cls,
914                 const struct AppEgoMessage *emsg)
915 {
916   struct GNUNET_SOCIAL_App *app = cls;
917
918   uint16_t name_size = ntohs (emsg->header.size) - sizeof (*emsg);
919
920   struct GNUNET_HashCode ego_pub_hash;
921   GNUNET_CRYPTO_hash (&emsg->ego_pub_key, sizeof (emsg->ego_pub_key),
922                       &ego_pub_hash);
923
924   struct GNUNET_SOCIAL_Ego *
925     ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
926   if (NULL == ego)
927   {
928     ego = GNUNET_malloc (sizeof (*ego));
929     ego->pub_key = emsg->ego_pub_key;
930     ego->name = GNUNET_malloc (name_size);
931     GNUNET_memcpy (ego->name, &emsg[1], name_size);
932   }
933   else
934   {
935     ego->name = GNUNET_realloc (ego->name, name_size);
936     GNUNET_memcpy (ego->name, &emsg[1], name_size);
937   }
938
939   GNUNET_CONTAINER_multihashmap_put (app->egos, &ego_pub_hash, ego,
940                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
941
942   if (NULL != app->ego_cb)
943     app->ego_cb (app->cb_cls, ego, &ego->pub_key, ego->name);
944 }
945
946
947 static void
948 handle_app_ego_end (void *cls,
949                     const struct GNUNET_MessageHeader *msg)
950 {
951   //struct GNUNET_SOCIAL_App *app = cls;
952 }
953
954
955 static int
956 check_app_place (void *cls,
957                  const struct AppPlaceMessage *pmsg)
958 {
959   return GNUNET_OK;
960 }
961
962
963 static void
964 handle_app_place (void *cls,
965                   const struct AppPlaceMessage *pmsg)
966 {
967   struct GNUNET_SOCIAL_App *app = cls;
968
969   if ((GNUNET_YES == pmsg->is_host && NULL == app->host_cb)
970       || (GNUNET_NO == pmsg->is_host && NULL == app->guest_cb))
971     return;
972
973   struct GNUNET_HashCode ego_pub_hash;
974   GNUNET_CRYPTO_hash (&pmsg->ego_pub_key, sizeof (pmsg->ego_pub_key),
975                       &ego_pub_hash);
976   struct GNUNET_SOCIAL_Ego *
977     ego = GNUNET_CONTAINER_multihashmap_get (app->egos, &ego_pub_hash);
978   if (NULL == ego)
979   {
980     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failure to obtain ego %s.\n",
981                 GNUNET_h2s (&ego_pub_hash));
982     GNUNET_break (0);
983     return;
984   }
985
986   if (GNUNET_YES == pmsg->is_host)
987   {
988     if (NULL != app->host_cb) {
989       struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
990       hconn->app = app;
991       hconn->plc_msg = *pmsg;
992       app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
993       GNUNET_free (hconn);
994     }
995   }
996   else if (NULL != app->guest_cb)
997   {
998     struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
999     gconn->app = app;
1000     gconn->plc_msg = *pmsg;
1001     app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
1002     GNUNET_free (gconn);
1003   }
1004 }
1005
1006
1007 static void
1008 handle_app_place_end (void *cls,
1009                       const struct GNUNET_MessageHeader *msg)
1010 {
1011   struct GNUNET_SOCIAL_App *app = cls;
1012
1013   if (NULL != app->connected_cb)
1014     app->connected_cb (app->cb_cls);
1015 }
1016
1017
1018 /*** CLEANUP / DISCONNECT ***/
1019
1020
1021 static void
1022 host_cleanup (struct GNUNET_SOCIAL_Host *hst)
1023 {
1024   if (NULL != hst->slicer)
1025   {
1026     GNUNET_PSYC_slicer_destroy (hst->slicer);
1027     hst->slicer = NULL;
1028   }
1029   GNUNET_free (hst);
1030 }
1031
1032
1033 static void
1034 guest_cleanup (struct GNUNET_SOCIAL_Guest *gst)
1035 {
1036   GNUNET_free (gst);
1037 }
1038
1039
1040 static void
1041 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1042 {
1043   struct GNUNET_HashCode place_pub_hash;
1044   GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1045   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046               "%s place cleanup: %s\n",
1047               GNUNET_YES == plc->is_host ? "host" : "guest",
1048               GNUNET_h2s (&place_pub_hash));
1049
1050   if (NULL != plc->tmit)
1051   {
1052     GNUNET_PSYC_transmit_destroy (plc->tmit);
1053     plc->tmit = NULL;
1054   }
1055   if (NULL != plc->connect_env)
1056   {
1057     GNUNET_MQ_discard (plc->connect_env);
1058     plc->connect_env = NULL;
1059   }
1060   if (NULL != plc->mq)
1061   {
1062     GNUNET_MQ_destroy (plc->mq);
1063     plc->mq = NULL;
1064   }
1065   if (NULL != plc->disconnect_cb)
1066   {
1067     plc->disconnect_cb (plc->disconnect_cls);
1068     plc->disconnect_cb = NULL;
1069   }
1070
1071   (GNUNET_YES == plc->is_host)
1072     ? host_cleanup ((struct GNUNET_SOCIAL_Host *) plc)
1073     : guest_cleanup ((struct GNUNET_SOCIAL_Guest *) plc);
1074 }
1075
1076
1077 void
1078 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1079                   GNUNET_ContinuationCallback cb,
1080                   void *cls)
1081 {
1082   plc->disconnect_cb = cb;
1083   plc->disconnect_cls = cls;
1084
1085   if (NULL != plc->mq)
1086   {
1087     struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (plc->mq);
1088     if (NULL != env)
1089     {
1090       GNUNET_MQ_notify_sent (env, (GNUNET_MQ_NotifyCallback) place_cleanup, plc);
1091     }
1092     else
1093     {
1094       place_cleanup (plc);
1095     }
1096   }
1097   else
1098   {
1099     place_cleanup (plc);
1100   }
1101 }
1102
1103
1104 void
1105 place_leave (struct GNUNET_SOCIAL_Place *plc)
1106 {
1107   struct GNUNET_MessageHeader *msg;
1108   struct GNUNET_MQ_Envelope *
1109     env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1110
1111   GNUNET_MQ_send (plc->mq, env);
1112 }
1113
1114
1115 /*** HOST ***/
1116
1117
1118 static void
1119 host_connect (struct GNUNET_SOCIAL_Host *hst);
1120
1121
1122 static void
1123 host_reconnect (void *cls)
1124 {
1125   host_connect (cls);
1126 }
1127
1128
1129 /**
1130  * Host client disconnected from service.
1131  *
1132  * Reconnect after backoff period.
1133  */
1134 static void
1135 host_disconnected (void *cls, enum GNUNET_MQ_Error error)
1136 {
1137   struct GNUNET_SOCIAL_Host *hst = cls;
1138   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1139
1140   LOG (GNUNET_ERROR_TYPE_DEBUG,
1141        "Host client disconnected (%d), re-connecting\n",
1142        (int) error);
1143   if (NULL != plc->tmit)
1144   {
1145     GNUNET_PSYC_transmit_destroy (plc->tmit);
1146     plc->tmit = NULL;
1147   }
1148   if (NULL != plc->mq)
1149   {
1150     GNUNET_MQ_destroy (plc->mq);
1151     plc->mq = NULL;
1152   }
1153
1154   plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1155                                                       host_reconnect,
1156                                                       hst);
1157   plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1158 }
1159
1160
1161 static void
1162 host_connect (struct GNUNET_SOCIAL_Host *hst)
1163 {
1164   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1165
1166   struct GNUNET_MQ_MessageHandler handlers[] = {
1167     GNUNET_MQ_hd_fixed_size (host_enter_ack,
1168                              GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1169                              struct HostEnterAck,
1170                              hst),
1171     GNUNET_MQ_hd_var_size (host_enter_request,
1172                            GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1173                            struct GNUNET_PSYC_JoinRequestMessage,
1174                            hst),
1175     GNUNET_MQ_hd_var_size (host_message,
1176                            GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1177                            struct GNUNET_PSYC_MessageHeader,
1178                            hst),
1179     GNUNET_MQ_hd_fixed_size (place_message_ack,
1180                              GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1181                              struct GNUNET_MessageHeader,
1182                              plc),
1183     GNUNET_MQ_hd_var_size (place_history_result,
1184                            GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1185                            struct GNUNET_OperationResultMessage,
1186                            plc),
1187     GNUNET_MQ_hd_var_size (place_state_result,
1188                            GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1189                            struct GNUNET_OperationResultMessage,
1190                            plc),
1191     GNUNET_MQ_hd_var_size (place_result,
1192                            GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1193                            struct GNUNET_OperationResultMessage,
1194                            plc),
1195     GNUNET_MQ_handler_end ()
1196   };
1197
1198   plc->mq = GNUNET_CLIENT_connecT (plc->cfg, "social",
1199                                    handlers, host_disconnected, hst);
1200   GNUNET_assert (NULL != plc->mq);
1201   plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1202
1203   GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1204 }
1205
1206
1207 /**
1208  * Enter a place as host.
1209  *
1210  * A place is created upon first entering, and it is active until permanently
1211  * left using GNUNET_SOCIAL_host_leave().
1212  *
1213  * @param app
1214  *        Application handle.
1215  * @param ego
1216  *        Identity of the host.
1217  * @param policy
1218  *        Policy specifying entry and history restrictions for the place.
1219  * @param slicer
1220  *        Slicer to handle incoming messages.
1221  * @param enter_cb
1222  *        Function called when the place is entered and ready to use.
1223  * @param answer_door_cb
1224  *        Function to handle new nyms that want to enter.
1225  * @param farewell_cb
1226  *        Function to handle departing nyms.
1227  * @param cls
1228  *        Closure for the callbacks.
1229  *
1230  * @return Handle for the host. This handle contains the pubkey.
1231  */
1232 struct GNUNET_SOCIAL_Host *
1233 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1234                           const struct GNUNET_SOCIAL_Ego *ego,
1235                           enum GNUNET_PSYC_Policy policy,
1236                           struct GNUNET_PSYC_Slicer *slicer,
1237                           GNUNET_SOCIAL_HostEnterCallback enter_cb,
1238                           GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1239                           GNUNET_SOCIAL_FarewellCallback farewell_cb,
1240                           void *cls)
1241 {
1242   struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1243   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1244
1245   plc->cfg = app->cfg;
1246   plc->is_host = GNUNET_YES;
1247   plc->slicer = slicer;
1248
1249   hst->enter_cb = enter_cb;
1250   hst->answer_door_cb = answer_door_cb;
1251   hst->farewell_cb = farewell_cb;
1252   hst->cb_cls = cls;
1253
1254   plc->op = GNUNET_OP_create ();
1255
1256   hst->slicer = GNUNET_PSYC_slicer_create ();
1257   GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1258                                  host_recv_notice_place_leave_method,
1259                                  host_recv_notice_place_leave_modifier,
1260                                  NULL, host_recv_notice_place_leave_eom, hst);
1261
1262   uint16_t app_id_size = strlen (app->id) + 1;
1263   struct HostEnterRequest *hreq;
1264   plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1265                                           GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1266   hreq->policy = policy;
1267   hreq->ego_pub_key = ego->pub_key;
1268   GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1269
1270   host_connect (hst);
1271   return hst;
1272 }
1273
1274
1275 /**
1276  * Reconnect to an already entered place as host.
1277  *
1278  * @param hconn
1279  *        Host connection handle.
1280  *        @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1281  * @param slicer
1282  *        Slicer to handle incoming messages.
1283  * @param enter_cb
1284  *        Function called when the place is entered and ready to use.
1285  * @param answer_door_cb
1286  *        Function to handle new nyms that want to enter.
1287  * @param farewell_cb
1288  *        Function to handle departing nyms.
1289  * @param cls
1290  *        Closure for the callbacks.
1291  *
1292  * @return Handle for the host.
1293  */
1294  struct GNUNET_SOCIAL_Host *
1295 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1296                                     struct GNUNET_PSYC_Slicer *slicer,
1297                                     GNUNET_SOCIAL_HostEnterCallback enter_cb,
1298                                     GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1299                                     GNUNET_SOCIAL_FarewellCallback farewell_cb,
1300                                     void *cls)
1301 {
1302   struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1303   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1304
1305   hst->enter_cb = enter_cb;
1306   hst->answer_door_cb = answer_door_cb;
1307   hst->farewell_cb = farewell_cb;
1308   hst->cb_cls = cls;
1309
1310   plc->cfg = hconn->app->cfg;
1311   plc->is_host = GNUNET_YES;
1312   plc->slicer = slicer;
1313   plc->pub_key = hconn->plc_msg.place_pub_key;
1314   plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1315
1316   plc->op = GNUNET_OP_create ();
1317
1318   hst->slicer = GNUNET_PSYC_slicer_create ();
1319   GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1320                                  host_recv_notice_place_leave_method,
1321                                  host_recv_notice_place_leave_modifier,
1322                                  NULL, host_recv_notice_place_leave_eom, hst);
1323
1324   size_t app_id_size = strlen (hconn->app->id) + 1;
1325   struct HostEnterRequest *hreq;
1326   plc->connect_env = GNUNET_MQ_msg_extra (hreq, app_id_size,
1327                                           GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1328   hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1329   hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1330   GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1331
1332   host_connect (hst);
1333   return hst;
1334 }
1335
1336
1337 /**
1338  * Decision whether to admit @a nym into the place or refuse entry.
1339  *
1340  * @param hst
1341  *        Host of the place.
1342  * @param nym
1343  *        Handle for the entity that wanted to enter.
1344  * @param is_admitted
1345  *        #GNUNET_YES    if @a nym is admitted,
1346  *        #GNUNET_NO     if @a nym is refused entry,
1347  *        #GNUNET_SYSERR if we cannot answer the request.
1348  * @param method_name
1349  *        Method name for the rejection message.
1350  * @param env
1351  *        Environment containing variables for the message, or NULL.
1352  * @param data
1353  *        Data for the rejection message to send back.
1354  * @param data_size
1355  *        Number of bytes in @a data for method.
1356  * @return #GNUNET_OK on success,
1357  *         #GNUNET_SYSERR if the message is too large.
1358  */
1359 int
1360 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1361                                    struct GNUNET_SOCIAL_Nym *nym,
1362                                    int is_admitted,
1363                                    const struct GNUNET_PSYC_Message *entry_resp)
1364 {
1365   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1366   struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1367   uint16_t entry_resp_size
1368     = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1369
1370   if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1371     return GNUNET_SYSERR;
1372
1373   struct GNUNET_MQ_Envelope *
1374     env = GNUNET_MQ_msg_extra (dcsn, entry_resp_size,
1375                                GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1376   dcsn->is_admitted = htonl (is_admitted);
1377   dcsn->slave_pub_key = nym->pub_key;
1378
1379   if (0 < entry_resp_size)
1380     GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1381
1382   GNUNET_MQ_send (plc->mq, env);
1383   return GNUNET_OK;
1384 }
1385
1386
1387 /**
1388  * Throw @a nym out of the place.
1389  *
1390  * The @a nym reference will remain valid until the
1391  * #GNUNET_SOCIAL_FarewellCallback is invoked,
1392  * which should be very soon after this call.
1393  *
1394  * @param host
1395  *        Host of the place.
1396  * @param nym
1397  *        Handle for the entity to be ejected.
1398  * @param env
1399  *        Environment for the message or NULL.
1400  */
1401 void
1402 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1403                           const struct GNUNET_SOCIAL_Nym *nym,
1404                           struct GNUNET_PSYC_Environment *e)
1405 {
1406   struct GNUNET_PSYC_Environment *env = e;
1407   if (NULL == env)
1408     env = GNUNET_PSYC_env_create ();
1409   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1410                        "_nym", &nym->pub_key, sizeof (nym->pub_key));
1411   GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1412                                GNUNET_SOCIAL_ANNOUNCE_NONE);
1413   if (NULL == e)
1414     GNUNET_PSYC_env_destroy (env);
1415 }
1416
1417
1418 /**
1419  * Get the public key of @a ego.
1420  *
1421  * @param ego
1422  *        Ego.
1423  *
1424  * @return Public key of ego.
1425  */
1426 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1427 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1428 {
1429   return &ego->pub_key;
1430 }
1431
1432
1433 /**
1434  * Get the hash of the public key of @a ego.
1435  *
1436  * @param ego
1437  *        Ego.
1438  *
1439  * @return Hash of the public key of @a ego.
1440  */
1441 const struct GNUNET_HashCode *
1442 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1443 {
1444   return &ego->pub_key_hash;
1445 }
1446
1447
1448 /**
1449  * Get the name of @a ego.
1450  *
1451  * @param ego
1452  *        Ego.
1453  *
1454  * @return Public key of @a ego.
1455  */
1456 const char *
1457 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1458 {
1459   return ego->name;
1460 }
1461
1462
1463 /**
1464  * Get the public key of @a nym.
1465  *
1466  * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1467  *
1468  * @param nym
1469  *        Pseudonym.
1470  *
1471  * @return Public key of @a nym.
1472  */
1473 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1474 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1475 {
1476   return &nym->pub_key;
1477 }
1478
1479
1480 /**
1481  * Get the hash of the public key of @a nym.
1482  *
1483  * @param nym
1484  *        Pseudonym.
1485  *
1486  * @return Hash of the public key of @a nym.
1487  */
1488 const struct GNUNET_HashCode *
1489 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1490 {
1491   return &nym->pub_key_hash;
1492 }
1493
1494
1495 /**
1496  * Send a message to all nyms that are present in the place.
1497  *
1498  * This function is restricted to the host.  Nyms can only send requests
1499  * to the host who can decide to relay it to everyone in the place.
1500  *
1501  * @param host  Host of the place.
1502  * @param method_name Method to use for the announcement.
1503  * @param env  Environment containing variables for the message and operations
1504  *          on objects of the place.  Can be NULL.
1505  * @param notify Function to call to get the payload of the announcement.
1506  * @param notify_cls Closure for @a notify.
1507  * @param flags Flags for this announcement.
1508  *
1509  * @return NULL on error (announcement already in progress?).
1510  */
1511 struct GNUNET_SOCIAL_Announcement *
1512 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1513                              const char *method_name,
1514                              const struct GNUNET_PSYC_Environment *env,
1515                              GNUNET_PSYC_TransmitNotifyData notify_data,
1516                              void *notify_data_cls,
1517                              enum GNUNET_SOCIAL_AnnounceFlags flags)
1518 {
1519   if (GNUNET_OK ==
1520       GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1521                                     NULL, notify_data, notify_data_cls, flags))
1522     return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1523   else
1524     return NULL;
1525 }
1526
1527
1528 /**
1529  * Resume transmitting announcement.
1530  *
1531  * @param a
1532  *        The announcement to resume.
1533  */
1534 void
1535 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1536 {
1537   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1538 }
1539
1540
1541 /**
1542  * Cancel announcement.
1543  *
1544  * @param a
1545  *        The announcement to cancel.
1546  */
1547 void
1548 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1549 {
1550   GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1551 }
1552
1553
1554 /**
1555  * Obtain handle for a hosted place.
1556  *
1557  * The returned handle can be used to access the place API.
1558  *
1559  * @param host  Handle for the host.
1560  *
1561  * @return Handle for the hosted place, valid as long as @a host is valid.
1562  */
1563 struct GNUNET_SOCIAL_Place *
1564 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1565 {
1566   return &hst->plc;
1567 }
1568
1569
1570 /**
1571  * Disconnect from a home.
1572  *
1573  * Invalidates host handle.
1574  *
1575  * @param hst
1576  *        The host to disconnect.
1577  */
1578 void
1579 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1580                                GNUNET_ContinuationCallback disconnect_cb,
1581                                void *cls)
1582 {
1583   place_disconnect (&hst->plc, disconnect_cb, cls);
1584 }
1585
1586
1587 /**
1588  * Stop hosting the home.
1589  *
1590  * Sends a _notice_place_closing announcement to the home.
1591  * Invalidates host handle.
1592  *
1593  * @param hst
1594  *        The host leaving.
1595  * @param env
1596  *        Environment for the message or NULL.
1597  *        _nym is set to @e nym regardless whether an @e env is provided.
1598  * @param disconnect_cb
1599  *        Function called after the host left the place
1600  *        and disconnected from the social service.
1601  * @param cls
1602  *        Closure for @a disconnect_cb.
1603  */
1604 void
1605 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1606                           const struct GNUNET_PSYC_Environment *env,
1607                           GNUNET_ContinuationCallback disconnect_cb,
1608                           void *cls)
1609 {
1610   GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1611                                GNUNET_SOCIAL_ANNOUNCE_NONE);
1612   place_leave (&hst->plc);
1613   GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1614 }
1615
1616
1617 /*** GUEST ***/
1618
1619
1620 static void
1621 guest_connect (struct GNUNET_SOCIAL_Guest *gst);
1622
1623
1624 static void
1625 guest_reconnect (void *cls)
1626 {
1627   guest_connect (cls);
1628 }
1629
1630
1631 /**
1632  * Guest client disconnected from service.
1633  *
1634  * Reconnect after backoff period.
1635  */
1636 static void
1637 guest_disconnected (void *cls, enum GNUNET_MQ_Error error)
1638 {
1639   struct GNUNET_SOCIAL_Guest *gst = cls;
1640   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1641
1642   LOG (GNUNET_ERROR_TYPE_DEBUG,
1643        "Guest client disconnected (%d), re-connecting\n",
1644        (int) error);
1645   if (NULL != plc->tmit)
1646   {
1647     GNUNET_PSYC_transmit_destroy (plc->tmit);
1648     plc->tmit = NULL;
1649   }
1650   if (NULL != plc->mq)
1651   {
1652     GNUNET_MQ_destroy (plc->mq);
1653     plc->mq = NULL;
1654   }
1655
1656   plc->reconnect_task = GNUNET_SCHEDULER_add_delayed (plc->reconnect_delay,
1657                                                       guest_reconnect,
1658                                                       gst);
1659   plc->reconnect_delay = GNUNET_TIME_STD_BACKOFF (plc->reconnect_delay);
1660 }
1661
1662
1663 static void
1664 guest_connect (struct GNUNET_SOCIAL_Guest *gst)
1665 {
1666   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1667
1668   struct GNUNET_MQ_MessageHandler handlers[] = {
1669     GNUNET_MQ_hd_fixed_size (guest_enter_ack,
1670                              GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1671                              struct GNUNET_PSYC_CountersResultMessage,
1672                              gst),
1673     GNUNET_MQ_hd_var_size (guest_enter_decision,
1674                            GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1675                            struct GNUNET_PSYC_JoinDecisionMessage,
1676                            gst),
1677     GNUNET_MQ_hd_var_size (place_message,
1678                            GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1679                            struct GNUNET_PSYC_MessageHeader,
1680                            plc),
1681     GNUNET_MQ_hd_fixed_size (place_message_ack,
1682                              GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1683                              struct GNUNET_MessageHeader,
1684                              plc),
1685     GNUNET_MQ_hd_var_size (place_history_result,
1686                            GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1687                            struct GNUNET_OperationResultMessage,
1688                            plc),
1689     GNUNET_MQ_hd_var_size (place_state_result,
1690                            GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1691                            struct GNUNET_OperationResultMessage,
1692                            plc),
1693     GNUNET_MQ_hd_var_size (place_result,
1694                            GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1695                            struct GNUNET_OperationResultMessage,
1696                            plc),
1697     GNUNET_MQ_handler_end ()
1698   };
1699
1700   plc->mq = GNUNET_CLIENT_connecT (plc->cfg, "social",
1701                                    handlers, guest_disconnected, gst);
1702   GNUNET_assert (NULL != plc->mq);
1703   plc->tmit = GNUNET_PSYC_transmit_create (plc->mq);
1704
1705   GNUNET_MQ_send_copy (plc->mq, plc->connect_env);
1706 }
1707
1708
1709 static struct GNUNET_MQ_Envelope *
1710 guest_enter_request_create (const char *app_id,
1711                             const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1712                             const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1713                             const struct GNUNET_PeerIdentity *origin,
1714                             size_t relay_count,
1715                             const struct GNUNET_PeerIdentity *relays,
1716                             const struct GNUNET_PSYC_Message *join_msg)
1717 {
1718   uint16_t app_id_size = strlen (app_id) + 1;
1719   uint16_t join_msg_size = ntohs (join_msg->header.size);
1720   uint16_t relay_size = relay_count * sizeof (*relays);
1721
1722   struct GuestEnterRequest *greq;
1723   struct GNUNET_MQ_Envelope *
1724     env = GNUNET_MQ_msg_extra (greq, app_id_size + relay_size + join_msg_size,
1725                                GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1726   greq->place_pub_key = *place_pub_key;
1727   greq->ego_pub_key = *ego_pub_key;
1728   greq->origin = *origin;
1729   greq->relay_count = htonl (relay_count);
1730
1731   char *p = (char *) &greq[1];
1732   GNUNET_memcpy (p, app_id, app_id_size);
1733   p += app_id_size;
1734
1735   if (0 < relay_size)
1736   {
1737     GNUNET_memcpy (p, relays, relay_size);
1738     p += relay_size;
1739   }
1740
1741   GNUNET_memcpy (p, join_msg, join_msg_size);
1742   return env;
1743 }
1744
1745
1746 /**
1747  * Request entry to a place as a guest.
1748  *
1749  * @param app
1750  *        Application handle.
1751  * @param ego
1752  *        Identity of the guest.
1753  * @param place_pub_key
1754  *        Public key of the place to enter.
1755  * @param flags
1756  *        Flags for the entry.
1757  * @param origin
1758  *        Peer identity of the origin of the underlying multicast group.
1759  * @param relay_count
1760  *        Number of elements in the @a relays array.
1761  * @param relays
1762  *        Relays for the underlying multicast group.
1763  * @param method_name
1764  *        Method name for the message.
1765  * @param env
1766  *        Environment containing variables for the message, or NULL.
1767  * @param data
1768  *        Payload for the message to give to the enter callback.
1769  * @param data_size
1770  *        Number of bytes in @a data.
1771  * @param slicer
1772  *        Slicer to use for processing incoming requests from guests.
1773  *
1774  * @return NULL on errors, otherwise handle for the guest.
1775  */
1776 struct GNUNET_SOCIAL_Guest *
1777 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1778                            const struct GNUNET_SOCIAL_Ego *ego,
1779                            const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1780                            enum GNUNET_PSYC_SlaveJoinFlags flags,
1781                            const struct GNUNET_PeerIdentity *origin,
1782                            uint32_t relay_count,
1783                            const struct GNUNET_PeerIdentity *relays,
1784                            const struct GNUNET_PSYC_Message *entry_msg,
1785                            struct GNUNET_PSYC_Slicer *slicer,
1786                            GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1787                            GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1788                            void *cls)
1789 {
1790   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1791   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1792
1793   plc->ego_pub_key = ego->pub_key;
1794   plc->pub_key = *place_pub_key;
1795   plc->cfg = app->cfg;
1796   plc->is_host = GNUNET_NO;
1797   plc->slicer = slicer;
1798
1799   plc->op = GNUNET_OP_create ();
1800
1801   plc->connect_env
1802     = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1803                                   origin, relay_count, relays, entry_msg);
1804
1805   gst->enter_cb = local_enter_cb;
1806   gst->entry_dcsn_cb = entry_dcsn_cb;
1807   gst->cb_cls = cls;
1808
1809   guest_connect (gst);
1810   return gst;
1811 }
1812
1813
1814 /**
1815  * Request entry to a place by name as a guest.
1816  *
1817  * @param app
1818  *        Application handle.
1819  * @param ego
1820  *        Identity of the guest.
1821  * @param gns_name
1822  *        GNS name of the place to enter.  Either in the form of
1823  *        'room.friend.gnu', or 'NYMPUBKEY.zkey'.  This latter case refers to
1824  *        the 'PLACE' record of the empty label ("+") in the GNS zone with the
1825  *        nym's public key 'NYMPUBKEY', and can be used to request entry to a
1826  *        pseudonym's place directly.
1827  * @param password
1828  *        Password to decrypt the record, or NULL for cleartext records.
1829  * @param join_msg
1830  *        Entry request message or NULL.
1831  * @param slicer
1832  *        Slicer to use for processing incoming requests from guests.
1833  * @param local_enter_cb
1834  *        Called upon connection established to the social service.
1835  * @param entry_decision_cb
1836  *        Called upon receiving entry decision.
1837  *
1838  * @return NULL on errors, otherwise handle for the guest.
1839  */
1840 struct GNUNET_SOCIAL_Guest *
1841 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1842                                    const struct GNUNET_SOCIAL_Ego *ego,
1843                                    const char *gns_name,
1844                                    const char *password,
1845                                    const struct GNUNET_PSYC_Message *join_msg,
1846                                    struct GNUNET_PSYC_Slicer *slicer,
1847                                    GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1848                                    GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1849                                    void *cls)
1850 {
1851   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1852   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1853
1854   if (NULL == password)
1855     password = "";
1856
1857   uint16_t app_id_size = strlen (app->id) + 1;
1858   uint16_t gns_name_size = strlen (gns_name) + 1;
1859   uint16_t password_size = strlen (password) + 1;
1860
1861   uint16_t join_msg_size = 0;
1862   if (NULL != join_msg)
1863     join_msg_size = ntohs (join_msg->header.size);
1864
1865   struct GuestEnterByNameRequest *greq;
1866   plc->connect_env
1867     = GNUNET_MQ_msg_extra (greq, app_id_size + gns_name_size
1868                            + password_size + join_msg_size,
1869                            GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1870
1871   greq->ego_pub_key = ego->pub_key;
1872
1873   char *p = (char *) &greq[1];
1874   GNUNET_memcpy (p, app->id, app_id_size);
1875   p += app_id_size;
1876   GNUNET_memcpy (p, gns_name, gns_name_size);
1877   p += gns_name_size;
1878   GNUNET_memcpy (p, password, password_size);
1879   p += password_size;
1880   if (NULL != join_msg)
1881     GNUNET_memcpy (p, join_msg, join_msg_size);
1882
1883   plc->ego_pub_key = ego->pub_key;
1884   plc->cfg = app->cfg;
1885   plc->is_host = GNUNET_NO;
1886   plc->slicer = slicer;
1887
1888   plc->op = GNUNET_OP_create ();
1889
1890   gst->enter_cb = local_enter_cb;
1891   gst->entry_dcsn_cb = entry_decision_cb;
1892   gst->cb_cls = cls;
1893
1894   guest_connect (gst);
1895   return gst;
1896 }
1897
1898
1899 /**
1900  * Reconnect to an already entered place as guest.
1901  *
1902  * @param gconn
1903  *        Guest connection handle.
1904  *        @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1905  * @param flags
1906  *        Flags for the entry.
1907  * @param slicer
1908  *        Slicer to use for processing incoming requests from guests.
1909  * @param local_enter_cb
1910  *        Called upon connection established to the social service.
1911  * @param entry_decision_cb
1912  *        Called upon receiving entry decision.
1913  *
1914  * @return NULL on errors, otherwise handle for the guest.
1915  */
1916 struct GNUNET_SOCIAL_Guest *
1917 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1918                                      enum GNUNET_PSYC_SlaveJoinFlags flags,
1919                                      struct GNUNET_PSYC_Slicer *slicer,
1920                                      GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1921                                      void *cls)
1922 {
1923   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1924   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1925
1926   uint16_t app_id_size = strlen (gconn->app->id) + 1;
1927   struct GuestEnterRequest *greq;
1928   plc->connect_env
1929     = GNUNET_MQ_msg_extra (greq, app_id_size,
1930                            GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1931   greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1932   greq->place_pub_key = gconn->plc_msg.place_pub_key;
1933   greq->flags = htonl (flags);
1934
1935   GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1936
1937   plc->cfg = gconn->app->cfg;
1938   plc->is_host = GNUNET_NO;
1939   plc->slicer = slicer;
1940   plc->pub_key = gconn->plc_msg.place_pub_key;
1941   plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1942
1943   plc->op = GNUNET_OP_create ();
1944
1945   gst->enter_cb = local_enter_cb;
1946   gst->cb_cls = cls;
1947
1948   guest_connect (gst);
1949   return gst;
1950 }
1951
1952
1953 /**
1954  * Talk to the host of the place.
1955  *
1956  * @param place
1957  *        Place where we want to talk to the host.
1958  * @param method_name
1959  *        Method to invoke on the host.
1960  * @param env
1961  *        Environment containing variables for the message, or NULL.
1962  * @param notify_data
1963  *        Function to use to get the payload for the method.
1964  * @param notify_data_cls
1965  *        Closure for @a notify_data.
1966  * @param flags
1967  *        Flags for the message being sent.
1968  *
1969  * @return NULL if we are already trying to talk to the host,
1970  *         otherwise handle to cancel the request.
1971  */
1972 struct GNUNET_SOCIAL_TalkRequest *
1973 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1974                           const char *method_name,
1975                           const struct GNUNET_PSYC_Environment *env,
1976                           GNUNET_PSYC_TransmitNotifyData notify_data,
1977                           void *notify_data_cls,
1978                           enum GNUNET_SOCIAL_TalkFlags flags)
1979 {
1980   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1981   GNUNET_assert (NULL != plc->tmit);
1982
1983   if (GNUNET_OK ==
1984       GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1985                                     NULL, notify_data, notify_data_cls, flags))
1986     return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1987   else
1988     return NULL;
1989 }
1990
1991
1992 /**
1993  * Resume talking to the host of the place.
1994  *
1995  * @param tr
1996  *        Talk request to resume.
1997  */
1998 void
1999 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
2000 {
2001   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
2002 }
2003
2004
2005 /**
2006  * Cancel talking to the host of the place.
2007  *
2008  * @param tr
2009  *        Talk request to cancel.
2010  */
2011 void
2012 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
2013 {
2014   GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
2015 }
2016
2017
2018 /**
2019  * Disconnect from a place.
2020  *
2021  * Invalidates guest handle.
2022  *
2023  * @param gst
2024  *        The guest to disconnect.
2025  */
2026 void
2027 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
2028                                 GNUNET_ContinuationCallback disconnect_cb,
2029                                 void *cls)
2030 {
2031   place_disconnect (&gst->plc, disconnect_cb, cls);
2032 }
2033
2034
2035 /**
2036  * Leave a place temporarily or permanently.
2037  *
2038  * Notifies the owner of the place about leaving, and destroys the place handle.
2039  *
2040  * @param place
2041  *        Place to leave.
2042  * @param keep_active
2043  *        Keep place active after last application disconnected.
2044  *        #GNUNET_YES or #GNUNET_NO
2045  * @param env
2046  *        Optional environment for the leave message if @a keep_active
2047  *        is #GNUNET_NO.  NULL if not needed.
2048  * @param leave_cb
2049  *        Called upon disconnecting from the social service.
2050  */
2051 void
2052 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
2053                            struct GNUNET_PSYC_Environment *env,
2054                            GNUNET_ContinuationCallback disconnect_cb,
2055                            void *cls)
2056 {
2057   GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
2058                             GNUNET_SOCIAL_TALK_NONE);
2059   place_leave (&gst->plc);
2060   GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
2061 }
2062
2063
2064 /**
2065  * Obtain handle for a place entered as guest.
2066  *
2067  * The returned handle can be used to access the place API.
2068  *
2069  * @param guest  Handle for the guest.
2070  *
2071  * @return Handle for the place, valid as long as @a guest is valid.
2072  */
2073 struct GNUNET_SOCIAL_Place *
2074 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
2075 {
2076   return &gst->plc;
2077 }
2078
2079
2080 /**
2081  * Obtain the public key of a place.
2082  *
2083  * @param plc
2084  *        Place.
2085  *
2086  * @return Public key of the place.
2087  */
2088 const struct GNUNET_CRYPTO_EddsaPublicKey *
2089 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
2090 {
2091   return &plc->pub_key;
2092 }
2093
2094
2095 /**
2096  * Set message processing @a flags for a @a method_prefix.
2097  *
2098  * @param plc
2099  *        Place.
2100  * @param method_prefix
2101  *        Method prefix @a flags apply to.
2102  * @param flags
2103  *        The flags that apply to a matching @a method_prefix.
2104  */
2105 void
2106 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2107                                   const char *method_prefix,
2108                                   enum GNUNET_SOCIAL_MsgProcFlags flags)
2109 {
2110   GNUNET_assert (NULL != method_prefix);
2111   struct MsgProcRequest *mpreq;
2112   uint16_t method_size = strnlen (method_prefix,
2113                                   GNUNET_SERVER_MAX_MESSAGE_SIZE
2114                                   - sizeof (*mpreq)) + 1;
2115   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2116
2117   struct GNUNET_MQ_Envelope *
2118     env = GNUNET_MQ_msg_extra (mpreq, method_size,
2119                                GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2120   mpreq->flags = htonl (flags);
2121   GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2122
2123   GNUNET_MQ_send (plc->mq, env);
2124 }
2125
2126
2127 /**
2128  * Clear all message processing flags previously set for this place.
2129  */
2130 void
2131 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2132 {
2133   struct GNUNET_MessageHeader *req;
2134   struct GNUNET_MQ_Envelope *
2135     env = GNUNET_MQ_msg (req, GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2136
2137   GNUNET_MQ_send (plc->mq, env);
2138 }
2139
2140
2141 static struct GNUNET_SOCIAL_HistoryRequest *
2142 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2143                       uint64_t start_message_id,
2144                       uint64_t end_message_id,
2145                       uint64_t message_limit,
2146                       const char *method_prefix,
2147                       uint32_t flags,
2148                       struct GNUNET_PSYC_Slicer *slicer,
2149                       GNUNET_ResultCallback result_cb,
2150                       void *cls)
2151 {
2152   struct GNUNET_PSYC_HistoryRequestMessage *req;
2153   struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2154   hist->plc = plc;
2155   hist->slicer = slicer;
2156   hist->result_cb = result_cb;
2157   hist->cls = cls;
2158   hist->op_id = GNUNET_OP_add (plc->op, op_recv_history_result, hist, NULL);
2159
2160   GNUNET_assert (NULL != method_prefix);
2161   uint16_t method_size = strnlen (method_prefix,
2162                                   GNUNET_SERVER_MAX_MESSAGE_SIZE
2163                                   - sizeof (*req)) + 1;
2164   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2165
2166   struct GNUNET_MQ_Envelope *
2167     env = GNUNET_MQ_msg_extra (req, method_size,
2168                                GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2169   req->start_message_id = GNUNET_htonll (start_message_id);
2170   req->end_message_id = GNUNET_htonll (end_message_id);
2171   req->message_limit = GNUNET_htonll (message_limit);
2172   req->flags = htonl (flags);
2173   req->op_id = GNUNET_htonll (hist->op_id);
2174   GNUNET_memcpy (&req[1], method_prefix, method_size);
2175
2176   GNUNET_MQ_send (plc->mq, env);
2177   return hist;
2178 }
2179
2180
2181 /**
2182  * Learn about the history of a place.
2183  *
2184  * Messages are returned through the @a slicer function
2185  * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2186  *
2187  * @param place
2188  *        Place we want to learn more about.
2189  * @param start_message_id
2190  *        First historic message we are interested in.
2191  * @param end_message_id
2192  *        Last historic message we are interested in (inclusive).
2193  * @param method_prefix
2194  *        Only retrieve messages with this method prefix.
2195  * @param flags
2196  *        OR'ed GNUNET_PSYC_HistoryReplayFlags
2197  * @param slicer
2198  *        Slicer to use for retrieved messages.
2199  *        Can be the same as the slicer of the place.
2200  * @param result_cb
2201  *        Function called after all messages retrieved.
2202  *        NULL if not needed.
2203  * @param cls Closure for @a result_cb.
2204  */
2205 struct GNUNET_SOCIAL_HistoryRequest *
2206 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2207                                     uint64_t start_message_id,
2208                                     uint64_t end_message_id,
2209                                     const char *method_prefix,
2210                                     uint32_t flags,
2211                                     struct GNUNET_PSYC_Slicer *slicer,
2212                                     GNUNET_ResultCallback result_cb,
2213                                     void *cls)
2214 {
2215   return place_history_replay (plc, start_message_id, end_message_id, 0,
2216                                method_prefix, flags, slicer, result_cb, cls);
2217 }
2218
2219
2220 /**
2221  * Learn about the history of a place.
2222  *
2223  * Sends messages through the slicer function of the place where
2224  * start_message_id <= message_id <= end_message_id.
2225  * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2226  *
2227  * To get the latest message, use 0 for both the start and end message ID.
2228  *
2229  * @param place
2230  *        Place we want to learn more about.
2231  * @param message_limit
2232  *        Maximum number of historic messages we are interested in.
2233  * @param method_prefix
2234  *        Only retrieve messages with this method prefix.
2235  * @param flags
2236  *        OR'ed GNUNET_PSYC_HistoryReplayFlags
2237  * @param result_cb
2238  *        Function called after all messages retrieved.
2239  *        NULL if not needed.
2240  * @param cls Closure for @a result_cb.
2241  */
2242 struct GNUNET_SOCIAL_HistoryRequest *
2243 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2244                                            uint64_t message_limit,
2245                                            const char *method_prefix,
2246                                            uint32_t flags,
2247                                            struct GNUNET_PSYC_Slicer *slicer,
2248                                            GNUNET_ResultCallback result_cb,
2249                                            void *cls)
2250 {
2251   return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2252                                slicer, result_cb, cls);
2253 }
2254
2255
2256 /**
2257  * Cancel learning about the history of a place.
2258  *
2259  * @param hist
2260  *        History lesson to cancel.
2261  */
2262 void
2263 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2264 {
2265   GNUNET_OP_remove (hist->plc->op, hist->op_id);
2266   GNUNET_free (hist);
2267 }
2268
2269
2270 /**
2271  * Request matching state variables.
2272  */
2273 static struct GNUNET_SOCIAL_LookHandle *
2274 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2275                  uint16_t type, const char *name,
2276                  GNUNET_PSYC_StateVarCallback var_cb,
2277                  GNUNET_ResultCallback result_cb, void *cls)
2278 {
2279   struct GNUNET_PSYC_StateRequestMessage *req;
2280   struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2281   look->plc = plc;
2282   look->var_cb = var_cb;
2283   look->result_cb = result_cb;
2284   look->cls = cls;
2285   look->op_id = GNUNET_OP_add (plc->op, &op_recv_state_result, look, NULL);
2286
2287   GNUNET_assert (NULL != name);
2288   size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2289                               - sizeof (*req)) + 1;
2290   struct GNUNET_MQ_Envelope *
2291     env = GNUNET_MQ_msg_extra (req, name_size, type);
2292   req->op_id = GNUNET_htonll (look->op_id);
2293   GNUNET_memcpy (&req[1], name, name_size);
2294
2295   GNUNET_MQ_send (plc->mq, env);
2296   return look;
2297 }
2298
2299
2300 /**
2301  * Look at a particular object in the place.
2302  *
2303  * The best matching object is returned (its name might be less specific than
2304  * what was requested).
2305  *
2306  * @param place
2307  *        The place where to look.
2308  * @param full_name
2309  *        Full name of the object.
2310  * @param value_size
2311  *        Set to the size of the returned value.
2312  *
2313  * @return NULL if there is no such object at this place.
2314  */
2315 struct GNUNET_SOCIAL_LookHandle *
2316 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2317                              const char *full_name,
2318                              GNUNET_PSYC_StateVarCallback var_cb,
2319                              GNUNET_ResultCallback result_cb,
2320                              void *cls)
2321 {
2322   return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2323                           full_name, var_cb, result_cb, cls);
2324 }
2325
2326
2327 /**
2328  * Look for objects in the place with a matching name prefix.
2329  *
2330  * @param place
2331  *        The place where to look.
2332  * @param name_prefix
2333  *        Look at objects with names beginning with this value.
2334  * @param var_cb
2335  *        Function to call for each object found.
2336  * @param cls
2337  *        Closure for callback function.
2338  *
2339  * @return Handle that can be used to stop looking at objects.
2340  */
2341 struct GNUNET_SOCIAL_LookHandle *
2342 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2343                               const char *name_prefix,
2344                               GNUNET_PSYC_StateVarCallback var_cb,
2345                               GNUNET_ResultCallback result_cb,
2346                               void *cls)
2347 {
2348   return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2349                           name_prefix, var_cb, result_cb, cls);
2350 }
2351
2352
2353 /**
2354  * Cancel a state request operation.
2355  *
2356  * @param sr
2357  *        Handle for the operation to cancel.
2358  */
2359 void
2360 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2361 {
2362   GNUNET_OP_remove (look->plc->op, look->op_id);
2363   GNUNET_free (look);
2364 }
2365
2366
2367 static void
2368 op_recv_zone_add_place_result (void *cls, int64_t result,
2369                                const void *err_msg, uint16_t err_msg_size)
2370 {
2371   LOG (GNUNET_ERROR_TYPE_DEBUG,
2372        "Received zone add place result: %" PRId64 ".\n", result);
2373
2374   struct ZoneAddPlaceHandle *add_plc = cls;
2375   if (NULL != add_plc->result_cb)
2376     add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2377
2378   GNUNET_free (add_plc);
2379 }
2380
2381
2382 /**
2383  * Advertise @e place in the GNS zone of @e ego.
2384  *
2385  * @param app
2386  *        Application handle.
2387  * @param ego
2388  *        Ego.
2389  * @param place_pub_key
2390  *        Public key of place to add.
2391  * @param name
2392  *        The name for the PLACE record to put in the zone.
2393  * @param password
2394  *        Password used to encrypt the record or NULL to keep it cleartext.
2395  * @param relay_count
2396  *        Number of elements in the @a relays array.
2397  * @param relays
2398  *        List of relays to put in the PLACE record to advertise
2399  *        as entry points to the place in addition to the origin.
2400  * @param expiration_time
2401  *        Expiration time of the record, use 0 to remove the record.
2402  * @param result_cb
2403  *        Function called with the result of the operation.
2404  * @param result_cls
2405  *        Closure for @a result_cb
2406  *
2407  * @return #GNUNET_OK if the request was sent,
2408  *         #GNUNET_SYSERR on error, e.g. the name/password is too long.
2409  */
2410 int
2411 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2412                               const struct GNUNET_SOCIAL_Ego *ego,
2413                               const char *name,
2414                               const char *password,
2415                               const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2416                               const struct GNUNET_PeerIdentity *origin,
2417                               uint32_t relay_count,
2418                               const struct GNUNET_PeerIdentity *relays,
2419                               struct GNUNET_TIME_Absolute expiration_time,
2420                               GNUNET_ResultCallback result_cb,
2421                               void *result_cls)
2422 {
2423   struct ZoneAddPlaceRequest *preq;
2424   size_t name_size = strlen (name) + 1;
2425   size_t password_size = strlen (password) + 1;
2426   size_t relay_size = relay_count * sizeof (*relays);
2427   size_t payload_size = name_size + password_size + relay_size;
2428
2429   if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*preq) + payload_size)
2430     return GNUNET_SYSERR;
2431
2432   struct GNUNET_MQ_Envelope *
2433     env = GNUNET_MQ_msg_extra (preq, payload_size,
2434                                GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2435   preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2436   preq->ego_pub_key = ego->pub_key;
2437   preq->place_pub_key = *place_pub_key;
2438   preq->origin = *origin;
2439   preq->relay_count = htonl (relay_count);
2440
2441   char *p = (char *) &preq[1];
2442   GNUNET_memcpy (p, name, name_size);
2443   p += name_size;
2444   GNUNET_memcpy (p, password, password_size);
2445   p += password_size;
2446   GNUNET_memcpy (p, relays, relay_size);
2447
2448   struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2449   add_plc->result_cb = result_cb;
2450   add_plc->result_cls = result_cls;
2451
2452   preq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2453                                               op_recv_zone_add_place_result,
2454                                               add_plc, NULL));
2455
2456   GNUNET_MQ_send (app->mq, env);
2457   return GNUNET_OK;
2458 }
2459
2460
2461 static void
2462 op_recv_zone_add_nym_result (void *cls, int64_t result,
2463                              const void *err_msg, uint16_t err_msg_size)
2464 {
2465   LOG (GNUNET_ERROR_TYPE_DEBUG,
2466        "Received zone add nym result: %" PRId64 ".\n", result);
2467
2468   struct ZoneAddNymHandle *add_nym = cls;
2469   if (NULL != add_nym->result_cb)
2470     add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2471
2472   GNUNET_free (add_nym);
2473 }
2474
2475
2476 /**
2477  * Add nym to the GNS zone of @e ego.
2478  *
2479  * @param cfg
2480  *        Configuration.
2481  * @param ego
2482  *        Ego.
2483  * @param name
2484  *        The name for the PKEY record to put in the zone.
2485  * @param nym_pub_key
2486  *        Public key of nym to add.
2487  * @param expiration_time
2488  *        Expiration time of the record, use 0 to remove the record.
2489  * @param result_cb
2490  *        Function called with the result of the operation.
2491  * @param result_cls
2492  *        Closure for @a result_cb
2493  *
2494  * @return #GNUNET_OK if the request was sent,
2495  *         #GNUNET_SYSERR on error, e.g. the name is too long.
2496  */
2497 int
2498 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2499                             const struct GNUNET_SOCIAL_Ego *ego,
2500                             const char *name,
2501                             const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2502                             struct GNUNET_TIME_Absolute expiration_time,
2503                             GNUNET_ResultCallback result_cb,
2504                             void *result_cls)
2505 {
2506   struct ZoneAddNymRequest *nreq;
2507
2508   size_t name_size = strlen (name) + 1;
2509   if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2510     return GNUNET_SYSERR;
2511
2512   struct GNUNET_MQ_Envelope *
2513     env = GNUNET_MQ_msg_extra (nreq, name_size,
2514                                GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2515   nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2516   nreq->ego_pub_key = ego->pub_key;
2517   nreq->nym_pub_key = *nym_pub_key;
2518   GNUNET_memcpy (&nreq[1], name, name_size);
2519
2520   struct ZoneAddNymHandle *add_nym = GNUNET_malloc (sizeof (*add_nym));
2521   add_nym->result_cb = result_cb;
2522   add_nym->result_cls = result_cls;
2523
2524   nreq->op_id = GNUNET_htonll (GNUNET_OP_add (app->op,
2525                                               op_recv_zone_add_nym_result,
2526                                               add_nym, NULL));
2527
2528   GNUNET_MQ_send (app->mq, env);
2529   return GNUNET_OK;
2530 }
2531
2532
2533 /*** APP ***/
2534
2535
2536 static void
2537 app_connect (struct GNUNET_SOCIAL_App *app);
2538
2539
2540 static void
2541 app_reconnect (void *cls)
2542 {
2543   app_connect (cls);
2544 }
2545
2546
2547 /**
2548  * App client disconnected from service.
2549  *
2550  * Reconnect after backoff period.
2551  */
2552 static void
2553 app_disconnected (void *cls, enum GNUNET_MQ_Error error)
2554 {
2555   struct GNUNET_SOCIAL_App *app = cls;
2556
2557   LOG (GNUNET_ERROR_TYPE_DEBUG,
2558        "App client disconnected (%d), re-connecting\n",
2559        (int) error);
2560   if (NULL != app->mq)
2561   {
2562     GNUNET_MQ_destroy (app->mq);
2563     app->mq = NULL;
2564   }
2565
2566   app->reconnect_task = GNUNET_SCHEDULER_add_delayed (app->reconnect_delay,
2567                                                       app_reconnect,
2568                                                       app);
2569   app->reconnect_delay = GNUNET_TIME_STD_BACKOFF (app->reconnect_delay);
2570 }
2571
2572
2573 static void
2574 app_connect (struct GNUNET_SOCIAL_App *app)
2575 {
2576   struct GNUNET_MQ_MessageHandler handlers[] = {
2577     GNUNET_MQ_hd_var_size (app_ego,
2578                            GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
2579                            struct AppEgoMessage,
2580                            app),
2581     GNUNET_MQ_hd_fixed_size (app_ego_end,
2582                              GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
2583                              struct GNUNET_MessageHeader,
2584                              app),
2585     GNUNET_MQ_hd_var_size (app_place,
2586                            GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
2587                            struct AppPlaceMessage,
2588                            app),
2589     GNUNET_MQ_hd_fixed_size (app_place_end,
2590                              GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
2591                              struct GNUNET_MessageHeader,
2592                              app),
2593     GNUNET_MQ_hd_var_size (app_result,
2594                            GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
2595                            struct GNUNET_OperationResultMessage,
2596                            app),
2597     GNUNET_MQ_handler_end ()
2598   };
2599
2600   app->mq = GNUNET_CLIENT_connecT (app->cfg, "social",
2601                                    handlers, app_disconnected, app);
2602   GNUNET_assert (NULL != app->mq);
2603   GNUNET_MQ_send_copy (app->mq, app->connect_env);
2604 }
2605
2606
2607 /**
2608  * Connect application to the social service.
2609  *
2610  * The @host_place_cb and @guest_place_cb functions are
2611  * initially called for each entered places,
2612  * then later each time a new place is entered with the current application ID.
2613  *
2614  * @param cfg
2615  *        Configuration.
2616  * @param id
2617  *        Application ID.
2618  * @param ego_cb
2619  *        Function to notify about an available ego.
2620  * @param host_cb
2621  *        Function to notify about a place entered as host.
2622  * @param guest_cb
2623  *        Function to notify about a place entered as guest.
2624  * @param cls
2625  *        Closure for the callbacks.
2626  *
2627  * @return Handle that can be used to stop listening.
2628  */
2629 struct GNUNET_SOCIAL_App *
2630 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2631                            const char *id,
2632                            GNUNET_SOCIAL_AppEgoCallback ego_cb,
2633                            GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2634                            GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2635                            GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2636                            void *cls)
2637 {
2638   uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2639   if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2640     return NULL;
2641   app_id_size++;
2642
2643   struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2644   app->cfg = cfg;
2645   app->ego_cb = ego_cb;
2646   app->host_cb = host_cb;
2647   app->guest_cb = guest_cb;
2648   app->connected_cb = connected_cb;
2649   app->cb_cls = cls;
2650   app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2651   app->op = GNUNET_OP_create ();
2652   app->id = GNUNET_malloc (app_id_size);
2653   GNUNET_memcpy (app->id, id, app_id_size);
2654
2655   struct AppConnectRequest *creq;
2656   app->connect_env = GNUNET_MQ_msg_extra (creq, app_id_size,
2657                                           GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2658   GNUNET_memcpy (&creq[1], app->id, app_id_size);
2659
2660   app_connect (app);
2661   return app;
2662 }
2663
2664
2665 static void
2666 app_cleanup (struct GNUNET_SOCIAL_App *app)
2667 {
2668   if (NULL != app->mq)
2669   {
2670     GNUNET_MQ_destroy (app->mq);
2671     app->mq = NULL;
2672   }
2673   if (NULL != app->disconnect_cb)
2674   {
2675     app->disconnect_cb (app->disconnect_cls);
2676     app->disconnect_cb = NULL;
2677   }
2678   GNUNET_free (app);
2679 }
2680
2681 /**
2682  * Disconnect application.
2683  *
2684  * @param app
2685  *        Application handle.
2686  * @param disconnect_cb
2687  *        Disconnect callback.
2688  * @param disconnect_cls
2689  *        Disconnect closure.
2690  */
2691 void
2692 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2693                               GNUNET_ContinuationCallback disconnect_cb,
2694                               void *disconnect_cls)
2695 {
2696   app->disconnect_cb = disconnect_cb;
2697   app->disconnect_cls = disconnect_cls;
2698
2699   if (NULL != app->mq)
2700   {
2701     struct GNUNET_MQ_Envelope *env = GNUNET_MQ_get_last_envelope (app->mq);
2702     if (NULL != env)
2703     {
2704       GNUNET_MQ_notify_sent (env, (GNUNET_MQ_NotifyCallback) app_cleanup, app);
2705     }
2706     else
2707     {
2708       app_cleanup (app);
2709     }
2710   }
2711   else
2712   {
2713     app_cleanup (app);
2714   }
2715 }
2716
2717
2718 /**
2719  * Detach application from a place.
2720  *
2721  * Removes the place from the entered places list for this application.
2722  * Note: this does not disconnect from the place.
2723  *
2724  * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2725  *
2726  * @param app
2727  *        Application.
2728  * @param plc
2729  *        Place.
2730  */
2731 void
2732 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2733                           struct GNUNET_SOCIAL_Place *plc)
2734 {
2735   struct AppDetachRequest *dreq;
2736   struct GNUNET_MQ_Envelope *
2737     env = GNUNET_MQ_msg (dreq, GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2738   dreq->place_pub_key = plc->pub_key;
2739   dreq->ego_pub_key = plc->ego_pub_key;
2740
2741   GNUNET_MQ_send (app->mq, env);
2742 }
2743
2744
2745 /* end of social_api.c */