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 * Timeout task, if the incoming peer has not been accepted
46 * after the timeout, it will be disconnected.
48 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
51 * Unique request id for the request from a remote peer, sent to the
52 * client, which will accept or reject the request. Set to '0' iff
53 * the request has not been suggested yet.
61 * A listener is inhabited by a client, and waits for evaluation
62 * requests from remote peers.
67 * Listeners are held in a doubly linked list.
69 struct Listener *next;
72 * Listeners are held in a doubly linked list.
74 struct Listener *prev;
77 * Client that owns the listener.
78 * Only one client may own a listener.
80 struct GNUNET_SERVER_Client *client;
83 * Message queue for the client
85 struct GNUNET_MQ_Handle *client_mq;
88 * Application ID for the operation, used to distinguish
89 * multiple operations of the same type with the same peer.
91 struct GNUNET_HashCode app_id;
94 * The type of the operation.
96 enum GNUNET_SET_OperationType operation;
101 * Configuration of our local peer.
103 static const struct GNUNET_CONFIGURATION_Handle *configuration;
106 * Handle to the cadet service, used to listen for and connect to
109 static struct GNUNET_CADET_Handle *cadet;
112 * Sets are held in a doubly linked list.
114 static struct Set *sets_head;
117 * Sets are held in a doubly linked list.
119 static struct Set *sets_tail;
122 * Listeners are held in a doubly linked list.
124 static struct Listener *listeners_head;
127 * Listeners are held in a doubly linked list.
129 static struct Listener *listeners_tail;
132 * Incoming sockets from remote peers are held in a doubly linked
135 static struct Operation *incoming_head;
138 * Incoming sockets from remote peers are held in a doubly linked
141 static struct Operation *incoming_tail;
144 * Counter for allocating unique IDs for clients, used to identify
145 * incoming operation requests from remote peers, that the client can
146 * choose to accept or refuse.
148 static uint32_t suggest_id = 1;
152 * Get set that is owned by the given client, if any.
154 * @param client client to look for
155 * @return set that the client owns, NULL if the client
159 set_get (struct GNUNET_SERVER_Client *client)
163 for (set = sets_head; NULL != set; set = set->next)
164 if (set->client == client)
171 * Get the listener associated with the given client, if any.
173 * @param client the client
174 * @return listener associated with the client, NULL
177 static struct Listener *
178 listener_get (struct GNUNET_SERVER_Client *client)
180 struct Listener *listener;
182 for (listener = listeners_head; NULL != listener; listener = listener->next)
183 if (listener->client == client)
190 * Get the incoming socket associated with the given id.
192 * @param id id to look for
193 * @return the incoming socket associated with the id,
194 * or NULL if there is none
196 static struct Operation *
197 get_incoming (uint32_t id)
199 struct Operation *op;
201 for (op = incoming_head; NULL != op; op = op->next)
202 if (op->state->suggest_id == id)
204 // FIXME: remove this assertion once the corresponding bug is gone!
205 GNUNET_assert (GNUNET_YES == op->is_incoming);
213 * Destroy a listener, free all resources associated with it.
215 * @param listener listener to destroy
218 listener_destroy (struct Listener *listener)
220 /* If the client is not dead yet, destroy it.
221 * The client's destroy callback will destroy the listener again. */
222 if (NULL != listener->client)
224 struct GNUNET_SERVER_Client *client = listener->client;
226 listener->client = NULL;
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "disconnecting listener client\n");
229 GNUNET_SERVER_client_disconnect (client);
232 if (NULL != listener->client_mq)
234 GNUNET_MQ_destroy (listener->client_mq);
235 listener->client_mq = NULL;
237 GNUNET_CONTAINER_DLL_remove (listeners_head,
240 GNUNET_free (listener);
245 * Context for the #garbage_collect_cb().
247 struct GarbageContext
251 * Map for which we are garbage collecting removed elements.
253 struct GNUNET_CONTAINER_MultiHashMap *map;
256 * Lowest generation for which an operation is still pending.
258 unsigned int min_op_generation;
261 * Largest generation for which an operation is still pending.
263 unsigned int max_op_generation;
269 * Function invoked to check if an element can be removed from
270 * the set's history because it is no longer needed.
272 * @param cls the `struct GarbageContext *`
273 * @param key key of the element in the map
274 * @param value the `struct ElementEntry *`
275 * @return #GNUNET_OK (continue to iterate)
278 garbage_collect_cb (void *cls,
279 const struct GNUNET_HashCode *key,
282 struct GarbageContext *gc = cls;
283 struct ElementEntry *ee = value;
285 if (GNUNET_YES != ee->removed)
287 if ( (gc->max_op_generation < ee->generation_added) ||
288 (ee->generation_removed > gc->min_op_generation) )
290 GNUNET_assert (GNUNET_YES ==
291 GNUNET_CONTAINER_multihashmap_remove (gc->map,
301 * Collect and destroy elements that are not needed anymore, because
302 * their lifetime (as determined by their generation) does not overlap
303 * with any active set operation.
305 * @param set set to garbage collect
308 collect_generation_garbage (struct Set *set)
310 struct Operation *op;
311 struct GarbageContext gc;
313 gc.min_op_generation = UINT_MAX;
314 gc.max_op_generation = 0;
315 for (op = set->ops_head; NULL != op; op = op->next)
317 gc.min_op_generation = GNUNET_MIN (gc.min_op_generation,
318 op->generation_created);
319 gc.max_op_generation = GNUNET_MAX (gc.max_op_generation,
320 op->generation_created);
322 gc.map = set->elements;
323 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
330 * Destroy the given operation. Call the implementation-specific
331 * cancel function of the operation. Disconnects from the remote
332 * peer. Does not disconnect the client, as there may be multiple
333 * operations per set.
335 * @param op operation to destroy
336 * @param gc #GNUNET_YES to perform garbage collection on the set
339 _GSS_operation_destroy (struct Operation *op,
343 struct GNUNET_CADET_Channel *channel;
347 /* already in #_GSS_operation_destroy() */
350 GNUNET_assert (GNUNET_NO == op->is_incoming);
351 GNUNET_assert (NULL != op->spec);
353 GNUNET_CONTAINER_DLL_remove (op->spec->set->ops_head,
354 op->spec->set->ops_tail,
358 if (NULL != op->spec)
360 if (NULL != op->spec->context_msg)
362 GNUNET_free (op->spec->context_msg);
363 op->spec->context_msg = NULL;
365 GNUNET_free (op->spec);
370 GNUNET_MQ_destroy (op->mq);
373 if (NULL != (channel = op->channel))
376 GNUNET_CADET_channel_destroy (channel);
378 if (GNUNET_YES == gc)
379 collect_generation_garbage (set);
380 /* We rely on the channel end handler to free 'op'. When 'op->channel' was NULL,
381 * there was a channel end handler that will free 'op' on the call stack. */
386 * Iterator over hash map entries to free element entries.
389 * @param key current key code
390 * @param value a `struct ElementEntry *` to be free'd
391 * @return #GNUNET_YES (continue to iterate)
394 destroy_elements_iterator (void *cls,
395 const struct GNUNET_HashCode *key,
398 struct ElementEntry *ee = value;
406 * Destroy a set, and free all resources and operations associated with it.
408 * @param set the set to destroy
411 set_destroy (struct Set *set)
413 if (NULL != set->client)
415 /* If the client is not dead yet, destroy it. The client's destroy
416 * callback will call `set_destroy()` again in this case. We do
417 * this so that the channel end handler still has a valid set handle
419 struct GNUNET_SERVER_Client *client = set->client;
422 GNUNET_SERVER_client_disconnect (client);
425 GNUNET_assert (NULL != set->state);
426 while (NULL != set->ops_head)
427 _GSS_operation_destroy (set->ops_head, GNUNET_NO);
428 set->vt->destroy_set (set->state);
430 if (NULL != set->client_mq)
432 GNUNET_MQ_destroy (set->client_mq);
433 set->client_mq = NULL;
435 if (NULL != set->iter)
437 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
440 if (NULL != set->elements)
442 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
443 &destroy_elements_iterator,
445 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
446 set->elements = NULL;
448 GNUNET_CONTAINER_DLL_remove (sets_head,
456 * Clean up after a client has disconnected
458 * @param cls closure, unused
459 * @param client the client to clean up after
462 handle_client_disconnect (void *cls,
463 struct GNUNET_SERVER_Client *client)
466 struct Listener *listener;
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 "client disconnected, cleaning up\n");
470 set = set_get (client);
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Client's set destroyed\n");
478 listener = listener_get (client);
479 if (NULL != listener)
481 listener->client = NULL;
482 listener_destroy (listener);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Client's listener destroyed\n");
490 * Destroy an incoming request from a remote peer
492 * @param incoming remote request to destroy
495 incoming_destroy (struct Operation *incoming)
497 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
498 GNUNET_CONTAINER_DLL_remove (incoming_head,
501 if (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task)
503 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
504 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
506 GNUNET_assert (NULL != incoming->state);
507 GNUNET_free (incoming->state);
508 /* make sure that the tunnel end handler will not destroy us again */
510 incoming->state = NULL;
511 if (NULL != incoming->mq)
513 GNUNET_MQ_destroy (incoming->mq);
516 if (NULL != incoming->channel)
518 GNUNET_CADET_channel_destroy (incoming->channel);
519 incoming->channel = NULL;
525 * Find a listener that is interested in the given operation type
526 * and application id.
528 * @param op operation type to look for
529 * @param app_id application id to look for
530 * @return a matching listener, or NULL if no listener matches the
531 * given operation and application id
533 static struct Listener *
534 listener_get_by_target (enum GNUNET_SET_OperationType op,
535 const struct GNUNET_HashCode *app_id)
539 for (l = listeners_head; NULL != l; l = l->next)
540 if ( (l->operation == op) &&
541 (0 == GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id)) )
547 // ----------------------
549 * Suggest the given request to the listener. The listening client can
550 * then accept or reject the remote request.
552 * @param incoming the incoming peer with the request to suggest
553 * @param listener the listener to suggest the request to
556 incoming_suggest (struct Operation *incoming,
557 struct Listener *listener)
559 struct GNUNET_MQ_Envelope *mqm;
560 struct GNUNET_SET_RequestMessage *cmsg;
562 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
563 GNUNET_assert (NULL != incoming->state);
564 GNUNET_assert (NULL != incoming->spec);
565 GNUNET_assert (0 == incoming->state->suggest_id);
566 incoming->state->suggest_id = suggest_id++;
568 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task);
569 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
570 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
572 mqm = GNUNET_MQ_msg_nested_mh (cmsg,
573 GNUNET_MESSAGE_TYPE_SET_REQUEST,
574 incoming->spec->context_msg);
575 GNUNET_assert (NULL != mqm);
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "suggesting request with accept id %u\n",
578 incoming->state->suggest_id);
579 cmsg->accept_id = htonl (incoming->state->suggest_id);
580 cmsg->peer_id = incoming->spec->peer;
581 GNUNET_MQ_send (listener->client_mq, mqm);
586 * Handle a request for a set operation from
589 * This msg is expected as the first and only msg handled through the
590 * non-operation bound virtual table, acceptance of this operation replaces
591 * our virtual table and subsequent msgs would be routed differently.
593 * @param op the operation state
594 * @param mh the received message
595 * @return #GNUNET_OK if the channel should be kept alive,
596 * #GNUNET_SYSERR to destroy the channel
599 handle_incoming_msg (struct Operation *op,
600 const struct GNUNET_MessageHeader *mh)
602 const struct OperationRequestMessage *msg;
603 struct Listener *listener;
604 struct OperationSpecification *spec;
606 msg = (const struct OperationRequestMessage *) mh;
607 GNUNET_assert (GNUNET_YES == op->is_incoming);
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
615 return GNUNET_SYSERR;
618 /* double operation request */
619 if (NULL != op->spec)
622 return GNUNET_SYSERR;
625 spec = GNUNET_new (struct OperationSpecification);
626 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
627 // for simplicity we just backup the context msg instead of rebuilding it later on
628 if (NULL != spec->context_msg)
629 spec->context_msg = GNUNET_copy_message (spec->context_msg);
630 spec->operation = ntohl (msg->operation);
631 spec->app_id = msg->app_id;
632 spec->salt = ntohl (msg->salt);
633 spec->peer = op->state->peer;
634 spec->remote_element_count = ntohl (msg->element_count);
638 if ( (NULL != spec->context_msg) &&
639 (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
642 return GNUNET_SYSERR;
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "received P2P operation request (op %u, app %s)\n",
647 ntohl (msg->operation),
648 GNUNET_h2s (&msg->app_id));
649 listener = listener_get_by_target (ntohl (msg->operation),
651 if (NULL == listener)
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "no listener matches incoming request, waiting with timeout\n");
657 incoming_suggest (op, listener);
663 * Send the next element of a set to the set's client. The next element is given by
664 * the set's current hashmap iterator. The set's iterator will be set to NULL if there
665 * are no more elements in the set. The caller must ensure that the set's iterator is
668 * @param set set that should send its next element to its client
671 send_client_element (struct Set *set)
674 struct ElementEntry *ee;
675 struct GNUNET_MQ_Envelope *ev;
677 GNUNET_assert (NULL != set->iter);
678 ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter,
680 (const void **) &ee);
681 if (GNUNET_NO == ret)
683 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
684 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
689 struct GNUNET_SET_IterResponseMessage *msg;
691 GNUNET_assert (NULL != ee);
692 ev = GNUNET_MQ_msg_extra (msg,
694 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
695 memcpy (&msg[1], ee->element.data, ee->element.size);
696 msg->element_type = ee->element.type;
698 GNUNET_MQ_send (set->client_mq, ev);
703 * Called when a client wants to iterate the elements of a set.
706 * @param client client that sent the message
707 * @param m message sent by the client
710 handle_client_iterate (void *cls,
711 struct GNUNET_SERVER_Client *client,
712 const struct GNUNET_MessageHeader *m)
716 // iterate over a non existing set
717 set = set_get (client);
721 GNUNET_SERVER_client_disconnect (client);
725 // only one concurrent iterate-action per set
726 if (NULL != set->iter)
729 GNUNET_SERVER_client_disconnect (client);
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
733 GNUNET_CONTAINER_multihashmap_size (set->elements));
734 GNUNET_SERVER_receive_done (client, GNUNET_OK);
735 set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
736 send_client_element (set);
741 * Called when a client wants to create a new set.
744 * @param client client that sent the message
745 * @param m message sent by the client
748 handle_client_create_set (void *cls,
749 struct GNUNET_SERVER_Client *client,
750 const struct GNUNET_MessageHeader *m)
752 const struct GNUNET_SET_CreateMessage *msg;
755 msg = (const struct GNUNET_SET_CreateMessage *) m;
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "client created new set (operation %u)\n",
758 ntohs (msg->operation));
760 // max. one set per client!
761 if (NULL != set_get (client))
764 GNUNET_SERVER_client_disconnect (client);
768 set = GNUNET_new (struct Set);
770 switch (ntohs (msg->operation))
772 case GNUNET_SET_OPERATION_INTERSECTION:
773 set->vt = _GSS_intersection_vt ();
775 case GNUNET_SET_OPERATION_UNION:
776 set->vt = _GSS_union_vt ();
781 GNUNET_SERVER_client_disconnect (client);
785 set->state = set->vt->create ();
786 set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
787 set->client = client;
788 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
789 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
790 GNUNET_SERVER_receive_done (client, GNUNET_OK);
795 * Called when a client wants to create a new listener.
798 * @param client client that sent the message
799 * @param m message sent by the client
802 handle_client_listen (void *cls,
803 struct GNUNET_SERVER_Client *client,
804 const struct GNUNET_MessageHeader *m)
806 const struct GNUNET_SET_ListenMessage *msg;
807 struct Listener *listener;
808 struct Operation *op;
810 msg = (const struct GNUNET_SET_ListenMessage *) m;
811 /* max. one per client! */
812 if (NULL != listener_get (client))
815 GNUNET_SERVER_client_disconnect (client);
819 listener = GNUNET_new (struct Listener);
820 listener->client = client;
821 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
822 listener->app_id = msg->app_id;
823 listener->operation = ntohl (msg->operation);
824 GNUNET_CONTAINER_DLL_insert_tail (listeners_head,
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "new listener created (op %u, app %s)\n",
830 GNUNET_h2s (&listener->app_id));
832 /* check for incoming requests the listener is interested in */
833 for (op = incoming_head; NULL != op; op = op->next)
835 if (NULL == op->spec)
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "request has no spec yet\n");
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "considering (op: %u, app: %s, suggest: %u)\n",
844 GNUNET_h2s (&op->spec->app_id),
845 op->state->suggest_id);
847 /* don't consider the incoming request if it has been already suggested to a listener */
848 if (0 != op->state->suggest_id)
850 if (listener->operation != op->spec->operation)
852 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "request suggested\n");
856 incoming_suggest (op, listener);
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "considered all incoming requests\n");
860 GNUNET_SERVER_receive_done (client, GNUNET_OK);
865 * Called when the listening client rejects an operation
866 * request by another peer.
869 * @param client client that sent the message
870 * @param m message sent by the client
873 handle_client_reject (void *cls,
874 struct GNUNET_SERVER_Client *client,
875 const struct GNUNET_MessageHeader *m)
877 struct Operation *incoming;
878 const struct GNUNET_SET_AcceptRejectMessage *msg;
880 msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
881 GNUNET_break (0 == ntohl (msg->request_id));
883 // no matching incoming operation for this reject
884 incoming = get_incoming (ntohl (msg->accept_reject_id));
885 if (NULL == incoming)
887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "peer request rejected by client\n");
893 GNUNET_CADET_channel_destroy (incoming->channel);
894 //channel destruction handler called immediately upon destruction
895 GNUNET_SERVER_receive_done (client, GNUNET_OK);
900 * Called when a client wants to add/remove an element to/from a
904 * @param client client that sent the message
905 * @param m message sent by the client
908 handle_client_add_remove (void *cls,
909 struct GNUNET_SERVER_Client *client,
910 const struct GNUNET_MessageHeader *m)
913 const struct GNUNET_SET_ElementMessage *msg;
914 struct GNUNET_SET_Element el;
915 struct ElementEntry *ee;
917 // client without a set requested an operation
918 set = set_get (client);
922 GNUNET_SERVER_client_disconnect (client);
925 GNUNET_SERVER_receive_done (client, GNUNET_OK);
926 msg = (const struct GNUNET_SET_ElementMessage *) m;
927 el.size = ntohs (m->size) - sizeof *msg;
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "client ins/rem element of size %u\n", el.size);
931 if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
933 struct GNUNET_HashCode hash;
935 GNUNET_CRYPTO_hash (el.data, el.size, &hash);
936 ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
939 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
940 "client tried to remove non-existing element\n");
943 if (GNUNET_YES == ee->removed)
945 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
946 "client tried to remove element twice\n");
949 ee->removed = GNUNET_YES;
950 ee->generation_removed = set->current_generation;
951 set->vt->remove (set->state, ee);
955 struct ElementEntry *ee_dup;
957 ee = GNUNET_malloc (el.size + sizeof *ee);
958 ee->element.size = el.size;
959 memcpy (&ee[1], el.data, el.size);
960 ee->element.data = &ee[1];
961 ee->generation_added = set->current_generation;
962 ee->remote = GNUNET_NO;
963 GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
964 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "element inserted twice, ignoring\n");
973 GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
975 set->vt->add (set->state, ee);
981 * Called when a client wants to evaluate a set operation with another peer.
984 * @param client client that sent the message
985 * @param m message sent by the client
988 handle_client_evaluate (void *cls,
989 struct GNUNET_SERVER_Client *client,
990 const struct GNUNET_MessageHeader *m)
993 const struct GNUNET_SET_EvaluateMessage *msg;
994 struct OperationSpecification *spec;
995 struct Operation *op;
997 set = set_get (client);
1001 GNUNET_SERVER_client_disconnect (client);
1005 msg = (const struct GNUNET_SET_EvaluateMessage *) m;
1006 spec = GNUNET_new (struct OperationSpecification);
1007 spec->operation = set->operation;
1008 spec->app_id = msg->app_id;
1009 spec->salt = ntohl (msg->salt);
1010 spec->peer = msg->target_peer;
1012 spec->result_mode = ntohs (msg->result_mode);
1013 spec->client_request_id = ntohl (msg->request_id);
1014 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
1016 // for simplicity we just backup the context msg instead of rebuilding it later on
1017 if (NULL != spec->context_msg)
1018 spec->context_msg = GNUNET_copy_message (spec->context_msg);
1020 op = GNUNET_new (struct Operation);
1022 op->generation_created = set->current_generation++;
1024 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1026 op->channel = GNUNET_CADET_channel_create (cadet, op, &msg->target_peer,
1027 GNUNET_APPLICATION_TYPE_SET,
1028 GNUNET_CADET_OPTION_RELIABLE);
1030 op->mq = GNUNET_CADET_mq_create (op->channel);
1032 set->vt->evaluate (op);
1033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1038 * Handle an ack from a client, and send the next element.
1041 * @param client the client
1042 * @param m the message
1045 handle_client_iter_ack (void *cls,
1046 struct GNUNET_SERVER_Client *client,
1047 const struct GNUNET_MessageHeader *m)
1051 // client without a set requested an operation
1052 set = set_get (client);
1056 GNUNET_SERVER_client_disconnect (client);
1060 // client sent an ack, but we were not expecting one
1061 if (NULL == set->iter)
1064 GNUNET_SERVER_client_disconnect (client);
1068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1069 send_client_element (set);
1074 * Handle a request from the client to
1075 * cancel a running set operation.
1078 * @param client the client
1079 * @param mh the message
1082 handle_client_cancel (void *cls,
1083 struct GNUNET_SERVER_Client *client,
1084 const struct GNUNET_MessageHeader *mh)
1086 const struct GNUNET_SET_CancelMessage *msg =
1087 (const struct GNUNET_SET_CancelMessage *) mh;
1089 struct Operation *op;
1092 // client without a set requested an operation
1093 set = set_get (client);
1097 GNUNET_SERVER_client_disconnect (client);
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "client requested cancel for op %u\n",
1103 ntohl (msg->request_id));
1106 for (op = set->ops_head; NULL != op; op = op->next)
1108 if (op->spec->client_request_id == ntohl (msg->request_id))
1115 /* It may happen that the operation was destroyed due to
1116 * the other peer disconnecting. The client may not know about this
1117 * yet and try to cancel the (non non-existent) operation.
1119 if (GNUNET_NO != found)
1120 _GSS_operation_destroy (op,
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "client canceled non-existent op\n");
1127 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1132 * Handle a request from the client to accept
1133 * a set operation that came from a remote peer.
1134 * We forward the accept to the associated operation for handling
1137 * @param client the client
1138 * @param mh the message
1141 handle_client_accept (void *cls,
1142 struct GNUNET_SERVER_Client *client,
1143 const struct GNUNET_MessageHeader *mh)
1146 const struct GNUNET_SET_AcceptRejectMessage *msg;
1147 struct Operation *op;
1149 msg = (const struct GNUNET_SET_AcceptRejectMessage *) mh;
1151 // client without a set requested an operation
1152 set = set_get (client);
1157 GNUNET_SERVER_client_disconnect (client);
1161 op = get_incoming (ntohl (msg->accept_reject_id));
1163 /* it is not an error if the set op does not exist -- it may
1164 * have been destroyed when the partner peer disconnected. */
1167 struct GNUNET_SET_ResultMessage *result_message;
1168 struct GNUNET_MQ_Envelope *ev;
1169 ev = GNUNET_MQ_msg (result_message, GNUNET_MESSAGE_TYPE_SET_RESULT);
1170 result_message->request_id = msg->request_id;
1171 result_message->element_type = 0;
1172 result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1173 GNUNET_MQ_send (set->client_mq, ev);
1174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "client accepting %u\n",
1180 ntohl (msg->accept_reject_id));
1182 GNUNET_assert (GNUNET_YES == op->is_incoming);
1185 op->spec->set = set;
1187 GNUNET_assert (GNUNET_YES == op->is_incoming);
1188 op->is_incoming = GNUNET_NO;
1189 GNUNET_assert (NULL != op->state);
1190 GNUNET_free (op->state);
1192 GNUNET_CONTAINER_DLL_remove (incoming_head,
1196 GNUNET_assert (NULL != op->spec->set);
1197 GNUNET_assert (NULL != op->spec->set->vt);
1199 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1203 op->spec->client_request_id = ntohl (msg->request_id);
1204 op->spec->result_mode = ntohs (msg->result_mode);
1205 op->generation_created = set->current_generation++;
1206 op->vt = op->spec->set->vt;
1207 GNUNET_assert (NULL != op->vt->accept);
1208 set->vt->accept (op);
1209 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1214 * Called to clean up, after a shutdown has been requested.
1216 * @param cls closure
1217 * @param tc context information (why was this task triggered now)
1220 shutdown_task (void *cls,
1221 const struct GNUNET_SCHEDULER_TaskContext *tc)
1223 while (NULL != incoming_head)
1224 incoming_destroy (incoming_head);
1226 while (NULL != listeners_head)
1227 listener_destroy (listeners_head);
1229 while (NULL != sets_head)
1230 set_destroy (sets_head);
1232 /* it's important to destroy cadet at the end, as all channels
1233 * must be destroyed before the cadet handle! */
1236 GNUNET_CADET_disconnect (cadet);
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 "handled shutdown request\n");
1245 * Timeout happens iff:
1246 * - we suggested an operation to our listener,
1247 * but did not receive a response in time
1248 * - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1249 * - shutdown (obviously)
1251 * @param cls channel context
1252 * @param tc context information (why was this task triggered now)
1255 incoming_timeout_cb (void *cls,
1256 const struct GNUNET_SCHEDULER_TaskContext *tc)
1258 struct Operation *incoming = cls;
1260 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1261 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1262 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "remote peer timed out\n");
1266 incoming_destroy (incoming);
1271 * Terminates an incoming operation in case we have not yet received an
1272 * operation request. Called by the channel destruction handler.
1274 * @param op the channel context
1277 handle_incoming_disconnect (struct Operation *op)
1279 GNUNET_assert (GNUNET_YES == op->is_incoming);
1280 /* channel is already dead, incoming_destroy must not
1283 incoming_destroy (op);
1289 * Method called whenever another peer has added us to a channel
1290 * the other peer initiated.
1291 * Only called (once) upon reception of data with a message type which was
1292 * subscribed to in GNUNET_CADET_connect().
1294 * The channel context represents the operation itself and gets added to a DLL,
1295 * from where it gets looked up when our local listener client responds
1296 * to a proposed/suggested operation or connects and associates with this operation.
1298 * @param cls closure
1299 * @param channel new handle to the channel
1300 * @param initiator peer that started the channel
1301 * @param port Port this channel is for.
1302 * @param options Unused.
1303 * @return initial channel context for the channel
1304 * (can be NULL -- that's not an error)
1307 channel_new_cb (void *cls,
1308 struct GNUNET_CADET_Channel *channel,
1309 const struct GNUNET_PeerIdentity *initiator,
1310 uint32_t port, enum GNUNET_CADET_ChannelOption options)
1312 struct Operation *incoming;
1313 static const struct SetVT incoming_vt = {
1314 .msg_handler = handle_incoming_msg,
1315 .peer_disconnect = handle_incoming_disconnect
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "new incoming channel\n");
1321 if (GNUNET_APPLICATION_TYPE_SET != port)
1324 GNUNET_CADET_channel_destroy (channel);
1328 incoming = GNUNET_new (struct Operation);
1329 incoming->is_incoming = GNUNET_YES;
1330 incoming->state = GNUNET_new (struct OperationState);
1331 incoming->state->peer = *initiator;
1332 incoming->channel = channel;
1333 incoming->mq = GNUNET_CADET_mq_create (incoming->channel);
1334 incoming->vt = &incoming_vt;
1335 incoming->state->timeout_task =
1336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1337 &incoming_timeout_cb,
1339 GNUNET_CONTAINER_DLL_insert_tail (incoming_head,
1348 * Function called whenever a channel is destroyed. Should clean up
1349 * any associated state. It must NOT call
1350 * GNUNET_CADET_channel_destroy() on the channel.
1352 * The peer_disconnect function is part of a a virtual table set initially either
1353 * when a peer creates a new channel with us (channel_new_cb), or once we create
1354 * a new channel ourselves (evaluate).
1356 * Once we know the exact type of operation (union/intersection), the vt is
1357 * replaced with an operation specific instance (_GSS_[op]_vt).
1359 * @param cls closure (set from GNUNET_CADET_connect())
1360 * @param channel connection to the other end (henceforth invalid)
1361 * @param channel_ctx place where local state associated
1362 * with the channel is stored
1365 channel_end_cb (void *cls,
1366 const struct GNUNET_CADET_Channel *channel,
1369 struct Operation *op = channel_ctx;
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "channel end cb called\n");
1374 /* the vt can be null if a client already requested canceling op. */
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "calling peer disconnect due to channel end\n");
1379 op->vt->peer_disconnect (op);
1382 if (GNUNET_YES == op->keep)
1385 /* cadet will never call us with the context again! */
1386 GNUNET_free (channel_ctx);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "channel end cb finished\n");
1393 * Functions with this signature are called whenever a message is
1394 * received via a cadet channel.
1396 * The msg_handler is a virtual table set in initially either when a peer
1397 * creates a new channel with us (channel_new_cb), or once we create a new channel
1398 * ourselves (evaluate).
1400 * Once we know the exact type of operation (union/intersection), the vt is
1401 * replaced with an operation specific instance (_GSS_[op]_vt).
1403 * @param cls Closure (set from GNUNET_CADET_connect()).
1404 * @param channel Connection to the other end.
1405 * @param channel_ctx Place to store local state associated with the channel.
1406 * @param message The actual message.
1407 * @return #GNUNET_OK to keep the channel open,
1408 * #GNUNET_SYSERR to close it (signal serious error).
1411 dispatch_p2p_message (void *cls,
1412 struct GNUNET_CADET_Channel *channel,
1414 const struct GNUNET_MessageHeader *message)
1416 struct Operation *op = *channel_ctx;
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "dispatching cadet message (type: %u)\n",
1421 ntohs (message->type));
1422 /* do this before the handler, as the handler might kill the channel */
1423 GNUNET_CADET_receive_done (channel);
1425 ret = op->vt->msg_handler (op, message);
1427 ret = GNUNET_SYSERR;
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "handled cadet message (type: %u)\n",
1430 ntohs (message->type));
1436 * Function called by the service's run
1437 * method to run service-specific setup code.
1439 * @param cls closure
1440 * @param server the initialized server
1441 * @param cfg configuration to use
1444 run (void *cls, struct GNUNET_SERVER_Handle *server,
1445 const struct GNUNET_CONFIGURATION_Handle *cfg)
1447 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1448 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1449 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1450 {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
1451 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
1452 {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
1453 sizeof (struct GNUNET_SET_CreateMessage)},
1454 {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1455 sizeof (struct GNUNET_MessageHeader)},
1456 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
1457 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
1458 sizeof (struct GNUNET_SET_ListenMessage)},
1459 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
1460 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1461 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
1462 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
1463 sizeof (struct GNUNET_SET_CancelMessage)},
1466 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1467 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1468 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1469 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1470 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
1471 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1472 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1473 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
1474 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1475 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART, 0},
1478 static const uint32_t cadet_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1480 configuration = cfg;
1481 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1482 &shutdown_task, NULL);
1483 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1484 GNUNET_SERVER_add_handlers (server, server_handlers);
1486 cadet = GNUNET_CADET_connect (cfg, NULL, channel_new_cb, channel_end_cb,
1487 cadet_handlers, cadet_ports);
1490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1491 _("Could not connect to cadet service\n"));
1498 * The main function for the set service.
1500 * @param argc number of arguments from the command line
1501 * @param argv command line arguments
1502 * @return 0 ok, 1 on error
1505 main (int argc, char *const *argv)
1509 ret = GNUNET_SERVICE_run (argc, argv, "set",
1510 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1511 return (GNUNET_OK == ret) ? 0 : 1;
1514 /* end of gnunet-service-set.c */