2 This file is part of GNUnet.
3 (C) 2013-2014 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.
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;
49 * Connection to the service.
51 struct GNUNET_CLIENT_Connection *client;
56 struct GNUNET_MQ_Handle *mq;
59 * Head of active STORE requests.
61 struct GNUNET_PEERSTORE_StoreContext *store_head;
64 * Tail of active STORE requests.
66 struct GNUNET_PEERSTORE_StoreContext *store_tail;
69 * Head of active ITERATE requests.
71 struct GNUNET_PEERSTORE_IterateContext *iterate_head;
74 * Tail of active ITERATE requests.
76 struct GNUNET_PEERSTORE_IterateContext *iterate_tail;
79 * Hashmap of watch requests
81 struct GNUNET_CONTAINER_MultiHashMap *watches;
84 * Are we in the process of disconnecting but need to sync first?
91 * Context for a store request
93 struct GNUNET_PEERSTORE_StoreContext
98 struct GNUNET_PEERSTORE_StoreContext *next;
103 struct GNUNET_PEERSTORE_StoreContext *prev;
106 * Handle to the PEERSTORE service.
108 struct GNUNET_PEERSTORE_Handle *h;
111 * Continuation called with service response
113 GNUNET_PEERSTORE_Continuation cont;
116 * Closure for @e cont
121 * Which subsystem does the store?
126 * Key for the store operation.
131 * Contains @e size bytes.
136 * Peer the store is for.
138 struct GNUNET_PeerIdentity peer;
141 * Number of bytes in @e value.
146 * When does the value expire?
148 struct GNUNET_TIME_Absolute expiry;
151 * Options for the store operation.
153 enum GNUNET_PEERSTORE_StoreOption options;
158 * Context for a iterate request
160 struct GNUNET_PEERSTORE_IterateContext
165 struct GNUNET_PEERSTORE_IterateContext *next;
170 struct GNUNET_PEERSTORE_IterateContext *prev;
173 * Handle to the PEERSTORE service.
175 struct GNUNET_PEERSTORE_Handle *h;
178 * Which subsystem does the store?
183 * Peer the store is for.
185 struct GNUNET_PeerIdentity peer;
188 * Key for the store operation.
195 struct GNUNET_TIME_Relative timeout;
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.
213 * Task identifier for the function called
214 * on iterate request timeout
216 struct GNUNET_SCHEDULER_Task * timeout_task;
221 * Context for a watch request
223 struct GNUNET_PEERSTORE_WatchContext
228 struct GNUNET_PEERSTORE_WatchContext *next;
233 struct GNUNET_PEERSTORE_WatchContext *prev;
236 * Handle to the PEERSTORE service.
238 struct GNUNET_PEERSTORE_Handle *h;
241 * Callback with each record received
243 GNUNET_PEERSTORE_Processor callback;
246 * Closure for @e callback
251 * Hash of the combined key
253 struct GNUNET_HashCode keyhash;
257 /******************************************************************************/
258 /******************* DECLARATIONS *********************/
259 /******************************************************************************/
262 * When a response for iterate request is received
264 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
265 * @param msg message received, NULL on timeout or fatal error
268 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
271 * When a watch record is received
273 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
274 * @param msg message received, NULL on timeout or fatal error
277 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
280 * Close the existing connection to PEERSTORE and reconnect.
282 * @param h handle to the service
285 reconnect (struct GNUNET_PEERSTORE_Handle *h);
289 * Callback after MQ envelope is sent
291 * @param cls a `struct GNUNET_PEERSTORE_StoreContext *`
294 store_request_sent (void *cls)
296 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
297 GNUNET_PEERSTORE_Continuation cont;
301 cont_cls = sc->cont_cls;
302 GNUNET_PEERSTORE_store_cancel (sc);
304 cont (cont_cls, GNUNET_OK);
309 * MQ message handlers
311 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
312 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
313 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
314 sizeof (struct GNUNET_MessageHeader)},
315 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
316 GNUNET_MQ_HANDLERS_END
319 /******************************************************************************/
320 /******************* CONNECTION FUNCTIONS *********************/
321 /******************************************************************************/
324 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
326 struct GNUNET_PEERSTORE_Handle *h = cls;
328 LOG (GNUNET_ERROR_TYPE_ERROR,
329 _("Received an error notification from MQ of type: %d\n"), error);
335 * Iterator over previous watches to resend them
337 * @param cls the `struct GNUNET_PEERSTORE_Handle`
338 * @param key key for the watch
339 * @param value the `struct GNUNET_PEERSTORE_WatchContext *`
340 * @return #GNUNET_YES (continue to iterate)
343 rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value)
345 struct GNUNET_PEERSTORE_Handle *h = cls;
346 struct GNUNET_PEERSTORE_WatchContext *wc = value;
347 struct StoreKeyHashMessage *hm;
348 struct GNUNET_MQ_Envelope *ev;
350 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
351 hm->keyhash = wc->keyhash;
352 GNUNET_MQ_send (h->mq, ev);
358 * Called when the iterate request is timedout
360 * @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
361 * @param tc Scheduler task context (unused)
364 iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
366 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
367 GNUNET_PEERSTORE_Processor callback;
370 ic->timeout_task = NULL;
371 callback = ic->callback;
372 callback_cls = ic->callback_cls;
373 GNUNET_PEERSTORE_iterate_cancel (ic);
374 if (NULL != callback)
375 callback (callback_cls, NULL, _("timeout"));
380 * Close the existing connection to PEERSTORE and reconnect.
382 * @param h handle to the service
385 reconnect (struct GNUNET_PEERSTORE_Handle *h)
387 struct GNUNET_PEERSTORE_IterateContext *ic;
388 struct GNUNET_PEERSTORE_IterateContext *next;
389 GNUNET_PEERSTORE_Processor icb;
391 struct GNUNET_PEERSTORE_StoreContext *sc;
392 struct GNUNET_MQ_Envelope *ev;
394 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
395 for (ic = h->iterate_head; NULL != ic; ic = next)
399 icb_cls = ic->callback_cls;
400 GNUNET_PEERSTORE_iterate_cancel (ic);
402 icb (icb_cls, NULL, _("Iteration canceled due to reconnection."));
406 GNUNET_MQ_destroy (h->mq);
409 if (NULL != h->client)
411 GNUNET_CLIENT_disconnect (h->client);
415 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
416 GNUNET_assert (NULL != h->client);
418 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
419 &handle_client_error, h);
420 LOG (GNUNET_ERROR_TYPE_DEBUG,
421 "Resending pending requests after reconnect.\n");
422 if (NULL != h->watches)
423 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h);
424 for (ic = h->iterate_head; NULL != ic; ic = ic->next)
427 PEERSTORE_create_record_mq_envelope (ic->sub_system, &ic->peer, ic->key,
429 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
430 GNUNET_MQ_send (h->mq, ev);
432 GNUNET_SCHEDULER_add_delayed (ic->timeout, &iterate_timeout, ic);
434 for (sc = h->store_head; NULL != sc; sc = sc->next)
437 PEERSTORE_create_record_mq_envelope (sc->sub_system, &sc->peer, sc->key,
438 sc->value, sc->size, &sc->expiry,
440 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
441 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
442 GNUNET_MQ_send (h->mq, ev);
448 * Iterator over watch requests to cancel them.
451 * @param key key to the watch request
452 * @param value watch context
453 * @return #GNUNET_YES to continue iteration
456 destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
458 struct GNUNET_PEERSTORE_WatchContext *wc = value;
460 GNUNET_PEERSTORE_watch_cancel (wc);
466 * Kill the connection to the service. This can be delayed in case of pending
467 * STORE requests and the user explicitly asked to sync first. Otherwise it is
468 * performed instantly.
470 * @param h Handle to the service.
473 do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
477 GNUNET_MQ_destroy (h->mq);
480 if (NULL != h->client)
482 GNUNET_CLIENT_disconnect (h->client);
490 * Connect to the PEERSTORE service.
492 * @param cfg configuration to use
493 * @return NULL on error
495 struct GNUNET_PEERSTORE_Handle *
496 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
498 struct GNUNET_PEERSTORE_Handle *h;
500 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
502 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
503 if (NULL == h->client)
509 h->disconnecting = GNUNET_NO;
511 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
512 &handle_client_error, h);
515 GNUNET_CLIENT_disconnect (h->client);
519 LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
525 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
527 * Any pending STORE requests will depend on @e snyc_first flag.
529 * @param h handle to disconnect
530 * @param sync_first send any pending STORE requests before disconnecting
533 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
535 struct GNUNET_PEERSTORE_IterateContext *ic;
536 struct GNUNET_PEERSTORE_IterateContext *ic_iter;
537 struct GNUNET_PEERSTORE_StoreContext *sc;
538 struct GNUNET_PEERSTORE_StoreContext *sc_iter;
540 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
541 if (NULL != h->watches)
543 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
544 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
547 ic_iter = h->iterate_head;
548 while (NULL != ic_iter)
552 ic_iter = ic_iter->next;
553 GNUNET_PEERSTORE_iterate_cancel (ic);
555 if (NULL != h->store_head)
557 if (GNUNET_YES == sync_first)
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Delaying disconnection due to pending store requests.\n");
561 h->disconnecting = GNUNET_YES;
564 sc_iter = h->store_head;
565 while (NULL != sc_iter)
568 sc_iter = sc_iter->next;
569 GNUNET_PEERSTORE_store_cancel (sc);
576 /******************************************************************************/
577 /******************* STORE FUNCTIONS *********************/
578 /******************************************************************************/
582 * Cancel a store request
584 * @param sc Store request context
587 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
589 struct GNUNET_PEERSTORE_Handle *h = sc->h;
591 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
592 GNUNET_free (sc->sub_system);
593 GNUNET_free (sc->value);
594 GNUNET_free (sc->key);
596 if ((GNUNET_YES == h->disconnecting) && (NULL == h->store_head))
602 * Store a new entry in the PEERSTORE.
603 * Note that stored entries can be lost in some cases
604 * such as power failure.
606 * @param h Handle to the PEERSTORE service
607 * @param sub_system name of the sub system
608 * @param peer Peer Identity
609 * @param key entry key
610 * @param value entry value BLOB
611 * @param size size of @e value
612 * @param expiry absolute time after which the entry is (possibly) deleted
613 * @param options options specific to the storage operation
614 * @param cont Continuation function after the store request is sent
615 * @param cont_cls Closure for @a cont
617 struct GNUNET_PEERSTORE_StoreContext *
618 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
619 const char *sub_system,
620 const struct GNUNET_PeerIdentity *peer, const char *key,
621 const void *value, size_t size,
622 struct GNUNET_TIME_Absolute expiry,
623 enum GNUNET_PEERSTORE_StoreOption options,
624 GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
626 struct GNUNET_MQ_Envelope *ev;
627 struct GNUNET_PEERSTORE_StoreContext *sc;
629 LOG (GNUNET_ERROR_TYPE_DEBUG,
630 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
631 size, sub_system, GNUNET_i2s (peer), key);
632 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
634 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
635 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
637 sc->sub_system = GNUNET_strdup (sub_system);
639 sc->key = GNUNET_strdup (key);
640 sc->value = GNUNET_memdup (value, size);
643 sc->options = options;
645 sc->cont_cls = cont_cls;
648 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
649 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
650 GNUNET_MQ_send (h->mq, ev);
656 /******************************************************************************/
657 /******************* ITERATE FUNCTIONS *********************/
658 /******************************************************************************/
661 * When a response for iterate request is received
663 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
664 * @param msg message received, NULL on timeout or fatal error
667 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
669 struct GNUNET_PEERSTORE_Handle *h = cls;
670 struct GNUNET_PEERSTORE_IterateContext *ic;
671 GNUNET_PEERSTORE_Processor callback;
674 struct GNUNET_PEERSTORE_Record *record;
677 ic = h->iterate_head;
680 LOG (GNUNET_ERROR_TYPE_ERROR,
681 _("Unexpected iteration response, this should not happen.\n"));
685 ic->iterating = GNUNET_YES;
686 callback = ic->callback;
687 callback_cls = ic->callback_cls;
688 if (NULL == msg) /* Connection error */
690 if (NULL != callback)
691 callback (callback_cls, NULL,
692 _("Error communicating with `PEERSTORE' service."));
696 msg_type = ntohs (msg->type);
697 if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
699 ic->iterating = GNUNET_NO;
700 GNUNET_PEERSTORE_iterate_cancel (ic);
701 if (NULL != callback)
702 callback (callback_cls, NULL, NULL);
705 if (NULL != callback)
707 record = PEERSTORE_parse_record_message (msg);
710 callback (callback_cls, NULL,
711 _("Received a malformed response from service."));
714 continue_iter = callback (callback_cls, record, NULL);
715 PEERSTORE_destroy_record (record);
717 if (GNUNET_NO == continue_iter)
724 * Cancel an iterate request
725 * Please do not call after the iterate request is done
727 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
730 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
732 if (NULL != ic->timeout_task)
734 GNUNET_SCHEDULER_cancel (ic->timeout_task);
735 ic->timeout_task = NULL;
737 if (GNUNET_NO == ic->iterating)
739 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic);
740 GNUNET_free (ic->sub_system);
742 GNUNET_free (ic->key);
751 * Iterate over records matching supplied key information
753 * @param h handle to the PEERSTORE service
754 * @param sub_system name of sub system
755 * @param peer Peer identity (can be NULL)
756 * @param key entry key string (can be NULL)
757 * @param timeout time after which the iterate request is canceled
758 * @param callback function called with each matching record, all NULL's on end
759 * @param callback_cls closure for @a callback
760 * @return Handle to iteration request
762 struct GNUNET_PEERSTORE_IterateContext *
763 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
764 const char *sub_system,
765 const struct GNUNET_PeerIdentity *peer,
766 const char *key, struct GNUNET_TIME_Relative timeout,
767 GNUNET_PEERSTORE_Processor callback,
770 struct GNUNET_MQ_Envelope *ev;
771 struct GNUNET_PEERSTORE_IterateContext *ic;
773 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0,
775 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
776 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
778 ic->callback = callback;
779 ic->callback_cls = callback_cls;
781 ic->sub_system = GNUNET_strdup (sub_system);
785 ic->key = GNUNET_strdup (key);
786 ic->timeout = timeout;
787 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head,
790 LOG (GNUNET_ERROR_TYPE_DEBUG,
791 "Sending an iterate request for sub system `%s'\n", sub_system);
792 GNUNET_MQ_send (h->mq, ev);
794 GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic);
799 /******************************************************************************/
800 /******************* WATCH FUNCTIONS *********************/
801 /******************************************************************************/
804 * When a watch record is received
806 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
807 * @param msg message received, NULL on timeout or fatal error
810 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
812 struct GNUNET_PEERSTORE_Handle *h = cls;
813 struct GNUNET_PEERSTORE_Record *record;
814 struct GNUNET_HashCode keyhash;
815 struct GNUNET_PEERSTORE_WatchContext *wc;
819 LOG (GNUNET_ERROR_TYPE_ERROR,
821 ("Problem receiving a watch response, no way to determine which request.\n"));
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
826 record = PEERSTORE_parse_record_message (msg);
827 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
828 // FIXME: what if there are multiple watches for the same key?
829 wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash);
832 LOG (GNUNET_ERROR_TYPE_ERROR,
833 _("Received a watch result for a non existing watch.\n"));
834 PEERSTORE_destroy_record (record);
838 if (NULL != wc->callback)
839 wc->callback (wc->callback_cls, record, NULL);
840 PEERSTORE_destroy_record (record);
845 * Cancel a watch request
847 * @param wc handle to the watch request
850 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
852 struct GNUNET_PEERSTORE_Handle *h = wc->h;
853 struct GNUNET_MQ_Envelope *ev;
854 struct StoreKeyHashMessage *hm;
856 LOG (GNUNET_ERROR_TYPE_DEBUG,
857 "Canceling watch.\n");
858 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
859 hm->keyhash = wc->keyhash;
860 GNUNET_MQ_send (h->mq, ev);
861 GNUNET_CONTAINER_multihashmap_remove (h->watches,
869 * Request watching a given key
870 * User will be notified with any new values added to key
872 * @param h handle to the PEERSTORE service
873 * @param sub_system name of sub system
874 * @param peer Peer identity
875 * @param key entry key string
876 * @param callback function called with each new value
877 * @param callback_cls closure for @a callback
878 * @return Handle to watch request
880 struct GNUNET_PEERSTORE_WatchContext *
881 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
882 const char *sub_system,
883 const struct GNUNET_PeerIdentity *peer, const char *key,
884 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
886 struct GNUNET_MQ_Envelope *ev;
887 struct StoreKeyHashMessage *hm;
888 struct GNUNET_PEERSTORE_WatchContext *wc;
890 GNUNET_assert (NULL != sub_system);
891 GNUNET_assert (NULL != peer);
892 GNUNET_assert (NULL != key);
893 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
894 PEERSTORE_hash_key (sub_system,
898 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
899 wc->callback = callback;
900 wc->callback_cls = callback_cls;
902 wc->keyhash = hm->keyhash;
903 if (NULL == h->watches)
904 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
905 GNUNET_assert (GNUNET_OK ==
906 GNUNET_CONTAINER_multihashmap_put (h->watches,
909 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
910 LOG (GNUNET_ERROR_TYPE_DEBUG,
911 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
915 GNUNET_MQ_send (h->mq, ev);
919 /* end of peerstore_api.c */