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, &destroy_watch, NULL);
484 GNUNET_CONTAINER_multihashmap_destroy (h->watches);
487 ic_iter = h->iterate_head;
488 while (NULL != ic_iter)
491 ic_iter = ic_iter->next;
492 GNUNET_PEERSTORE_iterate_cancel (ic);
494 if (NULL != h->store_head)
496 if (GNUNET_YES == sync_first)
498 LOG (GNUNET_ERROR_TYPE_DEBUG,
499 "Delaying disconnection due to pending store requests.\n");
500 h->disconnecting = GNUNET_YES;
503 sc_iter = h->store_head;
504 while (NULL != sc_iter)
507 sc_iter = sc_iter->next;
508 GNUNET_PEERSTORE_store_cancel (sc);
515 /******************************************************************************/
516 /******************* STORE FUNCTIONS *********************/
517 /******************************************************************************/
520 * Callback after MQ envelope is sent
522 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
525 store_request_sent (void *cls)
527 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
528 GNUNET_PEERSTORE_Continuation cont;
533 cont_cls = sc->cont_cls;
534 GNUNET_PEERSTORE_store_cancel (sc);
536 cont (cont_cls, GNUNET_OK);
541 * Cancel a store request
543 * @param sc Store request context
546 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
548 struct GNUNET_PEERSTORE_Handle *h = sc->h;
552 GNUNET_MQ_send_cancel (sc->ev);
555 GNUNET_CONTAINER_DLL_remove (sc->h->store_head, sc->h->store_tail, sc);
557 if (GNUNET_YES == h->disconnecting && NULL == h->store_head)
563 * Store a new entry in the PEERSTORE.
564 * Note that stored entries can be lost in some cases
565 * such as power failure.
567 * @param h Handle to the PEERSTORE service
568 * @param sub_system name of the sub system
569 * @param peer Peer Identity
570 * @param key entry key
571 * @param value entry value BLOB
572 * @param size size of @e value
573 * @param expiry absolute time after which the entry is (possibly) deleted
574 * @param options options specific to the storage operation
575 * @param cont Continuation function after the store request is sent
576 * @param cont_cls Closure for 'cont'
578 struct GNUNET_PEERSTORE_StoreContext *
579 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
580 const char *sub_system,
581 const struct GNUNET_PeerIdentity *peer, const char *key,
582 const void *value, size_t size,
583 struct GNUNET_TIME_Absolute expiry,
584 enum GNUNET_PEERSTORE_StoreOption options,
585 GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
587 struct GNUNET_MQ_Envelope *ev;
588 struct GNUNET_PEERSTORE_StoreContext *sc;
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
592 size, sub_system, GNUNET_i2s (peer), key);
593 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, value, size,
595 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
596 sc = GNUNET_new (struct GNUNET_PEERSTORE_StoreContext);
600 sc->cont_cls = cont_cls;
602 GNUNET_CONTAINER_DLL_insert_tail (h->store_head, h->store_tail, sc);
603 GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
604 GNUNET_MQ_send (h->mq, ev);
610 /******************************************************************************/
611 /******************* ITERATE FUNCTIONS *********************/
612 /******************************************************************************/
615 * When a response for iterate request is received
617 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
618 * @param msg message received, NULL on timeout or fatal error
621 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
623 struct GNUNET_PEERSTORE_Handle *h = cls;
624 struct GNUNET_PEERSTORE_IterateContext *ic;
625 GNUNET_PEERSTORE_Processor callback;
628 struct GNUNET_PEERSTORE_Record *record;
631 ic = h->iterate_head;
634 LOG (GNUNET_ERROR_TYPE_ERROR,
635 _("Unexpected iteration response, this should not happen.\n"));
639 callback = ic->callback;
640 callback_cls = ic->callback_cls;
641 if (NULL == msg) /* Connection error */
644 if (NULL != callback)
645 callback (callback_cls, NULL,
646 _("Error communicating with `PEERSTORE' service."));
650 msg_type = ntohs (msg->type);
651 if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
653 ic->iterating = GNUNET_NO;
654 GNUNET_PEERSTORE_iterate_cancel (ic);
655 if (NULL != callback)
656 callback (callback_cls, NULL, NULL);
659 if (NULL != callback)
661 record = PEERSTORE_parse_record_message (msg);
664 callback (callback_cls, NULL,
665 _("Received a malformed response from service."));
668 continue_iter = callback (callback_cls, record, NULL);
669 PEERSTORE_destroy_record (record);
671 if (GNUNET_NO == continue_iter)
678 * Callback after MQ envelope is sent
680 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
683 iterate_request_sent (void *cls)
685 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
687 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
688 ic->iterating = GNUNET_YES;
694 * Called when the iterate request is timedout
696 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
697 * @param tc Scheduler task context (unused)
700 iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
702 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
704 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
705 GNUNET_PEERSTORE_iterate_cancel (ic);
710 * Cancel an iterate request
711 * Please do not call after the iterate request is done
713 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
716 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
718 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
720 GNUNET_SCHEDULER_cancel (ic->timeout_task);
721 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
723 if (GNUNET_NO == ic->iterating)
727 GNUNET_MQ_send_cancel (ic->ev);
730 GNUNET_CONTAINER_DLL_remove (ic->h->iterate_head, ic->h->iterate_tail, ic);
739 * Iterate over records matching supplied key information
741 * @param h handle to the PEERSTORE service
742 * @param sub_system name of sub system
743 * @param peer Peer identity (can be NULL)
744 * @param key entry key string (can be NULL)
745 * @param timeout time after which the iterate request is canceled
746 * @param callback function called with each matching record, all NULL's on end
747 * @param callback_cls closure for @a callback
748 * @return Handle to iteration request
750 struct GNUNET_PEERSTORE_IterateContext *
751 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
752 const char *sub_system,
753 const struct GNUNET_PeerIdentity *peer,
754 const char *key, struct GNUNET_TIME_Relative timeout,
755 GNUNET_PEERSTORE_Processor callback,
758 struct GNUNET_MQ_Envelope *ev;
759 struct GNUNET_PEERSTORE_IterateContext *ic;
761 ev = PEERSTORE_create_record_mq_envelope (sub_system, peer, key, NULL, 0,
763 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
764 ic = GNUNET_new (struct GNUNET_PEERSTORE_IterateContext);
766 ic->callback = callback;
767 ic->callback_cls = callback_cls;
770 ic->iterating = GNUNET_NO;
771 GNUNET_CONTAINER_DLL_insert_tail (h->iterate_head, h->iterate_tail, ic);
772 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "Sending an iterate request for sub system `%s'\n", sub_system);
774 GNUNET_MQ_notify_sent (ev, &iterate_request_sent, ic);
775 GNUNET_MQ_send (h->mq, ev);
777 GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic);
782 /******************************************************************************/
783 /******************* WATCH FUNCTIONS *********************/
784 /******************************************************************************/
787 * When a watch record is received
789 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
790 * @param msg message received, NULL on timeout or fatal error
793 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
795 struct GNUNET_PEERSTORE_Handle *h = cls;
796 struct GNUNET_PEERSTORE_Record *record;
797 struct GNUNET_HashCode keyhash;
798 struct GNUNET_PEERSTORE_WatchContext *wc;
802 LOG (GNUNET_ERROR_TYPE_ERROR,
804 ("Problem receiving a watch response, no way to determine which request.\n"));
808 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
809 record = PEERSTORE_parse_record_message (msg);
810 PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
811 wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash);
814 LOG (GNUNET_ERROR_TYPE_ERROR,
815 _("Received a watch result for a non existing watch.\n"));
819 if (NULL != wc->callback)
820 wc->callback (wc->callback_cls, record, NULL);
821 PEERSTORE_destroy_record (record);
826 * Callback after MQ envelope is sent
828 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
831 watch_request_sent (void *cls)
833 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
835 wc->request_sent = GNUNET_YES;
841 * Cancel a watch request
843 * @param wc handle to the watch request
846 GNUNET_PEERSTORE_watch_cancel (struct GNUNET_PEERSTORE_WatchContext *wc)
848 struct GNUNET_PEERSTORE_Handle *h = wc->h;
849 struct GNUNET_MQ_Envelope *ev;
850 struct StoreKeyHashMessage *hm;
852 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
853 if (GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
855 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
856 hm->keyhash = wc->keyhash;
857 GNUNET_MQ_send (h->mq, ev);
859 wc->callback_cls = NULL;
863 GNUNET_MQ_send_cancel (wc->ev);
866 GNUNET_CONTAINER_multihashmap_remove (h->watches, &wc->keyhash, wc);
873 * Request watching a given key
874 * User will be notified with any new values added to key
876 * @param h handle to the PEERSTORE service
877 * @param sub_system name of sub system
878 * @param peer Peer identity
879 * @param key entry key string
880 * @param callback function called with each new value
881 * @param callback_cls closure for @a callback
882 * @return Handle to watch request
884 struct GNUNET_PEERSTORE_WatchContext *
885 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
886 const char *sub_system,
887 const struct GNUNET_PeerIdentity *peer, const char *key,
888 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
890 struct GNUNET_MQ_Envelope *ev;
891 struct StoreKeyHashMessage *hm;
892 struct GNUNET_PEERSTORE_WatchContext *wc;
894 GNUNET_assert (NULL != sub_system);
895 GNUNET_assert (NULL != peer);
896 GNUNET_assert (NULL != key);
897 ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
898 PEERSTORE_hash_key (sub_system, peer, key, &hm->keyhash);
899 wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext);
901 wc->callback = callback;
902 wc->callback_cls = callback_cls;
905 wc->request_sent = GNUNET_NO;
906 wc->keyhash = hm->keyhash;
907 if (NULL == h->watches)
908 h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
909 GNUNET_CONTAINER_multihashmap_put (h->watches, &wc->keyhash, wc,
910 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
911 LOG (GNUNET_ERROR_TYPE_DEBUG,
912 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
913 sub_system, GNUNET_i2s (peer), key);
914 GNUNET_MQ_notify_sent (ev, &watch_request_sent, wc);
915 GNUNET_MQ_send (h->mq, ev);
919 /* end of peerstore_api.c */