2 This file is part of GNUnet.
3 Copyright (C) 2013-2016, 2019 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
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
50 struct GNUNET_MQ_Handle *mq;
53 * Head of active STORE requests.
55 struct GNUNET_PEERSTORE_StoreContext *store_head;
58 * Tail of active STORE requests.
60 struct GNUNET_PEERSTORE_StoreContext *store_tail;
63 * Head of active ITERATE requests.
65 struct GNUNET_PEERSTORE_IterateContext *iterate_head;
68 * Tail of active ITERATE requests.
70 struct GNUNET_PEERSTORE_IterateContext *iterate_tail;
73 * Hashmap of watch requests
75 struct GNUNET_CONTAINER_MultiHashMap *watches;
78 * ID of the task trying to reconnect to the service.
80 struct GNUNET_SCHEDULER_Task *reconnect_task;
83 * Delay until we try to reconnect.
85 struct GNUNET_TIME_Relative reconnect_delay;
88 * Are we in the process of disconnecting but need to sync first?
94 * Context for a store request
96 struct GNUNET_PEERSTORE_StoreContext
101 struct GNUNET_PEERSTORE_StoreContext *next;
106 struct GNUNET_PEERSTORE_StoreContext *prev;
109 * Handle to the PEERSTORE service.
111 struct GNUNET_PEERSTORE_Handle *h;
114 * Continuation called with service response
116 GNUNET_PEERSTORE_Continuation cont;
119 * Closure for @e cont
124 * Which subsystem does the store?
129 * Key for the store operation.
134 * Contains @e size bytes.
139 * Peer the store is for.
141 struct GNUNET_PeerIdentity peer;
144 * Number of bytes in @e value.
149 * When does the value expire?
151 struct GNUNET_TIME_Absolute expiry;
154 * Options for the store operation.
156 enum GNUNET_PEERSTORE_StoreOption options;
160 * Context for a iterate request
162 struct GNUNET_PEERSTORE_IterateContext
167 struct GNUNET_PEERSTORE_IterateContext *next;
172 struct GNUNET_PEERSTORE_IterateContext *prev;
175 * Handle to the PEERSTORE service.
177 struct GNUNET_PEERSTORE_Handle *h;
180 * Which subsystem does the store?
185 * Peer the store is for.
187 struct GNUNET_PeerIdentity peer;
190 * Key for the store operation.
195 * Callback with each matching record
197 GNUNET_PEERSTORE_Processor callback;
200 * Closure for @e callback
205 * #GNUNET_YES if we are currently processing records.
211 * Context for a watch request
213 struct GNUNET_PEERSTORE_WatchContext
218 struct GNUNET_PEERSTORE_WatchContext *next;
223 struct GNUNET_PEERSTORE_WatchContext *prev;
226 * Handle to the PEERSTORE service.
228 struct GNUNET_PEERSTORE_Handle *h;
231 * Callback with each record received
233 GNUNET_PEERSTORE_Processor callback;
236 * Closure for @e callback
241 * Hash of the combined key
243 struct GNUNET_HashCode keyhash;
246 /******************************************************************************/
247 /******************* DECLARATIONS *********************/
248 /******************************************************************************/
251 * Close the existing connection to PEERSTORE and reconnect.
253 * @param cls a `struct GNUNET_PEERSTORE_Handle *h`
256 reconnect (void *cls);
260 * Disconnect from the peerstore service.
262 * @param h peerstore handle to disconnect
265 disconnect (struct GNUNET_PEERSTORE_Handle *h)
267 struct GNUNET_PEERSTORE_IterateContext *next;
269 for (struct GNUNET_PEERSTORE_IterateContext *ic = h->iterate_head; NULL != ic;
273 if (GNUNET_YES == ic->iterating)
275 GNUNET_PEERSTORE_Processor icb;
279 icb_cls = ic->callback_cls;
280 GNUNET_PEERSTORE_iterate_cancel (ic);
282 icb (icb_cls, NULL, "Iteration canceled due to reconnection");
288 GNUNET_MQ_destroy (h->mq);
295 * Function that will schedule the job that will try
296 * to connect us again to the client.
298 * @param h peerstore to reconnect
301 disconnect_and_schedule_reconnect (struct GNUNET_PEERSTORE_Handle *h)
303 GNUNET_assert (NULL == h->reconnect_task);
305 LOG (GNUNET_ERROR_TYPE_DEBUG,
306 "Scheduling task to reconnect to PEERSTORE service in %s.\n",
307 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
309 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
310 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
315 * Callback after MQ envelope is sent
317 * @param cls a `struct GNUNET_PEERSTORE_StoreContext *`
320 store_request_sent (void *cls)
322 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
323 GNUNET_PEERSTORE_Continuation cont;
327 cont_cls = sc->cont_cls;
328 GNUNET_PEERSTORE_store_cancel (sc);
330 cont (cont_cls, GNUNET_OK);
334 /******************************************************************************/
335 /******************* CONNECTION FUNCTIONS *********************/
336 /******************************************************************************/
340 * Function called when we had trouble talking to the service.
343 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
345 struct GNUNET_PEERSTORE_Handle *h = cls;
347 LOG (GNUNET_ERROR_TYPE_ERROR,
348 "Received an error notification from MQ of type: %d\n",
350 disconnect_and_schedule_reconnect (h);
355 * Iterator over previous watches to resend them
357 * @param cls the `struct GNUNET_PEERSTORE_Handle`
358 * @param key key for the watch
359 * @param value the `struct GNUNET_PEERSTORE_WatchContext *`
360 * @return #GNUNET_YES (continue to iterate)
363 rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value)
365 struct GNUNET_PEERSTORE_Handle *h = cls;
366 struct GNUNET_PEERSTORE_WatchContext *wc = value;
367 struct StoreKeyHashMessage *hm;
368 struct GNUNET_MQ_Envelope *ev;
370 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
371 hm->keyhash = wc->keyhash;
372 GNUNET_MQ_send (h->mq, ev);
378 * Iterator over watch requests to cancel them.
381 * @param key key to the watch request
382 * @param value watch context
383 * @return #GNUNET_YES to continue iteration
386 destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
388 struct GNUNET_PEERSTORE_WatchContext *wc = value;
390 GNUNET_PEERSTORE_watch_cancel (wc);
396 * Kill the connection to the service. This can be delayed in case of pending
397 * STORE requests and the user explicitly asked to sync first. Otherwise it is
398 * performed instantly.
400 * @param h Handle to the service.
403 final_disconnect (struct GNUNET_PEERSTORE_Handle *h)
407 GNUNET_MQ_destroy (h->mq);
415 * Connect to the PEERSTORE service.
417 * @param cfg configuration to use
418 * @return NULL on error
420 struct GNUNET_PEERSTORE_Handle *
421 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
423 struct GNUNET_PEERSTORE_Handle *h;
425 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
427 h->disconnecting = GNUNET_NO;
439 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
441 * Any pending STORE requests will depend on @e snyc_first flag.
443 * @param h handle to disconnect
444 * @param sync_first send any pending STORE requests before disconnecting
447 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
449 struct GNUNET_PEERSTORE_IterateContext *ic;
450 struct GNUNET_PEERSTORE_StoreContext *sc;
452 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
453 if (NULL != h->watches)
455 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
456 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
459 while (NULL != (ic = h->iterate_head))
462 GNUNET_PEERSTORE_iterate_cancel (ic);
464 if (NULL != h->store_head)
466 if (GNUNET_YES == sync_first)
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "Delaying disconnection due to pending store requests.\n");
470 h->disconnecting = GNUNET_YES;
473 while (NULL != (sc = h->store_head))
474 GNUNET_PEERSTORE_store_cancel (sc);
476 final_disconnect (h);
480 /******************************************************************************/
481 /******************* STORE FUNCTIONS *********************/
482 /******************************************************************************/
486 * Cancel a store request
488 * @param sc Store request context
491 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
493 struct GNUNET_PEERSTORE_Handle *h = sc->h;
495 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
496 GNUNET_free (sc->sub_system);
497 GNUNET_free (sc->value);
498 GNUNET_free (sc->key);
500 if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head))
501 final_disconnect (h);
506 * Store a new entry in the PEERSTORE.
507 * Note that stored entries can be lost in some cases
508 * such as power failure.
510 * @param h Handle to the PEERSTORE service
511 * @param sub_system name of the sub system
512 * @param peer Peer Identity
513 * @param key entry key
514 * @param value entry value BLOB
515 * @param size size of @e value
516 * @param expiry absolute time after which the entry is (possibly) deleted
517 * @param options options specific to the storage operation
518 * @param cont Continuation function after the store request is sent
519 * @param cont_cls Closure for @a cont
521 struct GNUNET_PEERSTORE_StoreContext *
522 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
523 const char *sub_system,
524 const struct GNUNET_PeerIdentity *peer,
528 struct GNUNET_TIME_Absolute expiry,
529 enum GNUNET_PEERSTORE_StoreOption options,
530 GNUNET_PEERSTORE_Continuation cont,
533 struct GNUNET_MQ_Envelope *ev;
534 struct GNUNET_PEERSTORE_StoreContext *sc;
536 LOG (GNUNET_ERROR_TYPE_DEBUG,
537 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
543 PEERSTORE_create_record_mq_envelope (sub_system,
550 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
551 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
553 sc->sub_system = GNUNET_strdup (sub_system);
555 sc->key = GNUNET_strdup (key);
556 sc->value = GNUNET_memdup (value, size);
559 sc->options = options;
561 sc->cont_cls = cont_cls;
564 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
565 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
566 GNUNET_MQ_send (h->mq, ev);
571 /******************************************************************************/
572 /******************* ITERATE FUNCTIONS *********************/
573 /******************************************************************************/
577 * When a response for iterate request is received
579 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
580 * @param msg message received
583 handle_iterate_end (void *cls, const struct GNUNET_MessageHeader *msg)
585 struct GNUNET_PEERSTORE_Handle *h = cls;
586 struct GNUNET_PEERSTORE_IterateContext *ic;
587 GNUNET_PEERSTORE_Processor callback;
590 ic = h->iterate_head;
593 LOG (GNUNET_ERROR_TYPE_ERROR,
594 _ ("Unexpected iteration response, this should not happen.\n"));
595 disconnect_and_schedule_reconnect (h);
598 callback = ic->callback;
599 callback_cls = ic->callback_cls;
600 ic->iterating = GNUNET_NO;
601 GNUNET_PEERSTORE_iterate_cancel (ic);
602 if (NULL != callback)
603 callback (callback_cls, NULL, NULL);
604 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
609 * When a response for iterate request is received, check the
610 * message is well-formed.
612 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
613 * @param msg message received
616 check_iterate_result (void *cls, const struct StoreRecordMessage *msg)
618 /* we defer validation to #handle_iterate_result */
624 * When a response for iterate request is received
626 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
627 * @param msg message received
630 handle_iterate_result (void *cls, const struct StoreRecordMessage *msg)
632 struct GNUNET_PEERSTORE_Handle *h = cls;
633 struct GNUNET_PEERSTORE_IterateContext *ic;
634 GNUNET_PEERSTORE_Processor callback;
636 struct GNUNET_PEERSTORE_Record *record;
638 ic = h->iterate_head;
641 LOG (GNUNET_ERROR_TYPE_ERROR,
642 _ ("Unexpected iteration response, this should not happen.\n"));
643 disconnect_and_schedule_reconnect (h);
646 ic->iterating = GNUNET_YES;
647 callback = ic->callback;
648 callback_cls = ic->callback_cls;
649 if (NULL == callback)
651 record = PEERSTORE_parse_record_message (msg);
654 callback (callback_cls,
656 _ ("Received a malformed response from service."));
660 callback (callback_cls, record, NULL);
661 PEERSTORE_destroy_record (record);
667 * Cancel an iterate request
668 * Please do not call after the iterate request is done
670 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
673 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
675 if (GNUNET_NO == ic->iterating)
677 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic);
678 GNUNET_free (ic->sub_system);
679 GNUNET_free_non_null (ic->key);
688 * Iterate over records matching supplied key information
690 * @param h handle to the PEERSTORE service
691 * @param sub_system name of sub system
692 * @param peer Peer identity (can be NULL)
693 * @param key entry key string (can be NULL)
694 * @param callback function called with each matching record, all NULL's on end
695 * @param callback_cls closure for @a callback
696 * @return Handle to iteration request
698 struct GNUNET_PEERSTORE_IterateContext *
699 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
700 const char *sub_system,
701 const struct GNUNET_PeerIdentity *peer,
703 GNUNET_PEERSTORE_Processor callback,
706 struct GNUNET_MQ_Envelope *ev;
707 struct GNUNET_PEERSTORE_IterateContext *ic;
710 PEERSTORE_create_record_mq_envelope (sub_system,
715 GNUNET_TIME_UNIT_FOREVER_ABS,
717 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
718 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
719 ic->callback = callback;
720 ic->callback_cls = callback_cls;
722 ic->sub_system = GNUNET_strdup (sub_system);
726 ic->key = GNUNET_strdup (key);
727 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head, h->iterate_tail, ic);
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Sending an iterate request for sub system `%s'\n",
731 GNUNET_MQ_send (h->mq, ev);
736 /******************************************************************************/
737 /******************* WATCH FUNCTIONS *********************/
738 /******************************************************************************/
741 * When a watch record is received, validate it is well-formed.
743 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
744 * @param msg message received
747 check_watch_record (void *cls, const struct StoreRecordMessage *msg)
749 /* we defer validation to #handle_watch_result */
755 * When a watch record is received, process it.
757 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
758 * @param msg message received
761 handle_watch_record (void *cls, const struct StoreRecordMessage *msg)
763 struct GNUNET_PEERSTORE_Handle *h = cls;
764 struct GNUNET_PEERSTORE_Record *record;
765 struct GNUNET_HashCode keyhash;
766 struct GNUNET_PEERSTORE_WatchContext *wc;
768 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
769 record = PEERSTORE_parse_record_message (msg);
772 disconnect_and_schedule_reconnect (h);
775 PEERSTORE_hash_key (record->sub_system, &record->peer, record->key, &keyhash);
776 // FIXME: what if there are multiple watches for the same key?
777 wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash);
780 LOG (GNUNET_ERROR_TYPE_ERROR,
781 _ ("Received a watch result for a non existing watch.\n"));
782 PEERSTORE_destroy_record (record);
783 disconnect_and_schedule_reconnect (h);
786 if (NULL != wc->callback)
787 wc->callback (wc->callback_cls, record, NULL);
788 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
789 PEERSTORE_destroy_record (record);
794 * Close the existing connection to PEERSTORE and reconnect.
796 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
799 reconnect (void *cls)
801 struct GNUNET_PEERSTORE_Handle *h = cls;
802 struct GNUNET_MQ_MessageHandler mq_handlers[] =
803 { GNUNET_MQ_hd_fixed_size (iterate_end,
804 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
805 struct GNUNET_MessageHeader,
807 GNUNET_MQ_hd_var_size (iterate_result,
808 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD,
809 struct StoreRecordMessage,
811 GNUNET_MQ_hd_var_size (watch_record,
812 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD,
813 struct StoreRecordMessage,
815 GNUNET_MQ_handler_end () };
816 struct GNUNET_MQ_Envelope *ev;
818 h->reconnect_task = NULL;
819 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
820 h->mq = GNUNET_CLIENT_connect (h->cfg,
823 &handle_client_error,
827 LOG (GNUNET_ERROR_TYPE_DEBUG,
828 "Resending pending requests after reconnect.\n");
829 if (NULL != h->watches)
830 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h);
831 for (struct GNUNET_PEERSTORE_IterateContext *ic = h->iterate_head; NULL != ic;
835 PEERSTORE_create_record_mq_envelope (ic->sub_system,
840 GNUNET_TIME_UNIT_FOREVER_ABS,
842 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
843 GNUNET_MQ_send (h->mq, ev);
845 for (struct GNUNET_PEERSTORE_StoreContext *sc = h->store_head; NULL != sc;
849 PEERSTORE_create_record_mq_envelope (sc->sub_system,
856 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
857 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
858 GNUNET_MQ_send (h->mq, ev);
864 * Cancel a watch request
866 * @param wc handle to the watch request
869 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
871 struct GNUNET_PEERSTORE_Handle *h = wc->h;
872 struct GNUNET_MQ_Envelope *ev;
873 struct StoreKeyHashMessage *hm;
875 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
876 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
877 hm->keyhash = wc->keyhash;
878 GNUNET_MQ_send (h->mq, ev);
881 GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc));
887 * Request watching a given key
888 * User will be notified with any new values added to key
890 * @param h handle to the PEERSTORE service
891 * @param sub_system name of sub system
892 * @param peer Peer identity
893 * @param key entry key string
894 * @param callback function called with each new value
895 * @param callback_cls closure for @a callback
896 * @return Handle to watch request
898 struct GNUNET_PEERSTORE_WatchContext *
899 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
900 const char *sub_system,
901 const struct GNUNET_PeerIdentity *peer,
903 GNUNET_PEERSTORE_Processor callback,
906 struct GNUNET_MQ_Envelope *ev;
907 struct StoreKeyHashMessage *hm;
908 struct GNUNET_PEERSTORE_WatchContext *wc;
910 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
911 PEERSTORE_hash_key (sub_system, peer, key, &hm->keyhash);
912 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
913 wc->callback = callback;
914 wc->callback_cls = callback_cls;
916 wc->keyhash = hm->keyhash;
917 if (NULL == h->watches)
918 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
919 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
923 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
924 LOG (GNUNET_ERROR_TYPE_DEBUG,
925 "Sending a watch request for subsystem `%s', peer `%s', key `%s'.\n",
929 GNUNET_MQ_send (h->mq, ev);
933 /* end of peerstore_api.c */