2 This file is part of GNUnet.
3 Copyright (C) 2013-2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file peerstore/peerstore_api.c
22 * @brief API for peerstore
23 * @author Omar Tarabai
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
29 #include "peerstore_common.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "peerstore-api",__VA_ARGS__)
33 /******************************************************************************/
34 /************************ DATA STRUCTURES ****************************/
35 /******************************************************************************/
38 * Handle to the PEERSTORE service.
40 struct GNUNET_PEERSTORE_Handle
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
51 struct GNUNET_MQ_Handle *mq;
54 * Head of active STORE requests.
56 struct GNUNET_PEERSTORE_StoreContext *store_head;
59 * Tail of active STORE requests.
61 struct GNUNET_PEERSTORE_StoreContext *store_tail;
64 * Head of active ITERATE requests.
66 struct GNUNET_PEERSTORE_IterateContext *iterate_head;
69 * Tail of active ITERATE requests.
71 struct GNUNET_PEERSTORE_IterateContext *iterate_tail;
74 * Hashmap of watch requests
76 struct GNUNET_CONTAINER_MultiHashMap *watches;
79 * Are we in the process of disconnecting but need to sync first?
86 * Context for a store request
88 struct GNUNET_PEERSTORE_StoreContext
93 struct GNUNET_PEERSTORE_StoreContext *next;
98 struct GNUNET_PEERSTORE_StoreContext *prev;
101 * Handle to the PEERSTORE service.
103 struct GNUNET_PEERSTORE_Handle *h;
106 * Continuation called with service response
108 GNUNET_PEERSTORE_Continuation cont;
111 * Closure for @e cont
116 * Which subsystem does the store?
121 * Key for the store operation.
126 * Contains @e size bytes.
131 * Peer the store is for.
133 struct GNUNET_PeerIdentity peer;
136 * Number of bytes in @e value.
141 * When does the value expire?
143 struct GNUNET_TIME_Absolute expiry;
146 * Options for the store operation.
148 enum GNUNET_PEERSTORE_StoreOption options;
153 * Context for a iterate request
155 struct GNUNET_PEERSTORE_IterateContext
160 struct GNUNET_PEERSTORE_IterateContext *next;
165 struct GNUNET_PEERSTORE_IterateContext *prev;
168 * Handle to the PEERSTORE service.
170 struct GNUNET_PEERSTORE_Handle *h;
173 * Which subsystem does the store?
178 * Peer the store is for.
180 struct GNUNET_PeerIdentity peer;
183 * Key for the store operation.
190 struct GNUNET_TIME_Relative timeout;
193 * Callback with each matching record
195 GNUNET_PEERSTORE_Processor callback;
198 * Closure for @e callback
203 * #GNUNET_YES if we are currently processing records.
208 * Task identifier for the function called
209 * on iterate request timeout
211 struct GNUNET_SCHEDULER_Task *timeout_task;
216 * Context for a watch request
218 struct GNUNET_PEERSTORE_WatchContext
223 struct GNUNET_PEERSTORE_WatchContext *next;
228 struct GNUNET_PEERSTORE_WatchContext *prev;
231 * Handle to the PEERSTORE service.
233 struct GNUNET_PEERSTORE_Handle *h;
236 * Callback with each record received
238 GNUNET_PEERSTORE_Processor callback;
241 * Closure for @e callback
246 * Hash of the combined key
248 struct GNUNET_HashCode keyhash;
252 /******************************************************************************/
253 /******************* DECLARATIONS *********************/
254 /******************************************************************************/
257 * Close the existing connection to PEERSTORE and reconnect.
259 * @param h handle to the service
262 reconnect (struct GNUNET_PEERSTORE_Handle *h);
266 * Callback after MQ envelope is sent
268 * @param cls a `struct GNUNET_PEERSTORE_StoreContext *`
271 store_request_sent (void *cls)
273 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
274 GNUNET_PEERSTORE_Continuation cont;
278 cont_cls = sc->cont_cls;
279 GNUNET_PEERSTORE_store_cancel (sc);
281 cont (cont_cls, GNUNET_OK);
285 /******************************************************************************/
286 /******************* CONNECTION FUNCTIONS *********************/
287 /******************************************************************************/
290 handle_client_error (void *cls,
291 enum GNUNET_MQ_Error error)
293 struct GNUNET_PEERSTORE_Handle *h = cls;
295 LOG (GNUNET_ERROR_TYPE_ERROR,
296 _("Received an error notification from MQ of type: %d\n"),
303 * Iterator over previous watches to resend them
305 * @param cls the `struct GNUNET_PEERSTORE_Handle`
306 * @param key key for the watch
307 * @param value the `struct GNUNET_PEERSTORE_WatchContext *`
308 * @return #GNUNET_YES (continue to iterate)
311 rewatch_it (void *cls,
312 const struct GNUNET_HashCode *key,
315 struct GNUNET_PEERSTORE_Handle *h = cls;
316 struct GNUNET_PEERSTORE_WatchContext *wc = value;
317 struct StoreKeyHashMessage *hm;
318 struct GNUNET_MQ_Envelope *ev;
320 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
321 hm->keyhash = wc->keyhash;
322 GNUNET_MQ_send (h->mq, ev);
328 * Called when the iterate request is timedout
330 * @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
333 iterate_timeout (void *cls)
335 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
336 GNUNET_PEERSTORE_Processor callback;
339 ic->timeout_task = NULL;
340 callback = ic->callback;
341 callback_cls = ic->callback_cls;
342 GNUNET_PEERSTORE_iterate_cancel (ic);
343 if (NULL != callback)
344 callback (callback_cls, NULL, _("timeout"));
349 * Iterator over watch requests to cancel them.
352 * @param key key to the watch request
353 * @param value watch context
354 * @return #GNUNET_YES to continue iteration
357 destroy_watch (void *cls,
358 const struct GNUNET_HashCode *key,
361 struct GNUNET_PEERSTORE_WatchContext *wc = value;
363 GNUNET_PEERSTORE_watch_cancel (wc);
369 * Kill the connection to the service. This can be delayed in case of pending
370 * STORE requests and the user explicitly asked to sync first. Otherwise it is
371 * performed instantly.
373 * @param h Handle to the service.
376 do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
380 GNUNET_MQ_destroy (h->mq);
388 * Connect to the PEERSTORE service.
390 * @param cfg configuration to use
391 * @return NULL on error
393 struct GNUNET_PEERSTORE_Handle *
394 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
396 struct GNUNET_PEERSTORE_Handle *h;
398 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
400 h->disconnecting = GNUNET_NO;
412 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
414 * Any pending STORE requests will depend on @e snyc_first flag.
416 * @param h handle to disconnect
417 * @param sync_first send any pending STORE requests before disconnecting
420 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h,
423 struct GNUNET_PEERSTORE_IterateContext *ic;
424 struct GNUNET_PEERSTORE_StoreContext *sc;
426 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
427 if (NULL != h->watches)
429 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
430 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
433 while (NULL != (ic = h->iterate_head))
436 GNUNET_PEERSTORE_iterate_cancel (ic);
438 if (NULL != h->store_head)
440 if (GNUNET_YES == sync_first)
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "Delaying disconnection due to pending store requests.\n");
444 h->disconnecting = GNUNET_YES;
447 while (NULL != (sc = h->store_head))
448 GNUNET_PEERSTORE_store_cancel (sc);
454 /******************************************************************************/
455 /******************* STORE FUNCTIONS *********************/
456 /******************************************************************************/
460 * Cancel a store request
462 * @param sc Store request context
465 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
467 struct GNUNET_PEERSTORE_Handle *h = sc->h;
469 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
470 GNUNET_free (sc->sub_system);
471 GNUNET_free (sc->value);
472 GNUNET_free (sc->key);
474 if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head))
480 * Store a new entry in the PEERSTORE.
481 * Note that stored entries can be lost in some cases
482 * such as power failure.
484 * @param h Handle to the PEERSTORE service
485 * @param sub_system name of the sub system
486 * @param peer Peer Identity
487 * @param key entry key
488 * @param value entry value BLOB
489 * @param size size of @e value
490 * @param expiry absolute time after which the entry is (possibly) deleted
491 * @param options options specific to the storage operation
492 * @param cont Continuation function after the store request is sent
493 * @param cont_cls Closure for @a cont
495 struct GNUNET_PEERSTORE_StoreContext *
496 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
497 const char *sub_system,
498 const struct GNUNET_PeerIdentity *peer,
500 const void *value, size_t size,
501 struct GNUNET_TIME_Absolute expiry,
502 enum GNUNET_PEERSTORE_StoreOption options,
503 GNUNET_PEERSTORE_Continuation cont,
506 struct GNUNET_MQ_Envelope *ev;
507 struct GNUNET_PEERSTORE_StoreContext *sc;
509 LOG (GNUNET_ERROR_TYPE_DEBUG,
510 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
511 size, sub_system, GNUNET_i2s (peer), key);
512 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
514 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
515 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
517 sc->sub_system = GNUNET_strdup (sub_system);
519 sc->key = GNUNET_strdup (key);
520 sc->value = GNUNET_memdup (value, size);
523 sc->options = options;
525 sc->cont_cls = cont_cls;
528 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
529 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
530 GNUNET_MQ_send (h->mq, ev);
536 /******************************************************************************/
537 /******************* ITERATE FUNCTIONS *********************/
538 /******************************************************************************/
542 * When a response for iterate request is received
544 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
545 * @param msg message received
548 handle_iterate_end (void *cls,
549 const struct GNUNET_MessageHeader *msg)
551 struct GNUNET_PEERSTORE_Handle *h = cls;
552 struct GNUNET_PEERSTORE_IterateContext *ic;
553 GNUNET_PEERSTORE_Processor callback;
556 ic = h->iterate_head;
559 LOG (GNUNET_ERROR_TYPE_ERROR,
560 _("Unexpected iteration response, this should not happen.\n"));
564 callback = ic->callback;
565 callback_cls = ic->callback_cls;
566 ic->iterating = GNUNET_NO;
567 GNUNET_PEERSTORE_iterate_cancel (ic);
568 if (NULL != callback)
569 callback (callback_cls, NULL, NULL);
574 * When a response for iterate request is received, check the
575 * message is well-formed.
577 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
578 * @param msg message received
581 check_iterate_result (void *cls,
582 const struct GNUNET_MessageHeader *msg)
584 /* we defer validation to #handle_iterate_result */
590 * When a response for iterate request is received
592 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
593 * @param msg message received
596 handle_iterate_result (void *cls,
597 const struct GNUNET_MessageHeader *msg)
599 struct GNUNET_PEERSTORE_Handle *h = cls;
600 struct GNUNET_PEERSTORE_IterateContext *ic;
601 GNUNET_PEERSTORE_Processor callback;
603 struct GNUNET_PEERSTORE_Record *record;
605 ic = h->iterate_head;
608 LOG (GNUNET_ERROR_TYPE_ERROR,
609 _("Unexpected iteration response, this should not happen.\n"));
613 ic->iterating = GNUNET_YES;
614 callback = ic->callback;
615 callback_cls = ic->callback_cls;
616 if (NULL == callback)
618 record = PEERSTORE_parse_record_message (msg);
621 callback (callback_cls,
623 _("Received a malformed response from service."));
627 callback (callback_cls,
630 PEERSTORE_destroy_record (record);
636 * Cancel an iterate request
637 * Please do not call after the iterate request is done
639 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
642 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
644 if (NULL != ic->timeout_task)
646 GNUNET_SCHEDULER_cancel (ic->timeout_task);
647 ic->timeout_task = NULL;
649 if (GNUNET_NO == ic->iterating)
651 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head,
654 GNUNET_free (ic->sub_system);
655 GNUNET_free_non_null (ic->key);
664 * Iterate over records matching supplied key information
666 * @param h handle to the PEERSTORE service
667 * @param sub_system name of sub system
668 * @param peer Peer identity (can be NULL)
669 * @param key entry key string (can be NULL)
670 * @param timeout time after which the iterate request is canceled
671 * @param callback function called with each matching record, all NULL's on end
672 * @param callback_cls closure for @a callback
673 * @return Handle to iteration request
675 struct GNUNET_PEERSTORE_IterateContext *
676 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
677 const char *sub_system,
678 const struct GNUNET_PeerIdentity *peer,
680 struct GNUNET_TIME_Relative timeout,
681 GNUNET_PEERSTORE_Processor callback,
684 struct GNUNET_MQ_Envelope *ev;
685 struct GNUNET_PEERSTORE_IterateContext *ic;
687 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0,
689 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
690 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
692 ic->callback = callback;
693 ic->callback_cls = callback_cls;
695 ic->sub_system = GNUNET_strdup (sub_system);
699 ic->key = GNUNET_strdup (key);
700 ic->timeout = timeout;
701 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head,
704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "Sending an iterate request for sub system `%s'\n",
707 GNUNET_MQ_send (h->mq, ev);
709 GNUNET_SCHEDULER_add_delayed (timeout,
716 /******************************************************************************/
717 /******************* WATCH FUNCTIONS *********************/
718 /******************************************************************************/
721 * When a watch record is received, validate it is well-formed.
723 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
724 * @param msg message received
727 check_watch_record (void *cls,
728 const struct GNUNET_MessageHeader *msg)
730 /* we defer validation to #handle_watch_result */
736 * When a watch record is received, process it.
738 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
739 * @param msg message received
742 handle_watch_record (void *cls,
743 const struct GNUNET_MessageHeader *msg)
745 struct GNUNET_PEERSTORE_Handle *h = cls;
746 struct GNUNET_PEERSTORE_Record *record;
747 struct GNUNET_HashCode keyhash;
748 struct GNUNET_PEERSTORE_WatchContext *wc;
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 "Received a watch record from service.\n");
752 record = PEERSTORE_parse_record_message (msg);
758 PEERSTORE_hash_key (record->sub_system,
762 // FIXME: what if there are multiple watches for the same key?
763 wc = GNUNET_CONTAINER_multihashmap_get (h->watches,
767 LOG (GNUNET_ERROR_TYPE_ERROR,
768 _("Received a watch result for a non existing watch.\n"));
769 PEERSTORE_destroy_record (record);
773 if (NULL != wc->callback)
774 wc->callback (wc->callback_cls,
777 PEERSTORE_destroy_record (record);
782 * Close the existing connection to PEERSTORE and reconnect.
784 * @param h handle to the service
787 reconnect (struct GNUNET_PEERSTORE_Handle *h)
789 GNUNET_MQ_hd_fixed_size (iterate_end,
790 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
791 struct GNUNET_MessageHeader);
792 GNUNET_MQ_hd_var_size (iterate_result,
793 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD,
794 struct GNUNET_MessageHeader);
795 GNUNET_MQ_hd_var_size (watch_record,
796 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD,
797 struct GNUNET_MessageHeader);
798 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
799 make_iterate_end_handler (h),
800 make_iterate_result_handler (h),
801 make_watch_record_handler (h),
802 GNUNET_MQ_handler_end ()
804 struct GNUNET_PEERSTORE_IterateContext *ic;
805 struct GNUNET_PEERSTORE_IterateContext *next;
806 GNUNET_PEERSTORE_Processor icb;
808 struct GNUNET_PEERSTORE_StoreContext *sc;
809 struct GNUNET_MQ_Envelope *ev;
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
812 "Reconnecting...\n");
813 for (ic = h->iterate_head; NULL != ic; ic = next)
816 if (GNUNET_YES == ic->iterating)
819 icb_cls = ic->callback_cls;
820 GNUNET_PEERSTORE_iterate_cancel (ic);
824 "Iteration canceled due to reconnection");
829 GNUNET_MQ_destroy (h->mq);
832 h->mq = GNUNET_CLIENT_connecT (h->cfg,
835 &handle_client_error,
839 LOG (GNUNET_ERROR_TYPE_DEBUG,
840 "Resending pending requests after reconnect.\n");
841 if (NULL != h->watches)
842 GNUNET_CONTAINER_multihashmap_iterate (h->watches,
845 for (ic = h->iterate_head; NULL != ic; ic = ic->next)
847 ev = PEERSTORE_create_record_mq_envelope (ic->sub_system,
852 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
853 GNUNET_MQ_send (h->mq, ev);
855 = GNUNET_SCHEDULER_add_delayed (ic->timeout,
859 for (sc = h->store_head; NULL != sc; sc = sc->next)
861 ev = PEERSTORE_create_record_mq_envelope (sc->sub_system,
868 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
869 GNUNET_MQ_notify_sent (ev,
872 GNUNET_MQ_send (h->mq,
879 * Cancel a watch request
881 * @param wc handle to the watch request
884 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
886 struct GNUNET_PEERSTORE_Handle *h = wc->h;
887 struct GNUNET_MQ_Envelope *ev;
888 struct StoreKeyHashMessage *hm;
890 LOG (GNUNET_ERROR_TYPE_DEBUG,
891 "Canceling watch.\n");
892 ev = GNUNET_MQ_msg (hm,
893 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
894 hm->keyhash = wc->keyhash;
895 GNUNET_MQ_send (h->mq, ev);
896 GNUNET_CONTAINER_multihashmap_remove (h->watches,
904 * Request watching a given key
905 * User will be notified with any new values added to key
907 * @param h handle to the PEERSTORE service
908 * @param sub_system name of sub system
909 * @param peer Peer identity
910 * @param key entry key string
911 * @param callback function called with each new value
912 * @param callback_cls closure for @a callback
913 * @return Handle to watch request
915 struct GNUNET_PEERSTORE_WatchContext *
916 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
917 const char *sub_system,
918 const struct GNUNET_PeerIdentity *peer,
920 GNUNET_PEERSTORE_Processor callback,
923 struct GNUNET_MQ_Envelope *ev;
924 struct StoreKeyHashMessage *hm;
925 struct GNUNET_PEERSTORE_WatchContext *wc;
927 ev = GNUNET_MQ_msg (hm,
928 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
929 PEERSTORE_hash_key (sub_system,
933 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
934 wc->callback = callback;
935 wc->callback_cls = callback_cls;
937 wc->keyhash = hm->keyhash;
938 if (NULL == h->watches)
939 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
940 GNUNET_assert (GNUNET_OK ==
941 GNUNET_CONTAINER_multihashmap_put (h->watches,
944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
950 GNUNET_MQ_send (h->mq, ev);
954 /* end of peerstore_api.c */