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