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 * if sent, cannot be canceled
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 *'
259 static void watch_request_sent (void *cls);
262 * Callback after MQ envelope is sent
264 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
266 static void iterate_request_sent (void *cls);
269 * Callback after MQ envelope is sent
271 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
273 static void store_request_sent (void *cls);
276 * MQ message handlers
278 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
279 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
280 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)},
281 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
282 GNUNET_MQ_HANDLERS_END
285 /******************************************************************************/
286 /******************* CONNECTION FUNCTIONS *********************/
287 /******************************************************************************/
290 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
292 struct GNUNET_PEERSTORE_Handle *h = cls;
294 LOG(GNUNET_ERROR_TYPE_ERROR, _("Received an error notification from MQ of type: %d\n"), error);
299 * Iterator over previous watches to resend them
301 static int rewatch_it(void *cls,
302 const struct GNUNET_HashCode *key,
305 struct GNUNET_PEERSTORE_Handle *h = cls;
306 struct GNUNET_PEERSTORE_WatchContext *wc = value;
307 struct StoreKeyHashMessage *hm;
309 if(GNUNET_YES == wc->request_sent)
310 { /* Envelope gone, create new one. */
311 wc->ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
312 hm->keyhash = wc->keyhash;
313 wc->request_sent = GNUNET_NO;
315 GNUNET_MQ_notify_sent(wc->ev, &watch_request_sent, wc);
316 GNUNET_MQ_send(h->mq, wc->ev);
321 * Close the existing connection to PEERSTORE and reconnect.
323 * @param h handle to the service
326 reconnect (struct GNUNET_PEERSTORE_Handle *h)
328 struct GNUNET_PEERSTORE_IterateContext *ic;
329 GNUNET_PEERSTORE_Processor icb;
331 struct GNUNET_PEERSTORE_StoreContext *sc;
333 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
336 GNUNET_MQ_destroy(h->mq);
339 if (NULL != h->client)
341 GNUNET_CLIENT_disconnect (h->client);
344 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
345 GNUNET_assert(NULL != h->client);
346 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
348 &handle_client_error,
350 LOG(GNUNET_ERROR_TYPE_DEBUG,
351 "Resending pending requests after reconnect.\n");
352 if (NULL != h->watches)
354 GNUNET_CONTAINER_multihashmap_iterate(h->watches,
357 ic = h->iterate_head;
360 if (GNUNET_YES == ic->request_sent)
363 icb_cls = ic->callback_cls;
364 GNUNET_PEERSTORE_iterate_cancel(ic);
366 icb(icb_cls, NULL,_("Iteration canceled due to reconnection."));
370 GNUNET_MQ_notify_sent(ic->ev, &iterate_request_sent, ic);
371 GNUNET_MQ_send(h->mq, ic->ev);
378 GNUNET_MQ_notify_sent(sc->ev, &store_request_sent, sc);
379 GNUNET_MQ_send(h->mq, sc->ev);
385 * Iterator over watch requests to cancel them.
388 * @param key key to the watch request
389 * @param value watch context
390 * @return #GNUNET_YES to continue iteration
393 destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
395 struct GNUNET_PEERSTORE_WatchContext *wc = value;
397 GNUNET_PEERSTORE_watch_cancel (wc);
402 * Kill the connection to the service. This can be delayed in case of pending
403 * STORE requests and the user explicitly asked to sync first. Otherwise it is
404 * performed instantly.
406 * @param h Handle to the service.
408 static void do_disconnect (struct GNUNET_PEERSTORE_Handle *h)
412 GNUNET_MQ_destroy(h->mq);
415 if (NULL != h->client)
417 GNUNET_CLIENT_disconnect (h->client);
424 * Connect to the PEERSTORE service.
426 * @return NULL on error
428 struct GNUNET_PEERSTORE_Handle *
429 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
431 struct GNUNET_PEERSTORE_Handle *h;
433 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
434 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
435 if(NULL == h->client)
441 h->disconnecting = GNUNET_NO;
442 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
444 &handle_client_error,
451 LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
456 * Disconnect from the PEERSTORE service. Any pending ITERATE and WATCH requests
457 * will be canceled. Any pending STORE requests will depend on @snyc_first flag.
459 * @param h handle to disconnect
460 * @param sync_first send any pending STORE requests before disconnecting
463 GNUNET_PEERSTORE_disconnect (struct GNUNET_PEERSTORE_Handle *h, int sync_first)
465 struct GNUNET_PEERSTORE_IterateContext *ic;
466 struct GNUNET_PEERSTORE_IterateContext *ic_iter;
467 struct GNUNET_PEERSTORE_StoreContext *sc;
468 struct GNUNET_PEERSTORE_StoreContext *sc_iter;
470 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
471 if(NULL != h->watches)
473 GNUNET_CONTAINER_multihashmap_iterate (h->watches, &destroy_watch, NULL);
474 GNUNET_CONTAINER_multihashmap_destroy(h->watches);
477 ic_iter = h->iterate_head;
478 while (NULL != ic_iter)
481 ic_iter = ic_iter->next;
482 GNUNET_PEERSTORE_iterate_cancel (ic);
484 if (NULL != h->store_head)
486 if (GNUNET_YES == sync_first)
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "Delaying disconnection due to pending store requests.\n");
490 h->disconnecting = GNUNET_YES;
493 sc_iter = h->store_head;
494 while (NULL != sc_iter)
497 sc_iter = sc_iter->next;
498 GNUNET_PEERSTORE_store_cancel (sc);
505 /******************************************************************************/
506 /******************* STORE FUNCTIONS *********************/
507 /******************************************************************************/
510 * Callback after MQ envelope is sent
512 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
514 static void store_request_sent (void *cls)
516 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
517 GNUNET_PEERSTORE_Continuation cont;
522 cont_cls = sc->cont_cls;
523 GNUNET_PEERSTORE_store_cancel(sc);
525 cont(cont_cls, GNUNET_OK);
529 * Cancel a store request
531 * @param sc Store request context
534 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
536 struct GNUNET_PEERSTORE_Handle *h = sc->h;
540 GNUNET_MQ_send_cancel(sc->ev);
543 GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc);
545 if (GNUNET_YES == h->disconnecting && NULL == h->store_head)
550 * Store a new entry in the PEERSTORE.
551 * Note that stored entries can be lost in some cases
552 * such as power failure.
554 * @param h Handle to the PEERSTORE service
555 * @param sub_system name of the sub system
556 * @param peer Peer Identity
557 * @param key entry key
558 * @param value entry value BLOB
559 * @param size size of 'value'
560 * @param expiry absolute time after which the entry is (possibly) deleted
561 * @param cont Continuation function after the store request is sent
562 * @param cont_cls Closure for 'cont'
564 struct GNUNET_PEERSTORE_StoreContext *
565 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
566 const char *sub_system,
567 const struct GNUNET_PeerIdentity *peer,
571 struct GNUNET_TIME_Absolute expiry,
572 enum GNUNET_PEERSTORE_StoreOption options,
573 GNUNET_PEERSTORE_Continuation cont,
576 struct GNUNET_MQ_Envelope *ev;
577 struct GNUNET_PEERSTORE_StoreContext *sc;
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
581 size, sub_system, GNUNET_i2s (peer), key);
582 ev = PEERSTORE_create_record_mq_envelope(sub_system,
589 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
590 sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
593 sc->cont_cls = cont_cls;
595 GNUNET_CONTAINER_DLL_insert_tail(h->store_head, h->store_tail, sc);
596 GNUNET_MQ_notify_sent(ev, &store_request_sent, sc);
597 GNUNET_MQ_send(h->mq, ev);
602 /******************************************************************************/
603 /******************* ITERATE FUNCTIONS *********************/
604 /******************************************************************************/
607 * When a response for iterate request is received
609 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
610 * @param msg message received, NULL on timeout or fatal error
613 handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
615 struct GNUNET_PEERSTORE_Handle *h = cls;
616 struct GNUNET_PEERSTORE_IterateContext *ic;
617 GNUNET_PEERSTORE_Processor callback;
620 struct GNUNET_PEERSTORE_Record *record;
623 ic = h->iterate_head;
626 LOG(GNUNET_ERROR_TYPE_ERROR, _("Unexpected iteration response, this should not happen.\n"));
630 callback = ic->callback;
631 callback_cls = ic->callback_cls;
632 if(NULL == msg) /* Connection error */
636 callback(callback_cls, NULL,
637 _("Error communicating with `PEERSTORE' service."));
641 msg_type = ntohs(msg->type);
642 if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
644 ic->request_sent = GNUNET_NO;
645 GNUNET_PEERSTORE_iterate_cancel(ic);
647 callback(callback_cls, NULL, NULL);
652 record = PEERSTORE_parse_record_message(msg);
654 continue_iter = callback(callback_cls, NULL, _("Received a malformed response from service."));
657 continue_iter = callback(callback_cls, record, NULL);
658 PEERSTORE_destroy_record(record);
660 if(GNUNET_NO == continue_iter)
667 * Callback after MQ envelope is sent
669 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
671 static void iterate_request_sent (void *cls)
673 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
675 LOG(GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
676 ic->request_sent = GNUNET_YES;
681 * Called when the iterate request is timedout
683 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
686 iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
688 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
690 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
691 GNUNET_PEERSTORE_iterate_cancel(ic);
695 * Cancel an iterate request
696 * Please do not call after the iterate request is done
698 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
701 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
703 if(GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
705 GNUNET_SCHEDULER_cancel(ic->timeout_task);
706 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
708 if(GNUNET_NO == ic->request_sent)
712 GNUNET_MQ_send_cancel(ic->ev);
715 GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic);
723 * Iterate over records matching supplied key information
725 * @param h handle to the PEERSTORE service
726 * @param sub_system name of sub system
727 * @param peer Peer identity (can be NULL)
728 * @param key entry key string (can be NULL)
729 * @param timeout time after which the iterate request is canceled
730 * @param callback function called with each matching record, all NULL's on end
731 * @param callback_cls closure for @a callback
732 * @return Handle to iteration request
734 struct GNUNET_PEERSTORE_IterateContext *
735 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
736 const char *sub_system,
737 const struct GNUNET_PeerIdentity *peer,
739 struct GNUNET_TIME_Relative timeout,
740 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
742 struct GNUNET_MQ_Envelope *ev;
743 struct GNUNET_PEERSTORE_IterateContext *ic;
745 ev = PEERSTORE_create_record_mq_envelope(sub_system,
752 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
753 ic = GNUNET_new(struct GNUNET_PEERSTORE_IterateContext);
754 ic->callback = callback;
755 ic->callback_cls = callback_cls;
758 ic->request_sent = GNUNET_NO;
759 GNUNET_CONTAINER_DLL_insert_tail(h->iterate_head, h->iterate_tail, ic);
760 LOG(GNUNET_ERROR_TYPE_DEBUG,
761 "Sending an iterate request for sub system `%s'\n", sub_system);
762 GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic);
763 GNUNET_MQ_send(h->mq, ev);
764 ic->timeout_task = GNUNET_SCHEDULER_add_delayed(timeout, &iterate_timeout, ic);
768 /******************************************************************************/
769 /******************* WATCH FUNCTIONS *********************/
770 /******************************************************************************/
773 * When a watch record is received
775 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
776 * @param msg message received, NULL on timeout or fatal error
779 handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
781 struct GNUNET_PEERSTORE_Handle *h = cls;
782 struct GNUNET_PEERSTORE_Record *record;
783 struct GNUNET_HashCode keyhash;
784 struct GNUNET_PEERSTORE_WatchContext *wc;
788 LOG(GNUNET_ERROR_TYPE_ERROR,
789 _("Problem receiving a watch response, no way to determine which request.\n"));
793 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
794 record = PEERSTORE_parse_record_message(msg);
795 PEERSTORE_hash_key(record->sub_system,
796 record->peer, record->key, &keyhash);
797 wc = GNUNET_CONTAINER_multihashmap_get(h->watches, &keyhash);
800 LOG(GNUNET_ERROR_TYPE_ERROR,
801 _("Received a watch result for a non existing watch.\n"));
805 if(NULL != wc->callback)
806 wc->callback(wc->callback_cls, record, NULL);
807 PEERSTORE_destroy_record(record);
811 * Callback after MQ envelope is sent
813 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
815 static void watch_request_sent (void *cls)
817 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
819 wc->request_sent = GNUNET_YES;
824 * Cancel a watch request
826 * @wc handle to the watch request
829 GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc)
831 struct GNUNET_PEERSTORE_Handle *h = wc->h;
832 struct GNUNET_MQ_Envelope *ev;
833 struct StoreKeyHashMessage *hm;
835 LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
836 if(GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
838 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
839 hm->keyhash = wc->keyhash;
840 GNUNET_MQ_send(h->mq, ev);
842 wc->callback_cls = NULL;
846 GNUNET_MQ_send_cancel(wc->ev);
849 GNUNET_CONTAINER_multihashmap_remove(h->watches, &wc->keyhash, wc);
855 * Request watching a given key
856 * User will be notified with any new values added to key
858 * @param h handle to the PEERSTORE service
859 * @param sub_system name of sub system
860 * @param peer Peer identity
861 * @param key entry key string
862 * @param callback function called with each new value
863 * @param callback_cls closure for @a callback
864 * @return Handle to watch request
866 struct GNUNET_PEERSTORE_WatchContext *
867 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
868 const char *sub_system,
869 const struct GNUNET_PeerIdentity *peer,
871 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
873 struct GNUNET_MQ_Envelope *ev;
874 struct StoreKeyHashMessage *hm;
875 struct GNUNET_PEERSTORE_WatchContext *wc;
877 GNUNET_assert(NULL != sub_system);
878 GNUNET_assert(NULL != peer);
879 GNUNET_assert(NULL != key);
880 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
881 PEERSTORE_hash_key(sub_system, peer, key, &hm->keyhash);
882 wc = GNUNET_new(struct GNUNET_PEERSTORE_WatchContext);
883 wc->callback = callback;
884 wc->callback_cls = callback_cls;
887 wc->request_sent = GNUNET_NO;
888 wc->keyhash = hm->keyhash;
889 if(NULL == h->watches)
890 h->watches = GNUNET_CONTAINER_multihashmap_create(5, GNUNET_NO);
891 GNUNET_CONTAINER_multihashmap_put(h->watches, &wc->keyhash,
892 wc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
893 LOG(GNUNET_ERROR_TYPE_DEBUG,
894 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
895 sub_system, GNUNET_i2s(peer), key);
896 GNUNET_MQ_notify_sent(ev, &watch_request_sent, wc);
897 GNUNET_MQ_send(h->mq, ev);
901 /* end of peerstore_api.c */