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