-avoid 'hu', as it is unsigned short, not uint16_t
[oweals/gnunet.git] / src / include / gnunet_social_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_social_service.h
23  * @brief Social service; implements social interactions using the PSYC service.
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27 #ifndef GNUNET_SOCIAL_SERVICE_H
28 #define GNUNET_SOCIAL_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_env_lib.h"
40 #include "gnunet_identity_service.h"
41 #include "gnunet_psyc_service.h"
42
43
44 /**
45  * Version number of GNUnet Social API.
46  */
47 #define GNUNET_SOCIAL_VERSION 0x00000000
48
49
50 /**
51  * Handle for a pseudonym of another user in the network.
52  */
53 struct GNUNET_SOCIAL_Nym;
54
55 /**
56  * Handle for a place where social interactions happen.
57  */
58 struct GNUNET_SOCIAL_Place;
59
60 /**
61  * Host handle for a place that we entered.
62  */
63 struct GNUNET_SOCIAL_Host;
64
65 /**
66  * Guest handle for place that we entered.
67  */
68 struct GNUNET_SOCIAL_Guest;
69
70 /**
71  * Handle to an implementation of try-and-slice.
72  */
73 struct GNUNET_SOCIAL_Slicer;
74
75
76 /**
77  * Function called upon receiving a message indicating a call to a @e method.
78  *
79  * This function is called one or more times for each message until all data
80  * fragments arrive from the network.
81  *
82  * @param cls  Closure.
83  * @param message_id  Message counter, monotonically increasing from 1.
84  * @param nym  The sender of the message.  Can be NULL if the message is not
85  *          connected to a pseudonym.
86  * @param flags  OR'ed GNUNET_PSYC_MessageFlags
87  * @param method_name  Original method name from PSYC (may be more specific
88  *          than the registered method name due to try-and-slice matching).
89  * @param env  Environment containing variables for the message and operations
90  *          on objects of the place, or NULL.
91  *          Only set for the first call of this function for each @a message_id,
92  *          NULL when notifying about further data fragments.
93  * @param data_offset  Byte offset of @a data in the overall data of the method.
94  * @param data_size  Number of bytes in @a data.
95  * @param data  Data stream given to the method (might not be zero-terminated
96  *          if data is binary).
97  * @param end  End of message?
98  *          #GNUNET_NO  if there are further fragments,
99  *          #GNUNET_YES if this is the last fragment,
100  *          #GNUNET_SYSERR indicates the message was cancelled by the sender.
101  *
102  * @return #GNUNET_YES the application keeps @a env for further use,
103  *         #GNUNET_NO  @a env is free()'d after the function returns.
104  */
105 typedef int
106 (*GNUNET_SOCIAL_MethodCallback) (void *cls,
107                                  uint64_t message_id,
108                                  uint32_t flags,
109                                  const struct GNUNET_SOCIAL_Nym *nym,
110                                  const char *method_name,
111                                  struct GNUNET_ENV_Environment *env,
112                                  uint64_t data_offset,
113                                  size_t data_size,
114                                  const void *data,
115                                  int end);
116
117
118 /**
119  * Create a try-and-slice instance.
120  *
121  * @return A new try-and-slice construct.
122  */
123 struct GNUNET_SOCIAL_Slicer *
124 GNUNET_SOCIAL_slicer_create (void);
125
126
127 /**
128  * Add a method to the try-and-slice instance.
129  *
130  * A slicer processes messages and calls methods that match a message. A match
131  * happens whenever the method name of a message starts with the method_name
132  * parameter given here.
133  *
134  * @param slicer The try-and-slice instance to extend.
135  * @param method_name Name of the given method, use empty string for default.
136  * @param method Method to invoke.
137  * @param method_cls Closure for method.
138  */
139 void
140 GNUNET_SOCIAL_slicer_add (struct GNUNET_SOCIAL_Slicer *slicer,
141                           const char *method_name,
142                           GNUNET_SOCIAL_MethodCallback method_cb,
143                           void *cls);
144
145
146 /**
147  * Remove a registered method from the try-and-slice instance.
148  *
149  * @param slicer The try-and-slice instance.
150  * @param method_name Name of the method to remove.
151  * @param method Method handler.
152  */
153 void
154 GNUNET_SOCIAL_slicer_remove (struct GNUNET_SOCIAL_Slicer *slicer,
155                              const char *method_name,
156                              GNUNET_SOCIAL_MethodCallback method_cb);
157
158 /**
159  * Destroy a given try-and-slice instance.
160  *
161  * @param slicer slicer to destroy
162  */
163 void
164 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer);
165
166
167 /**
168  * Function called asking for nym to be admitted to the place.
169  *
170  * Should call either GNUNET_SOCIAL_host_admit() or
171  * GNUNET_SOCIAL_host_reject_entry() (possibly asynchronously).  If this host
172  * cannot decide, it is fine to call neither function, in which case hopefully
173  * some other host of the place exists that will make the decision.  The @a nym
174  * reference remains valid until the #GNUNET_SOCIAL_FarewellCallback is invoked
175  * for it.
176  *
177  * @param cls Closure.
178  * @param nym Handle for the user who wants to enter.
179  * @param method_name Method name in the entry request.
180  * @param variable_count Number of elements in the @a variables array.
181  * @param variables Variables present in the message.
182  * @param data Payload given on enter (e.g. a password).
183  * @param data_size Number of bytes in @a data.
184  */
185 typedef void
186 (*GNUNET_SOCIAL_AnswerDoorCallback) (void *cls,
187                                      struct GNUNET_SOCIAL_Nym *nym,
188                                      size_t variable_count,
189                                      const char *method_name,
190                                      struct GNUNET_ENV_Modifier *variables,
191                                      const void *data,
192                                      size_t data_size);
193
194
195 /**
196  * Function called when a @a nym leaves the place.
197  *
198  * This is also called if the @a nym was never given permission to enter
199  * (i.e. the @a nym stopped asking to get in).
200  *
201  * @param cls Closure.
202  * @param nym Handle for the user who left.
203  * @param variable_count Number of elements in the @a variables array.
204  * @param variables Variables present in the message.
205  */
206 typedef void
207 (*GNUNET_SOCIAL_FarewellCallback) (void *cls,
208                                    struct GNUNET_SOCIAL_Nym *nym,
209                                    size_t variable_count,
210                                    struct GNUNET_ENV_Modifier *variables);
211
212
213 /**
214  * Enter a place as host.
215  *
216  * A place is created upon first entering, and it is active until permanently
217  * left using GNUNET_SOCIAL_host_leave().
218  *
219  * @param cfg  Configuration to contact the social service.
220  * @param place_keyfile  File with the private-public key pair of the place,
221  *        created if the file does not exist; pass NULL for ephemeral places.
222  * @param policy  Policy specifying entry and history restrictions of the place.
223  * @param ego  Identity of the host.
224  * @param slicer  Slicer to handle incoming messages.
225  * @param listener_cb  Function to handle new nyms that want to enter.
226  * @param farewell_cb  Function to handle departing nyms.
227  * @param cls  Closure for @a listener_cb and @a farewell_cb.
228  *
229  * @return Handle for the host.
230  */
231 struct GNUNET_SOCIAL_Host *
232 GNUNET_SOCIAL_host_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
233                           const char *place_keyfile,
234                           enum GNUNET_PSYC_Policy policy,
235                           struct GNUNET_IDENTITY_Ego *ego,
236                           struct GNUNET_SOCIAL_Slicer *slicer,
237                           GNUNET_SOCIAL_AnswerDoorCallback listener_cb,
238                           GNUNET_SOCIAL_FarewellCallback farewell_cb,
239                           void *cls);
240
241
242 /**
243  * Admit @a nym to the place.
244  *
245  * The @a nym reference will remain valid until either the @a host or @a nym
246  * leaves the place.
247  *
248  * @param host  Host of the place.
249  * @param nym  Handle for the entity that wants to enter.
250  */
251 void
252 GNUNET_SOCIAL_host_admit (struct GNUNET_SOCIAL_Host *host,
253                           struct GNUNET_SOCIAL_Nym *nym);
254
255
256 /**
257  * Throw @a nym out of the place.
258  *
259  * The @a nym reference will remain valid until the
260  * #GNUNET_SOCIAL_FarewellCallback is invoked,
261  * which should be very soon after this call.
262  *
263  * @param host  Host of the place.
264  * @param nym  Handle for the entity to be ejected.
265  */
266 void
267 GNUNET_SOCIAL_host_eject (struct GNUNET_SOCIAL_Host *host,
268                           struct GNUNET_SOCIAL_Nym *nym);
269
270
271 /**
272  * Refuse @a nym entry into the place.
273  *
274  * @param host  Host of the place.
275  * @param nym Handle for the entity that wanted to enter.
276  * @param method_name Method name for the rejection message.
277  * @param env Environment containing variables for the message, or NULL.
278  * @param data Data for the rejection message to send back.
279  * @param data_size Number of bytes in @a data for method.
280  */
281 void
282 GNUNET_SOCIAL_host_refuse_entry (struct GNUNET_SOCIAL_Host *host,
283                                  struct GNUNET_SOCIAL_Nym *nym,
284                                  const char *method_name,
285                                  const struct GNUNET_ENV_Environment *env,
286                                  const void *data,
287                                  size_t data_size);
288
289
290 /**
291  * Get the public key of a @a nym.
292  *
293  * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
294  *
295  * @param nym Pseudonym to map to a cryptographic identifier.
296  * @param[out] nym_key Set to the public key of the nym.
297  */
298 void
299 GNUNET_SOCIAL_nym_get_key (struct GNUNET_SOCIAL_Nym *nym,
300                            struct GNUNET_CRYPTO_EddsaPublicKey *nym_key);
301
302
303 /**
304  * Obtain the private-public key pair of the host.
305  *
306  * @param host  Host to get the key of.
307  * @param[out] host_key  Set to the private-public key pair of the host.  The
308  *                 public part is suitable for storing in GNS within a "PLACE"
309  *                 record, along with peer IDs to join at.
310  */
311 void
312 GNUNET_SOCIAL_host_get_key (struct GNUNET_SOCIAL_Host *host,
313                             struct GNUNET_CRYPTO_EddsaPrivateKey *host_key);
314
315
316 /**
317  * Advertise the place in the GNS zone of the @e ego of the @a host.
318  *
319  * @param host  Host of the place.
320  * @param name The name for the PLACE record to put in the zone.
321  * @param peer_count Number of elements in the @a peers array.
322  * @param peers List of peers in the PLACE record that can be used to send join
323  *        requests to.
324  * @param expiration_time Expiration time of the record, use 0 to remove the record.
325  * @param password Password used to encrypt the record.
326  */
327 void
328 GNUNET_SOCIAL_host_advertise (struct GNUNET_SOCIAL_Host *host,
329                               const char *name,
330                               size_t peer_count,
331                               const struct GNUNET_PeerIdentity *peers,
332                               struct GNUNET_TIME_Relative expiration_time,
333                               const char *password);
334
335
336 /**
337  * Flags for announcements by a host.
338  */
339 enum GNUNET_SOCIAL_AnnounceFlags
340 {
341   GNUNET_SOCIAL_ANNOUNCE_NONE = 0,
342
343   /**
344    * Whether this announcement removes all objects from the place.
345    *
346    * New objects can be still added to the now empty place using the @e env
347    * parameter of the same announcement.
348    */
349   GNUNET_SOCIAL_ANNOUNCE_CLEAR_OBJECTS = 1 << 0
350 };
351
352
353 /**
354  * Handle for an announcement request.
355  */
356 struct GNUNET_SOCIAL_Announcement;
357
358
359 /**
360  * Send a message to all nyms that are present in the place.
361  *
362  * This function is restricted to the host.  Nyms can only send requests
363  * to the host who can decide to relay it to everyone in the place.
364  *
365  * @param host  Host of the place.
366  * @param method_name Method to use for the announcement.
367  * @param env  Environment containing variables for the message and operations
368  *          on objects of the place.  Can be NULL.
369  * @param notify Function to call to get the payload of the announcement.
370  * @param notify_cls Closure for @a notify.
371  * @param flags Flags for this announcement.
372  *
373  * @return NULL on error (announcement already in progress?).
374  */
375 struct GNUNET_SOCIAL_Announcement *
376 GNUNET_SOCIAL_host_announce (struct GNUNET_SOCIAL_Host *host,
377                              const char *method_name,
378                              const struct GNUNET_ENV_Environment *env,
379                              GNUNET_CONNECTION_TransmitReadyNotify notify,
380                              void *notify_cls,
381                              enum GNUNET_SOCIAL_AnnounceFlags flags);
382
383
384 /**
385  * Cancel announcement.
386  *
387  * @param a The announcement to cancel.
388  */
389 void
390 GNUNET_SOCIAL_host_announce_cancel (struct GNUNET_SOCIAL_Announcement *a);
391
392
393 /**
394  * Obtain handle for a hosted place.
395  *
396  * The returned handle can be used to access the place API.
397  *
398  * @param host  Handle for the host.
399  *
400  * @return Handle for the hosted place, valid as long as @a host is valid.
401  */
402 struct GNUNET_SOCIAL_Place *
403 GNUNET_SOCIAL_host_get_place (struct GNUNET_SOCIAL_Host *host);
404
405
406 /**
407  * Stop hosting a place.
408  *
409  * Invalidates host handle.
410  *
411  * @param host  Host leaving the place.
412  * @param keep_active  Keep the place active after last host disconnected.
413  */
414 void
415 GNUNET_SOCIAL_host_leave (struct GNUNET_SOCIAL_Host *host, int keep_active);
416
417
418 /**
419  * Request entry to a place as a guest.
420  *
421  * @param cfg  Configuration to contact the social service.
422  * @param ego  Identity of the guest.
423  * @param address GNS name of the place to enter.  Either in the form of
424  *        'room.friend.gnu', or 'NYMPUBKEY.zkey'.  This latter case refers to
425  *        the 'PLACE' record of the empty label ("+") in the GNS zone with the
426  *        nym's public key 'NYMPUBKEY', and can be used to request entry to a
427  *        pseudonym's place directly.
428  * @param method_name Method name for the message.
429  * @param env Environment containing variables for the message, or NULL.
430  * @param data Payload for the message to give to the enter callback.
431  * @param data_size Number of bytes in @a data.
432  * @param slicer Slicer to use for processing incoming requests from guests.
433  *
434  * @return NULL on errors, otherwise handle for the guest.
435  */
436 struct GNUNET_SOCIAL_Guest *
437 GNUNET_SOCIAL_guest_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
438                            struct GNUNET_IDENTITY_Ego *ego,
439                            char *address,
440                            const char *method_name,
441                            const struct GNUNET_ENV_Environment *env,
442                            const void *data,
443                            size_t data_size,
444                            struct GNUNET_SOCIAL_Slicer *slicer);
445
446 /**
447  * Request entry to a place as a guest.
448  *
449  * @param cfg Configuration to contact the social service.
450  * @param ego  Identity of the guest.
451  * @param crypto_address Public key of the place to enter.
452  * @param origin Peer identity of the origin of the underlying multicast group.
453  * @param relay_count Number of elements in the @a relays array.
454  * @param relays Relays for the underlying multicast group.
455  * @param method_name Method name for the message.
456  * @param env Environment containing variables for the message, or NULL.
457  * @param data Payload for the message to give to the enter callback.
458  * @param data_size Number of bytes in @a data.
459  * @param slicer Slicer to use for processing incoming requests from guests.
460  *
461  * @return NULL on errors, otherwise handle for the guest.
462  */
463 struct GNUNET_SOCIAL_Guest *
464 GNUNET_SOCIAL_guest_enter2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
465                             struct GNUNET_IDENTITY_Ego *ego,
466                             struct GNUNET_CRYPTO_EddsaPublicKey *crypto_address,
467                             struct GNUNET_PeerIdentity *origin,
468                             size_t relay_count,
469                             struct GNUNET_PeerIdentity *relays,
470                             const char *method_name,
471                             const struct GNUNET_ENV_Environment *env,
472                             const void *data,
473                             size_t data_size,
474                             struct GNUNET_SOCIAL_Slicer *slicer);
475
476
477 /**
478  * Flags for talking to the host of a place.
479  */
480 enum GNUNET_SOCIAL_TalkFlags
481 {
482   GNUNET_SOCIAL_TALK_NONE = 0
483 };
484
485
486 /**
487  * A talk request.
488  */
489 struct GNUNET_SOCIAL_TalkRequest;
490
491
492 /**
493  * Talk to the host of the place.
494  *
495  * @param place Place where we want to talk to the host.
496  * @param method_name Method to invoke on the host.
497  * @param env Environment containing variables for the message, or NULL.
498  * @param notify Function to use to get the payload for the method.
499  * @param notify_cls Closure for @a notify.
500  * @param flags Flags for the message being sent.
501  *
502  * @return NULL if we are already trying to talk to the host,
503  *         otherwise handle to cancel the request.
504  */
505 struct GNUNET_SOCIAL_TalkRequest *
506 GNUNET_SOCIAL_guest_talk (struct GNUNET_SOCIAL_Place *place,
507                           const char *method_name,
508                           const struct GNUNET_ENV_Environment *env,
509                           GNUNET_CONNECTION_TransmitReadyNotify notify,
510                           void *notify_cls,
511                           enum GNUNET_SOCIAL_TalkFlags flags);
512
513
514 /**
515  * Cancel talking to the host of the place.
516  *
517  * @param tr Talk request to cancel.
518  */
519 void
520 GNUNET_SOCIAL_guest_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr);
521
522
523 /**
524  * Leave a place permanently.
525  *
526  * Notifies the owner of the place about leaving, and destroys the place handle.
527  *
528  * @param place Place to leave permanently.
529  * @param keep_active Keep place active after last application disconnected.
530  */
531 void
532 GNUNET_SOCIAL_guest_leave (struct GNUNET_SOCIAL_Place *place, int keep_active);
533
534
535 /**
536  * Obtain handle for a place entered as guest.
537  *
538  * The returned handle can be used to access the place API.
539  *
540  * @param guest  Handle for the guest.
541  *
542  * @return Handle for the place, valid as long as @a guest is valid.
543  */
544 struct GNUNET_SOCIAL_Place *
545 GNUNET_SOCIAL_guest_get_place (struct GNUNET_SOCIAL_Host *guest);
546
547
548 /**
549  * A history lesson.
550  */
551 struct GNUNET_SOCIAL_HistoryLesson;
552
553 /**
554  * Learn about the history of a place.
555  *
556  * Sends messages through the slicer function of the place where
557  * start_message_id <= message_id <= end_message_id.
558  * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
559  *
560  * To get the latest message, use 0 for both the start and end message ID.
561  *
562  * @param place Place we want to learn more about.
563  * @param start_message_id First historic message we are interested in.
564  * @param end_message_id Last historic message we are interested in (inclusive).
565  * @param slicer Slicer to use to process history.  Can be the same as the
566  *               slicer of the place, as the HISTORIC flag allows distinguishing
567  *               old messages from fresh ones.
568  * @param finish_cb Function called after the last message in the history lesson
569  *              is passed through the @a slicer. NULL if not needed.
570  * @param finish_cb_cls Closure for @a finish_cb.
571  * @return Handle to abort history lesson, never NULL (multiple lessons
572  *         at the same time are allowed).
573  */
574 struct GNUNET_SOCIAL_HistoryLesson *
575 GNUNET_SOCIAL_place_get_history (struct GNUNET_SOCIAL_Place *place,
576                                  uint64_t start_message_id,
577                                  uint64_t end_message_id,
578                                  const struct GNUNET_SOCIAL_Slicer *slicer,
579                                  void (*finish_cb)(void *),
580                                  void *finish_cb_cls);
581
582
583 /**
584  * Stop processing messages from the history lesson.
585  *
586  * Must not be called after the finish callback of the history lesson is called.
587  *
588  * @param hist History lesson to cancel.
589  */
590 void
591 GNUNET_SOCIAL_place_get_history_cancel (struct GNUNET_SOCIAL_HistoryLesson *hist);
592
593
594 struct GNUNET_SOCIAL_WatchHandle;
595
596 /**
597  * Watch a place for changed objects.
598  *
599  * @param place Place to watch.
600  * @param object_filter Object prefix to match.
601  * @param state_cb Function to call when an object/state changes.
602  * @param state_cb_cls Closure for callback.
603  *
604  * @return Handle that can be used to cancel watching.
605  */
606 struct GNUNET_SOCIAL_WatchHandle *
607 GNUNET_SOCIAL_place_watch (struct GNUNET_SOCIAL_Place *place,
608                            const char *object_filter,
609                            GNUNET_PSYC_StateCallback state_cb,
610                            void *state_cb_cls);
611
612
613 /**
614  * Cancel watching a place for changed objects.
615  *
616  * @param wh Watch handle to cancel.
617  */
618 void
619 GNUNET_SOCIAL_place_watch_cancel (struct GNUNET_SOCIAL_WatchHandle *wh);
620
621
622 struct GNUNET_SOCIAL_LookHandle;
623
624
625 /**
626  * Look at objects in the place with a matching name prefix.
627  *
628  * @param place The place to look its objects at.
629  * @param name_prefix Look at objects with names beginning with this value.
630  * @param state_cb Function to call for each object found.
631  * @param state_cb_cls Closure for callback function.
632  *
633  * @return Handle that can be used to stop looking at objects.
634  */
635 struct GNUNET_SOCIAL_LookHandle *
636 GNUNET_SOCIAL_place_look (struct GNUNET_SOCIAL_Place *place,
637                           const char *name_prefix,
638                           GNUNET_PSYC_StateCallback state_cb,
639                           void *state_cb_cls);
640
641
642 /**
643  * Stop looking at objects.
644  *
645  * @param lh Look handle to stop.
646  */
647 void
648 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *lh);
649
650
651
652 /**
653  * Look at a particular object in the place.
654  *
655  * The best matching object is returned (its name might be less specific than
656  * what was requested).
657  *
658  * @param place The place to look the object at.
659  * @param full_name Full name of the object.
660  * @param value_size Set to the size of the returned value.
661  * @return NULL if there is no such object at this place.
662  */
663 const void *
664 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *place,
665                              const char *full_name,
666                              size_t *value_size);
667
668
669 #if 0                           /* keep Emacsens' auto-indent happy */
670 {
671 #endif
672 #ifdef __cplusplus
673 }
674 #endif
675
676 /* ifndef GNUNET_SOCIAL_SERVICE_H */
677 #endif
678 /* end of gnunet_social_service.h */