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 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
473 GNUNET_CONTAINER_DLL_remove (incoming_head,
476 if (GNUNET_SCHEDULER_NO_TASK != incoming->timeout_task)
478 GNUNET_SCHEDULER_cancel (incoming->timeout_task);
479 incoming->timeout_task = GNUNET_SCHEDULER_NO_TASK;
481 /* make sure that the tunnel end handler will not destroy us again */
483 if (NULL != incoming->spec)
485 GNUNET_free (incoming->spec);
486 incoming->spec = NULL;
488 if (NULL != incoming->mq)
490 GNUNET_MQ_destroy (incoming->mq);
493 if (NULL != incoming->channel)
495 GNUNET_CADET_channel_destroy (incoming->channel);
496 incoming->channel = NULL;
502 * Find a listener that is interested in the given operation type
503 * and application id.
505 * @param op operation type to look for
506 * @param app_id application id to look for
507 * @return a matching listener, or NULL if no listener matches the
508 * given operation and application id
510 static struct Listener *
511 listener_get_by_target (enum GNUNET_SET_OperationType op,
512 const struct GNUNET_HashCode *app_id)
514 struct Listener *listener;
516 for (listener = listeners_head; NULL != listener; listener = listener->next)
517 if ( (listener->operation == op) &&
518 (0 == GNUNET_CRYPTO_hash_cmp (app_id, &listener->app_id)) )
525 * Suggest the given request to the listener. The listening client can
526 * then accept or reject the remote request.
528 * @param incoming the incoming peer with the request to suggest
529 * @param listener the listener to suggest the request to
532 incoming_suggest (struct Operation *incoming,
533 struct Listener *listener)
535 struct GNUNET_MQ_Envelope *mqm;
536 struct GNUNET_SET_RequestMessage *cmsg;
538 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
539 GNUNET_assert (NULL != incoming->spec);
540 GNUNET_assert (0 == incoming->suggest_id);
541 incoming->suggest_id = suggest_id++;
544 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->timeout_task);
545 GNUNET_SCHEDULER_cancel (incoming->timeout_task);
546 incoming->timeout_task = GNUNET_SCHEDULER_NO_TASK;
547 mqm = GNUNET_MQ_msg_nested_mh (cmsg,
548 GNUNET_MESSAGE_TYPE_SET_REQUEST,
549 incoming->spec->context_msg);
550 GNUNET_assert (NULL != mqm);
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Suggesting incoming request with accept id %u to listener\n",
553 incoming->suggest_id);
554 cmsg->accept_id = htonl (incoming->suggest_id);
555 cmsg->peer_id = incoming->spec->peer;
556 GNUNET_MQ_send (listener->client_mq, mqm);
561 * Handle a request for a set operation from another peer. Checks if we
562 * have a listener waiting for such a request (and in that case initiates
563 * asking the listener about accepting the connection). If no listener
564 * is waiting, we queue the operation request in hope that a listener
565 * shows up soon (before timeout).
567 * This msg is expected as the first and only msg handled through the
568 * non-operation bound virtual table, acceptance of this operation replaces
569 * our virtual table and subsequent msgs would be routed differently (as
570 * we then know what type of operation this is).
572 * @param op the operation state
573 * @param mh the received message
574 * @return #GNUNET_OK if the channel should be kept alive,
575 * #GNUNET_SYSERR to destroy the channel
578 handle_incoming_msg (struct Operation *op,
579 const struct GNUNET_MessageHeader *mh)
581 const struct OperationRequestMessage *msg;
582 struct Listener *listener;
583 struct OperationSpecification *spec;
584 const struct GNUNET_MessageHeader *nested_context;
586 msg = (const struct OperationRequestMessage *) mh;
587 GNUNET_assert (GNUNET_YES == op->is_incoming);
588 if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
591 return GNUNET_SYSERR;
593 /* double operation request */
594 if (NULL != op->spec)
597 return GNUNET_SYSERR;
599 spec = GNUNET_new (struct OperationSpecification);
600 nested_context = GNUNET_MQ_extract_nested_mh (msg);
601 if ( (NULL != nested_context) &&
602 (ntohs (nested_context->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
606 return GNUNET_SYSERR;
608 /* Make a copy of the nested_context (application-specific context
609 information that is opaque to set) so we can pass it to the
611 if (NULL != nested_context)
612 spec->context_msg = GNUNET_copy_message (nested_context);
613 spec->operation = ntohl (msg->operation);
614 spec->app_id = msg->app_id;
615 spec->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
617 spec->peer = op->peer;
618 spec->remote_element_count = ntohl (msg->element_count);
621 listener = listener_get_by_target (ntohl (msg->operation),
623 if (NULL == listener)
625 GNUNET_break (GNUNET_SCHEDULER_NO_TASK != op->timeout_task);
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "No matching listener for incoming request (op %u, app %s), waiting with timeout\n",
628 ntohl (msg->operation),
629 GNUNET_h2s (&msg->app_id));
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "Received P2P operation request (op %u, app %s) for active listener\n",
634 ntohl (msg->operation),
635 GNUNET_h2s (&msg->app_id));
636 incoming_suggest (op, listener);
642 * Send the next element of a set to the set's client. The next element is given by
643 * the set's current hashmap iterator. The set's iterator will be set to NULL if there
644 * are no more elements in the set. The caller must ensure that the set's iterator is
647 * The client will acknowledge each received element with a
648 * #GNUNET_MESSAGE_TYPE_SET_ITER_ACK message. Our
649 * #handle_client_iter_ack() will then trigger the next transmission.
650 * Note that the #GNUNET_MESSAGE_TYPE_SET_ITER_DONE is not acknowledged.
652 * @param set set that should send its next element to its client
655 send_client_element (struct Set *set)
658 struct ElementEntry *ee;
659 struct GNUNET_MQ_Envelope *ev;
660 struct GNUNET_SET_IterResponseMessage *msg;
662 GNUNET_assert (NULL != set->iter);
663 ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter,
665 (const void **) &ee);
666 if (GNUNET_NO == ret)
668 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
669 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
675 GNUNET_assert (NULL != ee);
676 ev = GNUNET_MQ_msg_extra (msg,
678 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
682 msg->element_type = ee->element.element_type;
683 msg->iteration_id = htons (set->iteration_id);
685 GNUNET_MQ_send (set->client_mq, ev);
690 * Called when a client wants to iterate the elements of a set.
691 * Checks if we have a set associated with the client and if we
692 * can right now start an iteration. If all checks out, starts
693 * sending the elements of the set to the client.
696 * @param client client that sent the message
697 * @param m message sent by the client
700 handle_client_iterate (void *cls,
701 struct GNUNET_SERVER_Client *client,
702 const struct GNUNET_MessageHeader *m)
706 set = set_get (client);
709 /* attempt to iterate over a non existing set */
711 GNUNET_SERVER_client_disconnect (client);
714 if (NULL != set->iter)
716 /* Only one concurrent iterate-action allowed per set */
718 GNUNET_SERVER_client_disconnect (client);
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Iterating set with %u elements\n",
723 GNUNET_CONTAINER_multihashmap_size (set->elements));
724 GNUNET_SERVER_receive_done (client,
726 set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
727 send_client_element (set);
732 * Called when a client wants to create a new set. This is typically
733 * the first request from a client, and includes the type of set
734 * operation to be performed.
737 * @param client client that sent the message
738 * @param m message sent by the client
741 handle_client_create_set (void *cls,
742 struct GNUNET_SERVER_Client *client,
743 const struct GNUNET_MessageHeader *m)
745 const struct GNUNET_SET_CreateMessage *msg;
748 msg = (const struct GNUNET_SET_CreateMessage *) m;
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Client created new set (operation %u)\n",
751 ntohl (msg->operation));
752 if (NULL != set_get (client))
754 /* There can only be one set per client */
756 GNUNET_SERVER_client_disconnect (client);
759 set = GNUNET_new (struct Set);
760 switch (ntohl (msg->operation))
762 case GNUNET_SET_OPERATION_INTERSECTION:
763 set->vt = _GSS_intersection_vt ();
765 case GNUNET_SET_OPERATION_UNION:
766 set->vt = _GSS_union_vt ();
771 GNUNET_SERVER_client_disconnect (client);
774 set->state = set->vt->create ();
775 set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
776 set->client = client;
777 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
778 GNUNET_CONTAINER_DLL_insert (sets_head,
781 GNUNET_SERVER_receive_done (client,
787 * Called when a client wants to create a new listener.
790 * @param client client that sent the message
791 * @param m message sent by the client
794 handle_client_listen (void *cls,
795 struct GNUNET_SERVER_Client *client,
796 const struct GNUNET_MessageHeader *m)
798 const struct GNUNET_SET_ListenMessage *msg;
799 struct Listener *listener;
800 struct Operation *op;
802 msg = (const struct GNUNET_SET_ListenMessage *) m;
803 if (NULL != listener_get (client))
805 /* max. one active listener per client! */
807 GNUNET_SERVER_client_disconnect (client);
810 listener = GNUNET_new (struct Listener);
811 listener->client = client;
812 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
813 listener->app_id = msg->app_id;
814 listener->operation = ntohl (msg->operation);
815 GNUNET_CONTAINER_DLL_insert_tail (listeners_head,
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "New listener created (op %u, app %s)\n",
821 GNUNET_h2s (&listener->app_id));
823 /* check for existing incoming requests the listener might be interested in */
824 for (op = incoming_head; NULL != op; op = op->next)
826 if (NULL == op->spec)
827 continue; /* no details available yet */
828 if (0 != op->suggest_id)
829 continue; /* this one has been already suggested to a listener */
830 if (listener->operation != op->spec->operation)
831 continue; /* incompatible operation */
832 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id,
834 continue; /* incompatible appliation */
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Found matching existing request\n");
837 incoming_suggest (op,
840 GNUNET_SERVER_receive_done (client, GNUNET_OK);
845 * Called when the listening client rejects an operation
846 * request by another peer.
849 * @param client client that sent the message
850 * @param m message sent by the client
853 handle_client_reject (void *cls,
854 struct GNUNET_SERVER_Client *client,
855 const struct GNUNET_MessageHeader *m)
857 struct Operation *incoming;
858 const struct GNUNET_SET_RejectMessage *msg;
860 msg = (const struct GNUNET_SET_RejectMessage *) m;
861 incoming = get_incoming (ntohl (msg->accept_reject_id));
862 if (NULL == incoming)
864 /* no matching incoming operation for this reject */
866 GNUNET_SERVER_receive_done (client,
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 "Peer request (op %u, app %s) rejected by client\n",
872 incoming->spec->operation,
873 GNUNET_h2s (&incoming->spec->app_id));
874 GNUNET_CADET_channel_destroy (incoming->channel);
875 GNUNET_SERVER_receive_done (client,
881 * Called when a client wants to add an element to a set it inhabits.
884 * @param client client that sent the message
885 * @param m message sent by the client
888 handle_client_add (void *cls,
889 struct GNUNET_SERVER_Client *client,
890 const struct GNUNET_MessageHeader *m)
893 const struct GNUNET_SET_ElementMessage *msg;
894 struct GNUNET_SET_Element el;
895 struct ElementEntry *ee;
896 struct ElementEntry *ee_dup;
898 set = set_get (client);
901 /* client without a set requested an operation */
903 GNUNET_SERVER_client_disconnect (client);
906 GNUNET_SERVER_receive_done (client,
908 msg = (const struct GNUNET_SET_ElementMessage *) m;
909 el.size = ntohs (m->size) - sizeof *msg;
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "Client inserts element of size %u\n",
914 ee = GNUNET_malloc (el.size + sizeof *ee);
915 ee->element.size = el.size;
919 ee->element.data = &ee[1];
920 ee->generation_added = set->current_generation;
921 ee->remote = GNUNET_NO;
922 GNUNET_CRYPTO_hash (ee->element.data,
925 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
929 /* same element inserted twice */
934 GNUNET_break (GNUNET_YES ==
935 GNUNET_CONTAINER_multihashmap_put (set->elements,
938 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
939 set->vt->add (set->state, ee);
944 * Called when a client wants to remove an element from a set it
948 * @param client client that sent the message
949 * @param m message sent by the client
952 handle_client_remove (void *cls,
953 struct GNUNET_SERVER_Client *client,
954 const struct GNUNET_MessageHeader *m)
957 const struct GNUNET_SET_ElementMessage *msg;
958 struct GNUNET_SET_Element el;
959 struct ElementEntry *ee;
960 struct GNUNET_HashCode hash;
962 set = set_get (client);
965 /* client without a set requested an operation */
967 GNUNET_SERVER_client_disconnect (client);
970 GNUNET_SERVER_receive_done (client,
972 msg = (const struct GNUNET_SET_ElementMessage *) m;
973 el.size = ntohs (m->size) - sizeof *msg;
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 "Client removes element of size %u\n",
978 GNUNET_CRYPTO_hash (el.data,
981 ee = GNUNET_CONTAINER_multihashmap_get (set->elements,
985 /* Client tried to remove non-existing element */
989 if (GNUNET_YES == ee->removed)
991 /* Client tried to remove element twice */
995 ee->removed = GNUNET_YES;
996 ee->generation_removed = set->current_generation;
997 set->vt->remove (set->state, ee);
1002 * Called when a client wants to initiate a set operation with another
1003 * peer. Initiates the CADET connection to the listener and sends the
1007 * @param client client that sent the message
1008 * @param m message sent by the client
1011 handle_client_evaluate (void *cls,
1012 struct GNUNET_SERVER_Client *client,
1013 const struct GNUNET_MessageHeader *m)
1016 const struct GNUNET_SET_EvaluateMessage *msg;
1017 struct OperationSpecification *spec;
1018 struct Operation *op;
1019 const struct GNUNET_MessageHeader *context;
1021 set = set_get (client);
1025 GNUNET_SERVER_client_disconnect (client);
1028 msg = (const struct GNUNET_SET_EvaluateMessage *) m;
1029 spec = GNUNET_new (struct OperationSpecification);
1030 spec->operation = set->operation;
1031 spec->app_id = msg->app_id;
1032 spec->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1034 spec->peer = msg->target_peer;
1036 spec->result_mode = ntohl (msg->result_mode);
1037 spec->client_request_id = ntohl (msg->request_id);
1038 context = GNUNET_MQ_extract_nested_mh (msg);
1039 op = GNUNET_new (struct Operation);
1041 op->generation_created = set->current_generation++;
1043 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1046 op->channel = GNUNET_CADET_channel_create (cadet,
1049 GNUNET_APPLICATION_TYPE_SET,
1050 GNUNET_CADET_OPTION_RELIABLE);
1051 op->mq = GNUNET_CADET_mq_create (op->channel);
1052 set->vt->evaluate (op,
1054 GNUNET_SERVER_receive_done (client,
1060 * Handle an ack from a client, and send the next element. Note
1061 * that we only expect acks for set elements, not after the
1062 * #GNUNET_MESSAGE_TYPE_SET_ITER_DONE message.
1065 * @param client the client
1066 * @param m the message
1069 handle_client_iter_ack (void *cls,
1070 struct GNUNET_SERVER_Client *client,
1071 const struct GNUNET_MessageHeader *m)
1073 const struct GNUNET_SET_IterAckMessage *ack;
1076 set = set_get (client);
1079 /* client without a set acknowledged receiving a value */
1081 GNUNET_SERVER_client_disconnect (client);
1084 if (NULL == set->iter)
1086 /* client sent an ack, but we were not expecting one (as
1087 set iteration has finished) */
1089 GNUNET_SERVER_client_disconnect (client);
1092 ack = (const struct GNUNET_SET_IterAckMessage *) m;
1093 GNUNET_SERVER_receive_done (client,
1095 if (ntohl (ack->send_more))
1097 send_client_element (set);
1101 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
1103 set->iteration_id++;
1109 * Handle a request from the client to
1110 * cancel a running set operation.
1113 * @param client the client
1114 * @param mh the message
1117 handle_client_cancel (void *cls,
1118 struct GNUNET_SERVER_Client *client,
1119 const struct GNUNET_MessageHeader *mh)
1121 const struct GNUNET_SET_CancelMessage *msg =
1122 (const struct GNUNET_SET_CancelMessage *) mh;
1124 struct Operation *op;
1127 set = set_get (client);
1130 /* client without a set requested an operation */
1132 GNUNET_SERVER_client_disconnect (client);
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "Client requested cancel for op %u\n",
1137 ntohl (msg->request_id));
1139 for (op = set->ops_head; NULL != op; op = op->next)
1141 if (op->spec->client_request_id == ntohl (msg->request_id))
1147 if (GNUNET_NO == found)
1149 /* It may happen that the operation was already destroyed due to
1150 * the other peer disconnecting. The client may not know about this
1151 * yet and try to cancel the (just barely non-existent) operation.
1152 * So this is not a hard error.
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Client canceled non-existent op\n");
1159 _GSS_operation_destroy (op,
1162 GNUNET_SERVER_receive_done (client,
1168 * Handle a request from the client to accept a set operation that
1169 * came from a remote peer. We forward the accept to the associated
1170 * operation for handling
1173 * @param client the client
1174 * @param mh the message
1177 handle_client_accept (void *cls,
1178 struct GNUNET_SERVER_Client *client,
1179 const struct GNUNET_MessageHeader *mh)
1182 const struct GNUNET_SET_AcceptMessage *msg;
1183 struct Operation *op;
1184 struct GNUNET_SET_ResultMessage *result_message;
1185 struct GNUNET_MQ_Envelope *ev;
1187 msg = (const struct GNUNET_SET_AcceptMessage *) mh;
1188 set = set_get (client);
1191 /* client without a set requested to accept */
1193 GNUNET_SERVER_client_disconnect (client);
1196 op = get_incoming (ntohl (msg->accept_reject_id));
1199 /* It is not an error if the set op does not exist -- it may
1200 * have been destroyed when the partner peer disconnected. */
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Client accepted request that is no longer active\n");
1203 ev = GNUNET_MQ_msg (result_message,
1204 GNUNET_MESSAGE_TYPE_SET_RESULT);
1205 result_message->request_id = msg->request_id;
1206 result_message->element_type = 0;
1207 result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1208 GNUNET_MQ_send (set->client_mq, ev);
1209 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "Client accepting request %u\n",
1215 ntohl (msg->accept_reject_id));
1216 GNUNET_assert (GNUNET_YES == op->is_incoming);
1217 op->is_incoming = GNUNET_NO;
1218 GNUNET_CONTAINER_DLL_remove (incoming_head,
1221 op->spec->set = set;
1222 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1225 op->spec->client_request_id = ntohl (msg->request_id);
1226 op->spec->result_mode = ntohl (msg->result_mode);
1227 op->generation_created = set->current_generation++;
1229 op->vt->accept (op);
1230 GNUNET_SERVER_receive_done (client,
1236 * Called to clean up, after a shutdown has been requested.
1238 * @param cls closure
1239 * @param tc context information (why was this task triggered now)
1242 shutdown_task (void *cls,
1243 const struct GNUNET_SCHEDULER_TaskContext *tc)
1245 while (NULL != incoming_head)
1246 incoming_destroy (incoming_head);
1247 while (NULL != listeners_head)
1248 listener_destroy (listeners_head);
1249 while (NULL != sets_head)
1250 set_destroy (sets_head);
1252 /* it's important to destroy cadet at the end, as all channels
1253 * must be destroyed before the cadet handle! */
1256 GNUNET_CADET_disconnect (cadet);
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 "handled shutdown request\n");
1265 * Timeout happens iff:
1266 * - we suggested an operation to our listener,
1267 * but did not receive a response in time
1268 * - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1269 * - shutdown (obviously)
1271 * @param cls channel context
1272 * @param tc context information (why was this task triggered now)
1275 incoming_timeout_cb (void *cls,
1276 const struct GNUNET_SCHEDULER_TaskContext *tc)
1278 struct Operation *incoming = cls;
1280 incoming->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1281 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1282 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Remote peer's incoming request timed out\n");
1286 incoming_destroy (incoming);
1291 * Terminates an incoming operation in case we have not yet received an
1292 * operation request. Called by the channel destruction handler.
1294 * @param op the channel context
1297 handle_incoming_disconnect (struct Operation *op)
1299 GNUNET_assert (GNUNET_YES == op->is_incoming);
1300 /* channel is already dead, incoming_destroy must not
1303 incoming_destroy (op);
1309 * Method called whenever another peer has added us to a channel the
1310 * other peer initiated. Only called (once) upon reception of data
1311 * with a message type which was subscribed to in
1312 * GNUNET_CADET_connect().
1314 * The channel context represents the operation itself and gets added to a DLL,
1315 * from where it gets looked up when our local listener client responds
1316 * to a proposed/suggested operation or connects and associates with this operation.
1318 * @param cls closure
1319 * @param channel new handle to the channel
1320 * @param initiator peer that started the channel
1321 * @param port Port this channel is for.
1322 * @param options Unused.
1323 * @return initial channel context for the channel
1324 * returns NULL on error
1327 channel_new_cb (void *cls,
1328 struct GNUNET_CADET_Channel *channel,
1329 const struct GNUNET_PeerIdentity *initiator,
1331 enum GNUNET_CADET_ChannelOption options)
1333 static const struct SetVT incoming_vt = {
1334 .msg_handler = &handle_incoming_msg,
1335 .peer_disconnect = &handle_incoming_disconnect
1337 struct Operation *incoming;
1339 if (GNUNET_APPLICATION_TYPE_SET != port)
1342 GNUNET_CADET_channel_destroy (channel);
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "New incoming channel\n");
1347 incoming = GNUNET_new (struct Operation);
1348 incoming->is_incoming = GNUNET_YES;
1349 incoming->peer = *initiator;
1350 incoming->channel = channel;
1351 incoming->mq = GNUNET_CADET_mq_create (incoming->channel);
1352 incoming->vt = &incoming_vt;
1353 incoming->timeout_task
1354 = GNUNET_SCHEDULER_add_delayed (INCOMING_CHANNEL_TIMEOUT,
1355 &incoming_timeout_cb,
1357 GNUNET_CONTAINER_DLL_insert_tail (incoming_head,
1365 * Function called whenever a channel is destroyed. Should clean up
1366 * any associated state. It must NOT call
1367 * GNUNET_CADET_channel_destroy() on the channel.
1369 * The peer_disconnect function is part of a a virtual table set initially either
1370 * when a peer creates a new channel with us (#channel_new_cb()), or once we create
1371 * a new channel ourselves (evaluate).
1373 * Once we know the exact type of operation (union/intersection), the vt is
1374 * replaced with an operation specific instance (_GSS_[op]_vt).
1376 * @param cls closure (set from GNUNET_CADET_connect())
1377 * @param channel connection to the other end (henceforth invalid)
1378 * @param channel_ctx place where local state associated
1379 * with the channel is stored
1382 channel_end_cb (void *cls,
1383 const struct GNUNET_CADET_Channel *channel,
1386 struct Operation *op = channel_ctx;
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "channel_end_cb called\n");
1392 /* the vt can be null if a client already requested canceling op. */
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "calling peer disconnect due to channel end\n");
1397 op->vt->peer_disconnect (op);
1402 /* cadet will never call us with the context again! */
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 "channel_end_cb finished\n");
1411 * Functions with this signature are called whenever a message is
1412 * received via a cadet channel.
1414 * The msg_handler is a virtual table set in initially either when a peer
1415 * creates a new channel with us (channel_new_cb), or once we create a new channel
1416 * ourselves (evaluate).
1418 * Once we know the exact type of operation (union/intersection), the vt is
1419 * replaced with an operation specific instance (_GSS_[op]_vt).
1421 * @param cls Closure (set from GNUNET_CADET_connect()).
1422 * @param channel Connection to the other end.
1423 * @param channel_ctx Place to store local state associated with the channel.
1424 * @param message The actual message.
1425 * @return #GNUNET_OK to keep the channel open,
1426 * #GNUNET_SYSERR to close it (signal serious error).
1429 dispatch_p2p_message (void *cls,
1430 struct GNUNET_CADET_Channel *channel,
1432 const struct GNUNET_MessageHeader *message)
1434 struct Operation *op = *channel_ctx;
1437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1438 "Dispatching cadet message (type: %u)\n",
1439 ntohs (message->type));
1440 /* do this before the handler, as the handler might kill the channel */
1441 GNUNET_CADET_receive_done (channel);
1443 ret = op->vt->msg_handler (op,
1446 ret = GNUNET_SYSERR;
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 "Handled cadet message (type: %u)\n",
1449 ntohs (message->type));
1455 * Function called by the service's run
1456 * method to run service-specific setup code.
1458 * @param cls closure
1459 * @param server the initialized server
1460 * @param cfg configuration to use
1464 struct GNUNET_SERVER_Handle *server,
1465 const struct GNUNET_CONFIGURATION_Handle *cfg)
1467 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1468 { &handle_client_accept, NULL,
1469 GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1470 sizeof (struct GNUNET_SET_AcceptMessage)},
1471 { &handle_client_iter_ack, NULL,
1472 GNUNET_MESSAGE_TYPE_SET_ITER_ACK,
1473 sizeof (struct GNUNET_SET_IterAckMessage) },
1474 { &handle_client_add, NULL,
1475 GNUNET_MESSAGE_TYPE_SET_ADD,
1477 { &handle_client_create_set, NULL,
1478 GNUNET_MESSAGE_TYPE_SET_CREATE,
1479 sizeof (struct GNUNET_SET_CreateMessage)},
1480 { &handle_client_iterate, NULL,
1481 GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1482 sizeof (struct GNUNET_MessageHeader)},
1483 { &handle_client_evaluate, NULL,
1484 GNUNET_MESSAGE_TYPE_SET_EVALUATE,
1486 { &handle_client_listen, NULL,
1487 GNUNET_MESSAGE_TYPE_SET_LISTEN,
1488 sizeof (struct GNUNET_SET_ListenMessage)},
1489 { &handle_client_reject, NULL,
1490 GNUNET_MESSAGE_TYPE_SET_REJECT,
1491 sizeof (struct GNUNET_SET_RejectMessage)},
1492 { &handle_client_remove, NULL,
1493 GNUNET_MESSAGE_TYPE_SET_REMOVE,
1495 { &handle_client_cancel, NULL,
1496 GNUNET_MESSAGE_TYPE_SET_CANCEL,
1497 sizeof (struct GNUNET_SET_CancelMessage)},
1500 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1501 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1502 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1503 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1504 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE, 0},
1505 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1506 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1507 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
1508 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1509 { &dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE, 0},
1512 static const uint32_t cadet_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1514 configuration = cfg;
1515 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1516 &shutdown_task, NULL);
1517 GNUNET_SERVER_disconnect_notify (server,
1518 &handle_client_disconnect, NULL);
1519 GNUNET_SERVER_add_handlers (server,
1521 cadet = GNUNET_CADET_connect (cfg, NULL,
1528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1529 _("Could not connect to cadet service\n"));
1536 * The main function for the set service.
1538 * @param argc number of arguments from the command line
1539 * @param argv command line arguments
1540 * @return 0 ok, 1 on error
1548 ret = GNUNET_SERVICE_run (argc, argv, "set",
1549 GNUNET_SERVICE_OPTION_NONE,
1551 return (GNUNET_OK == ret) ? 0 : 1;
1554 /* end of gnunet-service-set.c */