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 /******************************************************************************/
291 * Function called when we had trouble talking to the service.
294 handle_client_error (void *cls,
295 enum GNUNET_MQ_Error error)
297 struct GNUNET_PEERSTORE_Handle *h = cls;
299 LOG (GNUNET_ERROR_TYPE_ERROR,
300 "Received an error notification from MQ of type: %d\n",
307 * Iterator over previous watches to resend them
309 * @param cls the `struct GNUNET_PEERSTORE_Handle`
310 * @param key key for the watch
311 * @param value the `struct GNUNET_PEERSTORE_WatchContext *`
312 * @return #GNUNET_YES (continue to iterate)
315 rewatch_it (void *cls,
316 const struct GNUNET_HashCode *key,
319 struct GNUNET_PEERSTORE_Handle *h = cls;
320 struct GNUNET_PEERSTORE_WatchContext *wc = value;
321 struct StoreKeyHashMessage *hm;
322 struct GNUNET_MQ_Envelope *ev;
324 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
325 hm->keyhash = wc->keyhash;
326 GNUNET_MQ_send (h->mq, ev);
332 * Called when the iterate request is timedout
334 * @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
337 iterate_timeout (void *cls)
339 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
340 GNUNET_PEERSTORE_Processor callback;
343 ic->timeout_task = NULL;
344 callback = ic->callback;
345 callback_cls = ic->callback_cls;
346 GNUNET_PEERSTORE_iterate_cancel (ic);
347 if (NULL != callback)
348 callback (callback_cls,
355 * Iterator over watch requests to cancel them.
358 * @param key key to the watch request
359 * @param value watch context
360 * @return #GNUNET_YES to continue iteration
363 destroy_watch (void *cls,
364 const struct GNUNET_HashCode *key,
367 struct GNUNET_PEERSTORE_WatchContext *wc = value;
369 GNUNET_PEERSTORE_watch_cancel (wc);
375 * Kill the connection to the service. This can be delayed in case of pending
376 * STORE requests and the user explicitly asked to sync first. Otherwise it is
377 * performed instantly.
379 * @param h Handle to the service.
382 do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
386 GNUNET_MQ_destroy (h->mq);
394 * Connect to the PEERSTORE service.
396 * @param cfg configuration to use
397 * @return NULL on error
399 struct GNUNET_PEERSTORE_Handle *
400 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
402 struct GNUNET_PEERSTORE_Handle *h;
404 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
406 h->disconnecting = GNUNET_NO;
418 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
420 * Any pending STORE requests will depend on @e snyc_first flag.
422 * @param h handle to disconnect
423 * @param sync_first send any pending STORE requests before disconnecting
426 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h,
429 struct GNUNET_PEERSTORE_IterateContext *ic;
430 struct GNUNET_PEERSTORE_StoreContext *sc;
432 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
433 if (NULL != h->watches)
435 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
436 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
439 while (NULL != (ic = h->iterate_head))
442 GNUNET_PEERSTORE_iterate_cancel (ic);
444 if (NULL != h->store_head)
446 if (GNUNET_YES == sync_first)
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 "Delaying disconnection due to pending store requests.\n");
450 h->disconnecting = GNUNET_YES;
453 while (NULL != (sc = h->store_head))
454 GNUNET_PEERSTORE_store_cancel (sc);
460 /******************************************************************************/
461 /******************* STORE FUNCTIONS *********************/
462 /******************************************************************************/
466 * Cancel a store request
468 * @param sc Store request context
471 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
473 struct GNUNET_PEERSTORE_Handle *h = sc->h;
475 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
476 GNUNET_free (sc->sub_system);
477 GNUNET_free (sc->value);
478 GNUNET_free (sc->key);
480 if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head))
486 * Store a new entry in the PEERSTORE.
487 * Note that stored entries can be lost in some cases
488 * such as power failure.
490 * @param h Handle to the PEERSTORE service
491 * @param sub_system name of the sub system
492 * @param peer Peer Identity
493 * @param key entry key
494 * @param value entry value BLOB
495 * @param size size of @e value
496 * @param expiry absolute time after which the entry is (possibly) deleted
497 * @param options options specific to the storage operation
498 * @param cont Continuation function after the store request is sent
499 * @param cont_cls Closure for @a cont
501 struct GNUNET_PEERSTORE_StoreContext *
502 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
503 const char *sub_system,
504 const struct GNUNET_PeerIdentity *peer,
506 const void *value, size_t size,
507 struct GNUNET_TIME_Absolute expiry,
508 enum GNUNET_PEERSTORE_StoreOption options,
509 GNUNET_PEERSTORE_Continuation cont,
512 struct GNUNET_MQ_Envelope *ev;
513 struct GNUNET_PEERSTORE_StoreContext *sc;
515 LOG (GNUNET_ERROR_TYPE_DEBUG,
516 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
517 size, sub_system, GNUNET_i2s (peer), key);
518 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
520 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
521 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
523 sc->sub_system = GNUNET_strdup (sub_system);
525 sc->key = GNUNET_strdup (key);
526 sc->value = GNUNET_memdup (value, size);
529 sc->options = options;
531 sc->cont_cls = cont_cls;
534 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
535 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
536 GNUNET_MQ_send (h->mq, ev);
542 /******************************************************************************/
543 /******************* ITERATE FUNCTIONS *********************/
544 /******************************************************************************/
548 * When a response for iterate request is received
550 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
551 * @param msg message received
554 handle_iterate_end (void *cls,
555 const struct GNUNET_MessageHeader *msg)
557 struct GNUNET_PEERSTORE_Handle *h = cls;
558 struct GNUNET_PEERSTORE_IterateContext *ic;
559 GNUNET_PEERSTORE_Processor callback;
562 ic = h->iterate_head;
565 LOG (GNUNET_ERROR_TYPE_ERROR,
566 _("Unexpected iteration response, this should not happen.\n"));
570 callback = ic->callback;
571 callback_cls = ic->callback_cls;
572 ic->iterating = GNUNET_NO;
573 GNUNET_PEERSTORE_iterate_cancel (ic);
574 if (NULL != callback)
575 callback (callback_cls, NULL, NULL);
580 * When a response for iterate request is received, check the
581 * message is well-formed.
583 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
584 * @param msg message received
587 check_iterate_result (void *cls,
588 const struct StoreRecordMessage *msg)
590 /* we defer validation to #handle_iterate_result */
596 * When a response for iterate request is received
598 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
599 * @param msg message received
602 handle_iterate_result (void *cls,
603 const struct StoreRecordMessage *msg)
605 struct GNUNET_PEERSTORE_Handle *h = cls;
606 struct GNUNET_PEERSTORE_IterateContext *ic;
607 GNUNET_PEERSTORE_Processor callback;
609 struct GNUNET_PEERSTORE_Record *record;
611 ic = h->iterate_head;
614 LOG (GNUNET_ERROR_TYPE_ERROR,
615 _("Unexpected iteration response, this should not happen.\n"));
619 ic->iterating = GNUNET_YES;
620 callback = ic->callback;
621 callback_cls = ic->callback_cls;
622 if (NULL == callback)
624 record = PEERSTORE_parse_record_message (msg);
627 callback (callback_cls,
629 _("Received a malformed response from service."));
633 callback (callback_cls,
636 PEERSTORE_destroy_record (record);
642 * Cancel an iterate request
643 * Please do not call after the iterate request is done
645 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
648 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
650 if (NULL != ic->timeout_task)
652 GNUNET_SCHEDULER_cancel (ic->timeout_task);
653 ic->timeout_task = NULL;
655 if (GNUNET_NO == ic->iterating)
657 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head,
660 GNUNET_free (ic->sub_system);
661 GNUNET_free_non_null (ic->key);
670 * Iterate over records matching supplied key information
672 * @param h handle to the PEERSTORE service
673 * @param sub_system name of sub system
674 * @param peer Peer identity (can be NULL)
675 * @param key entry key string (can be NULL)
676 * @param timeout time after which the iterate request is canceled
677 * @param callback function called with each matching record, all NULL's on end
678 * @param callback_cls closure for @a callback
679 * @return Handle to iteration request
681 struct GNUNET_PEERSTORE_IterateContext *
682 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
683 const char *sub_system,
684 const struct GNUNET_PeerIdentity *peer,
686 struct GNUNET_TIME_Relative timeout,
687 GNUNET_PEERSTORE_Processor callback,
690 struct GNUNET_MQ_Envelope *ev;
691 struct GNUNET_PEERSTORE_IterateContext *ic;
693 ev = PEERSTORE_create_record_mq_envelope (sub_system,
697 GNUNET_TIME_UNIT_FOREVER_ABS,
699 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
700 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
702 ic->callback = callback;
703 ic->callback_cls = callback_cls;
705 ic->sub_system = GNUNET_strdup (sub_system);
709 ic->key = GNUNET_strdup (key);
710 ic->timeout = timeout;
711 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head,
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "Sending an iterate request for sub system `%s'\n",
717 GNUNET_MQ_send (h->mq, ev);
719 GNUNET_SCHEDULER_add_delayed (timeout,
726 /******************************************************************************/
727 /******************* WATCH FUNCTIONS *********************/
728 /******************************************************************************/
731 * When a watch record is received, validate it is well-formed.
733 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
734 * @param msg message received
737 check_watch_record (void *cls,
738 const struct StoreRecordMessage *msg)
740 /* we defer validation to #handle_watch_result */
746 * When a watch record is received, process it.
748 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
749 * @param msg message received
752 handle_watch_record (void *cls,
753 const struct StoreRecordMessage *msg)
755 struct GNUNET_PEERSTORE_Handle *h = cls;
756 struct GNUNET_PEERSTORE_Record *record;
757 struct GNUNET_HashCode keyhash;
758 struct GNUNET_PEERSTORE_WatchContext *wc;
760 LOG (GNUNET_ERROR_TYPE_DEBUG,
761 "Received a watch record from service.\n");
762 record = PEERSTORE_parse_record_message (msg);
768 PEERSTORE_hash_key (record->sub_system,
772 // FIXME: what if there are multiple watches for the same key?
773 wc = GNUNET_CONTAINER_multihashmap_get (h->watches,
777 LOG (GNUNET_ERROR_TYPE_ERROR,
778 _("Received a watch result for a non existing watch.\n"));
779 PEERSTORE_destroy_record (record);
783 if (NULL != wc->callback)
784 wc->callback (wc->callback_cls,
787 PEERSTORE_destroy_record (record);
792 * Close the existing connection to PEERSTORE and reconnect.
794 * @param h handle to the service
797 reconnect (struct GNUNET_PEERSTORE_Handle *h)
799 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
800 GNUNET_MQ_hd_fixed_size (iterate_end,
801 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
802 struct GNUNET_MessageHeader,
804 GNUNET_MQ_hd_var_size (iterate_result,
805 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD,
806 struct StoreRecordMessage,
808 GNUNET_MQ_hd_var_size (watch_record,
809 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD,
810 struct StoreRecordMessage,
812 GNUNET_MQ_handler_end ()
814 struct GNUNET_PEERSTORE_IterateContext *ic;
815 struct GNUNET_PEERSTORE_IterateContext *next;
816 GNUNET_PEERSTORE_Processor icb;
818 struct GNUNET_PEERSTORE_StoreContext *sc;
819 struct GNUNET_MQ_Envelope *ev;
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Reconnecting...\n");
823 for (ic = h->iterate_head; NULL != ic; ic = next)
826 if (GNUNET_YES == ic->iterating)
829 icb_cls = ic->callback_cls;
830 GNUNET_PEERSTORE_iterate_cancel (ic);
834 "Iteration canceled due to reconnection");
839 GNUNET_MQ_destroy (h->mq);
842 h->mq = GNUNET_CLIENT_connect (h->cfg,
845 &handle_client_error,
849 LOG (GNUNET_ERROR_TYPE_DEBUG,
850 "Resending pending requests after reconnect.\n");
851 if (NULL != h->watches)
852 GNUNET_CONTAINER_multihashmap_iterate (h->watches,
855 for (ic = h->iterate_head; NULL != ic; ic = ic->next)
857 ev = PEERSTORE_create_record_mq_envelope (ic->sub_system,
861 GNUNET_TIME_UNIT_FOREVER_ABS,
863 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
864 GNUNET_MQ_send (h->mq, ev);
865 if (NULL != ic->timeout_task)
866 GNUNET_SCHEDULER_cancel (ic->timeout_task);
868 = GNUNET_SCHEDULER_add_delayed (ic->timeout,
872 for (sc = h->store_head; NULL != sc; sc = sc->next)
874 ev = PEERSTORE_create_record_mq_envelope (sc->sub_system,
881 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
882 GNUNET_MQ_notify_sent (ev,
885 GNUNET_MQ_send (h->mq,
892 * Cancel a watch request
894 * @param wc handle to the watch request
897 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
899 struct GNUNET_PEERSTORE_Handle *h = wc->h;
900 struct GNUNET_MQ_Envelope *ev;
901 struct StoreKeyHashMessage *hm;
903 LOG (GNUNET_ERROR_TYPE_DEBUG,
904 "Canceling watch.\n");
905 ev = GNUNET_MQ_msg (hm,
906 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
907 hm->keyhash = wc->keyhash;
908 GNUNET_MQ_send (h->mq, ev);
909 GNUNET_CONTAINER_multihashmap_remove (h->watches,
917 * Request watching a given key
918 * User will be notified with any new values added to key
920 * @param h handle to the PEERSTORE service
921 * @param sub_system name of sub system
922 * @param peer Peer identity
923 * @param key entry key string
924 * @param callback function called with each new value
925 * @param callback_cls closure for @a callback
926 * @return Handle to watch request
928 struct GNUNET_PEERSTORE_WatchContext *
929 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
930 const char *sub_system,
931 const struct GNUNET_PeerIdentity *peer,
933 GNUNET_PEERSTORE_Processor callback,
936 struct GNUNET_MQ_Envelope *ev;
937 struct StoreKeyHashMessage *hm;
938 struct GNUNET_PEERSTORE_WatchContext *wc;
940 ev = GNUNET_MQ_msg (hm,
941 GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
942 PEERSTORE_hash_key (sub_system,
946 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
947 wc->callback = callback;
948 wc->callback_cls = callback_cls;
950 wc->keyhash = hm->keyhash;
951 if (NULL == h->watches)
952 h->watches = GNUNET_CONTAINER_multihashmap_create (5,
954 GNUNET_assert (GNUNET_OK ==
955 GNUNET_CONTAINER_multihashmap_put (h->watches,
958 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
959 LOG (GNUNET_ERROR_TYPE_DEBUG,
960 "Sending a watch request for subsystem `%s', peer `%s', key `%s'.\n",
964 GNUNET_MQ_send (h->mq,
969 /* end of peerstore_api.c */