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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 * ID of the task trying to reconnect to the service.
81 struct GNUNET_SCHEDULER_Task *reconnect_task;
84 * Delay until we try to reconnect.
86 struct GNUNET_TIME_Relative reconnect_delay;
89 * Are we in the process of disconnecting but need to sync first?
96 * Context for a store request
98 struct GNUNET_PEERSTORE_StoreContext
103 struct GNUNET_PEERSTORE_StoreContext *next;
108 struct GNUNET_PEERSTORE_StoreContext *prev;
111 * Handle to the PEERSTORE service.
113 struct GNUNET_PEERSTORE_Handle *h;
116 * Continuation called with service response
118 GNUNET_PEERSTORE_Continuation cont;
121 * Closure for @e cont
126 * Which subsystem does the store?
131 * Key for the store operation.
136 * Contains @e size bytes.
141 * Peer the store is for.
143 struct GNUNET_PeerIdentity peer;
146 * Number of bytes in @e value.
151 * When does the value expire?
153 struct GNUNET_TIME_Absolute expiry;
156 * Options for the store operation.
158 enum GNUNET_PEERSTORE_StoreOption options;
163 * Context for a iterate request
165 struct GNUNET_PEERSTORE_IterateContext
170 struct GNUNET_PEERSTORE_IterateContext *next;
175 struct GNUNET_PEERSTORE_IterateContext *prev;
178 * Handle to the PEERSTORE service.
180 struct GNUNET_PEERSTORE_Handle *h;
183 * Which subsystem does the store?
188 * Peer the store is for.
190 struct GNUNET_PeerIdentity peer;
193 * Key for the store operation.
198 * Callback with each matching record
200 GNUNET_PEERSTORE_Processor callback;
203 * Closure for @e callback
208 * #GNUNET_YES if we are currently processing records.
215 * Context for a watch request
217 struct GNUNET_PEERSTORE_WatchContext
222 struct GNUNET_PEERSTORE_WatchContext *next;
227 struct GNUNET_PEERSTORE_WatchContext *prev;
230 * Handle to the PEERSTORE service.
232 struct GNUNET_PEERSTORE_Handle *h;
235 * Callback with each record received
237 GNUNET_PEERSTORE_Processor callback;
240 * Closure for @e callback
245 * Hash of the combined key
247 struct GNUNET_HashCode keyhash;
251 /******************************************************************************/
252 /******************* DECLARATIONS *********************/
253 /******************************************************************************/
256 * Close the existing connection to PEERSTORE and reconnect.
258 * @param cls a `struct GNUNET_PEERSTORE_Handle *h`
261 reconnect (void *cls);
265 * Disconnect from the peerstore service.
267 * @param h peerstore handle to disconnect
270 disconnect (struct GNUNET_PEERSTORE_Handle *h)
272 struct GNUNET_PEERSTORE_IterateContext *next;
274 for (struct GNUNET_PEERSTORE_IterateContext *ic = h->iterate_head;
279 if (GNUNET_YES == ic->iterating)
281 GNUNET_PEERSTORE_Processor icb;
285 icb_cls = ic->callback_cls;
286 GNUNET_PEERSTORE_iterate_cancel (ic);
290 "Iteration canceled due to reconnection");
296 GNUNET_MQ_destroy (h->mq);
303 * Function that will schedule the job that will try
304 * to connect us again to the client.
306 * @param h peerstore to reconnect
309 disconnect_and_schedule_reconnect (struct GNUNET_PEERSTORE_Handle *h)
311 GNUNET_assert (NULL == h->reconnect_task);
313 LOG (GNUNET_ERROR_TYPE_DEBUG,
314 "Scheduling task to reconnect to PEERSTORE service in %s.\n",
315 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
318 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
321 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
327 * Callback after MQ envelope is sent
329 * @param cls a `struct GNUNET_PEERSTORE_StoreContext *`
332 store_request_sent (void *cls)
334 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
335 GNUNET_PEERSTORE_Continuation cont;
339 cont_cls = sc->cont_cls;
340 GNUNET_PEERSTORE_store_cancel (sc);
342 cont (cont_cls, GNUNET_OK);
346 /******************************************************************************/
347 /******************* CONNECTION FUNCTIONS *********************/
348 /******************************************************************************/
352 * Function called when we had trouble talking to the service.
355 handle_client_error (void *cls,
356 enum GNUNET_MQ_Error error)
358 struct GNUNET_PEERSTORE_Handle *h = cls;
360 LOG (GNUNET_ERROR_TYPE_ERROR,
361 "Received an error notification from MQ of type: %d\n",
363 disconnect_and_schedule_reconnect (h);
368 * Iterator over previous watches to resend them
370 * @param cls the `struct GNUNET_PEERSTORE_Handle`
371 * @param key key for the watch
372 * @param value the `struct GNUNET_PEERSTORE_WatchContext *`
373 * @return #GNUNET_YES (continue to iterate)
376 rewatch_it (void *cls,
377 const struct GNUNET_HashCode *key,
380 struct GNUNET_PEERSTORE_Handle *h = cls;
381 struct GNUNET_PEERSTORE_WatchContext *wc = value;
382 struct StoreKeyHashMessage *hm;
383 struct GNUNET_MQ_Envelope *ev;
385 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
386 hm->keyhash = wc->keyhash;
387 GNUNET_MQ_send (h->mq, ev);
393 * Iterator over watch requests to cancel them.
396 * @param key key to the watch request
397 * @param value watch context
398 * @return #GNUNET_YES to continue iteration
401 destroy_watch (void *cls,
402 const struct GNUNET_HashCode *key,
405 struct GNUNET_PEERSTORE_WatchContext *wc = value;
407 GNUNET_PEERSTORE_watch_cancel (wc);
413 * Kill the connection to the service. This can be delayed in case of pending
414 * STORE requests and the user explicitly asked to sync first. Otherwise it is
415 * performed instantly.
417 * @param h Handle to the service.
420 final_disconnect (struct GNUNET_PEERSTORE_Handle *h)
424 GNUNET_MQ_destroy (h->mq);
432 * Connect to the PEERSTORE service.
434 * @param cfg configuration to use
435 * @return NULL on error
437 struct GNUNET_PEERSTORE_Handle *
438 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
440 struct GNUNET_PEERSTORE_Handle *h;
442 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
444 h->disconnecting = GNUNET_NO;
456 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
458 * Any pending STORE requests will depend on @e snyc_first flag.
460 * @param h handle to disconnect
461 * @param sync_first send any pending STORE requests before disconnecting
464 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h,
467 struct GNUNET_PEERSTORE_IterateContext *ic;
468 struct GNUNET_PEERSTORE_StoreContext *sc;
470 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
471 if (NULL != h->watches)
473 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
474 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
477 while (NULL != (ic = h->iterate_head))
480 GNUNET_PEERSTORE_iterate_cancel (ic);
482 if (NULL != h->store_head)
484 if (GNUNET_YES == sync_first)
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Delaying disconnection due to pending store requests.\n");
488 h->disconnecting = GNUNET_YES;
491 while (NULL != (sc = h->store_head))
492 GNUNET_PEERSTORE_store_cancel (sc);
494 final_disconnect (h);
498 /******************************************************************************/
499 /******************* STORE FUNCTIONS *********************/
500 /******************************************************************************/
504 * Cancel a store request
506 * @param sc Store request context
509 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
511 struct GNUNET_PEERSTORE_Handle *h = sc->h;
513 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
514 GNUNET_free (sc->sub_system);
515 GNUNET_free (sc->value);
516 GNUNET_free (sc->key);
518 if ( (GNUNET_YES == h->disconnecting) &&
519 (NULL == h->store_head) )
520 final_disconnect (h);
525 * Store a new entry in the PEERSTORE.
526 * Note that stored entries can be lost in some cases
527 * such as power failure.
529 * @param h Handle to the PEERSTORE service
530 * @param sub_system name of the sub system
531 * @param peer Peer Identity
532 * @param key entry key
533 * @param value entry value BLOB
534 * @param size size of @e value
535 * @param expiry absolute time after which the entry is (possibly) deleted
536 * @param options options specific to the storage operation
537 * @param cont Continuation function after the store request is sent
538 * @param cont_cls Closure for @a cont
540 struct GNUNET_PEERSTORE_StoreContext *
541 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
542 const char *sub_system,
543 const struct GNUNET_PeerIdentity *peer,
545 const void *value, size_t size,
546 struct GNUNET_TIME_Absolute expiry,
547 enum GNUNET_PEERSTORE_StoreOption options,
548 GNUNET_PEERSTORE_Continuation cont,
551 struct GNUNET_MQ_Envelope *ev;
552 struct GNUNET_PEERSTORE_StoreContext *sc;
554 LOG (GNUNET_ERROR_TYPE_DEBUG,
555 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
556 size, sub_system, GNUNET_i2s (peer), key);
557 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
559 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
560 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
562 sc->sub_system = GNUNET_strdup (sub_system);
564 sc->key = GNUNET_strdup (key);
565 sc->value = GNUNET_memdup (value, size);
568 sc->options = options;
570 sc->cont_cls = cont_cls;
573 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
574 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
575 GNUNET_MQ_send (h->mq, ev);
581 /******************************************************************************/
582 /******************* ITERATE FUNCTIONS *********************/
583 /******************************************************************************/
587 * When a response for iterate request is received
589 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
590 * @param msg message received
593 handle_iterate_end (void *cls,
594 const struct GNUNET_MessageHeader *msg)
596 struct GNUNET_PEERSTORE_Handle *h = cls;
597 struct GNUNET_PEERSTORE_IterateContext *ic;
598 GNUNET_PEERSTORE_Processor callback;
601 ic = h->iterate_head;
604 LOG (GNUNET_ERROR_TYPE_ERROR,
605 _("Unexpected iteration response, this should not happen.\n"));
606 disconnect_and_schedule_reconnect (h);
609 callback = ic->callback;
610 callback_cls = ic->callback_cls;
611 ic->iterating = GNUNET_NO;
612 GNUNET_PEERSTORE_iterate_cancel (ic);
613 if (NULL != callback)
614 callback (callback_cls,
617 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
622 * When a response for iterate request is received, check the
623 * message is well-formed.
625 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
626 * @param msg message received
629 check_iterate_result (void *cls,
630 const struct StoreRecordMessage *msg)
632 /* we defer validation to #handle_iterate_result */
638 * When a response for iterate request is received
640 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
641 * @param msg message received
644 handle_iterate_result (void *cls,
645 const struct StoreRecordMessage *msg)
647 struct GNUNET_PEERSTORE_Handle *h = cls;
648 struct GNUNET_PEERSTORE_IterateContext *ic;
649 GNUNET_PEERSTORE_Processor callback;
651 struct GNUNET_PEERSTORE_Record *record;
653 ic = h->iterate_head;
656 LOG (GNUNET_ERROR_TYPE_ERROR,
657 _("Unexpected iteration response, this should not happen.\n"));
658 disconnect_and_schedule_reconnect (h);
661 ic->iterating = GNUNET_YES;
662 callback = ic->callback;
663 callback_cls = ic->callback_cls;
664 if (NULL == callback)
666 record = PEERSTORE_parse_record_message (msg);
669 callback (callback_cls,
671 _("Received a malformed response from service."));
675 callback (callback_cls,
678 PEERSTORE_destroy_record (record);
684 * Cancel an iterate request
685 * Please do not call after the iterate request is done
687 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
690 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
692 if (GNUNET_NO == ic->iterating)
694 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head,
697 GNUNET_free (ic->sub_system);
698 GNUNET_free_non_null (ic->key);
707 * Iterate over records matching supplied key information
709 * @param h handle to the PEERSTORE service
710 * @param sub_system name of sub system
711 * @param peer Peer identity (can be NULL)
712 * @param key entry key string (can be NULL)
713 * @param callback function called with each matching record, all NULL's on end
714 * @param callback_cls closure for @a callback
715 * @return Handle to iteration request
717 struct GNUNET_PEERSTORE_IterateContext *
718 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
719 const char *sub_system,
720 const struct GNUNET_PeerIdentity *peer,
722 GNUNET_PEERSTORE_Processor callback,
725 struct GNUNET_MQ_Envelope *ev;
726 struct GNUNET_PEERSTORE_IterateContext *ic;
728 ev = PEERSTORE_create_record_mq_envelope (sub_system,
732 GNUNET_TIME_UNIT_FOREVER_ABS,
734 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
735 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
736 ic->callback = callback;
737 ic->callback_cls = callback_cls;
739 ic->sub_system = GNUNET_strdup (sub_system);
743 ic->key = GNUNET_strdup (key);
744 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head,
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Sending an iterate request for sub system `%s'\n",
750 GNUNET_MQ_send (h->mq, ev);
755 /******************************************************************************/
756 /******************* WATCH FUNCTIONS *********************/
757 /******************************************************************************/
760 * When a watch record is received, validate it is well-formed.
762 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
763 * @param msg message received
766 check_watch_record (void *cls,
767 const struct StoreRecordMessage *msg)
769 /* we defer validation to #handle_watch_result */
775 * When a watch record is received, process it.
777 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
778 * @param msg message received
781 handle_watch_record (void *cls,
782 const struct StoreRecordMessage *msg)
784 struct GNUNET_PEERSTORE_Handle *h = cls;
785 struct GNUNET_PEERSTORE_Record *record;
786 struct GNUNET_HashCode keyhash;
787 struct GNUNET_PEERSTORE_WatchContext *wc;
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Received a watch record from service.\n");
791 record = PEERSTORE_parse_record_message (msg);
794 disconnect_and_schedule_reconnect (h);
797 PEERSTORE_hash_key (record->sub_system,
801 // FIXME: what if there are multiple watches for the same key?
802 wc = GNUNET_CONTAINER_multihashmap_get (h->watches,
806 LOG (GNUNET_ERROR_TYPE_ERROR,
807 _("Received a watch result for a non existing watch.\n"));
808 PEERSTORE_destroy_record (record);
809 disconnect_and_schedule_reconnect (h);
812 if (NULL != wc->callback)
813 wc->callback (wc->callback_cls,
816 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
817 PEERSTORE_destroy_record (record);
822 * Close the existing connection to PEERSTORE and reconnect.
824 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
827 reconnect (void *cls)
829 struct GNUNET_PEERSTORE_Handle *h = cls;
830 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
831 GNUNET_MQ_hd_fixed_size (iterate_end,
832 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
833 struct GNUNET_MessageHeader,
835 GNUNET_MQ_hd_var_size (iterate_result,
836 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD,
837 struct StoreRecordMessage,
839 GNUNET_MQ_hd_var_size (watch_record,
840 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD,
841 struct StoreRecordMessage,
843 GNUNET_MQ_handler_end ()
845 struct GNUNET_MQ_Envelope *ev;
847 LOG (GNUNET_ERROR_TYPE_DEBUG,
848 "Reconnecting...\n");
849 h->mq = GNUNET_CLIENT_connect (h->cfg,
852 &handle_client_error,
856 LOG (GNUNET_ERROR_TYPE_DEBUG,
857 "Resending pending requests after reconnect.\n");
858 if (NULL != h->watches)
859 GNUNET_CONTAINER_multihashmap_iterate (h->watches,
862 for (struct GNUNET_PEERSTORE_IterateContext *ic = h->iterate_head;
866 ev = PEERSTORE_create_record_mq_envelope (ic->sub_system,
870 GNUNET_TIME_UNIT_FOREVER_ABS,
872 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
873 GNUNET_MQ_send (h->mq, ev);
875 for (struct GNUNET_PEERSTORE_StoreContext *sc = h->store_head;
879 ev = PEERSTORE_create_record_mq_envelope (sc->sub_system,
886 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
887 GNUNET_MQ_notify_sent (ev,
890 GNUNET_MQ_send (h->mq,
897 * Cancel a watch request
899 * @param wc handle to the watch request
902 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
904 struct GNUNET_PEERSTORE_Handle *h = wc->h;
905 struct GNUNET_MQ_Envelope *ev;
906 struct StoreKeyHashMessage *hm;
908 LOG (GNUNET_ERROR_TYPE_DEBUG,
909 "Canceling watch.\n");
910 ev = GNUNET_MQ_msg (hm,
911 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
912 hm->keyhash = wc->keyhash;
913 GNUNET_MQ_send (h->mq, ev);
914 GNUNET_CONTAINER_multihashmap_remove (h->watches,
922 * Request watching a given key
923 * User will be notified with any new values added to key
925 * @param h handle to the PEERSTORE service
926 * @param sub_system name of sub system
927 * @param peer Peer identity
928 * @param key entry key string
929 * @param callback function called with each new value
930 * @param callback_cls closure for @a callback
931 * @return Handle to watch request
933 struct GNUNET_PEERSTORE_WatchContext *
934 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
935 const char *sub_system,
936 const struct GNUNET_PeerIdentity *peer,
938 GNUNET_PEERSTORE_Processor callback,
941 struct GNUNET_MQ_Envelope *ev;
942 struct StoreKeyHashMessage *hm;
943 struct GNUNET_PEERSTORE_WatchContext *wc;
945 ev = GNUNET_MQ_msg (hm,
946 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
947 PEERSTORE_hash_key (sub_system,
951 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
952 wc->callback = callback;
953 wc->callback_cls = callback_cls;
955 wc->keyhash = hm->keyhash;
956 if (NULL == h->watches)
957 h->watches = GNUNET_CONTAINER_multihashmap_create (5,
959 GNUNET_assert (GNUNET_OK ==
960 GNUNET_CONTAINER_multihashmap_put (h->watches,
963 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
964 LOG (GNUNET_ERROR_TYPE_DEBUG,
965 "Sending a watch request for subsystem `%s', peer `%s', key `%s'.\n",
969 GNUNET_MQ_send (h->mq,
974 /* end of peerstore_api.c */