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