2 This file is part of GNUnet
3 (C) 2013, 2014 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.
21 * @file set/gnunet-service-set.c
22 * @brief two-peer set operations
23 * @author Florian Dold
24 * @author Christian Grothoff
26 #include "gnunet-service-set.h"
27 #include "gnunet-service-set_protocol.h"
30 * How long do we hold on to an incoming channel if there is
31 * no local listener before giving up?
33 #define INCOMING_CHANNEL_TIMEOUT GNUNET_TIME_UNIT_MINUTES
36 * A listener is inhabited by a client, and waits for evaluation
37 * requests from remote peers.
42 * Listeners are held in a doubly linked list.
44 struct Listener *next;
47 * Listeners are held in a doubly linked list.
49 struct Listener *prev;
52 * Client that owns the listener.
53 * Only one client may own a listener.
55 struct GNUNET_SERVER_Client *client;
58 * Message queue for the client
60 struct GNUNET_MQ_Handle *client_mq;
63 * Application ID for the operation, used to distinguish
64 * multiple operations of the same type with the same peer.
66 struct GNUNET_HashCode app_id;
69 * The type of the operation.
71 enum GNUNET_SET_OperationType operation;
76 * Configuration of our local peer.
78 static const struct GNUNET_CONFIGURATION_Handle *configuration;
81 * Handle to the cadet service, used to listen for and connect to
84 static struct GNUNET_CADET_Handle *cadet;
87 * Sets are held in a doubly linked list.
89 static struct Set *sets_head;
92 * Sets are held in a doubly linked list.
94 static struct Set *sets_tail;
97 * Listeners are held in a doubly linked list.
99 static struct Listener *listeners_head;
102 * Listeners are held in a doubly linked list.
104 static struct Listener *listeners_tail;
107 * Incoming sockets from remote peers are held in a doubly linked
110 static struct Operation *incoming_head;
113 * Incoming sockets from remote peers are held in a doubly linked
116 static struct Operation *incoming_tail;
119 * Counter for allocating unique IDs for clients, used to identify
120 * incoming operation requests from remote peers, that the client can
121 * choose to accept or refuse.
123 static uint32_t suggest_id = 1;
127 * Get set that is owned by the given client, if any.
129 * @param client client to look for
130 * @return set that the client owns, NULL if the client
134 set_get (struct GNUNET_SERVER_Client *client)
138 for (set = sets_head; NULL != set; set = set->next)
139 if (set->client == client)
146 * Get the listener associated with the given client, if any.
148 * @param client the client
149 * @return listener associated with the client, NULL
152 static struct Listener *
153 listener_get (struct GNUNET_SERVER_Client *client)
155 struct Listener *listener;
157 for (listener = listeners_head; NULL != listener; listener = listener->next)
158 if (listener->client == client)
165 * Get the incoming socket associated with the given id.
167 * @param id id to look for
168 * @return the incoming socket associated with the id,
169 * or NULL if there is none
171 static struct Operation *
172 get_incoming (uint32_t id)
174 struct Operation *op;
176 for (op = incoming_head; NULL != op; op = op->next)
177 if (op->suggest_id == id)
179 GNUNET_assert (GNUNET_YES == op->is_incoming);
187 * Destroy a listener, free all resources associated with it.
189 * @param listener listener to destroy
192 listener_destroy (struct Listener *listener)
194 /* If the client is not dead yet, destroy it.
195 * The client's destroy callback will destroy the listener again. */
196 if (NULL != listener->client)
198 struct GNUNET_SERVER_Client *client = listener->client;
200 listener->client = NULL;
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Disconnecting listener client\n");
203 GNUNET_SERVER_client_disconnect (client);
206 if (NULL != listener->client_mq)
208 GNUNET_MQ_destroy (listener->client_mq);
209 listener->client_mq = NULL;
211 GNUNET_CONTAINER_DLL_remove (listeners_head,
214 GNUNET_free (listener);
219 * Context for the #garbage_collect_cb().
221 struct GarbageContext
225 * Map for which we are garbage collecting removed elements.
227 struct GNUNET_CONTAINER_MultiHashMap *map;
230 * Lowest generation for which an operation is still pending.
232 unsigned int min_op_generation;
235 * Largest generation for which an operation is still pending.
237 unsigned int max_op_generation;
243 * Function invoked to check if an element can be removed from
244 * the set's history because it is no longer needed.
246 * @param cls the `struct GarbageContext *`
247 * @param key key of the element in the map
248 * @param value the `struct ElementEntry *`
249 * @return #GNUNET_OK (continue to iterate)
252 garbage_collect_cb (void *cls,
253 const struct GNUNET_HashCode *key,
256 struct GarbageContext *gc = cls;
257 struct ElementEntry *ee = value;
259 if (GNUNET_YES != ee->removed)
261 if ( (gc->max_op_generation < ee->generation_added) ||
262 (ee->generation_removed > gc->min_op_generation) )
264 GNUNET_assert (GNUNET_YES ==
265 GNUNET_CONTAINER_multihashmap_remove (gc->map,
275 * Collect and destroy elements that are not needed anymore, because
276 * their lifetime (as determined by their generation) does not overlap
277 * with any active set operation.
279 * @param set set to garbage collect
282 collect_generation_garbage (struct Set *set)
284 struct Operation *op;
285 struct GarbageContext gc;
287 gc.min_op_generation = UINT_MAX;
288 gc.max_op_generation = 0;
289 for (op = set->ops_head; NULL != op; op = op->next)
291 gc.min_op_generation = GNUNET_MIN (gc.min_op_generation,
292 op->generation_created);
293 gc.max_op_generation = GNUNET_MAX (gc.max_op_generation,
294 op->generation_created);
296 gc.map = set->elements;
297 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
304 * Destroy the given operation. Call the implementation-specific
305 * cancel function of the operation. Disconnects from the remote
306 * peer. Does not disconnect the client, as there may be multiple
307 * operations per set.
309 * @param op operation to destroy
310 * @param gc #GNUNET_YES to perform garbage collection on the set
313 _GSS_operation_destroy (struct Operation *op,
317 struct GNUNET_CADET_Channel *channel;
321 /* already in #_GSS_operation_destroy() */
324 GNUNET_assert (GNUNET_NO == op->is_incoming);
325 GNUNET_assert (NULL != op->spec);
327 GNUNET_CONTAINER_DLL_remove (set->ops_head,
332 if (NULL != op->spec)
334 if (NULL != op->spec->context_msg)
336 GNUNET_free (op->spec->context_msg);
337 op->spec->context_msg = NULL;
339 GNUNET_free (op->spec);
344 GNUNET_MQ_destroy (op->mq);
347 if (NULL != (channel = op->channel))
350 GNUNET_CADET_channel_destroy (channel);
352 if (GNUNET_YES == gc)
353 collect_generation_garbage (set);
354 /* We rely on the channel end handler to free 'op'. When 'op->channel' was NULL,
355 * there was a channel end handler that will free 'op' on the call stack. */
360 * Iterator over hash map entries to free element entries.
363 * @param key current key code
364 * @param value a `struct ElementEntry *` to be free'd
365 * @return #GNUNET_YES (continue to iterate)
368 destroy_elements_iterator (void *cls,
369 const struct GNUNET_HashCode *key,
372 struct ElementEntry *ee = value;
380 * Destroy a set, and free all resources and operations associated with it.
382 * @param set the set to destroy
385 set_destroy (struct Set *set)
387 if (NULL != set->client)
389 /* If the client is not dead yet, destroy it. The client's destroy
390 * callback will call `set_destroy()` again in this case. We do
391 * this so that the channel end handler still has a valid set handle
393 struct GNUNET_SERVER_Client *client = set->client;
396 GNUNET_SERVER_client_disconnect (client);
399 GNUNET_assert (NULL != set->state);
400 while (NULL != set->ops_head)
401 _GSS_operation_destroy (set->ops_head, GNUNET_NO);
402 set->vt->destroy_set (set->state);
404 if (NULL != set->client_mq)
406 GNUNET_MQ_destroy (set->client_mq);
407 set->client_mq = NULL;
409 if (NULL != set->iter)
411 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
415 if (NULL != set->elements)
417 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
418 &destroy_elements_iterator,
420 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
421 set->elements = NULL;
423 GNUNET_CONTAINER_DLL_remove (sets_head,
431 * Clean up after a client has disconnected
433 * @param cls closure, unused
434 * @param client the client to clean up after
437 handle_client_disconnect (void *cls,
438 struct GNUNET_SERVER_Client *client)
441 struct Listener *listener;
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 "client disconnected, cleaning up\n");
445 set = set_get (client);
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Client's set destroyed\n");
453 listener = listener_get (client);
454 if (NULL != listener)
456 listener->client = NULL;
457 listener_destroy (listener);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Client's listener destroyed\n");
465 * Destroy an incoming request from a remote peer
467 * @param incoming remote request to destroy
470 incoming_destroy (struct Operation *incoming)
472 struct GNUNET_CADET_Channel *channel;
474 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
475 GNUNET_CONTAINER_DLL_remove (incoming_head,
478 if (NULL != incoming->timeout_task)
480 GNUNET_SCHEDULER_cancel (incoming->timeout_task);
481 incoming->timeout_task = NULL;
483 /* make sure that the tunnel end handler will not destroy us again */
485 if (NULL != incoming->spec)
487 GNUNET_free (incoming->spec);
488 incoming->spec = NULL;
490 if (NULL != incoming->mq)
492 GNUNET_MQ_destroy (incoming->mq);
495 if (NULL != (channel = incoming->channel))
497 incoming->channel = NULL;
498 GNUNET_CADET_channel_destroy (channel);
504 * Find a listener that is interested in the given operation type
505 * and application id.
507 * @param op operation type to look for
508 * @param app_id application id to look for
509 * @return a matching listener, or NULL if no listener matches the
510 * given operation and application id
512 static struct Listener *
513 listener_get_by_target (enum GNUNET_SET_OperationType op,
514 const struct GNUNET_HashCode *app_id)
516 struct Listener *listener;
518 for (listener = listeners_head; NULL != listener; listener = listener->next)
519 if ( (listener->operation == op) &&
520 (0 == GNUNET_CRYPTO_hash_cmp (app_id, &listener->app_id)) )
527 * Suggest the given request to the listener. The listening client can
528 * then accept or reject the remote request.
530 * @param incoming the incoming peer with the request to suggest
531 * @param listener the listener to suggest the request to
534 incoming_suggest (struct Operation *incoming,
535 struct Listener *listener)
537 struct GNUNET_MQ_Envelope *mqm;
538 struct GNUNET_SET_RequestMessage *cmsg;
540 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
541 GNUNET_assert (NULL != incoming->spec);
542 GNUNET_assert (0 == incoming->suggest_id);
543 incoming->suggest_id = suggest_id++;
546 GNUNET_assert (NULL != incoming->timeout_task);
547 GNUNET_SCHEDULER_cancel (incoming->timeout_task);
548 incoming->timeout_task = NULL;
549 mqm = GNUNET_MQ_msg_nested_mh (cmsg,
550 GNUNET_MESSAGE_TYPE_SET_REQUEST,
551 incoming->spec->context_msg);
552 GNUNET_assert (NULL != mqm);
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "Suggesting incoming request with accept id %u to listener\n",
555 incoming->suggest_id);
556 cmsg->accept_id = htonl (incoming->suggest_id);
557 cmsg->peer_id = incoming->spec->peer;
558 GNUNET_MQ_send (listener->client_mq, mqm);
563 * Handle a request for a set operation from another peer. Checks if we
564 * have a listener waiting for such a request (and in that case initiates
565 * asking the listener about accepting the connection). If no listener
566 * is waiting, we queue the operation request in hope that a listener
567 * shows up soon (before timeout).
569 * This msg is expected as the first and only msg handled through the
570 * non-operation bound virtual table, acceptance of this operation replaces
571 * our virtual table and subsequent msgs would be routed differently (as
572 * we then know what type of operation this is).
574 * @param op the operation state
575 * @param mh the received message
576 * @return #GNUNET_OK if the channel should be kept alive,
577 * #GNUNET_SYSERR to destroy the channel
580 handle_incoming_msg (struct Operation *op,
581 const struct GNUNET_MessageHeader *mh)
583 const struct OperationRequestMessage *msg;
584 struct Listener *listener;
585 struct OperationSpecification *spec;
586 const struct GNUNET_MessageHeader *nested_context;
588 msg = (const struct OperationRequestMessage *) mh;
589 GNUNET_assert (GNUNET_YES == op->is_incoming);
590 if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
593 return GNUNET_SYSERR;
595 /* double operation request */
596 if (NULL != op->spec)
599 return GNUNET_SYSERR;
601 spec = GNUNET_new (struct OperationSpecification);
602 nested_context = GNUNET_MQ_extract_nested_mh (msg);
603 if ( (NULL != nested_context) &&
604 (ntohs (nested_context->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
608 return GNUNET_SYSERR;
610 /* Make a copy of the nested_context (application-specific context
611 information that is opaque to set) so we can pass it to the
613 if (NULL != nested_context)
614 spec->context_msg = GNUNET_copy_message (nested_context);
615 spec->operation = ntohl (msg->operation);
616 spec->app_id = msg->app_id;
617 spec->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
619 spec->peer = op->peer;
620 spec->remote_element_count = ntohl (msg->element_count);
623 listener = listener_get_by_target (ntohl (msg->operation),
625 if (NULL == listener)
627 GNUNET_break (NULL != op->timeout_task);
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "No matching listener for incoming request (op %u, app %s), waiting with timeout\n",
630 ntohl (msg->operation),
631 GNUNET_h2s (&msg->app_id));
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Received P2P operation request (op %u, app %s) for active listener\n",
636 ntohl (msg->operation),
637 GNUNET_h2s (&msg->app_id));
638 incoming_suggest (op, listener);
644 * Send the next element of a set to the set's client. The next element is given by
645 * the set's current hashmap iterator. The set's iterator will be set to NULL if there
646 * are no more elements in the set. The caller must ensure that the set's iterator is
649 * The client will acknowledge each received element with a
650 * #GNUNET_MESSAGE_TYPE_SET_ITER_ACK message. Our
651 * #handle_client_iter_ack() will then trigger the next transmission.
652 * Note that the #GNUNET_MESSAGE_TYPE_SET_ITER_DONE is not acknowledged.
654 * @param set set that should send its next element to its client
657 send_client_element (struct Set *set)
660 struct ElementEntry *ee;
661 struct GNUNET_MQ_Envelope *ev;
662 struct GNUNET_SET_IterResponseMessage *msg;
664 GNUNET_assert (NULL != set->iter);
665 ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter,
667 (const void **) &ee);
668 if (GNUNET_NO == ret)
670 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
671 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
677 GNUNET_assert (NULL != ee);
678 ev = GNUNET_MQ_msg_extra (msg,
680 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
684 msg->element_type = ee->element.element_type;
685 msg->iteration_id = htons (set->iteration_id);
687 GNUNET_MQ_send (set->client_mq, ev);
692 * Called when a client wants to iterate the elements of a set.
693 * Checks if we have a set associated with the client and if we
694 * can right now start an iteration. If all checks out, starts
695 * sending the elements of the set to the client.
698 * @param client client that sent the message
699 * @param m message sent by the client
702 handle_client_iterate (void *cls,
703 struct GNUNET_SERVER_Client *client,
704 const struct GNUNET_MessageHeader *m)
708 set = set_get (client);
711 /* attempt to iterate over a non existing set */
713 GNUNET_SERVER_client_disconnect (client);
716 if (NULL != set->iter)
718 /* Only one concurrent iterate-action allowed per set */
720 GNUNET_SERVER_client_disconnect (client);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Iterating set with %u elements\n",
725 GNUNET_CONTAINER_multihashmap_size (set->elements));
726 GNUNET_SERVER_receive_done (client,
728 set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
729 send_client_element (set);
734 * Called when a client wants to create a new set. This is typically
735 * the first request from a client, and includes the type of set
736 * operation to be performed.
739 * @param client client that sent the message
740 * @param m message sent by the client
743 handle_client_create_set (void *cls,
744 struct GNUNET_SERVER_Client *client,
745 const struct GNUNET_MessageHeader *m)
747 const struct GNUNET_SET_CreateMessage *msg;
750 msg = (const struct GNUNET_SET_CreateMessage *) m;
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Client created new set (operation %u)\n",
753 ntohl (msg->operation));
754 if (NULL != set_get (client))
756 /* There can only be one set per client */
758 GNUNET_SERVER_client_disconnect (client);
761 set = GNUNET_new (struct Set);
762 switch (ntohl (msg->operation))
764 case GNUNET_SET_OPERATION_INTERSECTION:
765 set->vt = _GSS_intersection_vt ();
767 case GNUNET_SET_OPERATION_UNION:
768 set->vt = _GSS_union_vt ();
773 GNUNET_SERVER_client_disconnect (client);
776 set->state = set->vt->create ();
777 set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
778 set->client = client;
779 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
780 GNUNET_CONTAINER_DLL_insert (sets_head,
783 GNUNET_SERVER_receive_done (client,
789 * Called when a client wants to create a new listener.
792 * @param client client that sent the message
793 * @param m message sent by the client
796 handle_client_listen (void *cls,
797 struct GNUNET_SERVER_Client *client,
798 const struct GNUNET_MessageHeader *m)
800 const struct GNUNET_SET_ListenMessage *msg;
801 struct Listener *listener;
802 struct Operation *op;
804 msg = (const struct GNUNET_SET_ListenMessage *) m;
805 if (NULL != listener_get (client))
807 /* max. one active listener per client! */
809 GNUNET_SERVER_client_disconnect (client);
812 listener = GNUNET_new (struct Listener);
813 listener->client = client;
814 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
815 listener->app_id = msg->app_id;
816 listener->operation = ntohl (msg->operation);
817 GNUNET_CONTAINER_DLL_insert_tail (listeners_head,
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "New listener created (op %u, app %s)\n",
823 GNUNET_h2s (&listener->app_id));
825 /* check for existing incoming requests the listener might be interested in */
826 for (op = incoming_head; NULL != op; op = op->next)
828 if (NULL == op->spec)
829 continue; /* no details available yet */
830 if (0 != op->suggest_id)
831 continue; /* this one has been already suggested to a listener */
832 if (listener->operation != op->spec->operation)
833 continue; /* incompatible operation */
834 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id,
836 continue; /* incompatible appliation */
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Found matching existing request\n");
839 incoming_suggest (op,
842 GNUNET_SERVER_receive_done (client, GNUNET_OK);
847 * Called when the listening client rejects an operation
848 * request by another peer.
851 * @param client client that sent the message
852 * @param m message sent by the client
855 handle_client_reject (void *cls,
856 struct GNUNET_SERVER_Client *client,
857 const struct GNUNET_MessageHeader *m)
859 struct Operation *incoming;
860 const struct GNUNET_SET_RejectMessage *msg;
862 msg = (const struct GNUNET_SET_RejectMessage *) m;
863 incoming = get_incoming (ntohl (msg->accept_reject_id));
864 if (NULL == incoming)
866 /* no matching incoming operation for this reject */
868 GNUNET_SERVER_receive_done (client,
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Peer request (op %u, app %s) rejected by client\n",
874 incoming->spec->operation,
875 GNUNET_h2s (&incoming->spec->app_id));
876 GNUNET_CADET_channel_destroy (incoming->channel);
877 GNUNET_SERVER_receive_done (client,
883 * Called when a client wants to add an element to a set it inhabits.
886 * @param client client that sent the message
887 * @param m message sent by the client
890 handle_client_add (void *cls,
891 struct GNUNET_SERVER_Client *client,
892 const struct GNUNET_MessageHeader *m)
895 const struct GNUNET_SET_ElementMessage *msg;
896 struct GNUNET_SET_Element el;
897 struct ElementEntry *ee;
898 struct ElementEntry *ee_dup;
900 set = set_get (client);
903 /* client without a set requested an operation */
905 GNUNET_SERVER_client_disconnect (client);
908 GNUNET_SERVER_receive_done (client,
910 msg = (const struct GNUNET_SET_ElementMessage *) m;
911 el.size = ntohs (m->size) - sizeof *msg;
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Client inserts element of size %u\n",
916 ee = GNUNET_malloc (el.size + sizeof *ee);
917 ee->element.size = el.size;
921 ee->element.data = &ee[1];
922 ee->generation_added = set->current_generation;
923 ee->remote = GNUNET_NO;
924 GNUNET_CRYPTO_hash (ee->element.data,
927 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
931 /* same element inserted twice */
936 GNUNET_break (GNUNET_YES ==
937 GNUNET_CONTAINER_multihashmap_put (set->elements,
940 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
941 set->vt->add (set->state, ee);
946 * Called when a client wants to remove an element from a set it
950 * @param client client that sent the message
951 * @param m message sent by the client
954 handle_client_remove (void *cls,
955 struct GNUNET_SERVER_Client *client,
956 const struct GNUNET_MessageHeader *m)
959 const struct GNUNET_SET_ElementMessage *msg;
960 struct GNUNET_SET_Element el;
961 struct ElementEntry *ee;
962 struct GNUNET_HashCode hash;
964 set = set_get (client);
967 /* client without a set requested an operation */
969 GNUNET_SERVER_client_disconnect (client);
972 GNUNET_SERVER_receive_done (client,
974 msg = (const struct GNUNET_SET_ElementMessage *) m;
975 el.size = ntohs (m->size) - sizeof *msg;
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Client removes element of size %u\n",
980 GNUNET_CRYPTO_hash (el.data,
983 ee = GNUNET_CONTAINER_multihashmap_get (set->elements,
987 /* Client tried to remove non-existing element */
991 if (GNUNET_YES == ee->removed)
993 /* Client tried to remove element twice */
997 ee->removed = GNUNET_YES;
998 ee->generation_removed = set->current_generation;
999 set->vt->remove (set->state, ee);
1004 * Called when a client wants to initiate a set operation with another
1005 * peer. Initiates the CADET connection to the listener and sends the
1009 * @param client client that sent the message
1010 * @param m message sent by the client
1013 handle_client_evaluate (void *cls,
1014 struct GNUNET_SERVER_Client *client,
1015 const struct GNUNET_MessageHeader *m)
1018 const struct GNUNET_SET_EvaluateMessage *msg;
1019 struct OperationSpecification *spec;
1020 struct Operation *op;
1021 const struct GNUNET_MessageHeader *context;
1023 set = set_get (client);
1027 GNUNET_SERVER_client_disconnect (client);
1030 msg = (const struct GNUNET_SET_EvaluateMessage *) m;
1031 spec = GNUNET_new (struct OperationSpecification);
1032 spec->operation = set->operation;
1033 spec->app_id = msg->app_id;
1034 spec->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1036 spec->peer = msg->target_peer;
1038 spec->result_mode = ntohl (msg->result_mode);
1039 spec->client_request_id = ntohl (msg->request_id);
1040 context = GNUNET_MQ_extract_nested_mh (msg);
1041 op = GNUNET_new (struct Operation);
1043 op->generation_created = set->current_generation++;
1045 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1048 op->channel = GNUNET_CADET_channel_create (cadet,
1051 GNUNET_APPLICATION_TYPE_SET,
1052 GNUNET_CADET_OPTION_RELIABLE);
1053 op->mq = GNUNET_CADET_mq_create (op->channel);
1054 set->vt->evaluate (op,
1056 GNUNET_SERVER_receive_done (client,
1062 * Handle an ack from a client, and send the next element. Note
1063 * that we only expect acks for set elements, not after the
1064 * #GNUNET_MESSAGE_TYPE_SET_ITER_DONE message.
1067 * @param client the client
1068 * @param m the message
1071 handle_client_iter_ack (void *cls,
1072 struct GNUNET_SERVER_Client *client,
1073 const struct GNUNET_MessageHeader *m)
1075 const struct GNUNET_SET_IterAckMessage *ack;
1078 set = set_get (client);
1081 /* client without a set acknowledged receiving a value */
1083 GNUNET_SERVER_client_disconnect (client);
1086 if (NULL == set->iter)
1088 /* client sent an ack, but we were not expecting one (as
1089 set iteration has finished) */
1091 GNUNET_SERVER_client_disconnect (client);
1094 ack = (const struct GNUNET_SET_IterAckMessage *) m;
1095 GNUNET_SERVER_receive_done (client,
1097 if (ntohl (ack->send_more))
1099 send_client_element (set);
1103 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
1105 set->iteration_id++;
1111 * Handle a request from the client to
1112 * cancel a running set operation.
1115 * @param client the client
1116 * @param mh the message
1119 handle_client_cancel (void *cls,
1120 struct GNUNET_SERVER_Client *client,
1121 const struct GNUNET_MessageHeader *mh)
1123 const struct GNUNET_SET_CancelMessage *msg =
1124 (const struct GNUNET_SET_CancelMessage *) mh;
1126 struct Operation *op;
1129 set = set_get (client);
1132 /* client without a set requested an operation */
1134 GNUNET_SERVER_client_disconnect (client);
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "Client requested cancel for op %u\n",
1139 ntohl (msg->request_id));
1141 for (op = set->ops_head; NULL != op; op = op->next)
1143 if (op->spec->client_request_id == ntohl (msg->request_id))
1149 if (GNUNET_NO == found)
1151 /* It may happen that the operation was already destroyed due to
1152 * the other peer disconnecting. The client may not know about this
1153 * yet and try to cancel the (just barely non-existent) operation.
1154 * So this is not a hard error.
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Client canceled non-existent op\n");
1161 _GSS_operation_destroy (op,
1164 GNUNET_SERVER_receive_done (client,
1170 * Handle a request from the client to accept a set operation that
1171 * came from a remote peer. We forward the accept to the associated
1172 * operation for handling
1175 * @param client the client
1176 * @param mh the message
1179 handle_client_accept (void *cls,
1180 struct GNUNET_SERVER_Client *client,
1181 const struct GNUNET_MessageHeader *mh)
1184 const struct GNUNET_SET_AcceptMessage *msg;
1185 struct Operation *op;
1186 struct GNUNET_SET_ResultMessage *result_message;
1187 struct GNUNET_MQ_Envelope *ev;
1189 msg = (const struct GNUNET_SET_AcceptMessage *) mh;
1190 set = set_get (client);
1193 /* client without a set requested to accept */
1195 GNUNET_SERVER_client_disconnect (client);
1198 op = get_incoming (ntohl (msg->accept_reject_id));
1201 /* It is not an error if the set op does not exist -- it may
1202 * have been destroyed when the partner peer disconnected. */
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Client accepted request that is no longer active\n");
1205 ev = GNUNET_MQ_msg (result_message,
1206 GNUNET_MESSAGE_TYPE_SET_RESULT);
1207 result_message->request_id = msg->request_id;
1208 result_message->element_type = 0;
1209 result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1210 GNUNET_MQ_send (set->client_mq, ev);
1211 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "Client accepting request %u\n",
1217 ntohl (msg->accept_reject_id));
1218 GNUNET_assert (GNUNET_YES == op->is_incoming);
1219 op->is_incoming = GNUNET_NO;
1220 GNUNET_CONTAINER_DLL_remove (incoming_head,
1223 op->spec->set = set;
1224 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1227 op->spec->client_request_id = ntohl (msg->request_id);
1228 op->spec->result_mode = ntohl (msg->result_mode);
1229 op->generation_created = set->current_generation++;
1231 op->vt->accept (op);
1232 GNUNET_SERVER_receive_done (client,
1238 * Called to clean up, after a shutdown has been requested.
1240 * @param cls closure
1241 * @param tc context information (why was this task triggered now)
1244 shutdown_task (void *cls,
1245 const struct GNUNET_SCHEDULER_TaskContext *tc)
1247 while (NULL != incoming_head)
1248 incoming_destroy (incoming_head);
1249 while (NULL != listeners_head)
1250 listener_destroy (listeners_head);
1251 while (NULL != sets_head)
1252 set_destroy (sets_head);
1254 /* it's important to destroy cadet at the end, as all channels
1255 * must be destroyed before the cadet handle! */
1258 GNUNET_CADET_disconnect (cadet);
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "handled shutdown request\n");
1267 * Timeout happens iff:
1268 * - we suggested an operation to our listener,
1269 * but did not receive a response in time
1270 * - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1271 * - shutdown (obviously)
1273 * @param cls channel context
1274 * @param tc context information (why was this task triggered now)
1277 incoming_timeout_cb (void *cls,
1278 const struct GNUNET_SCHEDULER_TaskContext *tc)
1280 struct Operation *incoming = cls;
1282 incoming->timeout_task = NULL;
1283 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1284 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1287 "Remote peer's incoming request timed out\n");
1288 incoming_destroy (incoming);
1293 * Terminates an incoming operation in case we have not yet received an
1294 * operation request. Called by the channel destruction handler.
1296 * @param op the channel context
1299 handle_incoming_disconnect (struct Operation *op)
1301 GNUNET_assert (GNUNET_YES == op->is_incoming);
1302 /* channel is already dead, incoming_destroy must not
1305 incoming_destroy (op);
1311 * Method called whenever another peer has added us to a channel the
1312 * other peer initiated. Only called (once) upon reception of data
1313 * with a message type which was subscribed to in
1314 * GNUNET_CADET_connect().
1316 * The channel context represents the operation itself and gets added to a DLL,
1317 * from where it gets looked up when our local listener client responds
1318 * to a proposed/suggested operation or connects and associates with this operation.
1320 * @param cls closure
1321 * @param channel new handle to the channel
1322 * @param initiator peer that started the channel
1323 * @param port Port this channel is for.
1324 * @param options Unused.
1325 * @return initial channel context for the channel
1326 * returns NULL on error
1329 channel_new_cb (void *cls,
1330 struct GNUNET_CADET_Channel *channel,
1331 const struct GNUNET_PeerIdentity *initiator,
1333 enum GNUNET_CADET_ChannelOption options)
1335 static const struct SetVT incoming_vt = {
1336 .msg_handler = &handle_incoming_msg,
1337 .peer_disconnect = &handle_incoming_disconnect
1339 struct Operation *incoming;
1341 if (GNUNET_APPLICATION_TYPE_SET != port)
1344 GNUNET_CADET_channel_destroy (channel);
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "New incoming channel\n");
1349 incoming = GNUNET_new (struct Operation);
1350 incoming->is_incoming = GNUNET_YES;
1351 incoming->peer = *initiator;
1352 incoming->channel = channel;
1353 incoming->mq = GNUNET_CADET_mq_create (incoming->channel);
1354 incoming->vt = &incoming_vt;
1355 incoming->timeout_task
1356 = GNUNET_SCHEDULER_add_delayed (INCOMING_CHANNEL_TIMEOUT,
1357 &incoming_timeout_cb,
1359 GNUNET_CONTAINER_DLL_insert_tail (incoming_head,
1367 * Function called whenever a channel is destroyed. Should clean up
1368 * any associated state. It must NOT call
1369 * GNUNET_CADET_channel_destroy() on the channel.
1371 * The peer_disconnect function is part of a a virtual table set initially either
1372 * when a peer creates a new channel with us (#channel_new_cb()), or once we create
1373 * a new channel ourselves (evaluate).
1375 * Once we know the exact type of operation (union/intersection), the vt is
1376 * replaced with an operation specific instance (_GSS_[op]_vt).
1378 * @param cls closure (set from GNUNET_CADET_connect())
1379 * @param channel connection to the other end (henceforth invalid)
1380 * @param channel_ctx place where local state associated
1381 * with the channel is stored
1384 channel_end_cb (void *cls,
1385 const struct GNUNET_CADET_Channel *channel,
1388 struct Operation *op = channel_ctx;
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391 "channel_end_cb called\n");
1394 /* the vt can be null if a client already requested canceling op. */
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "calling peer disconnect due to channel end\n");
1399 op->vt->peer_disconnect (op);
1404 /* cadet will never call us with the context again! */
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1408 "channel_end_cb finished\n");
1413 * Functions with this signature are called whenever a message is
1414 * received via a cadet channel.
1416 * The msg_handler is a virtual table set in initially either when a peer
1417 * creates a new channel with us (channel_new_cb), or once we create a new channel
1418 * ourselves (evaluate).
1420 * Once we know the exact type of operation (union/intersection), the vt is
1421 * replaced with an operation specific instance (_GSS_[op]_vt).
1423 * @param cls Closure (set from GNUNET_CADET_connect()).
1424 * @param channel Connection to the other end.
1425 * @param channel_ctx Place to store local state associated with the channel.
1426 * @param message The actual message.
1427 * @return #GNUNET_OK to keep the channel open,
1428 * #GNUNET_SYSERR to close it (signal serious error).
1431 dispatch_p2p_message (void *cls,
1432 struct GNUNET_CADET_Channel *channel,
1434 const struct GNUNET_MessageHeader *message)
1436 struct Operation *op = *channel_ctx;
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1440 "Dispatching cadet message (type: %u)\n",
1441 ntohs (message->type));
1442 /* do this before the handler, as the handler might kill the channel */
1443 GNUNET_CADET_receive_done (channel);
1445 ret = op->vt->msg_handler (op,
1448 ret = GNUNET_SYSERR;
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 "Handled cadet message (type: %u)\n",
1451 ntohs (message->type));
1457 * Function called by the service's run
1458 * method to run service-specific setup code.
1460 * @param cls closure
1461 * @param server the initialized server
1462 * @param cfg configuration to use
1466 struct GNUNET_SERVER_Handle *server,
1467 const struct GNUNET_CONFIGURATION_Handle *cfg)
1469 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1470 { &handle_client_accept, NULL,
1471 GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1472 sizeof (struct GNUNET_SET_AcceptMessage)},
1473 { &handle_client_iter_ack, NULL,
1474 GNUNET_MESSAGE_TYPE_SET_ITER_ACK,
1475 sizeof (struct GNUNET_SET_IterAckMessage) },
1476 { &handle_client_add, NULL,
1477 GNUNET_MESSAGE_TYPE_SET_ADD,
1479 { &handle_client_create_set, NULL,
1480 GNUNET_MESSAGE_TYPE_SET_CREATE,
1481 sizeof (struct GNUNET_SET_CreateMessage)},
1482 { &handle_client_iterate, NULL,
1483 GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1484 sizeof (struct GNUNET_MessageHeader)},
1485 { &handle_client_evaluate, NULL,
1486 GNUNET_MESSAGE_TYPE_SET_EVALUATE,
1488 { &handle_client_listen, NULL,
1489 GNUNET_MESSAGE_TYPE_SET_LISTEN,
1490 sizeof (struct GNUNET_SET_ListenMessage)},
1491 { &handle_client_reject, NULL,
1492 GNUNET_MESSAGE_TYPE_SET_REJECT,
1493 sizeof (struct GNUNET_SET_RejectMessage)},
1494 { &handle_client_remove, NULL,
1495 GNUNET_MESSAGE_TYPE_SET_REMOVE,
1497 { &handle_client_cancel, NULL,
1498 GNUNET_MESSAGE_TYPE_SET_CANCEL,
1499 sizeof (struct GNUNET_SET_CancelMessage)},
1502 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1503 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1504 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1505 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1506 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE, 0},
1507 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1508 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1509 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
1510 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1511 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE, 0},
1514 static const uint32_t cadet_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1516 configuration = cfg;
1517 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1518 &shutdown_task, NULL);
1519 GNUNET_SERVER_disconnect_notify (server,
1520 &handle_client_disconnect, NULL);
1521 GNUNET_SERVER_add_handlers (server,
1523 cadet = GNUNET_CADET_connect (cfg, NULL,
1530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1531 _("Could not connect to cadet service\n"));
1538 * The main function for the set service.
1540 * @param argc number of arguments from the command line
1541 * @param argv command line arguments
1542 * @return 0 ok, 1 on error
1550 ret = GNUNET_SERVICE_run (argc, argv, "set",
1551 GNUNET_SERVICE_OPTION_NONE,
1553 return (GNUNET_OK == ret) ? 0 : 1;
1556 /* end of gnunet-service-set.c */