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