GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
+ by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
}
+/**
+ * Iterator over hash map entries.
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return GNUNET_YES if we should continue to
+ * iterate,
+ * GNUNET_NO if not.
+ */
+static int
+destroy_elements_iterator (void *cls,
+ const struct GNUNET_HashCode * key,
+ void *value)
+{
+ struct ElementEntry *ee = value;
+
+ GNUNET_free (ee);
+ return GNUNET_YES;
+}
+
+
/**
* Destroy a set, and free all resources associated with it.
*
set_destroy (struct Set *set)
{
/* If the client is not dead yet, destroy it.
- * The client's destroy callback will destroy the set again. */
+ * The client's destroy callback will destroy the set again.
+ * We do this so that the tunnel end handler still has a valid set handle
+ * to destroy. */
if (NULL != set->client)
{
struct GNUNET_SERVER_Client *client = set->client;
GNUNET_SERVER_client_disconnect (client);
return;
}
+ GNUNET_assert (NULL != set->state);
+ set->vt->destroy_set (set->state);
+ set->state = NULL;
if (NULL != set->client_mq)
{
GNUNET_MQ_destroy (set->client_mq);
set->client_mq = NULL;
}
- GNUNET_assert (NULL != set->state);
- set->vt->destroy_set (set->state);
- set->state = NULL;
+ if (NULL != set->iter)
+ {
+ GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
+ set->iter = NULL;
+ }
GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
+ if (NULL != set->elements)
+ {
+ GNUNET_CONTAINER_multihashmap_iterate (set->elements,
+ destroy_elements_iterator, NULL);
+ GNUNET_CONTAINER_multihashmap_destroy (set->elements);
+ set->elements = NULL;
+ }
GNUNET_free (set);
}
{
set->client = NULL;
set_destroy (set);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's set destroyed)\n");
}
listener = listener_get (client);
if (NULL != listener)
{
listener->client = NULL;
listener_destroy (listener);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's listener destroyed)\n");
}
}
mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
incoming->spec->context_msg);
GNUNET_assert (NULL != mqm);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n", incoming->suggest_id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n",
+ incoming->suggest_id);
cmsg->accept_id = htonl (incoming->suggest_id);
cmsg->peer_id = incoming->spec->peer;
GNUNET_MQ_send (listener->client_mq, mqm);
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received P2P operation request (op %u, app %s)\n",
- ntohs (msg->operation), GNUNET_h2s (&msg->app_id));
- listener = listener_get_by_target (ntohs (msg->operation), &msg->app_id);
+ ntohl (msg->operation), GNUNET_h2s (&msg->app_id));
+ listener = listener_get_by_target (ntohl (msg->operation), &msg->app_id);
if (NULL == listener)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
}
+static void
+send_client_element (struct Set *set)
+{
+ int ret;
+ struct ElementEntry *ee;
+ struct GNUNET_MQ_Envelope *ev;
+
+ GNUNET_assert (NULL != set->iter);
+ ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, NULL, (const void **) &ee);
+ if (GNUNET_NO == ret)
+ {
+ ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
+ }
+ else
+ {
+ struct GNUNET_SET_IterResponseMessage *msg;
+
+ GNUNET_assert (NULL != ee);
+ ev = GNUNET_MQ_msg_extra (msg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
+ memcpy (&msg[1], ee->element.data, ee->element.size);
+ msg->element_type = ee->element.type;
+ }
+ GNUNET_MQ_send (set->client_mq, ev);
+}
+
+
+/**
+ * Called when a client wants to iterate the elements of a set.
+ *
+ * @param cls unused
+ * @param client client that sent the message
+ * @param m message sent by the client
+ */
+static void
+handle_client_iterate (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *m)
+{
+ struct Set *set;
+
+ set = set_get (client);
+ if (NULL == set)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
+ if (NULL != set->iter)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
+ GNUNET_CONTAINER_multihashmap_size (set->elements));
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
+ send_client_element (set);
+}
+
+
/**
* Called when a client wants to create a new set.
*
return;
}
- set = NULL;
set = GNUNET_new (struct Set);
switch (ntohs (msg->operation))
set->vt = _GSS_union_vt ();
break;
default:
+ GNUNET_free (set);
GNUNET_break (0);
GNUNET_SERVER_client_disconnect (client);
return;
}
set->state = set->vt->create ();
+ set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
set->client = client;
set->client_mq = GNUNET_MQ_queue_for_server_client (client);
GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
listener->client = client;
listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
listener->app_id = msg->app_id;
- listener->operation = ntohs (msg->operation);
+ listener->operation = ntohl (msg->operation);
GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new listener created (op %u, app %s)\n",
listener->operation, GNUNET_h2s (&listener->app_id));
for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
{
- if ( (NULL == incoming->spec) ||
- (0 != incoming->suggest_id) )
+ if (NULL == incoming->spec)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request has no spec yet\n");
+ continue;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considering (op: %u, app: %s, suggest: %u)\n",
+ incoming->spec->operation, GNUNET_h2s (&incoming->spec->app_id), incoming->suggest_id);
+
+ if (0 != incoming->suggest_id)
continue;
if (listener->operation != incoming->spec->operation)
continue;
if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &incoming->spec->app_id))
continue;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request suggested\n");
incoming_suggest (incoming, listener);
}
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considered all incoming requests\n");
GNUNET_SERVER_receive_done (client, GNUNET_OK);
}
const struct GNUNET_MessageHeader *m)
{
struct Incoming *incoming;
- struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) m;
+ const struct GNUNET_SET_AcceptRejectMessage *msg;
+ msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
GNUNET_break (0 == ntohl (msg->request_id));
incoming = get_incoming (ntohl (msg->accept_reject_id));
struct Set *set;
const struct GNUNET_SET_ElementMessage *msg;
struct GNUNET_SET_Element el;
+ struct ElementEntry *ee;
set = set_get (client);
if (NULL == set)
GNUNET_SERVER_client_disconnect (client);
return;
}
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
msg = (const struct GNUNET_SET_ElementMessage *) m;
el.size = ntohs (m->size) - sizeof *msg;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client ins/rem element of size %u\n", el.size);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "client ins/rem element of size %u\n", el.size);
el.data = &msg[1];
if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
- set->vt->remove (set->state, &el);
+ {
+ struct GNUNET_HashCode hash;
+
+ GNUNET_CRYPTO_hash (el.data, el.size, &hash);
+ ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
+ if (NULL == ee)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove non-existing element\n");
+ return;
+ }
+ if (GNUNET_YES == ee->removed)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove element twice\n");
+ return;
+ }
+ ee->removed = GNUNET_YES;
+ ee->generation_removed = set->current_generation;
+ set->vt->remove (set->state, ee);
+ }
else
- set->vt->add (set->state, &el);
- GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ {
+ struct ElementEntry *ee_dup;
+
+ ee = GNUNET_malloc (el.size + sizeof *ee);
+ ee->element.size = el.size;
+ memcpy (&ee[1], el.data, el.size);
+ ee->element.data = &ee[1];
+ ee->generation_added = set->current_generation;
+ ee->remote = GNUNET_NO;
+ GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
+ ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
+ &ee->element_hash);
+ if (NULL != ee_dup)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "element inserted twice, ignoring\n");
+ GNUNET_free (ee);
+ return;
+ }
+ GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+ set->vt->add (set->state, ee);
+ }
}
spec->peer = msg->target_peer;
spec->set = set;
spec->client_request_id = ntohl (msg->request_id);
+ spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
+ if (NULL != spec->context_msg)
+ spec->context_msg = GNUNET_copy_message (spec->context_msg);
tunnel = GNUNET_MESH_tunnel_create (mesh, tc, &msg->target_peer,
GNUNET_APPLICATION_TYPE_SET,
* @param m the message
*/
static void
-handle_client_ack (void *cls,
+handle_client_iter_ack (void *cls,
struct GNUNET_SERVER_Client *client,
const struct GNUNET_MessageHeader *m)
{
- /* FIXME: implement */
+ struct Set *set;
+
+ set = set_get (client);
+ if (NULL == set)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
+ if (NULL == set->iter)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ send_client_element (set);
}
incoming->tc = GNUNET_new (struct TunnelContext);;
incoming->tc->vt = &incoming_vt;
incoming->tc->op = (struct OperationState *) incoming;
- incoming->timeout_task =
+ incoming->timeout_task =
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
/**
* Functions with this signature are called whenever a message is
* received.
- *
+ *
* Each time the function must call GNUNET_MESH_receive_done on the tunnel
* in order to receive the next message. This doesn't need to be immediate:
* can be delayed if some processing is done on the message.
* @param tunnel Connection to the other end.
* @param tunnel_ctx Place to store local state associated with the tunnel.
* @param message The actual message.
- *
+ *
* @return GNUNET_OK to keep the tunnel open,
* GNUNET_SYSERR to close it (signal serious error).
*/
struct TunnelContext *tc = *tunnel_ctx;
int ret;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message\n");
- ret = tc->vt->msg_handler (tc->op, message);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message (type: %u)\n",
+ ntohs (message->type));
+ /* do this before the handler, as the handler might kill the tunnel */
GNUNET_MESH_receive_done (tunnel);
-
+ ret = tc->vt->msg_handler (tc->op, message);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled mesh message (type: %u)\n",
+ ntohs (message->type));
return ret;
}
static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
{handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
sizeof (struct GNUNET_SET_AcceptRejectMessage)},
- {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
+ {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
{handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
{handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
sizeof (struct GNUNET_SET_CreateMessage)},
+ {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
+ sizeof (struct GNUNET_MessageHeader)},
{handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
{handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
sizeof (struct GNUNET_SET_ListenMessage)},
int ret;
ret = GNUNET_SERVICE_run (argc, argv, "set",
GNUNET_SERVICE_OPTION_NONE, &run, NULL);
- GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit (%d)\n", GNUNET_OK != ret);
return (GNUNET_OK == ret) ? 0 : 1;
}