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