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 "set_protocol.h"
31 * A listener is inhabited by a client, and waits for evaluation
32 * requests from remote peers.
37 * Listeners are held in a doubly linked list.
39 struct Listener *next;
42 * Listeners are held in a doubly linked list.
44 struct Listener *prev;
47 * Client that owns the listener.
48 * Only one client may own a listener.
50 struct GNUNET_SERVER_Client *client;
53 * Message queue for the client
55 struct GNUNET_MQ_Handle *client_mq;
58 * Application ID for the operation, used to distinguish
59 * multiple operations of the same type with the same peer.
61 struct GNUNET_HashCode app_id;
64 * The type of the operation.
66 enum GNUNET_SET_OperationType operation;
71 * Configuration of our local peer.
73 static const struct GNUNET_CONFIGURATION_Handle *configuration;
76 * Handle to the cadet service, used to listen for and connect to
79 static struct GNUNET_CADET_Handle *cadet;
82 * Sets are held in a doubly linked list.
84 static struct Set *sets_head;
87 * Sets are held in a doubly linked list.
89 static struct Set *sets_tail;
92 * Listeners are held in a doubly linked list.
94 static struct Listener *listeners_head;
97 * Listeners are held in a doubly linked list.
99 static struct Listener *listeners_tail;
102 * Incoming sockets from remote peers are held in a doubly linked
105 static struct Operation *incoming_head;
108 * Incoming sockets from remote peers are held in a doubly linked
111 static struct Operation *incoming_tail;
114 * Counter for allocating unique IDs for clients, used to identify
115 * incoming operation requests from remote peers, that the client can
116 * choose to accept or refuse.
118 static uint32_t suggest_id = 1;
122 * Get set that is owned by the given client, if any.
124 * @param client client to look for
125 * @return set that the client owns, NULL if the client
129 set_get (struct GNUNET_SERVER_Client *client)
133 for (set = sets_head; NULL != set; set = set->next)
134 if (set->client == client)
141 * Get the listener associated with the given client, if any.
143 * @param client the client
144 * @return listener associated with the client, NULL
147 static struct Listener *
148 listener_get (struct GNUNET_SERVER_Client *client)
150 struct Listener *listener;
152 for (listener = listeners_head; NULL != listener; listener = listener->next)
153 if (listener->client == client)
160 * Get the incoming socket associated with the given id.
162 * @param id id to look for
163 * @return the incoming socket associated with the id,
164 * or NULL if there is none
166 static struct Operation *
167 get_incoming (uint32_t id)
169 struct Operation *op;
171 for (op = incoming_head; NULL != op; op = op->next)
172 if (op->suggest_id == id)
174 // FIXME: remove this assertion once the corresponding bug is gone!
175 GNUNET_assert (GNUNET_YES == op->is_incoming);
183 * Destroy a listener, free all resources associated with it.
185 * @param listener listener to destroy
188 listener_destroy (struct Listener *listener)
190 /* If the client is not dead yet, destroy it.
191 * The client's destroy callback will destroy the listener again. */
192 if (NULL != listener->client)
194 struct GNUNET_SERVER_Client *client = listener->client;
196 listener->client = NULL;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "disconnecting listener client\n");
199 GNUNET_SERVER_client_disconnect (client);
202 if (NULL != listener->client_mq)
204 GNUNET_MQ_destroy (listener->client_mq);
205 listener->client_mq = NULL;
207 GNUNET_CONTAINER_DLL_remove (listeners_head,
210 GNUNET_free (listener);
215 * Context for the #garbage_collect_cb().
217 struct GarbageContext
221 * Map for which we are garbage collecting removed elements.
223 struct GNUNET_CONTAINER_MultiHashMap *map;
226 * Lowest generation for which an operation is still pending.
228 unsigned int min_op_generation;
231 * Largest generation for which an operation is still pending.
233 unsigned int max_op_generation;
239 * Function invoked to check if an element can be removed from
240 * the set's history because it is no longer needed.
242 * @param cls the `struct GarbageContext *`
243 * @param key key of the element in the map
244 * @param value the `struct ElementEntry *`
245 * @return #GNUNET_OK (continue to iterate)
248 garbage_collect_cb (void *cls,
249 const struct GNUNET_HashCode *key,
252 struct GarbageContext *gc = cls;
253 struct ElementEntry *ee = value;
255 if (GNUNET_YES != ee->removed)
257 if ( (gc->max_op_generation < ee->generation_added) ||
258 (ee->generation_removed > gc->min_op_generation) )
260 GNUNET_assert (GNUNET_YES ==
261 GNUNET_CONTAINER_multihashmap_remove (gc->map,
271 * Collect and destroy elements that are not needed anymore, because
272 * their lifetime (as determined by their generation) does not overlap
273 * with any active set operation.
275 * @param set set to garbage collect
278 collect_generation_garbage (struct Set *set)
280 struct Operation *op;
281 struct GarbageContext gc;
283 gc.min_op_generation = UINT_MAX;
284 gc.max_op_generation = 0;
285 for (op = set->ops_head; NULL != op; op = op->next)
287 gc.min_op_generation = GNUNET_MIN (gc.min_op_generation,
288 op->generation_created);
289 gc.max_op_generation = GNUNET_MAX (gc.max_op_generation,
290 op->generation_created);
292 gc.map = set->elements;
293 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
300 * Destroy the given operation. Call the implementation-specific
301 * cancel function of the operation. Disconnects from the remote
302 * peer. Does not disconnect the client, as there may be multiple
303 * operations per set.
305 * @param op operation to destroy
306 * @param gc #GNUNET_YES to perform garbage collection on the set
309 _GSS_operation_destroy (struct Operation *op,
313 struct GNUNET_CADET_Channel *channel;
317 /* already in #_GSS_operation_destroy() */
320 GNUNET_assert (GNUNET_NO == op->is_incoming);
321 GNUNET_assert (NULL != op->spec);
323 GNUNET_CONTAINER_DLL_remove (op->spec->set->ops_head,
324 op->spec->set->ops_tail,
328 if (NULL != op->spec)
330 if (NULL != op->spec->context_msg)
332 GNUNET_free (op->spec->context_msg);
333 op->spec->context_msg = NULL;
335 GNUNET_free (op->spec);
340 GNUNET_MQ_destroy (op->mq);
343 if (NULL != (channel = op->channel))
346 GNUNET_CADET_channel_destroy (channel);
348 if (GNUNET_YES == gc)
349 collect_generation_garbage (set);
350 /* We rely on the channel end handler to free 'op'. When 'op->channel' was NULL,
351 * there was a channel end handler that will free 'op' on the call stack. */
356 * Iterator over hash map entries to free element entries.
359 * @param key current key code
360 * @param value a `struct ElementEntry *` to be free'd
361 * @return #GNUNET_YES (continue to iterate)
364 destroy_elements_iterator (void *cls,
365 const struct GNUNET_HashCode *key,
368 struct ElementEntry *ee = value;
376 * Destroy a set, and free all resources and operations associated with it.
378 * @param set the set to destroy
381 set_destroy (struct Set *set)
383 if (NULL != set->client)
385 /* If the client is not dead yet, destroy it. The client's destroy
386 * callback will call `set_destroy()` again in this case. We do
387 * this so that the channel end handler still has a valid set handle
389 struct GNUNET_SERVER_Client *client = set->client;
392 GNUNET_SERVER_client_disconnect (client);
395 GNUNET_assert (NULL != set->state);
396 while (NULL != set->ops_head)
397 _GSS_operation_destroy (set->ops_head, GNUNET_NO);
398 set->vt->destroy_set (set->state);
400 if (NULL != set->client_mq)
402 GNUNET_MQ_destroy (set->client_mq);
403 set->client_mq = NULL;
405 if (NULL != set->iter)
407 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
410 if (NULL != set->elements)
412 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
413 &destroy_elements_iterator,
415 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
416 set->elements = NULL;
418 GNUNET_CONTAINER_DLL_remove (sets_head,
426 * Clean up after a client has disconnected
428 * @param cls closure, unused
429 * @param client the client to clean up after
432 handle_client_disconnect (void *cls,
433 struct GNUNET_SERVER_Client *client)
436 struct Listener *listener;
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "client disconnected, cleaning up\n");
440 set = set_get (client);
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
446 "Client's set destroyed\n");
448 listener = listener_get (client);
449 if (NULL != listener)
451 listener->client = NULL;
452 listener_destroy (listener);
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 "Client's listener destroyed\n");
460 * Destroy an incoming request from a remote peer
462 * @param incoming remote request to destroy
465 incoming_destroy (struct Operation *incoming)
467 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
468 GNUNET_CONTAINER_DLL_remove (incoming_head,
471 if (GNUNET_SCHEDULER_NO_TASK != incoming->timeout_task)
473 GNUNET_SCHEDULER_cancel (incoming->timeout_task);
474 incoming->timeout_task = GNUNET_SCHEDULER_NO_TASK;
476 /* make sure that the tunnel end handler will not destroy us again */
478 if (NULL != incoming->mq)
480 GNUNET_MQ_destroy (incoming->mq);
483 if (NULL != incoming->channel)
485 GNUNET_CADET_channel_destroy (incoming->channel);
486 incoming->channel = NULL;
492 * Find a listener that is interested in the given operation type
493 * and application id.
495 * @param op operation type to look for
496 * @param app_id application id to look for
497 * @return a matching listener, or NULL if no listener matches the
498 * given operation and application id
500 static struct Listener *
501 listener_get_by_target (enum GNUNET_SET_OperationType op,
502 const struct GNUNET_HashCode *app_id)
506 for (l = listeners_head; NULL != l; l = l->next)
507 if ( (l->operation == op) &&
508 (0 == GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id)) )
515 * Suggest the given request to the listener. The listening client can
516 * then accept or reject the remote request.
518 * @param incoming the incoming peer with the request to suggest
519 * @param listener the listener to suggest the request to
522 incoming_suggest (struct Operation *incoming,
523 struct Listener *listener)
525 struct GNUNET_MQ_Envelope *mqm;
526 struct GNUNET_SET_RequestMessage *cmsg;
528 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
529 GNUNET_assert (NULL != incoming->spec);
530 GNUNET_assert (0 == incoming->suggest_id);
531 incoming->suggest_id = suggest_id++;
534 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->timeout_task);
535 GNUNET_SCHEDULER_cancel (incoming->timeout_task);
536 incoming->timeout_task = GNUNET_SCHEDULER_NO_TASK;
537 mqm = GNUNET_MQ_msg_nested_mh (cmsg,
538 GNUNET_MESSAGE_TYPE_SET_REQUEST,
539 incoming->spec->context_msg);
540 GNUNET_assert (NULL != mqm);
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "suggesting request with accept id %u\n",
543 incoming->suggest_id);
544 cmsg->accept_id = htonl (incoming->suggest_id);
545 cmsg->peer_id = incoming->spec->peer;
546 GNUNET_MQ_send (listener->client_mq, mqm);
551 * Handle a request for a set operation from another peer.
553 * This msg is expected as the first and only msg handled through the
554 * non-operation bound virtual table, acceptance of this operation replaces
555 * our virtual table and subsequent msgs would be routed differently (as
556 * we then know what type of operation this is).
558 * @param op the operation state
559 * @param mh the received message
560 * @return #GNUNET_OK if the channel should be kept alive,
561 * #GNUNET_SYSERR to destroy the channel
564 handle_incoming_msg (struct Operation *op,
565 const struct GNUNET_MessageHeader *mh)
567 const struct OperationRequestMessage *msg;
568 struct Listener *listener;
569 struct OperationSpecification *spec;
571 msg = (const struct OperationRequestMessage *) mh;
572 GNUNET_assert (GNUNET_YES == op->is_incoming);
573 if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
576 return GNUNET_SYSERR;
578 /* double operation request */
579 if (NULL != op->spec)
582 return GNUNET_SYSERR;
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 spec = GNUNET_new (struct OperationSpecification);
587 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
588 // for simplicity we just backup the context msg instead of rebuilding it later on
589 if (NULL != spec->context_msg)
590 spec->context_msg = GNUNET_copy_message (spec->context_msg);
591 spec->operation = ntohl (msg->operation);
592 spec->app_id = msg->app_id;
593 spec->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
595 spec->peer = op->peer;
596 spec->remote_element_count = ntohl (msg->element_count);
600 if ( (NULL != spec->context_msg) &&
601 (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
604 return GNUNET_SYSERR;
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608 "received P2P operation request (op %u, app %s)\n",
609 ntohl (msg->operation),
610 GNUNET_h2s (&msg->app_id));
611 listener = listener_get_by_target (ntohl (msg->operation),
613 if (NULL == listener)
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "no listener matches incoming request, waiting with timeout\n");
619 incoming_suggest (op, listener);
625 * Send the next element of a set to the set's client. The next element is given by
626 * the set's current hashmap iterator. The set's iterator will be set to NULL if there
627 * are no more elements in the set. The caller must ensure that the set's iterator is
630 * @param set set that should send its next element to its client
633 send_client_element (struct Set *set)
636 struct ElementEntry *ee;
637 struct GNUNET_MQ_Envelope *ev;
639 GNUNET_assert (NULL != set->iter);
640 ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter,
642 (const void **) &ee);
643 if (GNUNET_NO == ret)
645 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
646 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
651 struct GNUNET_SET_IterResponseMessage *msg;
653 GNUNET_assert (NULL != ee);
654 ev = GNUNET_MQ_msg_extra (msg,
656 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
657 memcpy (&msg[1], ee->element.data, ee->element.size);
658 msg->element_type = ee->element.element_type;
660 GNUNET_MQ_send (set->client_mq, ev);
665 * Called when a client wants to iterate the elements of a set.
668 * @param client client that sent the message
669 * @param m message sent by the client
672 handle_client_iterate (void *cls,
673 struct GNUNET_SERVER_Client *client,
674 const struct GNUNET_MessageHeader *m)
678 // iterate over a non existing set
679 set = set_get (client);
683 GNUNET_SERVER_client_disconnect (client);
687 // only one concurrent iterate-action per set
688 if (NULL != set->iter)
691 GNUNET_SERVER_client_disconnect (client);
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "iterating union set with %u elements\n",
696 GNUNET_CONTAINER_multihashmap_size (set->elements));
697 GNUNET_SERVER_receive_done (client, GNUNET_OK);
698 set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
699 send_client_element (set);
704 * Called when a client wants to create a new set.
707 * @param client client that sent the message
708 * @param m message sent by the client
711 handle_client_create_set (void *cls,
712 struct GNUNET_SERVER_Client *client,
713 const struct GNUNET_MessageHeader *m)
715 const struct GNUNET_SET_CreateMessage *msg;
718 msg = (const struct GNUNET_SET_CreateMessage *) m;
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "client created new set (operation %u)\n",
721 ntohs (msg->operation));
723 // max. one set per client!
724 if (NULL != set_get (client))
727 GNUNET_SERVER_client_disconnect (client);
731 set = GNUNET_new (struct Set);
733 switch (ntohl (msg->operation))
735 case GNUNET_SET_OPERATION_INTERSECTION:
736 set->vt = _GSS_intersection_vt ();
738 case GNUNET_SET_OPERATION_UNION:
739 set->vt = _GSS_union_vt ();
744 GNUNET_SERVER_client_disconnect (client);
748 set->state = set->vt->create ();
749 set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
750 set->client = client;
751 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
752 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
753 GNUNET_SERVER_receive_done (client, GNUNET_OK);
758 * Called when a client wants to create a new listener.
761 * @param client client that sent the message
762 * @param m message sent by the client
765 handle_client_listen (void *cls,
766 struct GNUNET_SERVER_Client *client,
767 const struct GNUNET_MessageHeader *m)
769 const struct GNUNET_SET_ListenMessage *msg;
770 struct Listener *listener;
771 struct Operation *op;
773 msg = (const struct GNUNET_SET_ListenMessage *) m;
774 /* max. one per client! */
775 if (NULL != listener_get (client))
778 GNUNET_SERVER_client_disconnect (client);
782 listener = GNUNET_new (struct Listener);
783 listener->client = client;
784 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
785 listener->app_id = msg->app_id;
786 listener->operation = ntohl (msg->operation);
787 GNUNET_CONTAINER_DLL_insert_tail (listeners_head,
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "new listener created (op %u, app %s)\n",
793 GNUNET_h2s (&listener->app_id));
795 /* check for incoming requests the listener is interested in */
796 for (op = incoming_head; NULL != op; op = op->next)
798 if (NULL == op->spec)
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "request has no spec yet\n");
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "considering (op: %u, app: %s, suggest: %u)\n",
807 GNUNET_h2s (&op->spec->app_id),
810 /* don't consider the incoming request if it has been already suggested to a listener */
811 if (0 != op->suggest_id)
813 if (listener->operation != op->spec->operation)
815 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "request suggested\n");
819 incoming_suggest (op, listener);
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "considered all incoming requests\n");
823 GNUNET_SERVER_receive_done (client, GNUNET_OK);
828 * Called when the listening client rejects an operation
829 * request by another peer.
832 * @param client client that sent the message
833 * @param m message sent by the client
836 handle_client_reject (void *cls,
837 struct GNUNET_SERVER_Client *client,
838 const struct GNUNET_MessageHeader *m)
840 struct Operation *incoming;
841 const struct GNUNET_SET_RejectMessage *msg;
843 msg = (const struct GNUNET_SET_RejectMessage *) m;
844 GNUNET_break (0 == ntohl (msg->request_id));
846 // no matching incoming operation for this reject
847 incoming = get_incoming (ntohl (msg->accept_reject_id));
848 if (NULL == incoming)
850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "peer request rejected by client\n");
856 GNUNET_CADET_channel_destroy (incoming->channel);
857 //channel destruction handler called immediately upon destruction
858 GNUNET_SERVER_receive_done (client, GNUNET_OK);
863 * Called when a client wants to add/remove an element to/from a
867 * @param client client that sent the message
868 * @param m message sent by the client
871 handle_client_add_remove (void *cls,
872 struct GNUNET_SERVER_Client *client,
873 const struct GNUNET_MessageHeader *m)
876 const struct GNUNET_SET_ElementMessage *msg;
877 struct GNUNET_SET_Element el;
878 struct ElementEntry *ee;
880 // client without a set requested an operation
881 set = set_get (client);
885 GNUNET_SERVER_client_disconnect (client);
888 GNUNET_SERVER_receive_done (client, GNUNET_OK);
889 msg = (const struct GNUNET_SET_ElementMessage *) m;
890 el.size = ntohs (m->size) - sizeof *msg;
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "client ins/rem element of size %u\n", el.size);
894 if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
896 struct GNUNET_HashCode hash;
898 GNUNET_CRYPTO_hash (el.data, el.size, &hash);
899 ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
902 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
903 "client tried to remove non-existing element\n");
906 if (GNUNET_YES == ee->removed)
908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
909 "client tried to remove element twice\n");
912 ee->removed = GNUNET_YES;
913 ee->generation_removed = set->current_generation;
914 set->vt->remove (set->state, ee);
918 struct ElementEntry *ee_dup;
920 ee = GNUNET_malloc (el.size + sizeof *ee);
921 ee->element.size = el.size;
922 memcpy (&ee[1], el.data, el.size);
923 ee->element.data = &ee[1];
924 ee->generation_added = set->current_generation;
925 ee->remote = GNUNET_NO;
926 GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
927 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "element inserted twice, ignoring\n");
936 GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
938 set->vt->add (set->state, ee);
944 * Called when a client wants to evaluate a set operation with another peer.
947 * @param client client that sent the message
948 * @param m message sent by the client
951 handle_client_evaluate (void *cls,
952 struct GNUNET_SERVER_Client *client,
953 const struct GNUNET_MessageHeader *m)
956 const struct GNUNET_SET_EvaluateMessage *msg;
957 struct OperationSpecification *spec;
958 struct Operation *op;
960 set = set_get (client);
964 GNUNET_SERVER_client_disconnect (client);
968 msg = (const struct GNUNET_SET_EvaluateMessage *) m;
969 spec = GNUNET_new (struct OperationSpecification);
970 spec->operation = set->operation;
971 spec->app_id = msg->app_id;
972 spec->salt = ntohl (msg->salt);
973 spec->peer = msg->target_peer;
975 spec->result_mode = ntohs (msg->result_mode);
976 spec->client_request_id = ntohl (msg->request_id);
977 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
979 // for simplicity we just backup the context msg instead of rebuilding it later on
980 if (NULL != spec->context_msg)
981 spec->context_msg = GNUNET_copy_message (spec->context_msg);
983 op = GNUNET_new (struct Operation);
985 op->generation_created = set->current_generation++;
987 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
989 op->channel = GNUNET_CADET_channel_create (cadet, op, &msg->target_peer,
990 GNUNET_APPLICATION_TYPE_SET,
991 GNUNET_CADET_OPTION_RELIABLE);
993 op->mq = GNUNET_CADET_mq_create (op->channel);
995 set->vt->evaluate (op);
996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1001 * Handle an ack from a client, and send the next element.
1004 * @param client the client
1005 * @param m the message
1008 handle_client_iter_ack (void *cls,
1009 struct GNUNET_SERVER_Client *client,
1010 const struct GNUNET_MessageHeader *m)
1014 // client without a set requested an operation
1015 set = set_get (client);
1019 GNUNET_SERVER_client_disconnect (client);
1023 // client sent an ack, but we were not expecting one
1024 if (NULL == set->iter)
1027 GNUNET_SERVER_client_disconnect (client);
1031 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1032 send_client_element (set);
1037 * Handle a request from the client to
1038 * cancel a running set operation.
1041 * @param client the client
1042 * @param mh the message
1045 handle_client_cancel (void *cls,
1046 struct GNUNET_SERVER_Client *client,
1047 const struct GNUNET_MessageHeader *mh)
1049 const struct GNUNET_SET_CancelMessage *msg =
1050 (const struct GNUNET_SET_CancelMessage *) mh;
1052 struct Operation *op;
1055 // client without a set requested an operation
1056 set = set_get (client);
1060 GNUNET_SERVER_client_disconnect (client);
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 "client requested cancel for op %u\n",
1066 ntohl (msg->request_id));
1069 for (op = set->ops_head; NULL != op; op = op->next)
1071 if (op->spec->client_request_id == ntohl (msg->request_id))
1078 /* It may happen that the operation was destroyed due to
1079 * the other peer disconnecting. The client may not know about this
1080 * yet and try to cancel the (non non-existent) operation.
1082 if (GNUNET_NO != found)
1083 _GSS_operation_destroy (op,
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 "client canceled non-existent op\n");
1090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1095 * Handle a request from the client to accept
1096 * a set operation that came from a remote peer.
1097 * We forward the accept to the associated operation for handling
1100 * @param client the client
1101 * @param mh the message
1104 handle_client_accept (void *cls,
1105 struct GNUNET_SERVER_Client *client,
1106 const struct GNUNET_MessageHeader *mh)
1109 const struct GNUNET_SET_AcceptMessage *msg;
1110 struct Operation *op;
1112 msg = (const struct GNUNET_SET_AcceptMessage *) mh;
1114 // client without a set requested an operation
1115 set = set_get (client);
1120 GNUNET_SERVER_client_disconnect (client);
1124 op = get_incoming (ntohl (msg->accept_reject_id));
1126 /* it is not an error if the set op does not exist -- it may
1127 * have been destroyed when the partner peer disconnected. */
1130 struct GNUNET_SET_ResultMessage *result_message;
1131 struct GNUNET_MQ_Envelope *ev;
1132 ev = GNUNET_MQ_msg (result_message, GNUNET_MESSAGE_TYPE_SET_RESULT);
1133 result_message->request_id = msg->request_id;
1134 result_message->element_type = 0;
1135 result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1136 GNUNET_MQ_send (set->client_mq, ev);
1137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "client accepting %u\n",
1143 ntohl (msg->accept_reject_id));
1145 GNUNET_assert (GNUNET_YES == op->is_incoming);
1148 op->spec->set = set;
1150 GNUNET_assert (GNUNET_YES == op->is_incoming);
1151 op->is_incoming = GNUNET_NO;
1152 GNUNET_CONTAINER_DLL_remove (incoming_head,
1156 GNUNET_assert (NULL != op->spec->set);
1157 GNUNET_assert (NULL != op->spec->set->vt);
1159 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1163 op->spec->client_request_id = ntohl (msg->request_id);
1164 op->spec->result_mode = ntohl (msg->result_mode);
1165 op->generation_created = set->current_generation++;
1166 op->vt = op->spec->set->vt;
1167 GNUNET_assert (NULL != op->vt->accept);
1168 set->vt->accept (op);
1169 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1174 * Called to clean up, after a shutdown has been requested.
1176 * @param cls closure
1177 * @param tc context information (why was this task triggered now)
1180 shutdown_task (void *cls,
1181 const struct GNUNET_SCHEDULER_TaskContext *tc)
1183 while (NULL != incoming_head)
1184 incoming_destroy (incoming_head);
1186 while (NULL != listeners_head)
1187 listener_destroy (listeners_head);
1189 while (NULL != sets_head)
1190 set_destroy (sets_head);
1192 /* it's important to destroy cadet at the end, as all channels
1193 * must be destroyed before the cadet handle! */
1196 GNUNET_CADET_disconnect (cadet);
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1200 "handled shutdown request\n");
1205 * Timeout happens iff:
1206 * - we suggested an operation to our listener,
1207 * but did not receive a response in time
1208 * - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1209 * - shutdown (obviously)
1211 * @param cls channel context
1212 * @param tc context information (why was this task triggered now)
1215 incoming_timeout_cb (void *cls,
1216 const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 struct Operation *incoming = cls;
1220 incoming->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1221 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1222 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "remote peer timed out\n");
1226 incoming_destroy (incoming);
1231 * Terminates an incoming operation in case we have not yet received an
1232 * operation request. Called by the channel destruction handler.
1234 * @param op the channel context
1237 handle_incoming_disconnect (struct Operation *op)
1239 GNUNET_assert (GNUNET_YES == op->is_incoming);
1240 /* channel is already dead, incoming_destroy must not
1243 incoming_destroy (op);
1249 * Method called whenever another peer has added us to a channel the
1250 * other peer initiated. Only called (once) upon reception of data
1251 * with a message type which was subscribed to in
1252 * GNUNET_CADET_connect().
1254 * The channel context represents the operation itself and gets added to a DLL,
1255 * from where it gets looked up when our local listener client responds
1256 * to a proposed/suggested operation or connects and associates with this operation.
1258 * @param cls closure
1259 * @param channel new handle to the channel
1260 * @param initiator peer that started the channel
1261 * @param port Port this channel is for.
1262 * @param options Unused.
1263 * @return initial channel context for the channel
1264 * (can be NULL -- that's not an error)
1267 channel_new_cb (void *cls,
1268 struct GNUNET_CADET_Channel *channel,
1269 const struct GNUNET_PeerIdentity *initiator,
1271 enum GNUNET_CADET_ChannelOption options)
1273 static const struct SetVT incoming_vt = {
1274 .msg_handler = &handle_incoming_msg,
1275 .peer_disconnect = &handle_incoming_disconnect
1277 struct Operation *incoming;
1279 if (GNUNET_APPLICATION_TYPE_SET != port)
1282 GNUNET_CADET_channel_destroy (channel);
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "new incoming channel\n");
1287 incoming = GNUNET_new (struct Operation);
1288 incoming->is_incoming = GNUNET_YES;
1289 incoming->peer = *initiator;
1290 incoming->channel = channel;
1291 incoming->mq = GNUNET_CADET_mq_create (incoming->channel);
1292 incoming->vt = &incoming_vt;
1293 incoming->timeout_task =
1294 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1295 &incoming_timeout_cb,
1297 GNUNET_CONTAINER_DLL_insert_tail (incoming_head,
1305 * Function called whenever a channel is destroyed. Should clean up
1306 * any associated state. It must NOT call
1307 * GNUNET_CADET_channel_destroy() on the channel.
1309 * The peer_disconnect function is part of a a virtual table set initially either
1310 * when a peer creates a new channel with us (channel_new_cb), or once we create
1311 * a new channel ourselves (evaluate).
1313 * Once we know the exact type of operation (union/intersection), the vt is
1314 * replaced with an operation specific instance (_GSS_[op]_vt).
1316 * @param cls closure (set from GNUNET_CADET_connect())
1317 * @param channel connection to the other end (henceforth invalid)
1318 * @param channel_ctx place where local state associated
1319 * with the channel is stored
1322 channel_end_cb (void *cls,
1323 const struct GNUNET_CADET_Channel *channel,
1326 struct Operation *op = channel_ctx;
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "channel end cb called\n");
1331 /* the vt can be null if a client already requested canceling op. */
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "calling peer disconnect due to channel end\n");
1336 op->vt->peer_disconnect (op);
1339 if (GNUNET_YES == op->keep)
1342 /* cadet will never call us with the context again! */
1343 GNUNET_free (channel_ctx);
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1345 "channel end cb finished\n");
1350 * Functions with this signature are called whenever a message is
1351 * received via a cadet channel.
1353 * The msg_handler is a virtual table set in initially either when a peer
1354 * creates a new channel with us (channel_new_cb), or once we create a new channel
1355 * ourselves (evaluate).
1357 * Once we know the exact type of operation (union/intersection), the vt is
1358 * replaced with an operation specific instance (_GSS_[op]_vt).
1360 * @param cls Closure (set from GNUNET_CADET_connect()).
1361 * @param channel Connection to the other end.
1362 * @param channel_ctx Place to store local state associated with the channel.
1363 * @param message The actual message.
1364 * @return #GNUNET_OK to keep the channel open,
1365 * #GNUNET_SYSERR to close it (signal serious error).
1368 dispatch_p2p_message (void *cls,
1369 struct GNUNET_CADET_Channel *channel,
1371 const struct GNUNET_MessageHeader *message)
1373 struct Operation *op = *channel_ctx;
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 "dispatching cadet message (type: %u)\n",
1378 ntohs (message->type));
1379 /* do this before the handler, as the handler might kill the channel */
1380 GNUNET_CADET_receive_done (channel);
1382 ret = op->vt->msg_handler (op, message);
1384 ret = GNUNET_SYSERR;
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "handled cadet message (type: %u)\n",
1387 ntohs (message->type));
1393 * Function called by the service's run
1394 * method to run service-specific setup code.
1396 * @param cls closure
1397 * @param server the initialized server
1398 * @param cfg configuration to use
1401 run (void *cls, struct GNUNET_SERVER_Handle *server,
1402 const struct GNUNET_CONFIGURATION_Handle *cfg)
1404 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1405 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1406 sizeof (struct GNUNET_SET_AcceptMessage)},
1407 {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
1408 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
1409 {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
1410 sizeof (struct GNUNET_SET_CreateMessage)},
1411 {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1412 sizeof (struct GNUNET_MessageHeader)},
1413 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
1414 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
1415 sizeof (struct GNUNET_SET_ListenMessage)},
1416 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
1417 sizeof (struct GNUNET_SET_RejectMessage)},
1418 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
1419 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
1420 sizeof (struct GNUNET_SET_CancelMessage)},
1423 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1424 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1425 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1426 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1427 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
1428 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1429 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1430 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
1431 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1432 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART, 0},
1435 static const uint32_t cadet_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1437 configuration = cfg;
1438 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1439 &shutdown_task, NULL);
1440 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1441 GNUNET_SERVER_add_handlers (server, server_handlers);
1443 cadet = GNUNET_CADET_connect (cfg, NULL, channel_new_cb, channel_end_cb,
1444 cadet_handlers, cadet_ports);
1447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1448 _("Could not connect to cadet service\n"));
1455 * The main function for the set service.
1457 * @param argc number of arguments from the command line
1458 * @param argv command line arguments
1459 * @return 0 ok, 1 on error
1462 main (int argc, char *const *argv)
1466 ret = GNUNET_SERVICE_run (argc, argv, "set",
1467 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1468 return (GNUNET_OK == ret) ? 0 : 1;
1471 /* end of gnunet-service-set.c */