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)
203 if (op->state->suggest_id == id)
210 * Destroy a listener, free all resources associated with it.
212 * @param listener listener to destroy
215 listener_destroy (struct Listener *listener)
217 /* If the client is not dead yet, destroy it.
218 * The client's destroy callback will destroy the listener again. */
219 if (NULL != listener->client)
221 struct GNUNET_SERVER_Client *client = listener->client;
222 listener->client = NULL;
223 GNUNET_SERVER_client_disconnect (client);
226 if (NULL != listener->client_mq)
228 GNUNET_MQ_destroy (listener->client_mq);
229 listener->client_mq = NULL;
231 GNUNET_CONTAINER_DLL_remove (listeners_head, listeners_tail, listener);
232 GNUNET_free (listener);
237 * Iterator over hash map entries to free
241 * @param key current key code
242 * @param value value in the hash map
243 * @return GNUNET_YES if we should continue to
248 destroy_elements_iterator (void *cls,
249 const struct GNUNET_HashCode * key,
252 struct ElementEntry *ee = value;
260 * Collect and destroy elements that are not needed anymore, because
261 * their lifetime (as determined by their generation) does not overlap with any active
265 collect_generation_garbage (struct Set *set)
267 struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
268 struct ElementEntry *ee;
269 struct GNUNET_CONTAINER_MultiHashMap *new_elements;
271 struct Operation *op;
273 new_elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
274 iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
276 (res = GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL, (const void **) &ee)))
278 if (GNUNET_NO == ee->removed)
280 for (op = set->ops_head; NULL != op; op = op->next)
281 if ( (op->generation_created >= ee->generation_added) &&
282 (op->generation_created < ee->generation_removed) )
287 // we don't expect collisions, thus the replace option
288 GNUNET_CONTAINER_multihashmap_put (new_elements, &ee->element_hash, ee,
289 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
291 GNUNET_CONTAINER_multihashmap_iterator_destroy (iter);
292 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
293 set->elements = new_elements;
298 * Destroy the given operation. Call the implementation-specific cancel function
299 * of the operation. Disconnects from the remote peer.
300 * Does not disconnect the client, as there may be multiple operations per set.
302 * @param op operation to destroy
305 _GSS_operation_destroy (struct Operation *op)
314 GNUNET_assert (GNUNET_NO == op->is_incoming);
315 GNUNET_assert (NULL != op->spec);
316 GNUNET_CONTAINER_DLL_remove (op->spec->set->ops_head,
317 op->spec->set->ops_tail,
323 if (NULL != op->spec)
325 if (NULL != op->spec->context_msg)
327 GNUNET_free (op->spec->context_msg);
328 op->spec->context_msg = NULL;
330 GNUNET_free (op->spec);
336 GNUNET_MQ_destroy (op->mq);
340 if (NULL != op->tunnel)
342 GNUNET_MESH_tunnel_destroy (op->tunnel);
346 collect_generation_garbage (set);
348 /* We rely on the tunnel end handler to free 'op'. When 'op->tunnel' was NULL,
349 * there was a tunnel end handler that will free 'op' on the call stack. */
354 * Destroy a set, and free all resources associated with it.
356 * @param set the set to destroy
359 set_destroy (struct Set *set)
361 /* If the client is not dead yet, destroy it.
362 * The client's destroy callback will destroy the set again.
363 * We do this so that the tunnel end handler still has a valid set handle
365 // TODO: use client context
366 if (NULL != set->client)
368 struct GNUNET_SERVER_Client *client = set->client;
370 GNUNET_SERVER_client_disconnect (client);
373 GNUNET_assert (NULL != set->state);
374 while (NULL != set->ops_head)
375 _GSS_operation_destroy (set->ops_head);
376 set->vt->destroy_set (set->state);
378 if (NULL != set->client_mq)
380 GNUNET_MQ_destroy (set->client_mq);
381 set->client_mq = NULL;
383 if (NULL != set->iter)
385 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
388 GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
389 if (NULL != set->elements)
391 GNUNET_CONTAINER_multihashmap_iterate (set->elements,
392 destroy_elements_iterator, NULL);
393 GNUNET_CONTAINER_multihashmap_destroy (set->elements);
394 set->elements = NULL;
401 * Clean up after a client has disconnected
403 * @param cls closure, unused
404 * @param client the client to clean up after
407 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
410 struct Listener *listener;
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected, cleaning up\n");
414 set = set_get (client);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's set destroyed)\n");
421 listener = listener_get (client);
422 if (NULL != listener)
424 listener->client = NULL;
425 listener_destroy (listener);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's listener destroyed)\n");
432 * Destroy an incoming request from a remote peer
434 * @param incoming remote request to destroy
437 incoming_destroy (struct Operation *incoming)
439 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
440 if (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task)
442 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
443 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
445 GNUNET_free (incoming->state);
449 * remove & free state of the operation from the incoming list
451 * @param incoming the element to remove
455 incoming_retire (struct Operation *incoming)
457 incoming->is_incoming = GNUNET_NO;
458 GNUNET_free (incoming->state);
459 incoming->state = NULL;
460 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
465 * Find a listener that is interested in the given operation type
466 * and application id.
468 * @param op operation type to look for
469 * @param app_id application id to look for
470 * @return a matching listener, or NULL if no listener matches the
471 * given operation and application id
473 static struct Listener *
474 listener_get_by_target (enum GNUNET_SET_OperationType op,
475 const struct GNUNET_HashCode *app_id)
479 for (l = listeners_head; NULL != l; l = l->next)
481 if (l->operation != op)
483 if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
492 * Suggest the given request to the listener,
493 * who can accept or reject the request.
495 * @param incoming the incoming peer with the request to suggest
496 * @param listener the listener to suggest the request to
499 incoming_suggest (struct Operation *incoming, struct Listener *listener)
501 struct GNUNET_MQ_Envelope *mqm;
502 struct GNUNET_SET_RequestMessage *cmsg;
504 GNUNET_assert (NULL != incoming->spec);
505 GNUNET_assert (0 == incoming->state->suggest_id);
506 incoming->state->suggest_id = suggest_id++;
508 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task);
509 GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
510 incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
512 mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
513 incoming->spec->context_msg);
514 GNUNET_assert (NULL != mqm);
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n",
516 incoming->state->suggest_id);
517 cmsg->accept_id = htonl (incoming->state->suggest_id);
518 cmsg->peer_id = incoming->spec->peer;
519 GNUNET_MQ_send (listener->client_mq, mqm);
524 * Handle a request for a set operation from
527 * This msg is expected as the first and only msg handled through the
528 * non-operation bound virtual table, acceptance of this operation replaces
529 * our virtual table and subsequent msgs would be routed differently.
531 * @param op the operation state
532 * @param mh the received message
533 * @return GNUNET_OK if the tunnel should be kept alive,
534 * GNUNET_SYSERR to destroy the tunnel
537 handle_incoming_msg (struct Operation *op,
538 const struct GNUNET_MessageHeader *mh)
540 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
541 struct Listener *listener;
542 struct OperationSpecification *spec;
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got op request\n");
546 if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
549 return GNUNET_SYSERR;
552 /* double operation request */
553 if (NULL != op->spec)
556 return GNUNET_SYSERR;
559 spec = GNUNET_new (struct OperationSpecification);
560 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
561 // for simplicity we just backup the context msg instead of rebuilding it later on
562 if (NULL != spec->context_msg)
563 spec->context_msg = GNUNET_copy_message (spec->context_msg);
564 spec->operation = ntohl (msg->operation);
565 spec->app_id = msg->app_id;
566 spec->salt = ntohl (msg->salt);
567 spec->peer = op->state->peer;
571 if ( (NULL != spec->context_msg) &&
572 (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
575 return GNUNET_SYSERR;
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received P2P operation request (op %u, app %s)\n",
579 ntohl (msg->operation), GNUNET_h2s (&msg->app_id));
580 listener = listener_get_by_target (ntohl (msg->operation), &msg->app_id);
581 if (NULL == listener)
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "no listener matches incoming request, waiting with timeout\n");
587 incoming_suggest (op, listener);
593 * Send the next element of a set to the set's client. The next element is given by
594 * the set's current hashmap iterator. The set's iterator will be set to NULL if there
595 * are no more elements in the set. The caller must ensure that the set's iterator is
598 * @param set set that should send its next element to its client
601 send_client_element (struct Set *set)
604 struct ElementEntry *ee;
605 struct GNUNET_MQ_Envelope *ev;
607 GNUNET_assert (NULL != set->iter);
608 ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, NULL, (const void **) &ee);
609 if (GNUNET_NO == ret)
611 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
612 GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
617 struct GNUNET_SET_IterResponseMessage *msg;
619 GNUNET_assert (NULL != ee);
620 ev = GNUNET_MQ_msg_extra (msg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
621 memcpy (&msg[1], ee->element.data, ee->element.size);
622 msg->element_type = ee->element.type;
624 GNUNET_MQ_send (set->client_mq, ev);
629 * Called when a client wants to iterate the elements of a set.
632 * @param client client that sent the message
633 * @param m message sent by the client
636 handle_client_iterate (void *cls,
637 struct GNUNET_SERVER_Client *client,
638 const struct GNUNET_MessageHeader *m)
642 // iterate over a non existing set
643 set = set_get (client);
647 GNUNET_SERVER_client_disconnect (client);
651 // only one concurrent iterate-action per set
652 if (NULL != set->iter)
655 GNUNET_SERVER_client_disconnect (client);
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
659 GNUNET_CONTAINER_multihashmap_size (set->elements));
660 GNUNET_SERVER_receive_done (client, GNUNET_OK);
661 set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
662 send_client_element (set);
667 * Called when a client wants to create a new set.
670 * @param client client that sent the message
671 * @param m message sent by the client
674 handle_client_create_set (void *cls,
675 struct GNUNET_SERVER_Client *client,
676 const struct GNUNET_MessageHeader *m)
678 struct GNUNET_SET_CreateMessage *msg = (struct GNUNET_SET_CreateMessage *) m;
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client created new set (operation %u)\n",
682 ntohs (msg->operation));
684 // max. one set per client!
685 if (NULL != set_get (client))
688 GNUNET_SERVER_client_disconnect (client);
692 set = GNUNET_new (struct Set);
694 switch (ntohs (msg->operation))
696 case GNUNET_SET_OPERATION_INTERSECTION:
697 // FIXME: implement intersection vt
698 // set->vt = _GSS_intersection_vt ();
700 case GNUNET_SET_OPERATION_UNION:
701 set->vt = _GSS_union_vt ();
706 GNUNET_SERVER_client_disconnect (client);
710 set->state = set->vt->create ();
711 set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
712 set->client = client;
713 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
714 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
715 GNUNET_SERVER_receive_done (client, GNUNET_OK);
720 * Called when a client wants to create a new listener.
723 * @param client client that sent the message
724 * @param m message sent by the client
727 handle_client_listen (void *cls,
728 struct GNUNET_SERVER_Client *client,
729 const struct GNUNET_MessageHeader *m)
731 struct GNUNET_SET_ListenMessage *msg = (struct GNUNET_SET_ListenMessage *) m;
732 struct Listener *listener;
733 struct Operation *op;
735 // max. one per client!
736 if (NULL != listener_get (client))
739 GNUNET_SERVER_client_disconnect (client);
743 listener = GNUNET_new (struct Listener);
744 listener->client = client;
745 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
746 listener->app_id = msg->app_id;
747 listener->operation = ntohl (msg->operation);
748 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new listener created (op %u, app %s)\n",
750 listener->operation, GNUNET_h2s (&listener->app_id));
752 /* check for incoming requests the listener is interested in */
753 for (op = incoming_head; NULL != op; op = op->next)
755 if (NULL == op->spec)
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request has no spec yet\n");
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considering (op: %u, app: %s, suggest: %u)\n",
761 op->spec->operation, GNUNET_h2s (&op->spec->app_id), op->state->suggest_id);
763 /* don't consider the incoming request if it has been already suggested to a listener */
764 if (0 != op->state->suggest_id)
766 if (listener->operation != op->spec->operation)
768 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request suggested\n");
771 incoming_suggest (op, listener);
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considered all incoming requests\n");
774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
779 * Called when the listening client rejects an operation
780 * request by another peer.
783 * @param client client that sent the message
784 * @param m message sent by the client
787 handle_client_reject (void *cls,
788 struct GNUNET_SERVER_Client *client,
789 const struct GNUNET_MessageHeader *m)
791 struct Operation *incoming;
792 const struct GNUNET_SET_AcceptRejectMessage *msg;
794 msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
795 GNUNET_break (0 == ntohl (msg->request_id));
797 // no matching incoming operation for this reject
798 incoming = get_incoming (ntohl (msg->accept_reject_id));
799 if (NULL == incoming)
801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer request rejected by client\n");
806 GNUNET_MESH_tunnel_destroy (incoming->tunnel);
807 //tunnel destruction handler called immediately upon destruction
808 GNUNET_SERVER_receive_done (client, GNUNET_OK);
813 * Called when a client wants to add/remove an element to/from a
817 * @param client client that sent the message
818 * @param m message sent by the client
821 handle_client_add_remove (void *cls,
822 struct GNUNET_SERVER_Client *client,
823 const struct GNUNET_MessageHeader *m)
826 const struct GNUNET_SET_ElementMessage *msg;
827 struct GNUNET_SET_Element el;
828 struct ElementEntry *ee;
830 // client without a set requested an operation
831 set = set_get (client);
835 GNUNET_SERVER_client_disconnect (client);
838 GNUNET_SERVER_receive_done (client, GNUNET_OK);
839 msg = (const struct GNUNET_SET_ElementMessage *) m;
840 el.size = ntohs (m->size) - sizeof *msg;
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "client ins/rem element of size %u\n", el.size);
844 if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
846 struct GNUNET_HashCode hash;
848 GNUNET_CRYPTO_hash (el.data, el.size, &hash);
849 ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove non-existing element\n");
855 if (GNUNET_YES == ee->removed)
857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove element twice\n");
860 ee->removed = GNUNET_YES;
861 ee->generation_removed = set->current_generation;
862 set->vt->remove (set->state, ee);
866 struct ElementEntry *ee_dup;
868 ee = GNUNET_malloc (el.size + sizeof *ee);
869 ee->element.size = el.size;
870 memcpy (&ee[1], el.data, el.size);
871 ee->element.data = &ee[1];
872 ee->generation_added = set->current_generation;
873 ee->remote = GNUNET_NO;
874 GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
875 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "element inserted twice, ignoring\n");
883 GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
884 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
885 set->vt->add (set->state, ee);
891 * Called when a client wants to evaluate a set operation with another peer.
894 * @param client client that sent the message
895 * @param m message sent by the client
898 handle_client_evaluate (void *cls,
899 struct GNUNET_SERVER_Client *client,
900 const struct GNUNET_MessageHeader *m)
903 struct GNUNET_SET_EvaluateMessage *msg;
904 struct OperationSpecification *spec;
905 struct Operation *op;
907 set = set_get (client);
911 GNUNET_SERVER_client_disconnect (client);
915 msg = (struct GNUNET_SET_EvaluateMessage *) m;
916 spec = GNUNET_new (struct OperationSpecification);
917 spec->operation = set->operation;
918 spec->app_id = msg->app_id;
919 spec->salt = ntohl (msg->salt);
920 spec->peer = msg->target_peer;
922 spec->result_mode = ntohs (msg->result_mode);
923 spec->client_request_id = ntohl (msg->request_id);
924 spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
926 // for simplicity we just backup the context msg instead of rebuilding it later on
927 if (NULL != spec->context_msg)
928 spec->context_msg = GNUNET_copy_message (spec->context_msg);
930 op = GNUNET_new (struct Operation);
932 op->generation_created = set->current_generation++;
934 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
936 op->tunnel = GNUNET_MESH_tunnel_create (mesh, op, &msg->target_peer,
937 GNUNET_APPLICATION_TYPE_SET,
941 op->mq = GNUNET_MESH_mq_create (op->tunnel);
943 set->vt->evaluate (op);
944 GNUNET_SERVER_receive_done (client, GNUNET_OK);
949 * Handle an ack from a client, and send the next element.
952 * @param client the client
953 * @param m the message
956 handle_client_iter_ack (void *cls,
957 struct GNUNET_SERVER_Client *client,
958 const struct GNUNET_MessageHeader *m)
962 // client without a set requested an operation
963 set = set_get (client);
967 GNUNET_SERVER_client_disconnect (client);
971 // client sent an ack, but we were not expecting one
972 if (NULL == set->iter)
975 GNUNET_SERVER_client_disconnect (client);
979 GNUNET_SERVER_receive_done (client, GNUNET_OK);
980 send_client_element (set);
985 * Handle a request from the client to
986 * cancel a running set operation.
989 * @param client the client
990 * @param mh the message
993 handle_client_cancel (void *cls,
994 struct GNUNET_SERVER_Client *client,
995 const struct GNUNET_MessageHeader *mh)
997 const struct GNUNET_SET_CancelMessage *msg =
998 (const struct GNUNET_SET_CancelMessage *) mh;
1000 struct Operation *op;
1003 // client without a set requested an operation
1004 set = set_get (client);
1008 GNUNET_SERVER_client_disconnect (client);
1012 for (op = set->ops_head; NULL != op; op = op->next)
1014 if (op->spec->client_request_id == msg->request_id)
1021 if (GNUNET_NO == found)
1024 GNUNET_SERVER_client_disconnect (client);
1028 _GSS_operation_destroy (op);
1033 * Handle a request from the client to accept
1034 * a set operation that came from a remote peer.
1035 * We forward the accept to the associated operation for handling
1038 * @param client the client
1039 * @param mh the message
1042 handle_client_accept (void *cls,
1043 struct GNUNET_SERVER_Client *client,
1044 const struct GNUNET_MessageHeader *mh)
1047 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) mh;
1048 struct Operation *op;
1050 op = get_incoming (ntohl (msg->accept_reject_id));
1052 // incoming operation does not exist
1056 GNUNET_SERVER_client_disconnect (client);
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client accepting %u\n", ntohl (msg->accept_reject_id));
1062 GNUNET_assert (GNUNET_YES == op->is_incoming);
1064 // client without a set requested an operation
1065 set = set_get (client);
1070 GNUNET_SERVER_client_disconnect (client);
1074 op->spec->set = set;
1076 incoming_retire (op);
1078 GNUNET_assert (NULL != op->spec->set);
1079 GNUNET_assert (NULL != op->spec->set->vt);
1081 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
1083 op->spec->client_request_id = ntohl (msg->request_id);
1084 op->spec->result_mode = ntohs (msg->result_mode);
1085 op->generation_created = set->current_generation++;
1086 op->vt = op->spec->set->vt;
1087 GNUNET_assert (NULL != op->vt->accept);
1088 set->vt->accept (op);
1089 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1094 * Called to clean up, after a shutdown has been requested.
1096 * @param cls closure
1097 * @param tc context information (why was this task triggered now)
1100 shutdown_task (void *cls,
1101 const struct GNUNET_SCHEDULER_TaskContext *tc)
1103 while (NULL != incoming_head)
1104 incoming_destroy (incoming_head);
1106 while (NULL != listeners_head)
1107 listener_destroy (listeners_head);
1109 while (NULL != sets_head)
1110 set_destroy (sets_head);
1112 /* it's important to destroy mesh at the end, as all tunnels
1113 * must be destroyed before the mesh handle! */
1116 GNUNET_MESH_disconnect (mesh);
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled shutdown request\n");
1125 * Timeout happens iff:
1126 * - we suggested an operation to our listener,
1127 * but did not receive a response in time
1128 * - we got the tunnel from a peer but no GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
1129 * - shutdown (obviously)
1130 * @param cls tunnel context
1131 * @param tc context information (why was this task triggered now)
1134 incoming_timeout_cb (void *cls,
1135 const struct GNUNET_SCHEDULER_TaskContext *tc)
1137 struct Operation *incoming = cls;
1139 GNUNET_assert (GNUNET_YES == incoming->is_incoming);
1141 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "remote peer timed out\n");
1145 incoming_destroy (incoming);
1150 * Terminates an incoming operation in case we have not yet received an
1151 * operation request. Called by the tunnel destruction handler.
1153 * @param op the tunnel context
1156 handle_incoming_disconnect (struct Operation *op)
1158 GNUNET_assert (GNUNET_YES == op->is_incoming);
1160 if (NULL == op->tunnel)
1163 incoming_destroy (op);
1168 * Method called whenever another peer has added us to a tunnel
1169 * the other peer initiated.
1170 * Only called (once) upon reception of data with a message type which was
1171 * subscribed to in GNUNET_MESH_connect.
1173 * The tunnel context represents the operation itself and gets added to a DLL,
1174 * from where it gets looked up when our local listener client responds
1175 * to a proposed/suggested operation or connects and associates with this operation.
1177 * @param cls closure
1178 * @param tunnel new handle to the tunnel
1179 * @param initiator peer that started the tunnel
1180 * @param port Port this tunnel is for.
1181 * @return initial tunnel context for the tunnel
1182 * (can be NULL -- that's not an error)
1185 tunnel_new_cb (void *cls,
1186 struct GNUNET_MESH_Tunnel *tunnel,
1187 const struct GNUNET_PeerIdentity *initiator,
1190 struct Operation *incoming;
1191 static const struct SetVT incoming_vt = {
1192 .msg_handler = handle_incoming_msg,
1193 .peer_disconnect = handle_incoming_disconnect
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new incoming tunnel\n");
1198 GNUNET_assert (port == GNUNET_APPLICATION_TYPE_SET);
1199 incoming = GNUNET_new (struct Operation);
1200 incoming->is_incoming = GNUNET_YES;
1201 incoming->state = GNUNET_new (struct OperationState);
1202 incoming->state->peer = *initiator;
1203 incoming->tunnel = tunnel;
1204 incoming->mq = GNUNET_MESH_mq_create (incoming->tunnel);
1205 incoming->vt = &incoming_vt;
1206 incoming->state->timeout_task =
1207 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
1208 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
1215 * Function called whenever a tunnel is destroyed. Should clean up
1216 * any associated state.
1217 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
1220 * The peer_disconnect function is part of a a virtual table set initially either
1221 * when a peer creates a new tunnel with us (tunnel_new_cb), or once we create
1222 * a new tunnel ourselves (evaluate).
1224 * Once we know the exact type of operation (union/intersection), the vt is
1225 * replaced with an operation specific instance (_GSS_[op]_vt).
1227 * @param cls closure (set from GNUNET_MESH_connect)
1228 * @param tunnel connection to the other end (henceforth invalid)
1229 * @param tunnel_ctx place where local state associated
1230 * with the tunnel is stored
1233 tunnel_end_cb (void *cls,
1234 const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
1236 struct Operation *op = tunnel_ctx;
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel end cb called\n");
1243 op->vt->peer_disconnect (op);
1244 /* mesh will never call us with the context again! */
1245 GNUNET_free (tunnel_ctx);
1250 * Functions with this signature are called whenever any message is
1251 * received via the mesh tunnel.
1253 * The msg_handler is a virtual table set in initially either when a peer
1254 * creates a new tunnel with us (tunnel_new_cb), or once we create a new tunnel
1255 * ourselves (evaluate).
1257 * Once we know the exact type of operation (union/intersection), the vt is
1258 * replaced with an operation specific instance (_GSS_[op]_vt).
1260 * @param cls Closure (set from GNUNET_MESH_connect).
1261 * @param tunnel Connection to the other end.
1262 * @param tunnel_ctx Place to store local state associated with the tunnel.
1263 * @param message The actual message.
1265 * @return GNUNET_OK to keep the tunnel open,
1266 * GNUNET_SYSERR to close it (signal serious error).
1269 dispatch_p2p_message (void *cls,
1270 struct GNUNET_MESH_Tunnel *tunnel,
1272 const struct GNUNET_MessageHeader *message)
1274 struct Operation *op = *tunnel_ctx;
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message (type: %u)\n",
1278 ntohs (message->type));
1279 /* do this before the handler, as the handler might kill the tunnel */
1280 GNUNET_MESH_receive_done (tunnel);
1281 ret = op->vt->msg_handler (op, message);
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled mesh message (type: %u)\n",
1283 ntohs (message->type));
1289 * Function called by the service's run
1290 * method to run service-specific setup code.
1292 * @param cls closure
1293 * @param server the initialized server
1294 * @param cfg configuration to use
1297 run (void *cls, struct GNUNET_SERVER_Handle *server,
1298 const struct GNUNET_CONFIGURATION_Handle *cfg)
1300 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1301 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
1302 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1303 {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
1304 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
1305 {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
1306 sizeof (struct GNUNET_SET_CreateMessage)},
1307 {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
1308 sizeof (struct GNUNET_MessageHeader)},
1309 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
1310 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN,
1311 sizeof (struct GNUNET_SET_ListenMessage)},
1312 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
1313 sizeof (struct GNUNET_SET_AcceptRejectMessage)},
1314 {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
1315 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
1316 sizeof (struct GNUNET_SET_CancelMessage)},
1319 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1320 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
1321 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
1322 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
1323 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
1324 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
1325 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
1326 {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
1329 static const uint32_t mesh_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
1331 configuration = cfg;
1332 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1333 &shutdown_task, NULL);
1334 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1335 GNUNET_SERVER_add_handlers (server, server_handlers);
1337 mesh = GNUNET_MESH_connect (cfg, NULL, tunnel_new_cb, tunnel_end_cb,
1338 mesh_handlers, mesh_ports);
1341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not connect to mesh\n");
1345 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "started\n");
1350 * The main function for the set service.
1352 * @param argc number of arguments from the command line
1353 * @param argv command line arguments
1354 * @return 0 ok, 1 on error
1357 main (int argc, char *const *argv)
1360 ret = GNUNET_SERVICE_run (argc, argv, "set",
1361 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
1362 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit (%d)\n", GNUNET_OK != ret);
1363 return (GNUNET_OK == ret) ? 0 : 1;