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