926995f6bb03a73a7fb47ac066ddc44945709286
[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     struct GNUNET_SOCIAL_HostConnection *hconn = GNUNET_malloc (sizeof (*hconn));
971     hconn->app = app;
972     hconn->plc_msg = *pmsg;
973     if (NULL != app->host_cb)
974       app->host_cb (app->cb_cls, hconn, ego, &pmsg->place_pub_key, pmsg->place_state);
975   }
976   else
977   {
978     struct GNUNET_SOCIAL_GuestConnection *gconn = GNUNET_malloc (sizeof (*gconn));
979     gconn->app = app;
980     gconn->plc_msg = *pmsg;
981     if (NULL != app->guest_cb)
982       app->guest_cb (app->cb_cls, gconn, ego, &pmsg->place_pub_key, pmsg->place_state);
983   }
984 }
985
986
987 static void
988 app_recv_place_end (void *cls,
989                   struct GNUNET_CLIENT_MANAGER_Connection *client,
990                   const struct GNUNET_MessageHeader *msg)
991 {
992   struct GNUNET_SOCIAL_App *
993     app = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*app));
994
995   if (NULL != app->connected_cb)
996     app->connected_cb (app->cb_cls);
997 }
998
999
1000 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
1001 {
1002   { host_recv_enter_ack, NULL,
1003     GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
1004     sizeof (struct HostEnterAck), GNUNET_NO },
1005
1006   { host_recv_enter_request, NULL,
1007     GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1008     sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1009
1010   { host_recv_message, NULL,
1011     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1012     sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1013
1014   { place_recv_message_ack, NULL,
1015     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1016     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1017
1018   { place_recv_history_result, NULL,
1019     GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1020     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1021
1022   { place_recv_state_result, NULL,
1023     GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1024     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1025
1026   { place_recv_result, NULL,
1027     GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1028     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1029
1030   { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1031
1032   { NULL, NULL, 0, 0, GNUNET_NO }
1033 };
1034
1035
1036 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
1037 {
1038   { guest_recv_enter_ack, NULL,
1039     GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
1040     sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
1041
1042   { host_recv_enter_request, NULL,
1043     GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
1044     sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
1045
1046   { place_recv_message, NULL,
1047     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
1048     sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
1049
1050   { place_recv_message_ack, NULL,
1051     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
1052     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1053
1054   { guest_recv_join_decision, NULL,
1055     GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
1056     sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1057
1058   { place_recv_history_result, NULL,
1059     GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1060     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1061
1062   { place_recv_state_result, NULL,
1063     GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1064     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1065
1066   { place_recv_result, NULL,
1067     GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1068     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1069
1070   { place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1071
1072   { NULL, NULL, 0, 0, GNUNET_NO }
1073 };
1074
1075
1076 static struct GNUNET_CLIENT_MANAGER_MessageHandler app_handlers[] =
1077 {
1078   { app_recv_ego, NULL,
1079     GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO,
1080     sizeof (struct AppEgoMessage), GNUNET_YES },
1081
1082   { app_recv_ego_end, NULL,
1083     GNUNET_MESSAGE_TYPE_SOCIAL_APP_EGO_END,
1084     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1085
1086   { app_recv_place, NULL,
1087     GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE,
1088     sizeof (struct AppPlaceMessage), GNUNET_NO },
1089
1090   { app_recv_place_end, NULL,
1091     GNUNET_MESSAGE_TYPE_SOCIAL_APP_PLACE_END,
1092     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
1093
1094   { app_recv_result, NULL,
1095     GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1096     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1097
1098   { app_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1099
1100   { NULL, NULL, 0, 0, GNUNET_NO }
1101 };
1102
1103
1104 static void
1105 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1106 {
1107   struct GNUNET_HashCode place_pub_hash;
1108   GNUNET_CRYPTO_hash (&plc->pub_key, sizeof (plc->pub_key), &place_pub_hash);
1109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110               "%s place cleanup: %s\n",
1111               GNUNET_YES == plc->is_host ? "host" : "guest",
1112               GNUNET_h2s (&place_pub_hash));
1113
1114   if (NULL != plc->tmit)
1115     GNUNET_PSYC_transmit_destroy (plc->tmit);
1116   if (NULL != plc->connect_msg)
1117     GNUNET_free (plc->connect_msg);
1118   if (NULL != plc->disconnect_cb)
1119     plc->disconnect_cb (plc->disconnect_cls);
1120 }
1121
1122
1123 static void
1124 host_cleanup (void *cls)
1125 {
1126   struct GNUNET_SOCIAL_Host *hst = cls;
1127   place_cleanup (&hst->plc);
1128   if (NULL != hst->slicer)
1129   {
1130     GNUNET_PSYC_slicer_destroy (hst->slicer);
1131     hst->slicer = NULL;
1132   }
1133   GNUNET_free (hst);
1134 }
1135
1136
1137 static void
1138 guest_cleanup (void *cls)
1139 {
1140   struct GNUNET_SOCIAL_Guest *gst = cls;
1141   place_cleanup (&gst->plc);
1142   GNUNET_free (gst);
1143 }
1144
1145
1146 /*** HOST ***/
1147
1148 /**
1149  * Enter a place as host.
1150  *
1151  * A place is created upon first entering, and it is active until permanently
1152  * left using GNUNET_SOCIAL_host_leave().
1153  *
1154  * @param app
1155  *        Application handle.
1156  * @param ego
1157  *        Identity of the host.
1158  * @param place_key
1159  *        Private-public key pair of the place.
1160  *        NULL to generate a key.
1161  * @param policy
1162  *        Policy specifying entry and history restrictions for the place.
1163  * @param slicer
1164  *        Slicer to handle incoming messages.
1165  * @param enter_cb
1166  *        Function called when the place is entered and ready to use.
1167  * @param answer_door_cb
1168  *        Function to handle new nyms that want to enter.
1169  * @param farewell_cb
1170  *        Function to handle departing nyms.
1171  * @param cls
1172  *        Closure for the callbacks.
1173  *
1174  * @return Handle for the host.
1175  */
1176 struct GNUNET_SOCIAL_Host *
1177 GNUNET_SOCIAL_host_enter (const struct GNUNET_SOCIAL_App *app,
1178                           const struct GNUNET_SOCIAL_Ego *ego,
1179                           enum GNUNET_PSYC_Policy policy,
1180                           struct GNUNET_PSYC_Slicer *slicer,
1181                           GNUNET_SOCIAL_HostEnterCallback enter_cb,
1182                           GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1183                           GNUNET_SOCIAL_FarewellCallback farewell_cb,
1184                           void *cls)
1185 {
1186   struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1187   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1188
1189   plc->cfg = app->cfg;
1190   plc->is_host = GNUNET_YES;
1191   plc->slicer = slicer;
1192
1193   hst->enter_cb = enter_cb;
1194   hst->answer_door_cb = answer_door_cb;
1195   hst->farewell_cb = farewell_cb;
1196   hst->cb_cls = cls;
1197
1198   plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1199   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1200
1201   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1202
1203   hst->slicer = GNUNET_PSYC_slicer_create ();
1204   GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1205                                  host_recv_notice_place_leave_method,
1206                                  host_recv_notice_place_leave_modifier,
1207                                  NULL, host_recv_notice_place_leave_eom, hst);
1208
1209   uint16_t app_id_size = strlen (app->id) + 1;
1210   struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1211   hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1212   hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1213   hreq->policy = policy;
1214   hreq->ego_pub_key = ego->pub_key;
1215   GNUNET_memcpy (&hreq[1], app->id, app_id_size);
1216
1217   plc->connect_msg = &hreq->header;
1218   place_send_connect_msg (plc);
1219
1220   return hst;
1221 }
1222
1223
1224 /**
1225  * Reconnect to an already entered place as host.
1226  *
1227  * @param hconn
1228  *        Host connection handle.
1229  *        @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppHostPlaceCallback()
1230  * @param slicer
1231  *        Slicer to handle incoming messages.
1232  * @param enter_cb
1233  *        Function called when the place is entered and ready to use.
1234  * @param answer_door_cb
1235  *        Function to handle new nyms that want to enter.
1236  * @param farewell_cb
1237  *        Function to handle departing nyms.
1238  * @param cls
1239  *        Closure for the callbacks.
1240  *
1241  * @return Handle for the host.
1242  */
1243  struct GNUNET_SOCIAL_Host *
1244 GNUNET_SOCIAL_host_enter_reconnect (struct GNUNET_SOCIAL_HostConnection *hconn,
1245                                     struct GNUNET_PSYC_Slicer *slicer,
1246                                     GNUNET_SOCIAL_HostEnterCallback enter_cb,
1247                                     GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1248                                     GNUNET_SOCIAL_FarewellCallback farewell_cb,
1249                                     void *cls)
1250 {
1251   struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1252   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1253
1254   size_t app_id_size = strlen (hconn->app->id) + 1;
1255   struct HostEnterRequest *hreq = GNUNET_malloc (sizeof (*hreq) + app_id_size);
1256
1257   hst->enter_cb = enter_cb;
1258   hst->answer_door_cb = answer_door_cb;
1259   hst->farewell_cb = farewell_cb;
1260   hst->cb_cls = cls;
1261
1262   plc->cfg = hconn->app->cfg;
1263   plc->is_host = GNUNET_YES;
1264   plc->slicer = slicer;
1265   plc->pub_key = hconn->plc_msg.place_pub_key;
1266   plc->ego_pub_key = hconn->plc_msg.ego_pub_key;
1267
1268   plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", host_handlers);
1269   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1270
1271   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1272
1273   hst->slicer = GNUNET_PSYC_slicer_create ();
1274   GNUNET_PSYC_slicer_method_add (hst->slicer, "_notice_place_leave", NULL,
1275                                  host_recv_notice_place_leave_method,
1276                                  host_recv_notice_place_leave_modifier,
1277                                  NULL, host_recv_notice_place_leave_eom, hst);
1278
1279   hreq->header.size = htons (sizeof (*hreq) + app_id_size);
1280   hreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1281   hreq->place_pub_key = hconn->plc_msg.place_pub_key;
1282   hreq->ego_pub_key = hconn->plc_msg.ego_pub_key;
1283   GNUNET_memcpy (&hreq[1], hconn->app->id, app_id_size);
1284
1285   plc->connect_msg = &hreq->header;
1286   place_send_connect_msg (plc);
1287
1288   GNUNET_free (hconn);
1289   return hst;
1290 }
1291
1292
1293 /**
1294  * Decision whether to admit @a nym into the place or refuse entry.
1295  *
1296  * @param hst
1297  *        Host of the place.
1298  * @param nym
1299  *        Handle for the entity that wanted to enter.
1300  * @param is_admitted
1301  *        #GNUNET_YES    if @a nym is admitted,
1302  *        #GNUNET_NO     if @a nym is refused entry,
1303  *        #GNUNET_SYSERR if we cannot answer the request.
1304  * @param method_name
1305  *        Method name for the rejection message.
1306  * @param env
1307  *        Environment containing variables for the message, or NULL.
1308  * @param data
1309  *        Data for the rejection message to send back.
1310  * @param data_size
1311  *        Number of bytes in @a data for method.
1312  * @return #GNUNET_OK on success,
1313  *         #GNUNET_SYSERR if the message is too large.
1314  */
1315 int
1316 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1317                                    struct GNUNET_SOCIAL_Nym *nym,
1318                                    int is_admitted,
1319                                    const struct GNUNET_PSYC_Message *entry_resp)
1320 {
1321   struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1322   uint16_t entry_resp_size
1323     = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1324
1325   if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1326     return GNUNET_SYSERR;
1327
1328   dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1329   dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1330   dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1331   dcsn->is_admitted = htonl (is_admitted);
1332   dcsn->slave_pub_key = nym->pub_key;
1333
1334   if (0 < entry_resp_size)
1335     GNUNET_memcpy (&dcsn[1], entry_resp, entry_resp_size);
1336
1337   GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1338   GNUNET_free (dcsn);
1339   return GNUNET_OK;
1340 }
1341
1342
1343 /**
1344  * Throw @a nym out of the place.
1345  *
1346  * The @a nym reference will remain valid until the
1347  * #GNUNET_SOCIAL_FarewellCallback is invoked,
1348  * which should be very soon after this call.
1349  *
1350  * @param host
1351  *        Host of the place.
1352  * @param nym
1353  *        Handle for the entity to be ejected.
1354  * @param env
1355  *        Environment for the message or NULL.
1356  */
1357 void
1358 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *hst,
1359                           const struct GNUNET_SOCIAL_Nym *nym,
1360                           struct GNUNET_PSYC_Environment *e)
1361 {
1362   struct GNUNET_PSYC_Environment *env = e;
1363   if (NULL == env)
1364     env = GNUNET_PSYC_env_create ();
1365   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
1366                        "_nym", &nym->pub_key, sizeof (nym->pub_key));
1367   GNUNET_SOCIAL_host_announce (hst, "_notice_place_leave", env, NULL, NULL,
1368                                GNUNET_SOCIAL_ANNOUNCE_NONE);
1369   if (NULL == e)
1370     GNUNET_PSYC_env_destroy (env);
1371 }
1372
1373
1374 /**
1375  * Get the public key of @a ego.
1376  *
1377  * @param ego
1378  *        Ego.
1379  *
1380  * @return Public key of ego.
1381  */
1382 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1383 GNUNET_SOCIAL_ego_get_pub_key (const struct GNUNET_SOCIAL_Ego *ego)
1384 {
1385   return &ego->pub_key;
1386 }
1387
1388
1389 /**
1390  * Get the hash of the public key of @a ego.
1391  *
1392  * @param ego
1393  *        Ego.
1394  *
1395  * @return Hash of the public key of @a ego.
1396  */
1397 const struct GNUNET_HashCode *
1398 GNUNET_SOCIAL_ego_get_pub_key_hash (const struct GNUNET_SOCIAL_Ego *ego)
1399 {
1400   return &ego->pub_key_hash;
1401 }
1402
1403
1404 /**
1405  * Get the name of @a ego.
1406  *
1407  * @param ego
1408  *        Ego.
1409  *
1410  * @return Public key of @a ego.
1411  */
1412 const char *
1413 GNUNET_SOCIAL_ego_get_name (const struct GNUNET_SOCIAL_Ego *ego)
1414 {
1415   return ego->name;
1416 }
1417
1418
1419 /**
1420  * Get the public key of @a nym.
1421  *
1422  * Suitable, for example, to be used with GNUNET_SOCIAL_zone_add_nym().
1423  *
1424  * @param nym
1425  *        Pseudonym.
1426  *
1427  * @return Public key of @a nym.
1428  */
1429 const struct GNUNET_CRYPTO_EcdsaPublicKey *
1430 GNUNET_SOCIAL_nym_get_pub_key (const struct GNUNET_SOCIAL_Nym *nym)
1431 {
1432   return &nym->pub_key;
1433 }
1434
1435
1436 /**
1437  * Get the hash of the public key of @a nym.
1438  *
1439  * @param nym
1440  *        Pseudonym.
1441  *
1442  * @return Hash of the public key of @a nym.
1443  */
1444 const struct GNUNET_HashCode *
1445 GNUNET_SOCIAL_nym_get_pub_key_hash (const struct GNUNET_SOCIAL_Nym *nym)
1446 {
1447   return &nym->pub_key_hash;
1448 }
1449
1450
1451 /**
1452  * Send a message to all nyms that are present in the place.
1453  *
1454  * This function is restricted to the host.  Nyms can only send requests
1455  * to the host who can decide to relay it to everyone in the place.
1456  *
1457  * @param host  Host of the place.
1458  * @param method_name Method to use for the announcement.
1459  * @param env  Environment containing variables for the message and operations
1460  *          on objects of the place.  Can be NULL.
1461  * @param notify Function to call to get the payload of the announcement.
1462  * @param notify_cls Closure for @a notify.
1463  * @param flags Flags for this announcement.
1464  *
1465  * @return NULL on error (announcement already in progress?).
1466  */
1467 struct GNUNET_SOCIAL_Announcement *
1468 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1469                              const char *method_name,
1470                              const struct GNUNET_PSYC_Environment *env,
1471                              GNUNET_PSYC_TransmitNotifyData notify_data,
1472                              void *notify_data_cls,
1473                              enum GNUNET_SOCIAL_AnnounceFlags flags)
1474 {
1475   if (GNUNET_OK ==
1476       GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1477                                     NULL, notify_data, notify_data_cls, flags))
1478     return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1479   else
1480     return NULL;
1481 }
1482
1483
1484 /**
1485  * Resume transmitting announcement.
1486  *
1487  * @param a
1488  *        The announcement to resume.
1489  */
1490 void
1491 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1492 {
1493   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1494 }
1495
1496
1497 /**
1498  * Cancel announcement.
1499  *
1500  * @param a
1501  *        The announcement to cancel.
1502  */
1503 void
1504 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1505 {
1506   GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1507 }
1508
1509
1510 /**
1511  * Obtain handle for a hosted place.
1512  *
1513  * The returned handle can be used to access the place API.
1514  *
1515  * @param host  Handle for the host.
1516  *
1517  * @return Handle for the hosted place, valid as long as @a host is valid.
1518  */
1519 struct GNUNET_SOCIAL_Place *
1520 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1521 {
1522   return &hst->plc;
1523 }
1524
1525
1526 void
1527 place_leave (struct GNUNET_SOCIAL_Place *plc)
1528 {
1529   struct GNUNET_MessageHeader msg;
1530   msg.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_PLACE_LEAVE);
1531   msg.size = htons (sizeof (msg));
1532   GNUNET_CLIENT_MANAGER_transmit (plc->client, &msg);
1533 }
1534
1535
1536 void
1537 place_disconnect (struct GNUNET_SOCIAL_Place *plc,
1538                   GNUNET_ContinuationCallback disconnect_cb,
1539                   void *disconnect_cls)
1540 {
1541   plc->disconnect_cb = disconnect_cb;
1542   plc->disconnect_cls = disconnect_cls;
1543
1544   GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1545                                     GNUNET_YES == plc->is_host
1546                                     ? host_cleanup : guest_cleanup,
1547                                     plc);
1548 }
1549
1550
1551 /**
1552  * Disconnect from a home.
1553  *
1554  * Invalidates host handle.
1555  *
1556  * @param hst
1557  *        The host to disconnect.
1558  */
1559 void
1560 GNUNET_SOCIAL_host_disconnect (struct GNUNET_SOCIAL_Host *hst,
1561                                GNUNET_ContinuationCallback disconnect_cb,
1562                                void *cls)
1563 {
1564   place_disconnect (&hst->plc, disconnect_cb, cls);
1565 }
1566
1567
1568 /**
1569  * Stop hosting the home.
1570  *
1571  * Sends a _notice_place_closing announcement to the home.
1572  * Invalidates host handle.
1573  *
1574  * @param hst
1575  *        The host leaving.
1576  * @param env
1577  *        Environment for the message or NULL.
1578  *        _nym is set to @e nym regardless whether an @e env is provided.
1579  * @param disconnect_cb
1580  *        Function called after the host left the place
1581  *        and disconnected from the social service.
1582  * @param cls
1583  *        Closure for @a disconnect_cb.
1584  */
1585 void
1586 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1587                           const struct GNUNET_PSYC_Environment *env,
1588                           GNUNET_ContinuationCallback disconnect_cb,
1589                           void *cls)
1590 {
1591   GNUNET_SOCIAL_host_announce (hst, "_notice_place_closing", env, NULL, NULL,
1592                                GNUNET_SOCIAL_ANNOUNCE_NONE);
1593   place_leave (&hst->plc);
1594   GNUNET_SOCIAL_host_disconnect (hst, disconnect_cb, cls);
1595 }
1596
1597
1598 /*** GUEST ***/
1599
1600 static struct GuestEnterRequest *
1601 guest_enter_request_create (const char *app_id,
1602                             const struct GNUNET_CRYPTO_EcdsaPublicKey *ego_pub_key,
1603                             const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1604                             const struct GNUNET_PeerIdentity *origin,
1605                             size_t relay_count,
1606                             const struct GNUNET_PeerIdentity *relays,
1607                             const struct GNUNET_PSYC_Message *join_msg)
1608 {
1609   uint16_t app_id_size = strlen (app_id) + 1;
1610   uint16_t join_msg_size = ntohs (join_msg->header.size);
1611   uint16_t relay_size = relay_count * sizeof (*relays);
1612
1613   struct GuestEnterRequest *
1614     greq = GNUNET_malloc (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1615
1616   greq->header.size = htons (sizeof (*greq) + app_id_size + relay_size + join_msg_size);
1617   greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1618   greq->place_pub_key = *place_pub_key;
1619   greq->ego_pub_key = *ego_pub_key;
1620   greq->origin = *origin;
1621   greq->relay_count = htonl (relay_count);
1622
1623   char *p = (char *) &greq[1];
1624   GNUNET_memcpy (p, app_id, app_id_size);
1625   p += app_id_size;
1626
1627   if (0 < relay_size)
1628   {
1629     GNUNET_memcpy (p, relays, relay_size);
1630     p += relay_size;
1631   }
1632
1633   GNUNET_memcpy (p, join_msg, join_msg_size);
1634   return greq;
1635 }
1636
1637
1638 /**
1639  * Request entry to a place as a guest.
1640  *
1641  * @param app
1642  *        Application handle.
1643  * @param ego
1644  *        Identity of the guest.
1645  * @param place_pub_key
1646  *        Public key of the place to enter.
1647  * @param flags
1648  *        Flags for the entry.
1649  * @param origin
1650  *        Peer identity of the origin of the underlying multicast group.
1651  * @param relay_count
1652  *        Number of elements in the @a relays array.
1653  * @param relays
1654  *        Relays for the underlying multicast group.
1655  * @param method_name
1656  *        Method name for the message.
1657  * @param env
1658  *        Environment containing variables for the message, or NULL.
1659  * @param data
1660  *        Payload for the message to give to the enter callback.
1661  * @param data_size
1662  *        Number of bytes in @a data.
1663  * @param slicer
1664  *        Slicer to use for processing incoming requests from guests.
1665  *
1666  * @return NULL on errors, otherwise handle for the guest.
1667  */
1668 struct GNUNET_SOCIAL_Guest *
1669 GNUNET_SOCIAL_guest_enter (const struct GNUNET_SOCIAL_App *app,
1670                            const struct GNUNET_SOCIAL_Ego *ego,
1671                            const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
1672                            enum GNUNET_PSYC_SlaveJoinFlags flags,
1673                            const struct GNUNET_PeerIdentity *origin,
1674                            uint32_t relay_count,
1675                            const struct GNUNET_PeerIdentity *relays,
1676                            const struct GNUNET_PSYC_Message *entry_msg,
1677                            struct GNUNET_PSYC_Slicer *slicer,
1678                            GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1679                            GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1680                            void *cls)
1681 {
1682   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1683   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1684
1685   plc->ego_pub_key = ego->pub_key;
1686   plc->pub_key = *place_pub_key;
1687   plc->cfg = app->cfg;
1688   plc->is_host = GNUNET_NO;
1689   plc->slicer = slicer;
1690
1691   gst->enter_cb = local_enter_cb;
1692   gst->entry_dcsn_cb = entry_dcsn_cb;
1693   gst->cb_cls = cls;
1694
1695   plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1696   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1697
1698   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1699
1700   struct GuestEnterRequest *
1701     greq = guest_enter_request_create (app->id, &ego->pub_key, &plc->pub_key,
1702                                        origin, relay_count, relays, entry_msg);
1703   plc->connect_msg = &greq->header;
1704   place_send_connect_msg (plc);
1705   return gst;
1706 }
1707
1708
1709 /**
1710  * Request entry to a place by name as a guest.
1711  *
1712  * @param app
1713  *        Application handle.
1714  * @param ego
1715  *        Identity of the guest.
1716  * @param gns_name
1717  *        GNS name of the place to enter.  Either in the form of
1718  *        'room.friend.gnu', or 'NYMPUBKEY.zkey'.  This latter case refers to
1719  *        the 'PLACE' record of the empty label ("+") in the GNS zone with the
1720  *        nym's public key 'NYMPUBKEY', and can be used to request entry to a
1721  *        pseudonym's place directly.
1722  * @param password
1723  *        Password to decrypt the record, or NULL for cleartext records.
1724  * @param join_msg
1725  *        Entry request message or NULL.
1726  * @param slicer
1727  *        Slicer to use for processing incoming requests from guests.
1728  * @param local_enter_cb
1729  *        Called upon connection established to the social service.
1730  * @param entry_decision_cb
1731  *        Called upon receiving entry decision.
1732  *
1733  * @return NULL on errors, otherwise handle for the guest.
1734  */
1735 struct GNUNET_SOCIAL_Guest *
1736 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_SOCIAL_App *app,
1737                                    const struct GNUNET_SOCIAL_Ego *ego,
1738                                    const char *gns_name,
1739                                    const char *password,
1740                                    const struct GNUNET_PSYC_Message *join_msg,
1741                                    struct GNUNET_PSYC_Slicer *slicer,
1742                                    GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1743                                    GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1744                                    void *cls)
1745 {
1746   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1747   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1748
1749   if (NULL == password)
1750     password = "";
1751
1752   uint16_t app_id_size = strlen (app->id) + 1;
1753   uint16_t gns_name_size = strlen (gns_name) + 1;
1754   uint16_t password_size = strlen (password) + 1;
1755
1756   uint16_t join_msg_size = 0;
1757   if (NULL != join_msg)
1758     join_msg_size = ntohs (join_msg->header.size);
1759
1760   uint16_t greq_size = sizeof (struct GuestEnterByNameRequest)
1761     + app_id_size + gns_name_size + password_size + join_msg_size;
1762   struct GuestEnterByNameRequest *greq = GNUNET_malloc (greq_size);
1763   greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_BY_NAME);
1764   greq->header.size = htons (greq_size);
1765   greq->ego_pub_key = ego->pub_key;
1766
1767   char *p = (char *) &greq[1];
1768   GNUNET_memcpy (p, app->id, app_id_size);
1769   p += app_id_size;
1770   GNUNET_memcpy (p, gns_name, gns_name_size);
1771   p += gns_name_size;
1772   GNUNET_memcpy (p, password, password_size);
1773   p += password_size;
1774   if (NULL != join_msg)
1775     GNUNET_memcpy (p, join_msg, join_msg_size);
1776
1777   gst->enter_cb = local_enter_cb;
1778   gst->entry_dcsn_cb = entry_decision_cb;
1779   gst->cb_cls = cls;
1780
1781   plc->ego_pub_key = ego->pub_key;
1782   plc->cfg = app->cfg;
1783   plc->is_host = GNUNET_NO;
1784   plc->slicer = slicer;
1785
1786   plc->client = GNUNET_CLIENT_MANAGER_connect (app->cfg, "social", guest_handlers);
1787   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1788
1789   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1790
1791   plc->connect_msg = &greq->header;
1792   place_send_connect_msg (plc);
1793
1794   return gst;
1795 }
1796
1797
1798 /**
1799  * Reconnect to an already entered place as guest.
1800  *
1801  * @param gconn
1802  *        Guest connection handle.
1803  *        @see GNUNET_SOCIAL_app_connect() & GNUNET_SOCIAL_AppGuestPlaceCallback()
1804  * @param flags
1805  *        Flags for the entry.
1806  * @param slicer
1807  *        Slicer to use for processing incoming requests from guests.
1808  * @param local_enter_cb
1809  *        Called upon connection established to the social service.
1810  * @param entry_decision_cb
1811  *        Called upon receiving entry decision.
1812  *
1813  * @return NULL on errors, otherwise handle for the guest.
1814  */
1815 struct GNUNET_SOCIAL_Guest *
1816 GNUNET_SOCIAL_guest_enter_reconnect (struct GNUNET_SOCIAL_GuestConnection *gconn,
1817                                      enum GNUNET_PSYC_SlaveJoinFlags flags,
1818                                      struct GNUNET_PSYC_Slicer *slicer,
1819                                      GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1820                                      void *cls)
1821 {
1822   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1823   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1824
1825   uint16_t app_id_size = strlen (gconn->app->id) + 1;
1826   uint16_t greq_size = sizeof (struct GuestEnterRequest) + app_id_size;
1827   struct GuestEnterRequest *greq = GNUNET_malloc (greq_size);
1828   greq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1829   greq->header.size = htons (greq_size);
1830   greq->ego_pub_key = gconn->plc_msg.ego_pub_key;
1831   greq->place_pub_key = gconn->plc_msg.place_pub_key;
1832   greq->flags = htonl (flags);
1833
1834   GNUNET_memcpy (&greq[1], gconn->app->id, app_id_size);
1835
1836   gst->enter_cb = local_enter_cb;
1837   gst->cb_cls = cls;
1838
1839   plc->cfg = gconn->app->cfg;
1840   plc->is_host = GNUNET_NO;
1841   plc->slicer = slicer;
1842   plc->pub_key = gconn->plc_msg.place_pub_key;
1843   plc->ego_pub_key = gconn->plc_msg.ego_pub_key;
1844
1845   plc->client = GNUNET_CLIENT_MANAGER_connect (plc->cfg, "social", guest_handlers);
1846   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1847
1848   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1849
1850   plc->connect_msg = &greq->header;
1851   place_send_connect_msg (plc);
1852
1853   GNUNET_free (gconn);
1854   return gst;
1855 }
1856
1857
1858 /**
1859  * Talk to the host of the place.
1860  *
1861  * @param place
1862  *        Place where we want to talk to the host.
1863  * @param method_name
1864  *        Method to invoke on the host.
1865  * @param env
1866  *        Environment containing variables for the message, or NULL.
1867  * @param notify_data
1868  *        Function to use to get the payload for the method.
1869  * @param notify_data_cls
1870  *        Closure for @a notify_data.
1871  * @param flags
1872  *        Flags for the message being sent.
1873  *
1874  * @return NULL if we are already trying to talk to the host,
1875  *         otherwise handle to cancel the request.
1876  */
1877 struct GNUNET_SOCIAL_TalkRequest *
1878 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1879                           const char *method_name,
1880                           const struct GNUNET_PSYC_Environment *env,
1881                           GNUNET_PSYC_TransmitNotifyData notify_data,
1882                           void *notify_data_cls,
1883                           enum GNUNET_SOCIAL_TalkFlags flags)
1884 {
1885   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1886   GNUNET_assert (NULL != plc->tmit);
1887
1888   if (GNUNET_OK ==
1889       GNUNET_PSYC_transmit_message (plc->tmit, method_name, env,
1890                                     NULL, notify_data, notify_data_cls, flags))
1891     return (struct GNUNET_SOCIAL_TalkRequest *) plc->tmit;
1892   else
1893     return NULL;
1894 }
1895
1896
1897 /**
1898  * Resume talking to the host of the place.
1899  *
1900  * @param tr
1901  *        Talk request to resume.
1902  */
1903 void
1904 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1905 {
1906   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1907 }
1908
1909
1910 /**
1911  * Cancel talking to the host of the place.
1912  *
1913  * @param tr
1914  *        Talk request to cancel.
1915  */
1916 void
1917 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1918 {
1919   GNUNET_PSYC_transmit_cancel ( (struct GNUNET_PSYC_TransmitHandle *) tr);
1920 }
1921
1922
1923 /**
1924  * Disconnect from a place.
1925  *
1926  * Invalidates guest handle.
1927  *
1928  * @param gst
1929  *        The guest to disconnect.
1930  */
1931 void
1932 GNUNET_SOCIAL_guest_disconnect (struct GNUNET_SOCIAL_Guest *gst,
1933                                 GNUNET_ContinuationCallback disconnect_cb,
1934                                 void *cls)
1935 {
1936   place_disconnect (&gst->plc, disconnect_cb, cls);
1937 }
1938
1939
1940 /**
1941  * Leave a place temporarily or permanently.
1942  *
1943  * Notifies the owner of the place about leaving, and destroys the place handle.
1944  *
1945  * @param place
1946  *        Place to leave.
1947  * @param keep_active
1948  *        Keep place active after last application disconnected.
1949  *        #GNUNET_YES or #GNUNET_NO
1950  * @param env
1951  *        Optional environment for the leave message if @a keep_active
1952  *        is #GNUNET_NO.  NULL if not needed.
1953  * @param leave_cb
1954  *        Called upon disconnecting from the social service.
1955  */
1956 void
1957 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1958                            struct GNUNET_PSYC_Environment *env,
1959                            GNUNET_ContinuationCallback disconnect_cb,
1960                            void *cls)
1961 {
1962   GNUNET_SOCIAL_guest_talk (gst, "_notice_place_leave", env, NULL, NULL,
1963                             GNUNET_SOCIAL_TALK_NONE);
1964   place_leave (&gst->plc);
1965   GNUNET_SOCIAL_guest_disconnect (gst, disconnect_cb, cls);
1966 }
1967
1968
1969 /**
1970  * Obtain handle for a place entered as guest.
1971  *
1972  * The returned handle can be used to access the place API.
1973  *
1974  * @param guest  Handle for the guest.
1975  *
1976  * @return Handle for the place, valid as long as @a guest is valid.
1977  */
1978 struct GNUNET_SOCIAL_Place *
1979 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1980 {
1981   return &gst->plc;
1982 }
1983
1984
1985 /**
1986  * Obtain the public key of a place.
1987  *
1988  * @param plc
1989  *        Place.
1990  *
1991  * @return Public key of the place.
1992  */
1993 const struct GNUNET_CRYPTO_EddsaPublicKey *
1994 GNUNET_SOCIAL_place_get_pub_key (const struct GNUNET_SOCIAL_Place *plc)
1995 {
1996   return &plc->pub_key;
1997 }
1998
1999
2000 /**
2001  * Set message processing @a flags for a @a method_prefix.
2002  *
2003  * @param plc
2004  *        Place.
2005  * @param method_prefix
2006  *        Method prefix @a flags apply to.
2007  * @param flags
2008  *        The flags that apply to a matching @a method_prefix.
2009  */
2010 void
2011 GNUNET_SOCIAL_place_msg_proc_set (struct GNUNET_SOCIAL_Place *plc,
2012                                   const char *method_prefix,
2013                                   enum GNUNET_SOCIAL_MsgProcFlags flags)
2014 {
2015   GNUNET_assert (NULL != method_prefix);
2016   struct MsgProcRequest *mpreq;
2017   uint16_t method_size = strnlen (method_prefix,
2018                                   GNUNET_SERVER_MAX_MESSAGE_SIZE
2019                                   - sizeof (*mpreq)) + 1;
2020   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2021   mpreq = GNUNET_malloc (sizeof (*mpreq) + method_size);
2022
2023   mpreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_SET);
2024   mpreq->header.size = htons (sizeof (*mpreq) + method_size);
2025   mpreq->flags = htonl (flags);
2026   GNUNET_memcpy (&mpreq[1], method_prefix, method_size);
2027
2028   GNUNET_CLIENT_MANAGER_transmit (plc->client, &mpreq->header);
2029   GNUNET_free (mpreq);
2030 }
2031
2032
2033 /**
2034  * Clear all message processing flags previously set for this place.
2035  */
2036 void
2037 GNUNET_SOCIAL_place_msg_proc_clear (struct GNUNET_SOCIAL_Place *plc)
2038 {
2039   struct GNUNET_MessageHeader req;
2040   req.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_MSG_PROC_CLEAR);
2041   req.size = htons (sizeof (req));
2042   GNUNET_CLIENT_MANAGER_transmit (plc->client, &req);
2043 }
2044
2045
2046 static struct GNUNET_SOCIAL_HistoryRequest *
2047 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2048                       uint64_t start_message_id,
2049                       uint64_t end_message_id,
2050                       uint64_t message_limit,
2051                       const char *method_prefix,
2052                       uint32_t flags,
2053                       struct GNUNET_PSYC_Slicer *slicer,
2054                       GNUNET_ResultCallback result_cb,
2055                       void *cls)
2056 {
2057   struct GNUNET_PSYC_HistoryRequestMessage *req;
2058   struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
2059   hist->plc = plc;
2060   hist->slicer = slicer;
2061   hist->result_cb = result_cb;
2062   hist->cls = cls;
2063   hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2064                                               &op_recv_history_result, hist);
2065
2066   GNUNET_assert (NULL != method_prefix);
2067   uint16_t method_size = strnlen (method_prefix,
2068                                   GNUNET_SERVER_MAX_MESSAGE_SIZE
2069                                   - sizeof (*req)) + 1;
2070   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
2071   req = GNUNET_malloc (sizeof (*req) + method_size);
2072   req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
2073   req->header.size = htons (sizeof (*req) + method_size);
2074   req->start_message_id = GNUNET_htonll (start_message_id);
2075   req->end_message_id = GNUNET_htonll (end_message_id);
2076   req->message_limit = GNUNET_htonll (message_limit);
2077   req->flags = htonl (flags);
2078   req->op_id = GNUNET_htonll (hist->op_id);
2079   GNUNET_memcpy (&req[1], method_prefix, method_size);
2080
2081   GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2082   GNUNET_free (req);
2083   return hist;
2084 }
2085
2086
2087 /**
2088  * Learn about the history of a place.
2089  *
2090  * Messages are returned through the @a slicer function
2091  * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2092  *
2093  * @param place
2094  *        Place we want to learn more about.
2095  * @param start_message_id
2096  *        First historic message we are interested in.
2097  * @param end_message_id
2098  *        Last historic message we are interested in (inclusive).
2099  * @param method_prefix
2100  *        Only retrieve messages with this method prefix.
2101  * @param flags
2102  *        OR'ed GNUNET_PSYC_HistoryReplayFlags
2103  * @param slicer
2104  *        Slicer to use for retrieved messages.
2105  *        Can be the same as the slicer of the place.
2106  * @param result_cb
2107  *        Function called after all messages retrieved.
2108  *        NULL if not needed.
2109  * @param cls Closure for @a result_cb.
2110  */
2111 struct GNUNET_SOCIAL_HistoryRequest *
2112 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
2113                                     uint64_t start_message_id,
2114                                     uint64_t end_message_id,
2115                                     const char *method_prefix,
2116                                     uint32_t flags,
2117                                     struct GNUNET_PSYC_Slicer *slicer,
2118                                     GNUNET_ResultCallback result_cb,
2119                                     void *cls)
2120 {
2121   return place_history_replay (plc, start_message_id, end_message_id, 0,
2122                                method_prefix, flags, slicer, result_cb, cls);
2123 }
2124
2125
2126 /**
2127  * Learn about the history of a place.
2128  *
2129  * Sends messages through the slicer function of the place where
2130  * start_message_id <= message_id <= end_message_id.
2131  * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
2132  *
2133  * To get the latest message, use 0 for both the start and end message ID.
2134  *
2135  * @param place
2136  *        Place we want to learn more about.
2137  * @param message_limit
2138  *        Maximum number of historic messages we are interested in.
2139  * @param method_prefix
2140  *        Only retrieve messages with this method prefix.
2141  * @param flags
2142  *        OR'ed GNUNET_PSYC_HistoryReplayFlags
2143  * @param result_cb
2144  *        Function called after all messages retrieved.
2145  *        NULL if not needed.
2146  * @param cls Closure for @a result_cb.
2147  */
2148 struct GNUNET_SOCIAL_HistoryRequest *
2149 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
2150                                            uint64_t message_limit,
2151                                            const char *method_prefix,
2152                                            uint32_t flags,
2153                                            struct GNUNET_PSYC_Slicer *slicer,
2154                                            GNUNET_ResultCallback result_cb,
2155                                            void *cls)
2156 {
2157   return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
2158                                slicer, result_cb, cls);
2159 }
2160
2161
2162 /**
2163  * Cancel learning about the history of a place.
2164  *
2165  * @param hist
2166  *        History lesson to cancel.
2167  */
2168 void
2169 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
2170 {
2171   GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
2172   GNUNET_free (hist);
2173 }
2174
2175
2176 /**
2177  * Request matching state variables.
2178  */
2179 static struct GNUNET_SOCIAL_LookHandle *
2180 place_state_get (struct GNUNET_SOCIAL_Place *plc,
2181                  uint16_t type, const char *name,
2182                  GNUNET_PSYC_StateVarCallback var_cb,
2183                  GNUNET_ResultCallback result_cb, void *cls)
2184 {
2185   struct GNUNET_PSYC_StateRequestMessage *req;
2186   struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
2187   look->plc = plc;
2188   look->var_cb = var_cb;
2189   look->result_cb = result_cb;
2190   look->cls = cls;
2191   look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
2192                                               &op_recv_state_result, look);
2193
2194   GNUNET_assert (NULL != name);
2195   size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
2196                               - sizeof (*req)) + 1;
2197   req = GNUNET_malloc (sizeof (*req) + name_size);
2198   req->header.type = htons (type);
2199   req->header.size = htons (sizeof (*req) + name_size);
2200   req->op_id = GNUNET_htonll (look->op_id);
2201   GNUNET_memcpy (&req[1], name, name_size);
2202
2203   GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
2204   GNUNET_free (req);
2205   return look;
2206 }
2207
2208
2209 /**
2210  * Look at a particular object in the place.
2211  *
2212  * The best matching object is returned (its name might be less specific than
2213  * what was requested).
2214  *
2215  * @param place
2216  *        The place where to look.
2217  * @param full_name
2218  *        Full name of the object.
2219  * @param value_size
2220  *        Set to the size of the returned value.
2221  *
2222  * @return NULL if there is no such object at this place.
2223  */
2224 struct GNUNET_SOCIAL_LookHandle *
2225 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
2226                              const char *full_name,
2227                              GNUNET_PSYC_StateVarCallback var_cb,
2228                              GNUNET_ResultCallback result_cb,
2229                              void *cls)
2230 {
2231   return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
2232                           full_name, var_cb, result_cb, cls);
2233 }
2234
2235
2236 /**
2237  * Look for objects in the place with a matching name prefix.
2238  *
2239  * @param place
2240  *        The place where to look.
2241  * @param name_prefix
2242  *        Look at objects with names beginning with this value.
2243  * @param var_cb
2244  *        Function to call for each object found.
2245  * @param cls
2246  *        Closure for callback function.
2247  *
2248  * @return Handle that can be used to stop looking at objects.
2249  */
2250 struct GNUNET_SOCIAL_LookHandle *
2251 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2252                               const char *name_prefix,
2253                               GNUNET_PSYC_StateVarCallback var_cb,
2254                               GNUNET_ResultCallback result_cb,
2255                               void *cls)
2256 {
2257   return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2258                           name_prefix, var_cb, result_cb, cls);
2259 }
2260
2261
2262 /**
2263  * Cancel a state request operation.
2264  *
2265  * @param sr
2266  *        Handle for the operation to cancel.
2267  */
2268 void
2269 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2270 {
2271   GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2272   GNUNET_free (look);
2273 }
2274
2275
2276 static void
2277 op_recv_zone_add_place_result (void *cls, int64_t result,
2278                                const void *err_msg, uint16_t err_msg_size)
2279 {
2280   LOG (GNUNET_ERROR_TYPE_DEBUG,
2281        "Received zone add place result: %" PRId64 ".\n", result);
2282
2283   struct ZoneAddPlaceHandle *add_plc = cls;
2284   if (NULL != add_plc->result_cb)
2285     add_plc->result_cb (add_plc->result_cls, result, err_msg, err_msg_size);
2286
2287   GNUNET_free (add_plc->req);
2288   GNUNET_free (add_plc);
2289 }
2290
2291
2292 /**
2293  * Advertise @e place in the GNS zone of @e ego.
2294  *
2295  * @param app
2296  *        Application handle.
2297  * @param ego
2298  *        Ego.
2299  * @param place_pub_key
2300  *        Public key of place to add.
2301  * @param name
2302  *        The name for the PLACE record to put in the zone.
2303  * @param password
2304  *        Password used to encrypt the record or NULL to keep it cleartext.
2305  * @param relay_count
2306  *        Number of elements in the @a relays array.
2307  * @param relays
2308  *        List of relays to put in the PLACE record to advertise
2309  *        as entry points to the place in addition to the origin.
2310  * @param expiration_time
2311  *        Expiration time of the record, use 0 to remove the record.
2312  * @param result_cb
2313  *        Function called with the result of the operation.
2314  * @param result_cls
2315  *        Closure for @a result_cb
2316  *
2317  * @return #GNUNET_OK if the request was sent,
2318  *         #GNUNET_SYSERR on error, e.g. the name/password is too long.
2319  */
2320 int
2321 GNUNET_SOCIAL_zone_add_place (const struct GNUNET_SOCIAL_App *app,
2322                               const struct GNUNET_SOCIAL_Ego *ego,
2323                               const char *name,
2324                               const char *password,
2325                               const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
2326                               const struct GNUNET_PeerIdentity *origin,
2327                               uint32_t relay_count,
2328                               const struct GNUNET_PeerIdentity *relays,
2329                               struct GNUNET_TIME_Absolute expiration_time,
2330                               GNUNET_ResultCallback result_cb,
2331                               void *result_cls)
2332 {
2333   struct ZoneAddPlaceRequest *preq;
2334   size_t name_size = strlen (name) + 1;
2335   size_t password_size = strlen (password) + 1;
2336   size_t relay_size = relay_count * sizeof (*relays);
2337   size_t preq_size = sizeof (*preq) + name_size + password_size + relay_size;
2338
2339   if (GNUNET_SERVER_MAX_MESSAGE_SIZE < preq_size)
2340     return GNUNET_SYSERR;
2341
2342   preq = GNUNET_malloc (preq_size);
2343   preq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_PLACE);
2344   preq->header.size = htons (preq_size);
2345   preq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2346   preq->ego_pub_key = ego->pub_key;
2347   preq->place_pub_key = *place_pub_key;
2348   preq->origin = *origin;
2349   preq->relay_count = htonl (relay_count);
2350
2351   char *p = (char *) &preq[1];
2352   GNUNET_memcpy (p, name, name_size);
2353   p += name_size;
2354   GNUNET_memcpy (p, password, password_size);
2355   p += password_size;
2356   GNUNET_memcpy (p, relays, relay_size);
2357
2358   struct ZoneAddPlaceHandle * add_plc = GNUNET_malloc (sizeof (*add_plc));
2359   add_plc->req = preq;
2360   add_plc->result_cb = result_cb;
2361   add_plc->result_cls = result_cls;
2362
2363   preq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2364                                                              op_recv_zone_add_place_result,
2365                                                              add_plc));
2366   GNUNET_CLIENT_MANAGER_transmit_now (app->client, &preq->header);
2367   return GNUNET_OK;
2368 }
2369
2370
2371 static void
2372 op_recv_zone_add_nym_result (void *cls, int64_t result,
2373                              const void *err_msg, uint16_t err_msg_size)
2374 {
2375   LOG (GNUNET_ERROR_TYPE_DEBUG,
2376        "Received zone add nym result: %" PRId64 ".\n", result);
2377
2378   struct ZoneAddNymHandle *add_nym = cls;
2379   if (NULL != add_nym->result_cb)
2380     add_nym->result_cb (add_nym->result_cls, result, err_msg, err_msg_size);
2381
2382   GNUNET_free (add_nym->req);
2383   GNUNET_free (add_nym);
2384 }
2385
2386
2387 /**
2388  * Add nym to the GNS zone of @e ego.
2389  *
2390  * @param cfg
2391  *        Configuration.
2392  * @param ego
2393  *        Ego.
2394  * @param name
2395  *        The name for the PKEY record to put in the zone.
2396  * @param nym_pub_key
2397  *        Public key of nym to add.
2398  * @param expiration_time
2399  *        Expiration time of the record, use 0 to remove the record.
2400  * @param result_cb
2401  *        Function called with the result of the operation.
2402  * @param result_cls
2403  *        Closure for @a result_cb
2404  *
2405  * @return #GNUNET_OK if the request was sent,
2406  *         #GNUNET_SYSERR on error, e.g. the name is too long.
2407  */
2408 int
2409 GNUNET_SOCIAL_zone_add_nym (const struct GNUNET_SOCIAL_App *app,
2410                             const struct GNUNET_SOCIAL_Ego *ego,
2411                             const char *name,
2412                             const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_pub_key,
2413                             struct GNUNET_TIME_Absolute expiration_time,
2414                             GNUNET_ResultCallback result_cb,
2415                             void *result_cls)
2416 {
2417   struct ZoneAddNymRequest *nreq;
2418
2419   size_t name_size = strlen (name) + 1;
2420   if (GNUNET_SERVER_MAX_MESSAGE_SIZE < sizeof (*nreq) + name_size)
2421     return GNUNET_SYSERR;
2422
2423   nreq = GNUNET_malloc (sizeof (*nreq) + name_size);
2424   nreq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_ZONE_ADD_NYM);
2425   nreq->header.size = htons (sizeof (*nreq) + name_size);
2426   nreq->expiration_time = GNUNET_htonll (expiration_time.abs_value_us);
2427   nreq->ego_pub_key = ego->pub_key;
2428   nreq->nym_pub_key = *nym_pub_key;
2429   GNUNET_memcpy (&nreq[1], name, name_size);
2430
2431   struct ZoneAddNymHandle * add_nym = GNUNET_malloc (sizeof (*add_nym));
2432   add_nym->req = nreq;
2433   add_nym->result_cb = result_cb;
2434   add_nym->result_cls = result_cls;
2435
2436   nreq->op_id = GNUNET_htonll (GNUNET_CLIENT_MANAGER_op_add (app->client,
2437                                                              op_recv_zone_add_nym_result,
2438                                                              add_nym));
2439   GNUNET_CLIENT_MANAGER_transmit_now (app->client, &nreq->header);
2440   return GNUNET_OK;
2441 }
2442
2443
2444 /**
2445  * Connect application to the social service.
2446  *
2447  * The @host_place_cb and @guest_place_cb functions are
2448  * initially called for each entered places,
2449  * then later each time a new place is entered with the current application ID.
2450  *
2451  * @param cfg
2452  *        Configuration.
2453  * @param id
2454  *        Application ID.
2455  * @param notify_host
2456  *        Function to notify about a place entered as host.
2457  * @param notify_guest
2458  *        Function to notify about a place entered as guest..
2459  * @param notify_cls
2460  *        Closure for the callbacks.
2461  *
2462  * @return Handle that can be used to stop listening.
2463  */
2464 struct GNUNET_SOCIAL_App *
2465 GNUNET_SOCIAL_app_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
2466                            const char *id,
2467                            GNUNET_SOCIAL_AppEgoCallback ego_cb,
2468                            GNUNET_SOCIAL_AppHostPlaceCallback host_cb,
2469                            GNUNET_SOCIAL_AppGuestPlaceCallback guest_cb,
2470                            GNUNET_SOCIAL_AppConnectedCallback connected_cb,
2471                            void *cls)
2472 {
2473   uint16_t app_id_size = strnlen (id, GNUNET_SOCIAL_APP_MAX_ID_SIZE);
2474   if (GNUNET_SOCIAL_APP_MAX_ID_SIZE == app_id_size)
2475     return NULL;
2476   app_id_size++;
2477
2478   struct GNUNET_SOCIAL_App *app = GNUNET_malloc (sizeof *app);
2479   app->cfg = cfg;
2480   app->ego_cb = ego_cb;
2481   app->host_cb = host_cb;
2482   app->guest_cb = guest_cb;
2483   app->connected_cb = connected_cb;
2484   app->cb_cls = cls;
2485   app->egos = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
2486   app->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social",
2487                                                app_handlers);
2488   GNUNET_CLIENT_MANAGER_set_user_context_ (app->client, app, sizeof (*app));
2489
2490   app->id = GNUNET_malloc (app_id_size);
2491   GNUNET_memcpy (app->id, id, app_id_size);
2492
2493   struct AppConnectRequest *creq = GNUNET_malloc (sizeof (*creq) + app_id_size);
2494   creq->header.size = htons (sizeof (*creq) + app_id_size);
2495   creq->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_CONNECT);
2496   GNUNET_memcpy (&creq[1], app->id, app_id_size);
2497
2498   app->connect_msg = &creq->header;
2499   app_send_connect_msg (app);
2500
2501   return app;
2502 }
2503
2504
2505 /**
2506  * Disconnect application.
2507  *
2508  * @param app
2509  *        Application handle.
2510  */
2511 void
2512 GNUNET_SOCIAL_app_disconnect (struct GNUNET_SOCIAL_App *app,
2513                               GNUNET_ContinuationCallback disconnect_cb,
2514                               void *disconnect_cls)
2515 {
2516   GNUNET_CLIENT_MANAGER_disconnect (app->client, GNUNET_NO,
2517                                     disconnect_cb, disconnect_cls);
2518 }
2519
2520
2521 /**
2522  * Detach application from a place.
2523  *
2524  * Removes the place from the entered places list for this application.
2525  * Note: this does not disconnect from the place.
2526  *
2527  * @see GNUNET_SOCIAL_host_disconnect() and GNUNET_SOCIAL_guest_disconnect()
2528  *
2529  * @param app
2530  *        Application.
2531  * @param plc
2532  *        Place.
2533  */
2534 void
2535 GNUNET_SOCIAL_app_detach (struct GNUNET_SOCIAL_App *app,
2536                           struct GNUNET_SOCIAL_Place *plc)
2537 {
2538   struct AppDetachRequest dreq;
2539   dreq.header.size = htons (sizeof (dreq));
2540   dreq.header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_APP_DETACH);
2541   dreq.place_pub_key = plc->pub_key;
2542   GNUNET_CLIENT_MANAGER_transmit_now (plc->client, &dreq.header);
2543 }
2544
2545
2546 /* end of social_api.c */