2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
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 set/gnunet-service-set.c
23 * @brief two-peer set operations
24 * @author Florian Dold
26 #include "gnunet-service-set.h"
27 #include "set_protocol.h"
31 * State of an operation where the peer has connected to us, but is not yet
32 * evaluating a set operation. Once the peer has sent a concrete request, and
33 * the client has accepted or rejected it, this information will be deleted
34 * and replaced by the real set operation state.
39 * The identity of the requesting peer. Needs to
40 * be stored here as the op spec might not have been created yet.
42 struct GNUNET_PeerIdentity peer;
45 * Unique request id for the request from
46 * a remote peer, sent to the client, which will
47 * accept or reject the request.
48 * Set to '0' iff the request has not been
54 * Timeout task, if the incoming peer has not been accepted
55 * after the timeout, it will be disconnected.
57 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
62 * A listener is inhabited by a client, and
63 * waits for evaluation requests from remote peers.
68 * Listeners are held in a doubly linked list.
70 struct Listener *next;
73 * Listeners are held in a doubly linked list.
75 struct Listener *prev;
78 * Client that owns the listener.
79 * Only one client may own a listener.
81 struct GNUNET_SERVER_Client *client;
84 * Message queue for the client
86 struct GNUNET_MQ_Handle *client_mq;
89 * The type of the operation.
91 enum GNUNET_SET_OperationType operation;
94 * Application ID for the operation, used to distinguish
95 * multiple operations of the same type with the same peer.
97 struct GNUNET_HashCode app_id;
102 * Configuration of our local peer.
104 static const struct GNUNET_CONFIGURATION_Handle *configuration;
107 * Handle to the mesh service, used
108 * to listen for and connect to remote peers.
110 static struct GNUNET_MESH_Handle *mesh;
113 * Sets are held in a doubly linked list.
115 static struct Set *sets_head;
118 * Sets are held in a doubly linked list.
120 static struct Set *sets_tail;
123 * Listeners are held in a doubly linked list.
125 static struct Listener *listeners_head;
128 * Listeners are held in a doubly linked list.
130 static struct Listener *listeners_tail;
133 * Incoming sockets from remote peers are
134 * held in a doubly linked list.
136 static struct Operation *incoming_head;
139 * Incoming sockets from remote peers are
140 * held in a doubly linked list.
142 static struct Operation *incoming_tail;
145 * Counter for allocating unique IDs for clients,
146 * used to identify incoming operation requests from remote peers,
147 * that the client can choose to accept or refuse.
149 static uint32_t suggest_id = 1;
153 * Get set that is owned by the given client, if any.
155 * @param client client to look for
156 * @return set that the client owns, NULL if the client
160 set_get (struct GNUNET_SERVER_Client *client)
164 for (set = sets_head; NULL != set; set = set->next)
165 if (set->client == client)
172 * Get the listener associated with the given client, if any.
174 * @param client the client
175 * @return listener associated with the client, NULL
178 static struct Listener *
179 listener_get (struct GNUNET_SERVER_Client *client)
181 struct Listener *listener;
183 for (listener = listeners_head; NULL != listener; listener = listener->next)
184 if (listener->client == client)
191 * Get the incoming socket associated with the given id.
193 * @param id id to look for
194 * @return the incoming socket associated with the id,
195 * or NULL if there is none
197 static struct Operation *
198 get_incoming (uint32_t id)
200 struct Operation *op;
202 for (op = incoming_head; NULL != op; op = op)
203 if (op->state->suggest_id == id)
210 * Destroy a listener, free all resources associated with it.
212 * @param listener listener to destroy
215 listener_destroy (struct Listener *listener)
217 /* If the client is not dead yet, destroy it.
218 * The client's destroy callback will destroy the listener again. */
219 if (NULL != listener->client)
221 struct GNUNET_SERVER_Client *client = listener->client;
222 listener->client = NULL;
223 GNUNET_SERVER_client_disconnect (client);
226 if (NULL != listener->client_mq)
228 GNUNET_MQ_destroy (listener->client_mq);
229 listener->client_mq = NULL;
231 GNUNET_CONTAINER_DLL_remove (listeners_head, listeners_tail, listener);
232 GNUNET_free (listener);
237 * Collect and destroy elements that are not needed anymore, because
238 * their lifetime (as determined by their generation) does not overlap with any active
241 * We hereby replace the old element hashmap with a new one, instead of removing elements.
244 collect_generation_garbage (struct Set *set)
246 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
247 struct ElementEntry *ee;
248 struct GNUNET_CONTAINER_MultiHashMap *new_elements;
250 struct Operation *op;
252 new_elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
253 iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
255 (res = GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL, (const void **) &ee)))
257 if (GNUNET_NO == ee->removed)
259 for (op = set->ops_head; NULL != op; op = op->next)
260 if ((op->generation_created >= ee->generation_added) &&
261 (op->generation_created < ee->generation_removed))
266 // we don't expect collisions, thus the replace option
267 GNUNET_CONTAINER_multihashmap_put (new_elements, &ee->element_hash, ee,
268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
270 GNUNET_CONTAINER_multihashmap_iterator_destroy (iter);
271 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
272 set->elements = new_elements;
277 * Destroy the given operation. Call the implementation-specific cancel function
278 * of the operation. Disconnects from the remote peer.
279 * Does not disconnect the client, as there may be multiple operations per set.
281 * @param op operation to destroy
284 _GSS_operation_destroy (struct Operation *op)
293 GNUNET_assert (GNUNET_NO == op->is_incoming);
294 GNUNET_assert (NULL != op->spec);
295 GNUNET_CONTAINER_DLL_remove (op->spec->set->ops_head,
296 op->spec->set->ops_tail,
302 if (NULL != op->spec)
304 if (NULL != op->spec->context_msg)
306 GNUNET_free (op->spec->context_msg);
307 op->spec->context_msg = NULL;
309 GNUNET_free (op->spec);
315 GNUNET_MQ_destroy (op->mq);
319 if (NULL != op->tunnel)
321 GNUNET_MESH_tunnel_destroy (op->tunnel);
325 collect_generation_garbage (set);
327 /* We rely on the tunnel end handler to free 'op'. When 'op->tunnel' was NULL,
328 * there was a tunnel end handler that will free 'op' on the call stack. */
333 * Iterator over hash map entries to free
337 * @param key current key code
338 * @param value value in the hash map
339 * @return GNUNET_YES if we should continue to
344 destroy_elements_iterator (void *cls,
345 const struct GNUNET_HashCode * key,
348 struct ElementEntry *ee = value;
356 * Destroy a set, and free all resources associated with it.
358 * @param set the set to destroy
361 set_destroy (struct Set *set)
363 /* If the client is not dead yet, destroy it.
364 * The client's destroy callback will destroy the set again.
365 * We do this so that the tunnel end handler still has a valid set handle
367 if (NULL != set->client)
369 struct GNUNET_SERVER_Client *client = set->client;
371 GNUNET_SERVER_client_disconnect (client);
374 GNUNET_assert (NULL != set->state);
375 while (NULL != set->ops_head)
376 _GSS_operation_destroy (set->ops_head);
377 set->vt->destroy_set (set->state);
379 if (NULL != set->client_mq)
381 GNUNET_MQ_destroy (set->client_mq);
382 set->client_mq = NULL;
384 if (NULL != set->iter)
386 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
389 GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
390 if (NULL != set->elements)
392 // free all elements in the hashtable, before destroying the table
393 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
394 destroy_elements_iterator, NULL);
395 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
396 set->elements = NULL;
403 * Clean up after a client has disconnected
405 * @param cls closure, unused
406 * @param client the client to clean up after
409 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
412 struct Listener *listener;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected, cleaning up\n");
416 set = set_get (client);
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's set destroyed)\n");
423 listener = listener_get (client);
424 if (NULL != listener)
426 listener->client = NULL;
427 listener_destroy (listener);
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's listener destroyed)\n");
434 * Destroy an incoming request from a remote peer
436 * @param incoming remote request to destroy
439 incoming_destroy (struct Operation *incoming)
441 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
442 if (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task)
444 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
445 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
447 GNUNET_free (incoming->state);
451 * remove & free state of the operation from the incoming list
453 * @param incoming the element to remove
457 incoming_retire (struct Operation *incoming)
459 incoming->is_incoming = GNUNET_NO;
460 GNUNET_free (incoming->state);
461 incoming->state = NULL;
462 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
467 * Find a listener that is interested in the given operation type
468 * and application id.
470 * @param op operation type to look for
471 * @param app_id application id to look for
472 * @return a matching listener, or NULL if no listener matches the
473 * given operation and application id
475 static struct Listener *
476 listener_get_by_target (enum GNUNET_SET_OperationType op,
477 const struct GNUNET_HashCode *app_id)
481 for (l = listeners_head; NULL != l; l = l->next)
483 if (l->operation != op)
485 if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
494 * Suggest the given request to the listener. The listening client can then
495 * accept or reject the remote request.
497 * @param incoming the incoming peer with the request to suggest
498 * @param listener the listener to suggest the request to
501 incoming_suggest (struct Operation *incoming, struct Listener *listener)
503 struct GNUNET_MQ_Envelope *mqm;
504 struct GNUNET_SET_RequestMessage *cmsg;
506 GNUNET_assert (NULL != incoming->spec);
507 GNUNET_assert (0 == incoming->state->suggest_id);
508 incoming->state->suggest_id = suggest_id++;
510 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task);
511 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
512 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
514 mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
515 incoming->spec->context_msg);
516 GNUNET_assert (NULL != mqm);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n",
518 incoming->state->suggest_id);
519 cmsg->accept_id = htonl (incoming->state->suggest_id);
520 cmsg->peer_id = incoming->spec->peer;
521 GNUNET_MQ_send (listener->client_mq, mqm);
526 * Handle a request for a set operation from
529 * This msg is expected as the first and only msg handled through the
530 * non-operation bound virtual table, acceptance of this operation replaces
531 * our virtual table and subsequent msgs would be routed differently.
533 * @param op the operation state
534 * @param mh the received message
535 * @return GNUNET_OK if the tunnel should be kept alive,
536 * GNUNET_SYSERR to destroy the tunnel
539 handle_incoming_msg (struct Operation *op,
540 const struct GNUNET_MessageHeader *mh)
542 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
543 struct Listener *listener;
544 struct OperationSpecification *spec;
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got op request\n");
548 if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
551 return GNUNET_SYSERR;
554 /* double operation request */
555 if (NULL != op->spec)
558 return GNUNET_SYSERR;
561 spec = GNUNET_new (struct OperationSpecification);
562 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
563 // for simplicity we just backup the context msg instead of rebuilding it later on
564 if (NULL != spec->context_msg)
565 spec->context_msg = GNUNET_copy_message (spec->context_msg);
566 spec->operation = ntohl (msg->operation);
567 spec->app_id = msg->app_id;
568 spec->salt = ntohl (msg->salt);
569 spec->peer = op->state->peer;
573 if ( (NULL != spec->context_msg) &&
574 (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
577 return GNUNET_SYSERR;
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received P2P operation request (op %u, app %s)\n",
581 ntohl (msg->operation), GNUNET_h2s (&msg->app_id));
582 listener = listener_get_by_target (ntohl (msg->operation), &msg->app_id);
583 if (NULL == listener)
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "no listener matches incoming request, waiting with timeout\n");
589 incoming_suggest (op, listener);
595 * Send the next element of a set to the set's client. The next element is given by
596 * the set's current hashmap iterator. The set's iterator will be set to NULL if there
597 * are no more elements in the set. The caller must ensure that the set's iterator is
600 * @param set set that should send its next element to its client
603 send_client_element (struct Set *set)
606 struct ElementEntry *ee;
607 struct GNUNET_MQ_Envelope *ev;
609 GNUNET_assert (NULL != set->iter);
610 ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, NULL, (const void **) &ee);
611 if (GNUNET_NO == ret)
613 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
614 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
619 struct GNUNET_SET_IterResponseMessage *msg;
621 GNUNET_assert (NULL != ee);
622 ev = GNUNET_MQ_msg_extra (msg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
623 memcpy (&msg[1], ee->element.data, ee->element.size);
624 msg->element_type = ee->element.type;
626 GNUNET_MQ_send (set->client_mq, ev);
631 * Called when a client wants to iterate the elements of a set.
634 * @param client client that sent the message
635 * @param m message sent by the client
638 handle_client_iterate (void *cls,
639 struct GNUNET_SERVER_Client *client,
640 const struct GNUNET_MessageHeader *m)
644 // iterate over a non existing set
645 set = set_get (client);
649 GNUNET_SERVER_client_disconnect (client);
653 // only one concurrent iterate-action per set
654 if (NULL != set->iter)
657 GNUNET_SERVER_client_disconnect (client);
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
661 GNUNET_CONTAINER_multihashmap_size (set->elements));
662 GNUNET_SERVER_receive_done (client, GNUNET_OK);
663 set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
664 send_client_element (set);
669 * Called when a client wants to create a new set.
672 * @param client client that sent the message
673 * @param m message sent by the client
676 handle_client_create_set (void *cls,
677 struct GNUNET_SERVER_Client *client,
678 const struct GNUNET_MessageHeader *m)
680 struct GNUNET_SET_CreateMessage *msg = (struct GNUNET_SET_CreateMessage *) m;
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client created new set (operation %u)\n",
684 ntohs (msg->operation));
686 // max. one set per client!
687 if (NULL != set_get (client))
690 GNUNET_SERVER_client_disconnect (client);
694 set = GNUNET_new (struct Set);
696 switch (ntohs (msg->operation))
698 case GNUNET_SET_OPERATION_INTERSECTION:
699 // FIXME: implement intersection vt
700 // set->vt = _GSS_intersection_vt ();
702 case GNUNET_SET_OPERATION_UNION:
703 set->vt = _GSS_union_vt ();
708 GNUNET_SERVER_client_disconnect (client);
712 set->state = set->vt->create ();
713 set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
714 set->client = client;
715 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
716 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
722 * Called when a client wants to create a new listener.
725 * @param client client that sent the message
726 * @param m message sent by the client
729 handle_client_listen (void *cls,
730 struct GNUNET_SERVER_Client *client,
731 const struct GNUNET_MessageHeader *m)
733 struct GNUNET_SET_ListenMessage *msg = (struct GNUNET_SET_ListenMessage *) m;
734 struct Listener *listener;
735 struct Operation *op;
737 // max. one per client!
738 if (NULL != listener_get (client))
741 GNUNET_SERVER_client_disconnect (client);
745 listener = GNUNET_new (struct Listener);
746 listener->client = client;
747 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
748 listener->app_id = msg->app_id;
749 listener->operation = ntohl (msg->operation);
750 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new listener created (op %u, app %s)\n",
752 listener->operation, GNUNET_h2s (&listener->app_id));
754 /* check for incoming requests the listener is interested in */
755 for (op = incoming_head; NULL != op; op = op->next)
757 if (NULL == op->spec)
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request has no spec yet\n");
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considering (op: %u, app: %s, suggest: %u)\n",
763 op->spec->operation, GNUNET_h2s (&op->spec->app_id), op->state->suggest_id);
765 /* don't consider the incoming request if it has been already suggested to a listener */
766 if (0 != op->state->suggest_id)
768 if (listener->operation != op->spec->operation)
770 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request suggested\n");
773 incoming_suggest (op, listener);
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considered all incoming requests\n");
776 GNUNET_SERVER_receive_done (client, GNUNET_OK);
781 * Called when the listening client rejects an operation
782 * request by another peer.
785 * @param client client that sent the message
786 * @param m message sent by the client
789 handle_client_reject (void *cls,
790 struct GNUNET_SERVER_Client *client,
791 const struct GNUNET_MessageHeader *m)
793 struct Operation *incoming;
794 const struct GNUNET_SET_AcceptRejectMessage *msg;
796 msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
797 GNUNET_break (0 == ntohl (msg->request_id));
799 // no matching incoming operation for this reject
800 incoming = get_incoming (ntohl (msg->accept_reject_id));
801 if (NULL == incoming)
803 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer request rejected by client\n");
808 GNUNET_MESH_tunnel_destroy (incoming->tunnel);
809 //tunnel destruction handler called immediately upon destruction
810 GNUNET_SERVER_receive_done (client, GNUNET_OK);
815 * Called when a client wants to add/remove an element to/from a
819 * @param client client that sent the message
820 * @param m message sent by the client
823 handle_client_add_remove (void *cls,
824 struct GNUNET_SERVER_Client *client,
825 const struct GNUNET_MessageHeader *m)
828 const struct GNUNET_SET_ElementMessage *msg;
829 struct GNUNET_SET_Element el;
830 struct ElementEntry *ee;
832 // client without a set requested an operation
833 set = set_get (client);
837 GNUNET_SERVER_client_disconnect (client);
840 GNUNET_SERVER_receive_done (client, GNUNET_OK);
841 msg = (const struct GNUNET_SET_ElementMessage *) m;
842 el.size = ntohs (m->size) - sizeof *msg;
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "client ins/rem element of size %u\n", el.size);
846 if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
848 struct GNUNET_HashCode hash;
850 GNUNET_CRYPTO_hash (el.data, el.size, &hash);
851 ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove non-existing element\n");
857 if (GNUNET_YES == ee->removed)
859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove element twice\n");
862 ee->removed = GNUNET_YES;
863 ee->generation_removed = set->current_generation;
864 set->vt->remove (set->state, ee);
868 struct ElementEntry *ee_dup;
870 ee = GNUNET_malloc (el.size + sizeof *ee);
871 ee->element.size = el.size;
872 memcpy (&ee[1], el.data, el.size);
873 ee->element.data = &ee[1];
874 ee->generation_added = set->current_generation;
875 ee->remote = GNUNET_NO;
876 GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
877 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "element inserted twice, ignoring\n");
885 GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
886 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
887 set->vt->add (set->state, ee);
893 * Called when a client wants to evaluate a set operation with another peer.
896 * @param client client that sent the message
897 * @param m message sent by the client
900 handle_client_evaluate (void *cls,
901 struct GNUNET_SERVER_Client *client,
902 const struct GNUNET_MessageHeader *m)
905 struct GNUNET_SET_EvaluateMessage *msg;
906 struct OperationSpecification *spec;
907 struct Operation *op;
909 set = set_get (client);
913 GNUNET_SERVER_client_disconnect (client);
917 msg = (struct GNUNET_SET_EvaluateMessage *) m;
918 spec = GNUNET_new (struct OperationSpecification);
919 spec->operation = set->operation;
920 spec->app_id = msg->app_id;
921 spec->salt = ntohl (msg->salt);
922 spec->peer = msg->target_peer;
924 spec->result_mode = ntohs (msg->result_mode);
925 spec->client_request_id = ntohl (msg->request_id);
926 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
928 // for simplicity we just backup the context msg instead of rebuilding it later on
929 if (NULL != spec->context_msg)
930 spec->context_msg = GNUNET_copy_message (spec->context_msg);
932 op = GNUNET_new (struct Operation);
934 op->generation_created = set->current_generation++;
936 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
938 op->tunnel = GNUNET_MESH_tunnel_create (mesh, op, &msg->target_peer,
939 GNUNET_APPLICATION_TYPE_SET,
943 op->mq = GNUNET_MESH_mq_create (op->tunnel);
945 set->vt->evaluate (op);
946 GNUNET_SERVER_receive_done (client, GNUNET_OK);
951 * Handle an ack from a client, and send the next element.
954 * @param client the client
955 * @param m the message
958 handle_client_iter_ack (void *cls,
959 struct GNUNET_SERVER_Client *client,
960 const struct GNUNET_MessageHeader *m)
964 // client without a set requested an operation
965 set = set_get (client);
969 GNUNET_SERVER_client_disconnect (client);
973 // client sent an ack, but we were not expecting one
974 if (NULL == set->iter)
977 GNUNET_SERVER_client_disconnect (client);
981 GNUNET_SERVER_receive_done (client, GNUNET_OK);
982 send_client_element (set);
987 * Handle a request from the client to
988 * cancel a running set operation.
991 * @param client the client
992 * @param mh the message
995 handle_client_cancel (void *cls,
996 struct GNUNET_SERVER_Client *client,
997 const struct GNUNET_MessageHeader *mh)
999 const struct GNUNET_SET_CancelMessage *msg =
1000 (const struct GNUNET_SET_CancelMessage *) mh;
1002 struct Operation *op;
1005 // client without a set requested an operation
1006 set = set_get (client);
1010 GNUNET_SERVER_client_disconnect (client);
1014 for (op = set->ops_head; NULL != op; op = op->next)
1016 if (op->spec->client_request_id == msg->request_id)
1023 if (GNUNET_NO == found)
1026 GNUNET_SERVER_client_disconnect (client);
1030 _GSS_operation_destroy (op);
1035 * Handle a request from the client to accept
1036 * a set operation that came from a remote peer.
1037 * We forward the accept to the associated operation for handling
1040 * @param client the client
1041 * @param mh the message
1044 handle_client_accept (void *cls,
1045 struct GNUNET_SERVER_Client *client,
1046 const struct GNUNET_MessageHeader *mh)
1049 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) mh;
1050 struct Operation *op;
1052 op = get_incoming (ntohl (msg->accept_reject_id));
1054 // incoming operation does not exist
1058 GNUNET_SERVER_client_disconnect (client);
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client accepting %u\n", ntohl (msg->accept_reject_id));
1064 GNUNET_assert (GNUNET_YES == op->is_incoming);
1066 // client without a set requested an operation
1067 set = set_get (client);
1072 GNUNET_SERVER_client_disconnect (client);
1076 op->spec->set = set;
1078 incoming_retire (op);
1080 GNUNET_assert (NULL != op->spec->set);
1081 GNUNET_assert (NULL != op->spec->set->vt);
1083 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1085 op->spec->client_request_id = ntohl (msg->request_id);
1086 op->spec->result_mode = ntohs (msg->result_mode);
1087 op->generation_created = set->current_generation++;
1088 op->vt = op->spec->set->vt;
1089 GNUNET_assert (NULL != op->vt->accept);
1090 set->vt->accept (op);
1091 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1096 * Called to clean up, after a shutdown has been requested.
1098 * @param cls closure
1099 * @param tc context information (why was this task triggered now)
1102 shutdown_task (void *cls,
1103 const struct GNUNET_SCHEDULER_TaskContext *tc)
1105 while (NULL != incoming_head)
1106 incoming_destroy (incoming_head);
1108 while (NULL != listeners_head)
1109 listener_destroy (listeners_head);
1111 while (NULL != sets_head)
1112 set_destroy (sets_head);
1114 /* it's important to destroy mesh at the end, as all tunnels
1115 * must be destroyed before the mesh handle! */
1118 GNUNET_MESH_disconnect (mesh);
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled shutdown request\n");
1127 * Timeout happens iff:
1128 * - we suggested an operation to our listener,
1129 * but did not receive a response in time
1130 * - we got the tunnel from a peer but no GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1131 * - shutdown (obviously)
1132 * @param cls tunnel context
1133 * @param tc context information (why was this task triggered now)
1136 incoming_timeout_cb (void *cls,
1137 const struct GNUNET_SCHEDULER_TaskContext *tc)
1139 struct Operation *incoming = cls;
1141 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1143 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "remote peer timed out\n");
1147 incoming_destroy (incoming);
1152 * Terminates an incoming operation in case we have not yet received an
1153 * operation request. Called by the tunnel destruction handler.
1155 * @param op the tunnel context
1158 handle_incoming_disconnect (struct Operation *op)
1160 GNUNET_assert (GNUNET_YES == op->is_incoming);
1162 if (NULL == op->tunnel)
1165 incoming_destroy (op);
1170 * Method called whenever another peer has added us to a tunnel
1171 * the other peer initiated.
1172 * Only called (once) upon reception of data with a message type which was
1173 * subscribed to in GNUNET_MESH_connect.
1175 * The tunnel context represents the operation itself and gets added to a DLL,
1176 * from where it gets looked up when our local listener client responds
1177 * to a proposed/suggested operation or connects and associates with this operation.
1179 * @param cls closure
1180 * @param tunnel new handle to the tunnel
1181 * @param initiator peer that started the tunnel
1182 * @param port Port this tunnel is for.
1183 * @return initial tunnel context for the tunnel
1184 * (can be NULL -- that's not an error)
1187 tunnel_new_cb (void *cls,
1188 struct GNUNET_MESH_Tunnel *tunnel,
1189 const struct GNUNET_PeerIdentity *initiator,
1192 struct Operation *incoming;
1193 static const struct SetVT incoming_vt = {
1194 .msg_handler = handle_incoming_msg,
1195 .peer_disconnect = handle_incoming_disconnect
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new incoming tunnel\n");
1200 GNUNET_assert (port == GNUNET_APPLICATION_TYPE_SET);
1201 incoming = GNUNET_new (struct Operation);
1202 incoming->is_incoming = GNUNET_YES;
1203 incoming->state = GNUNET_new (struct OperationState);
1204 incoming->state->peer = *initiator;
1205 incoming->tunnel = tunnel;
1206 incoming->mq = GNUNET_MESH_mq_create (incoming->tunnel);
1207 incoming->vt = &incoming_vt;
1208 incoming->state->timeout_task =
1209 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
1210 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
1217 * Function called whenever a tunnel is destroyed. Should clean up
1218 * any associated state.
1219 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
1222 * The peer_disconnect function is part of a a virtual table set initially either
1223 * when a peer creates a new tunnel with us (tunnel_new_cb), or once we create
1224 * a new tunnel ourselves (evaluate).
1226 * Once we know the exact type of operation (union/intersection), the vt is
1227 * replaced with an operation specific instance (_GSS_[op]_vt).
1229 * @param cls closure (set from GNUNET_MESH_connect)
1230 * @param tunnel connection to the other end (henceforth invalid)
1231 * @param tunnel_ctx place where local state associated
1232 * with the tunnel is stored
1235 tunnel_end_cb (void *cls,
1236 const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
1238 struct Operation *op = tunnel_ctx;
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel end cb called\n");
1245 op->vt->peer_disconnect (op);
1246 /* mesh will never call us with the context again! */
1247 GNUNET_free (tunnel_ctx);
1252 * Functions with this signature are called whenever any message is
1253 * received via the mesh tunnel.
1255 * The msg_handler is a virtual table set in initially either when a peer
1256 * creates a new tunnel with us (tunnel_new_cb), or once we create a new tunnel
1257 * ourselves (evaluate).
1259 * Once we know the exact type of operation (union/intersection), the vt is
1260 * replaced with an operation specific instance (_GSS_[op]_vt).
1262 * @param cls Closure (set from GNUNET_MESH_connect).
1263 * @param tunnel Connection to the other end.
1264 * @param tunnel_ctx Place to store local state associated with the tunnel.
1265 * @param message The actual message.
1267 * @return GNUNET_OK to keep the tunnel open,
1268 * GNUNET_SYSERR to close it (signal serious error).
1271 dispatch_p2p_message (void *cls,
1272 struct GNUNET_MESH_Tunnel *tunnel,
1274 const struct GNUNET_MessageHeader *message)
1276 struct Operation *op = *tunnel_ctx;
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message (type: %u)\n",
1280 ntohs (message->type));
1281 /* do this before the handler, as the handler might kill the tunnel */
1282 GNUNET_MESH_receive_done (tunnel);
1283 ret = op->vt->msg_handler (op, message);
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled mesh message (type: %u)\n",
1285 ntohs (message->type));
1291 * Function called by the service's run
1292 * method to run service-specific setup code.
1294 * @param cls closure
1295 * @param server the initialized server
1296 * @param cfg configuration to use
1299 run (void *cls, struct GNUNET_SERVER_Handle *server,
1300 const struct GNUNET_CONFIGURATION_Handle *cfg)
1302 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1303 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1304 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1305 {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
1306 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
1307 {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
1308 sizeof (struct GNUNET_SET_CreateMessage)},
1309 {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1310 sizeof (struct GNUNET_MessageHeader)},
1311 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
1312 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
1313 sizeof (struct GNUNET_SET_ListenMessage)},
1314 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
1315 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1316 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
1317 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
1318 sizeof (struct GNUNET_SET_CancelMessage)},
1321 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1322 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1323 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1324 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1325 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
1326 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1327 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1328 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1331 static const uint32_t mesh_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1333 configuration = cfg;
1334 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1335 &shutdown_task, NULL);
1336 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1337 GNUNET_SERVER_add_handlers (server, server_handlers);
1339 mesh = GNUNET_MESH_connect (cfg, NULL, tunnel_new_cb, tunnel_end_cb,
1340 mesh_handlers, mesh_ports);
1343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not connect to mesh\n");
1347 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "started\n");
1352 * The main function for the set service.
1354 * @param argc number of arguments from the command line
1355 * @param argv command line arguments
1356 * @return 0 ok, 1 on error
1359 main (int argc, char *const *argv)
1362 ret = GNUNET_SERVICE_run (argc, argv, "set",
1363 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1364 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit (%d)\n", GNUNET_OK != ret);
1365 return (GNUNET_OK == ret) ? 0 : 1;