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