2 * This file is part of GNUnet
3 * (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file social/social_api.c
23 * @brief Social service; implements social functionality using the PSYC service.
24 * @author Gabor X Toth
30 #include "gnunet_util_lib.h"
31 #include "gnunet_env_lib.h"
32 #include "gnunet_psyc_service.h"
33 #include "gnunet_social_service.h"
38 * Handle for another user (who is likely pseudonymous) in the network.
40 struct GNUNET_SOCIAL_Nym
47 * Handle for a place where social interactions happen.
49 struct GNUNET_SOCIAL_Place
56 * Handle for a place that one of our egos hosts.
58 struct GNUNET_SOCIAL_Home
65 * Handle to an implementation of try-and-slice.
67 struct GNUNET_SOCIAL_Slicer
74 * Handle for an announcement request.
76 struct GNUNET_SOCIAL_Announcement
82 struct GNUNET_SOCIAL_WatchHandle
88 struct GNUNET_SOCIAL_LookHandle
97 struct GNUNET_SOCIAL_TalkRequest
106 struct GNUNET_SOCIAL_HistoryLesson
113 * Create a try-and-slice instance.
115 * @return A new try-and-slice construct.
117 struct GNUNET_SOCIAL_Slicer *
118 GNUNET_SOCIAL_slicer_create (void)
125 * Add a method to the try-and-slice instance.
127 * A slicer processes messages and calls methods that match a message. A match
128 * happens whenever the method name of a message starts with the method_name
129 * parameter given here.
131 * @param slicer The try-and-slice instance to extend.
132 * @param method_name Name of the given method, use empty string for default.
133 * @param method Method to invoke.
134 * @param method_cls Closure for method.
137 GNUNET_SOCIAL_slicer_add (struct GNUNET_SOCIAL_Slicer *slicer,
138 const char *method_name,
139 GNUNET_SOCIAL_Method method,
147 * Remove a registered method from the try-and-slice instance.
149 * @param slicer The try-and-slice instance.
150 * @param method_name Name of the method to remove.
151 * @param method Method handler.
154 GNUNET_SOCIAL_slicer_remove (struct GNUNET_SOCIAL_Slicer *slicer,
155 const char *method_name,
156 GNUNET_SOCIAL_Method method)
163 * Destroy a given try-and-slice instance.
165 * @param slicer slicer to destroy
168 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer)
175 * Enter a home where guests (nyms) can be hosted.
177 * A home is created upon first entering, and exists until
178 * GNUNET_SOCIAL_home_destroy() is called. It can also be left temporarily using
179 * GNUNET_SOCIAL_home_leave().
181 * @param cfg Configuration to contact the social service.
182 * @param home_keyfile File with the private-public key pair of the home,
183 * created if the file does not exist; pass NULL for ephemeral homes.
184 * @param policy Policy specifying entry and history restrictions of the home.
185 * @param ego Owner of the home (host).
186 * @param slicer Slicer to handle guests talking.
187 * @param listener_cb Function to handle new nyms that want to enter.
188 * @param farewell_cb Function to handle departing nyms.
189 * @param cls Closure for @a listener_cb and @a farewell_cb.
190 * @return Handle for a new home.
192 struct GNUNET_SOCIAL_Home *
193 GNUNET_SOCIAL_home_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
194 const char *home_keyfile,
195 enum GNUNET_PSYC_Policy policy,
196 struct GNUNET_IDENTITY_Ego *ego,
197 struct GNUNET_SOCIAL_Slicer *slicer,
198 GNUNET_SOCIAL_AnswerDoorCallback listener_cb,
199 GNUNET_SOCIAL_FarewellCallback farewell_cb,
207 * Admit @a nym to the @a home.
209 * The @a nym reference will remain valid until either the home is destroyed or
212 * @param home Home to allow @a nym to enter.
213 * @param nym Handle for the entity that wants to enter.
216 GNUNET_SOCIAL_home_admit (struct GNUNET_SOCIAL_Home *home,
217 struct GNUNET_SOCIAL_Nym *nym)
224 * Throw @a nym out of the @a home.
226 * The @a nym reference will remain valid until the
227 * #GNUNET_SOCIAL_FarewellCallback is invoked,
228 * which should be very soon after this call.
230 * @param home Home to eject @a nym from.
231 * @param nym Handle for the entity to be ejected.
234 GNUNET_SOCIAL_home_eject (struct GNUNET_SOCIAL_Home *home,
235 struct GNUNET_SOCIAL_Nym *nym)
242 * Refuse @a nym entry into the @a home.
244 * @param home Home to disallow @a nym to enter.
245 * @param nym Handle for the entity that wanted to enter.
246 * @param method_name Method name for the rejection message.
247 * @param env Environment containing variables for the message, or NULL.
248 * @param data Data for the rejection message to send back.
249 * @param data_size Number of bytes in @a data for method.
252 GNUNET_SOCIAL_home_reject_entry (struct GNUNET_SOCIAL_Home *home,
253 struct GNUNET_SOCIAL_Nym *nym,
254 const char *method_name,
255 const struct GNUNET_ENV_Environment *env,
264 * Get the public key of a nym.
266 * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
268 * @param nym Pseudonym to map to a cryptographic identifier.
269 * @param[out] nym_key Set to the public key of the nym.
272 GNUNET_SOCIAL_nym_get_key (struct GNUNET_SOCIAL_Nym *nym,
273 struct GNUNET_CRYPTO_EddsaPublicKey *nym_key)
280 * Obtain the private-public key pair of the home.
282 * @param home Home to get the key of.
283 * @param[out] home_key Set to the private-public key pair of the home. The public part is suitable for storing in GNS within a "PLACE" record, along with peer IDs to join at.
286 GNUNET_SOCIAL_home_get_key (struct GNUNET_SOCIAL_Home *home,
287 struct GNUNET_CRYPTO_EddsaPrivateKey *home_key)
294 * Advertise @a home under @a name in the GNS zone of the @e ego.
296 * @param home The home to advertise.
297 * @param name The name for the PLACE record to put in the zone.
298 * @param peer_count Number of elements in the @a peers array.
299 * @param peers List of peers in the PLACE record that can be used to send join
301 * @param expiration_time Expiration time of the record, use 0 to remove the record.
302 * @param password Password used to encrypt the record.
305 GNUNET_SOCIAL_home_advertise (struct GNUNET_SOCIAL_Home *home,
308 const struct GNUNET_PeerIdentity *peers,
309 struct GNUNET_TIME_Relative expiration_time,
310 const char *password)
317 * Send a message to all nyms that are present in the home.
319 * This function is restricted to the home owner. Nyms can only send requests
320 * to the home owner who can decide to relay it to other guests.
322 * @param home Home to address the announcement to.
323 * @param method_name Method to use for the announcement.
324 * @param env Environment containing variables for the message and operations on
325 * objects of the home, or NULL.
326 * @param notify Function to call to get the payload of the announcement.
327 * @param notify_cls Closure for @a notify.
328 * @param flags Flags for this announcement.
329 * @return NULL on error (announcement already in progress?).
331 struct GNUNET_SOCIAL_Announcement *
332 GNUNET_SOCIAL_home_announce (struct GNUNET_SOCIAL_Home *home,
333 const char *method_name,
334 const struct GNUNET_ENV_Environment *env,
335 GNUNET_CONNECTION_TransmitReadyNotify notify,
337 enum GNUNET_SOCIAL_AnnounceFlags flags)
344 * Cancel announcement.
346 * @param a The announcement to cancel.
349 GNUNET_SOCIAL_home_announce_cancel (struct GNUNET_SOCIAL_Announcement *a)
356 * Convert our home to a place so we can access it via the place API.
358 * @param home Handle for the home.
359 * @return Place handle for the same home, valid as long as @a home is valid;
360 * do NOT try to GNUNET_SOCIAL_place_leave() this place, it's your home!
362 struct GNUNET_SOCIAL_Place *
363 GNUNET_SOCIAL_home_get_place (struct GNUNET_SOCIAL_Home *home)
372 * Invalidates home handle.
373 * Guests will be disconnected until the home is restarted.
375 * @param home Home to leave.
376 * @param keep_active Keep home active after last application disconnected.
379 GNUNET_SOCIAL_home_leave (struct GNUNET_SOCIAL_Home *home, int keep_active)
385 * Request entry to a place (home hosted by someone else).
387 * @param cfg Configuration to contact the social service.
388 * @param ego Owner of the home (host).
389 * @param address GNS name of the place to enter. Either in the form of
390 * 'room.friend.gnu', or 'NYMPUBKEY.zkey'. This latter case refers to
391 * the 'PLACE' record of the empty label ("+") in the GNS zone with the
392 * nym's public key 'NYMPUBKEY', and can be used to request entry to a
393 * pseudonym's place directly.
394 * @param method_name Method name for the message.
395 * @param env Environment containing variables for the message, or NULL.
396 * @param data Payload for the message to give to the enter callback.
397 * @param data_size Number of bytes in @a data.
398 * @param slicer Slicer to use for processing incoming requests from guests.
399 * @return NULL on errors, otherwise handle to the place.
401 struct GNUNET_SOCIAL_Place *
402 GNUNET_SOCIAL_place_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
403 struct GNUNET_IDENTITY_Ego *ego,
405 const char *method_name,
406 const struct GNUNET_ENV_Environment *env,
409 struct GNUNET_SOCIAL_Slicer *slicer)
415 * Request entry to a place (home hosted by someone else).
417 * @param cfg Configuration to contact the social service.
418 * @param ego Owner of the home (host).
419 * @param crypto_address Public key of the place to enter.
420 * @param origin Peer identity of the origin of the underlying multicast group.
421 * @param relay_count Number of elements in the @a relays array.
422 * @param relays Relays for the underlying multicast group.
423 * @param method_name Method name for the message.
424 * @param env Environment containing variables for the message, or NULL.
425 * @param data Payload for the message to give to the enter callback.
426 * @param data_size Number of bytes in @a data.
427 * @param slicer Slicer to use for processing incoming requests from guests.
428 * @return NULL on errors, otherwise handle to the place.
430 struct GNUNET_SOCIAL_Place *
431 GNUNET_SOCIAL_place_enter2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
432 struct GNUNET_IDENTITY_Ego *ego,
433 struct GNUNET_CRYPTO_EddsaPublicKey *crypto_address,
434 struct GNUNET_PeerIdentity *origin,
436 struct GNUNET_PeerIdentity *relays,
437 const char *method_name,
438 const struct GNUNET_ENV_Environment *env,
441 struct GNUNET_SOCIAL_Slicer *slicer)
448 * Watch a place for changed objects.
450 * @param place Place to watch.
451 * @param object_filter Object prefix to match.
452 * @param state_cb Function to call when an object/state changes.
453 * @param state_cb_cls Closure for callback.
455 * @return Handle that can be used to cancel watching.
457 struct GNUNET_SOCIAL_WatchHandle *
458 GNUNET_SOCIAL_place_watch (struct GNUNET_SOCIAL_Place *place,
459 const char *object_filter,
460 GNUNET_PSYC_StateCallback state_cb,
468 * Cancel watching a place for changed objects.
470 * @param wh Watch handle to cancel.
473 GNUNET_SOCIAL_place_watch_cancel (struct GNUNET_SOCIAL_WatchHandle *wh)
480 * Look at objects in the place with a matching name prefix.
482 * @param place The place to look its objects at.
483 * @param name_prefix Look at objects with names beginning with this value.
484 * @param state_cb Function to call for each object found.
485 * @param state_cb_cls Closure for callback function.
487 * @return Handle that can be used to stop looking at objects.
489 struct GNUNET_SOCIAL_LookHandle *
490 GNUNET_SOCIAL_place_look (struct GNUNET_SOCIAL_Place *place,
491 const char *name_prefix,
492 GNUNET_PSYC_StateCallback state_cb,
500 * Stop looking at objects.
502 * @param lh Look handle to stop.
505 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *lh)
513 * Look at a particular object in the place.
515 * The best matching object is returned (its name might be less specific than
516 * what was requested).
518 * @param place The place to look the object at.
519 * @param full_name Full name of the object.
520 * @param value_size Set to the size of the returned value.
521 * @return NULL if there is no such object at this place.
524 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *place,
525 const char *full_name,
533 * Talk to the host of the place.
535 * @param place Place where we want to talk to the host.
536 * @param method_name Method to invoke on the host.
537 * @param env Environment containing variables for the message, or NULL.
538 * @param notify Function to use to get the payload for the method.
539 * @param notify_cls Closure for @a notify.
540 * @param flags Flags for the message being sent.
541 * @return NULL if we are already trying to talk to the host,
542 * otherwise handle to cancel the request.
544 struct GNUNET_SOCIAL_TalkRequest *
545 GNUNET_SOCIAL_place_talk (struct GNUNET_SOCIAL_Place *place,
546 const char *method_name,
547 const struct GNUNET_ENV_Environment *env,
548 GNUNET_CONNECTION_TransmitReadyNotify notify,
550 enum GNUNET_SOCIAL_TalkFlags flags)
557 * Cancel talking to the host of the place.
559 * @param tr Talk request to cancel.
562 GNUNET_SOCIAL_place_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr)
569 * Learn about the history of a place.
571 * Sends messages through the slicer function of the place where
572 * start_message_id <= message_id <= end_message_id.
573 * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
575 * To get the latest message, use 0 for both the start and end message ID.
577 * @param place Place we want to learn more about.
578 * @param start_message_id First historic message we are interested in.
579 * @param end_message_id Last historic message we are interested in (inclusive).
580 * @param slicer Slicer to use to process history. Can be the same as the
581 * slicer of the place, as the HISTORIC flag allows distinguishing
582 * old messages from fresh ones.
583 * @param finish_cb Function called after the last message in the history lesson
584 * is passed through the @a slicer. NULL if not needed.
585 * @param finish_cb_cls Closure for @a finish_cb.
586 * @return Handle to abort history lesson, never NULL (multiple lessons
587 * at the same time are allowed).
589 struct GNUNET_SOCIAL_HistoryLesson *
590 GNUNET_SOCIAL_place_get_history (struct GNUNET_SOCIAL_Place *place,
591 uint64_t start_message_id,
592 uint64_t end_message_id,
593 const struct GNUNET_SOCIAL_Slicer *slicer,
594 void (*finish_cb)(void *),
602 * Stop processing messages from the history lesson.
604 * Must not be called after the finish callback of the history lesson is called.
606 * @param hist History lesson to cancel.
609 GNUNET_SOCIAL_place_get_history_cancel (struct GNUNET_SOCIAL_HistoryLesson *hist)
616 * Leave a place permanently.
618 * Notifies the owner of the place about leaving, and destroys the place handle.
620 * @param place Place to leave permanently.
621 * @param keep_active Keep place active after last application disconnected.
624 GNUNET_SOCIAL_place_leave (struct GNUNET_SOCIAL_Place *place, int keep_active)