-improve indentation, reduce duplication of PIDs in core's neighbour map
[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 place where social interactions happen.
53  */
54 struct GNUNET_SOCIAL_Place
55 {
56   /**
57    * Configuration to use.
58    */
59   const struct GNUNET_CONFIGURATION_Handle *cfg;
60
61   /**
62    * Client connection to the service.
63    */
64   struct GNUNET_CLIENT_MANAGER_Connection *client;
65
66   /**
67    * Transmission handle;
68    */
69   struct GNUNET_PSYC_TransmitHandle *tmit;
70
71   /**
72    * Receipt handle;
73    */
74   struct GNUNET_PSYC_ReceiveHandle *recv;
75
76   /**
77    * Message to send on reconnect.
78    */
79   struct GNUNET_MessageHeader *connect_msg;
80
81   /**
82    * Slicer for processing incoming methods.
83    */
84   struct GNUNET_SOCIAL_Slicer *slicer;
85
86   /**
87    * Function called after disconnected from the service.
88    */
89   GNUNET_ContinuationCallback disconnect_cb;
90
91   /**
92    * Closure for @a disconnect_cb.
93    */
94   void *disconnect_cls;
95
96   /**
97    * Public key of the place.
98    */
99   struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
100
101   /**
102    * Private key of the ego.
103    */
104   struct GNUNET_CRYPTO_EcdsaPrivateKey ego_key;
105
106   /**
107    * Does this place belong to a host (#GNUNET_YES) or guest (#GNUNET_NO)?
108    */
109   uint8_t is_host;
110
111   /**
112    * Is this place in the process of disconnecting from the service?
113    * #GNUNET_YES or #GNUNET_NO
114    */
115   uint8_t is_disconnecting;
116 };
117
118
119 /**
120  * Host handle for a place that we entered.
121  */
122 struct GNUNET_SOCIAL_Host
123 {
124   struct GNUNET_SOCIAL_Place plc;
125
126   struct GNUNET_CRYPTO_EddsaPrivateKey place_key;
127
128   GNUNET_SOCIAL_HostEnterCallback enter_cb;
129
130   GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb;
131
132   GNUNET_SOCIAL_FarewellCallback farewell_cb;
133
134   /**
135    * Closure for callbacks.
136    */
137   void *cb_cls;
138 };
139
140
141 /**
142  * Guest handle for place that we entered.
143  */
144 struct GNUNET_SOCIAL_Guest
145 {
146   struct GNUNET_SOCIAL_Place plc;
147
148   GNUNET_SOCIAL_GuestEnterCallback enter_cb;
149
150   GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb;
151
152   /**
153    * Closure for callbacks.
154    */
155   void *cb_cls;
156 };
157
158
159 /**
160  * Handle for a pseudonym of another user in the network.
161  */
162 struct GNUNET_SOCIAL_Nym
163 {
164   struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
165   struct GNUNET_HashCode pub_key_hash;
166 };
167
168
169 /**
170  * Hash map of all nyms.
171  * pub_key_hash -> struct GNUNET_SOCIAL_Nym *
172  */
173 struct GNUNET_CONTAINER_MultiHashMap *nyms;
174
175
176 /**
177  * Handle for a try-and-slice instance.
178  */
179 struct GNUNET_SOCIAL_Slicer
180 {
181   /**
182    * Message handlers: method_name -> SlicerCallbacks
183    */
184   struct GNUNET_CONTAINER_MultiHashMap *handlers;
185
186
187   /**
188    * Currently being processed message part.
189    */
190   const struct GNUNET_MessageHeader *msg;
191
192   /**
193    * ID of currently being received message.
194    */
195   uint64_t message_id;
196
197   /**
198    * Method name of currently being received message.
199    */
200   char *method_name;
201
202   /**
203    * Public key of the nym the current message originates from.
204    */
205   struct GNUNET_CRYPTO_EcdsaPublicKey nym_key;
206
207   /**
208    * Size of @a method_name (including terminating \0).
209    */
210   uint16_t method_name_size;
211 };
212
213
214 /**
215  * Callbacks for a slicer method handler.
216  */
217 struct SlicerCallbacks
218 {
219   GNUNET_SOCIAL_MethodCallback method_cb;
220   GNUNET_SOCIAL_ModifierCallback modifier_cb;
221   GNUNET_SOCIAL_DataCallback data_cb;
222   GNUNET_SOCIAL_EndOfMessageCallback eom_cb;
223   void *cls;
224 };
225
226
227 struct SlicerRemoveClosure
228 {
229   struct GNUNET_SOCIAL_Slicer *slicer;
230   struct SlicerCallbacks rm_cbs;
231 };
232
233
234 /**
235  * Handle for an announcement request.
236  */
237 struct GNUNET_SOCIAL_Announcement
238 {
239
240 };
241
242
243 /**
244  * A talk request.
245  */
246 struct GNUNET_SOCIAL_TalkRequest
247 {
248
249 };
250
251
252 struct GNUNET_SOCIAL_WatchHandle
253 {
254
255 };
256
257
258 /**
259  * A history lesson.
260  */
261 struct GNUNET_SOCIAL_HistoryRequest
262 {
263   /**
264    * Place.
265    */
266   struct GNUNET_SOCIAL_Place *plc;
267
268   /**
269    * Operation ID.
270    */
271   uint64_t op_id;
272
273   /**
274    * Message handler.
275    */
276   struct GNUNET_PSYC_ReceiveHandle *recv;
277
278   /**
279    * Function to call when the operation finished.
280    */
281   GNUNET_ResultCallback result_cb;
282
283   /**
284    * Closure for @a result_cb.
285    */
286   void *cls;
287 };
288
289
290 struct GNUNET_SOCIAL_LookHandle
291 {
292   /**
293    * Place.
294    */
295   struct GNUNET_SOCIAL_Place *plc;
296
297   /**
298    * Operation ID.
299    */
300   uint64_t op_id;
301
302   /**
303    * State variable result callback.
304    */
305   GNUNET_PSYC_StateVarCallback var_cb;
306
307   /**
308    * Function to call when the operation finished.
309    */
310   GNUNET_ResultCallback result_cb;
311
312   /**
313    * Closure for @a result_cb.
314    */
315   void *cls;
316 };
317
318
319 static struct GNUNET_SOCIAL_Nym *
320 nym_get_or_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key)
321 {
322   struct GNUNET_SOCIAL_Nym *nym = NULL;
323   struct GNUNET_HashCode pub_key_hash;
324
325   if (NULL == pub_key)
326     return NULL;
327
328   GNUNET_CRYPTO_hash (pub_key, sizeof (*pub_key), &pub_key_hash);
329
330   if (NULL == nyms)
331     nyms = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
332   else
333     nym = GNUNET_CONTAINER_multihashmap_get (nyms, &pub_key_hash);
334
335   if (NULL == nym)
336   {
337     nym = GNUNET_new (struct GNUNET_SOCIAL_Nym);
338     nym->pub_key = *pub_key;
339     nym->pub_key_hash = pub_key_hash;
340     GNUNET_CONTAINER_multihashmap_put (nyms, &nym->pub_key_hash, nym,
341                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
342   }
343   return nym;
344 }
345
346
347 static void
348 nym_destroy (struct GNUNET_SOCIAL_Nym *nym)
349 {
350   GNUNET_CONTAINER_multihashmap_remove (nyms, &nym->pub_key_hash, nym);
351   GNUNET_free (nym);
352 }
353
354
355 /**
356  * Call a handler for an incoming message part.
357  *
358  * @param cls
359  * @param key
360  * @param value
361  *
362  * @return
363  */
364 int
365 slicer_handler_notify (void *cls, const struct GNUNET_HashCode *key,
366                        void *value)
367 {
368   struct GNUNET_SOCIAL_Slicer *slicer = cls;
369   const struct GNUNET_MessageHeader *msg = slicer->msg;
370   struct SlicerCallbacks *cbs = value;
371   uint16_t ptype = ntohs (msg->type);
372
373   switch (ptype)
374   {
375   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
376   {
377     if (NULL == cbs->method_cb)
378       break;
379     struct GNUNET_PSYC_MessageMethod *
380       meth = (struct GNUNET_PSYC_MessageMethod *) msg;
381     cbs->method_cb (cbs->cls, meth, slicer->message_id,
382                     ntohl (meth->flags),
383                     nym_get_or_create (&slicer->nym_key),
384                     slicer->method_name);
385     break;
386   }
387
388   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
389   {
390     if (NULL == cbs->modifier_cb)
391       break;
392     struct GNUNET_PSYC_MessageModifier *
393       mod = (struct GNUNET_PSYC_MessageModifier *) msg;
394     cbs->modifier_cb (cbs->cls, mod, slicer->message_id,
395                       mod->oper, (const char *) &mod[1],
396                       (const void *) &mod[1] + ntohs (mod->name_size),
397                       ntohs (mod->value_size));
398     break;
399   }
400
401   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
402   {
403     if (NULL == cbs->modifier_cb)
404       break;
405     /* FIXME: concatenate until done */
406     break;
407   }
408
409   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
410   {
411     if (NULL == cbs->data_cb)
412       break;
413     uint64_t data_offset = 0; // FIXME
414     cbs->data_cb (cbs->cls, msg, slicer->message_id,
415                   data_offset, &msg[1], ntohs (msg->size) - sizeof (*msg));
416     break;
417   }
418
419   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
420     if (NULL == cbs->eom_cb)
421       break;
422     cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_NO);
423     break;
424
425   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
426     if (NULL == cbs->eom_cb)
427       break;
428     cbs->eom_cb (cbs->cls, msg, slicer->message_id, GNUNET_YES);
429     break;
430   }
431   return GNUNET_YES;
432 }
433
434
435 /**
436  * Process an incoming message part and call matching handlers.
437  *
438  * @param cls
439  *        Closure.
440  * @param message_id
441  *        ID of the message.
442  * @param flags
443  *        Flags for the message.
444  *        @see enum GNUNET_PSYC_MessageFlags
445  * @param msg
446  *        The message part. as it arrived from the network.
447  */
448 static void
449 slicer_message (void *cls, uint64_t message_id, uint64_t fragment_offset,
450                 uint32_t flags, const struct GNUNET_MessageHeader *msg)
451 {
452   struct GNUNET_SOCIAL_Slicer *slicer = cls;
453   uint16_t ptype = ntohs (msg->type);
454   if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD == ptype)
455   {
456     struct GNUNET_PSYC_MessageMethod *
457       meth = (struct GNUNET_PSYC_MessageMethod *) msg;
458     slicer->method_name_size = ntohs (meth->header.size) - sizeof (*meth);
459     slicer->method_name = GNUNET_malloc (slicer->method_name_size);
460     memcpy (slicer->method_name, &meth[1], slicer->method_name_size);
461     slicer->message_id = message_id;
462   }
463   else
464   {
465     GNUNET_assert (message_id == slicer->message_id);
466   }
467
468   LOG (GNUNET_ERROR_TYPE_DEBUG,
469        "Slicer received message of type %u and size %u, "
470        "with ID %" PRIu64 " and method %s\n",
471        ptype, ntohs (msg->size), message_id, slicer->method_name);
472
473   slicer->msg = msg;
474   char *name = GNUNET_malloc (slicer->method_name_size);
475   memcpy (name, slicer->method_name, slicer->method_name_size);
476   do
477   {
478     struct GNUNET_HashCode key;
479     uint16_t name_len = strlen (name);
480     GNUNET_CRYPTO_hash (name, name_len, &key);
481     GNUNET_CONTAINER_multihashmap_get_multiple (slicer->handlers, &key,
482                                                 &slicer_handler_notify, slicer);
483     char *p = strrchr (name, '_');
484     if (NULL == p)
485       break;
486     *p = '\0';
487   } while (1);
488   GNUNET_free (name);
489   slicer->msg = NULL;
490
491   if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END <= ptype)
492     GNUNET_free (slicer->method_name);
493 }
494
495
496 /**
497  * Create a try-and-slice instance.
498  *
499  * @return A new try-and-slice construct.
500  */
501 struct GNUNET_SOCIAL_Slicer *
502 GNUNET_SOCIAL_slicer_create (void)
503 {
504   struct GNUNET_SOCIAL_Slicer *slicer = GNUNET_malloc (sizeof (*slicer));
505   slicer->handlers = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
506   return slicer;
507 }
508
509
510 /**
511  * Add a method to the try-and-slice instance.
512  *
513  * A slicer processes messages and calls methods that match a message. A match
514  * happens whenever the method name of a message starts with the method_name
515  * parameter given here.
516  *
517  * @param slicer The try-and-slice instance to extend.
518  * @param method_name Name of the given method, use empty string for default.
519  * @param method Method to invoke.
520  * @param method_cls Closure for method.
521  */
522 void
523 GNUNET_SOCIAL_slicer_add (struct GNUNET_SOCIAL_Slicer *slicer,
524                           const char *method_name,
525                           GNUNET_SOCIAL_MethodCallback method_cb,
526                           GNUNET_SOCIAL_ModifierCallback modifier_cb,
527                           GNUNET_SOCIAL_DataCallback data_cb,
528                           GNUNET_SOCIAL_EndOfMessageCallback eom_cb,
529                           void *cls)
530 {
531   struct GNUNET_HashCode key;
532   GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
533
534   struct SlicerCallbacks *cbs = GNUNET_malloc (sizeof (*cbs));
535   cbs->method_cb = method_cb;
536   cbs->modifier_cb = modifier_cb;
537   cbs->data_cb = data_cb;
538   cbs->eom_cb = eom_cb;
539   cbs->cls = cls;
540
541   GNUNET_CONTAINER_multihashmap_put (slicer->handlers, &key, cbs,
542                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
543 }
544
545
546 int
547 slicer_remove_handler (void *cls, const struct GNUNET_HashCode *key, void *value)
548 {
549   struct SlicerRemoveClosure *rm_cls = cls;
550   struct GNUNET_SOCIAL_Slicer *slicer = rm_cls->slicer;
551   struct SlicerCallbacks *rm_cbs = &rm_cls->rm_cbs;
552   struct SlicerCallbacks *cbs = value;
553
554   if (cbs->method_cb == rm_cbs->method_cb
555       && cbs->modifier_cb == rm_cbs->modifier_cb
556       && cbs->data_cb == rm_cbs->data_cb
557       && cbs->eom_cb == rm_cbs->eom_cb)
558   {
559     GNUNET_CONTAINER_multihashmap_remove (slicer->handlers, key, cbs);
560     GNUNET_free (cbs);
561     return GNUNET_NO;
562   }
563   return GNUNET_YES;
564 }
565
566
567 /**
568  * Remove a registered method from the try-and-slice instance.
569  *
570  * Removes the first matching handler registered with @a method and the given callbacks.
571  *
572  * @param slicer The try-and-slice instance.
573  * @param method_name Name of the method to remove.
574  * @param method Method handler.
575  *
576  * @return #GNUNET_OK if a method handler was removed,
577  *         #GNUNET_NO if no handler matched the given method name and callbacks.
578  */
579 int
580 GNUNET_SOCIAL_slicer_remove (struct GNUNET_SOCIAL_Slicer *slicer,
581                              const char *method_name,
582                              GNUNET_SOCIAL_MethodCallback method_cb,
583                              GNUNET_SOCIAL_ModifierCallback modifier_cb,
584                              GNUNET_SOCIAL_DataCallback data_cb,
585                              GNUNET_SOCIAL_EndOfMessageCallback eom_cb)
586 {
587   struct GNUNET_HashCode key;
588   GNUNET_CRYPTO_hash (method_name, strlen (method_name), &key);
589
590   struct SlicerRemoveClosure rm_cls;
591   rm_cls.slicer = slicer;
592   struct SlicerCallbacks *rm_cbs = &rm_cls.rm_cbs;
593   rm_cbs->method_cb = method_cb;
594   rm_cbs->modifier_cb = modifier_cb;
595   rm_cbs->data_cb = data_cb;
596   rm_cbs->eom_cb = eom_cb;
597
598   return
599     (GNUNET_SYSERR
600      == GNUNET_CONTAINER_multihashmap_get_multiple (slicer->handlers, &key,
601                                                     &slicer_remove_handler,
602                                                     &rm_cls))
603     ? GNUNET_NO
604     : GNUNET_OK;
605 }
606
607
608 int
609 slicer_free_handler (void *cls, const struct GNUNET_HashCode *key, void *value)
610 {
611   struct SlicerCallbacks *cbs = value;
612   GNUNET_free (cbs);
613   return GNUNET_YES;
614 }
615
616
617 /**
618  * Destroy a given try-and-slice instance.
619  *
620  * @param slicer
621  *        Slicer to destroy
622  */
623 void
624 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer)
625 {
626   GNUNET_CONTAINER_multihashmap_iterate (slicer->handlers, &slicer_free_handler,
627                                          NULL);
628   GNUNET_CONTAINER_multihashmap_destroy (slicer->handlers);
629   GNUNET_free (slicer);
630 }
631
632
633 static void
634 place_send_connect_msg (struct GNUNET_SOCIAL_Place *plc)
635 {
636   uint16_t cmsg_size = ntohs (plc->connect_msg->size);
637   struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
638   memcpy (cmsg, plc->connect_msg, cmsg_size);
639   GNUNET_CLIENT_MANAGER_transmit_now (plc->client, cmsg);
640 }
641
642
643 static void
644 place_recv_result (void *cls,
645                    struct GNUNET_CLIENT_MANAGER_Connection *client,
646                    const struct GNUNET_MessageHeader *msg)
647 {
648   struct GNUNET_SOCIAL_Place *
649     plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
650
651   const struct GNUNET_OperationResultMessage *
652     res = (const struct GNUNET_OperationResultMessage *) msg;
653
654   uint16_t size = ntohs (msg->size);
655   if (size < sizeof (*res))
656   { /* Error, message too small. */
657     GNUNET_break (0);
658     return;
659   }
660
661   uint16_t data_size = size - sizeof (*res);
662   const char *data = (0 < data_size) ? (const char *) &res[1] : NULL;
663   GNUNET_CLIENT_MANAGER_op_result (plc->client, GNUNET_ntohll (res->op_id),
664                                    GNUNET_ntohll (res->result_code),
665                                    data, data_size);
666 }
667
668
669 static void
670 op_recv_history_result (void *cls, int64_t result,
671                         const void *err_msg, uint16_t err_msg_size)
672 {
673   LOG (GNUNET_ERROR_TYPE_DEBUG,
674        "Received history replay result: %" PRId64 ".\n", result);
675
676   struct GNUNET_SOCIAL_HistoryRequest *hist = cls;
677
678   if (NULL != hist->result_cb)
679     hist->result_cb (hist->cls, result, err_msg, err_msg_size);
680
681   GNUNET_PSYC_receive_destroy (hist->recv);
682   GNUNET_free (hist);
683 }
684
685
686 static void
687 op_recv_state_result (void *cls, int64_t result,
688                       const void *err_msg, uint16_t err_msg_size)
689 {
690   LOG (GNUNET_ERROR_TYPE_DEBUG,
691        "Received state request result: %" PRId64 ".\n", result);
692
693   struct GNUNET_SOCIAL_LookHandle *look = cls;
694
695   if (NULL != look->result_cb)
696     look->result_cb (look->cls, result, err_msg, err_msg_size);
697
698   GNUNET_free (look);
699 }
700
701
702 static void
703 place_recv_history_result (void *cls,
704                            struct GNUNET_CLIENT_MANAGER_Connection *client,
705                            const struct GNUNET_MessageHeader *msg)
706 {
707   struct GNUNET_SOCIAL_Place *
708     plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
709
710   const struct GNUNET_OperationResultMessage *
711     res = (const struct GNUNET_OperationResultMessage *) msg;
712   struct GNUNET_PSYC_MessageHeader *
713     pmsg = (struct GNUNET_PSYC_MessageHeader *) &res[1];
714
715   LOG (GNUNET_ERROR_TYPE_DEBUG,
716        "%p Received historic fragment for message #%" PRIu64 ".\n",
717        plc, GNUNET_ntohll (pmsg->message_id));
718
719   GNUNET_ResultCallback result_cb = NULL;
720   struct GNUNET_SOCIAL_HistoryRequest *hist = NULL;
721
722   if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
723                                                    GNUNET_ntohll (res->op_id),
724                                                    &result_cb, (void *) &hist))
725   { /* Operation not found. */
726     LOG (GNUNET_ERROR_TYPE_WARNING,
727          "%p Replay operation not found for historic fragment of message #%"
728          PRIu64 ".\n",
729          plc, GNUNET_ntohll (pmsg->message_id));
730     return;
731   }
732
733   uint16_t size = ntohs (msg->size);
734   if (size < sizeof (*res) + sizeof (*pmsg))
735   { /* Error, message too small. */
736     GNUNET_break (0);
737     return;
738   }
739
740   GNUNET_PSYC_receive_message (hist->recv,
741                                (const struct GNUNET_PSYC_MessageHeader *) pmsg);
742 }
743
744
745 static void
746 place_recv_state_result (void *cls,
747                          struct GNUNET_CLIENT_MANAGER_Connection *client,
748                          const struct GNUNET_MessageHeader *msg)
749 {
750   struct GNUNET_SOCIAL_Place *
751     plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
752
753   const struct GNUNET_OperationResultMessage *
754     res = (const struct GNUNET_OperationResultMessage *) msg;
755
756 #if FIXME
757   GNUNET_ResultCallback result_cb = NULL;
758   struct GNUNET_PSYC_StateRequest *sr = NULL;
759
760   if (GNUNET_YES != GNUNET_CLIENT_MANAGER_op_find (plc->client,
761                                                    GNUNET_ntohll (res->op_id),
762                                                    &result_cb, (void *) &sr))
763   { /* Operation not found. */
764     return;
765   }
766
767   const struct GNUNET_MessageHeader *
768     modc = (struct GNUNET_MessageHeader *) &res[1];
769   uint16_t modc_size = ntohs (modc->size);
770   if (ntohs (msg->size) - sizeof (*msg) != modc_size)
771   {
772     GNUNET_break (0);
773     return;
774   }
775   switch (ntohs (modc->type))
776   {
777   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
778   {
779     const struct GNUNET_PSYC_MessageModifier *
780       mod = (const struct GNUNET_PSYC_MessageModifier *) modc;
781
782     const char *name = (const char *) &mod[1];
783     uint16_t name_size = ntohs (mod->name_size);
784     if ('\0' != name[name_size - 1])
785     {
786       GNUNET_break (0);
787       return;
788     }
789     sr->var_cb (sr->cls, name, name + name_size, ntohs (mod->value_size));
790     break;
791   }
792
793   case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
794     sr->var_cb (sr->cls, NULL, (const char *) &modc[1],
795                 modc_size - sizeof (*modc));
796     break;
797   }
798 #endif
799 }
800
801
802 static void
803 place_recv_message_ack (void *cls,
804                         struct GNUNET_CLIENT_MANAGER_Connection *client,
805                         const struct GNUNET_MessageHeader *msg)
806 {
807   struct GNUNET_SOCIAL_Place *
808     plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
809   GNUNET_PSYC_transmit_got_ack (plc->tmit);
810 }
811
812
813 static void
814 place_recv_message (void *cls,
815                     struct GNUNET_CLIENT_MANAGER_Connection *client,
816                     const struct GNUNET_MessageHeader *msg)
817 {
818   struct GNUNET_SOCIAL_Place *
819     plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
820   GNUNET_PSYC_receive_message (plc->recv,
821                                (const struct GNUNET_PSYC_MessageHeader *) msg);
822 }
823
824
825 static void
826 place_recv_disconnect (void *cls,
827                        struct GNUNET_CLIENT_MANAGER_Connection *client,
828                        const struct GNUNET_MessageHeader *msg)
829 {
830   struct GNUNET_SOCIAL_Place *
831     plc = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*plc));
832
833   GNUNET_CLIENT_MANAGER_reconnect (client);
834   place_send_connect_msg (plc);
835 }
836
837
838 static void
839 host_recv_enter_ack (void *cls,
840                      struct GNUNET_CLIENT_MANAGER_Connection *client,
841                      const struct GNUNET_MessageHeader *msg)
842 {
843   struct GNUNET_SOCIAL_Host *
844     hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
845                                                    sizeof (struct GNUNET_SOCIAL_Place));
846
847   struct GNUNET_PSYC_CountersResultMessage *
848     cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
849   int32_t result = ntohl (cres->result_code) + INT32_MIN;
850   if (NULL != hst->enter_cb)
851     hst->enter_cb (hst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
852 }
853
854
855 static void
856 host_recv_enter_request (void *cls,
857                          struct GNUNET_CLIENT_MANAGER_Connection *client,
858                          const struct GNUNET_MessageHeader *msg)
859 {
860   struct GNUNET_SOCIAL_Host *
861     hst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
862                                                    sizeof (struct GNUNET_SOCIAL_Place));
863   if (NULL == hst->answer_door_cb)
864      return;
865
866   const char *method_name = NULL;
867   struct GNUNET_ENV_Environment *env = NULL;
868   const void *data = NULL;
869   uint16_t data_size = 0;
870   char *str;
871   const struct GNUNET_PSYC_JoinRequestMessage *
872     req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
873   const struct GNUNET_PSYC_Message *entry_msg = NULL;
874
875   if (sizeof (*req) + sizeof (*entry_msg) <= ntohs (req->header.size))
876   {
877     entry_msg = (struct GNUNET_PSYC_Message *) &req[1];
878     LOG (GNUNET_ERROR_TYPE_DEBUG,
879          "Received entry_msg of type %u and size %u.\n",
880          ntohs (entry_msg->header.type), ntohs (entry_msg->header.size));
881
882     env = GNUNET_ENV_environment_create ();
883     if (GNUNET_OK != GNUNET_PSYC_message_parse (entry_msg, &method_name, env,
884                                                 &data, &data_size))
885     {
886       GNUNET_break_op (0);
887       str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&req->slave_key);
888       LOG (GNUNET_ERROR_TYPE_WARNING,
889            "Ignoring invalid entry request from nym %s.\n",
890            str);
891       GNUNET_free (str);
892       GNUNET_ENV_environment_destroy (env);
893       return;
894     }
895   }
896
897   struct GNUNET_SOCIAL_Nym *nym = nym_get_or_create (&req->slave_key);
898   hst->answer_door_cb (hst->cb_cls, nym, method_name, env,
899                        data_size, data);
900
901   if (NULL != env)
902     GNUNET_ENV_environment_destroy (env);
903 }
904
905
906 static void
907 guest_recv_enter_ack (void *cls,
908                      struct GNUNET_CLIENT_MANAGER_Connection *client,
909                      const struct GNUNET_MessageHeader *msg)
910 {
911   struct GNUNET_SOCIAL_Guest *
912     gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
913                                                    sizeof (struct GNUNET_SOCIAL_Place));
914
915   struct GNUNET_PSYC_CountersResultMessage *
916     cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
917   int32_t result = ntohl (cres->result_code) + INT32_MIN;
918   if (NULL != gst->enter_cb)
919     gst->enter_cb (gst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
920 }
921
922
923 static void
924 guest_recv_join_decision (void *cls,
925                           struct GNUNET_CLIENT_MANAGER_Connection *client,
926                           const struct GNUNET_MessageHeader *msg)
927 {
928   struct GNUNET_SOCIAL_Guest *
929     gst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
930                                                    sizeof (struct GNUNET_SOCIAL_Place));
931   const struct GNUNET_PSYC_JoinDecisionMessage *
932     dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
933
934   struct GNUNET_PSYC_Message *pmsg = NULL;
935   if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
936     pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
937
938   if (NULL != gst->entry_dcsn_cb)
939     gst->entry_dcsn_cb (gst->cb_cls, ntohl (dcsn->is_admitted), pmsg);
940 }
941
942
943 static struct GNUNET_CLIENT_MANAGER_MessageHandler host_handlers[] =
944 {
945   { &host_recv_enter_ack, NULL,
946     GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER_ACK,
947     sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
948
949   { &host_recv_enter_request, NULL,
950     GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
951     sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
952
953   { &place_recv_message, NULL,
954     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
955     sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
956
957   { &place_recv_message_ack, NULL,
958     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
959     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
960
961   { &place_recv_history_result, NULL,
962     GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
963     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
964
965   { &place_recv_state_result, NULL,
966     GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
967     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
968
969   { &place_recv_result, NULL,
970     GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
971     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
972
973   { &place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
974
975   { NULL, NULL, 0, 0, GNUNET_NO }
976 };
977
978
979 static struct GNUNET_CLIENT_MANAGER_MessageHandler guest_handlers[] =
980 {
981   { &guest_recv_enter_ack, NULL,
982     GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER_ACK,
983     sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
984
985   { &host_recv_enter_request, NULL,
986     GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
987     sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
988
989   { &place_recv_message, NULL,
990     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
991     sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
992
993   { &place_recv_message_ack, NULL,
994     GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
995     sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
996
997   { &guest_recv_join_decision, NULL,
998     GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
999     sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
1000
1001   { &place_recv_history_result, NULL,
1002     GNUNET_MESSAGE_TYPE_PSYC_HISTORY_RESULT,
1003     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1004
1005   { &place_recv_state_result, NULL,
1006     GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
1007     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1008
1009   { &place_recv_result, NULL,
1010     GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
1011     sizeof (struct GNUNET_OperationResultMessage), GNUNET_YES },
1012
1013   { &place_recv_disconnect, NULL, 0, 0, GNUNET_NO },
1014
1015   { NULL, NULL, 0, 0, GNUNET_NO }
1016 };
1017
1018
1019 static void
1020 place_cleanup (struct GNUNET_SOCIAL_Place *plc)
1021 {
1022   GNUNET_PSYC_transmit_destroy (plc->tmit);
1023   GNUNET_PSYC_receive_destroy (plc->recv);
1024   GNUNET_free (plc->connect_msg);
1025   if (NULL != plc->disconnect_cb)
1026     plc->disconnect_cb (plc->disconnect_cls);
1027 }
1028
1029
1030 static void
1031 host_cleanup (void *cls)
1032 {
1033   struct GNUNET_SOCIAL_Host *hst = cls;
1034   place_cleanup (&hst->plc);
1035   GNUNET_free (hst);
1036 }
1037
1038
1039 static void
1040 guest_cleanup (void *cls)
1041 {
1042   struct GNUNET_SOCIAL_Guest *gst = cls;
1043   place_cleanup (&gst->plc);
1044   GNUNET_free (gst);
1045 }
1046
1047
1048 /**
1049  * Enter a place as host.
1050  *
1051  * A place is created upon first entering, and it is active until permanently
1052  * left using GNUNET_SOCIAL_host_leave().
1053  *
1054  * @param cfg
1055  *        Configuration to contact the social service.
1056  * @param ego
1057  *        Identity of the host.
1058  * @param place_key
1059  *        Private-public key pair of the place.
1060  *        NULL for ephemeral places.
1061  * @param policy
1062  *        Policy specifying entry and history restrictions for the place.
1063  * @param slicer
1064  *        Slicer to handle incoming messages.
1065  * @param answer_door_cb
1066  *        Function to handle new nyms that want to enter.
1067  * @param farewell_cb
1068  *        Function to handle departing nyms.
1069  * @param cls
1070  *        Closure for the callbacks.
1071  *
1072  * @return Handle for the host.
1073  */
1074 struct GNUNET_SOCIAL_Host *
1075 GNUNET_SOCIAL_host_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1076                           const struct GNUNET_IDENTITY_Ego *ego,
1077                           const struct GNUNET_CRYPTO_EddsaPrivateKey *place_key,
1078                           enum GNUNET_PSYC_Policy policy,
1079                           struct GNUNET_SOCIAL_Slicer *slicer,
1080                           GNUNET_SOCIAL_HostEnterCallback enter_cb,
1081                           GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1082                           GNUNET_SOCIAL_FarewellCallback farewell_cb,
1083                           void *cls)
1084 {
1085   struct GNUNET_SOCIAL_Host *hst = GNUNET_malloc (sizeof (*hst));
1086   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1087   struct HostEnterRequest *req = GNUNET_malloc (sizeof (*req));
1088
1089   if (NULL != place_key)
1090   {
1091     hst->place_key = *place_key;
1092   }
1093   else
1094   {
1095     struct GNUNET_CRYPTO_EddsaPrivateKey *
1096       ephemeral_key = GNUNET_CRYPTO_eddsa_key_create ();
1097     hst->place_key = *ephemeral_key;
1098     GNUNET_CRYPTO_eddsa_key_get_public (&hst->place_key, &plc->pub_key);
1099     GNUNET_CRYPTO_eddsa_key_clear (ephemeral_key);
1100     GNUNET_free (ephemeral_key);
1101   }
1102   plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1103
1104   req->header.size = htons (sizeof (*req));
1105   req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_HOST_ENTER);
1106   req->policy = policy;
1107   req->place_key = hst->place_key;
1108   req->host_key = plc->ego_key;
1109
1110   plc->connect_msg = (struct GNUNET_MessageHeader *) req;
1111   plc->cfg = cfg;
1112   plc->is_host = GNUNET_YES;
1113   plc->slicer = slicer;
1114
1115   hst->plc.ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1116   hst->enter_cb = enter_cb;
1117   hst->answer_door_cb = answer_door_cb;
1118   hst->cb_cls = cls;
1119
1120   plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", host_handlers);
1121   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, hst, sizeof (*plc));
1122
1123   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1124   plc->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, plc->slicer);
1125
1126   place_send_connect_msg (plc);
1127   return hst;
1128 }
1129
1130
1131 /**
1132  * Enter a place as host.
1133  *
1134  * A place is created upon first entering, and it is active until permanently
1135  * left using GNUNET_SOCIAL_host_leave().
1136  *
1137  * @param cfg
1138  *        Configuration to contact the social service.
1139  * @param ego
1140  *        Identity of the host.
1141  * @param gns_name
1142  *        GNS name in the zone of the @a ego that contains the
1143  *        public key of the place in a PLACE record.
1144  * @param policy
1145  *        Policy specifying entry and history restrictions for the place.
1146  * @param slicer
1147  *        Slicer to handle incoming messages.
1148  * @param answer_door_cb
1149  *        Function to handle new nyms that want to enter.
1150  * @param farewell_cb
1151  *        Function to handle departing nyms.
1152  * @param cls
1153  *        Closure for the callbacks.
1154  *
1155  * @return Handle for the host.
1156  */
1157 struct GNUNET_SOCIAL_Host *
1158 GNUNET_SOCIAL_host_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
1159                                   struct GNUNET_IDENTITY_Ego *ego,
1160                                   const char *gns_name,
1161                                   enum GNUNET_PSYC_Policy policy,
1162                                   struct GNUNET_SOCIAL_Slicer *slicer,
1163                                   GNUNET_SOCIAL_HostEnterCallback enter_cb,
1164                                   GNUNET_SOCIAL_AnswerDoorCallback answer_door_cb,
1165                                   GNUNET_SOCIAL_FarewellCallback farewell_cb,
1166                                   void *cls)
1167 {
1168   struct GNUNET_CRYPTO_EddsaPrivateKey place_key = {};
1169
1170   /* FIXME:
1171    * 1. get public key by looking up PLACE entry under gns_name
1172    *    in the zone of the ego.
1173    * 2. get private key from $GNUNET_DATA_HOME/social/places/PUB_KEY_HASH
1174    */
1175
1176   return GNUNET_SOCIAL_host_enter (cfg, ego, &place_key, policy, slicer,
1177                                    enter_cb, answer_door_cb, farewell_cb, cls);
1178 }
1179
1180
1181 /**
1182  * Decision whether to admit @a nym into the place or refuse entry.
1183  *
1184  * @param hst
1185  *        Host of the place.
1186  * @param nym
1187  *        Handle for the entity that wanted to enter.
1188  * @param is_admitted
1189  *        #GNUNET_YES    if @a nym is admitted,
1190  *        #GNUNET_NO     if @a nym is refused entry,
1191  *        #GNUNET_SYSERR if we cannot answer the request.
1192  * @param method_name
1193  *        Method name for the rejection message.
1194  * @param env
1195  *        Environment containing variables for the message, or NULL.
1196  * @param data
1197  *        Data for the rejection message to send back.
1198  * @param data_size
1199  *        Number of bytes in @a data for method.
1200  * @return #GNUNET_OK on success,
1201  *         #GNUNET_SYSERR if the message is too large.
1202  */
1203 int
1204 GNUNET_SOCIAL_host_entry_decision (struct GNUNET_SOCIAL_Host *hst,
1205                                    struct GNUNET_SOCIAL_Nym *nym,
1206                                    int is_admitted,
1207                                    const struct GNUNET_PSYC_Message *entry_resp)
1208 {
1209   struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
1210   uint16_t entry_resp_size
1211     = (NULL != entry_resp) ? ntohs (entry_resp->header.size) : 0;
1212
1213   if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < sizeof (*dcsn) + entry_resp_size)
1214     return GNUNET_SYSERR;
1215
1216   dcsn = GNUNET_malloc (sizeof (*dcsn) + entry_resp_size);
1217   dcsn->header.size = htons (sizeof (*dcsn) + entry_resp_size);
1218   dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
1219   dcsn->is_admitted = htonl (is_admitted);
1220   dcsn->slave_key = nym->pub_key;
1221
1222   if (0 < entry_resp_size)
1223     memcpy (&dcsn[1], entry_resp, entry_resp_size);
1224
1225   GNUNET_CLIENT_MANAGER_transmit (hst->plc.client, &dcsn->header);
1226   return GNUNET_OK;
1227 }
1228
1229
1230 /**
1231  * Throw @a nym out of the place.
1232  *
1233  * The @a nym reference will remain valid until the
1234  * #GNUNET_SOCIAL_FarewellCallback is invoked,
1235  * which should be very soon after this call.
1236  *
1237  * @param host  Host of the place.
1238  * @param nym  Handle for the entity to be ejected.
1239  */
1240 void
1241 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *host,
1242                           struct GNUNET_SOCIAL_Nym *nym)
1243 {
1244
1245 }
1246
1247
1248 /**
1249  * Get the public key of a @a nym.
1250  *
1251  * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
1252  *
1253  * @param nym Pseudonym to map to a cryptographic identifier.
1254  * @param[out] nym_key Set to the public key of the nym.
1255  */
1256 struct GNUNET_CRYPTO_EcdsaPublicKey *
1257 GNUNET_SOCIAL_nym_get_key (struct GNUNET_SOCIAL_Nym *nym)
1258 {
1259   return &nym->pub_key;
1260 }
1261
1262
1263 /**
1264  * Obtain the private-public key pair of the hosted place.
1265  *
1266  * The public part is suitable for storing in GNS within a PLACE record,
1267  * along with peer IDs to join at.
1268  *
1269  * @param host
1270  *        Host of the place.
1271  *
1272  * @return Private-public key pair of the hosted place.
1273  */
1274 const struct GNUNET_CRYPTO_EddsaPrivateKey *
1275 GNUNET_SOCIAL_host_get_place_key (struct GNUNET_SOCIAL_Host *hst)
1276 {
1277   return &hst->place_key;
1278 }
1279
1280
1281 static void
1282 namestore_result_host_advertise (void *cls, int32_t success, const char *emsg)
1283 {
1284
1285 }
1286
1287
1288 /**
1289  * Connected to core service.
1290  */
1291 static void
1292 core_connected_cb  (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1293 {
1294   this_peer = *my_identity;
1295   // FIXME
1296 }
1297
1298
1299 /**
1300  * Advertise the place in the GNS zone of the @e ego of the @a host.
1301  *
1302  * @param hst  Host of the place.
1303  * @param name The name for the PLACE record to put in the zone.
1304  * @param peer_count Number of elements in the @a peers array.
1305  * @param peers List of peers in the PLACE record that can be used to send join
1306  *        requests to.
1307  * @param expiration_time Expiration time of the record, use 0 to remove the record.
1308  * @param password Password used to encrypt the record.
1309  */
1310 void
1311 GNUNET_SOCIAL_host_advertise (struct GNUNET_SOCIAL_Host *hst,
1312                               const char *name,
1313                               size_t peer_count,
1314                               const struct GNUNET_PeerIdentity *peers,
1315                               struct GNUNET_TIME_Relative expiration_time,
1316                               const char *password)
1317 {
1318   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1319   if (NULL == namestore)
1320     namestore = GNUNET_NAMESTORE_connect (plc->cfg);
1321   if (NULL == core)
1322     core = GNUNET_CORE_connect (plc->cfg, NULL, core_connected_cb, NULL, NULL,
1323                                 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
1324
1325   struct GNUNET_GNSRECORD_Data rd = { 0 };
1326   rd.record_type = GNUNET_GNSRECORD_TYPE_PLACE;
1327   rd.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
1328   rd.expiration_time
1329     = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_WEEKS, 1).rel_value_us;
1330
1331   struct GNUNET_GNSRECORD_PlaceData *rec = GNUNET_malloc (sizeof (*rec));
1332   rec->place_key = plc->pub_key;
1333   rec->origin = this_peer;
1334   rec->relay_count = htons (0); // FIXME
1335
1336   rd.data_size = sizeof (*rec);
1337   rd.data = rec;
1338
1339   GNUNET_NAMESTORE_records_store (namestore, &hst->plc.ego_key,
1340                                   name, 1, &rd, namestore_result_host_advertise,
1341                                   hst);
1342 }
1343
1344
1345 /**
1346  * Send a message to all nyms that are present in the place.
1347  *
1348  * This function is restricted to the host.  Nyms can only send requests
1349  * to the host who can decide to relay it to everyone in the place.
1350  *
1351  * @param host  Host of the place.
1352  * @param method_name Method to use for the announcement.
1353  * @param env  Environment containing variables for the message and operations
1354  *          on objects of the place.  Can be NULL.
1355  * @param notify Function to call to get the payload of the announcement.
1356  * @param notify_cls Closure for @a notify.
1357  * @param flags Flags for this announcement.
1358  *
1359  * @return NULL on error (announcement already in progress?).
1360  */
1361 struct GNUNET_SOCIAL_Announcement *
1362 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *hst,
1363                              const char *method_name,
1364                              const struct GNUNET_ENV_Environment *env,
1365                              GNUNET_PSYC_TransmitNotifyData notify_data,
1366                              void *notify_data_cls,
1367                              enum GNUNET_SOCIAL_AnnounceFlags flags)
1368 {
1369   if (GNUNET_OK ==
1370       GNUNET_PSYC_transmit_message (hst->plc.tmit, method_name, env,
1371                                     NULL, notify_data, notify_data_cls, flags));
1372   return (struct GNUNET_SOCIAL_Announcement *) hst->plc.tmit;
1373 }
1374
1375
1376 /**
1377  * Resume transmitting announcement.
1378  *
1379  * @param a
1380  *        The announcement to resume.
1381  */
1382 void
1383 GNUNET_SOCIAL_host_announce_resume (struct GNUNET_SOCIAL_Announcement *a)
1384 {
1385   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) a);
1386 }
1387
1388
1389 /**
1390  * Cancel announcement.
1391  *
1392  * @param a
1393  *        The announcement to cancel.
1394  */
1395 void
1396 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
1397 {
1398   GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) a);
1399 }
1400
1401
1402 /**
1403  * Obtain handle for a hosted place.
1404  *
1405  * The returned handle can be used to access the place API.
1406  *
1407  * @param host  Handle for the host.
1408  *
1409  * @return Handle for the hosted place, valid as long as @a host is valid.
1410  */
1411 struct GNUNET_SOCIAL_Place *
1412 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *hst)
1413 {
1414   return &hst->plc;
1415 }
1416
1417
1418 /**
1419  * Stop hosting a place.
1420  *
1421  * Invalidates host handle.
1422  *
1423  * @param host  Host leaving the place.
1424  * @param keep_active  Keep the place active after last host disconnected.
1425  */
1426 void
1427 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *hst,
1428                           int keep_active,
1429                           GNUNET_ContinuationCallback leave_cb,
1430                           void *leave_cls)
1431 {
1432   struct GNUNET_SOCIAL_Place *plc = &hst->plc;
1433
1434  /* FIXME: send msg to service */
1435
1436   plc->is_disconnecting = GNUNET_YES;
1437   plc->disconnect_cb = leave_cb;
1438   plc->disconnect_cls = leave_cls;
1439
1440   GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1441                                     &host_cleanup, hst);
1442 }
1443
1444
1445 static struct GuestEnterRequest *
1446 guest_enter_request_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *guest_key,
1447                             const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1448                             const struct GNUNET_PeerIdentity *origin,
1449                             size_t relay_count,
1450                             const struct GNUNET_PeerIdentity *relays,
1451                             const struct GNUNET_PSYC_Message *join_msg)
1452 {
1453   uint16_t join_msg_size = ntohs (join_msg->header.size);
1454   uint16_t relay_size = relay_count * sizeof (*relays);
1455
1456   struct GuestEnterRequest *
1457     req = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size);
1458
1459   req->header.size = htons (sizeof (*req) + relay_size + join_msg_size);
1460   req->header.type = htons (GNUNET_MESSAGE_TYPE_SOCIAL_GUEST_ENTER);
1461   req->place_key = *place_key;
1462   req->guest_key = *guest_key;
1463   req->origin = *origin;
1464   req->relay_count = relay_count;
1465
1466   uint16_t p = sizeof (*req);
1467   if (0 < relay_size)
1468   {
1469     memcpy ((char *) req + p, relays, relay_size);
1470     p += relay_size;
1471   }
1472
1473   memcpy ((char *) req + p, join_msg, join_msg_size);
1474   return req;
1475 }
1476
1477 /**
1478  * Request entry to a place as a guest.
1479  *
1480  * @param cfg Configuration to contact the social service.
1481  * @param ego  Identity of the guest.
1482  * @param crypto_address Public key of the place to enter.
1483  * @param origin Peer identity of the origin of the underlying multicast group.
1484  * @param relay_count Number of elements in the @a relays array.
1485  * @param relays Relays for the underlying multicast group.
1486  * @param method_name Method name for the message.
1487  * @param env Environment containing variables for the message, or NULL.
1488  * @param data Payload for the message to give to the enter callback.
1489  * @param data_size Number of bytes in @a data.
1490  * @param slicer Slicer to use for processing incoming requests from guests.
1491  *
1492  * @return NULL on errors, otherwise handle for the guest.
1493  */
1494 struct GNUNET_SOCIAL_Guest *
1495 GNUNET_SOCIAL_guest_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
1496                            const struct GNUNET_IDENTITY_Ego *ego,
1497                            const struct GNUNET_CRYPTO_EddsaPublicKey *place_key,
1498                            const struct GNUNET_PeerIdentity *origin,
1499                            uint32_t relay_count,
1500                            const struct GNUNET_PeerIdentity *relays,
1501                            const struct GNUNET_PSYC_Message *entry_msg,
1502                            struct GNUNET_SOCIAL_Slicer *slicer,
1503                            GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1504                            GNUNET_SOCIAL_EntryDecisionCallback entry_dcsn_cb,
1505                            void *cls)
1506 {
1507   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1508   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1509
1510   struct GuestEnterRequest *
1511     req = guest_enter_request_create (&plc->ego_key, place_key, origin,
1512                                       relay_count, relays, entry_msg);
1513   plc->connect_msg = &req->header;
1514   plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1515   plc->pub_key = *place_key;
1516   plc->cfg = cfg;
1517   plc->is_host = GNUNET_YES;
1518   plc->slicer = slicer;
1519
1520   gst->enter_cb = local_enter_cb;
1521   gst->entry_dcsn_cb = entry_dcsn_cb;
1522   gst->cb_cls = cls;
1523
1524   plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
1525   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1526
1527   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1528   plc->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, plc->slicer);
1529
1530   place_send_connect_msg (plc);
1531   return gst;
1532 }
1533
1534
1535 /**
1536  * Result of a GNS name lookup for entering a place.
1537  *
1538  * @see GNUNET_SOCIAL_guest_enter_by_name
1539  */
1540 static void
1541 gns_result_guest_enter (void *cls, uint32_t rd_count,
1542                         const struct GNUNET_GNSRECORD_Data *rd)
1543 {
1544   struct GNUNET_SOCIAL_Guest *gst = cls;
1545   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1546
1547   const struct GNUNET_GNSRECORD_PlaceData *
1548     rec = (const struct GNUNET_GNSRECORD_PlaceData *) rd->data;
1549
1550   if (0 == rd_count)
1551   {
1552     if (NULL != gst->enter_cb)
1553       gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
1554     return;
1555   }
1556
1557
1558   if (rd->data_size < sizeof (*rec))
1559   {
1560     GNUNET_break_op (0);
1561     if (NULL != gst->enter_cb)
1562       gst->enter_cb (gst->cb_cls, GNUNET_SYSERR, 0);
1563     return;
1564   }
1565
1566   struct GuestEnterRequest *
1567     req = (struct GuestEnterRequest *) plc->connect_msg;
1568   uint16_t req_size = ntohs (req->header.size);
1569
1570   struct GNUNET_PeerIdentity *relays = NULL;
1571   uint16_t relay_count = ntohs (rec->relay_count);
1572
1573   if (0 < relay_count)
1574   {
1575     uint16_t relay_size = relay_count * sizeof (struct GNUNET_PeerIdentity);
1576     struct GuestEnterRequest *
1577       req2 = GNUNET_malloc (req_size + relay_size);
1578
1579     req2->header.size = htons (req_size + relay_size);
1580     req2->header.type = req->header.type;
1581     req2->guest_key = req->guest_key;
1582
1583     uint16_t p = sizeof (*req);
1584     if (0 < relay_size)
1585     {
1586       memcpy ((char *) req2 + p, relays, relay_size);
1587       p += relay_size;
1588     }
1589
1590     memcpy ((char *) req + p, &req[1], req_size - sizeof (*req));
1591
1592     plc->connect_msg = &req2->header;
1593     GNUNET_free (req);
1594     req = req2;
1595   }
1596
1597   req->place_key = rec->place_key;
1598   req->origin = rec->origin;
1599   req->relay_count = rec->relay_count;
1600   memcpy (&req[1], &rec[1],
1601           ntohl (rec->relay_count) * sizeof (struct GNUNET_PeerIdentity));
1602
1603   plc->connect_msg = &req->header;
1604   plc->pub_key = req->place_key;
1605
1606   plc->tmit = GNUNET_PSYC_transmit_create (plc->client);
1607   plc->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, plc);
1608
1609   place_send_connect_msg (plc);
1610 }
1611
1612 /**
1613  * Request entry to a place as a guest.
1614  *
1615  * @param cfg  Configuration to contact the social service.
1616  * @param ego  Identity of the guest.
1617  * @param address GNS name of the place to enter.  Either in the form of
1618  *        'room.friend.gnu', or 'NYMPUBKEY.zkey'.  This latter case refers to
1619  *        the 'PLACE' record of the empty label ("+") in the GNS zone with the
1620  *        nym's public key 'NYMPUBKEY', and can be used to request entry to a
1621  *        pseudonym's place directly.
1622  * @param method_name Method name for the message.
1623  * @param env Environment containing variables for the message, or NULL.
1624  * @param data Payload for the message to give to the enter callback.
1625  * @param data_size Number of bytes in @a data.
1626  * @param slicer Slicer to use for processing incoming requests from guests.
1627  *
1628  * @return NULL on errors, otherwise handle for the guest.
1629  */
1630 struct GNUNET_SOCIAL_Guest *
1631 GNUNET_SOCIAL_guest_enter_by_name (const struct GNUNET_CONFIGURATION_Handle *cfg,
1632                                    struct GNUNET_IDENTITY_Ego *ego,
1633                                    char *gns_name,
1634                                    const struct GNUNET_PSYC_Message *join_msg,
1635                                    struct GNUNET_SOCIAL_Slicer *slicer,
1636                                    GNUNET_SOCIAL_GuestEnterCallback local_enter_cb,
1637                                    GNUNET_SOCIAL_EntryDecisionCallback entry_decision_cb,
1638                                    void *cls)
1639 {
1640   struct GNUNET_SOCIAL_Guest *gst = GNUNET_malloc (sizeof (*gst));
1641   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1642
1643   gst->enter_cb = local_enter_cb;
1644   gst->cb_cls = cls;
1645
1646   plc->ego_key = *GNUNET_IDENTITY_ego_get_private_key (ego);
1647   plc->cfg = cfg;
1648   plc->is_host = GNUNET_NO;
1649   plc->slicer = slicer;
1650
1651   struct GuestEnterRequest *
1652     req = guest_enter_request_create (&plc->ego_key, NULL, NULL, 0, NULL,
1653                                       join_msg);
1654   plc->connect_msg = &req->header;
1655
1656   /* FIXME: get the public key of the origin and relays
1657    *        by looking up the PLACE record of gns_name.
1658    */
1659   if (NULL == gns)
1660     gns = GNUNET_GNS_connect (cfg);
1661
1662   plc->client = GNUNET_CLIENT_MANAGER_connect (cfg, "social", guest_handlers);
1663   GNUNET_CLIENT_MANAGER_set_user_context_ (plc->client, gst, sizeof (*plc));
1664
1665   struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
1666   GNUNET_IDENTITY_ego_get_public_key (ego, &ego_pub_key);
1667   GNUNET_GNS_lookup (gns, gns_name, &ego_pub_key,
1668                      GNUNET_GNSRECORD_TYPE_PLACE, GNUNET_GNS_LO_DEFAULT,
1669                      NULL, gns_result_guest_enter, gst);
1670
1671   return gst;
1672 }
1673
1674
1675 /**
1676  * Talk to the host of the place.
1677  *
1678  * @param place
1679  *        Place where we want to talk to the host.
1680  * @param method_name
1681  *        Method to invoke on the host.
1682  * @param env
1683  *        Environment containing variables for the message, or NULL.
1684  * @param notify_data
1685  *        Function to use to get the payload for the method.
1686  * @param notify_data_cls
1687  *        Closure for @a notify_data.
1688  * @param flags
1689  *        Flags for the message being sent.
1690  *
1691  * @return NULL if we are already trying to talk to the host,
1692  *         otherwise handle to cancel the request.
1693  */
1694 struct GNUNET_SOCIAL_TalkRequest *
1695 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Guest *gst,
1696                           const char *method_name,
1697                           const struct GNUNET_ENV_Environment *env,
1698                           GNUNET_PSYC_TransmitNotifyData notify_data,
1699                           void *notify_data_cls,
1700                           enum GNUNET_SOCIAL_TalkFlags flags)
1701 {
1702   if (GNUNET_OK ==
1703       GNUNET_PSYC_transmit_message (gst->plc.tmit, method_name, env,
1704                                     NULL, notify_data, notify_data_cls, flags));
1705   return (struct GNUNET_SOCIAL_TalkRequest *) gst->plc.tmit;
1706 }
1707
1708
1709 /**
1710  * Resume talking to the host of the place.
1711  *
1712  * @param tr
1713  *        Talk request to resume.
1714  */
1715 void
1716 GNUNET_SOCIAL_guest_talk_resume (struct GNUNET_SOCIAL_TalkRequest *tr)
1717 {
1718   GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tr);
1719 }
1720
1721
1722 /**
1723  * Cancel talking to the host of the place.
1724  *
1725  * @param tr
1726  *        Talk request to cancel.
1727  */
1728 void
1729 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
1730 {
1731   GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tr);
1732 }
1733
1734
1735 /**
1736  * Leave a place permanently.
1737  *
1738  * Notifies the owner of the place about leaving, and destroys the place handle.
1739  *
1740  * @param place Place to leave permanently.
1741  * @param keep_active Keep place active after last application disconnected.
1742  */
1743 void
1744 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Guest *gst,
1745                            int keep_active,
1746                            GNUNET_ContinuationCallback leave_cb,
1747                            void *leave_cls)
1748 {
1749   struct GNUNET_SOCIAL_Place *plc = &gst->plc;
1750
1751   /* FIXME: send msg to service */
1752
1753   plc->is_disconnecting = GNUNET_YES;
1754   plc->disconnect_cb = leave_cb;
1755   plc->disconnect_cls = leave_cls;
1756
1757   GNUNET_CLIENT_MANAGER_disconnect (plc->client, GNUNET_YES,
1758                                     &guest_cleanup, gst);
1759 }
1760
1761
1762 /**
1763  * Obtain handle for a place entered as guest.
1764  *
1765  * The returned handle can be used to access the place API.
1766  *
1767  * @param guest  Handle for the guest.
1768  *
1769  * @return Handle for the place, valid as long as @a guest is valid.
1770  */
1771 struct GNUNET_SOCIAL_Place *
1772 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Guest *gst)
1773 {
1774   return &gst->plc;
1775 }
1776
1777
1778 /**
1779  * Watch a place for changed objects.
1780  *
1781  * @param place
1782  *        Place to watch.
1783  * @param object_filter
1784  *        Object prefix to match.
1785  * @param var_cb
1786  *        Function to call when an object/state var changes.
1787  * @param cls
1788  *        Closure for callback.
1789  *
1790  * @return Handle that can be used to cancel watching.
1791  */
1792 struct GNUNET_SOCIAL_WatchHandle *
1793 GNUNET_SOCIAL_place_watch (struct GNUNET_SOCIAL_Place *place,
1794                            const char *object_filter,
1795                            GNUNET_PSYC_StateVarCallback var_cb,
1796                            void *cls)
1797 {
1798   return NULL;
1799 }
1800
1801
1802 /**
1803  * Cancel watching a place for changed objects.
1804  *
1805  * @param wh Watch handle to cancel.
1806  */
1807 void
1808 GNUNET_SOCIAL_place_watch_cancel (struct GNUNET_SOCIAL_WatchHandle *wh)
1809 {
1810
1811 }
1812
1813
1814 static struct GNUNET_SOCIAL_HistoryRequest *
1815 place_history_replay (struct GNUNET_SOCIAL_Place *plc,
1816                       uint64_t start_message_id,
1817                       uint64_t end_message_id,
1818                       uint64_t message_limit,
1819                       const char *method_prefix,
1820                       uint32_t flags,
1821                       struct GNUNET_SOCIAL_Slicer *slicer,
1822                       GNUNET_ResultCallback result_cb,
1823                       void *cls)
1824 {
1825   struct GNUNET_PSYC_HistoryRequestMessage *req;
1826   struct GNUNET_SOCIAL_HistoryRequest *hist = GNUNET_malloc (sizeof (*hist));
1827   hist->plc = plc;
1828   hist->recv = GNUNET_PSYC_receive_create (NULL, &slicer_message, slicer);
1829   hist->result_cb = result_cb;
1830   hist->cls = cls;
1831   hist->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
1832                                               &op_recv_history_result, hist);
1833
1834   GNUNET_assert (NULL != method_prefix);
1835   uint16_t method_size = strnlen (method_prefix,
1836                                   GNUNET_SERVER_MAX_MESSAGE_SIZE
1837                                   - sizeof (*req)) + 1;
1838   GNUNET_assert ('\0' == method_prefix[method_size - 1]);
1839   req = GNUNET_malloc (sizeof (*req) + method_size);
1840   req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
1841   req->header.size = htons (sizeof (*req) + method_size);
1842   req->start_message_id = GNUNET_htonll (start_message_id);
1843   req->end_message_id = GNUNET_htonll (end_message_id);
1844   req->message_limit = GNUNET_htonll (message_limit);
1845   req->flags = htonl (flags);
1846   req->op_id = GNUNET_htonll (hist->op_id);
1847   memcpy (&req[1], method_prefix, method_size);
1848
1849   GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
1850   return hist;
1851 }
1852
1853
1854 /**
1855  * Learn about the history of a place.
1856  *
1857  * Messages are returned through the @a slicer function
1858  * and have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
1859  *
1860  * @param place
1861  *        Place we want to learn more about.
1862  * @param start_message_id
1863  *        First historic message we are interested in.
1864  * @param end_message_id
1865  *        Last historic message we are interested in (inclusive).
1866  * @param method_prefix
1867  *        Only retrieve messages with this method prefix.
1868  * @param flags
1869  *        OR'ed GNUNET_PSYC_HistoryReplayFlags
1870  * @param slicer
1871  *        Slicer to use for retrieved messages.
1872  *        Can be the same as the slicer of the place.
1873  * @param result_cb
1874  *        Function called after all messages retrieved.
1875  *        NULL if not needed.
1876  * @param cls Closure for @a result_cb.
1877  */
1878 struct GNUNET_SOCIAL_HistoryRequest *
1879 GNUNET_SOCIAL_place_history_replay (struct GNUNET_SOCIAL_Place *plc,
1880                                     uint64_t start_message_id,
1881                                     uint64_t end_message_id,
1882                                     const char *method_prefix,
1883                                     uint32_t flags,
1884                                     struct GNUNET_SOCIAL_Slicer *slicer,
1885                                     GNUNET_ResultCallback result_cb,
1886                                     void *cls)
1887 {
1888   return place_history_replay (plc, start_message_id, end_message_id, 0,
1889                                method_prefix, flags, slicer, result_cb, cls);
1890 }
1891
1892
1893 /**
1894  * Learn about the history of a place.
1895  *
1896  * Sends messages through the slicer function of the place where
1897  * start_message_id <= message_id <= end_message_id.
1898  * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
1899  *
1900  * To get the latest message, use 0 for both the start and end message ID.
1901  *
1902  * @param place
1903  *        Place we want to learn more about.
1904  * @param message_limit
1905  *        Maximum number of historic messages we are interested in.
1906  * @param method_prefix
1907  *        Only retrieve messages with this method prefix.
1908  * @param flags
1909  *        OR'ed GNUNET_PSYC_HistoryReplayFlags
1910  * @param result_cb
1911  *        Function called after all messages retrieved.
1912  *        NULL if not needed.
1913  * @param cls Closure for @a result_cb.
1914  */
1915 struct GNUNET_SOCIAL_HistoryRequest *
1916 GNUNET_SOCIAL_place_history_replay_latest (struct GNUNET_SOCIAL_Place *plc,
1917                                            uint64_t message_limit,
1918                                            const char *method_prefix,
1919                                            uint32_t flags,
1920                                            struct GNUNET_SOCIAL_Slicer *slicer,
1921                                            GNUNET_ResultCallback result_cb,
1922                                            void *cls)
1923 {
1924   return place_history_replay (plc, 0, 0, message_limit, method_prefix, flags,
1925                                slicer, result_cb, cls);
1926 }
1927
1928
1929 /**
1930  * Cancel learning about the history of a place.
1931  *
1932  * @param hist
1933  *        History lesson to cancel.
1934  */
1935 void
1936 GNUNET_SOCIAL_place_history_replay_cancel (struct GNUNET_SOCIAL_HistoryRequest *hist)
1937 {
1938   GNUNET_PSYC_receive_destroy (hist->recv);
1939   GNUNET_CLIENT_MANAGER_op_cancel (hist->plc->client, hist->op_id);
1940   GNUNET_free (hist);
1941 }
1942
1943
1944 /**
1945  * Request matching state variables.
1946  */
1947 static struct GNUNET_SOCIAL_LookHandle *
1948 place_state_get (struct GNUNET_SOCIAL_Place *plc,
1949                  uint16_t type, const char *name,
1950                  GNUNET_PSYC_StateVarCallback var_cb,
1951                  GNUNET_ResultCallback result_cb, void *cls)
1952 {
1953   struct GNUNET_PSYC_StateRequestMessage *req;
1954   struct GNUNET_SOCIAL_LookHandle *look = GNUNET_malloc (sizeof (*look));
1955   look->plc = plc;
1956   look->var_cb = var_cb;
1957   look->result_cb = result_cb;
1958   look->cls = cls;
1959   look->op_id = GNUNET_CLIENT_MANAGER_op_add (plc->client,
1960                                               &op_recv_state_result, look);
1961
1962   GNUNET_assert (NULL != name);
1963   size_t name_size = strnlen (name, GNUNET_SERVER_MAX_MESSAGE_SIZE
1964                               - sizeof (*req)) + 1;
1965   req = GNUNET_malloc (sizeof (*req) + name_size);
1966   req->header.type = htons (type);
1967   req->header.size = htons (sizeof (*req) + name_size);
1968   req->op_id = GNUNET_htonll (look->op_id);
1969   memcpy (&req[1], name, name_size);
1970
1971   GNUNET_CLIENT_MANAGER_transmit (plc->client, &req->header);
1972   return look;
1973 }
1974
1975
1976 /**
1977  * Look at a particular object in the place.
1978  *
1979  * The best matching object is returned (its name might be less specific than
1980  * what was requested).
1981  *
1982  * @param place
1983  *        The place to look the object at.
1984  * @param full_name
1985  *        Full name of the object.
1986  * @param value_size
1987  *        Set to the size of the returned value.
1988  *
1989  * @return NULL if there is no such object at this place.
1990  */
1991 struct GNUNET_SOCIAL_LookHandle *
1992 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *plc,
1993                              const char *full_name,
1994                              GNUNET_PSYC_StateVarCallback var_cb,
1995                              GNUNET_ResultCallback result_cb,
1996                              void *cls)
1997 {
1998   return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET,
1999                           full_name, var_cb, result_cb, cls);
2000 }
2001
2002
2003 /**
2004  * Look for objects in the place with a matching name prefix.
2005  *
2006  * @param place
2007  *        The place to look its objects at.
2008  * @param name_prefix
2009  *        Look at objects with names beginning with this value.
2010  * @param var_cb
2011  *        Function to call for each object found.
2012  * @param cls
2013  *        Closure for callback function.
2014  *
2015  * @return Handle that can be used to stop looking at objects.
2016  */
2017 struct GNUNET_SOCIAL_LookHandle *
2018 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *plc,
2019                               const char *name_prefix,
2020                               GNUNET_PSYC_StateVarCallback var_cb,
2021                               GNUNET_ResultCallback result_cb,
2022                               void *cls)
2023 {
2024   return place_state_get (plc, GNUNET_MESSAGE_TYPE_PSYC_STATE_GET_PREFIX,
2025                           name_prefix, var_cb, result_cb, cls);
2026 }
2027
2028
2029 /**
2030  * Cancel a state request operation.
2031  *
2032  * @param sr
2033  *        Handle for the operation to cancel.
2034  */
2035 void
2036 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *look)
2037 {
2038   GNUNET_CLIENT_MANAGER_op_cancel (look->plc->client, look->op_id);
2039   GNUNET_free (look);
2040 }
2041
2042
2043 /* end of social_api.c */