2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file set/gnunet-service-set.c
23 * @brief two-peer set operations
24 * @author Florian Dold
26 #include "gnunet-service-set.h"
27 #include "set_protocol.h"
31 * State of an operation where the peer has connected to us, but is not yet
32 * evaluating a set operation. Once the peer has sent a concrete request, and
33 * the client has accepted or rejected it, this information will be deleted
34 * and replaced by the real set operation state.
39 * The identity of the requesting peer. Needs to
40 * be stored here as the op spec might not have been created yet.
42 struct GNUNET_PeerIdentity peer;
45 * Unique request id for the request from
46 * a remote peer, sent to the client, which will
47 * accept or reject the request.
48 * Set to '0' iff the request has not been
54 * Timeout task, if the incoming peer has not been accepted
55 * after the timeout, it will be disconnected.
57 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
62 * A listener is inhabited by a client, and
63 * waits for evaluation requests from remote peers.
68 * Listeners are held in a doubly linked list.
70 struct Listener *next;
73 * Listeners are held in a doubly linked list.
75 struct Listener *prev;
78 * Client that owns the listener.
79 * Only one client may own a listener.
81 struct GNUNET_SERVER_Client *client;
84 * Message queue for the client
86 struct GNUNET_MQ_Handle *client_mq;
89 * The type of the operation.
91 enum GNUNET_SET_OperationType operation;
94 * Application ID for the operation, used to distinguish
95 * multiple operations of the same type with the same peer.
97 struct GNUNET_HashCode app_id;
102 * Configuration of our local peer.
104 static const struct GNUNET_CONFIGURATION_Handle *configuration;
107 * Handle to the mesh service, used
108 * to listen for and connect to remote peers.
110 static struct GNUNET_MESH_Handle *mesh;
113 * Sets are held in a doubly linked list.
115 static struct Set *sets_head;
118 * Sets are held in a doubly linked list.
120 static struct Set *sets_tail;
123 * Listeners are held in a doubly linked list.
125 static struct Listener *listeners_head;
128 * Listeners are held in a doubly linked list.
130 static struct Listener *listeners_tail;
133 * Incoming sockets from remote peers are
134 * held in a doubly linked list.
136 static struct Operation *incoming_head;
139 * Incoming sockets from remote peers are
140 * held in a doubly linked list.
142 static struct Operation *incoming_tail;
145 * Counter for allocating unique IDs for clients,
146 * used to identify incoming operation requests from remote peers,
147 * that the client can choose to accept or refuse.
149 static uint32_t suggest_id = 1;
153 * Get set that is owned by the given client, if any.
155 * @param client client to look for
156 * @return set that the client owns, NULL if the client
160 set_get (struct GNUNET_SERVER_Client *client)
164 for (set = sets_head; NULL != set; set = set->next)
165 if (set->client == client)
172 * Get the listener associated with the given client, if any.
174 * @param client the client
175 * @return listener associated with the client, NULL
178 static struct Listener *
179 listener_get (struct GNUNET_SERVER_Client *client)
181 struct Listener *listener;
183 for (listener = listeners_head; NULL != listener; listener = listener->next)
184 if (listener->client == client)
191 * Get the incoming socket associated with the given id.
193 * @param id id to look for
194 * @return the incoming socket associated with the id,
195 * or NULL if there is none
197 static struct Operation *
198 get_incoming (uint32_t id)
200 struct Operation *op;
202 for (op = incoming_head; NULL != op; op = op->next)
203 if (op->state->suggest_id == id)
205 // FIXME: remove this assertion once the corresponding bug is gone!
206 GNUNET_assert (GNUNET_YES == op->is_incoming);
214 * Destroy a listener, free all resources associated with it.
216 * @param listener listener to destroy
219 listener_destroy (struct Listener *listener)
221 /* If the client is not dead yet, destroy it.
222 * The client's destroy callback will destroy the listener again. */
223 if (NULL != listener->client)
225 struct GNUNET_SERVER_Client *client = listener->client;
226 listener->client = NULL;
227 GNUNET_SERVER_client_disconnect (client);
230 if (NULL != listener->client_mq)
232 GNUNET_MQ_destroy (listener->client_mq);
233 listener->client_mq = NULL;
235 GNUNET_CONTAINER_DLL_remove (listeners_head, listeners_tail, listener);
236 GNUNET_free (listener);
241 * Collect and destroy elements that are not needed anymore, because
242 * their lifetime (as determined by their generation) does not overlap with any active
245 * We hereby replace the old element hashmap with a new one, instead of removing elements.
248 collect_generation_garbage (struct Set *set)
250 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
251 struct ElementEntry *ee;
252 struct GNUNET_CONTAINER_MultiHashMap *new_elements;
254 struct Operation *op;
256 new_elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
257 iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
259 (res = GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL, (const void **) &ee)))
261 if (GNUNET_NO == ee->removed)
263 for (op = set->ops_head; NULL != op; op = op->next)
264 if ((op->generation_created >= ee->generation_added) &&
265 (op->generation_created < ee->generation_removed))
270 // we don't expect collisions, thus the replace option
271 GNUNET_CONTAINER_multihashmap_put (new_elements, &ee->element_hash, ee,
272 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
274 GNUNET_CONTAINER_multihashmap_iterator_destroy (iter);
275 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
276 set->elements = new_elements;
281 * Destroy the given operation. Call the implementation-specific cancel function
282 * of the operation. Disconnects from the remote peer.
283 * Does not disconnect the client, as there may be multiple operations per set.
285 * @param op operation to destroy
288 _GSS_operation_destroy (struct Operation *op)
291 struct GNUNET_MESH_Channel *channel;
298 GNUNET_assert (GNUNET_NO == op->is_incoming);
299 GNUNET_assert (NULL != op->spec);
300 GNUNET_CONTAINER_DLL_remove (op->spec->set->ops_head,
301 op->spec->set->ops_tail,
307 if (NULL != op->spec)
309 if (NULL != op->spec->context_msg)
311 GNUNET_free (op->spec->context_msg);
312 op->spec->context_msg = NULL;
314 GNUNET_free (op->spec);
320 GNUNET_MQ_destroy (op->mq);
324 if (NULL != (channel = op->channel))
327 GNUNET_MESH_channel_destroy (channel);
330 collect_generation_garbage (set);
332 /* We rely on the channel end handler to free 'op'. When 'op->channel' was NULL,
333 * there was a channel end handler that will free 'op' on the call stack. */
338 * Iterator over hash map entries to free
342 * @param key current key code
343 * @param value a `struct ElementEntry *` to be free'd
344 * @return #GNUNET_YES if we should continue to
349 destroy_elements_iterator (void *cls,
350 const struct GNUNET_HashCode * key,
353 struct ElementEntry *ee = value;
361 * Destroy a set, and free all resources associated with it.
363 * @param set the set to destroy
366 set_destroy (struct Set *set)
368 /* If the client is not dead yet, destroy it.
369 * The client's destroy callback will destroy the set again.
370 * We do this so that the channel end handler still has a valid set handle
372 if (NULL != set->client)
374 struct GNUNET_SERVER_Client *client = set->client;
376 GNUNET_SERVER_client_disconnect (client);
379 GNUNET_assert (NULL != set->state);
380 while (NULL != set->ops_head)
381 _GSS_operation_destroy (set->ops_head);
382 set->vt->destroy_set (set->state);
384 if (NULL != set->client_mq)
386 GNUNET_MQ_destroy (set->client_mq);
387 set->client_mq = NULL;
389 if (NULL != set->iter)
391 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
394 GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
395 if (NULL != set->elements)
397 // free all elements in the hashtable, before destroying the table
398 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
399 destroy_elements_iterator, NULL);
400 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
401 set->elements = NULL;
408 * Clean up after a client has disconnected
410 * @param cls closure, unused
411 * @param client the client to clean up after
414 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
417 struct Listener *listener;
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "client disconnected, cleaning up\n");
421 set = set_get (client);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "(client's set destroyed)\n");
429 listener = listener_get (client);
430 if (NULL != listener)
432 listener->client = NULL;
433 listener_destroy (listener);
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "(client's listener destroyed)\n");
441 * Destroy an incoming request from a remote peer
443 * @param incoming remote request to destroy
446 incoming_destroy (struct Operation *incoming)
448 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
449 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
450 if (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task)
452 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
453 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
455 GNUNET_assert (NULL != incoming->state);
456 GNUNET_free (incoming->state);
457 incoming->state = NULL;
462 * remove & free state of the operation from the incoming list
464 * @param incoming the element to remove
467 incoming_retire (struct Operation *incoming)
469 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
470 incoming->is_incoming = GNUNET_NO;
471 GNUNET_assert (NULL != incoming->state);
472 GNUNET_free (incoming->state);
473 incoming->state = NULL;
474 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
479 * Find a listener that is interested in the given operation type
480 * and application id.
482 * @param op operation type to look for
483 * @param app_id application id to look for
484 * @return a matching listener, or NULL if no listener matches the
485 * given operation and application id
487 static struct Listener *
488 listener_get_by_target (enum GNUNET_SET_OperationType op,
489 const struct GNUNET_HashCode *app_id)
493 for (l = listeners_head; NULL != l; l = l->next)
495 if (l->operation != op)
497 if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
506 * Suggest the given request to the listener. The listening client can then
507 * accept or reject the remote request.
509 * @param incoming the incoming peer with the request to suggest
510 * @param listener the listener to suggest the request to
513 incoming_suggest (struct Operation *incoming, struct Listener *listener)
515 struct GNUNET_MQ_Envelope *mqm;
516 struct GNUNET_SET_RequestMessage *cmsg;
518 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
519 GNUNET_assert (NULL != incoming->state);
520 GNUNET_assert (NULL != incoming->spec);
521 GNUNET_assert (0 == incoming->state->suggest_id);
522 incoming->state->suggest_id = suggest_id++;
524 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task);
525 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
526 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
528 mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
529 incoming->spec->context_msg);
530 GNUNET_assert (NULL != mqm);
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n",
532 incoming->state->suggest_id);
533 cmsg->accept_id = htonl (incoming->state->suggest_id);
534 cmsg->peer_id = incoming->spec->peer;
535 GNUNET_MQ_send (listener->client_mq, mqm);
540 * Handle a request for a set operation from
543 * This msg is expected as the first and only msg handled through the
544 * non-operation bound virtual table, acceptance of this operation replaces
545 * our virtual table and subsequent msgs would be routed differently.
547 * @param op the operation state
548 * @param mh the received message
549 * @return #GNUNET_OK if the channel should be kept alive,
550 * #GNUNET_SYSERR to destroy the channel
553 handle_incoming_msg (struct Operation *op,
554 const struct GNUNET_MessageHeader *mh)
556 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
557 struct Listener *listener;
558 struct OperationSpecification *spec;
560 GNUNET_assert (GNUNET_YES == op->is_incoming);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got op request\n");
564 if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
567 return GNUNET_SYSERR;
570 /* double operation request */
571 if (NULL != op->spec)
574 return GNUNET_SYSERR;
577 spec = GNUNET_new (struct OperationSpecification);
578 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
579 // for simplicity we just backup the context msg instead of rebuilding it later on
580 if (NULL != spec->context_msg)
581 spec->context_msg = GNUNET_copy_message (spec->context_msg);
582 spec->operation = ntohl (msg->operation);
583 spec->app_id = msg->app_id;
584 spec->salt = ntohl (msg->salt);
585 spec->peer = op->state->peer;
586 spec->remote_element_count = ntohl (msg->element_count);
590 if ( (NULL != spec->context_msg) &&
591 (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
594 return GNUNET_SYSERR;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received P2P operation request (op %u, app %s)\n",
598 ntohl (msg->operation), GNUNET_h2s (&msg->app_id));
599 listener = listener_get_by_target (ntohl (msg->operation), &msg->app_id);
600 if (NULL == listener)
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "no listener matches incoming request, waiting with timeout\n");
606 incoming_suggest (op, listener);
612 * Send the next element of a set to the set's client. The next element is given by
613 * the set's current hashmap iterator. The set's iterator will be set to NULL if there
614 * are no more elements in the set. The caller must ensure that the set's iterator is
617 * @param set set that should send its next element to its client
620 send_client_element (struct Set *set)
623 struct ElementEntry *ee;
624 struct GNUNET_MQ_Envelope *ev;
626 GNUNET_assert (NULL != set->iter);
627 ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, NULL, (const void **) &ee);
628 if (GNUNET_NO == ret)
630 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
631 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
636 struct GNUNET_SET_IterResponseMessage *msg;
638 GNUNET_assert (NULL != ee);
639 ev = GNUNET_MQ_msg_extra (msg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
640 memcpy (&msg[1], ee->element.data, ee->element.size);
641 msg->element_type = ee->element.type;
643 GNUNET_MQ_send (set->client_mq, ev);
648 * Called when a client wants to iterate the elements of a set.
651 * @param client client that sent the message
652 * @param m message sent by the client
655 handle_client_iterate (void *cls,
656 struct GNUNET_SERVER_Client *client,
657 const struct GNUNET_MessageHeader *m)
661 // iterate over a non existing set
662 set = set_get (client);
666 GNUNET_SERVER_client_disconnect (client);
670 // only one concurrent iterate-action per set
671 if (NULL != set->iter)
674 GNUNET_SERVER_client_disconnect (client);
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
678 GNUNET_CONTAINER_multihashmap_size (set->elements));
679 GNUNET_SERVER_receive_done (client, GNUNET_OK);
680 set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
681 send_client_element (set);
686 * Called when a client wants to create a new set.
689 * @param client client that sent the message
690 * @param m message sent by the client
693 handle_client_create_set (void *cls,
694 struct GNUNET_SERVER_Client *client,
695 const struct GNUNET_MessageHeader *m)
697 const struct GNUNET_SET_CreateMessage *msg;
700 msg = (const struct GNUNET_SET_CreateMessage *) m;
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "client created new set (operation %u)\n",
703 ntohs (msg->operation));
705 // max. one set per client!
706 if (NULL != set_get (client))
709 GNUNET_SERVER_client_disconnect (client);
713 set = GNUNET_new (struct Set);
715 switch (ntohs (msg->operation))
717 case GNUNET_SET_OPERATION_INTERSECTION:
718 // FIXME: implement intersection vt
719 // set->vt = _GSS_intersection_vt ();
721 case GNUNET_SET_OPERATION_UNION:
722 set->vt = _GSS_union_vt ();
727 GNUNET_SERVER_client_disconnect (client);
731 set->state = set->vt->create ();
732 set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
733 set->client = client;
734 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
735 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
736 GNUNET_SERVER_receive_done (client, GNUNET_OK);
741 * Called when a client wants to create a new listener.
744 * @param client client that sent the message
745 * @param m message sent by the client
748 handle_client_listen (void *cls,
749 struct GNUNET_SERVER_Client *client,
750 const struct GNUNET_MessageHeader *m)
752 const struct GNUNET_SET_ListenMessage *msg;
753 struct Listener *listener;
754 struct Operation *op;
756 msg = (const struct GNUNET_SET_ListenMessage *) m;
757 /* max. one per client! */
758 if (NULL != listener_get (client))
761 GNUNET_SERVER_client_disconnect (client);
765 listener = GNUNET_new (struct Listener);
766 listener->client = client;
767 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
768 listener->app_id = msg->app_id;
769 listener->operation = ntohl (msg->operation);
770 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "new listener created (op %u, app %s)\n",
774 GNUNET_h2s (&listener->app_id));
776 /* check for incoming requests the listener is interested in */
777 for (op = incoming_head; NULL != op; op = op->next)
779 if (NULL == op->spec)
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "request has no spec yet\n");
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "considering (op: %u, app: %s, suggest: %u)\n",
788 GNUNET_h2s (&op->spec->app_id),
789 op->state->suggest_id);
791 /* don't consider the incoming request if it has been already suggested to a listener */
792 if (0 != op->state->suggest_id)
794 if (listener->operation != op->spec->operation)
796 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "request suggested\n");
800 incoming_suggest (op, listener);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "considered all incoming requests\n");
804 GNUNET_SERVER_receive_done (client, GNUNET_OK);
809 * Called when the listening client rejects an operation
810 * request by another peer.
813 * @param client client that sent the message
814 * @param m message sent by the client
817 handle_client_reject (void *cls,
818 struct GNUNET_SERVER_Client *client,
819 const struct GNUNET_MessageHeader *m)
821 struct Operation *incoming;
822 const struct GNUNET_SET_AcceptRejectMessage *msg;
824 msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
825 GNUNET_break (0 == ntohl (msg->request_id));
827 // no matching incoming operation for this reject
828 incoming = get_incoming (ntohl (msg->accept_reject_id));
829 if (NULL == incoming)
831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "peer request rejected by client\n");
837 GNUNET_MESH_channel_destroy (incoming->channel);
838 //channel destruction handler called immediately upon destruction
839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
844 * Called when a client wants to add/remove an element to/from a
848 * @param client client that sent the message
849 * @param m message sent by the client
852 handle_client_add_remove (void *cls,
853 struct GNUNET_SERVER_Client *client,
854 const struct GNUNET_MessageHeader *m)
857 const struct GNUNET_SET_ElementMessage *msg;
858 struct GNUNET_SET_Element el;
859 struct ElementEntry *ee;
861 // client without a set requested an operation
862 set = set_get (client);
866 GNUNET_SERVER_client_disconnect (client);
869 GNUNET_SERVER_receive_done (client, GNUNET_OK);
870 msg = (const struct GNUNET_SET_ElementMessage *) m;
871 el.size = ntohs (m->size) - sizeof *msg;
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "client ins/rem element of size %u\n", el.size);
875 if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
877 struct GNUNET_HashCode hash;
879 GNUNET_CRYPTO_hash (el.data, el.size, &hash);
880 ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
884 "client tried to remove non-existing element\n");
887 if (GNUNET_YES == ee->removed)
889 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
890 "client tried to remove element twice\n");
893 ee->removed = GNUNET_YES;
894 ee->generation_removed = set->current_generation;
895 set->vt->remove (set->state, ee);
899 struct ElementEntry *ee_dup;
901 ee = GNUNET_malloc (el.size + sizeof *ee);
902 ee->element.size = el.size;
903 memcpy (&ee[1], el.data, el.size);
904 ee->element.data = &ee[1];
905 ee->generation_added = set->current_generation;
906 ee->remote = GNUNET_NO;
907 GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
908 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "element inserted twice, ignoring\n");
917 GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
918 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
919 set->vt->add (set->state, ee);
925 * Called when a client wants to evaluate a set operation with another peer.
928 * @param client client that sent the message
929 * @param m message sent by the client
932 handle_client_evaluate (void *cls,
933 struct GNUNET_SERVER_Client *client,
934 const struct GNUNET_MessageHeader *m)
937 const struct GNUNET_SET_EvaluateMessage *msg;
938 struct OperationSpecification *spec;
939 struct Operation *op;
941 set = set_get (client);
945 GNUNET_SERVER_client_disconnect (client);
949 msg = (const struct GNUNET_SET_EvaluateMessage *) m;
950 spec = GNUNET_new (struct OperationSpecification);
951 spec->operation = set->operation;
952 spec->app_id = msg->app_id;
953 spec->salt = ntohl (msg->salt);
954 spec->peer = msg->target_peer;
956 spec->result_mode = ntohs (msg->result_mode);
957 spec->client_request_id = ntohl (msg->request_id);
958 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
960 // for simplicity we just backup the context msg instead of rebuilding it later on
961 if (NULL != spec->context_msg)
962 spec->context_msg = GNUNET_copy_message (spec->context_msg);
964 op = GNUNET_new (struct Operation);
966 op->generation_created = set->current_generation++;
968 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
970 op->channel = GNUNET_MESH_channel_create (mesh, op, &msg->target_peer,
971 GNUNET_APPLICATION_TYPE_SET,
972 GNUNET_MESH_OPTION_RELIABLE);
974 op->mq = GNUNET_MESH_mq_create (op->channel);
976 set->vt->evaluate (op);
977 GNUNET_SERVER_receive_done (client, GNUNET_OK);
982 * Handle an ack from a client, and send the next element.
985 * @param client the client
986 * @param m the message
989 handle_client_iter_ack (void *cls,
990 struct GNUNET_SERVER_Client *client,
991 const struct GNUNET_MessageHeader *m)
995 // client without a set requested an operation
996 set = set_get (client);
1000 GNUNET_SERVER_client_disconnect (client);
1004 // client sent an ack, but we were not expecting one
1005 if (NULL == set->iter)
1008 GNUNET_SERVER_client_disconnect (client);
1012 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1013 send_client_element (set);
1018 * Handle a request from the client to
1019 * cancel a running set operation.
1022 * @param client the client
1023 * @param mh the message
1026 handle_client_cancel (void *cls,
1027 struct GNUNET_SERVER_Client *client,
1028 const struct GNUNET_MessageHeader *mh)
1030 const struct GNUNET_SET_CancelMessage *msg =
1031 (const struct GNUNET_SET_CancelMessage *) mh;
1033 struct Operation *op;
1036 // client without a set requested an operation
1037 set = set_get (client);
1041 GNUNET_SERVER_client_disconnect (client);
1045 for (op = set->ops_head; NULL != op; op = op->next)
1047 if (op->spec->client_request_id == msg->request_id)
1054 if (GNUNET_NO == found)
1057 GNUNET_SERVER_client_disconnect (client);
1061 _GSS_operation_destroy (op);
1066 * Handle a request from the client to accept
1067 * a set operation that came from a remote peer.
1068 * We forward the accept to the associated operation for handling
1071 * @param client the client
1072 * @param mh the message
1075 handle_client_accept (void *cls,
1076 struct GNUNET_SERVER_Client *client,
1077 const struct GNUNET_MessageHeader *mh)
1080 const struct GNUNET_SET_AcceptRejectMessage *msg;
1081 struct Operation *op;
1083 msg = (const struct GNUNET_SET_AcceptRejectMessage *) mh;
1084 op = get_incoming (ntohl (msg->accept_reject_id));
1086 // incoming operation does not exist
1090 GNUNET_SERVER_client_disconnect (client);
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "client accepting %u\n",
1096 ntohl (msg->accept_reject_id));
1098 GNUNET_assert (GNUNET_YES == op->is_incoming);
1100 // client without a set requested an operation
1101 set = set_get (client);
1106 GNUNET_SERVER_client_disconnect (client);
1110 op->spec->set = set;
1112 incoming_retire (op);
1114 GNUNET_assert (NULL != op->spec->set);
1115 GNUNET_assert (NULL != op->spec->set->vt);
1117 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1119 op->spec->client_request_id = ntohl (msg->request_id);
1120 op->spec->result_mode = ntohs (msg->result_mode);
1121 op->generation_created = set->current_generation++;
1122 op->vt = op->spec->set->vt;
1123 GNUNET_assert (NULL != op->vt->accept);
1124 set->vt->accept (op);
1125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1130 * Called to clean up, after a shutdown has been requested.
1132 * @param cls closure
1133 * @param tc context information (why was this task triggered now)
1136 shutdown_task (void *cls,
1137 const struct GNUNET_SCHEDULER_TaskContext *tc)
1139 while (NULL != incoming_head)
1140 incoming_destroy (incoming_head);
1142 while (NULL != listeners_head)
1143 listener_destroy (listeners_head);
1145 while (NULL != sets_head)
1146 set_destroy (sets_head);
1148 /* it's important to destroy mesh at the end, as all channels
1149 * must be destroyed before the mesh handle! */
1152 GNUNET_MESH_disconnect (mesh);
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "handled shutdown request\n");
1161 * Timeout happens iff:
1162 * - we suggested an operation to our listener,
1163 * but did not receive a response in time
1164 * - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1165 * - shutdown (obviously)
1167 * @param cls channel context
1168 * @param tc context information (why was this task triggered now)
1171 incoming_timeout_cb (void *cls,
1172 const struct GNUNET_SCHEDULER_TaskContext *tc)
1174 struct Operation *incoming = cls;
1176 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1177 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1178 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "remote peer timed out\n");
1182 incoming_destroy (incoming);
1187 * Terminates an incoming operation in case we have not yet received an
1188 * operation request. Called by the channel destruction handler.
1190 * @param op the channel context
1193 handle_incoming_disconnect (struct Operation *op)
1195 GNUNET_assert (GNUNET_YES == op->is_incoming);
1196 incoming_destroy (op);
1202 * Method called whenever another peer has added us to a channel
1203 * the other peer initiated.
1204 * Only called (once) upon reception of data with a message type which was
1205 * subscribed to in GNUNET_MESH_connect().
1207 * The channel context represents the operation itself and gets added to a DLL,
1208 * from where it gets looked up when our local listener client responds
1209 * to a proposed/suggested operation or connects and associates with this operation.
1211 * @param cls closure
1212 * @param channel new handle to the channel
1213 * @param initiator peer that started the channel
1214 * @param port Port this channel is for.
1215 * @param options Unused.
1216 * @return initial channel context for the channel
1217 * (can be NULL -- that's not an error)
1220 channel_new_cb (void *cls,
1221 struct GNUNET_MESH_Channel *channel,
1222 const struct GNUNET_PeerIdentity *initiator,
1223 uint32_t port, enum GNUNET_MESH_ChannelOption options)
1225 struct Operation *incoming;
1226 static const struct SetVT incoming_vt = {
1227 .msg_handler = handle_incoming_msg,
1228 .peer_disconnect = handle_incoming_disconnect
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "new incoming channel\n");
1234 if (GNUNET_APPLICATION_TYPE_SET != port)
1237 GNUNET_MESH_channel_destroy (channel);
1241 incoming = GNUNET_new (struct Operation);
1242 incoming->is_incoming = GNUNET_YES;
1243 incoming->state = GNUNET_new (struct OperationState);
1244 incoming->state->peer = *initiator;
1245 incoming->channel = channel;
1246 incoming->mq = GNUNET_MESH_mq_create (incoming->channel);
1247 incoming->vt = &incoming_vt;
1248 incoming->state->timeout_task =
1249 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1250 &incoming_timeout_cb, incoming);
1251 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
1258 * Function called whenever a channel is destroyed. Should clean up
1259 * any associated state. It must NOT call
1260 * GNUNET_MESH_channel_destroy() on the channel.
1262 * The peer_disconnect function is part of a a virtual table set initially either
1263 * when a peer creates a new channel with us (channel_new_cb), or once we create
1264 * a new channel ourselves (evaluate).
1266 * Once we know the exact type of operation (union/intersection), the vt is
1267 * replaced with an operation specific instance (_GSS_[op]_vt).
1269 * @param cls closure (set from GNUNET_MESH_connect())
1270 * @param channel connection to the other end (henceforth invalid)
1271 * @param channel_ctx place where local state associated
1272 * with the channel is stored
1275 channel_end_cb (void *cls,
1276 const struct GNUNET_MESH_Channel *channel, void *channel_ctx)
1278 struct Operation *op = channel_ctx;
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "channel end cb called\n");
1283 /* the vt can be null if a client already requested canceling op. */
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1287 "calling peer disconnect due to channel end\n");
1288 op->vt->peer_disconnect (op);
1291 if (GNUNET_YES == op->keep)
1294 /* mesh will never call us with the context again! */
1295 GNUNET_free (channel_ctx);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "channel end cb finished\n");
1302 * Functions with this signature are called whenever a message is
1303 * received via a mesh channel.
1305 * The msg_handler is a virtual table set in initially either when a peer
1306 * creates a new channel with us (channel_new_cb), or once we create a new channel
1307 * ourselves (evaluate).
1309 * Once we know the exact type of operation (union/intersection), the vt is
1310 * replaced with an operation specific instance (_GSS_[op]_vt).
1312 * @param cls Closure (set from GNUNET_MESH_connect()).
1313 * @param channel Connection to the other end.
1314 * @param channel_ctx Place to store local state associated with the channel.
1315 * @param message The actual message.
1316 * @return #GNUNET_OK to keep the channel open,
1317 * #GNUNET_SYSERR to close it (signal serious error).
1320 dispatch_p2p_message (void *cls,
1321 struct GNUNET_MESH_Channel *channel,
1323 const struct GNUNET_MessageHeader *message)
1325 struct Operation *op = *channel_ctx;
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "dispatching mesh message (type: %u)\n",
1330 ntohs (message->type));
1331 /* do this before the handler, as the handler might kill the channel */
1332 GNUNET_MESH_receive_done (channel);
1333 ret = op->vt->msg_handler (op, message);
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "handled mesh message (type: %u)\n",
1336 ntohs (message->type));
1342 * Function called by the service's run
1343 * method to run service-specific setup code.
1345 * @param cls closure
1346 * @param server the initialized server
1347 * @param cfg configuration to use
1350 run (void *cls, struct GNUNET_SERVER_Handle *server,
1351 const struct GNUNET_CONFIGURATION_Handle *cfg)
1353 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1354 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1355 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1356 {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
1357 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
1358 {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
1359 sizeof (struct GNUNET_SET_CreateMessage)},
1360 {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1361 sizeof (struct GNUNET_MessageHeader)},
1362 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
1363 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
1364 sizeof (struct GNUNET_SET_ListenMessage)},
1365 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
1366 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1367 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
1368 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
1369 sizeof (struct GNUNET_SET_CancelMessage)},
1372 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1373 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1374 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1375 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1376 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
1377 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1378 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1379 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
1380 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1383 static const uint32_t mesh_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1385 configuration = cfg;
1386 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1387 &shutdown_task, NULL);
1388 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1389 GNUNET_SERVER_add_handlers (server, server_handlers);
1391 mesh = GNUNET_MESH_connect (cfg, NULL, channel_new_cb, channel_end_cb,
1392 mesh_handlers, mesh_ports);
1395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1396 _("Could not connect to mesh service\n"));
1403 * The main function for the set service.
1405 * @param argc number of arguments from the command line
1406 * @param argv command line arguments
1407 * @return 0 ok, 1 on error
1410 main (int argc, char *const *argv)
1414 ret = GNUNET_SERVICE_run (argc, argv, "set",
1415 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1416 return (GNUNET_OK == ret) ? 0 : 1;
1419 /* end of gnunet-service-set.c */