2 This file is part of GNUnet.
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.
22 * @file peerstore/peerstore_api.c
23 * @brief API for peerstore
24 * @author Omar Tarabai
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 * MQ Envelope with store request message
113 struct GNUNET_MQ_Envelope *ev;
116 * Continuation called with service response
118 GNUNET_PEERSTORE_Continuation cont;
128 * Context for a iterate request
130 struct GNUNET_PEERSTORE_IterateContext
135 struct GNUNET_PEERSTORE_IterateContext *next;
140 struct GNUNET_PEERSTORE_IterateContext *prev;
143 * Handle to the PEERSTORE service.
145 struct GNUNET_PEERSTORE_Handle *h;
148 * MQ Envelope with iterate request message
150 struct GNUNET_MQ_Envelope *ev;
153 * Callback with each matching record
155 GNUNET_PEERSTORE_Processor callback;
158 * Closure for 'callback'
163 * #GNUNET_YES / #GNUNET_NO
164 * Iterate request has been sent and we are still expecting records
169 * Task identifier for the function called
170 * on iterate request timeout
172 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
177 * Context for a watch request
179 struct GNUNET_PEERSTORE_WatchContext
184 struct GNUNET_PEERSTORE_WatchContext *next;
189 struct GNUNET_PEERSTORE_WatchContext *prev;
192 * Handle to the PEERSTORE service.
194 struct GNUNET_PEERSTORE_Handle *h;
197 * MQ Envelope with watch request message
199 struct GNUNET_MQ_Envelope *ev;
202 * Callback with each record received
204 GNUNET_PEERSTORE_Processor callback;
207 * Closure for 'callback'
212 * Hash of the combined key
214 struct GNUNET_HashCode keyhash;
217 * #GNUNET_YES / #GNUNET_NO
218 * if sent, cannot be canceled
224 /******************************************************************************/
225 /******************* DECLARATIONS *********************/
226 /******************************************************************************/
229 * When a response for iterate request is received
231 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
232 * @param msg message received, NULL on timeout or fatal error
235 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
238 * When a watch record is received
240 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
241 * @param msg message received, NULL on timeout or fatal error
244 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
247 * Close the existing connection to PEERSTORE and reconnect.
249 * @param h handle to the service
252 reconnect (struct GNUNET_PEERSTORE_Handle *h);
255 * Callback after MQ envelope is sent
257 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
260 watch_request_sent (void *cls);
263 * Callback after MQ envelope is sent
265 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
268 iterate_request_sent (void *cls);
271 * Callback after MQ envelope is sent
273 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
276 store_request_sent (void *cls);
279 * MQ message handlers
281 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
282 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
283 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
284 sizeof (struct GNUNET_MessageHeader)},
285 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
286 GNUNET_MQ_HANDLERS_END
289 /******************************************************************************/
290 /******************* CONNECTION FUNCTIONS *********************/
291 /******************************************************************************/
294 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
296 struct GNUNET_PEERSTORE_Handle *h = cls;
298 LOG (GNUNET_ERROR_TYPE_ERROR,
299 _("Received an error notification from MQ of type: %d\n"), error);
305 * Iterator over previous watches to resend them
308 rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value)
310 struct GNUNET_PEERSTORE_Handle *h = cls;
311 struct GNUNET_PEERSTORE_WatchContext *wc = value;
312 struct StoreKeyHashMessage *hm;
314 if (GNUNET_YES == wc->request_sent)
315 { /* Envelope gone, create new one. */
316 wc->ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
317 hm->keyhash = wc->keyhash;
318 wc->request_sent = GNUNET_NO;
320 GNUNET_MQ_notify_sent (wc->ev, &watch_request_sent, wc);
321 GNUNET_MQ_send (h->mq, wc->ev);
327 * Close the existing connection to PEERSTORE and reconnect.
329 * @param h handle to the service
332 reconnect (struct GNUNET_PEERSTORE_Handle *h)
334 struct GNUNET_PEERSTORE_IterateContext *ic;
335 GNUNET_PEERSTORE_Processor icb;
337 struct GNUNET_PEERSTORE_StoreContext *sc;
339 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
342 GNUNET_MQ_destroy (h->mq);
345 if (NULL != h->client)
347 GNUNET_CLIENT_disconnect (h->client);
350 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
351 GNUNET_assert (NULL != h->client);
353 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
354 &handle_client_error, h);
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Resending pending requests after reconnect.\n");
357 if (NULL != h->watches)
359 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h);
361 ic = h->iterate_head;
364 if (GNUNET_YES == ic->iterating)
367 icb_cls = ic->callback_cls;
368 GNUNET_PEERSTORE_iterate_cancel (ic);
370 icb (icb_cls, NULL, _("Iteration canceled due to reconnection."));
374 GNUNET_MQ_notify_sent (ic->ev, &iterate_request_sent, ic);
375 GNUNET_MQ_send (h->mq, ic->ev);
382 GNUNET_MQ_notify_sent (sc->ev, &store_request_sent, sc);
383 GNUNET_MQ_send (h->mq, sc->ev);
390 * Iterator over watch requests to cancel them.
393 * @param key key to the watch request
394 * @param value watch context
395 * @return #GNUNET_YES to continue iteration
398 destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
400 struct GNUNET_PEERSTORE_WatchContext *wc = value;
402 GNUNET_PEERSTORE_watch_cancel (wc);
408 * Kill the connection to the service. This can be delayed in case of pending
409 * STORE requests and the user explicitly asked to sync first. Otherwise it is
410 * performed instantly.
412 * @param h Handle to the service.
415 do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
419 GNUNET_MQ_destroy (h->mq);
422 if (NULL != h->client)
424 GNUNET_CLIENT_disconnect (h->client);
432 * Connect to the PEERSTORE service.
434 * @return NULL on error
436 struct GNUNET_PEERSTORE_Handle *
437 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
439 struct GNUNET_PEERSTORE_Handle *h;
441 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
443 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
444 if (NULL == h->client)
450 h->disconnecting = GNUNET_NO;
452 GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
453 &handle_client_error, h);
459 LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
465 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
467 * Any pending STORE requests will depend on @e snyc_first flag.
469 * @param h handle to disconnect
470 * @param sync_first send any pending STORE requests before disconnecting
473 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
475 struct GNUNET_PEERSTORE_IterateContext *ic;
476 struct GNUNET_PEERSTORE_IterateContext *ic_iter;
477 struct GNUNET_PEERSTORE_StoreContext *sc;
478 struct GNUNET_PEERSTORE_StoreContext *sc_iter;
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
481 if (NULL != h->watches)
483 GNUNET_CONTAINER_multihashmap_iterate (h->watches,
486 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
489 ic_iter = h->iterate_head;
490 while (NULL != ic_iter)
494 ic_iter = ic_iter->next;
495 GNUNET_PEERSTORE_iterate_cancel (ic);
497 if (NULL != h->store_head)
499 if (GNUNET_YES == sync_first)
501 LOG (GNUNET_ERROR_TYPE_DEBUG,
502 "Delaying disconnection due to pending store requests.\n");
503 h->disconnecting = GNUNET_YES;
506 sc_iter = h->store_head;
507 while (NULL != sc_iter)
510 sc_iter = sc_iter->next;
511 GNUNET_PEERSTORE_store_cancel (sc);
518 /******************************************************************************/
519 /******************* STORE FUNCTIONS *********************/
520 /******************************************************************************/
523 * Callback after MQ envelope is sent
525 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
528 store_request_sent (void *cls)
530 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
531 GNUNET_PEERSTORE_Continuation cont;
536 cont_cls = sc->cont_cls;
537 GNUNET_PEERSTORE_store_cancel (sc);
539 cont (cont_cls, GNUNET_OK);
544 * Cancel a store request
546 * @param sc Store request context
549 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
551 struct GNUNET_PEERSTORE_Handle *h = sc->h;
555 GNUNET_MQ_send_cancel (sc->ev);
558 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
560 if (GNUNET_YES == h->disconnecting && NULL == h->store_head)
566 * Store a new entry in the PEERSTORE.
567 * Note that stored entries can be lost in some cases
568 * such as power failure.
570 * @param h Handle to the PEERSTORE service
571 * @param sub_system name of the sub system
572 * @param peer Peer Identity
573 * @param key entry key
574 * @param value entry value BLOB
575 * @param size size of @e value
576 * @param expiry absolute time after which the entry is (possibly) deleted
577 * @param options options specific to the storage operation
578 * @param cont Continuation function after the store request is sent
579 * @param cont_cls Closure for 'cont'
581 struct GNUNET_PEERSTORE_StoreContext *
582 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
583 const char *sub_system,
584 const struct GNUNET_PeerIdentity *peer, const char *key,
585 const void *value, size_t size,
586 struct GNUNET_TIME_Absolute expiry,
587 enum GNUNET_PEERSTORE_StoreOption options,
588 GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
590 struct GNUNET_MQ_Envelope *ev;
591 struct GNUNET_PEERSTORE_StoreContext *sc;
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
595 size, sub_system, GNUNET_i2s (peer), key);
596 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
598 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
599 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
603 sc->cont_cls = cont_cls;
605 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
606 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
607 GNUNET_MQ_send (h->mq, ev);
613 /******************************************************************************/
614 /******************* ITERATE FUNCTIONS *********************/
615 /******************************************************************************/
618 * When a response for iterate request is received
620 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
621 * @param msg message received, NULL on timeout or fatal error
624 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
626 struct GNUNET_PEERSTORE_Handle *h = cls;
627 struct GNUNET_PEERSTORE_IterateContext *ic;
628 GNUNET_PEERSTORE_Processor callback;
631 struct GNUNET_PEERSTORE_Record *record;
634 ic = h->iterate_head;
637 LOG (GNUNET_ERROR_TYPE_ERROR,
638 _("Unexpected iteration response, this should not happen.\n"));
642 callback = ic->callback;
643 callback_cls = ic->callback_cls;
644 if (NULL == msg) /* Connection error */
647 if (NULL != callback)
648 callback (callback_cls, NULL,
649 _("Error communicating with `PEERSTORE' service."));
653 msg_type = ntohs (msg->type);
654 if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
656 ic->iterating = GNUNET_NO;
657 GNUNET_PEERSTORE_iterate_cancel (ic);
658 if (NULL != callback)
659 callback (callback_cls, NULL, NULL);
662 if (NULL != callback)
664 record = PEERSTORE_parse_record_message (msg);
667 callback (callback_cls, NULL,
668 _("Received a malformed response from service."));
671 continue_iter = callback (callback_cls, record, NULL);
672 PEERSTORE_destroy_record (record);
674 if (GNUNET_NO == continue_iter)
681 * Callback after MQ envelope is sent
683 * @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
686 iterate_request_sent (void *cls)
688 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 "Iterate request sent to service.\n");
692 ic->iterating = GNUNET_YES;
698 * Called when the iterate request is timedout
700 * @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
701 * @param tc Scheduler task context (unused)
704 iterate_timeout (void *cls,
705 const struct GNUNET_SCHEDULER_TaskContext *tc)
707 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
708 GNUNET_PEERSTORE_Processor callback;
711 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
712 callback = ic->callback;
713 callback_cls = ic->callback_cls;
714 GNUNET_PEERSTORE_iterate_cancel (ic);
715 if (NULL != callback)
716 callback (callback_cls,
723 * Cancel an iterate request
724 * Please do not call after the iterate request is done
726 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
729 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
731 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
733 GNUNET_SCHEDULER_cancel (ic->timeout_task);
734 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
736 if (GNUNET_NO == ic->iterating)
740 GNUNET_MQ_send_cancel (ic->ev);
743 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head,
754 * Iterate over records matching supplied key information
756 * @param h handle to the PEERSTORE service
757 * @param sub_system name of sub system
758 * @param peer Peer identity (can be NULL)
759 * @param key entry key string (can be NULL)
760 * @param timeout time after which the iterate request is canceled
761 * @param callback function called with each matching record, all NULL's on end
762 * @param callback_cls closure for @a callback
763 * @return Handle to iteration request
765 struct GNUNET_PEERSTORE_IterateContext *
766 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
767 const char *sub_system,
768 const struct GNUNET_PeerIdentity *peer,
770 struct GNUNET_TIME_Relative timeout,
771 GNUNET_PEERSTORE_Processor callback,
774 struct GNUNET_MQ_Envelope *ev;
775 struct GNUNET_PEERSTORE_IterateContext *ic;
777 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0,
779 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
780 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
782 ic->callback = callback;
783 ic->callback_cls = callback_cls;
786 ic->iterating = GNUNET_NO;
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_notify_sent (ev, &iterate_request_sent, ic);
793 GNUNET_MQ_send (h->mq, ev);
795 GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic);
800 /******************************************************************************/
801 /******************* WATCH FUNCTIONS *********************/
802 /******************************************************************************/
805 * When a watch record is received
807 * @param cls a `struct GNUNET_PEERSTORE_Handle *`
808 * @param msg message received, NULL on timeout or fatal error
811 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
813 struct GNUNET_PEERSTORE_Handle *h = cls;
814 struct GNUNET_PEERSTORE_Record *record;
815 struct GNUNET_HashCode keyhash;
816 struct GNUNET_PEERSTORE_WatchContext *wc;
820 LOG (GNUNET_ERROR_TYPE_ERROR,
821 _("Problem receiving a watch response, no way to determine which request.\n"));
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Received a watch record from service.\n");
827 record = PEERSTORE_parse_record_message (msg);
828 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
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 * Callback after MQ envelope is sent
847 * @param cls a `struct GNUNET_PEERSTORE_WatchContext *`
850 watch_request_sent (void *cls)
852 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
854 wc->request_sent = GNUNET_YES;
860 * Cancel a watch request
862 * @param wc handle to the watch request
865 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
867 struct GNUNET_PEERSTORE_Handle *h = wc->h;
868 struct GNUNET_MQ_Envelope *ev;
869 struct StoreKeyHashMessage *hm;
871 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
872 if (GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
874 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
875 hm->keyhash = wc->keyhash;
876 GNUNET_MQ_send (h->mq, ev);
878 wc->callback_cls = NULL;
882 GNUNET_MQ_send_cancel (wc->ev);
885 GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc);
892 * Request watching a given key
893 * User will be notified with any new values added to key
895 * @param h handle to the PEERSTORE service
896 * @param sub_system name of sub system
897 * @param peer Peer identity
898 * @param key entry key string
899 * @param callback function called with each new value
900 * @param callback_cls closure for @a callback
901 * @return Handle to watch request
903 struct GNUNET_PEERSTORE_WatchContext *
904 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
905 const char *sub_system,
906 const struct GNUNET_PeerIdentity *peer, const char *key,
907 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
909 struct GNUNET_MQ_Envelope *ev;
910 struct StoreKeyHashMessage *hm;
911 struct GNUNET_PEERSTORE_WatchContext *wc;
913 GNUNET_assert (NULL != sub_system);
914 GNUNET_assert (NULL != peer);
915 GNUNET_assert (NULL != key);
916 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
917 PEERSTORE_hash_key (sub_system, peer, key, &hm->keyhash);
918 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
920 wc->callback = callback;
921 wc->callback_cls = callback_cls;
924 wc->request_sent = GNUNET_NO;
925 wc->keyhash = hm->keyhash;
926 if (NULL == h->watches)
927 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
928 GNUNET_CONTAINER_multihashmap_put (h->watches, &wc->keyhash, wc,
929 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
930 LOG (GNUNET_ERROR_TYPE_DEBUG,
931 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
932 sub_system, GNUNET_i2s (peer), key);
933 GNUNET_MQ_notify_sent (ev, &watch_request_sent, wc);
934 GNUNET_MQ_send (h->mq, ev);
938 /* end of peerstore_api.c */