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