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;
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 * MQ Envelope with store request message
108 struct GNUNET_MQ_Envelope *ev;
111 * Continuation called with service response
113 GNUNET_PEERSTORE_Continuation cont;
123 * Context for a iterate request
125 struct GNUNET_PEERSTORE_IterateContext
130 struct GNUNET_PEERSTORE_IterateContext *next;
135 struct GNUNET_PEERSTORE_IterateContext *prev;
138 * Handle to the PEERSTORE service.
140 struct GNUNET_PEERSTORE_Handle *h;
143 * MQ Envelope with iterate request message
145 struct GNUNET_MQ_Envelope *ev;
148 * Callback with each matching record
150 GNUNET_PEERSTORE_Processor callback;
153 * Closure for 'callback'
158 * #GNUNET_YES / #GNUNET_NO
159 * if sent, cannot be canceled
164 * Task identifier for the function called
165 * on iterate request timeout
167 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
172 * Context for a watch request
174 struct GNUNET_PEERSTORE_WatchContext
179 struct GNUNET_PEERSTORE_WatchContext *next;
184 struct GNUNET_PEERSTORE_WatchContext *prev;
187 * Handle to the PEERSTORE service.
189 struct GNUNET_PEERSTORE_Handle *h;
192 * MQ Envelope with watch request message
194 struct GNUNET_MQ_Envelope *ev;
197 * Callback with each record received
199 GNUNET_PEERSTORE_Processor callback;
202 * Closure for 'callback'
207 * Hash of the combined key
209 struct GNUNET_HashCode keyhash;
212 * #GNUNET_YES / #GNUNET_NO
213 * if sent, cannot be canceled
219 /******************************************************************************/
220 /******************* DECLARATIONS *********************/
221 /******************************************************************************/
224 * When a response for iterate request is received
226 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
227 * @param msg message received, NULL on timeout or fatal error
229 void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
232 * When a watch record is received
234 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
235 * @param msg message received, NULL on timeout or fatal error
237 void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
240 * Close the existing connection to PEERSTORE and reconnect.
242 * @param h handle to the service
245 reconnect (struct GNUNET_PEERSTORE_Handle *h);
248 * Callback after MQ envelope is sent
250 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
252 void watch_request_sent (void *cls);
255 * Callback after MQ envelope is sent
257 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
259 void iterate_request_sent (void *cls);
262 * Callback after MQ envelope is sent
264 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
266 void store_request_sent (void *cls);
269 * MQ message handlers
271 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
272 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
273 {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END, sizeof(struct GNUNET_MessageHeader)},
274 {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
275 GNUNET_MQ_HANDLERS_END
278 /******************************************************************************/
279 /******************* CONNECTION FUNCTIONS *********************/
280 /******************************************************************************/
283 handle_client_error (void *cls, enum GNUNET_MQ_Error error)
285 struct GNUNET_PEERSTORE_Handle *h = cls;
287 LOG(GNUNET_ERROR_TYPE_ERROR, _("Received an error notification from MQ of type: %d\n"), error);
292 * Iterator over previous watches to resend them
294 int rewatch_it(void *cls,
295 const struct GNUNET_HashCode *key,
298 struct GNUNET_PEERSTORE_Handle *h = cls;
299 struct GNUNET_PEERSTORE_WatchContext *wc = value;
300 struct StoreKeyHashMessage *hm;
302 if(GNUNET_YES == wc->request_sent)
303 { /* Envelope gone, create new one. */
304 wc->ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
305 hm->keyhash = wc->keyhash;
306 wc->request_sent = GNUNET_NO;
308 GNUNET_MQ_notify_sent(wc->ev, &watch_request_sent, wc);
309 GNUNET_MQ_send(h->mq, wc->ev);
314 * Close the existing connection to PEERSTORE and reconnect.
316 * @param h handle to the service
319 reconnect (struct GNUNET_PEERSTORE_Handle *h)
321 struct GNUNET_PEERSTORE_IterateContext *ic;
322 GNUNET_PEERSTORE_Processor icb;
324 struct GNUNET_PEERSTORE_StoreContext *sc;
326 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
329 GNUNET_MQ_destroy(h->mq);
332 if (NULL != h->client)
334 GNUNET_CLIENT_disconnect (h->client);
337 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
338 GNUNET_assert(NULL != h->client);
339 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
341 &handle_client_error,
343 LOG(GNUNET_ERROR_TYPE_DEBUG,
344 "Resending pending requests after reconnect.\n");
345 if (NULL != h->watches)
347 GNUNET_CONTAINER_multihashmap_iterate(h->watches,
350 ic = h->iterate_head;
353 if (GNUNET_YES == ic->request_sent)
356 icb_cls = ic->callback_cls;
357 GNUNET_PEERSTORE_iterate_cancel(ic);
359 icb(icb_cls, NULL,_("Iteration canceled due to reconnection."));
363 GNUNET_MQ_notify_sent(ic->ev, &iterate_request_sent, ic);
364 GNUNET_MQ_send(h->mq, ic->ev);
371 GNUNET_MQ_notify_sent(sc->ev, &store_request_sent, sc);
372 GNUNET_MQ_send(h->mq, sc->ev);
378 * Connect to the PEERSTORE service.
380 * @return NULL on error
382 struct GNUNET_PEERSTORE_Handle *
383 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
385 struct GNUNET_PEERSTORE_Handle *h;
387 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
388 h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
389 if(NULL == h->client)
395 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
397 &handle_client_error,
404 LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
409 * Disconnect from the PEERSTORE service
410 * Do not call in case of pending requests
412 * @param h handle to disconnect
415 GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
417 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
418 if(NULL != h->watches)
420 GNUNET_CONTAINER_multihashmap_destroy(h->watches);
425 GNUNET_MQ_destroy(h->mq);
428 if (NULL != h->client)
430 GNUNET_CLIENT_disconnect (h->client);
434 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnected, BYE!\n");
438 /******************************************************************************/
439 /******************* STORE FUNCTIONS *********************/
440 /******************************************************************************/
443 * Callback after MQ envelope is sent
445 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
447 void store_request_sent (void *cls)
449 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
450 GNUNET_PEERSTORE_Continuation cont;
455 cont_cls = sc->cont_cls;
456 GNUNET_PEERSTORE_store_cancel(sc);
458 cont(cont_cls, GNUNET_OK);
462 * Cancel a store request
464 * @param sc Store request context
467 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
471 GNUNET_MQ_send_cancel(sc->ev);
474 GNUNET_CONTAINER_DLL_remove(sc->h->store_head, sc->h->store_tail, sc);
479 * Store a new entry in the PEERSTORE.
480 * Note that stored entries can be lost in some cases
481 * such as power failure.
483 * @param h Handle to the PEERSTORE service
484 * @param sub_system name of the sub system
485 * @param peer Peer Identity
486 * @param key entry key
487 * @param value entry value BLOB
488 * @param size size of 'value'
489 * @param expiry absolute time after which the entry is (possibly) deleted
490 * @param cont Continuation function after the store request is processed
491 * @param cont_cls Closure for 'cont'
493 struct GNUNET_PEERSTORE_StoreContext *
494 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
495 const char *sub_system,
496 const struct GNUNET_PeerIdentity *peer,
500 struct GNUNET_TIME_Absolute expiry,
501 enum GNUNET_PEERSTORE_StoreOption options,
502 GNUNET_PEERSTORE_Continuation cont,
505 struct GNUNET_MQ_Envelope *ev;
506 struct GNUNET_PEERSTORE_StoreContext *sc;
508 LOG (GNUNET_ERROR_TYPE_DEBUG,
509 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
510 size, sub_system, GNUNET_i2s (peer), key);
511 ev = PEERSTORE_create_record_mq_envelope(sub_system,
518 GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
519 sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
522 sc->cont_cls = cont_cls;
524 GNUNET_CONTAINER_DLL_insert_tail(h->store_head, h->store_tail, sc);
525 GNUNET_MQ_notify_sent(ev, &store_request_sent, sc);
526 GNUNET_MQ_send(h->mq, ev);
531 /******************************************************************************/
532 /******************* ITERATE FUNCTIONS *********************/
533 /******************************************************************************/
536 * When a response for iterate request is received
538 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
539 * @param msg message received, NULL on timeout or fatal error
541 void handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
543 struct GNUNET_PEERSTORE_Handle *h = cls;
544 struct GNUNET_PEERSTORE_IterateContext *ic;
545 GNUNET_PEERSTORE_Processor callback;
548 struct GNUNET_PEERSTORE_Record *record;
551 ic = h->iterate_head;
554 LOG(GNUNET_ERROR_TYPE_ERROR, _("Unexpected iteration response, this should not happen.\n"));
558 callback = ic->callback;
559 callback_cls = ic->callback_cls;
560 if(NULL == msg) /* Connection error */
564 callback(callback_cls, NULL,
565 _("Error communicating with `PEERSTORE' service."));
569 msg_type = ntohs(msg->type);
570 if(GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
572 ic->request_sent = GNUNET_NO;
573 GNUNET_PEERSTORE_iterate_cancel(ic);
575 callback(callback_cls, NULL, NULL);
580 record = PEERSTORE_parse_record_message(msg);
582 continue_iter = callback(callback_cls, NULL, _("Received a malformed response from service."));
585 continue_iter = callback(callback_cls, record, NULL);
586 PEERSTORE_destroy_record(record);
588 if(GNUNET_NO == continue_iter)
595 * Callback after MQ envelope is sent
597 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
599 void iterate_request_sent (void *cls)
601 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
603 LOG(GNUNET_ERROR_TYPE_DEBUG, "Iterate request sent to service.\n");
604 ic->request_sent = GNUNET_YES;
609 * Called when the iterate request is timedout
611 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
613 void iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
615 struct GNUNET_PEERSTORE_IterateContext *ic = cls;
617 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
618 GNUNET_PEERSTORE_iterate_cancel(ic);
622 * Cancel an iterate request
623 * Please do not call after the iterate request is done
625 * @param ic Iterate request context as returned by GNUNET_PEERSTORE_iterate()
628 GNUNET_PEERSTORE_iterate_cancel (struct GNUNET_PEERSTORE_IterateContext *ic)
630 if(GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
632 GNUNET_SCHEDULER_cancel(ic->timeout_task);
633 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
635 if(GNUNET_NO == ic->request_sent)
639 GNUNET_MQ_send_cancel(ic->ev);
642 GNUNET_CONTAINER_DLL_remove(ic->h->iterate_head, ic->h->iterate_tail, ic);
650 * Iterate over records matching supplied key information
652 * @param h handle to the PEERSTORE service
653 * @param sub_system name of sub system
654 * @param peer Peer identity (can be NULL)
655 * @param key entry key string (can be NULL)
656 * @param timeout time after which the iterate request is canceled
657 * @param callback function called with each matching record, all NULL's on end
658 * @param callback_cls closure for @a callback
659 * @return Handle to iteration request
661 struct GNUNET_PEERSTORE_IterateContext *
662 GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
663 const char *sub_system,
664 const struct GNUNET_PeerIdentity *peer,
666 struct GNUNET_TIME_Relative timeout,
667 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
669 struct GNUNET_MQ_Envelope *ev;
670 struct GNUNET_PEERSTORE_IterateContext *ic;
672 ev = PEERSTORE_create_record_mq_envelope(sub_system,
679 GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
680 ic = GNUNET_new(struct GNUNET_PEERSTORE_IterateContext);
681 ic->callback = callback;
682 ic->callback_cls = callback_cls;
685 ic->request_sent = GNUNET_NO;
686 GNUNET_CONTAINER_DLL_insert_tail(h->iterate_head, h->iterate_tail, ic);
687 LOG(GNUNET_ERROR_TYPE_DEBUG,
688 "Sending an iterate request for sub system `%s'\n", sub_system);
689 GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic);
690 GNUNET_MQ_send(h->mq, ev);
691 ic->timeout_task = GNUNET_SCHEDULER_add_delayed(timeout, &iterate_timeout, ic);
695 /******************************************************************************/
696 /******************* WATCH FUNCTIONS *********************/
697 /******************************************************************************/
700 * When a watch record is received
702 * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
703 * @param msg message received, NULL on timeout or fatal error
705 void handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
707 struct GNUNET_PEERSTORE_Handle *h = cls;
708 struct GNUNET_PEERSTORE_Record *record;
709 struct GNUNET_HashCode keyhash;
710 struct GNUNET_PEERSTORE_WatchContext *wc;
714 LOG(GNUNET_ERROR_TYPE_ERROR,
715 "Problem receiving a watch response, no way to determine which request.\n");
719 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
720 record = PEERSTORE_parse_record_message(msg);
721 PEERSTORE_hash_key(record->sub_system,
722 record->peer, record->key, &keyhash);
723 wc = GNUNET_CONTAINER_multihashmap_get(h->watches, &keyhash);
724 if(NULL != wc->callback)
725 wc->callback(wc->callback_cls, record, NULL);
726 PEERSTORE_destroy_record(record);
730 * Callback after MQ envelope is sent
732 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
734 void watch_request_sent (void *cls)
736 struct GNUNET_PEERSTORE_WatchContext *wc = cls;
738 wc->request_sent = GNUNET_YES;
743 * Cancel a watch request
745 * @wc handle to the watch request
748 GNUNET_PEERSTORE_watch_cancel(struct GNUNET_PEERSTORE_WatchContext *wc)
750 struct GNUNET_PEERSTORE_Handle *h = wc->h;
751 struct GNUNET_MQ_Envelope *ev;
752 struct StoreKeyHashMessage *hm;
754 LOG(GNUNET_ERROR_TYPE_DEBUG, "Canceling watch.\n");
755 if(GNUNET_YES == wc->request_sent) /* If request already sent to service, send a cancel request. */
757 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
758 GNUNET_MQ_send(h->mq, ev);
760 wc->callback_cls = NULL;
764 GNUNET_MQ_send_cancel(wc->ev);
767 GNUNET_CONTAINER_multihashmap_remove(h->watches, &wc->keyhash, wc);
773 * Request watching a given key
774 * User will be notified with any new values added to key
776 * @param h handle to the PEERSTORE service
777 * @param sub_system name of sub system
778 * @param peer Peer identity
779 * @param key entry key string
780 * @param callback function called with each new value
781 * @param callback_cls closure for @a callback
782 * @return Handle to watch request
784 struct GNUNET_PEERSTORE_WatchContext *
785 GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
786 const char *sub_system,
787 const struct GNUNET_PeerIdentity *peer,
789 GNUNET_PEERSTORE_Processor callback, void *callback_cls)
791 struct GNUNET_MQ_Envelope *ev;
792 struct StoreKeyHashMessage *hm;
793 struct GNUNET_PEERSTORE_WatchContext *wc;
795 ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
796 PEERSTORE_hash_key(sub_system, peer, key, &hm->keyhash);
797 wc = GNUNET_new(struct GNUNET_PEERSTORE_WatchContext);
798 wc->callback = callback;
799 wc->callback_cls = callback_cls;
802 wc->request_sent = GNUNET_NO;
803 wc->keyhash = hm->keyhash;
804 if(NULL == h->watches)
805 h->watches = GNUNET_CONTAINER_multihashmap_create(5, GNUNET_NO);
806 GNUNET_CONTAINER_multihashmap_put(h->watches, &wc->keyhash,
807 wc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
808 LOG(GNUNET_ERROR_TYPE_DEBUG,
809 "Sending a watch request for ss `%s', peer `%s', key `%s'.\n",
810 sub_system, GNUNET_i2s(peer), key);
811 GNUNET_MQ_notify_sent(ev, &watch_request_sent, wc);
812 GNUNET_MQ_send(h->mq, ev);
816 /* end of peerstore_api.c */