/*
This file is part of GNUnet.
- Copyright (C) 2013-2014 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2013-2016 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
* @file peerstore/peerstore_api.c
*/
const struct GNUNET_CONFIGURATION_Handle *cfg;
- /**
- * Connection to the service.
- */
- struct GNUNET_CLIENT_Connection *client;
-
/**
* Message queue
*/
* Task identifier for the function called
* on iterate request timeout
*/
- struct GNUNET_SCHEDULER_Task * timeout_task;
+ struct GNUNET_SCHEDULER_Task *timeout_task;
};
/******************* DECLARATIONS *********************/
/******************************************************************************/
-/**
- * When a response for iterate request is received
- *
- * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
- * @param msg message received, NULL on timeout or fatal error
- */
-static void
-handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg);
-
-/**
- * When a watch record is received
- *
- * @param cls a 'struct GNUNET_PEERSTORE_Handle *'
- * @param msg message received, NULL on timeout or fatal error
- */
-static void
-handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg);
-
/**
* Close the existing connection to PEERSTORE and reconnect.
*
}
-/**
- * MQ message handlers
- */
-static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
- {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD, 0},
- {&handle_iterate_result, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
- sizeof (struct GNUNET_MessageHeader)},
- {&handle_watch_result, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD, 0},
- GNUNET_MQ_HANDLERS_END
-};
-
/******************************************************************************/
/******************* CONNECTION FUNCTIONS *********************/
/******************************************************************************/
static void
-handle_client_error (void *cls, enum GNUNET_MQ_Error error)
+handle_client_error (void *cls,
+ enum GNUNET_MQ_Error error)
{
struct GNUNET_PEERSTORE_Handle *h = cls;
LOG (GNUNET_ERROR_TYPE_ERROR,
- _("Received an error notification from MQ of type: %d\n"), error);
+ _("Received an error notification from MQ of type: %d\n"),
+ error);
reconnect (h);
}
* @return #GNUNET_YES (continue to iterate)
*/
static int
-rewatch_it (void *cls, const struct GNUNET_HashCode *key, void *value)
+rewatch_it (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
{
struct GNUNET_PEERSTORE_Handle *h = cls;
struct GNUNET_PEERSTORE_WatchContext *wc = value;
* Called when the iterate request is timedout
*
* @param cls a `struct GNUNET_PEERSTORE_IterateContext *`
- * @param tc Scheduler task context (unused)
*/
static void
-iterate_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+iterate_timeout (void *cls)
{
struct GNUNET_PEERSTORE_IterateContext *ic = cls;
GNUNET_PEERSTORE_Processor callback;
}
-/**
- * Close the existing connection to PEERSTORE and reconnect.
- *
- * @param h handle to the service
- */
-static void
-reconnect (struct GNUNET_PEERSTORE_Handle *h)
-{
- struct GNUNET_PEERSTORE_IterateContext *ic;
- struct GNUNET_PEERSTORE_IterateContext *next;
- GNUNET_PEERSTORE_Processor icb;
- void *icb_cls;
- struct GNUNET_PEERSTORE_StoreContext *sc;
- struct GNUNET_MQ_Envelope *ev;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Reconnecting...\n");
- for (ic = h->iterate_head; NULL != ic; ic = next)
- {
- next = ic->next;
- if (GNUNET_YES == ic->iterating)
- {
- icb = ic->callback;
- icb_cls = ic->callback_cls;
- GNUNET_PEERSTORE_iterate_cancel (ic);
- if (NULL != icb)
- icb (icb_cls,
- NULL,
- "Iteration canceled due to reconnection");
- }
- }
- if (NULL != h->mq)
- {
- GNUNET_MQ_destroy (h->mq);
- h->mq = NULL;
- }
- if (NULL != h->client)
- {
- GNUNET_CLIENT_disconnect (h->client);
- h->client = NULL;
- }
-
- h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
- GNUNET_assert (NULL != h->client);
- h->mq =
- GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
- &handle_client_error, h);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Resending pending requests after reconnect.\n");
- if (NULL != h->watches)
- GNUNET_CONTAINER_multihashmap_iterate (h->watches, &rewatch_it, h);
- for (ic = h->iterate_head; NULL != ic; ic = ic->next)
- {
- ev =
- PEERSTORE_create_record_mq_envelope (ic->sub_system, &ic->peer, ic->key,
- NULL, 0, NULL, 0,
- GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
- GNUNET_MQ_send (h->mq, ev);
- ic->timeout_task =
- GNUNET_SCHEDULER_add_delayed (ic->timeout, &iterate_timeout, ic);
- }
- for (sc = h->store_head; NULL != sc; sc = sc->next)
- {
- ev =
- PEERSTORE_create_record_mq_envelope (sc->sub_system, &sc->peer, sc->key,
- sc->value, sc->size, &sc->expiry,
- sc->options,
- GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
- GNUNET_MQ_notify_sent (ev, &store_request_sent, sc);
- GNUNET_MQ_send (h->mq, ev);
- }
-}
-
-
/**
* Iterator over watch requests to cancel them.
*
* @return #GNUNET_YES to continue iteration
*/
static int
-destroy_watch (void *cls, const struct GNUNET_HashCode *key, void *value)
+destroy_watch (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
{
struct GNUNET_PEERSTORE_WatchContext *wc = value;
GNUNET_MQ_destroy (h->mq);
h->mq = NULL;
}
- if (NULL != h->client)
- {
- GNUNET_CLIENT_disconnect (h->client);
- h->client = NULL;
- }
GNUNET_free (h);
}
struct GNUNET_PEERSTORE_Handle *h;
h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
-
- h->client = GNUNET_CLIENT_connect ("peerstore", cfg);
- if (NULL == h->client)
- {
- GNUNET_free (h);
- return NULL;
- }
h->cfg = cfg;
h->disconnecting = GNUNET_NO;
- h->mq =
- GNUNET_MQ_queue_for_connection_client (h->client, mq_handlers,
- &handle_client_error, h);
+ reconnect (h);
if (NULL == h->mq)
{
- GNUNET_CLIENT_disconnect (h->client);
GNUNET_free (h);
return NULL;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
return h;
}
struct GNUNET_PEERSTORE_StoreContext *
GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
const char *sub_system,
- const struct GNUNET_PeerIdentity *peer, const char *key,
+ const struct GNUNET_PeerIdentity *peer,
+ const char *key,
const void *value, size_t size,
struct GNUNET_TIME_Absolute expiry,
enum GNUNET_PEERSTORE_StoreOption options,
- GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
+ GNUNET_PEERSTORE_Continuation cont,
+ void *cont_cls)
{
struct GNUNET_MQ_Envelope *ev;
struct GNUNET_PEERSTORE_StoreContext *sc;
/******************* ITERATE FUNCTIONS *********************/
/******************************************************************************/
+
/**
* When a response for iterate request is received
*
* @param cls a `struct GNUNET_PEERSTORE_Handle *`
- * @param msg message received, NULL on timeout or fatal error
+ * @param msg message received
*/
static void
-handle_iterate_result (void *cls, const struct GNUNET_MessageHeader *msg)
+handle_iterate_end (void *cls,
+ const struct GNUNET_MessageHeader *msg)
{
struct GNUNET_PEERSTORE_Handle *h = cls;
struct GNUNET_PEERSTORE_IterateContext *ic;
GNUNET_PEERSTORE_Processor callback;
void *callback_cls;
- uint16_t msg_type;
- struct GNUNET_PEERSTORE_Record *record;
- int continue_iter;
ic = h->iterate_head;
if (NULL == ic)
reconnect (h);
return;
}
- ic->iterating = GNUNET_YES;
callback = ic->callback;
callback_cls = ic->callback_cls;
- if (NULL == msg) /* Connection error */
+ ic->iterating = GNUNET_NO;
+ GNUNET_PEERSTORE_iterate_cancel (ic);
+ if (NULL != callback)
+ callback (callback_cls, NULL, NULL);
+}
+
+
+/**
+ * When a response for iterate request is received, check the
+ * message is well-formed.
+ *
+ * @param cls a `struct GNUNET_PEERSTORE_Handle *`
+ * @param msg message received
+ */
+static int
+check_iterate_result (void *cls,
+ const struct GNUNET_MessageHeader *msg)
+{
+ /* we defer validation to #handle_iterate_result */
+ return GNUNET_OK;
+}
+
+
+/**
+ * When a response for iterate request is received
+ *
+ * @param cls a `struct GNUNET_PEERSTORE_Handle *`
+ * @param msg message received
+ */
+static void
+handle_iterate_result (void *cls,
+ const struct GNUNET_MessageHeader *msg)
+{
+ struct GNUNET_PEERSTORE_Handle *h = cls;
+ struct GNUNET_PEERSTORE_IterateContext *ic;
+ GNUNET_PEERSTORE_Processor callback;
+ void *callback_cls;
+ struct GNUNET_PEERSTORE_Record *record;
+
+ ic = h->iterate_head;
+ if (NULL == ic)
{
- if (NULL != callback)
- callback (callback_cls, NULL,
- _("Error communicating with `PEERSTORE' service."));
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ _("Unexpected iteration response, this should not happen.\n"));
reconnect (h);
return;
}
- msg_type = ntohs (msg->type);
- if (GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END == msg_type)
- {
- ic->iterating = GNUNET_NO;
- GNUNET_PEERSTORE_iterate_cancel (ic);
- if (NULL != callback)
- callback (callback_cls, NULL, NULL);
+ ic->iterating = GNUNET_YES;
+ callback = ic->callback;
+ callback_cls = ic->callback_cls;
+ if (NULL == callback)
return;
+ record = PEERSTORE_parse_record_message (msg);
+ if (NULL == record)
+ {
+ callback (callback_cls,
+ NULL,
+ _("Received a malformed response from service."));
}
- if (NULL != callback)
+ else
{
- record = PEERSTORE_parse_record_message (msg);
- if (NULL == record)
- continue_iter =
- callback (callback_cls, NULL,
- _("Received a malformed response from service."));
- else
- {
- continue_iter = callback (callback_cls, record, NULL);
- PEERSTORE_destroy_record (record);
- }
- if (GNUNET_NO == continue_iter)
- ic->callback = NULL;
+ callback (callback_cls,
+ record,
+ NULL);
+ PEERSTORE_destroy_record (record);
}
}
ic->h->iterate_tail,
ic);
GNUNET_free (ic->sub_system);
- if (NULL != ic->key)
- GNUNET_free (ic->key);
+ GNUNET_free_non_null (ic->key);
GNUNET_free (ic);
}
else
GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
const char *sub_system,
const struct GNUNET_PeerIdentity *peer,
- const char *key, struct GNUNET_TIME_Relative timeout,
+ const char *key,
+ struct GNUNET_TIME_Relative timeout,
GNUNET_PEERSTORE_Processor callback,
void *callback_cls)
{
h->iterate_tail,
ic);
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Sending an iterate request for sub system `%s'\n", sub_system);
+ "Sending an iterate request for sub system `%s'\n",
+ sub_system);
GNUNET_MQ_send (h->mq, ev);
ic->timeout_task =
- GNUNET_SCHEDULER_add_delayed (timeout, &iterate_timeout, ic);
+ GNUNET_SCHEDULER_add_delayed (timeout,
+ &iterate_timeout,
+ ic);
return ic;
}
/******************************************************************************/
/**
- * When a watch record is received
+ * When a watch record is received, validate it is well-formed.
+ *
+ * @param cls a `struct GNUNET_PEERSTORE_Handle *`
+ * @param msg message received
+ */
+static int
+check_watch_record (void *cls,
+ const struct GNUNET_MessageHeader *msg)
+{
+ /* we defer validation to #handle_watch_result */
+ return GNUNET_OK;
+}
+
+
+/**
+ * When a watch record is received, process it.
*
* @param cls a `struct GNUNET_PEERSTORE_Handle *`
- * @param msg message received, NULL on timeout or fatal error
+ * @param msg message received
*/
static void
-handle_watch_result (void *cls, const struct GNUNET_MessageHeader *msg)
+handle_watch_record (void *cls,
+ const struct GNUNET_MessageHeader *msg)
{
struct GNUNET_PEERSTORE_Handle *h = cls;
struct GNUNET_PEERSTORE_Record *record;
struct GNUNET_HashCode keyhash;
struct GNUNET_PEERSTORE_WatchContext *wc;
- if (NULL == msg)
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received a watch record from service.\n");
+ record = PEERSTORE_parse_record_message (msg);
+ if (NULL == record)
{
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _
- ("Problem receiving a watch response, no way to determine which request.\n"));
reconnect (h);
return;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a watch record from service.\n");
- record = PEERSTORE_parse_record_message (msg);
- PEERSTORE_hash_key (record->sub_system, record->peer, record->key, &keyhash);
+ PEERSTORE_hash_key (record->sub_system,
+ record->peer,
+ record->key,
+ &keyhash);
// FIXME: what if there are multiple watches for the same key?
- wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash);
+ wc = GNUNET_CONTAINER_multihashmap_get (h->watches,
+ &keyhash);
if (NULL == wc)
{
LOG (GNUNET_ERROR_TYPE_ERROR,
return;
}
if (NULL != wc->callback)
- wc->callback (wc->callback_cls, record, NULL);
+ wc->callback (wc->callback_cls,
+ record,
+ NULL);
PEERSTORE_destroy_record (record);
}
+/**
+ * Close the existing connection to PEERSTORE and reconnect.
+ *
+ * @param h handle to the service
+ */
+static void
+reconnect (struct GNUNET_PEERSTORE_Handle *h)
+{
+ GNUNET_MQ_hd_fixed_size (iterate_end,
+ GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END,
+ struct GNUNET_MessageHeader);
+ GNUNET_MQ_hd_var_size (iterate_result,
+ GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD,
+ struct GNUNET_MessageHeader);
+ GNUNET_MQ_hd_var_size (watch_record,
+ GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD,
+ struct GNUNET_MessageHeader);
+ struct GNUNET_MQ_MessageHandler mq_handlers[] = {
+ make_iterate_end_handler (h),
+ make_iterate_result_handler (h),
+ make_watch_record_handler (h),
+ GNUNET_MQ_handler_end ()
+ };
+ struct GNUNET_PEERSTORE_IterateContext *ic;
+ struct GNUNET_PEERSTORE_IterateContext *next;
+ GNUNET_PEERSTORE_Processor icb;
+ void *icb_cls;
+ struct GNUNET_PEERSTORE_StoreContext *sc;
+ struct GNUNET_MQ_Envelope *ev;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Reconnecting...\n");
+ for (ic = h->iterate_head; NULL != ic; ic = next)
+ {
+ next = ic->next;
+ if (GNUNET_YES == ic->iterating)
+ {
+ icb = ic->callback;
+ icb_cls = ic->callback_cls;
+ GNUNET_PEERSTORE_iterate_cancel (ic);
+ if (NULL != icb)
+ icb (icb_cls,
+ NULL,
+ "Iteration canceled due to reconnection");
+ }
+ }
+ if (NULL != h->mq)
+ {
+ GNUNET_MQ_destroy (h->mq);
+ h->mq = NULL;
+ }
+ h->mq = GNUNET_CLIENT_connecT (h->cfg,
+ "peerstore",
+ mq_handlers,
+ &handle_client_error,
+ h);
+ if (NULL == h->mq)
+ return;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Resending pending requests after reconnect.\n");
+ if (NULL != h->watches)
+ GNUNET_CONTAINER_multihashmap_iterate (h->watches,
+ &rewatch_it,
+ h);
+ for (ic = h->iterate_head; NULL != ic; ic = ic->next)
+ {
+ ev = PEERSTORE_create_record_mq_envelope (ic->sub_system,
+ &ic->peer,
+ ic->key,
+ NULL, 0,
+ NULL, 0,
+ GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE);
+ GNUNET_MQ_send (h->mq, ev);
+ ic->timeout_task
+ = GNUNET_SCHEDULER_add_delayed (ic->timeout,
+ &iterate_timeout,
+ ic);
+ }
+ for (sc = h->store_head; NULL != sc; sc = sc->next)
+ {
+ ev = PEERSTORE_create_record_mq_envelope (sc->sub_system,
+ &sc->peer,
+ sc->key,
+ sc->value,
+ sc->size,
+ &sc->expiry,
+ sc->options,
+ GNUNET_MESSAGE_TYPE_PEERSTORE_STORE);
+ GNUNET_MQ_notify_sent (ev,
+ &store_request_sent,
+ sc);
+ GNUNET_MQ_send (h->mq,
+ ev);
+ }
+}
+
+
/**
* Cancel a watch request
*
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Canceling watch.\n");
- ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
+ ev = GNUNET_MQ_msg (hm,
+ GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL);
hm->keyhash = wc->keyhash;
GNUNET_MQ_send (h->mq, ev);
GNUNET_CONTAINER_multihashmap_remove (h->watches,
struct GNUNET_PEERSTORE_WatchContext *
GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h,
const char *sub_system,
- const struct GNUNET_PeerIdentity *peer, const char *key,
- GNUNET_PEERSTORE_Processor callback, void *callback_cls)
+ const struct GNUNET_PeerIdentity *peer,
+ const char *key,
+ GNUNET_PEERSTORE_Processor callback,
+ void *callback_cls)
{
struct GNUNET_MQ_Envelope *ev;
struct StoreKeyHashMessage *hm;
struct GNUNET_PEERSTORE_WatchContext *wc;
- GNUNET_assert (NULL != sub_system);
- GNUNET_assert (NULL != peer);
- GNUNET_assert (NULL != key);
- ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
+ ev = GNUNET_MQ_msg (hm,
+ GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
PEERSTORE_hash_key (sub_system,
peer,
key,