missing check
[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 functionality 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_psyc_lib.h"
40 #include "gnunet_psyc_service.h"
41 #include "gnunet_multicast_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 another user (who is likely pseudonymous) 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  * Handle for a place that one of our egos hosts.
62  */
63 struct GNUNET_SOCIAL_Home;
64
65 /** 
66  * Handle to an implementation of try-and-slice.
67  */
68 struct GNUNET_SOCIAL_Slicer;
69
70
71 /** 
72  * Method called from SOCIAL upon receiving a message indicating a call
73  * to a @e method.
74  *
75  * @param cls Closure.
76  * @param nym The sender of the message. NULL for the ego's own messages to the home.
77  * @param full_method_name Original method name from PSYC (may be more
78  *        specific than the registered method name due to try-and-slice matching).
79
80  * @param message_id Unique message counter for this message
81  *                   (unique only in combination with the given sender for
82  *                    this channel).
83  * @param modifier_count Number of elements in the @a modifiers array.
84  * @param modifiers Modifiers present in the message. FIXME: use environment instead?
85  * @param data_offset Byte offset of @a data in the overall data of the method.
86  * @param data_size Number of bytes in @a data.
87  * @param data Data stream given to the method (might not be zero-terminated
88  *             if data is binary).
89  * @param flags Message flags indicating fragmentation status.
90  */
91 typedef int (*GNUNET_SOCIAL_Method)(void *cls,
92                                     struct GNUNET_SOCIAL_Nym *nym,
93                                     const char *full_method_name,
94                                     uint64_t message_id,
95                                     size_t modifier_count,
96                                     GNUNET_PSYC_Modifier *modifiers,
97                                     uint64_t data_offset,
98                                     size_t data_size,
99                                     const void *data,
100                                     enum GNUNET_PSYC_MessageFlags flags);
101
102
103 /** 
104  * Create a try-and-slice instance.
105  *
106  * @return A new try-and-slice construct.
107  */
108 struct GNUNET_SOCIAL_Slicer *
109 GNUNET_SOCIAL_slicer_create (void);
110
111
112 /** 
113  * Add a method to the try-and-slice instance.
114  *
115  * A slicer processes messages and calls methods that match a message. A match
116  * happens whenever the method name of a message starts with the method_name
117  * parameter given here.
118  *
119  * @param slicer The try-and-slice instance to extend.
120  * @param method_name Name of the given method, use empty string for default.
121  * @param method Method to invoke.
122  * @param method_cls Closure for method.
123  */
124 void
125 GNUNET_SOCIAL_slicer_add (struct GNUNET_SOCIAL_Slicer *slicer,
126                           const char *method_name,
127                           GNUNET_SOCIAL_Method method,
128                           void *method_cls);
129
130
131 /** 
132  * Remove a registered method from the try-and-slice instance.
133  *
134  * @param slicer The try-and-slice instance.
135  * @param method_name Name of the method to remove.
136  * @param method Method handler.
137  */
138 void
139 GNUNET_SOCIAL_slicer_remove (struct GNUNET_SOCIAL_Slicer *slicer,
140                              const char *method_name,
141                              GNUNET_SOCIAL_Method method);
142
143 /** 
144  * Destroy a given try-and-slice instance.
145  *
146  * @param slicer slicer to destroy
147  */
148 void
149 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer);
150
151
152 /** 
153  * Function called asking for nym to be admitted to the place.
154  *
155  * Should call either GNUNET_SOCIAL_home_admit() or
156  * GNUNET_SOCIAL_home_reject_entry() (possibly asynchronously).  If this owner
157  * cannot decide, it is fine to call neither function, in which case hopefully
158  * some other owner of the home exists that will make the decision. The @a nym
159  * reference remains valid until the #GNUNET_SOCIAL_FarewellCallback is invoked
160  * for it.
161  *
162  * @param cls Closure.
163  * @param nym Handle for the user who wants to enter.
164  * @param method_name Method name in the entry request.
165  * @param variable_count Number of elements in the @a variables array.
166  * @param variables Variables present in the message.
167  * @param data_size Number of bytes in @a data.
168  * @param data Payload given on enter (e.g. a password).
169  */
170 typedef void (*GNUNET_SOCIAL_AnswerDoorCallback)(void *cls,
171                                                  struct GNUNET_SOCIAL_Nym *nym,
172                                                  size_t variable_count,
173                                                  const char *method_name,
174                                                  GNUNET_PSYC_Modifier *variables,
175                                                  size_t data_size,
176                                                  const void *data);
177
178
179 /** 
180  * Function called when a @a nym leaves the place.
181  * 
182  * This is also called if the @a nym was never given permission to enter
183  * (i.e. the @a nym stopped asking to get in).
184  *
185  * @param cls Closure.
186  * @param nym Handle for the user who left.
187  * @param variable_count Number of elements in the @a variables array.
188  * @param variables Variables present in the message.
189  */
190 typedef void (*GNUNET_SOCIAL_FarewellCallback)(void *cls,
191                                                struct GNUNET_SOCIAL_Nym *nym,
192                                                size_t variable_count,
193                                                GNUNET_PSYC_Modifier *variables);
194
195
196 /** 
197  * Enter a home where guests (nyms) can be hosted.
198  *
199  * A home is created upon first entering, and exists until
200  * GNUNET_SOCIAL_home_destroy() is called. It can also be left temporarily using
201  * GNUNET_SOCIAL_home_leave().
202  *
203  * @param cfg Configuration to contact the social service.
204  * @param home_keyfile File with the private-public key pair of the home,
205  *        created if the file does not exist; pass NULL for ephemeral homes.
206  * @param policy Policy specifying entry and history restrictions of the home.
207  * @param ego Owner of the home (host).
208  * @param slicer Slicer to handle guests talking.
209  * @param listener_cb Function to handle new nyms that want to enter.
210  * @param farewell_cb Function to handle departing nyms.
211  * @param cls Closure for @a listener_cb and @a farewell_cb.
212  * @return Handle for a new home.
213  */
214 struct GNUNET_SOCIAL_Home *
215 GNUNET_SOCIAL_home_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
216                           const char *home_keyfile,
217                           enum GNUNET_PSYC_Policy policy,
218                           struct GNUNET_IDENTITY_Ego *ego,
219                           struct GNUNET_SOCIAL_Slicer *slicer,
220                           GNUNET_SOCIAL_AnswerDoorCallback listener_cb,
221                           GNUNET_SOCIAL_FarewellCallback farewell_cb,
222                           void *cls);
223
224
225 /** 
226  * Admit @a nym to the @a home.
227  *
228  * The @a nym reference will remain valid until either the home is destroyed or
229  * @a nym leaves.
230  *
231  * @param home Home to allow @a nym to enter.
232  * @param nym Handle for the entity that wants to enter.
233  */
234 void
235 GNUNET_SOCIAL_home_admit (struct GNUNET_SOCIAL_Home *home,
236                           struct GNUNET_SOCIAL_Nym *nym);
237
238
239 /** 
240  * Throw @a nym out of the @a home.
241  *
242  * The @a nym reference will remain valid until the
243  * #GNUNET_SOCIAL_FarewellCallback is invoked,
244  * which should be very soon after this call.
245  *
246  * @param home Home to eject @a nym from.
247  * @param nym Handle for the entity to be ejected.
248  */
249 void
250 GNUNET_SOCIAL_home_eject (struct GNUNET_SOCIAL_Home *home,
251                           struct GNUNET_SOCIAL_Nym *nym);
252
253
254 /** 
255  * Refuse @a nym entry into the @a home.
256  *
257  * @param home Home to disallow @a nym to enter.
258  * @param nym Handle for the entity that wanted to enter.
259  * @param method_name Method name for the rejection message.
260  * @param env Environment containing variables for the message, or NULL.
261  * @param data_size Number of bytes in @a data for method.
262  * @param data Data for the rejection message to send back.
263  */
264 void
265 GNUNET_SOCIAL_home_reject_entry (struct GNUNET_SOCIAL_Home *home,
266                                  struct GNUNET_SOCIAL_Nym *nym,
267                                  const char *method_name,
268                                  const struct GNUNET_ENV_Environment *env,
269                                  size_t data_size,
270                                  const void *data);
271
272
273 /** 
274  * Get the public key of a nym.
275  *
276  * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
277  *
278  * @param nym Pseudonym to map to a cryptographic identifier.
279  * @param[out] nym_key Set to the public key of the nym.
280  */
281 void
282 GNUNET_SOCIAL_nym_get_key (struct GNUNET_SOCIAL_Nym *nym,
283                            struct GNUNET_CRYPTO_EccPublicKey *nym_key);
284
285
286 /** 
287  * Obtain the private-public key pair of the home.
288  * 
289  * @param home Home to get the key of.
290  * @param[out] home_key Set to the private-public key pair of the home.  The public part is suitable for storing in GADS within a "PLACE" record, along with peer IDs to join at.
291  */
292 void
293 GNUNET_SOCIAL_home_get_key (struct GNUNET_SOCIAL_Home *home,
294                             struct GNUNET_CRYPTO_EccPrivateKey *home_key);
295
296
297 /** 
298  * Advertise @a home under @a name in the GADS zone of the @e ego.
299  *
300  * @param home The home to advertise.
301  * @param name The name for the PLACE record to put in the zone.
302  * @param peer_count Number of elements in the @a peers array.
303  * @param peers List of peers in the PLACE record that can be used to send join
304  *        requests to.
305  * @param expiration_time Expiration time of the record, use 0 to remove the record.
306  * @param password Password used to encrypt the record.
307  */
308 void
309 GNUNET_SOCIAL_home_advertise (struct GNUNET_SOCIAL_Home *home,
310                               const char *name,
311                               size_t peer_count,
312                               const struct GNUNET_PeerIdentity *peers,
313                               GNUNET_TIME_Relative expiration_time,
314                               const char *password);
315
316
317 /**
318  * Flags for announcements in a home.
319  */
320 enum GNUNET_PSYC_AnnouncementFlags
321 {
322   /** 
323    * Whether this announcement removes all objects from the home.
324    * 
325    * New objects can be still added to the now empty home using the @e env
326    * parameter of the same announcement.
327    */
328   GNUNET_SOCIAL_ANNOUNCEMENT_CLEAR_OBJECTS = 1 << 0
329 };
330
331
332 /** 
333  * Handle for an announcement request.
334  */
335 struct GNUNET_SOCIAL_Announcement;
336
337
338 /** 
339  * Send a message to all nyms that are present in the home.
340  *
341  * This function is restricted to the home owner.  Nyms can only send requests
342  * to the home owner who can decide to relay it to other guests.
343  *
344  * @param home Home to address the announcement to.
345  * @param method_name Method to use for the announcement.
346  * @param env Environment containing variables for the message and operations on
347  *        objects of the home, or NULL.
348  * @param notify Function to call to get the payload of the announcement.
349  * @param notify_cls Closure for @a notify.
350  * @param flags Flags for this announcement.
351  * @return NULL on error (announcement already in progress?).
352  */
353 struct GNUNET_SOCIAL_Announcement *
354 GNUNET_SOCIAL_home_announce (struct GNUNET_SOCIAL_Home *home,
355                              const char *method_name,
356                              const struct GNUNET_ENV_Environment *env,
357                              GNUNET_CONNECTION_TransmitReadyNotify notify,
358                              void *notify_cls,
359                              GNUNET_SOCIAL_AnnouncementFlags flags);
360
361
362 /** 
363  * Cancel announcement.
364  *
365  * @param a The announcement to cancel.
366  */
367 void
368 GNUNET_SOCIAL_home_announce_cancel (struct GNUNET_SOCIAL_Announcement *a);
369
370
371 /** 
372  * Convert our home to a place so we can access it via the place API.
373  *
374  * @param home Handle for the home.
375  * @return Place handle for the same home, valid as long as @a home is valid;
376  *         do NOT try to GNUNET_SOCIAL_place_leave() this place, it's your home!
377  */
378 struct GNUNET_SOCIAL_Place *
379 GNUNET_SOCIAL_home_get_place (struct GNUNET_SOCIAL_Home *home);
380
381
382 /** 
383  * Leave a home temporarily, visitors can stay.
384  *
385  * After leaving, handling of incoming messages are left to other clients of the
386  * social service, and stops after the last client exits.
387  *
388  * @param home Home to leave temporarily (handle becomes invalid).
389  */
390 void
391 GNUNET_SOCIAL_home_away (struct GNUNET_SOCIAL_Home *home);
392
393
394 /** 
395  * Leave a home.
396
397  * Invalidates home handle.
398  * Guests will be disconnected until the home is restarted.
399  *
400  * @param home Home to leave.
401  */
402 void
403 GNUNET_SOCIAL_home_leave (struct GNUNET_SOCIAL_Home *home);
404
405 /** 
406  * Request entry to a place (home hosted by someone else).
407  *
408  * @param cfg Configuration to contact the social service.
409  * @param ego Owner of the home (host).
410  * @param address GADS name of the place to enter.  Either in the form of
411  *        'room.friend.gnu', or 'NYMPUBKEY.zkey'.  This latter case refers to
412  *        the 'PLACE' record of the empty label ("+") in the GADS zone with the
413  *        nym's public key 'NYMPUBKEY', and can be used to request entry to a
414  *        pseudonym's place directly.
415  * @param method_name Method name for the message.
416  * @param env Environment containing variables for the message, or NULL.
417  * @param data_size Number of bytes in @a data.
418  * @param data Payload for the message to give to the enter callback.
419  * @param slicer Slicer to use for processing incoming requests from guests.
420  * @return NULL on errors, otherwise handle to the place.
421  */
422 struct GNUNET_SOCIAL_Place *
423 GNUNET_SOCIAL_place_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
424                            struct GNUNET_IDENTITY_Ego *ego,
425                            char *address,
426                            const char *method_name,
427                            const struct GNUNET_ENV_Environment *env,
428                            size_t data_size,
429                            const void *data,
430                            struct GNUNET_SOCIAL_Slicer *slicer);
431
432 /** 
433  * Request entry to a place (home hosted by someone else).
434  *
435  * @param cfg Configuration to contact the social service.
436  * @param ego Owner of the home (host).
437  * @param crypto_address Public key of the place to enter.
438  * @param origin Peer identity of the origin of the underlying multicast group.
439  * @param relay_count Number of elements in the @a relays array.
440  * @param relays Relays for the underlying multicast group.
441  * @param method_name Method name for the message.
442  * @param env Environment containing variables for the message, or NULL.
443  * @param data_size Number of bytes in @a data.
444  * @param data Payload for the message to give to the enter callback.
445  * @param slicer Slicer to use for processing incoming requests from guests.
446  * @return NULL on errors, otherwise handle to the place.
447  */
448 struct GNUNET_SOCIAL_Place *
449 GNUNET_SOCIAL_place_enter2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
450                             struct GNUNET_IDENTITY_Ego *ego,
451                             struct GNUNET_CRYPTO_EccPublicKey *crypto_address,
452                             struct GNUNET_PeerIdentity *origin,
453                             size_t count_relays,
454                             struct GNUNET_PeerIdentity *relays,
455                             const char *method_name,
456                             const struct GNUNET_ENV_Environment *env,
457                             size_t data_size,
458                             const void *data,
459                             struct GNUNET_SOCIAL_Slicer *slicer);
460
461
462 struct GNUNET_SOCIAL_WatchHandle;
463
464 /** 
465  * Watch a place for changed objects.
466  *
467  * @param place Place to watch.
468  * @param object_filter Object prefix to match.
469  * @param state_cb Function to call when an object/state changes.
470  * @param state_cb_cls Closure for callback.
471  * 
472  * @return Handle that can be used to cancel watching.
473  */
474 struct GNUNET_SOCIAL_WatchHandle *
475 GNUNET_SOCIAL_place_watch (struct GNUNET_SOCIAL_Place *place,
476                            const char *object_filter,
477                            GNUNET_PSYC_StateCallback state_cb,
478                            void *state_cb_cls);
479
480
481 /** 
482  * Cancel watching a place for changed objects.
483  *
484  * @param wh Watch handle to cancel.
485  */
486 void
487 GNUNET_SOCIAL_place_watch_cancel (struct GNUNET_SOCIAL_WatchHandle *wh);
488
489
490 struct GNUNET_SOCIAL_LookHandle;
491
492
493 /** 
494  * Look at objects in the place with a matching name prefix.
495  *
496  * @param place The place to look its objects at.
497  * @param name_prefix Look at objects with names beginning with this value.
498  * @param state_cb Function to call for each object found.
499  * @param state_cb_cls Closure for callback function.
500  * 
501  * @return Handle that can be used to stop looking at objects.
502  */
503 struct GNUNET_SOCIAL_LookHandle *
504 GNUNET_SOCIAL_place_look (struct GNUNET_SOCIAL_Place *place,
505                           const char *name_prefix,
506                           GNUNET_PSYC_StateCallback state_cb,
507                           void *state_cb_cls);
508
509
510 /** 
511  * Stop looking at objects.
512  *
513  * @param lh Look handle to stop.
514  */
515 void
516 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *lh);
517
518
519
520 /** 
521  * Look at a particular object in the place.
522  *
523  * The best matching object is returned (its name might be less specific than
524  * what was requested).
525  *
526  * @param place The place to look the object at.
527  * @param full_name Full name of the object.
528  * @param value_size Set to the size of the returned value.
529  * @return NULL if there is no such object at this place.
530  */
531 const void *
532 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *place,
533                              const char *full_name,
534                              size_t *value_size);
535
536
537 /**
538  * Flags for talking to the host of a place.
539  */
540 enum GNUNET_SOCIAL_TalkFlags;
541
542
543 /** 
544  * A talk request.
545  */
546 struct GNUNET_SOCIAL_TalkRequest;
547
548 /** 
549  * Talk to the host of the place.
550  *
551  * @param place Place where we want to talk to the host.
552  * @param method_name Method to invoke on the host.
553  * @param env Environment containing variables for the message, or NULL.
554  * @param notify Function to use to get the payload for the method.
555  * @param notify_cls Closure for @a notify.
556  * @param flags Flags for the message being sent.
557  * @return NULL if we are already trying to talk to the host,
558  *         otherwise handle to cancel the request.
559  */
560 struct GNUNET_SOCIAL_TalkRequest *
561 GNUNET_SOCIAL_place_talk (struct GNUNET_SOCIAL_Place *place,
562                           const char *method_name,
563                           const struct GNUNET_ENV_Environment *env,
564                           GNUNET_CONNECTION_TransmitReadyNotify notify,
565                           void *notify_cls,
566                           GNUNET_SOCIAL_TalkFlags flags);
567
568
569 /** 
570  * Cancel talking to the host of the place.
571  *
572  * @param tr Talk request to cancel.
573  */
574 void
575 GNUNET_SOCIAL_place_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr);
576
577
578 /** 
579  * A history lesson.
580  */
581 struct GNUNET_SOCIAL_HistoryLesson;
582
583 /** 
584  * Learn about the history of a place.
585  *
586  * Sends messages through the slicer function of the place where
587  * start_message_id <= message_id <= end_message_id.
588  * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
589  *
590  * To get the latest message, use 0 for both the start and end message ID.
591  * 
592  * @param place Place we want to learn more about.
593  * @param start_message_id First historic message we are interested in.
594  * @param end_message_id Last historic message we are interested in (inclusive).
595  * @param slicer Slicer to use to process history.  Can be the same as the
596  *               slicer of the place, as the HISTORIC flag allows distinguishing
597  *               old messages from fresh ones.
598  * @param finish_cb Function called after the last message if the history lesson
599  *              is passed through the @a slicer. NULL if not needed.
600  * @param finish_cb_cls Closure for @a finish_cb.
601  * @return Handle to abort history lesson, never NULL (multiple lessons
602  *         at the same time are allowed).
603  */
604 struct GNUNET_SOCIAL_HistoryLesson *
605 GNUNET_SOCIAL_place_get_history (struct GNUNET_SOCIAL_Place *place,
606                                  uint64_t start_message_id,
607                                  uint64_t end_message_id,
608                                  const struct GNUNET_SOCIAL_Slicer *slicer,
609                                  void (*finish_cb)(void *),
610                                  void *finish_cb_cls);
611
612
613 /** 
614  * Stop processing messages from the history lesson.
615  *
616  * Must not be called after the finish callback of the history lesson is called.
617  *
618  * @param hist History lesson to cancel.
619  */
620 void
621 GNUNET_SOCIAL_place_get_history_cancel (struct GNUNET_SOCIAL_HistoryLesson *hist);
622
623
624 /** 
625  * Leave a place permanently.
626  *
627  * Notifies the owner of the place about leaving, and destroys the place handle.
628  * 
629  * @param place Place to leave permanently.
630  */
631 void
632 GNUNET_SOCIAL_place_leave (struct GNUNET_SOCIAL_Place *place);
633
634
635 /** 
636  * Leave a place temporarily.
637  *
638  * Stop following the conversation for the @a place and destroy the @a place
639  * handle.  Only affects the application calling this function, other clients of
640  * the service continue receiving the messages.
641  *
642  * @param place Place to leave temporarily.
643  */
644 void
645 GNUNET_SOCIAL_place_away (struct GNUNET_SOCIAL_Place *place);
646
647
648 #if 0                           /* keep Emacsens' auto-indent happy */
649 {
650 #endif
651 #ifdef __cplusplus
652 }
653 #endif
654
655 /* ifndef GNUNET_SOCIAL_SERVICE_H */
656 #endif
657 /* end of gnunet_social_service.h */