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